From a3d2b4a4b0b5c8836e033ba5862e88f9b550257f Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 21 Jun 2019 16:50:49 +0200 Subject: [PATCH] Add bound IP --- src/donar.c | 12 +++++++++--- src/donar_client.c | 4 +++- src/donar_init.c | 3 ++- src/donar_init.h | 4 ++-- src/donar_server.c | 5 ++++- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/donar.c b/src/donar.c index 1fc6402..5ebdf0d 100644 --- a/src/donar.c +++ b/src/donar.c @@ -14,7 +14,7 @@ int main(int argc, char** argv) { struct donar_params dp = {0}; donar_init_params (&dp); - while ((dp.opt = getopt(argc, argv, "vcse:r:o:a:bhl:d:f:")) != -1) { + while ((dp.opt = getopt(argc, argv, "vcse:r:o:a:bhl:d:f:i:")) != -1) { switch(dp.opt) { case 'v': dp.verbose++; @@ -53,6 +53,9 @@ int main(int argc, char** argv) { case 'd': sscanf(optarg, "%d,%d", &dp.fresh_data, &dp.redundant_data); break; + case 'i': + dp.bound_ip = strdup(optarg); + break; case 'f': dp.capture_file = strdup(optarg); break; @@ -63,6 +66,7 @@ int main(int argc, char** argv) { if (!(dp.is_server ^ dp.is_client)) goto in_error; if (dp.algo == NULL) goto in_error; + if (dp.bound_ip == NULL) dp.bound_ip = strdup("127.0.0.1"); if (dp.is_server) { struct donar_server_ctx ctx; @@ -77,15 +81,17 @@ int main(int argc, char** argv) { in_error: dp.errored = 1; - fprintf(stderr, "Usage as client : %s -c -a -o [-h] [-b] [-f ] [-l ] [-d ,] [-e ]* [-r ]*\n", argv[0]); - fprintf(stderr, "Usage as server : %s -s -a [-h] [-b] [-l ] [-f ] [-d ,] [-e ]* [-r ]*\n\n", argv[0]); + fprintf(stderr, "Usage as client : %s -c -a -o [-h] [-b] [-i ] [-f ] [-l ] [-d ,] [-e ]* [-r ]*\n", argv[0]); + fprintf(stderr, "Usage as server : %s -s -a [-h] [-b] [-i ] [-l ] [-f ] [-d ,] [-e ]* [-r ]*\n\n", argv[0]); fprintf(stderr, "Passed parameters: client=%d, server=%d, algo=%s, exposed_ports=%d, remote_ports=%d, onion_file=%s, links=%d, duplication=%d,%d\n", dp.is_client, dp.is_server, dp.algo, dp.exposed_ports->len, dp.remote_ports->len, dp.onion_file, dp.links, dp.fresh_data, dp.redundant_data); terminate: + // @FIXME: Should be refactored in free_donar_params() if (dp.onion_file != NULL) free(dp.onion_file); if (dp.algo != NULL) free(dp.algo); if (dp.capture_file != NULL) free(dp.capture_file); + if (dp.bound_ip != NULL) free(dp.bound_ip); g_ptr_array_free(dp.exposed_ports, TRUE); g_ptr_array_free(dp.remote_ports, TRUE); diff --git a/src/donar_client.c b/src/donar_client.c index cef5dc3..9962c65 100644 --- a/src/donar_client.c +++ b/src/donar_client.c @@ -103,7 +103,9 @@ void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp) { 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 (dp->exposed_ports, (void(*)(void*, void*))init_udp_exposed, &(ctx->evts)); + for (int i = 0; i < dp->exposed_ports->len; i++) { + init_udp_exposed(dp->bound_ip, g_ptr_array_index (dp->exposed_ports, i), &(ctx->evts)); + } printf("--- Local UDP services are exposed\n"); evt_core_loop(&(ctx->evts)); diff --git a/src/donar_init.c b/src/donar_init.c index 5d21c9e..c76717a 100644 --- a/src/donar_init.c +++ b/src/donar_init.c @@ -99,7 +99,7 @@ socket_failed: exit(EXIT_FAILURE); } -void init_udp_exposed(char* port, struct evt_core_ctx* evts) { +void init_udp_exposed(char* bound_ip, char* port, struct evt_core_ctx* evts) { int sock1, sock2; char url[1024]; @@ -147,6 +147,7 @@ void donar_init_params(struct donar_params* dp) { dp->onion_file = NULL; dp->algo = NULL; dp->capture_file = NULL; + dp->bound_ip = NULL; dp->is_server = 0; dp->is_client = 0; dp->is_healing = 0; diff --git a/src/donar_init.h b/src/donar_init.h index 3629fd7..a4d8ad6 100644 --- a/src/donar_init.h +++ b/src/donar_init.h @@ -10,11 +10,11 @@ struct donar_params { int opt, is_server, is_client, is_waiting_bootstrap, is_healing, errored, verbose, links, fresh_data, redundant_data; - char *port, *onion_file, *algo, *capture_file; + char *bound_ip, *port, *onion_file, *algo, *capture_file; GPtrArray *remote_ports, *exposed_ports; }; void signal_init(struct evt_core_ctx* evts); void init_udp_remote(char* port, struct evt_core_ctx* evts); -void init_udp_exposed(char* port, struct evt_core_ctx* evts); +void init_udp_exposed(char *bound_ip, 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 e403672..6295800 100644 --- a/src/donar_server.c +++ b/src/donar_server.c @@ -82,8 +82,11 @@ void donar_server(struct donar_server_ctx* ctx, struct donar_params* dp) { 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 (dp->exposed_ports, (void(*)(void*, void*))init_udp_exposed, &(ctx->evts)); + for (int i = 0; i < dp->exposed_ports->len; i++) { + init_udp_exposed(dp->bound_ip, g_ptr_array_index (dp->exposed_ports, i), &(ctx->evts)); + } printf("--- Local UDP services are exposed\n"); + evt_core_loop (&(ctx->evts)); destroy_resources (&(ctx->tos), &(ctx->tctl));