Home | History | Annotate | Line # | Download | only in dist
ixfr.c revision 1.1.1.2
      1      1.1  christos /*
      2      1.1  christos  * ixfr.c -- generating IXFR responses.
      3      1.1  christos  *
      4      1.1  christos  * Copyright (c) 2021, NLnet Labs. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * See LICENSE for the license.
      7      1.1  christos  *
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #include "config.h"
     11      1.1  christos 
     12      1.1  christos #include <errno.h>
     13      1.1  christos #include <string.h>
     14      1.1  christos #include <ctype.h>
     15      1.1  christos #ifdef HAVE_SYS_TYPES_H
     16      1.1  christos #  include <sys/types.h>
     17      1.1  christos #endif
     18      1.1  christos #ifdef HAVE_SYS_STAT_H
     19      1.1  christos #  include <sys/stat.h>
     20      1.1  christos #endif
     21      1.1  christos #include <unistd.h>
     22      1.1  christos 
     23      1.1  christos #include "ixfr.h"
     24      1.1  christos #include "packet.h"
     25      1.1  christos #include "rdata.h"
     26      1.1  christos #include "axfr.h"
     27      1.1  christos #include "options.h"
     28      1.1  christos #include "zonec.h"
     29      1.1  christos 
     30      1.1  christos /*
     31      1.1  christos  * For optimal compression IXFR response packets are limited in size
     32      1.1  christos  * to MAX_COMPRESSION_OFFSET.
     33      1.1  christos  */
     34      1.1  christos #define IXFR_MAX_MESSAGE_LEN MAX_COMPRESSION_OFFSET
     35      1.1  christos 
     36      1.1  christos /* draft-ietf-dnsop-rfc2845bis-06, section 5.3.1 says to sign every packet */
     37      1.1  christos #define IXFR_TSIG_SIGN_EVERY_NTH	0	/* tsig sign every N packets. */
     38      1.1  christos 
     39      1.1  christos /* initial space in rrs data for storing records */
     40      1.1  christos #define IXFR_STORE_INITIAL_SIZE 4096
     41      1.1  christos 
     42      1.1  christos /* store compression for one name */
     43      1.1  christos struct rrcompress_entry {
     44      1.1  christos 	/* rbtree node, key is this struct */
     45      1.1  christos 	struct rbnode node;
     46      1.1  christos 	/* the uncompressed domain name */
     47      1.1  christos 	const uint8_t* dname;
     48      1.1  christos 	/* the length of the dname, includes terminating 0 label */
     49      1.1  christos 	uint16_t len;
     50      1.1  christos 	/* the offset of the dname in the packet */
     51      1.1  christos 	uint16_t offset;
     52      1.1  christos };
     53      1.1  christos 
     54      1.1  christos /* structure to store compression data for the packet */
     55      1.1  christos struct pktcompression {
     56      1.1  christos 	/* rbtree of rrcompress_entry. sorted by dname */
     57      1.1  christos 	struct rbtree tree;
     58      1.1  christos 	/* allocation information, how many bytes allocated now */
     59      1.1  christos 	size_t alloc_now;
     60      1.1  christos 	/* allocation information, total size in block */
     61      1.1  christos 	size_t alloc_max;
     62      1.1  christos 	/* region to use if block full, this is NULL if unused */
     63      1.1  christos 	struct region* region;
     64      1.1  christos 	/* block of temp data for allocation */
     65      1.1  christos 	uint8_t block[sizeof(struct rrcompress_entry)*1024];
     66      1.1  christos };
     67      1.1  christos 
     68      1.1  christos /* compare two elements in the compression tree. Returns -1, 0, or 1. */
     69      1.1  christos static int compression_cmp(const void* a, const void* b)
     70      1.1  christos {
     71      1.1  christos 	struct rrcompress_entry* rra = (struct rrcompress_entry*)a;
     72      1.1  christos 	struct rrcompress_entry* rrb = (struct rrcompress_entry*)b;
     73      1.1  christos 	if(rra->len != rrb->len) {
     74      1.1  christos 		if(rra->len < rrb->len)
     75      1.1  christos 			return -1;
     76      1.1  christos 		return 1;
     77      1.1  christos 	}
     78      1.1  christos 	return memcmp(rra->dname, rrb->dname, rra->len);
     79      1.1  christos }
     80      1.1  christos 
     81      1.1  christos /* init the pktcompression to a new packet */
     82      1.1  christos static void pktcompression_init(struct pktcompression* pcomp)
     83      1.1  christos {
     84      1.1  christos 	pcomp->alloc_now = 0;
     85      1.1  christos 	pcomp->alloc_max = sizeof(pcomp->block);
     86      1.1  christos 	pcomp->region = NULL;
     87      1.1  christos 	pcomp->tree.root = RBTREE_NULL;
     88      1.1  christos 	pcomp->tree.count = 0;
     89      1.1  christos 	pcomp->tree.region = NULL;
     90      1.1  christos 	pcomp->tree.cmp = &compression_cmp;
     91      1.1  christos }
     92      1.1  christos 
     93      1.1  christos /* freeup the pktcompression data */
     94      1.1  christos static void pktcompression_freeup(struct pktcompression* pcomp)
     95      1.1  christos {
     96      1.1  christos 	if(pcomp->region) {
     97      1.1  christos 		region_destroy(pcomp->region);
     98      1.1  christos 		pcomp->region = NULL;
     99      1.1  christos 	}
    100      1.1  christos 	pcomp->alloc_now = 0;
    101      1.1  christos 	pcomp->tree.root = RBTREE_NULL;
    102      1.1  christos 	pcomp->tree.count = 0;
    103      1.1  christos }
    104      1.1  christos 
    105      1.1  christos /* alloc data in pktcompression */
    106      1.1  christos static void* pktcompression_alloc(struct pktcompression* pcomp, size_t s)
    107      1.1  christos {
    108      1.1  christos 	/* first attempt to allocate in the fixed block,
    109      1.1  christos 	 * that is very fast and on the stack in the pcomp struct */
    110      1.1  christos 	if(pcomp->alloc_now + s <= pcomp->alloc_max) {
    111      1.1  christos 		void* ret = pcomp->block + pcomp->alloc_now;
    112      1.1  christos 		pcomp->alloc_now += s;
    113      1.1  christos 		return ret;
    114      1.1  christos 	}
    115      1.1  christos 
    116      1.1  christos 	/* if that fails, create a region to allocate in,
    117      1.1  christos 	 * it is freed in the freeup */
    118      1.1  christos 	if(!pcomp->region) {
    119      1.1  christos 		pcomp->region = region_create(xalloc, free);
    120      1.1  christos 		if(!pcomp->region)
    121      1.1  christos 			return NULL;
    122      1.1  christos 	}
    123      1.1  christos 	return region_alloc(pcomp->region, s);
    124      1.1  christos }
    125      1.1  christos 
    126      1.1  christos /* find a pktcompression name, return offset if found */
    127      1.1  christos static uint16_t pktcompression_find(struct pktcompression* pcomp,
    128      1.1  christos 	const uint8_t* dname, size_t len)
    129      1.1  christos {
    130      1.1  christos 	struct rrcompress_entry key, *found;
    131      1.1  christos 	key.node.key = &key;
    132      1.1  christos 	key.dname = dname;
    133      1.1  christos 	key.len = len;
    134      1.1  christos 	found = (struct rrcompress_entry*)rbtree_search(&pcomp->tree, &key);
    135      1.1  christos 	if(found) return found->offset;
    136      1.1  christos 	return 0;
    137      1.1  christos }
    138      1.1  christos 
    139      1.1  christos /* insert a new domain name into the compression tree.
    140      1.1  christos  * it fails silently, no need to compress then. */
    141      1.1  christos static void pktcompression_insert(struct pktcompression* pcomp,
    142      1.1  christos 	const uint8_t* dname, size_t len, uint16_t offset)
    143      1.1  christos {
    144      1.1  christos 	struct rrcompress_entry* entry;
    145      1.1  christos 	if(len > 65535)
    146      1.1  christos 		return;
    147      1.1  christos 	if(offset > MAX_COMPRESSION_OFFSET)
    148      1.1  christos 		return; /* too far for a compression pointer */
    149      1.1  christos 	entry = pktcompression_alloc(pcomp, sizeof(*entry));
    150      1.1  christos 	if(!entry)
    151      1.1  christos 		return;
    152      1.1  christos 	memset(&entry->node, 0, sizeof(entry->node));
    153      1.1  christos 	entry->node.key = entry;
    154      1.1  christos 	entry->dname = dname;
    155      1.1  christos 	entry->len = len;
    156      1.1  christos 	entry->offset = offset;
    157      1.1  christos 	(void)rbtree_insert(&pcomp->tree, &entry->node);
    158      1.1  christos }
    159      1.1  christos 
    160      1.1  christos /* insert all the labels of a domain name */
    161      1.1  christos static void pktcompression_insert_with_labels(struct pktcompression* pcomp,
    162      1.1  christos 	uint8_t* dname, size_t len, uint16_t offset)
    163      1.1  christos {
    164      1.1  christos 	if(!dname)
    165      1.1  christos 		return;
    166      1.1  christos 	if(offset > MAX_COMPRESSION_OFFSET)
    167      1.1  christos 		return;
    168      1.1  christos 
    169      1.1  christos 	/* while we have not seen the end root label */
    170      1.1  christos 	while(len > 0 && dname[0] != 0) {
    171      1.1  christos 		size_t lablen;
    172      1.1  christos 		pktcompression_insert(pcomp, dname, len, offset);
    173      1.1  christos 		lablen = (size_t)(dname[0]);
    174      1.1  christos 		if( (lablen&0xc0) )
    175      1.1  christos 			return; /* the dname should be uncompressed */
    176      1.1  christos 		if(lablen+1 > len)
    177      1.1  christos 			return; /* len should be uncompressed wireformat len */
    178      1.1  christos 		if(offset > MAX_COMPRESSION_OFFSET - lablen - 1)
    179      1.1  christos 			return; /* offset moves too far for compression */
    180      1.1  christos 		/* skip label */
    181      1.1  christos 		len -= lablen+1;
    182      1.1  christos 		dname += lablen+1;
    183      1.1  christos 		offset += lablen+1;
    184      1.1  christos 	}
    185      1.1  christos }
    186      1.1  christos 
    187      1.1  christos /* calculate length of dname in uncompressed wireformat in buffer */
    188      1.1  christos static size_t dname_length(const uint8_t* buf, size_t len)
    189      1.1  christos {
    190      1.1  christos 	size_t l = 0;
    191      1.1  christos 	if(!buf || len == 0)
    192      1.1  christos 		return l;
    193      1.1  christos 	while(len > 0 && buf[0] != 0) {
    194      1.1  christos 		size_t lablen = (size_t)(buf[0]);
    195      1.1  christos 		if( (lablen&0xc0) )
    196      1.1  christos 			return 0; /* the name should be uncompressed */
    197      1.1  christos 		if(lablen+1 > len)
    198      1.1  christos 			return 0; /* should fit in the buffer */
    199      1.1  christos 		l += lablen+1;
    200      1.1  christos 		len -= lablen+1;
    201      1.1  christos 		buf += lablen+1;
    202      1.1  christos 	}
    203      1.1  christos 	if(len == 0)
    204      1.1  christos 		return 0; /* end label should fit in buffer */
    205      1.1  christos 	if(buf[0] != 0)
    206      1.1  christos 		return 0; /* must end in root label */
    207      1.1  christos 	l += 1; /* for the end root label */
    208      1.1  christos 	return l;
    209      1.1  christos }
    210      1.1  christos 
    211      1.1  christos /* write a compressed domain name into the packet,
    212      1.1  christos  * returns uncompressed wireformat length,
    213      1.1  christos  * 0 if it does not fit and -1 on failure, bad dname. */
    214      1.1  christos static int pktcompression_write_dname(struct buffer* packet,
    215      1.1  christos 	struct pktcompression* pcomp, const uint8_t* rr, size_t rrlen)
    216      1.1  christos {
    217      1.1  christos 	size_t wirelen = 0;
    218      1.1  christos 	size_t dname_len = dname_length(rr, rrlen);
    219      1.1  christos 	if(!rr || rrlen == 0 || dname_len == 0)
    220      1.1  christos 		return 0;
    221      1.1  christos 	while(rrlen > 0 && rr[0] != 0) {
    222      1.1  christos 		size_t lablen = (size_t)(rr[0]);
    223      1.1  christos 		uint16_t offset;
    224      1.1  christos 		if( (lablen&0xc0) )
    225      1.1  christos 			return -1; /* name should be uncompressed */
    226      1.1  christos 		if(lablen+1 > rrlen)
    227      1.1  christos 			return -1; /* name should fit */
    228      1.1  christos 
    229      1.1  christos 		/* see if the domain name has a compression pointer */
    230      1.1  christos 		if((offset=pktcompression_find(pcomp, rr, dname_len))!=0) {
    231      1.1  christos 			if(!buffer_available(packet, 2))
    232      1.1  christos 				return 0;
    233      1.1  christos 			buffer_write_u16(packet, (uint16_t)(0xc000 | offset));
    234      1.1  christos 			wirelen += dname_len;
    235      1.1  christos 			return wirelen;
    236      1.1  christos 		} else {
    237      1.1  christos 			if(!buffer_available(packet, lablen+1))
    238      1.1  christos 				return 0;
    239      1.1  christos 			/* insert the domain name at this position */
    240      1.1  christos 			pktcompression_insert(pcomp, rr, dname_len,
    241      1.1  christos 				buffer_position(packet));
    242      1.1  christos 			/* write it */
    243      1.1  christos 			buffer_write(packet, rr, lablen+1);
    244      1.1  christos 		}
    245      1.1  christos 
    246      1.1  christos 		wirelen += lablen+1;
    247      1.1  christos 		rr += lablen+1;
    248      1.1  christos 		rrlen -= lablen+1;
    249      1.1  christos 		dname_len -= lablen+1;
    250      1.1  christos 	}
    251      1.1  christos 	if(rrlen > 0 && rr[0] == 0) {
    252      1.1  christos 		/* write end root label */
    253      1.1  christos 		if(!buffer_available(packet, 1))
    254      1.1  christos 			return 0;
    255      1.1  christos 		buffer_write_u8(packet, 0);
    256      1.1  christos 		wirelen += 1;
    257      1.1  christos 	}
    258      1.1  christos 	return wirelen;
    259      1.1  christos }
    260      1.1  christos 
    261      1.1  christos /* write an RR into the packet with compression for domain names,
    262      1.1  christos  * return 0 and resets position if it does not fit in the packet. */
    263      1.1  christos static int ixfr_write_rr_pkt(struct query* query, struct buffer* packet,
    264  1.1.1.2  christos 	struct pktcompression* pcomp, const uint8_t* rr, size_t rrlen,
    265  1.1.1.2  christos 	uint16_t total_added)
    266      1.1  christos {
    267      1.1  christos 	size_t oldpos = buffer_position(packet);
    268      1.1  christos 	size_t rdpos;
    269      1.1  christos 	uint16_t tp;
    270      1.1  christos 	int dname_len;
    271      1.1  christos 	size_t rdlen;
    272      1.1  christos 	size_t i;
    273      1.1  christos 	rrtype_descriptor_type* descriptor;
    274      1.1  christos 
    275  1.1.1.2  christos 	if(total_added == 0) {
    276  1.1.1.2  christos 		size_t oldmaxlen = query->maxlen;
    277  1.1.1.2  christos 		/* RR > 16K can be first RR */
    278  1.1.1.2  christos 		query->maxlen = (query->tcp?TCP_MAX_MESSAGE_LEN:UDP_MAX_MESSAGE_LEN);
    279  1.1.1.2  christos 		if(query_overflow(query)) {
    280  1.1.1.2  christos 			query->maxlen = oldmaxlen;
    281  1.1.1.2  christos 			return 0;
    282  1.1.1.2  christos 		}
    283  1.1.1.2  christos 		query->maxlen = oldmaxlen;
    284  1.1.1.2  christos 	} else {
    285  1.1.1.2  christos 		if(buffer_position(packet) > MAX_COMPRESSION_OFFSET
    286  1.1.1.2  christos 			|| query_overflow(query)) {
    287  1.1.1.2  christos 			/* we are past the maximum length */
    288  1.1.1.2  christos 			return 0;
    289  1.1.1.2  christos 		}
    290      1.1  christos 	}
    291      1.1  christos 
    292      1.1  christos 	/* write owner */
    293      1.1  christos 	dname_len = pktcompression_write_dname(packet, pcomp, rr, rrlen);
    294      1.1  christos 	if(dname_len == -1)
    295      1.1  christos 		return 1; /* attempt to skip this malformed rr, could assert */
    296      1.1  christos 	if(dname_len == 0) {
    297      1.1  christos 		buffer_set_position(packet, oldpos);
    298      1.1  christos 		return 0;
    299      1.1  christos 	}
    300      1.1  christos 	rr += dname_len;
    301      1.1  christos 	rrlen -= dname_len;
    302      1.1  christos 
    303      1.1  christos 	/* type, class, ttl, rdatalen */
    304      1.1  christos 	if(!buffer_available(packet, 10)) {
    305      1.1  christos 		buffer_set_position(packet, oldpos);
    306      1.1  christos 		return 0;
    307      1.1  christos 	}
    308      1.1  christos 	if(10 > rrlen)
    309      1.1  christos 		return 1; /* attempt to skip this malformed rr, could assert */
    310      1.1  christos 	tp = read_uint16(rr);
    311      1.1  christos 	buffer_write(packet, rr, 8);
    312      1.1  christos 	rr += 8;
    313      1.1  christos 	rrlen -= 8;
    314      1.1  christos 	rdlen = read_uint16(rr);
    315      1.1  christos 	rr += 2;
    316      1.1  christos 	rrlen -= 2;
    317      1.1  christos 	rdpos = buffer_position(packet);
    318      1.1  christos 	buffer_write_u16(packet, 0);
    319      1.1  christos 	if(rdlen > rrlen)
    320      1.1  christos 		return 1; /* attempt to skip this malformed rr, could assert */
    321      1.1  christos 
    322      1.1  christos 	/* rdata */
    323      1.1  christos 	descriptor = rrtype_descriptor_by_type(tp);
    324      1.1  christos 	for(i=0; i<descriptor->maximum; i++) {
    325      1.1  christos 		size_t copy_len = 0;
    326      1.1  christos 		if(rdlen == 0)
    327      1.1  christos 			break;
    328      1.1  christos 
    329      1.1  christos 		switch(rdata_atom_wireformat_type(tp, i)) {
    330      1.1  christos 		case RDATA_WF_COMPRESSED_DNAME:
    331      1.1  christos 			dname_len = pktcompression_write_dname(packet, pcomp,
    332      1.1  christos 				rr, rdlen);
    333      1.1  christos 			if(dname_len == -1)
    334      1.1  christos 				return 1; /* attempt to skip malformed rr */
    335      1.1  christos 			if(dname_len == 0) {
    336      1.1  christos 				buffer_set_position(packet, oldpos);
    337      1.1  christos 				return 0;
    338      1.1  christos 			}
    339      1.1  christos 			rr += dname_len;
    340      1.1  christos 			rdlen -= dname_len;
    341      1.1  christos 			break;
    342      1.1  christos 		case RDATA_WF_UNCOMPRESSED_DNAME:
    343      1.1  christos 		case RDATA_WF_LITERAL_DNAME:
    344      1.1  christos 			copy_len = rdlen;
    345      1.1  christos 			break;
    346      1.1  christos 		case RDATA_WF_BYTE:
    347      1.1  christos 			copy_len = 1;
    348      1.1  christos 			break;
    349      1.1  christos 		case RDATA_WF_SHORT:
    350      1.1  christos 			copy_len = 2;
    351      1.1  christos 			break;
    352      1.1  christos 		case RDATA_WF_LONG:
    353      1.1  christos 			copy_len = 4;
    354      1.1  christos 			break;
    355      1.1  christos 		case RDATA_WF_TEXTS:
    356      1.1  christos 		case RDATA_WF_LONG_TEXT:
    357      1.1  christos 			copy_len = rdlen;
    358      1.1  christos 			break;
    359      1.1  christos 		case RDATA_WF_TEXT:
    360      1.1  christos 		case RDATA_WF_BINARYWITHLENGTH:
    361      1.1  christos 			copy_len = 1;
    362      1.1  christos 			if(rdlen > copy_len)
    363      1.1  christos 				copy_len += rr[0];
    364      1.1  christos 			break;
    365      1.1  christos 		case RDATA_WF_A:
    366      1.1  christos 			copy_len = 4;
    367      1.1  christos 			break;
    368      1.1  christos 		case RDATA_WF_AAAA:
    369      1.1  christos 			copy_len = 16;
    370      1.1  christos 			break;
    371      1.1  christos 		case RDATA_WF_ILNP64:
    372      1.1  christos 			copy_len = 8;
    373      1.1  christos 			break;
    374      1.1  christos 		case RDATA_WF_EUI48:
    375      1.1  christos 			copy_len = EUI48ADDRLEN;
    376      1.1  christos 			break;
    377      1.1  christos 		case RDATA_WF_EUI64:
    378      1.1  christos 			copy_len = EUI64ADDRLEN;
    379      1.1  christos 			break;
    380      1.1  christos 		case RDATA_WF_BINARY:
    381      1.1  christos 			copy_len = rdlen;
    382      1.1  christos 			break;
    383      1.1  christos 		case RDATA_WF_APL:
    384      1.1  christos 			copy_len = (sizeof(uint16_t)    /* address family */
    385      1.1  christos                                   + sizeof(uint8_t)   /* prefix */
    386      1.1  christos                                   + sizeof(uint8_t)); /* length */
    387      1.1  christos 			if(copy_len <= rdlen)
    388      1.1  christos 				copy_len += (rr[copy_len-1]&APL_LENGTH_MASK);
    389      1.1  christos 			break;
    390      1.1  christos 		case RDATA_WF_IPSECGATEWAY:
    391      1.1  christos 			copy_len = rdlen;
    392      1.1  christos 			break;
    393      1.1  christos 		case RDATA_WF_SVCPARAM:
    394      1.1  christos 			copy_len = 4;
    395      1.1  christos 			if(copy_len <= rdlen)
    396      1.1  christos 				copy_len += read_uint16(rr+2);
    397      1.1  christos 			break;
    398      1.1  christos 		default:
    399      1.1  christos 			copy_len = rdlen;
    400      1.1  christos 			break;
    401      1.1  christos 		}
    402      1.1  christos 		if(copy_len) {
    403      1.1  christos 			if(!buffer_available(packet, copy_len)) {
    404      1.1  christos 				buffer_set_position(packet, oldpos);
    405      1.1  christos 				return 0;
    406      1.1  christos 			}
    407      1.1  christos 			if(copy_len > rdlen)
    408      1.1  christos 				return 1; /* assert of skip malformed */
    409      1.1  christos 			buffer_write(packet, rr, copy_len);
    410      1.1  christos 			rr += copy_len;
    411      1.1  christos 			rdlen -= copy_len;
    412      1.1  christos 		}
    413      1.1  christos 	}
    414      1.1  christos 	/* write compressed rdata length */
    415      1.1  christos 	buffer_write_u16_at(packet, rdpos, buffer_position(packet)-rdpos-2);
    416  1.1.1.2  christos 	if(total_added == 0) {
    417  1.1.1.2  christos 		size_t oldmaxlen = query->maxlen;
    418  1.1.1.2  christos 		query->maxlen = (query->tcp?TCP_MAX_MESSAGE_LEN:UDP_MAX_MESSAGE_LEN);
    419  1.1.1.2  christos 		if(query_overflow(query)) {
    420  1.1.1.2  christos 			query->maxlen = oldmaxlen;
    421  1.1.1.2  christos 			buffer_set_position(packet, oldpos);
    422  1.1.1.2  christos 			return 0;
    423  1.1.1.2  christos 		}
    424  1.1.1.2  christos 		query->maxlen = oldmaxlen;
    425  1.1.1.2  christos 	} else {
    426  1.1.1.2  christos 		if(query_overflow(query)) {
    427  1.1.1.2  christos 			/* we are past the maximum length */
    428  1.1.1.2  christos 			buffer_set_position(packet, oldpos);
    429  1.1.1.2  christos 			return 0;
    430  1.1.1.2  christos 		}
    431      1.1  christos 	}
    432      1.1  christos 	return 1;
    433      1.1  christos }
    434      1.1  christos 
    435      1.1  christos /* parse the serial number from the IXFR query */
    436      1.1  christos static int parse_qserial(struct buffer* packet, uint32_t* qserial,
    437      1.1  christos 	size_t* snip_pos)
    438      1.1  christos {
    439      1.1  christos 	unsigned int i;
    440      1.1  christos 	uint16_t type, rdlen;
    441      1.1  christos 	/* we must have a SOA in the authority section */
    442      1.1  christos 	if(NSCOUNT(packet) == 0)
    443      1.1  christos 		return 0;
    444      1.1  christos 	/* skip over the question section, we want only one */
    445      1.1  christos 	buffer_set_position(packet, QHEADERSZ);
    446      1.1  christos 	if(QDCOUNT(packet) != 1)
    447      1.1  christos 		return 0;
    448      1.1  christos 	if(!packet_skip_rr(packet, 1))
    449      1.1  christos 		return 0;
    450      1.1  christos 	/* set position to snip off the authority section */
    451      1.1  christos 	*snip_pos = buffer_position(packet);
    452      1.1  christos 	/* skip over the authority section RRs until we find the SOA */
    453      1.1  christos 	for(i=0; i<NSCOUNT(packet); i++) {
    454      1.1  christos 		/* is this the SOA record? */
    455      1.1  christos 		if(!packet_skip_dname(packet))
    456      1.1  christos 			return 0; /* malformed name */
    457      1.1  christos 		if(!buffer_available(packet, 10))
    458      1.1  christos 			return 0; /* no type,class,ttl,rdatalen */
    459      1.1  christos 		type = buffer_read_u16(packet);
    460      1.1  christos 		buffer_skip(packet, 6);
    461      1.1  christos 		rdlen = buffer_read_u16(packet);
    462      1.1  christos 		if(!buffer_available(packet, rdlen))
    463      1.1  christos 			return 0;
    464      1.1  christos 		if(type == TYPE_SOA) {
    465      1.1  christos 			/* read serial from rdata, skip two dnames, then
    466      1.1  christos 			 * read the 32bit value */
    467      1.1  christos 			if(!packet_skip_dname(packet))
    468      1.1  christos 				return 0; /* malformed nsname */
    469      1.1  christos 			if(!packet_skip_dname(packet))
    470      1.1  christos 				return 0; /* malformed rname */
    471      1.1  christos 			if(!buffer_available(packet, 4))
    472      1.1  christos 				return 0;
    473      1.1  christos 			*qserial = buffer_read_u32(packet);
    474      1.1  christos 			return 1;
    475      1.1  christos 		}
    476      1.1  christos 		buffer_skip(packet, rdlen);
    477      1.1  christos 	}
    478      1.1  christos 	return 0;
    479      1.1  christos }
    480      1.1  christos 
    481      1.1  christos /* get serial from SOA RR */
    482      1.1  christos static uint32_t soa_rr_get_serial(struct rr* rr)
    483      1.1  christos {
    484      1.1  christos 	if(rr->rdata_count < 3)
    485      1.1  christos 		return 0;
    486      1.1  christos 	if(rr->rdatas[2].data[0] < 4)
    487      1.1  christos 		return 0;
    488      1.1  christos 	return read_uint32(&rr->rdatas[2].data[1]);
    489      1.1  christos }
    490      1.1  christos 
    491      1.1  christos /* get the current serial from the zone */
    492      1.1  christos uint32_t zone_get_current_serial(struct zone* zone)
    493      1.1  christos {
    494      1.1  christos 	if(!zone || !zone->soa_rrset)
    495      1.1  christos 		return 0;
    496      1.1  christos 	if(zone->soa_rrset->rr_count == 0)
    497      1.1  christos 		return 0;
    498      1.1  christos 	if(zone->soa_rrset->rrs[0].rdata_count < 3)
    499      1.1  christos 		return 0;
    500      1.1  christos 	if(zone->soa_rrset->rrs[0].rdatas[2].data[0] < 4)
    501      1.1  christos 		return 0;
    502      1.1  christos 	return read_uint32(&zone->soa_rrset->rrs[0].rdatas[2].data[1]);
    503      1.1  christos }
    504      1.1  christos 
    505      1.1  christos /* iterator over ixfr data. find first element, eg. oldest zone version
    506      1.1  christos  * change.
    507      1.1  christos  * The iterator can be started with the ixfr_data_first, but also with
    508      1.1  christos  * ixfr_data_last, or with an existing ixfr_data element to start from.
    509      1.1  christos  * Continue by using ixfr_data_next or ixfr_data_prev to ask for more elements
    510      1.1  christos  * until that returns NULL. NULL because end of list or loop was detected.
    511      1.1  christos  * The ixfr_data_prev uses a counter, start it at 0, it returns NULL when
    512      1.1  christos  * a loop is detected.
    513      1.1  christos  */
    514      1.1  christos static struct ixfr_data* ixfr_data_first(struct zone_ixfr* ixfr)
    515      1.1  christos {
    516      1.1  christos 	struct ixfr_data* n;
    517      1.1  christos 	if(!ixfr || !ixfr->data || ixfr->data->count==0)
    518      1.1  christos 		return NULL;
    519      1.1  christos 	n = (struct ixfr_data*)rbtree_search(ixfr->data, &ixfr->oldest_serial);
    520      1.1  christos 	if(!n || n == (struct ixfr_data*)RBTREE_NULL)
    521      1.1  christos 		return NULL;
    522      1.1  christos 	return n;
    523      1.1  christos }
    524      1.1  christos 
    525      1.1  christos /* iterator over ixfr data. find last element, eg. newest zone version
    526      1.1  christos  * change. */
    527      1.1  christos static struct ixfr_data* ixfr_data_last(struct zone_ixfr* ixfr)
    528      1.1  christos {
    529      1.1  christos 	struct ixfr_data* n;
    530      1.1  christos 	if(!ixfr || !ixfr->data || ixfr->data->count==0)
    531      1.1  christos 		return NULL;
    532      1.1  christos 	n = (struct ixfr_data*)rbtree_search(ixfr->data, &ixfr->newest_serial);
    533      1.1  christos 	if(!n || n == (struct ixfr_data*)RBTREE_NULL)
    534      1.1  christos 		return NULL;
    535      1.1  christos 	return n;
    536      1.1  christos }
    537      1.1  christos 
    538      1.1  christos /* iterator over ixfr data. fetch next item. If loop or nothing, NULL */
    539      1.1  christos static struct ixfr_data* ixfr_data_next(struct zone_ixfr* ixfr,
    540      1.1  christos 	struct ixfr_data* cur)
    541      1.1  christos {
    542      1.1  christos 	struct ixfr_data* n;
    543      1.1  christos 	if(!cur || cur == (struct ixfr_data*)RBTREE_NULL)
    544      1.1  christos 		return NULL;
    545      1.1  christos 	if(cur->oldserial == ixfr->newest_serial)
    546      1.1  christos 		return NULL; /* that was the last element */
    547      1.1  christos 	n = (struct ixfr_data*)rbtree_next(&cur->node);
    548      1.1  christos 	if(n && n != (struct ixfr_data*)RBTREE_NULL &&
    549      1.1  christos 		cur->newserial == n->oldserial) {
    550      1.1  christos 		/* the next rbtree item is the next ixfr data item */
    551      1.1  christos 		return n;
    552      1.1  christos 	}
    553      1.1  christos 	/* If the next item is last of tree, and we have to loop around,
    554      1.1  christos 	 * the search performs the lookup for the next item we need.
    555      1.1  christos 	 * If the next item exists, but also is not connected, the search
    556      1.1  christos 	 * finds the correct connected ixfr in the sorted tree. */
    557      1.1  christos 	/* try searching for the correct ixfr data item */
    558      1.1  christos 	n = (struct ixfr_data*)rbtree_search(ixfr->data, &cur->newserial);
    559      1.1  christos 	if(!n || n == (struct ixfr_data*)RBTREE_NULL)
    560      1.1  christos 		return NULL;
    561      1.1  christos 	return n;
    562      1.1  christos }
    563      1.1  christos 
    564      1.1  christos /* iterator over ixfr data. fetch the previous item. If loop or nothing NULL.*/
    565      1.1  christos static struct ixfr_data* ixfr_data_prev(struct zone_ixfr* ixfr,
    566      1.1  christos 	struct ixfr_data* cur, size_t* prevcount)
    567      1.1  christos {
    568      1.1  christos 	struct ixfr_data* prev;
    569      1.1  christos 	if(!cur || cur == (struct ixfr_data*)RBTREE_NULL)
    570      1.1  christos 		return NULL;
    571      1.1  christos 	if(cur->oldserial == ixfr->oldest_serial)
    572      1.1  christos 		return NULL; /* this was the first element */
    573      1.1  christos 	prev = (struct ixfr_data*)rbtree_previous(&cur->node);
    574      1.1  christos 	if(!prev || prev == (struct ixfr_data*)RBTREE_NULL) {
    575      1.1  christos 		/* We hit the first element in the tree, go again
    576      1.1  christos 		 * at the last one. Wrap around. */
    577      1.1  christos 		prev = (struct ixfr_data*)rbtree_last(ixfr->data);
    578      1.1  christos 	}
    579      1.1  christos 	while(prev && prev != (struct ixfr_data*)RBTREE_NULL) {
    580      1.1  christos 		if(prev->newserial == cur->oldserial) {
    581      1.1  christos 			/* This is the correct matching previous ixfr data */
    582      1.1  christos 			/* Increase the prevcounter every time the routine
    583      1.1  christos 			 * returns an item, and if that becomes too large, we
    584      1.1  christos 			 * are in a loop. in that case, stop. */
    585      1.1  christos 			if(prevcount) {
    586      1.1  christos 				(*prevcount)++;
    587      1.1  christos 				if(*prevcount > ixfr->data->count + 12) {
    588      1.1  christos 					/* Larger than the max number of items
    589      1.1  christos 					 * plus a small margin. The longest
    590      1.1  christos 					 * chain is all the ixfr elements in
    591      1.1  christos 					 * the tree. It loops. */
    592      1.1  christos 					return NULL;
    593      1.1  christos 				}
    594      1.1  christos 			}
    595      1.1  christos 			return prev;
    596      1.1  christos 		}
    597      1.1  christos 		prev = (struct ixfr_data*)rbtree_previous(&prev->node);
    598      1.1  christos 		if(!prev || prev == (struct ixfr_data*)RBTREE_NULL) {
    599      1.1  christos 			/* We hit the first element in the tree, go again
    600      1.1  christos 			 * at the last one. Wrap around. */
    601      1.1  christos 			prev = (struct ixfr_data*)rbtree_last(ixfr->data);
    602      1.1  christos 		}
    603      1.1  christos 	}
    604      1.1  christos 	/* no elements in list */
    605      1.1  christos 	return NULL;
    606      1.1  christos }
    607      1.1  christos 
    608      1.1  christos /* connect IXFRs, return true if connected, false if not. Return last serial */
    609      1.1  christos static int connect_ixfrs(struct zone_ixfr* ixfr, struct ixfr_data* data,
    610      1.1  christos 	uint32_t* end_serial)
    611      1.1  christos {
    612      1.1  christos 	struct ixfr_data* p = data;
    613      1.1  christos 	while(p != NULL) {
    614      1.1  christos 		struct ixfr_data* next = ixfr_data_next(ixfr, p);
    615      1.1  christos 		if(next) {
    616      1.1  christos 			if(p->newserial != next->oldserial) {
    617      1.1  christos 				/* These ixfrs are not connected,
    618      1.1  christos 				 * during IXFR processing that could already
    619      1.1  christos 				 * have been deleted, but we check here
    620      1.1  christos 				 * in any case */
    621      1.1  christos 				return 0;
    622      1.1  christos 			}
    623      1.1  christos 		} else {
    624      1.1  christos 			/* the chain of IXFRs ends in this serial number */
    625      1.1  christos 			*end_serial = p->newserial;
    626      1.1  christos 		}
    627      1.1  christos 		p = next;
    628      1.1  christos 	}
    629      1.1  christos 	return 1;
    630      1.1  christos }
    631      1.1  christos 
    632      1.1  christos /* Count length of next record in data */
    633      1.1  christos static size_t count_rr_length(const uint8_t* data, size_t data_len,
    634      1.1  christos 	size_t current)
    635      1.1  christos {
    636      1.1  christos 	uint8_t label_size;
    637      1.1  christos 	uint16_t rdlen;
    638      1.1  christos 	size_t i = current;
    639      1.1  christos 	if(current >= data_len)
    640      1.1  christos 		return 0;
    641      1.1  christos 	/* pass the owner dname */
    642      1.1  christos 	while(1) {
    643      1.1  christos 		if(i+1 > data_len)
    644      1.1  christos 			return 0;
    645      1.1  christos 		label_size = data[i++];
    646      1.1  christos 		if(label_size == 0) {
    647      1.1  christos 			break;
    648      1.1  christos 		} else if((label_size &0xc0) != 0) {
    649      1.1  christos 			return 0; /* uncompressed dnames in IXFR store */
    650      1.1  christos 		} else if(i+label_size > data_len) {
    651      1.1  christos 			return 0;
    652      1.1  christos 		} else {
    653      1.1  christos 			i += label_size;
    654      1.1  christos 		}
    655      1.1  christos 	}
    656      1.1  christos 	/* after dname, we pass type, class, ttl, rdatalen */
    657      1.1  christos 	if(i+10 > data_len)
    658      1.1  christos 		return 0;
    659      1.1  christos 	i += 8;
    660      1.1  christos 	rdlen = read_uint16(data+i);
    661      1.1  christos 	i += 2;
    662      1.1  christos 	/* pass over the rdata */
    663      1.1  christos 	if(i+((size_t)rdlen) > data_len)
    664      1.1  christos 		return 0;
    665      1.1  christos 	i += ((size_t)rdlen);
    666      1.1  christos 	return i-current;
    667      1.1  christos }
    668      1.1  christos 
    669      1.1  christos /* Copy RRs into packet until packet full, return number RRs added */
    670      1.1  christos static uint16_t ixfr_copy_rrs_into_packet(struct query* query,
    671      1.1  christos 	struct pktcompression* pcomp)
    672      1.1  christos {
    673      1.1  christos 	uint16_t total_added = 0;
    674      1.1  christos 
    675      1.1  christos 	/* Copy RRs into the packet until the answer is full,
    676      1.1  christos 	 * when an RR does not fit, we return and add no more. */
    677      1.1  christos 
    678      1.1  christos 	/* Add first SOA */
    679      1.1  christos 	if(query->ixfr_count_newsoa < query->ixfr_end_data->newsoa_len) {
    680      1.1  christos 		/* the new SOA is added from the end_data segment, it is
    681      1.1  christos 		 * the final SOA of the result of the IXFR */
    682      1.1  christos 		if(ixfr_write_rr_pkt(query, query->packet, pcomp,
    683      1.1  christos 			query->ixfr_end_data->newsoa,
    684  1.1.1.2  christos 			query->ixfr_end_data->newsoa_len, total_added)) {
    685      1.1  christos 			query->ixfr_count_newsoa = query->ixfr_end_data->newsoa_len;
    686      1.1  christos 			total_added++;
    687      1.1  christos 			query->ixfr_pos_of_newsoa = buffer_position(query->packet);
    688      1.1  christos 		} else {
    689      1.1  christos 			/* cannot add another RR, so return */
    690      1.1  christos 			return total_added;
    691      1.1  christos 		}
    692      1.1  christos 	}
    693      1.1  christos 
    694      1.1  christos 	/* Add second SOA */
    695      1.1  christos 	if(query->ixfr_count_oldsoa < query->ixfr_data->oldsoa_len) {
    696      1.1  christos 		if(ixfr_write_rr_pkt(query, query->packet, pcomp,
    697      1.1  christos 			query->ixfr_data->oldsoa,
    698  1.1.1.2  christos 			query->ixfr_data->oldsoa_len, total_added)) {
    699      1.1  christos 			query->ixfr_count_oldsoa = query->ixfr_data->oldsoa_len;
    700      1.1  christos 			total_added++;
    701      1.1  christos 		} else {
    702      1.1  christos 			/* cannot add another RR, so return */
    703      1.1  christos 			return total_added;
    704      1.1  christos 		}
    705      1.1  christos 	}
    706      1.1  christos 
    707      1.1  christos 	/* Add del data, with deleted RRs and a SOA */
    708      1.1  christos 	while(query->ixfr_count_del < query->ixfr_data->del_len) {
    709      1.1  christos 		size_t rrlen = count_rr_length(query->ixfr_data->del,
    710      1.1  christos 			query->ixfr_data->del_len, query->ixfr_count_del);
    711      1.1  christos 		if(rrlen && ixfr_write_rr_pkt(query, query->packet, pcomp,
    712      1.1  christos 			query->ixfr_data->del + query->ixfr_count_del,
    713  1.1.1.2  christos 			rrlen, total_added)) {
    714      1.1  christos 			query->ixfr_count_del += rrlen;
    715      1.1  christos 			total_added++;
    716      1.1  christos 		} else {
    717      1.1  christos 			/* the next record does not fit in the remaining
    718      1.1  christos 			 * space of the packet */
    719      1.1  christos 			return total_added;
    720      1.1  christos 		}
    721      1.1  christos 	}
    722      1.1  christos 
    723      1.1  christos 	/* Add add data, with added RRs and a SOA */
    724      1.1  christos 	while(query->ixfr_count_add < query->ixfr_data->add_len) {
    725      1.1  christos 		size_t rrlen = count_rr_length(query->ixfr_data->add,
    726      1.1  christos 			query->ixfr_data->add_len, query->ixfr_count_add);
    727      1.1  christos 		if(rrlen && ixfr_write_rr_pkt(query, query->packet, pcomp,
    728      1.1  christos 			query->ixfr_data->add + query->ixfr_count_add,
    729  1.1.1.2  christos 			rrlen, total_added)) {
    730      1.1  christos 			query->ixfr_count_add += rrlen;
    731      1.1  christos 			total_added++;
    732      1.1  christos 		} else {
    733      1.1  christos 			/* the next record does not fit in the remaining
    734      1.1  christos 			 * space of the packet */
    735      1.1  christos 			return total_added;
    736      1.1  christos 		}
    737      1.1  christos 	}
    738      1.1  christos 	return total_added;
    739      1.1  christos }
    740      1.1  christos 
    741      1.1  christos query_state_type query_ixfr(struct nsd *nsd, struct query *query)
    742      1.1  christos {
    743      1.1  christos 	uint16_t total_added = 0;
    744      1.1  christos 	struct pktcompression pcomp;
    745      1.1  christos 
    746      1.1  christos 	if (query->ixfr_is_done)
    747      1.1  christos 		return QUERY_PROCESSED;
    748      1.1  christos 
    749      1.1  christos 	pktcompression_init(&pcomp);
    750      1.1  christos 	if (query->maxlen > IXFR_MAX_MESSAGE_LEN)
    751      1.1  christos 		query->maxlen = IXFR_MAX_MESSAGE_LEN;
    752      1.1  christos 
    753      1.1  christos 	assert(!query_overflow(query));
    754      1.1  christos 	/* only keep running values for most packets */
    755      1.1  christos 	query->tsig_prepare_it = 0;
    756      1.1  christos 	query->tsig_update_it = 1;
    757      1.1  christos 	if(query->tsig_sign_it) {
    758      1.1  christos 		/* prepare for next updates */
    759      1.1  christos 		query->tsig_prepare_it = 1;
    760      1.1  christos 		query->tsig_sign_it = 0;
    761      1.1  christos 	}
    762      1.1  christos 
    763      1.1  christos 	if (query->ixfr_data == NULL) {
    764      1.1  christos 		/* This is the first packet, process the query further */
    765      1.1  christos 		uint32_t qserial = 0, current_serial = 0, end_serial = 0;
    766      1.1  christos 		struct zone* zone;
    767      1.1  christos 		struct ixfr_data* ixfr_data;
    768      1.1  christos 		size_t oldpos;
    769      1.1  christos 
    770      1.1  christos 		STATUP(nsd, rixfr);
    771      1.1  christos 		/* parse the serial number from the IXFR request */
    772      1.1  christos 		oldpos = QHEADERSZ;
    773      1.1  christos 		if(!parse_qserial(query->packet, &qserial, &oldpos)) {
    774      1.1  christos 			NSCOUNT_SET(query->packet, 0);
    775      1.1  christos 			ARCOUNT_SET(query->packet, 0);
    776      1.1  christos 			buffer_set_position(query->packet, oldpos);
    777      1.1  christos 			RCODE_SET(query->packet, RCODE_FORMAT);
    778      1.1  christos 			return QUERY_PROCESSED;
    779      1.1  christos 		}
    780      1.1  christos 		NSCOUNT_SET(query->packet, 0);
    781      1.1  christos 		ARCOUNT_SET(query->packet, 0);
    782      1.1  christos 		buffer_set_position(query->packet, oldpos);
    783      1.1  christos 		DEBUG(DEBUG_XFRD,1, (LOG_INFO, "ixfr query routine, %s IXFR=%u",
    784      1.1  christos 			dname_to_string(query->qname, NULL), (unsigned)qserial));
    785      1.1  christos 
    786      1.1  christos 		/* do we have an IXFR with this serial number? If not, serve AXFR */
    787      1.1  christos 		zone = namedb_find_zone(nsd->db, query->qname);
    788      1.1  christos 		if(!zone) {
    789      1.1  christos 			/* no zone is present */
    790      1.1  christos 			RCODE_SET(query->packet, RCODE_NOTAUTH);
    791      1.1  christos 			return QUERY_PROCESSED;
    792      1.1  christos 		}
    793      1.1  christos 		ZTATUP(nsd, zone, rixfr);
    794      1.1  christos 
    795      1.1  christos 		/* if the query is for same or newer serial than our current
    796      1.1  christos 		 * serial, then serve a single SOA with our current serial */
    797      1.1  christos 		current_serial = zone_get_current_serial(zone);
    798      1.1  christos 		if(compare_serial(qserial, current_serial) >= 0) {
    799      1.1  christos 			if(!zone->soa_rrset || zone->soa_rrset->rr_count != 1){
    800      1.1  christos 				RCODE_SET(query->packet, RCODE_SERVFAIL);
    801      1.1  christos 				return QUERY_PROCESSED;
    802      1.1  christos 			}
    803      1.1  christos 			query_add_compression_domain(query, zone->apex,
    804      1.1  christos 				QHEADERSZ);
    805      1.1  christos 			if(packet_encode_rr(query, zone->apex,
    806      1.1  christos 				&zone->soa_rrset->rrs[0],
    807      1.1  christos 				zone->soa_rrset->rrs[0].ttl)) {
    808      1.1  christos 				ANCOUNT_SET(query->packet, 1);
    809      1.1  christos 			} else {
    810      1.1  christos 				RCODE_SET(query->packet, RCODE_SERVFAIL);
    811      1.1  christos 			}
    812      1.1  christos 			AA_SET(query->packet);
    813      1.1  christos 			query_clear_compression_tables(query);
    814      1.1  christos 			if(query->tsig.status == TSIG_OK)
    815      1.1  christos 				query->tsig_sign_it = 1;
    816      1.1  christos 			return QUERY_PROCESSED;
    817      1.1  christos 		}
    818      1.1  christos 
    819      1.1  christos 		if(!zone->ixfr) {
    820      1.1  christos 			/* we have no ixfr information for the zone, make an AXFR */
    821      1.1  christos 			if(query->tsig_prepare_it)
    822      1.1  christos 				query->tsig_sign_it = 1;
    823  1.1.1.2  christos 			VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, no ixfr info for zone: %s",
    824  1.1.1.2  christos 				dname_to_string(query->qname, NULL)));
    825      1.1  christos 			return query_axfr(nsd, query, 0);
    826      1.1  christos 		}
    827      1.1  christos 		ixfr_data = zone_ixfr_find_serial(zone->ixfr, qserial);
    828      1.1  christos 		if(!ixfr_data) {
    829      1.1  christos 			/* the specific version is not available, make an AXFR */
    830      1.1  christos 			if(query->tsig_prepare_it)
    831      1.1  christos 				query->tsig_sign_it = 1;
    832  1.1.1.2  christos 			VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, no history for serial for zone: %s",
    833  1.1.1.2  christos 				dname_to_string(query->qname, NULL)));
    834      1.1  christos 			return query_axfr(nsd, query, 0);
    835      1.1  christos 		}
    836      1.1  christos 		/* see if the IXFRs connect to the next IXFR, and if it ends
    837      1.1  christos 		 * at the current served zone, if not, AXFR */
    838      1.1  christos 		if(!connect_ixfrs(zone->ixfr, ixfr_data, &end_serial) ||
    839      1.1  christos 			end_serial != current_serial) {
    840      1.1  christos 			if(query->tsig_prepare_it)
    841      1.1  christos 				query->tsig_sign_it = 1;
    842  1.1.1.2  christos 			VERBOSITY(2, (LOG_INFO, "ixfr fallback to axfr, incomplete history from this serial for zone: %s",
    843  1.1.1.2  christos 				dname_to_string(query->qname, NULL)));
    844      1.1  christos 			return query_axfr(nsd, query, 0);
    845      1.1  christos 		}
    846      1.1  christos 
    847      1.1  christos 		query->zone = zone;
    848      1.1  christos 		query->ixfr_data = ixfr_data;
    849      1.1  christos 		query->ixfr_is_done = 0;
    850      1.1  christos 		/* set up to copy the last version's SOA as first SOA */
    851      1.1  christos 		query->ixfr_end_data = ixfr_data_last(zone->ixfr);
    852      1.1  christos 		query->ixfr_count_newsoa = 0;
    853      1.1  christos 		query->ixfr_count_oldsoa = 0;
    854      1.1  christos 		query->ixfr_count_del = 0;
    855      1.1  christos 		query->ixfr_count_add = 0;
    856      1.1  christos 		query->ixfr_pos_of_newsoa = 0;
    857      1.1  christos 		/* the query name can be compressed to */
    858      1.1  christos 		pktcompression_insert_with_labels(&pcomp,
    859      1.1  christos 			buffer_at(query->packet, QHEADERSZ),
    860      1.1  christos 			query->qname->name_size, QHEADERSZ);
    861      1.1  christos 		if(query->tsig.status == TSIG_OK) {
    862      1.1  christos 			query->tsig_sign_it = 1; /* sign first packet in stream */
    863      1.1  christos 		}
    864      1.1  christos 	} else {
    865      1.1  christos 		/*
    866      1.1  christos 		 * Query name need not be repeated after the
    867      1.1  christos 		 * first response packet.
    868      1.1  christos 		 */
    869      1.1  christos 		buffer_set_limit(query->packet, QHEADERSZ);
    870      1.1  christos 		QDCOUNT_SET(query->packet, 0);
    871      1.1  christos 		query_prepare_response(query);
    872      1.1  christos 	}
    873      1.1  christos 
    874      1.1  christos 	total_added = ixfr_copy_rrs_into_packet(query, &pcomp);
    875      1.1  christos 
    876      1.1  christos 	while(query->ixfr_count_add >= query->ixfr_data->add_len) {
    877      1.1  christos 		struct ixfr_data* next = ixfr_data_next(query->zone->ixfr,
    878      1.1  christos 			query->ixfr_data);
    879      1.1  christos 		/* finished the ixfr_data */
    880      1.1  christos 		if(next) {
    881      1.1  christos 			/* move to the next IXFR */
    882      1.1  christos 			query->ixfr_data = next;
    883      1.1  christos 			/* we need to skip the SOA records, set len to done*/
    884      1.1  christos 			/* the newsoa count is already done, at end_data len */
    885      1.1  christos 			query->ixfr_count_oldsoa = next->oldsoa_len;
    886      1.1  christos 			/* and then set up to copy the del and add sections */
    887      1.1  christos 			query->ixfr_count_del = 0;
    888      1.1  christos 			query->ixfr_count_add = 0;
    889      1.1  christos 			total_added += ixfr_copy_rrs_into_packet(query, &pcomp);
    890      1.1  christos 		} else {
    891      1.1  christos 			/* we finished the IXFR */
    892      1.1  christos 			/* sign the last packet */
    893      1.1  christos 			query->tsig_sign_it = 1;
    894      1.1  christos 			query->ixfr_is_done = 1;
    895      1.1  christos 			break;
    896      1.1  christos 		}
    897      1.1  christos 	}
    898      1.1  christos 
    899      1.1  christos 	/* return the answer */
    900      1.1  christos 	AA_SET(query->packet);
    901      1.1  christos 	ANCOUNT_SET(query->packet, total_added);
    902      1.1  christos 	NSCOUNT_SET(query->packet, 0);
    903      1.1  christos 	ARCOUNT_SET(query->packet, 0);
    904      1.1  christos 
    905      1.1  christos 	if(!query->tcp && !query->ixfr_is_done) {
    906      1.1  christos 		TC_SET(query->packet);
    907      1.1  christos 		if(query->ixfr_pos_of_newsoa) {
    908      1.1  christos 			/* if we recorded the newsoa in the result, snip off
    909      1.1  christos 			 * the rest of the response, the RFC1995 response for
    910      1.1  christos 			 * when it does not fit is only the latest SOA */
    911      1.1  christos 			buffer_set_position(query->packet, query->ixfr_pos_of_newsoa);
    912      1.1  christos 			ANCOUNT_SET(query->packet, 1);
    913      1.1  christos 		}
    914      1.1  christos 		query->ixfr_is_done = 1;
    915      1.1  christos 	}
    916      1.1  christos 
    917      1.1  christos 	/* check if it needs tsig signatures */
    918      1.1  christos 	if(query->tsig.status == TSIG_OK) {
    919      1.1  christos #if IXFR_TSIG_SIGN_EVERY_NTH > 0
    920      1.1  christos 		if(query->tsig.updates_since_last_prepare >= IXFR_TSIG_SIGN_EVERY_NTH) {
    921      1.1  christos #endif
    922      1.1  christos 			query->tsig_sign_it = 1;
    923      1.1  christos #if IXFR_TSIG_SIGN_EVERY_NTH > 0
    924      1.1  christos 		}
    925      1.1  christos #endif
    926      1.1  christos 	}
    927      1.1  christos 	pktcompression_freeup(&pcomp);
    928      1.1  christos 	return QUERY_IN_IXFR;
    929      1.1  christos }
    930      1.1  christos 
    931      1.1  christos /* free ixfr_data structure */
    932      1.1  christos static void ixfr_data_free(struct ixfr_data* data)
    933      1.1  christos {
    934      1.1  christos 	if(!data)
    935      1.1  christos 		return;
    936      1.1  christos 	free(data->newsoa);
    937      1.1  christos 	free(data->oldsoa);
    938      1.1  christos 	free(data->del);
    939      1.1  christos 	free(data->add);
    940      1.1  christos 	free(data->log_str);
    941      1.1  christos 	free(data);
    942      1.1  christos }
    943      1.1  christos 
    944      1.1  christos size_t ixfr_data_size(struct ixfr_data* data)
    945      1.1  christos {
    946      1.1  christos 	return sizeof(struct ixfr_data) + data->newsoa_len + data->oldsoa_len
    947      1.1  christos 		+ data->del_len + data->add_len;
    948      1.1  christos }
    949      1.1  christos 
    950      1.1  christos struct ixfr_store* ixfr_store_start(struct zone* zone,
    951  1.1.1.2  christos 	struct ixfr_store* ixfr_store_mem)
    952      1.1  christos {
    953      1.1  christos 	struct ixfr_store* ixfr_store = ixfr_store_mem;
    954      1.1  christos 	memset(ixfr_store, 0, sizeof(*ixfr_store));
    955      1.1  christos 	ixfr_store->zone = zone;
    956      1.1  christos 	ixfr_store->data = xalloc_zero(sizeof(*ixfr_store->data));
    957      1.1  christos 	return ixfr_store;
    958      1.1  christos }
    959      1.1  christos 
    960      1.1  christos void ixfr_store_cancel(struct ixfr_store* ixfr_store)
    961      1.1  christos {
    962      1.1  christos 	ixfr_store->cancelled = 1;
    963      1.1  christos 	ixfr_data_free(ixfr_store->data);
    964      1.1  christos 	ixfr_store->data = NULL;
    965      1.1  christos }
    966      1.1  christos 
    967      1.1  christos void ixfr_store_free(struct ixfr_store* ixfr_store)
    968      1.1  christos {
    969      1.1  christos 	if(!ixfr_store)
    970      1.1  christos 		return;
    971      1.1  christos 	ixfr_data_free(ixfr_store->data);
    972      1.1  christos }
    973      1.1  christos 
    974      1.1  christos /* make space in record data for the new size, grows the allocation */
    975      1.1  christos static void ixfr_rrs_make_space(uint8_t** rrs, size_t* len, size_t* capacity,
    976      1.1  christos 	size_t added)
    977      1.1  christos {
    978      1.1  christos 	size_t newsize = 0;
    979      1.1  christos 	if(*rrs == NULL) {
    980      1.1  christos 		newsize = IXFR_STORE_INITIAL_SIZE;
    981      1.1  christos 	} else {
    982      1.1  christos 		if(*len + added <= *capacity)
    983      1.1  christos 			return; /* already enough space */
    984      1.1  christos 		newsize = (*capacity)*2;
    985      1.1  christos 	}
    986      1.1  christos 	if(*len + added > newsize)
    987      1.1  christos 		newsize = *len + added;
    988      1.1  christos 	if(*rrs == NULL) {
    989      1.1  christos 		*rrs = xalloc(newsize);
    990      1.1  christos 	} else {
    991      1.1  christos 		*rrs = xrealloc(*rrs, newsize);
    992      1.1  christos 	}
    993      1.1  christos 	*capacity = newsize;
    994      1.1  christos }
    995      1.1  christos 
    996      1.1  christos /* put new SOA record after delrrs and addrrs */
    997      1.1  christos static void ixfr_put_newsoa(struct ixfr_store* ixfr_store, uint8_t** rrs,
    998      1.1  christos 	size_t* len, size_t* capacity)
    999      1.1  christos {
   1000      1.1  christos 	uint8_t* soa;
   1001      1.1  christos 	size_t soa_len;
   1002      1.1  christos 	if(!ixfr_store->data)
   1003      1.1  christos 		return; /* data should be nonNULL, we are not cancelled */
   1004      1.1  christos 	soa = ixfr_store->data->newsoa;
   1005      1.1  christos 	soa_len= ixfr_store->data->newsoa_len;
   1006      1.1  christos 	ixfr_rrs_make_space(rrs, len, capacity, soa_len);
   1007      1.1  christos 	if(!*rrs || *len + soa_len > *capacity) {
   1008      1.1  christos 		log_msg(LOG_ERR, "ixfr_store addrr: cannot allocate space");
   1009      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1010      1.1  christos 		return;
   1011      1.1  christos 	}
   1012      1.1  christos 	memmove(*rrs + *len, soa, soa_len);
   1013      1.1  christos 	*len += soa_len;
   1014      1.1  christos }
   1015      1.1  christos 
   1016      1.1  christos /* trim unused storage from the rrs data */
   1017      1.1  christos static void ixfr_trim_capacity(uint8_t** rrs, size_t* len, size_t* capacity)
   1018      1.1  christos {
   1019      1.1  christos 	if(*rrs == NULL)
   1020      1.1  christos 		return;
   1021      1.1  christos 	if(*capacity == *len)
   1022      1.1  christos 		return;
   1023      1.1  christos 	*rrs = xrealloc(*rrs, *len);
   1024      1.1  christos 	*capacity = *len;
   1025      1.1  christos }
   1026      1.1  christos 
   1027      1.1  christos void ixfr_store_finish_data(struct ixfr_store* ixfr_store)
   1028      1.1  christos {
   1029      1.1  christos 	if(ixfr_store->data_trimmed)
   1030      1.1  christos 		return;
   1031      1.1  christos 	ixfr_store->data_trimmed = 1;
   1032      1.1  christos 
   1033      1.1  christos 	/* put new serial SOA record after delrrs and addrrs */
   1034      1.1  christos 	ixfr_put_newsoa(ixfr_store, &ixfr_store->data->del,
   1035      1.1  christos 		&ixfr_store->data->del_len, &ixfr_store->del_capacity);
   1036      1.1  christos 	ixfr_put_newsoa(ixfr_store, &ixfr_store->data->add,
   1037      1.1  christos 		&ixfr_store->data->add_len, &ixfr_store->add_capacity);
   1038      1.1  christos 
   1039      1.1  christos 	/* trim the data in the store, the overhead from capacity is
   1040      1.1  christos 	 * removed */
   1041      1.1  christos 	if(!ixfr_store->data)
   1042      1.1  christos 		return; /* data should be nonNULL, we are not cancelled */
   1043      1.1  christos 	ixfr_trim_capacity(&ixfr_store->data->del,
   1044      1.1  christos 		&ixfr_store->data->del_len, &ixfr_store->del_capacity);
   1045      1.1  christos 	ixfr_trim_capacity(&ixfr_store->data->add,
   1046      1.1  christos 		&ixfr_store->data->add_len, &ixfr_store->add_capacity);
   1047      1.1  christos }
   1048      1.1  christos 
   1049      1.1  christos void ixfr_store_finish(struct ixfr_store* ixfr_store, struct nsd* nsd,
   1050      1.1  christos 	char* log_buf)
   1051      1.1  christos {
   1052      1.1  christos 	if(ixfr_store->cancelled) {
   1053      1.1  christos 		ixfr_store_free(ixfr_store);
   1054      1.1  christos 		return;
   1055      1.1  christos 	}
   1056      1.1  christos 
   1057      1.1  christos 	ixfr_store_finish_data(ixfr_store);
   1058      1.1  christos 
   1059      1.1  christos 	if(ixfr_store->cancelled) {
   1060      1.1  christos 		ixfr_store_free(ixfr_store);
   1061      1.1  christos 		return;
   1062      1.1  christos 	}
   1063      1.1  christos 
   1064      1.1  christos 	if(log_buf && !ixfr_store->data->log_str)
   1065      1.1  christos 		ixfr_store->data->log_str = strdup(log_buf);
   1066      1.1  christos 
   1067      1.1  christos 	/* store the data in the zone */
   1068      1.1  christos 	if(!ixfr_store->zone->ixfr)
   1069      1.1  christos 		ixfr_store->zone->ixfr = zone_ixfr_create(nsd);
   1070      1.1  christos 	zone_ixfr_make_space(ixfr_store->zone->ixfr, ixfr_store->zone,
   1071      1.1  christos 		ixfr_store->data, ixfr_store);
   1072      1.1  christos 	if(ixfr_store->cancelled) {
   1073      1.1  christos 		ixfr_store_free(ixfr_store);
   1074      1.1  christos 		return;
   1075      1.1  christos 	}
   1076      1.1  christos 	zone_ixfr_add(ixfr_store->zone->ixfr, ixfr_store->data, 1);
   1077      1.1  christos 	ixfr_store->data = NULL;
   1078      1.1  christos 
   1079      1.1  christos 	/* free structure */
   1080      1.1  christos 	ixfr_store_free(ixfr_store);
   1081      1.1  christos }
   1082      1.1  christos 
   1083      1.1  christos /* read SOA rdata section for SOA storage */
   1084      1.1  christos static int read_soa_rdata(struct buffer* packet, uint8_t* primns,
   1085      1.1  christos 	int* primns_len, uint8_t* email, int* email_len,
   1086      1.1  christos 	uint32_t* serial, uint32_t* refresh, uint32_t* retry,
   1087      1.1  christos 	uint32_t* expire, uint32_t* minimum, size_t* sz)
   1088      1.1  christos {
   1089      1.1  christos 	if(!(*primns_len = dname_make_wire_from_packet(primns, packet, 1))) {
   1090      1.1  christos 		log_msg(LOG_ERR, "ixfr_store: cannot parse soa nsname in packet");
   1091      1.1  christos 		return 0;
   1092      1.1  christos 	}
   1093      1.1  christos 	*sz += *primns_len;
   1094      1.1  christos 	if(!(*email_len = dname_make_wire_from_packet(email, packet, 1))) {
   1095      1.1  christos 		log_msg(LOG_ERR, "ixfr_store: cannot parse soa maintname in packet");
   1096      1.1  christos 		return 0;
   1097      1.1  christos 	}
   1098      1.1  christos 	*sz += *email_len;
   1099      1.1  christos 	*serial = buffer_read_u32(packet);
   1100      1.1  christos 	*sz += 4;
   1101      1.1  christos 	*refresh = buffer_read_u32(packet);
   1102      1.1  christos 	*sz += 4;
   1103      1.1  christos 	*retry = buffer_read_u32(packet);
   1104      1.1  christos 	*sz += 4;
   1105      1.1  christos 	*expire = buffer_read_u32(packet);
   1106      1.1  christos 	*sz += 4;
   1107      1.1  christos 	*minimum = buffer_read_u32(packet);
   1108      1.1  christos 	*sz += 4;
   1109      1.1  christos 	return 1;
   1110      1.1  christos }
   1111      1.1  christos 
   1112      1.1  christos /* store SOA record data in memory buffer */
   1113      1.1  christos static void store_soa(uint8_t* soa, struct zone* zone, uint32_t ttl,
   1114      1.1  christos 	uint16_t rdlen_uncompressed, uint8_t* primns, int primns_len,
   1115      1.1  christos 	uint8_t* email, int email_len, uint32_t serial, uint32_t refresh,
   1116      1.1  christos 	uint32_t retry, uint32_t expire, uint32_t minimum)
   1117      1.1  christos {
   1118      1.1  christos 	uint8_t* sp = soa;
   1119      1.1  christos 	memmove(sp, dname_name(domain_dname(zone->apex)),
   1120      1.1  christos 		domain_dname(zone->apex)->name_size);
   1121      1.1  christos 	sp += domain_dname(zone->apex)->name_size;
   1122      1.1  christos 	write_uint16(sp, TYPE_SOA);
   1123      1.1  christos 	sp += 2;
   1124      1.1  christos 	write_uint16(sp, CLASS_IN);
   1125      1.1  christos 	sp += 2;
   1126      1.1  christos 	write_uint32(sp, ttl);
   1127      1.1  christos 	sp += 4;
   1128      1.1  christos 	write_uint16(sp, rdlen_uncompressed);
   1129      1.1  christos 	sp += 2;
   1130      1.1  christos 	memmove(sp, primns, primns_len);
   1131      1.1  christos 	sp += primns_len;
   1132      1.1  christos 	memmove(sp, email, email_len);
   1133      1.1  christos 	sp += email_len;
   1134      1.1  christos 	write_uint32(sp, serial);
   1135      1.1  christos 	sp += 4;
   1136      1.1  christos 	write_uint32(sp, refresh);
   1137      1.1  christos 	sp += 4;
   1138      1.1  christos 	write_uint32(sp, retry);
   1139      1.1  christos 	sp += 4;
   1140      1.1  christos 	write_uint32(sp, expire);
   1141      1.1  christos 	sp += 4;
   1142      1.1  christos 	write_uint32(sp, minimum);
   1143      1.1  christos }
   1144      1.1  christos 
   1145  1.1.1.2  christos void ixfr_store_add_newsoa(struct ixfr_store* ixfr_store, uint32_t ttl,
   1146  1.1.1.2  christos 	struct buffer* packet, size_t rrlen)
   1147      1.1  christos {
   1148      1.1  christos 	size_t oldpos, sz = 0;
   1149  1.1.1.2  christos 	uint32_t serial, refresh, retry, expire, minimum;
   1150  1.1.1.2  christos 	uint16_t rdlen_uncompressed;
   1151      1.1  christos 	int primns_len = 0, email_len = 0;
   1152      1.1  christos 	uint8_t primns[MAXDOMAINLEN + 1], email[MAXDOMAINLEN + 1];
   1153      1.1  christos 
   1154      1.1  christos 	if(ixfr_store->cancelled)
   1155      1.1  christos 		return;
   1156      1.1  christos 	if(ixfr_store->data->newsoa) {
   1157      1.1  christos 		free(ixfr_store->data->newsoa);
   1158      1.1  christos 		ixfr_store->data->newsoa = NULL;
   1159      1.1  christos 		ixfr_store->data->newsoa_len = 0;
   1160      1.1  christos 	}
   1161      1.1  christos 	oldpos = buffer_position(packet);
   1162      1.1  christos 
   1163      1.1  christos 	/* calculate the length */
   1164      1.1  christos 	sz = domain_dname(ixfr_store->zone->apex)->name_size;
   1165  1.1.1.2  christos 	sz += 2 /* type */ + 2 /* class */ + 4 /* ttl */ + 2 /* rdlen */;
   1166  1.1.1.2  christos 	if(!buffer_available(packet, rrlen)) {
   1167      1.1  christos 		/* not possible already parsed, but fail nicely anyway */
   1168      1.1  christos 		log_msg(LOG_ERR, "ixfr_store: not enough rdata space in packet");
   1169      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1170      1.1  christos 		buffer_set_position(packet, oldpos);
   1171      1.1  christos 		return;
   1172      1.1  christos 	}
   1173      1.1  christos 	if(!read_soa_rdata(packet, primns, &primns_len, email, &email_len,
   1174      1.1  christos 		&serial, &refresh, &retry, &expire, &minimum, &sz)) {
   1175      1.1  christos 		log_msg(LOG_ERR, "ixfr_store newsoa: cannot parse packet");
   1176      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1177      1.1  christos 		buffer_set_position(packet, oldpos);
   1178      1.1  christos 		return;
   1179      1.1  christos 	}
   1180      1.1  christos 	rdlen_uncompressed = primns_len + email_len + 4 + 4 + 4 + 4 + 4;
   1181      1.1  christos 
   1182  1.1.1.2  christos 	ixfr_store->data->newserial = serial;
   1183  1.1.1.2  christos 
   1184      1.1  christos 	/* store the soa record */
   1185      1.1  christos 	ixfr_store->data->newsoa = xalloc(sz);
   1186      1.1  christos 	ixfr_store->data->newsoa_len = sz;
   1187      1.1  christos 	store_soa(ixfr_store->data->newsoa, ixfr_store->zone, ttl,
   1188      1.1  christos 		rdlen_uncompressed, primns, primns_len, email, email_len,
   1189      1.1  christos 		serial, refresh, retry, expire, minimum);
   1190      1.1  christos 
   1191      1.1  christos 	buffer_set_position(packet, oldpos);
   1192      1.1  christos }
   1193      1.1  christos 
   1194      1.1  christos void ixfr_store_add_oldsoa(struct ixfr_store* ixfr_store, uint32_t ttl,
   1195      1.1  christos 	struct buffer* packet, size_t rrlen)
   1196      1.1  christos {
   1197      1.1  christos 	size_t oldpos, sz = 0;
   1198      1.1  christos 	uint32_t serial, refresh, retry, expire, minimum;
   1199      1.1  christos 	uint16_t rdlen_uncompressed;
   1200      1.1  christos 	int primns_len = 0, email_len = 0;
   1201      1.1  christos 	uint8_t primns[MAXDOMAINLEN + 1], email[MAXDOMAINLEN + 1];
   1202      1.1  christos 
   1203      1.1  christos 	if(ixfr_store->cancelled)
   1204      1.1  christos 		return;
   1205      1.1  christos 	if(ixfr_store->data->oldsoa) {
   1206      1.1  christos 		free(ixfr_store->data->oldsoa);
   1207      1.1  christos 		ixfr_store->data->oldsoa = NULL;
   1208      1.1  christos 		ixfr_store->data->oldsoa_len = 0;
   1209      1.1  christos 	}
   1210      1.1  christos 	/* we have the old SOA and thus we are sure it is an IXFR, make space*/
   1211      1.1  christos 	zone_ixfr_make_space(ixfr_store->zone->ixfr, ixfr_store->zone,
   1212      1.1  christos 		ixfr_store->data, ixfr_store);
   1213      1.1  christos 	if(ixfr_store->cancelled)
   1214      1.1  christos 		return;
   1215      1.1  christos 	oldpos = buffer_position(packet);
   1216      1.1  christos 
   1217      1.1  christos 	/* calculate the length */
   1218      1.1  christos 	sz = domain_dname(ixfr_store->zone->apex)->name_size;
   1219      1.1  christos 	sz += 2 /*type*/ + 2 /*class*/ + 4 /*ttl*/ + 2 /*rdlen*/;
   1220      1.1  christos 	if(!buffer_available(packet, rrlen)) {
   1221      1.1  christos 		/* not possible already parsed, but fail nicely anyway */
   1222      1.1  christos 		log_msg(LOG_ERR, "ixfr_store oldsoa: not enough rdata space in packet");
   1223      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1224      1.1  christos 		buffer_set_position(packet, oldpos);
   1225      1.1  christos 		return;
   1226      1.1  christos 	}
   1227      1.1  christos 	if(!read_soa_rdata(packet, primns, &primns_len, email, &email_len,
   1228      1.1  christos 		&serial, &refresh, &retry, &expire, &minimum, &sz)) {
   1229      1.1  christos 		log_msg(LOG_ERR, "ixfr_store oldsoa: cannot parse packet");
   1230      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1231      1.1  christos 		buffer_set_position(packet, oldpos);
   1232      1.1  christos 		return;
   1233      1.1  christos 	}
   1234      1.1  christos 	rdlen_uncompressed = primns_len + email_len + 4 + 4 + 4 + 4 + 4;
   1235      1.1  christos 
   1236  1.1.1.2  christos 	ixfr_store->data->oldserial = serial;
   1237  1.1.1.2  christos 
   1238      1.1  christos 	/* store the soa record */
   1239      1.1  christos 	ixfr_store->data->oldsoa = xalloc(sz);
   1240      1.1  christos 	ixfr_store->data->oldsoa_len = sz;
   1241      1.1  christos 	store_soa(ixfr_store->data->oldsoa, ixfr_store->zone, ttl,
   1242      1.1  christos 		rdlen_uncompressed, primns, primns_len, email, email_len,
   1243      1.1  christos 		serial, refresh, retry, expire, minimum);
   1244      1.1  christos 
   1245      1.1  christos 	buffer_set_position(packet, oldpos);
   1246      1.1  christos }
   1247      1.1  christos 
   1248      1.1  christos /* store RR in data segment */
   1249      1.1  christos static int ixfr_putrr(const struct dname* dname, uint16_t type, uint16_t klass,
   1250      1.1  christos 	uint32_t ttl, rdata_atom_type* rdatas, ssize_t rdata_num,
   1251      1.1  christos 	uint8_t** rrs, size_t* rrs_len, size_t* rrs_capacity)
   1252      1.1  christos {
   1253      1.1  christos 	size_t rdlen_uncompressed, sz;
   1254      1.1  christos 	uint8_t* sp;
   1255      1.1  christos 	int i;
   1256      1.1  christos 
   1257      1.1  christos 	/* find rdatalen */
   1258      1.1  christos 	rdlen_uncompressed = 0;
   1259      1.1  christos 	for(i=0; i<rdata_num; i++) {
   1260      1.1  christos 		if(rdata_atom_is_domain(type, i)) {
   1261      1.1  christos 			rdlen_uncompressed += domain_dname(rdatas[i].domain)
   1262      1.1  christos 				->name_size;
   1263      1.1  christos 		} else {
   1264      1.1  christos 			rdlen_uncompressed += rdatas[i].data[0];
   1265      1.1  christos 		}
   1266      1.1  christos 	}
   1267      1.1  christos 	sz = dname->name_size + 2 /*type*/ + 2 /*class*/ + 4 /*ttl*/ +
   1268      1.1  christos 		2 /*rdlen*/ + rdlen_uncompressed;
   1269      1.1  christos 
   1270      1.1  christos 	/* store RR in IXFR data */
   1271      1.1  christos 	ixfr_rrs_make_space(rrs, rrs_len, rrs_capacity, sz);
   1272      1.1  christos 	if(!*rrs || *rrs_len + sz > *rrs_capacity) {
   1273      1.1  christos 		return 0;
   1274      1.1  christos 	}
   1275      1.1  christos 	/* copy data into add */
   1276      1.1  christos 	sp = *rrs + *rrs_len;
   1277      1.1  christos 	*rrs_len += sz;
   1278      1.1  christos 	memmove(sp, dname_name(dname), dname->name_size);
   1279      1.1  christos 	sp += dname->name_size;
   1280      1.1  christos 	write_uint16(sp, type);
   1281      1.1  christos 	sp += 2;
   1282      1.1  christos 	write_uint16(sp, klass);
   1283      1.1  christos 	sp += 2;
   1284      1.1  christos 	write_uint32(sp, ttl);
   1285      1.1  christos 	sp += 4;
   1286      1.1  christos 	write_uint16(sp, rdlen_uncompressed);
   1287      1.1  christos 	sp += 2;
   1288      1.1  christos 	for(i=0; i<rdata_num; i++) {
   1289      1.1  christos 		if(rdata_atom_is_domain(type, i)) {
   1290      1.1  christos 			memmove(sp, dname_name(domain_dname(rdatas[i].domain)),
   1291      1.1  christos 				domain_dname(rdatas[i].domain)->name_size);
   1292      1.1  christos 			sp += domain_dname(rdatas[i].domain)->name_size;
   1293      1.1  christos 		} else {
   1294      1.1  christos 			memmove(sp, &rdatas[i].data[1], rdatas[i].data[0]);
   1295      1.1  christos 			sp += rdatas[i].data[0];
   1296      1.1  christos 		}
   1297      1.1  christos 	}
   1298      1.1  christos 	return 1;
   1299      1.1  christos }
   1300      1.1  christos 
   1301      1.1  christos void ixfr_store_putrr(struct ixfr_store* ixfr_store, const struct dname* dname,
   1302      1.1  christos 	uint16_t type, uint16_t klass, uint32_t ttl, struct buffer* packet,
   1303      1.1  christos 	uint16_t rrlen, struct region* temp_region, uint8_t** rrs,
   1304      1.1  christos 	size_t* rrs_len, size_t* rrs_capacity)
   1305      1.1  christos {
   1306      1.1  christos 	domain_table_type *temptable;
   1307      1.1  christos 	rdata_atom_type *rdatas;
   1308      1.1  christos 	ssize_t rdata_num;
   1309      1.1  christos 	size_t oldpos;
   1310      1.1  christos 
   1311      1.1  christos 	if(ixfr_store->cancelled)
   1312      1.1  christos 		return;
   1313      1.1  christos 
   1314      1.1  christos 	/* The SOA data is stored with separate calls. And then appended
   1315      1.1  christos 	 * during the finish operation. We do not have to store it here
   1316      1.1  christos 	 * when called from difffile's IXFR processing with type SOA. */
   1317      1.1  christos 	if(type == TYPE_SOA)
   1318      1.1  christos 		return;
   1319      1.1  christos 	/* make space for these RRs we have now; basically once we
   1320      1.1  christos 	 * grow beyond the current allowed amount an older IXFR is deleted. */
   1321      1.1  christos 	zone_ixfr_make_space(ixfr_store->zone->ixfr, ixfr_store->zone,
   1322      1.1  christos 		ixfr_store->data, ixfr_store);
   1323      1.1  christos 	if(ixfr_store->cancelled)
   1324      1.1  christos 		return;
   1325      1.1  christos 
   1326      1.1  christos 	/* parse rdata */
   1327      1.1  christos 	oldpos = buffer_position(packet);
   1328      1.1  christos 	temptable = domain_table_create(temp_region);
   1329      1.1  christos 	rdata_num = rdata_wireformat_to_rdata_atoms(temp_region, temptable,
   1330      1.1  christos 		type, rrlen, packet, &rdatas);
   1331      1.1  christos 	buffer_set_position(packet, oldpos);
   1332      1.1  christos 	if(rdata_num == -1) {
   1333      1.1  christos 		log_msg(LOG_ERR, "ixfr_store addrr: cannot parse packet");
   1334      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1335      1.1  christos 		return;
   1336      1.1  christos 	}
   1337      1.1  christos 
   1338      1.1  christos 	if(!ixfr_putrr(dname, type, klass, ttl, rdatas, rdata_num,
   1339      1.1  christos 		rrs, rrs_len, rrs_capacity)) {
   1340      1.1  christos 		log_msg(LOG_ERR, "ixfr_store addrr: cannot allocate space");
   1341      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1342      1.1  christos 		return;
   1343      1.1  christos 	}
   1344      1.1  christos }
   1345      1.1  christos 
   1346      1.1  christos void ixfr_store_delrr(struct ixfr_store* ixfr_store, const struct dname* dname,
   1347      1.1  christos 	uint16_t type, uint16_t klass, uint32_t ttl, struct buffer* packet,
   1348      1.1  christos 	uint16_t rrlen, struct region* temp_region)
   1349      1.1  christos {
   1350      1.1  christos 	ixfr_store_putrr(ixfr_store, dname, type, klass, ttl, packet, rrlen,
   1351      1.1  christos 		temp_region, &ixfr_store->data->del,
   1352      1.1  christos 		&ixfr_store->data->del_len, &ixfr_store->del_capacity);
   1353      1.1  christos }
   1354      1.1  christos 
   1355      1.1  christos void ixfr_store_addrr(struct ixfr_store* ixfr_store, const struct dname* dname,
   1356      1.1  christos 	uint16_t type, uint16_t klass, uint32_t ttl, struct buffer* packet,
   1357      1.1  christos 	uint16_t rrlen, struct region* temp_region)
   1358      1.1  christos {
   1359      1.1  christos 	ixfr_store_putrr(ixfr_store, dname, type, klass, ttl, packet, rrlen,
   1360      1.1  christos 		temp_region, &ixfr_store->data->add,
   1361      1.1  christos 		&ixfr_store->data->add_len, &ixfr_store->add_capacity);
   1362      1.1  christos }
   1363      1.1  christos 
   1364      1.1  christos int ixfr_store_addrr_rdatas(struct ixfr_store* ixfr_store,
   1365      1.1  christos 	const struct dname* dname, uint16_t type, uint16_t klass,
   1366      1.1  christos 	uint32_t ttl, rdata_atom_type* rdatas, ssize_t rdata_num)
   1367      1.1  christos {
   1368      1.1  christos 	if(ixfr_store->cancelled)
   1369      1.1  christos 		return 1;
   1370      1.1  christos 	if(type == TYPE_SOA)
   1371      1.1  christos 		return 1;
   1372      1.1  christos 	return ixfr_putrr(dname, type, klass, ttl, rdatas, rdata_num,
   1373      1.1  christos 		&ixfr_store->data->add, &ixfr_store->data->add_len,
   1374      1.1  christos 		&ixfr_store->add_capacity);
   1375      1.1  christos }
   1376      1.1  christos 
   1377      1.1  christos int ixfr_store_add_newsoa_rdatas(struct ixfr_store* ixfr_store,
   1378      1.1  christos 	const struct dname* dname, uint16_t type, uint16_t klass,
   1379      1.1  christos 	uint32_t ttl, rdata_atom_type* rdatas, ssize_t rdata_num)
   1380      1.1  christos {
   1381      1.1  christos 	size_t capacity = 0;
   1382  1.1.1.2  christos 	uint32_t serial;
   1383      1.1  christos 	if(ixfr_store->cancelled)
   1384      1.1  christos 		return 1;
   1385  1.1.1.2  christos 	if(rdata_num < 2 || rdata_atom_size(rdatas[2]) < 4)
   1386  1.1.1.2  christos 		return 0;
   1387  1.1.1.2  christos 	memcpy(&serial, rdata_atom_data(rdatas[2]), sizeof(serial));
   1388  1.1.1.2  christos 	ixfr_store->data->newserial = ntohl(serial);
   1389      1.1  christos 	if(!ixfr_putrr(dname, type, klass, ttl, rdatas, rdata_num,
   1390      1.1  christos 		&ixfr_store->data->newsoa, &ixfr_store->data->newsoa_len,
   1391      1.1  christos 		&ixfr_store->add_capacity))
   1392      1.1  christos 		return 0;
   1393      1.1  christos 	ixfr_trim_capacity(&ixfr_store->data->newsoa,
   1394      1.1  christos 		&ixfr_store->data->newsoa_len, &capacity);
   1395      1.1  christos 	return 1;
   1396      1.1  christos }
   1397      1.1  christos 
   1398      1.1  christos /* store rr uncompressed */
   1399      1.1  christos int ixfr_storerr_uncompressed(uint8_t* dname, size_t dname_len, uint16_t type,
   1400      1.1  christos 	uint16_t klass, uint32_t ttl, uint8_t* rdata, size_t rdata_len,
   1401      1.1  christos 	uint8_t** rrs, size_t* rrs_len, size_t* rrs_capacity)
   1402      1.1  christos {
   1403      1.1  christos 	size_t sz;
   1404      1.1  christos 	uint8_t* sp;
   1405      1.1  christos 
   1406      1.1  christos 	/* find rdatalen */
   1407      1.1  christos 	sz = dname_len + 2 /*type*/ + 2 /*class*/ + 4 /*ttl*/ +
   1408      1.1  christos 		2 /*rdlen*/ + rdata_len;
   1409      1.1  christos 
   1410      1.1  christos 	/* store RR in IXFR data */
   1411      1.1  christos 	ixfr_rrs_make_space(rrs, rrs_len, rrs_capacity, sz);
   1412      1.1  christos 	if(!*rrs || *rrs_len + sz > *rrs_capacity) {
   1413      1.1  christos 		return 0;
   1414      1.1  christos 	}
   1415      1.1  christos 	/* copy data into add */
   1416      1.1  christos 	sp = *rrs + *rrs_len;
   1417      1.1  christos 	*rrs_len += sz;
   1418      1.1  christos 	memmove(sp, dname, dname_len);
   1419      1.1  christos 	sp += dname_len;
   1420      1.1  christos 	write_uint16(sp, type);
   1421      1.1  christos 	sp += 2;
   1422      1.1  christos 	write_uint16(sp, klass);
   1423      1.1  christos 	sp += 2;
   1424      1.1  christos 	write_uint32(sp, ttl);
   1425      1.1  christos 	sp += 4;
   1426      1.1  christos 	write_uint16(sp, rdata_len);
   1427      1.1  christos 	sp += 2;
   1428      1.1  christos 	memmove(sp, rdata, rdata_len);
   1429      1.1  christos 	return 1;
   1430      1.1  christos }
   1431      1.1  christos 
   1432      1.1  christos int ixfr_store_delrr_uncompressed(struct ixfr_store* ixfr_store,
   1433      1.1  christos 	uint8_t* dname, size_t dname_len, uint16_t type, uint16_t klass,
   1434      1.1  christos 	uint32_t ttl, uint8_t* rdata, size_t rdata_len)
   1435      1.1  christos {
   1436      1.1  christos 	if(ixfr_store->cancelled)
   1437      1.1  christos 		return 1;
   1438      1.1  christos 	if(type == TYPE_SOA)
   1439      1.1  christos 		return 1;
   1440      1.1  christos 	return ixfr_storerr_uncompressed(dname, dname_len, type, klass,
   1441      1.1  christos 		ttl, rdata, rdata_len, &ixfr_store->data->del,
   1442      1.1  christos 		&ixfr_store->data->del_len, &ixfr_store->del_capacity);
   1443      1.1  christos }
   1444      1.1  christos 
   1445  1.1.1.2  christos static size_t skip_dname(uint8_t* rdata, size_t rdata_len)
   1446  1.1.1.2  christos {
   1447  1.1.1.2  christos 	for (size_t index=0; index < rdata_len; ) {
   1448  1.1.1.2  christos 		uint8_t label_size = rdata[index];
   1449  1.1.1.2  christos 		if (label_size == 0) {
   1450  1.1.1.2  christos 			return index + 1;
   1451  1.1.1.2  christos 		} else if ((label_size & 0xc0) != 0) {
   1452  1.1.1.2  christos 			return (index + 1 < rdata_len) ? index + 2 : 0;
   1453  1.1.1.2  christos 		} else {
   1454  1.1.1.2  christos 			/* loop breaks if index exceeds rdata_len */
   1455  1.1.1.2  christos 			index += label_size + 1;
   1456  1.1.1.2  christos 		}
   1457  1.1.1.2  christos 	}
   1458  1.1.1.2  christos 
   1459  1.1.1.2  christos 	return 0;
   1460  1.1.1.2  christos }
   1461  1.1.1.2  christos 
   1462      1.1  christos int ixfr_store_oldsoa_uncompressed(struct ixfr_store* ixfr_store,
   1463      1.1  christos 	uint8_t* dname, size_t dname_len, uint16_t type, uint16_t klass,
   1464      1.1  christos 	uint32_t ttl, uint8_t* rdata, size_t rdata_len)
   1465      1.1  christos {
   1466      1.1  christos 	size_t capacity = 0;
   1467      1.1  christos 	if(ixfr_store->cancelled)
   1468      1.1  christos 		return 1;
   1469      1.1  christos 	if(!ixfr_storerr_uncompressed(dname, dname_len, type, klass,
   1470      1.1  christos 		ttl, rdata, rdata_len, &ixfr_store->data->oldsoa,
   1471      1.1  christos 		&ixfr_store->data->oldsoa_len, &capacity))
   1472      1.1  christos 		return 0;
   1473  1.1.1.2  christos 	{
   1474  1.1.1.2  christos 		uint32_t serial;
   1475  1.1.1.2  christos 		size_t index, count = 0;
   1476  1.1.1.2  christos 		if (!(count = skip_dname(rdata, rdata_len)))
   1477  1.1.1.2  christos 			return 0;
   1478  1.1.1.2  christos 		index = count;
   1479  1.1.1.2  christos 		if (!(count = skip_dname(rdata+index, rdata_len-index)))
   1480  1.1.1.2  christos 			return 0;
   1481  1.1.1.2  christos 		index += count;
   1482  1.1.1.2  christos 		if (rdata_len - index < 4)
   1483  1.1.1.2  christos 			return 0;
   1484  1.1.1.2  christos 		memcpy(&serial, rdata+index, sizeof(serial));
   1485  1.1.1.2  christos 		ixfr_store->data->oldserial = ntohl(serial);
   1486  1.1.1.2  christos 	}
   1487      1.1  christos 	ixfr_trim_capacity(&ixfr_store->data->oldsoa,
   1488      1.1  christos 		&ixfr_store->data->oldsoa_len, &capacity);
   1489      1.1  christos 	return 1;
   1490      1.1  christos }
   1491      1.1  christos 
   1492      1.1  christos int zone_is_ixfr_enabled(struct zone* zone)
   1493      1.1  christos {
   1494      1.1  christos 	return zone->opts->pattern->store_ixfr;
   1495      1.1  christos }
   1496      1.1  christos 
   1497      1.1  christos /* compare ixfr elements */
   1498      1.1  christos static int ixfrcompare(const void* x, const void* y)
   1499      1.1  christos {
   1500      1.1  christos 	uint32_t* serial_x = (uint32_t*)x;
   1501      1.1  christos 	uint32_t* serial_y = (uint32_t*)y;
   1502      1.1  christos 	if(*serial_x < *serial_y)
   1503      1.1  christos 		return -1;
   1504      1.1  christos 	if(*serial_x > *serial_y)
   1505      1.1  christos 		return 1;
   1506      1.1  christos 	return 0;
   1507      1.1  christos }
   1508      1.1  christos 
   1509      1.1  christos struct zone_ixfr* zone_ixfr_create(struct nsd* nsd)
   1510      1.1  christos {
   1511      1.1  christos 	struct zone_ixfr* ixfr = xalloc_zero(sizeof(struct zone_ixfr));
   1512      1.1  christos 	ixfr->data = rbtree_create(nsd->region, &ixfrcompare);
   1513      1.1  christos 	return ixfr;
   1514      1.1  christos }
   1515      1.1  christos 
   1516      1.1  christos /* traverse tree postorder */
   1517      1.1  christos static void ixfr_tree_del(struct rbnode* node)
   1518      1.1  christos {
   1519      1.1  christos 	if(node == NULL || node == RBTREE_NULL)
   1520      1.1  christos 		return;
   1521      1.1  christos 	ixfr_tree_del(node->left);
   1522      1.1  christos 	ixfr_tree_del(node->right);
   1523      1.1  christos 	ixfr_data_free((struct ixfr_data*)node);
   1524      1.1  christos }
   1525      1.1  christos 
   1526      1.1  christos /* clear the ixfr data elements */
   1527      1.1  christos static void zone_ixfr_clear(struct zone_ixfr* ixfr)
   1528      1.1  christos {
   1529      1.1  christos 	if(!ixfr)
   1530      1.1  christos 		return;
   1531      1.1  christos 	if(ixfr->data) {
   1532      1.1  christos 		ixfr_tree_del(ixfr->data->root);
   1533      1.1  christos 		ixfr->data->root = RBTREE_NULL;
   1534      1.1  christos 		ixfr->data->count = 0;
   1535      1.1  christos 	}
   1536      1.1  christos 	ixfr->total_size = 0;
   1537      1.1  christos 	ixfr->oldest_serial = 0;
   1538      1.1  christos 	ixfr->newest_serial = 0;
   1539      1.1  christos }
   1540      1.1  christos 
   1541      1.1  christos void zone_ixfr_free(struct zone_ixfr* ixfr)
   1542      1.1  christos {
   1543      1.1  christos 	if(!ixfr)
   1544      1.1  christos 		return;
   1545      1.1  christos 	if(ixfr->data) {
   1546      1.1  christos 		ixfr_tree_del(ixfr->data->root);
   1547      1.1  christos 		ixfr->data = NULL;
   1548      1.1  christos 	}
   1549      1.1  christos 	free(ixfr);
   1550      1.1  christos }
   1551      1.1  christos 
   1552      1.1  christos void ixfr_store_delixfrs(struct zone* zone)
   1553      1.1  christos {
   1554      1.1  christos 	if(!zone)
   1555      1.1  christos 		return;
   1556      1.1  christos 	zone_ixfr_clear(zone->ixfr);
   1557      1.1  christos }
   1558      1.1  christos 
   1559      1.1  christos /* remove the oldest data entry from the ixfr versions */
   1560      1.1  christos static void zone_ixfr_remove_oldest(struct zone_ixfr* ixfr)
   1561      1.1  christos {
   1562      1.1  christos 	if(ixfr->data->count > 0) {
   1563      1.1  christos 		struct ixfr_data* oldest = ixfr_data_first(ixfr);
   1564      1.1  christos 		if(ixfr->oldest_serial == oldest->oldserial) {
   1565      1.1  christos 			if(ixfr->data->count > 1) {
   1566      1.1  christos 				struct ixfr_data* next = ixfr_data_next(ixfr, oldest);
   1567      1.1  christos 				assert(next);
   1568      1.1  christos 				if(next)
   1569      1.1  christos 					ixfr->oldest_serial = next->oldserial;
   1570      1.1  christos 				else 	ixfr->oldest_serial = oldest->newserial;
   1571      1.1  christos 			} else {
   1572      1.1  christos 				ixfr->oldest_serial = 0;
   1573      1.1  christos 			}
   1574      1.1  christos 		}
   1575      1.1  christos 		if(ixfr->newest_serial == oldest->oldserial) {
   1576      1.1  christos 			ixfr->newest_serial = 0;
   1577      1.1  christos 		}
   1578      1.1  christos 		zone_ixfr_remove(ixfr, oldest);
   1579      1.1  christos 	}
   1580      1.1  christos }
   1581      1.1  christos 
   1582      1.1  christos void zone_ixfr_make_space(struct zone_ixfr* ixfr, struct zone* zone,
   1583      1.1  christos 	struct ixfr_data* data, struct ixfr_store* ixfr_store)
   1584      1.1  christos {
   1585      1.1  christos 	size_t addsize;
   1586      1.1  christos 	if(!ixfr || !data)
   1587      1.1  christos 		return;
   1588      1.1  christos 	if(zone->opts->pattern->ixfr_number == 0) {
   1589      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1590      1.1  christos 		return;
   1591      1.1  christos 	}
   1592      1.1  christos 
   1593      1.1  christos 	/* Check the number of IXFRs allowed for this zone, if too many,
   1594      1.1  christos 	 * shorten the number to make space for another one */
   1595      1.1  christos 	while(ixfr->data->count >= zone->opts->pattern->ixfr_number) {
   1596      1.1  christos 		zone_ixfr_remove_oldest(ixfr);
   1597      1.1  christos 	}
   1598      1.1  christos 
   1599      1.1  christos 	if(zone->opts->pattern->ixfr_size == 0) {
   1600      1.1  christos 		/* no size limits imposed */
   1601      1.1  christos 		return;
   1602      1.1  christos 	}
   1603      1.1  christos 
   1604      1.1  christos 	/* Check the size of the current added data element 'data', and
   1605      1.1  christos 	 * see if that overflows the maximum storage size for IXFRs for
   1606      1.1  christos 	 * this zone, and if so, delete the oldest IXFR to make space */
   1607      1.1  christos 	addsize = ixfr_data_size(data);
   1608      1.1  christos 	while(ixfr->data->count > 0 && ixfr->total_size + addsize >
   1609      1.1  christos 		zone->opts->pattern->ixfr_size) {
   1610      1.1  christos 		zone_ixfr_remove_oldest(ixfr);
   1611      1.1  christos 	}
   1612      1.1  christos 
   1613      1.1  christos 	/* if deleting the oldest elements does not work, then this
   1614      1.1  christos 	 * IXFR is too big to store and we cancel it */
   1615      1.1  christos 	if(ixfr->data->count == 0 && ixfr->total_size + addsize >
   1616      1.1  christos 		zone->opts->pattern->ixfr_size) {
   1617      1.1  christos 		ixfr_store_cancel(ixfr_store);
   1618      1.1  christos 		return;
   1619      1.1  christos 	}
   1620      1.1  christos }
   1621      1.1  christos 
   1622      1.1  christos void zone_ixfr_remove(struct zone_ixfr* ixfr, struct ixfr_data* data)
   1623      1.1  christos {
   1624      1.1  christos 	rbtree_delete(ixfr->data, data->node.key);
   1625      1.1  christos 	ixfr->total_size -= ixfr_data_size(data);
   1626      1.1  christos 	ixfr_data_free(data);
   1627      1.1  christos }
   1628      1.1  christos 
   1629      1.1  christos void zone_ixfr_add(struct zone_ixfr* ixfr, struct ixfr_data* data, int isnew)
   1630      1.1  christos {
   1631      1.1  christos 	memset(&data->node, 0, sizeof(data->node));
   1632      1.1  christos 	if(ixfr->data->count == 0) {
   1633      1.1  christos 		ixfr->oldest_serial = data->oldserial;
   1634      1.1  christos 		ixfr->newest_serial = data->oldserial;
   1635      1.1  christos 	} else if(isnew) {
   1636      1.1  christos 		/* newest entry is last there is */
   1637      1.1  christos 		ixfr->newest_serial = data->oldserial;
   1638      1.1  christos 	} else {
   1639      1.1  christos 		/* added older entry, before the others */
   1640      1.1  christos 		ixfr->oldest_serial = data->oldserial;
   1641      1.1  christos 	}
   1642      1.1  christos 	data->node.key = &data->oldserial;
   1643      1.1  christos 	rbtree_insert(ixfr->data, &data->node);
   1644      1.1  christos 	ixfr->total_size += ixfr_data_size(data);
   1645      1.1  christos }
   1646      1.1  christos 
   1647      1.1  christos struct ixfr_data* zone_ixfr_find_serial(struct zone_ixfr* ixfr,
   1648      1.1  christos 	uint32_t qserial)
   1649      1.1  christos {
   1650      1.1  christos 	struct ixfr_data* data;
   1651      1.1  christos 	if(!ixfr)
   1652      1.1  christos 		return NULL;
   1653      1.1  christos 	if(!ixfr->data)
   1654      1.1  christos 		return NULL;
   1655      1.1  christos 	data = (struct ixfr_data*)rbtree_search(ixfr->data, &qserial);
   1656      1.1  christos 	if(data) {
   1657      1.1  christos 		assert(data->oldserial == qserial);
   1658      1.1  christos 		return data;
   1659      1.1  christos 	}
   1660      1.1  christos 	/* not found */
   1661      1.1  christos 	return NULL;
   1662      1.1  christos }
   1663      1.1  christos 
   1664      1.1  christos /* calculate the number of files we want */
   1665      1.1  christos static int ixfr_target_number_files(struct zone* zone)
   1666      1.1  christos {
   1667      1.1  christos 	int dest_num_files;
   1668      1.1  christos 	if(!zone->ixfr || !zone->ixfr->data)
   1669      1.1  christos 		return 0;
   1670      1.1  christos 	if(!zone_is_ixfr_enabled(zone))
   1671      1.1  christos 		return 0;
   1672      1.1  christos 	/* if we store ixfr, it is the configured number of files */
   1673      1.1  christos 	dest_num_files = (int)zone->opts->pattern->ixfr_number;
   1674      1.1  christos 	/* but if the number of available transfers is smaller, store less */
   1675      1.1  christos 	if(dest_num_files > (int)zone->ixfr->data->count)
   1676      1.1  christos 		dest_num_files = (int)zone->ixfr->data->count;
   1677      1.1  christos 	return dest_num_files;
   1678      1.1  christos }
   1679      1.1  christos 
   1680      1.1  christos /* create ixfrfile name in buffer for file_num. The num is 1 .. number. */
   1681      1.1  christos static void make_ixfr_name(char* buf, size_t len, const char* zfile,
   1682      1.1  christos 	int file_num)
   1683      1.1  christos {
   1684      1.1  christos 	if(file_num == 1)
   1685      1.1  christos 		snprintf(buf, len, "%s.ixfr", zfile);
   1686      1.1  christos 	else snprintf(buf, len, "%s.ixfr.%d", zfile, file_num);
   1687      1.1  christos }
   1688      1.1  christos 
   1689      1.1  christos /* create temp ixfrfile name in buffer for file_num. The num is 1 .. number. */
   1690      1.1  christos static void make_ixfr_name_temp(char* buf, size_t len, const char* zfile,
   1691      1.1  christos 	int file_num, int temp)
   1692      1.1  christos {
   1693      1.1  christos 	if(file_num == 1)
   1694      1.1  christos 		snprintf(buf, len, "%s.ixfr%s", zfile, (temp?".temp":""));
   1695      1.1  christos 	else snprintf(buf, len, "%s.ixfr.%d%s", zfile, file_num,
   1696      1.1  christos 		(temp?".temp":""));
   1697      1.1  christos }
   1698      1.1  christos 
   1699      1.1  christos /* see if ixfr file exists */
   1700      1.1  christos static int ixfr_file_exists_ctmp(const char* zfile, int file_num, int temp)
   1701      1.1  christos {
   1702      1.1  christos 	struct stat statbuf;
   1703      1.1  christos 	char ixfrfile[1024+24];
   1704      1.1  christos 	make_ixfr_name_temp(ixfrfile, sizeof(ixfrfile), zfile, file_num, temp);
   1705      1.1  christos 	memset(&statbuf, 0, sizeof(statbuf));
   1706      1.1  christos 	if(stat(ixfrfile, &statbuf) < 0) {
   1707      1.1  christos 		if(errno == ENOENT)
   1708      1.1  christos 			return 0;
   1709      1.1  christos 		/* file is not usable */
   1710      1.1  christos 		return 0;
   1711      1.1  christos 	}
   1712      1.1  christos 	return 1;
   1713      1.1  christos }
   1714      1.1  christos 
   1715      1.1  christos int ixfr_file_exists(const char* zfile, int file_num)
   1716      1.1  christos {
   1717      1.1  christos 	return ixfr_file_exists_ctmp(zfile, file_num, 0);
   1718      1.1  christos }
   1719      1.1  christos 
   1720      1.1  christos /* see if ixfr file exists */
   1721      1.1  christos static int ixfr_file_exists_temp(const char* zfile, int file_num)
   1722      1.1  christos {
   1723      1.1  christos 	return ixfr_file_exists_ctmp(zfile, file_num, 1);
   1724      1.1  christos }
   1725      1.1  christos 
   1726      1.1  christos /* unlink an ixfr file */
   1727      1.1  christos static int ixfr_unlink_it_ctmp(const char* zname, const char* zfile,
   1728      1.1  christos 	int file_num, int silent_enoent, int temp)
   1729      1.1  christos {
   1730      1.1  christos 	char ixfrfile[1024+24];
   1731      1.1  christos 	make_ixfr_name_temp(ixfrfile, sizeof(ixfrfile), zfile, file_num, temp);
   1732      1.1  christos 	VERBOSITY(3, (LOG_INFO, "delete zone %s IXFR data file %s",
   1733      1.1  christos 		zname, ixfrfile));
   1734      1.1  christos 	if(unlink(ixfrfile) < 0) {
   1735      1.1  christos 		if(silent_enoent && errno == ENOENT)
   1736      1.1  christos 			return 0;
   1737      1.1  christos 		log_msg(LOG_ERR, "error to delete file %s: %s", ixfrfile,
   1738      1.1  christos 			strerror(errno));
   1739      1.1  christos 		return 0;
   1740      1.1  christos 	}
   1741      1.1  christos 	return 1;
   1742      1.1  christos }
   1743      1.1  christos 
   1744      1.1  christos int ixfr_unlink_it(const char* zname, const char* zfile, int file_num,
   1745      1.1  christos 	int silent_enoent)
   1746      1.1  christos {
   1747      1.1  christos 	return ixfr_unlink_it_ctmp(zname, zfile, file_num, silent_enoent, 0);
   1748      1.1  christos }
   1749      1.1  christos 
   1750      1.1  christos /* unlink an ixfr file */
   1751      1.1  christos static int ixfr_unlink_it_temp(const char* zname, const char* zfile,
   1752      1.1  christos 	int file_num, int silent_enoent)
   1753      1.1  christos {
   1754      1.1  christos 	return ixfr_unlink_it_ctmp(zname, zfile, file_num, silent_enoent, 1);
   1755      1.1  christos }
   1756      1.1  christos 
   1757      1.1  christos /* read ixfr file header */
   1758      1.1  christos int ixfr_read_file_header(const char* zname, const char* zfile,
   1759      1.1  christos 	int file_num, uint32_t* oldserial, uint32_t* newserial,
   1760      1.1  christos 	size_t* data_size, int enoent_is_err)
   1761      1.1  christos {
   1762      1.1  christos 	char ixfrfile[1024+24];
   1763      1.1  christos 	char buf[1024];
   1764      1.1  christos 	FILE* in;
   1765      1.1  christos 	int num_lines = 0, got_old = 0, got_new = 0, got_datasize = 0;
   1766      1.1  christos 	make_ixfr_name(ixfrfile, sizeof(ixfrfile), zfile, file_num);
   1767      1.1  christos 	in = fopen(ixfrfile, "r");
   1768      1.1  christos 	if(!in) {
   1769      1.1  christos 		if((errno == ENOENT && enoent_is_err) || (errno != ENOENT))
   1770      1.1  christos 			log_msg(LOG_ERR, "could not open %s: %s", ixfrfile,
   1771      1.1  christos 				strerror(errno));
   1772      1.1  christos 		return 0;
   1773      1.1  christos 	}
   1774      1.1  christos 	/* read about 10 lines, this is where the header is */
   1775      1.1  christos 	while(!(got_old && got_new && got_datasize) && num_lines < 10) {
   1776      1.1  christos 		buf[0]=0;
   1777      1.1  christos 		buf[sizeof(buf)-1]=0;
   1778      1.1  christos 		if(!fgets(buf, sizeof(buf), in)) {
   1779      1.1  christos 			log_msg(LOG_ERR, "could not read %s: %s", ixfrfile,
   1780      1.1  christos 				strerror(errno));
   1781      1.1  christos 			fclose(in);
   1782      1.1  christos 			return 0;
   1783      1.1  christos 		}
   1784      1.1  christos 		num_lines++;
   1785      1.1  christos 		if(buf[0]!=0 && buf[strlen(buf)-1]=='\n')
   1786      1.1  christos 			buf[strlen(buf)-1]=0;
   1787      1.1  christos 		if(strncmp(buf, "; zone ", 7) == 0) {
   1788      1.1  christos 			if(strcmp(buf+7, zname) != 0) {
   1789      1.1  christos 				log_msg(LOG_ERR, "file has wrong zone, expected zone %s, but found %s in file %s",
   1790      1.1  christos 					zname, buf+7, ixfrfile);
   1791      1.1  christos 				fclose(in);
   1792      1.1  christos 				return 0;
   1793      1.1  christos 			}
   1794      1.1  christos 		} else if(strncmp(buf, "; from_serial ", 14) == 0) {
   1795      1.1  christos 			*oldserial = atoi(buf+14);
   1796      1.1  christos 			got_old = 1;
   1797      1.1  christos 		} else if(strncmp(buf, "; to_serial ", 12) == 0) {
   1798      1.1  christos 			*newserial = atoi(buf+12);
   1799      1.1  christos 			got_new = 1;
   1800      1.1  christos 		} else if(strncmp(buf, "; data_size ", 12) == 0) {
   1801      1.1  christos 			*data_size = (size_t)atoi(buf+12);
   1802      1.1  christos 			got_datasize = 1;
   1803      1.1  christos 		}
   1804      1.1  christos 	}
   1805      1.1  christos 	fclose(in);
   1806      1.1  christos 	if(!got_old)
   1807      1.1  christos 		return 0;
   1808      1.1  christos 	if(!got_new)
   1809      1.1  christos 		return 0;
   1810      1.1  christos 	if(!got_datasize)
   1811      1.1  christos 		return 0;
   1812      1.1  christos 	return 1;
   1813      1.1  christos }
   1814      1.1  christos 
   1815      1.1  christos /* delete rest ixfr files, that are after the current item */
   1816      1.1  christos static void ixfr_delete_rest_files(struct zone* zone, struct ixfr_data* from,
   1817      1.1  christos 	const char* zfile, int temp)
   1818      1.1  christos {
   1819      1.1  christos 	size_t prevcount = 0;
   1820      1.1  christos 	struct ixfr_data* data = from;
   1821      1.1  christos 	while(data) {
   1822      1.1  christos 		if(data->file_num != 0) {
   1823      1.1  christos 			(void)ixfr_unlink_it_ctmp(zone->opts->name, zfile,
   1824      1.1  christos 				data->file_num, 0, temp);
   1825      1.1  christos 			data->file_num = 0;
   1826      1.1  christos 		}
   1827      1.1  christos 		data = ixfr_data_prev(zone->ixfr, data, &prevcount);
   1828      1.1  christos 	}
   1829      1.1  christos }
   1830      1.1  christos 
   1831      1.1  christos void ixfr_delete_superfluous_files(struct zone* zone, const char* zfile,
   1832      1.1  christos 	int dest_num_files)
   1833      1.1  christos {
   1834      1.1  christos 	int i = dest_num_files + 1;
   1835      1.1  christos 	if(!ixfr_file_exists(zfile, i))
   1836      1.1  christos 		return;
   1837      1.1  christos 	while(ixfr_unlink_it(zone->opts->name, zfile, i, 1)) {
   1838      1.1  christos 		i++;
   1839      1.1  christos 	}
   1840      1.1  christos }
   1841      1.1  christos 
   1842      1.1  christos int ixfr_rename_it(const char* zname, const char* zfile, int oldnum,
   1843      1.1  christos 	int oldtemp, int newnum, int newtemp)
   1844      1.1  christos {
   1845      1.1  christos 	char ixfrfile_old[1024+24];
   1846      1.1  christos 	char ixfrfile_new[1024+24];
   1847      1.1  christos 	make_ixfr_name_temp(ixfrfile_old, sizeof(ixfrfile_old), zfile, oldnum,
   1848      1.1  christos 		oldtemp);
   1849      1.1  christos 	make_ixfr_name_temp(ixfrfile_new, sizeof(ixfrfile_new), zfile, newnum,
   1850      1.1  christos 		newtemp);
   1851      1.1  christos 	VERBOSITY(3, (LOG_INFO, "rename zone %s IXFR data file %s to %s",
   1852      1.1  christos 		zname, ixfrfile_old, ixfrfile_new));
   1853      1.1  christos 	if(rename(ixfrfile_old, ixfrfile_new) < 0) {
   1854      1.1  christos 		log_msg(LOG_ERR, "error to rename file %s: %s", ixfrfile_old,
   1855      1.1  christos 			strerror(errno));
   1856      1.1  christos 		return 0;
   1857      1.1  christos 	}
   1858      1.1  christos 	return 1;
   1859      1.1  christos }
   1860      1.1  christos 
   1861      1.1  christos /* delete if we have too many items in memory */
   1862      1.1  christos static void ixfr_delete_memory_items(struct zone* zone, int dest_num_files)
   1863      1.1  christos {
   1864      1.1  christos 	if(!zone->ixfr || !zone->ixfr->data)
   1865      1.1  christos 		return;
   1866      1.1  christos 	if(dest_num_files == (int)zone->ixfr->data->count)
   1867      1.1  christos 		return;
   1868      1.1  christos 	if(dest_num_files > (int)zone->ixfr->data->count) {
   1869      1.1  christos 		/* impossible, dest_num_files should be smaller */
   1870      1.1  christos 		return;
   1871      1.1  christos 	}
   1872      1.1  christos 
   1873      1.1  christos 	/* delete oldest ixfr, until we have dest_num_files entries */
   1874      1.1  christos 	while(dest_num_files < (int)zone->ixfr->data->count) {
   1875      1.1  christos 		zone_ixfr_remove_oldest(zone->ixfr);
   1876      1.1  christos 	}
   1877      1.1  christos }
   1878      1.1  christos 
   1879      1.1  christos /* rename the ixfr files that need to change name */
   1880      1.1  christos static int ixfr_rename_files(struct zone* zone, const char* zfile,
   1881      1.1  christos 	int dest_num_files)
   1882      1.1  christos {
   1883      1.1  christos 	struct ixfr_data* data, *startspot = NULL;
   1884      1.1  christos 	size_t prevcount = 0;
   1885      1.1  christos 	int destnum;
   1886      1.1  christos 	if(!zone->ixfr || !zone->ixfr->data)
   1887      1.1  christos 		return 1;
   1888      1.1  christos 
   1889      1.1  christos 	/* the oldest file is at the largest number */
   1890      1.1  christos 	data = ixfr_data_first(zone->ixfr);
   1891      1.1  christos 	destnum = dest_num_files;
   1892      1.1  christos 	if(!data)
   1893      1.1  christos 		return 1; /* nothing to do */
   1894      1.1  christos 	if(data->file_num == destnum)
   1895      1.1  christos 		return 1; /* nothing to do for rename */
   1896      1.1  christos 
   1897      1.1  christos 	/* rename the files to temporary files, because otherwise the
   1898      1.1  christos 	 * items would overwrite each other when the list touches itself.
   1899      1.1  christos 	 * On fail, the temporary files are removed and we end up with
   1900      1.1  christos 	 * the newly written data plus the remaining files, in order.
   1901      1.1  christos 	 * Thus, start the temporary rename at the oldest, then rename
   1902      1.1  christos 	 * to the final names starting from the newest. */
   1903      1.1  christos 	while(data && data->file_num != 0) {
   1904      1.1  christos 		/* if existing file at temporary name, delete that */
   1905      1.1  christos 		if(ixfr_file_exists_temp(zfile, data->file_num)) {
   1906      1.1  christos 			(void)ixfr_unlink_it_temp(zone->opts->name, zfile,
   1907      1.1  christos 				data->file_num, 0);
   1908      1.1  christos 		}
   1909      1.1  christos 
   1910      1.1  christos 		/* rename to temporary name */
   1911      1.1  christos 		if(!ixfr_rename_it(zone->opts->name, zfile, data->file_num, 0,
   1912      1.1  christos 			data->file_num, 1)) {
   1913      1.1  christos 			/* failure, we cannot store files */
   1914      1.1  christos 			/* delete the renamed files */
   1915      1.1  christos 			ixfr_delete_rest_files(zone, data, zfile, 1);
   1916      1.1  christos 			return 0;
   1917      1.1  christos 		}
   1918      1.1  christos 
   1919      1.1  christos 		/* the next cycle should start at the newest file that
   1920      1.1  christos 		 * has been renamed to a temporary name */
   1921      1.1  christos 		startspot = data;
   1922      1.1  christos 		data = ixfr_data_next(zone->ixfr, data);
   1923      1.1  christos 		destnum--;
   1924      1.1  christos 	}
   1925      1.1  christos 
   1926      1.1  christos 	/* rename the files to their final name position */
   1927      1.1  christos 	data = startspot;
   1928      1.1  christos 	while(data && data->file_num != 0) {
   1929      1.1  christos 		destnum++;
   1930      1.1  christos 
   1931      1.1  christos 		/* if there is an existing file, delete it */
   1932      1.1  christos 		if(ixfr_file_exists(zfile, destnum)) {
   1933      1.1  christos 			(void)ixfr_unlink_it(zone->opts->name, zfile,
   1934      1.1  christos 				destnum, 0);
   1935      1.1  christos 		}
   1936      1.1  christos 
   1937      1.1  christos 		if(!ixfr_rename_it(zone->opts->name, zfile, data->file_num, 1, destnum, 0)) {
   1938      1.1  christos 			/* failure, we cannot store files */
   1939      1.1  christos 			ixfr_delete_rest_files(zone, data, zfile, 1);
   1940      1.1  christos 			/* delete the previously renamed files, so in
   1941      1.1  christos 			 * memory stays as is, on disk we have the current
   1942      1.1  christos 			 * item (and newer transfers) okay. */
   1943      1.1  christos 			return 0;
   1944      1.1  christos 		}
   1945      1.1  christos 		data->file_num = destnum;
   1946      1.1  christos 
   1947      1.1  christos 		data = ixfr_data_prev(zone->ixfr, data, &prevcount);
   1948      1.1  christos 	}
   1949      1.1  christos 	return 1;
   1950      1.1  christos }
   1951      1.1  christos 
   1952      1.1  christos /* write the ixfr data file header */
   1953      1.1  christos static int ixfr_write_file_header(struct zone* zone, struct ixfr_data* data,
   1954      1.1  christos 	FILE* out)
   1955      1.1  christos {
   1956      1.1  christos 	if(!fprintf(out, "; IXFR data file\n"))
   1957      1.1  christos 		return 0;
   1958      1.1  christos 	if(!fprintf(out, "; zone %s\n", zone->opts->name))
   1959      1.1  christos 		return 0;
   1960      1.1  christos 	if(!fprintf(out, "; from_serial %u\n", (unsigned)data->oldserial))
   1961      1.1  christos 		return 0;
   1962      1.1  christos 	if(!fprintf(out, "; to_serial %u\n", (unsigned)data->newserial))
   1963      1.1  christos 		return 0;
   1964      1.1  christos 	if(!fprintf(out, "; data_size %u\n", (unsigned)ixfr_data_size(data)))
   1965      1.1  christos 		return 0;
   1966      1.1  christos 	if(data->log_str) {
   1967      1.1  christos 		if(!fprintf(out, "; %s\n", data->log_str))
   1968      1.1  christos 			return 0;
   1969      1.1  christos 	}
   1970      1.1  christos 	return 1;
   1971      1.1  christos }
   1972      1.1  christos 
   1973      1.1  christos /* print rdata on one line */
   1974      1.1  christos static int
   1975      1.1  christos oneline_print_rdata(buffer_type *output, rrtype_descriptor_type *descriptor,
   1976      1.1  christos 	rr_type* record)
   1977      1.1  christos {
   1978      1.1  christos 	size_t i;
   1979      1.1  christos 	size_t saved_position = buffer_position(output);
   1980      1.1  christos 
   1981      1.1  christos 	for (i = 0; i < record->rdata_count; ++i) {
   1982      1.1  christos 		if (i == 0) {
   1983      1.1  christos 			buffer_printf(output, "\t");
   1984      1.1  christos 		} else {
   1985      1.1  christos 			buffer_printf(output, " ");
   1986      1.1  christos 		}
   1987      1.1  christos 		if (!rdata_atom_to_string(
   1988      1.1  christos 			    output,
   1989      1.1  christos 			    (rdata_zoneformat_type) descriptor->zoneformat[i],
   1990      1.1  christos 			    record->rdatas[i], record))
   1991      1.1  christos 		{
   1992      1.1  christos 			buffer_set_position(output, saved_position);
   1993      1.1  christos 			return 0;
   1994      1.1  christos 		}
   1995      1.1  christos 	}
   1996      1.1  christos 
   1997      1.1  christos 	return 1;
   1998      1.1  christos }
   1999      1.1  christos 
   2000      1.1  christos /* parse wireformat RR into a struct RR in temp region */
   2001      1.1  christos static int parse_wirerr_into_temp(struct zone* zone, char* fname,
   2002      1.1  christos 	struct region* temp, uint8_t* buf, size_t len,
   2003      1.1  christos 	const dname_type** dname, struct rr* rr)
   2004      1.1  christos {
   2005      1.1  christos 	size_t bufpos = 0;
   2006      1.1  christos 	uint16_t rdlen;
   2007      1.1  christos 	ssize_t rdata_num;
   2008      1.1  christos 	buffer_type packet;
   2009      1.1  christos 	domain_table_type* owners;
   2010      1.1  christos 	owners = domain_table_create(temp);
   2011      1.1  christos 	memset(rr, 0, sizeof(*rr));
   2012      1.1  christos 	*dname = dname_make(temp, buf, 1);
   2013      1.1  christos 	if(!*dname) {
   2014      1.1  christos 		log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: failed to parse dname", zone->opts->name, fname);
   2015      1.1  christos 		return 0;
   2016      1.1  christos 	}
   2017      1.1  christos 	bufpos = (*dname)->name_size;
   2018      1.1  christos 	if(bufpos+10 > len) {
   2019      1.1  christos 		log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: buffer too short", zone->opts->name, fname);
   2020      1.1  christos 		return 0;
   2021      1.1  christos 	}
   2022      1.1  christos 	rr->type = read_uint16(buf+bufpos);
   2023      1.1  christos 	bufpos += 2;
   2024      1.1  christos 	rr->klass = read_uint16(buf+bufpos);
   2025      1.1  christos 	bufpos += 2;
   2026      1.1  christos 	rr->ttl = read_uint32(buf+bufpos);
   2027      1.1  christos 	bufpos += 4;
   2028      1.1  christos 	rdlen = read_uint16(buf+bufpos);
   2029      1.1  christos 	bufpos += 2;
   2030      1.1  christos 	if(bufpos + rdlen > len) {
   2031      1.1  christos 		log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: buffer too short for rdatalen", zone->opts->name, fname);
   2032      1.1  christos 		return 0;
   2033      1.1  christos 	}
   2034      1.1  christos 	buffer_create_from(&packet, buf+bufpos, rdlen);
   2035      1.1  christos 	rdata_num = rdata_wireformat_to_rdata_atoms(
   2036      1.1  christos 		temp, owners, rr->type, rdlen, &packet, &rr->rdatas);
   2037      1.1  christos 	if(rdata_num == -1) {
   2038      1.1  christos 		log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: cannot parse rdata", zone->opts->name, fname);
   2039      1.1  christos 		return 0;
   2040      1.1  christos 	}
   2041      1.1  christos 	rr->rdata_count = rdata_num;
   2042      1.1  christos 	return 1;
   2043      1.1  christos }
   2044      1.1  christos 
   2045      1.1  christos /* print RR on one line in output buffer. caller must zeroterminate, if
   2046      1.1  christos  * that is needed. */
   2047      1.1  christos static int print_rr_oneline(struct buffer* rr_buffer, const dname_type* dname,
   2048      1.1  christos 	struct rr* rr)
   2049      1.1  christos {
   2050      1.1  christos 	rrtype_descriptor_type *descriptor;
   2051      1.1  christos 	descriptor = rrtype_descriptor_by_type(rr->type);
   2052      1.1  christos 	buffer_printf(rr_buffer, "%s", dname_to_string(dname, NULL));
   2053      1.1  christos 	buffer_printf(rr_buffer, "\t%lu\t%s\t%s", (unsigned long)rr->ttl,
   2054      1.1  christos 		rrclass_to_string(rr->klass), rrtype_to_string(rr->type));
   2055      1.1  christos 	if(!oneline_print_rdata(rr_buffer, descriptor, rr)) {
   2056      1.1  christos 		if(!rdata_atoms_to_unknown_string(rr_buffer,
   2057      1.1  christos 			descriptor, rr->rdata_count, rr->rdatas)) {
   2058      1.1  christos 			return 0;
   2059      1.1  christos 		}
   2060      1.1  christos 	}
   2061      1.1  christos 	return 1;
   2062      1.1  christos }
   2063      1.1  christos 
   2064      1.1  christos /* write one RR to file, on one line */
   2065      1.1  christos static int ixfr_write_rr(struct zone* zone, FILE* out, char* fname,
   2066      1.1  christos 	uint8_t* buf, size_t len, struct region* temp, buffer_type* rr_buffer)
   2067      1.1  christos {
   2068      1.1  christos 	const dname_type* dname;
   2069      1.1  christos 	struct rr rr;
   2070      1.1  christos 
   2071      1.1  christos 	if(!parse_wirerr_into_temp(zone, fname, temp, buf, len, &dname, &rr)) {
   2072      1.1  christos 		region_free_all(temp);
   2073      1.1  christos 		return 0;
   2074      1.1  christos 	}
   2075      1.1  christos 
   2076      1.1  christos 	buffer_clear(rr_buffer);
   2077      1.1  christos 	if(!print_rr_oneline(rr_buffer, dname, &rr)) {
   2078      1.1  christos 		log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: cannot spool RR string into buffer", zone->opts->name, fname);
   2079      1.1  christos 		region_free_all(temp);
   2080      1.1  christos 		return 0;
   2081      1.1  christos 	}
   2082      1.1  christos 	buffer_write_u8(rr_buffer, 0);
   2083      1.1  christos 	buffer_flip(rr_buffer);
   2084      1.1  christos 
   2085      1.1  christos 	if(!fprintf(out, "%s\n", buffer_begin(rr_buffer))) {
   2086      1.1  christos 		log_msg(LOG_ERR, "failed to write zone %s IXFR data %s: cannot print RR string to file: %s", zone->opts->name, fname, strerror(errno));
   2087      1.1  christos 		region_free_all(temp);
   2088      1.1  christos 		return 0;
   2089      1.1  christos 	}
   2090      1.1  christos 	region_free_all(temp);
   2091      1.1  christos 	return 1;
   2092      1.1  christos }
   2093      1.1  christos 
   2094      1.1  christos /* write ixfr RRs to file */
   2095      1.1  christos static int ixfr_write_rrs(struct zone* zone, FILE* out, char* fname,
   2096      1.1  christos 	uint8_t* buf, size_t len, struct region* temp, buffer_type* rr_buffer)
   2097      1.1  christos {
   2098      1.1  christos 	size_t current = 0;
   2099      1.1  christos 	if(!buf || len == 0)
   2100      1.1  christos 		return 1;
   2101      1.1  christos 	while(current < len) {
   2102      1.1  christos 		size_t rrlen = count_rr_length(buf, len, current);
   2103      1.1  christos 		if(rrlen == 0)
   2104      1.1  christos 			return 0;
   2105      1.1  christos 		if(current + rrlen > len)
   2106      1.1  christos 			return 0;
   2107      1.1  christos 		if(!ixfr_write_rr(zone, out, fname, buf+current, rrlen,
   2108      1.1  christos 			temp, rr_buffer))
   2109      1.1  christos 			return 0;
   2110      1.1  christos 		current += rrlen;
   2111      1.1  christos 	}
   2112      1.1  christos 	return 1;
   2113      1.1  christos }
   2114      1.1  christos 
   2115      1.1  christos /* write the ixfr data file data */
   2116      1.1  christos static int ixfr_write_file_data(struct zone* zone, struct ixfr_data* data,
   2117      1.1  christos 	FILE* out, char* fname)
   2118      1.1  christos {
   2119      1.1  christos 	struct region* temp, *rrtemp;
   2120      1.1  christos 	buffer_type* rr_buffer;
   2121      1.1  christos 	temp = region_create(xalloc, free);
   2122      1.1  christos 	rrtemp = region_create(xalloc, free);
   2123      1.1  christos 	rr_buffer = buffer_create(rrtemp, MAX_RDLENGTH);
   2124      1.1  christos 
   2125      1.1  christos 	if(!ixfr_write_rrs(zone, out, fname, data->newsoa, data->newsoa_len,
   2126      1.1  christos 		temp, rr_buffer)) {
   2127      1.1  christos 		region_destroy(temp);
   2128      1.1  christos 		region_destroy(rrtemp);
   2129      1.1  christos 		return 0;
   2130      1.1  christos 	}
   2131      1.1  christos 	if(!ixfr_write_rrs(zone, out, fname, data->oldsoa, data->oldsoa_len,
   2132      1.1  christos 		temp, rr_buffer)) {
   2133      1.1  christos 		region_destroy(temp);
   2134      1.1  christos 		region_destroy(rrtemp);
   2135      1.1  christos 		return 0;
   2136      1.1  christos 	}
   2137      1.1  christos 	if(!ixfr_write_rrs(zone, out, fname, data->del, data->del_len,
   2138      1.1  christos 		temp, rr_buffer)) {
   2139      1.1  christos 		region_destroy(temp);
   2140      1.1  christos 		region_destroy(rrtemp);
   2141      1.1  christos 		return 0;
   2142      1.1  christos 	}
   2143      1.1  christos 	if(!ixfr_write_rrs(zone, out, fname, data->add, data->add_len,
   2144      1.1  christos 		temp, rr_buffer)) {
   2145      1.1  christos 		region_destroy(temp);
   2146      1.1  christos 		region_destroy(rrtemp);
   2147      1.1  christos 		return 0;
   2148      1.1  christos 	}
   2149      1.1  christos 	region_destroy(temp);
   2150      1.1  christos 	region_destroy(rrtemp);
   2151      1.1  christos 	return 1;
   2152      1.1  christos }
   2153      1.1  christos 
   2154      1.1  christos int ixfr_write_file(struct zone* zone, struct ixfr_data* data,
   2155      1.1  christos 	const char* zfile, int file_num)
   2156      1.1  christos {
   2157      1.1  christos 	char ixfrfile[1024+24];
   2158      1.1  christos 	FILE* out;
   2159      1.1  christos 	make_ixfr_name(ixfrfile, sizeof(ixfrfile), zfile, file_num);
   2160      1.1  christos 	VERBOSITY(1, (LOG_INFO, "writing zone %s IXFR data to file %s",
   2161      1.1  christos 		zone->opts->name, ixfrfile));
   2162      1.1  christos 	out = fopen(ixfrfile, "w");
   2163      1.1  christos 	if(!out) {
   2164      1.1  christos 		log_msg(LOG_ERR, "could not open for writing zone %s IXFR file %s: %s",
   2165      1.1  christos 			zone->opts->name, ixfrfile, strerror(errno));
   2166      1.1  christos 		return 0;
   2167      1.1  christos 	}
   2168      1.1  christos 
   2169      1.1  christos 	if(!ixfr_write_file_header(zone, data, out)) {
   2170      1.1  christos 		log_msg(LOG_ERR, "could not write file header for zone %s IXFR file %s: %s",
   2171      1.1  christos 			zone->opts->name, ixfrfile, strerror(errno));
   2172      1.1  christos 		fclose(out);
   2173      1.1  christos 		return 0;
   2174      1.1  christos 	}
   2175      1.1  christos 	if(!ixfr_write_file_data(zone, data, out, ixfrfile)) {
   2176      1.1  christos 		fclose(out);
   2177      1.1  christos 		return 0;
   2178      1.1  christos 	}
   2179      1.1  christos 
   2180      1.1  christos 	fclose(out);
   2181      1.1  christos 	data->file_num = file_num;
   2182      1.1  christos 	return 1;
   2183      1.1  christos }
   2184      1.1  christos 
   2185      1.1  christos /* write the ixfr files that need to be stored on disk */
   2186      1.1  christos static void ixfr_write_files(struct zone* zone, const char* zfile)
   2187      1.1  christos {
   2188      1.1  christos 	size_t prevcount = 0;
   2189      1.1  christos 	int num;
   2190      1.1  christos 	struct ixfr_data* data;
   2191      1.1  christos 	if(!zone->ixfr || !zone->ixfr->data)
   2192      1.1  christos 		return; /* nothing to write */
   2193      1.1  christos 
   2194      1.1  christos 	/* write unwritten files to disk */
   2195      1.1  christos 	data = ixfr_data_last(zone->ixfr);
   2196      1.1  christos 	num=1;
   2197      1.1  christos 	while(data && data->file_num == 0) {
   2198      1.1  christos 		if(!ixfr_write_file(zone, data, zfile, num)) {
   2199      1.1  christos 			/* There could be more files that are sitting on the
   2200      1.1  christos 			 * disk, remove them, they are not used without
   2201      1.1  christos 			 * this ixfr file.
   2202      1.1  christos 			 *
   2203      1.1  christos 			 * Give this element a file num, so it can be
   2204      1.1  christos 			 * deleted, it failed to write. It may be partial,
   2205      1.1  christos 			 * and we do not want to read that back in.
   2206      1.1  christos 			 * We are left with the newer transfers, that form
   2207      1.1  christos 			 * a correct list of transfers, that are wholly
   2208      1.1  christos 			 * written. */
   2209      1.1  christos 			data->file_num = num;
   2210      1.1  christos 			ixfr_delete_rest_files(zone, data, zfile, 0);
   2211      1.1  christos 			return;
   2212      1.1  christos 		}
   2213      1.1  christos 		num++;
   2214      1.1  christos 		data = ixfr_data_prev(zone->ixfr, data, &prevcount);
   2215      1.1  christos 	}
   2216      1.1  christos }
   2217      1.1  christos 
   2218      1.1  christos void ixfr_write_to_file(struct zone* zone, const char* zfile)
   2219      1.1  christos {
   2220      1.1  christos 	int dest_num_files = 0;
   2221      1.1  christos 	/* we just wrote the zonefile zfile, and it is time to write
   2222      1.1  christos 	 * the IXFR contents to the disk too. */
   2223      1.1  christos 	/* find out what the target number of files is that we want on
   2224      1.1  christos 	 * the disk */
   2225      1.1  christos 	dest_num_files = ixfr_target_number_files(zone);
   2226      1.1  christos 
   2227      1.1  christos 	/* delete if we have more than we need */
   2228      1.1  christos 	ixfr_delete_superfluous_files(zone, zfile, dest_num_files);
   2229      1.1  christos 
   2230      1.1  christos 	/* delete if we have too much in memory */
   2231      1.1  christos 	ixfr_delete_memory_items(zone, dest_num_files);
   2232      1.1  christos 
   2233      1.1  christos 	/* rename the transfers that we have that already have a file */
   2234      1.1  christos 	if(!ixfr_rename_files(zone, zfile, dest_num_files))
   2235      1.1  christos 		return;
   2236      1.1  christos 
   2237      1.1  christos 	/* write the transfers that are not written yet */
   2238      1.1  christos 	ixfr_write_files(zone, zfile);
   2239      1.1  christos }
   2240      1.1  christos 
   2241      1.1  christos /* skip whitespace */
   2242      1.1  christos static char* skipwhite(char* str)
   2243      1.1  christos {
   2244      1.1  christos 	while(isspace((unsigned char)*str))
   2245      1.1  christos 		str++;
   2246      1.1  christos 	return str;
   2247      1.1  christos }
   2248      1.1  christos 
   2249      1.1  christos /* read one RR from file */
   2250      1.1  christos static int ixfr_data_readrr(struct zone* zone, FILE* in, const char* ixfrfile,
   2251      1.1  christos 	struct region* tempregion, struct domain_table* temptable,
   2252      1.1  christos 	struct zone* tempzone, struct rr** rr)
   2253      1.1  christos {
   2254      1.1  christos 	char line[65536];
   2255      1.1  christos 	char* str;
   2256      1.1  christos 	struct domain* domain_parsed = NULL;
   2257      1.1  christos 	int num_rrs = 0;
   2258      1.1  christos 	line[sizeof(line)-1]=0;
   2259      1.1  christos 	while(!feof(in)) {
   2260      1.1  christos 		if(!fgets(line, sizeof(line), in)) {
   2261      1.1  christos 			if(errno == 0) {
   2262      1.1  christos 				log_msg(LOG_ERR, "zone %s IXFR data %s: "
   2263      1.1  christos 					"unexpected end of file", zone->opts->name, ixfrfile);
   2264      1.1  christos 				return 0;
   2265      1.1  christos 			}
   2266      1.1  christos 			log_msg(LOG_ERR, "zone %s IXFR data %s: "
   2267      1.1  christos 				"cannot read: %s", zone->opts->name, ixfrfile,
   2268      1.1  christos 				strerror(errno));
   2269      1.1  christos 			return 0;
   2270      1.1  christos 		}
   2271      1.1  christos 		str = skipwhite(line);
   2272      1.1  christos 		if(str[0] == 0) {
   2273      1.1  christos 			/* empty line */
   2274      1.1  christos 			continue;
   2275      1.1  christos 		}
   2276      1.1  christos 		if(str[0] == ';') {
   2277      1.1  christos 			/* comment line */
   2278      1.1  christos 			continue;
   2279      1.1  christos 		}
   2280      1.1  christos 		if(zonec_parse_string(tempregion, temptable, tempzone,
   2281      1.1  christos 			line, &domain_parsed, &num_rrs)) {
   2282      1.1  christos 			log_msg(LOG_ERR, "zone %s IXFR data %s: parse error",
   2283      1.1  christos 				zone->opts->name, ixfrfile);
   2284      1.1  christos 			return 0;
   2285      1.1  christos 		}
   2286      1.1  christos 		if(num_rrs != 1) {
   2287      1.1  christos 			log_msg(LOG_ERR, "zone %s IXFR data %s: parse error",
   2288      1.1  christos 				zone->opts->name, ixfrfile);
   2289      1.1  christos 			return 0;
   2290      1.1  christos 		}
   2291      1.1  christos 		*rr = &domain_parsed->rrsets->rrs[0];
   2292      1.1  christos 		return 1;
   2293      1.1  christos 	}
   2294      1.1  christos 	log_msg(LOG_ERR, "zone %s IXFR data %s: file too short, no newsoa",
   2295      1.1  christos 		zone->opts->name, ixfrfile);
   2296      1.1  christos 	return 0;
   2297      1.1  christos }
   2298      1.1  christos 
   2299      1.1  christos /* delete from domain table */
   2300      1.1  christos static void domain_table_delete(struct domain_table* table,
   2301      1.1  christos 	struct domain* domain)
   2302      1.1  christos {
   2303      1.1  christos #ifdef USE_RADIX_TREE
   2304      1.1  christos 	radix_delete(table->nametree, domain->rnode);
   2305      1.1  christos #else
   2306      1.1  christos 	rbtree_delete(table->names_to_domains, domain->node.key);
   2307      1.1  christos #endif
   2308      1.1  christos }
   2309      1.1  christos 
   2310      1.1  christos /* can we delete temp domain */
   2311      1.1  christos static int can_del_temp_domain(struct domain* domain)
   2312      1.1  christos {
   2313      1.1  christos 	struct domain* n;
   2314      1.1  christos 	/* we want to keep the zone apex */
   2315      1.1  christos 	if(domain->is_apex)
   2316      1.1  christos 		return 0;
   2317      1.1  christos 	if(domain->rrsets)
   2318      1.1  christos 		return 0;
   2319      1.1  christos 	if(domain->usage)
   2320      1.1  christos 		return 0;
   2321      1.1  christos 	/* check if there are domains under it */
   2322      1.1  christos 	n = domain_next(domain);
   2323      1.1  christos 	if(n && domain_is_subdomain(n, domain))
   2324      1.1  christos 		return 0;
   2325      1.1  christos 	return 1;
   2326      1.1  christos }
   2327      1.1  christos 
   2328      1.1  christos /* delete temporary domain */
   2329      1.1  christos static void ixfr_temp_deldomain(struct domain_table* temptable,
   2330      1.1  christos 	struct domain* domain)
   2331      1.1  christos {
   2332      1.1  christos 	struct domain* p;
   2333      1.1  christos 	if(!can_del_temp_domain(domain))
   2334      1.1  christos 		return;
   2335      1.1  christos 	p = domain->parent;
   2336      1.1  christos 	/* see if this domain is someones wildcard-child-closest-match,
   2337      1.1  christos 	 * which can only be the parent, and then it should use the
   2338      1.1  christos 	 * one-smaller than this domain as closest-match. */
   2339      1.1  christos 	if(domain->parent &&
   2340      1.1  christos 		domain->parent->wildcard_child_closest_match == domain)
   2341      1.1  christos 		domain->parent->wildcard_child_closest_match =
   2342      1.1  christos 			domain_previous_existing_child(domain);
   2343      1.1  christos 	domain_table_delete(temptable, domain);
   2344      1.1  christos 	while(p) {
   2345      1.1  christos 		struct domain* up = p->parent;
   2346      1.1  christos 		if(!can_del_temp_domain(p))
   2347      1.1  christos 			break;
   2348      1.1  christos 		if(p->parent && p->parent->wildcard_child_closest_match == p)
   2349      1.1  christos 			p->parent->wildcard_child_closest_match =
   2350      1.1  christos 				domain_previous_existing_child(p);
   2351      1.1  christos 		domain_table_delete(temptable, p);
   2352      1.1  christos 		p = up;
   2353      1.1  christos 	}
   2354      1.1  christos }
   2355      1.1  christos 
   2356      1.1  christos /* clear out the just read RR from the temp table */
   2357      1.1  christos static void clear_temp_table_of_rr(struct domain_table* temptable,
   2358      1.1  christos 	struct zone* tempzone, struct rr* rr)
   2359      1.1  christos {
   2360      1.1  christos #if 0 /* clear out by removing everything, alternate for the cleanout code */
   2361      1.1  christos 	/* clear domains from the tempzone,
   2362      1.1  christos 	 * the only domain left is the zone apex and its parents */
   2363      1.1  christos 	domain_type* domain;
   2364      1.1  christos #ifdef USE_RADIX_TREE
   2365      1.1  christos 	struct radnode* first = radix_first(temptable->nametree);
   2366      1.1  christos 	domain = first?(domain_type*)first->elem:NULL;
   2367      1.1  christos #else
   2368      1.1  christos 	domain = (domain_type*)rbtree_first(temptable->names_to_domains);
   2369      1.1  christos #endif
   2370      1.1  christos 	while(domain != (domain_type*)RBTREE_NULL && domain) {
   2371      1.1  christos 		domain_type* next = domain_next(domain);
   2372      1.1  christos 		if(domain != tempzone->apex &&
   2373      1.1  christos 			!domain_is_subdomain(tempzone->apex, domain)) {
   2374      1.1  christos 			domain_table_delete(temptable, domain);
   2375      1.1  christos 		} else {
   2376      1.1  christos 			if(!domain->parent /* is the root */ ||
   2377      1.1  christos 				domain == tempzone->apex)
   2378      1.1  christos 				domain->usage = 1;
   2379      1.1  christos 			else	domain->usage = 0;
   2380      1.1  christos 		}
   2381      1.1  christos 		domain = next;
   2382      1.1  christos 	}
   2383      1.1  christos 
   2384      1.1  christos 	if(rr->owner == tempzone->apex) {
   2385      1.1  christos 		tempzone->apex->rrsets = NULL;
   2386      1.1  christos 		tempzone->soa_rrset = NULL;
   2387      1.1  christos 		tempzone->soa_nx_rrset = NULL;
   2388      1.1  christos 		tempzone->ns_rrset = NULL;
   2389      1.1  christos 	}
   2390      1.1  christos 	return;
   2391      1.1  christos #endif
   2392      1.1  christos 
   2393      1.1  christos 	/* clear domains in the rdata */
   2394      1.1  christos 	unsigned i;
   2395      1.1  christos 	for(i=0; i<rr->rdata_count; i++) {
   2396      1.1  christos 		if(rdata_atom_is_domain(rr->type, i)) {
   2397      1.1  christos 			/* clear out that dname */
   2398      1.1  christos 			struct domain* domain =
   2399      1.1  christos 				rdata_atom_domain(rr->rdatas[i]);
   2400      1.1  christos 			domain->usage --;
   2401      1.1  christos 			if(domain != tempzone->apex && domain->usage == 0)
   2402      1.1  christos 				ixfr_temp_deldomain(temptable, domain);
   2403      1.1  christos 		}
   2404      1.1  christos 	}
   2405      1.1  christos 
   2406      1.1  christos 	/* clear domain_parsed */
   2407      1.1  christos 	if(rr->owner == tempzone->apex) {
   2408      1.1  christos 		tempzone->apex->rrsets = NULL;
   2409      1.1  christos 		tempzone->soa_rrset = NULL;
   2410      1.1  christos 		tempzone->soa_nx_rrset = NULL;
   2411      1.1  christos 		tempzone->ns_rrset = NULL;
   2412      1.1  christos 	} else {
   2413      1.1  christos 		rr->owner->rrsets = NULL;
   2414      1.1  christos 		if(rr->owner->usage == 0) {
   2415      1.1  christos 			ixfr_temp_deldomain(temptable, rr->owner);
   2416      1.1  christos 		}
   2417      1.1  christos 	}
   2418      1.1  christos }
   2419      1.1  christos 
   2420      1.1  christos /* read ixfr data new SOA */
   2421      1.1  christos static int ixfr_data_readnewsoa(struct ixfr_data* data, struct zone* zone,
   2422      1.1  christos 	FILE* in, const char* ixfrfile, struct region* tempregion,
   2423      1.1  christos 	struct domain_table* temptable, struct zone* tempzone,
   2424      1.1  christos 	uint32_t dest_serial)
   2425      1.1  christos {
   2426      1.1  christos 	struct rr* rr;
   2427      1.1  christos 	size_t capacity = 0;
   2428      1.1  christos 	if(!ixfr_data_readrr(zone, in, ixfrfile, tempregion, temptable,
   2429      1.1  christos 		tempzone, &rr))
   2430      1.1  christos 		return 0;
   2431      1.1  christos 	if(rr->type != TYPE_SOA) {
   2432      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data does not start with SOA",
   2433      1.1  christos 			zone->opts->name, ixfrfile);
   2434      1.1  christos 		return 0;
   2435      1.1  christos 	}
   2436      1.1  christos 	if(rr->klass != CLASS_IN) {
   2437      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data is not class IN",
   2438      1.1  christos 			zone->opts->name, ixfrfile);
   2439      1.1  christos 		return 0;
   2440      1.1  christos 	}
   2441      1.1  christos 	if(!zone->apex) {
   2442      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: zone has no apex, no zone data",
   2443      1.1  christos 			zone->opts->name, ixfrfile);
   2444      1.1  christos 		return 0;
   2445      1.1  christos 	}
   2446      1.1  christos 	if(dname_compare(domain_dname(zone->apex), domain_dname(rr->owner)) != 0) {
   2447      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data wrong SOA for zone %s",
   2448      1.1  christos 			zone->opts->name, ixfrfile, domain_to_string(rr->owner));
   2449      1.1  christos 		return 0;
   2450      1.1  christos 	}
   2451      1.1  christos 	data->newserial = soa_rr_get_serial(rr);
   2452      1.1  christos 	if(data->newserial != dest_serial) {
   2453      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data contains the wrong version, serial %u but want destination serial %u",
   2454      1.1  christos 			zone->opts->name, ixfrfile, data->newserial,
   2455      1.1  christos 			dest_serial);
   2456      1.1  christos 		return 0;
   2457      1.1  christos 	}
   2458      1.1  christos 	if(!ixfr_putrr(domain_dname(rr->owner), rr->type, rr->klass, rr->ttl, rr->rdatas, rr->rdata_count, &data->newsoa, &data->newsoa_len, &capacity)) {
   2459      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: cannot allocate space",
   2460      1.1  christos 			zone->opts->name, ixfrfile);
   2461      1.1  christos 		return 0;
   2462      1.1  christos 	}
   2463      1.1  christos 	clear_temp_table_of_rr(temptable, tempzone, rr);
   2464      1.1  christos 	region_free_all(tempregion);
   2465      1.1  christos 	ixfr_trim_capacity(&data->newsoa, &data->newsoa_len, &capacity);
   2466      1.1  christos 	return 1;
   2467      1.1  christos }
   2468      1.1  christos 
   2469      1.1  christos /* read ixfr data old SOA */
   2470      1.1  christos static int ixfr_data_readoldsoa(struct ixfr_data* data, struct zone* zone,
   2471      1.1  christos 	FILE* in, const char* ixfrfile, struct region* tempregion,
   2472      1.1  christos 	struct domain_table* temptable, struct zone* tempzone,
   2473      1.1  christos 	uint32_t* dest_serial)
   2474      1.1  christos {
   2475      1.1  christos 	struct rr* rr;
   2476      1.1  christos 	size_t capacity = 0;
   2477      1.1  christos 	if(!ixfr_data_readrr(zone, in, ixfrfile, tempregion, temptable,
   2478      1.1  christos 		tempzone, &rr))
   2479      1.1  christos 		return 0;
   2480      1.1  christos 	if(rr->type != TYPE_SOA) {
   2481      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data 2nd RR is not SOA",
   2482      1.1  christos 			zone->opts->name, ixfrfile);
   2483      1.1  christos 		return 0;
   2484      1.1  christos 	}
   2485      1.1  christos 	if(rr->klass != CLASS_IN) {
   2486      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data 2ndSOA is not class IN",
   2487      1.1  christos 			zone->opts->name, ixfrfile);
   2488      1.1  christos 		return 0;
   2489      1.1  christos 	}
   2490      1.1  christos 	if(!zone->apex) {
   2491      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: zone has no apex, no zone data",
   2492      1.1  christos 			zone->opts->name, ixfrfile);
   2493      1.1  christos 		return 0;
   2494      1.1  christos 	}
   2495      1.1  christos 	if(dname_compare(domain_dname(zone->apex), domain_dname(rr->owner)) != 0) {
   2496      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: IXFR data wrong 2nd SOA for zone %s",
   2497      1.1  christos 			zone->opts->name, ixfrfile, domain_to_string(rr->owner));
   2498      1.1  christos 		return 0;
   2499      1.1  christos 	}
   2500      1.1  christos 	data->oldserial = soa_rr_get_serial(rr);
   2501      1.1  christos 	if(!ixfr_putrr(domain_dname(rr->owner), rr->type, rr->klass, rr->ttl, rr->rdatas, rr->rdata_count, &data->oldsoa, &data->oldsoa_len, &capacity)) {
   2502      1.1  christos 		log_msg(LOG_ERR, "zone %s ixfr data %s: cannot allocate space",
   2503      1.1  christos 			zone->opts->name, ixfrfile);
   2504      1.1  christos 		return 0;
   2505      1.1  christos 	}
   2506      1.1  christos 	clear_temp_table_of_rr(temptable, tempzone, rr);
   2507      1.1  christos 	region_free_all(tempregion);
   2508      1.1  christos 	ixfr_trim_capacity(&data->oldsoa, &data->oldsoa_len, &capacity);
   2509      1.1  christos 	*dest_serial = data->oldserial;
   2510      1.1  christos 	return 1;
   2511      1.1  christos }
   2512      1.1  christos 
   2513      1.1  christos /* read ixfr data del section */
   2514      1.1  christos static int ixfr_data_readdel(struct ixfr_data* data, struct zone* zone,
   2515      1.1  christos 	FILE* in, const char* ixfrfile, struct region* tempregion,
   2516      1.1  christos 	struct domain_table* temptable, struct zone* tempzone)
   2517      1.1  christos {
   2518      1.1  christos 	struct rr* rr;
   2519      1.1  christos 	size_t capacity = 0;
   2520      1.1  christos 	while(1) {
   2521      1.1  christos 		if(!ixfr_data_readrr(zone, in, ixfrfile, tempregion, temptable,
   2522      1.1  christos 			tempzone, &rr))
   2523      1.1  christos 			return 0;
   2524      1.1  christos 		if(!ixfr_putrr(domain_dname(rr->owner), rr->type, rr->klass, rr->ttl, rr->rdatas, rr->rdata_count, &data->del, &data->del_len, &capacity)) {
   2525      1.1  christos 			log_msg(LOG_ERR, "zone %s ixfr data %s: cannot allocate space",
   2526      1.1  christos 				zone->opts->name, ixfrfile);
   2527      1.1  christos 			return 0;
   2528      1.1  christos 		}
   2529      1.1  christos 		/* check SOA and also serial, because there could be other
   2530      1.1  christos 		 * add and del sections from older versions collated, we can
   2531      1.1  christos 		 * see this del section end when it has the serial */
   2532      1.1  christos 		if(rr->type == TYPE_SOA &&
   2533      1.1  christos 			soa_rr_get_serial(rr) == data->newserial) {
   2534      1.1  christos 			/* end of del section. */
   2535      1.1  christos 			clear_temp_table_of_rr(temptable, tempzone, rr);
   2536      1.1  christos 			region_free_all(tempregion);
   2537      1.1  christos 			break;
   2538      1.1  christos 		}
   2539      1.1  christos 		clear_temp_table_of_rr(temptable, tempzone, rr);
   2540      1.1  christos 		region_free_all(tempregion);
   2541      1.1  christos 	}
   2542      1.1  christos 	ixfr_trim_capacity(&data->del, &data->del_len, &capacity);
   2543      1.1  christos 	return 1;
   2544      1.1  christos }
   2545      1.1  christos 
   2546      1.1  christos /* read ixfr data add section */
   2547      1.1  christos static int ixfr_data_readadd(struct ixfr_data* data, struct zone* zone,
   2548      1.1  christos 	FILE* in, const char* ixfrfile, struct region* tempregion,
   2549      1.1  christos 	struct domain_table* temptable, struct zone* tempzone)
   2550      1.1  christos {
   2551      1.1  christos 	struct rr* rr;
   2552      1.1  christos 	size_t capacity = 0;
   2553      1.1  christos 	while(1) {
   2554      1.1  christos 		if(!ixfr_data_readrr(zone, in, ixfrfile, tempregion, temptable,
   2555      1.1  christos 			tempzone, &rr))
   2556      1.1  christos 			return 0;
   2557      1.1  christos 		if(!ixfr_putrr(domain_dname(rr->owner), rr->type, rr->klass, rr->ttl, rr->rdatas, rr->rdata_count, &data->add, &data->add_len, &capacity)) {
   2558      1.1  christos 			log_msg(LOG_ERR, "zone %s ixfr data %s: cannot allocate space",
   2559      1.1  christos 				zone->opts->name, ixfrfile);
   2560      1.1  christos 			return 0;
   2561      1.1  christos 		}
   2562      1.1  christos 		if(rr->type == TYPE_SOA &&
   2563      1.1  christos 			soa_rr_get_serial(rr) == data->newserial) {
   2564      1.1  christos 			/* end of add section. */
   2565      1.1  christos 			clear_temp_table_of_rr(temptable, tempzone, rr);
   2566      1.1  christos 			region_free_all(tempregion);
   2567      1.1  christos 			break;
   2568      1.1  christos 		}
   2569      1.1  christos 		clear_temp_table_of_rr(temptable, tempzone, rr);
   2570      1.1  christos 		region_free_all(tempregion);
   2571      1.1  christos 	}
   2572      1.1  christos 	ixfr_trim_capacity(&data->add, &data->add_len, &capacity);
   2573      1.1  christos 	return 1;
   2574      1.1  christos }
   2575      1.1  christos 
   2576      1.1  christos /* read ixfr data from file */
   2577      1.1  christos static int ixfr_data_read(struct nsd* nsd, struct zone* zone, FILE* in,
   2578      1.1  christos 	const char* ixfrfile, uint32_t* dest_serial, int file_num)
   2579      1.1  christos {
   2580      1.1  christos 	struct ixfr_data* data = NULL;
   2581      1.1  christos 	struct region* tempregion, *stayregion;
   2582      1.1  christos 	struct domain_table* temptable;
   2583      1.1  christos 	struct zone* tempzone;
   2584      1.1  christos 
   2585      1.1  christos 	if(zone->ixfr &&
   2586      1.1  christos 		zone->ixfr->data->count == zone->opts->pattern->ixfr_number) {
   2587      1.1  christos 		VERBOSITY(3, (LOG_INFO, "zone %s skip %s IXFR data because only %d ixfr-number configured",
   2588      1.1  christos 			zone->opts->name, ixfrfile, (int)zone->opts->pattern->ixfr_number));
   2589      1.1  christos 		return 0;
   2590      1.1  christos 	}
   2591      1.1  christos 
   2592      1.1  christos 	/* the file has header comments, new soa, old soa, delsection,
   2593      1.1  christos 	 * addsection. The delsection and addsection end in a SOA of oldver
   2594      1.1  christos 	 * and newver respectively. */
   2595      1.1  christos 	data = xalloc_zero(sizeof(*data));
   2596      1.1  christos 	data->file_num = file_num;
   2597      1.1  christos 
   2598      1.1  christos 	/* the temp region is cleared after every RR */
   2599      1.1  christos 	tempregion = region_create(xalloc, free);
   2600      1.1  christos 	/* the stay region holds the temporary data that stays between RRs */
   2601      1.1  christos 	stayregion = region_create(xalloc, free);
   2602      1.1  christos 	temptable = domain_table_create(stayregion);
   2603      1.1  christos 	tempzone = region_alloc_zero(stayregion, sizeof(zone_type));
   2604      1.1  christos 	if(!zone->apex) {
   2605      1.1  christos 		ixfr_data_free(data);
   2606      1.1  christos 		region_destroy(tempregion);
   2607      1.1  christos 		region_destroy(stayregion);
   2608      1.1  christos 		return 0;
   2609      1.1  christos 	}
   2610      1.1  christos 	tempzone->apex = domain_table_insert(temptable,
   2611      1.1  christos 		domain_dname(zone->apex));
   2612      1.1  christos 	temptable->root->usage++;
   2613      1.1  christos 	tempzone->apex->usage++;
   2614      1.1  christos 	tempzone->opts = zone->opts;
   2615      1.1  christos 	/* switch to per RR region for new allocations in temp domain table */
   2616      1.1  christos 	temptable->region = tempregion;
   2617      1.1  christos 
   2618      1.1  christos 	if(!ixfr_data_readnewsoa(data, zone, in, ixfrfile, tempregion,
   2619      1.1  christos 		temptable, tempzone, *dest_serial)) {
   2620      1.1  christos 		ixfr_data_free(data);
   2621      1.1  christos 		region_destroy(tempregion);
   2622      1.1  christos 		region_destroy(stayregion);
   2623      1.1  christos 		return 0;
   2624      1.1  christos 	}
   2625      1.1  christos 	if(!ixfr_data_readoldsoa(data, zone, in, ixfrfile, tempregion,
   2626      1.1  christos 		temptable, tempzone, dest_serial)) {
   2627      1.1  christos 		ixfr_data_free(data);
   2628      1.1  christos 		region_destroy(tempregion);
   2629      1.1  christos 		region_destroy(stayregion);
   2630      1.1  christos 		return 0;
   2631      1.1  christos 	}
   2632      1.1  christos 	if(!ixfr_data_readdel(data, zone, in, ixfrfile, tempregion, temptable,
   2633      1.1  christos 		tempzone)) {
   2634      1.1  christos 		ixfr_data_free(data);
   2635      1.1  christos 		region_destroy(tempregion);
   2636      1.1  christos 		region_destroy(stayregion);
   2637      1.1  christos 		return 0;
   2638      1.1  christos 	}
   2639      1.1  christos 	if(!ixfr_data_readadd(data, zone, in, ixfrfile, tempregion, temptable,
   2640      1.1  christos 		tempzone)) {
   2641      1.1  christos 		ixfr_data_free(data);
   2642      1.1  christos 		region_destroy(tempregion);
   2643      1.1  christos 		region_destroy(stayregion);
   2644      1.1  christos 		return 0;
   2645      1.1  christos 	}
   2646      1.1  christos 
   2647      1.1  christos 	region_destroy(tempregion);
   2648      1.1  christos 	region_destroy(stayregion);
   2649      1.1  christos 
   2650      1.1  christos 	if(!zone->ixfr)
   2651      1.1  christos 		zone->ixfr = zone_ixfr_create(nsd);
   2652      1.1  christos 	if(zone->opts->pattern->ixfr_size != 0 &&
   2653      1.1  christos 		zone->ixfr->total_size + ixfr_data_size(data) >
   2654      1.1  christos 		zone->opts->pattern->ixfr_size) {
   2655      1.1  christos 		VERBOSITY(3, (LOG_INFO, "zone %s skip %s IXFR data because only ixfr-size: %u configured, and it is %u size",
   2656      1.1  christos 			zone->opts->name, ixfrfile, (unsigned)zone->opts->pattern->ixfr_size, (unsigned)ixfr_data_size(data)));
   2657      1.1  christos 		ixfr_data_free(data);
   2658      1.1  christos 		return 0;
   2659      1.1  christos 	}
   2660      1.1  christos 	zone_ixfr_add(zone->ixfr, data, 0);
   2661      1.1  christos 	VERBOSITY(3, (LOG_INFO, "zone %s read %s IXFR data of %u bytes",
   2662      1.1  christos 		zone->opts->name, ixfrfile, (unsigned)ixfr_data_size(data)));
   2663      1.1  christos 	return 1;
   2664      1.1  christos }
   2665      1.1  christos 
   2666      1.1  christos /* try to read the next ixfr file. returns false if it fails or if it
   2667      1.1  christos  * does not fit in the configured sizes */
   2668      1.1  christos static int ixfr_read_one_more_file(struct nsd* nsd, struct zone* zone,
   2669      1.1  christos 	const char* zfile, int num_files, uint32_t *dest_serial)
   2670      1.1  christos {
   2671      1.1  christos 	char ixfrfile[1024+24];
   2672      1.1  christos 	FILE* in;
   2673      1.1  christos 	int file_num = num_files+1;
   2674      1.1  christos 	make_ixfr_name(ixfrfile, sizeof(ixfrfile), zfile, file_num);
   2675      1.1  christos 	in = fopen(ixfrfile, "r");
   2676      1.1  christos 	if(!in) {
   2677      1.1  christos 		if(errno == ENOENT) {
   2678      1.1  christos 			/* the file does not exist, we reached the end
   2679      1.1  christos 			 * of the list of IXFR files */
   2680      1.1  christos 			return 0;
   2681      1.1  christos 		}
   2682      1.1  christos 		log_msg(LOG_ERR, "could not read zone %s IXFR file %s: %s",
   2683      1.1  christos 			zone->opts->name, ixfrfile, strerror(errno));
   2684      1.1  christos 		return 0;
   2685      1.1  christos 	}
   2686      1.1  christos 	warn_if_directory("IXFR data", in, ixfrfile);
   2687      1.1  christos 	if(!ixfr_data_read(nsd, zone, in, ixfrfile, dest_serial, file_num)) {
   2688      1.1  christos 		fclose(in);
   2689      1.1  christos 		return 0;
   2690      1.1  christos 	}
   2691      1.1  christos 	fclose(in);
   2692      1.1  christos 	return 1;
   2693      1.1  christos }
   2694      1.1  christos 
   2695      1.1  christos void ixfr_read_from_file(struct nsd* nsd, struct zone* zone, const char* zfile)
   2696      1.1  christos {
   2697      1.1  christos 	uint32_t serial;
   2698      1.1  christos 	int num_files = 0;
   2699      1.1  christos 	/* delete the existing data, the zone data in memory has likely
   2700      1.1  christos 	 * changed, eg. due to reading a new zonefile. So that needs new
   2701      1.1  christos 	 * IXFRs */
   2702      1.1  christos 	zone_ixfr_clear(zone->ixfr);
   2703      1.1  christos 
   2704      1.1  christos 	/* track the serial number that we need to end up with, and check
   2705      1.1  christos 	 * that the IXFRs match up and result in the required version */
   2706      1.1  christos 	serial = zone_get_current_serial(zone);
   2707      1.1  christos 
   2708      1.1  christos 	while(ixfr_read_one_more_file(nsd, zone, zfile, num_files, &serial)) {
   2709      1.1  christos 		num_files++;
   2710      1.1  christos 	}
   2711      1.1  christos 	if(num_files > 0) {
   2712      1.1  christos 		VERBOSITY(1, (LOG_INFO, "zone %s read %d IXFR transfers with success",
   2713      1.1  christos 			zone->opts->name, num_files));
   2714      1.1  christos 	}
   2715      1.1  christos }
   2716