warp-vpn

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

commit 39166bfc5470797db57977c1341d2c5dc35aaedc
parent 86348191f0e2e02b3eb2cb903aee30be3bb6e928
Author: sin <sin@2f30.org>
Date:   Tue, 22 Mar 2016 10:10:29 +0000

Add primitive linux support

Requires libbsd for arc4random and possibly others.

Diffstat:
Mconfig.mk | 4++++
Mstun.c | 42++++++++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/config.mk b/config.mk @@ -17,6 +17,10 @@ RANLIB = ranlib CFLAGS = -O2 -std=c99 LDFLAGS = -lcrypto -s +# linux +#CFLAGS = -O2 -std=c99 +#LDFLAGS = -lcrypto -lbsd -s + # optimized static #CFLAGS = -static -O2 -std=c99 #LDFLAGS = -lcrypto -static -s diff --git a/stun.c b/stun.c @@ -4,7 +4,12 @@ #include <sys/socket.h> #include <net/if.h> +#ifdef __linux__ +#include <linux/if_tun.h> +#include <bsd/stdlib.h> +#else #include <net/if_tun.h> +#endif #include <netinet/in.h> #include <netinet/tcp.h> #include <arpa/inet.h> @@ -106,6 +111,42 @@ dummydec(EVP_CIPHER_CTX *ectx, unsigned char *plaintext, return len; } +#ifdef __linux__ +#undef explicit_bzero +#define explicit_bzero bzero + +int +opentun(char *tundev) +{ + struct ifreq ifr; + int ret, fd; + + 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); + ifr.ifr_name[IFNAMSIZ - 1] = '\0'; + ret = ioctl(fd, TUNSETIFF, &ifr); + if (ret < 0) + err(1, "ioctl: TUNSETIFF %s", tundev); + return fd; +} + +int +writetun(int fd, unsigned char *buf, int len) +{ + return write(fd, buf, len); +} + +int +readtun(int fd, unsigned char *buf, int len) +{ + return read(fd, buf, len); +} + +#else int opentun(char *tundev) { @@ -162,6 +203,7 @@ readtun(int fd, unsigned char *buf, int len) return n - sizeof(type); return n; } +#endif void pack16(unsigned char *buf, uint16_t n)