summaryrefslogtreecommitdiffstats
path: root/src/fastd.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-03-16 08:08:16 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-03-16 08:08:16 +0100
commit78fe2cda0572433e40889bcd7d64dd22707bfdd0 (patch)
treecd4afdcba440e81ebd18992bdb21fa549aacdab4 /src/fastd.c
parentd9ed50094da3890b10872a4955dceb2817931f1f (diff)
downloadfastd-78fe2cda0572433e40889bcd7d64dd22707bfdd0.tar
fastd-78fe2cda0572433e40889bcd7d64dd22707bfdd0.zip
Move command line parsing to a new file
Diffstat (limited to 'src/fastd.c')
-rw-r--r--src/fastd.c245
1 files changed, 1 insertions, 244 deletions
diff --git a/src/fastd.c b/src/fastd.c
index 496df97..cae7c64 100644
--- a/src/fastd.c
+++ b/src/fastd.c
@@ -30,28 +30,16 @@
#include "peer.h"
#include "task.h"
-#include <config.h>
-
-#include <arpa/inet.h>
#include <fcntl.h>
-#include <getopt.h>
#include <linux/if_tun.h>
#include <net/if.h>
#include <poll.h>
-#include <stdarg.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
-extern fastd_method fastd_method_null;
-
-#ifdef WITH_METHOD_ECFXP
-extern fastd_method fastd_method_ec25519_fhmqvc_xsalsa20_poly1305;
-#endif
-
-
static void init_tuntap(fastd_context *ctx) {
struct ifreq ifr;
@@ -126,237 +114,6 @@ static void init_socket(fastd_context *ctx) {
}
}
-static void default_config(fastd_config *conf) {
- conf->loglevel = LOG_DEBUG;
-
- conf->peer_stale_time = 300;
- conf->peer_stale_time_temp = 30;
- conf->eth_addr_stale_time = 300;
-
- conf->ifname = NULL;
-
- memset(&conf->bind_addr_in, 0, sizeof(struct sockaddr_in));
- conf->bind_addr_in.sin_family = AF_UNSPEC;
- conf->bind_addr_in.sin_port = htons(1337);
- conf->bind_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
-
- memset(&conf->bind_addr_in6, 0, sizeof(struct sockaddr_in6));
- conf->bind_addr_in6.sin6_family = AF_UNSPEC;
- conf->bind_addr_in6.sin6_port = htons(1337);
- conf->bind_addr_in6.sin6_addr = in6addr_any;
-
- conf->mtu = 1500;
- conf->protocol = PROTOCOL_ETHERNET;
- conf->method = &fastd_method_null;
- conf->peers = NULL;
-}
-
-static void configure(fastd_context *ctx, fastd_config *conf, int argc, char *argv[]) {
- default_config(conf);
-
- fastd_peer_config **current_peer = &conf->peers;
-
- static const struct option long_options[] = {
- {"interface", required_argument, 0, 'i'},
- {"bind", required_argument, 0, 'b'},
- {"mtu", required_argument, 0, 'M'},
- {"protocol", required_argument, 0, 'P'},
- {"method", required_argument, 0, 'm'},
- {"peer", required_argument, 0, 'p'},
- {0, 0, 0, 0}
- };
-
- int c;
- int option_index = 0;
- long l;
- char *charptr;
- char *endptr;
- char *addrstr;
-
- bool v4_peers = false, v6_peers = false;
-
-
- conf->n_floating = 0;
-
- while ((c = getopt_long (argc, argv, "i:b:M:P:m:p:", long_options, &option_index)) != -1) {
- switch(c) {
- case 'i':
- conf->ifname = optarg;
- break;
-
- case 'b':
- if (optarg[0] == '[') {
- charptr = strchr(optarg, ']');
- if (!charptr || (charptr[1] != ':' && charptr[1] != '\0'))
- exit_error(ctx, "invalid bind address `%s'", optarg);
-
- addrstr = strndup(optarg+1, charptr-optarg-1);
-
- if (charptr[1] == ':')
- charptr++;
- else
- charptr = NULL;
- }
- else {
- charptr = strchr(optarg, ':');
- if (charptr) {
- addrstr = strndup(optarg, charptr-optarg);
- }
- else {
- addrstr = strdup(optarg);
- }
- }
-
- if (charptr) {
- l = strtol(charptr+1, &endptr, 10);
- if (*endptr || l > 65535)
- exit_error(ctx, "invalid bind port `%s'", charptr+1);
- }
-
- if (strcmp(addrstr, "any") == 0) {
- conf->bind_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
- conf->bind_addr_in.sin_port = htons(l);
-
- conf->bind_addr_in6.sin6_addr = in6addr_any;
- conf->bind_addr_in6.sin6_port = htons(l);
- }
- else if (optarg[0] == '[') {
- conf->bind_addr_in6.sin6_family = AF_INET6;
- if (inet_pton(AF_INET6, addrstr, &conf->bind_addr_in6.sin6_addr) != 1)
- exit_error(ctx, "invalid bind address `%s'", addrstr);
- conf->bind_addr_in6.sin6_port = htons(l);
- }
- else {
- conf->bind_addr_in.sin_family = AF_INET;
- if (inet_pton(AF_INET, addrstr, &conf->bind_addr_in.sin_addr) != 1)
- exit_error(ctx, "invalid bind address `%s'", addrstr);
- conf->bind_addr_in.sin_port = htons(l);
- }
-
- free(addrstr);
-
- break;
-
- case 'M':
- conf->mtu = strtol(optarg, &endptr, 10);
- if (*endptr || conf->mtu < 576)
- exit_error(ctx, "invalid mtu `%s'", optarg);
- break;
-
- case 'P':
- if (!strcmp(optarg, "ethernet"))
- conf->protocol = PROTOCOL_ETHERNET;
- else if (!strcmp(optarg, "ip"))
- conf->protocol = PROTOCOL_IP;
- else
- exit_error(ctx, "invalid protocol `%s'", optarg);
- break;
-
- case 'm':
- if (!strcmp(optarg, "null"))
- conf->method = &fastd_method_null;
-#ifdef WITH_METHOD_ECFXP
- if (!strcmp(optarg, "ecfxp"))
- conf->method = &fastd_method_ec25519_fhmqvc_xsalsa20_poly1305;
-#endif
- else
- exit_error(ctx, "invalid method `%s'", optarg);
- break;
-
- case 'p':
- *current_peer = malloc(sizeof(fastd_peer_config));
- (*current_peer)->next = NULL;
-
- memset(&(*current_peer)->address, 0, sizeof(fastd_peer_address));
- if (strcmp(optarg, "float") == 0) {
- (*current_peer)->address.sa.sa_family = AF_UNSPEC;
- conf->n_floating++;
- continue;
- }
-
- if (optarg[0] == '[') {
- charptr = strchr(optarg, ']');
- if (!charptr || (charptr[1] != ':' && charptr[1] != '\0'))
- exit_error(ctx, "invalid peer address `%s'", optarg);
-
- addrstr = strndup(optarg+1, charptr-optarg-1);
-
- if (charptr[1] == ':')
- charptr++;
- else
- charptr = NULL;
- }
- else {
- charptr = strchr(optarg, ':');
- if (charptr)
- addrstr = strndup(optarg, charptr-optarg);
- else
- addrstr = strdup(optarg);
- }
-
- if (charptr) {
- l = strtol(charptr+1, &endptr, 10);
- if (*endptr || l > 65535)
- exit_error(ctx, "invalid peer port `%s'", charptr+1);
- }
- else {
- l = 1337; /* default port */
- }
-
- if (optarg[0] == '[') {
- v6_peers = true;
- (*current_peer)->address.in6.sin6_family = AF_INET6;
- if (inet_pton(AF_INET6, addrstr, &(*current_peer)->address.in6.sin6_addr) != 1)
- exit_error(ctx, "invalid peer address `%s'", addrstr);
- (*current_peer)->address.in6.sin6_port = htons(l);
- }
- else {
- v4_peers = true;
- (*current_peer)->address.in.sin_family = AF_INET;
- if (inet_pton(AF_INET, addrstr, &(*current_peer)->address.in.sin_addr) != 1)
- exit_error(ctx, "invalid peer address `%s'", addrstr);
- (*current_peer)->address.in.sin_port = htons(l);
- }
-
- free(addrstr);
-
- current_peer = &(*current_peer)->next;
-
- break;
-
- case '?':
- exit(1);
-
- default:
- abort();
- }
- }
-
- if (conf->n_floating && conf->bind_addr_in.sin_family == AF_UNSPEC
- && conf->bind_addr_in6.sin6_family == AF_UNSPEC) {
- conf->bind_addr_in.sin_family = AF_INET;
- conf->bind_addr_in6.sin6_family = AF_INET6;
- }
- else if (v4_peers) {
- conf->bind_addr_in.sin_family = AF_INET;
- }
- else if (v6_peers) {
- conf->bind_addr_in6.sin6_family = AF_INET6;
- }
-
- bool ok = true;
- if (conf->protocol == PROTOCOL_IP && (!conf->peers || conf->peers->next)) {
- pr_error(ctx, "for protocol `ip' exactly one peer must be configured");
- ok = false;
- }
-
- if (ok)
- ok = conf->method->check_config(ctx, conf);
-
- if (!ok)
- exit_error(ctx, "config error");
-}
-
static void init_peers(fastd_context *ctx) {
fastd_peer_config *peer_conf;
for (peer_conf = ctx->conf->peers; peer_conf; peer_conf = peer_conf->next)
@@ -631,7 +388,7 @@ int main(int argc, char *argv[]) {
memset(&ctx, 0, sizeof(ctx));
fastd_config conf;
- configure(&ctx, &conf, argc, argv);
+ fastd_configure(&ctx, &conf, argc, argv);
ctx.conf = &conf;
update_time(&ctx);