OpenDNSSEC-signer  1.4.5
xfrd.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
32 #include "config.h"
33 #include "daemon/engine.h"
34 #include "daemon/xfrhandler.h"
35 #include "shared/duration.h"
36 #include "shared/file.h"
37 #include "shared/log.h"
38 #include "shared/status.h"
39 #include "shared/util.h"
40 #include "signer/backup.h"
41 #include "signer/domain.h"
42 #include "signer/zone.h"
43 #include "wire/tcpset.h"
44 #include "wire/xfrd.h"
45 
46 #include <unistd.h>
47 #include <fcntl.h>
48 
49 #define XFRD_TSIG_MAX_UNSIGNED 100
50 
51 static const char* xfrd_str = "xfrd";
52 
53 static void xfrd_handle_zone(netio_type* netio,
54  netio_handler_type* handler, netio_events_type event_types);
55 static void xfrd_make_request(xfrd_type* xfrd);
56 
57 static socklen_t xfrd_acl_sockaddr(acl_type* acl, unsigned int port,
58  struct sockaddr_storage *sck);
59 
60 static void xfrd_write_soa(xfrd_type* xfrd, buffer_type* buffer);
61 static int xfrd_parse_soa(xfrd_type* xfrd, buffer_type* buffer,
62  unsigned rdata_only, unsigned update, uint32_t t,
63  uint32_t* serial);
64 static ods_status xfrd_parse_rrs(xfrd_type* xfrd, buffer_type* buffer,
65  uint16_t count, int* done);
66 static xfrd_pkt_status xfrd_parse_packet(xfrd_type* xfrd,
67  buffer_type* buffer);
68 static xfrd_pkt_status xfrd_handle_packet(xfrd_type* xfrd,
69  buffer_type* buffer);
70 
71 static void xfrd_tcp_obtain(xfrd_type* xfrd, tcp_set_type* set);
72 static void xfrd_tcp_read(xfrd_type* xfrd, tcp_set_type* set);
73 static void xfrd_tcp_release(xfrd_type* xfrd, tcp_set_type* set);
74 static void xfrd_tcp_write(xfrd_type* xfrd, tcp_set_type* set);
75 static void xfrd_tcp_xfr(xfrd_type* xfrd, tcp_set_type* set);
76 static int xfrd_tcp_open(xfrd_type* xfrd, tcp_set_type* set);
77 
78 static void xfrd_udp_obtain(xfrd_type* xfrd);
79 static void xfrd_udp_read(xfrd_type* xfrd);
80 static void xfrd_udp_release(xfrd_type* xfrd);
81 static int xfrd_udp_read_packet(xfrd_type* xfrd);
82 static int xfrd_udp_send(xfrd_type* xfrd, buffer_type* buffer);
83 static int xfrd_udp_send_request_ixfr(xfrd_type* xfrd);
84 
85 static time_t xfrd_time(xfrd_type* xfrd);
86 static void xfrd_set_timer(xfrd_type* xfrd, time_t t);
87 static void xfrd_set_timer_time(xfrd_type* xfrd, time_t t);
88 static void xfrd_unset_timer(xfrd_type* xfrd);
89 
90 
95 static uint8_t
96 xfrd_recover_dname(uint8_t* dname, const char* name)
97 {
98  const uint8_t *s = (const uint8_t *) name;
99  uint8_t *h;
100  uint8_t *p;
101  uint8_t *d = dname;
102  size_t label_length;
103 
104  if (strcmp(name, ".") == 0) {
105  /* Root domain. */
106  dname[0] = 0;
107  return 1;
108  }
109  for (h = d, p = h + 1; *s; ++s, ++p) {
110  if (p - dname >= MAXDOMAINLEN) {
111  return 0;
112  }
113  switch (*s) {
114  case '.':
115  if (p == h + 1) {
116  /* Empty label. */
117  return 0;
118  } else {
119  label_length = p - h - 1;
120  if (label_length > MAXLABELLEN) {
121  return 0;
122  }
123  *h = label_length;
124  h = p;
125  }
126  break;
127  case '\\':
128  /* Handle escaped characters (RFC1035 5.1) */
129  if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
130  int val = (ldns_hexdigit_to_int(s[1]) * 100 +
131  ldns_hexdigit_to_int(s[2]) * 10 +
132  ldns_hexdigit_to_int(s[3]));
133  if (0 <= val && val <= 255) {
134  s += 3;
135  *p = val;
136  } else {
137  *p = *++s;
138  }
139  } else if (s[1] != '\0') {
140  *p = *++s;
141  }
142  break;
143  default:
144  *p = *s;
145  break;
146  }
147  }
148  if (p != h + 1) {
149  /* Terminate last label. */
150  label_length = p - h - 1;
151  if (label_length > MAXLABELLEN) {
152  return 0;
153  }
154  *h = label_length;
155  h = p;
156  }
157  /* Add root label. */
158  *h = 0;
159  return p-dname;
160 }
161 
162 
167 static void
168 xfrd_recover(xfrd_type* xfrd)
169 {
170  zone_type* zone = (zone_type*) xfrd->zone;
171  char* file = NULL;
172  FILE* fd = NULL;
173  int round_num = 0;
174  int master_num = 0;
175  int next_master = 0;
176  uint32_t timeout = 0;
177  uint32_t serial_xfr = 0;
178  uint32_t serial_notify = 0;
179  uint32_t serial_disk = 0;
180  time_t serial_xfr_acquired = 0;
181  time_t serial_notify_acquired = 0;
182  time_t serial_disk_acquired = 0;
183  uint32_t soa_ttl = 0;
184  uint32_t soa_serial = 0;
185  uint32_t soa_refresh = 0;
186  uint32_t soa_retry = 0;
187  uint32_t soa_expire = 0;
188  uint32_t soa_minimum = 0;
189  const char* soa_mname = NULL;
190  const char* soa_rname = NULL;
191 
192  if (zone && zone->name) {
193  file = ods_build_path(zone->name, ".xfrd-state", 0, 1);
194  if (file) {
195  ods_log_debug("[%s] recover state file zone %s", xfrd_str,
196  zone->name);
197  fd = ods_fopen(file, NULL, "r");
198  if (fd) {
199  if (!backup_read_check_str(fd, ODS_SE_FILE_MAGIC_V3)) {
200  ods_log_error("[%s] corrupted state file zone %s: read "
201  "magic (start) error", xfrd_str, zone->name);
202  goto xfrd_recover_error;
203  }
204  if (!backup_read_check_str(fd, ";;Zone:") |
205  !backup_read_check_str(fd, "name") |
206  !backup_read_check_str(fd, zone->name) |
207  !backup_read_check_str(fd, "ttl") |
208  !backup_read_uint32_t(fd, &soa_ttl) |
209  !backup_read_check_str(fd, "mname") |
210  !backup_read_str(fd, &soa_mname) |
211  !backup_read_check_str(fd, "rname") |
212  !backup_read_str(fd, &soa_rname) |
213  !backup_read_check_str(fd, "serial") |
214  !backup_read_uint32_t(fd, &soa_serial) |
215  !backup_read_check_str(fd, "refresh") |
216  !backup_read_uint32_t(fd, &soa_refresh) |
217  !backup_read_check_str(fd, "retry") |
218  !backup_read_uint32_t(fd, &soa_retry) |
219  !backup_read_check_str(fd, "expire") |
220  !backup_read_uint32_t(fd, &soa_expire) |
221  !backup_read_check_str(fd, "minimum") |
222  !backup_read_uint32_t(fd, &soa_minimum)) {
223  ods_log_error("[%s] corrupted state file zone %s: read "
224  ";;Zone error", xfrd_str, zone->name);
225  goto xfrd_recover_error;
226  }
227  if (!backup_read_check_str(fd, ";;Master:") |
228  !backup_read_check_str(fd, "num") |
229  !backup_read_int(fd, &master_num) |
230  !backup_read_check_str(fd, "next") |
231  !backup_read_int(fd, &next_master) |
232  !backup_read_check_str(fd, "round") |
233  !backup_read_int(fd, &round_num) |
234  !backup_read_check_str(fd, "timeout") |
235  !backup_read_uint32_t(fd, &timeout)) {
236  ods_log_error("[%s] corrupt state file zone %s: read "
237  ";;Master error", xfrd_str, zone->name);
238  goto xfrd_recover_error;
239  }
240  if (!backup_read_check_str(fd, ";;Serial:") |
241  !backup_read_check_str(fd, "xfr") |
242  !backup_read_uint32_t(fd, &serial_xfr) |
243  !backup_read_time_t(fd, &serial_xfr_acquired) |
244  !backup_read_check_str(fd, "notify") |
245  !backup_read_uint32_t(fd, &serial_notify) |
246  !backup_read_time_t(fd, &serial_notify_acquired) |
247  !backup_read_check_str(fd, "disk") |
248  !backup_read_uint32_t(fd, &serial_disk) |
249  !backup_read_time_t(fd, &serial_disk_acquired)) {
250  ods_log_error("[%s] corrupt state file zone %s: read "
251  ";;Serial error", xfrd_str, zone->name);
252  goto xfrd_recover_error;
253  }
254  if (!backup_read_check_str(fd, ODS_SE_FILE_MAGIC_V3)) {
255  ods_log_error("[%s] corrupt state file zone %s: read "
256  "magic (end) error", xfrd_str, zone->name);
257  goto xfrd_recover_error;
258  }
259 
260  /* all ok */
261  xfrd->master_num = master_num;
262  xfrd->next_master = next_master;
263  xfrd->round_num = round_num;
264  xfrd->timeout.tv_sec = timeout;
265  xfrd->timeout.tv_nsec = 0;
266  xfrd->master = NULL; /* acl_find_num(...) */
267  xfrd->soa.ttl = htonl(soa_ttl);
268  xfrd->soa.serial = htonl(soa_serial);
269  xfrd->soa.refresh = htonl(soa_refresh);
270  xfrd->soa.retry = htonl(soa_retry);
271  xfrd->soa.expire = htonl(soa_expire);
272  xfrd->soa.minimum = htonl(soa_minimum);
273  xfrd->soa.mname[0] = xfrd_recover_dname(xfrd->soa.mname+1,
274  soa_mname);
275  xfrd->soa.rname[0] = xfrd_recover_dname(xfrd->soa.rname+1,
276  soa_rname);
277  xfrd->serial_xfr = serial_xfr;
278  xfrd->serial_xfr_acquired = serial_xfr_acquired;
279  xfrd->serial_notify = serial_notify;
280  xfrd->serial_notify_acquired = serial_notify_acquired;
281  xfrd->serial_disk = serial_disk;
282  xfrd->serial_disk_acquired = serial_disk_acquired;
283  if (!timeout || serial_notify_acquired ||
284  (serial_disk_acquired &&
285  (uint32_t)xfrd_time(xfrd) - serial_disk_acquired >
286  soa_refresh)) {
288  }
289  if (serial_disk_acquired &&
290  ((uint32_t)xfrd_time(xfrd) - serial_disk_acquired >
291  soa_expire)) {
293  }
294 
295 xfrd_recover_error:
296  free((void*)soa_mname);
297  free((void*)soa_rname);
298  ods_fclose(fd);
299  }
300  free(file);
301  }
302  }
303  return;
304 }
305 
306 
311 xfrd_type*
312 xfrd_create(void* xfrhandler, void* zone)
313 {
314  xfrd_type* xfrd = NULL;
315  allocator_type* allocator = NULL;
316  if (!xfrhandler || !zone) {
317  return NULL;
318  }
319  allocator = allocator_create(malloc, free);
320  if (!allocator) {
321  ods_log_error("[%s] unable to create zone xfr structure: "
322  "allocator_create() failed", xfrd_str);
323  return NULL;
324  }
325  xfrd = (xfrd_type*) allocator_alloc(allocator, sizeof(xfrd_type));
326  if (!xfrd) {
327  ods_log_error("[%s] unable to create zone xfr structure: "
328  " allocator_alloc() failed", xfrd_str);
329  allocator_cleanup(allocator);
330  return NULL;
331  }
333  lock_basic_init(&xfrd->rw_lock);
334 
335  xfrd->allocator = allocator;
336  xfrd->xfrhandler = xfrhandler;
337  xfrd->zone = zone;
338  xfrd->tcp_conn = -1;
339  xfrd->round_num = -1;
340  xfrd->master_num = 0;
341  xfrd->next_master = -1;
342  xfrd->master = NULL;
344  xfrd->serial_xfr = 0;
345  xfrd->serial_disk = 0;
346  xfrd->serial_notify = 0;
347  xfrd->serial_xfr_acquired = 0;
348  xfrd->serial_disk_acquired = 0;
349  xfrd->serial_notify_acquired = 0;
351  xfrd->query_id = 0;
352  xfrd->msg_seq_nr = 0;
353  xfrd->msg_rr_count = 0;
354  xfrd->msg_old_serial = 0;
355  xfrd->msg_new_serial = 0;
356  xfrd->msg_is_ixfr = 0;
357  xfrd->udp_waiting = 0;
358  xfrd->udp_waiting_next = NULL;
359  xfrd->tcp_waiting = 0;
360  xfrd->tcp_waiting_next = NULL;
361  xfrd->tsig_rr = tsig_rr_create(allocator);
362  if (!xfrd->tsig_rr) {
363  xfrd_cleanup(xfrd, 0);
364  return NULL;
365  }
366  memset(&xfrd->soa, 0, sizeof(xfrd->soa));
367  xfrd->soa.ttl = 0;
368  xfrd->soa.mname[0] = 1;
369  xfrd->soa.rname[0] = 1;
370  xfrd->soa.serial = 0;
371  xfrd->soa.refresh = 3600;
372  xfrd->soa.retry = 300;
373  xfrd->soa.expire = 604800;
374  xfrd->soa.minimum = 3600;
375  xfrd->handler.fd = -1;
376  xfrd->handler.user_data = (void*) xfrd;
377  xfrd->handler.timeout = 0;
378  xfrd->handler.event_types =
380  xfrd->handler.event_handler = xfrd_handle_zone;
381  xfrd_set_timer_time(xfrd, 0);
382  xfrd_recover(xfrd);
383  return xfrd;
384 }
385 
386 
391 static time_t
392 xfrd_time(xfrd_type* xfrd)
393 {
394  ods_log_assert(xfrd);
395  ods_log_assert(xfrd->xfrhandler);
396  return xfrhandler_time((xfrhandler_type*) xfrd->xfrhandler);
397 }
398 
399 
404 static void
405 xfrd_set_timer(xfrd_type* xfrd, time_t t)
406 {
407  if (!xfrd || !xfrd->xfrhandler) {
408  return;
409  }
414  if(t > xfrd_time(xfrd) + 10) {
415  time_t extra = t - xfrd_time(xfrd);
416  time_t base = extra*9/10;
417  t = xfrd_time(xfrd) + base +
418  random()%(extra-base);
419  }
420  xfrd->handler.timeout = &xfrd->timeout;
421  xfrd->timeout.tv_sec = t;
422  xfrd->timeout.tv_nsec = 0;
423  return;
424 }
425 
426 
431 static void
432 xfrd_unset_timer(xfrd_type* xfrd)
433 {
434  ods_log_assert(xfrd);
435  xfrd->handler.timeout = NULL;
436  return;
437 }
438 
439 
444 static void
445 xfrd_set_timer_time(xfrd_type* xfrd, time_t t)
446 {
447  ods_log_assert(xfrd);
448  xfrd_set_timer(xfrd, xfrd_time(xfrd) + t);
449  return;
450 }
451 
452 
457 void
459 {
460  zone_type* zone = NULL;
461  if (!xfrd || !xfrd->zone || !xfrd->xfrhandler) {
462  return;
463  }
464  zone = (zone_type*) xfrd->zone;
465  ods_log_debug("[%s] zone %s sets timer timeout now", xfrd_str,
466  zone->name);
467  xfrd_set_timer_time(xfrd, 0);
468  return;
469 }
470 
471 
476 void
478 {
479  zone_type* zone = NULL;
480  if (!xfrd || !xfrd->zone || !xfrd->xfrhandler) {
481  return;
482  }
483  zone = (zone_type*) xfrd->zone;
484  ods_log_debug("[%s] zone %s sets timer timeout retry %u", xfrd_str,
485  zone->name, (unsigned) xfrd->soa.retry);
486  xfrd_set_timer_time(xfrd, xfrd->soa.retry);
487  return;
488 }
489 
490 
495 void
497 {
498  zone_type* zone = NULL;
499  if (!xfrd || !xfrd->zone || !xfrd->xfrhandler) {
500  return;
501  }
502  zone = (zone_type*) xfrd->zone;
503  ods_log_debug("[%s] zone %s sets timer timeout refresh %u", xfrd_str,
504  zone->name, (unsigned) xfrd->soa.refresh);
505  xfrd_set_timer_time(xfrd, xfrd->soa.refresh);
506  return;
507 }
508 
509 
514 static socklen_t
515 xfrd_acl_sockaddr(acl_type* acl, unsigned int port,
516  struct sockaddr_storage *sck)
517 {
518  ods_log_assert(acl);
519  ods_log_assert(sck);
520  ods_log_assert(port);
521  memset(sck, 0, sizeof(struct sockaddr_storage));
522  if (acl->family == AF_INET6) {
523  struct sockaddr_in6* sa = (struct sockaddr_in6*)sck;
524  sa->sin6_family = AF_INET6;
525  sa->sin6_port = htons(port);
526  sa->sin6_addr = acl->addr.addr6;
527  return sizeof(struct sockaddr_in6);
528  } else {
529  struct sockaddr_in* sa = (struct sockaddr_in*)sck;
530  sa->sin_family = AF_INET;
531  sa->sin_port = htons(port);
532  sa->sin_addr = acl->addr.addr;
533  return sizeof(struct sockaddr_in);
534  }
535  return 0;
536 }
537 
538 
543 socklen_t
544 xfrd_acl_sockaddr_to(acl_type* acl, struct sockaddr_storage *to)
545 {
546  unsigned int port = 0;
547  if (!acl || !to) {
548  return 0;
549  }
550  port = acl->port ? acl->port : (unsigned) atoi(DNS_PORT_STRING);
551  return xfrd_acl_sockaddr(acl, port, to);
552 }
553 
554 
559 static void
560 xfrd_tsig_sign(xfrd_type* xfrd, buffer_type* buffer)
561 {
562  tsig_algo_type* algo = NULL;
563  if (!xfrd || !xfrd->tsig_rr || !xfrd->master || !xfrd->master->tsig ||
564  !xfrd->master->tsig->key || !buffer) {
565  return; /* no tsig configured */
566  }
567  algo = tsig_lookup_algo(xfrd->master->tsig->algorithm);
568  if (!algo) {
569  ods_log_error("[%s] unable to sign request: tsig unknown algorithm "
570  "%s", xfrd_str, xfrd->master->tsig->algorithm);
571  return;
572  }
573  ods_log_assert(algo);
574  tsig_rr_reset(xfrd->tsig_rr, algo, xfrd->master->tsig->key);
575  xfrd->tsig_rr->original_query_id = buffer_pkt_id(buffer);
576  xfrd->tsig_rr->algo_name = ldns_rdf_clone(xfrd->tsig_rr->algo->wf_name);
577  xfrd->tsig_rr->key_name = ldns_rdf_clone(xfrd->tsig_rr->key->dname);
578  log_dname(xfrd->tsig_rr->key_name, "tsig sign query with key", LOG_DEBUG);
579  log_dname(xfrd->tsig_rr->algo_name, "tsig sign query with algorithm",
580  LOG_DEBUG);
581  tsig_rr_prepare(xfrd->tsig_rr);
582  tsig_rr_update(xfrd->tsig_rr, buffer, buffer_position(buffer));
583  tsig_rr_sign(xfrd->tsig_rr);
584  ods_log_debug("[%s] tsig append rr to request id=%u", xfrd_str,
585  buffer_pkt_id(buffer));
586  tsig_rr_append(xfrd->tsig_rr, buffer);
587  buffer_pkt_set_arcount(buffer, buffer_pkt_arcount(buffer)+1);
588  tsig_rr_prepare(xfrd->tsig_rr);
589  return;
590 }
591 
592 
597 static int
598 xfrd_tsig_process(xfrd_type* xfrd, buffer_type* buffer)
599 {
600  zone_type* zone = NULL;
601  int have_tsig = 0;
602  if (!xfrd || !xfrd->tsig_rr || !xfrd->master || !xfrd->master->tsig ||
603  !xfrd->master->tsig->key || !buffer) {
604  return 1; /* no tsig configured */
605  }
606  zone = (zone_type*) xfrd->zone;
607  ods_log_assert(zone);
608  ods_log_assert(zone->name);
609  ods_log_assert(xfrd->master->address);
610  if (!tsig_rr_find(xfrd->tsig_rr, buffer)) {
611  ods_log_error("[%s] unable to process tsig: xfr zone %s from %s "
612  "has malformed tsig rr", xfrd_str, zone->name,
613  xfrd->master->address);
614  return 0;
615  }
616  if (xfrd->tsig_rr->status == TSIG_OK) {
617  have_tsig = 1;
618  if (xfrd->tsig_rr->error_code != LDNS_RCODE_NOERROR) {
619  ods_log_error("[%s] zone %s, from %s has tsig error (%s)",
620  xfrd_str, zone->name, xfrd->master->address,
622  }
623  /* strip the TSIG resource record off... */
624  buffer_set_limit(buffer, xfrd->tsig_rr->position);
625  buffer_pkt_set_arcount(buffer, buffer_pkt_arcount(buffer)-1);
626  }
627  /* keep running the TSIG hash */
628  tsig_rr_update(xfrd->tsig_rr, buffer, buffer_limit(buffer));
629  if (have_tsig) {
630  if (!tsig_rr_verify(xfrd->tsig_rr)) {
631  ods_log_error("[%s] unable to process tsig: xfr zone %s from %s "
632  "has bad tsig signature", xfrd_str, zone->name,
633  xfrd->master->address);
634  return 0;
635  }
636  /* prepare for next tsigs */
637  tsig_rr_prepare(xfrd->tsig_rr);
638  } else if (xfrd->tsig_rr->update_since_last_prepare >
640  /* we allow a number of non-tsig signed packets */
641  ods_log_error("[%s] unable to process tsig: xfr zone %s, from %s "
642  "has too many consecutive packets without tsig", xfrd_str,
643  zone->name, xfrd->master->address);
644  return 0;
645  }
646  if (!have_tsig && xfrd->msg_seq_nr == 0) {
647  ods_log_error("[%s] unable to process tsig: xfr zone %s from %s "
648  "has no tsig in first packet of reply", xfrd_str,
649  zone->name, xfrd->master->address);
650  return 0;
651  }
652  /* process TSIG ok */
653  return 1;
654 }
655 
656 
661 static void
662 xfrd_commit_packet(xfrd_type* xfrd)
663 {
664  zone_type* zone = NULL;
665  char* xfrfile = NULL;
666  FILE* fd = NULL;
667  ods_log_assert(xfrd);
668  zone = (zone_type*) xfrd->zone;
669  xfrfile = ods_build_path(zone->name, ".xfrd", 0, 1);
670  if (!xfrfile) {
671  ods_log_crit("[%s] unable to commit xfr zone %s: build path failed",
672  xfrd_str, zone->name);
673  return;
674  }
675  ods_log_assert(zone);
676  ods_log_assert(zone->name);
677  lock_basic_lock(&zone->zone_lock);
678  lock_basic_lock(&xfrd->rw_lock);
680  /* mark end packet */
681  fd = ods_fopen(xfrfile, NULL, "a");
682  free((void*)xfrfile);
683  if (fd) {
684  fprintf(fd, ";;ENDPACKET\n");
685  ods_fclose(fd);
686  } else {
687  lock_basic_unlock(&xfrd->rw_lock);
690  ods_log_crit("[%s] unable to commit xfr zone %s: ods_fopen() failed "
691  "(%s)", xfrd_str, zone->name, strerror(errno));
692  return;
693  }
694  /* update soa serial management */
695  xfrd->serial_disk = xfrd->msg_new_serial;
696  xfrd->serial_disk_acquired = xfrd_time(xfrd);
697  xfrd->soa.serial = xfrd->serial_disk;
698  if (util_serial_gt(xfrd->serial_disk, xfrd->serial_xfr) &&
700  /* reschedule task */
701  int ret = 0;
702  xfrhandler_type* xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
703  engine_type* engine = (engine_type*) xfrhandler->engine;
704  ods_log_assert(xfrhandler);
705  ods_log_assert(engine);
706  ods_log_debug("[%s] reschedule task for zone %s: disk serial=%u "
707  "acquired=%u, memory serial=%u acquired=%u", xfrd_str,
708  zone->name, xfrd->serial_disk,
709  xfrd->serial_disk_acquired, xfrd->serial_xfr,
710  xfrd->serial_xfr_acquired);
711  ret = zone_reschedule_task(zone, engine->taskq, TASK_READ);
712  if (ret != ODS_STATUS_OK) {
713  ods_log_crit("[%s] unable to reschedule task for zone %s: %s",
714  xfrd_str, zone->name, ods_status2str(ret));
715  } else {
716  engine_wakeup_workers(engine);
717  }
718  }
720  lock_basic_unlock(&xfrd->rw_lock);
722  return;
723 }
724 
725 
730 static void
731 xfrd_dump_packet(xfrd_type* xfrd, buffer_type* buffer)
732 {
733  zone_type* zone = NULL;
734  char* xfrfile = NULL;
735  FILE* fd = NULL;
736  ldns_pkt* pkt = NULL;
737  ldns_status status = LDNS_STATUS_OK;
738  ods_log_assert(buffer);
739  ods_log_assert(xfrd);
740  zone = (zone_type*) xfrd->zone;
741  ods_log_assert(zone);
742  ods_log_assert(zone->name);
743  status = ldns_wire2pkt(&pkt, buffer_begin(buffer), buffer_limit(buffer));
744  if (status != LDNS_STATUS_OK) {
745  ods_log_crit("[%s] unable to dump packet zone %s: ldns_wire2pkt() "
746  "failed (%s)", xfrd_str, zone->name,
747  ldns_get_errorstr_by_id(status));
748  return;
749  }
750  ods_log_assert(pkt);
751  xfrfile = ods_build_path(zone->name, ".xfrd", 0, 1);
752  if (!xfrfile) {
753  ods_log_crit("[%s] unable to dump packet zone %s: build path failed",
754  xfrd_str, zone->name);
755  return;
756  }
757  lock_basic_lock(&xfrd->rw_lock);
758  fd = ods_fopen(xfrfile, NULL, "a");
759  free((void*) xfrfile);
760  if (!fd) {
761  ods_log_crit("[%s] unable to dump packet zone %s: ods_fopen() failed "
762  "(%s)", xfrd_str, zone->name, strerror(errno));
763  lock_basic_unlock(&xfrd->rw_lock);
764  return;
765  }
766  ods_log_assert(fd);
767  if (xfrd->msg_seq_nr == 0) {
768  fprintf(fd, ";;BEGINPACKET\n");
769  }
770  ldns_rr_list_print(fd, ldns_pkt_answer(pkt));
771  ods_fclose(fd);
772  lock_basic_unlock(&xfrd->rw_lock);
773  ldns_pkt_free(pkt);
774  return;
775 }
776 
777 
782 static void
783 xfrd_write_soa(xfrd_type* xfrd, buffer_type* buffer)
784 {
785  zone_type* zone = NULL;
786  size_t rdlength_pos = 0;
787  uint16_t rdlength = 0;
788  ods_log_assert(xfrd);
789  ods_log_assert(buffer);
790  zone = (zone_type*) xfrd->zone;
791  ods_log_assert(zone);
792  ods_log_assert(zone->apex);
793  buffer_write_rdf(buffer, zone->apex);
794  buffer_write_u16(buffer, (uint16_t) LDNS_RR_TYPE_SOA);
795  buffer_write_u16(buffer, (uint16_t) zone->klass);
796  buffer_write_u32(buffer, xfrd->soa.ttl);
797  rdlength_pos = buffer_position(buffer);
798  buffer_skip(buffer, sizeof(rdlength));
799  buffer_write(buffer, xfrd->soa.mname+1, xfrd->soa.mname[0]);
800  buffer_write(buffer, xfrd->soa.rname+1, xfrd->soa.rname[0]);
801  buffer_write_u32(buffer, xfrd->soa.serial);
802  buffer_write_u32(buffer, xfrd->soa.refresh);
803  buffer_write_u32(buffer, xfrd->soa.retry);
804  buffer_write_u32(buffer, xfrd->soa.expire);
805  buffer_write_u32(buffer, xfrd->soa.minimum);
806  rdlength = buffer_position(buffer) - rdlength_pos - sizeof(rdlength);
807  buffer_write_u16_at(buffer, rdlength_pos, rdlength);
808  return;
809 }
810 
811 
816 static void
817 xfrd_update_soa(xfrd_type* xfrd, buffer_type* buffer, uint32_t ttl,
818  uint16_t mname_pos, uint16_t rname_pos,
819  uint32_t refresh, uint32_t retry, uint32_t expire, uint32_t minimum)
820 {
821  zone_type* zone = NULL;
822  ods_log_assert(xfrd);
823  ods_log_assert(buffer);
824  zone = (zone_type*) xfrd->zone;
825  ods_log_assert(zone);
826  ods_log_assert(zone->apex);
827  xfrd->soa.ttl = ttl;
828  xfrd->soa.refresh = refresh;
829  xfrd->soa.retry = retry;
830  xfrd->soa.expire = expire;
831  xfrd->soa.minimum = minimum;
832  buffer_set_position(buffer, mname_pos);
833  if (!(xfrd->soa.mname[0] =
834  buffer_read_dname(buffer, xfrd->soa.mname+1, 1))) {
835  xfrd->soa.mname[0] = 1;
836  xfrd->soa.mname[1] = 0;
837  }
838  buffer_set_position(buffer, rname_pos);
839  if (!(xfrd->soa.rname[0] =
840  buffer_read_dname(buffer, xfrd->soa.rname+1, 1))) {
841  xfrd->soa.rname[0] = 1;
842  xfrd->soa.rname[1] = 0;
843  }
844  return;
845 }
846 
847 
852 static int
853 xfrd_parse_soa(xfrd_type* xfrd, buffer_type* buffer, unsigned rdata_only,
854  unsigned update, uint32_t t, uint32_t* soa_serial)
855 {
856  ldns_rr_type type = LDNS_RR_TYPE_SOA;
857  uint16_t mname_pos = 0;
858  uint16_t rname_pos = 0;
859  uint16_t pos = 0;
860  uint32_t serial = 0;
861  uint32_t refresh = 0;
862  uint32_t retry = 0;
863  uint32_t expire = 0;
864  uint32_t minimum = 0;
865  uint32_t ttl = t;
866  ods_log_assert(xfrd);
867  ods_log_assert(buffer);
868 
869  /* type class ttl */
870  if (!rdata_only) {
871  if (!buffer_available(buffer, 10)) {
872  ods_log_debug("[%s] unable to parse soa: rr too short",
873  xfrd_str);
874  return 0;
875  }
876  type = (ldns_rr_type) buffer_read_u16(buffer);
877  if (type != LDNS_RR_TYPE_SOA) {
878  ods_log_debug("[%s] unable to parse soa: rrtype %u != soa",
879  xfrd_str, (unsigned) type);
880  return 0;
881  }
882  (void)buffer_read_u16(buffer); /* class */
883  ttl = buffer_read_u32(buffer);
884  /* rdata length */
885  if (!buffer_available(buffer, buffer_read_u16(buffer))) {
886  ods_log_debug("[%s] unable to parse soa: rdata too short",
887  xfrd_str);
888  return 0;
889  }
890  }
891  /* MNAME */
892  mname_pos = buffer_position(buffer);
893  if (!buffer_skip_dname(buffer)) {
894  ods_log_debug("[%s] unable to parse soa: bad mname",
895  xfrd_str);
896  return 0;
897  }
898  /* RNAME */
899  rname_pos = buffer_position(buffer);
900  if (!buffer_skip_dname(buffer)) {
901  ods_log_debug("[%s] unable to parse soa: bad rname",
902  xfrd_str);
903  return 0;
904  }
905  serial = buffer_read_u32(buffer);
906  refresh = buffer_read_u32(buffer);
907  retry = buffer_read_u32(buffer);
908  expire = buffer_read_u32(buffer);
909  minimum = buffer_read_u32(buffer);
910  pos = buffer_position(buffer);
911  if (soa_serial) {
912  *soa_serial = serial;
913  }
914  if (update) {
915  xfrd_update_soa(xfrd, buffer, ttl, mname_pos, rname_pos,
916  refresh, retry, expire, minimum);
917  }
918  buffer_set_position(buffer, pos);
919  return 1;
920 }
921 
922 
927 static ods_status
928 xfrd_parse_rrs(xfrd_type* xfrd, buffer_type* buffer, uint16_t count,
929  int* done)
930 {
931  ldns_rr_type type = 0;
932  uint16_t rrlen = 0;
933  uint32_t ttl = 0;
934  uint32_t serial = 0;
935  uint32_t tmp_serial = 0;
936  size_t i = 0;
937  ods_log_assert(xfrd);
938  ods_log_assert(buffer);
939  ods_log_assert(done);
940  for (i=0; i < count; ++i, ++xfrd->msg_rr_count) {
941  if (*done) {
942  return ODS_STATUS_OK;
943  }
944  if (!buffer_skip_dname(buffer)) {
945  return ODS_STATUS_SKIPDNAME;
946  }
947  if (!buffer_available(buffer, 10)) {
948  return ODS_STATUS_BUFAVAIL;
949  }
950  (void)buffer_position(buffer);
951  type = (ldns_rr_type) buffer_read_u16(buffer);
952  (void)buffer_read_u16(buffer); /* class */
953  ttl = buffer_read_u32(buffer);
954  rrlen = buffer_read_u16(buffer);
955  if (!buffer_available(buffer, rrlen)) {
956  return ODS_STATUS_BUFAVAIL;
957  }
958  if (type == LDNS_RR_TYPE_SOA) {
959  if (!xfrd_parse_soa(xfrd, buffer, 1, 0, ttl, &serial)) {
960  return ODS_STATUS_PARSESOA;
961  }
962  if (xfrd->msg_rr_count == 1 && serial != xfrd->msg_new_serial) {
963  /* 2nd RR is SOA with different serial, this is an IXFR */
964  xfrd->msg_is_ixfr = 1;
966  if (!xfrd->serial_disk_acquired) {
968  /* got IXFR but need AXFR */
969  return ODS_STATUS_REQAXFR;
970  }
971  if (serial != xfrd->serial_disk) {
973  /* bad start serial in IXFR */
974  return ODS_STATUS_INSERIAL;
975  }
977  xfrd->msg_old_serial = serial;
978  tmp_serial = serial;
979  } else if (serial == xfrd->msg_new_serial) {
980  /* saw another SOA of new serial. */
981  if (xfrd->msg_is_ixfr == 1) {
982  xfrd->msg_is_ixfr = 2; /* seen middle SOA in ixfr */
983  } else {
984  *done = 1; /* final axfr/ixfr soa */
985  }
986  } else if (xfrd->msg_is_ixfr) {
987  /* some additional checks */
988  if (util_serial_gt(serial, xfrd->msg_new_serial)) {
989  /* bad middle serial in IXFR (too high) */
990  return ODS_STATUS_INSERIAL;
991  }
992  if (util_serial_gt(tmp_serial, serial)) {
993  /* middle serial decreases in IXFR */
994  return ODS_STATUS_INSERIAL;
995  }
996  /* serial ok, update tmp serial */
997  tmp_serial = serial;
998  }
999  } else {
1000  buffer_skip(buffer, rrlen);
1001  }
1002  }
1003  return ODS_STATUS_OK;
1004 }
1005 
1006 
1011 static xfrd_pkt_status
1012 xfrd_parse_packet(xfrd_type* xfrd, buffer_type* buffer)
1013 {
1014  zone_type* zone = NULL;
1015  uint16_t qdcount = 0;
1016  uint16_t ancount = 0;
1017  uint16_t ancount_todo = 0;
1018  uint16_t rrcount = 0;
1019  uint32_t serial = 0;
1020  int done = 0;
1021  ods_status status = ODS_STATUS_OK;
1022  ods_log_assert(buffer);
1023  ods_log_assert(xfrd);
1024  ods_log_assert(xfrd->master);
1025  ods_log_assert(xfrd->master->address);
1026  zone = (zone_type*) xfrd->zone;
1027  ods_log_assert(zone);
1028  ods_log_assert(zone->name);
1029  /* check packet size */
1030  if (!buffer_available(buffer, BUFFER_PKT_HEADER_SIZE)) {
1031  ods_log_error("[%s] unable to parse packet: zone %s received bad "
1032  "packet from %s (too small)", xfrd_str, zone->name,
1033  xfrd->master->address);
1034  return XFRD_PKT_BAD;
1035  }
1036  /* check query id */
1037  if (buffer_pkt_id(buffer) != xfrd->query_id) {
1038  ods_log_error("[%s] bad packet: zone %s received bad query id "
1039  "%u from %s (expected %u)", xfrd_str, zone->name,
1040  buffer_pkt_id(buffer), xfrd->master->address, xfrd->query_id);
1041  return XFRD_PKT_BAD;
1042  }
1043  /* check rcode */
1044  if (buffer_pkt_rcode(buffer) != LDNS_RCODE_NOERROR) {
1045  ods_log_error("[%s] bad packet: zone %s received error code %s from %s",
1046  xfrd_str, zone->name, ldns_pkt_rcode2str(buffer_pkt_rcode(buffer)),
1047  xfrd->master->address);
1048  if (buffer_pkt_rcode(buffer) == LDNS_RCODE_NOTIMPL) {
1049  return XFRD_PKT_NOTIMPL;
1050  } else if (buffer_pkt_rcode(buffer) != LDNS_RCODE_NOTAUTH) {
1051  return XFRD_PKT_BAD;
1052  }
1053  }
1054  /* check tsig */
1055  if (!xfrd_tsig_process(xfrd, buffer)) {
1056  ods_log_error("[%s] bad packet: zone %s received bad tsig "
1057  "from %s", xfrd_str, zone->name, xfrd->master->address);
1058  return XFRD_PKT_BAD;
1059  }
1060  /* skip header and question section */
1062  qdcount = buffer_pkt_qdcount(buffer);
1063  for (rrcount = 0; rrcount < qdcount; rrcount++) {
1064  if (!buffer_skip_rr(buffer, 1)) {
1065  ods_log_error("[%s] bad packet: zone %s received bad "
1066  "question section from %s (bad rr)", xfrd_str, zone->name,
1067  xfrd->master->address);
1068  return XFRD_PKT_BAD;
1069  }
1070  }
1071  /* answer section */
1072  ancount = buffer_pkt_ancount(buffer);
1073  if (xfrd->msg_rr_count == 0 && ancount == 0) {
1074  if (xfrd->tcp_conn == -1 && buffer_pkt_tc(buffer)) {
1075  ods_log_debug("[%s] zone %s received tc from %s, retry tcp",
1076  xfrd_str, zone->name, xfrd->master->address);
1077  return XFRD_PKT_TC;
1078  }
1079  ods_log_error("[%s] bad packet: zone %s received bad xfr packet "
1080  "from %s (nodata)", xfrd_str, zone->name, xfrd->master->address);
1081  return XFRD_PKT_BAD;
1082  }
1083 
1084  ancount_todo = ancount;
1085  if (xfrd->msg_rr_count == 0) {
1086  /* parse the first RR, see if it is a SOA */
1087  if (!buffer_skip_dname(buffer) ||
1088  !xfrd_parse_soa(xfrd, buffer, 0, 1, 0, &serial)) {
1089  ods_log_error("[%s] bad packet: zone %s received bad xfr "
1090  "packet from %s (bad soa)", xfrd_str, zone->name,
1091  xfrd->master->address);
1092  return XFRD_PKT_BAD;
1093  }
1094  /* check serial */
1095  lock_basic_lock(&xfrd->serial_lock);
1096  if (xfrd->serial_disk_acquired && xfrd->serial_disk == serial) {
1097  ods_log_debug("[%s] zone %s got update indicating current "
1098  "serial %u from %s", xfrd_str, zone->name, serial,
1099  xfrd->master->address);
1100  xfrd->serial_disk_acquired = xfrd_time(xfrd);
1101  if (xfrd->serial_xfr == serial) {
1103  if (!xfrd->serial_notify_acquired) {
1104  /* not notified or anything, so stop asking around */
1105  xfrd->round_num = -1; /* next try start a new round */
1106  xfrd_set_timer_refresh(xfrd);
1107  ods_log_debug("[%s] zone %s wait refresh time", xfrd_str,
1108  zone->name);
1110  return XFRD_PKT_NEWLEASE;
1111  }
1112  /* try next master */
1113  ods_log_debug("[%s] zone %s try next master", xfrd_str,
1114  zone->name);
1116  return XFRD_PKT_BAD;
1117  }
1118  }
1119  if (xfrd->serial_disk_acquired &&
1120  !util_serial_gt(serial, xfrd->serial_disk)) {
1121  ods_log_debug("[%s] zone %s ignoring old serial %u from %s "
1122  "(have %u)", xfrd_str, zone->name, serial,
1123  xfrd->master->address, xfrd->serial_disk);
1125  return XFRD_PKT_BAD;
1126  }
1127 
1128  xfrd->msg_new_serial = serial;
1129  if (xfrd->serial_disk_acquired) {
1130  xfrd->msg_old_serial = xfrd->serial_disk;
1131  } else {
1132  xfrd->msg_old_serial = 0;
1133  }
1134  /* update notify serial if this xfr is newer */
1135  if (ancount > 1 && xfrd->serial_notify_acquired &&
1136  util_serial_gt(serial, xfrd->serial_notify)) {
1137  xfrd->serial_notify = serial;
1138  }
1140  xfrd->msg_rr_count = 1;
1141  xfrd->msg_is_ixfr = 0;
1142  ancount_todo = ancount - 1;
1143  }
1144  /* check tc bit */
1145  if (xfrd->tcp_conn == -1 && buffer_pkt_tc(buffer)) {
1146  ods_log_debug("[%s] zone %s received tc from %s, retry tcp",
1147  xfrd_str, zone->name, xfrd->master->address);
1148  return XFRD_PKT_TC;
1149  }
1150  if (xfrd->tcp_conn == -1 && ancount < 2) {
1151  /* too short to be a real ixfr/axfr data transfer */
1152  ods_log_debug("[%s] zone %s received too short udp reply from %s, "
1153  "retry tcp", xfrd_str, zone->name, xfrd->master->address);
1154  return XFRD_PKT_TC;
1155  }
1156  status = xfrd_parse_rrs(xfrd, buffer, ancount_todo, &done);
1157  if (status != ODS_STATUS_OK) {
1158  ods_log_error("[%s] bad packet: zone %s received bad xfr packet "
1159  "from %s (%s)", xfrd_str, zone->name, xfrd->master->address,
1160  ods_status2str(status));
1161  return XFRD_PKT_BAD;
1162  }
1163  if (xfrd->tcp_conn == -1 && !done) {
1164  ods_log_error("[%s] bad packet: zone %s received bad xfr packet "
1165  "(xfr over udp incomplete)", xfrd_str, zone->name,
1166  xfrd->master->address);
1167  return XFRD_PKT_BAD;
1168  }
1169  if (!done) {
1170  return XFRD_PKT_MORE;
1171  }
1172  return XFRD_PKT_XFR;
1173 }
1174 
1175 
1180 static xfrd_pkt_status
1181 xfrd_handle_packet(xfrd_type* xfrd, buffer_type* buffer)
1182 {
1184  zone_type* zone = NULL;
1185  ods_log_assert(xfrd);
1186  ods_log_assert(xfrd->master);
1187  ods_log_assert(xfrd->master->address);
1188  zone = (zone_type*) xfrd->zone;
1189  ods_log_assert(zone);
1190  ods_log_assert(zone->name);
1191  res = xfrd_parse_packet(xfrd, buffer);
1192  ods_log_debug("[%s] zone %s xfr packet parsed (res %d)", xfrd_str, zone->name, res);
1193 
1194  switch (res) {
1195  case XFRD_PKT_MORE:
1196  case XFRD_PKT_XFR:
1197  /* continue with commit */
1198  break;
1199  case XFRD_PKT_NEWLEASE:
1200  case XFRD_PKT_TC:
1201  return res;
1202  break;
1203  case XFRD_PKT_NOTIMPL:
1204  case XFRD_PKT_BAD:
1205  default:
1206  /* rollback */
1207  if (xfrd->msg_seq_nr > 0) {
1208  buffer_clear(buffer);
1209  ods_log_info("[%s] zone %s xfr rollback", xfrd_str,
1210  zone->name);
1211  buffer_flip(buffer);
1212  }
1213  return res;
1214  break;
1215  }
1216  /* dump reply on disk to diff file */
1217  xfrd_dump_packet(xfrd, buffer);
1218  /* more? */
1219  xfrd->msg_seq_nr++;
1220  if (res == XFRD_PKT_MORE) {
1221  /* wait for more */
1222  return XFRD_PKT_MORE;
1223  }
1224  /* done */
1225  buffer_clear(buffer);
1226  buffer_flip(buffer);
1227  /* commit packet */
1228  xfrd_commit_packet(xfrd);
1229  /* next time */
1230  lock_basic_lock(&xfrd->serial_lock);
1231 
1232  ods_log_debug("[%s] zone %s notify acquired %u, serial on disk %u, "
1233  "notify serial %u", xfrd_str, zone->name,
1234  xfrd->serial_notify_acquired, xfrd->serial_disk,
1235  xfrd->serial_notify);
1236 
1237  if (xfrd->serial_notify_acquired &&
1238  !util_serial_gt(xfrd->serial_notify, xfrd->serial_disk)) {
1239  ods_log_debug("[%s] zone %s reset notify acquired", xfrd_str,
1240  zone->name);
1241  xfrd->serial_notify_acquired = 0;
1242  }
1243  if (!xfrd->serial_notify_acquired) {
1244  ods_log_debug("[%s] zone %s xfr done", xfrd_str, zone->name);
1245  xfrd->round_num = -1; /* next try start anew */
1246  xfrd_set_timer_refresh(xfrd);
1248  return XFRD_PKT_XFR;
1249  }
1251  /* try to get an even newer serial */
1252  ods_log_debug("[%s] zone %s get newer serial", xfrd_str, zone->name);
1253  return XFRD_PKT_BAD;
1254 }
1255 
1256 
1264 static void
1265 xfrd_tcp_write(xfrd_type* xfrd, tcp_set_type* set)
1266 {
1267  zone_type* zone = NULL;
1268  tcp_conn_type* tcp = NULL;
1269  int ret = 0;
1270  int error = 0;
1271  socklen_t len = 0;
1272 
1273  ods_log_assert(set);
1274  ods_log_assert(xfrd);
1275  ods_log_assert(xfrd->tcp_conn != -1);
1276  zone = (zone_type*) xfrd->zone;
1277  ods_log_assert(zone);
1278  ods_log_assert(zone->name);
1279  tcp = set->tcp_conn[xfrd->tcp_conn];
1280  if (tcp->total_bytes == 0) {
1281  /* check for pending error from nonblocking connect */
1282  /* from Stevens, unix network programming, vol1, 3rd ed, p450 */
1283  len = sizeof(error);
1284  if (getsockopt(tcp->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
1285  error = errno; /* on solaris errno is error */
1286  }
1287  if (error == EINPROGRESS || error == EWOULDBLOCK) {
1288  ods_log_debug("[%s] zone %s zero write, write again later (%s)",
1289  xfrd_str, zone->name, strerror(error));
1290  return; /* try again later */
1291  }
1292  if (error != 0) {
1293  ods_log_error("[%s] zone %s cannot tcp connect to %s: %s",
1294  xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1295  xfrd_set_timer_now(xfrd);
1296  xfrd_tcp_release(xfrd, set);
1297  return;
1298  }
1299  }
1300  ret = tcp_conn_write(tcp);
1301  if(ret == -1) {
1302  ods_log_error("[%s] zone %s cannot tcp write to %s: %s",
1303  xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1304  xfrd_set_timer_now(xfrd);
1305  xfrd_tcp_release(xfrd, set);
1306  return;
1307  }
1308  if (ret == 0) {
1309  ods_log_debug("[%s] zone %s zero write, write again later",
1310  xfrd_str, zone->name);
1311  return; /* write again later */
1312  }
1313  /* done writing, get ready for reading */
1314  ods_log_debug("[%s] zone %s done writing, get ready for reading",
1315  xfrd_str, zone->name);
1316  tcp->is_reading = 1;
1317  tcp_conn_ready(tcp);
1319  xfrd_tcp_read(xfrd, set);
1320  return;
1321 }
1322 
1323 
1328 static int
1329 xfrd_tcp_open(xfrd_type* xfrd, tcp_set_type* set)
1330 {
1331  int fd, family, conn;
1332  struct sockaddr_storage to;
1333  socklen_t to_len;
1334  zone_type* zone = NULL;
1335 
1336  ods_log_assert(set);
1337  ods_log_assert(xfrd);
1338  ods_log_assert(xfrd->tcp_conn != -1);
1339  ods_log_assert(xfrd->master);
1340  ods_log_assert(xfrd->master->address);
1341  zone = (zone_type*) xfrd->zone;
1342  ods_log_assert(zone);
1343  ods_log_assert(zone->name);
1344  ods_log_debug("[%s] zone %s open tcp connection to %s", xfrd_str,
1345  zone->name, xfrd->master->address);
1346  set->tcp_conn[xfrd->tcp_conn]->is_reading = 0;
1347  set->tcp_conn[xfrd->tcp_conn]->total_bytes = 0;
1348  set->tcp_conn[xfrd->tcp_conn]->msglen = 0;
1349  if (xfrd->master->family == AF_INET6) {
1350  family = PF_INET6;
1351  } else {
1352  family = PF_INET;
1353  }
1354  fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
1355  set->tcp_conn[xfrd->tcp_conn]->fd = fd;
1356  if (fd == -1) {
1357  ods_log_error("[%s] zone %s cannot create tcp socket to %s: %s",
1358  xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1359  xfrd_set_timer_now(xfrd);
1360  xfrd_tcp_release(xfrd, set);
1361  return 0;
1362  }
1363  if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
1364  ods_log_error("[%s] zone %s cannot fcntl tcp socket to %s: %s",
1365  xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1366  xfrd_set_timer_now(xfrd);
1367  xfrd_tcp_release(xfrd, set);
1368  return 0;
1369  }
1370  to_len = xfrd_acl_sockaddr_to(xfrd->master, &to);
1371  /* bind it? */
1372 
1373  conn = connect(fd, (struct sockaddr*)&to, to_len);
1374  if (conn == -1 && errno != EINPROGRESS) {
1375  ods_log_error("[%s] zone %s cannot connect tcp socket to %s: %s",
1376  xfrd_str, zone->name, xfrd->master->address, strerror(errno));
1377  xfrd_set_timer_now(xfrd);
1378  xfrd_tcp_release(xfrd, set);
1379  return 0;
1380  }
1381  xfrd->handler.fd = fd;
1383  xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1384  return 1;
1385 }
1386 
1387 
1392 static void
1393 xfrd_tcp_obtain(xfrd_type* xfrd, tcp_set_type* set)
1394 {
1395  int i = 0;
1396 
1397  ods_log_assert(set);
1398  ods_log_assert(xfrd);
1399  ods_log_assert(xfrd->tcp_conn == -1);
1400  ods_log_assert(xfrd->tcp_waiting == 0);
1401  if (set->tcp_count < TCPSET_MAX) {
1402  ods_log_assert(!set->tcp_waiting_first);
1403  set->tcp_count ++;
1404  /* find a free tcp_buffer */
1405  for (i=0; i < TCPSET_MAX; i++) {
1406  if (set->tcp_conn[i]->fd == -1) {
1407  xfrd->tcp_conn = i;
1408  break;
1409  }
1410  }
1411  ods_log_assert(xfrd->tcp_conn != -1);
1412  xfrd->tcp_waiting = 0;
1413  /* stop udp use (if any) */
1414  if (xfrd->handler.fd != -1) {
1415  xfrd_udp_release(xfrd);
1416  }
1417  if (!xfrd_tcp_open(xfrd, set)) {
1418  return;
1419  }
1420  xfrd_tcp_xfr(xfrd, set);
1421  return;
1422  }
1423  /* wait, at end of line */
1424  ods_log_verbose("[%s] max number of tcp connections (%d) reached",
1425  xfrd_str, TCPSET_MAX);
1426  xfrd->tcp_waiting = 1;
1427  xfrd_unset_timer(xfrd);
1428  return;
1429 }
1430 
1431 
1436 static void
1437 xfrd_tcp_xfr(xfrd_type* xfrd, tcp_set_type* set)
1438 {
1439  tcp_conn_type* tcp = NULL;
1440  zone_type* zone = NULL;
1441 
1442  ods_log_assert(set);
1443  ods_log_assert(xfrd);
1444  zone = (zone_type*) xfrd->zone;
1445  ods_log_assert(zone);
1446  ods_log_assert(zone->name);
1447  ods_log_assert(xfrd->tcp_conn != -1);
1448  ods_log_assert(xfrd->tcp_waiting == 0);
1449  ods_log_assert(xfrd->master);
1450  ods_log_assert(xfrd->master->address);
1451  /* start AXFR or IXFR for the zone */
1452  tcp = set->tcp_conn[xfrd->tcp_conn];
1453 
1454  if (xfrd->serial_xfr_acquired <= 0 || xfrd->master->ixfr_disabled) {
1455  ods_log_debug("[%s] zone %s request axfr to %s", xfrd_str,
1456  zone->name, xfrd->master->address);
1457  buffer_pkt_query(tcp->packet, zone->apex, LDNS_RR_TYPE_AXFR,
1458  zone->klass);
1459  } else {
1460  ods_log_debug("[%s] zone %s request ixfr to %s", xfrd_str,
1461  zone->name, xfrd->master->address);
1462  buffer_pkt_query(tcp->packet, zone->apex, LDNS_RR_TYPE_IXFR,
1463  zone->klass);
1464  buffer_pkt_set_nscount(tcp->packet, 1);
1465  xfrd_write_soa(xfrd, tcp->packet);
1466  }
1467  /* make packet */
1468  xfrd->query_id = buffer_pkt_id(tcp->packet);
1469  xfrd->msg_seq_nr = 0;
1470  xfrd->msg_rr_count = 0;
1471  xfrd->msg_old_serial = 0;
1472  xfrd->msg_new_serial = 0;
1473  xfrd->msg_is_ixfr = 0;
1474  xfrd_tsig_sign(xfrd, tcp->packet);
1475  buffer_flip(tcp->packet);
1476  tcp->msglen = buffer_limit(tcp->packet);
1477  ods_log_debug("[%s] zone %s sending tcp query id=%d", xfrd_str,
1478  zone->name, xfrd->query_id);
1479  /* wait for select to complete connect before write */
1480  return;
1481 }
1482 
1483 
1488 static void
1489 xfrd_tcp_read(xfrd_type* xfrd, tcp_set_type* set)
1490 {
1491  tcp_conn_type* tcp = NULL;
1492  int ret = 0;
1493 
1494  ods_log_assert(set);
1495  ods_log_assert(xfrd);
1496  ods_log_assert(xfrd->tcp_conn != -1);
1497  tcp = set->tcp_conn[xfrd->tcp_conn];
1498  ret = tcp_conn_read(tcp);
1499  if (ret == -1) {
1500  xfrd_set_timer_now(xfrd);
1501  xfrd_tcp_release(xfrd, set);
1502  return;
1503  }
1504  if (ret == 0) {
1505  return;
1506  }
1507  /* completed msg */
1508  buffer_flip(tcp->packet);
1509  ret = xfrd_handle_packet(xfrd, tcp->packet);
1510  switch (ret) {
1511  case XFRD_PKT_MORE:
1512  tcp_conn_ready(tcp);
1513  break;
1514  case XFRD_PKT_XFR:
1515  case XFRD_PKT_NEWLEASE:
1516  ods_log_debug("[%s] tcp read %s: release connection", xfrd_str,
1517  XFRD_PKT_XFR?"xfr":"newlease");
1518  xfrd_tcp_release(xfrd, set);
1519  ods_log_assert(xfrd->round_num == -1);
1520  break;
1521  case XFRD_PKT_NOTIMPL:
1522  xfrd->master->ixfr_disabled = time_now();
1523  ods_log_debug("[%s] disable ixfr requests for %s from now (%u)",
1524  xfrd_str, xfrd->master->address, xfrd->master->ixfr_disabled);
1525  /* break; */
1526  case XFRD_PKT_BAD:
1527  default:
1528  ods_log_debug("[%s] tcp read %s: release connection", xfrd_str,
1529  ret==XFRD_PKT_BAD?"bad":"notimpl");
1530  xfrd_tcp_release(xfrd, set);
1531  xfrd_make_request(xfrd);
1532  break;
1533  }
1534  return;
1535 }
1536 
1537 
1542 static void
1543 xfrd_tcp_release(xfrd_type* xfrd, tcp_set_type* set)
1544 {
1545  int conn = 0;
1546  zone_type* zone = NULL;
1547 
1548  ods_log_assert(set);
1549  ods_log_assert(xfrd);
1550  ods_log_assert(xfrd->master);
1551  ods_log_assert(xfrd->master->address);
1552  ods_log_assert(xfrd->tcp_conn != -1);
1553  ods_log_assert(xfrd->tcp_waiting == 0);
1554  zone = (zone_type*) xfrd->zone;
1555  ods_log_debug("[%s] zone %s release tcp connection to %s", xfrd_str,
1556  zone->name, xfrd->master->address);
1557  conn = xfrd->tcp_conn;
1558  xfrd->tcp_conn = -1;
1559  xfrd->tcp_waiting = 0;
1560  xfrd->handler.fd = -1;
1562 
1563  if (set->tcp_conn[conn]->fd != -1) {
1564  close(set->tcp_conn[conn]->fd);
1565  }
1566  set->tcp_conn[conn]->fd = -1;
1567  set->tcp_count --;
1568  return;
1569 }
1570 
1571 
1579 static int
1580 xfrd_udp_send(xfrd_type* xfrd, buffer_type* buffer)
1581 {
1582  struct sockaddr_storage to;
1583  socklen_t to_len = 0;
1584  int fd = -1;
1585  int family = PF_INET;
1586  ssize_t nb = -1;
1587  ods_log_assert(buffer);
1588  ods_log_assert(xfrd);
1589  ods_log_assert(xfrd->master);
1590  ods_log_assert(xfrd->master->address);
1591  /* this will set the remote port to acl->port or TCP_PORT */
1592  to_len = xfrd_acl_sockaddr_to(xfrd->master, &to);
1593  /* get the address family of the remote host */
1594  if (xfrd->master->family == AF_INET6) {
1595  family = PF_INET6;
1596  }
1597  /* create socket */
1598  fd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
1599  if (fd == -1) {
1600  ods_log_error("[%s] unable to send data over udp to %s: "
1601  "socket() failed (%s)", xfrd_str, xfrd->master->address,
1602  strerror(errno));
1603  return -1;
1604  }
1605  /* bind it? */
1606 
1607  /* send it (udp) */
1608  ods_log_deeebug("[%s] send %d bytes over udp to %s", xfrd_str,
1609  buffer_remaining(buffer), xfrd->master->address);
1610  nb = sendto(fd, buffer_current(buffer), buffer_remaining(buffer), 0,
1611  (struct sockaddr*)&to, to_len);
1612  if (nb == -1) {
1613  ods_log_error("[%s] unable to send data over udp to %s: "
1614  "sendto() failed (%s)", xfrd_str, xfrd->master->address,
1615  strerror(errno));
1616  close(fd);
1617  return -1;
1618  }
1619  return fd;
1620 }
1621 
1622 
1627 static int
1628 xfrd_udp_send_request_ixfr(xfrd_type* xfrd)
1629 {
1630  int fd;
1631  xfrhandler_type* xfrhandler = NULL;
1632  zone_type* zone = NULL;
1633  ods_log_assert(xfrd);
1634  ods_log_assert(xfrd->master);
1635  ods_log_assert(xfrd->master->address);
1636  zone = (zone_type*) xfrd->zone;
1637  ods_log_assert(zone);
1638  ods_log_assert(zone->name);
1639  if (xfrd->tcp_conn != -1) {
1640  /* tcp is using the handler.fd */
1641  ods_log_error("[%s] unable to transfer zone %s: tried to send "
1642  "udp while tcp obtained", xfrd_str, zone->name);
1643  return -1;
1644  }
1645  /* make packet */
1646  xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1647  ods_log_assert(xfrhandler);
1648  buffer_pkt_query(xfrhandler->packet, zone->apex, LDNS_RR_TYPE_IXFR,
1649  zone->klass);
1650  xfrd->query_id = buffer_pkt_id(xfrhandler->packet);
1651  xfrd->msg_seq_nr = 0;
1652  xfrd->msg_rr_count = 0;
1653  xfrd->msg_old_serial = 0;
1654  xfrd->msg_new_serial = 0;
1655  xfrd->msg_is_ixfr = 0;
1656  buffer_pkt_set_nscount(xfrhandler->packet, 1);
1657  xfrd_write_soa(xfrd, xfrhandler->packet);
1658  xfrd_tsig_sign(xfrd, xfrhandler->packet);
1659  buffer_flip(xfrhandler->packet);
1660  xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_UDP_TIMEOUT);
1661  ods_log_debug("[%s] zone %s sending udp query id=%d qtype=IXFR to %s",
1662  xfrd_str, zone->name, xfrd->query_id, xfrd->master->address);
1663  if((fd = xfrd_udp_send(xfrd, xfrhandler->packet)) == -1) {
1664  return -1;
1665  }
1666  return fd;
1667 }
1668 
1673 static void
1674 xfrd_udp_obtain(xfrd_type* xfrd)
1675 {
1676  xfrhandler_type* xfrhandler = NULL;
1677  ods_log_assert(xfrd);
1678  ods_log_assert(xfrd->xfrhandler);
1679  ods_log_assert(xfrd->udp_waiting == 0);
1680  xfrhandler = (void*) xfrd->xfrhandler;
1681  if (xfrd->tcp_conn != -1) {
1682  /* no tcp and udp at the same time */
1683  xfrd_tcp_release(xfrd, xfrhandler->tcp_set);
1684  }
1685  if (xfrhandler->udp_use_num < XFRD_MAX_UDP) {
1686  xfrhandler->udp_use_num++;
1687  xfrd->handler.fd = xfrd_udp_send_request_ixfr(xfrd);
1688  if (xfrd->handler.fd == -1) {
1689  xfrhandler->udp_use_num--;
1690  }
1691  return;
1692  }
1693  /* queue the zone as last */
1694  xfrd->udp_waiting = 1;
1695  xfrd->udp_waiting_next = NULL;
1696  if (!xfrhandler->udp_waiting_first) {
1697  xfrhandler->udp_waiting_first = xfrd;
1698  }
1699  if (xfrhandler->udp_waiting_last) {
1700  xfrhandler->udp_waiting_last->udp_waiting_next = xfrd;
1701  }
1702  xfrhandler->udp_waiting_last = xfrd;
1703  xfrd_unset_timer(xfrd);
1704  return;
1705 }
1706 
1707 
1712 static int
1713 xfrd_udp_read_packet(xfrd_type* xfrd)
1714 {
1715  xfrhandler_type* xfrhandler = NULL;
1716  ssize_t received = 0;
1717  ods_log_assert(xfrd);
1718  xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1719  ods_log_assert(xfrhandler);
1720  /* read the data */
1721  buffer_clear(xfrhandler->packet);
1722  received = recvfrom(xfrd->handler.fd, buffer_begin(xfrhandler->packet),
1723  buffer_remaining(xfrhandler->packet), 0, NULL, NULL);
1724  if (received == -1) {
1725  ods_log_error("[%s] unable to read packet: recvfrom() failed fd %d "
1726  "(%s)", xfrd_str, xfrd->handler.fd, strerror(errno));
1727  return 0;
1728  }
1729  buffer_set_limit(xfrhandler->packet, received);
1730  return 1;
1731 }
1732 
1733 
1738 static void
1739 xfrd_udp_read(xfrd_type* xfrd)
1740 {
1741  xfrhandler_type* xfrhandler = NULL;
1742  zone_type* zone = NULL;
1744  ods_log_assert(xfrd);
1745  zone = (zone_type*) xfrd->zone;
1746  ods_log_assert(zone);
1747  ods_log_assert(zone->name);
1748  ods_log_debug("[%s] zone %s read data from udp", xfrd_str,
1749  zone->name);
1750  if (!xfrd_udp_read_packet(xfrd)) {
1751  ods_log_error("[%s] unable to read data from udp zone %s: "
1752  "xfrd_udp_read_packet() failed", xfrd_str, zone->name);
1753  xfrd_udp_release(xfrd);
1754  return;
1755  }
1756  xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1757  ods_log_assert(xfrhandler);
1758  res = xfrd_handle_packet(xfrd, xfrhandler->packet);
1759  switch (res) {
1760  case XFRD_PKT_TC:
1761  ods_log_debug("[%s] truncation from %s",
1762  xfrd_str, xfrd->master->address);
1763  xfrd_udp_release(xfrd);
1764  xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1765  xfrd_tcp_obtain(xfrd, xfrhandler->tcp_set);
1766  break;
1767  case XFRD_PKT_XFR:
1768  case XFRD_PKT_NEWLEASE:
1769  ods_log_debug("[%s] xfr/newlease from %s",
1770  xfrd_str, xfrd->master->address);
1771  /* nothing more to do */
1772  ods_log_assert(xfrd->round_num == -1);
1773  xfrd_udp_release(xfrd);
1774  break;
1775  case XFRD_PKT_NOTIMPL:
1776  xfrd->master->ixfr_disabled = time_now();
1777  ods_log_debug("[%s] disable ixfr requests for %s from now (%u)",
1778  xfrd_str, xfrd->master->address, xfrd->master->ixfr_disabled);
1779  /* break; */
1780  case XFRD_PKT_BAD:
1781  default:
1782  ods_log_debug("[%s] bad ixfr packet from %s",
1783  xfrd_str, xfrd->master->address);
1784  xfrd_udp_release(xfrd);
1785  xfrd_make_request(xfrd);
1786  break;
1787  }
1788  return;
1789 }
1790 
1791 
1796 static void
1797 xfrd_udp_release(xfrd_type* xfrd)
1798 {
1799  xfrhandler_type* xfrhandler = NULL;
1800 
1801  ods_log_assert(xfrd);
1802  ods_log_assert(xfrd->udp_waiting == 0);
1803  if(xfrd->handler.fd != -1)
1804  close(xfrd->handler.fd);
1805  xfrd->handler.fd = -1;
1806  xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1807  ods_log_assert(xfrhandler);
1808  /* see if there are waiting zones */
1809  if (xfrhandler->udp_use_num == XFRD_MAX_UDP) {
1810  while (xfrhandler->udp_waiting_first) {
1811  /* snip off waiting list */
1812  xfrd_type* wf = xfrhandler->udp_waiting_first;
1814  wf->udp_waiting = 0;
1815  xfrhandler->udp_waiting_first = wf->udp_waiting_next;
1816  if (xfrhandler->udp_waiting_last == wf) {
1817  xfrhandler->udp_waiting_last = NULL;
1818  }
1819  /* see if this zone needs udp connection */
1820  if (wf->tcp_conn == -1) {
1821  wf->handler.fd = xfrd_udp_send_request_ixfr(wf);
1822  if (wf->handler.fd != -1) {
1823  return;
1824  }
1825  }
1826  }
1827  }
1828  /* no waiting zones */
1829  if (xfrhandler->udp_use_num > 0) {
1830  xfrhandler->udp_use_num --;
1831  }
1832  return;
1833 }
1834 
1835 
1840 static void
1841 xfrd_make_request(xfrd_type* xfrd)
1842 {
1843  zone_type* zone = NULL;
1844  dnsin_type* dnsin = NULL;
1845  if (!xfrd || !xfrd->xfrhandler) {
1846  return;
1847  }
1848  zone = (zone_type*) xfrd->zone;
1849  ods_log_assert(zone);
1850  ods_log_assert(zone->name);
1851  ods_log_assert(zone->adinbound);
1854 
1855  dnsin = (dnsin_type*) zone->adinbound->config;
1856  if (xfrd->next_master != -1) {
1857  /* we are told to use this next master */
1858  xfrd->master_num = xfrd->next_master;
1859  xfrd->master = NULL; /* acl_find_num(...) */
1860  /* if there is no next master, fallback to use the first one */
1861  if (!xfrd->master) {
1862  xfrd->master = dnsin->request_xfr;
1863  xfrd->master_num = 0;
1864  }
1865  /* fallback to cycle master */
1866  xfrd->next_master = -1;
1867  xfrd->round_num = 0; /* fresh set of retries after notify */
1868  } else {
1869  /* cycle master */
1870  if (xfrd->round_num != -1 && xfrd->master &&
1871  xfrd->master->next) {
1872  /* try the next master */
1873  xfrd->master = xfrd->master->next;
1874  xfrd->master_num++;
1875  } else {
1876  /* start a new round */
1877  xfrd->master = dnsin->request_xfr;
1878  xfrd->master_num = 0;
1879  xfrd->round_num++;
1880  }
1881  if (xfrd->round_num >= XFRD_MAX_ROUNDS) {
1882  /* tried all servers that many times, wait */
1883  xfrd->round_num = -1;
1884  xfrd_set_timer_retry(xfrd);
1885  ods_log_verbose("[%s] zone %s make request wait retry",
1886  xfrd_str, zone->name);
1887  return;
1888  }
1889  }
1890  if (!xfrd->master) {
1891  ods_log_debug("[%s] unable to make request for zone %s: no master",
1892  xfrd_str, zone->name);
1893  xfrd->round_num = -1;
1894  xfrd_set_timer_retry(xfrd);
1895  return;
1896  }
1897  /* cache ixfr_disabled only for XFRD_NO_IXFR_CACHE time */
1898  if (xfrd->master->ixfr_disabled &&
1900  xfrd_time(xfrd)) {
1901  ods_log_verbose("[%s] clear negative caching ixfr disabled for "
1902  "master %s", xfrd_str, xfrd->master->address);
1903  ods_log_debug("[%s] clear negative caching calc: %u + %u <= %u",
1904  xfrd_str, xfrd->master->ixfr_disabled, XFRD_NO_IXFR_CACHE,
1905  xfrd_time(xfrd));
1906  xfrd->master->ixfr_disabled = 0;
1907  }
1908  /* perform xfr request */
1909  ods_log_debug("[%s] zone %s make request round %d master %s:%u",
1910  xfrd_str, zone->name, xfrd->round_num, xfrd->master->address,
1911  xfrd->master->port);
1912  if (xfrd->serial_xfr_acquired && !xfrd->master->ixfr_disabled) {
1913  xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_UDP_TIMEOUT);
1914  xfrd_udp_obtain(xfrd);
1915  } else if (!xfrd->serial_xfr_acquired || xfrd->master->ixfr_disabled) {
1916  xfrhandler_type* xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1917  ods_log_assert(xfrhandler);
1918  xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1919  xfrd_tcp_obtain(xfrd, xfrhandler->tcp_set);
1920  }
1921  return;
1922 }
1923 
1924 
1929 static void
1930 xfrd_handle_zone(netio_type* ATTR_UNUSED(netio),
1931  netio_handler_type* handler, netio_events_type event_types)
1932 {
1933  xfrd_type* xfrd = NULL;
1934  zone_type* zone = NULL;
1935 
1936  if (!handler) {
1937  return;
1938  }
1939  xfrd = (xfrd_type*) handler->user_data;
1940  ods_log_assert(xfrd);
1941  zone = (zone_type*) xfrd->zone;
1942  ods_log_assert(zone);
1943  ods_log_assert(zone->name);
1944 
1945  if (xfrd->tcp_conn != -1) {
1946  /* busy in tcp transaction */
1947  xfrhandler_type* xfrhandler = (xfrhandler_type*) xfrd->xfrhandler;
1948  ods_log_assert(xfrhandler);
1949  if (event_types & NETIO_EVENT_READ) {
1950  ods_log_deeebug("[%s] zone %s event tcp read", xfrd_str, zone->name);
1951  xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1952  xfrd_tcp_read(xfrd, xfrhandler->tcp_set);
1953  return;
1954  } else if (event_types & NETIO_EVENT_WRITE) {
1955  ods_log_deeebug("[%s] zone %s event tcp write", xfrd_str,
1956  zone->name);
1957  xfrd_set_timer(xfrd, xfrd_time(xfrd) + XFRD_TCP_TIMEOUT);
1958  xfrd_tcp_write(xfrd, xfrhandler->tcp_set);
1959  return;
1960  } else if (event_types & NETIO_EVENT_TIMEOUT) {
1961  /* tcp connection timed out. Stop it. */
1962  ods_log_deeebug("[%s] zone %s event tcp timeout", xfrd_str,
1963  zone->name);
1964  xfrd_tcp_release(xfrd, xfrhandler->tcp_set);
1965  /* continue to retry; as if a timeout happened */
1966  event_types = NETIO_EVENT_TIMEOUT;
1967  }
1968  }
1969 
1970  if (event_types & NETIO_EVENT_READ) {
1971  /* busy in udp transaction */
1972  ods_log_deeebug("[%s] zone %s event udp read", xfrd_str,
1973  zone->name);
1974  xfrd_set_timer_now(xfrd);
1975  xfrd_udp_read(xfrd);
1976  return;
1977  }
1978 
1979  /* timeout */
1980  ods_log_deeebug("[%s] zone %s timeout", xfrd_str, zone->name);
1981  if (handler->fd != -1) {
1982  ods_log_assert(xfrd->tcp_conn == -1);
1983  xfrd_udp_release(xfrd);
1984  }
1985  if (xfrd->tcp_waiting) {
1986  ods_log_deeebug("[%s] zone %s skips retry: tcp connections full",
1987  xfrd_str, zone->name);
1988  xfrd_unset_timer(xfrd);
1989  return;
1990  }
1991  if (xfrd->udp_waiting) {
1992  ods_log_deeebug("[%s] zone %s skips retry: udp connections full",
1993  xfrd_str, zone->name);
1994  xfrd_unset_timer(xfrd);
1995  return;
1996  }
1997  /* make a new request */
1998  xfrd_make_request(xfrd);
1999  return;
2000 }
2001 
2002 
2007 static void
2008 xfrd_backup_dname(FILE* out, uint8_t* dname)
2009 {
2010  uint8_t* d= dname+1;
2011  uint8_t len = *d++;
2012  uint8_t i;
2013  if (dname[0]<=1) {
2014  fprintf(out, ".");
2015  return;
2016  }
2017  while (len) {
2018  ods_log_assert(d - (dname+1) <= dname[0]);
2019  for (i=0; i<len; i++) {
2020  uint8_t ch = *d++;
2021  if (isalnum(ch) || ch == '-' || ch == '_') {
2022  fprintf(out, "%c", ch);
2023  } else if (ch == '.' || ch == '\\') {
2024  fprintf(out, "\\%c", ch);
2025  } else {
2026  fprintf(out, "\\%03u", (unsigned int)ch);
2027  }
2028  }
2029  fprintf(out, ".");
2030  len = *d++;
2031  }
2032  return;
2033 }
2034 
2035 
2040 static void
2041 xfrd_backup(xfrd_type* xfrd)
2042 {
2043  zone_type* zone = (zone_type*) xfrd->zone;
2044  char* file = NULL;
2045  int timeout = 0;
2046  FILE* fd = NULL;
2047  if (zone && zone->name) {
2048  file = ods_build_path(zone->name, ".xfrd-state", 0, 1);
2049  if (file) {
2050  fd = ods_fopen(file, NULL, "w");
2051  if (fd) {
2052  if (xfrd->handler.timeout) {
2053  timeout = xfrd->timeout.tv_sec;
2054  }
2055  fprintf(fd, "%s\n", ODS_SE_FILE_MAGIC_V3);
2056  fprintf(fd, ";;Zone: name %s ttl %u mname ",
2057  zone->name,
2058  (unsigned) ntohl(xfrd->soa.ttl));
2059  xfrd_backup_dname(fd, xfrd->soa.mname),
2060  fprintf(fd, " rname ");
2061  xfrd_backup_dname(fd, xfrd->soa.rname),
2062  fprintf(fd, " serial %u refresh %u retry %u expire %u "
2063  "minimum %u\n",
2064  (unsigned) xfrd->soa.serial,
2065  (unsigned) xfrd->soa.refresh,
2066  (unsigned) xfrd->soa.retry,
2067  (unsigned) xfrd->soa.expire,
2068  (unsigned) xfrd->soa.minimum);
2069  fprintf(fd, ";;Master: num %d next %d round %d timeout %d\n",
2070  xfrd->master_num,
2071  xfrd->next_master,
2072  xfrd->round_num,
2073  timeout);
2074  fprintf(fd, ";;Serial: xfr %u %u notify %u %u disk %u %u\n",
2075  (unsigned) xfrd->serial_xfr,
2076  (unsigned) xfrd->serial_xfr_acquired,
2077  (unsigned) xfrd->serial_notify,
2078  (unsigned) xfrd->serial_notify_acquired,
2079  (unsigned) xfrd->serial_disk,
2080  (unsigned) xfrd->serial_disk_acquired);
2081  fprintf(fd, "%s\n", ODS_SE_FILE_MAGIC_V3);
2082  ods_fclose(fd);
2083  }
2084  free(file);
2085  }
2086  }
2087  return;
2088 }
2089 
2090 
2095 static void
2096 xfrd_unlink(xfrd_type* xfrd)
2097 {
2098  zone_type* zone = (zone_type*) xfrd->zone;
2099  char* file = NULL;
2100  if (zone && zone->name) {
2101  ods_log_info("[%s] unlink zone %s xfrd state", xfrd_str, zone->name);
2102  file = ods_build_path(zone->name, ".xfrd-state", 0, 1);
2103  if (file) {
2104  (void)unlink(file);
2105  free(file);
2106  }
2107  }
2108  return;
2109 }
2110 
2111 
2116 void
2117 xfrd_cleanup(xfrd_type* xfrd, int backup)
2118 {
2119  allocator_type* allocator = NULL;
2120  lock_basic_type serial_lock;
2121  lock_basic_type rw_lock;
2122  if (!xfrd) {
2123  return;
2124  }
2125  /* backup */
2126  if (backup) {
2127  xfrd_backup(xfrd);
2128  } else {
2129  xfrd_unlink(xfrd);
2130  }
2131 
2132  allocator = xfrd->allocator;
2133  serial_lock = xfrd->serial_lock;
2134  rw_lock = xfrd->rw_lock;
2135  tsig_rr_cleanup(xfrd->tsig_rr);
2136  allocator_deallocate(allocator, (void*) xfrd);
2137  allocator_cleanup(allocator);
2138  lock_basic_destroy(&serial_lock);
2139  lock_basic_destroy(&rw_lock);
2140  return;
2141 }