#include <signal.h>
#include <fcntl.h>
#include <netinet/in.h>
+#ifdef HAVE_POLL_H
+#include <sys/poll.h>
+#endif
#define _GNU_SOURCE
#include <getopt.h>
{
int in = fileno (stdin), out = fileno (stdout),
nbuffer, wrote;
+#ifdef HAVE_POLL
+ struct pollfd readfds[2], writefds[1];
+#else
fd_set readfds, writefds;
+#endif
char buffer[BUFSIZ], *retry;
struct sigaction act;
act.sa_flags = SA_RESTART|SA_RESETHAND;
sigaction (SIGALRM, &act, NULL);
+#ifdef HAVE_POLL
+ readfds[0].fd = in;
+ readfds[1].fd = tls_fd;
+ readfds[0].events = POLLIN;
+ readfds[1].events = POLLIN;
+ writefds[0].events = POLLOUT;
+#endif
+
while (1)
{
+#ifdef HAVE_POLL
+ if (poll (readfds, 2, -1) == -1 && errno != EINTR)
+#else
FD_ZERO (&readfds);
FD_SET (tls_fd, &readfds);
FD_SET (in, &readfds);
if (select (tls_fd+1, &readfds, NULL, NULL, NULL) == -1
&& errno != EINTR )
+#endif
{
- perror ("select");
+ perror ("poll");
return 1;
}
+#ifdef HAVE_POLL
+ if (readfds[0].revents & POLLIN)
+#else
if (FD_ISSET (in, &readfds))
+#endif
{
nbuffer = read (in, buffer, sizeof buffer -1);
goto finish;
for (retry = buffer; nbuffer > 0; nbuffer -= wrote, retry += wrote)
{
+#ifdef HAVE_POLL
+ writefds[0].fd = tls_fd;
+ if (poll (writefds, 1, -1) == -1)
+#else
FD_ZERO (&writefds);
FD_SET (tls_fd, &writefds);
if (select (tls_fd+1, NULL, &writefds, NULL, NULL) == -1)
+#endif
{
- perror ("select");
+ perror ("poll");
return 1;
}
if (tls_conn)
if (wrote < 0) goto finish;
}
}
+#ifdef HAVE_POLL
+ if (readfds[1].revents & POLLIN)
+#else
if (FD_ISSET (tls_fd, &readfds))
+#endif
{
if (tls_conn)
nbuffer = SSL_read (tls_conn, buffer, sizeof buffer -1);
goto finish;
for (retry = buffer; nbuffer > 0; nbuffer -= wrote, retry += wrote)
{
+#ifdef HAVE_POLL
+ writefds[0].fd = out;
+ if (poll (writefds, 1, -1) == -1)
+#else
FD_ZERO (&writefds);
FD_SET (out, &writefds);
if (select (out+1, NULL, &writefds, NULL, NULL) == -1)
+#endif
{
- perror ("select");
+ perror ("poll");
return 1;
}
wrote = write (out, retry, nbuffer);