Codebase list getdns / debian/1.1.0_a2-2 src / server.c
debian/1.1.0_a2-2

Tree @debian/1.1.0_a2-2 (Download .tar.gz)

server.c @debian/1.1.0_a2-2raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/*
 * Copyright (c) 2013, NLNet Labs, Verisign, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * * Neither the names of the copyright holders nor the
 *   names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include <netdb.h>
#include "getdns/getdns_extra.h"
#include "context.h"
#include "types-internal.h"
#include "debug.h"
#include "util/rbtree.h"
#include "server.h"

#define DNS_REQUEST_SZ          4096
#define DOWNSTREAM_IDLE_TIMEOUT 5000
#define TCP_LISTEN_BACKLOG      16

typedef struct listen_set listen_set;
typedef enum listen_set_action {
	to_stay, to_add, to_remove
} listen_set_action;

typedef struct connection connection;
typedef struct listener listener;
struct listener {
	getdns_eventloop_event   event;
	socklen_t                addr_len;
	struct sockaddr_storage  addr;
	int                      fd;
	getdns_transport_list_t  transport;

	listen_set_action        action;
	listener                *to_replace;
	listen_set              *set;

	/* Should be per context eventually */
	connection              *connections;
};

/* listen set is temporarily a singly linked list node, to associate the set
 * with a context.  Eventually it has to become a context attribute.
 */
struct listen_set {
	getdns_context           *context;
	getdns_request_handler_t  handler;

	_getdns_rbtree_t          connections_set;
	size_t                    count;
	listener                  items[];
};

typedef struct tcp_to_write tcp_to_write;
struct tcp_to_write {
	size_t        write_buf_len;
	size_t        written;
	tcp_to_write *next;
	uint8_t       write_buf[];
};

struct connection {
	/* struct connection is a sub struct of _getdns_rbnode_t */
	_getdns_rbnode_t        super;

	listener               *l;
	struct sockaddr_storage remote_in;
	socklen_t               addrlen;

	connection             *next;
	connection            **prev_next;
};

typedef struct tcp_connection {
	/* struct tcp_connection is a sub struct of connection */
	connection              super;

	int                     fd;
	getdns_eventloop_event  event;

	uint8_t                *read_buf;
	size_t                  read_buf_len;
	uint8_t                *read_pos;
	size_t                  to_read;

	tcp_to_write           *to_write;
	size_t                  to_answer;
} tcp_connection;


static void free_listen_set_when_done(listen_set *set);
static void tcp_connection_destroy(tcp_connection *conn)
{
	struct mem_funcs *mf;
	getdns_eventloop *loop;

	tcp_to_write *cur, *next;

	if (!(mf = &conn->super.l->set->context->mf))
		return;

	if (getdns_context_get_eventloop(conn->super.l->set->context, &loop))
		return;

	if (conn->event.read_cb||conn->event.write_cb||conn->event.timeout_cb)
		loop->vmt->clear(loop, &conn->event);

	if (conn->fd >= 0)
		(void) close(conn->fd);
	GETDNS_FREE(*mf, conn->read_buf);

	for (cur = conn->to_write; cur; cur = next) {
		next = cur->next;
		GETDNS_FREE(*mf, cur);
	}
	if (conn->to_answer > 0)
		return;

	/* Unlink this connection */
	(void) _getdns_rbtree_delete(
	    &conn->super.l->set->connections_set, conn);
	DEBUG_SERVER("[connection del] count: %d\n",
	    (int)conn->super.l->set->connections_set.count);
	if ((*conn->super.prev_next = conn->super.next))
		conn->super.next->prev_next = conn->super.prev_next;

	free_listen_set_when_done(conn->super.l->set);
	GETDNS_FREE(*mf, conn);
}

