diff --git a/src/donar.c b/src/donar.c index 9dfc185..635e985 100644 --- a/src/donar.c +++ b/src/donar.c @@ -8,79 +8,74 @@ #include "donar_client.h" #include "donar_server.h" -void free_port (void* ptr) { - free(ptr); -} - int main(int argc, char** argv) { setvbuf(stdout, NULL, _IONBF, 0); printf("~ Donar ~\n"); - int opt, is_server, is_client, errored; - char *port, *onion_file, *algo; - onion_file = NULL; - algo = NULL; - is_server = 0; - is_client = 0; - errored = 0; - GPtrArray* remote_ports = g_ptr_array_new_with_free_func (free_port); - GPtrArray* exposed_ports = g_ptr_array_new_with_free_func (free_port); + struct donar_params dp; + donar_init_params (&dp); - while ((opt = getopt(argc, argv, "csh:e:r:o:a:")) != -1) { - switch(opt) { + while ((dp.opt = getopt(argc, argv, "csh:e:r:o:a:b:h:")) != -1) { + switch(dp.opt) { case 's': - is_server = 1; + dp.is_server = 1; break; case 'e': - port = strdup(optarg); - if (port == NULL) goto terminate; - g_ptr_array_add (exposed_ports, port); + dp.port = strdup(optarg); + if (dp.port == NULL) goto terminate; + g_ptr_array_add (dp.exposed_ports, dp.port); break; case 'r': - port = strdup(optarg); - if (port == NULL) goto terminate; - g_ptr_array_add (remote_ports, port); + dp.port = strdup(optarg); + if (dp.port == NULL) goto terminate; + g_ptr_array_add (dp.remote_ports, dp.port); break; case 'o': - onion_file = strdup(optarg); + dp.onion_file = strdup(optarg); break; case 'c': - is_client = 1; + dp.is_client = 1; break; case 'a': - algo = strdup(optarg); + dp.algo = strdup(optarg); + break; + case 'h': + dp.is_healing = 1; + break; + case 'b': + dp.is_waiting_bootstrap = 1; break; default: goto in_error; } } - if (!(is_server ^ is_client)) goto in_error; - if (algo == NULL) goto in_error; + if (!(dp.is_server ^ dp.is_client)) goto in_error; + if (dp.algo == NULL) goto in_error; - if (is_server) { + if (dp.is_server) { struct donar_server_ctx ctx; - if (exposed_ports->len < 1 && remote_ports->len < 1) goto in_error; - donar_server(&ctx, algo, exposed_ports, remote_ports); - } else if (is_client) { + if (dp.exposed_ports->len < 1 && dp.remote_ports->len < 1) goto in_error; + donar_server(&ctx, &dp); + } else if (dp.is_client) { struct donar_client_ctx ctx; - if ((exposed_ports->len < 1 && remote_ports->len < 1) || onion_file == NULL) goto in_error; - donar_client(&ctx, algo, onion_file, exposed_ports, remote_ports); + if ((dp.exposed_ports->len < 1 && dp.remote_ports->len < 1) || dp.onion_file == NULL) goto in_error; + donar_client(&ctx, &dp); } goto terminate; in_error: - errored = 1; - fprintf(stderr, "Usage as client : %s -c -a -o -e -r \n", argv[0]); - fprintf(stderr, "Usage as server : %s -s -a -e -r \n\n", argv[0]); + dp.errored = 1; + fprintf(stderr, "Usage as client : %s -c -a [-h] [-b] -o -e [-e ...]* -r [-r ...]*\n", argv[0]); + fprintf(stderr, "Usage as server : %s -s -a [-h] [-b] -e [-e ...]* -r [-r ...]*\n\n", argv[0]); fprintf(stderr, "Passed parameters: client=%d, server=%d, algo=%s, exposed_ports=%d, remote_ports=%d, onion_file=%s\n", - is_client, is_server, algo, exposed_ports->len, remote_ports->len, onion_file); + dp.is_client, dp.is_server, dp.algo, dp.exposed_ports->len, dp.remote_ports->len, dp.onion_file); terminate: - if (onion_file != NULL) free(onion_file); - if (algo != NULL) free(algo); - g_ptr_array_free(exposed_ports, TRUE); - g_ptr_array_free(remote_ports, TRUE); + if (dp.onion_file != NULL) free(dp.onion_file); + if (dp.algo != NULL) free(dp.algo); + g_ptr_array_free(dp.exposed_ports, TRUE); + g_ptr_array_free(dp.remote_ports, TRUE); - return errored; + return dp.errored; } diff --git a/src/donar_client.c b/src/donar_client.c index 32bf02e..206976f 100644 --- a/src/donar_client.c +++ b/src/donar_client.c @@ -69,12 +69,11 @@ void init_socks5_sinks(struct donar_client_ctx* app_ctx) { evt_core_add_cat(&app_ctx->evts, &template); } -void donar_client(struct donar_client_ctx* ctx, char* algoname, - char* onion_file, GPtrArray* exposed_ports, GPtrArray* remote_ports) { +void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp) { struct algo_skel algo = {0}; evt_core_init (&(ctx->evts)); - init_algo(&ctx->evts, &algo, algoname); + init_algo(&ctx->evts, &algo, dp->algo); socks5_init (&ctx->evts); init_socks5_sinks(ctx); evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_co)); @@ -84,7 +83,7 @@ void donar_client(struct donar_client_ctx* ctx, char* algoname, evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_write)); printf("--- Categories created\n"); - load_onion_services (ctx, onion_file, CLIENT_PORT_SIZE); + load_onion_services (ctx, dp->onion_file, CLIENT_PORT_SIZE); printf("--- Onion services loaded\n"); for (int i = 0; i < CLIENT_PORT_SIZE; i++) { @@ -92,10 +91,10 @@ void donar_client(struct donar_client_ctx* ctx, char* algoname, } printf("--- TCP Clients Connected\n"); - g_ptr_array_foreach (remote_ports, (void(*)(void*, void*))init_udp_remote, &(ctx->evts)); + g_ptr_array_foreach (dp->remote_ports, (void(*)(void*, void*))init_udp_remote, &(ctx->evts)); printf("--- Remote ports are binded locally\n"); - g_ptr_array_foreach (exposed_ports, (void(*)(void*, void*))init_udp_exposed, &(ctx->evts)); + g_ptr_array_foreach (dp->exposed_ports, (void(*)(void*, void*))init_udp_exposed, &(ctx->evts)); printf("--- Local UDP services are exposed\n"); evt_core_loop(&(ctx->evts)); diff --git a/src/donar_client.h b/src/donar_client.h index 497259d..4abfb03 100644 --- a/src/donar_client.h +++ b/src/donar_client.h @@ -19,5 +19,4 @@ struct donar_client_ctx { } client_sock[CLIENT_PORT_SIZE]; }; -void donar_client(struct donar_client_ctx* ctx, char* algoname, - char* onion_file, GPtrArray* exposed_ports, GPtrArray* remote_ports); +void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp); diff --git a/src/donar_init.c b/src/donar_init.c index d6dce07..9c48f87 100644 --- a/src/donar_init.c +++ b/src/donar_init.c @@ -87,3 +87,19 @@ socket_failed: fprintf(stderr, "UDP socket init failed\n"); exit(EXIT_FAILURE); } + +void free_port (void* ptr) { + free(ptr); +} + +void donar_init_params(struct donar_params* dp) { + dp->onion_file = NULL; + dp->algo = NULL; + dp->is_server = 0; + dp->is_client = 0; + dp->is_healing = 0; + dp->is_waiting_bootstrap = 0; + dp->errored = 0; + dp->remote_ports = g_ptr_array_new_with_free_func (free_port); + dp->exposed_ports = g_ptr_array_new_with_free_func (free_port); +} diff --git a/src/donar_init.h b/src/donar_init.h index bb6fe6c..72595c5 100644 --- a/src/donar_init.h +++ b/src/donar_init.h @@ -6,5 +6,12 @@ #include "evt_core.h" #include "packet.h" +struct donar_params { + int opt, is_server, is_client, is_waiting_bootstrap, is_healing, errored; + char *port, *onion_file, *algo; + GPtrArray *remote_ports, *exposed_ports; +}; + void init_udp_remote(char* port, struct evt_core_ctx* evts); void init_udp_exposed(char* port, struct evt_core_ctx* evts); +void donar_init_params(struct donar_params* dp); diff --git a/src/donar_server.c b/src/donar_server.c index f557261..4335f0b 100644 --- a/src/donar_server.c +++ b/src/donar_server.c @@ -51,12 +51,11 @@ socket_create_err: exit(EXIT_FAILURE); } -void donar_server(struct donar_server_ctx* ctx, char* algoname, - GPtrArray* exposed_ports, GPtrArray* remote_ports) { +void donar_server(struct donar_server_ctx* ctx, struct donar_params* dp) { struct algo_skel algo = {0}; evt_core_init (&(ctx->evts)); - init_algo(&ctx->evts, &algo, algoname); + init_algo(&ctx->evts, &algo, dp->algo); evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_co)); evt_core_add_cat (&(ctx->evts), &(algo.on_udp_read)); evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_read)); @@ -74,10 +73,10 @@ void donar_server(struct donar_server_ctx* ctx, char* algoname, init_tcp_servers(ctx); printf("--- TCP servers are listening\n"); - g_ptr_array_foreach (remote_ports, (void(*)(void*, void*))init_udp_remote, &(ctx->evts)); + g_ptr_array_foreach (dp->remote_ports, (void(*)(void*, void*))init_udp_remote, &(ctx->evts)); printf("--- Remote ports are binded locally\n"); - g_ptr_array_foreach (exposed_ports, (void(*)(void*, void*))init_udp_exposed, &(ctx->evts)); + g_ptr_array_foreach (dp->exposed_ports, (void(*)(void*, void*))init_udp_exposed, &(ctx->evts)); printf("--- Local UDP services are exposed\n"); evt_core_loop (&(ctx->evts)); diff --git a/src/donar_server.h b/src/donar_server.h index bc4875b..0e761da 100644 --- a/src/donar_server.h +++ b/src/donar_server.h @@ -20,5 +20,4 @@ struct donar_server_ctx { uint16_t ports[PORT_SIZE]; }; -void donar_server(struct donar_server_ctx* ctx, char* algoname, - GPtrArray* exposed_ports, GPtrArray* remote_ports); +void donar_server(struct donar_server_ctx* ctx, struct donar_params* dp);