tor_multipath_voip/src/donar.c
2019-03-06 16:39:39 +01:00

89 lines
2.6 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) {
setvbuf(stdout, NULL, _IONBF, 0);
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* 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) {
switch(opt) {
case 's':
is_server = 1;
break;
case 'e':
port = strdup(optarg);
if (port == NULL) goto terminate;
g_ptr_array_add (exposed_ports, port);
break;
case 'r':
port = strdup(optarg);
if (port == NULL) goto terminate;
g_ptr_array_add (remote_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 = {0};
init_algo(&as, algo);
if (is_server) {
struct donar_server_ctx ctx;
if (exposed_ports->len < 1 && remote_ports->len < 1) goto in_error;
donar_server(&ctx, &as, exposed_ports, remote_ports);
} else if (is_client) {
struct donar_client_ctx ctx;
if ((exposed_ports->len < 1 && remote_ports->len < 1) || onion_file == NULL) goto in_error;
donar_client(&ctx, &as, onion_file, exposed_ports, remote_ports);
}
goto terminate;
in_error:
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 server : %s -s -a <algo> -e <exposed udp port> -r <remote udp port>\n\n", argv[0]);
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);
terminate:
if (onion_file != NULL) free(onion_file);
if (algo != NULL) free(algo);
g_ptr_array_free(exposed_ports, TRUE);
g_ptr_array_free(remote_ports, TRUE);
return errored;
}