static void tcp_write_cb(void *userarg)
{
	tcp_connection *conn = (tcp_connection *)userarg;
	struct mem_funcs *mf;
	getdns_eventloop *loop;

	tcp_to_write *to_write;
	ssize_t written;

	assert(userarg);

	if (!(mf = &conn->super.l->set->context->mf))
		return;

	if (getdns_context_get_eventloop(conn->super.l->set->context, &loop))
		return;

	/* Reset tcp_connection idle timeout */
	loop->vmt->clear(loop, &conn->event);

	if (!conn->to_write) {
		conn->event.write_cb = NULL;
		(void) loop->vmt->schedule(loop, conn->fd,
		    DOWNSTREAM_IDLE_TIMEOUT, &conn->event);
		return;
	}
	to_write = conn->to_write;
	if (conn->fd == -1 || 
	    (written = write(conn->fd, &to_write->write_buf[to_write->written],
	    to_write->write_buf_len - to_write->written)) == -1) {

		/* IO error, close connection */
		conn->event.read_cb = conn->event.write_cb =
		    conn->event.timeout_cb = NULL;
		tcp_connection_destroy(conn);
		return;
	}
	to_write->written += written;
	if (to_write->written == to_write->write_buf_len) {
		conn->to_write = to_write->next;
		GETDNS_FREE(*mf, to_write);
	}
	if (!conn->to_write)
		conn->event.write_cb = NULL;

	(void) loop->vmt->schedule(loop, conn->fd,
	    DOWNSTREAM_IDLE_TIMEOUT, &conn->event);
}

static void
_getdns_cancel_reply(getdns_context *context, connection *conn)
{
	struct mem_funcs *mf;

	if (!context || !conn)
		return;

	if (conn->l->transport == GETDNS_TRANSPORT_TCP) {
		tcp_connection *tcp_conn = (tcp_connection *)conn;

		if (tcp_conn->to_answer > 0 && --tcp_conn->to_answer == 0 &&
		    tcp_conn->fd == -1)
			tcp_connection_destroy(tcp_conn);

	} else if (conn->l->transport == GETDNS_TRANSPORT_UDP &&
	    (mf = &conn->l->set->context->mf)) {
		listen_set *set = conn->l->set;

		/* Unlink this connection */
		(void) _getdns_rbtree_delete(
		    &set->connections_set, conn);
		DEBUG_SERVER("[connection del] count: %d\n",
		    (int)set->connections_set.count);
		if ((*conn->prev_next = conn->next))
			conn->next->prev_next = conn->prev_next;
		GETDNS_FREE(*mf, conn);
		free_listen_set_when_done(set);
	}
}

getdns_return_t
getdns_reply(
    getdns_context *context, getdns_transaction_t request_id, getdns_dict *reply)
{
	/* TODO: Check request_id at context->outbound_requests */
	connection *conn = (connection *)(intptr_t)request_id;
	struct mem_funcs *mf;
	getdns_eventloop *loop;
	uint8_t buf[65536];
	size_t len;
	getdns_return_t r;

	if (!context || !conn)
		return GETDNS_RETURN_INVALID_PARAMETER;

	if (!context->server)
		return GETDNS_RETURN_GENERIC_ERROR;;

	if (_getdns_rbtree_search(&context->server->connections_set, conn)
	    != &conn->super)
		return GETDNS_RETURN_NO_SUCH_LIST_ITEM;

	if (!reply) {
		_getdns_cancel_reply(context, conn);
		return GETDNS_RETURN_GOOD;
	}
	if (!(mf = &conn->l->set->context->mf))
		return GETDNS_RETURN_GENERIC_ERROR;;

	if ((r = getdns_context_get_eventloop(conn->l->set->context, &loop)))
		return r;

	len = sizeof(buf);
	if ((r = getdns_msg_dict2wire_buf(reply, buf, &len)))
		return r;

	else if (conn->l->transport == GETDNS_TRANSPORT_UDP) {
		listener *l = conn->l;

		if (conn->l->fd >= 0 && sendto(conn->l->fd, buf, len, 0,
		    (struct sockaddr *)&conn->remote_in, conn->addrlen) == -1) {
			/* IO error, cleanup this listener */
			loop->vmt->clear(loop, &conn->l->event);
			close(conn->l->fd);
			conn->l->fd = -1;
		}
		/* Unlink this connection */
		(void) _getdns_rbtree_delete(
		    &l->set->connections_set, conn);
		DEBUG_SERVER("[connection del] count: %d\n",
		    (int)l->set->connections_set.count);
		if ((*conn->prev_next = conn->next))
			conn->next->prev_next = conn->prev_next;

		GETDNS_FREE(*mf, conn);
		if (l->fd < 0)
			free_listen_set_when_done(l->set);

	} else if (conn->l->transport == GETDNS_TRANSPORT_TCP) {
		tcp_connection *conn = (tcp_connection *)(intptr_t)request_id;
		tcp_to_write **to_write_p;
		tcp_to_write *to_write;

		if (conn->fd == -1) {
			if (conn->to_answer > 0)
				--conn->to_answer;
			tcp_connection_destroy(conn);
			return GETDNS_RETURN_GOOD;
		}
		if (!(to_write = (tcp_to_write *)GETDNS_XMALLOC(
		    *mf, uint8_t, sizeof(tcp_to_write) + len + 2)))
			return GETDNS_RETURN_MEMORY_ERROR;

		to_write->write_buf_len = len + 2;
		to_write->write_buf[0] = (len >> 8) & 0xFF;
		to_write->write_buf[1] = len & 0xFF;
		to_write->written = 0;
		to_write->next = NULL;
		(void) memcpy(to_write->write_buf + 2, buf, len);

		/* Appen to_write to conn->to_write list */
		for ( to_write_p = &conn->to_write
		    ; *to_write_p
		    ; to_write_p = &(*to_write_p)->next)
			; /* pass */
		*to_write_p = to_write;

		loop->vmt->clear(loop, &conn->event);
		conn->event.write_cb = tcp_write_cb;
		if (conn->to_answer > 0)
			conn->to_answer--;
		(void) loop->vmt->schedule(loop,
		    conn->fd, DOWNSTREAM_IDLE_TIMEOUT,
		    &conn->event);
	}
	/* TODO: other transport types */

	return r;
}

