summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-05-27 04:53:38 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-05-27 04:53:38 +0200
commite90be1577d672068878118fc61fab75e3028b56f (patch)
treec6b154f25da7ff9e6d81b7a4313599233f678cbd /src
parenta0e0e9898a7c1abeb4dbf6ba188a9bcaf523e003 (diff)
downloadfastd-e90be1577d672068878118fc61fab75e3028b56f.tar
fastd-e90be1577d672068878118fc61fab75e3028b56f.zip
Move tuntap handling out of poll.c
Diffstat (limited to 'src')
-rw-r--r--src/fastd.h3
-rw-r--r--src/poll.c39
-rw-r--r--src/send.c31
-rw-r--r--src/tuntap.c6
4 files changed, 38 insertions, 41 deletions
diff --git a/src/fastd.h b/src/fastd.h
index 6fe0422..8a11a41 100644
--- a/src/fastd.h
+++ b/src/fastd.h
@@ -302,6 +302,7 @@ extern fastd_config_t conf; /**< The global configuration */
void fastd_send(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer, size_t stat_size);
void fastd_send_handshake(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer);
void fastd_send_all(fastd_peer_t *source_peer, fastd_buffer_t buffer);
+void fastd_send_data(fastd_buffer_t buffer);
void fastd_receive(fastd_socket_t *sock);
void fastd_handle_receive(fastd_peer_t *peer, fastd_buffer_t buffer);
@@ -316,7 +317,7 @@ void fastd_socket_error(fastd_socket_t *sock);
void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote);
void fastd_tuntap_open(void);
-fastd_buffer_t fastd_tuntap_read(void);
+void fastd_tuntap_handle(void);
void fastd_tuntap_write(fastd_buffer_t buffer);
void fastd_tuntap_close(void);
diff --git a/src/poll.c b/src/poll.c
index 0a2e11a..e57d349 100644
--- a/src/poll.c
+++ b/src/poll.c
@@ -41,41 +41,6 @@
#endif
-static inline bool handle_tap(fastd_buffer_t buffer) {
- if (conf.mode != MODE_TAP)
- return false;
-
- if (buffer.len < ETH_HLEN) {
- pr_debug("truncated packet on tap interface");
- fastd_buffer_free(buffer);
- return true;
- }
-
- fastd_eth_addr_t dest_addr = fastd_get_dest_address(buffer);
- if (!fastd_eth_addr_is_unicast(dest_addr))
- return false;
-
- fastd_peer_t *peer = fastd_peer_find_by_eth_addr(dest_addr);
-
- if (!peer)
- return false;
-
- conf.protocol->send(peer, buffer);
- return true;
-}
-
-static void handle_tuntap(void) {
- fastd_buffer_t buffer = fastd_tuntap_read();
- if (!buffer.len)
- return;
-
- if (handle_tap(buffer))
- return;
-
- /* TUN mode or multicast packet */
- fastd_send_all(NULL, buffer);
-}
-
static inline int handshake_timeout(void) {
if (!ctx.handshake_queue.next)
return -1;
@@ -181,7 +146,7 @@ void fastd_poll_handle(void) {
for (i = 0; i < (size_t)ret; i++) {
if (events[i].data.ptr == &ctx.tunfd) {
if (events[i].events & EPOLLIN)
- handle_tuntap();
+ fastd_tuntap_handle();
}
else if (events[i].data.ptr == &ctx.async_rfd) {
if (events[i].events & EPOLLIN)
@@ -291,7 +256,7 @@ void fastd_poll_handle(void) {
fastd_update_time();
if (VECTOR_INDEX(ctx.pollfds, 0).revents & POLLIN)
- handle_tuntap();
+ fastd_tuntap_handle();
if (VECTOR_INDEX(ctx.pollfds, 1).revents & POLLIN)
fastd_async_handle();
diff --git a/src/send.c b/src/send.c
index aec58db..9ebccf9 100644
--- a/src/send.c
+++ b/src/send.c
@@ -197,3 +197,34 @@ void fastd_send_all(fastd_peer_t *source_peer, fastd_buffer_t buffer) {
fastd_buffer_free(buffer);
}
+
+static inline bool send_data_tap_single(fastd_buffer_t buffer) {
+ if (conf.mode != MODE_TAP)
+ return false;
+
+ if (buffer.len < ETH_HLEN) {
+ pr_debug("truncated packet on tap interface");
+ fastd_buffer_free(buffer);
+ return true;
+ }
+
+ fastd_eth_addr_t dest_addr = fastd_get_dest_address(buffer);
+ if (!fastd_eth_addr_is_unicast(dest_addr))
+ return false;
+
+ fastd_peer_t *peer = fastd_peer_find_by_eth_addr(dest_addr);
+
+ if (!peer)
+ return false;
+
+ conf.protocol->send(peer, buffer);
+ return true;
+}
+
+void fastd_send_data(fastd_buffer_t buffer) {
+ if (send_data_tap_single(buffer))
+ return;
+
+ /* TUN mode or multicast packet */
+ fastd_send_all(NULL, buffer);
+}
diff --git a/src/tuntap.c b/src/tuntap.c
index ddb93b1..53cbccd 100644
--- a/src/tuntap.c
+++ b/src/tuntap.c
@@ -296,7 +296,7 @@ void fastd_tuntap_open(void) {
/** Reads a packet from the TUN/TAP device */
-fastd_buffer_t fastd_tuntap_read(void) {
+void fastd_tuntap_handle(void) {
size_t max_len = fastd_max_inner_packet();
fastd_buffer_t buffer;
@@ -309,7 +309,7 @@ fastd_buffer_t fastd_tuntap_read(void) {
if (len < 0) {
if (errno == EINTR) {
fastd_buffer_free(buffer);
- return (fastd_buffer_t){};
+ return;
}
exit_errno("read");
@@ -320,7 +320,7 @@ fastd_buffer_t fastd_tuntap_read(void) {
if (multiaf_tun && conf.mode == MODE_TUN)
fastd_buffer_push_head(&buffer, 4);
- return buffer;
+ fastd_send_data(buffer);
}
/** Writes a packet to the TUN/TAP device */