X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fbsd%2Fdevice.c;h=badd77d7a4b07787329e4ba8279e0d4d133b2ddc;hb=76ec2548d0a3e2f1ea92da025549e2abce2bf502;hp=6d68bc781aa7a66c71414e41cb63259e36099f4b;hpb=f6e87ab476a0faf8b124ecaaa27f967d825e6457;p=tinc diff --git a/src/bsd/device.c b/src/bsd/device.c index 6d68bc78..badd77d7 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-2017 Guus Sliepen + 2001-2022 Guus Sliepen 2009 Grzegorz Dymarek This program is free software; you can redistribute it and/or modify @@ -24,14 +24,11 @@ #include "../conf.h" #include "../device.h" #include "../logger.h" -#include "../names.h" -#include "../net.h" #include "../route.h" -#include "../utils.h" #include "../xalloc.h" #ifdef ENABLE_TUNEMU -#include "bsd/tunemu.h" +#include "tunemu.h" #endif #ifdef HAVE_NET_IF_UTUN_H @@ -40,8 +37,13 @@ #include #endif +#if defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY) +#define DEFAULT_TUN_DEVICE "/dev/tun" // Use the autoclone device +#define DEFAULT_TAP_DEVICE "/dev/tap" +#else #define DEFAULT_TUN_DEVICE "/dev/tun0" #define DEFAULT_TAP_DEVICE "/dev/tap0" +#endif typedef enum device_type { DEVICE_TYPE_TUN, @@ -56,7 +58,7 @@ typedef enum device_type { int device_fd = -1; char *device = NULL; char *iface = NULL; -static char *device_info = NULL; +static const char *device_info = "OS X utun device"; #if defined(ENABLE_TUNEMU) static device_type_t device_type = DEVICE_TYPE_TUNEMU; #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY) @@ -74,7 +76,9 @@ static bool setup_utun(void) { return false; } - struct ctl_info info = {}; + struct ctl_info info; + + memset(&info, 0, sizeof(info)); strlcpy(info.ctl_name, UTUN_CONTROL_NAME, sizeof(info.ctl_name)); @@ -83,7 +87,7 @@ static bool setup_utun(void) { return false; } - int unit = -1; + long unit = -1; char *p = strstr(device, "utun"), *e = NULL; if(p) { @@ -116,8 +120,6 @@ static bool setup_utun(void) { iface = xstrdup(name); } - device_info = "OS X utun device"; - logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info); return true; @@ -125,13 +127,13 @@ static bool setup_utun(void) { #endif static bool setup_device(void) { - get_config_string(lookup_config(config_tree, "Device"), &device); + get_config_string(lookup_config(&config_tree, "Device"), &device); // Find out if it's supposed to be a tun or a tap device char *type; - if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) { + if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) { if(!strcasecmp(type, "tun")) /* use default */; @@ -233,7 +235,7 @@ static bool setup_device(void) { realname = device; } - if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) { + if(!get_config_string(lookup_config(&config_tree, "Interface"), &iface)) { iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname); } else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname)) { logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly."); @@ -300,10 +302,7 @@ static bool setup_device(void) { struct ifreq ifr; if(ioctl(device_fd, TAPGIFNAME, (void *)&ifr) == 0) { - if(iface) { - free(iface); - } - + free(iface); iface = xstrdup(ifr.ifr_name); } } @@ -354,7 +353,7 @@ static void close_device(void) { } static bool read_packet(vpn_packet_t *packet) { - int inlen; + ssize_t inlen; switch(device_type) { case DEVICE_TYPE_TUN: @@ -503,7 +502,7 @@ static bool write_packet(vpn_packet_t *packet) { #ifdef ENABLE_TUNEMU case DEVICE_TYPE_TUNEMU: - if(tunemu_write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) { + if(tunemu_write(DATA(packet) + 14, packet->len - 14) < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info, device, strerror(errno)); return false;