static void tcp_read_cb(void *userarg)
{
	tcp_connection *conn = (tcp_connection *)userarg;
	ssize_t bytes_read;
	getdns_return_t r;
	struct mem_funcs *mf;
	getdns_eventloop *loop;
	getdns_dict *request_dict;

	assert(userarg);

	if (!(mf = &conn->super.l->set->context->mf))
		return;

	if ((r = getdns_context_get_eventloop(conn->super.l->set->context, &loop)))
		return;

	/* Reset tcp_connection idle timeout */
	loop->vmt->clear(loop, &conn->event);
	(void) loop->vmt->schedule(loop, conn->fd,
	    DOWNSTREAM_IDLE_TIMEOUT, &conn->event);

	if ((bytes_read = read(conn->fd, conn->read_pos, conn->to_read)) == -1) {
		if (errno == EAGAIN || errno == EWOULDBLOCK)
			return; /* Come back to do the read later */

		/* IO error, close connection */
		tcp_connection_destroy(conn);
		return;
	}
	if (bytes_read == 0) {
		/* remote end closed connection, cleanup */
		tcp_connection_destroy(conn);
		return;
	}
	assert(bytes_read <= conn->to_read);

	conn->to_read  -= bytes_read;
	conn->read_pos += bytes_read;
	if (conn->to_read)
		return; /* More to read */

	if (conn->read_pos - conn->read_buf == 2) {
		/* read length of dns msg to read */
		conn->to_read = (conn->read_buf[0] << 8) | conn->read_buf[1];
		if (conn->to_read > conn->read_buf_len) {
			GETDNS_FREE(*mf, conn->read_buf);
			while (conn->to_read > conn->read_buf_len)
				conn->read_buf_len *= 2;
			if (!(conn->read_buf = GETDNS_XMALLOC(
			    *mf, uint8_t, conn->read_buf_len))) {
				/* Memory error */
				tcp_connection_destroy(conn);
				return;
			}
		}
		if (conn->to_read < 12) {
			/* Request smaller than DNS header, FORMERR */
			tcp_connection_destroy(conn);
			return;
		}
		conn->read_pos = conn->read_buf;
		return;  /* Read DNS message */
	}
	if ((r = getdns_wire2msg_dict(conn->read_buf,
	    (conn->read_pos - conn->read_buf), &request_dict)))
		; /* FROMERR on input, ignore */

	else {
		conn->to_answer++;

		/* Call request handler */
		conn->super.l->set->handler(
		    conn->super.l->set->context, request_dict, (intptr_t)conn);

		conn->read_pos = conn->read_buf;
		conn->to_read = 2;
		return; /* Read more requests */
	}
	conn->read_pos = conn->read_buf;
	conn->to_read = 2;
	 /* Read more requests */
}

