diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-02-26 01:02:35 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-02-26 01:02:35 +0100 |
commit | 4ab4858d5473c816cc526b296ae9da6c6956219d (patch) | |
tree | 67ac9253e6aa74a68699aa2a9e8e43d89e14c088 /src/peer.c | |
parent | 87b9b80a55d3567e76fc86d3abc7f3cfd6d92e6f (diff) | |
download | fastd-4ab4858d5473c816cc526b296ae9da6c6956219d.tar fastd-4ab4858d5473c816cc526b296ae9da6c6956219d.zip |
Experimental support for accepting connections from unknown peers
Diffstat (limited to 'src/peer.c')
-rw-r--r-- | src/peer.c | 68 |
1 files changed, 66 insertions, 2 deletions
@@ -151,7 +151,7 @@ static void reset_peer(fastd_context_t *ctx, fastd_peer_t *peer) { } static void setup_peer(fastd_context_t *ctx, fastd_peer_t *peer) { - if (peer->config->hostname) + if (!peer->config || peer->config->hostname) peer->address.sa.sa_family = AF_UNSPEC; else peer->address = peer->config->address; @@ -187,13 +187,25 @@ static void delete_peer(fastd_context_t *ctx, fastd_peer_t *peer) { for (cur_peer = &ctx->peers; *cur_peer; cur_peer = &(*cur_peer)->next) { if ((*cur_peer) == peer) { *cur_peer = peer->next; + ctx->n_peers--; break; } } + if (!*cur_peer) { + for (cur_peer = &ctx->peers_temp; *cur_peer; cur_peer = &(*cur_peer)->next) { + if ((*cur_peer) == peer) { + *cur_peer = peer->next; + break; + } + } + } ctx->conf->protocol->free_peer_state(ctx, peer); + + if (!peer->config) + free(peer->protocol_config); + free(peer); - ctx->n_peers--; } @@ -387,6 +399,7 @@ fastd_peer_t* fastd_peer_add(fastd_context_t *ctx, fastd_peer_config_t *peer_con peer->config = peer_conf; peer->group = find_peer_group(ctx->peer_group, peer_conf->group); + peer->protocol_config = peer_conf->protocol_config; peer->protocol_state = NULL; peer->sock = NULL; setup_peer(ctx, peer); @@ -397,6 +410,57 @@ fastd_peer_t* fastd_peer_add(fastd_context_t *ctx, fastd_peer_config_t *peer_con return peer; } +fastd_peer_t* fastd_peer_add_temporary(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *addr) { + if (!ctx->conf->on_verify) + exit_bug(ctx, "tried to add temporary peer without on-verify command"); + + fastd_peer_t *peer = malloc(sizeof(fastd_peer_t)); + + peer->next = ctx->peers_temp; + ctx->peers_temp = peer; + + peer->config = NULL; + peer->group = ctx->peer_group; + peer->protocol_state = NULL; + peer->sock = sock; + setup_peer(ctx, peer); + + peer->address = *addr; + + pr_debug(ctx, "adding temporary peer for %I", addr); + + return peer; +} + +bool fastd_peer_verify_temporary(fastd_context_t *ctx, fastd_peer_t *peer) { + if (!ctx->conf->on_verify) + exit_bug(ctx, "tried to verify temporary peer without on-verify command"); + + int ret; + if (!fastd_shell_exec(ctx, peer, ctx->conf->on_verify, ctx->conf->on_verify_dir, &ret)) + return false; + + if (WIFSIGNALED(ret)) { + pr_error(ctx, "verify command exited with signal %i", WTERMSIG(ret)); + return false; + } + else if (WEXITSTATUS(ret)) { + pr_debug(ctx, "verify command exited with status %i", WEXITSTATUS(ret)); + return false; + } + + return true; +} + +void fastd_peer_enable_temporary(fastd_context_t *ctx, fastd_peer_t *peer) { + if (peer->config) + exit_bug(ctx, "trying to re-enable non-temporary peer"); + + peer->next = ctx->peers; + ctx->peers = peer; + ctx->n_peers++; +} + void fastd_peer_set_established(fastd_context_t *ctx, fastd_peer_t *peer) { if (!peer->established) { peer->established = true; |