radsecproxy-1.6.5.
[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, &pthread_attr, 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     struct tls *accepted_tls = NULL;
358
359     debug(DBG_DBG, "dtlsservernew: starting");
360     conf = find_clconf(handle, (struct sockaddr *)&params->addr, NULL);
361     if (conf) {
362         ctx = tlsgetctx(handle, conf->tlsconf);
363         if (!ctx)
364             goto exit;
365         ssl = dtlsacccon(1, ctx, params->sock, (struct sockaddr *)&params->addr, params->sesscache->rbios);
366         if (!ssl)
367             goto exit;
368         cert = verifytlscert(ssl);
369         if (!cert)
370             goto exit;
371         accepted_tls = conf->tlsconf;
372     }
373
374     while (conf) {
375         if (accepted_tls == conf->tlsconf && verifyconfcert(cert, conf)) {
376             X509_free(cert);
377             client = addclient(conf, 1);
378             if (client) {
379                 client->sock = params->sock;
380                 client->addr = addr_copy((struct sockaddr *)&params->addr);
381                 client->rbios = params->sesscache->rbios;
382                 client->ssl = ssl;
383                 dtlsserverrd(client);
384                 removeclient(client);
385                 delay = 0;
386             } else {
387                 debug(DBG_WARN, "dtlsservernew: failed to create new client instance");
388             }
389             goto exit;
390         }
391         conf = find_clconf(handle, (struct sockaddr *)&params->addr, &cur);
392     }
393     debug(DBG_WARN, "dtlsservernew: ignoring request, no matching TLS client");
394
395     if (cert)
396         X509_free(cert);
397
398 exit:
399     if (ssl) {
400         SSL_shutdown(ssl);
401         SSL_free(ssl);
402     }
403     pthread_mutex_lock(&params->sesscache->mutex);
404     freebios(params->sesscache->rbios);
405     params->sesscache->rbios = NULL;
406     gettimeofday(&params->sesscache->expiry, NULL);
407     params->sesscache->expiry.tv_sec += delay;
408     pthread_mutex_unlock(&params->sesscache->mutex);
409     free(params);
410     ERR_remove_state(0);
411     pthread_exit(NULL);
412     debug(DBG_DBG, "dtlsservernew: exiting");
413 }
414
415 void cacheexpire(struct hash *cache, struct timeval *last) {
416     struct timeval now;
417     struct hash_entry *he;
418     struct sessioncacheentry *e;
419
420     gettimeofday(&now, NULL);
421     if (now.tv_sec - last->tv_sec < 19)
422         return;
423
424     for (he = hash_first(cache); he; he = hash_next(he)) {
425         e = (struct sessioncacheentry *)he->data;
426         pthread_mutex_lock(&e->mutex);
427         if (!e->expiry.tv_sec || e->expiry.tv_sec > now.tv_sec) {
428             pthread_mutex_unlock(&e->mutex);
429             continue;
430         }
431         debug(DBG_DBG, "cacheexpire: freeing entry");
432         hash_extract(cache, he->key, he->keylen);
433         if (e->rbios) {
434             freebios(e->rbios);
435             e->rbios = NULL;
436         }
437         pthread_mutex_unlock(&e->mutex);
438         pthread_mutex_destroy(&e->mutex);
439     }
440     last->tv_sec = now.tv_sec;
441 }
442
443 void *udpdtlsserverrd(void *arg) {
444     int ndesc, cnt, s = *(int *)arg;
445     unsigned char buf[4];
446     struct sockaddr_storage from;
447     socklen_t fromlen = sizeof(from);
448     struct dtlsservernewparams *params;
449     fd_set readfds;
450     struct timeval timeout, lastexpiry;
451     pthread_t dtlsserverth;
452     struct hash *sessioncache;
453     struct sessioncacheentry *cacheentry;
454
455     sessioncache = hash_create();
456     if (!sessioncache)
457         debugx(1, DBG_ERR, "udpdtlsserverrd: malloc failed");
458     gettimeofday(&lastexpiry, NULL);
459
460     for (;;) {
461         FD_ZERO(&readfds);
462         FD_SET(s, &readfds);
463         memset(&timeout, 0, sizeof(struct timeval));
464         timeout.tv_sec = 60;
465         ndesc = select(s + 1, &readfds, NULL, NULL, &timeout);
466         if (ndesc < 1) {
467             cacheexpire(sessioncache, &lastexpiry);
468             continue;
469         }
470         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
471         if (cnt == -1) {
472             debug(DBG_WARN, "udpdtlsserverrd: recv failed");
473             cacheexpire(sessioncache, &lastexpiry);
474             continue;
475         }
476         cacheentry = hash_read(sessioncache, &from, fromlen);
477         if (cacheentry) {
478             debug(DBG_DBG, "udpdtlsserverrd: cache hit");
479             pthread_mutex_lock(&cacheentry->mutex);
480             if (cacheentry->rbios) {
481                 if (udp2bio(s, cacheentry->rbios, cnt))
482                     debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
483             } else
484                 recv(s, buf, 1, 0);
485             pthread_mutex_unlock(&cacheentry->mutex);
486             cacheexpire(sessioncache, &lastexpiry);
487             continue;
488         }
489
490         /* from new source */
491         debug(DBG_DBG, "udpdtlsserverrd: cache miss");
492         params = malloc(sizeof(struct dtlsservernewparams));
493         if (!params) {
494             cacheexpire(sessioncache, &lastexpiry);
495             recv(s, buf, 1, 0);
496             continue;
497         }
498         memset(params, 0, sizeof(struct dtlsservernewparams));
499         params->sesscache = malloc(sizeof(struct sessioncacheentry));
500         if (!params->sesscache) {
501             free(params);
502             cacheexpire(sessioncache, &lastexpiry);
503             recv(s, buf, 1, 0);
504             continue;
505         }
506         memset(params->sesscache, 0, sizeof(struct sessioncacheentry));
507         pthread_mutex_init(&params->sesscache->mutex, NULL);
508         params->sesscache->rbios = newqueue();
509         if (hash_insert(sessioncache, &from, fromlen, params->sesscache)) {
510             params->sock = s;
511             memcpy(&params->addr, &from, fromlen);
512
513             if (udp2bio(s, params->sesscache->rbios, cnt)) {
514                 debug(DBG_DBG, "udpdtlsserverrd: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
515                 if (!pthread_create(&dtlsserverth, &pthread_attr, dtlsservernew, (void *)params)) {
516                     pthread_detach(dtlsserverth);
517                     cacheexpire(sessioncache, &lastexpiry);
518                     continue;
519                 }
520                 debug(DBG_ERR, "udpdtlsserverrd: pthread_create failed");
521             }
522             hash_extract(sessioncache, &from, fromlen);
523         }
524         freebios(params->sesscache->rbios);
525         pthread_mutex_destroy(&params->sesscache->mutex);
526         free(params->sesscache);
527         free(params);
528         cacheexpire(sessioncache, &lastexpiry);
529     }
530 }
531
532 int dtlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
533     struct timeval now;
534     time_t elapsed;
535     X509 *cert;
536     SSL_CTX *ctx = NULL;
537     struct hostportres *hp;
538
539     debug(DBG_DBG, "dtlsconnect: called from %s", text);
540     pthread_mutex_lock(&server->lock);
541     if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
542         /* already reconnected, nothing to do */
543         debug(DBG_DBG, "dtlsconnect(%s): seems already reconnected", text);
544         pthread_mutex_unlock(&server->lock);
545         return 1;
546     }
547
548     hp = (struct hostportres *)list_first(server->conf->hostports)->data;
549     for (;;) {
550         gettimeofday(&now, NULL);
551         elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
552
553         if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
554             debug(DBG_DBG, "dtlsconnect: timeout");
555             SSL_free(server->ssl);
556             server->ssl = NULL;
557             pthread_mutex_unlock(&server->lock);
558             return 0;
559         }
560
561         if (server->connectionok) {
562             server->connectionok = 0;
563             sleep(2);
564         } else if (elapsed < 1)
565             sleep(2);
566         else if (elapsed < 60) {
567             debug(DBG_INFO, "dtlsconnect: sleeping %lds", elapsed);
568             sleep(elapsed);
569         } else if (elapsed < 100000) {
570             debug(DBG_INFO, "dtlsconnect: sleeping %ds", 60);
571             sleep(60);
572         } else
573             server->lastconnecttry.tv_sec = now.tv_sec;  /* no sleep at startup */
574         debug(DBG_WARN, "dtlsconnect: trying to open DTLS connection to %s port %s", hp->host, hp->port);
575
576         SSL_free(server->ssl);
577         server->ssl = NULL;
578         ctx = tlsgetctx(handle, server->conf->tlsconf);
579         if (!ctx)
580             continue;
581         server->ssl = dtlsacccon(0, ctx, server->sock, hp->addrinfo->ai_addr, server->rbios);
582         if (!server->ssl)
583             continue;
584         debug(DBG_DBG, "dtlsconnect: DTLS: ok");
585
586         cert = verifytlscert(server->ssl);
587         if (!cert)
588             continue;
589
590         if (verifyconfcert(cert, server->conf))
591             break;
592         X509_free(cert);
593     }
594     X509_free(cert);
595     debug(DBG_WARN, "dtlsconnect: DTLS connection to %s port %s up", hp->host, hp->port);
596     server->connectionok = 1;
597     gettimeofday(&server->lastconnecttry, NULL);
598     pthread_mutex_unlock(&server->lock);
599     return 1;
600 }
601
602 int clientradputdtls(struct server *server, unsigned char *rad) {
603     int cnt;
604     size_t len;
605     unsigned long error;
606     struct clsrvconf *conf = server->conf;
607
608     if (!server->connectionok)
609         return 0;
610     len = RADLEN(rad);
611     if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
612         while ((error = ERR_get_error()))
613             debug(DBG_ERR, "clientradputdtls: DTLS: %s", ERR_error_string(error, NULL));
614         return 0;
615     }
616     debug(DBG_DBG, "clientradputdtls: Sent %d bytes, Radius packet of length %d to DTLS peer %s", cnt, len, conf->name);
617     return 1;
618 }
619
620 /* reads UDP containing DTLS and passes it on to dtlsclientrd */
621 void *udpdtlsclientrd(void *arg) {
622     int cnt, s = *(int *)arg;
623     unsigned char buf[4];
624     struct sockaddr_storage from;
625     socklen_t fromlen = sizeof(from);
626     struct clsrvconf *conf;
627     fd_set readfds;
628
629     for (;;) {
630         FD_ZERO(&readfds);
631         FD_SET(s, &readfds);
632         if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
633             continue;
634         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
635         if (cnt == -1) {
636             debug(DBG_WARN, "udpdtlsclientrd: recv failed");
637             continue;
638         }
639
640         conf = find_srvconf(handle, (struct sockaddr *)&from, NULL);
641         if (!conf) {
642             debug(DBG_WARN, "udpdtlsclientrd: got packet from wrong or unknown DTLS peer %s, ignoring", addr2string((struct sockaddr *)&from));
643             recv(s, buf, 4, 0);
644             continue;
645         }
646         if (udp2bio(s, conf->servers->rbios, cnt))
647             debug(DBG_DBG, "radudpget: got DTLS in UDP from %s", addr2string((struct sockaddr *)&from));
648     }
649 }
650
651 void *dtlsclientrd(void *arg) {
652     struct server *server = (struct server *)arg;
653     unsigned char *buf;
654     struct timeval lastconnecttry;
655     int secs;
656
657     for (;;) {
658         /* yes, lastconnecttry is really necessary */
659         lastconnecttry = server->lastconnecttry;
660         for (secs = 0; !(buf = raddtlsget(server->ssl, server->rbios, 10)) && !server->lostrqs && secs < IDLE_TIMEOUT; secs += 10);
661         if (!buf) {
662             dtlsconnect(server, &lastconnecttry, 0, "dtlsclientrd");
663             continue;
664         }
665         replyh(server, buf);
666     }
667     ERR_remove_state(0);
668     server->clientrdgone = 1;
669     return NULL;
670 }
671
672 void addserverextradtls(struct clsrvconf *conf) {
673     switch (((struct hostportres *)list_first(conf->hostports)->data)->addrinfo->ai_family) {
674     case AF_INET:
675         if (client4_sock < 0) {
676             client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
677             if (client4_sock < 0)
678                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
679         }
680         conf->servers->sock = client4_sock;
681         break;
682     case AF_INET6:
683         if (client6_sock < 0) {
684             client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
685             if (client6_sock < 0)
686                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->name);
687         }
688         conf->servers->sock = client6_sock;
689         break;
690     default:
691         debugx(1, DBG_ERR, "addserver: unsupported address family");
692     }
693 }
694
695 void initextradtls() {
696     pthread_t cl4th, cl6th;
697
698     if (srcres) {
699         freeaddrinfo(srcres);
700         srcres = NULL;
701     }
702
703     if (client4_sock >= 0)
704         if (pthread_create(&cl4th, &pthread_attr, udpdtlsclientrd, (void *)&client4_sock))
705             debugx(1, DBG_ERR, "pthread_create failed");
706     if (client6_sock >= 0)
707         if (pthread_create(&cl6th, &pthread_attr, udpdtlsclientrd, (void *)&client6_sock))
708             debugx(1, DBG_ERR, "pthread_create failed");
709 }
710 #else
711 const struct protodefs *dtlsinit(uint8_t h) {
712     return NULL;
713 }
714 #endif
715
716 /* Local Variables: */
717 /* c-file-style: "stroustrup" */
718 /* End: */