X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fbsd%2Fdevice.c;h=9a1688a805624a0146f08e93ef09e8e62ee30754;hb=3fba80174dbe29bcfe0d121a2a1d2e61be5ee57b;hp=e8fdc1bc3abc94b603f022f8382c415d39babb96;hpb=798fa2f04c52b0639713f74b1195847bec40c16a;p=meshlink diff --git a/src/bsd/device.c b/src/bsd/device.c index e8fdc1bc..9a1688a8 100644 --- a/src/bsd/device.c +++ b/src/bsd/device.c @@ -1,7 +1,7 @@ /* device.c -- Interaction BSD tun/tap device Copyright (C) 2001-2005 Ivo Timmermans, - 2001-2009 Guus Sliepen + 2001-2012 Guus Sliepen 2009 Grzegorz Dymarek This program is free software; you can redistribute it and/or modify @@ -22,6 +22,7 @@ #include "system.h" #include "conf.h" +#include "device.h" #include "logger.h" #include "net.h" #include "route.h" @@ -51,13 +52,13 @@ static uint64_t device_total_in = 0; static uint64_t device_total_out = 0; #if defined(TUNEMU) static device_type_t device_type = DEVICE_TYPE_TUNEMU; -#elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) +#elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY) static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD; #else static device_type_t device_type = DEVICE_TYPE_TUN; #endif -bool setup_device(void) { +static bool setup_device(void) { char *type; if(!get_config_string(lookup_config(config_tree, "Device"), &device)) @@ -105,6 +106,10 @@ bool setup_device(void) { return false; } +#ifdef FD_CLOEXEC + fcntl(device_fd, F_SETFD, FD_CLOEXEC); +#endif + switch(device_type) { default: device_type = DEVICE_TYPE_TUN; @@ -174,7 +179,7 @@ bool setup_device(void) { return true; } -void close_device(void) { +static void close_device(void) { switch(device_type) { #ifdef HAVE_TUNEMU case DEVICE_TYPE_TUNEMU: @@ -189,21 +194,20 @@ void close_device(void) { free(iface); } -bool read_packet(vpn_packet_t *packet) { - int lenin; +static bool read_packet(vpn_packet_t *packet) { + int inlen; switch(device_type) { case DEVICE_TYPE_TUN: #ifdef HAVE_TUNEMU case DEVICE_TYPE_TUNEMU: if(device_type == DEVICE_TYPE_TUNEMU) - lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14); + inlen = tunemu_read(device_fd, packet->data + 14, MTU - 14); else -#else - lenin = read(device_fd, packet->data + 14, MTU - 14); #endif + inlen = read(device_fd, packet->data + 14, MTU - 14); - if(lenin <= 0) { + if(inlen <= 0) { logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno)); return false; @@ -225,14 +229,14 @@ bool read_packet(vpn_packet_t *packet) { return false; } - packet->len = lenin + 14; + packet->len = inlen + 14; break; case DEVICE_TYPE_TUNIFHEAD: { u_int32_t type; - struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}}; + struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, MTU - 14}}; - if((lenin = readv(device_fd, vector, 2)) <= 0) { + if((inlen = readv(device_fd, vector, 2)) <= 0) { logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno)); return false; @@ -256,18 +260,18 @@ bool read_packet(vpn_packet_t *packet) { return false; } - packet->len = lenin + 10; + packet->len = inlen + 10; break; } case DEVICE_TYPE_TAP: - if((lenin = read(device_fd, packet->data, MTU)) <= 0) { + if((inlen = read(device_fd, packet->data, MTU)) <= 0) { logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno)); return false; } - packet->len = lenin; + packet->len = inlen; break; default: @@ -282,7 +286,7 @@ bool read_packet(vpn_packet_t *packet) { return true; } -bool write_packet(vpn_packet_t *packet) { +static bool write_packet(vpn_packet_t *packet) { ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info); @@ -297,7 +301,7 @@ bool write_packet(vpn_packet_t *packet) { case DEVICE_TYPE_TUNIFHEAD: { u_int32_t type; - struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}}; + struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, packet->len - 14}}; int af; af = (packet->data[12] << 8) + packet->data[13]; @@ -351,8 +355,16 @@ bool write_packet(vpn_packet_t *packet) { return true; } -void dump_device_stats(void) { +static void dump_device_stats(void) { logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device); logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in); logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out); } + +const devops_t os_devops = { + .setup = setup_device, + .close = close_device, + .read = read_packet, + .write = write_packet, + .dump_stats = dump_device_stats, +};