Partially implemented VPN_connect

This commit is contained in:
Pierre Pronchery 2010-09-05 17:01:23 +00:00
parent 79bc84fa08
commit 4f0b854fe3
8 changed files with 158 additions and 9 deletions

View File

@ -1,6 +1,6 @@
PACKAGE = VPN
VERSION = 0.0.0
SUBDIRS = data src
SUBDIRS = data include src
RM = rm -f
LN = ln -f
TAR = tar -czvf
@ -24,12 +24,16 @@ dist:
$(PACKAGE)-$(VERSION)/data/Makefile \
$(PACKAGE)-$(VERSION)/data/VPN.interface \
$(PACKAGE)-$(VERSION)/data/project.conf \
$(PACKAGE)-$(VERSION)/include/VPN.h \
$(PACKAGE)-$(VERSION)/include/Makefile \
$(PACKAGE)-$(VERSION)/include/project.conf \
$(PACKAGE)-$(VERSION)/src/vpn.c \
$(PACKAGE)-$(VERSION)/src/main.c \
$(PACKAGE)-$(VERSION)/src/Makefile \
$(PACKAGE)-$(VERSION)/src/appbroker.sh \
$(PACKAGE)-$(VERSION)/src/vpn.h \
$(PACKAGE)-$(VERSION)/src/project.conf \
$(PACKAGE)-$(VERSION)/COPYING \
$(PACKAGE)-$(VERSION)/Makefile \
$(PACKAGE)-$(VERSION)/config.h \
$(PACKAGE)-$(VERSION)/project.conf

23
include/Makefile Normal file
View File

@ -0,0 +1,23 @@
PREFIX = /usr/local
DESTDIR =
RM = rm -f
LN = ln -f
MKDIR = mkdir -p
INSTALL = install
INCLUDEDIR= $(PREFIX)/include
all:
clean:
distclean: clean
install: all
$(MKDIR) $(DESTDIR)$(INCLUDEDIR)
$(INSTALL) -m 0644 -- VPN.h $(DESTDIR)$(INCLUDEDIR)/VPN.h
uninstall:
$(RM) -- $(DESTDIR)$(INCLUDEDIR)/VPN.h
.PHONY: all clean distclean install uninstall

26
include/VPN.h Normal file
View File

@ -0,0 +1,26 @@
/* $Id$ */
/* Copyright (c) 2010 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS System VPN */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef VPN_VPN_H
# define VPN_VPN_H
/* VPN */
/* types */
typedef enum _VPNProtocol { VPN_PROTOCOL_IP_TCP = 0 } VPNProtocol;
#endif /* !VPN_VPN_H */

2
include/project.conf Normal file
View File

@ -0,0 +1,2 @@
includes=VPN.h
dist=Makefile

View File

@ -2,5 +2,5 @@ package=VPN
version=0.0.0
config=h
subdirs=data,src
subdirs=data,include,src
dist=COPYING,Makefile,config.h

View File

@ -3,7 +3,7 @@ PREFIX = /usr/local
DESTDIR =
BINDIR = $(PREFIX)/bin
CC = cc
CPPFLAGSF=
CPPFLAGSF= -I ../include
CPPFLAGS= -I $(PREFIX)/include
CFLAGSF = -W
CFLAGS = -Wall -g -O2 -pedantic

View File

@ -1,4 +1,5 @@
targets=../data/VPN.h,VPN
cppflags_force=-I ../include
cppflags=-I $(PREFIX)/include
cflags_force=-W
cflags=-Wall -g -O2 -pedantic

105
src/vpn.c
View File

@ -20,6 +20,9 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "VPN.h"
#include "../data/VPN.h"
#include "../config.h"
@ -45,7 +48,11 @@ static size_t _clients_cnt;
/* accessors */
static VPNClient * _client_get(void);
static VPNClient * _client_check(VPNClient * client, int32_t fd);
static int _client_remove_socket(VPNClient * client, int32_t fd);
/* useful */
static VPNClient * _client_add(VPNClient * client);
static VPNClient * _client_add_socket(VPNClient * client, int32_t fd);
static VPNClient * _client_remove_socket(VPNClient * client, int32_t fd);
/* public */
@ -81,6 +88,42 @@ int32_t VPN_close(int32_t fd)
}
/* VPN_connect */
int32_t VPN_connect(uint32_t protocol, String const * uri)
{
int32_t ret;
VPNProtocol vprotocol = protocol;
int sdomain;
int stype;
int sprotocol = 0;
struct sockaddr * sockaddr;
socklen_t ssize;
struct sockaddr_in sa_in;
switch(vprotocol)
{
case VPN_PROTOCOL_IP_TCP:
sdomain = PF_INET;
stype = SOCK_STREAM;
/* FIXME initialize sa_in */
sockaddr = (struct sockaddr*)&sa_in;
ssize = sizeof(sa_in);
break;
default:
return -1;
}
if((ret = socket(sdomain, stype, sprotocol)) == -1)
return -1;
if(connect(ret, sockaddr, ssize) != 0
|| _client_add_socket(NULL, ret) == NULL)
{
close(ret); /* XXX necessary when connect() failed? */
return -1;
}
return ret;
}
/* private */
/* functions */
/* accessors */
@ -119,25 +162,75 @@ static VPNClient * _client_check(VPNClient * client, int32_t fd)
}
/* useful */
/* client_add */
static VPNClient * _client_add(VPNClient * client)
{
void * id;
VPNClient * p;
if(client == NULL && (client = _client_get()) != NULL)
return client;
if((id = appserver_get_client_id(_appserver)) == NULL)
{
error_print(PACKAGE);
return NULL;
}
if((p = realloc(_clients, sizeof(*p) * (_clients_cnt + 1))) == NULL)
{
error_set_print(PACKAGE, 1, "%s", strerror(errno));
return NULL;
}
_clients = p;
p = &_clients[_clients_cnt++];
p->id = id;
p->sockets = NULL;
p->sockets_cnt = 0;
return p;
}
/* client_add_socket */
static VPNClient * _client_add_socket(VPNClient * client, int32_t fd)
{
int32_t * p;
if((client = _client_add(client)) == NULL)
return NULL;
if((p = realloc(client->sockets, sizeof(*p)
* (client->sockets_cnt + 1))) == NULL)
{
error_set_print(PACKAGE, 1, "%s", strerror(errno));
return NULL;
}
client->sockets = p;
client->sockets[client->sockets_cnt++] = fd;
return client;
}
/* client_remove_socket */
static int _client_remove_socket(VPNClient * client, int32_t fd)
static VPNClient * _client_remove_socket(VPNClient * client, int32_t fd)
{
size_t i;
int32_t * p;
if(fd < 0) /* XXX should never happen */
return error_set_print(PACKAGE, 1, "%s", strerror(EINVAL));
{
error_set_print(PACKAGE, 1, "%s", strerror(EINVAL));
return NULL;
}
if(client == NULL && (client = _client_get()) == NULL)
return 1;
return NULL;
for(i = 0; i < client->sockets_cnt; i++)
if(client->sockets[i] == fd)
break;
if(i == client->sockets_cnt)
return 0;
return client;
p = &client->sockets[i];
memmove(p, p + 1, (--client->sockets_cnt - i) * sizeof(*p));
if((p = realloc(client->sockets, sizeof(*p) * client->sockets_cnt))
!= NULL || client->sockets_cnt == 0)
client->sockets = p; /* we can ignore errors */
return 0;
return client;
}