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