diff options
author | Christian Pointner <equinox@anytun.org> | 2014-06-29 18:20:30 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anytun.org> | 2014-06-29 18:20:30 +0000 |
commit | 6412ec923d0f07b68bc2b5c474fcb98f5886a449 (patch) | |
tree | 86b6cdf963a7c6518612f866d02521c1ee0196ee | |
parent | added options parser support for keyexchange sockets (diff) |
creating and closing unix domain sockets works now
-rw-r--r-- | src/Makefile | 1 | ||||
-rw-r--r-- | src/uanytun.c | 9 | ||||
-rw-r--r-- | src/unixdomain.c | 165 | ||||
-rw-r--r-- | src/unixdomain.h | 71 |
4 files changed, 246 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile index feccbaa..1a639ac 100644 --- a/src/Makefile +++ b/src/Makefile @@ -65,6 +65,7 @@ OBJ := log.o \ encrypted_packet.o \ seq_window.o \ cipher.o \ + unixdomain.o \ uanytun.o diff --git a/src/uanytun.c b/src/uanytun.c index a2fc73b..256f583 100644 --- a/src/uanytun.c +++ b/src/uanytun.c @@ -77,6 +77,8 @@ typedef u_int8_t auth_algo_t; #include "daemon.h" #include "sysexec.h" +#include "unixdomain.h" + int init_main_loop(options_t* opt, cipher_t* c, auth_algo_t* aa, key_derivation_t* kd, seq_win_t* seq_win) { @@ -232,6 +234,11 @@ int main_loop(tun_device_t* dev, udp_t* sock, options_t* opt) if(ret) return ret; + + unixdomain_t kx_data; + unixdomain_init(&kx_data, opt->kx_data_interface_); + + FD_ZERO(&readfds); FD_SET(dev->fd_, &readfds); int nfds = udp_fill_fd_set(sock, &readfds); @@ -287,6 +294,8 @@ int main_loop(tun_device_t* dev, udp_t* sock, options_t* opt) } } + unixdomain_close(&kx_data); + cipher_close(&c); #ifndef NO_CRYPT auth_algo_close(&aa); diff --git a/src/unixdomain.c b/src/unixdomain.c new file mode 100644 index 0000000..ceceed0 --- /dev/null +++ b/src/unixdomain.c @@ -0,0 +1,165 @@ +/* + * uAnytun + * + * uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full + * featured implementation uAnytun has no support for multiple connections + * or synchronisation. It is a small single threaded implementation intended + * to act as a client on small platforms. + * The secure anycast tunneling protocol (satp) defines a protocol used + * for communication between any combination of unicast and anycast + * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel + * mode and allows tunneling of every ETHER TYPE protocol (e.g. + * ethernet, ip, arp ...). satp directly includes cryptography and + * message authentication based on the methods used by SRTP. It is + * intended to deliver a generic, scaleable and secure solution for + * tunneling and relaying of packets of any protocol. + * + * + * Copyright (C) 2007-2014 Christian Pointner <equinox@anytun.org> + * + * This file is part of uAnytun. + * + * uAnytun 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, either version 3 of the License, or + * any later version. + * + * uAnytun 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 uAnytun. If not, see <http://www.gnu.org/licenses/>. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations + * including the two. + * You must obey the GNU General Public License in all respects + * for all of the code used other than OpenSSL. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you + * do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source + * files in the program, then also delete it here. + */ + +#define _GNU_SOURCE +#include "datatypes.h" + +#include "unixdomain.h" + +#include "log.h" + +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> + + +int unixdomain_init(unixdomain_t* sock, const char* path) +{ + if(!sock) + return -1; + + sock->client_fd_ = -1; + sock->server_fd_ = -1; + memset(&(sock->server_addr_), 0, sizeof(sock->server_addr_)); + sock->server_addr_.sun_family = AF_UNIX; + + if(!path) + return 0; + + sock->server_fd_ = socket(AF_UNIX, SOCK_STREAM, 0); + if(sock->server_fd_ < 0) { + log_printf(ERROR, "Error on opening unix domain socket: %s", strerror(errno)); + return -1; + } + + strncpy(sock->server_addr_.sun_path, path, sizeof(sock->server_addr_.sun_path)-1); + + unlink(sock->server_addr_.sun_path); // remove stale socket + // TODO: error handling + + bind(sock->server_fd_, (struct sockaddr*)&(sock->server_addr_), sizeof(sock->server_addr_)); + // TODO: error handling + + listen(sock->server_fd_, 1); + // TODO: error handling + + log_printf(NOTICE, "unixdomain socket listening on: %s", sock->server_addr_.sun_path); + + return 0; +} + +int unixdomain_fill_fd_set(unixdomain_t* sock, fd_set* set) +{ + int max_fd = 0; + + if(sock->server_fd_ >= 0) { + FD_SET(sock->server_fd_, set); + max_fd = sock->server_fd_ > max_fd ? sock->server_fd_ : max_fd; + } + + if(sock->client_fd_ >= 0) { + FD_SET(sock->client_fd_, set); + max_fd = sock->client_fd_ > max_fd ? sock->client_fd_ : max_fd; + } + + return max_fd; +} + +void unixdomain_close(unixdomain_t* sock) +{ + if(!sock) + return; + + if(sock->client_fd_ >= 0) + close(sock->client_fd_); + if(sock->server_fd_ >= 0) { + close(sock->server_fd_); + unlink(sock->server_addr_.sun_path); + // TODO: error handling? + } +} + +int unixdomain_accept(unixdomain_t* sock) +{ + if(!sock) + return -1; + + int new_client = accept(sock->server_fd_, NULL, NULL); + if(new_client < 0) { +// TODO: error HANDLING + return -1; + } + + if(sock->client_fd_ < 0) + sock->client_fd_ = new_client; + else + close(new_client); + + return 0; +} + +int unixdomain_read(unixdomain_t* sock, u_int8_t* buf, u_int32_t len) +{ + if(!sock || !buf || sock->client_fd_ < 0) + return -1; + + return recv(sock->client_fd_, buf, len, 0); +} + + +int unixdomain_write(unixdomain_t* sock, u_int8_t* buf, u_int32_t len) +{ + if(!sock || !buf || sock->client_fd_ < 0) + return 0; + + return send(sock->client_fd_, buf, len, 0); +} diff --git a/src/unixdomain.h b/src/unixdomain.h new file mode 100644 index 0000000..818e2ce --- /dev/null +++ b/src/unixdomain.h @@ -0,0 +1,71 @@ +/* + * uAnytun + * + * uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full + * featured implementation uAnytun has no support for multiple connections + * or synchronisation. It is a small single threaded implementation intended + * to act as a client on small platforms. + * The secure anycast tunneling protocol (satp) defines a protocol used + * for communication between any combination of unicast and anycast + * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel + * mode and allows tunneling of every ETHER TYPE protocol (e.g. + * ethernet, ip, arp ...). satp directly includes cryptography and + * message authentication based on the methods used by SRTP. It is + * intended to deliver a generic, scaleable and secure solution for + * tunneling and relaying of packets of any protocol. + * + * + * Copyright (C) 2007-2014 Christian Pointner <equinox@anytun.org> + * + * This file is part of uAnytun. + * + * uAnytun 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, either version 3 of the License, or + * any later version. + * + * uAnytun 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 uAnytun. If not, see <http://www.gnu.org/licenses/>. + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations + * including the two. + * You must obey the GNU General Public License in all respects + * for all of the code used other than OpenSSL. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you + * do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source + * files in the program, then also delete it here. + */ + +#ifndef UANYTUN_unixdomain_h_INCLUDED +#define UANYTUN_unixdomain_h_INCLUDED + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> + +struct unixdomain_struct { + struct sockaddr_un server_addr_; + int server_fd_; + int client_fd_; +}; +typedef struct unixdomain_struct unixdomain_t; + +int unixdomain_init(unixdomain_t* sock, const char* path); +int unixdomain_fill_fd_set(unixdomain_t* sock, fd_set* set); +void unixdomain_close(unixdomain_t* sock); + +int unixdomain_accept(unixdomain_t* sock); +int unixdomain_read(unixdomain_t* sock, u_int8_t* buf, u_int32_t len); +int unixdomain_write(unixdomain_t* sock, u_int8_t* buf, u_int32_t len); + +#endif |