2016-02-22 16:49:43 -08:00
|
|
|
#include <assert.h>
|
2016-02-14 02:12:35 +00:00
|
|
|
#include <getopt.h>
|
2016-02-22 16:49:43 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2016-02-17 01:16:11 +00:00
|
|
|
#include <string.h>
|
2016-02-21 13:57:35 -08:00
|
|
|
#include <unistd.h>
|
2016-02-17 17:19:57 -08:00
|
|
|
|
2016-02-16 03:42:41 +00:00
|
|
|
#include "beast.h"
|
2016-02-22 16:36:27 -08:00
|
|
|
#include "hex.h"
|
2016-02-22 16:49:43 -08:00
|
|
|
#include "incoming.h"
|
|
|
|
|
#include "json.h"
|
2016-02-22 16:36:27 -08:00
|
|
|
#include "opts.h"
|
2016-02-22 16:49:43 -08:00
|
|
|
#include "outgoing.h"
|
|
|
|
|
#include "peer.h"
|
2016-02-22 16:36:27 -08:00
|
|
|
#include "rand.h"
|
2016-02-22 16:49:43 -08:00
|
|
|
#include "receive.h"
|
|
|
|
|
#include "send.h"
|
2016-02-22 21:53:25 -08:00
|
|
|
#include "server.h"
|
2016-02-22 16:49:43 -08:00
|
|
|
#include "stats.h"
|
2016-02-22 14:45:18 -08:00
|
|
|
#include "wakeup.h"
|
2016-02-20 23:22:00 -08:00
|
|
|
|
2016-02-17 16:40:09 -08:00
|
|
|
static void print_usage(const char *name) {
|
2016-02-17 01:16:11 +00:00
|
|
|
fprintf(stderr,
|
2016-02-17 16:04:11 -08:00
|
|
|
"\n"
|
2016-02-17 01:16:11 +00:00
|
|
|
"Usage: %s [OPTION]...\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Options:\n"
|
|
|
|
|
"\t--help\n"
|
|
|
|
|
"\t--dump=FORMAT\n"
|
2016-02-17 17:19:57 -08:00
|
|
|
"\t--connect-receive=HOST/PORT\n"
|
|
|
|
|
"\t--connect-send=FORMAT=HOST/PORT\n"
|
|
|
|
|
"\t--listen-receive=[HOST/]PORT\n"
|
|
|
|
|
"\t--listen-send=FORMAT=[HOST/]PORT\n"
|
2016-02-17 16:40:09 -08:00
|
|
|
, name);
|
2016-02-17 17:19:57 -08:00
|
|
|
receive_print_usage();
|
|
|
|
|
send_print_usage();
|
2016-02-17 16:04:11 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 15:41:27 -08:00
|
|
|
static bool parse_opts(int argc, char *argv[]) {
|
2016-02-17 01:16:11 +00:00
|
|
|
static struct option long_options[] = {
|
2016-02-17 17:19:57 -08:00
|
|
|
{"dump", required_argument, 0, 'd'},
|
|
|
|
|
{"connect-receive", required_argument, 0, 'c'},
|
|
|
|
|
{"connect-send", required_argument, 0, 's'},
|
|
|
|
|
{"listen-receive", required_argument, 0, 'l'},
|
|
|
|
|
{"listen-send", required_argument, 0, 'm'},
|
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
|
{0, 0, 0, 0 },
|
2016-02-17 01:16:11 +00:00
|
|
|
};
|
|
|
|
|
|
2016-02-14 02:12:35 +00:00
|
|
|
int opt;
|
2016-02-17 01:16:11 +00:00
|
|
|
while ((opt = getopt_long_only(argc, argv, "", long_options, NULL)) != -1) {
|
2016-02-17 16:04:11 -08:00
|
|
|
bool (*handler)(char *) = NULL;
|
2016-02-14 02:12:35 +00:00
|
|
|
switch (opt) {
|
2016-02-15 21:12:26 +00:00
|
|
|
case 'd':
|
2016-02-20 23:22:00 -08:00
|
|
|
handler = opts_add_dump;
|
2016-02-14 02:12:35 +00:00
|
|
|
break;
|
|
|
|
|
|
2016-02-17 17:19:57 -08:00
|
|
|
case 'c':
|
2016-02-20 23:22:00 -08:00
|
|
|
handler = opts_add_connect_receive;
|
2016-02-17 17:19:57 -08:00
|
|
|
break;
|
2016-02-17 01:16:11 +00:00
|
|
|
|
2016-02-17 17:19:57 -08:00
|
|
|
case 's':
|
2016-02-20 23:22:00 -08:00
|
|
|
handler = opts_add_connect_send;
|
2016-02-17 13:56:13 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'l':
|
2016-02-20 23:22:00 -08:00
|
|
|
handler = opts_add_listen_receive;
|
2016-02-17 13:41:33 -08:00
|
|
|
break;
|
|
|
|
|
|
2016-02-17 17:19:57 -08:00
|
|
|
case 'm':
|
2016-02-20 23:22:00 -08:00
|
|
|
handler = opts_add_listen_send;
|
2016-02-17 17:19:57 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'h':
|
2016-02-14 02:12:35 +00:00
|
|
|
default:
|
2016-02-17 16:40:09 -08:00
|
|
|
print_usage(argv[0]);
|
2016-02-16 02:28:05 +00:00
|
|
|
return false;
|
2016-02-14 02:12:35 +00:00
|
|
|
}
|
2016-02-17 16:04:11 -08:00
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
|
if (!handler(optarg)) {
|
2016-02-20 23:22:00 -08:00
|
|
|
fprintf(stderr, "Invalid flag value: %s\n", optarg);
|
2016-02-17 16:40:09 -08:00
|
|
|
print_usage(argv[0]);
|
2016-02-17 16:04:11 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-14 02:12:35 +00:00
|
|
|
}
|
2016-02-17 01:16:11 +00:00
|
|
|
|
|
|
|
|
if (optind != argc) {
|
|
|
|
|
fprintf(stderr, "Not a flag: %s\n", argv[optind]);
|
2016-02-17 16:40:09 -08:00
|
|
|
print_usage(argv[0]);
|
2016-02-17 01:16:11 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 02:28:05 +00:00
|
|
|
return true;
|
2016-02-14 02:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2016-02-21 13:57:35 -08:00
|
|
|
assert(!close(0));
|
|
|
|
|
|
2016-02-14 22:26:34 +00:00
|
|
|
hex_init();
|
2016-02-21 15:56:07 -08:00
|
|
|
rand_init();
|
2016-02-22 21:53:25 -08:00
|
|
|
server_init();
|
2016-02-18 09:33:32 -08:00
|
|
|
wakeup_init();
|
2016-02-22 16:27:44 -08:00
|
|
|
peer_init();
|
2016-02-18 09:33:32 -08:00
|
|
|
|
2016-02-17 17:19:57 -08:00
|
|
|
send_init();
|
2016-02-18 09:33:32 -08:00
|
|
|
|
2016-02-16 03:42:41 +00:00
|
|
|
beast_init();
|
2016-02-16 02:28:05 +00:00
|
|
|
json_init();
|
2016-02-17 08:30:32 +00:00
|
|
|
stats_init();
|
2016-02-14 02:12:35 +00:00
|
|
|
|
2016-02-17 15:41:27 -08:00
|
|
|
if (!parse_opts(argc, argv)) {
|
2016-02-16 02:28:05 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 15:41:27 -08:00
|
|
|
peer_loop();
|
2016-02-20 23:22:00 -08:00
|
|
|
|
2016-02-21 15:56:07 -08:00
|
|
|
rand_cleanup();
|
2016-02-20 23:56:40 -08:00
|
|
|
wakeup_cleanup();
|
2016-02-20 23:22:00 -08:00
|
|
|
send_cleanup();
|
2016-02-20 23:56:40 -08:00
|
|
|
|
2016-02-21 13:57:35 -08:00
|
|
|
incoming_cleanup();
|
|
|
|
|
outgoing_cleanup();
|
|
|
|
|
|
2016-02-14 02:12:35 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|