warp-vpn

point to point VPN implementation
git clone git://git.2f30.org/warp-vpn.git
Log | Files | Refs | README

dev_linux.c (1111B)


      1 #include <sys/types.h>
      2 #include <sys/ioctl.h>
      3 #include <sys/socket.h>
      4 
      5 #include <net/if.h>
      6 #include <linux/if_tun.h>
      7 
      8 #include <fcntl.h>
      9 #include <string.h>
     10 #include <unistd.h>
     11 
     12 #include "warp.h"
     13 
     14 int
     15 devopen(char *ifname)
     16 {
     17 	struct ifreq ifr;
     18 	int fd, s;
     19 
     20 	if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
     21 		fatal("open %s", "/dev/net/tun");
     22 
     23 	memset(&ifr, 0, sizeof(ifr));
     24 	ifr.ifr_flags = (devtype == TUNDEV ? IFF_TUN : IFF_TAP) | IFF_NO_PI;
     25 	strncpy(ifr.ifr_name, ifname, IF_NAMESIZE);
     26 	ifr.ifr_name[IF_NAMESIZE - 1] = '\0';
     27 	if (ioctl(fd, TUNSETIFF, &ifr) < 0)
     28 		fatal("TUNSETIFF %s", ifname);
     29 
     30 	/* dummy socket so we can manipulate the params */
     31 	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
     32 		fatal("socket");
     33 	if (devtype == TUNDEV)
     34 		ifr.ifr_mtu = MAXPAYLOADLEN;
     35 	else
     36 		ifr.ifr_mtu = MAXPAYLOADLEN - 14; /* make room for ethernet header */
     37 	if (ioctl(s, SIOCSIFMTU, &ifr) < 0)
     38 		fatal("SIOCSIFMTU %s", ifname);
     39 	close(s);
     40 
     41 	return fd;
     42 }
     43 
     44 int
     45 devwrite(int fd, unsigned char *buf, int len)
     46 {
     47 	return write(fd, buf, len);
     48 }
     49 
     50 int
     51 devread(int fd, unsigned char *buf, int len)
     52 {
     53 	return read(fd, buf, len);
     54 }