xref: /openbsd/usr.sbin/nsd/xfrd.c (revision 274d7c50)
1 /*
2  * xfrd.c - XFR (transfer) Daemon source file. Coordinates SOA updates.
3  *
4  * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  */
9 
10 #include "config.h"
11 #include <assert.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include "xfrd.h"
19 #include "xfrd-tcp.h"
20 #include "xfrd-disk.h"
21 #include "xfrd-notify.h"
22 #include "options.h"
23 #include "util.h"
24 #include "netio.h"
25 #include "region-allocator.h"
26 #include "nsd.h"
27 #include "packet.h"
28 #include "rdata.h"
29 #include "difffile.h"
30 #include "ipc.h"
31 #include "remote.h"
32 #include "rrl.h"
33 #ifdef USE_DNSTAP
34 #include "dnstap/dnstap_collector.h"
35 #endif
36 
37 #ifdef HAVE_SYSTEMD
38 #include <systemd/sd-daemon.h>
39 #endif
40 
41 #define XFRD_UDP_TIMEOUT 10 /* seconds, before a udp request times out */
42 #define XFRD_NO_IXFR_CACHE 172800 /* 48h before retrying ixfr's after notimpl */
43 #define XFRD_LOWERBOUND_REFRESH 1 /* seconds, smallest refresh timeout */
44 #define XFRD_LOWERBOUND_RETRY 1 /* seconds, smallest retry timeout */
45 #define XFRD_MAX_ROUNDS 1 /* max number of rounds along the masters */
46 #define XFRD_TSIG_MAX_UNSIGNED 103 /* max number of packets without tsig in a tcp stream. */
47 			/* rfc recommends 100, +3 for offbyone errors/interoperability. */
48 #define XFRD_CHILD_REAP_TIMEOUT 60 /* seconds to wakeup and reap lost children */
49 		/* these are reload processes that SIGCHILDed but the signal
50 		 * was lost, and need waitpid to remove their process entry. */
51 
52 /* the daemon state */
53 xfrd_state_type* xfrd = 0;
54 
55 /* main xfrd loop */
56 static void xfrd_main(void);
57 /* shut down xfrd, close sockets. */
58 static void xfrd_shutdown(void);
59 /* delete pending task xfr files in tmp */
60 static void xfrd_clean_pending_tasks(struct nsd* nsd, udb_base* u);
61 /* create zone rbtree at start */
62 static void xfrd_init_zones(void);
63 /* initial handshake with SOAINFO from main and send expire to main */
64 static void xfrd_receive_soa(int socket, int shortsoa);
65 
66 /* handle incoming notification message. soa can be NULL. true if transfer needed. */
67 static int xfrd_handle_incoming_notify(xfrd_zone_type* zone,
68 	xfrd_soa_type* soa);
69 
70 /* call with buffer just after the soa dname. returns 0 on error. */
71 static int xfrd_parse_soa_info(buffer_type* packet, xfrd_soa_type* soa);
72 /* set the zone state to a new state (takes care of expiry messages) */
73 static void xfrd_set_zone_state(xfrd_zone_type* zone,
74 	enum xfrd_zone_state new_zone_state);
75 /* set timer for retry amount (depends on zone_state) */
76 static void xfrd_set_timer_retry(xfrd_zone_type* zone);
77 /* set timer for refresh timeout (depends on zone_state) */
78 static void xfrd_set_timer_refresh(xfrd_zone_type* zone);
79 
80 /* set reload timeout */
81 static void xfrd_set_reload_timeout(void);
82 /* handle reload timeout */
83 static void xfrd_handle_reload(int fd, short event, void* arg);
84 /* handle child timeout */
85 static void xfrd_handle_child_timer(int fd, short event, void* arg);
86 
87 /* send ixfr request, returns fd of connection to read on */
88 static int xfrd_send_ixfr_request_udp(xfrd_zone_type* zone);
89 /* obtain udp socket slot */
90 static void xfrd_udp_obtain(xfrd_zone_type* zone);
91 
92 /* read data via udp */
93 static void xfrd_udp_read(xfrd_zone_type* zone);
94 
95 /* find master by notify number */
96 static int find_same_master_notify(xfrd_zone_type* zone, int acl_num_nfy);
97 
98 /* set the write timer to activate */
99 static void xfrd_write_timer_set(void);
100 
101 static void
102 xfrd_signal_callback(int sig, short event, void* ATTR_UNUSED(arg))
103 {
104 	if(!(event & EV_SIGNAL))
105 		return;
106 	sig_handler(sig);
107 }
108 
109 static struct event* xfrd_sig_evs[10];
110 static int xfrd_sig_num = 0;
111 
112 static void
113 xfrd_sigsetup(int sig)
114 {
115 	struct event *ev = xalloc_zero(sizeof(*ev));
116 	assert(xfrd_sig_num <= (int)(sizeof(xfrd_sig_evs)/sizeof(ev)));
117 	xfrd_sig_evs[xfrd_sig_num++] = ev;
118 	signal_set(ev, sig, xfrd_signal_callback, NULL);
119 	if(event_base_set(xfrd->event_base, ev) != 0) {
120 		log_msg(LOG_ERR, "xfrd sig handler: event_base_set failed");
121 	}
122 	if(signal_add(ev, NULL) != 0) {
123 		log_msg(LOG_ERR, "xfrd sig handler: signal_add failed");
124 	}
125 }
126 
127 void
128 xfrd_init(int socket, struct nsd* nsd, int shortsoa, int reload_active,
129 	pid_t nsd_pid)
130 {
131 	region_type* region;
132 
133 	assert(xfrd == 0);
134 	/* to setup signalhandling */
135 	nsd->server_kind = NSD_SERVER_MAIN;
136 
137 	region = region_create_custom(xalloc, free, DEFAULT_CHUNK_SIZE,
138 		DEFAULT_LARGE_OBJECT_SIZE, DEFAULT_INITIAL_CLEANUP_SIZE, 1);
139 	xfrd = (xfrd_state_type*)region_alloc(region, sizeof(xfrd_state_type));
140 	memset(xfrd, 0, sizeof(xfrd_state_type));
141 	xfrd->region = region;
142 	xfrd->xfrd_start_time = time(0);
143 	xfrd->event_base = nsd_child_event_base();
144 	if(!xfrd->event_base) {
145 		log_msg(LOG_ERR, "xfrd: cannot create event base");
146 		exit(1);
147 	}
148 	xfrd->nsd = nsd;
149 	xfrd->packet = buffer_create(xfrd->region, QIOBUFSZ);
150 	xfrd->udp_waiting_first = NULL;
151 	xfrd->udp_waiting_last = NULL;
152 	xfrd->udp_use_num = 0;
153 	xfrd->got_time = 0;
154 	xfrd->xfrfilenumber = 0;
155 #ifdef USE_ZONE_STATS
156 	xfrd->zonestat_safe = nsd->zonestatdesired;
157 #endif
158 	xfrd->activated_first = NULL;
159 	xfrd->ipc_pass = buffer_create(xfrd->region, QIOBUFSZ);
160 	xfrd->last_task = region_alloc(xfrd->region, sizeof(*xfrd->last_task));
161 	udb_ptr_init(xfrd->last_task, xfrd->nsd->task[xfrd->nsd->mytask]);
162 	assert(shortsoa || udb_base_get_userdata(xfrd->nsd->task[xfrd->nsd->mytask])->data == 0);
163 
164 	xfrd->reload_handler.ev_fd = -1;
165 	xfrd->reload_added = 0;
166 	xfrd->reload_timeout.tv_sec = 0;
167 	xfrd->reload_cmd_last_sent = xfrd->xfrd_start_time;
168 	xfrd->can_send_reload = !reload_active;
169 	xfrd->reload_pid = nsd_pid;
170 	xfrd->child_timer_added = 0;
171 
172 	xfrd->ipc_send_blocked = 0;
173 	memset(&xfrd->ipc_handler, 0, sizeof(xfrd->ipc_handler));
174 	event_set(&xfrd->ipc_handler, socket, EV_PERSIST|EV_READ,
175 		xfrd_handle_ipc, xfrd);
176 	if(event_base_set(xfrd->event_base, &xfrd->ipc_handler) != 0)
177 		log_msg(LOG_ERR, "xfrd ipc handler: event_base_set failed");
178 	if(event_add(&xfrd->ipc_handler, NULL) != 0)
179 		log_msg(LOG_ERR, "xfrd ipc handler: event_add failed");
180 	xfrd->ipc_handler_flags = EV_PERSIST|EV_READ;
181 	xfrd->ipc_conn = xfrd_tcp_create(xfrd->region, QIOBUFSZ);
182 	/* not reading using ipc_conn yet */
183 	xfrd->ipc_conn->is_reading = 0;
184 	xfrd->ipc_conn->fd = socket;
185 	xfrd->need_to_send_reload = 0;
186 	xfrd->need_to_send_shutdown = 0;
187 	xfrd->need_to_send_stats = 0;
188 
189 	xfrd->write_zonefile_needed = 0;
190 	if(nsd->options->zonefiles_write)
191 		xfrd_write_timer_set();
192 
193 	xfrd->notify_waiting_first = NULL;
194 	xfrd->notify_waiting_last = NULL;
195 	xfrd->notify_udp_num = 0;
196 
197 #ifdef HAVE_SSL
198 	daemon_remote_attach(xfrd->nsd->rc, xfrd);
199 #endif
200 
201 	xfrd->tcp_set = xfrd_tcp_set_create(xfrd->region);
202 	xfrd->tcp_set->tcp_timeout = nsd->tcp_timeout;
203 #ifndef HAVE_ARC4RANDOM
204 	srandom((unsigned long) getpid() * (unsigned long) time(NULL));
205 #endif
206 
207 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd pre-startup"));
208 	xfrd_init_zones();
209 	xfrd_receive_soa(socket, shortsoa);
210 	if(nsd->options->xfrdfile != NULL && nsd->options->xfrdfile[0]!=0)
211 		xfrd_read_state(xfrd);
212 
213 	/* did we get killed before startup was successful? */
214 	if(nsd->signal_hint_shutdown) {
215 		kill(nsd_pid, SIGTERM);
216 		xfrd_shutdown();
217 		return;
218 	}
219 
220 	/* init libevent signals now, so that in the previous init scripts
221 	 * the normal sighandler is called, and can set nsd->signal_hint..
222 	 * these are also looked at in sig_process before we run the main loop*/
223 	xfrd_sigsetup(SIGHUP);
224 	xfrd_sigsetup(SIGTERM);
225 	xfrd_sigsetup(SIGQUIT);
226 	xfrd_sigsetup(SIGCHLD);
227 	xfrd_sigsetup(SIGALRM);
228 	xfrd_sigsetup(SIGILL);
229 	xfrd_sigsetup(SIGUSR1);
230 	xfrd_sigsetup(SIGINT);
231 
232 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd startup"));
233 #ifdef HAVE_SYSTEMD
234 	sd_notify(0, "READY=1");
235 #endif
236 	xfrd_main();
237 }
238 
239 static void
240 xfrd_process_activated(void)
241 {
242 	xfrd_zone_type* zone;
243 	while((zone = xfrd->activated_first)) {
244 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s activation",
245 			zone->apex_str));
246 		/* pop zone from activated list */
247 		xfrd->activated_first = zone->activated_next;
248 		if(zone->activated_next)
249 			zone->activated_next->activated_prev = NULL;
250 		zone->is_activated = 0;
251 		/* run it : no events, specifically not the TIMEOUT event,
252 		 * so that running zone transfers are not interrupted */
253 		xfrd_handle_zone(zone->zone_handler.ev_fd, 0, zone);
254 	}
255 }
256 
257 static void
258 xfrd_sig_process(void)
259 {
260 	int status;
261 	pid_t child_pid;
262 
263 	if(xfrd->nsd->signal_hint_quit || xfrd->nsd->signal_hint_shutdown) {
264 		xfrd->nsd->signal_hint_quit = 0;
265 		xfrd->nsd->signal_hint_shutdown = 0;
266 		xfrd->need_to_send_shutdown = 1;
267 		if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
268 			ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
269 		}
270 	} else if(xfrd->nsd->signal_hint_reload_hup) {
271 		log_msg(LOG_WARNING, "SIGHUP received, reloading...");
272 		xfrd->nsd->signal_hint_reload_hup = 0;
273 		if(xfrd->nsd->options->zonefiles_check) {
274 			task_new_check_zonefiles(xfrd->nsd->task[
275 				xfrd->nsd->mytask], xfrd->last_task, NULL);
276 		}
277 		xfrd_set_reload_now(xfrd);
278 	} else if(xfrd->nsd->signal_hint_statsusr) {
279 		xfrd->nsd->signal_hint_statsusr = 0;
280 		xfrd->need_to_send_stats = 1;
281 		if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
282 			ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
283 		}
284 	}
285 
286 	/* collect children that exited. */
287 	xfrd->nsd->signal_hint_child = 0;
288 	while((child_pid = waitpid(-1, &status, WNOHANG)) != -1 && child_pid != 0) {
289 		if(status != 0) {
290 			log_msg(LOG_ERR, "process %d exited with status %d",
291 				(int)child_pid, status);
292 		}
293 	}
294 	if(!xfrd->child_timer_added) {
295 		struct timeval tv;
296 		tv.tv_sec = XFRD_CHILD_REAP_TIMEOUT;
297 		tv.tv_usec = 0;
298 		memset(&xfrd->child_timer, 0, sizeof(xfrd->child_timer));
299 		event_set(&xfrd->child_timer, -1, EV_TIMEOUT,
300 			xfrd_handle_child_timer, xfrd);
301 		if(event_base_set(xfrd->event_base, &xfrd->child_timer) != 0)
302 			log_msg(LOG_ERR, "xfrd child timer: event_base_set failed");
303 		if(event_add(&xfrd->child_timer, &tv) != 0)
304 			log_msg(LOG_ERR, "xfrd child timer: event_add failed");
305 		xfrd->child_timer_added = 1;
306 	}
307 }
308 
309 static void
310 xfrd_main(void)
311 {
312 	/* we may have signals from the startup period, process them */
313 	xfrd_sig_process();
314 	xfrd->shutdown = 0;
315 	while(!xfrd->shutdown)
316 	{
317 		/* process activated zones before blocking in select again */
318 		xfrd_process_activated();
319 		/* dispatch may block for a longer period, so current is gone */
320 		xfrd->got_time = 0;
321 		if(event_base_loop(xfrd->event_base, EVLOOP_ONCE) == -1) {
322 			if (errno != EINTR) {
323 				log_msg(LOG_ERR,
324 					"xfrd dispatch failed: %s",
325 					strerror(errno));
326 			}
327 		}
328 		xfrd_sig_process();
329 	}
330 	xfrd_shutdown();
331 }
332 
333 static void
334 xfrd_shutdown()
335 {
336 	xfrd_zone_type* zone;
337 
338 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd shutdown"));
339 #ifdef HAVE_SYSTEMD
340 	sd_notify(0, "STOPPING=1");
341 #endif
342 	event_del(&xfrd->ipc_handler);
343 	close(xfrd->ipc_handler.ev_fd); /* notifies parent we stop */
344 	zone_list_close(nsd.options);
345 	if(xfrd->nsd->options->xfrdfile != NULL && xfrd->nsd->options->xfrdfile[0]!=0)
346 		xfrd_write_state(xfrd);
347 	if(xfrd->reload_added) {
348 		event_del(&xfrd->reload_handler);
349 		xfrd->reload_added = 0;
350 	}
351 	if(xfrd->child_timer_added) {
352 		event_del(&xfrd->child_timer);
353 		xfrd->child_timer_added = 0;
354 	}
355 	if(xfrd->nsd->options->zonefiles_write) {
356 		event_del(&xfrd->write_timer);
357 	}
358 #ifdef HAVE_SSL
359 	daemon_remote_close(xfrd->nsd->rc); /* close sockets of rc */
360 #endif
361 	/* close sockets */
362 	RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
363 	{
364 		if(zone->event_added) {
365 			event_del(&zone->zone_handler);
366 			if(zone->zone_handler.ev_fd != -1) {
367 				close(zone->zone_handler.ev_fd);
368 				zone->zone_handler.ev_fd = -1;
369 			}
370 			zone->event_added = 0;
371 		}
372 	}
373 	close_notify_fds(xfrd->notify_zones);
374 
375 	/* wait for server parent (if necessary) */
376 	if(xfrd->reload_pid != -1) {
377 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd wait for servermain %d",
378 			(int)xfrd->reload_pid));
379 		while(1) {
380 			if(waitpid(xfrd->reload_pid, NULL, 0) == -1) {
381 				if(errno == EINTR) continue;
382 				if(errno == ECHILD) break;
383 				log_msg(LOG_ERR, "xfrd: waitpid(%d): %s",
384 					(int)xfrd->reload_pid, strerror(errno));
385 			}
386 			break;
387 		}
388 	}
389 
390 	/* if we are killed past this point this is not a problem,
391 	 * some files left in /tmp are cleaned by the OS, but it is neater
392 	 * to clean them out */
393 
394 	/* unlink xfr files for running transfers */
395 	RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
396 	{
397 		if(zone->msg_seq_nr)
398 			xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
399 	}
400 	/* unlink xfr files in not-yet-done task file */
401 	xfrd_clean_pending_tasks(xfrd->nsd, xfrd->nsd->task[xfrd->nsd->mytask]);
402 	xfrd_del_tempdir(xfrd->nsd);
403 #ifdef HAVE_SSL
404 	daemon_remote_delete(xfrd->nsd->rc); /* ssl-delete secret keys */
405 	if (xfrd->nsd->tls_ctx)
406 		SSL_CTX_free(xfrd->nsd->tls_ctx);
407 #endif
408 #ifdef USE_DNSTAP
409 	dt_collector_close(nsd.dt_collector, &nsd);
410 #endif
411 
412 	/* process-exit cleans up memory used by xfrd process */
413 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd shutdown complete"));
414 #ifdef MEMCLEAN /* OS collects memory pages */
415 	if(xfrd->zones) {
416 		xfrd_zone_type* z;
417 		RBTREE_FOR(z, xfrd_zone_type*, xfrd->zones) {
418 			tsig_delete_record(&z->tsig, NULL);
419 		}
420 	}
421 	if(xfrd->notify_zones) {
422 		struct notify_zone* n;
423 		RBTREE_FOR(n, struct notify_zone*, xfrd->notify_zones) {
424 			tsig_delete_record(&n->notify_tsig, NULL);
425 		}
426 	}
427 	if(xfrd_sig_num > 0) {
428 		int i;
429 		for(i=0; i<xfrd_sig_num; i++) {
430 			signal_del(xfrd_sig_evs[i]);
431 			free(xfrd_sig_evs[i]);
432 		}
433 		for(i=0; i<(int)nsd.ifs; i++) {
434 			if(nsd.udp[i].s != -1 && nsd.udp[i].addr)
435 				freeaddrinfo(nsd.udp[i].addr);
436 			if(nsd.tcp[i].s != -1 && nsd.tcp[i].addr)
437 				freeaddrinfo(nsd.tcp[i].addr);
438 		}
439 	}
440 #ifdef RATELIMIT
441 	rrl_mmap_deinit();
442 #endif
443 #ifdef USE_DNSTAP
444 	dt_collector_destroy(nsd.dt_collector, &nsd);
445 #endif
446 	udb_base_free(nsd.task[0]);
447 	udb_base_free(nsd.task[1]);
448 	event_base_free(xfrd->event_base);
449 	region_destroy(xfrd->region);
450 	nsd_options_destroy(nsd.options);
451 	region_destroy(nsd.region);
452 	log_finalize();
453 #endif
454 
455 	exit(0);
456 }
457 
458 static void
459 xfrd_clean_pending_tasks(struct nsd* nsd, udb_base* u)
460 {
461 	udb_ptr t;
462 	udb_ptr_new(&t, u, udb_base_get_userdata(u));
463 	/* no dealloc of entries, we delete the entire file when done */
464 	while(!udb_ptr_is_null(&t)) {
465 		if(TASKLIST(&t)->task_type == task_apply_xfr) {
466 			xfrd_unlink_xfrfile(nsd, TASKLIST(&t)->yesno);
467 		}
468 		udb_ptr_set_rptr(&t, u, &TASKLIST(&t)->next);
469 	}
470 	udb_ptr_unlink(&t, u);
471 }
472 
473 void
474 xfrd_init_slave_zone(xfrd_state_type* xfrd, struct zone_options* zone_opt)
475 {
476 	xfrd_zone_type *xzone;
477 	xzone = (xfrd_zone_type*)region_alloc(xfrd->region,
478 		sizeof(xfrd_zone_type));
479 	memset(xzone, 0, sizeof(xfrd_zone_type));
480 	xzone->apex = zone_opt->node.key;
481 	xzone->apex_str = zone_opt->name;
482 	xzone->state = xfrd_zone_refreshing;
483 	xzone->zone_options = zone_opt;
484 	/* first retry will use first master */
485 	xzone->master = xzone->zone_options->pattern->request_xfr;
486 	xzone->master_num = 0;
487 	xzone->next_master = 0;
488 	xzone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
489 
490 	xzone->soa_nsd_acquired = 0;
491 	xzone->soa_disk_acquired = 0;
492 	xzone->soa_notified_acquired = 0;
493 	/* [0]=1, [1]=0; "." domain name */
494 	xzone->soa_nsd.prim_ns[0] = 1;
495 	xzone->soa_nsd.email[0] = 1;
496 	xzone->soa_disk.prim_ns[0]=1;
497 	xzone->soa_disk.email[0]=1;
498 	xzone->soa_notified.prim_ns[0]=1;
499 	xzone->soa_notified.email[0]=1;
500 
501 	xzone->zone_handler.ev_fd = -1;
502 	xzone->zone_handler_flags = 0;
503 	xzone->event_added = 0;
504 
505 	xzone->tcp_conn = -1;
506 	xzone->tcp_waiting = 0;
507 	xzone->udp_waiting = 0;
508 	xzone->is_activated = 0;
509 
510 	xzone->multi_master_first_master = -1;
511 	xzone->multi_master_update_check = -1;
512 	tsig_create_record_custom(&xzone->tsig, NULL, 0, 0, 4);
513 
514 	/* set refreshing anyway, if we have data it may be old */
515 	xfrd_set_refresh_now(xzone);
516 
517 	xzone->node.key = xzone->apex;
518 	rbtree_insert(xfrd->zones, (rbnode_type*)xzone);
519 }
520 
521 static void
522 xfrd_init_zones()
523 {
524 	struct zone_options *zone_opt;
525 	assert(xfrd->zones == 0);
526 
527 	xfrd->zones = rbtree_create(xfrd->region,
528 		(int (*)(const void *, const void *)) dname_compare);
529 	xfrd->notify_zones = rbtree_create(xfrd->region,
530 		(int (*)(const void *, const void *)) dname_compare);
531 
532 	RBTREE_FOR(zone_opt, struct zone_options*, xfrd->nsd->options->zone_options)
533 	{
534 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: adding %s zone",
535 			zone_opt->name));
536 
537 		init_notify_send(xfrd->notify_zones, xfrd->region, zone_opt);
538 		if(!zone_is_slave(zone_opt)) {
539 			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s, "
540 				"master zone has no outgoing xfr requests",
541 				zone_opt->name));
542 			continue;
543 		}
544 		xfrd_init_slave_zone(xfrd, zone_opt);
545 	}
546 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: started server %d "
547 		"secondary zones", (int)xfrd->zones->count));
548 }
549 
550 static void
551 xfrd_process_soa_info_task(struct task_list_d* task)
552 {
553 	xfrd_soa_type soa;
554 	xfrd_soa_type* soa_ptr = &soa;
555 	xfrd_zone_type* zone;
556 	DEBUG(DEBUG_IPC,1, (LOG_INFO, "xfrd: process SOAINFO %s",
557 		dname_to_string(task->zname, 0)));
558 	zone = (xfrd_zone_type*)rbtree_search(xfrd->zones, task->zname);
559 	if(task->size <= sizeof(struct task_list_d)+dname_total_size(
560 		task->zname)+sizeof(uint32_t)*6 + sizeof(uint8_t)*2) {
561 		/* NSD has zone without any info */
562 		DEBUG(DEBUG_IPC,1, (LOG_INFO, "SOAINFO for %s lost zone",
563 			dname_to_string(task->zname,0)));
564 		soa_ptr = NULL;
565 	} else {
566 		uint8_t* p = (uint8_t*)task->zname + dname_total_size(
567 			task->zname);
568 		/* read the soa info */
569 		memset(&soa, 0, sizeof(soa));
570 		/* left out type, klass, count for speed */
571 		soa.type = htons(TYPE_SOA);
572 		soa.klass = htons(CLASS_IN);
573 		memmove(&soa.ttl, p, sizeof(uint32_t));
574 		p += sizeof(uint32_t);
575 		soa.rdata_count = htons(7);
576 		memmove(soa.prim_ns, p, sizeof(uint8_t));
577 		p += sizeof(uint8_t);
578 		memmove(soa.prim_ns+1, p, soa.prim_ns[0]);
579 		p += soa.prim_ns[0];
580 		memmove(soa.email, p, sizeof(uint8_t));
581 		p += sizeof(uint8_t);
582 		memmove(soa.email+1, p, soa.email[0]);
583 		p += soa.email[0];
584 		memmove(&soa.serial, p, sizeof(uint32_t));
585 		p += sizeof(uint32_t);
586 		memmove(&soa.refresh, p, sizeof(uint32_t));
587 		p += sizeof(uint32_t);
588 		memmove(&soa.retry, p, sizeof(uint32_t));
589 		p += sizeof(uint32_t);
590 		memmove(&soa.expire, p, sizeof(uint32_t));
591 		p += sizeof(uint32_t);
592 		memmove(&soa.minimum, p, sizeof(uint32_t));
593 		/* p += sizeof(uint32_t); if we wanted to read further */
594 		DEBUG(DEBUG_IPC,1, (LOG_INFO, "SOAINFO for %s %u",
595 			dname_to_string(task->zname,0),
596 			(unsigned)ntohl(soa.serial)));
597 	}
598 
599 	if(!zone) {
600 		DEBUG(DEBUG_IPC,1, (LOG_INFO, "xfrd: zone %s master zone updated",
601 			dname_to_string(task->zname,0)));
602 		notify_handle_master_zone_soainfo(xfrd->notify_zones,
603 			task->zname, soa_ptr);
604 		return;
605 	}
606 	xfrd_handle_incoming_soa(zone, soa_ptr, xfrd_time());
607 }
608 
609 static void
610 xfrd_receive_soa(int socket, int shortsoa)
611 {
612 	sig_atomic_t cmd;
613 	struct udb_base* xtask = xfrd->nsd->task[xfrd->nsd->mytask];
614 	udb_ptr last_task, t;
615 	xfrd_zone_type* zone;
616 
617 	if(!shortsoa) {
618 		/* put all expired zones into mytask */
619 		udb_ptr_init(&last_task, xtask);
620 		RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones) {
621 			if(zone->state == xfrd_zone_expired) {
622 				task_new_expire(xtask, &last_task, zone->apex, 1);
623 			}
624 		}
625 		udb_ptr_unlink(&last_task, xtask);
626 
627 		/* send RELOAD to main to give it this tasklist */
628 		task_process_sync(xtask);
629 		cmd = NSD_RELOAD;
630 		if(!write_socket(socket, &cmd,  sizeof(cmd))) {
631 			log_msg(LOG_ERR, "problems sending reload xfrdtomain: %s",
632 				strerror(errno));
633 		}
634 	}
635 
636 	/* receive RELOAD_DONE to get SOAINFO tasklist */
637 	if(block_read(&nsd, socket, &cmd, sizeof(cmd), -1) != sizeof(cmd) ||
638 		cmd != NSD_RELOAD_DONE) {
639 		if(nsd.signal_hint_shutdown)
640 			return;
641 		log_msg(LOG_ERR, "did not get start signal from main");
642 		exit(1);
643 	}
644 	if(block_read(NULL, socket, &xfrd->reload_pid, sizeof(pid_t), -1)
645 		!= sizeof(pid_t)) {
646 		log_msg(LOG_ERR, "xfrd cannot get reload_pid");
647 	}
648 
649 	/* process tasklist (SOAINFO data) */
650 	udb_ptr_unlink(xfrd->last_task, xtask);
651 	/* if shortsoa: then use my own taskdb that nsdparent filled */
652 	if(!shortsoa)
653 		xfrd->nsd->mytask = 1 - xfrd->nsd->mytask;
654 	xtask = xfrd->nsd->task[xfrd->nsd->mytask];
655 	task_remap(xtask);
656 	udb_ptr_new(&t, xtask, udb_base_get_userdata(xtask));
657 	while(!udb_ptr_is_null(&t)) {
658 		xfrd_process_soa_info_task(TASKLIST(&t));
659 	 	udb_ptr_set_rptr(&t, xtask, &TASKLIST(&t)->next);
660 	}
661 	udb_ptr_unlink(&t, xtask);
662 	task_clear(xtask);
663 	udb_ptr_init(xfrd->last_task, xfrd->nsd->task[xfrd->nsd->mytask]);
664 
665 	if(!shortsoa) {
666 		/* receive RELOAD_DONE that signals the other tasklist is
667 		 * empty, and thus xfrd can operate (can call reload and swap
668 		 * to the other, empty, tasklist) */
669 		if(block_read(NULL, socket, &cmd, sizeof(cmd), -1) !=
670 			sizeof(cmd) ||
671 			cmd != NSD_RELOAD_DONE) {
672 			log_msg(LOG_ERR, "did not get start signal 2 from "
673 				"main");
674 			exit(1);
675 		}
676 	} else {
677 		/* for shortsoa version, do expire later */
678 		/* if expire notifications, put in my task and
679 		 * schedule a reload to make sure they are processed */
680 		RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones) {
681 			if(zone->state == xfrd_zone_expired) {
682 				xfrd_send_expire_notification(zone);
683 			}
684 		}
685 	}
686 }
687 
688 void
689 xfrd_reopen_logfile(void)
690 {
691 	if (xfrd->nsd->file_rotation_ok)
692 		log_reopen(xfrd->nsd->log_filename, 0);
693 }
694 
695 void
696 xfrd_deactivate_zone(xfrd_zone_type* z)
697 {
698 	if(z->is_activated) {
699 		/* delete from activated list */
700 		if(z->activated_prev)
701 			z->activated_prev->activated_next = z->activated_next;
702 		else	xfrd->activated_first = z->activated_next;
703 		if(z->activated_next)
704 			z->activated_next->activated_prev = z->activated_prev;
705 		z->is_activated = 0;
706 	}
707 }
708 
709 void
710 xfrd_del_slave_zone(xfrd_state_type* xfrd, const dname_type* dname)
711 {
712 	xfrd_zone_type* z = (xfrd_zone_type*)rbtree_delete(xfrd->zones, dname);
713 	if(!z) return;
714 
715 	/* io */
716 	if(z->tcp_waiting) {
717 		/* delete from tcp waiting list */
718 		if(z->tcp_waiting_prev)
719 			z->tcp_waiting_prev->tcp_waiting_next =
720 				z->tcp_waiting_next;
721 		else xfrd->tcp_set->tcp_waiting_first = z->tcp_waiting_next;
722 		if(z->tcp_waiting_next)
723 			z->tcp_waiting_next->tcp_waiting_prev =
724 				z->tcp_waiting_prev;
725 		else xfrd->tcp_set->tcp_waiting_last = z->tcp_waiting_prev;
726 		z->tcp_waiting = 0;
727 	}
728 	if(z->udp_waiting) {
729 		/* delete from udp waiting list */
730 		if(z->udp_waiting_prev)
731 			z->udp_waiting_prev->udp_waiting_next =
732 				z->udp_waiting_next;
733 		else	xfrd->udp_waiting_first = z->udp_waiting_next;
734 		if(z->udp_waiting_next)
735 			z->udp_waiting_next->udp_waiting_prev =
736 				z->udp_waiting_prev;
737 		else	xfrd->udp_waiting_last = z->udp_waiting_prev;
738 		z->udp_waiting = 0;
739 	}
740 	xfrd_deactivate_zone(z);
741 	if(z->tcp_conn != -1) {
742 		xfrd_tcp_release(xfrd->tcp_set, z);
743 	} else if(z->zone_handler.ev_fd != -1 && z->event_added) {
744 		xfrd_udp_release(z);
745 	} else if(z->event_added)
746 		event_del(&z->zone_handler);
747 	if(z->msg_seq_nr)
748 		xfrd_unlink_xfrfile(xfrd->nsd, z->xfrfilenumber);
749 
750 	/* tsig */
751 	tsig_delete_record(&z->tsig, NULL);
752 
753 	/* z->dname is recycled when the zone_options is removed */
754 	region_recycle(xfrd->region, z, sizeof(*z));
755 }
756 
757 void
758 xfrd_free_namedb(struct nsd* nsd)
759 {
760 	namedb_close_udb(nsd->db);
761 	namedb_close(nsd->db);
762 	nsd->db = 0;
763 }
764 
765 static void
766 xfrd_set_timer_refresh(xfrd_zone_type* zone)
767 {
768 	time_t set_refresh;
769 	time_t set_expire;
770 	time_t set_min;
771 	time_t set;
772 	if(zone->soa_disk_acquired == 0 || zone->state != xfrd_zone_ok) {
773 		xfrd_set_timer_retry(zone);
774 		return;
775 	}
776 	/* refresh or expire timeout, whichever is earlier */
777 	set_refresh = ntohl(zone->soa_disk.refresh);
778 	if (set_refresh > (time_t)zone->zone_options->pattern->max_refresh_time)
779 		set_refresh = zone->zone_options->pattern->max_refresh_time;
780 	else if (set_refresh < (time_t)zone->zone_options->pattern->min_refresh_time)
781 		set_refresh = zone->zone_options->pattern->min_refresh_time;
782 	set_refresh += zone->soa_disk_acquired;
783 	set_expire = zone->soa_disk_acquired + ntohl(zone->soa_disk.expire);
784 	if(set_refresh < set_expire)
785 		set = set_refresh;
786 	else set = set_expire;
787 	set_min = zone->soa_disk_acquired + XFRD_LOWERBOUND_REFRESH;
788 	if(set < set_min)
789 		set = set_min;
790 	if(set < xfrd_time())
791 		set = 0;
792 	else	set -= xfrd_time();
793 	xfrd_set_timer(zone, set);
794 }
795 
796 static void
797 xfrd_set_timer_retry(xfrd_zone_type* zone)
798 {
799 	time_t set_retry;
800 	int mult;
801 	/* perform exponential backoff in all the cases */
802 	if(zone->fresh_xfr_timeout == 0)
803 		zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
804 	else {
805 		/* exponential backoff - some master data in zones is paid-for
806 		   but non-working, and will not get fixed. */
807 		zone->fresh_xfr_timeout *= 2;
808 		if(zone->fresh_xfr_timeout > XFRD_TRANSFER_TIMEOUT_MAX)
809 			zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_MAX;
810 	}
811 	/* exponential backoff multiplier, starts at 1, backs off */
812 	mult = zone->fresh_xfr_timeout / XFRD_TRANSFER_TIMEOUT_START;
813 	if(mult == 0) mult = 1;
814 
815 	/* set timer for next retry or expire timeout if earlier. */
816 	if(zone->soa_disk_acquired == 0) {
817 		/* if no information, use reasonable timeout */
818 #ifdef HAVE_ARC4RANDOM_UNIFORM
819 		xfrd_set_timer(zone, zone->fresh_xfr_timeout
820 			+ arc4random_uniform(zone->fresh_xfr_timeout));
821 #elif HAVE_ARC4RANDOM
822 		xfrd_set_timer(zone, zone->fresh_xfr_timeout
823                         + arc4random() % zone->fresh_xfr_timeout);
824 #else
825 		xfrd_set_timer(zone, zone->fresh_xfr_timeout
826 			+ random()%zone->fresh_xfr_timeout);
827 #endif
828 	} else if(zone->state == xfrd_zone_expired ||
829 		xfrd_time() + (time_t)ntohl(zone->soa_disk.retry)*mult <
830 		zone->soa_disk_acquired + (time_t)ntohl(zone->soa_disk.expire))
831 	{
832 		set_retry = ntohl(zone->soa_disk.retry);
833 		set_retry *= mult;
834 		if(set_retry > (time_t)zone->zone_options->pattern->max_retry_time)
835 			set_retry = zone->zone_options->pattern->max_retry_time;
836 		else if(set_retry < (time_t)zone->zone_options->pattern->min_retry_time)
837 			set_retry = zone->zone_options->pattern->min_retry_time;
838 		if(set_retry < XFRD_LOWERBOUND_RETRY)
839 			set_retry = XFRD_LOWERBOUND_RETRY;
840 		xfrd_set_timer(zone, set_retry);
841 	} else {
842 		set_retry = ntohl(zone->soa_disk.expire);
843 		if(set_retry < XFRD_LOWERBOUND_RETRY)
844 			xfrd_set_timer(zone, XFRD_LOWERBOUND_RETRY);
845 		else {
846 			if(zone->soa_disk_acquired + set_retry < xfrd_time())
847 				xfrd_set_timer(zone, XFRD_LOWERBOUND_RETRY);
848 			else xfrd_set_timer(zone, zone->soa_disk_acquired +
849 				set_retry - xfrd_time());
850 		}
851 	}
852 }
853 
854 void
855 xfrd_handle_zone(int ATTR_UNUSED(fd), short event, void* arg)
856 {
857 	xfrd_zone_type* zone = (xfrd_zone_type*)arg;
858 
859 	if(zone->tcp_conn != -1) {
860 		if(event == 0) /* activated, but already in TCP, nothing to do*/
861 			return;
862 		/* busy in tcp transaction: an internal error */
863 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s event tcp", zone->apex_str));
864 		xfrd_tcp_release(xfrd->tcp_set, zone);
865 		/* continue to retry; as if a timeout happened */
866 		event = EV_TIMEOUT;
867 	}
868 
869 	if((event & EV_READ)) {
870 		/* busy in udp transaction */
871 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s event udp read", zone->apex_str));
872 		xfrd_udp_read(zone);
873 		return;
874 	}
875 
876 	/* timeout */
877 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s timeout", zone->apex_str));
878 	if(zone->zone_handler.ev_fd != -1 && zone->event_added &&
879 		(event & EV_TIMEOUT)) {
880 		assert(zone->tcp_conn == -1);
881 		xfrd_udp_release(zone);
882 	}
883 
884 	if(zone->tcp_waiting) {
885 		DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s skips retry, TCP connections full",
886 			zone->apex_str));
887 		xfrd_unset_timer(zone);
888 		return;
889 	}
890 	if(zone->udp_waiting) {
891 		DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s skips retry, UDP connections full",
892 			zone->apex_str));
893 		xfrd_unset_timer(zone);
894 		return;
895 	}
896 
897 	if(zone->soa_disk_acquired)
898 	{
899 		if (zone->state != xfrd_zone_expired &&
900 			xfrd_time() >= zone->soa_disk_acquired + (time_t)ntohl(zone->soa_disk.expire)) {
901 			/* zone expired */
902 			log_msg(LOG_ERR, "xfrd: zone %s has expired", zone->apex_str);
903 			xfrd_set_zone_state(zone, xfrd_zone_expired);
904 		}
905 		else if(zone->state == xfrd_zone_ok &&
906 			xfrd_time() >= zone->soa_disk_acquired + (time_t)ntohl(zone->soa_disk.refresh)) {
907 			/* zone goes to refreshing state. */
908 			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s is refreshing", zone->apex_str));
909 			xfrd_set_zone_state(zone, xfrd_zone_refreshing);
910 		}
911 	}
912 
913 	/* only make a new request if no request is running (UDPorTCP) */
914 	if(zone->zone_handler.ev_fd == -1 && zone->tcp_conn == -1) {
915 		/* make a new request */
916 		xfrd_make_request(zone);
917 	}
918 }
919 
920 void
921 xfrd_make_request(xfrd_zone_type* zone)
922 {
923 	if(zone->next_master != -1) {
924 		/* we are told to use this next master */
925 		DEBUG(DEBUG_XFRD,1, (LOG_INFO,
926 			"xfrd zone %s use master %i",
927 			zone->apex_str, zone->next_master));
928 		zone->master_num = zone->next_master;
929 		zone->master = acl_find_num(zone->zone_options->pattern->
930 			request_xfr, zone->master_num);
931 		/* if there is no next master, fallback to use the first one */
932 		if(!zone->master) {
933 			zone->master = zone->zone_options->pattern->request_xfr;
934 			zone->master_num = 0;
935 		}
936 		/* fallback to cycle master */
937 		zone->next_master = -1;
938 		zone->round_num = 0; /* fresh set of retries after notify */
939 	} else {
940 		/* cycle master */
941 
942 		if(zone->round_num != -1 && zone->master && zone->master->next)
943 		{
944 			/* try the next master */
945 			zone->master = zone->master->next;
946 			zone->master_num++;
947 		} else {
948 			/* start a new round */
949 			zone->master = zone->zone_options->pattern->request_xfr;
950 			zone->master_num = 0;
951 			zone->round_num++;
952 		}
953 		if(zone->round_num >= XFRD_MAX_ROUNDS) {
954 			/* tried all servers that many times, wait */
955 			zone->round_num = -1;
956 			xfrd_set_timer_retry(zone);
957 			DEBUG(DEBUG_XFRD,1, (LOG_INFO,
958 				"xfrd zone %s makereq wait_retry, rd %d mr %d nx %d",
959 				zone->apex_str, zone->round_num, zone->master_num, zone->next_master));
960                        zone->multi_master_first_master = -1;
961                        return;
962                }
963 	}
964 
965 	/* multi-master-check */
966 	if(zone->zone_options->pattern->multi_master_check) {
967 		if(zone->multi_master_first_master == zone->master_num &&
968 			zone->round_num > 0 &&
969 			zone->state != xfrd_zone_expired) {
970 			/* tried all servers and update zone */
971 			if(zone->multi_master_update_check >= 0) {
972 				VERBOSITY(2, (LOG_INFO, "xfrd: multi master "
973 					"check: zone %s completed transfers",
974 					zone->apex_str));
975 			}
976 			zone->round_num = -1; /* next try start anew */
977 			zone->multi_master_first_master = -1;
978 			xfrd_set_timer_refresh(zone);
979 			return;
980 		}
981 		if(zone->multi_master_first_master < 0) {
982 			zone->multi_master_first_master = zone->master_num;
983 			zone->multi_master_update_check = -1;
984 		}
985 	}
986 
987 	/* cache ixfr_disabled only for XFRD_NO_IXFR_CACHE time */
988 	if (zone->master->ixfr_disabled &&
989 	   (zone->master->ixfr_disabled + XFRD_NO_IXFR_CACHE) <= time(NULL)) {
990 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "clear negative caching ixfr "
991 						"disabled for master %s num "
992 						"%d ",
993 			zone->master->ip_address_spec, zone->master_num));
994 		zone->master->ixfr_disabled = 0;
995 	}
996 
997 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s make request round %d mr %d nx %d",
998 		zone->apex_str, zone->round_num, zone->master_num, zone->next_master));
999 	/* perform xfr request */
1000 	if (!zone->master->use_axfr_only && zone->soa_disk_acquired > 0 &&
1001 		!zone->master->ixfr_disabled) {
1002 
1003 		if (zone->master->allow_udp) {
1004 			xfrd_set_timer(zone, XFRD_UDP_TIMEOUT);
1005 			xfrd_udp_obtain(zone);
1006 		}
1007 		else { /* doing 3 rounds of IXFR/TCP might not be useful */
1008 			xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1009 			xfrd_tcp_obtain(xfrd->tcp_set, zone);
1010 		}
1011 	}
1012 	else if (zone->master->use_axfr_only || zone->soa_disk_acquired <= 0) {
1013 		xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1014 		xfrd_tcp_obtain(xfrd->tcp_set, zone);
1015 	}
1016 	else if (zone->master->ixfr_disabled) {
1017 		if (zone->zone_options->pattern->allow_axfr_fallback) {
1018 			xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1019 			xfrd_tcp_obtain(xfrd->tcp_set, zone);
1020 		} else {
1021 			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s axfr "
1022 				"fallback not allowed, skipping master %s.",
1023 				zone->apex_str, zone->master->ip_address_spec));
1024 		}
1025 	}
1026 }
1027 
1028 static void
1029 xfrd_udp_obtain(xfrd_zone_type* zone)
1030 {
1031 	assert(zone->udp_waiting == 0);
1032 	if(zone->tcp_conn != -1) {
1033 		/* no tcp and udp at the same time */
1034 		xfrd_tcp_release(xfrd->tcp_set, zone);
1035 	}
1036 	if(xfrd->udp_use_num < XFRD_MAX_UDP) {
1037 		int fd;
1038 		xfrd->udp_use_num++;
1039 		fd = xfrd_send_ixfr_request_udp(zone);
1040 		if(fd == -1)
1041 			xfrd->udp_use_num--;
1042 		else {
1043 			if(zone->event_added)
1044 				event_del(&zone->zone_handler);
1045 			memset(&zone->zone_handler, 0,
1046 				sizeof(zone->zone_handler));
1047 			event_set(&zone->zone_handler, fd,
1048 				EV_PERSIST|EV_READ|EV_TIMEOUT,
1049 				xfrd_handle_zone, zone);
1050 			if(event_base_set(xfrd->event_base, &zone->zone_handler) != 0)
1051 				log_msg(LOG_ERR, "xfrd udp: event_base_set failed");
1052 			if(event_add(&zone->zone_handler, &zone->timeout) != 0)
1053 				log_msg(LOG_ERR, "xfrd udp: event_add failed");
1054 			zone->zone_handler_flags=EV_PERSIST|EV_READ|EV_TIMEOUT;
1055 			zone->event_added = 1;
1056 		}
1057 		return;
1058 	}
1059 	/* queue the zone as last */
1060 	zone->udp_waiting = 1;
1061 	zone->udp_waiting_next = NULL;
1062 	zone->udp_waiting_prev = xfrd->udp_waiting_last;
1063 	if(!xfrd->udp_waiting_first)
1064 		xfrd->udp_waiting_first = zone;
1065 	if(xfrd->udp_waiting_last)
1066 		xfrd->udp_waiting_last->udp_waiting_next = zone;
1067 	xfrd->udp_waiting_last = zone;
1068 	xfrd_unset_timer(zone);
1069 }
1070 
1071 time_t
1072 xfrd_time()
1073 {
1074 	if(!xfrd->got_time) {
1075 		xfrd->current_time = time(0);
1076 		xfrd->got_time = 1;
1077 	}
1078 	return xfrd->current_time;
1079 }
1080 
1081 void
1082 xfrd_copy_soa(xfrd_soa_type* soa, rr_type* rr)
1083 {
1084 	const uint8_t* rr_ns_wire = dname_name(domain_dname(rdata_atom_domain(rr->rdatas[0])));
1085 	uint8_t rr_ns_len = domain_dname(rdata_atom_domain(rr->rdatas[0]))->name_size;
1086 	const uint8_t* rr_em_wire = dname_name(domain_dname(rdata_atom_domain(rr->rdatas[1])));
1087 	uint8_t rr_em_len = domain_dname(rdata_atom_domain(rr->rdatas[1]))->name_size;
1088 
1089 	if(rr->type != TYPE_SOA || rr->rdata_count != 7) {
1090 		log_msg(LOG_ERR, "xfrd: copy_soa called with bad rr, type %d rrs %u.",
1091 			rr->type, rr->rdata_count);
1092 		return;
1093 	}
1094 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: copy_soa rr, type %d rrs %u, ttl %u.",
1095 			(int)rr->type, (unsigned)rr->rdata_count, (unsigned)rr->ttl));
1096 	soa->type = htons(rr->type);
1097 	soa->klass = htons(rr->klass);
1098 	soa->ttl = htonl(rr->ttl);
1099 	soa->rdata_count = htons(rr->rdata_count);
1100 
1101 	/* copy dnames */
1102 	soa->prim_ns[0] = rr_ns_len;
1103 	memcpy(soa->prim_ns+1, rr_ns_wire, rr_ns_len);
1104 	soa->email[0] = rr_em_len;
1105 	memcpy(soa->email+1, rr_em_wire, rr_em_len);
1106 
1107 	/* already in network format */
1108 	memcpy(&soa->serial, rdata_atom_data(rr->rdatas[2]), sizeof(uint32_t));
1109 	memcpy(&soa->refresh, rdata_atom_data(rr->rdatas[3]), sizeof(uint32_t));
1110 	memcpy(&soa->retry, rdata_atom_data(rr->rdatas[4]), sizeof(uint32_t));
1111 	memcpy(&soa->expire, rdata_atom_data(rr->rdatas[5]), sizeof(uint32_t));
1112 	memcpy(&soa->minimum, rdata_atom_data(rr->rdatas[6]), sizeof(uint32_t));
1113 	DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1114 		"xfrd: copy_soa rr, serial %u refresh %u retry %u expire %u",
1115 		(unsigned)ntohl(soa->serial), (unsigned)ntohl(soa->refresh),
1116 		(unsigned)ntohl(soa->retry), (unsigned)ntohl(soa->expire)));
1117 }
1118 
1119 static void
1120 xfrd_set_zone_state(xfrd_zone_type* zone, enum xfrd_zone_state s)
1121 {
1122 	if(s != zone->state) {
1123 		enum xfrd_zone_state old = zone->state;
1124 		zone->state = s;
1125 		if((s == xfrd_zone_expired || old == xfrd_zone_expired)
1126 			&& s!=old) {
1127 			xfrd_send_expire_notification(zone);
1128 		}
1129 	}
1130 }
1131 
1132 void
1133 xfrd_set_refresh_now(xfrd_zone_type* zone)
1134 {
1135 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd zone %s is activated, state %d",
1136 		zone->apex_str, zone->state));
1137 	if(!zone->is_activated) {
1138 		/* push onto list */
1139 		zone->activated_prev = 0;
1140 		zone->activated_next = xfrd->activated_first;
1141 		if(xfrd->activated_first)
1142 			xfrd->activated_first->activated_prev = zone;
1143 		xfrd->activated_first = zone;
1144 		zone->is_activated = 1;
1145 	}
1146 }
1147 
1148 void
1149 xfrd_unset_timer(xfrd_zone_type* zone)
1150 {
1151 	assert(zone->zone_handler.ev_fd == -1);
1152 	if(zone->event_added)
1153 		event_del(&zone->zone_handler);
1154 	zone->zone_handler_flags = 0;
1155 	zone->event_added = 0;
1156 }
1157 
1158 void
1159 xfrd_set_timer(xfrd_zone_type* zone, time_t t)
1160 {
1161 	int fd = zone->zone_handler.ev_fd;
1162 	int fl = ((fd == -1)?EV_TIMEOUT:zone->zone_handler_flags);
1163 	/* randomize the time, within 90%-100% of original */
1164 	/* not later so zones cannot expire too late */
1165 	/* only for times far in the future */
1166 	if(t > 10) {
1167 		time_t base = t*9/10;
1168 #ifdef HAVE_ARC4RANDOM_UNIFORM
1169 		t = base + arc4random_uniform(t-base);
1170 #elif HAVE_ARC4RANDOM
1171 		t = base + arc4random() % (t-base);
1172 #else
1173 		t = base + random()%(t-base);
1174 #endif
1175 	}
1176 
1177 	/* keep existing flags and fd, but re-add with timeout */
1178 	if(zone->event_added)
1179 		event_del(&zone->zone_handler);
1180 	else	fd = -1;
1181 	zone->timeout.tv_sec = t;
1182 	zone->timeout.tv_usec = 0;
1183 	memset(&zone->zone_handler, 0, sizeof(zone->zone_handler));
1184 	event_set(&zone->zone_handler, fd, fl, xfrd_handle_zone, zone);
1185 	if(event_base_set(xfrd->event_base, &zone->zone_handler) != 0)
1186 		log_msg(LOG_ERR, "xfrd timer: event_base_set failed");
1187 	if(event_add(&zone->zone_handler, &zone->timeout) != 0)
1188 		log_msg(LOG_ERR, "xfrd timer: event_add failed");
1189 	zone->zone_handler_flags = fl;
1190 	zone->event_added = 1;
1191 }
1192 
1193 void
1194 xfrd_handle_incoming_soa(xfrd_zone_type* zone,
1195 	xfrd_soa_type* soa, time_t acquired)
1196 {
1197 	if(soa == NULL) {
1198 		/* nsd no longer has a zone in memory */
1199 		zone->soa_nsd_acquired = 0;
1200 		xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1201 		xfrd_set_refresh_now(zone);
1202 		return;
1203 	}
1204 	if(zone->soa_nsd_acquired && soa->serial == zone->soa_nsd.serial)
1205 		return;
1206 
1207 	if(zone->soa_disk_acquired && soa->serial == zone->soa_disk.serial)
1208 	{
1209 		/* soa in disk has been loaded in memory */
1210 		log_msg(LOG_INFO, "zone %s serial %u is updated to %u",
1211 			zone->apex_str, (unsigned)ntohl(zone->soa_nsd.serial),
1212 			(unsigned)ntohl(soa->serial));
1213 		zone->soa_nsd = zone->soa_disk;
1214 		zone->soa_nsd_acquired = zone->soa_disk_acquired;
1215 		xfrd->write_zonefile_needed = 1;
1216 		/* reset exponential backoff, we got a normal timer now */
1217 		zone->fresh_xfr_timeout = 0;
1218 		if(xfrd_time() - zone->soa_disk_acquired
1219 			< (time_t)ntohl(zone->soa_disk.refresh))
1220 		{
1221 			/* zone ok, wait for refresh time */
1222 			xfrd_set_zone_state(zone, xfrd_zone_ok);
1223 			zone->round_num = -1;
1224 			xfrd_set_timer_refresh(zone);
1225 		} else if(xfrd_time() - zone->soa_disk_acquired
1226 			< (time_t)ntohl(zone->soa_disk.expire))
1227 		{
1228 			/* zone refreshing */
1229 			xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1230 			xfrd_set_refresh_now(zone);
1231 		}
1232 		if(xfrd_time() - zone->soa_disk_acquired
1233 			>= (time_t)ntohl(zone->soa_disk.expire)) {
1234 			/* zone expired */
1235 			xfrd_set_zone_state(zone, xfrd_zone_expired);
1236 			xfrd_set_refresh_now(zone);
1237 		}
1238 
1239 		if(zone->soa_notified_acquired != 0 &&
1240 			(zone->soa_notified.serial == 0 ||
1241 		   	compare_serial(ntohl(zone->soa_disk.serial),
1242 				ntohl(zone->soa_notified.serial)) >= 0))
1243 		{	/* read was in response to this notification */
1244 			zone->soa_notified_acquired = 0;
1245 		}
1246 		if(zone->soa_notified_acquired && zone->state == xfrd_zone_ok)
1247 		{
1248 			/* refresh because of notification */
1249 			xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1250 			xfrd_set_refresh_now(zone);
1251 		}
1252 		xfrd_send_notify(xfrd->notify_zones, zone->apex, &zone->soa_nsd);
1253 		return;
1254 	}
1255 
1256 	/* user must have manually provided zone data */
1257 	DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1258 		"xfrd: zone %s serial %u from zonefile. refreshing",
1259 		zone->apex_str, (unsigned)ntohl(soa->serial)));
1260 	zone->soa_nsd = *soa;
1261 	zone->soa_disk = *soa;
1262 	zone->soa_nsd_acquired = acquired;
1263 	zone->soa_disk_acquired = acquired;
1264 	if(zone->soa_notified_acquired != 0 &&
1265 		(zone->soa_notified.serial == 0 ||
1266 	   	compare_serial(ntohl(zone->soa_disk.serial),
1267 			ntohl(zone->soa_notified.serial)) >= 0))
1268 	{	/* user provided in response to this notification */
1269 		zone->soa_notified_acquired = 0;
1270 	}
1271 	xfrd_set_zone_state(zone, xfrd_zone_refreshing);
1272 	xfrd_set_refresh_now(zone);
1273 	xfrd_send_notify(xfrd->notify_zones, zone->apex, &zone->soa_nsd);
1274 }
1275 
1276 void
1277 xfrd_send_expire_notification(xfrd_zone_type* zone)
1278 {
1279 	task_new_expire(xfrd->nsd->task[xfrd->nsd->mytask], xfrd->last_task,
1280 		zone->apex, zone->state == xfrd_zone_expired);
1281 	xfrd_set_reload_timeout();
1282 }
1283 
1284 int
1285 xfrd_udp_read_packet(buffer_type* packet, int fd, struct sockaddr* src,
1286 	socklen_t* srclen)
1287 {
1288 	ssize_t received;
1289 
1290 	/* read the data */
1291 	buffer_clear(packet);
1292 	received = recvfrom(fd, buffer_begin(packet), buffer_remaining(packet),
1293 		0, src, srclen);
1294 	if(received == -1) {
1295 		log_msg(LOG_ERR, "xfrd: recvfrom failed: %s",
1296 			strerror(errno));
1297 		return 0;
1298 	}
1299 	buffer_set_limit(packet, received);
1300 	return 1;
1301 }
1302 
1303 void
1304 xfrd_udp_release(xfrd_zone_type* zone)
1305 {
1306 	assert(zone->udp_waiting == 0);
1307 	if(zone->event_added)
1308 		event_del(&zone->zone_handler);
1309 	if(zone->zone_handler.ev_fd != -1) {
1310 		close(zone->zone_handler.ev_fd);
1311 	}
1312 	zone->zone_handler.ev_fd = -1;
1313 	zone->zone_handler_flags = 0;
1314 	zone->event_added = 0;
1315 	/* see if there are waiting zones */
1316 	if(xfrd->udp_use_num == XFRD_MAX_UDP)
1317 	{
1318 		while(xfrd->udp_waiting_first) {
1319 			/* snip off waiting list */
1320 			xfrd_zone_type* wz = xfrd->udp_waiting_first;
1321 			assert(wz->udp_waiting);
1322 			wz->udp_waiting = 0;
1323 			xfrd->udp_waiting_first = wz->udp_waiting_next;
1324 			if(wz->udp_waiting_next)
1325 				wz->udp_waiting_next->udp_waiting_prev = NULL;
1326 			if(xfrd->udp_waiting_last == wz)
1327 				xfrd->udp_waiting_last = NULL;
1328 			/* see if this zone needs udp connection */
1329 			if(wz->tcp_conn == -1) {
1330 				int fd = xfrd_send_ixfr_request_udp(wz);
1331 				if(fd != -1) {
1332 					if(wz->event_added)
1333 						event_del(&wz->zone_handler);
1334 					memset(&wz->zone_handler, 0,
1335 						sizeof(wz->zone_handler));
1336 					event_set(&wz->zone_handler, fd,
1337 						EV_READ|EV_TIMEOUT|EV_PERSIST,
1338 						xfrd_handle_zone, wz);
1339 					if(event_base_set(xfrd->event_base,
1340 						&wz->zone_handler) != 0)
1341 						log_msg(LOG_ERR, "cannot set event_base for ixfr");
1342 					if(event_add(&wz->zone_handler, &wz->timeout) != 0)
1343 						log_msg(LOG_ERR, "cannot add event for ixfr");
1344 					wz->zone_handler_flags = EV_READ|EV_TIMEOUT|EV_PERSIST;
1345 					wz->event_added = 1;
1346 					return;
1347 				} else {
1348 					/* make this zone do something with
1349 					 * this failure to act */
1350 					xfrd_set_refresh_now(wz);
1351 				}
1352 			}
1353 		}
1354 	}
1355 	/* no waiting zones */
1356 	if(xfrd->udp_use_num > 0)
1357 		xfrd->udp_use_num--;
1358 }
1359 
1360 /** disable ixfr for master */
1361 void
1362 xfrd_disable_ixfr(xfrd_zone_type* zone)
1363 {
1364 	if(!(zone->master->ixfr_disabled &&
1365 		(zone->master->ixfr_disabled + XFRD_NO_IXFR_CACHE) <= time(NULL))) {
1366 		/* start new round, with IXFR disabled */
1367 		zone->round_num = 0;
1368 		zone->next_master = zone->master_num;
1369 	}
1370 	zone->master->ixfr_disabled = time(NULL);
1371 }
1372 
1373 static void
1374 xfrd_udp_read(xfrd_zone_type* zone)
1375 {
1376 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s read udp data", zone->apex_str));
1377 	if(!xfrd_udp_read_packet(xfrd->packet, zone->zone_handler.ev_fd,
1378 		NULL, NULL)) {
1379 		zone->master->bad_xfr_count++;
1380 		if (zone->master->bad_xfr_count > 2) {
1381 			xfrd_disable_ixfr(zone);
1382 			zone->master->bad_xfr_count = 0;
1383 		}
1384 		/* drop packet */
1385 		xfrd_udp_release(zone);
1386 		/* query next server */
1387 		xfrd_make_request(zone);
1388 		return;
1389 	}
1390 	switch(xfrd_handle_received_xfr_packet(zone, xfrd->packet)) {
1391 		case xfrd_packet_tcp:
1392 			xfrd_set_timer(zone, xfrd->tcp_set->tcp_timeout);
1393 			xfrd_udp_release(zone);
1394 			xfrd_tcp_obtain(xfrd->tcp_set, zone);
1395 			break;
1396 		case xfrd_packet_transfer:
1397 			if(zone->zone_options->pattern->multi_master_check) {
1398 				xfrd_udp_release(zone);
1399 				xfrd_make_request(zone);
1400 				break;
1401 			}
1402 			/* fallthrough */
1403 		case xfrd_packet_newlease:
1404 			/* nothing more to do */
1405 			assert(zone->round_num == -1);
1406 			xfrd_udp_release(zone);
1407 			break;
1408 		case xfrd_packet_notimpl:
1409 			xfrd_disable_ixfr(zone);
1410 			/* drop packet */
1411 			xfrd_udp_release(zone);
1412 			/* query next server */
1413 			xfrd_make_request(zone);
1414 			break;
1415 		case xfrd_packet_more:
1416 		case xfrd_packet_drop:
1417 			/* drop packet */
1418 			xfrd_udp_release(zone);
1419 			/* query next server */
1420 			xfrd_make_request(zone);
1421 			break;
1422 		case xfrd_packet_bad:
1423 		default:
1424 			zone->master->bad_xfr_count++;
1425 			if (zone->master->bad_xfr_count > 2) {
1426 				xfrd_disable_ixfr(zone);
1427 				zone->master->bad_xfr_count = 0;
1428 			}
1429 			/* drop packet */
1430 			xfrd_udp_release(zone);
1431 			/* query next server */
1432 			xfrd_make_request(zone);
1433 			break;
1434 	}
1435 }
1436 
1437 int
1438 xfrd_send_udp(struct acl_options* acl, buffer_type* packet,
1439 	struct acl_options* ifc)
1440 {
1441 #ifdef INET6
1442 	struct sockaddr_storage to;
1443 #else
1444 	struct sockaddr_in to;
1445 #endif /* INET6 */
1446 	int fd, family;
1447 
1448 	/* this will set the remote port to acl->port or TCP_PORT */
1449 	socklen_t to_len = xfrd_acl_sockaddr_to(acl, &to);
1450 
1451 	/* get the address family of the remote host */
1452 	if(acl->is_ipv6) {
1453 #ifdef INET6
1454 		family = PF_INET6;
1455 #else
1456 		return -1;
1457 #endif /* INET6 */
1458 	} else {
1459 		family = PF_INET;
1460 	}
1461 
1462 	fd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
1463 	if(fd == -1) {
1464 		log_msg(LOG_ERR, "xfrd: cannot create udp socket to %s: %s",
1465 			acl->ip_address_spec, strerror(errno));
1466 		return -1;
1467 	}
1468 
1469 	/* bind it */
1470 	if (!xfrd_bind_local_interface(fd, ifc, acl, 0)) {
1471 		log_msg(LOG_ERR, "xfrd: cannot bind outgoing interface '%s' to "
1472 				 "udp socket: No matching ip addresses found",
1473 			ifc->ip_address_spec);
1474 		close(fd);
1475 		return -1;
1476 	}
1477 
1478 	/* send it (udp) */
1479 	if(sendto(fd,
1480 		buffer_current(packet),
1481 		buffer_remaining(packet), 0,
1482 		(struct sockaddr*)&to, to_len) == -1)
1483 	{
1484 		log_msg(LOG_ERR, "xfrd: sendto %s failed %s",
1485 			acl->ip_address_spec, strerror(errno));
1486 		close(fd);
1487 		return -1;
1488 	}
1489 	return fd;
1490 }
1491 
1492 int
1493 xfrd_bind_local_interface(int sockd, struct acl_options* ifc,
1494 	struct acl_options* acl, int tcp)
1495 {
1496 #ifdef SO_LINGER
1497 	struct linger linger = {1, 0};
1498 #endif
1499 	socklen_t frm_len;
1500 #ifdef INET6
1501 	struct sockaddr_storage frm;
1502 #else
1503 	struct sockaddr_in frm;
1504 #endif /* INET6 */
1505 	int ret = 1;
1506 
1507 	if (!ifc) /* no outgoing interface set */
1508 		return 1;
1509 
1510 	while (ifc) {
1511 		if (ifc->is_ipv6 != acl->is_ipv6) {
1512 			/* check if we have a matching address family */
1513 			ifc = ifc->next;
1514 			continue;
1515 		}
1516 
1517 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: bind() %s to %s socket",
1518 			ifc->ip_address_spec, tcp? "tcp":"udp"));
1519 		ret = 0;
1520 		frm_len = xfrd_acl_sockaddr_frm(ifc, &frm);
1521 
1522 		if (tcp) {
1523 #ifdef SO_REUSEADDR
1524 			if (setsockopt(sockd, SOL_SOCKET, SO_REUSEADDR, &frm,
1525 				frm_len) < 0) {
1526 				VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt "
1527 			     "SO_REUSEADDR failed: %s", strerror(errno)));
1528 			}
1529 #else
1530 			VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt SO_REUSEADDR "
1531 			     "failed: SO_REUSEADDR not defined"));
1532 #endif /* SO_REUSEADDR */
1533 
1534 			if (ifc->port != 0) {
1535 #ifdef SO_LINGER
1536 				if (setsockopt(sockd, SOL_SOCKET, SO_LINGER,
1537 					&linger, sizeof(linger)) < 0) {
1538 					VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt "
1539 				     "SO_LINGER failed: %s", strerror(errno)));
1540 				}
1541 #else
1542 				VERBOSITY(2, (LOG_WARNING, "xfrd: setsockopt SO_LINGER "
1543 					"failed: SO_LINGER not defined"));
1544 #endif /* SO_LINGER */
1545 			}
1546 		}
1547 
1548 		/* found one */
1549 		if(bind(sockd, (struct sockaddr*)&frm, frm_len) >= 0) {
1550 			DEBUG(DEBUG_XFRD,2, (LOG_INFO, "xfrd: bind() %s to %s "
1551 						       "socket was successful",
1552 			ifc->ip_address_spec, tcp? "tcp":"udp"));
1553 			return 1;
1554 		}
1555 
1556 		DEBUG(DEBUG_XFRD,2, (LOG_INFO, "xfrd: bind() %s to %s socket"
1557 					       "failed: %s",
1558 			ifc->ip_address_spec, tcp? "tcp":"udp",
1559 			strerror(errno)));
1560 
1561 		log_msg(LOG_WARNING, "xfrd: could not bind source address:port to "
1562 		     "socket: %s", strerror(errno));
1563 		/* try another */
1564 		ifc = ifc->next;
1565 	}
1566 	return ret;
1567 }
1568 
1569 void
1570 xfrd_tsig_sign_request(buffer_type* packet, tsig_record_type* tsig,
1571 	struct acl_options* acl)
1572 {
1573 	tsig_algorithm_type* algo;
1574 	assert(acl->key_options && acl->key_options->tsig_key);
1575 	algo = tsig_get_algorithm_by_name(acl->key_options->algorithm);
1576 	if(!algo) {
1577 		log_msg(LOG_ERR, "tsig unknown algorithm %s",
1578 			acl->key_options->algorithm);
1579 		return;
1580 	}
1581 	assert(algo);
1582 	tsig_init_record(tsig, algo, acl->key_options->tsig_key);
1583 	tsig_init_query(tsig, ID(packet));
1584 	tsig_prepare(tsig);
1585 	tsig_update(tsig, packet, buffer_position(packet));
1586 	tsig_sign(tsig);
1587 	tsig_append_rr(tsig, packet);
1588 	ARCOUNT_SET(packet, ARCOUNT(packet) + 1);
1589 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "appending tsig to packet"));
1590 	/* prepare for validating tsigs */
1591 	tsig_prepare(tsig);
1592 }
1593 
1594 static int
1595 xfrd_send_ixfr_request_udp(xfrd_zone_type* zone)
1596 {
1597 	int fd;
1598 
1599 	/* make sure we have a master to query the ixfr request to */
1600 	assert(zone->master);
1601 
1602 	if(zone->tcp_conn != -1) {
1603 		/* tcp is using the zone_handler.fd */
1604 		log_msg(LOG_ERR, "xfrd: %s tried to send udp whilst tcp engaged",
1605 			zone->apex_str);
1606 		return -1;
1607 	}
1608 	xfrd_setup_packet(xfrd->packet, TYPE_IXFR, CLASS_IN, zone->apex,
1609 		qid_generate());
1610 	zone->query_id = ID(xfrd->packet);
1611 	/* delete old xfr file? */
1612 	if(zone->msg_seq_nr)
1613 		xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
1614 	zone->msg_seq_nr = 0;
1615 	zone->msg_rr_count = 0;
1616 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "sent query with ID %d", zone->query_id));
1617         NSCOUNT_SET(xfrd->packet, 1);
1618 	xfrd_write_soa_buffer(xfrd->packet, zone->apex, &zone->soa_disk);
1619 	/* if we have tsig keys, sign the ixfr query */
1620 	if(zone->master->key_options && zone->master->key_options->tsig_key) {
1621 		xfrd_tsig_sign_request(xfrd->packet, &zone->tsig, zone->master);
1622 	}
1623 	buffer_flip(xfrd->packet);
1624 	xfrd_set_timer(zone, XFRD_UDP_TIMEOUT);
1625 
1626 	if((fd = xfrd_send_udp(zone->master, xfrd->packet,
1627 		zone->zone_options->pattern->outgoing_interface)) == -1)
1628 		return -1;
1629 
1630 	DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1631 		"xfrd sent udp request for ixfr=%u for zone %s to %s",
1632 		(unsigned)ntohl(zone->soa_disk.serial),
1633 		zone->apex_str, zone->master->ip_address_spec));
1634 	return fd;
1635 }
1636 
1637 static int xfrd_parse_soa_info(buffer_type* packet, xfrd_soa_type* soa)
1638 {
1639 	if(!buffer_available(packet, 10))
1640 		return 0;
1641 	soa->type = htons(buffer_read_u16(packet));
1642 	soa->klass = htons(buffer_read_u16(packet));
1643 	soa->ttl = htonl(buffer_read_u32(packet));
1644 	if(ntohs(soa->type) != TYPE_SOA || ntohs(soa->klass) != CLASS_IN)
1645 	{
1646 		return 0;
1647 	}
1648 
1649 	if(!buffer_available(packet, buffer_read_u16(packet)) /* rdata length */ ||
1650 		!(soa->prim_ns[0] = dname_make_wire_from_packet(soa->prim_ns+1, packet, 1)) ||
1651 		!(soa->email[0] = dname_make_wire_from_packet(soa->email+1, packet, 1)))
1652 	{
1653 		return 0;
1654 	}
1655 	soa->rdata_count = 7; /* rdata in SOA */
1656 	soa->serial = htonl(buffer_read_u32(packet));
1657 	soa->refresh = htonl(buffer_read_u32(packet));
1658 	soa->retry = htonl(buffer_read_u32(packet));
1659 	soa->expire = htonl(buffer_read_u32(packet));
1660 	soa->minimum = htonl(buffer_read_u32(packet));
1661 
1662 	return 1;
1663 }
1664 
1665 
1666 /*
1667  * Check the RRs in an IXFR/AXFR reply.
1668  * returns 0 on error, 1 on correct parseable packet.
1669  * done = 1 if the last SOA in an IXFR/AXFR has been seen.
1670  * soa then contains that soa info.
1671  * (soa contents is modified by the routine)
1672  */
1673 static int
1674 xfrd_xfr_check_rrs(xfrd_zone_type* zone, buffer_type* packet, size_t count,
1675 	int *done, xfrd_soa_type* soa, region_type* temp)
1676 {
1677 	/* first RR has already been checked */
1678 	uint32_t tmp_serial = 0;
1679 	uint16_t type, rrlen;
1680 	size_t i, soapos, mempos;
1681 	const dname_type* dname;
1682 	domain_table_type* owners;
1683 	rdata_atom_type* rdatas;
1684 
1685 	for(i=0; i<count; ++i,++zone->msg_rr_count)
1686 	{
1687 		if (*done) {
1688 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr has "
1689 				"trailing garbage", zone->apex_str));
1690 			return 0;
1691 		}
1692 		region_free_all(temp);
1693 		owners = domain_table_create(temp);
1694 		/* check the dname for errors */
1695 		dname = dname_make_from_packet(temp, packet, 1, 1);
1696 		if(!dname) {
1697 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr unable "
1698 				"to parse owner name", zone->apex_str));
1699 			return 0;
1700 		}
1701 		if(!buffer_available(packet, 10)) {
1702 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr hdr "
1703 				"too small", zone->apex_str));
1704 			return 0;
1705 		}
1706 		soapos = buffer_position(packet);
1707 		type = buffer_read_u16(packet);
1708 		(void)buffer_read_u16(packet); /* class */
1709 		(void)buffer_read_u32(packet); /* ttl */
1710 		rrlen = buffer_read_u16(packet);
1711 		if(!buffer_available(packet, rrlen)) {
1712 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr pkt "
1713 				"too small", zone->apex_str));
1714 			return 0;
1715 		}
1716 		mempos = buffer_position(packet);
1717 		if(rdata_wireformat_to_rdata_atoms(temp, owners, type, rrlen,
1718 			packet, &rdatas) == -1) {
1719 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr unable "
1720 				"to parse rdata", zone->apex_str));
1721 			return 0;
1722 		}
1723 		if(type == TYPE_SOA) {
1724 			/* check the SOAs */
1725 			buffer_set_position(packet, soapos);
1726 			if(!xfrd_parse_soa_info(packet, soa)) {
1727 				DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1728 					"unable to parse soainfo", zone->apex_str));
1729 				return 0;
1730 			}
1731 			if(zone->msg_rr_count == 1 &&
1732 				ntohl(soa->serial) != zone->msg_new_serial) {
1733 				/* 2nd RR is SOA with lower serial, this is an IXFR */
1734 				zone->msg_is_ixfr = 1;
1735 				if(!zone->soa_disk_acquired) {
1736 					DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1737 						"got ixfr but need axfr", zone->apex_str));
1738 					return 0; /* got IXFR but need AXFR */
1739 				}
1740 				if(ntohl(soa->serial) != ntohl(zone->soa_disk.serial)) {
1741 					DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1742 						"bad start serial", zone->apex_str));
1743 					return 0; /* bad start serial in IXFR */
1744 				}
1745 				zone->msg_old_serial = ntohl(soa->serial);
1746 				tmp_serial = ntohl(soa->serial);
1747 			}
1748 			else if(ntohl(soa->serial) == zone->msg_new_serial) {
1749 				/* saw another SOA of new serial. */
1750 				if(zone->msg_is_ixfr == 1) {
1751 					zone->msg_is_ixfr = 2; /* seen middle SOA in ixfr */
1752 				} else {
1753 					/* 2nd SOA for AXFR or 3rd newSOA for IXFR */
1754 					*done = 1;
1755 				}
1756 			}
1757 			else if (zone->msg_is_ixfr) {
1758 				/* some additional checks */
1759 				if(ntohl(soa->serial) > zone->msg_new_serial) {
1760 					DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1761 						"bad middle serial", zone->apex_str));
1762 					return 0; /* bad middle serial in IXFR */
1763 				}
1764 				if(ntohl(soa->serial) < tmp_serial) {
1765 					DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s xfr "
1766 						"serial decreasing not allowed", zone->apex_str));
1767 					return 0; /* middle serial decreases in IXFR */
1768 				}
1769 				/* serial ok, update tmp serial */
1770 				tmp_serial = ntohl(soa->serial);
1771 			}
1772 		}
1773 		buffer_set_position(packet, mempos);
1774 		buffer_skip(packet, rrlen);
1775 	}
1776 	/* packet seems to have a valid DNS RR structure */
1777 	return 1;
1778 }
1779 
1780 static int
1781 xfrd_xfr_process_tsig(xfrd_zone_type* zone, buffer_type* packet)
1782 {
1783 	int have_tsig = 0;
1784 	assert(zone && zone->master && zone->master->key_options
1785 		&& zone->master->key_options->tsig_key && packet);
1786 	if(!tsig_find_rr(&zone->tsig, packet)) {
1787 		log_msg(LOG_ERR, "xfrd: zone %s, from %s: malformed tsig RR",
1788 			zone->apex_str, zone->master->ip_address_spec);
1789 		return 0;
1790 	}
1791 	if(zone->tsig.status == TSIG_OK) {
1792 		have_tsig = 1;
1793 		if (zone->tsig.error_code != TSIG_ERROR_NOERROR) {
1794 			log_msg(LOG_ERR, "xfrd: zone %s, from %s: tsig error "
1795 				"(%s)", zone->apex_str,
1796 				zone->master->ip_address_spec,
1797 				tsig_error(zone->tsig.error_code));
1798 		}
1799 	}
1800 	if(have_tsig) {
1801 		/* strip the TSIG resource record off... */
1802 		buffer_set_limit(packet, zone->tsig.position);
1803 		ARCOUNT_SET(packet, ARCOUNT(packet) - 1);
1804 	}
1805 
1806 	/* keep running the TSIG hash */
1807 	tsig_update(&zone->tsig, packet, buffer_limit(packet));
1808 	if(have_tsig) {
1809 		if (!tsig_verify(&zone->tsig)) {
1810 			log_msg(LOG_ERR, "xfrd: zone %s, from %s: bad tsig signature",
1811 				zone->apex_str, zone->master->ip_address_spec);
1812 			return 0;
1813 		}
1814 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s, from %s: good tsig signature",
1815 			zone->apex_str, zone->master->ip_address_spec));
1816 		/* prepare for next tsigs */
1817 		tsig_prepare(&zone->tsig);
1818 	}
1819 	else if(zone->tsig.updates_since_last_prepare > XFRD_TSIG_MAX_UNSIGNED) {
1820 		/* we allow a number of non-tsig signed packets */
1821 		log_msg(LOG_INFO, "xfrd: zone %s, from %s: too many consecutive "
1822 			"packets without TSIG", zone->apex_str,
1823 			zone->master->ip_address_spec);
1824 		return 0;
1825 	}
1826 
1827 	if(!have_tsig && zone->msg_seq_nr == 0) {
1828 		log_msg(LOG_ERR, "xfrd: zone %s, from %s: no tsig in first packet of reply",
1829 			zone->apex_str, zone->master->ip_address_spec);
1830 		return 0;
1831 	}
1832 	return 1;
1833 }
1834 
1835 /* parse the received packet. returns xfrd packet result code. */
1836 static enum xfrd_packet_result
1837 xfrd_parse_received_xfr_packet(xfrd_zone_type* zone, buffer_type* packet,
1838 	xfrd_soa_type* soa)
1839 {
1840 	size_t rr_count;
1841 	size_t qdcount = QDCOUNT(packet);
1842 	size_t ancount = ANCOUNT(packet), ancount_todo;
1843 	size_t nscount = NSCOUNT(packet);
1844 	int done = 0;
1845 	region_type* tempregion = NULL;
1846 
1847 	/* has to be axfr / ixfr reply */
1848 	if(!buffer_available(packet, QHEADERSZ)) {
1849 		log_msg(LOG_INFO, "packet too small");
1850 		return xfrd_packet_bad;
1851 	}
1852 
1853 	/* only check ID in first response message. Could also check that
1854 	 * AA bit and QR bit are set, but not needed.
1855 	 */
1856 	DEBUG(DEBUG_XFRD,2, (LOG_INFO,
1857 		"got query with ID %d and %d needed", ID(packet), zone->query_id));
1858 	if(ID(packet) != zone->query_id) {
1859 		log_msg(LOG_ERR, "xfrd: zone %s received bad query id from %s, "
1860 				 "dropped",
1861 			zone->apex_str, zone->master->ip_address_spec);
1862 		return xfrd_packet_bad;
1863 	}
1864 	/* check RCODE in all response messages */
1865 	if(RCODE(packet) != RCODE_OK) {
1866 		log_msg(LOG_ERR, "xfrd: zone %s received error code %s from "
1867 				 "%s",
1868 			zone->apex_str, rcode2str(RCODE(packet)),
1869 			zone->master->ip_address_spec);
1870 		if (RCODE(packet) == RCODE_IMPL ||
1871 			RCODE(packet) == RCODE_FORMAT) {
1872 			return xfrd_packet_notimpl;
1873 		}
1874 		if (RCODE(packet) != RCODE_NOTAUTH) {
1875 			/* RFC 2845: If NOTAUTH, client should do TSIG checking */
1876 			return xfrd_packet_drop;
1877 		}
1878 	}
1879 	/* check TSIG */
1880 	if(zone->master->key_options) {
1881 		if(!xfrd_xfr_process_tsig(zone, packet)) {
1882 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "dropping xfr reply due "
1883 				"to bad TSIG"));
1884 			return xfrd_packet_bad;
1885 		}
1886 	}
1887 	if (RCODE(packet) == RCODE_NOTAUTH) {
1888 		return xfrd_packet_drop;
1889 	}
1890 
1891 	buffer_skip(packet, QHEADERSZ);
1892 
1893 	/* skip question section */
1894 	for(rr_count = 0; rr_count < qdcount; ++rr_count) {
1895 		if (!packet_skip_rr(packet, 1)) {
1896 			log_msg(LOG_ERR, "xfrd: zone %s, from %s: bad RR in "
1897 					 		 "question section",
1898 				zone->apex_str, zone->master->ip_address_spec);
1899 			return xfrd_packet_bad;
1900 		}
1901 	}
1902 	if(zone->msg_rr_count == 0 && ancount == 0) {
1903 		if(zone->tcp_conn == -1 && TC(packet)) {
1904 			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: TC flagged"));
1905 			return xfrd_packet_tcp;
1906 		}
1907 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: too short xfr packet: no "
1908 					       			   "answer"));
1909 		/* if IXFR is unknown, fallback to AXFR (if allowed) */
1910 		if (nscount == 1) {
1911 			if(!packet_skip_dname(packet) || !xfrd_parse_soa_info(packet, soa)) {
1912 				DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1913 					"no SOA begins authority section",
1914 					zone->apex_str, zone->master->ip_address_spec));
1915 				return xfrd_packet_bad;
1916 			}
1917 			return xfrd_packet_notimpl;
1918 		}
1919 		return xfrd_packet_bad;
1920 	}
1921 	ancount_todo = ancount;
1922 
1923 	tempregion = region_create(xalloc, free);
1924 	if(zone->msg_rr_count == 0) {
1925 		const dname_type* soaname = dname_make_from_packet(tempregion,
1926 			packet, 1, 1);
1927 		if(!soaname) { /* parse failure */
1928 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1929 				"parse error in SOA record",
1930 				zone->apex_str, zone->master->ip_address_spec));
1931 			region_destroy(tempregion);
1932 			return xfrd_packet_bad;
1933 		}
1934 		if(dname_compare(soaname, zone->apex) != 0) { /* wrong name */
1935 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1936 				"wrong SOA record",
1937 				zone->apex_str, zone->master->ip_address_spec));
1938 			region_destroy(tempregion);
1939 			return xfrd_packet_bad;
1940 		}
1941 
1942 		/* parse the first RR, see if it is a SOA */
1943 		if(!xfrd_parse_soa_info(packet, soa))
1944 		{
1945 			DEBUG(DEBUG_XFRD,1, (LOG_ERR, "xfrd: zone %s, from %s: "
1946 						      "bad SOA rdata",
1947 				zone->apex_str, zone->master->ip_address_spec));
1948 			region_destroy(tempregion);
1949 			return xfrd_packet_bad;
1950 		}
1951 		if(zone->soa_disk_acquired != 0 &&
1952 			zone->state != xfrd_zone_expired /* if expired - accept anything */ &&
1953 			compare_serial(ntohl(soa->serial), ntohl(zone->soa_disk.serial)) < 0) {
1954 			DEBUG(DEBUG_XFRD,1, (LOG_INFO,
1955 				"xfrd: zone %s ignoring old serial from %s",
1956 				zone->apex_str, zone->master->ip_address_spec));
1957 			VERBOSITY(1, (LOG_INFO,
1958 				"xfrd: zone %s ignoring old serial from %s",
1959 				zone->apex_str, zone->master->ip_address_spec));
1960 			region_destroy(tempregion);
1961 			return xfrd_packet_bad;
1962 		}
1963 		if(zone->soa_disk_acquired != 0 && zone->soa_disk.serial == soa->serial) {
1964 			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s got "
1965 						       "update indicating "
1966 						       "current serial",
1967 				zone->apex_str));
1968 			/* (even if notified) the lease on the current soa is renewed */
1969 			zone->soa_disk_acquired = xfrd_time();
1970 			if(zone->soa_nsd.serial == soa->serial)
1971 				zone->soa_nsd_acquired = xfrd_time();
1972 			xfrd_set_zone_state(zone, xfrd_zone_ok);
1973  			DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s is ok",
1974 				zone->apex_str));
1975 			if(zone->zone_options->pattern->multi_master_check) {
1976 				region_destroy(tempregion);
1977 				return xfrd_packet_drop;
1978 			}
1979 			if(zone->soa_notified_acquired == 0) {
1980 				/* not notified or anything, so stop asking around */
1981 				zone->round_num = -1; /* next try start a new round */
1982 				xfrd_set_timer_refresh(zone);
1983 				region_destroy(tempregion);
1984 				return xfrd_packet_newlease;
1985 			}
1986 			/* try next master */
1987 			region_destroy(tempregion);
1988 			return xfrd_packet_drop;
1989 		}
1990 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "IXFR reply has ok serial (have \
1991 %u, reply %u).", (unsigned)ntohl(zone->soa_disk.serial), (unsigned)ntohl(soa->serial)));
1992 		/* serial is newer than soa_disk */
1993 		if(ancount == 1) {
1994 			/* single record means it is like a notify */
1995 			(void)xfrd_handle_incoming_notify(zone, soa);
1996 		}
1997 		else if(zone->soa_notified_acquired && zone->soa_notified.serial &&
1998 			compare_serial(ntohl(zone->soa_notified.serial), ntohl(soa->serial)) < 0) {
1999 			/* this AXFR/IXFR notifies me that an even newer serial exists */
2000 			zone->soa_notified.serial = soa->serial;
2001 		}
2002 		zone->msg_new_serial = ntohl(soa->serial);
2003 		zone->msg_rr_count = 1;
2004 		zone->msg_is_ixfr = 0;
2005 		if(zone->soa_disk_acquired)
2006 			zone->msg_old_serial = ntohl(zone->soa_disk.serial);
2007 		else zone->msg_old_serial = 0;
2008 		ancount_todo = ancount - 1;
2009 	}
2010 
2011 	if(zone->tcp_conn == -1 && TC(packet)) {
2012 		DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2013 			"xfrd: zone %s received TC from %s. retry tcp.",
2014 			zone->apex_str, zone->master->ip_address_spec));
2015 		region_destroy(tempregion);
2016 		return xfrd_packet_tcp;
2017 	}
2018 
2019 	if(zone->tcp_conn == -1 && ancount < 2) {
2020 		/* too short to be a real ixfr/axfr data transfer: need at */
2021 		/* least two RRs in the answer section. */
2022 		/* The serial is newer, so try tcp to this master. */
2023 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: udp reply is short. Try "
2024 					       			   "tcp anyway."));
2025 		region_destroy(tempregion);
2026 		return xfrd_packet_tcp;
2027 	}
2028 
2029 	if(!xfrd_xfr_check_rrs(zone, packet, ancount_todo, &done, soa,
2030 		tempregion))
2031 	{
2032 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: zone %s sent bad xfr "
2033 					       			   "reply.", zone->apex_str));
2034 		region_destroy(tempregion);
2035 		return xfrd_packet_bad;
2036 	}
2037 	region_destroy(tempregion);
2038 	if(zone->tcp_conn == -1 && done == 0) {
2039 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: udp reply incomplete"));
2040 		return xfrd_packet_bad;
2041 	}
2042 	if(done == 0)
2043 		return xfrd_packet_more;
2044 	if(zone->master->key_options) {
2045 		if(zone->tsig.updates_since_last_prepare != 0) {
2046 			log_msg(LOG_INFO, "xfrd: last packet of reply has no "
2047 					 		  "TSIG");
2048 			return xfrd_packet_bad;
2049 		}
2050 	}
2051 	return xfrd_packet_transfer;
2052 }
2053 
2054 const char*
2055 xfrd_pretty_time(time_t v)
2056 {
2057 	struct tm* tm = localtime(&v);
2058 	static char buf[64];
2059 	if(!strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", tm))
2060 		snprintf(buf, sizeof(buf), "strftime-err-%u", (unsigned)v);
2061 	return buf;
2062 }
2063 
2064 enum xfrd_packet_result
2065 xfrd_handle_received_xfr_packet(xfrd_zone_type* zone, buffer_type* packet)
2066 {
2067 	xfrd_soa_type soa;
2068 	enum xfrd_packet_result res;
2069         uint64_t xfrfile_size;
2070 
2071 	/* parse and check the packet - see if it ends the xfr */
2072 	switch((res=xfrd_parse_received_xfr_packet(zone, packet, &soa)))
2073 	{
2074 		case xfrd_packet_more:
2075 		case xfrd_packet_transfer:
2076 			/* continue with commit */
2077 			break;
2078 		case xfrd_packet_newlease:
2079 			return xfrd_packet_newlease;
2080 		case xfrd_packet_tcp:
2081 			return xfrd_packet_tcp;
2082 		case xfrd_packet_notimpl:
2083 		case xfrd_packet_bad:
2084 		case xfrd_packet_drop:
2085 		default:
2086 		{
2087 			/* rollback */
2088 			if(zone->msg_seq_nr > 0) {
2089 				/* do not process xfr - if only one part simply ignore it. */
2090 				/* delete file with previous parts of commit */
2091 				xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2092 				VERBOSITY(1, (LOG_INFO, "xfrd: zone %s "
2093 					"reverted transfer %u from %s",
2094 					zone->apex_str, zone->msg_rr_count?
2095 					(int)zone->msg_new_serial:0,
2096 					zone->master->ip_address_spec));
2097 				zone->msg_seq_nr = 0;
2098 			} else if (res == xfrd_packet_bad) {
2099 				VERBOSITY(1, (LOG_INFO, "xfrd: zone %s "
2100 					"bad transfer %u from %s",
2101 					zone->apex_str, zone->msg_rr_count?
2102 					(int)zone->msg_new_serial:0,
2103 					zone->master->ip_address_spec));
2104 			}
2105 			if (res == xfrd_packet_notimpl)
2106 				return res;
2107 			else
2108 				return xfrd_packet_bad;
2109 		}
2110 	}
2111 
2112 	/* dump reply on disk to diff file */
2113 	/* if first part, get new filenumber.  Numbers can wrap around, 64bit
2114 	 * is enough so we do not collide with older-transfers-in-progress */
2115 	if(zone->msg_seq_nr == 0)
2116 		zone->xfrfilenumber = xfrd->xfrfilenumber++;
2117 	diff_write_packet(dname_to_string(zone->apex,0),
2118 		zone->zone_options->pattern->pname,
2119 		zone->msg_old_serial, zone->msg_new_serial, zone->msg_seq_nr,
2120 		buffer_begin(packet), buffer_limit(packet), xfrd->nsd,
2121 		zone->xfrfilenumber);
2122 	VERBOSITY(3, (LOG_INFO,
2123 		"xfrd: zone %s written received XFR packet from %s with serial %u to "
2124 		"disk", zone->apex_str, zone->master->ip_address_spec,
2125 		(int)zone->msg_new_serial));
2126 	zone->msg_seq_nr++;
2127 
2128         xfrfile_size = xfrd_get_xfrfile_size(xfrd->nsd, zone->xfrfilenumber);
2129 	if( zone->zone_options->pattern->size_limit_xfr != 0 &&
2130 	    xfrfile_size > zone->zone_options->pattern->size_limit_xfr ) {
2131             /*	    xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2132                     xfrd_set_reload_timeout(); */
2133             log_msg(LOG_INFO, "xfrd : transferred zone data was too large %llu", (long long unsigned)xfrfile_size);
2134 	    return xfrd_packet_bad;
2135 	}
2136 	if(res == xfrd_packet_more) {
2137 		/* wait for more */
2138 		return xfrd_packet_more;
2139 	}
2140 
2141 	/* done. we are completely sure of this */
2142 	buffer_clear(packet);
2143 	buffer_printf(packet, "received update to serial %u at %s from %s",
2144 		(unsigned)zone->msg_new_serial, xfrd_pretty_time(xfrd_time()),
2145 		zone->master->ip_address_spec);
2146 	if(zone->master->key_options) {
2147 		buffer_printf(packet, " TSIG verified with key %s",
2148 			zone->master->key_options->name);
2149 	}
2150 	buffer_flip(packet);
2151 	diff_write_commit(zone->apex_str, zone->msg_old_serial,
2152 		zone->msg_new_serial, zone->msg_seq_nr, 1,
2153 		(char*)buffer_begin(packet), xfrd->nsd, zone->xfrfilenumber);
2154 	VERBOSITY(1, (LOG_INFO, "xfrd: zone %s committed \"%s\"",
2155 		zone->apex_str, (char*)buffer_begin(packet)));
2156 	/* reset msg seq nr, so if that is nonnull we know xfr file exists */
2157 	zone->msg_seq_nr = 0;
2158 	/* now put apply_xfr task on the tasklist */
2159 	if(!task_new_apply_xfr(xfrd->nsd->task[xfrd->nsd->mytask],
2160 		xfrd->last_task, zone->apex, zone->msg_old_serial,
2161 		zone->msg_new_serial, zone->xfrfilenumber)) {
2162 		/* delete the file and pretend transfer was bad to continue */
2163 		xfrd_unlink_xfrfile(xfrd->nsd, zone->xfrfilenumber);
2164 		xfrd_set_reload_timeout();
2165 		return xfrd_packet_bad;
2166 	}
2167 	/* update the disk serial no. */
2168 	zone->soa_disk_acquired = xfrd_time();
2169 	zone->soa_disk = soa;
2170 	if(zone->soa_notified_acquired && (
2171 		zone->soa_notified.serial == 0 ||
2172 		compare_serial(htonl(zone->soa_disk.serial),
2173 		htonl(zone->soa_notified.serial)) >= 0))
2174 	{
2175 		zone->soa_notified_acquired = 0;
2176 	}
2177 	if(!zone->soa_notified_acquired) {
2178 		/* do not set expired zone to ok:
2179 		 * it would cause nsd to start answering
2180 		 * bad data, since the zone is not loaded yet.
2181 		 * if nsd does not reload < retry time, more
2182 		 * queries (for even newer versions) are made.
2183 		 * For expired zone after reload it is set ok (SOAINFO ipc). */
2184 		if(zone->state != xfrd_zone_expired)
2185 			xfrd_set_zone_state(zone, xfrd_zone_ok);
2186 		DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2187 			"xfrd: zone %s is waiting for reload",
2188 			zone->apex_str));
2189 		if(zone->zone_options->pattern->multi_master_check) {
2190 			zone->multi_master_update_check = zone->master_num;
2191 			xfrd_set_reload_timeout();
2192 			return xfrd_packet_transfer;
2193 		}
2194 		zone->round_num = -1; /* next try start anew */
2195 		xfrd_set_timer_refresh(zone);
2196 		xfrd_set_reload_timeout();
2197 		return xfrd_packet_transfer;
2198 	} else {
2199 		/* try to get an even newer serial */
2200 		/* pretend it was bad to continue queries */
2201 		xfrd_set_reload_timeout();
2202 		return xfrd_packet_bad;
2203 	}
2204 }
2205 
2206 static void
2207 xfrd_set_reload_timeout()
2208 {
2209 	if(xfrd->nsd->options->xfrd_reload_timeout == -1)
2210 		return; /* automatic reload disabled. */
2211 	if(xfrd->reload_timeout.tv_sec == 0 ||
2212 		xfrd_time() >= (time_t)xfrd->reload_timeout.tv_sec ) {
2213 		/* no reload wait period (or it passed), do it right away */
2214 		xfrd_set_reload_now(xfrd);
2215 		/* start reload wait period */
2216 		xfrd->reload_timeout.tv_sec = xfrd_time() +
2217 			xfrd->nsd->options->xfrd_reload_timeout;
2218 		xfrd->reload_timeout.tv_usec = 0;
2219 		return;
2220 	}
2221 	/* cannot reload now, set that after the timeout a reload has to happen */
2222 	if(xfrd->reload_added == 0) {
2223 		struct timeval tv;
2224 		tv.tv_sec = xfrd->reload_timeout.tv_sec - xfrd_time();
2225 		tv.tv_usec = 0;
2226 		if(tv.tv_sec > xfrd->nsd->options->xfrd_reload_timeout)
2227 			tv.tv_sec = xfrd->nsd->options->xfrd_reload_timeout;
2228 		memset(&xfrd->reload_handler, 0, sizeof(xfrd->reload_handler));
2229 		event_set(&xfrd->reload_handler, -1, EV_TIMEOUT,
2230 			xfrd_handle_reload, xfrd);
2231 		if(event_base_set(xfrd->event_base, &xfrd->reload_handler) != 0)
2232 			log_msg(LOG_ERR, "cannot set reload event base");
2233 		if(event_add(&xfrd->reload_handler, &tv) != 0)
2234 			log_msg(LOG_ERR, "cannot add reload event");
2235 		xfrd->reload_added = 1;
2236 	}
2237 }
2238 
2239 static void
2240 xfrd_handle_reload(int ATTR_UNUSED(fd), short event, void* ATTR_UNUSED(arg))
2241 {
2242 	/* reload timeout */
2243 	assert(event & EV_TIMEOUT);
2244 	(void)event;
2245 	/* timeout wait period after this request is sent */
2246 	xfrd->reload_added = 0;
2247 	xfrd->reload_timeout.tv_sec = xfrd_time() +
2248 		xfrd->nsd->options->xfrd_reload_timeout;
2249 	xfrd_set_reload_now(xfrd);
2250 }
2251 
2252 void
2253 xfrd_handle_notify_and_start_xfr(xfrd_zone_type* zone, xfrd_soa_type* soa)
2254 {
2255 	if(xfrd_handle_incoming_notify(zone, soa)) {
2256 		if(zone->zone_handler.ev_fd == -1 && zone->tcp_conn == -1 &&
2257 			!zone->tcp_waiting && !zone->udp_waiting) {
2258 			xfrd_set_refresh_now(zone);
2259 		}
2260 		/* zones with no content start expbackoff again; this is also
2261 		 * for nsd-control started transfer commands, and also when
2262 		 * the master apparently sends notifies (is back up) */
2263 		if(zone->soa_disk_acquired == 0)
2264 			zone->fresh_xfr_timeout = XFRD_TRANSFER_TIMEOUT_START;
2265 	}
2266 }
2267 
2268 void
2269 xfrd_handle_passed_packet(buffer_type* packet,
2270 	int acl_num, int acl_num_xfr)
2271 {
2272 	uint8_t qnamebuf[MAXDOMAINLEN];
2273 	uint16_t qtype, qclass;
2274 	const dname_type* dname;
2275 	region_type* tempregion = region_create(xalloc, free);
2276 	xfrd_zone_type* zone;
2277 
2278 	buffer_skip(packet, QHEADERSZ);
2279 	if(!packet_read_query_section(packet, qnamebuf, &qtype, &qclass)) {
2280 		region_destroy(tempregion);
2281 		return; /* drop bad packet */
2282 	}
2283 
2284 	dname = dname_make(tempregion, qnamebuf, 1);
2285 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "xfrd: got passed packet for %s, acl "
2286 		   "%d", dname_to_string(dname,0), acl_num));
2287 
2288 	/* find the zone */
2289 	zone = (xfrd_zone_type*)rbtree_search(xfrd->zones, dname);
2290 	if(!zone) {
2291 		/* this could be because the zone has been deleted meanwhile */
2292 		DEBUG(DEBUG_XFRD, 1, (LOG_INFO, "xfrd: incoming packet for "
2293 			"unknown zone %s", dname_to_string(dname,0)));
2294 		region_destroy(tempregion);
2295 		return; /* drop packet for unknown zone */
2296 	}
2297 	region_destroy(tempregion);
2298 
2299 	/* handle */
2300 	if(OPCODE(packet) == OPCODE_NOTIFY) {
2301 		xfrd_soa_type soa;
2302 		int have_soa = 0;
2303 		int next;
2304 		/* get serial from a SOA */
2305 		if(ANCOUNT(packet) == 1 && packet_skip_dname(packet) &&
2306 			xfrd_parse_soa_info(packet, &soa)) {
2307 				have_soa = 1;
2308 		}
2309 		xfrd_handle_notify_and_start_xfr(zone, have_soa?&soa:NULL);
2310 		/* First, see if our notifier has a match in provide-xfr */
2311 		if (acl_find_num(zone->zone_options->pattern->request_xfr,
2312 				acl_num_xfr))
2313 			next = acl_num_xfr;
2314 		else /* If not, find master that matches notifiers ACL entry */
2315 			next = find_same_master_notify(zone, acl_num);
2316 		if(next != -1) {
2317 			zone->next_master = next;
2318 			DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2319 				"xfrd: notify set next master to query %d",
2320 				next));
2321 		}
2322 	}
2323 	else {
2324 		/* ignore other types of messages */
2325 	}
2326 }
2327 
2328 static int
2329 xfrd_handle_incoming_notify(xfrd_zone_type* zone, xfrd_soa_type* soa)
2330 {
2331 	if(soa && zone->soa_disk_acquired && zone->state != xfrd_zone_expired &&
2332 	   compare_serial(ntohl(soa->serial),ntohl(zone->soa_disk.serial)) <= 0)
2333 	{
2334 		DEBUG(DEBUG_XFRD,1, (LOG_INFO,
2335 			"xfrd: ignored notify %s %u old serial, zone valid "
2336 			"(soa disk serial %u)", zone->apex_str,
2337 			(unsigned)ntohl(soa->serial),
2338 			(unsigned)ntohl(zone->soa_disk.serial)));
2339 		return 0; /* ignore notify with old serial, we have a valid zone */
2340 	}
2341 	if(soa == 0) {
2342 		zone->soa_notified.serial = 0;
2343 	}
2344 	else if (zone->soa_notified_acquired == 0 ||
2345 		 zone->soa_notified.serial == 0 ||
2346 		 compare_serial(ntohl(soa->serial),
2347 			ntohl(zone->soa_notified.serial)) > 0)
2348 	{
2349 		zone->soa_notified = *soa;
2350 	}
2351 	zone->soa_notified_acquired = xfrd_time();
2352 	if(zone->state == xfrd_zone_ok) {
2353 		xfrd_set_zone_state(zone, xfrd_zone_refreshing);
2354 	}
2355 	/* transfer right away */
2356 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "Handle incoming notify for zone %s",
2357 		zone->apex_str));
2358 	return 1;
2359 }
2360 
2361 static int
2362 find_same_master_notify(xfrd_zone_type* zone, int acl_num_nfy)
2363 {
2364 	struct acl_options* nfy_acl = acl_find_num(zone->zone_options->pattern->
2365 		allow_notify, acl_num_nfy);
2366 	int num = 0;
2367 	struct acl_options* master = zone->zone_options->pattern->request_xfr;
2368 	if(!nfy_acl)
2369 		return -1;
2370 	while(master)
2371 	{
2372 		if(acl_addr_matches_host(nfy_acl, master))
2373 			return num;
2374 		master = master->next;
2375 		num++;
2376 	}
2377 	return -1;
2378 }
2379 
2380 void
2381 xfrd_check_failed_updates()
2382 {
2383 	/* see if updates have not come through */
2384 	xfrd_zone_type* zone;
2385 	RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
2386 	{
2387 		/* zone has a disk soa, and no nsd soa or a different nsd soa */
2388 		if(zone->soa_disk_acquired != 0 &&
2389 			(zone->soa_nsd_acquired == 0 ||
2390 			zone->soa_disk.serial != zone->soa_nsd.serial))
2391 		{
2392 			if(zone->soa_disk_acquired <
2393 				xfrd->reload_cmd_last_sent)
2394 			{
2395 				/* this zone should have been loaded, since its disk
2396 				   soa time is before the time of the reload cmd. */
2397 				xfrd_soa_type dumped_soa = zone->soa_disk;
2398 				log_msg(LOG_ERR, "xfrd: zone %s: soa serial %u "
2399 						 		 "update failed, restarting "
2400 						 		 "transfer (notified zone)",
2401 					zone->apex_str, (unsigned)ntohl(zone->soa_disk.serial));
2402 				/* revert the soa; it has not been acquired properly */
2403 				if(zone->soa_disk_acquired == zone->soa_nsd_acquired) {
2404 					/* this was the same as served,
2405 					 * perform force_axfr , re-download
2406 					 * same serial from master */
2407 					zone->soa_disk_acquired = 0;
2408 					zone->soa_nsd_acquired = 0;
2409 				} else {
2410 					/* revert soa to the one in server */
2411 					zone->soa_disk_acquired = zone->soa_nsd_acquired;
2412 					zone->soa_disk = zone->soa_nsd;
2413 				}
2414 				/* pretend we are notified with disk soa.
2415 				   This will cause a refetch of the data, and reload. */
2416 				xfrd_handle_incoming_notify(zone, &dumped_soa);
2417 				xfrd_set_timer_refresh(zone);
2418 			} else if(zone->soa_disk_acquired >= xfrd->reload_cmd_last_sent) {
2419 				/* this zone still has to be loaded,
2420 				   make sure reload is set to be sent. */
2421 				if(xfrd->need_to_send_reload == 0 &&
2422 					xfrd->reload_added == 0) {
2423 					log_msg(LOG_ERR, "xfrd: zone %s: needs "
2424 									 "to be loaded. reload lost? "
2425 									 "try again", zone->apex_str);
2426 					xfrd_set_reload_timeout();
2427 				}
2428 			}
2429 		}
2430 	}
2431 }
2432 
2433 void
2434 xfrd_prepare_zones_for_reload()
2435 {
2436 	xfrd_zone_type* zone;
2437 	RBTREE_FOR(zone, xfrd_zone_type*, xfrd->zones)
2438 	{
2439 		/* zone has a disk soa, and no nsd soa or a different nsd soa */
2440 		if(zone->soa_disk_acquired != 0 &&
2441 			(zone->soa_nsd_acquired == 0 ||
2442 			zone->soa_disk.serial != zone->soa_nsd.serial))
2443 		{
2444 			if(zone->soa_disk_acquired == xfrd_time()) {
2445 				/* antedate by one second.
2446 				 * this makes sure that the zone time is before
2447 				 * reload, so that check_failed_zones() is
2448 				 * certain of the result.
2449 				 */
2450 				zone->soa_disk_acquired--;
2451 			}
2452 		}
2453 	}
2454 }
2455 
2456 struct buffer*
2457 xfrd_get_temp_buffer()
2458 {
2459 	return xfrd->packet;
2460 }
2461 
2462 #ifdef BIND8_STATS
2463 /** process stat info task */
2464 static void
2465 xfrd_process_stat_info_task(xfrd_state_type* xfrd, struct task_list_d* task)
2466 {
2467 	size_t i;
2468 	stc_type* p = (void*)task->zname + sizeof(struct nsdst);
2469 	stats_add(&xfrd->nsd->st, (struct nsdst*)task->zname);
2470 	for(i=0; i<xfrd->nsd->child_count; i++) {
2471 		xfrd->nsd->children[i].query_count += *p++;
2472 	}
2473 	/* got total, now see if users are interested in these statistics */
2474 #ifdef HAVE_SSL
2475 	daemon_remote_process_stats(xfrd->nsd->rc);
2476 #endif
2477 }
2478 #endif /* BIND8_STATS */
2479 
2480 #ifdef USE_ZONE_STATS
2481 /** process zonestat inc task */
2482 static void
2483 xfrd_process_zonestat_inc_task(xfrd_state_type* xfrd, struct task_list_d* task)
2484 {
2485 	xfrd->zonestat_safe = (unsigned)task->oldserial;
2486 	zonestat_remap(xfrd->nsd, 0, xfrd->zonestat_safe*sizeof(struct nsdst));
2487 	xfrd->nsd->zonestatsize[0] = xfrd->zonestat_safe;
2488 	zonestat_remap(xfrd->nsd, 1, xfrd->zonestat_safe*sizeof(struct nsdst));
2489 	xfrd->nsd->zonestatsize[1] = xfrd->zonestat_safe;
2490 }
2491 #endif /* USE_ZONE_STATS */
2492 
2493 static void
2494 xfrd_handle_taskresult(xfrd_state_type* xfrd, struct task_list_d* task)
2495 {
2496 #ifndef BIND8_STATS
2497 	(void)xfrd;
2498 #endif
2499 	switch(task->task_type) {
2500 	case task_soa_info:
2501 		xfrd_process_soa_info_task(task);
2502 		break;
2503 #ifdef BIND8_STATS
2504 	case task_stat_info:
2505 		xfrd_process_stat_info_task(xfrd, task);
2506 		break;
2507 #endif /* BIND8_STATS */
2508 #ifdef USE_ZONE_STATS
2509 	case task_zonestat_inc:
2510 		xfrd_process_zonestat_inc_task(xfrd, task);
2511 		break;
2512 #endif
2513 	default:
2514 		log_msg(LOG_WARNING, "unhandled task result in xfrd from "
2515 			"reload type %d", (int)task->task_type);
2516 	}
2517 }
2518 
2519 void xfrd_process_task_result(xfrd_state_type* xfrd, struct udb_base* taskudb)
2520 {
2521 	udb_ptr t;
2522 	/* remap it for usage */
2523 	task_remap(taskudb);
2524 	/* process the task-results in the taskudb */
2525 	udb_ptr_new(&t, taskudb, udb_base_get_userdata(taskudb));
2526 	while(!udb_ptr_is_null(&t)) {
2527 		xfrd_handle_taskresult(xfrd, TASKLIST(&t));
2528 		udb_ptr_set_rptr(&t, taskudb, &TASKLIST(&t)->next);
2529 	}
2530 	udb_ptr_unlink(&t, taskudb);
2531 	/* clear the udb so it can be used by xfrd to make new tasks for
2532 	 * reload, this happens when the reload signal is sent, and thus
2533 	 * the taskudbs are swapped */
2534 	task_clear(taskudb);
2535 #ifdef HAVE_SYSTEMD
2536 	sd_notify(0, "READY=1");
2537 #endif
2538 }
2539 
2540 void xfrd_set_reload_now(xfrd_state_type* xfrd)
2541 {
2542 #ifdef HAVE_SYSTEMD
2543 	sd_notify(0, "RELOADING=1");
2544 #endif
2545 	xfrd->need_to_send_reload = 1;
2546 	if(!(xfrd->ipc_handler_flags&EV_WRITE)) {
2547 		ipc_xfrd_set_listening(xfrd, EV_PERSIST|EV_READ|EV_WRITE);
2548 	}
2549 }
2550 
2551 static void
2552 xfrd_handle_write_timer(int ATTR_UNUSED(fd), short event, void* ATTR_UNUSED(arg))
2553 {
2554 	/* timeout for write events */
2555 	assert(event & EV_TIMEOUT);
2556 	(void)event;
2557 	if(xfrd->nsd->options->zonefiles_write == 0)
2558 		return;
2559 	/* call reload to write changed zonefiles */
2560 	if(!xfrd->write_zonefile_needed) {
2561 		DEBUG(DEBUG_XFRD,2, (LOG_INFO, "zonefiles write timer (nothing)"));
2562 		xfrd_write_timer_set();
2563 		return;
2564 	}
2565 	DEBUG(DEBUG_XFRD,1, (LOG_INFO, "zonefiles write timer"));
2566 	task_new_write_zonefiles(xfrd->nsd->task[xfrd->nsd->mytask],
2567 		xfrd->last_task, NULL);
2568 	xfrd_set_reload_now(xfrd);
2569 	xfrd->write_zonefile_needed = 0;
2570 	xfrd_write_timer_set();
2571 }
2572 
2573 static void xfrd_write_timer_set()
2574 {
2575 	struct timeval tv;
2576 	if(xfrd->nsd->options->zonefiles_write == 0)
2577 		return;
2578 	tv.tv_sec = xfrd->nsd->options->zonefiles_write;
2579 	tv.tv_usec = 0;
2580 	memset(&xfrd->write_timer, 0, sizeof(xfrd->write_timer));
2581 	event_set(&xfrd->write_timer, -1, EV_TIMEOUT,
2582 		xfrd_handle_write_timer, xfrd);
2583 	if(event_base_set(xfrd->event_base, &xfrd->write_timer) != 0)
2584 		log_msg(LOG_ERR, "xfrd write timer: event_base_set failed");
2585 	if(event_add(&xfrd->write_timer, &tv) != 0)
2586 		log_msg(LOG_ERR, "xfrd write timer: event_add failed");
2587 }
2588 
2589 static void xfrd_handle_child_timer(int ATTR_UNUSED(fd), short event,
2590 	void* ATTR_UNUSED(arg))
2591 {
2592 	assert(event & EV_TIMEOUT);
2593 	(void)event;
2594 	/* only used to wakeup the process to reap children, note the
2595 	 * event is no longer registered */
2596 	xfrd->child_timer_added = 0;
2597 }
2598