static void tcp_timeout_cb(void *userarg)
{
	tcp_connection *conn = (tcp_connection *)userarg;

	assert(userarg);

	if (conn->to_answer) {
		getdns_eventloop *loop;

		if (getdns_context_get_eventloop(
		    conn->super.l->set->context, &loop))
			return;

		loop->vmt->clear(loop, &conn->event);
		(void) loop->vmt->schedule(loop,
		    conn->fd, DOWNSTREAM_IDLE_TIMEOUT,
		    &conn->event);
	} else
		tcp_connection_destroy(conn);
}

static void tcp_accept_cb(void *userarg)
{
	listener *l = (listener *)userarg;
	tcp_connection *conn;
	struct mem_funcs *mf;
	getdns_eventloop *loop;
	getdns_return_t r;

	assert(userarg);

	if (!(mf = &l->set->context->mf))
		return;

	if ((r = getdns_context_get_eventloop(l->set->context, &loop)))
		return;

	if (!(conn = GETDNS_MALLOC(*mf, tcp_connection)))
		return;

	(void) memset(conn, 0, sizeof(tcp_connection));
	conn->super.l = l;
	conn->super.addrlen = sizeof(conn->super.remote_in);
	if ((conn->fd = accept(l->fd, (struct sockaddr *)
	    &conn->super.remote_in, &conn->super.addrlen)) == -1) {
		/* IO error, cleanup this listener */
		loop->vmt->clear(loop, &l->event);
		close(l->fd);
		l->fd = -1;
		GETDNS_FREE(*mf, conn);
		return;
	}
	if (!(conn->read_buf = malloc(DNS_REQUEST_SZ))) {
		/* Memory error */
		GETDNS_FREE(*mf, conn);
		return;
	}
	conn->read_buf_len = DNS_REQUEST_SZ;
	conn->read_pos = conn->read_buf;
	conn->to_read = 2;
	conn->event.userarg = conn;
	conn->event.read_cb = tcp_read_cb;
	conn->event.timeout_cb = tcp_timeout_cb;

	/* Insert connection */
	conn->super.super.key = conn;
	if (!_getdns_rbtree_insert(
	    &l->set->connections_set, &conn->super.super)) {
		/* Memory error */
		GETDNS_FREE(*mf, conn);
		return;
	}
	DEBUG_SERVER("[connection add] count: %d\n",
	    (int)l->set->connections_set.count);
	if ((conn->super.next = l->connections))
		conn->super.next->prev_next = &conn->super.next;
	conn->super.prev_next = &l->connections;
	l->connections = (connection *)conn;
	

	(void) loop->vmt->schedule(loop, conn->fd,
	    DOWNSTREAM_IDLE_TIMEOUT, &conn->event);
}

