diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-05-20 10:36:39 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-05-20 10:36:39 +0200 |
commit | 3c8abc1921c51b654dace98002bdf7ed49f7489c (patch) | |
tree | ba75acf2005bc7a8039c6e5b3fc56cfd9814366b /src | |
parent | 23510adffedd1b8b8b642238c25801e658423d6e (diff) | |
download | fastd-3c8abc1921c51b654dace98002bdf7ed49f7489c.tar fastd-3c8abc1921c51b654dace98002bdf7ed49f7489c.zip |
Add send_all() function to reduce duplicate code
Diffstat (limited to 'src')
-rw-r--r-- | src/fastd.c | 47 | ||||
-rw-r--r-- | src/fastd.h | 2 |
2 files changed, 24 insertions, 25 deletions
diff --git a/src/fastd.c b/src/fastd.c index 78862d7..bbf1e03 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -605,26 +605,33 @@ void fastd_send_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, cons fastd_send_type(ctx, sock, local_addr, remote_addr, PACKET_HANDSHAKE, buffer); } -static inline void handle_forward(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t *buffer) { - const fastd_eth_addr_t *dest_addr = fastd_get_dest_address(ctx, *buffer); +static inline void send_all(fastd_context_t *ctx, fastd_peer_t *source_peer, fastd_buffer_t buffer) { + fastd_peer_t *dest_peer; + for (dest_peer = ctx->peers; dest_peer; dest_peer = dest_peer->next) { + if (dest_peer == source_peer || !fastd_peer_is_established(dest_peer)) + continue; + + ctx->conf->protocol->send(ctx, dest_peer, fastd_buffer_dup(ctx, buffer, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx))); + } + + fastd_buffer_free(buffer); +} + +static inline void handle_forward(fastd_context_t *ctx, fastd_peer_t *source_peer, fastd_buffer_t buffer) { + const fastd_eth_addr_t *dest_addr = fastd_get_dest_address(ctx, buffer); if (fastd_eth_addr_is_unicast(dest_addr)) { fastd_peer_t *dest_peer = fastd_peer_find_by_eth_addr(ctx, dest_addr); - if (!dest_peer || dest_peer == peer || !fastd_peer_is_established(dest_peer)) + if (!dest_peer || dest_peer == source_peer || !fastd_peer_is_established(dest_peer)) { + fastd_buffer_free(buffer); return; + } - ctx->conf->protocol->send(ctx, dest_peer, *buffer); - *buffer = FASTD_BUFFER_NULL; + ctx->conf->protocol->send(ctx, dest_peer, buffer); } else { - fastd_peer_t *dest_peer; - for (dest_peer = ctx->peers; dest_peer; dest_peer = dest_peer->next) { - if (dest_peer == peer || !fastd_peer_is_established(dest_peer)) - continue; - - ctx->conf->protocol->send(ctx, dest_peer, fastd_buffer_dup(ctx, *buffer, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx))); - } + send_all(ctx, source_peer, buffer); } } @@ -645,8 +652,10 @@ void fastd_handle_receive(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer if (write(ctx->tunfd, buffer.data, buffer.len) < 0) pr_warn_errno(ctx, "write"); - if (ctx->conf->mode == MODE_TAP && ctx->conf->forward) - handle_forward(ctx, peer, &buffer); + if (ctx->conf->mode == MODE_TAP && ctx->conf->forward) { + handle_forward(ctx, peer, buffer); + return; + } fastd_buffer_free(buffer); } @@ -876,15 +885,7 @@ static void handle_tun(fastd_context_t *ctx) { return; /* TUN mode or multicast packet */ - fastd_peer_t *peer; - for (peer = ctx->peers; peer; peer = peer->next) { - if (!fastd_peer_is_established(peer)) - continue; - - ctx->conf->protocol->send(ctx, peer, fastd_buffer_dup(ctx, buffer, methods_min_encrypt_head_space(ctx), methods_min_encrypt_tail_space(ctx))); - } - - fastd_buffer_free(buffer); + send_all(ctx, NULL, buffer); } static inline void handle_socket_control(fastd_context_t *ctx, struct msghdr *message, const fastd_socket_t *sock, fastd_peer_address_t *local_addr) { diff --git a/src/fastd.h b/src/fastd.h index 2a4e9ac..4ce5bb4 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -378,8 +378,6 @@ static inline size_t alignto(size_t l, size_t a) { return ((l+a-1)/a)*a; } -#define FASTD_BUFFER_NULL ((fastd_buffer_t){}) - static inline fastd_buffer_t fastd_buffer_alloc(const fastd_context_t *ctx, size_t len, size_t head_space, size_t tail_space) { size_t base_len = head_space+len+tail_space; void *ptr; |