tor_multipath_voip/src/donar.c
2019-02-15 18:09:51 +01:00

82 lines
2.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <gmodule.h>
#include "algo_skel.h"
#include "donar_client.h"
#include "donar_server.h"
void free_port (void* ptr) {
free(ptr);
}
int main(int argc, char** argv) {
printf("~ Donar ~\n");
int opt, is_server, is_client, errored;
char *port, *onion_file, *algo;
onion_file = NULL;
algo = NULL;
is_server = 0;
is_client = 0;
errored = 0;
GPtrArray* ports = g_ptr_array_new_with_free_func (free_port);
while ((opt = getopt(argc, argv, "csh:p:o:a:")) != -1) {
switch(opt) {
case 's':
is_server = 1;
break;
case 'p':
port = strdup(optarg);
if (port == NULL) goto terminate;
g_ptr_array_add (ports, port);
break;
case 'o':
onion_file = strdup(optarg);
break;
case 'c':
is_client = 1;
break;
case 'a':
algo = strdup(optarg);
break;
default:
goto in_error;
}
}
if (!(is_server ^ is_client)) goto in_error;
if (algo == NULL) goto in_error;
struct algo_skel as;
init_algo(&as, algo);
if (is_server) {
struct donar_server_ctx ctx;
if (ports->len < 1) goto in_error;
donar_server(&ctx, &as, ports);
} else if (is_client) {
struct donar_client_ctx ctx;
if (ports->len < 1 || onion_file == NULL) goto in_error;
donar_client(&ctx, &as, onion_file, ports);
}
goto terminate;
in_error:
errored = 1;
fprintf(stderr, "Usage as client : %s -c -a <algo> -p <udp port 1> -p <udp port 2> -o <onion service file>\n", argv[0]);
fprintf(stderr, "Usage as server : %s -s -a <algo> -p <udp port 1> -p <udp port 2>\n\n", argv[0]);
fprintf(stderr, "Passed parameters: client=%d, server=%d, algo=%s, registered_ports=%d, onion_file=%s\n",
is_client, is_server, algo, ports->len, onion_file);
terminate:
if (onion_file != NULL) free(onion_file);
if (port != NULL) free(port);
if (algo != NULL) free(algo);
return errored;
}