stun

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

commit 799f3cee234877f41351a47c8dbfcb0daab5daec
parent 45d0f744cd44a92d51103064d3b3fae242b3c0a8
Author: sin <sin@2f30.org>
Date:   Thu, 24 Mar 2016 10:13:12 +0000

squash tun/tap code

Diffstat:
Mstun.c | 152++++++++++++++++++++++++++-----------------------------------------------------
1 file changed, 50 insertions(+), 102 deletions(-)

diff --git a/stun.c b/stun.c @@ -178,31 +178,9 @@ dummydec(EVP_CIPHER_CTX *ectx, unsigned char *plaintext, return len; } -int (*opendev)(char *); -int (*writedev)(int, unsigned char *, int); -int (*readdev)(int, unsigned char *, int); - #if defined(__linux__) int -opentap(char *tapdev) -{ - logerr("linux tap support is not ready"); -} - -int -writetap(int fd, unsigned char *buf, int len) -{ - return write(fd, buf, len); -} - -int -readtap(int fd, unsigned char *buf, int len) -{ - return read(fd, buf, len); -} - -int -opentun(char *tundev) +opendev(char *dev) { struct ifreq ifr; int fd, s; @@ -212,11 +190,11 @@ opentun(char *tundev) logerr("failed to open %s", "/dev/net/tun"); memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_flags = IFF_TUN | IFF_NO_PI; - strncpy(ifr.ifr_name, tundev, IF_NAMESIZE); + ifr.ifr_flags = (devtype == TUNDEV ? IFF_TUN : IFF_TAP) | IFF_NO_PI; + strncpy(ifr.ifr_name, dev, IF_NAMESIZE); ifr.ifr_name[IF_NAMESIZE - 1] = '\0'; if (ioctl(fd, TUNSETIFF, &ifr) < 0) - logerr("failed to set TUNSETIFF on %s", tundev); + logerr("failed to set TUNSETIFF on %s", dev); /* dummy socket so we can manipulate the params */ s = socket(AF_INET, SOCK_STREAM, 0); @@ -224,111 +202,94 @@ opentun(char *tundev) logerr("failed to create socket"); ifr.ifr_mtu = MTU; if (ioctl(s, SIOCSIFMTU, &ifr) < 0) - logerr("failed to set MTU on %s", tundev); + logerr("failed to set MTU on %s", dev); close(s); return fd; } int -writetun(int fd, unsigned char *buf, int len) +writedev(int fd, unsigned char *buf, int len) { return write(fd, buf, len); } int -readtun(int fd, unsigned char *buf, int len) +readdev(int fd, unsigned char *buf, int len) { return read(fd, buf, len); } #elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__) int -opentap(char *tapdev) +opendev(char *dev) { struct tuninfo ti; int fd; - fd = open(tapdev, O_RDWR); + fd = open(dev, O_RDWR); if (fd < 0) - logerr("failed to open %s", tapdev); + logerr("failed to open %s", dev); if (ioctl(fd, TUNGIFINFO, &ti) < 0) - logerr("failed to set TUNGIFINFO on %s", tapdev); - ti.mtu = MTU - 14; + logerr("failed to set TUNGIFINFO on %s", dev); + if (devtype == TUNDEV) + ti.mtu = MTU; + else + ti.mtu = MTU - 14; /* make some room for ethernet header */ if (ioctl(fd, TUNSIFINFO, &ti) < 0) - logerr("failed to set TUNSIFINFO on %s", tapdev); - return fd; -} - -int -writetap(int fd, unsigned char *buf, int len) -{ - return write(fd, buf, len); -} + logerr("failed to set TUNSIFINFO on %s", dev); -int -readtap(int fd, unsigned char *buf, int len) -{ - return read(fd, buf, len); -} - -int -opentun(char *tundev) -{ - struct tuninfo ti; - int fd; - - fd = open(tundev, O_RDWR); - if (fd < 0) - logerr("failed to open %s", tundev); - if (ioctl(fd, TUNGIFINFO, &ti) < 0) - logerr("failed to set TUNGIFINFO on %s", tundev); - ti.mtu = MTU; - if (ioctl(fd, TUNSIFINFO, &ti) < 0) - logerr("failed to set TUNSIFINFO on %s", tundev); + if (devtype == TUNDEV) { #if defined(TUNSIFHEAD) - int one = 1; - if (ioctl(fd, TUNSIFHEAD, &one) < 0) - logerr("failed to set TUNSIFHEAD on %s", tundev); + int one = 1; + if (ioctl(fd, TUNSIFHEAD, &one) < 0) + logerr("failed to set TUNSIFHEAD on %s", dev); #endif + } return fd; } int -writetun(int fd, unsigned char *buf, int len) +writedev(int fd, unsigned char *buf, int len) { struct iovec iov[2]; uint32_t type = htonl(AF_INET); int n; - 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; + if (devtype == 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) + return n - sizeof(type); + return n; + } + return write(fd, buf, len); } int -readtun(int fd, unsigned char *buf, int len) +readdev(int fd, unsigned char *buf, int len) { struct iovec iov[2]; uint32_t type; int n; - 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; + if (devtype == 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) + return n - sizeof(type); + return n; + } + return readdev(fd, buf, len); } #endif @@ -705,20 +666,7 @@ main(int argc, char *argv[]) daemonize(); openlog("stun", LOG_PID | LOG_NDELAY, LOG_DAEMON); - switch (devtype) { - case TUNDEV: - devfd = opentun(argv[0]); - opendev = opentun; - writedev = writetun; - readdev = readtun; - break; - case TAPDEV: - devfd = opentap(argv[0]); - opendev = opentap; - writedev = writetap; - readdev = readtap; - break; - } + devfd = opendev(argv[0]); pw = getenv("STUNPW"); if (!pw)