tor_multipath_voip/src/donar.c

82 lines
2.4 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
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-03-28 10:47:14 +00:00
struct donar_params dp;
donar_init_params (&dp);
2019-02-13 14:32:38 +00:00
2019-03-28 14:35:48 +00:00
while ((dp.opt = getopt(argc, argv, "cse:r:o:a:bh")) != -1) {
2019-03-28 10:47:14 +00:00
switch(dp.opt) {
2019-02-11 09:23:38 +00:00
case 's':
2019-03-28 10:47:14 +00:00
dp.is_server = 1;
2019-02-11 09:23:38 +00:00
break;
2019-02-19 18:15:37 +00:00
case 'e':
2019-03-28 10:47:14 +00:00
dp.port = strdup(optarg);
if (dp.port == NULL) goto terminate;
g_ptr_array_add (dp.exposed_ports, dp.port);
2019-02-19 18:15:37 +00:00
break;
case 'r':
2019-03-28 10:47:14 +00:00
dp.port = strdup(optarg);
if (dp.port == NULL) goto terminate;
g_ptr_array_add (dp.remote_ports, dp.port);
2019-02-11 22:40:37 +00:00
break;
case 'o':
2019-03-28 10:47:14 +00:00
dp.onion_file = strdup(optarg);
2019-02-11 22:40:37 +00:00
break;
2019-02-11 09:23:38 +00:00
case 'c':
2019-03-28 10:47:14 +00:00
dp.is_client = 1;
2019-02-11 09:23:38 +00:00
break;
2019-02-13 14:32:38 +00:00
case 'a':
2019-03-28 10:47:14 +00:00
dp.algo = strdup(optarg);
break;
case 'h':
dp.is_healing = 1;
break;
case 'b':
dp.is_waiting_bootstrap = 1;
2019-02-13 14:32:38 +00:00
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-03-28 10:47:14 +00:00
if (!(dp.is_server ^ dp.is_client)) goto in_error;
if (dp.algo == NULL) goto in_error;
2019-02-11 09:23:38 +00:00
2019-03-28 10:47:14 +00:00
if (dp.is_server) {
2019-02-11 15:23:20 +00:00
struct donar_server_ctx ctx;
2019-03-28 10:47:14 +00:00
if (dp.exposed_ports->len < 1 && dp.remote_ports->len < 1) goto in_error;
donar_server(&ctx, &dp);
} else if (dp.is_client) {
struct donar_client_ctx ctx;
2019-03-28 10:47:14 +00:00
if ((dp.exposed_ports->len < 1 && dp.remote_ports->len < 1) || dp.onion_file == NULL) goto in_error;
donar_client(&ctx, &dp);
2019-02-11 15:23:20 +00:00
}
2019-02-13 14:32:38 +00:00
goto terminate;
in_error:
2019-03-28 10:47:14 +00:00
dp.errored = 1;
fprintf(stderr, "Usage as client : %s -c -a <algo> [-h] [-b] -o <onion service file> -e <exposed udp port> [-e ...]* -r <remote udp port> [-r ...]*\n", argv[0]);
fprintf(stderr, "Usage as server : %s -s -a <algo> [-h] [-b] -e <exposed udp port> [-e ...]* -r <remote udp port> [-r ...]*\n\n", argv[0]);
2019-02-19 18:15:37 +00:00
fprintf(stderr, "Passed parameters: client=%d, server=%d, algo=%s, exposed_ports=%d, remote_ports=%d, onion_file=%s\n",
2019-03-28 10:47:14 +00:00
dp.is_client, dp.is_server, dp.algo, dp.exposed_ports->len, dp.remote_ports->len, dp.onion_file);
2019-02-13 14:32:38 +00:00
terminate:
2019-03-28 10:47:14 +00:00
if (dp.onion_file != NULL) free(dp.onion_file);
if (dp.algo != NULL) free(dp.algo);
g_ptr_array_free(dp.exposed_ports, TRUE);
g_ptr_array_free(dp.remote_ports, TRUE);
2019-02-11 09:23:38 +00:00
2019-03-28 10:47:14 +00:00
return dp.errored;
2019-02-08 13:28:39 +00:00
}