From a03d90ca84881b3c1faea7559af32df12655bc39 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 15 Feb 2019 18:09:51 +0100 Subject: [PATCH] WIP multiple port support --- src/donar.c | 32 +++++++++++++++++--------------- src/donar_client.c | 12 ++++++------ src/donar_client.h | 3 ++- src/donar_server.c | 27 ++++++++++++++++++--------- src/donar_server.h | 3 ++- src/packet.h | 2 ++ 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/donar.c b/src/donar.c index 1567d62..9af7a19 100644 --- a/src/donar.c +++ b/src/donar.c @@ -3,33 +3,36 @@ #include #include #include +#include #include "algo_skel.h" #include "donar_client.h" #include "donar_server.h" +void free_port (void* ptr) { + free(ptr); +} + int main(int argc, char** argv) { printf("~ Donar ~\n"); int opt, is_server, is_client, errored; - char *host, *port, *onion_file, *algo; - host = NULL; - port = NULL; + char *port, *onion_file, *algo; onion_file = NULL; algo = NULL; is_server = 0; is_client = 0; errored = 0; + GPtrArray* ports = g_ptr_array_new_with_free_func (free_port); while ((opt = getopt(argc, argv, "csh:p:o:a:")) != -1) { switch(opt) { case 's': is_server = 1; break; - case 'h': - host = strdup(optarg); - break; case 'p': port = strdup(optarg); + if (port == NULL) goto terminate; + g_ptr_array_add (ports, port); break; case 'o': onion_file = strdup(optarg); @@ -53,24 +56,23 @@ int main(int argc, char** argv) { if (is_server) { struct donar_server_ctx ctx; - if (host == NULL || port == NULL) goto in_error; - donar_server(&ctx, &as, host, port); + if (ports->len < 1) goto in_error; + donar_server(&ctx, &as, ports); } else if (is_client) { struct donar_client_ctx ctx; - if (port == NULL || onion_file == NULL) goto in_error; - donar_client(&ctx, &as, onion_file, port); + if (ports->len < 1 || onion_file == NULL) goto in_error; + donar_client(&ctx, &as, onion_file, ports); } goto terminate; in_error: errored = 1; - fprintf(stderr, "Usage as client : %s -c -a -p -o \n", argv[0]); - fprintf(stderr, "Usage as server : %s -s -a -h -p \n\n", argv[0]); - fprintf(stderr, "Passed parameters: client=%d, server=%d, algo=%s, host=%s, port=%s, onion_file=%s\n", - is_client, is_server, algo, host, port, onion_file); + fprintf(stderr, "Usage as client : %s -c -a -p -p -o \n", argv[0]); + fprintf(stderr, "Usage as server : %s -s -a -p -p \n\n", argv[0]); + fprintf(stderr, "Passed parameters: client=%d, server=%d, algo=%s, registered_ports=%d, onion_file=%s\n", + is_client, is_server, algo, ports->len, onion_file); terminate: - if (host != NULL) free(host); if (onion_file != NULL) free(onion_file); if (port != NULL) free(port); if (algo != NULL) free(algo); diff --git a/src/donar_client.c b/src/donar_client.c index d62d48e..34328e7 100644 --- a/src/donar_client.c +++ b/src/donar_client.c @@ -88,9 +88,9 @@ on_socks5_err: init_tcp_client (app_ctx, pos); } -void init_udp_server(struct donar_client_ctx* ctx, char* port) { +void init_udp_socket(char* port, struct donar_client_ctx* ctx) { int sock1, sock2; - sock1 = create_udp_server(port); + sock1 = create_udp_client ("127.0.0.1", port); if (sock1 < 0) goto socket_failed; sock2 = dup(sock1); if (sock2 < 0) goto socket_failed; @@ -99,11 +99,11 @@ void init_udp_server(struct donar_client_ctx* ctx, char* port) { return; socket_failed: - fprintf(stderr, "Socket initialization for the UDP server failed\n"); + fprintf(stderr, "UDP socket init failed\n"); exit(EXIT_FAILURE); } -void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* onion_file, char* port) { +void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* onion_file, GPtrArray* ports) { evt_core_init (&(ctx->evts)); struct evt_core_cat init_socks5 = { .app_ctx = ctx, @@ -129,8 +129,8 @@ void donar_client(struct donar_client_ctx* ctx, struct algo_skel* algo, char* on } printf("--- TCP Clients Connected\n"); - init_udp_server (ctx, port); - printf("--- UDP Server is listening\n"); + g_ptr_array_foreach (ports, (void(*)(void*, void*))init_udp_socket, ctx); + printf("--- UDP Sockets are configured\n"); evt_core_loop(&(ctx->evts)); diff --git a/src/donar_client.h b/src/donar_client.h index 356104f..5f44fba 100644 --- a/src/donar_client.h +++ b/src/donar_client.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include "algo_skel.h" #include "tor_os.h" #include "socks5.h" @@ -17,4 +18,4 @@ struct donar_client_ctx { } client_sock[CLIENT_PORT_SIZE]; }; -void donar_client(struct donar_client_ctx* ctx, struct algo_skel* as, char* onion_file, char* port); +void donar_client(struct donar_client_ctx* ctx, struct algo_skel* as, char* onion_file, GPtrArray* ports); diff --git a/src/donar_server.c b/src/donar_server.c index bf377db..38b9278 100644 --- a/src/donar_server.c +++ b/src/donar_server.c @@ -40,8 +40,22 @@ socket_create_err: exit(EXIT_FAILURE); } +void serv_init_udp_socket(char* port, struct donar_server_ctx* ctx) { + int sock1, sock2; + sock1 = create_udp_client ("127.0.0.1", 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; -void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* udp_host, char* udp_port) { +socket_failed: + fprintf(stderr, "UDP socket init failed\n"); + exit(EXIT_FAILURE); +} + +void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, GPtrArray* ports) { evt_core_init (&(ctx->evts)); evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_co)); evt_core_add_cat (&(ctx->evts), &(algo->on_udp_read)); @@ -50,14 +64,6 @@ void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* ud evt_core_add_cat (&(ctx->evts), &(algo->on_tcp_write)); printf("--- Categories created\n"); - int sock = create_udp_client (udp_host, udp_port); - if (sock < 0) { - fprintf(stderr, "Unable to create a UDP client socket\n"); - exit(EXIT_FAILURE); - } - evt_core_add_fd (&(ctx->evts), "udp-read", sock); - evt_core_add_fd (&(ctx->evts), "udp-write", dup(sock)); - printf("--- UDP client is connected\n"); for (uint16_t i = 0; i < PORT_SIZE ; i++) { ctx->ports[i] = 7500 + i; @@ -67,6 +73,9 @@ void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* ud init_tcp_servers(ctx); printf("--- TCP servers are listening\n"); + g_ptr_array_foreach (ports, (void(*)(void*, void*))serv_init_udp_socket, ctx); + printf("--- UDP Sockets are configured\n"); + evt_core_loop (&(ctx->evts)); destroy_resources (&(ctx->tos), &(ctx->tctl)); diff --git a/src/donar_server.h b/src/donar_server.h index 053af17..728a2bb 100644 --- a/src/donar_server.h +++ b/src/donar_server.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "socks5.h" #include "tor_os.h" #include "tor_ctl.h" @@ -18,4 +19,4 @@ struct donar_server_ctx { uint16_t ports[PORT_SIZE]; }; -void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, char* udp_host, char* udp_port); +void donar_server(struct donar_server_ctx* ctx, struct algo_skel* algo, GPtrArray* ports); diff --git a/src/packet.h b/src/packet.h index 12a77dd..234de96 100644 --- a/src/packet.h +++ b/src/packet.h @@ -31,6 +31,8 @@ union abstract_packet { char raw; struct { uint16_t size; + uint16_t port; + uint8_t id; char payload; } str; };