s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
tls_timeoutio.c
Go to the documentation of this file.
1/* This is essentially taken from Eric Vermeulen's TLS patch */
2#include "select.h"
3#include "error.h"
4#include "ndelay.h"
5#include "now.h"
6#include "logmsg.h"
7#include "ucspissl.h"
8#include "tls_timeoutio.h"
9
10int tls_timeoutio(int (*fun)(),
11 int t, int rfd, int wfd, SSL *ssl, char *buf, int len)
12{
13 int n;
14 const datetime_sec end = (datetime_sec)t + now();
15
16 do {
17 fd_set fds;
18 struct timeval tv;
19
20 const int r = buf ? fun(ssl,buf,len) : fun(ssl);
21 if (r > 0) return r;
22
23 t = end - now();
24 if (t < 0) break;
25 tv.tv_sec = (time_t)t; tv.tv_usec = 0;
26
27 FD_ZERO(&fds);
28 switch (SSL_get_error(ssl,r)) {
29 default: return r; /* some other error */
30 case SSL_ERROR_WANT_READ:
31 FD_SET(rfd,&fds); n = select(rfd + 1,&fds,NULL,NULL,&tv);
32 break;
33 case SSL_ERROR_WANT_WRITE:
34 FD_SET(wfd,&fds); n = select(wfd + 1,NULL,&fds,NULL,&tv);
35 break;
36 }
37
38 /* n is the number of descriptors that changed status */
39 } while (n > 0);
40
41 if (n != -1) errno = ETIMEDOUT;
42 return -1;
43}
44
45int tls_timeoutaccept(int t,int rfd,int wfd,SSL *ssl)
46{
47 int r;
48
49 /* if connection is established, keep NDELAY */
50 if (ndelay_on(rfd) == -1 || ndelay_on(wfd) == -1) return -1;
51 r = tls_timeoutio(SSL_accept,t,rfd,wfd,ssl,NULL,0);
52
53 if (r <= 0) { ndelay_off(rfd); ndelay_off(wfd); }
54 else SSL_set_mode(ssl,SSL_MODE_ENABLE_PARTIAL_WRITE);
55
56 return r;
57}
58
59int tls_timeoutconn(int t,int rfd,int wfd,SSL *ssl)
60{
61 int r;
62
63 /* if connection is established, keep NDELAY */
64 if (ndelay_on(rfd) == -1 || ndelay_on(wfd) == -1) return -1;
65 r = tls_timeoutio(SSL_connect,t,rfd,wfd,ssl,NULL,0);
66
67 if (r <= 0) { ndelay_off(rfd); ndelay_off(wfd); }
68 else SSL_set_mode(ssl,SSL_MODE_ENABLE_PARTIAL_WRITE);
69
70 return r;
71}
72
73int tls_timeoutrehandshake(int t,int rfd,int wfd,SSL *ssl)
74{
75 int r;
76
77 SSL_renegotiate(ssl);
78 r = tls_timeoutio(SSL_do_handshake,t,rfd,wfd,ssl,NULL,0);
79 if (r <= 0) return r;
80 if (SSL_get_state(ssl) & SSL_ST_CONNECT) return -2; /* now a macro in ssl.h */
81
82 /* this is for the client only */
83 SSL_set_connect_state(ssl);
84
85 return tls_timeoutio(SSL_do_handshake,t,rfd,wfd,ssl,NULL,0);
86}
87
88int tls_timeoutread(int t,int rfd,int wfd,SSL *ssl,char *buf,int len)
89{
90 if (!buf) return 0;
91 if (SSL_pending(ssl)) return SSL_read(ssl,buf,len);
92 return tls_timeoutio(SSL_read,t,rfd,wfd,ssl,buf,len);
93}
94
95int tls_timeoutwrite(int t,int rfd,int wfd,SSL *ssl,char *buf,int len)
96{
97 if (!buf) return 0;
98 return tls_timeoutio(SSL_write,t,rfd,wfd,ssl,buf,len);
99}
fd_set fds
Definition: chkspawn.c:9
long datetime_sec
Definition: datetime.h:15
char buf[100+FMT_ULONG]
Definition: hier.c:10
datetime_sec now()
Definition: now.c:5
SSL * ssl
Definition: qmail-remote.c:105
int tls_timeoutconn(int t, int rfd, int wfd, SSL *ssl)
Definition: tls_timeoutio.c:59
int tls_timeoutwrite(int t, int rfd, int wfd, SSL *ssl, char *buf, int len)
Definition: tls_timeoutio.c:95
int tls_timeoutaccept(int t, int rfd, int wfd, SSL *ssl)
Definition: tls_timeoutio.c:45
int tls_timeoutrehandshake(int t, int rfd, int wfd, SSL *ssl)
Definition: tls_timeoutio.c:73
int tls_timeoutread(int t, int rfd, int wfd, SSL *ssl, char *buf, int len)
Definition: tls_timeoutio.c:88
int tls_timeoutio(int(*fun)(), int t, int rfd, int wfd, SSL *ssl, char *buf, int len)
Definition: tls_timeoutio.c:10