From 8fee2b43e8b015191404627dd21e72e2c51f8254 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 13 Feb 2019 15:32:38 +0100 Subject: [PATCH] Refactor to support multi algo --- CMakeLists.txt | 1 + src/algo_skel.c | 13 ++++++++++++ src/algo_skel.h | 16 +++++++++++++++ src/donar.c | 53 ++++++++++++++++++++++++++++--------------------- src/socks5.h | 2 +- 5 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 src/algo_skel.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f6a9952..6808b0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ list(APPEND CSOURCES src/evt_core.h src/evt_core.c src/algo_skel.h + src/algo_skel.c src/algo_naive.c src/utils.h src/utils.c diff --git a/src/algo_skel.c b/src/algo_skel.c new file mode 100644 index 0000000..9fa5226 --- /dev/null +++ b/src/algo_skel.c @@ -0,0 +1,13 @@ +#include "algo_skel.h" + +void init_algo(struct algo_skel* as, char* name) { + for (int i = 0; i < sizeof(available_algo) / sizeof(available_algo[0]); i++) { + if (strcmp(available_algo[i].name, name) == 0) { + available_algo[i].init(as); + return; + } + } + fprintf(stderr, "Algorithm %s has not been found\n", name); + exit(EXIT_FAILURE); +} + diff --git a/src/algo_skel.h b/src/algo_skel.h index cf7a67f..4dbdfba 100644 --- a/src/algo_skel.h +++ b/src/algo_skel.h @@ -2,6 +2,7 @@ #include #include #include +#include #include "evt_core.h" #include "utils.h" @@ -13,4 +14,19 @@ struct algo_skel { struct evt_core_cat on_tcp_co; }; +typedef void (*algo_init)(struct algo_skel* as); + +void init_algo(struct algo_skel* as, char* name); void algo_naive(struct algo_skel* as); + +struct algo_desc { + algo_init init; + char* name; +}; + +static struct algo_desc available_algo[] = { + { + .init = algo_naive, + .name = "naive" + } +}; diff --git a/src/donar.c b/src/donar.c index caffc56..1567d62 100644 --- a/src/donar.c +++ b/src/donar.c @@ -10,14 +10,17 @@ int main(int argc, char** argv) { printf("~ Donar ~\n"); - int opt, is_server, is_client; - char *host, *port, *onion_file; + int opt, is_server, is_client, errored; + char *host, *port, *onion_file, *algo; host = NULL; port = NULL; onion_file = NULL; + algo = NULL; is_server = 0; is_client = 0; - while ((opt = getopt(argc, argv, "csh:p:o:")) != -1) { + errored = 0; + + while ((opt = getopt(argc, argv, "csh:p:o:a:")) != -1) { switch(opt) { case 's': is_server = 1; @@ -34,39 +37,43 @@ int main(int argc, char** argv) { case 'c': is_client = 1; break; + case 'a': + algo = strdup(optarg); + break; default: - fprintf(stderr, "Usage: %s [-c -p -o | -s -h -p ]\n", argv[0]); - exit(EXIT_FAILURE); + goto in_error; } } - if (!(is_server ^ is_client)) { - fprintf(stderr, "You need to choose between client and server\n"); - exit(EXIT_FAILURE); - } + if (!(is_server ^ is_client)) goto in_error; + if (algo == NULL) goto in_error; struct algo_skel as; - algo_naive (&as); + init_algo(&as, algo); if (is_server) { struct donar_server_ctx ctx; - if (host == NULL || port == NULL) { - fprintf(stderr, "You need to set a host -h and a port -p\n"); - exit(EXIT_FAILURE); - } - printf("params: %s:%s\n", host, port); + if (host == NULL || port == NULL) goto in_error; donar_server(&ctx, &as, host, port); } else if (is_client) { struct donar_client_ctx ctx; - if (port == NULL || onion_file == NULL) { - fprintf(stderr, "You need to set an onion_file -o and a port -p\n"); - exit(EXIT_FAILURE); - } + if (port == NULL || onion_file == NULL) goto in_error; donar_client(&ctx, &as, onion_file, port); } + goto terminate; - free(onion_file); - free(port); - free(host); - return 0; +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); + +terminate: + if (host != NULL) free(host); + if (onion_file != NULL) free(onion_file); + if (port != NULL) free(port); + if (algo != NULL) free(algo); + + return errored; } diff --git a/src/socks5.h b/src/socks5.h index b67dc71..4cd6380 100644 --- a/src/socks5.h +++ b/src/socks5.h @@ -29,7 +29,7 @@ union socks5_addr { uint8_t ipv6[16]; }; -static char* rep_msg[9] = { +static char* rep_msg[] = { "Succeeded", "General SOCKS server failure", "Connection not allowed by ruleset",