From 6a249659bf167aa8e7f72917e9c9f23ba9477e1c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 25 May 2014 23:10:26 +0200 Subject: Unify fastd_peer_group_t and fastd_peer_group_config_t into a single structure --- src/config.c | 14 +++++++------- src/fastd.c | 39 --------------------------------------- src/fastd.h | 4 +--- src/peer.c | 40 +++++++++++----------------------------- src/peer.h | 18 ++++++++---------- src/types.h | 1 - 6 files changed, 27 insertions(+), 89 deletions(-) diff --git a/src/config.c b/src/config.c index 265df62..ab655e2 100644 --- a/src/config.c +++ b/src/config.c @@ -62,7 +62,7 @@ static void default_config(void) { conf.protocol = &fastd_protocol_ec25519_fhmqvc; - conf.peer_group = calloc(1, sizeof(fastd_peer_group_config_t)); + conf.peer_group = calloc(1, sizeof(fastd_peer_group_t)); conf.peer_group->name = strdup("default"); conf.peer_group->max_connections = -1; @@ -135,7 +135,7 @@ void fastd_config_bind_address(const fastd_peer_address_t *address, const char * } void fastd_config_peer_group_push(const char *name) { - fastd_peer_group_config_t *group = calloc(1, sizeof(fastd_peer_group_config_t)); + fastd_peer_group_t *group = calloc(1, sizeof(fastd_peer_group_t)); group->name = strdup(name); group->max_connections = -1; @@ -151,9 +151,9 @@ void fastd_config_peer_group_pop(void) { conf.peer_group = conf.peer_group->parent; } -static void free_peer_group(fastd_peer_group_config_t *group) { +static void free_peer_group(fastd_peer_group_t *group) { while (group->children) { - fastd_peer_group_config_t *next = group->children->next; + fastd_peer_group_t *next = group->children->next; free_peer_group(group->children); group->children = next; } @@ -163,11 +163,11 @@ static void free_peer_group(fastd_peer_group_config_t *group) { free(group); } -static bool has_peer_group_peer_dirs(const fastd_peer_group_config_t *group) { +static bool has_peer_group_peer_dirs(const fastd_peer_group_t *group) { if (group->peer_dirs) return true; - const fastd_peer_group_config_t *child; + const fastd_peer_group_t *child; for (child = group->children; child; child = child->next) { if (has_peer_group_peer_dirs(child)) return true; @@ -538,7 +538,7 @@ void fastd_config_verify(void) { static void peer_dirs_read_peer_group(void) { read_peer_dirs(); - fastd_peer_group_config_t *base = conf.peer_group, *group; + fastd_peer_group_t *base = conf.peer_group, *group; for (group = conf.peer_group->children; group; group = group->next) { conf.peer_group = group; peer_dirs_read_peer_group(); diff --git a/src/fastd.c b/src/fastd.c index 5542ea8..7e5b640 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -190,42 +190,6 @@ static inline void on_post_down(void) { fastd_shell_command_exec(&conf.on_post_down, NULL); } -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_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); - children = &(*children)->next; - } - - return ret; -} - -static void init_peer_groups(void) { - ctx.peer_group = init_peer_group(conf.peer_group, NULL); -} - -static void free_peer_group(fastd_peer_group_t *group) { - while (group->children) { - fastd_peer_group_t *child = group->children; - group->children = group->children->next; - - free_peer_group(child); - } - - free(group); -} - -static void delete_peer_groups(void) { - free_peer_group(ctx.peer_group); -} - static void init_peers(void) { fastd_peer_config_t *peer_conf; for (peer_conf = conf.peers; peer_conf; peer_conf = peer_conf->next) @@ -564,8 +528,6 @@ int main(int argc, char *argv[]) { fastd_tuntap_open(); - init_peer_groups(); - write_pid(getpid()); #ifdef ENABLE_SYSTEMD @@ -634,7 +596,6 @@ int main(int argc, char *argv[]) { on_down(); delete_peers(); - delete_peer_groups(); fastd_tuntap_close(); close_sockets(); diff --git a/src/fastd.h b/src/fastd.h index f9e8c5a..6fe0422 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -207,7 +207,7 @@ struct fastd_config { const fastd_cipher_t **ciphers; /**< All supported ciphers */ const fastd_mac_t **macs; /**< All supported message authentication codes */ - fastd_peer_group_config_t *peer_group; /**< The root peer group configuration */ + fastd_peer_group_t *peer_group; /**< The root peer group configuration */ fastd_peer_config_t *peers; /**< The configured peers */ bool has_floating; /**< Specifies if any of the configured peers have floating remotes */ @@ -245,8 +245,6 @@ struct fastd_context { struct timespec now; /**< The current monotonous timestamp */ - fastd_peer_group_t *peer_group; /**< The root peer group */ - uint64_t next_peer_id; /**< An monotonously increasing ID peers are identified with in some components */ VECTOR(fastd_peer_t*) peers; /**< The currectly active peers */ diff --git a/src/peer.c b/src/peer.c index c6eee35..5837ca5 100644 --- a/src/peer.c +++ b/src/peer.c @@ -181,7 +181,7 @@ static inline void free_socket(fastd_peer_t *peer) { free_socket_by_id(peer_index(peer)); } -static inline bool has_group_config_constraints(const fastd_peer_group_config_t *group) { +static inline bool has_group_config_constraints(const fastd_peer_group_t *group) { for (; group; group = group->parent) { if (group->max_connections >= 0) return true; @@ -250,22 +250,7 @@ void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay) { fastd_dlist_insert(list, &peer->handshake_entry); } -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_t *child; - for (child = group->children; child; child = child->next) { - fastd_peer_group_t *ret = find_peer_group(child, config); - - if (ret) - return ret; - } - - return NULL; -} - -static inline bool is_group_in(fastd_peer_group_t *group1, fastd_peer_group_t *group2) { +static inline bool is_group_in(const fastd_peer_group_t *group1, const fastd_peer_group_t *group2) { while (group1) { if (group1 == group2) return true; @@ -276,8 +261,8 @@ static inline bool is_group_in(fastd_peer_group_t *group1, fastd_peer_group_t *g return false; } -static bool is_peer_in_group(fastd_peer_t *peer, fastd_peer_group_t *group) { - return is_group_in(peer->group, group); +static bool is_peer_in_group(const fastd_peer_t *peer, const fastd_peer_group_t *group) { + return is_group_in(fastd_peer_get_group(peer), group); } static void reset_peer(fastd_peer_t *peer) { @@ -307,7 +292,7 @@ static void reset_peer(fastd_peer_t *peer) { static void init_handshake(fastd_peer_t *peer) { unsigned delay = 0; - if (has_group_config_constraints(peer->group->conf)) + if (has_group_config_constraints(fastd_peer_get_group(peer))) delay = fastd_rand(0, 3000); if (!fastd_peer_is_established(peer)) @@ -626,7 +611,7 @@ void fastd_peer_delete(fastd_peer_t *peer) { delete_peer(peer); } -static inline size_t count_established_group_peers(fastd_peer_group_t *group) { +static inline size_t count_established_group_peers(const fastd_peer_group_t *group) { size_t i, ret = 0; for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); @@ -642,13 +627,13 @@ bool fastd_peer_may_connect(fastd_peer_t *peer) { if (fastd_peer_is_established(peer)) return true; - fastd_peer_group_t *group; + const fastd_peer_group_t *group; - for (group = peer->group; group; group = group->parent) { - if (group->conf->max_connections < 0) + for (group = fastd_peer_get_group(peer); group; group = group->parent) { + if (group->max_connections < 0) continue; - if (count_established_group_peers(group) >= (size_t)group->conf->max_connections) + if (count_established_group_peers(group) >= (size_t)group->max_connections) return false; } @@ -662,7 +647,6 @@ fastd_peer_t* fastd_peer_add(fastd_peer_config_t *peer_conf) { if (peer_conf) { peer->config = peer_conf; - peer->group = find_peer_group(ctx.peer_group, peer_conf->group); peer->protocol_config = peer_conf->protocol_config; VECTOR_ALLOC(peer->remotes, 0); @@ -680,15 +664,13 @@ fastd_peer_t* fastd_peer_add(fastd_peer_config_t *peer_conf) { VECTOR_ADD(peer->remotes, remote); } - pr_verbose("adding peer %P (group `%s')", peer, peer->group->conf->name); + pr_verbose("adding peer %P (group `%s')", peer, fastd_peer_get_group(peer)->name); } else { #ifdef WITH_VERIFY if (!fastd_shell_command_isset(&conf.on_verify)) exit_bug("tried to add temporary peer without on-verify command"); - peer->group = ctx.peer_group; - peer->verify_timeout = ctx.now; peer->verify_valid_timeout = ctx.now; diff --git a/src/peer.h b/src/peer.h index c44f599..f29545a 100644 --- a/src/peer.h +++ b/src/peer.h @@ -48,7 +48,6 @@ struct fastd_peer { uint64_t id; /**< A unique ID assigned to each peer */ const fastd_peer_config_t *config; /**< The peer's fastd_peer_config_t */ - fastd_peer_group_t *group; /**< The peer's peer group */ /** The socket used by the peer. This can either be a common bound socket or a dynamic, unbound socket that is used exclusively by this peer */ @@ -99,7 +98,7 @@ struct fastd_peer_config { fastd_remote_config_t *remotes; /**< A linked list of the peer's remote entries */ char *key; /**< The peer's public key */ bool floating; /**< Specifies if the peer has any floating remotes (or no remotes at all) */ - const fastd_peer_group_config_t *group; /**< The peer group the peer belongs to */ + const fastd_peer_group_t *group; /**< The peer group the peer belongs to */ fastd_protocol_peer_config_t *protocol_config; /**< The protocol-specific configuration of the peer */ }; @@ -109,14 +108,6 @@ struct fastd_peer_group { fastd_peer_group_t *parent; fastd_peer_group_t *children; - const fastd_peer_group_config_t *conf; -}; - -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_t *peer_dirs; @@ -243,6 +234,13 @@ static inline bool fastd_peer_is_established(const fastd_peer_t *peer) { } } +static inline const fastd_peer_group_t * fastd_peer_get_group(const fastd_peer_t *peer) { + if (peer->config) + return peer->config->group; + else + return conf.peer_group; +} + static inline bool fastd_remote_is_dynamic(const fastd_remote_t *remote) { return remote->config->hostname; } diff --git a/src/types.h b/src/types.h index 7f9f86e..2dae259 100644 --- a/src/types.h +++ b/src/types.h @@ -81,7 +81,6 @@ typedef struct fastd_buffer fastd_buffer_t; 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; -- cgit v1.2.3