From 4f0b854fe391fac2ce7a96779c3c0fbf6f75b21d Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sun, 5 Sep 2010 17:01:23 +0000 Subject: [PATCH] Partially implemented VPN_connect --- Makefile | 6 ++- include/Makefile | 23 ++++++++++ include/VPN.h | 26 +++++++++++ include/project.conf | 2 + project.conf | 2 +- src/Makefile | 2 +- src/project.conf | 1 + src/vpn.c | 105 ++++++++++++++++++++++++++++++++++++++++--- 8 files changed, 158 insertions(+), 9 deletions(-) create mode 100644 include/Makefile create mode 100644 include/VPN.h create mode 100644 include/project.conf diff --git a/Makefile b/Makefile index aff3dc3..d461f17 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/include/Makefile b/include/Makefile new file mode 100644 index 0000000..1cfd321 --- /dev/null +++ b/include/Makefile @@ -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 diff --git a/include/VPN.h b/include/VPN.h new file mode 100644 index 0000000..2253044 --- /dev/null +++ b/include/VPN.h @@ -0,0 +1,26 @@ +/* $Id$ */ +/* Copyright (c) 2010 Pierre Pronchery */ +/* 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 . */ + + + +#ifndef VPN_VPN_H +# define VPN_VPN_H + + +/* VPN */ +/* types */ +typedef enum _VPNProtocol { VPN_PROTOCOL_IP_TCP = 0 } VPNProtocol; + +#endif /* !VPN_VPN_H */ diff --git a/include/project.conf b/include/project.conf new file mode 100644 index 0000000..acbd8a7 --- /dev/null +++ b/include/project.conf @@ -0,0 +1,2 @@ +includes=VPN.h +dist=Makefile diff --git a/project.conf b/project.conf index 87a8a6d..1a99dcf 100644 --- a/project.conf +++ b/project.conf @@ -2,5 +2,5 @@ package=VPN version=0.0.0 config=h -subdirs=data,src +subdirs=data,include,src dist=COPYING,Makefile,config.h diff --git a/src/Makefile b/src/Makefile index 73a70a3..1eb1229 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 diff --git a/src/project.conf b/src/project.conf index 20da0b0..3df8082 100644 --- a/src/project.conf +++ b/src/project.conf @@ -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 diff --git a/src/vpn.c b/src/vpn.c index 9dc6254..9d86d30 100644 --- a/src/vpn.c +++ b/src/vpn.c @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#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; }