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.c
src/algo_skel.h
src/algo_skel.c
src/algo_naive.c
src/utils.h
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 <stdio.h>
#include <errno.h>
#include <string.h>
#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"
}
};

View file

@ -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 <udp port> -o <onion service file> | -s -h <udp host> -p <udp port>]\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 <algo> -p <udp port> -o <onion service file>\n", argv[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];
};
static char* rep_msg[9] = {
static char* rep_msg[] = {
"Succeeded",
"General SOCKS server failure",
"Connection not allowed by ruleset",