diff --git a/sockssrv.c b/sockssrv.c index dfe3e15..98e4622 100644 --- a/sockssrv.c +++ b/sockssrv.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include @@ -245,29 +245,25 @@ static void send_error(int fd, enum errorcode ec) { } static void copyloop(int fd1, int fd2) { - int maxfd = fd2; - if(fd1 > fd2) maxfd = fd1; - fd_set fdsc, fds; - FD_ZERO(&fdsc); - FD_SET(fd1, &fdsc); - FD_SET(fd2, &fdsc); + struct pollfd fds[2] = { + [0] = {.fd = fd1, .events = POLLIN}, + [1] = {.fd = fd2, .events = POLLIN}, + }; while(1) { - memcpy(&fds, &fdsc, sizeof(fds)); /* inactive connections are reaped after 15 min to free resources. usually programs send keep-alive packets so this should only happen when a connection is really unused. */ - struct timeval timeout = {.tv_sec = 60*15, .tv_usec = 0}; - switch(select(maxfd+1, &fds, 0, 0, &timeout)) { + switch(poll(fds, 2, 60*15*1000)) { case 0: send_error(fd1, EC_TTL_EXPIRED); return; case -1: - if(errno == EINTR) continue; - else perror("select"); + if(errno == EINTR || errno == EAGAIN) continue; + else perror("poll"); return; } - int infd = FD_ISSET(fd1, &fds) ? fd1 : fd2; + int infd = (fds[0].revents & POLLIN) ? fd1 : fd2; int outfd = infd == fd2 ? fd1 : fd2; char buf[1024]; ssize_t sent = 0, n = read(infd, buf, sizeof buf);