tor_multipath_voip/src/donar.c

87 lines
2.5 KiB
C
Raw Normal View History

2019-02-08 13:28:39 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
2019-02-15 17:09:51 +00:00
#include <gmodule.h>
2019-02-11 21:40:00 +00:00
#include "algo_skel.h"
2019-02-11 09:23:38 +00:00
#include "donar_client.h"
#include "donar_server.h"
2019-02-08 13:28:39 +00:00
2019-02-15 17:09:51 +00:00
void free_port (void* ptr) {
free(ptr);
}
2019-02-08 13:28:39 +00:00
int main(int argc, char** argv) {
setvbuf(stdout, NULL, _IONBF, 0);
2019-02-08 13:28:39 +00:00
printf("~ Donar ~\n");
2019-02-11 09:23:38 +00:00
2019-02-13 14:32:38 +00:00
int opt, is_server, is_client, errored;
2019-02-15 17:09:51 +00:00
char *port, *onion_file, *algo;
2019-02-11 22:40:37 +00:00
onion_file = NULL;
2019-02-13 14:32:38 +00:00
algo = NULL;
2019-02-11 22:40:37 +00:00
is_server = 0;
is_client = 0;
2019-02-13 14:32:38 +00:00
errored = 0;
2019-02-19 18:15:37 +00:00
GPtrArray* remote_ports = g_ptr_array_new_with_free_func (free_port);
GPtrArray* exposed_ports = g_ptr_array_new_with_free_func (free_port);
2019-02-13 14:32:38 +00:00
2019-02-19 18:15:37 +00:00
while ((opt = getopt(argc, argv, "csh:e:r:o:a:")) != -1) {
2019-02-11 09:23:38 +00:00
switch(opt) {
case 's':
is_server = 1;
break;
2019-02-19 18:15:37 +00:00
case 'e':
2019-02-11 22:40:37 +00:00
port = strdup(optarg);
2019-02-15 17:09:51 +00:00
if (port == NULL) goto terminate;
2019-02-19 18:15:37 +00:00
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);
2019-02-11 22:40:37 +00:00
break;
case 'o':
onion_file = strdup(optarg);
break;
2019-02-11 09:23:38 +00:00
case 'c':
is_client = 1;
break;
2019-02-13 14:32:38 +00:00
case 'a':
algo = strdup(optarg);
break;
2019-02-11 09:23:38 +00:00
default:
2019-02-13 14:32:38 +00:00
goto in_error;
2019-02-11 09:23:38 +00:00
}
}
2019-02-13 14:32:38 +00:00
if (!(is_server ^ is_client)) goto in_error;
if (algo == NULL) goto in_error;
2019-02-11 09:23:38 +00:00
2019-02-11 15:23:20 +00:00
if (is_server) {
struct donar_server_ctx ctx;
2019-02-20 15:58:31 +00:00
if (exposed_ports->len < 1 && remote_ports->len < 1) goto in_error;
2019-03-19 16:09:42 +00:00
donar_server(&ctx, algo, exposed_ports, remote_ports);
2019-02-11 15:23:20 +00:00
} else if (is_client) {
struct donar_client_ctx ctx;
2019-02-20 15:58:31 +00:00
if ((exposed_ports->len < 1 && remote_ports->len < 1) || onion_file == NULL) goto in_error;
2019-03-19 16:09:42 +00:00
donar_client(&ctx, algo, onion_file, exposed_ports, remote_ports);
2019-02-11 15:23:20 +00:00
}
2019-02-13 14:32:38 +00:00
goto terminate;
in_error:
errored = 1;
2019-02-19 18:15:37 +00:00
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);
2019-02-13 14:32:38 +00:00
terminate:
if (onion_file != NULL) free(onion_file);
if (algo != NULL) free(algo);
2019-02-19 18:15:37 +00:00
g_ptr_array_free(exposed_ports, TRUE);
g_ptr_array_free(remote_ports, TRUE);
2019-02-11 09:23:38 +00:00
2019-02-13 14:32:38 +00:00
return errored;
2019-02-08 13:28:39 +00:00
}