From d0386fb94854aeca9de00e7fac1e64c611c22692 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 12 Feb 2019 22:07:47 +0100 Subject: [PATCH] Seems to work, need to migrate to the UDP api however --- src/donar_client.c | 22 +++++++++++++++++++++- src/net_tools.c | 12 ++++++++++-- src/net_tools.h | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/donar_client.c b/src/donar_client.c index 80337b7..3308b29 100644 --- a/src/donar_client.c +++ b/src/donar_client.c @@ -41,6 +41,21 @@ failed_socks5: } } +void init_udp_server(struct donar_client_ctx* ctx, char* port) { + int sock1, sock2; + sock1 = create_udp_server(port); + if (sock1 < 0) goto socket_failed; + sock2 = dup(sock1); + if (sock2 < 0) goto socket_failed; + evt_core_add_fd (&(ctx->evts), "udp-read", sock1); + evt_core_add_fd (&(ctx->evts), "udp-write", sock2); + return; + +socket_failed: + fprintf(stderr, "Socket initialization for the UDP server failed\n"); + exit(EXIT_FAILURE); +} + void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* onion_file, char* port) { evt_core_init (&(ctx->evts)); evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_co)); @@ -54,5 +69,10 @@ void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* on printf("--- Onion services loaded\n"); init_tcp_clients(ctx); printf("--- TCP Clients Connected\n"); - // TODO: Debug that, listen UDP and launch loop + init_udp_server (ctx, port); + printf("--- UDP Server is listening\n"); + + evt_core_loop(&(ctx->evts)); + + tor_os_free (&(ctx->tos)); } diff --git a/src/net_tools.c b/src/net_tools.c index ca10bab..803e3d2 100644 --- a/src/net_tools.c +++ b/src/net_tools.c @@ -51,14 +51,14 @@ int create_udp_client(char* host, char* service) { return create_ip_client (host, service, SOCK_DGRAM); } -int create_tcp_server(char* service) { +int create_ip_server(char* service, int type) { int err, sock, enable; struct addrinfo conf; struct addrinfo *result, *cursor; memset(&conf, 0, sizeof(struct addrinfo)); conf.ai_family = AF_INET; // AF_UNSPEC to listen on IPv6 or IPv4 - conf.ai_socktype = SOCK_STREAM; + conf.ai_socktype = type; conf.ai_flags = 0; // AI_PASSIVE to listen on 0.0.0.0 conf.ai_protocol = 0; @@ -92,6 +92,14 @@ int create_tcp_server(char* service) { return sock; } +int create_tcp_server(char* service) { + return create_ip_server (service, SOCK_STREAM); +} + +int create_udp_server(char* service) { + return create_ip_server (service, SOCK_DGRAM); +} + /* * The idea is to get the file descriptor flags * then we add to the bitmask O_NONBLOCK and set the diff --git a/src/net_tools.h b/src/net_tools.h index 1ec2be8..b0105b8 100644 --- a/src/net_tools.h +++ b/src/net_tools.h @@ -11,6 +11,7 @@ int create_tcp_client(char* host, char* service); int create_udp_client(char* host, char* service); int create_tcp_server(char* service); +int create_udp_server(char* service); int make_socket_non_blocking(int fd); void add_fd_to_epoll(int epollfd, int fd, uint32_t flags); int read_entity(int fd, void* entity, int size);