From 74b73a980b91799843f8004edd90387ac482af46 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 2 Nov 2012 08:56:54 +0100 Subject: Keep track of corresonding peers in associated sockets --- src/fastd.c | 5 +++-- src/fastd.h | 3 ++- src/peer.c | 7 +++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/fastd.c b/src/fastd.c index ec33356..1d74cb3 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -152,7 +152,7 @@ static void init_sockets(fastd_context *ctx) { unsigned i; fastd_bind_address *addr = ctx->conf->bind_addrs; for (i = 0; i < ctx->conf->n_bind_addrs; i++) { - ctx->socks[i] = (fastd_socket){-2, addr}; + ctx->socks[i] = (fastd_socket){-2, addr, NULL}; if (addr == ctx->conf->bind_addr_default_v4) ctx->sock_default_v4 = &ctx->socks[i]; @@ -256,7 +256,7 @@ static void bind_sockets(fastd_context *ctx) { } } -fastd_socket* fastd_socket_open(fastd_context *ctx, int af) { +fastd_socket* fastd_socket_open(fastd_context *ctx, const fastd_peer *peer, int af) { const fastd_bind_address any_address = { .addr.sa.sa_family = af }; int fd = bind_socket(ctx, &any_address, true); @@ -267,6 +267,7 @@ fastd_socket* fastd_socket_open(fastd_context *ctx, int af) { sock->fd = fd; sock->addr = NULL; + sock->peer = peer; return sock; } diff --git a/src/fastd.h b/src/fastd.h index ecc3eee..1395df8 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -141,6 +141,7 @@ struct _fastd_bind_address { struct _fastd_socket { int fd; const fastd_bind_address *addr; + const fastd_peer *peer; }; struct _fastd_config { @@ -268,7 +269,7 @@ void fastd_send(fastd_context *ctx, const fastd_socket *sock, const fastd_peer_a 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); -fastd_socket* fastd_socket_open(fastd_context *ctx, int af); +fastd_socket* fastd_socket_open(fastd_context *ctx, const fastd_peer *peer, int af); void fastd_resolve_peer(fastd_context *ctx, fastd_peer *peer); diff --git a/src/peer.c b/src/peer.c index 9513703..cb7626f 100644 --- a/src/peer.c +++ b/src/peer.c @@ -149,6 +149,9 @@ static void on_disestablish(fastd_context *ctx, fastd_peer *peer) { static inline void free_socket(fastd_context *ctx, fastd_peer *peer) { if (peer->sock) { if (fastd_peer_is_socket_dynamic(peer)) { + if (peer->sock->peer != peer) + exit_bug(ctx, "dynamic peer socket mismatch"); + fastd_socket_close(ctx, peer->sock); free(peer->sock); } @@ -169,14 +172,14 @@ void fastd_peer_reset_socket(fastd_context *ctx, fastd_peer *peer) { if (ctx->sock_default_v4) peer->sock = ctx->sock_default_v4; else - peer->sock = fastd_socket_open(ctx, AF_INET); + peer->sock = fastd_socket_open(ctx, peer, AF_INET); break; case AF_INET6: if (ctx->sock_default_v6) peer->sock = ctx->sock_default_v6; else - peer->sock = fastd_socket_open(ctx, AF_INET6); + peer->sock = fastd_socket_open(ctx, peer, AF_INET6); } } -- cgit v1.2.3