diff options
-rw-r--r-- | src/config.c | 20 | ||||
-rw-r--r-- | src/config.l | 45 | ||||
-rw-r--r-- | src/config.y | 21 | ||||
-rw-r--r-- | src/fastd.c | 2 | ||||
-rw-r--r-- | src/fastd.h | 6 | ||||
-rw-r--r-- | src/handshake.c | 2 | ||||
-rw-r--r-- | src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c | 19 | ||||
-rw-r--r-- | src/protocol_null.c | 14 |
8 files changed, 73 insertions, 56 deletions
diff --git a/src/config.c b/src/config.c index 12b92b3..d584264 100644 --- a/src/config.c +++ b/src/config.c @@ -90,6 +90,10 @@ static bool config_match(const char *opt, ...) { static void fastd_read_config(fastd_context *ctx, fastd_config *conf, const char *filename) { yyscan_t scanner; + fastd_config_pstate *ps; + int token; + YYSTYPE token_val; + FILE *file; bool use_stdin = !strcmp(filename, "-"); @@ -98,12 +102,20 @@ static void fastd_read_config(fastd_context *ctx, fastd_config *conf, const char else file = fopen(filename, "r"); - fastd_config_lex_init(&scanner); - fastd_config_set_in(file, scanner); + fastd_config_yylex_init(&scanner); + fastd_config_yyset_in(file, scanner); + + ps = fastd_config_pstate_new(); + + do { + token = fastd_config_yylex(&token_val, scanner); - fastd_config_parse(ctx, conf, scanner); + if (token < 0) + exit_error(ctx, "config error: %s", token_val.str); + } while(fastd_config_push_parse(ps, token, &token_val, ctx, conf) == YYPUSH_MORE); - fastd_config_lex_destroy(scanner); + fastd_config_pstate_delete(ps); + fastd_config_yylex_destroy(scanner); if (!use_stdin) fclose(file); diff --git a/src/config.l b/src/config.l index 1173d44..54d3447 100644 --- a/src/config.l +++ b/src/config.l @@ -1,4 +1,4 @@ -%option prefix="fastd_config_" +%option prefix="fastd_config_yy" %option noyywrap %option bison-bridge %option reentrant @@ -7,10 +7,11 @@ #include <config.yy.h> } -%x STRING -%x ADDR6 +%s STRING +%s ADDR6 %% +<INITIAL>{ [0-9]+ { yylval->num = atoi(yytext); return TOK_INTEGER; } interface { yylval->str = yytext; return TOK_INTERFACE; } @@ -26,7 +27,7 @@ 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)) { yylval->str = "invalid address"; - return TOK_ERROR; + return -1; } return TOK_ADDR; @@ -38,27 +39,33 @@ tun { yylval->str = yytext; return TOK_TUN; } [A-Za-z_][A-Za-z0-9_]* { yylval->str = yytext; return TOK_IDENTIFIER; } -[;:\{\}] { return *yytext; } +[;:\{\}] { return yytext[0]; } [ \t\n] ; +} -\" BEGIN(STRING); +<INITIAL>\"\" { yylval->str = ""; return TOK_STRING; } +<INITIAL>\" BEGIN(STRING); <STRING>[^"]* { yylval->str = yytext; return TOK_STRING; } <STRING>\" BEGIN(INITIAL); -\[ BEGIN(ADDR6); -<ADDR6>[^\]]+ { - if (!inet_pton(AF_INET6, yytext, &yylval->addr6)) { - yylval->str = "invalid address"; - return TOK_ERROR; - } - - return TOK_ADDR6; +<INITIAL>\[ BEGIN(ADDR6); +<ADDR6>[^\]]+ { + if (!inet_pton(AF_INET6, yytext, &yylval->addr6)) { + yylval->str = "invalid address"; + return -1; } -<ADDR6>\] BEGIN(INITIAL); -<INITIAL,STRING,ADDR6>. { - yylval->str = "invalid character"; - return TOK_ERROR; - } + return TOK_ADDR6; + } +<ADDR6>\] BEGIN(INITIAL); + +. { + yylval->str = "invalid character"; + return -1; + } + +<INITIAL><<EOF>> { return 0; } +<STRING><<EOF>> { yylval->str = "unterminated string"; return -1; } +<ADDR6><<EOF>> { yylval->str = "unterminated address"; return -1; } %% diff --git a/src/config.y b/src/config.y index 02a9529..9868d57 100644 --- a/src/config.y +++ b/src/config.y @@ -1,11 +1,11 @@ %define api.pure +%define api.push-pull push %name-prefix "fastd_config_" -%lex-param {yyscan_t scanner} %parse-param {fastd_context *ctx} %parse-param {fastd_config *conf} -%parse-param {yyscan_t scanner} %code requires { + #include <fastd.h> #include <arpa/inet.h> } @@ -16,8 +16,6 @@ struct in6_addr addr6; } -%token <str> TOK_ERROR; - %token <num> TOK_INTEGER %token <str> TOK_STRING %token <str> TOK_IDENTIFIER @@ -41,11 +39,10 @@ %code { #include <config.h> - #include <config.ll.h> #include <stdint.h> #include <peer.h> - void fastd_config_error(fastd_context *ctx, fastd_config *conf, yyscan_t scanner, char *s); + void fastd_config_error(fastd_context *ctx, fastd_config *conf, char *s); extern fastd_protocol fastd_protocol_null; @@ -54,10 +51,6 @@ #endif } -%code provides { - #include <fastd.h> - int fastd_config_parse (fastd_context *ctx, fastd_config *conf, void *scanner); -} %type <str> maybe_string @@ -158,15 +151,15 @@ peer_key: TOK_STRING { free(conf->peers->key); conf->peers->key = strdup($1); } maybe_string: TOK_STRING - | { $$[0] = '\0'; } + | { $$ = ""; } ; maybe_port: ':' port { $$ = $2; } | { $$ = 0; } ; -maybe_port_default: ':' port { $$ = $2; } - | { $$ = htons(1337); } +maybe_port_default: ':' port { $$ = $2; } + | { $$ = htons(1337); } ; port: TOK_INTEGER { @@ -176,6 +169,6 @@ port: TOK_INTEGER { } ; %% -void fastd_config_error(fastd_context *ctx, fastd_config *conf, yyscan_t scanner, char *s) { +void fastd_config_error(fastd_context *ctx, fastd_config *conf, char *s) { exit_error(ctx, "config error: %s", s); } diff --git a/src/fastd.c b/src/fastd.c index 479ac95..40f0046 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -393,6 +393,8 @@ int main(int argc, char *argv[]) { update_time(&ctx); + conf.protocol->init(&ctx); + init_peers(&ctx); init_tuntap(&ctx); diff --git a/src/fastd.h b/src/fastd.h index c453790..a8346c1 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -62,11 +62,13 @@ struct _fastd_protocol { bool (*handle_config)(fastd_context *ctx, const fastd_config *conf, const char *option); bool (*check_config)(fastd_context *ctx, const fastd_config *conf); + void (*init)(fastd_context *ctx); + size_t (*max_packet_size)(fastd_context *ctx); char* (*peer_str)(const fastd_context *ctx, const fastd_peer *peer); - void (*init)(fastd_context *ctx, fastd_peer *peer); + void (*init_peer)(fastd_context *ctx, fastd_peer *peer); void (*handle_recv)(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer); void (*send)(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer); @@ -114,6 +116,8 @@ struct _fastd_context { size_t eth_addr_size; size_t n_eth_addr; fastd_peer_eth_addr *eth_addr; + + void *protocol_context; }; diff --git a/src/handshake.c b/src/handshake.c index 6220ddf..8fc82d0 100644 --- a/src/handshake.c +++ b/src/handshake.c @@ -184,7 +184,7 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_peer *peer, fastd_buffer b case REPLY_SUCCESS: pr_info(ctx, "Handshake with %P successful.", peer); fastd_peer_set_established(ctx, peer); - ctx->conf->protocol->init(ctx, peer); + ctx->conf->protocol->init_peer(ctx, peer); break; default: diff --git a/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c b/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c index 53901ba..4152601 100644 --- a/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c +++ b/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c @@ -36,9 +36,9 @@ #include <crypto_secretbox_xsalsa20poly1305.h> -typedef struct _protocol_config { +typedef struct _protocol_context { ecc_secret_key_256 secret_key; -} protocol_config; +} protocol_context; typedef struct _protocol_peer_config { ecc_public_key_256 public_key; @@ -48,15 +48,13 @@ typedef struct _protocol_peer_state { } protocol_peer_state; -static bool protocol_handle_config(fastd_context *ctx, const fastd_config *conf, const char *option) { - printf("Unknown option: %s\n", option); - return false; -} - static bool protocol_check_config(fastd_context *ctx, const fastd_config *conf) { return true; } +static void protocol_init(fastd_context *ctx) { +} + static size_t protocol_max_packet_size(fastd_context *ctx) { return (fastd_max_packet_size(ctx) - crypto_secretbox_xsalsa20poly1305_NONCEBYTES); } @@ -94,7 +92,7 @@ static char* protocol_peer_str(const fastd_context *ctx, const fastd_peer *peer) return NULL; } -static void protocol_init(fastd_context *ctx, fastd_peer *peer) { +static void protocol_init_peer(fastd_context *ctx, fastd_peer *peer) { pr_info(ctx, "Initializing session with %P...", peer); } @@ -113,14 +111,15 @@ static void protocol_free_peer_private(fastd_context *ctx, fastd_peer *peer) { const fastd_protocol fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305 = { .name = "ec25519-fhmqvc-xsalsa20-poly1305", - .handle_config = protocol_handle_config, .check_config = protocol_check_config, + .init = protocol_init, + .max_packet_size = protocol_max_packet_size, .peer_str = protocol_peer_str, - .init = protocol_init, + .init_peer = protocol_init_peer, .handle_recv = protocol_handle_recv, .send = protocol_send, diff --git a/src/protocol_null.c b/src/protocol_null.c index f365228..410d848 100644 --- a/src/protocol_null.c +++ b/src/protocol_null.c @@ -34,10 +34,6 @@ #include <arpa/inet.h> -static bool protocol_handle_config(fastd_context *ctx, const fastd_config *conf, const char *option) { - return false; -} - static bool protocol_check_config(fastd_context *ctx, const fastd_config *conf) { if (conf->n_floating > 1) { pr_error(ctx, "with protocol `null' use can't define more than one floating peer"); @@ -47,6 +43,9 @@ static bool protocol_check_config(fastd_context *ctx, const fastd_config *conf) return true; } +static void protocol_init(fastd_context *ctx) { +} + static size_t protocol_max_packet_size(fastd_context *ctx) { return fastd_max_packet_size(ctx); } @@ -84,7 +83,7 @@ static char* protocol_peer_str(const fastd_context *ctx, const fastd_peer *peer) return NULL; } -static void protocol_init(fastd_context *ctx, fastd_peer *peer) { +static void protocol_init_peer(fastd_context *ctx, fastd_peer *peer) { pr_info(ctx, "Connection with %P established.", peer); fastd_task_put_send(ctx, peer, fastd_buffer_alloc(0, 0, 0)); @@ -129,14 +128,15 @@ static void protocol_free_peer_private(fastd_context *ctx, fastd_peer *peer) { const fastd_protocol fastd_protocol_null = { .name = "null", - .handle_config = protocol_handle_config, .check_config = protocol_check_config, + .init = protocol_init, + .max_packet_size = protocol_max_packet_size, .peer_str = protocol_peer_str, - .init = protocol_init, + .init_peer = protocol_init_peer, .handle_recv = protocol_handle_recv, .send = protocol_send, |