diff options
author | Martin Willi <martin@revosec.ch> | 2014-11-21 11:23:08 +0100 |
---|---|---|
committer | Martin Willi <martin@revosec.ch> | 2014-11-21 12:02:07 +0100 |
commit | 946cf367d4129635ea35254b33977f07335488ff (patch) | |
tree | d2ecc744df3ba8f83baa1ce1ae9211e2a6ba70dd | |
parent | eaca7f2143ab4ef454227c0ed286733a0e0e1759 (diff) | |
download | strongswan-946cf367d4129635ea35254b33977f07335488ff.tar.bz2 strongswan-946cf367d4129635ea35254b33977f07335488ff.tar.xz |
tun-device: Read from tun to buffer on stack to avoid over-allocation of packets
Instead of allocating MTU-sized buffers for each packet, read to a stack buffer
and copy to an allocation of the actual packet size. While it requires an
additional copy on non-Apple platforms, this should make allocation more
efficient for small packets.
-rw-r--r-- | src/libstrongswan/networking/tun_device.c | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/src/libstrongswan/networking/tun_device.c b/src/libstrongswan/networking/tun_device.c index 035b846bb..81d215677 100644 --- a/src/libstrongswan/networking/tun_device.c +++ b/src/libstrongswan/networking/tun_device.c @@ -346,29 +346,27 @@ METHOD(tun_device_t, write_packet, bool, METHOD(tun_device_t, read_packet, bool, private_tun_device_t *this, chunk_t *packet) { + chunk_t data; ssize_t len; bool old; - *packet = chunk_alloc(get_mtu(this)); + data = chunk_alloca(get_mtu(this)); - thread_cleanup_push(free, packet->ptr); old = thread_cancelability(TRUE); - len = read(this->tunfd, packet->ptr, packet->len); + len = read(this->tunfd, data.ptr, data.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)); - free(packet->ptr); return FALSE; } - packet->len = len; + data.len = len; #ifdef __APPLE__ /* UTUN's prepend packets with a 32-bit protocol number */ - packet->len -= sizeof(u_int32_t); - memmove(packet->ptr, packet->ptr + sizeof(u_int32_t), packet->len); + data = chunk_skip(data, sizeof(u_int32_t)); #endif + *packet = chunk_clone(data); return TRUE; } |