From 6ce20e2bb6660694e995d890f2846fc26c96c8f7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 25 Mar 2012 00:16:20 +0100 Subject: Add keypair config --- src/config.c | 20 ++++++++++++++------ src/config.l | 2 ++ src/config.y | 13 ++++++++++++- src/fastd.h | 3 ++- src/peer.h | 1 + 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/config.c b/src/config.c index 13fb8b5..12b92b3 100644 --- a/src/config.c +++ b/src/config.c @@ -65,6 +65,7 @@ static void default_config(fastd_config *conf) { conf->mtu = 1500; conf->mode = MODE_TAP; conf->protocol = &fastd_protocol_null; + conf->secret = NULL; conf->peers = NULL; } @@ -90,16 +91,22 @@ static bool config_match(const char *opt, ...) { static void fastd_read_config(fastd_context *ctx, fastd_config *conf, const char *filename) { yyscan_t scanner; FILE *file; + bool use_stdin = !strcmp(filename, "-"); - file = fopen(filename, "r"); - fastd_config_lex_init(&scanner); + if (use_stdin) + file = stdin; + else + file = fopen(filename, "r"); + fastd_config_lex_init(&scanner); fastd_config_set_in(file, scanner); fastd_config_parse(ctx, conf, scanner); fastd_config_lex_destroy(scanner); - fclose(file); + + if (!use_stdin) + fclose(file); } #define IF_OPTION(args...) if(config_match(argv[i], args, NULL) && (++i)) @@ -131,7 +138,8 @@ void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *con } IF_OPTION_ARG("-i", "--interface") { - conf->ifname = arg; + free(conf->ifname); + conf->ifname = strdup(arg); continue; } @@ -160,7 +168,7 @@ void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *con if (charptr) { l = strtol(charptr+1, &endptr, 10); - if (*endptr || l > 65535) + if (*endptr || l < 0 || l > 65535) exit_error(ctx, "invalid bind port `%s'", charptr+1); } else { @@ -255,7 +263,7 @@ void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *con if (charptr) { l = strtol(charptr+1, &endptr, 10); - if (*endptr || l > 65535) + if (*endptr || l < 0 || l > 65535) exit_error(ctx, "invalid peer port `%s'", charptr+1); } else { diff --git a/src/config.l b/src/config.l index 5e5e666..1173d44 100644 --- a/src/config.l +++ b/src/config.l @@ -20,6 +20,8 @@ mode { yylval->str = yytext; return TOK_MODE; } protocol { yylval->str = yytext; return TOK_PROTOCOL; } peer { yylval->str = yytext; return TOK_PEER; } address { yylval->str = yytext; return TOK_ADDRESS; } +secret { yylval->str = yytext; return TOK_SECRET; } +key { yylval->str = yytext; return TOK_KEY; } [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} { if (!inet_pton(AF_INET, yytext, &yylval->addr)) { diff --git a/src/config.y b/src/config.y index 9a0d333..02a9529 100644 --- a/src/config.y +++ b/src/config.y @@ -29,6 +29,8 @@ %token TOK_PROTOCOL %token TOK_PEER %token TOK_ADDRESS +%token TOK_SECRET +%token TOK_KEY %token TOK_ADDR %token TOK_ADDR6 @@ -73,10 +75,11 @@ statement: TOK_INTERFACE interface ';' | TOK_MTU mtu ';' | TOK_MODE mode ';' | TOK_PROTOCOL protocol ';' + | TOK_SECRET secret ';' | TOK_PEER peer '{' peer_conf '}' ; -interface: TOK_STRING { conf->ifname = strdup($1); } +interface: TOK_STRING { free(conf->ifname); conf->ifname = strdup($1); } ; bind: TOK_ADDR maybe_port { @@ -116,6 +119,9 @@ protocol: maybe_string { } ; +secret: TOK_STRING { free(conf->secret); conf->secret = strdup($1); } + ; + peer: maybe_string { fastd_peer_config *current_peer = malloc(sizeof(fastd_peer_config)); current_peer->next = conf->peers; @@ -132,6 +138,7 @@ peer_conf: peer_conf peer_statement ; peer_statement: TOK_ADDRESS peer_address ';' + | TOK_KEY peer_key ';' ; peer_address: TOK_ADDR maybe_port_default { @@ -146,6 +153,10 @@ peer_address: TOK_ADDR maybe_port_default { } ; +peer_key: TOK_STRING { free(conf->peers->key); conf->peers->key = strdup($1); } + ; + + maybe_string: TOK_STRING | { $$[0] = '\0'; } ; diff --git a/src/fastd.h b/src/fastd.h index d911a71..c453790 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -81,7 +81,7 @@ struct _fastd_config { unsigned peer_stale_time_temp; unsigned eth_addr_stale_time; - const char *ifname; + char *ifname; struct sockaddr_in bind_addr_in; struct sockaddr_in6 bind_addr_in6; @@ -90,6 +90,7 @@ struct _fastd_config { fastd_mode mode; fastd_protocol *protocol; + char *secret; fastd_peer_config *peers; diff --git a/src/peer.h b/src/peer.h index 39fcc02..8b3b3ac 100644 --- a/src/peer.h +++ b/src/peer.h @@ -56,6 +56,7 @@ struct _fastd_peer_config { fastd_peer_config *next; fastd_peer_address address; + char *key; }; struct _fastd_peer_eth_addr { -- cgit v1.2.3