X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fraw_socket_device.c;h=02f6afaf95f372a7012b4e164aac33e5271d9520;hb=refs%2Fheads%2F1.1;hp=a8c2c861001e29a3b7f5aa49c1b2e528b11882ac;hpb=ed1d0878afe53032a4b63e87afd4a435015cf5de;p=tinc diff --git a/src/raw_socket_device.c b/src/raw_socket_device.c index a8c2c861..1c455e6a 100644 --- a/src/raw_socket_device.c +++ b/src/raw_socket_device.c @@ -28,51 +28,48 @@ #include "device.h" #include "net.h" #include "logger.h" -#include "utils.h" -#include "route.h" #include "xalloc.h" #if defined(PF_PACKET) && defined(ETH_P_ALL) && defined(AF_PACKET) && defined(SIOCGIFINDEX) -static char *device_info; +static const char *device_info = "raw_socket"; static bool setup_device(void) { - struct ifreq ifr; - struct sockaddr_ll sa; + struct ifreq ifr = {0}; + struct sockaddr_ll sa = {0}; - if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) + if(!get_config_string(lookup_config(&config_tree, "Interface"), &iface)) { iface = xstrdup("eth0"); + } - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) + if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) { device = xstrdup(iface); - - device_info = "raw socket"; + } if((device_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device_info, - strerror(errno)); + strerror(errno)); return false; } - memset(&ifr, 0, sizeof ifr); - #ifdef FD_CLOEXEC fcntl(device_fd, F_SETFD, FD_CLOEXEC); #endif strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ); + ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0; + if(ioctl(device_fd, SIOCGIFINDEX, &ifr)) { close(device_fd); logger(DEBUG_ALWAYS, LOG_ERR, "Can't find interface %s: %s", iface, - strerror(errno)); + strerror(errno)); return false; } - memset(&sa, '0', sizeof sa); sa.sll_family = AF_PACKET; sa.sll_protocol = htons(ETH_P_ALL); sa.sll_ifindex = ifr.ifr_ifindex; - if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof sa)) { + if(bind(device_fd, (struct sockaddr *) &sa, (socklen_t) sizeof(sa))) { logger(DEBUG_ALWAYS, LOG_ERR, "Could not bind %s to %s: %s", device, iface, strerror(errno)); return false; } @@ -83,37 +80,40 @@ static bool setup_device(void) { } static void close_device(void) { - close(device_fd); device_fd = -1; + close(device_fd); + device_fd = -1; - free(device); device = NULL; - free(iface); iface = NULL; + free(device); + device = NULL; + free(iface); + iface = NULL; device_info = NULL; } static bool read_packet(vpn_packet_t *packet) { - int inlen; + ssize_t inlen; - if((inlen = read(device_fd, packet->data, MTU)) <= 0) { + if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info, - device, strerror(errno)); + device, strerror(errno)); return false; } packet->len = inlen; logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, - device_info); + device_info); return true; } static bool write_packet(vpn_packet_t *packet) { logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s", - packet->len, device_info); + packet->len, device_info); - if(write(device_fd, packet->data, packet->len) < 0) { + if(write(device_fd, DATA(packet), packet->len) < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device, - strerror(errno)); + strerror(errno)); return false; }