X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Ffd_device.c;h=7360d17e7964da2b669b254970edb044f7416d4e;hb=2c6b2d70e6640f39563ad7bb0aa0ba87f883848c;hp=eab9e160824d8052abb4e0dbabf6491cf49a7918;hpb=d0ae7fa0162cf5b70dca1b34b42f49e94bf93a48;p=tinc diff --git a/src/fd_device.c b/src/fd_device.c index eab9e160..7360d17e 100644 --- a/src/fd_device.c +++ b/src/fd_device.c @@ -1,7 +1,7 @@ /* fd_device.c -- Interaction with Android tun fd Copyright (C) 2001-2005 Ivo Timmermans, - 2001-2016 Guus Sliepen + 2001-2021 Guus Sliepen 2009 Grzegorz Dymarek 2016-2020 Pacien TRAN-GIRARD @@ -20,16 +20,17 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "system.h" + +#ifdef HAVE_SYS_UN_H #include -#include "system.h" #include "conf.h" #include "device.h" #include "ethernet.h" #include "logger.h" #include "net.h" #include "route.h" -#include "utils.h" struct unix_socket_addr { size_t size; @@ -41,7 +42,7 @@ static int read_fd(int socket) { struct iovec iov = {0}; char cmsgbuf[CMSG_SPACE(sizeof(device_fd))]; struct msghdr msg = {0}; - int ret; + ssize_t ret; struct cmsghdr *cmsgptr; iov.iov_base = &iobuf; @@ -52,12 +53,15 @@ static int read_fd(int socket) { msg.msg_controllen = sizeof(cmsgbuf); if((ret = recvmsg(socket, &msg, 0)) < 1) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not read from unix socket (error %d)!", ret); + logger(DEBUG_ALWAYS, LOG_ERR, "Could not read from unix socket (error %zd)!", ret); return -1; } + #ifdef IP_RECVERR + if(msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) { #else + if(msg.msg_flags & (MSG_CTRUNC | MSG_OOB)) { #endif logger(DEBUG_ALWAYS, LOG_ERR, "Error while receiving message (flags %d)!", msg.msg_flags); @@ -65,19 +69,22 @@ static int read_fd(int socket) { } cmsgptr = CMSG_FIRSTHDR(&msg); + if(cmsgptr->cmsg_level != SOL_SOCKET) { logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG level: %d, expected %d!", - cmsgptr->cmsg_level, SOL_SOCKET); + cmsgptr->cmsg_level, SOL_SOCKET); return -1; } + if(cmsgptr->cmsg_type != SCM_RIGHTS) { logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG type: %d, expected %d!", - cmsgptr->cmsg_type, SCM_RIGHTS); + cmsgptr->cmsg_type, SCM_RIGHTS); return -1; } + if(cmsgptr->cmsg_len != CMSG_LEN(sizeof(device_fd))) { - logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG data length: %lu, expected %lu!", - (unsigned long)cmsgptr->cmsg_len, CMSG_LEN(sizeof(device_fd))); + logger(DEBUG_ALWAYS, LOG_ERR, "Wrong CMSG data length: %zu, expected %zu!", + cmsgptr->cmsg_len, CMSG_LEN(sizeof(device_fd))); return -1; } @@ -108,15 +115,18 @@ end: } static struct unix_socket_addr parse_socket_addr(const char *path) { - struct sockaddr_un socket_addr; + struct sockaddr_un socket_addr = { + .sun_family = AF_UNIX, + }; size_t path_length; if(strlen(path) >= sizeof(socket_addr.sun_path)) { logger(DEBUG_ALWAYS, LOG_ERR, "Unix socket path too long!"); - return (struct unix_socket_addr) {0}; + return (struct unix_socket_addr) { + 0 + }; } - socket_addr.sun_family = AF_UNIX; strncpy(socket_addr.sun_path, path, sizeof(socket_addr.sun_path)); if(path[0] == '@') { @@ -164,6 +174,10 @@ static bool setup_device(void) { static void close_device(void) { close(device_fd); device_fd = -1; + free(iface); + iface = NULL; + free(device); + device = NULL; } static inline uint16_t get_ip_ethertype(vpn_packet_t *packet) { @@ -187,7 +201,7 @@ static inline void set_etherheader(vpn_packet_t *packet, uint16_t ethertype) { } static bool read_packet(vpn_packet_t *packet) { - int lenin = read(device_fd, DATA(packet) + ETH_HLEN, MTU - ETH_HLEN); + ssize_t lenin = read(device_fd, DATA(packet) + ETH_HLEN, MTU - ETH_HLEN); if(lenin <= 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from fd/%d: %s!", device_fd, strerror(errno)); @@ -226,3 +240,4 @@ const devops_t fd_devops = { .read = read_packet, .write = write_packet, }; +#endif