static void udp_read_cb(void *userarg)
{
	listener *l = (listener *)userarg;
	connection *conn;
	struct mem_funcs *mf;
	getdns_eventloop *loop;
	getdns_dict *request_dict;

	/* Maximum reasonable size for requests */
	uint8_t buf[4096];
	ssize_t len;
	getdns_return_t r;
	
	assert(userarg);

	if (l->fd == -1)
		return;

	if (!(mf = &l->set->context->mf))
		return;

	if ((r = getdns_context_get_eventloop(l->set->context, &loop)))
		return;

	if (!(conn = GETDNS_MALLOC(*mf, connection)))
		return;

	conn->l = l;
	conn->addrlen = sizeof(conn->remote_in);
	if ((len = recvfrom(l->fd, buf, sizeof(buf), 0,
	    (struct sockaddr *)&conn->remote_in, &conn->addrlen)) == -1) {
		/* IO error, cleanup this listener. */
		loop->vmt->clear(loop, &l->event);
		close(l->fd);
		l->fd = -1;

#if 0 && defined(SERVER_DEBUG) && SERVER_DEBUG
	} else {
		char addrbuf[100];
		char hexbuf[4096], *hexptr;
		size_t l, i, j;

		if (conn->remote_in.ss_family == AF_INET) {
			if (inet_ntop(AF_INET,
			    &((struct sockaddr_in*)&conn->remote_in)->sin_addr,
			    addrbuf, sizeof(addrbuf))) {

				l = strlen(addrbuf);
				(void) snprintf(addrbuf + l,
				    sizeof(addrbuf) - l, ":%d", 
				    (int)((struct sockaddr_in*)
				    &conn->remote_in)->sin_port);
			} else
				(void) strncpy(
				    addrbuf, "error ipv4", sizeof(addrbuf));

		} else if (conn->remote_in.ss_family == AF_INET6) {
			addrbuf[0] = '[';
			if (inet_ntop(AF_INET6,
			    &((struct sockaddr_in6*)
			    &conn->remote_in)->sin6_addr,
			    addrbuf, sizeof(addrbuf))) {

				l = strlen(addrbuf);
				(void) snprintf(addrbuf + l,
				    sizeof(addrbuf) - l, ":%d", 
				    (int)((struct sockaddr_in6*)
				    &conn->remote_in)->sin6_port);
			} else
				(void) strncpy(
				    addrbuf, "error ipv6", sizeof(addrbuf));

		} else {
			(void) strncpy(
			    addrbuf, "unknown address", sizeof(addrbuf));
		}
		*(hexptr = hexbuf) = 0;
		for (i = 0; i < len; i++) {
			if (i % 12 == 0) {
				hexptr += snprintf(hexptr,
				    sizeof(hexbuf) - (hexptr - hexbuf) - 1,
				    "\n%.4x", (int)i);
			} else if (i % 4 == 0) {
				hexptr += snprintf(hexptr,
				    sizeof(hexbuf) - (hexptr - hexbuf) - 1,
				    " ");
			}
			if (hexptr - hexbuf > sizeof(hexbuf))
				break;
			hexptr += snprintf(hexptr,
			    sizeof(hexbuf) - (hexptr - hexbuf) - 1,
			    " %.2x", (int)buf[i]);
			if (hexptr - hexbuf > sizeof(hexbuf))
				break;
		}
		DEBUG_SERVER("Received %d bytes from %s: %s\n",
		    (int)len, addrbuf, hexbuf);
	}
	if (len == -1) {
		; /* pass */
#endif

	} else if ((r = getdns_wire2msg_dict(buf, len, &request_dict)))
		; /* FROMERR on input, ignore */

	else {
		/* Insert connection */
		conn->super.key = conn;
		if (!_getdns_rbtree_insert(
		    &l->set->connections_set, &conn->super)) {
			/* Memory error */
			GETDNS_FREE(*mf, conn);
			return;
		}
		DEBUG_SERVER("[connection add] count: %d\n",
		    (int)l->set->connections_set.count);
		if ((conn->next = l->connections))
			conn->next->prev_next = &conn->next;
		conn->prev_next = &l->connections;
		l->connections = conn;

		/* Call request handler */
		l->set->handler(l->set->context, request_dict, (intptr_t)conn);
		return;
	}
	GETDNS_FREE(*mf, conn);
}

static void free_listen_set_when_done(listen_set *set)
{
	struct mem_funcs *mf;
	size_t            i;

	assert(set);
	assert(set->context);

	if (!(mf = &set->context->mf))
		return;

	DEBUG_SERVER("To free listen set: %p\n", set);
	for (i = 0; i < set->count; i++) {
		listener *l = &set->items[i];

		if (l->fd >= 0)
			return;

		if (l->connections)
			return;
	}
	GETDNS_FREE(*mf, set);
	DEBUG_SERVER("Listen set: %p freed\n", set);
}

static void remove_listeners(listen_set *set)
{
	struct mem_funcs *mf;
	getdns_eventloop *loop;
	size_t            i;

	assert(set);
	assert(set->context);

	if (!(mf = &set->context->mf))
		return;

	if (getdns_context_get_eventloop(set->context, &loop))
		return;

	for (i = 0; i < set->count; i++) {
		listener *l = &set->items[i];
		tcp_connection **conn_p;

		if (l->action != to_remove || l->fd == -1)
			continue;

		loop->vmt->clear(loop, &l->event);
		close(l->fd);
		l->fd = -1;

		if (l->transport != GETDNS_TRANSPORT_TCP)
			continue;
		
		conn_p = (tcp_connection **)&l->connections;
		while (*conn_p) {
			tcp_connection_destroy(*conn_p);
			if (*conn_p && (*conn_p)->to_answer > 0)
				conn_p = (tcp_connection **)
				    &(*conn_p)->super.next;
		}
	}
	free_listen_set_when_done(set);
}

