49 #define XFRD_TSIG_MAX_UNSIGNED 100
51 static const char* xfrd_str =
"xfrd";
53 static void xfrd_handle_zone(
netio_type* netio,
55 static void xfrd_make_request(
xfrd_type* xfrd);
57 static socklen_t xfrd_acl_sockaddr(
acl_type* acl,
unsigned int port,
58 struct sockaddr_storage *sck);
62 unsigned rdata_only,
unsigned update, uint32_t t,
65 uint16_t count,
int* done);
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);
83 static int xfrd_udp_send_request_ixfr(
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);
96 xfrd_recover_dname(uint8_t* dname,
const char* name)
98 const uint8_t *s = (
const uint8_t *) name;
104 if (strcmp(name,
".") == 0) {
109 for (h = d, p = h + 1; *s; ++s, ++p) {
119 label_length = p - h - 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) {
139 }
else if (s[1] !=
'\0') {
150 label_length = p - h - 1;
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;
192 if (zone && zone->
name) {
201 "magic (start) error", xfrd_str, zone->
name);
202 goto xfrd_recover_error;
224 ";;Zone error", xfrd_str, zone->
name);
225 goto xfrd_recover_error;
237 ";;Master error", xfrd_str, zone->
name);
238 goto xfrd_recover_error;
251 ";;Serial error", xfrd_str, zone->
name);
252 goto xfrd_recover_error;
256 "magic (end) error", xfrd_str, zone->
name);
257 goto xfrd_recover_error;
264 xfrd->
timeout.tv_sec = timeout;
267 xfrd->
soa.
ttl = htonl(soa_ttl);
283 if (!timeout || serial_notify_acquired ||
284 (serial_disk_acquired &&
285 (uint32_t)xfrd_time(xfrd) - serial_disk_acquired >
289 if (serial_disk_acquired &&
290 ((uint32_t)xfrd_time(xfrd) - serial_disk_acquired >
296 free((
void*)soa_mname);
297 free((
void*)soa_rname);
316 if (!xfrhandler || !zone) {
322 "allocator_create() failed", xfrd_str);
328 " allocator_alloc() failed", xfrd_str);
366 memset(&xfrd->
soa, 0,
sizeof(xfrd->
soa));
381 xfrd_set_timer_time(xfrd, 0);
405 xfrd_set_timer(
xfrd_type* xfrd, time_t t)
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);
445 xfrd_set_timer_time(
xfrd_type* xfrd, time_t t)
448 xfrd_set_timer(xfrd, xfrd_time(xfrd) + t);
465 ods_log_debug(
"[%s] zone %s sets timer timeout now", xfrd_str,
467 xfrd_set_timer_time(xfrd, 0);
484 ods_log_debug(
"[%s] zone %s sets timer timeout retry %u", xfrd_str,
486 xfrd_set_timer_time(xfrd, xfrd->
soa.
retry);
503 ods_log_debug(
"[%s] zone %s sets timer timeout refresh %u", xfrd_str,
515 xfrd_acl_sockaddr(
acl_type* acl,
unsigned int port,
516 struct sockaddr_storage *sck)
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);
527 return sizeof(
struct sockaddr_in6);
529 struct sockaddr_in* sa = (
struct sockaddr_in*)sck;
530 sa->sin_family = AF_INET;
531 sa->sin_port = htons(port);
533 return sizeof(
struct sockaddr_in);
546 unsigned int port = 0;
551 return xfrd_acl_sockaddr(acl, port, to);
569 ods_log_error(
"[%s] unable to sign request: tsig unknown algorithm "
584 ods_log_debug(
"[%s] tsig append rr to request id=%u", xfrd_str,
611 ods_log_error(
"[%s] unable to process tsig: xfr zone %s from %s "
612 "has malformed tsig rr", xfrd_str, zone->
name,
631 ods_log_error(
"[%s] unable to process tsig: xfr zone %s from %s "
632 "has bad tsig signature", xfrd_str, zone->
name,
641 ods_log_error(
"[%s] unable to process tsig: xfr zone %s, from %s "
642 "has too many consecutive packets without tsig", xfrd_str,
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,
665 char* xfrfile = NULL;
671 ods_log_crit(
"[%s] unable to commit xfr zone %s: build path failed",
672 xfrd_str, zone->
name);
682 free((
void*)xfrfile);
684 fprintf(fd,
";;ENDPACKET\n");
690 ods_log_crit(
"[%s] unable to commit xfr zone %s: ods_fopen() failed "
691 "(%s)", xfrd_str, zone->
name, strerror(errno));
706 ods_log_debug(
"[%s] reschedule task for zone %s: disk serial=%u "
707 "acquired=%u, memory serial=%u acquired=%u", xfrd_str,
713 ods_log_crit(
"[%s] unable to reschedule task for zone %s: %s",
734 char* xfrfile = NULL;
736 ldns_pkt* pkt = NULL;
737 ldns_status status = LDNS_STATUS_OK;
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));
753 ods_log_crit(
"[%s] unable to dump packet zone %s: build path failed",
754 xfrd_str, zone->
name);
759 free((
void*) xfrfile);
761 ods_log_crit(
"[%s] unable to dump packet zone %s: ods_fopen() failed "
762 "(%s)", xfrd_str, zone->
name, strerror(errno));
768 fprintf(fd,
";;BEGINPACKET\n");
770 ldns_rr_list_print(fd, ldns_pkt_answer(pkt));
786 size_t rdlength_pos = 0;
787 uint16_t rdlength = 0;
818 uint16_t mname_pos, uint16_t rname_pos,
819 uint32_t refresh, uint32_t retry, uint32_t expire, uint32_t minimum)
854 unsigned update, uint32_t t, uint32_t* soa_serial)
856 ldns_rr_type type = LDNS_RR_TYPE_SOA;
857 uint16_t mname_pos = 0;
858 uint16_t rname_pos = 0;
861 uint32_t refresh = 0;
864 uint32_t minimum = 0;
877 if (type != LDNS_RR_TYPE_SOA) {
879 xfrd_str, (
unsigned) type);
912 *soa_serial = serial;
915 xfrd_update_soa(xfrd, buffer, ttl, mname_pos, rname_pos,
916 refresh, retry, expire, minimum);
931 ldns_rr_type type = 0;
935 uint32_t tmp_serial = 0;
958 if (type == LDNS_RR_TYPE_SOA) {
959 if (!xfrd_parse_soa(xfrd, buffer, 1, 0, ttl, &serial)) {
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;
1031 ods_log_error(
"[%s] unable to parse packet: zone %s received bad "
1032 "packet from %s (too small)", xfrd_str, zone->
name,
1038 ods_log_error(
"[%s] bad packet: zone %s received bad query id "
1039 "%u from %s (expected %u)", xfrd_str, zone->
name,
1045 ods_log_error(
"[%s] bad packet: zone %s received error code %s from %s",
1055 if (!xfrd_tsig_process(xfrd, buffer)) {
1063 for (rrcount = 0; rrcount < qdcount; rrcount++) {
1066 "question section from %s (bad rr)", xfrd_str, zone->
name,
1075 ods_log_debug(
"[%s] zone %s received tc from %s, retry tcp",
1079 ods_log_error(
"[%s] bad packet: zone %s received bad xfr packet "
1084 ancount_todo = ancount;
1088 !xfrd_parse_soa(xfrd, buffer, 0, 1, 0, &serial)) {
1090 "packet from %s (bad soa)", xfrd_str, zone->
name,
1098 "serial %u from %s", xfrd_str, zone->
name, serial,
1121 ods_log_debug(
"[%s] zone %s ignoring old serial %u from %s "
1122 "(have %u)", xfrd_str, zone->
name, serial,
1142 ancount_todo = ancount - 1;
1146 ods_log_debug(
"[%s] zone %s received tc from %s, retry tcp",
1150 if (xfrd->
tcp_conn == -1 && ancount < 2) {
1152 ods_log_debug(
"[%s] zone %s received too short udp reply from %s, "
1156 status = xfrd_parse_rrs(xfrd, buffer, ancount_todo, &done);
1158 ods_log_error(
"[%s] bad packet: zone %s received bad xfr packet "
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,
1191 res = xfrd_parse_packet(xfrd, buffer);
1192 ods_log_debug(
"[%s] zone %s xfr packet parsed (res %d)", xfrd_str, zone->
name, res);
1217 xfrd_dump_packet(xfrd, buffer);
1228 xfrd_commit_packet(xfrd);
1232 ods_log_debug(
"[%s] zone %s notify acquired %u, serial on disk %u, "
1233 "notify serial %u", xfrd_str, zone->
name,
1239 ods_log_debug(
"[%s] zone %s reset notify acquired", xfrd_str,
1279 tcp =
set->tcp_conn[xfrd->
tcp_conn];
1283 len =
sizeof(error);
1284 if (getsockopt(tcp->
fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
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));
1296 xfrd_tcp_release(xfrd,
set);
1305 xfrd_tcp_release(xfrd,
set);
1310 xfrd_str, zone->
name);
1314 ods_log_debug(
"[%s] zone %s done writing, get ready for reading",
1315 xfrd_str, zone->
name);
1319 xfrd_tcp_read(xfrd,
set);
1331 int fd, family, conn;
1332 struct sockaddr_storage to;
1344 ods_log_debug(
"[%s] zone %s open tcp connection to %s", xfrd_str,
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;
1354 fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
1355 set->tcp_conn[xfrd->
tcp_conn]->fd = fd;
1357 ods_log_error(
"[%s] zone %s cannot create tcp socket to %s: %s",
1360 xfrd_tcp_release(xfrd,
set);
1363 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
1364 ods_log_error(
"[%s] zone %s cannot fcntl tcp socket to %s: %s",
1367 xfrd_tcp_release(xfrd,
set);
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",
1378 xfrd_tcp_release(xfrd,
set);
1406 if (set->tcp_conn[i]->fd == -1) {
1415 xfrd_udp_release(xfrd);
1417 if (!xfrd_tcp_open(xfrd,
set)) {
1420 xfrd_tcp_xfr(xfrd,
set);
1425 xfrd_str, TCPSET_MAX);
1427 xfrd_unset_timer(xfrd);
1452 tcp =
set->tcp_conn[xfrd->
tcp_conn];
1465 xfrd_write_soa(xfrd, tcp->
packet);
1474 xfrd_tsig_sign(xfrd, tcp->
packet);
1477 ods_log_debug(
"[%s] zone %s sending tcp query id=%d", xfrd_str,
1497 tcp =
set->tcp_conn[xfrd->
tcp_conn];
1501 xfrd_tcp_release(xfrd,
set);
1509 ret = xfrd_handle_packet(xfrd, tcp->
packet);
1516 ods_log_debug(
"[%s] tcp read %s: release connection", xfrd_str,
1518 xfrd_tcp_release(xfrd,
set);
1523 ods_log_debug(
"[%s] disable ixfr requests for %s from now (%u)",
1528 ods_log_debug(
"[%s] tcp read %s: release connection", xfrd_str,
1530 xfrd_tcp_release(xfrd,
set);
1531 xfrd_make_request(xfrd);
1555 ods_log_debug(
"[%s] zone %s release tcp connection to %s", xfrd_str,
1563 if (set->tcp_conn[conn]->fd != -1) {
1564 close(set->tcp_conn[conn]->fd);
1566 set->tcp_conn[conn]->fd = -1;
1582 struct sockaddr_storage to;
1583 socklen_t to_len = 0;
1598 fd = socket(family, SOCK_DGRAM, IPPROTO_UDP);
1611 (
struct sockaddr*)&to, to_len);
1628 xfrd_udp_send_request_ixfr(
xfrd_type* xfrd)
1641 ods_log_error(
"[%s] unable to transfer zone %s: tried to send "
1642 "udp while tcp obtained", xfrd_str, zone->
name);
1657 xfrd_write_soa(xfrd, xfrhandler->
packet);
1658 xfrd_tsig_sign(xfrd, xfrhandler->
packet);
1661 ods_log_debug(
"[%s] zone %s sending udp query id=%d qtype=IXFR to %s",
1663 if((fd = xfrd_udp_send(xfrd, xfrhandler->
packet)) == -1) {
1683 xfrd_tcp_release(xfrd, xfrhandler->
tcp_set);
1687 xfrd->
handler.
fd = xfrd_udp_send_request_ixfr(xfrd);
1703 xfrd_unset_timer(xfrd);
1716 ssize_t received = 0;
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));
1750 if (!xfrd_udp_read_packet(xfrd)) {
1752 "xfrd_udp_read_packet() failed", xfrd_str, zone->
name);
1753 xfrd_udp_release(xfrd);
1758 res = xfrd_handle_packet(xfrd, xfrhandler->
packet);
1763 xfrd_udp_release(xfrd);
1765 xfrd_tcp_obtain(xfrd, xfrhandler->
tcp_set);
1773 xfrd_udp_release(xfrd);
1777 ods_log_debug(
"[%s] disable ixfr requests for %s from now (%u)",
1784 xfrd_udp_release(xfrd);
1785 xfrd_make_request(xfrd);
1821 wf->
handler.
fd = xfrd_udp_send_request_ixfr(wf);
1886 xfrd_str, zone->
name);
1891 ods_log_debug(
"[%s] unable to make request for zone %s: no master",
1892 xfrd_str, zone->
name);
1903 ods_log_debug(
"[%s] clear negative caching calc: %u + %u <= %u",
1909 ods_log_debug(
"[%s] zone %s make request round %d master %s:%u",
1914 xfrd_udp_obtain(xfrd);
1919 xfrd_tcp_obtain(xfrd, xfrhandler->
tcp_set);
1930 xfrd_handle_zone(
netio_type* ATTR_UNUSED(netio),
1952 xfrd_tcp_read(xfrd, xfrhandler->
tcp_set);
1958 xfrd_tcp_write(xfrd, xfrhandler->
tcp_set);
1964 xfrd_tcp_release(xfrd, xfrhandler->
tcp_set);
1970 if (event_types & NETIO_EVENT_READ) {
1975 xfrd_udp_read(xfrd);
1981 if (handler->
fd != -1) {
1983 xfrd_udp_release(xfrd);
1987 xfrd_str, zone->
name);
1988 xfrd_unset_timer(xfrd);
1993 xfrd_str, zone->
name);
1994 xfrd_unset_timer(xfrd);
1998 xfrd_make_request(xfrd);
2008 xfrd_backup_dname(FILE* out, uint8_t* dname)
2010 uint8_t* d= dname+1;
2019 for (i=0; i<len; i++) {
2021 if (isalnum(ch) || ch ==
'-' || ch ==
'_') {
2022 fprintf(out,
"%c", ch);
2023 }
else if (ch ==
'.' || ch ==
'\\') {
2024 fprintf(out,
"\\%c", ch);
2026 fprintf(out,
"\\%03u", (
unsigned int)ch);
2047 if (zone && zone->
name) {
2053 timeout = xfrd->
timeout.tv_sec;
2055 fprintf(fd,
"%s\n", ODS_SE_FILE_MAGIC_V3);
2056 fprintf(fd,
";;Zone: name %s ttl %u mname ",
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 "
2069 fprintf(fd,
";;Master: num %d next %d round %d timeout %d\n",
2074 fprintf(fd,
";;Serial: xfr %u %u notify %u %u disk %u %u\n",
2081 fprintf(fd,
"%s\n", ODS_SE_FILE_MAGIC_V3);
2100 if (zone && zone->
name) {