radsecproxy-1.6.
[radsecproxy.git] / dtls.c
1 /*
2  * Copyright (C) 2008-2009 Stig Venaas <venaas@uninett.no>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  */
8
9 #include <signal.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netdb.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <limits.h>
16 #ifdef SYS_SOLARIS9
17 #include <fcntl.h>
18 #endif
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/select.h>
22 #include <ctype.h>
23 #include <sys/wait.h>
24 #include <arpa/inet.h>
25 #include <regex.h>
26 #include <pthread.h>
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
29 #include "hash.h"
30 #include "radsecproxy.h"
31
32 #ifdef RADPROT_DTLS
33 #include "debug.h"
34 #include "util.h"
35 #include "hostport.h"
36
37 static void setprotoopts(struct commonprotoopts *opts);
38 static char **getlistenerargs();
39 void *udpdtlsserverrd(void *arg);
40 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text);
41 void *dtlsclientrd(void *arg);
42 int clientradputdtls(struct server *server, unsigned char *rad);
43 void addserverextradtls(struct clsrvconf *conf);
44 void dtlssetsrcres();
45 void initextradtls();
46
47 static const struct protodefs protodefs = {
48     "dtls",
49     "radsec", /* secretdefault */
50     SOCK_DGRAM, /* socktype */
51     "2083", /* portdefault */
52     REQUEST_RETRY_COUNT, /* retrycountdefault */
53     10, /* retrycountmax */
54     REQUEST_RETRY_INTERVAL, /* retryintervaldefault */
55     60, /* retryintervalmax */
56     DUPLICATE_INTERVAL, /* duplicateintervaldefault */
57     setprotoopts, /* setprotoopts */
58     getlistenerargs, /* getlistenerargs */
59     udpdtlsserverrd, /* listener */
60     dtlsconnect, /* connecter */
61     dtlsclientrd, /* clientconnreader */
62     clientradputdtls, /* clientradput */
63     NULL, /* addclient */
64     addserverextradtls, /* addserverextra */
65     dtlssetsrcres, /* setsrcres */
66     initextradtls /* initextra */
67 };
68
69 static int client4_sock = -1;
70 static int client6_sock = -1;
71 static struct addrinfo *srcres = NULL;
72 static uint8_t handle;
73 static struct commonprotoopts *protoopts = NULL;
74
75 const struct protodefs *dtlsinit(uint8_t h) {
76     handle = h;
77     return &protodefs;
78 }
79
80 static void setprotoopts(struct commonprotoopts *opts) {
81     protoopts = opts;
82 }
83
84 static char **getlistenerargs() {
85     return protoopts ? protoopts->listenargs : NULL;
86 }
87
88 struct sessioncacheentry {
89     pthread_mutex_t mutex;
90     struct gqueue *rbios;
91     struct timeval expiry;
92 };
93
94 struct dtlsservernewparams {
95     struct sessioncacheentry *sesscache;
96     int sock;
97     struct sockaddr_storage addr;
98 };
99
100 void dtlssetsrcres() {
101     if (!srcres)
102         srcres =
103             resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
104                                    AF_UNSPEC, NULL, protodefs.socktype);
105 }
106
107 int udp2bio(int s, struct gqueue *q, int cnt) {
108     unsigned char *buf;
109     BIO *rbio;
110
111     if (cnt < 1)
112         return 0;
113
114     buf = malloc(cnt);
115     if (!buf) {
116         unsigned char err;
117         debug(DBG_ERR, "udp2bio: malloc failed");
118         recv(s, &err, 1, 0);
119         return 0;
120     }
121
122     cnt = recv(s, buf, cnt, 0);
123     if (cnt < 1) {
124         debug(DBG_WARN, "udp2bio: recv failed");
125         free(buf);
126         return 0;
127     }
128
129     rbio = BIO_new_mem_buf(buf, cnt);
130     BIO_set_mem_eof_return(rbio, -1);
131
132     pthread_mutex_lock(&q->mutex);
133     if (!list_push(q->entries, rbio)) {
134         BIO_free(rbio);
135         pthread_mutex_unlock(&q->mutex);
136         return 0;
137     }
138     pthread_cond_signal(&q->cond);
139     pthread_mutex_unlock(&q->mutex);
140     return 1;
141 }
142
143 BIO *getrbio(SSL *ssl, struct gqueue *q, int timeout) {
144     BIO *rbio;
145     struct timeval now;
146     struct timespec to;
147
148     pthread_mutex_lock(&q->mutex);
149     if (!(rbio = (BIO *)list_shift(q->entries))) {
150         if (timeout) {
151             gettimeofday(&now, NULL);
152             memset(&to, 0, sizeof(struct timespec));
153             to.tv_sec = now.tv_sec + timeout;
154             pthread_cond_timedwait(&q->cond, &q->mutex, &to);
155         } else
156             pthread_cond_wait(&q->cond, &q->mutex);
157         rbio = (BIO *)list_shift(q->entries);
158     }
159     pthread_mutex_unlock(&q->mutex);
160     return rbio;
161 }
162
163 int dtlsread(SSL *ssl, struct gqueue *q, unsigned char *buf, int num, int timeout) {
164     int len, cnt;
165     BIO *rbio;
166
167     for (len = 0; len < num; len += cnt) {
168         cnt = SSL_read(ssl, buf + len, num - len);
169         if (cnt <= 0)
170             switch (cnt = SSL_get_error(ssl, cnt)) {
171             case SSL_ERROR_WANT_READ:
172                 rbio = getrbio(ssl, q, timeout);
173                 if (!rbio)
174                     return 0;
175                 BIO_free(ssl->rbio);
176                 ssl->rbio = rbio;
177                 cnt = 0;
178                 continue;
179             case SSL_ERROR_WANT_WRITE:
180                 cnt = 0;
181                 continue;
182             case SSL_ERROR_ZERO_RETURN:
183                 /* remote end sent close_notify, send one back */
184                 SSL_shutdown(ssl);
185                 return -1;
186             default:
187                 return -1;
188             }
189     }
190     return num;
191 }
192
193 /* accept if acc == 1, else connect */
194 SSL *dtlsacccon(uint8_t acc, SSL_CTX *ctx, int s, struct sockaddr *addr, struct gqueue *rbios) {
195     SSL *ssl;
196     int i, res;
197     unsigned long error;
198     BIO *mem0bio, *wbio;
199
200     ssl = SSL_new(ctx);
201     if (!ssl)
202         return NULL;
203
204     mem0bio = BIO_new(BIO_s_mem());
205     BIO_set_mem_eof_return(mem0bio, -1);
206     wbio = BIO_new_dgram(s, BIO_NOCLOSE);
207     i = BIO_dgram_set_peer(wbio, addr); /* i just to avoid warning */
208     SSL_set_bio(ssl, mem0bio, wbio);
209
210     for (i = 0; i < 5; i++) {
211         res = acc ? SSL_accept(ssl) : SSL_connect(ssl);
212         if (res > 0)
213             return ssl;
214         if (res == 0)
215             break;
216         if (SSL_get_error(ssl, res) == SSL_ERROR_WANT_READ) {
217             BIO_free(ssl->rbio);
218             ssl->rbio = getrbio(ssl, rbios, 5);
219             if (!ssl->rbio)
220                 break;
221         }
222         while ((error = ERR_get_error()))
223             debug(DBG_ERR, "dtls%st: DTLS: %s", acc ? "accep" : "connec", ERR_error_string(error, NULL));
224     }
225
226     SSL_free(ssl);
227     return NULL;
228 }
229
230 unsigned char *raddtlsget(SSL *ssl, struct gqueue *rbios, int timeout) {
231     int cnt, len;
232     unsigned char buf[4], *rad;
233
234     for (;;) {
235         cnt = dtlsread(ssl, rbios, buf, 4, timeout);
236         if (cnt < 1) {
237             debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
238             return NULL;
239         }
240
241         len = RADLEN(buf);
242         rad = malloc(len);
243         if (!rad) {
244             debug(DBG_ERR, "raddtlsget: malloc failed");
245             continue;
246         }
247         memcpy(rad, buf, 4);
248
249         cnt = dtlsread(ssl, rbios, rad + 4, len - 4, timeout);
250         if (cnt < 1) {
251             debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
252             free(rad);
253             return NULL;
254         }
255
256         if (len >= 20)
257             break;
258
259         free(rad);
260         debug(DBG_WARN, "raddtlsget: packet smaller than minimum radius size");
261     }
262
263     debug(DBG_DBG, "raddtlsget: got %d bytes", len);
264     return rad;
265 }
266
267 void *dtlsserverwr(void *arg) {
268     int cnt;
269     unsigned long error;
270     struct client *client = (struct client *)arg;
271     struct gqueue *replyq;
272     struct request *reply;
273
274     debug(DBG_DBG, "dtlsserverwr: starting for %s", addr2string(client->addr));
275     replyq = client->replyq;
276     for (;;) {
277         pthread_mutex_lock(&replyq->mutex);
278         while (!list_first(replyq->entries)) {
279             if (client->ssl) {
280                 debug(DBG_DBG, "dtlsserverwr: waiting for signal");
281                 pthread_cond_wait(&replyq->cond, &replyq->mutex);
282                 debug(DBG_DBG, "dtlsserverwr: got signal");
283             }
284             if (!client->ssl) {
285                 /* ssl might have changed while waiting */
286                 pthread_mutex_unlock(&replyq->mutex);
287                 debug(DBG_DBG, "dtlsserverwr: exiting as requested");
288                 ERR_remove_state(0);
289                 pthread_exit(NULL);
290             }
291         }
292         reply = (struct request *)list_shift(replyq->entries);
293         pthread_mutex_unlock(&replyq->mutex);
294         cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
295         if (cnt > 0)
296             debug(DBG_DBG, "dtlsserverwr: sent %d bytes, Radius packet of length %d to %s",
297                   cnt, RADLEN(reply->replybuf), addr2string(client->addr));
298         else
299             while ((error = ERR_get_error()))
300                 debug(DBG_ERR, "dtlsserverwr: SSL: %s", ERR_error_string(error, NULL));
301         freerq(reply);
302     }
303 }
304
305 void dtlsserverrd(struct client *client) {
306     struct request *rq;
307     uint8_t *buf;
308     pthread_t dtlsserverwrth;
309
310     debug(DBG_DBG, "dtlsserverrd: starting for %s", addr2string(client->addr));
311
312     if (pthread_create(&dtlsserverwrth, NULL, dtlsserverwr, (void *)client)) {
313         debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
314         return;
315     }
316
317     for (;;) {
318         buf = raddtlsget(client->ssl, client->rbios, IDLE_TIMEOUT);
319         if (!buf) {
320             debug(DBG_ERR, "dtlsserverrd: connection from %s lost", addr2string(client->addr));
321             break;
322         }
323         debug(DBG_DBG, "dtlsserverrd: got Radius message from %s", addr2string(client->addr));
324         rq = newrequest();
325         if (!rq) {
326             free(buf);
327             continue;
328         }
329         rq->buf = buf;
330         rq->from = client;
331         if (!radsrv(rq)) {
332             debug(DBG_ERR, "dtlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
333             break;
334         }
335     }
336
337     /* stop writer by setting ssl to NULL and give signal in case waiting for data */
338     client->ssl = NULL;
339
340     pthread_mutex_lock(&client->replyq->mutex);
341     pthread_cond_signal(&client->replyq->cond);
342     pthread_mutex_unlock(&client->replyq->mutex);
343     debug(DBG_DBG, "dtlsserverrd: waiting for writer to end");
344     pthread_join(dtlsserverwrth, NULL);
345     debug(DBG_DBG, "dtlsserverrd: reader for %s exiting", addr2string(client->addr));
346 }
347
348 void *dtlsservernew(void *arg) {
349     struct dtlsservernewparams *params = (struct dtlsservernewparams *)arg;
350     struct client *client;
351     struct clsrvconf *conf;
352     struct list_node *cur = NULL;
353     SSL *ssl = NULL;
354     X509 *cert = NULL;
355     SSL_CTX *ctx = NULL;
356     uint8_t delay = 60;
357
358     debug(DBG_DBG, "dtlsservernew: starting");
359     conf = find_clconf(handle, (struct sockaddr *)&params->addr, NULL);
360     if (conf) {
361         ctx = tlsgetctx(handle, conf->tlsconf);
362         if (!ctx)
363             goto exit;
364         ssl = dtlsacccon(1, ctx, params->sock, (struct sockaddr *)&params->addr, params->sesscache->rbios);
365         if (!ssl)
366             goto exit;
367         cert = verifytlscert(ssl);
368         if (!cert)
369             goto exit;
370     }
371
372     while (conf) {
373         if (verifyconfcert(cert, conf)) {
374             X509_free(cert);
375             client = addclient(conf, 1);
376             if (client) {
377                 client->sock = params->sock;
378                 client->addr = addr_copy((struct sockaddr *)&params->addr);
379                 client->rbios = params->sesscache->rbios;
380                 client->ssl = ssl;
381                 dtlsserverrd(client);
382                 removeclient(client);
383                 delay = 0;
384             } else {
385                 debug(DBG_WARN, "dtlsservernew: failed to create new client instance");
386             }
387             goto exit;
388         }
389         conf = find_clconf(handle, (struct sockaddr *)&params->addr, &cur);
390     }
391     debug(DBG_WARN, "dtlsservernew: ignoring request, no matching TLS client");
392
393     if (cert)
394         X509_free(cert);
395
396 exit:
397     if (ssl) {
398         SSL_shutdown(ssl);
399         SSL_free(ssl);
400     }
401     pthread_mutex_lock(&params->sesscache->mutex);
402     freebios(params->sesscache->rbios);
403     params->sesscache->rbios = NULL;
404     gettimeofday(&params->sesscache->expiry, NULL);
405     params->sesscache->expiry.tv_sec += delay;
406     pthread_mutex_unlock(&params->sesscache->mutex);
407     free(params);
408     ERR_remove_state(0);
409     pthread_exit(NULL);
410     debug(DBG_DBG, "dtlsservernew: exiting");
411 }
412
413 void cacheexpire(struct hash *cache, struct timeval *last) {
414     struct timeval now;
415     struct hash_entry *he;
416     struct sessioncacheentry *e;
417
418     gettimeofday(&now, NULL);
419     if (now.tv_sec - last->tv_sec < 19)
420         return;
421
422     for (he = hash_first(cache); he; he = hash_next(he)) {
423         e = (struct sessioncacheentry *)he->data;
424         pthread_mutex_lock(&e->mutex);
425         if (!e->expiry.tv_sec || e->expiry.tv_sec > now.tv_sec) {
426             pthread_mutex_unlock(&e->mutex);
427             continue;
428         }
429         debug(DBG_DBG, "cacheexpire: freeing entry");
430         hash_extract(cache, he->key, he->keylen);
431         if (e->rbios) {
432             freebios(e->rbios);
433             e->rbios = NULL;
434         }
435         pthread_mutex_unlock(&e->mutex);
436         pthread_mutex_destroy(&e->mutex);
437     }
438     last->tv_sec = now.tv_sec;
439 }
440
441 void *udpdtlsserverrd(void *arg) {
442     int ndesc, cnt, s = *(int *)arg;
443     unsigned char buf[4];
444     struct sockaddr_storage from;
445     socklen_t fromlen = sizeof(from);
446     struct dtlsservernewparams *params;
447     fd_set readfds;
448     struct timeval timeout, lastexpiry;
449     pthread_t dtlsserverth;
450     struct hash *sessioncache;
451     struct sessioncacheentry *cacheentry;
452
453     sessioncache = hash_create();
454     if (!sessioncache)
455         debugx(1, DBG_ERR, "udpdtlsserverrd: malloc failed");
456     gettimeofday(&lastexpiry, NULL);
457
458     for (;;) {
459         FD_ZERO(&readfds);
460         FD_SET(s, &readfds);
461         memset(&timeout, 0, sizeof(struct timeval));
462         timeout.tv_sec = 60;
463         ndesc = select(s + 1, &readfds, NULL, NULL, &timeout);
464         if (ndesc < 1) {
465             cacheexpire(sessioncache, &lastexpiry);
466             continue;
467         }
468         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
469         if (cnt == -1) {
470             debug(DBG_WARN, "udpdtlsserverrd: recv failed");
471             cacheexpire(sessioncache, &lastexpiry);
472             continue;
473         }
474         cacheentry = hash_read(sessioncache, &from, fromlen);
475         if (cacheentry) {
476             debug(DBG_DBG, "udpdtlsserverrd: cache hit");
477             pthread_mutex_lock(&cacheentry->mutex);
478             if (cacheentry->rbios) {
479                 if (udp2bio(s, cacheentry->rbios, cnt))
480                     debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
481             } else
482                 recv(s, buf, 1, 0);
483             pthread_mutex_unlock(&cacheentry->mutex);
484             cacheexpire(sessioncache, &lastexpiry);
485             continue;
486         }
487
488         /* from new source */
489         debug(DBG_DBG, "udpdtlsserverrd: cache miss");
490         params = malloc(sizeof(struct dtlsservernewparams));
491         if (!params) {
492             cacheexpire(sessioncache, &lastexpiry);
493             recv(s, buf, 1, 0);
494             continue;
495         }
496         memset(params, 0, sizeof(struct dtlsservernewparams));
497         params->sesscache = malloc(sizeof(struct sessioncacheentry));
498         if (!params->sesscache) {
499             free(params);
500             cacheexpire(sessioncache, &lastexpiry);
501             recv(s, buf, 1, 0);
502             continue;
503         }
504         memset(params->sesscache, 0, sizeof(struct sessioncacheentry));
505         pthread_mutex_init(&params->sesscache->mutex, NULL);
506         params->sesscache->rbios = newqueue();
507         if (hash_insert(sessioncache, &from, fromlen, params->sesscache)) {
508             params->sock = s;
509             memcpy(&params->addr, &from, fromlen);
510
511             if (udp2bio(s, params->sesscache->rbios, cnt)) {
512                 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
513                 if (!pthread_create(&dtlsserverth, NULL, dtlsservernew, (void *)params)) {
514                     pthread_detach(dtlsserverth);
515                     cacheexpire(sessioncache, &lastexpiry);
516                     continue;
517                 }
518                 debug(DBG_ERR, "udpdtlsserverrd: pthread_create failed");
519             }
520             hash_extract(sessioncache, &from, fromlen);
521         }
522         freebios(params->sesscache->rbios);
523         pthread_mutex_destroy(&params->sesscache->mutex);
524         free(params->sesscache);
525         free(params);
526         cacheexpire(sessioncache, &lastexpiry);
527     }
528 }
529
530 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
531     struct timeval now;
532     time_t elapsed;
533     X509 *cert;
534     SSL_CTX *ctx = NULL;
535     struct hostportres *hp;
536
537     debug(DBG_DBG, "dtlsconnect: called from %s", text);
538     pthread_mutex_lock(&server->lock);
539     if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
540         /* already reconnected, nothing to do */
541         debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
542         pthread_mutex_unlock(&server->lock);
543         return 1;
544     }
545
546     hp = (struct hostportres *)list_first(server->conf->hostports)->data;
547     for (;;) {
548         gettimeofday(&now, NULL);
549         elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
550
551         if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
552             debug(DBG_DBG, "dtlsconnect: timeout");
553             SSL_free(server->ssl);
554             server->ssl = NULL;
555             pthread_mutex_unlock(&server->lock);
556             return 0;
557         }
558
559         if (server->connectionok) {
560             server->connectionok = 0;
561             sleep(2);
562         } else if (elapsed < 1)
563             sleep(2);
564         else if (elapsed < 60) {
565             debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
566             sleep(elapsed);
567         } else if (elapsed < 100000) {
568             debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
569             sleep(60);
570         } else
571             server->lastconnecttry.tv_sec = now.tv_sec;  /* no sleep at startup */
572         debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", hp->host, hp->port);
573
574         SSL_free(server->ssl);
575         server->ssl = NULL;
576         ctx = tlsgetctx(handle, server->conf->tlsconf);
577         if (!ctx)
578             continue;
579         server->ssl = dtlsacccon(0, ctx, server->sock, hp->addrinfo->ai_addr, server->rbios);
580         if (!server->ssl)
581             continue;
582         debug(DBG_DBG, "dtlsconnect: DTLS: ok");
583
584         cert = verifytlscert(server->ssl);
585         if (!cert)
586             continue;
587
588         if (verifyconfcert(cert, server->conf))
589             break;
590         X509_free(cert);
591     }
592     X509_free(cert);
593     debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", hp->host, hp->port);
594     server->connectionok = 1;
595     gettimeofday(&server->lastconnecttry, NULL);
596     pthread_mutex_unlock(&server->lock);
597     return 1;
598 }
599
600 int clientradputdtls(struct server *server, unsigned char *rad) {
601     int cnt;
602     size_t len;
603     unsigned long error;
604     struct clsrvconf *conf = server->conf;
605
606     if (!server->connectionok)
607         return 0;
608     len = RADLEN(rad);
609     if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
610         while ((error = ERR_get_error()))
611             debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
612         return 0;
613     }
614     debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->name);
615     return 1;
616 }
617
618 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
619 void *udpdtlsclientrd(void *arg) {
620     int cnt, s = *(int *)arg;
621     unsigned char buf[4];
622     struct sockaddr_storage from;
623     socklen_t fromlen = sizeof(from);
624     struct clsrvconf *conf;
625     fd_set readfds;
626
627     for (;;) {
628         FD_ZERO(&readfds);
629         FD_SET(s, &readfds);
630         if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
631             continue;
632         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
633         if (cnt == -1) {
634             debug(DBG_WARN, "udpdtlsclientrd: recv failed");
635             continue;
636         }
637
638         conf = find_srvconf(handle, (struct sockaddr *)&from, NULL);
639         if (!conf) {
640             debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from));
641             recv(s, buf, 4, 0);
642             continue;
643         }
644         if (udp2bio(s, conf->servers->rbios, cnt))
645             debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
646     }
647 }
648
649 void *dtlsclientrd(void *arg) {
650     struct server *server = (struct server *)arg;
651     unsigned char *buf;
652     struct timeval lastconnecttry;
653     int secs;
654
655     for (;;) {
656         /* yes, lastconnecttry is really necessary */
657         lastconnecttry = server->lastconnecttry;
658         for (secs = 0; !(buf = raddtlsget(server->ssl, server->rbios, 10)) && !server->lostrqs && secs < IDLE_TIMEOUT; secs += 10);
659         if (!buf) {
660             dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
661             continue;
662         }
663         replyh(server, buf);
664     }
665     ERR_remove_state(0);
666     server->clientrdgone = 1;
667     return NULL;
668 }
669
670 void addserverextradtls(struct clsrvconf *conf) {
671     switch (((struct hostportres *)list_first(conf->hostports)->data)->addrinfo->ai_family) {
672     case AF_INET:
673         if (client4_sock < 0) {
674             client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
675             if (client4_sock < 0)
676                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
677         }
678         conf->servers->sock = client4_sock;
679         break;
680     case AF_INET6:
681         if (client6_sock < 0) {
682             client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
683             if (client6_sock < 0)
684                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
685         }
686         conf->servers->sock = client6_sock;
687         break;
688     default:
689         debugx(1, DBG_ERR, "addserver: unsupported address family");
690     }
691 }
692
693 void initextradtls() {
694     pthread_t cl4th, cl6th;
695
696     if (srcres) {
697         freeaddrinfo(srcres);
698         srcres = NULL;
699     }
700
701     if (client4_sock >= 0)
702         if (pthread_create(&cl4th, NULL, udpdtlsclientrd, (void *)&client4_sock))
703             debugx(1, DBG_ERR, "pthread_create failed");
704     if (client6_sock >= 0)
705         if (pthread_create(&cl6th, NULL, udpdtlsclientrd, (void *)&client6_sock))
706             debugx(1, DBG_ERR, "pthread_create failed");
707 }
708 #else
709 const struct protodefs *dtlsinit(uint8_t h) {
710     return NULL;
711 }
712 #endif
713
714 /* Local Variables: */
715 /* c-file-style: "stroustrup" */
716 /* End: */