From da2cea6559f03d4d4b00b8380de2d32416db0030 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 9 Aug 2013 00:10:16 +0200 Subject: Merge Linux and FreeBSD fastd_tuntap_{read,write} implementations --- src/tuntap.c | 51 ++++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/tuntap.c b/src/tuntap.c index 4ee1660..fb7026f 100644 --- a/src/tuntap.c +++ b/src/tuntap.c @@ -35,6 +35,10 @@ #include + +static const bool multiaf_tun = false; + + void fastd_tuntap_open(fastd_context_t *ctx) { struct ifreq ifr = {}; @@ -84,34 +88,15 @@ void fastd_tuntap_open(fastd_context_t *ctx) { pr_debug(ctx, "tun/tap device initialized."); } -fastd_buffer_t fastd_tuntap_read(fastd_context_t *ctx) { - size_t max_len = fastd_max_packet_size(ctx); - fastd_buffer_t buffer = fastd_buffer_alloc(ctx, max_len, ctx->conf->min_encrypt_head_space, ctx->conf->min_encrypt_tail_space); - - ssize_t len = read(ctx->tunfd, buffer.data, max_len); - if (len < 0) { - if (errno == EINTR) { - fastd_buffer_free(buffer); - return (fastd_buffer_t){}; - } - - exit_errno(ctx, "read"); - } - - buffer.len = len; - return buffer; -} - -void fastd_tuntap_write(fastd_context_t *ctx, fastd_buffer_t buffer) { - if (write(ctx->tunfd, buffer.data, buffer.len) < 0) - pr_warn_errno(ctx, "write"); -} - #elif defined(__FreeBSD__) #include #include + +static const bool multiaf_tun = true; + + static void setup_tap(fastd_context_t *ctx) { struct ifreq ifr = {}; @@ -204,11 +189,18 @@ void fastd_tuntap_open(fastd_context_t *ctx) { pr_debug(ctx, "tun/tap device initialized."); } +#else + +#error unknown tun/tap implementation + +#endif + + fastd_buffer_t fastd_tuntap_read(fastd_context_t *ctx) { size_t max_len = fastd_max_packet_size(ctx); fastd_buffer_t buffer; - if (ctx->conf->mode == MODE_TUN) + if (multiaf_tun && ctx->conf->mode == MODE_TUN) buffer = fastd_buffer_alloc(ctx, max_len+4, ctx->conf->min_encrypt_head_space+12, ctx->conf->min_encrypt_tail_space); else buffer = fastd_buffer_alloc(ctx, max_len, ctx->conf->min_encrypt_head_space, ctx->conf->min_encrypt_tail_space); @@ -225,14 +217,14 @@ fastd_buffer_t fastd_tuntap_read(fastd_context_t *ctx) { buffer.len = len; - if (ctx->conf->mode == MODE_TUN) + if (multiaf_tun && ctx->conf->mode == MODE_TUN) fastd_buffer_push_head(ctx, &buffer, 4); return buffer; } void fastd_tuntap_write(fastd_context_t *ctx, fastd_buffer_t buffer) { - if (ctx->conf->mode == MODE_TUN) { + if (multiaf_tun && ctx->conf->mode == MODE_TUN) { uint8_t version = *((uint8_t*)buffer.data) >> 4; int af; @@ -258,13 +250,6 @@ void fastd_tuntap_write(fastd_context_t *ctx, fastd_buffer_t buffer) { pr_warn_errno(ctx, "write"); } -#else - -#error unknown tun/tap implementation - -#endif - - void fastd_tuntap_close(fastd_context_t *ctx) { if (close(ctx->tunfd)) pr_warn_errno(ctx, "closing tun/tap: close"); -- cgit v1.2.3