summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/uanytun.c16
-rw-r--r--src/udp.c63
-rw-r--r--src/udp.h22
3 files changed, 59 insertions, 42 deletions
diff --git a/src/uanytun.c b/src/uanytun.c
index 322e722..81d3724 100644
--- a/src/uanytun.c
+++ b/src/uanytun.c
@@ -103,7 +103,7 @@ int init_main_loop(options_t* opt, cipher_t* c, auth_algo_t* aa, key_derivation_
return 0;
}
-int process_tun_data(tun_device_t* dev, udp_socket_t* sock, options_t* opt, plain_packet_t* plain_packet, encrypted_packet_t* encrypted_packet,
+int process_tun_data(tun_device_t* dev, udp_t* sock, options_t* opt, plain_packet_t* plain_packet, encrypted_packet_t* encrypted_packet,
cipher_t* c, auth_algo_t* aa, key_derivation_t* kd, seq_nr_t seq_nr)
{
plain_packet_set_payload_length(plain_packet, -1);
@@ -132,7 +132,7 @@ int process_tun_data(tun_device_t* dev, udp_socket_t* sock, options_t* opt, plai
#ifndef NO_CRYPT
auth_algo_generate(aa, kd, kd_outbound, encrypted_packet);
#endif
-
+
len = udp_write(sock, encrypted_packet_get_packet(encrypted_packet), encrypted_packet_get_length(encrypted_packet));
if(len == -1)
log_printf(ERROR, "error on sending udp packet: %s", strerror(errno));
@@ -140,7 +140,7 @@ int process_tun_data(tun_device_t* dev, udp_socket_t* sock, options_t* opt, plai
return 0;
}
-int process_sock_data(tun_device_t* dev, udp_socket_t* sock, options_t* opt, plain_packet_t* plain_packet, encrypted_packet_t* encrypted_packet,
+int process_sock_data(tun_device_t* dev, udp_t* sock, options_t* opt, plain_packet_t* plain_packet, encrypted_packet_t* encrypted_packet,
cipher_t* c, auth_algo_t* aa, key_derivation_t* kd, seq_win_t* seq_win)
{
plain_packet_set_payload_length(plain_packet, -1);
@@ -206,7 +206,7 @@ int process_sock_data(tun_device_t* dev, udp_socket_t* sock, options_t* opt, pla
}
-int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt)
+int main_loop(tun_device_t* dev, udp_t* sock, options_t* opt)
{
log_printf(INFO, "entering main loop");
@@ -228,8 +228,8 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt)
FD_ZERO(&readfds);
FD_SET(dev->fd_, &readfds);
- FD_SET(sock->fd_, &readfds);
- int nfds = dev->fd_ > sock->fd_ ? dev->fd_ : sock->fd_;
+ FD_SET(sock->socks_->fd_, &readfds);
+ int nfds = dev->fd_ > sock->socks_->fd_ ? dev->fd_ : sock->socks_->fd_;
int return_value = 0;
int sig_fd = signal_init();
@@ -264,7 +264,7 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt)
break;
}
- if(FD_ISSET(sock->fd_, &readyfds)) {
+ if(FD_ISSET(sock->socks_->fd_, &readyfds)) {
return_value = process_sock_data(dev, sock, opt, &plain_packet, &encrypted_packet, &c, &aa, &kd, &seq_win);
if(return_value)
break;
@@ -370,7 +370,7 @@ int main(int argc, char* argv[])
}
- udp_socket_t sock;
+ udp_t sock;
ret = udp_init(&sock, opt.local_addr_, opt.local_port_, opt.resolv_addr_type_);
if(ret) {
log_printf(ERROR, "error on udp_init, exitting");
diff --git a/src/udp.c b/src/udp.c
index 7f539a8..5e436dc 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -49,14 +49,22 @@
#include <arpa/inet.h>
#include <netinet/in.h>
-int udp_init(udp_socket_t* sock, const char* local_addr, const char* port, resolv_addr_type_t resolv_type)
+int udp_init(udp_t* sock, const char* local_addr, const char* port, resolv_addr_type_t resolv_type)
{
if(!sock || !port)
return -1;
-
- sock->fd_ = 0;
- memset(&(sock->local_end_), 0, sizeof(sock->local_end_));
- memset(&(sock->remote_end_), 0, sizeof(sock->local_end_));
+
+ sock->socks_ = malloc(sizeof(udp_socket_t));
+ if(!sock->socks_) {
+ log_printf(ERROR, "memory error at udp_init");
+ return -2;
+ }
+
+ sock->socks_->next_ = NULL;
+ sock->socks_->fd_ = 0;
+ memset(&(sock->socks_->local_end_), 0, sizeof(sock->socks_->local_end_));
+
+ memset(&(sock->remote_end_), 0, sizeof(sock->remote_end_));
sock->remote_end_set_ = 0;
struct addrinfo hints, *res;
@@ -64,12 +72,12 @@ int udp_init(udp_socket_t* sock, const char* local_addr, const char* port, resol
res = NULL;
memset (&hints, 0, sizeof (hints));
hints.ai_socktype = SOCK_DGRAM;
- hints.ai_flags |= AI_PASSIVE;
+ hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
switch(resolv_type) {
- case IPV4_ONLY: hints.ai_family = PF_INET; break;
- case IPV6_ONLY: hints.ai_family = PF_INET6; break;
- default: hints.ai_family = PF_UNSPEC; break;
+ case IPV4_ONLY: hints.ai_family = AF_INET; break;
+ case IPV6_ONLY: hints.ai_family = AF_INET6; break;
+ default: hints.ai_family = AF_UNSPEC; break;
}
int errcode = getaddrinfo(local_addr, port, &hints, &res);
@@ -85,17 +93,17 @@ int udp_init(udp_socket_t* sock, const char* local_addr, const char* port, resol
return -1;
}
- memcpy(&(sock->local_end_), res->ai_addr, res->ai_addrlen);
+ memcpy(&(sock->socks_->local_end_), res->ai_addr, res->ai_addrlen);
- sock->fd_ = socket(res->ai_family, SOCK_DGRAM, 0);
- if(sock->fd_ < 0) {
+ sock->socks_->fd_ = socket(res->ai_family, SOCK_DGRAM, 0);
+ if(sock->socks_->fd_ < 0) {
log_printf(ERROR, "Error on opening udp socket: %s", strerror(errno));
freeaddrinfo(res);
udp_close(sock);
return -1;
}
- errcode = bind(sock->fd_, res->ai_addr, res->ai_addrlen);
+ errcode = bind(sock->socks_->fd_, res->ai_addr, res->ai_addrlen);
if(errcode) {
log_printf(ERROR, "Error on binding udp socket: %s", strerror(errno));
freeaddrinfo(res);
@@ -117,7 +125,7 @@ int udp_init(udp_socket_t* sock, const char* local_addr, const char* port, resol
return 0;
}
-int udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* port, resolv_addr_type_t resolv_type)
+int udp_set_remote(udp_t* sock, const char* remote_addr, const char* port, resolv_addr_type_t resolv_type)
{
if(!sock || !remote_addr || !port)
return -1;
@@ -150,13 +158,16 @@ int udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* port
return 0;
}
-void udp_close(udp_socket_t* sock)
+void udp_close(udp_t* sock)
{
if(!sock)
return;
- if(sock->fd_ > 0)
- close(sock->fd_);
+ if(sock->socks_->fd_ > 0)
+ close(sock->socks_->fd_);
+
+ if(sock->socks_)
+ free(sock->socks_);
}
char* udp_endpoint_to_string(udp_endpoint_t e)
@@ -194,15 +205,15 @@ char* udp_endpoint_to_string(udp_endpoint_t e)
return ret;
}
-char* udp_get_local_end_string(udp_socket_t* sock)
+char* udp_get_local_end_string(udp_t* sock)
{
if(!sock)
return NULL;
- return udp_endpoint_to_string(sock->local_end_);
+ return udp_endpoint_to_string(sock->socks_->local_end_);
}
-char* udp_get_remote_end_string(udp_socket_t* sock)
+char* udp_get_remote_end_string(udp_t* sock)
{
if(!sock || !sock->remote_end_set_)
return NULL;
@@ -210,27 +221,27 @@ char* udp_get_remote_end_string(udp_socket_t* sock)
return udp_endpoint_to_string(sock->remote_end_);
}
-int udp_read(udp_socket_t* sock, u_int8_t* buf, u_int32_t len, udp_endpoint_t* remote_end)
+int udp_read(udp_t* sock, u_int8_t* buf, u_int32_t len, udp_endpoint_t* remote_end)
{
if(!sock || !remote_end)
return -1;
socklen_t socklen = sizeof(*remote_end);
- return recvfrom(sock->fd_, buf, len, 0, (struct sockaddr *)remote_end, &socklen);
+ return recvfrom(sock->socks_->fd_, buf, len, 0, (struct sockaddr *)remote_end, &socklen);
}
-int udp_write(udp_socket_t* sock, u_int8_t* buf, u_int32_t len)
+int udp_write(udp_t* sock, u_int8_t* buf, u_int32_t len)
{
if(!sock || !sock->remote_end_set_)
return -1;
socklen_t socklen = sizeof(sock->remote_end_);
#ifdef NO_V4MAPPED
- if((((struct sockaddr *)&sock->local_end_)->sa_family) == AF_INET)
+ if((((struct sockaddr *)&sock->socks_->local_end_)->sa_family) == AF_INET)
socklen = sizeof(struct sockaddr_in);
- else if ((((struct sockaddr *)&sock->local_end_)->sa_family) == AF_INET6)
+ else if ((((struct sockaddr *)&sock->socks_->local_end_)->sa_family) == AF_INET6)
socklen = sizeof(struct sockaddr_in6);
#endif
- return sendto(sock->fd_, buf, len, 0, (struct sockaddr *)&(sock->remote_end_), socklen);;
+ return sendto(sock->socks_->fd_, buf, len, 0, (struct sockaddr *)&(sock->remote_end_), socklen);;
}
diff --git a/src/udp.h b/src/udp.h
index 02a1c26..e74e2cd 100644
--- a/src/udp.h
+++ b/src/udp.h
@@ -46,20 +46,26 @@ typedef struct sockaddr_storage udp_endpoint_t;
struct udp_socket_struct {
int fd_;
udp_endpoint_t local_end_;
+ struct udp_socket_struct* next_;
+};
+typedef struct udp_socket_struct udp_socket_t;
+
+struct udp_struct {
+ udp_socket_t* socks_;
udp_endpoint_t remote_end_;
int remote_end_set_;
};
-typedef struct udp_socket_struct udp_socket_t;
+typedef struct udp_struct udp_t;
-int udp_init(udp_socket_t* sock, const char* local_addr, const char* port, resolv_addr_type_t resolv_type);
-int udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* port, resolv_addr_type_t resolv_type);
-void udp_close(udp_socket_t* sock);
+int udp_init(udp_t* sock, const char* local_addr, const char* port, resolv_addr_type_t resolv_type);
+int udp_set_remote(udp_t* sock, const char* remote_addr, const char* port, resolv_addr_type_t resolv_type);
+void udp_close(udp_t* sock);
char* udp_endpoint_to_string(udp_endpoint_t e);
-char* udp_get_local_end_string(udp_socket_t* sock);
-char* udp_get_remote_end_string(udp_socket_t* sock);
+char* udp_get_local_end_string(udp_t* sock);
+char* udp_get_remote_end_string(udp_t* sock);
-int udp_read(udp_socket_t* sock, u_int8_t* buf, u_int32_t len, udp_endpoint_t* remote_end);
-int udp_write(udp_socket_t* sock, u_int8_t* buf, u_int32_t len);
+int udp_read(udp_t* sock, u_int8_t* buf, u_int32_t len, udp_endpoint_t* remote_end);
+int udp_write(udp_t* sock, u_int8_t* buf, u_int32_t len);
#endif