ncache.c revision d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3c
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1999-2003 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.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉/* $Id: ncache.c,v 1.37 2004/12/21 10:45:17 jinmei 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));
4607e7a9b8dfb1b41c70e51c2b603daaf22cf302Mark Andrews 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)
421e4cf66e4cba0b0751a34a9c027e39fe0474f9Mark Andrews ncrdataset.attributes |= DNS_RDATASETATTR_NXDOMAIN;
55254a46f91419b92eee0d20dfb958e8dd52526cBob Halley return (dns_db_addrdataset(cache, node, NULL, now, &ncrdataset,
4e142a5bccd2944174ad9ae58d86cf03e170054dBob Halleydns_ncache_towire(dns_rdataset_t *rdataset, dns_compress_t *cctx,
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington isc_buffer_t *target, unsigned int options,
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);
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington if ((options & DNS_NCACHETOWIRE_OMITDNSSEC) != 0 &&
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);
c866769e664ba0a6a5e6f9375245f5ccca393009David Lawrence dns_compress_rollback(cctx, (isc_uint16_t)savedbuffer.used);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrewsrdataset_disassociate(dns_rdataset_t *rdataset) {
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews unsigned int count;
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews * The privateuint4 field is the number of rdata beyond the cursor
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews * position, so we decrement the total count by one before storing
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews unsigned int count;
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews unsigned int length;
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews unsigned char *raw;
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrewsrdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrewsrdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews * Reset iterator state.
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrewsstatic unsigned int
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews unsigned int count;
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrewsstatic dns_rdatasetmethods_t rdataset_methods = {
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrewsdns_ncache_getrdataset(dns_rdataset_t *ncacherdataset, dns_name_t *name,
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews dns_rdatatype_t type, dns_rdataset_t *rdataset)
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews unsigned int i, rcount;
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews INSIST(dns_rdataset_next(ncacherdataset) == ISC_R_NOMORE);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews isc_buffer_init(&source, rdata.data, rdata.length);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews isc_buffer_remainingregion(&source, &remaining);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews if (ttype == type && dns_name_equal(&tname, name)) {
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews isc_buffer_remainingregion(&source, &remaining);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews for (i = 0; i < rcount; i++) {
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews isc_buffer_remainingregion(&source, &remaining);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews isc_buffer_remainingregion(&source, &remaining);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews isc_buffer_remainingregion(&source, &remaining);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews * Reset iterator state.