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