warp-vpn

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

tunnel.c (732B)


      1 #include <poll.h>
      2 
      3 #include "warp.h"
      4 
      5 int
      6 tunnel(int netfd, int devfd)
      7 {
      8 	unsigned char buf[MAXPAYLOADLEN];
      9 	unsigned long long outlen;
     10 	struct pollfd pfd[2];
     11 	int n;
     12 
     13 	pfd[0].fd = netfd;
     14 	pfd[0].events = POLLIN;
     15 	pfd[1].fd = devfd;
     16 	pfd[1].events = POLLIN;
     17 	for (;;) {
     18 		if (poll(pfd, 2, -1) < 0) {
     19 			logwarn("poll");
     20 			return -1;
     21 		}
     22 
     23 		if (pfd[0].revents & (POLLIN | POLLHUP)) {
     24 			n = netread(netfd, buf, sizeof(buf), &outlen);
     25 			if (n == PKTFAILED)
     26 				return -1;
     27 			else if (n == PKTCOMPLETE)
     28 				devwrite(devfd, buf, outlen);
     29 		}
     30 
     31 		if (pfd[1].revents & (POLLIN | POLLHUP)) {
     32 			n = devread(devfd, buf, sizeof(buf));
     33 			if (n > 0)
     34 				if (netwrite(netfd, buf, n, &outlen) == PKTFAILED)
     35 					return -1;
     36 		}
     37 	}
     38 	return 0;
     39 }