ncache.c revision 364a82f7c25b62967678027043425201a5e5171a
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Copyright (C) 1999, 2000 Internet Software Consortium.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Permission to use, copy, modify, and distribute this software for any
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * purpose with or without fee is hereby granted, provided that the above
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * copyright notice and this permission notice appear in all copies.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * The format of an ncache rdata is a sequence of one or more records of
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * the following format:
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * owner name
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * rdata count
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * rdata length These two occur 'rdata count'
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * rdata times.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewscopy_rdataset(dns_rdataset_t *rdataset, isc_buffer_t *buffer) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int count;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Copy the rdataset count to the buffer.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_putuint16(buffer, (isc_uint16_t)count);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Copy the rdata length to the buffer.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_putuint16(buffer, (isc_uint16_t)r.length);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Copy the rdata to the buffer.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_ncache_add(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Convert the authority data from 'message' into a negative cache
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * rdataset, and store it in 'cache' at 'node'.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * We assume that all data in the authority section has been
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * validated by the caller.
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * First, build an ncache rdata in buffer.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_message_currentname(message, DNS_SECTION_AUTHORITY,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if ((name->attributes & DNS_NAMEATTR_NCACHE) != 0) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Copy the owner name to the buffer.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Copy the type to the buffer.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Copy the rdataset into the buffer.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * We didn't find any authority data from which to create a
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * negative cache rdataset. In particular, we have no SOA.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * We trust that the caller wants negative caching, so this
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * means we have a "type 3 nxdomain" or "type 3 nodata"
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * response (see RFC 2308 for details).
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * We will now build a suitable negative cache rdataset that
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * will cause zero bytes to be emitted when converted to
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * wire format.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * The ownername must exist, but it doesn't matter what value
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * it has. We use the root name.
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * Copy the type and a zero rdata count to the buffer.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * RFC 2308, section 5, says that negative answers without
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * SOAs should not be cached.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Set trust.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * The response has aa set and we haven't followed
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * any CNAME or DNAME chains.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Now add it to the cache.
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews ISC_LIST_APPEND(ncrdatalist.rdata, &rdata, link);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_rdatalist_tordataset(&ncrdatalist, &ncrdataset);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (dns_db_addrdataset(cache, node, NULL, now, &ncrdataset,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_ncache_towire(dns_rdataset_t *rdataset, dns_compress_t *cctx,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Convert the negative caching rdataset 'rdataset' to wire format,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * compressing names as specified in 'cctx', and storing the result in
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(dns_rdataset_next(rdataset) == ISC_R_NOMORE);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_init(&source, rdata.data, rdata.length);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_remainingregion(&source, &remaining);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews for (i = 0; i < rcount; i++) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Get the length of this rdata and set up an
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * rdata structure for it.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_remainingregion(&source, &remaining);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_remainingregion(&source, &remaining);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Write the name.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * See if we have space for type, class, ttl, and
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * rdata length. Write the type, class, and ttl.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_remainingregion(target, &tremaining);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_putuint16(target, rdataset->rdclass);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Save space for rdata length.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Write the rdata.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_rdata_towire(&rdata, cctx, target);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Set the rdata length field to the compressed
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews isc_buffer_remainingregion(&source, &remaining);