stun

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

commit 90fc341c2bb7ae5bacbc51986deae17fe6b383c3
parent 2d3560eed3dd059a95122ee38315e578deeef495
Author: sin <sin@2f30.org>
Date:   Tue, 22 Mar 2016 10:41:41 +0000

Set flags + mtu for linux tun

Diffstat:
Mstun.c | 21++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/stun.c b/stun.c @@ -119,11 +119,12 @@ int opentun(char *tundev) { struct ifreq ifr; - int ret, fd; + int ret, fd, s; fd = open("/dev/net/tun", O_RDWR); if (fd < 0) err(1, "open %s", "/dev/net/tun"); + memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TUN | IFF_NO_PI; strncpy(ifr.ifr_name, tundev, IFNAMSIZ); @@ -131,6 +132,24 @@ opentun(char *tundev) ret = ioctl(fd, TUNSETIFF, &ifr); if (ret < 0) err(1, "ioctl: TUNSETIFF %s", tundev); + + /* dummy socket so we can manipulate the params */ + s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) + err(1, "socket"); + ret = ioctl(s, SIOCGIFFLAGS, &ifr); + if (ret < 0) + err(1, "ioctl: SIOCGIFFLAGS %s", tundev); + ifr.ifr_flags |= IFF_UP | IFF_RUNNING; + ret = ioctl(s, SIOCSIFFLAGS, &ifr); + if (ret < 0) + err(1, "ioctl: SIOCSIFFLAGS %s", tundev); + ifr.ifr_mtu = MTU; + ret = ioctl(s, SIOCSIFMTU, &ifr); + if (ret < 0) + err(1, "ioctl: SIOCSIFMTU %s", tundev); + close(s); + return fd; }