Refactor to support multi algo

This commit is contained in:
Quentin Dufour 2019-02-13 15:32:38 +01:00
parent 670b64e56c
commit 8fee2b43e8
5 changed files with 61 additions and 24 deletions

View file

@ -17,6 +17,7 @@ list(APPEND CSOURCES
src/evt_core.h src/evt_core.h
src/evt_core.c src/evt_core.c
src/algo_skel.h src/algo_skel.h
src/algo_skel.c
src/algo_naive.c src/algo_naive.c
src/utils.h src/utils.h
src/utils.c src/utils.c

13
src/algo_skel.c Normal file
View file

@ -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);
}

View file

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include "evt_core.h" #include "evt_core.h"
#include "utils.h" #include "utils.h"
@ -13,4 +14,19 @@ struct algo_skel {
struct evt_core_cat on_tcp_co; 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); 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"
}
};

View file

@ -10,14 +10,17 @@
int main(int argc, char** argv) { int main(int argc, char** argv) {
printf("~ Donar ~\n"); printf("~ Donar ~\n");
int opt, is_server, is_client; int opt, is_server, is_client, errored;
char *host, *port, *onion_file; char *host, *port, *onion_file, *algo;
host = NULL; host = NULL;
port = NULL; port = NULL;
onion_file = NULL; onion_file = NULL;
algo = NULL;
is_server = 0; is_server = 0;
is_client = 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) { switch(opt) {
case 's': case 's':
is_server = 1; is_server = 1;
@ -34,39 +37,43 @@ int main(int argc, char** argv) {
case 'c': case 'c':
is_client = 1; is_client = 1;
break; break;
case 'a':
algo = strdup(optarg);
break;
default: default:
fprintf(stderr, "Usage: %s [-c -p <udp port> -o <onion service file> | -s -h <udp host> -p <udp port>]\n", argv[0]); goto in_error;
exit(EXIT_FAILURE);
} }
} }
if (!(is_server ^ is_client)) { if (!(is_server ^ is_client)) goto in_error;
fprintf(stderr, "You need to choose between client and server\n"); if (algo == NULL) goto in_error;
exit(EXIT_FAILURE);
}
struct algo_skel as; struct algo_skel as;
algo_naive (&as); init_algo(&as, algo);
if (is_server) { if (is_server) {
struct donar_server_ctx ctx; struct donar_server_ctx ctx;
if (host == NULL || port == NULL) { if (host == NULL || port == NULL) goto in_error;
fprintf(stderr, "You need to set a host -h and a port -p\n");
exit(EXIT_FAILURE);
}
printf("params: %s:%s\n", host, port);
donar_server(&ctx, &as, host, port); donar_server(&ctx, &as, host, port);
} else if (is_client) { } else if (is_client) {
struct donar_client_ctx ctx; struct donar_client_ctx ctx;
if (port == NULL || onion_file == NULL) { if (port == NULL || onion_file == NULL) goto in_error;
fprintf(stderr, "You need to set an onion_file -o and a port -p\n");
exit(EXIT_FAILURE);
}
donar_client(&ctx, &as, onion_file, port); donar_client(&ctx, &as, onion_file, port);
} }
goto terminate;
free(onion_file); in_error:
free(port); errored = 1;
free(host); fprintf(stderr, "Usage as client : %s -c -a <algo> -p <udp port> -o <onion service file>\n", argv[0]);
return 0; fprintf(stderr, "Usage as server : %s -s -a <algo> -h <udp host> -p <udp port>\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;
} }

View file

@ -29,7 +29,7 @@ union socks5_addr {
uint8_t ipv6[16]; uint8_t ipv6[16];
}; };
static char* rep_msg[9] = { static char* rep_msg[] = {
"Succeeded", "Succeeded",
"General SOCKS server failure", "General SOCKS server failure",
"Connection not allowed by ruleset", "Connection not allowed by ruleset",