ncache.c revision 84185d19c7a9ef1ac23cc6236c8773697d4efeb1
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 1999-2001 Internet Software Consortium.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * Permission to use, copy, modify, and distribute this software for any
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * purpose with or without fee is hereby granted, provided that the above
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
15a44745412679c30a6d022733925af70a38b715David Lawrence * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15a44745412679c30a6d022733925af70a38b715David Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15a44745412679c30a6d022733925af70a38b715David Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington/* $Id: ncache.c,v 1.27 2002/01/22 09:07:22 bwelling Exp $ */
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * The format of an ncache rdata is a sequence of one or more records of
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * the following format:
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * owner name
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * rdata count
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * rdata length These two occur 'rdata count'
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * rdata times.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halleycopy_rdataset(dns_rdataset_t *rdataset, isc_buffer_t *buffer) {
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley unsigned int count;
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * Copy the rdataset count to the buffer.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley isc_buffer_putuint16(buffer, (isc_uint16_t)count);
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * Copy the rdata length to the buffer.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley isc_buffer_putuint16(buffer, (isc_uint16_t)r.length);
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * Copy the rdata to the buffer.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halleydns_ncache_add(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
7cd4c3ddd1baf5f2b204562fdba3da37c716cc78Andreas Gustafsson dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * Convert the authority data from 'message' into a negative cache
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * rdataset, and store it in 'cache' at 'node'.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * We assume that all data in the authority section has been
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * validated by the caller.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley * First, build an ncache rdata in buffer.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_init(&buffer, data, sizeof(data));
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley dns_message_currentname(message, DNS_SECTION_AUTHORITY,
66b2f0d4bfa342770aa5e26a005a0c0ec5071231Bob Halley if ((name->attributes & DNS_NAMEATTR_NCACHE) != 0) {
e0df061f35a26d2bbd0986aa889f88b3710b32d4Bob Halley * Copy the owner name to the buffer.
e0df061f35a26d2bbd0986aa889f88b3710b32d4Bob Halley * Copy the type to the buffer.
e0df061f35a26d2bbd0986aa889f88b3710b32d4Bob Halley * Copy the rdataset into the buffer.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * We didn't find any authority data from which to create a
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * negative cache rdataset. In particular, we have no SOA.
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * We trust that the caller wants negative caching, so this
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * means we have a "type 3 nxdomain" or "type 3 nodata"
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * response (see RFC 2308 for details).
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * We will now build a suitable negative cache rdataset that
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * will cause zero bytes to be emitted when converted to
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * wire format.
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * The ownername must exist, but it doesn't matter what value
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * it has. We use the root name.
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * Copy the type and a zero rdata count to the buffer.
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * RFC 2308, section 5, says that negative answers without
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * SOAs should not be cached.
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * Set trust.
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 &&
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * The response has aa set and we haven't followed
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * any CNAME or DNAME chains.
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley * Now add it to the cache.
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halley ISC_LIST_APPEND(ncrdatalist.rdata, &rdata, link);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson RUNTIME_CHECK(dns_rdatalist_tordataset(&ncrdatalist, &ncrdataset)
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley return (dns_db_addrdataset(cache, node, NULL, now, &ncrdataset,
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halleydns_ncache_towire(dns_rdataset_t *rdataset, dns_compress_t *cctx,
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington isc_buffer_t *target, isc_boolean_t omit_dnssec,
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington unsigned int *countp)
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * Convert the negative caching rdataset 'rdataset' to wire format,
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * compressing names as specified in 'cctx', and storing the result in
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley INSIST(dns_rdataset_next(rdataset) == ISC_R_NOMORE);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_init(&source, rdata.data, rdata.length);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_remainingregion(&source, &remaining);
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley for (i = 0; i < rcount; i++) {
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * Get the length of this rdata and set up an
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * rdata structure for it.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_remainingregion(&source, &remaining);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_remainingregion(&source, &remaining);
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington if (omit_dnssec && dns_rdatatype_isdnssec(type))
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * Write the name.
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * See if we have space for type, class, ttl, and
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * rdata length. Write the type, class, and ttl.
8569ab045a4cf6ecd1b5a3354ddb1c93ef34ea57Brian Wellington isc_buffer_availableregion(target, &tavailable);
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * Save space for rdata length.
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * Write the rdata.
948eabe2a254a8a278ef6325f3790e75329ee656Bob Halley * Set the rdata length field to the compressed
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_remainingregion(&source, &remaining);