Skip to content

Commit

Permalink
Don't wait for _writable_ when _reading_ an SSL socket.
Browse files Browse the repository at this point in the history
Also, don't select() at all if SSL_pending() says there's data to
read.

Patch by Fabian Mauchle.
  • Loading branch information
Linus Nordberg committed Sep 2, 2013
1 parent edaa77b commit ae28195
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,25 @@ int tlsconnect(struct server *server, struct timeval *when, int timeout, char *t
/* returns 0 on timeout, -1 on error and num if ok */
int sslreadtimeout(SSL *ssl, unsigned char *buf, int num, int timeout) {
int s, ndesc, cnt, len;
fd_set readfds, writefds;
fd_set readfds;
struct timeval timer;

s = SSL_get_fd(ssl);
if (s < 0)
return -1;
/* make socket non-blocking? */
for (len = 0; len < num; len += cnt) {
FD_ZERO(&readfds);
FD_SET(s, &readfds);
writefds = readfds;
if (timeout) {
timer.tv_sec = timeout;
timer.tv_usec = 0;
if (SSL_pending(ssl) == 0) {
FD_ZERO(&readfds);
FD_SET(s, &readfds);
if (timeout) {
timer.tv_sec = timeout;
timer.tv_usec = 0;
}
ndesc = select(s + 1, &readfds, NULL, NULL, timeout ? &timer : NULL);
if (ndesc < 1)
return ndesc;
}
ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
if (ndesc < 1)
return ndesc;

cnt = SSL_read(ssl, buf + len, num - len);
if (cnt <= 0)
Expand Down

0 comments on commit ae28195

Please sign in to comment.