warp-vpn

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

commit c3a973ccab814e503dee96f28c03270ce4c319b8
parent b9e48021ed4e9a319e24c0c30008faf9c6dd487b
Author: sin <sin@2f30.org>
Date:   Tue, 12 Apr 2016 17:02:47 +0100

factor out tunnel handling

Diffstat:
Makefile | 5+++--
stun.c | 35-----------------------------------
stun.h | 2+-
tunnel.c | 39+++++++++++++++++++++++++++++++++++++++
4 files changed, 43 insertions(+), 38 deletions(-)

diff --git a/Makefile b/Makefile @@ -2,8 +2,9 @@ include config.mk DISTFILES = Makefile README WHATSNEW UNLICENSE arg.h auth.c \ client.c config.mk crypto.c dev_bsd.c dev_linux.c log.c \ - netpkt.c server.c stun.8 stun.c stun.h util.c -OBJ = $(EXTRAOBJ) auth.o client.o crypto.o log.o netpkt.o server.o stun.o util.o + netpkt.c server.c stun.8 stun.c stun.h tunnel.c util.c +OBJ = $(EXTRAOBJ) auth.o client.o crypto.o log.o netpkt.o server.o \ + stun.o tunnel.o util.o BIN = stun all: $(BIN) diff --git a/stun.c b/stun.c @@ -43,7 +43,6 @@ #include <sys/resource.h> -#include <poll.h> #include <signal.h> #include <stdio.h> #include <stdint.h> @@ -63,40 +62,6 @@ int devtype = TUNDEV; int debug; int sflag; -int -tunnel(int netfd, int devfd) -{ - unsigned char buf[MAXPAYLOADLEN]; - size_t outlen; - struct pollfd pfd[2]; - int n; - - pfd[0].fd = netfd; - pfd[0].events = POLLIN; - pfd[1].fd = devfd; - pfd[1].events = POLLIN; - for (;;) { - if (poll(pfd, 2, -1) < 0) - logerr("poll failed"); - - if (pfd[0].revents & (POLLIN | POLLHUP)) { - n = netread(netfd, buf, sizeof(buf), &outlen); - if (n == PKTFAILED) - return -1; - else if (n == PKTCOMPLETE) - devwrite(devfd, buf, outlen); - } - - if (pfd[1].revents & (POLLIN | POLLHUP)) { - n = devread(devfd, buf, sizeof(buf)); - if (n > 0) - if (netwrite(netfd, buf, n, &outlen) == PKTFAILED) - return -1; - } - } - return 0; -} - void usage(void) { diff --git a/stun.h b/stun.h @@ -60,7 +60,7 @@ void netinit(void); int serverinit(char *, char *); int serveraccept(int); -/* stun.c */ +/* tunnel.c */ int tunnel(int, int); /* util.c */ diff --git a/tunnel.c b/tunnel.c @@ -0,0 +1,39 @@ +#include <poll.h> +#include <stdint.h> +#include <stdio.h> + +#include "stun.h" + +int +tunnel(int netfd, int devfd) +{ + unsigned char buf[MAXPAYLOADLEN]; + size_t outlen; + struct pollfd pfd[2]; + int n; + + pfd[0].fd = netfd; + pfd[0].events = POLLIN; + pfd[1].fd = devfd; + pfd[1].events = POLLIN; + for (;;) { + if (poll(pfd, 2, -1) < 0) + logerr("poll failed"); + + if (pfd[0].revents & (POLLIN | POLLHUP)) { + n = netread(netfd, buf, sizeof(buf), &outlen); + if (n == PKTFAILED) + return -1; + else if (n == PKTCOMPLETE) + devwrite(devfd, buf, outlen); + } + + if (pfd[1].revents & (POLLIN | POLLHUP)) { + n = devread(devfd, buf, sizeof(buf)); + if (n > 0) + if (netwrite(netfd, buf, n, &outlen) == PKTFAILED) + return -1; + } + } + return 0; +}