From eaca7f2143ab4ef454227c0ed286733a0e0e1759 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Wed, 5 Nov 2014 16:43:01 +0100 Subject: [PATCH] tun-device: Remove the superfluous use of select() before read() --- src/libstrongswan/networking/tun_device.c | 21 +++++---------------- src/libstrongswan/networking/tun_device.h | 4 +--- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/src/libstrongswan/networking/tun_device.c b/src/libstrongswan/networking/tun_device.c index ff2c4a337e..035b846bb6 100644 --- a/src/libstrongswan/networking/tun_device.c +++ b/src/libstrongswan/networking/tun_device.c @@ -347,31 +347,20 @@ METHOD(tun_device_t, read_packet, bool, private_tun_device_t *this, chunk_t *packet) { ssize_t len; - fd_set set; bool old; - FD_ZERO(&set); - FD_SET(this->tunfd, &set); + *packet = chunk_alloc(get_mtu(this)); + thread_cleanup_push(free, packet->ptr); old = thread_cancelability(TRUE); - len = select(this->tunfd + 1, &set, NULL, NULL, NULL); - thread_cancelability(old); - - if (len < 0) - { - DBG1(DBG_LIB, "select on TUN device %s failed: %s", this->if_name, - strerror(errno)); - return FALSE; - } - /* FIXME: this is quite expensive for lots of small packets, copy from - * local buffer instead? */ - *packet = chunk_alloc(get_mtu(this)); len = read(this->tunfd, packet->ptr, packet->len); + thread_cancelability(old); + thread_cleanup_pop(FALSE); if (len < 0) { DBG1(DBG_LIB, "reading from TUN device %s failed: %s", this->if_name, strerror(errno)); - chunk_free(packet); + free(packet->ptr); return FALSE; } packet->len = len; diff --git a/src/libstrongswan/networking/tun_device.h b/src/libstrongswan/networking/tun_device.h index 543125bebf..880369ba71 100644 --- a/src/libstrongswan/networking/tun_device.h +++ b/src/libstrongswan/networking/tun_device.h @@ -31,8 +31,6 @@ typedef struct tun_device_t tun_device_t; * Class to create TUN devices * * Creating such a device requires the CAP_NET_ADMIN capability. - * - * @note The implementation is currently very Linux specific */ struct tun_device_t { @@ -42,7 +40,7 @@ struct tun_device_t { * @note This call blocks until a packet is available. It is a thread * cancellation point. * - * @param packet the packet read from the device + * @param packet the packet read from the device, allocated * @return TRUE if successful */ bool (*read_packet)(tun_device_t *this, chunk_t *packet); -- 2.47.2