WIP: Fix the Proxy-State issue.
[libradsec.git] / dtls.c
1 /* Copyright (c) 2007-2009, UNINETT AS
2  * Copyright (c) 2012, NORDUnet A/S */
3 /* See LICENSE for licensing information. */
4
5 #include <signal.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <netdb.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <limits.h>
12 #ifdef SYS_SOLARIS9
13 #include <fcntl.h>
14 #endif
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <sys/select.h>
18 #include <ctype.h>
19 #include <sys/wait.h>
20 #include <arpa/inet.h>
21 #include <regex.h>
22 #include <pthread.h>
23 #include <openssl/ssl.h>
24 #include <openssl/err.h>
25 #include "hash.h"
26 #include "radsecproxy.h"
27
28 #ifdef RADPROT_DTLS
29 #include "debug.h"
30 #include "util.h"
31 #include "hostport.h"
32
33 static void setprotoopts(struct commonprotoopts *opts);
34 static char **getlistenerargs();
35 void *udpdtlsserverrd(void *arg);
36 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text);
37 void *dtlsclientrd(void *arg);
38 int clientradputdtls(struct server *server, unsigned char *rad);
39 void addserverextradtls(struct clsrvconf *conf);
40 void dtlssetsrcres();
41 void initextradtls();
42
43 static const struct protodefs protodefs = {
44     "dtls",
45     "radsec", /* secretdefault */
46     SOCK_DGRAM, /* socktype */
47     "2083", /* portdefault */
48     REQUEST_RETRY_COUNT, /* retrycountdefault */
49     10, /* retrycountmax */
50     REQUEST_RETRY_INTERVAL, /* retryintervaldefault */
51     60, /* retryintervalmax */
52     DUPLICATE_INTERVAL, /* duplicateintervaldefault */
53     setprotoopts, /* setprotoopts */
54     getlistenerargs, /* getlistenerargs */
55     udpdtlsserverrd, /* listener */
56     dtlsconnect, /* connecter */
57     dtlsclientrd, /* clientconnreader */
58     clientradputdtls, /* clientradput */
59     NULL, /* addclient */
60     addserverextradtls, /* addserverextra */
61     dtlssetsrcres, /* setsrcres */
62     initextradtls /* initextra */
63 };
64
65 static int client4_sock = -1;
66 static int client6_sock = -1;
67 static struct addrinfo *srcres = NULL;
68 static uint8_t handle;
69 static struct commonprotoopts *protoopts = NULL;
70
71 const struct protodefs *dtlsinit(uint8_t h) {
72     handle = h;
73     return &protodefs;
74 }
75
76 static void setprotoopts(struct commonprotoopts *opts) {
77     protoopts = opts;
78 }
79
80 static char **getlistenerargs() {
81     return protoopts ? protoopts->listenargs : NULL;
82 }
83
84 struct sessioncacheentry {
85     pthread_mutex_t mutex;
86     struct gqueue *rbios;
87     struct timeval expiry;
88 };
89
90 struct dtlsservernewparams {
91     struct sessioncacheentry *sesscache;
92     int sock;
93     struct sockaddr_storage addr;
94 };
95
96 void dtlssetsrcres() {
97     if (!srcres)
98         srcres =
99             resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
100                                    AF_UNSPEC, NULL, protodefs.socktype);
101 }
102
103 int udp2bio(int s, struct gqueue *q, int cnt) {
104     unsigned char *buf;
105     BIO *rbio;
106
107     if (cnt < 1)
108         return 0;
109
110     buf = malloc(cnt);
111     if (!buf) {
112         unsigned char err;
113         debug(DBG_ERR, "udp2bio: malloc failed");
114         recv(s, &err, 1, 0);
115         return 0;
116     }
117
118     cnt = recv(s, buf, cnt, 0);
119     if (cnt < 1) {
120         debug(DBG_WARN, "udp2bio: recv failed");
121         free(buf);
122         return 0;
123     }
124
125     rbio = BIO_new_mem_buf(buf, cnt);
126     BIO_set_mem_eof_return(rbio, -1);
127
128     pthread_mutex_lock(&q->mutex);
129     if (!list_push(q->entries, rbio)) {
130         BIO_free(rbio);
131         pthread_mutex_unlock(&q->mutex);
132         return 0;
133     }
134     pthread_cond_signal(&q->cond);
135     pthread_mutex_unlock(&q->mutex);
136     return 1;
137 }
138
139 BIO *getrbio(SSL *ssl, struct gqueue *q, int timeout) {
140     BIO *rbio;
141     struct timeval now;
142     struct timespec to;
143
144     pthread_mutex_lock(&q->mutex);
145     if (!(rbio = (BIO *)list_shift(q->entries))) {
146         if (timeout) {
147             gettimeofday(&now, NULL);
148             memset(&to, 0, sizeof(struct timespec));
149             to.tv_sec = now.tv_sec + timeout;
150             pthread_cond_timedwait(&q->cond, &q->mutex, &to);
151         } else
152             pthread_cond_wait(&q->cond, &q->mutex);
153         rbio = (BIO *)list_shift(q->entries);
154     }
155     pthread_mutex_unlock(&q->mutex);
156     return rbio;
157 }
158
159 int dtlsread(SSL *ssl, struct gqueue *q, unsigned char *buf, int num, int timeout) {
160     int len, cnt;
161     BIO *rbio;
162
163     for (len = 0; len < num; len += cnt) {
164         cnt = SSL_read(ssl, buf + len, num - len);
165         if (cnt <= 0)
166             switch (cnt = SSL_get_error(ssl, cnt)) {
167             case SSL_ERROR_WANT_READ:
168                 rbio = getrbio(ssl, q, timeout);
169                 if (!rbio)
170                     return 0;
171                 BIO_free(ssl->rbio);
172                 ssl->rbio = rbio;
173                 cnt = 0;
174                 continue;
175             case SSL_ERROR_WANT_WRITE:
176                 cnt = 0;
177                 continue;
178             case SSL_ERROR_ZERO_RETURN:
179                 /* remote end sent close_notify, send one back */
180                 SSL_shutdown(ssl);
181                 return -1;
182             default:
183                 return -1;
184             }
185     }
186     return num;
187 }
188
189 /* accept if acc == 1, else connect */
190 SSL *dtlsacccon(uint8_t acc, SSL_CTX *ctx, int s, struct sockaddr *addr, struct gqueue *rbios) {
191     SSL *ssl;
192     int i, res;
193     unsigned long error;
194     BIO *mem0bio, *wbio;
195
196     ssl = SSL_new(ctx);
197     if (!ssl)
198         return NULL;
199
200     mem0bio = BIO_new(BIO_s_mem());
201     BIO_set_mem_eof_return(mem0bio, -1);
202     wbio = BIO_new_dgram(s, BIO_NOCLOSE);
203     i = BIO_dgram_set_peer(wbio, addr); /* i just to avoid warning */
204     SSL_set_bio(ssl, mem0bio, wbio);
205
206     for (i = 0; i < 5; i++) {
207         res = acc ? SSL_accept(ssl) : SSL_connect(ssl);
208         if (res > 0)
209             return ssl;
210         if (res == 0)
211             break;
212         if (SSL_get_error(ssl, res) == SSL_ERROR_WANT_READ) {
213             BIO_free(ssl->rbio);
214             ssl->rbio = getrbio(ssl, rbios, 5);
215             if (!ssl->rbio)
216                 break;
217         }
218         while ((error = ERR_get_error()))
219             debug(DBG_ERR, "dtls%st: DTLS: %s", acc ? "accep" : "connec", ERR_error_string(error, NULL));
220     }
221
222     SSL_free(ssl);
223     return NULL;
224 }
225
226 unsigned char *raddtlsget(SSL *ssl, struct gqueue *rbios, int timeout) {
227     int cnt, len;
228     unsigned char buf[4], *rad;
229
230     for (;;) {
231         cnt = dtlsread(ssl, rbios, buf, 4, timeout);
232         if (cnt < 1) {
233             debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
234             return NULL;
235         }
236
237         len = RADLEN(buf);
238         rad = malloc(len);
239         if (!rad) {
240             debug(DBG_ERR, "raddtlsget: malloc failed");
241             continue;
242         }
243         memcpy(rad, buf, 4);
244
245         cnt = dtlsread(ssl, rbios, rad + 4, len - 4, timeout);
246         if (cnt < 1) {
247             debug(DBG_DBG, cnt ? "raddtlsget: connection lost" : "raddtlsget: timeout");
248             free(rad);
249             return NULL;
250         }
251
252         if (len >= 20)
253             break;
254
255         free(rad);
256         debug(DBG_WARN, "raddtlsget: packet smaller than minimum radius size");
257     }
258
259     debug(DBG_DBG, "raddtlsget: got %d bytes", len);
260     return rad;
261 }
262
263 void *dtlsserverwr(void *arg) {
264     int cnt;
265     unsigned long error;
266     struct client *client = (struct client *)arg;
267     struct gqueue *replyq;
268     struct request *reply;
269
270     debug(DBG_DBG, "dtlsserverwr: starting for %s", addr2string(client->addr));
271     replyq = client->replyq;
272     for (;;) {
273         pthread_mutex_lock(&replyq->mutex);
274         while (!list_first(replyq->entries)) {
275             if (client->ssl) {
276                 debug(DBG_DBG, "dtlsserverwr: waiting for signal");
277                 pthread_cond_wait(&replyq->cond, &replyq->mutex);
278                 debug(DBG_DBG, "dtlsserverwr: got signal");
279             }
280             if (!client->ssl) {
281                 /* ssl might have changed while waiting */
282                 pthread_mutex_unlock(&replyq->mutex);
283                 debug(DBG_DBG, "dtlsserverwr: exiting as requested");
284                 ERR_remove_state(0);
285                 pthread_exit(NULL);
286             }
287         }
288         reply = (struct request *)list_shift(replyq->entries);
289         pthread_mutex_unlock(&replyq->mutex);
290         cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
291         if (cnt > 0)
292             debug(DBG_DBG, "dtlsserverwr: sent %d bytes, Radius packet of length %d to %s",
293                   cnt, RADLEN(reply->replybuf), addr2string(client->addr));
294         else
295             while ((error = ERR_get_error()))
296                 debug(DBG_ERR, "dtlsserverwr: SSL: %s", ERR_error_string(error, NULL));
297         freerq(reply);
298     }
299 }
300
301 void dtlsserverrd(struct client *client) {
302     struct request *rq;
303     uint8_t *buf;
304     pthread_t dtlsserverwrth;
305
306     debug(DBG_DBG, "dtlsserverrd: starting for %s", addr2string(client->addr));
307
308     if (pthread_create(&dtlsserverwrth, &pthread_attr, dtlsserverwr, (void *)client)) {
309         debug(DBG_ERR, "dtlsserverrd: pthread_create failed");
310         return;
311     }
312
313     for (;;) {
314         buf = raddtlsget(client->ssl, client->rbios, IDLE_TIMEOUT);
315         if (!buf) {
316             debug(DBG_ERR, "dtlsserverrd: connection from %s lost", addr2string(client->addr));
317             break;
318         }
319         debug(DBG_DBG, "dtlsserverrd: got Radius message from %s", addr2string(client->addr));
320         rq = newrequest();
321         if (!rq) {
322             free(buf);
323             continue;
324         }
325         rq->buf = buf;
326         rq->from = client;
327         if (!radsrv(rq)) {
328             debug(DBG_ERR, "dtlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
329             break;
330         }
331     }
332
333     /* stop writer by setting ssl to NULL and give signal in case waiting for data */
334     client->ssl = NULL;
335
336     pthread_mutex_lock(&client->replyq->mutex);
337     pthread_cond_signal(&client->replyq->cond);
338     pthread_mutex_unlock(&client->replyq->mutex);
339     debug(DBG_DBG, "dtlsserverrd: waiting for writer to end");
340     pthread_join(dtlsserverwrth, NULL);
341     debug(DBG_DBG, "dtlsserverrd: reader for %s exiting", addr2string(client->addr));
342 }
343
344 void *dtlsservernew(void *arg) {
345     struct dtlsservernewparams *params = (struct dtlsservernewparams *)arg;
346     struct client *client;
347     struct clsrvconf *conf;
348     struct list_node *cur = NULL;
349     SSL *ssl = NULL;
350     X509 *cert = NULL;
351     SSL_CTX *ctx = NULL;
352     uint8_t delay = 60;
353     struct tls *accepted_tls = NULL;
354
355     debug(DBG_DBG, "dtlsservernew: starting");
356     conf = find_clconf(handle, (struct sockaddr *)&params->addr, NULL);
357     if (conf) {
358         ctx = tlsgetctx(handle, conf->tlsconf);
359         if (!ctx)
360             goto exit;
361         ssl = dtlsacccon(1, ctx, params->sock, (struct sockaddr *)&params->addr, params->sesscache->rbios);
362         if (!ssl)
363             goto exit;
364         cert = verifytlscert(ssl);
365         if (!cert)
366             goto exit;
367         accepted_tls = conf->tlsconf;
368     }
369
370     while (conf) {
371         if (accepted_tls == conf->tlsconf && 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, &pthread_attr, 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     struct hostportres *hp;
534
535     debug(DBG_DBG, "dtlsconnect: called from %s", text);
536     pthread_mutex_lock(&server->lock);
537     if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
538         /* already reconnected, nothing to do */
539         debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
540         pthread_mutex_unlock(&server->lock);
541         return 1;
542     }
543
544     hp = (struct hostportres *)list_first(server->conf->hostports)->data;
545     for (;;) {
546         gettimeofday(&now, NULL);
547         elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
548
549         if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
550             debug(DBG_DBG, "dtlsconnect: timeout");
551             SSL_free(server->ssl);
552             server->ssl = NULL;
553             pthread_mutex_unlock(&server->lock);
554             return 0;
555         }
556
557         if (server->connectionok) {
558             server->connectionok = 0;
559             sleep(2);
560         } else if (elapsed < 1)
561             sleep(2);
562         else if (elapsed < 60) {
563             debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
564             sleep(elapsed);
565         } else if (elapsed < 100000) {
566             debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
567             sleep(60);
568         } else
569             server->lastconnecttry.tv_sec = now.tv_sec;  /* no sleep at startup */
570         debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", hp->host, hp->port);
571
572         SSL_free(server->ssl);
573         server->ssl = NULL;
574         ctx = tlsgetctx(handle, server->conf->tlsconf);
575         if (!ctx)
576             continue;
577         server->ssl = dtlsacccon(0, ctx, server->sock, hp->addrinfo->ai_addr, server->rbios);
578         if (!server->ssl)
579             continue;
580         debug(DBG_DBG, "dtlsconnect: DTLS: ok");
581
582         cert = verifytlscert(server->ssl);
583         if (!cert)
584             continue;
585
586         if (verifyconfcert(cert, server->conf))
587             break;
588         X509_free(cert);
589     }
590     X509_free(cert);
591     debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", hp->host, hp->port);
592     server->connectionok = 1;
593     gettimeofday(&server->lastconnecttry, NULL);
594     pthread_mutex_unlock(&server->lock);
595     return 1;
596 }
597
598 int clientradputdtls(struct server *server, unsigned char *rad) {
599     int cnt;
600     size_t len;
601     unsigned long error;
602     struct clsrvconf *conf = server->conf;
603
604     if (!server->connectionok)
605         return 0;
606     len = RADLEN(rad);
607     if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
608         while ((error = ERR_get_error()))
609             debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
610         return 0;
611     }
612     debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->name);
613     return 1;
614 }
615
616 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
617 void *udpdtlsclientrd(void *arg) {
618     int cnt, s = *(int *)arg;
619     unsigned char buf[4];
620     struct sockaddr_storage from;
621     socklen_t fromlen = sizeof(from);
622     struct clsrvconf *conf;
623     fd_set readfds;
624
625     for (;;) {
626         FD_ZERO(&readfds);
627         FD_SET(s, &readfds);
628         if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
629             continue;
630         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
631         if (cnt == -1) {
632             debug(DBG_WARN, "udpdtlsclientrd: recv failed");
633             continue;
634         }
635
636         conf = find_srvconf(handle, (struct sockaddr *)&from, NULL);
637         if (!conf) {
638             debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from));
639             recv(s, buf, 4, 0);
640             continue;
641         }
642         if (udp2bio(s, conf->servers->rbios, cnt))
643             debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
644     }
645 }
646
647 void *dtlsclientrd(void *arg) {
648     struct server *server = (struct server *)arg;
649     unsigned char *buf;
650     struct timeval lastconnecttry;
651     int secs;
652
653     for (;;) {
654         /* yes, lastconnecttry is really necessary */
655         lastconnecttry = server->lastconnecttry;
656         for (secs = 0; !(buf = raddtlsget(server->ssl, server->rbios, 10)) && !server->lostrqs && secs < IDLE_TIMEOUT; secs += 10);
657         if (!buf) {
658             dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
659             continue;
660         }
661         replyh(server, buf);
662     }
663     ERR_remove_state(0);
664     server->clientrdgone = 1;
665     return NULL;
666 }
667
668 void addserverextradtls(struct clsrvconf *conf) {
669     switch (((struct hostportres *)list_first(conf->hostports)->data)->addrinfo->ai_family) {
670     case AF_INET:
671         if (client4_sock < 0) {
672             client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
673             if (client4_sock < 0)
674                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
675         }
676         conf->servers->sock = client4_sock;
677         break;
678     case AF_INET6:
679         if (client6_sock < 0) {
680             client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
681             if (client6_sock < 0)
682                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
683         }
684         conf->servers->sock = client6_sock;
685         break;
686     default:
687         debugx(1, DBG_ERR, "addserver: unsupported address family");
688     }
689 }
690
691 void initextradtls() {
692     pthread_t cl4th, cl6th;
693
694     if (srcres) {
695         freeaddrinfo(srcres);
696         srcres = NULL;
697     }
698
699     if (client4_sock >= 0)
700         if (pthread_create(&cl4th, &pthread_attr, udpdtlsclientrd, (void *)&client4_sock))
701             debugx(1, DBG_ERR, "pthread_create failed");
702     if (client6_sock >= 0)
703         if (pthread_create(&cl6th, &pthread_attr, udpdtlsclientrd, (void *)&client6_sock))
704             debugx(1, DBG_ERR, "pthread_create failed");
705 }
706 #else
707 const struct protodefs *dtlsinit(uint8_t h) {
708     return NULL;
709 }
710 #endif
711
712 /* Local Variables: */
713 /* c-file-style: "stroustrup" */
714 /* End: */