diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2012-12-18 21:00:44 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2012-12-18 21:00:44 +0100 |
commit | 224ea7b0815187d2a638808e8a50fb29fda832f6 (patch) | |
tree | aabc417ce70e0d83258b8b51439e8f2ccc4b8ffe /src | |
parent | 766a0c145dad84b9af35b2bebc032be71509b3df (diff) | |
download | fastd-224ea7b0815187d2a638808e8a50fb29fda832f6.tar fastd-224ea7b0815187d2a638808e8a50fb29fda832f6.zip |
Convert type names to _t convention
Diffstat (limited to 'src')
-rw-r--r-- | src/config.c | 162 | ||||
-rw-r--r-- | src/config.y | 18 | ||||
-rw-r--r-- | src/crypto.c | 58 | ||||
-rw-r--r-- | src/crypto.h | 32 | ||||
-rw-r--r-- | src/crypto_linux.c | 40 | ||||
-rw-r--r-- | src/fastd.c | 154 | ||||
-rw-r--r-- | src/fastd.h | 236 | ||||
-rw-r--r-- | src/handshake.c | 44 | ||||
-rw-r--r-- | src/handshake.h | 28 | ||||
-rw-r--r-- | src/method_aes128_gcm.c | 122 | ||||
-rw-r--r-- | src/method_null.c | 24 | ||||
-rw-r--r-- | src/method_xsalsa20_poly1305.c | 30 | ||||
-rw-r--r-- | src/packet.h | 8 | ||||
-rw-r--r-- | src/peer.c | 108 | ||||
-rw-r--r-- | src/peer.h | 90 | ||||
-rw-r--r-- | src/printf.c | 22 | ||||
-rw-r--r-- | src/protocol_ec25519_fhmqvc.c | 131 | ||||
-rw-r--r-- | src/queue.c | 18 | ||||
-rw-r--r-- | src/queue.h | 24 | ||||
-rw-r--r-- | src/random.c | 2 | ||||
-rw-r--r-- | src/resolve.c | 20 | ||||
-rw-r--r-- | src/task.c | 72 | ||||
-rw-r--r-- | src/task.h | 33 | ||||
-rw-r--r-- | src/types.h | 64 |
24 files changed, 767 insertions, 773 deletions
diff --git a/src/config.c b/src/config.c index 8ae1ca6..57059e5 100644 --- a/src/config.c +++ b/src/config.c @@ -40,52 +40,52 @@ #include <sys/types.h> -extern const fastd_protocol fastd_protocol_ec25519_fhmqvc; +extern const fastd_protocol_t fastd_protocol_ec25519_fhmqvc; -extern const fastd_method fastd_method_null; +extern const fastd_method_t fastd_method_null; #ifdef WITH_METHOD_XSALSA20_POLY1305 -extern const fastd_method fastd_method_xsalsa20_poly1305; +extern const fastd_method_t fastd_method_xsalsa20_poly1305; #endif #ifdef WITH_METHOD_AES128_GCM -extern const fastd_method fastd_method_aes128_gcm; +extern const fastd_method_t fastd_method_aes128_gcm; #endif #ifdef USE_CRYPTO_AES128CTR #ifdef WITH_CRYPTO_AES128CTR_NACL -extern const fastd_crypto_aes128ctr fastd_crypto_aes128ctr_nacl; +extern const fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_nacl; #endif #ifdef WITH_CRYPTO_AES128CTR_LINUX -extern const fastd_crypto_aes128ctr fastd_crypto_aes128ctr_linux; +extern const fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_linux; #endif #ifdef WITH_CRYPTO_AES128CTR_NACL -static const fastd_crypto_aes128ctr *fastd_crypto_aes128ctr_default = &fastd_crypto_aes128ctr_nacl; +static const fastd_crypto_aes128ctr_t *fastd_crypto_aes128ctr_default = &fastd_crypto_aes128ctr_nacl; #else -static const fastd_crypto_aes128ctr *fastd_crypto_aes128ctr_default = &fastd_crypto_aes128ctr_linux; +static const fastd_crypto_aes128ctr_t *fastd_crypto_aes128ctr_default = &fastd_crypto_aes128ctr_linux; #endif #endif #ifdef USE_CRYPTO_GHASH #ifdef WITH_CRYPTO_GHASH_BUILTIN -extern const fastd_crypto_ghash fastd_crypto_ghash_builtin; +extern const fastd_crypto_ghash_t fastd_crypto_ghash_builtin; #endif #ifdef WITH_CRYPTO_GHASH_LINUX -extern const fastd_crypto_ghash fastd_crypto_ghash_linux; +extern const fastd_crypto_ghash_t fastd_crypto_ghash_linux; #endif #ifdef WITH_CRYPTO_GHASH_BUILTIN -static const fastd_crypto_ghash *fastd_crypto_ghash_default = &fastd_crypto_ghash_builtin; +static const fastd_crypto_ghash_t *fastd_crypto_ghash_default = &fastd_crypto_ghash_builtin; #else -static const fastd_crypto_ghash *fastd_crypto_ghash_default = &fastd_crypto_ghash_linux; +static const fastd_crypto_ghash_t *fastd_crypto_ghash_default = &fastd_crypto_ghash_linux; #endif #endif -static void default_config(fastd_config *conf) { - memset(conf, 0, sizeof(fastd_config)); +static void default_config(fastd_config_t *conf) { + memset(conf, 0, sizeof(fastd_config_t)); conf->log_stderr_level = -1; conf->log_syslog_level = -1; @@ -116,7 +116,7 @@ static void default_config(fastd_config *conf) { conf->crypto_ghash = fastd_crypto_ghash_default; #endif - conf->peer_group = calloc(1, sizeof(fastd_peer_group_config)); + conf->peer_group = calloc(1, sizeof(fastd_peer_group_config_t)); conf->peer_group->name = strdup("default"); } @@ -139,7 +139,7 @@ static bool config_match(const char *opt, ...) { return match; } -bool fastd_config_protocol(fastd_context *ctx, fastd_config *conf, const char *name) { +bool fastd_config_protocol(fastd_context_t *ctx, fastd_config_t *conf, const char *name) { if (!strcmp(name, "ec25519-fhmqvc")) conf->protocol = &fastd_protocol_ec25519_fhmqvc; else @@ -148,7 +148,7 @@ bool fastd_config_protocol(fastd_context *ctx, fastd_config *conf, const char *n return true; } -static inline const fastd_method* parse_method_name(const char *name) { +static inline const fastd_method_t* parse_method_name(const char *name) { if (!strcmp(name, "null")) return &fastd_method_null; #ifdef WITH_METHOD_XSALSA20_POLY1305 @@ -163,8 +163,8 @@ static inline const fastd_method* parse_method_name(const char *name) { return NULL; } -bool fastd_config_method(fastd_context *ctx, fastd_config *conf, const char *name) { - const fastd_method *method = parse_method_name(name); +bool fastd_config_method(fastd_context_t *ctx, fastd_config_t *conf, const char *name) { + const fastd_method_t *method = parse_method_name(name); if (!method) return false; @@ -185,7 +185,7 @@ bool fastd_config_method(fastd_context *ctx, fastd_config *conf, const char *nam exit_bug(ctx, "MAX_METHODS too low"); } -bool fastd_config_crypto(fastd_context *ctx, fastd_config *conf, const char *alg, const char *impl) { +bool fastd_config_crypto(fastd_context_t *ctx, fastd_config_t *conf, const char *alg, const char *impl) { #ifdef USE_CRYPTO_AES128CTR if (!strcasecmp(alg, "aes128-ctr") || !strcasecmp(alg, "aes128") || !strcasecmp(alg, "aes-ctr") || !strcasecmp(alg, "aes")) { if (!strcasecmp(impl, "default")) @@ -227,8 +227,8 @@ bool fastd_config_crypto(fastd_context *ctx, fastd_config *conf, const char *alg return false; } -void fastd_config_bind_address(fastd_context *ctx, fastd_config *conf, const fastd_peer_address *address, const char *bindtodev, bool default_v4, bool default_v6) { - fastd_bind_address *addr = malloc(sizeof(fastd_bind_address)); +void fastd_config_bind_address(fastd_context_t *ctx, fastd_config_t *conf, const fastd_peer_address_t *address, const char *bindtodev, bool default_v4, bool default_v6) { + fastd_bind_address_t *addr = malloc(sizeof(fastd_bind_address_t)); addr->next = conf->bind_addrs; conf->bind_addrs = addr; conf->n_bind_addrs++; @@ -245,8 +245,8 @@ void fastd_config_bind_address(fastd_context *ctx, fastd_config *conf, const fas conf->bind_addr_default_v6 = addr; } -void fastd_config_peer_group_push(fastd_context *ctx, fastd_config *conf, const char *name) { - fastd_peer_group_config *group = calloc(1, sizeof(fastd_peer_group_config)); +void fastd_config_peer_group_push(fastd_context_t *ctx, fastd_config_t *conf, const char *name) { + fastd_peer_group_config_t *group = calloc(1, sizeof(fastd_peer_group_config_t)); group->name = strdup(name); group->parent = conf->peer_group; @@ -257,13 +257,13 @@ void fastd_config_peer_group_push(fastd_context *ctx, fastd_config *conf, const conf->peer_group = group; } -void fastd_config_peer_group_pop(fastd_context *ctx, fastd_config *conf) { +void fastd_config_peer_group_pop(fastd_context_t *ctx, fastd_config_t *conf) { conf->peer_group = conf->peer_group->parent; } -static void free_peer_group(fastd_peer_group_config *group) { +static void free_peer_group(fastd_peer_group_config_t *group) { while (group->children) { - fastd_peer_group_config *next = group->children->next; + fastd_peer_group_config_t *next = group->children->next; free_peer_group(group->children); group->children = next; } @@ -273,11 +273,11 @@ static void free_peer_group(fastd_peer_group_config *group) { free(group); } -static bool has_peer_group_peer_dirs(const fastd_peer_group_config *group) { +static bool has_peer_group_peer_dirs(const fastd_peer_group_config_t *group) { if (group->peer_dirs) return true; - const fastd_peer_group_config *child; + const fastd_peer_group_config_t *child; for (child = group->children; child; child = child->next) { if (has_peer_group_peer_dirs(child)) return true; @@ -286,7 +286,7 @@ static bool has_peer_group_peer_dirs(const fastd_peer_group_config *group) { return false; } -bool fastd_config_add_log_file(fastd_context *ctx, fastd_config *conf, const char *name, int level) { +bool fastd_config_add_log_file(fastd_context_t *ctx, fastd_config_t *conf, const char *name, int level) { char *name2 = strdup(name); char *name3 = strdup(name); @@ -298,7 +298,7 @@ bool fastd_config_add_log_file(fastd_context *ctx, fastd_config *conf, const cha if (!chdir(dir)) { char *logdir = get_current_dir_name(); - fastd_log_file *file = malloc(sizeof(fastd_log_file)); + fastd_log_file_t *file = malloc(sizeof(fastd_log_file_t)); file->filename = malloc(strlen(logdir) + 1 + strlen(base) + 1); strcpy(file->filename, logdir); @@ -323,7 +323,7 @@ bool fastd_config_add_log_file(fastd_context *ctx, fastd_config *conf, const cha return true; } -static void read_peer_dir(fastd_context *ctx, fastd_config *conf, const char *dir) { +static void read_peer_dir(fastd_context_t *ctx, fastd_config_t *conf, const char *dir) { DIR *dirh = opendir("."); if (dirh) { @@ -369,7 +369,7 @@ static void read_peer_dir(fastd_context *ctx, fastd_config *conf, const char *di } } -void fastd_read_peer_dir(fastd_context *ctx, fastd_config *conf, const char *dir) { +void fastd_read_peer_dir(fastd_context_t *ctx, fastd_config_t *conf, const char *dir) { char *oldcwd = get_current_dir_name(); if (!chdir(dir)) { @@ -389,7 +389,7 @@ void fastd_read_peer_dir(fastd_context *ctx, fastd_config *conf, const char *dir free(oldcwd); } -bool fastd_read_config(fastd_context *ctx, fastd_config *conf, const char *filename, bool peer_config, int depth) { +bool fastd_read_config(fastd_context_t *ctx, fastd_config_t *conf, const char *filename, bool peer_config, int depth) { if (depth >= MAX_CONFIG_DEPTH) exit_error(ctx, "maximum config include depth exceeded"); @@ -400,7 +400,7 @@ bool fastd_read_config(fastd_context *ctx, fastd_config *conf, const char *filen FILE *file; yyscan_t scanner; fastd_config_pstate *ps; - fastd_string_stack *strings = NULL; + fastd_string_stack_t *strings = NULL; fastd_config_yylex_init(&scanner); ps = fastd_config_pstate_new(); @@ -479,7 +479,7 @@ bool fastd_read_config(fastd_context *ctx, fastd_config *conf, const char *filen return ret; } -static void count_peers(fastd_context *ctx, fastd_config *conf) { +static void count_peers(fastd_context_t *ctx, fastd_config_t *conf) { conf->n_floating = 0; conf->n_v4 = 0; conf->n_v6 = 0; @@ -487,7 +487,7 @@ static void count_peers(fastd_context *ctx, fastd_config *conf) { conf->n_dynamic_v4 = 0; conf->n_dynamic_v6 = 0; - fastd_peer_config *peer; + fastd_peer_config_t *peer; for (peer = conf->peers; peer; peer = peer->next) { switch (peer->address.sa.sa_family) { case AF_UNSPEC: @@ -561,7 +561,7 @@ static void print_usage(const char *options, const char *message) { puts(message); } -static void usage(fastd_context *ctx, fastd_config *conf) { +static void usage(fastd_context_t *ctx, fastd_config_t *conf) { #define OR ", " #define OPTION(func, options, message) print_usage(" " options, message); #define OPTION_ARG(func, options, arg, message) print_usage(" " options " " arg, message); @@ -576,12 +576,12 @@ static void usage(fastd_context *ctx, fastd_config *conf) { #undef OPTION_ARG } -static void version(fastd_context *ctx, fastd_config *conf) { +static void version(fastd_context_t *ctx, fastd_config_t *conf) { puts("fastd " FASTD_VERSION); exit(0); } -static int parse_log_level(fastd_context *ctx, const char *arg) { +static int parse_log_level(fastd_context_t *ctx, const char *arg) { if (!strcmp(arg, "fatal")) return LOG_CRIT; else if (!strcmp(arg, "error")) @@ -600,20 +600,20 @@ static int parse_log_level(fastd_context *ctx, const char *arg) { -static void option_log_level(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_log_level(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { conf->log_stderr_level = parse_log_level(ctx, arg); } -static void option_syslog_level(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_syslog_level(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { conf->log_syslog_level = parse_log_level(ctx, arg); } -static void option_syslog_ident(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_syslog_ident(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { free(conf->log_syslog_ident); conf->log_syslog_ident = strdup(arg); } -static void option_config(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_config(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { if (!strcmp(arg, "-")) arg = NULL; @@ -621,18 +621,18 @@ static void option_config(fastd_context *ctx, fastd_config *conf, const char *ar exit(1); } -static void option_config_peer(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_config_peer(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { fastd_peer_config_new(ctx, conf); if(!fastd_read_config(ctx, conf, arg, true, 0)) exit(1); } -static void option_config_peer_dir(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_config_peer_dir(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { fastd_read_peer_dir(ctx, conf, arg); } -static void option_mode(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_mode(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { if (!strcmp(arg, "tap")) conf->mode = MODE_TAP; else if (!strcmp(arg, "tun")) @@ -641,12 +641,12 @@ static void option_mode(fastd_context *ctx, fastd_config *conf, const char *arg) exit_error(ctx, "invalid mode `%s'", arg); } -static void option_interface(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_interface(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { free(conf->ifname); conf->ifname = strdup(arg); } -static void option_mtu(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_mtu(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { char *endptr; conf->mtu = strtol(arg, &endptr, 10); @@ -654,7 +654,7 @@ static void option_mtu(fastd_context *ctx, fastd_config *conf, const char *arg) exit_error(ctx, "invalid mtu `%s'", arg); } -static void option_bind(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_bind(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { long l; char *charptr; char *endptr; @@ -691,7 +691,7 @@ static void option_bind(fastd_context *ctx, fastd_config *conf, const char *arg) l = 0; } - fastd_peer_address addr = {}; + fastd_peer_address_t addr = {}; if (strcmp(addrstr, "any") == 0) { /* nothing to do */ @@ -716,21 +716,21 @@ static void option_bind(fastd_context *ctx, fastd_config *conf, const char *arg) fastd_config_bind_address(ctx, conf, &addr, NULL, false, false); } -static void option_protocol(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_protocol(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { if (!fastd_config_protocol(ctx, conf, arg)) exit_error(ctx, "invalid protocol `%s'", arg); } -static void option_method(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_method(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { if (!fastd_config_method(ctx, conf, arg)) exit_error(ctx, "invalid method `%s'", arg); } -static void option_forward(fastd_context *ctx, fastd_config *conf) { +static void option_forward(fastd_context_t *ctx, fastd_config_t *conf) { conf->forward = true; } -static void option_on_up(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_on_up(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { free(conf->on_up); free(conf->on_up_dir); @@ -738,7 +738,7 @@ static void option_on_up(fastd_context *ctx, fastd_config *conf, const char *arg conf->on_up_dir = get_current_dir_name(); } -static void option_on_down(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_on_down(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { free(conf->on_down); free(conf->on_down_dir); @@ -746,7 +746,7 @@ static void option_on_down(fastd_context *ctx, fastd_config *conf, const char *a conf->on_down_dir = get_current_dir_name(); } -static void option_on_establish(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_on_establish(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { free(conf->on_establish); free(conf->on_establish_dir); @@ -754,7 +754,7 @@ static void option_on_establish(fastd_context *ctx, fastd_config *conf, const ch conf->on_establish_dir = get_current_dir_name(); } -static void option_on_disestablish(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_on_disestablish(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { free(conf->on_disestablish); free(conf->on_disestablish_dir); @@ -762,31 +762,31 @@ static void option_on_disestablish(fastd_context *ctx, fastd_config *conf, const conf->on_disestablish_dir = get_current_dir_name(); } -static void option_daemon(fastd_context *ctx, fastd_config *conf) { +static void option_daemon(fastd_context_t *ctx, fastd_config_t *conf) { conf->daemon = true; } -static void option_pid_file(fastd_context *ctx, fastd_config *conf, const char *arg) { +static void option_pid_file(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) { free(conf->pid_file); conf->pid_file = strdup(arg); } -static void option_generate_key(fastd_context *ctx, fastd_config *conf) { +static void option_generate_key(fastd_context_t *ctx, fastd_config_t *conf) { conf->generate_key = true; conf->show_key = false; } -static void option_show_key(fastd_context *ctx, fastd_config *conf) { +static void option_show_key(fastd_context_t *ctx, fastd_config_t *conf) { conf->generate_key = false; conf->show_key = true; } -static void option_machine_readable(fastd_context *ctx, fastd_config *conf) { +static void option_machine_readable(fastd_context_t *ctx, fastd_config_t *conf) { conf->machine_readable = true; } -void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *const argv[]) { +void fastd_configure(fastd_context_t *ctx, fastd_config_t *conf, int argc, char *const argv[]) { #define OR , #define OPTION(func, options, message) \ if(config_match(argv[i], options, NULL)) { \ @@ -838,10 +838,10 @@ void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *con #undef OPTION_ARG } -static void reconfigure_read_peer_dirs(fastd_context *ctx, fastd_config *new_conf, fastd_string_stack *dirs) { +static void reconfigure_read_peer_dirs(fastd_context_t *ctx, fastd_config_t *new_conf, fastd_string_stack_t *dirs) { char *oldcwd = get_current_dir_name(); - fastd_string_stack *dir; + fastd_string_stack_t *dir; for (dir = dirs; dir; dir = dir->next) { if (!chdir(dir->str)) read_peer_dir(ctx, new_conf, dir->str); @@ -855,18 +855,18 @@ static void reconfigure_read_peer_dirs(fastd_context *ctx, fastd_config *new_con free(oldcwd); } -static void reconfigure_read_peer_group(fastd_context *ctx, fastd_config *new_conf) { +static void reconfigure_read_peer_group(fastd_context_t *ctx, fastd_config_t *new_conf) { reconfigure_read_peer_dirs(ctx, new_conf, new_conf->peer_group->peer_dirs); - fastd_peer_group_config *group; + fastd_peer_group_config_t *group; for (group = new_conf->peer_group->children; group; group = group->next) { new_conf->peer_group = group; reconfigure_read_peer_group(ctx, new_conf); } } -static void reconfigure_handle_old_peers(fastd_context *ctx, fastd_peer_config **old_peers, fastd_peer_config **new_peers) { - fastd_peer_config **peer, **next, **new_peer, **new_next; +static void reconfigure_handle_old_peers(fastd_context_t *ctx, fastd_peer_config_t **old_peers, fastd_peer_config_t **new_peers) { + fastd_peer_config_t **peer, **next, **new_peer, **new_next; for (peer = old_peers; *peer; peer = next) { next = &(*peer)->next; @@ -882,7 +882,7 @@ static void reconfigure_handle_old_peers(fastd_context *ctx, fastd_peer_config * if (fastd_peer_config_equal(*peer, *new_peer)) { pr_verbose(ctx, "peer `%s' unchanged", (*peer)->name); - fastd_peer_config *free_peer = *new_peer; + fastd_peer_config_t *free_peer = *new_peer; *new_peer = *new_next; fastd_peer_config_free(free_peer); peer = NULL; @@ -900,7 +900,7 @@ static void reconfigure_handle_old_peers(fastd_context *ctx, fastd_peer_config * if (peer && (!new_peer || !*new_peer)) { pr_verbose(ctx, "removing peer `%s'", (*peer)->name); - fastd_peer_config *free_peer = *peer; + fastd_peer_config_t *free_peer = *peer; *peer = *next; next = peer; @@ -909,16 +909,16 @@ static void reconfigure_handle_old_peers(fastd_context *ctx, fastd_peer_config * } } -static void reconfigure_reset_waiting(fastd_context *ctx) { - fastd_peer *peer; +static void reconfigure_reset_waiting(fastd_context_t *ctx) { + fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { if (!fastd_peer_is_established(peer)) fastd_peer_reset(ctx, peer); } } -static void reconfigure_handle_new_peers(fastd_context *ctx, fastd_peer_config **peers, fastd_peer_config *new_peers) { - fastd_peer_config *peer, *next; +static void reconfigure_handle_new_peers(fastd_context_t *ctx, fastd_peer_config_t **peers, fastd_peer_config_t *new_peers) { + fastd_peer_config_t *peer, *next; for (peer = new_peers; peer; peer = next) { next = peer->next; @@ -931,10 +931,10 @@ static void reconfigure_handle_new_peers(fastd_context *ctx, fastd_peer_config * } } -void fastd_reconfigure(fastd_context *ctx, fastd_config *conf) { +void fastd_reconfigure(fastd_context_t *ctx, fastd_config_t *conf) { pr_info(ctx, "reconfigure triggered"); - fastd_config temp_conf; + fastd_config_t temp_conf; temp_conf.peer_group = conf->peer_group; temp_conf.peers = NULL; @@ -948,19 +948,19 @@ void fastd_reconfigure(fastd_context *ctx, fastd_config *conf) { count_peers(ctx, conf); } -void fastd_config_release(fastd_context *ctx, fastd_config *conf) { +void fastd_config_release(fastd_context_t *ctx, fastd_config_t *conf) { while (conf->peers) fastd_peer_config_delete(ctx, conf); while (conf->log_files) { - fastd_log_file *next = conf->log_files->next; + fastd_log_file_t *next = conf->log_files->next; free(conf->log_files->filename); free(conf->log_files); conf->log_files = next; } while (conf->bind_addrs) { - fastd_bind_address *next = conf->bind_addrs->next; + fastd_bind_address_t *next = conf->bind_addrs->next; free(conf->bind_addrs->bindtodev); free(conf->bind_addrs); conf->bind_addrs = next; diff --git a/src/config.y b/src/config.y index 66912a2..d91dd61 100644 --- a/src/config.y +++ b/src/config.y @@ -28,8 +28,8 @@ %define api.push-pull push %name-prefix "fastd_config_" %locations -%parse-param {fastd_context *ctx} -%parse-param {fastd_config *conf} +%parse-param {fastd_context_t *ctx} +%parse-param {fastd_config_t *conf} %parse-param {const char *filename} %parse-param {int depth} @@ -42,12 +42,12 @@ %union { int num; - fastd_string_stack *str; + fastd_string_stack_t *str; char *error; bool boolean; struct in_addr addr4; struct in6_addr addr6; - fastd_peer_address addr; + fastd_peer_address_t addr; } %token START_CONFIG @@ -113,7 +113,7 @@ #include <stdint.h> #include <unistd.h> - void fastd_config_error(YYLTYPE *loc, fastd_context *ctx, fastd_config *conf, const char *filename, int depth, char *s); + void fastd_config_error(YYLTYPE *loc, fastd_context_t *ctx, fastd_config_t *conf, const char *filename, int depth, char *s); } @@ -215,13 +215,13 @@ bind: bind_address maybe_bind_interface maybe_bind_default { bind_address: TOK_ADDR4 maybe_port { - $$ = (fastd_peer_address){ .in = { .sin_family = AF_INET, .sin_addr = $1, .sin_port = htons($2) } }; + $$ = (fastd_peer_address_t){ .in = { .sin_family = AF_INET, .sin_addr = $1, .sin_port = htons($2) } }; } | TOK_ADDR6 maybe_port { - $$ = (fastd_peer_address){ .in6 = { .sin6_family = AF_INET6, .sin6_addr = $1, .sin6_port = htons($2) } }; + $$ = (fastd_peer_address_t){ .in6 = { .sin6_family = AF_INET6, .sin6_addr = $1, .sin6_port = htons($2) } }; } | TOK_ANY maybe_port { - $$ = (fastd_peer_address){ .in = { .sin_family = AF_UNSPEC, .sin_port = htons($2) } }; + $$ = (fastd_peer_address_t){ .in = { .sin_family = AF_UNSPEC, .sin_port = htons($2) } }; } ; @@ -453,6 +453,6 @@ port: colon_or_port TOK_INTEGER { } ; %% -void fastd_config_error(YYLTYPE *loc, fastd_context *ctx, fastd_config *conf, const char *filename, int depth, char *s) { +void fastd_config_error(YYLTYPE *loc, fastd_context_t *ctx, fastd_config_t *conf, const char *filename, int depth, char *s) { pr_error(ctx, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); } diff --git a/src/crypto.c b/src/crypto.c index 6aae261..6c4bbb3 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -34,17 +34,17 @@ #include <crypto_stream_aes128ctr.h> -struct _fastd_crypto_aes128ctr_state { - fastd_buffer d; +struct fastd_crypto_aes128ctr_state { + fastd_buffer_t d; }; -static fastd_crypto_aes128ctr_context* aes128ctr_init(fastd_context *ctx) { - return (fastd_crypto_aes128ctr_context*)1; +static fastd_crypto_aes128ctr_context_t* aes128ctr_init(fastd_context_t *ctx) { + return (fastd_crypto_aes128ctr_context_t*)1; } -static fastd_crypto_aes128ctr_state* aes128ctr_set_key(fastd_context *ctx, const fastd_crypto_aes128ctr_context *cctx, const fastd_block128 *key) { - fastd_crypto_aes128ctr_state *cstate = malloc(sizeof(fastd_crypto_aes128ctr_state)); +static fastd_crypto_aes128ctr_state_t* aes128ctr_set_key(fastd_context_t *ctx, const fastd_crypto_aes128ctr_context_t *cctx, const fastd_block128_t *key) { + fastd_crypto_aes128ctr_state_t *cstate = malloc(sizeof(fastd_crypto_aes128ctr_state_t)); cstate->d = fastd_buffer_alloc(crypto_stream_aes128ctr_BEFORENMBYTES, 0, 0); crypto_stream_aes128ctr_beforenm(cstate->d.data, key->b); @@ -52,22 +52,22 @@ static fastd_crypto_aes128ctr_state* aes128ctr_set_key(fastd_context *ctx, const return cstate; } -static bool aes128ctr_crypt(fastd_context *ctx, const fastd_crypto_aes128ctr_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t len, const fastd_block128 *iv) { +static bool aes128ctr_crypt(fastd_context_t *ctx, const fastd_crypto_aes128ctr_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const fastd_block128_t *iv) { crypto_stream_aes128ctr_xor_afternm(out->b, in->b, len, iv->b, cstate->d.data); return true; } -static void aes128ctr_free_state(fastd_context *ctx, fastd_crypto_aes128ctr_state *cstate) { +static void aes128ctr_free_state(fastd_context_t *ctx, fastd_crypto_aes128ctr_state_t *cstate) { if (cstate) { fastd_buffer_free(cstate->d); free(cstate); } } -static void aes128ctr_free(fastd_context *ctx, fastd_crypto_aes128ctr_context *cctx) { +static void aes128ctr_free(fastd_context_t *ctx, fastd_crypto_aes128ctr_context_t *cctx) { } -fastd_crypto_aes128ctr fastd_crypto_aes128ctr_nacl = { +fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_nacl = { .name = "nacl", .init = aes128ctr_init, @@ -84,19 +84,19 @@ fastd_crypto_aes128ctr fastd_crypto_aes128ctr_nacl = { #ifdef USE_CRYPTO_GHASH #ifdef WITH_CRYPTO_GHASH_BUILTIN -struct _fastd_crypto_ghash_state { - fastd_block128 H[32][16]; +struct fastd_crypto_ghash_state { + fastd_block128_t H[32][16]; }; -static const fastd_block128 r = { .b = {0xe1} }; +static const fastd_block128_t r = { .b = {0xe1} }; -static inline uint8_t shr(fastd_block128 *out, const fastd_block128 *in, int n) { +static inline uint8_t shr(fastd_block128_t *out, const fastd_block128_t *in, int n) { int i; uint8_t c = 0; - for (i = 0; i < sizeof(fastd_block128); i++) { + for (i = 0; i < sizeof(fastd_block128_t); i++) { uint8_t c2 = in->b[i] << (8-n); out->b[i] = (in->b[i] >> n) | c; c = c2; @@ -105,8 +105,8 @@ static inline uint8_t shr(fastd_block128 *out, const fastd_block128 *in, int n) return (c >> (8-n)); } -static inline void mulH_a(fastd_block128 *x, const fastd_crypto_ghash_state *cstate) { - fastd_block128 out = {}; +static inline void mulH_a(fastd_block128_t *x, const fastd_crypto_ghash_state_t *cstate) { + fastd_block128_t out = {}; int i; for (i = 0; i < 16; i++) { @@ -118,15 +118,15 @@ static inline void mulH_a(fastd_block128 *x, const fastd_crypto_ghash_state *cst } -static fastd_crypto_ghash_context* ghash_init(fastd_context *ctx) { - return (fastd_crypto_ghash_context*)1; +static fastd_crypto_ghash_context_t* ghash_init(fastd_context_t *ctx) { + return (fastd_crypto_ghash_context_t*)1; } -static fastd_crypto_ghash_state* ghash_set_h(fastd_context *ctx, const fastd_crypto_ghash_context *cctx, const fastd_block128 *h) { - fastd_crypto_ghash_state *cstate = malloc(sizeof(fastd_crypto_ghash_state)); +static fastd_crypto_ghash_state_t* ghash_set_h(fastd_context_t *ctx, const fastd_crypto_ghash_context_t *cctx, const fastd_block128_t *h) { + fastd_crypto_ghash_state_t *cstate = malloc(sizeof(fastd_crypto_ghash_state_t)); - fastd_block128 Hbase[4]; - fastd_block128 Rbase[4]; + fastd_block128_t Hbase[4]; + fastd_block128_t Rbase[4]; Hbase[0] = *h; Rbase[0] = r; @@ -140,7 +140,7 @@ static fastd_crypto_ghash_state* ghash_set_h(fastd_context *ctx, const fastd_cry shr(&Rbase[i], &Rbase[i-1], 1); } - fastd_block128 R[16]; + fastd_block128_t R[16]; memset(cstate->H, 0, sizeof(cstate->H)); memset(R, 0, sizeof(R)); @@ -166,8 +166,8 @@ static fastd_crypto_ghash_state* ghash_set_h(fastd_context *ctx, const fastd_cry return cstate; } -static bool ghash_hash(fastd_context *ctx, const fastd_crypto_ghash_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t n_blocks) { - memset(out, 0, sizeof(fastd_block128)); +static bool ghash_hash(fastd_context_t *ctx, const fastd_crypto_ghash_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) { + memset(out, 0, sizeof(fastd_block128_t)); int i; for (i = 0; i < n_blocks; i++) { @@ -178,14 +178,14 @@ static bool ghash_hash(fastd_context *ctx, const fastd_crypto_ghash_state *cstat return true; } -static void ghash_free_state(fastd_context *ctx, fastd_crypto_ghash_state *cstate) { +static void ghash_free_state(fastd_context_t *ctx, fastd_crypto_ghash_state_t *cstate) { free(cstate); } -static void ghash_free(fastd_context *ctx, fastd_crypto_ghash_context *cctx) { +static void ghash_free(fastd_context_t *ctx, fastd_crypto_ghash_context_t *cctx) { } -fastd_crypto_ghash fastd_crypto_ghash_builtin = { +fastd_crypto_ghash_t fastd_crypto_ghash_builtin = { .name = "builtin", .init = ghash_init, diff --git a/src/crypto.h b/src/crypto.h index 89c44d6..5cb1761 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -32,45 +32,45 @@ #include <stdint.h> -typedef union _fastd_block128 { +typedef union fastd_block128 { uint8_t b[16]; uint64_t qw[2]; -} __attribute__((aligned(16))) fastd_block128; +} __attribute__((aligned(16))) fastd_block128_t; #ifdef USE_CRYPTO_AES128CTR -struct _fastd_crypto_aes128ctr { +struct fastd_crypto_aes128ctr { const char *name; - fastd_crypto_aes128ctr_context* (*init)(fastd_context *ctx); - fastd_crypto_aes128ctr_state* (*set_key)(fastd_context *ctx, const fastd_crypto_aes128ctr_context *cctx, const fastd_block128 *key); - bool (*crypt)(fastd_context *ctx, const fastd_crypto_aes128ctr_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t len, const fastd_block128 *iv); + fastd_crypto_aes128ctr_context_t* (*init)(fastd_context_t *ctx); + fastd_crypto_aes128ctr_state_t* (*set_key)(fastd_context_t *ctx, const fastd_crypto_aes128ctr_context_t *cctx, const fastd_block128_t *key); + bool (*crypt)(fastd_context_t *ctx, const fastd_crypto_aes128ctr_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const fastd_block128_t *iv); - void (*free_state)(fastd_context *ctx, fastd_crypto_aes128ctr_state *cstate); - void (*free)(fastd_context *ctx, fastd_crypto_aes128ctr_context *cctx); + void (*free_state)(fastd_context_t *ctx, fastd_crypto_aes128ctr_state_t *cstate); + void (*free)(fastd_context_t *ctx, fastd_crypto_aes128ctr_context_t *cctx); }; #endif #ifdef USE_CRYPTO_GHASH -struct _fastd_crypto_ghash { +struct fastd_crypto_ghash { const char *name; - fastd_crypto_ghash_context* (*init)(fastd_context *ctx); - fastd_crypto_ghash_state* (*set_h)(fastd_context *ctx, const fastd_crypto_ghash_context *cctx, const fastd_block128 *h); - bool (*hash)(fastd_context *ctx, const fastd_crypto_ghash_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t n_blocks); + fastd_crypto_ghash_context_t* (*init)(fastd_context_t *ctx); + fastd_crypto_ghash_state_t* (*set_h)(fastd_context_t *ctx, const fastd_crypto_ghash_context_t *cctx, const fastd_block128_t *h); + bool (*hash)(fastd_context_t *ctx, const fastd_crypto_ghash_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks); - void (*free_state)(fastd_context *ctx, fastd_crypto_ghash_state *cstate); - void (*free)(fastd_context *ctx, fastd_crypto_ghash_context *cctx); + void (*free_state)(fastd_context_t *ctx, fastd_crypto_ghash_state_t *cstate); + void (*free)(fastd_context_t *ctx, fastd_crypto_ghash_context_t *cctx); }; #endif -static inline void xor(fastd_block128 *x, const fastd_block128 *a, const fastd_block128 *b) { +static inline void xor(fastd_block128_t *x, const fastd_block128_t *a, const fastd_block128_t *b) { x->qw[0] = a->qw[0] ^ b->qw[0]; x->qw[1] = a->qw[1] ^ b->qw[1]; } -static inline void xor_a(fastd_block128 *x, const fastd_block128 *a) { +static inline void xor_a(fastd_block128_t *x, const fastd_block128_t *a) { xor(x, x, a); } diff --git a/src/crypto_linux.c b/src/crypto_linux.c index e3f3dd9..bf099c7 100644 --- a/src/crypto_linux.c +++ b/src/crypto_linux.c @@ -39,15 +39,15 @@ #ifdef USE_CRYPTO_AES128CTR #ifdef WITH_CRYPTO_AES128CTR_LINUX -struct _fastd_crypto_aes128ctr_context { +struct fastd_crypto_aes128ctr_context { int fd; }; -struct _fastd_crypto_aes128ctr_state { +struct fastd_crypto_aes128ctr_state { int fd; }; -static fastd_crypto_aes128ctr_context* aes128ctr_init(fastd_context *ctx) { +static fastd_crypto_aes128ctr_context_t* aes128ctr_init(fastd_context_t *ctx) { int fd = socket(AF_ALG, SOCK_SEQPACKET, 0); if (fd < 0) goto error; @@ -59,7 +59,7 @@ static fastd_crypto_aes128ctr_context* aes128ctr_init(fastd_context *ctx) { if (bind(fd, (struct sockaddr*)&sa, sizeof(sa)) < 0) goto error; - fastd_crypto_aes128ctr_context *cctx = malloc(sizeof(fastd_crypto_aes128ctr_context)); + fastd_crypto_aes128ctr_context_t *cctx = malloc(sizeof(fastd_crypto_aes128ctr_context_t)); cctx->fd = fd; return cctx; @@ -71,7 +71,7 @@ static fastd_crypto_aes128ctr_context* aes128ctr_init(fastd_context *ctx) { return NULL; } -static fastd_crypto_aes128ctr_state* aes128ctr_set_key(fastd_context *ctx, const fastd_crypto_aes128ctr_context *cctx, const fastd_block128 *key) { +static fastd_crypto_aes128ctr_state_t* aes128ctr_set_key(fastd_context_t *ctx, const fastd_crypto_aes128ctr_context_t *cctx, const fastd_block128_t *key) { if (setsockopt(cctx->fd, SOL_ALG, ALG_SET_KEY, key->b, 16) < 0) { pr_error_errno(ctx, "aes128ctr_set_key(linux): setsockopt"); return NULL; @@ -84,13 +84,13 @@ static fastd_crypto_aes128ctr_state* aes128ctr_set_key(fastd_context *ctx, const return NULL; } - fastd_crypto_aes128ctr_state *cstate = malloc(sizeof(fastd_crypto_aes128ctr_state)); + fastd_crypto_aes128ctr_state_t *cstate = malloc(sizeof(fastd_crypto_aes128ctr_state_t)); cstate->fd = fd; return cstate; } -static bool aes128ctr_crypt(fastd_context *ctx, const fastd_crypto_aes128ctr_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t len, const fastd_block128 *iv) { +static bool aes128ctr_crypt(fastd_context_t *ctx, const fastd_crypto_aes128ctr_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const fastd_block128_t *iv) { if (!len) return false; @@ -130,21 +130,21 @@ static bool aes128ctr_crypt(fastd_context *ctx, const fastd_crypto_aes128ctr_sta return true; } -static void aes128ctr_free_state(fastd_context *ctx, fastd_crypto_aes128ctr_state *cstate) { +static void aes128ctr_free_state(fastd_context_t *ctx, fastd_crypto_aes128ctr_state_t *cstate) { if (cstate) { close(cstate->fd); free(cstate); } } -static void aes128ctr_free(fastd_context *ctx, fastd_crypto_aes128ctr_context *cctx) { +static void aes128ctr_free(fastd_context_t *ctx, fastd_crypto_aes128ctr_context_t *cctx) { if (cctx) { close(cctx->fd); free(cctx); } } -fastd_crypto_aes128ctr fastd_crypto_aes128ctr_linux = { +fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_linux = { .name = "linux", .init = aes128ctr_init, @@ -161,15 +161,15 @@ fastd_crypto_aes128ctr fastd_crypto_aes128ctr_linux = { #ifdef USE_CRYPTO_GHASH #ifdef WITH_CRYPTO_GHASH_LINUX -struct _fastd_crypto_ghash_context { +struct fastd_crypto_ghash_context { int fd; }; -struct _fastd_crypto_ghash_state { +struct fastd_crypto_ghash_state { int fd; }; -static fastd_crypto_ghash_context* ghash_init(fastd_context *ctx) { +static fastd_crypto_ghash_context_t* ghash_init(fastd_context_t *ctx) { int fd = socket(AF_ALG, SOCK_SEQPACKET, 0); if (fd < 0) goto error; @@ -181,7 +181,7 @@ static fastd_crypto_ghash_context* ghash_init(fastd_context *ctx) { if (bind(fd, (struct sockaddr*)&sa, sizeof(sa)) < 0) goto error; - fastd_crypto_ghash_context *cctx = malloc(sizeof(fastd_crypto_ghash_context)); + fastd_crypto_ghash_context_t *cctx = malloc(sizeof(fastd_crypto_ghash_context_t)); cctx->fd = fd; return cctx; @@ -193,7 +193,7 @@ static fastd_crypto_ghash_context* ghash_init(fastd_context *ctx) { return NULL; } -static fastd_crypto_ghash_state* ghash_set_h(fastd_context *ctx, const fastd_crypto_ghash_context *cctx, const fastd_block128 *h) { +static fastd_crypto_ghash_state_t* ghash_set_h(fastd_context_t *ctx, const fastd_crypto_ghash_context_t *cctx, const fastd_block128_t *h) { if (setsockopt(cctx->fd, SOL_ALG, ALG_SET_KEY, h, 16) < 0) { pr_error_errno(ctx, "ghash_set_h(linux): setsockopt"); return NULL; @@ -206,13 +206,13 @@ static fastd_crypto_ghash_state* ghash_set_h(fastd_context *ctx, const fastd_cry return NULL; } - fastd_crypto_ghash_state *cstate = malloc(sizeof(fastd_crypto_ghash_state)); + fastd_crypto_ghash_state_t *cstate = malloc(sizeof(fastd_crypto_ghash_state_t)); cstate->fd = fd; return cstate; } -static bool ghash_hash(fastd_context *ctx, const fastd_crypto_ghash_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t n_blocks) { +static bool ghash_hash(fastd_context_t *ctx, const fastd_crypto_ghash_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) { if (!n_blocks) return false; @@ -229,21 +229,21 @@ static bool ghash_hash(fastd_context *ctx, const fastd_crypto_ghash_state *cstat return true; } -static void ghash_free_state(fastd_context *ctx, fastd_crypto_ghash_state *cstate) { +static void ghash_free_state(fastd_context_t *ctx, fastd_crypto_ghash_state_t *cstate) { if (cstate) { close(cstate->fd); free(cstate); } } -static void ghash_free(fastd_context *ctx, fastd_crypto_ghash_context *cctx) { +static void ghash_free(fastd_context_t *ctx, fastd_crypto_ghash_context_t *cctx) { if (cctx) { close(cctx->fd); free(cctx); } } -fastd_crypto_ghash fastd_crypto_ghash_linux = { +fastd_crypto_ghash_t fastd_crypto_ghash_linux = { .name = "linux", .init = ghash_init, diff --git a/src/fastd.c b/src/fastd.c index 9e7e2df..f9d22ad 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -57,7 +57,7 @@ static void on_terminate(int signo) { terminate = true; } -static void init_signals(fastd_context *ctx) { +static void init_signals(fastd_context_t *ctx) { struct sigaction action; action.sa_flags = 0; @@ -80,7 +80,7 @@ static void init_signals(fastd_context *ctx) { exit_errno(ctx, "sigaction"); } -static void init_pipes(fastd_context *ctx) { +static void init_pipes(fastd_context_t *ctx) { int pipefd[2]; if (pipe(pipefd)) @@ -90,13 +90,13 @@ static void init_pipes(fastd_context *ctx) { ctx->resolvewfd = pipefd[1]; } -static void init_log(fastd_context *ctx) { +static void init_log(fastd_context_t *ctx) { if (ctx->conf->log_syslog_level >= 0) openlog(ctx->conf->log_syslog_ident, LOG_PID, LOG_DAEMON); - fastd_log_file *config; + fastd_log_file_t *config; for (config = ctx->conf->log_files; config; config = config->next) { - fastd_log_fd *file = malloc(sizeof(fastd_log_fd)); + fastd_log_fd_t *file = malloc(sizeof(fastd_log_fd_t)); file->config = config; file->fd = open(config->filename, O_WRONLY|O_APPEND|O_CREAT, 0600); @@ -106,9 +106,9 @@ static void init_log(fastd_context *ctx) { } } -static void close_log(fastd_context *ctx) { +static void close_log(fastd_context_t *ctx) { while (ctx->log_files) { - fastd_log_fd *next = ctx->log_files->next; + fastd_log_fd_t *next = ctx->log_files->next; close(ctx->log_files->fd); free(ctx->log_files); @@ -119,7 +119,7 @@ static void close_log(fastd_context *ctx) { closelog(); } -static void crypto_init(fastd_context *ctx) { +static void crypto_init(fastd_context_t *ctx) { #ifdef USE_CRYPTO_AES128CTR ctx->crypto_aes128ctr = ctx->conf->crypto_aes128ctr->init(ctx); if (!ctx->crypto_aes128ctr) @@ -133,7 +133,7 @@ static void crypto_init(fastd_context *ctx) { #endif } -static void crypto_free(fastd_context *ctx) { +static void crypto_free(fastd_context_t *ctx) { #ifdef USE_CRYPTO_AES128CTR ctx->conf->crypto_aes128ctr->free(ctx, ctx->crypto_aes128ctr); ctx->crypto_aes128ctr = NULL; @@ -146,13 +146,13 @@ static void crypto_free(fastd_context *ctx) { } -static void init_sockets(fastd_context *ctx) { - ctx->socks = malloc(ctx->conf->n_bind_addrs * sizeof(fastd_socket)); +static void init_sockets(fastd_context_t *ctx) { + ctx->socks = malloc(ctx->conf->n_bind_addrs * sizeof(fastd_socket_t)); unsigned i; - fastd_bind_address *addr = ctx->conf->bind_addrs; + fastd_bind_address_t *addr = ctx->conf->bind_addrs; for (i = 0; i < ctx->conf->n_bind_addrs; i++) { - ctx->socks[i] = (fastd_socket){-2, addr, NULL}; + ctx->socks[i] = (fastd_socket_t){-2, addr, NULL}; if (addr == ctx->conf->bind_addr_default_v4) ctx->sock_default_v4 = &ctx->socks[i]; @@ -166,7 +166,7 @@ static void init_sockets(fastd_context *ctx) { ctx->n_socks = ctx->conf->n_bind_addrs; } -static int bind_socket(fastd_context *ctx, const fastd_bind_address *addr, bool warn) { +static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, bool warn) { int fd = -1; int af = AF_UNSPEC; @@ -202,7 +202,7 @@ static int bind_socket(fastd_context *ctx, const fastd_bind_address *addr, bool } } - fastd_peer_address bind_address = addr->addr; + fastd_peer_address_t bind_address = addr->addr; if (bind_address.sa.sa_family == AF_UNSPEC) { memset(&bind_address, 0, sizeof(bind_address)); @@ -238,7 +238,7 @@ static int bind_socket(fastd_context *ctx, const fastd_bind_address *addr, bool return -1; } -static void bind_sockets(fastd_context *ctx) { +static void bind_sockets(fastd_context_t *ctx) { unsigned i; for (i = 0; i < ctx->n_socks; i++) { @@ -256,14 +256,14 @@ static void bind_sockets(fastd_context *ctx) { } } -fastd_socket* fastd_socket_open(fastd_context *ctx, fastd_peer *peer, int af) { - const fastd_bind_address any_address = { .addr.sa.sa_family = af }; +fastd_socket_t* fastd_socket_open(fastd_context_t *ctx, fastd_peer_t *peer, int af) { + const fastd_bind_address_t any_address = { .addr.sa.sa_family = af }; int fd = bind_socket(ctx, &any_address, true); if (fd < 0) return NULL; - fastd_socket *sock = malloc(sizeof(fastd_socket)); + fastd_socket_t *sock = malloc(sizeof(fastd_socket_t)); sock->fd = fd; sock->addr = NULL; @@ -272,7 +272,7 @@ fastd_socket* fastd_socket_open(fastd_context *ctx, fastd_peer *peer, int af) { return sock; } -static void init_tuntap(fastd_context *ctx) { +static void init_tuntap(fastd_context_t *ctx) { struct ifreq ifr; pr_debug(ctx, "initializing tun/tap device..."); @@ -323,14 +323,14 @@ static void init_tuntap(fastd_context *ctx) { pr_debug(ctx, "tun/tap device initialized."); } -static void close_tuntap(fastd_context *ctx) { +static void close_tuntap(fastd_context_t *ctx) { if(close(ctx->tunfd)) pr_warn_errno(ctx, "closing tun/tap: close"); free(ctx->ifname); } -static void close_sockets(fastd_context *ctx) { +static void close_sockets(fastd_context_t *ctx) { unsigned i; for (i = 0; i < ctx->n_socks; i++) fastd_socket_close(ctx, &ctx->socks[i]); @@ -338,7 +338,7 @@ static void close_sockets(fastd_context *ctx) { free(ctx->socks); } -static size_t methods_max_packet_size(fastd_context *ctx) { +static size_t methods_max_packet_size(fastd_context_t *ctx) { size_t ret = ctx->conf->methods[0]->max_packet_size(ctx); int i; @@ -354,7 +354,7 @@ static size_t methods_max_packet_size(fastd_context *ctx) { return ret; } -static size_t methods_min_encrypt_head_space(fastd_context *ctx) { +static size_t methods_min_encrypt_head_space(fastd_context_t *ctx) { size_t ret = ctx->conf->methods[0]->min_encrypt_head_space(ctx); int i; @@ -370,7 +370,7 @@ static size_t methods_min_encrypt_head_space(fastd_context *ctx) { return alignto(ret, 16); } -static size_t methods_min_decrypt_head_space(fastd_context *ctx) { +static size_t methods_min_decrypt_head_space(fastd_context_t *ctx) { size_t ret = ctx->conf->methods[0]->min_decrypt_head_space(ctx); int i; @@ -387,7 +387,7 @@ static size_t methods_min_decrypt_head_space(fastd_context *ctx) { return alignto(ret, 16) + 8; } -static size_t methods_min_encrypt_tail_space(fastd_context *ctx) { +static size_t methods_min_encrypt_tail_space(fastd_context_t *ctx) { size_t ret = ctx->conf->methods[0]->min_encrypt_tail_space(ctx); int i; @@ -403,7 +403,7 @@ static size_t methods_min_encrypt_tail_space(fastd_context *ctx) { return ret; } -static size_t methods_min_decrypt_tail_space(fastd_context *ctx) { +static size_t methods_min_decrypt_tail_space(fastd_context_t *ctx) { size_t ret = ctx->conf->methods[0]->min_decrypt_tail_space(ctx); int i; @@ -419,7 +419,7 @@ static size_t methods_min_decrypt_tail_space(fastd_context *ctx) { return ret; } -static void fastd_send_type(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, uint8_t packet_type, fastd_buffer buffer) { +static void fastd_send_type(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, uint8_t packet_type, fastd_buffer_t buffer) { if (!sock) exit_bug(ctx, "send: sock == NULL"); @@ -460,17 +460,17 @@ static void fastd_send_type(fastd_context *ctx, const fastd_socket *sock, const fastd_buffer_free(buffer); } -void fastd_send(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, fastd_buffer buffer) { +void fastd_send(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_buffer_t buffer) { fastd_send_type(ctx, sock, address, PACKET_DATA, buffer); } -void fastd_send_handshake(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, fastd_buffer buffer) { +void fastd_send_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_buffer_t buffer) { fastd_send_type(ctx, sock, address, PACKET_HANDSHAKE, buffer); } -void fastd_handle_receive(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer) { +void fastd_handle_receive(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer) { if (ctx->conf->mode == MODE_TAP) { - const fastd_eth_addr *src_addr = fastd_get_source_address(ctx, buffer); + const fastd_eth_addr_t *src_addr = fastd_get_source_address(ctx, buffer); if (fastd_eth_addr_is_unicast(src_addr)) fastd_peer_eth_addr_add(ctx, peer, src_addr); @@ -480,10 +480,10 @@ void fastd_handle_receive(fastd_context *ctx, fastd_peer *peer, fastd_buffer buf pr_warn_errno(ctx, "write"); if (ctx->conf->mode == MODE_TAP && ctx->conf->forward) { - const fastd_eth_addr *dest_addr = fastd_get_dest_address(ctx, buffer); + const fastd_eth_addr_t *dest_addr = fastd_get_dest_address(ctx, buffer); if (fastd_eth_addr_is_unicast(dest_addr)) { - fastd_peer *dest_peer = fastd_peer_find_by_eth_addr(ctx, dest_addr); + fastd_peer_t *dest_peer = fastd_peer_find_by_eth_addr(ctx, dest_addr); if (dest_peer && dest_peer != peer && fastd_peer_is_established(dest_peer)) { ctx->conf->protocol->send(ctx, dest_peer, buffer); @@ -493,10 +493,10 @@ void fastd_handle_receive(fastd_context *ctx, fastd_peer *peer, fastd_buffer buf } } else { - fastd_peer *dest_peer; + fastd_peer_t *dest_peer; for (dest_peer = ctx->peers; dest_peer; dest_peer = dest_peer->next) { if (dest_peer != peer && fastd_peer_is_established(dest_peer)) { - fastd_buffer send_buffer = fastd_buffer_alloc(buffer.len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); + fastd_buffer_t send_buffer = fastd_buffer_alloc(buffer.len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); memcpy(send_buffer.data, buffer.data, buffer.len); ctx->conf->protocol->send(ctx, dest_peer, send_buffer); } @@ -510,7 +510,7 @@ void fastd_handle_receive(fastd_context *ctx, fastd_peer *peer, fastd_buffer buf } } -static void on_up(fastd_context *ctx) { +static void on_up(fastd_context_t *ctx) { if (!ctx->conf->on_up) return; @@ -536,7 +536,7 @@ static void on_up(fastd_context *ctx) { free(cwd); } -static void on_down(fastd_context *ctx) { +static void on_down(fastd_context_t *ctx) { if (!ctx->conf->on_down) return; @@ -562,14 +562,14 @@ static void on_down(fastd_context *ctx) { free(cwd); } -static fastd_peer_group* init_peer_group(const fastd_peer_group_config *config, fastd_peer_group *parent) { - fastd_peer_group *ret = calloc(1, sizeof(fastd_peer_group)); +static fastd_peer_group_t* init_peer_group(const fastd_peer_group_config_t *config, fastd_peer_group_t *parent) { + fastd_peer_group_t *ret = calloc(1, sizeof(fastd_peer_group_t)); ret->conf = config; ret->parent = parent; - fastd_peer_group **children = &ret->children; - fastd_peer_group_config *child_config; + fastd_peer_group_t **children = &ret->children; + fastd_peer_group_config_t *child_config; for (child_config = config->children; child_config; child_config = child_config->next) { *children = init_peer_group(child_config, ret); @@ -579,13 +579,13 @@ static fastd_peer_group* init_peer_group(const fastd_peer_group_config *config, return ret; } -static void init_peer_groups(fastd_context *ctx) { +static void init_peer_groups(fastd_context_t *ctx) { ctx->peer_group = init_peer_group(ctx->conf->peer_group, NULL); } -static void free_peer_group(fastd_peer_group *group) { +static void free_peer_group(fastd_peer_group_t *group) { while (group->children) { - fastd_peer_group *child = group->children; + fastd_peer_group_t *child = group->children; group->children = group->children->next; free_peer_group(child); @@ -594,12 +594,12 @@ static void free_peer_group(fastd_peer_group *group) { free(group); } -static void delete_peer_groups(fastd_context *ctx) { +static void delete_peer_groups(fastd_context_t *ctx) { free_peer_group(ctx->peer_group); } -static void init_peers(fastd_context *ctx) { - fastd_peer_config *peer_conf; +static void init_peers(fastd_context_t *ctx) { + fastd_peer_config_t *peer_conf; for (peer_conf = ctx->conf->peers; peer_conf; peer_conf = peer_conf->next) { ctx->conf->protocol->peer_configure(ctx, peer_conf); @@ -608,8 +608,8 @@ static void init_peers(fastd_context *ctx) { } } -static void delete_peers(fastd_context *ctx) { - fastd_peer *peer, *next; +static void delete_peers(fastd_context_t *ctx) { + fastd_peer_t *peer, *next; for (peer = ctx->peers; peer; peer = next) { next = peer->next; @@ -617,15 +617,15 @@ static void delete_peers(fastd_context *ctx) { } } -static inline void update_time(fastd_context *ctx) { +static inline void update_time(fastd_context_t *ctx) { clock_gettime(CLOCK_MONOTONIC, &ctx->now); } -static inline void schedule_new_handshake(fastd_context *ctx, fastd_peer *peer) { +static inline void schedule_new_handshake(fastd_context_t *ctx, fastd_peer_t *peer) { fastd_task_schedule_handshake(ctx, peer, fastd_rand(ctx, 17500, 22500)); } -static void send_handshake(fastd_context *ctx, fastd_peer *peer) { +static void send_handshake(fastd_context_t *ctx, fastd_peer_t *peer) { if (!fastd_peer_may_connect(ctx, peer)) { schedule_new_handshake(ctx, peer); return; @@ -650,8 +650,8 @@ static void send_handshake(fastd_context *ctx, fastd_peer *peer) { schedule_new_handshake(ctx, peer); } -static void handle_tasks(fastd_context *ctx) { - fastd_task *task; +static void handle_tasks(fastd_context_t *ctx) { + fastd_task_t *task; while ((task = fastd_task_get(ctx)) != NULL) { switch (task->type) { case TASK_HANDSHAKE: @@ -679,9 +679,9 @@ static void handle_tasks(fastd_context *ctx) { } } -static void handle_tun(fastd_context *ctx) { +static void handle_tun(fastd_context_t *ctx) { size_t max_len = fastd_max_packet_size(ctx); - fastd_buffer buffer = fastd_buffer_alloc(max_len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); + fastd_buffer_t buffer = fastd_buffer_alloc(max_len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); ssize_t len = read(ctx->tunfd, buffer.data, max_len); if (len < 0) { @@ -695,10 +695,10 @@ static void handle_tun(fastd_context *ctx) { buffer.len = len; - fastd_peer *peer = NULL; + fastd_peer_t *peer = NULL; if (ctx->conf->mode == MODE_TAP) { - const fastd_eth_addr *dest_addr = fastd_get_dest_address(ctx, buffer); + const fastd_eth_addr_t *dest_addr = fastd_get_dest_address(ctx, buffer); if (fastd_eth_addr_is_unicast(dest_addr)) { peer = fastd_peer_find_by_eth_addr(ctx, dest_addr); @@ -718,7 +718,7 @@ static void handle_tun(fastd_context *ctx) { if (peer == NULL) { for (peer = ctx->peers; peer; peer = peer->next) { if (fastd_peer_is_established(peer)) { - fastd_buffer send_buffer = fastd_buffer_alloc(len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); + fastd_buffer_t send_buffer = fastd_buffer_alloc(len, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx)); memcpy(send_buffer.data, buffer.data, len); ctx->conf->protocol->send(ctx, peer, send_buffer); } @@ -728,12 +728,12 @@ static void handle_tun(fastd_context *ctx) { } } -static void handle_socket(fastd_context *ctx, fastd_socket *sock) { +static void handle_socket(fastd_context_t *ctx, fastd_socket_t *sock) { size_t max_len = PACKET_TYPE_LEN + methods_max_packet_size(ctx); - fastd_buffer buffer = fastd_buffer_alloc(max_len, methods_min_decrypt_head_space(ctx), methods_min_decrypt_tail_space(ctx)); + fastd_buffer_t buffer = fastd_buffer_alloc(max_len, methods_min_decrypt_head_space(ctx), methods_min_decrypt_tail_space(ctx)); uint8_t *packet_type; - fastd_peer_address recvaddr; + fastd_peer_address_t recvaddr; socklen_t recvaddrlen = sizeof(recvaddr); ssize_t len = recvfrom(sock->fd, buffer.data, buffer.len, 0, (struct sockaddr*)&recvaddr, &recvaddrlen); @@ -752,7 +752,7 @@ static void handle_socket(fastd_context *ctx, fastd_socket *sock) { fastd_buffer_push_head(&buffer, 1); - fastd_peer *peer = NULL; + fastd_peer_t *peer = NULL; if (sock->peer) { if (fastd_peer_address_equal(&sock->peer->address, &recvaddr)) { @@ -808,8 +808,8 @@ static void handle_socket(fastd_context *ctx, fastd_socket *sock) { } } -static void handle_resolv_returns(fastd_context *ctx) { - fastd_resolve_return resolve_return; +static void handle_resolv_returns(fastd_context_t *ctx) { + fastd_resolve_return_t resolve_return; if (read(ctx->resolverfd, &resolve_return, sizeof(resolve_return)) < 0) { if (errno != EINTR) @@ -818,7 +818,7 @@ static void handle_resolv_returns(fastd_context *ctx) { return; } - fastd_peer *peer; + fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { if (!peer->config) continue; @@ -844,7 +844,7 @@ static void handle_resolv_returns(fastd_context *ctx) { free(resolve_return.hostname); } -static inline void handle_socket_error(fastd_context *ctx, fastd_socket *sock) { +static inline void handle_socket_error(fastd_context_t *ctx, fastd_socket_t *sock) { if (sock->addr->bindtodev) pr_warn(ctx, "socket bind %I on `%s' lost", &sock->addr->addr, sock->addr->bindtodev); else @@ -853,7 +853,7 @@ static inline void handle_socket_error(fastd_context *ctx, fastd_socket *sock) { fastd_socket_close(ctx, sock); } -static void handle_input(fastd_context *ctx) { +static void handle_input(fastd_context_t *ctx) { const size_t n_fds = 2 + ctx->n_socks + ctx->n_peers; struct pollfd fds[n_fds]; fds[0].fd = ctx->tunfd; @@ -867,7 +867,7 @@ static void handle_input(fastd_context *ctx) { fds[i].events = POLLIN; } - fastd_peer *peer; + fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { if (peer->sock && fastd_peer_is_socket_dynamic(peer)) fds[i].fd = peer->sock->fd; @@ -922,8 +922,8 @@ static void handle_input(fastd_context *ctx) { exit_bug(ctx, "fd count mismatch"); } -static void cleanup_peers(fastd_context *ctx) { - fastd_peer *peer, *next; +static void cleanup_peers(fastd_context_t *ctx) { + fastd_peer_t *peer, *next; for (peer = ctx->peers; peer; peer = next) { next = peer->next; @@ -935,7 +935,7 @@ static void cleanup_peers(fastd_context *ctx) { } } -static void maintenance(fastd_context *ctx) { +static void maintenance(fastd_context_t *ctx) { cleanup_peers(ctx); fastd_peer_eth_addr_cleanup(ctx); @@ -943,7 +943,7 @@ static void maintenance(fastd_context *ctx) { } -static void close_fds(fastd_context *ctx) { +static void close_fds(fastd_context_t *ctx) { struct rlimit rl; int fd, maxfd; @@ -965,7 +965,7 @@ static void close_fds(fastd_context *ctx) { } } -static void write_pid(fastd_context *ctx, pid_t pid) { +static void write_pid(fastd_context_t *ctx, pid_t pid) { if (!ctx->conf->pid_file) return; @@ -983,7 +983,7 @@ static void write_pid(fastd_context *ctx, pid_t pid) { } int main(int argc, char *argv[]) { - fastd_context ctx; + fastd_context_t ctx; memset(&ctx, 0, sizeof(ctx)); close_fds(&ctx); @@ -993,7 +993,7 @@ int main(int argc, char *argv[]) { init_signals(&ctx); init_pipes(&ctx); - fastd_config conf; + fastd_config_t conf; fastd_configure(&ctx, &conf, argc, argv); ctx.conf = &conf; diff --git a/src/fastd.h b/src/fastd.h index 4d85cc7..0b29797 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -54,7 +54,7 @@ #define MAX_METHODS 3 -struct _fastd_buffer { +struct fastd_buffer { void *base; size_t base_len; @@ -62,115 +62,115 @@ struct _fastd_buffer { size_t len; }; -struct _fastd_eth_addr { +struct fastd_eth_addr { uint8_t data[ETH_ALEN]; }; -struct _fastd_protocol { +struct fastd_protocol { const char *name; - fastd_protocol_config* (*init)(fastd_context *ctx); - void (*peer_configure)(fastd_context *ctx, fastd_peer_config *peer_conf); + fastd_protocol_config_t* (*init)(fastd_context_t *ctx); + void (*peer_configure)(fastd_context_t *ctx, fastd_peer_config_t *peer_conf); - void (*handshake_init)(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, const fastd_peer_config *peer_conf); - void (*handshake_handle)(fastd_context *ctx, fastd_socket *sock, const fastd_peer_address *address, const fastd_peer_config *peer_conf, const fastd_handshake *handshake, const fastd_method *method); + void (*handshake_init)(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf); + void (*handshake_handle)(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, const fastd_handshake_t *handshake, const fastd_method_t *method); - void (*handle_recv)(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer); - void (*send)(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer); + void (*handle_recv)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer); + void (*send)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer); - void (*init_peer_state)(fastd_context *ctx, fastd_peer *peer); - void (*reset_peer_state)(fastd_context *ctx, fastd_peer *peer); - void (*free_peer_state)(fastd_context *ctx, fastd_peer *peer); + void (*init_peer_state)(fastd_context_t *ctx, fastd_peer_t *peer); + void (*reset_peer_state)(fastd_context_t *ctx, fastd_peer_t *peer); + void (*free_peer_state)(fastd_context_t *ctx, fastd_peer_t *peer); - void (*generate_key)(fastd_context *ctx); - void (*show_key)(fastd_context *ctx); + void (*generate_key)(fastd_context_t *ctx); + void (*show_key)(fastd_context_t *ctx); }; -struct _fastd_method { +struct fastd_method { const char *name; - size_t (*max_packet_size)(fastd_context *ctx); - size_t (*min_encrypt_head_space)(fastd_context *ctx); - size_t (*min_decrypt_head_space)(fastd_context *ctx); - size_t (*min_encrypt_tail_space)(fastd_context *ctx); - size_t (*min_decrypt_tail_space)(fastd_context *ctx); + size_t (*max_packet_size)(fastd_context_t *ctx); + size_t (*min_encrypt_head_space)(fastd_context_t *ctx); + size_t (*min_decrypt_head_space)(fastd_context_t *ctx); + size_t (*min_encrypt_tail_space)(fastd_context_t *ctx); + size_t (*min_decrypt_tail_space)(fastd_context_t *ctx); - fastd_method_session_state* (*session_init)(fastd_context *ctx, uint8_t *secret, size_t length, bool initiator); - bool (*session_is_valid)(fastd_context *ctx, fastd_method_session_state *session); - bool (*session_is_initiator)(fastd_context *ctx, fastd_method_session_state *session); - bool (*session_want_refresh)(fastd_context *ctx, fastd_method_session_state *session); - void (*session_free)(fastd_context *ctx, fastd_method_session_state *session); + fastd_method_session_state_t* (*session_init)(fastd_context_t *ctx, uint8_t *secret, size_t length, bool initiator); + bool (*session_is_valid)(fastd_context_t *ctx, fastd_method_session_state_t *session); + bool (*session_is_initiator)(fastd_context_t *ctx, fastd_method_session_state_t *session); + bool (*session_want_refresh)(fastd_context_t *ctx, fastd_method_session_state_t *session); + void (*session_free)(fastd_context_t *ctx, fastd_method_session_state_t *session); - bool (*encrypt)(fastd_context *ctx, fastd_peer *peer, fastd_method_session_state *session, fastd_buffer *out, fastd_buffer in); - bool (*decrypt)(fastd_context *ctx, fastd_peer *peer, fastd_method_session_state *session, fastd_buffer *out, fastd_buffer in); + bool (*encrypt)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in); + bool (*decrypt)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in); }; -union _fastd_peer_address { +union fastd_peer_address { struct sockaddr sa; struct sockaddr_in in; struct sockaddr_in6 in6; }; -struct _fastd_resolve_return { +struct fastd_resolve_return { char *hostname; - fastd_peer_address constraints; + fastd_peer_address_t constraints; - fastd_peer_address addr; + fastd_peer_address_t addr; }; -struct _fastd_log_file { - fastd_log_file *next; +struct fastd_log_file { + fastd_log_file_t *next; int level; char *filename; }; -struct _fastd_log_fd { - fastd_log_fd *next; +struct fastd_log_fd { + fastd_log_fd_t *next; - fastd_log_file *config; + fastd_log_file_t *config; int fd; }; -struct _fastd_bind_address { - fastd_bind_address *next; - fastd_peer_address addr; +struct fastd_bind_address { + fastd_bind_address_t *next; + fastd_peer_address_t addr; char *bindtodev; }; -struct _fastd_socket { +struct fastd_socket { int fd; - const fastd_bind_address *addr; - fastd_peer *peer; + const fastd_bind_address_t *addr; + fastd_peer_t *peer; }; -struct _fastd_peer_group_config { - fastd_peer_group_config *next; - fastd_peer_group_config *parent; - fastd_peer_group_config *children; +struct fastd_peer_group_config { + fastd_peer_group_config_t *next; + fastd_peer_group_config_t *parent; + fastd_peer_group_config_t *children; char *name; - fastd_string_stack *peer_dirs; + fastd_string_stack_t *peer_dirs; /* contraints */ unsigned max_connections; }; -struct _fastd_peer_group { - fastd_peer_group *next; - fastd_peer_group *parent; - fastd_peer_group *children; +struct fastd_peer_group { + fastd_peer_group_t *next; + fastd_peer_group_t *parent; + fastd_peer_group_t *children; - const fastd_peer_group_config *conf; + const fastd_peer_group_config_t *conf; unsigned n_connections; }; -struct _fastd_config { +struct fastd_config { int log_stderr_level; int log_syslog_level; char *log_syslog_ident; - fastd_log_file *log_files; + fastd_log_file_t *log_files; unsigned keepalive_interval; unsigned peer_stale_time; @@ -185,32 +185,32 @@ struct _fastd_config { char *ifname; unsigned n_bind_addrs; - fastd_bind_address *bind_addrs; + fastd_bind_address_t *bind_addrs; - fastd_bind_address *bind_addr_default_v4; - fastd_bind_address *bind_addr_default_v6; + fastd_bind_address_t *bind_addr_default_v4; + fastd_bind_address_t *bind_addr_default_v6; uint16_t mtu; - fastd_mode mode; + fastd_mode_t mode; bool forward; - const fastd_protocol *protocol; - const fastd_method *methods[MAX_METHODS]; - const fastd_method *method_default; + const fastd_protocol_t *protocol; + const fastd_method_t *methods[MAX_METHODS]; + const fastd_method_t *method_default; char *secret; unsigned key_valid; unsigned key_refresh; #ifdef USE_CRYPTO_AES128CTR - const fastd_crypto_aes128ctr *crypto_aes128ctr; + const fastd_crypto_aes128ctr_t *crypto_aes128ctr; #endif #ifdef USE_CRYPTO_GHASH - const fastd_crypto_ghash *crypto_ghash; + const fastd_crypto_ghash_t *crypto_ghash; #endif - fastd_peer_group_config *peer_group; - fastd_peer_config *peers; + fastd_peer_group_config_t *peer_group; + fastd_peer_config_t *peers; unsigned n_floating; unsigned n_v4; @@ -219,7 +219,7 @@ struct _fastd_config { unsigned n_dynamic_v4; unsigned n_dynamic_v6; - fastd_protocol_config *protocol_config; + fastd_protocol_config_t *protocol_config; char *on_up; char *on_up_dir; @@ -241,19 +241,19 @@ struct _fastd_config { bool show_key; }; -struct _fastd_context { - const fastd_config *conf; +struct fastd_context { + const fastd_config_t *conf; - fastd_log_fd *log_files; + fastd_log_fd_t *log_files; char *ifname; struct timespec now; unsigned n_peers; - fastd_peer_group *peer_group; - fastd_peer *peers; - fastd_queue task_queue; + fastd_peer_group_t *peer_group; + fastd_peer_t *peers; + fastd_queue_t task_queue; int resolverfd; int resolvewfd; @@ -261,61 +261,61 @@ struct _fastd_context { int tunfd; unsigned n_socks; - fastd_socket *socks; + fastd_socket_t *socks; - fastd_socket *sock_default_v4; - fastd_socket *sock_default_v6; + fastd_socket_t *sock_default_v4; + fastd_socket_t *sock_default_v6; #ifdef USE_CRYPTO_AES128CTR - fastd_crypto_aes128ctr_context *crypto_aes128ctr; + fastd_crypto_aes128ctr_context_t *crypto_aes128ctr; #endif #ifdef USE_CRYPTO_GHASH - fastd_crypto_ghash_context *crypto_ghash; + fastd_crypto_ghash_context_t *crypto_ghash; #endif size_t eth_addr_size; size_t n_eth_addr; - fastd_peer_eth_addr *eth_addr; + fastd_peer_eth_addr_t *eth_addr; unsigned int randseed; - fastd_protocol_state *protocol_state; + fastd_protocol_state_t *protocol_state; }; -struct _fastd_string_stack { - fastd_string_stack *next; +struct fastd_string_stack { + fastd_string_stack_t *next; char str[]; }; -void fastd_send(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, fastd_buffer buffer); -void fastd_send_handshake(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, fastd_buffer buffer); -void fastd_handle_receive(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer); +void fastd_send(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_buffer_t buffer); +void fastd_send_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_buffer_t buffer); +void fastd_handle_receive(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer); -fastd_socket* fastd_socket_open(fastd_context *ctx, fastd_peer *peer, int af); +fastd_socket_t* fastd_socket_open(fastd_context_t *ctx, fastd_peer_t *peer, int af); -void fastd_resolve_peer(fastd_context *ctx, fastd_peer *peer); +void fastd_resolve_peer(fastd_context_t *ctx, fastd_peer_t *peer); -int fastd_vsnprintf(const fastd_context *ctx, char *buffer, size_t size, const char *format, va_list ap); -void fastd_logf(const fastd_context *ctx, int level, const char *format, ...); +int fastd_vsnprintf(const fastd_context_t *ctx, char *buffer, size_t size, const char *format, va_list ap); +void fastd_logf(const fastd_context_t *ctx, int level, const char *format, ...); -void fastd_read_peer_dir(fastd_context *ctx, fastd_config *conf, const char *dir); -bool fastd_read_config(fastd_context *ctx, fastd_config *conf, const char *filename, bool peer_config, int depth); +void fastd_read_peer_dir(fastd_context_t *ctx, fastd_config_t *conf, const char *dir); +bool fastd_read_config(fastd_context_t *ctx, fastd_config_t *conf, const char *filename, bool peer_config, int depth); -bool fastd_config_protocol(fastd_context *ctx, fastd_config *conf, const char *name); -bool fastd_config_method(fastd_context *ctx, fastd_config *conf, const char *name); -bool fastd_config_crypto(fastd_context *ctx, fastd_config *conf, const char *alg, const char *impl); -bool fastd_config_add_log_file(fastd_context *ctx, fastd_config *conf, const char *name, int level); -void fastd_config_bind_address(fastd_context *ctx, fastd_config *conf, const fastd_peer_address *address, const char *bindtodev, bool default_v4, bool default_v6); -void fastd_config_peer_group_push(fastd_context *ctx, fastd_config *conf, const char *name); -void fastd_config_peer_group_pop(fastd_context *ctx, fastd_config *conf); -void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *const argv[]); -void fastd_reconfigure(fastd_context *ctx, fastd_config *conf); -void fastd_config_release(fastd_context *ctx, fastd_config *conf); +bool fastd_config_protocol(fastd_context_t *ctx, fastd_config_t *conf, const char *name); +bool fastd_config_method(fastd_context_t *ctx, fastd_config_t *conf, const char *name); +bool fastd_config_crypto(fastd_context_t *ctx, fastd_config_t *conf, const char *alg, const char *impl); +bool fastd_config_add_log_file(fastd_context_t *ctx, fastd_config_t *conf, const char *name, int level); +void fastd_config_bind_address(fastd_context_t *ctx, fastd_config_t *conf, const fastd_peer_address_t *address, const char *bindtodev, bool default_v4, bool default_v6); +void fastd_config_peer_group_push(fastd_context_t *ctx, fastd_config_t *conf, const char *name); +void fastd_config_peer_group_pop(fastd_context_t *ctx, fastd_config_t *conf); +void fastd_config_release(fastd_context_t *ctx, fastd_config_t *conf); +void fastd_configure(fastd_context_t *ctx, fastd_config_t *conf, int argc, char *const argv[]); +void fastd_reconfigure(fastd_context_t *ctx, fastd_config_t *conf); -void fastd_random_bytes(fastd_context *ctx, void *buffer, size_t len, bool secure); +void fastd_random_bytes(fastd_context_t *ctx, void *buffer, size_t len, bool secure); -static inline int fastd_rand(fastd_context *ctx, int min, int max) { +static inline int fastd_rand(fastd_context_t *ctx, int min, int max) { unsigned int r = (unsigned int)rand_r(&ctx->randseed); return (r%(max-min) + min); } @@ -348,20 +348,20 @@ static inline size_t alignto(size_t l, size_t a) { return ((l+a-1)/a)*a; } -static inline fastd_buffer fastd_buffer_alloc(size_t len, size_t head_space, size_t tail_space) { +static inline fastd_buffer_t fastd_buffer_alloc(size_t len, size_t head_space, size_t tail_space) { size_t base_len = head_space+len+tail_space; void *ptr; if (posix_memalign(&ptr, 16, base_len)) - return (fastd_buffer){ .base = NULL, .base_len = 0, .data = NULL, .len = 0 }; + return (fastd_buffer_t){ .base = NULL, .base_len = 0, .data = NULL, .len = 0 }; - return (fastd_buffer){ .base = ptr, .base_len = base_len, .data = ptr+head_space, .len = len }; + return (fastd_buffer_t){ .base = ptr, .base_len = base_len, .data = ptr+head_space, .len = len }; } -static inline void fastd_buffer_free(fastd_buffer buffer) { +static inline void fastd_buffer_free(fastd_buffer_t buffer) { free(buffer.base); } -static inline void fastd_buffer_pull_head(fastd_buffer *buffer, size_t len) { +static inline void fastd_buffer_pull_head(fastd_buffer_t *buffer, size_t len) { buffer->data -= len; buffer->len += len; @@ -369,7 +369,7 @@ static inline void fastd_buffer_pull_head(fastd_buffer *buffer, size_t len) { abort(); } -static inline void fastd_buffer_push_head(fastd_buffer *buffer, size_t len) { +static inline void fastd_buffer_push_head(fastd_buffer_t *buffer, size_t len) { if (buffer->len < len) abort(); @@ -377,7 +377,7 @@ static inline void fastd_buffer_push_head(fastd_buffer *buffer, size_t len) { buffer->len -= len; } -static inline size_t fastd_max_packet_size(const fastd_context *ctx) { +static inline size_t fastd_max_packet_size(const fastd_context_t *ctx) { switch (ctx->conf->mode) { case MODE_TAP: return ctx->conf->mtu+ETH_HLEN; @@ -388,17 +388,17 @@ static inline size_t fastd_max_packet_size(const fastd_context *ctx) { } } -static inline fastd_string_stack* fastd_string_stack_dup(const char *str) { - fastd_string_stack *ret = malloc(sizeof(fastd_string_stack) + strlen(str) + 1); +static inline fastd_string_stack_t* fastd_string_stack_dup(const char *str) { + fastd_string_stack_t *ret = malloc(sizeof(fastd_string_stack_t) + strlen(str) + 1); ret->next = NULL; strcpy(ret->str, str); return ret; } -static inline fastd_string_stack* fastd_string_stack_dupn(const char *str, size_t len) { +static inline fastd_string_stack_t* fastd_string_stack_dupn(const char *str, size_t len) { size_t str_len = strnlen(str, len); - fastd_string_stack *ret = malloc(sizeof(fastd_string_stack) + str_len + 1); + fastd_string_stack_t *ret = malloc(sizeof(fastd_string_stack_t) + str_len + 1); ret->next = NULL; strncpy(ret->str, str, str_len); ret->str[str_len] = 0; @@ -406,23 +406,23 @@ static inline fastd_string_stack* fastd_string_stack_dupn(const char *str, size_ return ret; } -static inline fastd_string_stack* fastd_string_stack_push(fastd_string_stack *stack, const char *str) { - fastd_string_stack *ret = malloc(sizeof(fastd_string_stack) + strlen(str) + 1); +static inline fastd_string_stack_t* fastd_string_stack_push(fastd_string_stack_t *stack, const char *str) { + fastd_string_stack_t *ret = malloc(sizeof(fastd_string_stack_t) + strlen(str) + 1); ret->next = stack; strcpy(ret->str, str); return ret; } -static inline void fastd_string_stack_free(fastd_string_stack *str) { +static inline void fastd_string_stack_free(fastd_string_stack_t *str) { while(str) { - fastd_string_stack *next = str->next; + fastd_string_stack_t *next = str->next; free(str); str = next; } } -static inline void fastd_socket_close(fastd_context *ctx, fastd_socket *sock) { +static inline void fastd_socket_close(fastd_context_t *ctx, fastd_socket_t *sock) { if (sock->fd >= 0) { if(close(sock->fd)) pr_error_errno(ctx, "closing socket: close"); diff --git a/src/handshake.c b/src/handshake.c index 7bd627f..28a8ec1 100644 --- a/src/handshake.c +++ b/src/handshake.c @@ -60,7 +60,7 @@ static const char const *REPLY_TYPES[REPLY_MAX] = { #define AS_UINT16(ptr) ((*(uint8_t*)(ptr).data) + (*((uint8_t*)(ptr).data+1) << 8)) -static uint8_t* create_method_list(fastd_context *ctx, size_t *len) { +static uint8_t* create_method_list(fastd_context_t *ctx, size_t *len) { *len = strlen(ctx->conf->methods[0]->name); int i; @@ -84,7 +84,7 @@ static uint8_t* create_method_list(fastd_context *ctx, size_t *len) { return ret; } -fastd_buffer fastd_handshake_new_init(fastd_context *ctx, size_t tail_space) { +fastd_buffer_t fastd_handshake_new_init(fastd_context_t *ctx, size_t tail_space) { size_t version_len = strlen(FASTD_VERSION); size_t protocol_len = strlen(ctx->conf->protocol->name); size_t method_len = strlen(ctx->conf->method_default->name); @@ -92,7 +92,7 @@ fastd_buffer fastd_handshake_new_init(fastd_context *ctx, size_t tail_space) { size_t method_list_len; uint8_t *method_list = create_method_list(ctx, &method_list_len); - fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, + fastd_buffer_t buffer = fastd_buffer_alloc(sizeof(fastd_packet_t), 0, 2*5 + /* handshake type, mode */ 6 + /* MTU */ 4+version_len + /* version name */ @@ -101,7 +101,7 @@ fastd_buffer fastd_handshake_new_init(fastd_context *ctx, size_t tail_space) { 4+method_list_len + /* supported method name list */ tail_space ); - fastd_packet *request = buffer.data; + fastd_packet_t *request = buffer.data; request->rsv1 = 0; request->rsv2 = 0; @@ -120,7 +120,7 @@ fastd_buffer fastd_handshake_new_init(fastd_context *ctx, size_t tail_space) { return buffer; } -static const fastd_method* method_from_name(fastd_context *ctx, const char *name, size_t n) { +static const fastd_method_t* method_from_name(fastd_context_t *ctx, const char *name, size_t n) { int i; for (i = 0; i < MAX_METHODS; i++) { if (!ctx->conf->methods[i]) @@ -133,7 +133,7 @@ static const fastd_method* method_from_name(fastd_context *ctx, const char *name return NULL; } -fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, const fastd_handshake *handshake, const fastd_method *method, size_t tail_space) { +fastd_buffer_t fastd_handshake_new_reply(fastd_context_t *ctx, const fastd_handshake_t *handshake, const fastd_method_t *method, size_t tail_space) { bool first = (AS_UINT8(handshake->records[RECORD_HANDSHAKE_TYPE]) == 1); size_t version_len = strlen(FASTD_VERSION); size_t method_len = strlen(method->name); @@ -143,13 +143,13 @@ fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, const fastd_handshake extra_size = 6 + /* MTU */ 4+version_len; /* version name */ - fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, + fastd_buffer_t buffer = fastd_buffer_alloc(sizeof(fastd_packet_t), 0, 2*5 + /* handshake type, reply code */ 4+method_len + /* method name */ extra_size + tail_space ); - fastd_packet *request = buffer.data; + fastd_packet_t *request = buffer.data; request->rsv1 = 0; request->rsv2 = 0; @@ -166,12 +166,12 @@ fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, const fastd_handshake return buffer; } -static fastd_string_stack* parse_string_list(uint8_t *data, size_t len) { +static fastd_string_stack_t* parse_string_list(uint8_t *data, size_t len) { uint8_t *end = data+len; - fastd_string_stack *ret = NULL; + fastd_string_stack_t *ret = NULL; while (data < end) { - fastd_string_stack *part = fastd_string_stack_dupn((char*)data, end-data); + fastd_string_stack_t *part = fastd_string_stack_dupn((char*)data, end-data); part->next = ret; ret = part; data += strlen(part->str) + 1; @@ -180,16 +180,16 @@ static fastd_string_stack* parse_string_list(uint8_t *data, size_t len) { return ret; } -void fastd_handshake_handle(fastd_context *ctx, fastd_socket *sock, const fastd_peer_address *address, const fastd_peer_config *peer_conf, fastd_buffer buffer) { - if (buffer.len < sizeof(fastd_packet)) { +void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, fastd_buffer_t buffer) { + if (buffer.len < sizeof(fastd_packet_t)) { pr_warn(ctx, "received a short handshake from %I", address); goto end_free; } - fastd_handshake handshake; + fastd_handshake_t handshake; memset(&handshake, 0, sizeof(handshake)); - fastd_packet *packet = buffer.data; + fastd_packet_t *packet = buffer.data; uint8_t *ptr = packet->tlv_data; while (true) { @@ -253,14 +253,14 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_socket *sock, const fastd_ goto send_reply; } - const fastd_method *method = NULL; + const fastd_method_t *method = NULL; if (handshake.records[RECORD_METHOD_LIST].data && handshake.records[RECORD_METHOD_LIST].length) { - fastd_string_stack *method_list = parse_string_list(handshake.records[RECORD_METHOD_LIST].data, handshake.records[RECORD_METHOD_LIST].length); + fastd_string_stack_t *method_list = parse_string_list(handshake.records[RECORD_METHOD_LIST].data, handshake.records[RECORD_METHOD_LIST].length); - fastd_string_stack *method_name = method_list; + fastd_string_stack_t *method_name = method_list; while (method_name) { - const fastd_method *cur_method = method_from_name(ctx, method_name->str, SIZE_MAX); + const fastd_method_t *cur_method = method_from_name(ctx, method_name->str, SIZE_MAX); if (cur_method) method = cur_method; @@ -294,8 +294,8 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_socket *sock, const fastd_ send_reply: if (reply_code) { - fastd_buffer reply_buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, 3*5 /* enough space for handshake type, reply code and error detail */); - fastd_packet *reply = reply_buffer.data; + fastd_buffer_t reply_buffer = fastd_buffer_alloc(sizeof(fastd_packet_t), 0, 3*5 /* enough space for handshake type, reply code and error detail */); + fastd_packet_t *reply = reply_buffer.data; reply->rsv1 = 0; reply->rsv2 = 0; @@ -319,7 +319,7 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_socket *sock, const fastd_ uint8_t reply_code = AS_UINT8(handshake.records[RECORD_REPLY_CODE]); if (reply_code == REPLY_SUCCESS) { - const fastd_method *method = ctx->conf->method_default; + const fastd_method_t *method = ctx->conf->method_default; if (handshake.records[RECORD_METHOD_NAME].data) { method = method_from_name(ctx, handshake.records[RECORD_METHOD_NAME].data, handshake.records[RECORD_METHOD_NAME].length); diff --git a/src/handshake.h b/src/handshake.h index 91bf751..ff9a859 100644 --- a/src/handshake.h +++ b/src/handshake.h @@ -30,7 +30,7 @@ #include "fastd.h" -typedef enum _fastd_handshake_record_type { +typedef enum fastd_handshake_record_type { RECORD_HANDSHAKE_TYPE = 0, RECORD_REPLY_CODE, RECORD_ERROR_DETAIL, @@ -47,34 +47,34 @@ typedef enum _fastd_handshake_record_type { RECORD_VERSION_NAME, RECORD_METHOD_LIST, RECORD_MAX, -} fastd_handshake_record_type; +} fastd_handshake_record_type_t; -typedef enum _fastd_reply_code { +typedef enum fastd_reply_code { REPLY_SUCCESS = 0, REPLY_MANDATORY_MISSING, REPLY_UNACCEPTABLE_VALUE, REPLY_MAX, -} fastd_reply_code; +} fastd_reply_code_t; -typedef struct _fastd_handshake_record { +typedef struct fastd_handshake_record { size_t length; void *data; -} fastd_handshake_record; +} fastd_handshake_record_t; -struct _fastd_handshake { +struct fastd_handshake { uint8_t type; - fastd_handshake_record records[RECORD_MAX]; + fastd_handshake_record_t records[RECORD_MAX]; }; -fastd_buffer fastd_handshake_new_init(fastd_context *ctx, size_t tail_space); -fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, const fastd_handshake *handshake, const fastd_method *method, size_t tail_space); +fastd_buffer_t fastd_handshake_new_init(fastd_context_t *ctx, size_t tail_space); +fastd_buffer_t fastd_handshake_new_reply(fastd_context_t *ctx, const fastd_handshake_t *handshake, const fastd_method_t *method, size_t tail_space); -void fastd_handshake_handle(fastd_context *ctx, fastd_socket *sock, const fastd_peer_address *address, const fastd_peer_config *peer_conf, fastd_buffer buffer); +void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, fastd_buffer_t buffer); -static inline void fastd_handshake_add(fastd_context *ctx, fastd_buffer *buffer, fastd_handshake_record_type type, size_t len, const void *data) { +static inline void fastd_handshake_add(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, size_t len, const void *data) { if ((uint8_t*)buffer->data + buffer->len + 4 + len > (uint8_t*)buffer->base + buffer->base_len) exit_bug(ctx, "not enough buffer allocated for handshake"); @@ -89,7 +89,7 @@ static inline void fastd_handshake_add(fastd_context *ctx, fastd_buffer *buffer, buffer->len += 4 + len; } -static inline void fastd_handshake_add_uint8(fastd_context *ctx, fastd_buffer *buffer, fastd_handshake_record_type type, uint8_t value) { +static inline void fastd_handshake_add_uint8(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint8_t value) { if ((uint8_t*)buffer->data + buffer->len + 5 > (uint8_t*)buffer->base + buffer->base_len) exit_bug(ctx, "not enough buffer allocated for handshake"); @@ -104,7 +104,7 @@ static inline void fastd_handshake_add_uint8(fastd_context *ctx, fastd_buffer *b buffer->len += 5; } -static inline void fastd_handshake_add_uint16(fastd_context *ctx, fastd_buffer *buffer, fastd_handshake_record_type type, uint16_t value) { +static inline void fastd_handshake_add_uint16(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint16_t value) { if ((uint8_t*)buffer->data + buffer->len + 6 > (uint8_t*)buffer->base + buffer->base_len) exit_bug(ctx, "not enough buffer allocated for handshake"); diff --git a/src/method_aes128_gcm.c b/src/method_aes128_gcm.c index f21904e..ea6b81f 100644 --- a/src/method_aes128_gcm.c +++ b/src/method_aes128_gcm.c @@ -31,7 +31,7 @@ #define KEYBYTES 16 #define NONCEBYTES 7 -struct _fastd_method_session_state { +struct fastd_method_session_state { struct timespec valid_till; struct timespec refresh_after; @@ -41,8 +41,8 @@ struct _fastd_method_session_state { struct timespec receive_last; uint64_t receive_reorder_seen; - fastd_crypto_aes128ctr_state *cstate_aes128ctr; - fastd_crypto_ghash_state *cstate_ghash; + fastd_crypto_aes128ctr_state_t *cstate_aes128ctr; + fastd_crypto_ghash_state_t *cstate_ghash; }; @@ -75,35 +75,35 @@ static inline bool is_nonce_valid(const uint8_t nonce[NONCEBYTES], const uint8_t return true; } -static size_t method_max_packet_size(fastd_context *ctx) { - return (fastd_max_packet_size(ctx) + NONCEBYTES + sizeof(fastd_block128)); +static size_t method_max_packet_size(fastd_context_t *ctx) { + return (fastd_max_packet_size(ctx) + NONCEBYTES + sizeof(fastd_block128_t)); } -static size_t method_min_encrypt_head_space(fastd_context *ctx) { - return sizeof(fastd_block128); +static size_t method_min_encrypt_head_space(fastd_context_t *ctx) { + return sizeof(fastd_block128_t); } -static size_t method_min_decrypt_head_space(fastd_context *ctx) { +static size_t method_min_decrypt_head_space(fastd_context_t *ctx) { return 0; } -static size_t method_min_encrypt_tail_space(fastd_context *ctx) { - return (sizeof(fastd_block128)-1); +static size_t method_min_encrypt_tail_space(fastd_context_t *ctx) { + return (sizeof(fastd_block128_t)-1); } -static size_t method_min_decrypt_tail_space(fastd_context *ctx) { - return (2*sizeof(fastd_block128)-1); +static size_t method_min_decrypt_tail_space(fastd_context_t *ctx) { + return (2*sizeof(fastd_block128_t)-1); } -static fastd_method_session_state* method_session_init(fastd_context *ctx, uint8_t *secret, size_t length, bool initiator) { +static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, uint8_t *secret, size_t length, bool initiator) { int i; if (length < KEYBYTES) exit_bug(ctx, "aes128-gcm: tried to init with short secret"); - fastd_method_session_state *session = malloc(sizeof(fastd_method_session_state)); + fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t)); session->valid_till = ctx->now; session->valid_till.tv_sec += ctx->conf->key_valid; @@ -111,14 +111,14 @@ static fastd_method_session_state* method_session_init(fastd_context *ctx, uint8 session->refresh_after = ctx->now; session->refresh_after.tv_sec += ctx->conf->key_refresh; - fastd_block128 key; - memcpy(key.b, secret, sizeof(fastd_block128)); + fastd_block128_t key; + memcpy(key.b, secret, sizeof(fastd_block128_t)); session->cstate_aes128ctr = ctx->conf->crypto_aes128ctr->set_key(ctx, ctx->crypto_aes128ctr, &key); - static const fastd_block128 zeroblock = {}; - fastd_block128 H; + static const fastd_block128_t zeroblock = {}; + fastd_block128_t H; - ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, &H, &zeroblock, sizeof(fastd_block128), &zeroblock); + ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, &H, &zeroblock, sizeof(fastd_block128_t), &zeroblock); session->cstate_ghash = ctx->conf->crypto_ghash->set_h(ctx, ctx->crypto_ghash, &H); @@ -133,72 +133,72 @@ static fastd_method_session_state* method_session_init(fastd_context *ctx, uint8 return session; } -static bool method_session_is_valid(fastd_context *ctx, fastd_method_session_state *session) { +static bool method_session_is_valid(fastd_context_t *ctx, fastd_method_session_state_t *session) { return (session && timespec_after(&session->valid_till, &ctx->now)); } -static bool method_session_is_initiator(fastd_context *ctx, fastd_method_session_state *session) { +static bool method_session_is_initiator(fastd_context_t *ctx, fastd_method_session_state_t *session) { return (session->send_nonce[0] & 1); } -static bool method_session_want_refresh(fastd_context *ctx, fastd_method_session_state *session) { +static bool method_session_want_refresh(fastd_context_t *ctx, fastd_method_session_state_t *session) { return timespec_after(&ctx->now, &session->refresh_after); } -static void method_session_free(fastd_context *ctx, fastd_method_session_state *session) { +static void method_session_free(fastd_context_t *ctx, fastd_method_session_state_t *session) { if(session) { ctx->conf->crypto_aes128ctr->free_state(ctx, session->cstate_aes128ctr); ctx->conf->crypto_ghash->free_state(ctx, session->cstate_ghash); - memset(session, 0, sizeof(fastd_method_session_state)); + memset(session, 0, sizeof(fastd_method_session_state_t)); free(session); } } -static inline void put_size(fastd_block128 *out, size_t len) { - memset(out, 0, sizeof(fastd_block128)-5); - out->b[sizeof(fastd_block128)-5] = len >> 29; - out->b[sizeof(fastd_block128)-4] = len >> 21; - out->b[sizeof(fastd_block128)-3] = len >> 13; - out->b[sizeof(fastd_block128)-2] = len >> 5; - out->b[sizeof(fastd_block128)-1] = len << 3; +static inline void put_size(fastd_block128_t *out, size_t len) { + memset(out, 0, sizeof(fastd_block128_t)-5); + out->b[sizeof(fastd_block128_t)-5] = len >> 29; + out->b[sizeof(fastd_block128_t)-4] = len >> 21; + out->b[sizeof(fastd_block128_t)-3] = len >> 13; + out->b[sizeof(fastd_block128_t)-2] = len >> 5; + out->b[sizeof(fastd_block128_t)-1] = len << 3; } -static bool method_encrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_session_state *session, fastd_buffer *out, fastd_buffer in) { - fastd_buffer_pull_head(&in, sizeof(fastd_block128)); - memset(in.data, 0, sizeof(fastd_block128)); +static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { + fastd_buffer_pull_head(&in, sizeof(fastd_block128_t)); + memset(in.data, 0, sizeof(fastd_block128_t)); - size_t tail_len = alignto(in.len, sizeof(fastd_block128))-in.len; - *out = fastd_buffer_alloc(in.len, alignto(NONCEBYTES, 16), sizeof(fastd_block128)+tail_len); + size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; + *out = fastd_buffer_alloc(in.len, alignto(NONCEBYTES, 16), sizeof(fastd_block128_t)+tail_len); if (tail_len) memset(in.data+in.len, 0, tail_len); - fastd_block128 nonce; + fastd_block128_t nonce; memcpy(nonce.b, session->send_nonce, NONCEBYTES); - memset(nonce.b+NONCEBYTES, 0, sizeof(fastd_block128)-NONCEBYTES-1); - nonce.b[sizeof(fastd_block128)-1] = 1; + memset(nonce.b+NONCEBYTES, 0, sizeof(fastd_block128_t)-NONCEBYTES-1); + nonce.b[sizeof(fastd_block128_t)-1] = 1; - int n_blocks = (in.len+sizeof(fastd_block128)-1)/sizeof(fastd_block128); + int n_blocks = (in.len+sizeof(fastd_block128_t)-1)/sizeof(fastd_block128_t); - fastd_block128 *inblocks = in.data; - fastd_block128 *outblocks = out->data; - fastd_block128 sig; + fastd_block128_t *inblocks = in.data; + fastd_block128_t *outblocks = out->data; + fastd_block128_t sig; - bool ok = ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, outblocks, inblocks, n_blocks*sizeof(fastd_block128), &nonce); + bool ok = ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), &nonce); if (ok) { if (tail_len) memset(out->data+out->len, 0, tail_len); - put_size(&outblocks[n_blocks], in.len-sizeof(fastd_block128)); + put_size(&outblocks[n_blocks], in.len-sizeof(fastd_block128_t)); ok = ctx->conf->crypto_ghash->hash(ctx, session->cstate_ghash, &sig, outblocks+1, n_blocks); } if (!ok) { /* restore original buffer */ - fastd_buffer_push_head(&in, sizeof(fastd_block128)); + fastd_buffer_push_head(&in, sizeof(fastd_block128_t)); fastd_buffer_free(*out); return false; } @@ -214,17 +214,17 @@ static bool method_encrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_se return true; } -static bool method_decrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_session_state *session, fastd_buffer *out, fastd_buffer in) { - if (in.len < NONCEBYTES+sizeof(fastd_block128)) +static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { + if (in.len < NONCEBYTES+sizeof(fastd_block128_t)) return false; if (!method_session_is_valid(ctx, session)) return false; - fastd_block128 nonce; + fastd_block128_t nonce; memcpy(nonce.b, in.data, NONCEBYTES); - memset(nonce.b+NONCEBYTES, 0, sizeof(fastd_block128)-NONCEBYTES-1); - nonce.b[sizeof(fastd_block128)-1] = 1; + memset(nonce.b+NONCEBYTES, 0, sizeof(fastd_block128_t)-NONCEBYTES-1); + nonce.b[sizeof(fastd_block128_t)-1] = 1; int64_t age; if (!is_nonce_valid(nonce.b, session->receive_nonce, &age)) @@ -240,27 +240,27 @@ static bool method_decrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_se fastd_buffer_push_head(&in, NONCEBYTES); - size_t tail_len = alignto(in.len, sizeof(fastd_block128))-in.len; + size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; *out = fastd_buffer_alloc(in.len, 0, tail_len); - int n_blocks = (in.len+sizeof(fastd_block128)-1)/sizeof(fastd_block128); + int n_blocks = (in.len+sizeof(fastd_block128_t)-1)/sizeof(fastd_block128_t); - fastd_block128 *inblocks = in.data; - fastd_block128 *outblocks = out->data; - fastd_block128 sig; + fastd_block128_t *inblocks = in.data; + fastd_block128_t *outblocks = out->data; + fastd_block128_t sig; - bool ok = ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, outblocks, inblocks, n_blocks*sizeof(fastd_block128), &nonce); + bool ok = ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), &nonce); if (ok) { if (tail_len) memset(in.data+in.len, 0, tail_len); - put_size(&inblocks[n_blocks], in.len-sizeof(fastd_block128)); + put_size(&inblocks[n_blocks], in.len-sizeof(fastd_block128_t)); ok = ctx->conf->crypto_ghash->hash(ctx, session->cstate_ghash, &sig, inblocks+1, n_blocks); } - if (!ok || memcmp(&sig, &outblocks[0], sizeof(fastd_block128)) != 0) { + if (!ok || memcmp(&sig, &outblocks[0], sizeof(fastd_block128_t)) != 0) { fastd_buffer_free(*out); /* restore input buffer */ @@ -271,7 +271,7 @@ static bool method_decrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_se fastd_buffer_free(in); - fastd_buffer_push_head(out, sizeof(fastd_block128)); + fastd_buffer_push_head(out, sizeof(fastd_block128_t)); if (age < 0) { session->receive_reorder_seen >>= age; @@ -292,7 +292,7 @@ static bool method_decrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_se return true; } -const fastd_method fastd_method_aes128_gcm = { +const fastd_method_t fastd_method_aes128_gcm = { .name = "aes128-gcm", .max_packet_size = method_max_packet_size, diff --git a/src/method_null.c b/src/method_null.c index 7cdbe00..1956fc5 100644 --- a/src/method_null.c +++ b/src/method_null.c @@ -27,42 +27,42 @@ #include "fastd.h" -static size_t method_max_packet_size(fastd_context *ctx) { +static size_t method_max_packet_size(fastd_context_t *ctx) { return fastd_max_packet_size(ctx); } -static size_t method_min_head_tail_space(fastd_context *ctx) { +static size_t method_min_head_tail_space(fastd_context_t *ctx) { return 0; } -static fastd_method_session_state* method_session_init(fastd_context *ctx, uint8_t *secret, size_t length, bool initiator) { +static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, uint8_t *secret, size_t length, bool initiator) { if (initiator) - return (fastd_method_session_state*)1; + return (fastd_method_session_state_t*)1; else - return (fastd_method_session_state*)2; + return (fastd_method_session_state_t*)2; } -static bool method_session_is_valid(fastd_context *ctx, fastd_method_session_state *session) { +static bool method_session_is_valid(fastd_context_t *ctx, fastd_method_session_state_t *session) { return session; } -static bool method_session_is_initiator(fastd_context *ctx, fastd_method_session_state *session) { - return (session == (fastd_method_session_state*)1); +static bool method_session_is_initiator(fastd_context_t *ctx, fastd_method_session_state_t *session) { + return (session == (fastd_method_session_state_t*)1); } -static bool method_session_want_refresh(fastd_context *ctx, fastd_method_session_state *session) { +static bool method_session_want_refresh(fastd_context_t *ctx, fastd_method_session_state_t *session) { return false; } -static void method_session_free(fastd_context *ctx, fastd_method_session_state *session) { +static void method_session_free(fastd_context_t *ctx, fastd_method_session_state_t *session) { } -static bool method_passthrough(fastd_context *ctx, fastd_peer *peer, fastd_method_session_state *session, fastd_buffer *out, fastd_buffer in) { +static bool method_passthrough(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { *out = in; return true; } -const fastd_method fastd_method_null = { +const fastd_method_t fastd_method_null = { .name = "null", .max_packet_size = method_max_packet_size, diff --git a/src/method_xsalsa20_poly1305.c b/src/method_xsalsa20_poly1305.c index 23f4c5d..37c7fb7 100644 --- a/src/method_xsalsa20_poly1305.c +++ b/src/method_xsalsa20_poly1305.c @@ -31,7 +31,7 @@ #define NONCEBYTES 7 -struct _fastd_method_session_state { +struct fastd_method_session_state { struct timespec valid_till; struct timespec refresh_after; @@ -74,29 +74,29 @@ static inline bool is_nonce_valid(const uint8_t nonce[NONCEBYTES], const uint8_t return true; } -static size_t method_max_packet_size(fastd_context *ctx) { +static size_t method_max_packet_size(fastd_context_t *ctx) { return (fastd_max_packet_size(ctx) + NONCEBYTES + crypto_secretbox_xsalsa20poly1305_ZEROBYTES - crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES); } -static size_t method_min_encrypt_head_space(fastd_context *ctx) { +static size_t method_min_encrypt_head_space(fastd_context_t *ctx) { return crypto_secretbox_xsalsa20poly1305_ZEROBYTES; } -static size_t method_min_decrypt_head_space(fastd_context *ctx) { +static size_t method_min_decrypt_head_space(fastd_context_t *ctx) { return (crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES - NONCEBYTES); } -static size_t method_min_tail_space(fastd_context *ctx) { +static size_t method_min_tail_space(fastd_context_t *ctx) { return 0; } -static fastd_method_session_state* method_session_init(fastd_context *ctx, uint8_t *secret, size_t length, bool initiator) { +static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, uint8_t *secret, size_t length, bool initiator) { int i; if (length < crypto_secretbox_xsalsa20poly1305_KEYBYTES) exit_bug(ctx, "xsalsa20-poly1305: tried to init with short secret"); - fastd_method_session_state *session = malloc(sizeof(fastd_method_session_state)); + fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t)); session->valid_till = ctx->now; session->valid_till.tv_sec += ctx->conf->key_valid; @@ -117,26 +117,26 @@ static fastd_method_session_state* method_session_init(fastd_context *ctx, uint8 return session; } -static bool method_session_is_valid(fastd_context *ctx, fastd_method_session_state *session) { +static bool method_session_is_valid(fastd_context_t *ctx, fastd_method_session_state_t *session) { return (session && timespec_after(&session->valid_till, &ctx->now)); } -static bool method_session_is_initiator(fastd_context *ctx, fastd_method_session_state *session) { +static bool method_session_is_initiator(fastd_context_t *ctx, fastd_method_session_state_t *session) { return (session->send_nonce[0] & 1); } -static bool method_session_want_refresh(fastd_context *ctx, fastd_method_session_state *session) { +static bool method_session_want_refresh(fastd_context_t *ctx, fastd_method_session_state_t *session) { return timespec_after(&ctx->now, &session->refresh_after); } -static void method_session_free(fastd_context *ctx, fastd_method_session_state *session) { +static void method_session_free(fastd_context_t *ctx, fastd_method_session_state_t *session) { if(session) { - memset(session, 0, sizeof(fastd_method_session_state)); + memset(session, 0, sizeof(fastd_method_session_state_t)); free(session); } } -static bool method_encrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_session_state *session, fastd_buffer *out, fastd_buffer in) { +static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { fastd_buffer_pull_head(&in, crypto_secretbox_xsalsa20poly1305_ZEROBYTES); memset(in.data, 0, crypto_secretbox_xsalsa20poly1305_ZEROBYTES); @@ -158,7 +158,7 @@ static bool method_encrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_se return true; } -static bool method_decrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_session_state *session, fastd_buffer *out, fastd_buffer in) { +static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { if (in.len < NONCEBYTES) return false; @@ -218,7 +218,7 @@ static bool method_decrypt(fastd_context *ctx, fastd_peer *peer, fastd_method_se return true; } -const fastd_method fastd_method_xsalsa20_poly1305 = { +const fastd_method_t fastd_method_xsalsa20_poly1305 = { .name = "xsalsa20-poly1305", .max_packet_size = method_max_packet_size, diff --git a/src/packet.h b/src/packet.h index 16c37ce..099f855 100644 --- a/src/packet.h +++ b/src/packet.h @@ -33,16 +33,16 @@ #define PACKET_TYPE_LEN 1 -typedef enum _fastd_packet_type { +typedef enum fastd_packet_type { PACKET_UNKNOWN = 0, PACKET_HANDSHAKE, PACKET_DATA, -} fastd_packet_type; +} fastd_packet_type_t; -typedef struct __attribute__ ((__packed__)) _fastd_packet { +typedef struct __attribute__ ((__packed__)) fastd_packet { uint8_t rsv1; uint16_t rsv2; uint8_t tlv_data[]; -} fastd_packet; +} fastd_packet_t; #endif /* _FASTD_PACKET_H_ */ @@ -32,7 +32,7 @@ #include <arpa/inet.h> -static void on_establish(fastd_context *ctx, fastd_peer *peer) { +static void on_establish(fastd_context_t *ctx, fastd_peer_t *peer) { if (!ctx->conf->on_establish) return; @@ -89,7 +89,7 @@ static void on_establish(fastd_context *ctx, fastd_peer *peer) { free(cwd); } -static void on_disestablish(fastd_context *ctx, fastd_peer *peer) { +static void on_disestablish(fastd_context_t *ctx, fastd_peer_t *peer) { if (!ctx->conf->on_disestablish) return; @@ -146,7 +146,7 @@ static void on_disestablish(fastd_context *ctx, fastd_peer *peer) { free(cwd); } -static inline void free_socket(fastd_context *ctx, fastd_peer *peer) { +static inline void free_socket(fastd_context_t *ctx, fastd_peer_t *peer) { if (peer->sock) { if (fastd_peer_is_socket_dynamic(peer)) { if (peer->sock->peer != peer) @@ -159,7 +159,7 @@ static inline void free_socket(fastd_context *ctx, fastd_peer *peer) { } } -static bool has_group_config_constraints(const fastd_peer_group_config *group) { +static bool has_group_config_constraints(const fastd_peer_group_config_t *group) { for (; group; group = group->parent) { if (group->max_connections) return true; @@ -168,7 +168,7 @@ static bool has_group_config_constraints(const fastd_peer_group_config *group) { return false; } -void fastd_peer_reset_socket(fastd_context *ctx, fastd_peer *peer) { +void fastd_peer_reset_socket(fastd_context_t *ctx, fastd_peer_t *peer) { if (peer->address.sa.sa_family == AF_UNSPEC) { free_socket(ctx, peer); return; @@ -197,13 +197,13 @@ void fastd_peer_reset_socket(fastd_context *ctx, fastd_peer *peer) { } } -static inline fastd_peer_group* find_peer_group(fastd_peer_group *group, const fastd_peer_group_config *config) { +static inline fastd_peer_group_t* find_peer_group(fastd_peer_group_t *group, const fastd_peer_group_config_t *config) { if (group->conf == config) return group; - fastd_peer_group *child; + fastd_peer_group_t *child; for (child = group->children; child; child = child->next) { - fastd_peer_group *ret = find_peer_group(child, config); + fastd_peer_group_t *ret = find_peer_group(child, config); if (ret) return ret; @@ -212,7 +212,7 @@ static inline fastd_peer_group* find_peer_group(fastd_peer_group *group, const f return NULL; } -static inline bool is_group_in(fastd_peer_group *group1, fastd_peer_group *group2) { +static inline bool is_group_in(fastd_peer_group_t *group1, fastd_peer_group_t *group2) { while (group1) { if (group1 == group2) return true; @@ -223,11 +223,11 @@ static inline bool is_group_in(fastd_peer_group *group1, fastd_peer_group *group return false; } -static bool is_peer_in_group(fastd_peer *peer, fastd_peer_group *group) { +static bool is_peer_in_group(fastd_peer_t *peer, fastd_peer_group_t *group) { return is_group_in(peer->group, group); } -static void reset_peer(fastd_context *ctx, fastd_peer *peer) { +static void reset_peer(fastd_context_t *ctx, fastd_peer_t *peer) { if (peer->established) on_disestablish(ctx, peer); @@ -250,7 +250,7 @@ static void reset_peer(fastd_context *ctx, fastd_peer *peer) { fastd_task_delete_peer(ctx, peer); } -static void setup_peer(fastd_context *ctx, fastd_peer *peer) { +static void setup_peer(fastd_context_t *ctx, fastd_peer_t *peer) { if (peer->config->hostname) peer->address.sa.sa_family = AF_UNSPEC; else @@ -280,10 +280,10 @@ static void setup_peer(fastd_context *ctx, fastd_peer *peer) { } } -static void delete_peer(fastd_context *ctx, fastd_peer *peer) { +static void delete_peer(fastd_context_t *ctx, fastd_peer_t *peer) { pr_debug(ctx, "deleting peer %P", peer); - fastd_peer **cur_peer; + fastd_peer_t **cur_peer; for (cur_peer = &ctx->peers; *cur_peer; cur_peer = &(*cur_peer)->next) { if ((*cur_peer) == peer) { *cur_peer = peer->next; @@ -297,12 +297,12 @@ static void delete_peer(fastd_context *ctx, fastd_peer *peer) { } -fastd_peer_config* fastd_peer_config_new(fastd_context *ctx, fastd_config *conf) { - fastd_peer_config *peer = malloc(sizeof(fastd_peer_config)); +fastd_peer_config_t* fastd_peer_config_new(fastd_context_t *ctx, fastd_config_t *conf) { + fastd_peer_config_t *peer = malloc(sizeof(fastd_peer_config_t)); peer->enabled = true; peer->hostname = NULL; - memset(&peer->address, 0, sizeof(fastd_peer_address)); + memset(&peer->address, 0, sizeof(fastd_peer_address_t)); peer->dynamic_float = false; peer->config_source_dir = NULL; @@ -318,7 +318,7 @@ fastd_peer_config* fastd_peer_config_new(fastd_context *ctx, fastd_config *conf) return peer; } -void fastd_peer_config_free(fastd_peer_config *peer) { +void fastd_peer_config_free(fastd_peer_config_t *peer) { free(peer->name); free(peer->hostname); free(peer->key); @@ -326,14 +326,14 @@ void fastd_peer_config_free(fastd_peer_config *peer) { free(peer); } -void fastd_peer_config_delete(fastd_context *ctx, fastd_config *conf) { - fastd_peer_config *peer = conf->peers, *next = peer->next; +void fastd_peer_config_delete(fastd_context_t *ctx, fastd_config_t *conf) { + fastd_peer_config_t *peer = conf->peers, *next = peer->next; fastd_peer_config_free(peer); conf->peers = next; } -void fastd_peer_config_purge(fastd_context *ctx, fastd_peer_config *conf) { - fastd_peer *peer, *next; +void fastd_peer_config_purge(fastd_context_t *ctx, fastd_peer_config_t *conf) { + fastd_peer_t *peer, *next; for (peer = ctx->peers; peer; peer = next) { next = peer->next; @@ -344,7 +344,7 @@ void fastd_peer_config_purge(fastd_context *ctx, fastd_peer_config *conf) { fastd_peer_config_free(conf); } -bool fastd_peer_address_equal(const fastd_peer_address *addr1, const fastd_peer_address *addr2) { +bool fastd_peer_address_equal(const fastd_peer_address_t *addr1, const fastd_peer_address_t *addr2) { if (addr1->sa.sa_family != addr2->sa.sa_family) return false; @@ -369,11 +369,11 @@ bool fastd_peer_address_equal(const fastd_peer_address *addr1, const fastd_peer_ return true; } -void fastd_peer_address_simplify(fastd_peer_address *addr) { +void fastd_peer_address_simplify(fastd_peer_address_t *addr) { if (addr->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&addr->in6.sin6_addr)) { struct sockaddr_in6 mapped = addr->in6; - memset(addr, 0, sizeof(fastd_peer_address)); + memset(addr, 0, sizeof(fastd_peer_address_t)); addr->in.sin_family = AF_INET; addr->in.sin_port = mapped.sin6_port; memcpy(&addr->in.sin_addr.s_addr, &mapped.sin6_addr.s6_addr[12], 4); @@ -381,13 +381,13 @@ void fastd_peer_address_simplify(fastd_peer_address *addr) { } -bool fastd_peer_claim_address(fastd_context *ctx, fastd_peer *new_peer, fastd_socket *sock, const fastd_peer_address *addr) { +bool fastd_peer_claim_address(fastd_context_t *ctx, fastd_peer_t *new_peer, fastd_socket_t *sock, const fastd_peer_address_t *addr) { if (addr->sa.sa_family == AF_UNSPEC) { if (fastd_peer_is_established(new_peer)) fastd_peer_reset(ctx, new_peer); } else { - fastd_peer *peer; + fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { if (!fastd_peer_address_equal(&peer->address, addr)) continue; @@ -399,14 +399,14 @@ bool fastd_peer_claim_address(fastd_context *ctx, fastd_peer *new_peer, fastd_so if (fastd_peer_is_established(new_peer)) fastd_peer_reset(ctx, new_peer); - memset(&new_peer->address, 0, sizeof(fastd_peer_address)); + memset(&new_peer->address, 0, sizeof(fastd_peer_address_t)); return false; } if (fastd_peer_is_established(peer)) fastd_peer_reset(ctx, peer); - memset(&peer->address, 0, sizeof(fastd_peer_address)); + memset(&peer->address, 0, sizeof(fastd_peer_address_t)); break; } } @@ -420,7 +420,7 @@ bool fastd_peer_claim_address(fastd_context *ctx, fastd_peer *new_peer, fastd_so return true; } -bool fastd_peer_config_equal(const fastd_peer_config *peer1, const fastd_peer_config *peer2) { +bool fastd_peer_config_equal(const fastd_peer_config_t *peer1, const fastd_peer_config_t *peer2) { if (peer1->group != peer2->group) return false; @@ -439,21 +439,21 @@ bool fastd_peer_config_equal(const fastd_peer_config *peer1, const fastd_peer_co return true; } -void fastd_peer_reset(fastd_context *ctx, fastd_peer *peer) { +void fastd_peer_reset(fastd_context_t *ctx, fastd_peer_t *peer) { pr_debug(ctx, "resetting peer %P", peer); reset_peer(ctx, peer); setup_peer(ctx, peer); } -void fastd_peer_delete(fastd_context *ctx, fastd_peer *peer) { +void fastd_peer_delete(fastd_context_t *ctx, fastd_peer_t *peer) { reset_peer(ctx, peer); delete_peer(ctx, peer); } -static inline unsigned count_established_group_peers(fastd_context *ctx, fastd_peer_group *group) { +static inline unsigned count_established_group_peers(fastd_context_t *ctx, fastd_peer_group_t *group) { unsigned ret = 0; - fastd_peer *peer; + fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { if (fastd_peer_is_established(peer) && is_peer_in_group(peer, group)) ret++; @@ -462,11 +462,11 @@ static inline unsigned count_established_group_peers(fastd_context *ctx, fastd_p return ret; } -bool fastd_peer_may_connect(fastd_context *ctx, fastd_peer *peer) { +bool fastd_peer_may_connect(fastd_context_t *ctx, fastd_peer_t *peer) { if (fastd_peer_is_established(peer)) return true; - fastd_peer_group *group; + fastd_peer_group_t *group; for (group = peer->group; group; group = group->parent) { if (!group->conf->max_connections) @@ -479,8 +479,8 @@ bool fastd_peer_may_connect(fastd_context *ctx, fastd_peer *peer) { return true; } -fastd_peer* fastd_peer_add(fastd_context *ctx, fastd_peer_config *peer_conf) { - fastd_peer *peer = malloc(sizeof(fastd_peer)); +fastd_peer_t* fastd_peer_add(fastd_context_t *ctx, fastd_peer_config_t *peer_conf) { + fastd_peer_t *peer = malloc(sizeof(fastd_peer_t)); peer->next = ctx->peers; ctx->peers = peer; @@ -497,7 +497,7 @@ fastd_peer* fastd_peer_add(fastd_context *ctx, fastd_peer_config *peer_conf) { return peer; } -void fastd_peer_set_established(fastd_context *ctx, fastd_peer *peer) { +void fastd_peer_set_established(fastd_context_t *ctx, fastd_peer_t *peer) { if (!peer->established) { peer->established = true; on_establish(ctx, peer); @@ -507,25 +507,25 @@ void fastd_peer_set_established(fastd_context *ctx, fastd_peer *peer) { return; } -const fastd_eth_addr* fastd_get_source_address(const fastd_context *ctx, fastd_buffer buffer) { +const fastd_eth_addr_t* fastd_get_source_address(const fastd_context_t *ctx, fastd_buffer_t buffer) { switch (ctx->conf->mode) { case MODE_TAP: - return (fastd_eth_addr*)&((struct ethhdr*)buffer.data)->h_source; + return (fastd_eth_addr_t*)&((struct ethhdr*)buffer.data)->h_source; default: exit_bug(ctx, "invalid mode"); } } -const fastd_eth_addr* fastd_get_dest_address(const fastd_context *ctx, fastd_buffer buffer) { +const fastd_eth_addr_t* fastd_get_dest_address(const fastd_context_t *ctx, fastd_buffer_t buffer) { switch (ctx->conf->mode) { case MODE_TAP: - return (fastd_eth_addr*)&((struct ethhdr*)buffer.data)->h_dest; + return (fastd_eth_addr_t*)&((struct ethhdr*)buffer.data)->h_dest; default: exit_bug(ctx, "invalid mode"); } } -bool fastd_peer_config_matches_dynamic(const fastd_peer_config *config, const fastd_peer_address *addr) { +bool fastd_peer_config_matches_dynamic(const fastd_peer_config_t *config, const fastd_peer_address_t *addr) { if (!config->hostname) return false; @@ -545,20 +545,20 @@ bool fastd_peer_config_matches_dynamic(const fastd_peer_config *config, const fa return true; } -static inline int fastd_eth_addr_cmp(const fastd_eth_addr *addr1, const fastd_eth_addr *addr2) { +static inline int fastd_eth_addr_cmp(const fastd_eth_addr_t *addr1, const fastd_eth_addr_t *addr2) { return memcmp(addr1->data, addr2->data, ETH_ALEN); } -static inline int fastd_peer_eth_addr_cmp(const fastd_peer_eth_addr *addr1, const fastd_peer_eth_addr *addr2) { +static inline int fastd_peer_eth_addr_cmp(const fastd_peer_eth_addr_t *addr1, const fastd_peer_eth_addr_t *addr2) { return fastd_eth_addr_cmp(&addr1->addr, &addr2->addr); } -static inline fastd_peer_eth_addr* peer_get_by_addr(fastd_context *ctx, const fastd_eth_addr *addr) { - return bsearch(container_of(addr, fastd_peer_eth_addr, addr), ctx->eth_addr, ctx->n_eth_addr, sizeof(fastd_peer_eth_addr), +static inline fastd_peer_eth_addr_t* peer_get_by_addr(fastd_context_t *ctx, const fastd_eth_addr_t *addr) { + return bsearch(container_of(addr, fastd_peer_eth_addr_t, addr), ctx->eth_addr, ctx->n_eth_addr, sizeof(fastd_peer_eth_addr_t), (int (*)(const void *, const void *))fastd_peer_eth_addr_cmp); } -void fastd_peer_eth_addr_add(fastd_context *ctx, fastd_peer *peer, const fastd_eth_addr *addr) { +void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_eth_addr_t *addr) { int min = 0, max = ctx->n_eth_addr; while (max > min) { @@ -585,19 +585,19 @@ void fastd_peer_eth_addr_add(fastd_context *ctx, fastd_peer *peer, const fastd_e else ctx->eth_addr_size *= 2; - ctx->eth_addr = realloc(ctx->eth_addr, ctx->eth_addr_size*sizeof(fastd_peer_eth_addr)); + ctx->eth_addr = realloc(ctx->eth_addr, ctx->eth_addr_size*sizeof(fastd_peer_eth_addr_t)); } int i; for (i = ctx->n_eth_addr-1; i > min; i--) ctx->eth_addr[i] = ctx->eth_addr[i-1]; - ctx->eth_addr[min] = (fastd_peer_eth_addr){ *addr, peer, ctx->now }; + ctx->eth_addr[min] = (fastd_peer_eth_addr_t){ *addr, peer, ctx->now }; pr_debug(ctx, "learned new MAC address %E on peer %P", addr, peer); } -void fastd_peer_eth_addr_cleanup(fastd_context *ctx) { +void fastd_peer_eth_addr_cleanup(fastd_context_t *ctx) { int i, deleted = 0; for (i = 0; i < ctx->n_eth_addr; i++) { @@ -614,8 +614,8 @@ void fastd_peer_eth_addr_cleanup(fastd_context *ctx) { ctx->n_eth_addr -= deleted; } -fastd_peer *fastd_peer_find_by_eth_addr(fastd_context *ctx, const fastd_eth_addr *addr) { - fastd_peer_eth_addr *peer_eth_addr = peer_get_by_addr(ctx, addr); +fastd_peer_t* fastd_peer_find_by_eth_addr(fastd_context_t *ctx, const fastd_eth_addr_t *addr) { + fastd_peer_eth_addr_t *peer_eth_addr = peer_get_by_addr(ctx, addr); if (peer_eth_addr) return peer_eth_addr->peer; @@ -30,14 +30,14 @@ #include "fastd.h" -struct _fastd_peer { - fastd_peer *next; +struct fastd_peer { + fastd_peer_t *next; - const fastd_peer_config *config; - fastd_peer_group *group; + const fastd_peer_config_t *config; + fastd_peer_group_t *group; - fastd_socket *sock; - fastd_peer_address address; + fastd_socket_t *sock; + fastd_peer_address_t address; bool established; @@ -46,16 +46,16 @@ struct _fastd_peer { struct timespec seen; struct timespec last_handshake; - fastd_peer_address last_handshake_address; + fastd_peer_address_t last_handshake_address; struct timespec last_handshake_response; - fastd_peer_address last_handshake_response_address; + fastd_peer_address_t last_handshake_response_address; - fastd_protocol_peer_state *protocol_state; + fastd_protocol_peer_state_t *protocol_state; }; -struct _fastd_peer_config { - fastd_peer_config *next; +struct fastd_peer_config { + fastd_peer_config_t *next; const char *config_source_dir; @@ -63,77 +63,77 @@ struct _fastd_peer_config { char *name; char *hostname; - fastd_peer_address address; + fastd_peer_address_t address; bool dynamic_float; char *key; - const fastd_peer_group_config *group; + const fastd_peer_group_config_t *group; - fastd_protocol_peer_config *protocol_config; + fastd_protocol_peer_config_t *protocol_config; }; -struct _fastd_peer_eth_addr { - fastd_eth_addr addr; - fastd_peer *peer; +struct fastd_peer_eth_addr { + fastd_eth_addr_t addr; + fastd_peer_t *peer; struct timespec seen; }; -bool fastd_peer_address_equal(const fastd_peer_address *addr1, const fastd_peer_address *addr2); -void fastd_peer_address_simplify(fastd_peer_address *addr); +bool fastd_peer_address_equal(const fastd_peer_address_t *addr1, const fastd_peer_address_t *addr2); +void fastd_peer_address_simplify(fastd_peer_address_t *addr); -fastd_peer_config* fastd_peer_config_new(fastd_context *ctx, fastd_config *conf); -void fastd_peer_config_free(fastd_peer_config *peer); -void fastd_peer_config_delete(fastd_context *ctx, fastd_config *conf); -void fastd_peer_config_purge(fastd_context *ctx, fastd_peer_config *conf); -bool fastd_peer_config_equal(const fastd_peer_config *peer1, const fastd_peer_config *peer2); +fastd_peer_config_t* fastd_peer_config_new(fastd_context_t *ctx, fastd_config_t *conf); +void fastd_peer_config_free(fastd_peer_config_t *peer); +void fastd_peer_config_delete(fastd_context_t *ctx, fastd_config_t *conf); +void fastd_peer_config_purge(fastd_context_t *ctx, fastd_peer_config_t *conf); +bool fastd_peer_config_equal(const fastd_peer_config_t *peer1, const fastd_peer_config_t *peer2); -void fastd_peer_reset(fastd_context *ctx, fastd_peer *peer); -void fastd_peer_delete(fastd_context *ctx, fastd_peer *peer); -fastd_peer* fastd_peer_add(fastd_context *ctx, fastd_peer_config *conf); -void fastd_peer_set_established(fastd_context *ctx, fastd_peer *peer); -bool fastd_peer_may_connect(fastd_context *ctx, fastd_peer *peer); -bool fastd_peer_claim_address(fastd_context *ctx, fastd_peer *peer, fastd_socket *sock, const fastd_peer_address *addr); -void fastd_peer_reset_socket(fastd_context *ctx, fastd_peer *peer); +void fastd_peer_reset(fastd_context_t *ctx, fastd_peer_t *peer); +void fastd_peer_delete(fastd_context_t *ctx, fastd_peer_t *peer); +fastd_peer_t* fastd_peer_add(fastd_context_t *ctx, fastd_peer_config_t *conf); +void fastd_peer_set_established(fastd_context_t *ctx, fastd_peer_t *peer); +bool fastd_peer_may_connect(fastd_context_t *ctx, fastd_peer_t *peer); +bool fastd_peer_claim_address(fastd_context_t *ctx, fastd_peer_t *peer, fastd_socket_t *sock, const fastd_peer_address_t *addr); +void fastd_peer_reset_socket(fastd_context_t *ctx, fastd_peer_t *peer); -const fastd_eth_addr* fastd_get_source_address(const fastd_context *ctx, fastd_buffer buffer); -const fastd_eth_addr* fastd_get_dest_address(const fastd_context *ctx, fastd_buffer buffer); +const fastd_eth_addr_t* fastd_get_source_address(const fastd_context_t *ctx, fastd_buffer_t buffer); +const fastd_eth_addr_t* fastd_get_dest_address(const fastd_context_t *ctx, fastd_buffer_t buffer); -static inline bool fastd_peer_config_is_floating(const fastd_peer_config *config) { +static inline bool fastd_peer_config_is_floating(const fastd_peer_config_t *config) { return ((config->hostname == NULL && config->address.sa.sa_family == AF_UNSPEC) || config->dynamic_float); } -static inline bool fastd_peer_config_is_dynamic(const fastd_peer_config *config) { +static inline bool fastd_peer_config_is_dynamic(const fastd_peer_config_t *config) { return (config->hostname != NULL); } -bool fastd_peer_config_matches_dynamic(const fastd_peer_config *config, const fastd_peer_address *addr); +bool fastd_peer_config_matches_dynamic(const fastd_peer_config_t *config, const fastd_peer_address_t *addr); -static inline bool fastd_peer_is_floating(const fastd_peer *peer) { +static inline bool fastd_peer_is_floating(const fastd_peer_t *peer) { return fastd_peer_config_is_floating(peer->config); } -static inline bool fastd_peer_is_dynamic(const fastd_peer *peer) { +static inline bool fastd_peer_is_dynamic(const fastd_peer_t *peer) { return fastd_peer_config_is_dynamic(peer->config); } -static inline bool fastd_peer_is_established(const fastd_peer *peer) { +static inline bool fastd_peer_is_established(const fastd_peer_t *peer) { return peer->established; } -static inline void fastd_peer_seen(fastd_context *ctx, fastd_peer *peer) { +static inline void fastd_peer_seen(fastd_context_t *ctx, fastd_peer_t *peer) { peer->seen = ctx->now; } -static inline bool fastd_peer_is_socket_dynamic(const fastd_peer *peer) { +static inline bool fastd_peer_is_socket_dynamic(const fastd_peer_t *peer) { return (!peer->sock || !peer->sock->addr); } -static inline bool fastd_eth_addr_is_unicast(const fastd_eth_addr *addr) { +static inline bool fastd_eth_addr_is_unicast(const fastd_eth_addr_t *addr) { return ((addr->data[0] & 1) == 0); } -void fastd_peer_eth_addr_add(fastd_context *ctx, fastd_peer *peer, const fastd_eth_addr *addr); -void fastd_peer_eth_addr_cleanup(fastd_context *ctx); -fastd_peer* fastd_peer_find_by_eth_addr(fastd_context *ctx, const fastd_eth_addr *addr); +void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_eth_addr_t *addr); +void fastd_peer_eth_addr_cleanup(fastd_context_t *ctx); +fastd_peer_t* fastd_peer_find_by_eth_addr(fastd_context_t *ctx, const fastd_eth_addr_t *addr); #endif /* _FASTD_PEER_H_ */ diff --git a/src/printf.c b/src/printf.c index d5f2ff6..7f5b6e6 100644 --- a/src/printf.c +++ b/src/printf.c @@ -39,7 +39,7 @@ static inline int snprintf_safe(char *buffer, size_t size, const char *format, . return ret < 0 ? 0 : ret > size ? size : ret; } -static int snprint_peer_address(const fastd_context *ctx, char *buffer, size_t size, const fastd_peer_address *address) { +static int snprint_peer_address(const fastd_context_t *ctx, char *buffer, size_t size, const fastd_peer_address_t *address) { char addr_buf[INET6_ADDRSTRLEN] = ""; switch (address->sa.sa_family) { @@ -63,14 +63,14 @@ static int snprint_peer_address(const fastd_context *ctx, char *buffer, size_t s } } -static int snprint_peer_str(const fastd_context *ctx, char *buffer, size_t size, const fastd_peer *peer) { +static int snprint_peer_str(const fastd_context_t *ctx, char *buffer, size_t size, const fastd_peer_t *peer) { if (peer->config && peer->config->name) return snprintf_safe(buffer, size, "<%s>", peer->config->name); else return snprintf_safe(buffer, size, "<(null)>"); } -int fastd_vsnprintf(const fastd_context *ctx, char *buffer, size_t size, const char *format, va_list ap) { +int fastd_vsnprintf(const fastd_context_t *ctx, char *buffer, size_t size, const char *format, va_list ap) { char *buffer_start = buffer; char *buffer_end = buffer+size; @@ -78,7 +78,7 @@ int fastd_vsnprintf(const fastd_context *ctx, char *buffer, size_t size, const c for (; *format; format++) { const void *p; - const fastd_eth_addr *eth_addr; + const fastd_eth_addr_t *eth_addr; if (buffer >= buffer_end) break; @@ -109,7 +109,7 @@ int fastd_vsnprintf(const fastd_context *ctx, char *buffer, size_t size, const c break; case 'E': - eth_addr = va_arg(ap, const fastd_eth_addr*); + eth_addr = va_arg(ap, const fastd_eth_addr_t*); if (eth_addr) { buffer += snprintf_safe(buffer, buffer_end-buffer, "%02x:%02x:%02x:%02x:%02x:%02x", @@ -122,19 +122,19 @@ int fastd_vsnprintf(const fastd_context *ctx, char *buffer, size_t size, const c break; case 'P': - p = va_arg(ap, const fastd_peer*); + p = va_arg(ap, const fastd_peer_t*); if (p) - buffer += snprint_peer_str(ctx, buffer, buffer_end-buffer, (const fastd_peer*)p); + buffer += snprint_peer_str(ctx, buffer, buffer_end-buffer, (const fastd_peer_t*)p); else buffer += snprintf_safe(buffer, buffer_end-buffer, "(null)"); break; case 'I': - p = va_arg(ap, const fastd_peer_address*); + p = va_arg(ap, const fastd_peer_address_t*); if (p) - buffer += snprint_peer_address(ctx, buffer, buffer_end-buffer, (const fastd_peer_address*)p); + buffer += snprint_peer_address(ctx, buffer, buffer_end-buffer, (const fastd_peer_address_t*)p); else buffer += snprintf_safe(buffer, buffer_end-buffer, "(null)"); break; @@ -171,7 +171,7 @@ static inline const char* get_log_prefix(int log_level) { } } -void fastd_logf(const fastd_context *ctx, int level, const char *format, ...) { +void fastd_logf(const fastd_context_t *ctx, int level, const char *format, ...) { char buffer[1024]; char timestr[100] = ""; va_list ap; @@ -199,7 +199,7 @@ void fastd_logf(const fastd_context *ctx, int level, const char *format, ...) { if (ctx->conf != NULL && level <= ctx->conf->log_syslog_level) syslog(level, "%s", buffer); - fastd_log_fd *file; + fastd_log_fd_t *file; for (file = ctx->log_files; file; file = file->next) { if (level <= file->config->level) dprintf(file->fd, "%s%s%s\n", timestr, get_log_prefix(level), buffer); diff --git a/src/protocol_ec25519_fhmqvc.c b/src/protocol_ec25519_fhmqvc.c index 6ebb771..eaaf94a 100644 --- a/src/protocol_ec25519_fhmqvc.c +++ b/src/protocol_ec25519_fhmqvc.c @@ -53,42 +53,42 @@ #endif -struct _fastd_protocol_config { +struct fastd_protocol_config { ecc_secret_key_256 secret_key; ecc_public_key_256 public_key; }; -typedef struct _handshake_key { +typedef struct handshake_key { uint64_t serial; struct timespec preferred_till; struct timespec valid_till; ecc_secret_key_256 secret_key; ecc_public_key_256 public_key; -} handshake_key; +} handshake_key_t; -struct _fastd_protocol_state { - handshake_key prev_handshake_key; - handshake_key handshake_key; +struct fastd_protocol_state { + handshake_key_t prev_handshake_key; + handshake_key_t handshake_key; }; -struct _fastd_protocol_peer_config { +struct fastd_protocol_peer_config { ecc_public_key_256 public_key; }; -typedef struct _protocol_session { +typedef struct protocol_session { struct timespec established; bool handshakes_cleaned; bool refreshing; - const fastd_method *method; - fastd_method_session_state *method_state; -} protocol_session; + const fastd_method_t *method; + fastd_method_session_state_t *method_state; +} protocol_session_t; -struct _fastd_protocol_peer_state { - protocol_session old_session; - protocol_session session; +struct fastd_protocol_peer_state { + protocol_session_t old_session; + protocol_session_t session; uint64_t last_serial; }; @@ -101,7 +101,7 @@ struct _fastd_protocol_peer_state { #define RECORD_T RECORD_PROTOCOL5 -static void send_empty(fastd_context *ctx, fastd_peer *peer, protocol_session *session); +static void send_empty(fastd_context_t *ctx, fastd_peer_t *peer, protocol_session_t *session); static inline bool read_key(uint8_t key[32], const char *hexkey) { @@ -115,20 +115,20 @@ static inline bool read_key(uint8_t key[32], const char *hexkey) { return true; } -static inline bool is_handshake_key_valid(fastd_context *ctx, const handshake_key *handshake_key) { +static inline bool is_handshake_key_valid(fastd_context_t *ctx, const handshake_key_t *handshake_key) { return timespec_after(&handshake_key->valid_till, &ctx->now); } -static inline bool is_handshake_key_preferred(fastd_context *ctx, const handshake_key *handshake_key) { +static inline bool is_handshake_key_preferred(fastd_context_t *ctx, const handshake_key_t *handshake_key) { return timespec_after(&handshake_key->preferred_till, &ctx->now); } -static inline bool is_session_valid(fastd_context *ctx, const protocol_session *session) { +static inline bool is_session_valid(fastd_context_t *ctx, const protocol_session_t *session) { return (session->method && session->method->session_is_valid(ctx, session->method_state)); } -static fastd_peer* get_peer(fastd_context *ctx, const fastd_peer_config *peer_conf) { - fastd_peer *peer; +static fastd_peer_t* get_peer(fastd_context_t *ctx, const fastd_peer_config_t *peer_conf) { + fastd_peer_t *peer; for (peer = ctx->peers; peer; peer = peer->next) { if (peer->config == peer_conf) break; @@ -139,13 +139,13 @@ static fastd_peer* get_peer(fastd_context *ctx, const fastd_peer_config *peer_co return peer; } -static bool backoff(fastd_context *ctx, const fastd_peer *peer) { +static bool backoff(fastd_context_t *ctx, const fastd_peer_t *peer) { return (peer->protocol_state && is_session_valid(ctx, &peer->protocol_state->session) && timespec_diff(&ctx->now, &peer->protocol_state->session.established) < 15000); } -static inline void check_session_refresh(fastd_context *ctx, fastd_peer *peer) { - protocol_session *session = &peer->protocol_state->session; +static inline void check_session_refresh(fastd_context_t *ctx, fastd_peer_t *peer) { + protocol_session_t *session = &peer->protocol_state->session; if (!session->refreshing && session->method->session_is_initiator(ctx, session->method_state) && session->method->session_want_refresh(ctx, session->method_state)) { pr_verbose(ctx, "refreshing session with %P", peer); @@ -155,8 +155,8 @@ static inline void check_session_refresh(fastd_context *ctx, fastd_peer *peer) { } } -static fastd_protocol_config* protocol_init(fastd_context *ctx) { - fastd_protocol_config *protocol_config = malloc(sizeof(fastd_protocol_config)); +static fastd_protocol_config_t* protocol_init(fastd_context_t *ctx) { + fastd_protocol_config_t *protocol_config = malloc(sizeof(fastd_protocol_config_t)); if (!ctx->conf->secret) exit_error(ctx, "no secret key configured"); @@ -171,7 +171,7 @@ static fastd_protocol_config* protocol_init(fastd_context *ctx) { return protocol_config; } -static void protocol_peer_configure(fastd_context *ctx, fastd_peer_config *peer_conf) { +static void protocol_peer_configure(fastd_context_t *ctx, fastd_peer_config_t *peer_conf) { ecc_public_key_256 key; if (!peer_conf->key) { @@ -192,18 +192,16 @@ static void protocol_peer_configure(fastd_context *ctx, fastd_peer_config *peer_ return; } - peer_conf->protocol_config = malloc(sizeof(fastd_protocol_peer_config)); + peer_conf->protocol_config = malloc(sizeof(fastd_protocol_peer_config_t)); peer_conf->protocol_config->public_key = key; } -static void init_protocol_state(fastd_context *ctx) { - if (!ctx->protocol_state) { - ctx->protocol_state = malloc(sizeof(fastd_protocol_state)); - memset(ctx->protocol_state, 0, sizeof(fastd_protocol_state)); - } +static void init_protocol_state(fastd_context_t *ctx) { + if (!ctx->protocol_state) + ctx->protocol_state = calloc(1, sizeof(fastd_protocol_state_t)); } -static void maintenance(fastd_context *ctx) { +static void maintenance(fastd_context_t *ctx) { init_protocol_state(ctx); if (!is_handshake_key_preferred(ctx, &ctx->protocol_state->handshake_key)) { @@ -228,10 +226,10 @@ static void maintenance(fastd_context *ctx) { } } -static void protocol_handshake_init(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, const fastd_peer_config *peer_conf) { +static void protocol_handshake_init(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf) { maintenance(ctx); - fastd_buffer buffer = fastd_handshake_new_init(ctx, 3*(4+PUBLICKEYBYTES) /* sender key, receipient key, handshake key */); + fastd_buffer_t buffer = fastd_handshake_new_init(ctx, 3*(4+PUBLICKEYBYTES) /* sender key, receipient key, handshake key */); fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, ctx->conf->protocol_config->public_key.p); @@ -245,8 +243,8 @@ static void protocol_handshake_init(fastd_context *ctx, const fastd_socket *sock fastd_send_handshake(ctx, sock, address, buffer); } -static void respond_handshake(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, const fastd_peer *peer, const handshake_key *handshake_key, const ecc_public_key_256 *peer_handshake_key, - const fastd_handshake *handshake, const fastd_method *method) { +static void respond_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_t *peer, const handshake_key_t *handshake_key, const ecc_public_key_256 *peer_handshake_key, + const fastd_handshake_t *handshake, const fastd_method_t *method) { pr_debug(ctx, "responding handshake with %P[%I]...", peer, address); uint8_t hashinput[5*PUBLICKEYBYTES]; @@ -294,7 +292,7 @@ static void respond_handshake(fastd_context *ctx, const fastd_socket *sock, cons crypto_auth_hmacsha256(hmacbuf, hashinput, 2*PUBLICKEYBYTES, shared_handshake_key); - fastd_buffer buffer = fastd_handshake_new_reply(ctx, handshake, method, 4*(4+PUBLICKEYBYTES) + 4+HMACBYTES); + fastd_buffer_t buffer = fastd_handshake_new_reply(ctx, handshake, method, 4*(4+PUBLICKEYBYTES) + 4+HMACBYTES); fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, ctx->conf->protocol_config->public_key.p); fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, peer->config->protocol_config->public_key.p); @@ -305,7 +303,7 @@ static void respond_handshake(fastd_context *ctx, const fastd_socket *sock, cons fastd_send_handshake(ctx, sock, address, buffer); } -static bool establish(fastd_context *ctx, fastd_peer *peer, const fastd_method *method, fastd_socket *sock, const fastd_peer_address *address, bool initiator, +static bool establish(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_method_t *method, fastd_socket_t *sock, const fastd_peer_address_t *address, bool initiator, const ecc_public_key_256 *A, const ecc_public_key_256 *B, const ecc_public_key_256 *X, const ecc_public_key_256 *Y, const ecc_public_key_256 *sigma, uint64_t serial) { uint8_t hashinput[5*PUBLICKEYBYTES]; @@ -362,8 +360,8 @@ static bool establish(fastd_context *ctx, fastd_peer *peer, const fastd_method * return true; } -static void finish_handshake(fastd_context *ctx, fastd_socket *sock, const fastd_peer_address *address, fastd_peer *peer, const handshake_key *handshake_key, const ecc_public_key_256 *peer_handshake_key, - const fastd_handshake *handshake, const fastd_method *method) { +static void finish_handshake(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, const handshake_key_t *handshake_key, const ecc_public_key_256 *peer_handshake_key, + const fastd_handshake_t *handshake, const fastd_method_t *method) { pr_debug(ctx, "finishing handshake with %P[%I]...", peer, address); uint8_t hashinput[5*PUBLICKEYBYTES]; @@ -422,7 +420,7 @@ static void finish_handshake(fastd_context *ctx, fastd_socket *sock, const fastd &peer->config->protocol_config->public_key, &sigma, handshake_key->serial)) return; - fastd_buffer buffer = fastd_handshake_new_reply(ctx, handshake, method, 4*(4+PUBLICKEYBYTES) + 4+HMACBYTES); + fastd_buffer_t buffer = fastd_handshake_new_reply(ctx, handshake, method, 4*(4+PUBLICKEYBYTES) + 4+HMACBYTES); fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, ctx->conf->protocol_config->public_key.p); fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, peer->config->protocol_config->public_key.p); @@ -433,8 +431,8 @@ static void finish_handshake(fastd_context *ctx, fastd_socket *sock, const fastd fastd_send_handshake(ctx, sock, address, buffer); } -static void handle_finish_handshake(fastd_context *ctx, fastd_socket *sock, const fastd_peer_address *address, fastd_peer *peer, const handshake_key *handshake_key, const ecc_public_key_256 *peer_handshake_key, - const fastd_handshake *handshake, const fastd_method *method) { +static void handle_finish_handshake(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, const handshake_key_t *handshake_key, const ecc_public_key_256 *peer_handshake_key, + const fastd_handshake_t *handshake, const fastd_method_t *method) { pr_debug(ctx, "handling handshake finish with %P[%I]...", peer, address); uint8_t hashinput[5*PUBLICKEYBYTES]; @@ -488,7 +486,7 @@ static void handle_finish_handshake(fastd_context *ctx, fastd_socket *sock, cons &ctx->conf->protocol_config->public_key, &sigma, handshake_key->serial); } -static bool check_peer_config_match(const fastd_peer_config *config, const fastd_peer_address *address, const unsigned char key[32]) { +static bool check_peer_config_match(const fastd_peer_config_t *config, const fastd_peer_address_t *address, const unsigned char key[32]) { if (!config->enabled || !config->protocol_config) return false; @@ -498,7 +496,7 @@ static bool check_peer_config_match(const fastd_peer_config *config, const fastd return (memcmp(config->protocol_config->public_key.p, key, PUBLICKEYBYTES) == 0); } -static const fastd_peer_config* match_sender_key(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_address *address, const fastd_peer_config *peer_conf, const unsigned char key[32]) { +static const fastd_peer_config_t* match_sender_key(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, const unsigned char key[32]) { if (sock->peer) { if (peer_conf != sock->peer->config) { if (peer_conf && !fastd_peer_config_is_floating(peer_conf) && !fastd_peer_config_is_dynamic(peer_conf)) @@ -520,7 +518,7 @@ static const fastd_peer_config* match_sender_key(fastd_context *ctx, const fastd if (peer_conf && !fastd_peer_config_is_floating(peer_conf) && !fastd_peer_config_is_dynamic(peer_conf)) return NULL; - const fastd_peer_config *config; + const fastd_peer_config_t *config; for (config = ctx->conf->peers; config; config = config->next) { if (!check_peer_config_match(config, address, key)) continue; @@ -536,12 +534,12 @@ static const fastd_peer_config* match_sender_key(fastd_context *ctx, const fastd return NULL; } -static inline bool has_field(const fastd_handshake *handshake, uint8_t type, size_t length) { +static inline bool has_field(const fastd_handshake_t *handshake, uint8_t type, size_t length) { return (handshake->records[type].length == length); } -static void protocol_handshake_handle(fastd_context *ctx, fastd_socket *sock, const fastd_peer_address *address, const fastd_peer_config *peer_conf, const fastd_handshake *handshake, const fastd_method *method) { - handshake_key *handshake_key; +static void protocol_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *address, const fastd_peer_config_t *peer_conf, const fastd_handshake_t *handshake, const fastd_method_t *method) { + handshake_key_t *handshake_key; char *peer_version_name = NULL; maintenance(ctx); @@ -557,7 +555,7 @@ static void protocol_handshake_handle(fastd_context *ctx, fastd_socket *sock, co return; } - fastd_peer *peer = get_peer(ctx, peer_conf); + fastd_peer_t *peer = get_peer(ctx, peer_conf); if (!fastd_peer_may_connect(ctx, peer)) { pr_debug(ctx, "ignoring handshake from %P[%I] because of local constraints", peer, address); @@ -657,7 +655,7 @@ static void protocol_handshake_handle(fastd_context *ctx, fastd_socket *sock, co } } -static void protocol_handle_recv(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer) { +static void protocol_handle_recv(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer) { if (!fastd_peer_is_established(peer)) { pr_debug(ctx, "received unexpected packet from %P, scheduling handshake", peer); fastd_task_schedule_handshake(ctx, peer, 0); @@ -668,7 +666,7 @@ static void protocol_handle_recv(fastd_context *ctx, fastd_peer *peer, fastd_buf if (!peer->protocol_state || !is_session_valid(ctx, &peer->protocol_state->session)) goto fail; - fastd_buffer recv_buffer; + fastd_buffer_t recv_buffer; bool ok = false; if (is_session_valid(ctx, &peer->protocol_state->old_session)) { @@ -717,8 +715,8 @@ static void protocol_handle_recv(fastd_context *ctx, fastd_peer *peer, fastd_buf fastd_buffer_free(buffer); } -static void session_send(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer, protocol_session *session) { - fastd_buffer send_buffer; +static void session_send(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer, protocol_session_t *session) { + fastd_buffer_t send_buffer; if (!session->method->encrypt(ctx, peer, session->method_state, &send_buffer, buffer)) { fastd_buffer_free(buffer); return; @@ -730,7 +728,7 @@ static void session_send(fastd_context *ctx, fastd_peer *peer, fastd_buffer buff fastd_task_schedule_keepalive(ctx, peer, ctx->conf->keepalive_interval*1000); } -static void protocol_send(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer) { +static void protocol_send(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer) { if (!peer->protocol_state || !fastd_peer_is_established(peer) || !is_session_valid(ctx, &peer->protocol_state->session)) { fastd_buffer_free(buffer); return; @@ -747,28 +745,27 @@ static void protocol_send(fastd_context *ctx, fastd_peer *peer, fastd_buffer buf } } -static void send_empty(fastd_context *ctx, fastd_peer *peer, protocol_session *session) { +static void send_empty(fastd_context_t *ctx, fastd_peer_t *peer, protocol_session_t *session) { session_send(ctx, peer, fastd_buffer_alloc(0, alignto(session->method->min_encrypt_head_space(ctx), 8), session->method->min_encrypt_tail_space(ctx)), session); } -static void protocol_init_peer_state(fastd_context *ctx, fastd_peer *peer) { +static void protocol_init_peer_state(fastd_context_t *ctx, fastd_peer_t *peer) { init_protocol_state(ctx); if (peer->protocol_state) exit_bug(ctx, "tried to reinit peer state"); - peer->protocol_state = malloc(sizeof(fastd_protocol_peer_state)); - memset(peer->protocol_state, 0, sizeof(fastd_protocol_peer_state)); + peer->protocol_state = calloc(1, sizeof(fastd_protocol_peer_state_t)); peer->protocol_state->last_serial = ctx->protocol_state->handshake_key.serial; } -static void reset_session(fastd_context *ctx, protocol_session *session) { +static void reset_session(fastd_context_t *ctx, protocol_session_t *session) { if (session->method) session->method->session_free(ctx, session->method_state); - memset(session, 0, sizeof(protocol_session)); + memset(session, 0, sizeof(protocol_session_t)); } -static void protocol_reset_peer_state(fastd_context *ctx, fastd_peer *peer) { +static void protocol_reset_peer_state(fastd_context_t *ctx, fastd_peer_t *peer) { if (!peer->protocol_state) return; @@ -776,7 +773,7 @@ static void protocol_reset_peer_state(fastd_context *ctx, fastd_peer *peer) { reset_session(ctx, &peer->protocol_state->session); } -static void protocol_free_peer_state(fastd_context *ctx, fastd_peer *peer) { +static void protocol_free_peer_state(fastd_context_t *ctx, fastd_peer_t *peer) { if (peer->protocol_state) { reset_session(ctx, &peer->protocol_state->old_session); reset_session(ctx, &peer->protocol_state->session); @@ -795,7 +792,7 @@ static void hexdump(const char *desc, unsigned char d[32]) { printf("\n"); } -static void protocol_generate_key(fastd_context *ctx) { +static void protocol_generate_key(fastd_context_t *ctx) { ecc_secret_key_256 secret_key; ecc_public_key_256 public_key; @@ -818,7 +815,7 @@ static void protocol_generate_key(fastd_context *ctx) { } } -static void protocol_show_key(fastd_context *ctx) { +static void protocol_show_key(fastd_context_t *ctx) { if (ctx->conf->machine_readable) hexdump("", ctx->conf->protocol_config->public_key.p); else @@ -826,7 +823,7 @@ static void protocol_show_key(fastd_context *ctx) { } -const fastd_protocol fastd_protocol_ec25519_fhmqvc = { +const fastd_protocol_t fastd_protocol_ec25519_fhmqvc = { .name = "ec25519-fhmqvc", .init = protocol_init, diff --git a/src/queue.c b/src/queue.c index 70ee54b..fa864d5 100644 --- a/src/queue.c +++ b/src/queue.c @@ -30,7 +30,7 @@ #include <stdint.h> -void fastd_queue_put(fastd_context *ctx, fastd_queue *queue, fastd_queue_entry *entry, int timeout) { +void fastd_queue_put(fastd_context_t *ctx, fastd_queue_t *queue, fastd_queue_entry_t *entry, int timeout) { entry->timeout = ctx->now; if (timeout) { @@ -43,7 +43,7 @@ void fastd_queue_put(fastd_context *ctx, fastd_queue *queue, fastd_queue_entry * } } - fastd_queue_entry **current; + fastd_queue_entry_t **current; for (current = &queue->head;; current = &(*current)->next) { if (!(*current) || timespec_after(&(*current)->timeout, &entry->timeout)) { entry->next = *current; @@ -53,17 +53,17 @@ void fastd_queue_put(fastd_context *ctx, fastd_queue *queue, fastd_queue_entry * } } -fastd_queue_entry* fastd_queue_get(fastd_context *ctx, fastd_queue *queue) { +fastd_queue_entry_t* fastd_queue_get(fastd_context_t *ctx, fastd_queue_t *queue) { if (!queue->head || fastd_queue_timeout(ctx, queue) > 0) return NULL; - fastd_queue_entry *entry = queue->head; + fastd_queue_entry_t *entry = queue->head; queue->head = entry->next; return entry; } -int fastd_queue_timeout(fastd_context *ctx, fastd_queue *queue) { +int fastd_queue_timeout(fastd_context_t *ctx, fastd_queue_t *queue) { if (!queue->head) return -1; @@ -74,8 +74,8 @@ int fastd_queue_timeout(fastd_context *ctx, fastd_queue *queue) { return diff_msec; } -void fastd_queue_filter(fastd_context *ctx, fastd_queue *queue, bool (*pred)(fastd_queue_entry*, void*), void *extra) { - fastd_queue_entry **entry, *next; +void fastd_queue_filter(fastd_context_t *ctx, fastd_queue_t *queue, bool (*pred)(fastd_queue_entry_t*, void*), void *extra) { + fastd_queue_entry_t **entry, *next; for (entry = &queue->head; *entry;) { next = (*entry)->next; @@ -86,8 +86,8 @@ void fastd_queue_filter(fastd_context *ctx, fastd_queue *queue, bool (*pred)(fas } } -bool fastd_queue_has_entry(fastd_context *ctx, fastd_queue *queue, bool (*pred)(fastd_queue_entry*, void*), void *extra) { - fastd_queue_entry *entry; +bool fastd_queue_has_entry(fastd_context_t *ctx, fastd_queue_t *queue, bool (*pred)(fastd_queue_entry_t*, void*), void *extra) { + fastd_queue_entry_t *entry; for (entry = queue->head; entry; entry = entry->next) { if (pred(entry, extra)) return true; diff --git a/src/queue.h b/src/queue.h index 8e93e0e..6e1a727 100644 --- a/src/queue.h +++ b/src/queue.h @@ -34,22 +34,22 @@ #include <time.h> -typedef struct _fastd_queue_entry fastd_queue_entry; +typedef struct fastd_queue_entry fastd_queue_entry_t; -struct _fastd_queue_entry { - fastd_queue_entry *next; +struct fastd_queue_entry { + fastd_queue_entry_t *next; struct timespec timeout; }; -typedef struct _fastd_queue { - fastd_queue_entry *head; -} fastd_queue; +typedef struct fastd_queue { + fastd_queue_entry_t *head; +} fastd_queue_t; -void fastd_queue_put(fastd_context *ctx, fastd_queue *queue, fastd_queue_entry *entry, int timeout); -fastd_queue_entry* fastd_queue_get(fastd_context *ctx, fastd_queue *queue); -int fastd_queue_timeout(fastd_context *ctx, fastd_queue *queue); -void fastd_queue_filter(fastd_context *ctx, fastd_queue *queue, bool (*pred)(fastd_queue_entry*, void*), void *extra); -bool fastd_queue_has_entry(fastd_context *ctx, fastd_queue *queue, bool (*pred)(fastd_queue_entry*, void*), void *extra); +void fastd_queue_put(fastd_context_t *ctx, fastd_queue_t *queue, fastd_queue_entry_t *entry, int timeout); +fastd_queue_entry_t* fastd_queue_get(fastd_context_t *ctx, fastd_queue_t *queue); +int fastd_queue_timeout(fastd_context_t *ctx, fastd_queue_t *queue); +void fastd_queue_filter(fastd_context_t *ctx, fastd_queue_t *queue, bool (*pred)(fastd_queue_entry_t*, void*), void *extra); +bool fastd_queue_has_entry(fastd_context_t *ctx, fastd_queue_t *queue, bool (*pred)(fastd_queue_entry_t*, void*), void *extra); -#endif /* _FASTD_QUEUE_H_ */ +#endif /* _FASTD_QUEUE_T_H_ */ diff --git a/src/random.c b/src/random.c index f859d07..54dd8e0 100644 --- a/src/random.c +++ b/src/random.c @@ -30,7 +30,7 @@ #include <sys/stat.h> -void fastd_random_bytes(fastd_context *ctx, void *buffer, size_t len, bool secure) { +void fastd_random_bytes(fastd_context_t *ctx, void *buffer, size_t len, bool secure) { int fd; size_t read_bytes = 0; diff --git a/src/resolve.c b/src/resolve.c index 642fd67..66da479 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -33,16 +33,16 @@ #include <pthread.h> -typedef struct _resolv_arg { - fastd_context *ctx; +typedef struct resolv_arg { + fastd_context_t *ctx; pthread_t master_thread; char *hostname; - fastd_peer_address constraints; -} resolv_arg; + fastd_peer_address_t constraints; +} resolv_arg_t; static void* resolve_peer(void *varg) { - resolv_arg *arg = varg; + resolv_arg_t *arg = varg; struct addrinfo hints; struct addrinfo *res = NULL; @@ -64,12 +64,12 @@ static void* resolve_peer(void *varg) { pr_verbose(arg->ctx, "resolving host `%s' failed: %s", arg->hostname, gai_strerror(gai_ret)); error = true; } - else if (res->ai_addrlen > sizeof(fastd_peer_address) || (res->ai_addr->sa_family != AF_INET && res->ai_addr->sa_family != AF_INET6)) { + else if (res->ai_addrlen > sizeof(fastd_peer_address_t) || (res->ai_addr->sa_family != AF_INET && res->ai_addr->sa_family != AF_INET6)) { pr_warn(arg->ctx, "resolving host `%s': unsupported address returned", arg->hostname); error = true; } - fastd_resolve_return ret; + fastd_resolve_return_t ret; memset(&ret, 0, sizeof(ret)); ret.hostname = arg->hostname; @@ -93,7 +93,7 @@ static void* resolve_peer(void *varg) { return NULL; } -void fastd_resolve_peer(fastd_context *ctx, fastd_peer *peer) { +void fastd_resolve_peer(fastd_context_t *ctx, fastd_peer_t *peer) { if (timespec_after(&peer->last_resolve, &peer->last_resolve_return)) { pr_debug(ctx, "not resolving %P as there is already a resolve running", peer); return; @@ -102,7 +102,7 @@ void fastd_resolve_peer(fastd_context *ctx, fastd_peer *peer) { if (timespec_diff(&ctx->now, &peer->last_resolve) < ctx->conf->min_resolve_interval*1000) { pr_debug(ctx, "not resolving %P as it has been resolved a short time ago", peer); - fastd_resolve_return ret; + fastd_resolve_return_t ret; memset(&ret, 0, sizeof(ret)); ret.hostname = strdup(peer->config->hostname); @@ -118,7 +118,7 @@ void fastd_resolve_peer(fastd_context *ctx, fastd_peer *peer) { pr_verbose(ctx, "resolving host `%s' for peer %P...", peer->config->hostname, peer); peer->last_resolve = ctx->now; - resolv_arg *arg = malloc(sizeof(resolv_arg)); + resolv_arg_t *arg = malloc(sizeof(resolv_arg_t)); arg->ctx = ctx; arg->master_thread = pthread_self(); @@ -27,13 +27,13 @@ #include "task.h" -fastd_task* fastd_task_get(fastd_context *ctx) { - return container_of(fastd_queue_get(ctx, &ctx->task_queue), fastd_task, entry); +fastd_task_t* fastd_task_get(fastd_context_t *ctx) { + return container_of(fastd_queue_get(ctx, &ctx->task_queue), fastd_task_t, entry); } -static bool is_handshake(fastd_queue_entry *data, void *extra) { - fastd_task *task = container_of(data, fastd_task, entry); - fastd_peer *peer = extra; +static bool is_handshake(fastd_queue_entry_t *data, void *extra) { + fastd_task_t *task = container_of(data, fastd_task_t, entry); + fastd_peer_t *peer = extra; if (task->peer != peer) return false; @@ -41,13 +41,13 @@ static bool is_handshake(fastd_queue_entry *data, void *extra) { return (task->type == TASK_HANDSHAKE); } -void fastd_task_schedule_handshake(fastd_context *ctx, fastd_peer *peer, int timeout) { +void fastd_task_schedule_handshake(fastd_context_t *ctx, fastd_peer_t *peer, int timeout) { if (fastd_queue_has_entry(ctx, &ctx->task_queue, is_handshake, peer)) { pr_debug(ctx, "not sending a handshake to %P, there still is one queued", peer); return; } - fastd_task *task = malloc(sizeof(fastd_task)); + fastd_task_t *task = malloc(sizeof(fastd_task_t)); task->type = TASK_HANDSHAKE; task->peer = peer; @@ -55,9 +55,9 @@ void fastd_task_schedule_handshake(fastd_context *ctx, fastd_peer *peer, int tim fastd_queue_put(ctx, &ctx->task_queue, &task->entry, timeout); } -static bool is_keepalive(fastd_queue_entry *data, void *extra) { - fastd_task *task = container_of(data, fastd_task, entry); - fastd_peer *peer = extra; +static bool is_keepalive(fastd_queue_entry_t *data, void *extra) { + fastd_task_t *task = container_of(data, fastd_task_t, entry); + fastd_peer_t *peer = extra; if (task->peer != peer) return false; @@ -65,13 +65,13 @@ static bool is_keepalive(fastd_queue_entry *data, void *extra) { return (task->type == TASK_KEEPALIVE); } -void fastd_task_schedule_keepalive(fastd_context *ctx, fastd_peer *peer, int timeout) { +void fastd_task_schedule_keepalive(fastd_context_t *ctx, fastd_peer_t *peer, int timeout) { if (fastd_queue_has_entry(ctx, &ctx->task_queue, is_keepalive, peer)) { pr_debug(ctx, "not sending a keepalive to %P, there still is one queued", peer); return; } - fastd_task *task = malloc(sizeof(fastd_task)); + fastd_task_t *task = malloc(sizeof(fastd_task_t)); task->type = TASK_KEEPALIVE; task->peer = peer; @@ -79,17 +79,17 @@ void fastd_task_schedule_keepalive(fastd_context *ctx, fastd_peer *peer, int tim fastd_queue_put(ctx, &ctx->task_queue, &task->entry, timeout); } -typedef struct _replace_peer_extra { - fastd_peer *old_peer; - fastd_peer *new_peer; -} replace_peer_extra; +typedef struct replace_peer_extra { + fastd_peer_t *old_peer; + fastd_peer_t *new_peer; +} replace_peer_extra_t; -static bool replace_peer(fastd_queue_entry *data, void *extra) { - replace_peer_extra *e = extra; - fastd_task *task = container_of(data, fastd_task, entry); - fastd_peer *old_peer = e->old_peer; - fastd_peer *new_peer = e->new_peer; +static bool replace_peer(fastd_queue_entry_t *data, void *extra) { + replace_peer_extra_t *e = extra; + fastd_task_t *task = container_of(data, fastd_task_t, entry); + fastd_peer_t *old_peer = e->old_peer; + fastd_peer_t *new_peer = e->new_peer; if (task->peer == old_peer) task->peer = new_peer; @@ -97,21 +97,21 @@ static bool replace_peer(fastd_queue_entry *data, void *extra) { return true; } -void fastd_task_replace_peer(fastd_context *ctx, fastd_peer *old_peer, fastd_peer *new_peer) { - replace_peer_extra extra = {old_peer, new_peer}; +void fastd_task_replace_peer(fastd_context_t *ctx, fastd_peer_t *old_peer, fastd_peer_t *new_peer) { + replace_peer_extra_t extra = {old_peer, new_peer}; fastd_queue_filter(ctx, &ctx->task_queue, replace_peer, &extra); } -typedef struct _delete_task_extra { - fastd_peer *peer; +typedef struct delete_task_extra { + fastd_peer_t *peer; bool handshake_only; bool keepalive_only; -} delete_task_extra; +} delete_task_extra_t; -static bool delete_task(fastd_queue_entry *data, void *extra) { - delete_task_extra *e = extra; - fastd_task *task = container_of(data, fastd_task, entry); - fastd_peer *peer = e->peer; +static bool delete_task(fastd_queue_entry_t *data, void *extra) { + delete_task_extra_t *e = extra; + fastd_task_t *task = container_of(data, fastd_task_t, entry); + fastd_peer_t *peer = e->peer; if (task->peer != peer) return true; @@ -127,17 +127,17 @@ static bool delete_task(fastd_queue_entry *data, void *extra) { return false; } -void fastd_task_delete_peer(fastd_context *ctx, fastd_peer *peer) { - delete_task_extra extra = {peer, false, false}; +void fastd_task_delete_peer(fastd_context_t *ctx, fastd_peer_t *peer) { + delete_task_extra_t extra = {peer, false, false}; fastd_queue_filter(ctx, &ctx->task_queue, delete_task, &extra); } -void fastd_task_delete_peer_handshakes(fastd_context *ctx, fastd_peer *peer) { - delete_task_extra extra = {peer, true, false}; +void fastd_task_delete_peer_handshakes(fastd_context_t *ctx, fastd_peer_t *peer) { + delete_task_extra_t extra = {peer, true, false}; fastd_queue_filter(ctx, &ctx->task_queue, delete_task, &extra); } -void fastd_task_delete_peer_keepalives(fastd_context *ctx, fastd_peer *peer) { - delete_task_extra extra = {peer, false, true}; +void fastd_task_delete_peer_keepalives(fastd_context_t *ctx, fastd_peer_t *peer) { + delete_task_extra_t extra = {peer, false, true}; fastd_queue_filter(ctx, &ctx->task_queue, delete_task, &extra); } @@ -33,35 +33,32 @@ #include <sys/uio.h> -typedef enum _fastd_task_type { +typedef enum fastd_task_type { TASK_HANDSHAKE, TASK_KEEPALIVE, -} fastd_task_type; +} fastd_task_type_t; -typedef struct _fastd_task_any { -} fastd_task_any; +typedef struct fastd_task { + fastd_queue_entry_t entry; -typedef struct _fastd_task { - fastd_queue_entry entry; + fastd_task_type_t type; + fastd_peer_t *peer; +} fastd_task_t; - fastd_task_type type; - fastd_peer *peer; -} fastd_task; - -static inline int fastd_task_timeout(fastd_context *ctx) { +static inline int fastd_task_timeout(fastd_context_t *ctx) { return fastd_queue_timeout(ctx, &ctx->task_queue); } -fastd_task* fastd_task_get(fastd_context *ctx); +fastd_task_t* fastd_task_get(fastd_context_t *ctx); -void fastd_task_schedule_handshake(fastd_context *ctx, fastd_peer *peer, int timeout); -void fastd_task_schedule_keepalive(fastd_context *ctx, fastd_peer *peer, int timeout); +void fastd_task_schedule_handshake(fastd_context_t *ctx, fastd_peer_t *peer, int timeout); +void fastd_task_schedule_keepalive(fastd_context_t *ctx, fastd_peer_t *peer, int timeout); -void fastd_task_replace_peer(fastd_context *ctx, fastd_peer *old_peer, fastd_peer *new_peer); -void fastd_task_delete_peer(fastd_context *ctx, fastd_peer *peer); -void fastd_task_delete_peer_handshakes(fastd_context *ctx, fastd_peer *peer); -void fastd_task_delete_peer_keepalives(fastd_context *ctx, fastd_peer *peer); +void fastd_task_replace_peer(fastd_context_t *ctx, fastd_peer_t *old_peer, fastd_peer_t *new_peer); +void fastd_task_delete_peer(fastd_context_t *ctx, fastd_peer_t *peer); +void fastd_task_delete_peer_handshakes(fastd_context_t *ctx, fastd_peer_t *peer); +void fastd_task_delete_peer_keepalives(fastd_context_t *ctx, fastd_peer_t *peer); #endif /* _FASTD_TASK_H_ */ diff --git a/src/types.h b/src/types.h index 830c5dc..cda16ec 100644 --- a/src/types.h +++ b/src/types.h @@ -36,62 +36,62 @@ #include <config.h> -typedef enum _fastd_mode { +typedef enum fastd_mode { MODE_TAP, MODE_TUN, -} fastd_mode; +} fastd_mode_t; -typedef struct _fastd_buffer fastd_buffer; +typedef struct fastd_buffer fastd_buffer_t; -typedef union _fastd_peer_address fastd_peer_address; -typedef struct _fastd_bind_address fastd_bind_address; -typedef struct _fastd_socket fastd_socket; -typedef struct _fastd_peer_group_config fastd_peer_group_config; -typedef struct _fastd_peer_group fastd_peer_group; -typedef struct _fastd_peer_config fastd_peer_config; -typedef struct _fastd_eth_addr fastd_eth_addr; -typedef struct _fastd_peer fastd_peer; -typedef struct _fastd_peer_eth_addr fastd_peer_eth_addr; +typedef union fastd_peer_address fastd_peer_address_t; +typedef struct fastd_bind_address fastd_bind_address_t; +typedef struct fastd_socket fastd_socket_t; +typedef struct fastd_peer_group_config fastd_peer_group_config_t; +typedef struct fastd_peer_group fastd_peer_group_t; +typedef struct fastd_peer_config fastd_peer_config_t; +typedef struct fastd_eth_addr fastd_eth_addr_t; +typedef struct fastd_peer fastd_peer_t; +typedef struct fastd_peer_eth_addr fastd_peer_eth_addr_t; -typedef struct _fastd_log_file fastd_log_file; -typedef struct _fastd_log_fd fastd_log_fd; +typedef struct fastd_log_file fastd_log_file_t; +typedef struct fastd_log_fd fastd_log_fd_t; -typedef struct _fastd_config fastd_config; -typedef struct _fastd_context fastd_context; +typedef struct fastd_config fastd_config_t; +typedef struct fastd_context fastd_context_t; -typedef struct _fastd_protocol fastd_protocol; -typedef struct _fastd_method fastd_method; +typedef struct fastd_protocol fastd_protocol_t; +typedef struct fastd_method fastd_method_t; -typedef struct _fastd_handshake fastd_handshake; +typedef struct fastd_handshake fastd_handshake_t; -typedef struct _fastd_string_stack fastd_string_stack; +typedef struct fastd_string_stack fastd_string_stack_t; -typedef struct _fastd_resolve_return fastd_resolve_return; +typedef struct fastd_resolve_return fastd_resolve_return_t; #ifdef USE_CRYPTO_AES128CTR -typedef struct _fastd_crypto_aes128ctr fastd_crypto_aes128ctr; +typedef struct fastd_crypto_aes128ctr fastd_crypto_aes128ctr_t; #endif #ifdef USE_CRYPTO_GHASH -typedef struct _fastd_crypto_ghash fastd_crypto_ghash; +typedef struct fastd_crypto_ghash fastd_crypto_ghash_t; #endif /* May be defined by the protocol/method/crypto implementations however they like */ -typedef struct _fastd_protocol_config fastd_protocol_config; -typedef struct _fastd_protocol_state fastd_protocol_state; -typedef struct _fastd_protocol_peer_config fastd_protocol_peer_config; -typedef struct _fastd_protocol_peer_state fastd_protocol_peer_state; +typedef struct fastd_protocol_config fastd_protocol_config_t; +typedef struct fastd_protocol_state fastd_protocol_state_t; +typedef struct fastd_protocol_peer_config fastd_protocol_peer_config_t; +typedef struct fastd_protocol_peer_state fastd_protocol_peer_state_t; -typedef struct _fastd_method_session_state fastd_method_session_state; +typedef struct fastd_method_session_state fastd_method_session_state_t; #ifdef USE_CRYPTO_AES128CTR -typedef struct _fastd_crypto_aes128ctr_context fastd_crypto_aes128ctr_context; -typedef struct _fastd_crypto_aes128ctr_state fastd_crypto_aes128ctr_state; +typedef struct fastd_crypto_aes128ctr_context fastd_crypto_aes128ctr_context_t; +typedef struct fastd_crypto_aes128ctr_state fastd_crypto_aes128ctr_state_t; #endif #ifdef USE_CRYPTO_GHASH -typedef struct _fastd_crypto_ghash_context fastd_crypto_ghash_context; -typedef struct _fastd_crypto_ghash_state fastd_crypto_ghash_state; +typedef struct fastd_crypto_ghash_context fastd_crypto_ghash_context_t; +typedef struct fastd_crypto_ghash_state fastd_crypto_ghash_state_t; #endif #endif /* _FASTD_TYPES_H_ */ |