diff options
Diffstat (limited to 'contrib/ipfilter/relay.c')
-rw-r--r-- | contrib/ipfilter/relay.c | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/contrib/ipfilter/relay.c b/contrib/ipfilter/relay.c index 58a44f0..6a67433 100644 --- a/contrib/ipfilter/relay.c +++ b/contrib/ipfilter/relay.c @@ -16,6 +16,10 @@ #include <netinet/in.h> #include <net/if.h> #include <sys/socket.h> +#if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105000000) +# include <poll.h> +# define USE_POLL +#endif #include "ip_nat.h" #define RELAY_BUFSZ 8192 @@ -26,7 +30,11 @@ char obuff[RELAY_BUFSZ]; int relay(ifd, ofd, rfd) int ifd, ofd, rfd; { +#ifdef USE_POLL + struct pollfd set[3]; +#else fd_set rfds, wfds; +#endif char *irh, *irt, *rrh, *rrt; char *iwh, *iwt, *rwh, *rwt; int nfd, n, rw; @@ -39,7 +47,21 @@ int ifd, ofd, rfd; if (nfd < rfd) nfd = rfd; +#ifdef USE_POLL + set[0].fd = rfd; + set[1].fd = ifd; + set[2].fd = ofd; +#endif + while (1) { +#ifdef USE_POLL + set[0].events = (iwh < (obuff + RELAY_BUFSZ) ? POLLIN : 0) | + (irh > irt ? POLLOUT : 0); + set[1].events = (irh < (ibuff + RELAY_BUFSZ) ? POLLIN : 0); + set[2].events = (iwh > iwt ? POLLOUT : 0); + + switch ((n = poll(set, 3, INFTIM))) +#else FD_ZERO(&rfds); FD_ZERO(&wfds); if (irh > irt) @@ -52,12 +74,18 @@ int ifd, ofd, rfd; FD_SET(rfd, &rfds); switch ((n = select(nfd + 1, &rfds, &wfds, NULL, NULL))) +#endif { case -1 : case 0 : return -1; default : - if (FD_ISSET(ifd, &rfds)) { +#ifdef USE_POLL + if (set[1].revents & POLLIN) +#else + if (FD_ISSET(ifd, &rfds)) +#endif + { rw = read(ifd, irh, ibuff + RELAY_BUFSZ - irh); if (rw == -1) return -1; @@ -66,14 +94,24 @@ int ifd, ofd, rfd; irh += rw; n--; } - if (n && FD_ISSET(ofd, &wfds)) { +#ifdef USE_POLL + if (set[2].revents & POLLOUT) +#else + if (n && FD_ISSET(ofd, &wfds)) +#endif + { rw = write(ofd, iwt, iwh - iwt); if (rw == -1) return -1; iwt += rw; n--; } - if (n && FD_ISSET(rfd, &rfds)) { +#ifdef USE_POLL + if (set[0].revents & POLLIN) +#else + if (n && FD_ISSET(rfd, &rfds)) +#endif + { rw = read(rfd, iwh, obuff + RELAY_BUFSZ - iwh); if (rw == -1) return -1; @@ -82,7 +120,12 @@ int ifd, ofd, rfd; iwh += rw; n--; } - if (n && FD_ISSET(rfd, &wfds)) { +#ifdef USE_POLL + if (set[0].revents & POLLOUT) +#else + if (n && FD_ISSET(rfd, &wfds)) +#endif + { rw = write(rfd, irt, irh - irt); if (rw == -1) return -1; |