diff options
-rw-r--r-- | src/peer.c | 9 | ||||
-rw-r--r-- | src/poll.c | 17 | ||||
-rw-r--r-- | src/poll.h | 6 | ||||
-rw-r--r-- | src/socket.c | 2 | ||||
-rw-r--r-- | src/tuntap.c | 6 |
5 files changed, 24 insertions, 16 deletions
@@ -48,16 +48,19 @@ static inline void free_socket(fastd_context_t *ctx, fastd_peer_t *peer) { fastd_socket_close(ctx, peer->sock); free(peer->sock); + peer->sock = NULL; size_t i; for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { if (VECTOR_INDEX(ctx->peers, i) == peer) { - fastd_poll_set_fd_peer(ctx, -1, i); + fastd_poll_set_fd_peer(ctx, i); break; } } } - peer->sock = NULL; + else { + peer->sock = NULL; + } } } @@ -104,7 +107,7 @@ void fastd_peer_reset_socket(fastd_context_t *ctx, fastd_peer_t *peer) { size_t i; for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { if (VECTOR_INDEX(ctx->peers, i) == peer) { - fastd_poll_set_fd_peer(ctx, peer->sock->fd, i); + fastd_poll_set_fd_peer(ctx, i); break; } } @@ -59,16 +59,21 @@ void fastd_poll_free(fastd_context_t *ctx) { } -void fastd_poll_set_fd_tuntap(fastd_context_t *ctx, int fd) { - VECTOR_INDEX(ctx->pollfds, 0).fd = fd; +void fastd_poll_set_fd_tuntap(fastd_context_t *ctx) { + VECTOR_INDEX(ctx->pollfds, 0).fd = ctx->tunfd; } -void fastd_poll_set_fd_sock(fastd_context_t *ctx, int fd, size_t i) { - VECTOR_INDEX(ctx->pollfds, 2+i).fd = fd; +void fastd_poll_set_fd_sock(fastd_context_t *ctx, size_t i) { + VECTOR_INDEX(ctx->pollfds, 2+i).fd = ctx->socks[i].fd; } -void fastd_poll_set_fd_peer(fastd_context_t *ctx, int fd, size_t i) { - VECTOR_INDEX(ctx->pollfds, 2+ctx->n_socks+i).fd = fd; +void fastd_poll_set_fd_peer(fastd_context_t *ctx, size_t i) { + fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + + if (!peer->sock || !fastd_peer_is_socket_dynamic(peer)) + VECTOR_INDEX(ctx->pollfds, 2+ctx->n_socks+i).fd = -1; + else + VECTOR_INDEX(ctx->pollfds, 2+ctx->n_socks+i).fd = peer->sock->fd; } void fastd_poll_add_peer(fastd_context_t *ctx) { @@ -33,9 +33,9 @@ void fastd_poll_init(fastd_context_t *ctx); void fastd_poll_free(fastd_context_t *ctx); -void fastd_poll_set_fd_tuntap(fastd_context_t *ctx, int fd); -void fastd_poll_set_fd_sock(fastd_context_t *ctx, int fd, size_t i); -void fastd_poll_set_fd_peer(fastd_context_t *ctx, int fd, size_t i); +void fastd_poll_set_fd_tuntap(fastd_context_t *ctx); +void fastd_poll_set_fd_sock(fastd_context_t *ctx, size_t i); +void fastd_poll_set_fd_peer(fastd_context_t *ctx, size_t i); void fastd_poll_add_peer(fastd_context_t *ctx); void fastd_poll_delete_peer(fastd_context_t *ctx, size_t i); diff --git a/src/socket.c b/src/socket.c index 6db4b9f..a1e0d02 100644 --- a/src/socket.c +++ b/src/socket.c @@ -196,7 +196,7 @@ bool fastd_socket_handle_binds(fastd_context_t *ctx) { continue; } - fastd_poll_set_fd_sock(ctx, ctx->socks[i].fd, i); + fastd_poll_set_fd_sock(ctx, i); fastd_peer_address_t bound_addr = *ctx->socks[i].bound_addr; if (!ctx->socks[i].addr->addr.sa.sa_family) diff --git a/src/tuntap.c b/src/tuntap.c index d638611..fd81c2a 100644 --- a/src/tuntap.c +++ b/src/tuntap.c @@ -102,7 +102,7 @@ void fastd_tuntap_open(fastd_context_t *ctx) { if (close(ctl_sock)) pr_error_errno(ctx, "close"); - fastd_poll_set_fd_tuntap(ctx, ctx->tunfd); + fastd_poll_set_fd_tuntap(ctx); pr_debug(ctx, "tun/tap device initialized."); } @@ -204,7 +204,7 @@ void fastd_tuntap_open(fastd_context_t *ctx) { exit_bug(ctx, "invalid mode"); } - fastd_poll_set_fd_tuntap(ctx, ctx->tunfd); + fastd_poll_set_fd_tuntap(ctx); pr_debug(ctx, "tun/tap device initialized."); } @@ -273,7 +273,7 @@ void fastd_tuntap_open(fastd_context_t *ctx) { exit_bug(ctx, "invalid mode"); } - fastd_poll_set_fd_tuntap(ctx, ctx->tunfd); + fastd_poll_set_fd_tuntap(ctx); pr_debug(ctx, "tun device initialized."); } |