diff options
Diffstat (limited to 'ssl_timeoutio.c')
| -rw-r--r-- | ssl_timeoutio.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/ssl_timeoutio.c b/ssl_timeoutio.c new file mode 100644 index 0000000..5b2dc9d --- /dev/null +++ b/ssl_timeoutio.c | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | #include "select.h" | ||
| 2 | #include "error.h" | ||
| 3 | #include "ndelay.h" | ||
| 4 | #include "now.h" | ||
| 5 | #include "ssl_timeoutio.h" | ||
| 6 | |||
| 7 | int ssl_timeoutio(int (*fun)(), | ||
| 8 | int t, int rfd, int wfd, SSL *ssl, char *buf, int len) | ||
| 9 | { | ||
| 10 | int n; | ||
| 11 | const datetime_sec end = (datetime_sec)t + now(); | ||
| 12 | |||
| 13 | do { | ||
| 14 | fd_set fds; | ||
| 15 | struct timeval tv; | ||
| 16 | |||
| 17 | const int r = buf ? fun(ssl, buf, len) : fun(ssl); | ||
| 18 | if (r > 0) return r; | ||
| 19 | |||
| 20 | t = end - now(); | ||
| 21 | if (t < 0) break; | ||
| 22 | tv.tv_sec = (time_t)t; tv.tv_usec = 0; | ||
| 23 | |||
| 24 | FD_ZERO(&fds); | ||
| 25 | switch (SSL_get_error(ssl, r)) | ||
| 26 | { | ||
| 27 | default: return r; /* some other error */ | ||
| 28 | case SSL_ERROR_WANT_READ: | ||
| 29 | FD_SET(rfd, &fds); n = select(rfd + 1, &fds, NULL, NULL, &tv); | ||
| 30 | break; | ||
| 31 | case SSL_ERROR_WANT_WRITE: | ||
| 32 | FD_SET(wfd, &fds); n = select(wfd + 1, NULL, &fds, NULL, &tv); | ||
| 33 | break; | ||
| 34 | } | ||
| 35 | |||
| 36 | /* n is the number of descriptors that changed status */ | ||
| 37 | } while (n > 0); | ||
| 38 | |||
| 39 | if (n != -1) errno = error_timeout; | ||
| 40 | return -1; | ||
| 41 | } | ||
| 42 | |||
| 43 | int ssl_timeoutaccept(int t, int rfd, int wfd, SSL *ssl) | ||
| 44 | { | ||
| 45 | int r; | ||
| 46 | |||
| 47 | /* if connection is established, keep NDELAY */ | ||
| 48 | if (ndelay_on(rfd) == -1 || ndelay_on(wfd) == -1) return -1; | ||
| 49 | r = ssl_timeoutio(SSL_accept, t, rfd, wfd, ssl, NULL, 0); | ||
| 50 | |||
| 51 | if (r <= 0) { ndelay_off(rfd); ndelay_off(wfd); } | ||
| 52 | else SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE); | ||
| 53 | |||
| 54 | return r; | ||
| 55 | } | ||
| 56 | |||
| 57 | int ssl_timeoutconn(int t, int rfd, int wfd, SSL *ssl) | ||
| 58 | { | ||
| 59 | int r; | ||
| 60 | |||
| 61 | /* if connection is established, keep NDELAY */ | ||
| 62 | if (ndelay_on(rfd) == -1 || ndelay_on(wfd) == -1) return -1; | ||
| 63 | r = ssl_timeoutio(SSL_connect, t, rfd, wfd, ssl, NULL, 0); | ||
| 64 | |||
| 65 | if (r <= 0) { ndelay_off(rfd); ndelay_off(wfd); } | ||
| 66 | else SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE); | ||
| 67 | |||
| 68 | return r; | ||
| 69 | } | ||
| 70 | |||
| 71 | int ssl_timeoutrehandshake(int t, int rfd, int wfd, SSL *ssl) | ||
| 72 | { | ||
| 73 | int r; | ||
| 74 | |||
| 75 | SSL_renegotiate(ssl); | ||
| 76 | r = ssl_timeoutio(SSL_do_handshake, t, rfd, wfd, ssl, NULL, 0); | ||
| 77 | if (r <= 0 || ssl->type == SSL_ST_CONNECT) return r; | ||
| 78 | |||
| 79 | /* this is for the server only */ | ||
| 80 | ssl->state = SSL_ST_ACCEPT; | ||
| 81 | return ssl_timeoutio(SSL_do_handshake, t, rfd, wfd, ssl, NULL, 0); | ||
| 82 | } | ||
| 83 | |||
| 84 | int ssl_timeoutread(int t, int rfd, int wfd, SSL *ssl, char *buf, int len) | ||
| 85 | { | ||
| 86 | if (!buf) return 0; | ||
| 87 | if (SSL_pending(ssl)) return SSL_read(ssl, buf, len); | ||
| 88 | return ssl_timeoutio(SSL_read, t, rfd, wfd, ssl, buf, len); | ||
| 89 | } | ||
| 90 | |||
| 91 | int ssl_timeoutwrite(int t, int rfd, int wfd, SSL *ssl, char *buf, int len) | ||
| 92 | { | ||
| 93 | if (!buf) return 0; | ||
| 94 | return ssl_timeoutio(SSL_write, t, rfd, wfd, ssl, buf, len); | ||
| 95 | } | ||
