ncache.c revision a8da00ef95ba37b9d071c2b8db1a0c967e060106
523ad879ce1abb51cae579a6393da0e0436fe8dfTinderbox User * Copyright (C) 2004, 2005, 2007, 2008, 2010-2014 Internet Systems Consortium, Inc. ("ISC")
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1999-2003 Internet Software Consortium.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Permission to use, copy, modify, and/or 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.
c1ee8bb4ba3e9ab1463403ed685729631de406b1Mark Andrews * The format of an ncache rdata is a sequence of zero 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.
48019314431389cca5f8eba7ee9aa5bc08a67f4eMark Andrewsaddoptout(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
48019314431389cca5f8eba7ee9aa5bc08a67f4eMark Andrews dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
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,
48019314431389cca5f8eba7ee9aa5bc08a67f4eMark Andrews return (addoptout(message, cache, node, covers, now, maxttl,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrewsdns_ncache_addoptout(dns_message_t *message, dns_db_t *cache,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_boolean_t optout, dns_rdataset_t *addedrdataset)
48019314431389cca5f8eba7ee9aa5bc08a67f4eMark Andrews return (addoptout(message, cache, node, covers, now, maxttl,
48019314431389cca5f8eba7ee9aa5bc08a67f4eMark Andrewsaddoptout(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
48019314431389cca5f8eba7ee9aa5bc08a67f4eMark Andrews dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews unsigned int next = 0;
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.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * Initialize the list.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * Build an ncache rdatas into 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 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.
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson RUNTIME_CHECK(dns_rdatalist_tordataset(&ncrdatalist, &ncrdataset)
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt ncrdataset.attributes |= DNS_RDATASETATTR_NEGATIVE;
421e4cf66e4cba0b0751a34a9c027e39fe0474f9Mark Andrews ncrdataset.attributes |= DNS_RDATASETATTR_NXDOMAIN;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews ncrdataset.attributes |= DNS_RDATASETATTR_OPTOUT;
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
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt REQUIRE((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews 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
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews INSIST(isc_buffer_remaininglength(&source) == 0);
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;
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrewsrdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust) {
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)
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt REQUIRE((ncacherdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark 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);
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews * Reset iterator state.
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrewsdns_ncache_getsigrdataset(dns_rdataset_t *ncacherdataset, dns_name_t *name,
515c7f3c43f76d7b439905b18009105364b36100Automatic Updater dns_rdatatype_t covers, dns_rdataset_t *rdataset)
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews unsigned char *raw;
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews unsigned int count;
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt REQUIRE((ncacherdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0);
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews isc_buffer_init(&source, rdata.data, rdata.length);
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews isc_buffer_remainingregion(&source, &remaining);
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews dns_rdata_fromregion(&rdata, rdataset->rdclass,
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews (void)dns_rdata_tostruct(&rdata, &rrsig, NULL);
b335299322e50f045f10e4636262cd2f8d407a8bMark Andrews isc_buffer_remainingregion(&source, &remaining);
ff30cdeb783ca7ffe69b222c56197828e882c229Mark Andrews * Reset iterator state.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrewsdns_ncache_current(dns_rdataset_t *ncacherdataset, dns_name_t *found,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews unsigned int count;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews unsigned char *raw;
6de9744cf9c64be2145f663e4051196a4eaa9d45Evan Hunt REQUIRE((ncacherdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_buffer_init(&source, rdata.data, rdata.length);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_buffer_remainingregion(&source, &remaining);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_buffer_remainingregion(&source, &remaining);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * Extract covers from RRSIG.
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews dns_rdata_fromregion(&rdata, rdataset->rdclass,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews (void)dns_rdata_tostruct(&rdata, &rrsig, NULL);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * Reset iterator state.