Add a donar parameters structure

This commit is contained in:
Quentin Dufour 2019-03-28 11:47:14 +01:00
parent 1b0954ecca
commit c12715e39e
7 changed files with 71 additions and 57 deletions

View file

@ -8,79 +8,74 @@
#include "donar_client.h" #include "donar_client.h"
#include "donar_server.h" #include "donar_server.h"
void free_port (void* ptr) {
free(ptr);
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
printf("~ Donar ~\n"); printf("~ Donar ~\n");
int opt, is_server, is_client, errored; struct donar_params dp;
char *port, *onion_file, *algo; donar_init_params (&dp);
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);
while ((opt = getopt(argc, argv, "csh:e:r:o:a:")) != -1) { while ((dp.opt = getopt(argc, argv, "csh:e:r:o:a:b:h:")) != -1) {
switch(opt) { switch(dp.opt) {
case 's': case 's':
is_server = 1; dp.is_server = 1;
break; break;
case 'e': case 'e':
port = strdup(optarg); dp.port = strdup(optarg);
if (port == NULL) goto terminate; if (dp.port == NULL) goto terminate;
g_ptr_array_add (exposed_ports, port); g_ptr_array_add (dp.exposed_ports, dp.port);
break; break;
case 'r': case 'r':
port = strdup(optarg); dp.port = strdup(optarg);
if (port == NULL) goto terminate; if (dp.port == NULL) goto terminate;
g_ptr_array_add (remote_ports, port); g_ptr_array_add (dp.remote_ports, dp.port);
break; break;
case 'o': case 'o':
onion_file = strdup(optarg); dp.onion_file = strdup(optarg);
break; break;
case 'c': case 'c':
is_client = 1; dp.is_client = 1;
break; break;
case 'a': 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; break;
default: default:
goto in_error; goto in_error;
} }
} }
if (!(is_server ^ is_client)) goto in_error; if (!(dp.is_server ^ dp.is_client)) goto in_error;
if (algo == NULL) goto in_error; if (dp.algo == NULL) goto in_error;
if (is_server) { if (dp.is_server) {
struct donar_server_ctx ctx; struct donar_server_ctx ctx;
if (exposed_ports->len < 1 && remote_ports->len < 1) goto in_error; if (dp.exposed_ports->len < 1 && dp.remote_ports->len < 1) goto in_error;
donar_server(&ctx, algo, exposed_ports, remote_ports); donar_server(&ctx, &dp);
} else if (is_client) { } else if (dp.is_client) {
struct donar_client_ctx ctx; struct donar_client_ctx ctx;
if ((exposed_ports->len < 1 && remote_ports->len < 1) || onion_file == NULL) goto in_error; if ((dp.exposed_ports->len < 1 && dp.remote_ports->len < 1) || dp.onion_file == NULL) goto in_error;
donar_client(&ctx, algo, onion_file, exposed_ports, remote_ports); donar_client(&ctx, &dp);
} }
goto terminate; goto terminate;
in_error: in_error:
errored = 1; dp.errored = 1;
fprintf(stderr, "Usage as client : %s -c -a <algo> -o <onion service file> -e <exposed udp port> -r <remote udp port>\n", argv[0]); fprintf(stderr, "Usage as client : %s -c -a <algo> [-h] [-b] -o <onion service file> -e <exposed udp port> [-e ...]* -r <remote udp port> [-r ...]*\n", argv[0]);
fprintf(stderr, "Usage as server : %s -s -a <algo> -e <exposed udp port> -r <remote udp port>\n\n", argv[0]); fprintf(stderr, "Usage as server : %s -s -a <algo> [-h] [-b] -e <exposed udp port> [-e ...]* -r <remote udp port> [-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", 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: terminate:
if (onion_file != NULL) free(onion_file); if (dp.onion_file != NULL) free(dp.onion_file);
if (algo != NULL) free(algo); if (dp.algo != NULL) free(dp.algo);
g_ptr_array_free(exposed_ports, TRUE); g_ptr_array_free(dp.exposed_ports, TRUE);
g_ptr_array_free(remote_ports, TRUE); g_ptr_array_free(dp.remote_ports, TRUE);
return errored; return dp.errored;
} }

View file

@ -69,12 +69,11 @@ void init_socks5_sinks(struct donar_client_ctx* app_ctx) {
evt_core_add_cat(&app_ctx->evts, &template); evt_core_add_cat(&app_ctx->evts, &template);
} }
void donar_client(struct donar_client_ctx* ctx, char* algoname, void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp) {
char* onion_file, GPtrArray* exposed_ports, GPtrArray* remote_ports) {
struct algo_skel algo = {0}; struct algo_skel algo = {0};
evt_core_init (&(ctx->evts)); evt_core_init (&(ctx->evts));
init_algo(&ctx->evts, &algo, algoname); init_algo(&ctx->evts, &algo, dp->algo);
socks5_init (&ctx->evts); socks5_init (&ctx->evts);
init_socks5_sinks(ctx); init_socks5_sinks(ctx);
evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_co)); 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)); evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_write));
printf("--- Categories created\n"); 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"); printf("--- Onion services loaded\n");
for (int i = 0; i < CLIENT_PORT_SIZE; i++) { 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"); 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"); 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"); printf("--- Local UDP services are exposed\n");
evt_core_loop(&(ctx->evts)); evt_core_loop(&(ctx->evts));

View file

@ -19,5 +19,4 @@ struct donar_client_ctx {
} client_sock[CLIENT_PORT_SIZE]; } client_sock[CLIENT_PORT_SIZE];
}; };
void donar_client(struct donar_client_ctx* ctx, char* algoname, void donar_client(struct donar_client_ctx* ctx, struct donar_params* dp);
char* onion_file, GPtrArray* exposed_ports, GPtrArray* remote_ports);

View file

@ -87,3 +87,19 @@ socket_failed:
fprintf(stderr, "UDP socket init failed\n"); fprintf(stderr, "UDP socket init failed\n");
exit(EXIT_FAILURE); 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);
}

View file

@ -6,5 +6,12 @@
#include "evt_core.h" #include "evt_core.h"
#include "packet.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_remote(char* port, struct evt_core_ctx* evts);
void init_udp_exposed(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);

View file

@ -51,12 +51,11 @@ socket_create_err:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void donar_server(struct donar_server_ctx* ctx, char* algoname, void donar_server(struct donar_server_ctx* ctx, struct donar_params* dp) {
GPtrArray* exposed_ports, GPtrArray* remote_ports) {
struct algo_skel algo = {0}; struct algo_skel algo = {0};
evt_core_init (&(ctx->evts)); 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_tcp_co));
evt_core_add_cat (&(ctx->evts), &(algo.on_udp_read)); evt_core_add_cat (&(ctx->evts), &(algo.on_udp_read));
evt_core_add_cat (&(ctx->evts), &(algo.on_tcp_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); init_tcp_servers(ctx);
printf("--- TCP servers are listening\n"); 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"); 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"); printf("--- Local UDP services are exposed\n");
evt_core_loop (&(ctx->evts)); evt_core_loop (&(ctx->evts));

View file

@ -20,5 +20,4 @@ struct donar_server_ctx {
uint16_t ports[PORT_SIZE]; uint16_t ports[PORT_SIZE];
}; };
void donar_server(struct donar_server_ctx* ctx, char* algoname, void donar_server(struct donar_server_ctx* ctx, struct donar_params* dp);
GPtrArray* exposed_ports, GPtrArray* remote_ports);