static getdns_return_t add_listeners(listen_set *set)
{
	static const int  enable = 1;

	struct mem_funcs *mf;
	getdns_eventloop *loop;
	size_t            i;
	getdns_return_t   r;

	assert(set);
	assert(set->context);

	if (!(mf = &set->context->mf))
		return GETDNS_RETURN_GENERIC_ERROR;

	if ((r = getdns_context_get_eventloop(set->context, &loop)))
		return r;

	r = GETDNS_RETURN_GENERIC_ERROR;
	for (i = 0; i < set->count; i++) {
		listener *l = &set->items[i];

		if (l->action != to_add)
			continue;

		if (l->transport != GETDNS_TRANSPORT_UDP &&
		    l->transport != GETDNS_TRANSPORT_TCP)
			continue;

		if ((l->fd = socket(l->addr.ss_family,
		    ( l->transport == GETDNS_TRANSPORT_UDP
		    ? SOCK_DGRAM : SOCK_STREAM), 0)) == -1)
			/* IO error */
			break;

		if (setsockopt(l->fd, SOL_SOCKET, SO_REUSEADDR,
		    &enable, sizeof(int)) < 0)
			; /* Ignore */

		if (bind(l->fd, (struct sockaddr *)&l->addr,
		    l->addr_len) == -1)
			/* IO error */
			break;

		if (l->transport == GETDNS_TRANSPORT_UDP) {
			l->event.userarg = l;
			l->event.read_cb = udp_read_cb;
			if ((r = loop->vmt->schedule(
			    loop, l->fd, -1, &l->event)))
				break;

		} else if (listen(l->fd, TCP_LISTEN_BACKLOG) == -1)
			/* IO error */
			break;

		else {
			l->event.userarg = l;
			l->event.read_cb = tcp_accept_cb;
			if ((r = loop->vmt->schedule(
			    loop, l->fd, -1, &l->event)))
				break;
		}
	}
	if (i < set->count)
		return r;

	return GETDNS_RETURN_GOOD;
}

static int
ptr_cmp(const void *a, const void *b)
{
	return a == b ? 0 : (a < b ? -1 : 1);
}

