summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/uanytun.c16
-rw-r--r--src/udp.c49
-rw-r--r--src/udp.h4
3 files changed, 30 insertions, 39 deletions
diff --git a/src/uanytun.c b/src/uanytun.c
index 1d6e646..1225690 100644
--- a/src/uanytun.c
+++ b/src/uanytun.c
@@ -231,21 +231,21 @@ int main(int argc, char* argv[])
}
- udp_socket_t* sock;
- udp_init(&sock, opt.local_addr_, opt.local_port_);
- if(!sock) {
+ udp_socket_t sock;
+ ret = udp_init(&sock, opt.local_addr_, opt.local_port_);
+ if(ret) {
log_printf(ERR, "error on udp_init, exitting");
options_clear(&opt);
tun_close(&dev);
- exit(-1);
+ exit(ret);
}
- char* local_string = udp_get_local_end_string(sock);
+ char* local_string = udp_get_local_end_string(&sock);
log_printf(NOTICE, "listening on: %s", local_string);
free(local_string);
if(opt.remote_addr_) {
- udp_set_remote(sock, opt.remote_addr_, opt.remote_port_);
- char* remote_string = udp_get_remote_end_string(sock);
+ udp_set_remote(&sock, opt.remote_addr_, opt.remote_port_);
+ char* remote_string = udp_get_remote_end_string(&sock);
log_printf(NOTICE, "set remote end to: %s", remote_string);
free(remote_string);
}
@@ -273,7 +273,7 @@ int main(int argc, char* argv[])
fclose(pid_file);
}
- ret = main_loop(&dev, sock, &opt);
+ ret = main_loop(&dev, &sock, &opt);
tun_close(&dev);
udp_close(&sock);
diff --git a/src/udp.c b/src/udp.c
index ca83561..853812c 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -43,17 +43,14 @@
#include <netdb.h>
#include <netinet/in.h>
-void udp_init(udp_socket_t** sock, const char* local_addr, const char* port)
+int udp_init(udp_socket_t* sock, const char* local_addr, const char* port)
{
if(!sock || !port)
return;
- *sock = malloc(sizeof(udp_socket_t));
- if(!*sock)
- return;
-
- memset(&((*sock)->local_end_), 0, sizeof((*sock)->local_end_));
- memset(&((*sock)->remote_end_), 0, sizeof((*sock)->local_end_));
+ sock->fd_ = 0;
+ memset(&(sock->local_end_), 0, sizeof(sock->local_end_));
+ memset(&(sock->remote_end_), 0, sizeof(sock->local_end_));
struct addrinfo hints, *res;
@@ -67,36 +64,34 @@ void udp_init(udp_socket_t** sock, const char* local_addr, const char* port)
hints.ai_family = PF_UNSPEC;
#endif
-
int errcode = getaddrinfo(local_addr, port, &hints, &res);
if (errcode != 0) {
log_printf(ERR, "Error resolving local address: %s", gai_strerror(errcode));
- free(*sock);
- *sock = NULL;
- return;
+ udp_close(sock);
+ return -1;
}
- memcpy(&((*sock)->local_end_), res->ai_addr, sizeof(*(res->ai_addr)));
+ memcpy(&(sock->local_end_), res->ai_addr, sizeof(*(res->ai_addr)));
- (*sock)->fd_ = socket(res->ai_family, SOCK_DGRAM, 0);
- if((*sock)->fd_ < 0) {
+ sock->fd_ = socket(res->ai_family, SOCK_DGRAM, 0);
+ if(sock->fd_ < 0) {
log_printf(ERR, "Error on opening udp socket: %m");
freeaddrinfo(res);
- free(*sock);
- *sock = NULL;
- return;
+ udp_close(sock);
+ return -1;
}
- errcode = bind((*sock)->fd_, res->ai_addr, res->ai_addrlen);
+ errcode = bind(sock->fd_, res->ai_addr, res->ai_addrlen);
if(errcode) {
log_printf(ERR, "Error on binding udp socket: %m");
freeaddrinfo(res);
- free(*sock);
- *sock = NULL;
- return;
+ udp_close(sock);
+ return -1;
}
freeaddrinfo(res);
+
+ return 0;
}
void udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* port)
@@ -115,7 +110,6 @@ void udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* por
hints.ai_family = PF_UNSPEC;
#endif
-
int errcode = getaddrinfo(remote_addr, port, &hints, &res);
if (errcode != 0) {
log_printf(ERR, "Error resolving remote address: %s", gai_strerror(errcode));
@@ -125,16 +119,13 @@ void udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* por
freeaddrinfo(res);
}
-void udp_close(udp_socket_t** sock)
+void udp_close(udp_socket_t* sock)
{
- if(!sock || !(*sock))
+ if(!sock)
return;
- if((*sock)->fd_ > 0)
- close((*sock)->fd_);
-
- free(*sock);
- *sock = NULL;
+ if(sock->fd_ > 0)
+ close(sock->fd_);
}
char* udp_endpoint_to_string(udp_endpoint_t e)
diff --git a/src/udp.h b/src/udp.h
index 7bddf15..d998b39 100644
--- a/src/udp.h
+++ b/src/udp.h
@@ -52,9 +52,9 @@ struct udp_socket_struct {
};
typedef struct udp_socket_struct udp_socket_t;
-void udp_init(udp_socket_t** sock, const char* local_addr, const char* port);
+int udp_init(udp_socket_t* sock, const char* local_addr, const char* port);
void udp_set_remote(udp_socket_t* sock, const char* remote_addr, const char* port);
-void udp_close(udp_socket_t** sock);
+void udp_close(udp_socket_t* sock);
char* udp_endpoint_to_string(udp_endpoint_t e);
char* udp_get_local_end_string(udp_socket_t* sock);