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