summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-04-20 02:16:44 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-04-20 02:16:44 +0200
commit8c705b9cd4c7866227f09af2a859744e47602ba4 (patch)
tree9b75234454e0611c484265698011b5df832d893c /src
parent0bf9268453d3af82bbd1257da547b1dd8f225ba2 (diff)
downloadfastd-8c705b9cd4c7866227f09af2a859744e47602ba4.tar
fastd-8c705b9cd4c7866227f09af2a859744e47602ba4.zip
Minimize poll interface to simplify implementation of alternative implementations
Diffstat (limited to 'src')
-rw-r--r--src/peer.c9
-rw-r--r--src/poll.c17
-rw-r--r--src/poll.h6
-rw-r--r--src/socket.c2
-rw-r--r--src/tuntap.c6
5 files changed, 24 insertions, 16 deletions
diff --git a/src/peer.c b/src/peer.c
index e7574ef..47b1370 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -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;
}
}
diff --git a/src/poll.c b/src/poll.c
index 1e87946..31e69bf 100644
--- a/src/poll.c
+++ b/src/poll.c
@@ -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) {
diff --git a/src/poll.h b/src/poll.h
index cc472b2..4049824 100644
--- a/src/poll.h
+++ b/src/poll.h
@@ -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.");
}