getdns_return_t getdns_context_set_listen_addresses(getdns_context *context,
    getdns_request_handler_t request_handler,
    const getdns_list *listen_addresses)
{
	static const getdns_transport_list_t listen_transports[]
		= { GETDNS_TRANSPORT_UDP, GETDNS_TRANSPORT_TCP };
	static const uint32_t transport_ports[] = { 53, 53 };
	static const size_t n_transports = sizeof( listen_transports)
	                                 / sizeof(*listen_transports);
	listen_set        *current_set;
	listen_set        *new_set;
	size_t             new_set_count;

	struct mem_funcs  *mf;
	getdns_eventloop  *loop;

	/* auxiliary variables */
	getdns_return_t r;
	size_t i;
	struct addrinfo hints;

	DEBUG_SERVER("getdns_context_set_listen_addresses(%p, %p, %p)\n",
	    context, request_handler,

	    listen_addresses);
	if (!(mf = &context->mf))
		return GETDNS_RETURN_GENERIC_ERROR;

	if ((r = getdns_context_get_eventloop(context, &loop)))
		return r;

	if (listen_addresses == NULL)
		new_set_count = 0;

	else if ((r = getdns_list_get_length(listen_addresses, &new_set_count)))
		return r;

	if ((current_set = context->server)) {
		for (i = 0; i < current_set->count; i++)
			current_set->items[i].action = to_remove;
	}
	if (new_set_count == 0) {
		if (!current_set)
			return GETDNS_RETURN_GOOD;
		
		context->server = NULL;
		/* action is already to_remove */
		remove_listeners(current_set);
		return GETDNS_RETURN_GOOD;
	}
	if (!request_handler)
		return GETDNS_RETURN_INVALID_PARAMETER;

	if (!(new_set = (listen_set *)GETDNS_XMALLOC(*mf, uint8_t,
	    sizeof(listen_set) +
	    sizeof(listener) * new_set_count * n_transports)))
		return GETDNS_RETURN_MEMORY_ERROR;

	_getdns_rbtree_init(&new_set->connections_set, ptr_cmp);

	DEBUG_SERVER("New listen set: %p, current_set: %p\n",
	    new_set, current_set);

	new_set->context = context;
	new_set->handler = request_handler;
	new_set->count = new_set_count * n_transports;
	(void) memset(new_set->items, 0,
	    sizeof(listener) * new_set_count * n_transports);
	for (i = 0; i < new_set->count; i++)
		new_set->items[i].fd = -1;

	(void) memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family    = AF_UNSPEC;
	hints.ai_flags     = AI_NUMERICHOST;

	for (i = 0; !r && i < new_set_count; i++) {
		getdns_dict             *dict = NULL;
		getdns_bindata          *address_data;
		struct sockaddr_storage  addr;
		getdns_bindata          *scope_id;
		char                     addrstr[1024], *eos;
		size_t                   t;

		if ((r = getdns_list_get_dict(listen_addresses, i, &dict))) {
			if ((r = getdns_list_get_bindata(
			    listen_addresses, i, &address_data)))
				break;

		} else if ((r = getdns_dict_get_bindata(
		    dict, "address_data", &address_data)))
			break;

		if (address_data->size == 4)
			addr.ss_family = AF_INET;
		else if (address_data->size == 16)
			addr.ss_family = AF_INET6;
		else {
			r = GETDNS_RETURN_INVALID_PARAMETER;
			break;
		}
		if (inet_ntop(addr.ss_family,
		    address_data->data, addrstr, 1024) == NULL) {
			r = GETDNS_RETURN_INVALID_PARAMETER;
			break;
		}
		if (dict && getdns_dict_get_bindata(dict,"scope_id",&scope_id)
		    == GETDNS_RETURN_GOOD) {
			if (strlen(addrstr) + scope_id->size > 1022) {
				r = GETDNS_RETURN_INVALID_PARAMETER;
				break;
			}
			eos = &addrstr[strlen(addrstr)];
			*eos++ = '%';
			(void) memcpy(eos, scope_id->data, scope_id->size);
			eos[scope_id->size] = 0;
		}
		for (t = 0; !r && t < n_transports; t++) {
			char portstr[1024];
			getdns_transport_list_t transport
			    = listen_transports[t];
			uint32_t port = transport_ports[t];
			struct addrinfo *ai;
			listener *l = &new_set->items[i*n_transports + t];
			size_t j;
			listener *cl;

			l->fd = -1;
			if (dict)
				(void) getdns_dict_get_int(dict,
				    ( transport == GETDNS_TRANSPORT_TLS
				    ? "tls_port" : "port" ), &port);

			(void) snprintf(portstr, 1024, "%d", (int)port);

			if (getaddrinfo(addrstr, portstr, &hints, &ai)) {
				r = GETDNS_RETURN_INVALID_PARAMETER;
				break;
			}
			if (!ai)
				continue;

			l->addr.ss_family = addr.ss_family;
			l->addr_len = ai->ai_addrlen;
			(void) memcpy(&l->addr, ai->ai_addr, ai->ai_addrlen);
			l->transport = transport;
			l->set = new_set;
			l->connections = NULL;
			freeaddrinfo(ai);

			/* Now determine the action */
			if (!current_set) {
				l->action = to_add;
				continue;
			}
			for (j = 0; j < current_set->count; j++) {
				cl = &current_set->items[j];
				
				if (l->transport == cl->transport &&
				    l->addr_len == cl->addr_len &&
				    !memcmp(&l->addr, &cl->addr, l->addr_len))
					break;
			}
			if (j == current_set->count) {
				/* Not found */
				l->action = to_add;
				continue;
			}
			l->action = to_stay;
			l->to_replace = cl;
			/* So the event can be rescheduled */
		}
	}
	if (r || (r = add_listeners(new_set))) {
		for (i = 0; i < new_set->count; i++)
			new_set->items[i].action = to_remove;

		remove_listeners(new_set);
		return r;
	}
	/* Reschedule all stayers */
	for (i = 0; i < new_set->count; i++) {
		listener *l = &new_set->items[i];

		if (l->action == to_stay) {
			connection *conn;

			loop->vmt->clear(loop, &l->to_replace->event);
			(void) memset(&l->to_replace->event, 0,
			    sizeof(getdns_eventloop_event));

			l->fd = l->to_replace->fd;
			l->event = l->to_replace->event;
			l->connections = l->to_replace->connections;
			for (conn = l->connections; conn; conn = conn->next)
				conn->l = l;

			l->to_replace->connections = NULL;
			l->to_replace->fd = -1;

			/* assume success on reschedule */
			(void) loop->vmt->schedule(loop, l->fd, -1, &l->event);
		}
	}
	if (current_set) {
		context->server = NULL;
		remove_listeners(current_set); /* Is already remove */
	}
	context->server = new_set;
	return GETDNS_RETURN_GOOD;
}