From 6d3df65e88c5d5b828ee86660cd3830a37f39ebd Mon Sep 17 00:00:00 2001 From: hniksic Date: Sun, 2 Nov 2003 15:17:59 -0800 Subject: [PATCH] [svn] Generalized the third argument to select_fd. --- src/ChangeLog | 6 ++++++ src/connect.c | 33 +++++++++++++++++++-------------- src/connect.h | 6 ++++++ src/gen_sslfunc.c | 4 ++-- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 9622f283..d4959915 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2003-11-03 Hrvoje Niksic + + * connect.c (select_fd): Generalize the third argument into WAIT, + so that the caller can request waiting for both read and write. + Updated callers. + 2003-11-02 Hrvoje Niksic * html-url.c (cleanup_html_url): Destroy the hash tables, don't diff --git a/src/connect.c b/src/connect.c index 4136cc6e..52dc0b01 100644 --- a/src/connect.c +++ b/src/connect.c @@ -464,29 +464,34 @@ bindport (const ip_address *bind_address, int *port, int *local_sock) } #ifdef HAVE_SELECT -/* Wait for file descriptor FD to be available, timing out after - MAXTIME seconds. "Available" means readable if writep is 0, - writeable otherwise. +/* Wait for file descriptor FD to be readable or writable or both, + timing out after MAXTIME seconds. Returns 1 if FD is available, 0 + for timeout and -1 for error. The argument WHAT can be a + combination of WAIT_READ and WAIT_WRITE. - Returns 1 if FD is available, 0 for timeout and -1 for error. */ + This is a mere convenience wrapper around the select call, and + should be taken as such. */ int -select_fd (int fd, double maxtime, int writep) +select_fd (int fd, double maxtime, int wait) { - fd_set fds; - fd_set *rd = NULL, *wrt = NULL; + fd_set fdset; + fd_set *rd = NULL, *wr = NULL; struct timeval tmout; int result; - FD_ZERO (&fds); - FD_SET (fd, &fds); - *(writep ? &wrt : &rd) = &fds; + FD_ZERO (&fdset); + FD_SET (fd, &fdset); + if (wait & WAIT_READ) + rd = &fdset; + if (wait & WAIT_WRITE) + wr = &fdset; tmout.tv_sec = (long)maxtime; tmout.tv_usec = 1000000L * (maxtime - (long)maxtime); do - result = select (fd + 1, rd, wrt, NULL, &tmout); + result = select (fd + 1, rd, wr, NULL, &tmout); while (result < 0 && errno == EINTR); /* When we've timed out, set errno to ETIMEDOUT for the convenience @@ -512,7 +517,7 @@ acceptport (int local_sock, int *sock) #ifdef HAVE_SELECT if (opt.connect_timeout) - if (select_fd (local_sock, opt.connect_timeout, 0) <= 0) + if (select_fd (local_sock, opt.connect_timeout, WAIT_READ) <= 0) return ACCEPTERR; #endif if ((*sock = accept (local_sock, sa, &addrlen)) < 0) @@ -576,7 +581,7 @@ iread (int fd, char *buf, int len) #ifdef HAVE_SELECT if (opt.read_timeout) - if (select_fd (fd, opt.read_timeout, 0) <= 0) + if (select_fd (fd, opt.read_timeout, WAIT_READ) <= 0) return -1; #endif do @@ -604,7 +609,7 @@ iwrite (int fd, char *buf, int len) { #ifdef HAVE_SELECT if (opt.read_timeout) - if (select_fd (fd, opt.read_timeout, 1) <= 0) + if (select_fd (fd, opt.read_timeout, WAIT_WRITE) <= 0) return -1; #endif do diff --git a/src/connect.h b/src/connect.h index ab161c0b..8b0ed4b3 100644 --- a/src/connect.h +++ b/src/connect.h @@ -37,6 +37,12 @@ enum { E_HOST = -100 }; +/* Flags for select_fd's WAIT argument. */ +enum { + WAIT_READ = 1, + WAIT_WRITE = 2 +}; + /* bindport flags */ #define BIND_ON_IPV4_ONLY LH_IPV4_ONLY #define BIND_ON_IPV6_ONLY LH_IPV6_ONLY diff --git a/src/gen_sslfunc.c b/src/gen_sslfunc.c index d9f1cecc..1e3ecffc 100644 --- a/src/gen_sslfunc.c +++ b/src/gen_sslfunc.c @@ -318,7 +318,7 @@ ssl_iread (SSL *con, char *buf, int len) BIO_get_fd (con->rbio, &fd); #ifdef HAVE_SELECT if (opt.read_timeout && !SSL_pending (con)) - if (select_fd (fd, opt.read_timeout, 0) <= 0) + if (select_fd (fd, opt.read_timeout, WAIT_READ) <= 0) return -1; #endif do @@ -344,7 +344,7 @@ ssl_iwrite (SSL *con, char *buf, int len) { #ifdef HAVE_SELECT if (opt.read_timeout) - if (select_fd (fd, opt.read_timeout, 1) <= 0) + if (select_fd (fd, opt.read_timeout, WAIT_WRITE) <= 0) return -1; #endif do