stun

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

commit 0ca549361445f8f982eb4bdb18f90b1c3ff8c669
parent 2adfcd8bcdd0469b809690660e95f1f609827e86
Author: sin <sin@2f30.org>
Date:   Thu, 31 Mar 2016 18:23:11 +0100

ignore errors from tun/tap

they are not usually fatal

Diffstat:
Mstun.c | 54++++++++++++++++--------------------------------------
1 file changed, 16 insertions(+), 38 deletions(-)

diff --git a/stun.c b/stun.c @@ -357,23 +357,13 @@ opendev(char *dev) int writedev(int fd, unsigned char *buf, int len) { - int n; - - n = write(fd, buf, len); - if (n < 0) - logerr("write failed"); - return n; + return write(fd, buf, len); } int readdev(int fd, unsigned char *buf, int len) { - int n; - - n = read(fd, buf, len); - if (n < 0) - logerr("read failed"); - return n; + return nread(fd, buf, len); } #elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__) int @@ -408,27 +398,20 @@ writedev(int fd, unsigned char *buf, int len) { struct iovec iov[2]; uint32_t type = htonl(AF_INET); - int n = -1; + int n; switch (devtype) { case TAPDEV: - n = write(fd, buf, len); - if (n < 0) - logerr("write failed"); - break; + return write(fd, buf, len); 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) + if (n > 0) n -= sizeof(type); break; - default: - logerr("invalid device type: %d", devtype); } return n; } @@ -438,27 +421,20 @@ readdev(int fd, unsigned char *buf, int len) { struct iovec iov[2]; uint32_t type; - int n = -1; + int n; switch (devtype) { case TAPDEV: - n = read(fd, buf, len); - if (n < 0) - logerr("read failed"); - break; + return read(fd, buf, len); 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) + if (n > 0) n -= sizeof(type); break; - default: - logerr("invalid device type: %d", devtype); } return n; } @@ -605,16 +581,18 @@ tunnel(int netfd, int devfd) if (n == BADPKT) { logwarn("bad packet"); } else { - n = writedev(devfd, buf, n); + writedev(devfd, buf, n); + } + } + + if (pfd[1].revents & (POLLIN | POLLHUP)) { + n = readdev(devfd, buf, MTU); + if (n > 0) { + n = writenet(netfd, buf, n); if (n <= 0) return -1; } } - - if (pfd[1].revents & (POLLIN | POLLHUP)) - if ((n = readdev(devfd, buf, MTU)) <= 0 || - (n = writenet(netfd, buf, n)) <= 0) - return -1; } return 0; }