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