stun

simple point to point tunnel
git clone git://git.2f30.org/stun
Log | Files | Refs | README

commit 0e78bda4c13f689bf95400d7e9b4f76eebf38898
parent 30f0119c50248150d08503521b0720cad6139b52
Author: sin <sin@2f30.org>
Date:   Tue, 29 Mar 2016 09:41:25 +0100

use switch

Diffstat:
Mstun.c | 56++++++++++++++++++++++++++++++++------------------------
1 file changed, 32 insertions(+), 24 deletions(-)

diff --git a/stun.c b/stun.c @@ -264,22 +264,26 @@ writedev(int fd, unsigned char *buf, int len) uint32_t type = htonl(AF_INET); int n; - if (devtype == TAPDEV) { + switch (devtype) { + case TAPDEV: n = write(fd, buf, len); if (n < 0) logerr("write failed"); - return n; + break; + case TUNDEV: + iov[0].iov_base = &type; + iov[0].iov_len = sizeof(type); + iov[1].iov_base = buf; + iov[1].iov_len = len; + n = writev(fd, iov, 2); + if (n < 0) + logerr("writev failed"); + else if (n > 0) + n -= sizeof(type); + break; + default: + logerr("invalid device type: %d", devtype); } - - iov[0].iov_base = &type; - iov[0].iov_len = sizeof(type); - iov[1].iov_base = buf; - iov[1].iov_len = len; - n = writev(fd, iov, 2); - if (n < 0) - logerr("writev failed"); - else if (n > 0) - return n - sizeof(type); return n; } @@ -290,22 +294,26 @@ readdev(int fd, unsigned char *buf, int len) uint32_t type; int n; - if (devtype == TAPDEV) { + switch (devtype) { + case TAPDEV: n = read(fd, buf, len); if (n < 0) logerr("read failed"); - return n; + break; + case TUNDEV: + iov[0].iov_base = &type; + iov[0].iov_len = sizeof(type); + iov[1].iov_base = buf; + iov[1].iov_len = len; + n = readv(fd, iov, 2); + if (n < 0) + logerr("readv failed"); + else if (n > 0) + n -= sizeof(type); + break; + default: + logerr("invalid device type: %d", devtype); } - - iov[0].iov_base = &type; - iov[0].iov_len = sizeof(type); - iov[1].iov_base = buf; - iov[1].iov_len = len; - n = readv(fd, iov, 2); - if (n < 0) - logerr("readv failed"); - else if (n > 0) - return n - sizeof(type); return n; } #endif