]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - contrib/bind9/lib/dns/ncache.c
Fix an off by one which can result in a assertion failure in BIND
[FreeBSD/releng/8.2.git] / contrib / bind9 / lib / dns / ncache.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2008, 2010  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: ncache.c,v 1.43.336.5 2010/05/19 09:56:44 marka Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <isc/buffer.h>
25 #include <isc/util.h>
26
27 #include <dns/db.h>
28 #include <dns/message.h>
29 #include <dns/ncache.h>
30 #include <dns/rdata.h>
31 #include <dns/rdatalist.h>
32 #include <dns/rdataset.h>
33 #include <dns/rdatastruct.h>
34
35 #define DNS_NCACHE_RDATA 20U
36
37 /*
38  * The format of an ncache rdata is a sequence of one or more records of
39  * the following format:
40  *
41  *      owner name
42  *      type
43  *      trust
44  *      rdata count
45  *              rdata length                    These two occur 'rdata count'
46  *              rdata                           times.
47  *
48  */
49
50 static inline isc_result_t
51 copy_rdataset(dns_rdataset_t *rdataset, isc_buffer_t *buffer) {
52         isc_result_t result;
53         unsigned int count;
54         isc_region_t ar, r;
55         dns_rdata_t rdata = DNS_RDATA_INIT;
56
57         /*
58          * Copy the rdataset count to the buffer.
59          */
60         isc_buffer_availableregion(buffer, &ar);
61         if (ar.length < 2)
62                 return (ISC_R_NOSPACE);
63         count = dns_rdataset_count(rdataset);
64         INSIST(count <= 65535);
65         isc_buffer_putuint16(buffer, (isc_uint16_t)count);
66
67         result = dns_rdataset_first(rdataset);
68         while (result == ISC_R_SUCCESS) {
69                 dns_rdataset_current(rdataset, &rdata);
70                 dns_rdata_toregion(&rdata, &r);
71                 INSIST(r.length <= 65535);
72                 isc_buffer_availableregion(buffer, &ar);
73                 if (ar.length < 2)
74                         return (ISC_R_NOSPACE);
75                 /*
76                  * Copy the rdata length to the buffer.
77                  */
78                 isc_buffer_putuint16(buffer, (isc_uint16_t)r.length);
79                 /*
80                  * Copy the rdata to the buffer.
81                  */
82                 result = isc_buffer_copyregion(buffer, &r);
83                 if (result != ISC_R_SUCCESS)
84                         return (result);
85                 dns_rdata_reset(&rdata);
86                 result = dns_rdataset_next(rdataset);
87         }
88         if (result != ISC_R_NOMORE)
89                 return (result);
90
91         return (ISC_R_SUCCESS);
92 }
93
94 isc_result_t
95 dns_ncache_add(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
96                dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
97                dns_rdataset_t *addedrdataset)
98 {
99         return (dns_ncache_addoptout(message, cache, node, covers, now, maxttl,
100                                     ISC_FALSE, addedrdataset));
101 }
102
103 isc_result_t
104 dns_ncache_addoptout(dns_message_t *message, dns_db_t *cache,
105                      dns_dbnode_t *node, dns_rdatatype_t covers,
106                      isc_stdtime_t now, dns_ttl_t maxttl,
107                      isc_boolean_t optout, dns_rdataset_t *addedrdataset)
108 {
109         isc_result_t result;
110         isc_buffer_t buffer;
111         isc_region_t r;
112         dns_rdataset_t *rdataset;
113         dns_rdatatype_t type;
114         dns_name_t *name;
115         dns_ttl_t ttl;
116         dns_trust_t trust;
117         dns_rdata_t rdata[DNS_NCACHE_RDATA];
118         dns_rdataset_t ncrdataset;
119         dns_rdatalist_t ncrdatalist;
120         unsigned char data[4096];
121         unsigned int next = 0;
122
123         /*
124          * Convert the authority data from 'message' into a negative cache
125          * rdataset, and store it in 'cache' at 'node'.
126          */
127
128         REQUIRE(message != NULL);
129
130         /*
131          * We assume that all data in the authority section has been
132          * validated by the caller.
133          */
134
135         /*
136          * Initialize the list.
137          */
138         ncrdatalist.rdclass = dns_db_class(cache);
139         ncrdatalist.type = 0;
140         ncrdatalist.covers = covers;
141         ncrdatalist.ttl = maxttl;
142         ISC_LIST_INIT(ncrdatalist.rdata);
143         ISC_LINK_INIT(&ncrdatalist, link);
144
145         /*
146          * Build an ncache rdatas into buffer.
147          */
148         ttl = maxttl;
149         trust = 0xffff;
150         isc_buffer_init(&buffer, data, sizeof(data));
151         if (message->counts[DNS_SECTION_AUTHORITY])
152                 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
153         else
154                 result = ISC_R_NOMORE;
155         while (result == ISC_R_SUCCESS) {
156                 name = NULL;
157                 dns_message_currentname(message, DNS_SECTION_AUTHORITY,
158                                         &name);
159                 if ((name->attributes & DNS_NAMEATTR_NCACHE) != 0) {
160                         for (rdataset = ISC_LIST_HEAD(name->list);
161                              rdataset != NULL;
162                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
163                                 if ((rdataset->attributes &
164                                      DNS_RDATASETATTR_NCACHE) == 0)
165                                         continue;
166                                 type = rdataset->type;
167                                 if (type == dns_rdatatype_rrsig)
168                                         type = rdataset->covers;
169                                 if (type == dns_rdatatype_soa ||
170                                     type == dns_rdatatype_nsec ||
171                                     type == dns_rdatatype_nsec3) {
172                                         if (ttl > rdataset->ttl)
173                                                 ttl = rdataset->ttl;
174                                         if (trust > rdataset->trust)
175                                                 trust = rdataset->trust;
176                                         /*
177                                          * Copy the owner name to the buffer.
178                                          */
179                                         dns_name_toregion(name, &r);
180                                         result = isc_buffer_copyregion(&buffer,
181                                                                        &r);
182                                         if (result != ISC_R_SUCCESS)
183                                                 return (result);
184                                         /*
185                                          * Copy the type to the buffer.
186                                          */
187                                         isc_buffer_availableregion(&buffer,
188                                                                    &r);
189                                         if (r.length < 3)
190                                                 return (ISC_R_NOSPACE);
191                                         isc_buffer_putuint16(&buffer,
192                                                              rdataset->type);
193                                         isc_buffer_putuint8(&buffer,
194                                                (unsigned char)rdataset->trust);
195                                         /*
196                                          * Copy the rdataset into the buffer.
197                                          */
198                                         result = copy_rdataset(rdataset,
199                                                                &buffer);
200                                         if (result != ISC_R_SUCCESS)
201                                                 return (result);
202
203                                         if (next >= DNS_NCACHE_RDATA)
204                                                 return (ISC_R_NOSPACE);
205                                         dns_rdata_init(&rdata[next]);
206                                         isc_buffer_remainingregion(&buffer, &r);
207                                         rdata[next].data = r.base;
208                                         rdata[next].length = r.length;
209                                         rdata[next].rdclass =
210                                                 ncrdatalist.rdclass;
211                                         rdata[next].type = 0;
212                                         rdata[next].flags = 0;
213                                         ISC_LIST_APPEND(ncrdatalist.rdata,
214                                                         &rdata[next], link);
215                                         isc_buffer_forward(&buffer, r.length);
216                                         next++;
217                                 }
218                         }
219                 }
220                 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
221         }
222         if (result != ISC_R_NOMORE)
223                 return (result);
224
225         if (trust == 0xffff) {
226                 /*
227                  * We didn't find any authority data from which to create a
228                  * negative cache rdataset.  In particular, we have no SOA.
229                  *
230                  * We trust that the caller wants negative caching, so this
231                  * means we have a "type 3 nxdomain" or "type 3 nodata"
232                  * response (see RFC2308 for details).
233                  *
234                  * We will now build a suitable negative cache rdataset that
235                  * will cause zero bytes to be emitted when converted to
236                  * wire format.
237                  */
238
239                 /*
240                  * The ownername must exist, but it doesn't matter what value
241                  * it has.  We use the root name.
242                  */
243                 dns_name_toregion(dns_rootname, &r);
244                 result = isc_buffer_copyregion(&buffer, &r);
245                 if (result != ISC_R_SUCCESS)
246                         return (result);
247                 /*
248                  * Copy the type and a zero rdata count to the buffer.
249                  */
250                 isc_buffer_availableregion(&buffer, &r);
251                 if (r.length < 5)
252                         return (ISC_R_NOSPACE);
253                 isc_buffer_putuint16(&buffer, 0);       /* type */
254                 /*
255                  * RFC2308, section 5, says that negative answers without
256                  * SOAs should not be cached.
257                  */
258                 ttl = 0;
259                 /*
260                  * Set trust.
261                  */
262                 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 &&
263                     message->counts[DNS_SECTION_ANSWER] == 0) {
264                         /*
265                          * The response has aa set and we haven't followed
266                          * any CNAME or DNAME chains.
267                          */
268                         trust = dns_trust_authauthority;
269                 } else
270                         trust = dns_trust_additional;
271                 isc_buffer_putuint8(&buffer, (unsigned char)trust); /* trust */
272                 isc_buffer_putuint16(&buffer, 0);       /* count */
273
274                 /*
275                  * Now add it to the cache.
276                  */
277                 if (next >= DNS_NCACHE_RDATA)
278                         return (ISC_R_NOSPACE);
279                 dns_rdata_init(&rdata[next]);
280                 isc_buffer_remainingregion(&buffer, &r);
281                 rdata[next].data = r.base;
282                 rdata[next].length = r.length;
283                 rdata[next].rdclass = ncrdatalist.rdclass;
284                 rdata[next].type = 0;
285                 rdata[next].flags = 0;
286                 ISC_LIST_APPEND(ncrdatalist.rdata, &rdata[next], link);
287         }
288
289         INSIST(trust != 0xffff);
290
291         ncrdatalist.ttl = ttl;
292
293         dns_rdataset_init(&ncrdataset);
294         RUNTIME_CHECK(dns_rdatalist_tordataset(&ncrdatalist, &ncrdataset)
295                       == ISC_R_SUCCESS);
296         ncrdataset.trust = trust;
297         if (message->rcode == dns_rcode_nxdomain)
298                 ncrdataset.attributes |= DNS_RDATASETATTR_NXDOMAIN;
299         if (optout)
300                 ncrdataset.attributes |= DNS_RDATASETATTR_OPTOUT;
301
302         return (dns_db_addrdataset(cache, node, NULL, now, &ncrdataset,
303                                    0, addedrdataset));
304 }
305
306 isc_result_t
307 dns_ncache_towire(dns_rdataset_t *rdataset, dns_compress_t *cctx,
308                   isc_buffer_t *target, unsigned int options,
309                   unsigned int *countp)
310 {
311         dns_rdata_t rdata = DNS_RDATA_INIT;
312         isc_result_t result;
313         isc_region_t remaining, tavailable;
314         isc_buffer_t source, savedbuffer, rdlen;
315         dns_name_t name;
316         dns_rdatatype_t type;
317         unsigned int i, rcount, count;
318
319         /*
320          * Convert the negative caching rdataset 'rdataset' to wire format,
321          * compressing names as specified in 'cctx', and storing the result in
322          * 'target'.
323          */
324
325         REQUIRE(rdataset != NULL);
326         REQUIRE(rdataset->type == 0);
327
328         savedbuffer = *target;
329         count = 0;
330
331         result = dns_rdataset_first(rdataset);
332         while (result == ISC_R_SUCCESS) {
333                 dns_rdataset_current(rdataset, &rdata);
334                 isc_buffer_init(&source, rdata.data, rdata.length);
335                 isc_buffer_add(&source, rdata.length);
336                 dns_name_init(&name, NULL);
337                 isc_buffer_remainingregion(&source, &remaining);
338                 dns_name_fromregion(&name, &remaining);
339                 INSIST(remaining.length >= name.length);
340                 isc_buffer_forward(&source, name.length);
341                 remaining.length -= name.length;
342
343                 INSIST(remaining.length >= 5);
344                 type = isc_buffer_getuint16(&source);
345                 isc_buffer_forward(&source, 1);
346                 rcount = isc_buffer_getuint16(&source);
347
348                 for (i = 0; i < rcount; i++) {
349                         /*
350                          * Get the length of this rdata and set up an
351                          * rdata structure for it.
352                          */
353                         isc_buffer_remainingregion(&source, &remaining);
354                         INSIST(remaining.length >= 2);
355                         dns_rdata_reset(&rdata);
356                         rdata.length = isc_buffer_getuint16(&source);
357                         isc_buffer_remainingregion(&source, &remaining);
358                         rdata.data = remaining.base;
359                         rdata.type = type;
360                         rdata.rdclass = rdataset->rdclass;
361                         INSIST(remaining.length >= rdata.length);
362                         isc_buffer_forward(&source, rdata.length);
363
364                         if ((options & DNS_NCACHETOWIRE_OMITDNSSEC) != 0 &&
365                             dns_rdatatype_isdnssec(type))
366                                 continue;
367
368                         /*
369                          * Write the name.
370                          */
371                         dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
372                         result = dns_name_towire(&name, cctx, target);
373                         if (result != ISC_R_SUCCESS)
374                                 goto rollback;
375
376                         /*
377                          * See if we have space for type, class, ttl, and
378                          * rdata length.  Write the type, class, and ttl.
379                          */
380                         isc_buffer_availableregion(target, &tavailable);
381                         if (tavailable.length < 10) {
382                                 result = ISC_R_NOSPACE;
383                                 goto rollback;
384                         }
385                         isc_buffer_putuint16(target, type);
386                         isc_buffer_putuint16(target, rdataset->rdclass);
387                         isc_buffer_putuint32(target, rdataset->ttl);
388
389                         /*
390                          * Save space for rdata length.
391                          */
392                         rdlen = *target;
393                         isc_buffer_add(target, 2);
394
395                         /*
396                          * Write the rdata.
397                          */
398                         result = dns_rdata_towire(&rdata, cctx, target);
399                         if (result != ISC_R_SUCCESS)
400                                 goto rollback;
401
402                         /*
403                          * Set the rdata length field to the compressed
404                          * length.
405                          */
406                         INSIST((target->used >= rdlen.used + 2) &&
407                                (target->used - rdlen.used - 2 < 65536));
408                         isc_buffer_putuint16(&rdlen,
409                                              (isc_uint16_t)(target->used -
410                                                             rdlen.used - 2));
411
412                         count++;
413                 }
414                 INSIST(isc_buffer_remaininglength(&source) == 0);
415                 result = dns_rdataset_next(rdataset);
416                 dns_rdata_reset(&rdata);
417         }
418         if (result != ISC_R_NOMORE)
419                 goto rollback;
420
421         *countp = count;
422
423         return (ISC_R_SUCCESS);
424
425  rollback:
426         INSIST(savedbuffer.used < 65536);
427         dns_compress_rollback(cctx, (isc_uint16_t)savedbuffer.used);
428         *countp = 0;
429         *target = savedbuffer;
430
431         return (result);
432 }
433
434 static void
435 rdataset_disassociate(dns_rdataset_t *rdataset) {
436         UNUSED(rdataset);
437 }
438
439 static isc_result_t
440 rdataset_first(dns_rdataset_t *rdataset) {
441         unsigned char *raw = rdataset->private3;
442         unsigned int count;
443
444         count = raw[0] * 256 + raw[1];
445         if (count == 0) {
446                 rdataset->private5 = NULL;
447                 return (ISC_R_NOMORE);
448         }
449         raw += 2;
450         /*
451          * The privateuint4 field is the number of rdata beyond the cursor
452          * position, so we decrement the total count by one before storing
453          * it.
454          */
455         count--;
456         rdataset->privateuint4 = count;
457         rdataset->private5 = raw;
458
459         return (ISC_R_SUCCESS);
460 }
461
462 static isc_result_t
463 rdataset_next(dns_rdataset_t *rdataset) {
464         unsigned int count;
465         unsigned int length;
466         unsigned char *raw;
467
468         count = rdataset->privateuint4;
469         if (count == 0)
470                 return (ISC_R_NOMORE);
471         count--;
472         rdataset->privateuint4 = count;
473         raw = rdataset->private5;
474         length = raw[0] * 256 + raw[1];
475         raw += length + 2;
476         rdataset->private5 = raw;
477
478         return (ISC_R_SUCCESS);
479 }
480
481 static void
482 rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
483         unsigned char *raw = rdataset->private5;
484         isc_region_t r;
485
486         REQUIRE(raw != NULL);
487
488         r.length = raw[0] * 256 + raw[1];
489         raw += 2;
490         r.base = raw;
491         dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
492 }
493
494 static void
495 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
496         *target = *source;
497
498         /*
499          * Reset iterator state.
500          */
501         target->privateuint4 = 0;
502         target->private5 = NULL;
503 }
504
505 static unsigned int
506 rdataset_count(dns_rdataset_t *rdataset) {
507         unsigned char *raw = rdataset->private3;
508         unsigned int count;
509
510         count = raw[0] * 256 + raw[1];
511
512         return (count);
513 }
514
515 static void
516 rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust) {
517         unsigned char *raw = rdataset->private3;
518
519         raw[-1] = (unsigned char)trust;
520 }
521
522 static dns_rdatasetmethods_t rdataset_methods = {
523         rdataset_disassociate,
524         rdataset_first,
525         rdataset_next,
526         rdataset_current,
527         rdataset_clone,
528         rdataset_count,
529         NULL,
530         NULL,
531         NULL,
532         NULL,
533         NULL,
534         NULL,
535         NULL,
536         rdataset_settrust,
537         NULL
538 };
539
540 isc_result_t
541 dns_ncache_getrdataset(dns_rdataset_t *ncacherdataset, dns_name_t *name,
542                        dns_rdatatype_t type, dns_rdataset_t *rdataset)
543 {
544         isc_result_t result;
545         dns_rdata_t rdata = DNS_RDATA_INIT;
546         isc_region_t remaining;
547         isc_buffer_t source;
548         dns_name_t tname;
549         dns_rdatatype_t ttype;
550         dns_trust_t trust = dns_trust_none;
551         dns_rdataset_t clone;
552
553         REQUIRE(ncacherdataset != NULL);
554         REQUIRE(ncacherdataset->type == 0);
555         REQUIRE(name != NULL);
556         REQUIRE(!dns_rdataset_isassociated(rdataset));
557         REQUIRE(type != dns_rdatatype_rrsig);
558
559         dns_rdataset_init(&clone);
560         dns_rdataset_clone(ncacherdataset, &clone);
561         result = dns_rdataset_first(&clone);
562         while (result == ISC_R_SUCCESS) {
563                 dns_rdataset_current(&clone, &rdata);
564                 isc_buffer_init(&source, rdata.data, rdata.length);
565                 isc_buffer_add(&source, rdata.length);
566                 dns_name_init(&tname, NULL);
567                 isc_buffer_remainingregion(&source, &remaining);
568                 dns_name_fromregion(&tname, &remaining);
569                 INSIST(remaining.length >= tname.length);
570                 isc_buffer_forward(&source, tname.length);
571                 remaining.length -= tname.length;
572
573                 INSIST(remaining.length >= 3);
574                 ttype = isc_buffer_getuint16(&source);
575
576                 if (ttype == type && dns_name_equal(&tname, name)) {
577                         trust = isc_buffer_getuint8(&source);
578                         INSIST(trust <= dns_trust_ultimate);
579                         isc_buffer_remainingregion(&source, &remaining);
580                         break;
581                 }
582                 result = dns_rdataset_next(&clone);
583                 dns_rdata_reset(&rdata);
584         }
585         dns_rdataset_disassociate(&clone);
586         if (result == ISC_R_NOMORE)
587                 return (ISC_R_NOTFOUND);
588         if (result != ISC_R_SUCCESS)
589                 return (result);
590
591         INSIST(remaining.length != 0);
592
593         rdataset->methods = &rdataset_methods;
594         rdataset->rdclass = ncacherdataset->rdclass;
595         rdataset->type = type;
596         rdataset->covers = 0;
597         rdataset->ttl = ncacherdataset->ttl;
598         rdataset->trust = trust;
599         rdataset->private1 = NULL;
600         rdataset->private2 = NULL;
601
602         rdataset->private3 = remaining.base;
603
604         /*
605          * Reset iterator state.
606          */
607         rdataset->privateuint4 = 0;
608         rdataset->private5 = NULL;
609         rdataset->private6 = NULL;
610         return (ISC_R_SUCCESS);
611 }
612
613 isc_result_t
614 dns_ncache_getsigrdataset(dns_rdataset_t *ncacherdataset, dns_name_t *name,
615                           dns_rdatatype_t covers, dns_rdataset_t *rdataset)
616 {
617         dns_name_t tname;
618         dns_rdata_rrsig_t rrsig;
619         dns_rdata_t rdata = DNS_RDATA_INIT;
620         dns_rdataset_t clone;
621         dns_rdatatype_t type;
622         dns_trust_t trust = dns_trust_none;
623         isc_buffer_t source;
624         isc_region_t remaining, sigregion;
625         isc_result_t result;
626         unsigned char *raw;
627         unsigned int count;
628
629         REQUIRE(ncacherdataset != NULL);
630         REQUIRE(ncacherdataset->type == 0);
631         REQUIRE(name != NULL);
632         REQUIRE(!dns_rdataset_isassociated(rdataset));
633
634         dns_rdataset_init(&clone);
635         dns_rdataset_clone(ncacherdataset, &clone);
636         result = dns_rdataset_first(&clone);
637         while (result == ISC_R_SUCCESS) {
638                 dns_rdataset_current(&clone, &rdata);
639                 isc_buffer_init(&source, rdata.data, rdata.length);
640                 isc_buffer_add(&source, rdata.length);
641                 dns_name_init(&tname, NULL);
642                 isc_buffer_remainingregion(&source, &remaining);
643                 dns_name_fromregion(&tname, &remaining);
644                 INSIST(remaining.length >= tname.length);
645                 isc_buffer_forward(&source, tname.length);
646                 remaining.length -= tname.length;
647                 remaining.base += tname.length;
648
649                 INSIST(remaining.length >= 2);
650                 type = isc_buffer_getuint16(&source);
651                 remaining.length -= 2;
652                 remaining.base += 2;
653
654                 if (type != dns_rdatatype_rrsig ||
655                     !dns_name_equal(&tname, name)) {
656                         result = dns_rdataset_next(&clone);
657                         dns_rdata_reset(&rdata);
658                         continue;
659                 }
660
661                 INSIST(remaining.length >= 1);
662                 trust = isc_buffer_getuint8(&source);
663                 INSIST(trust <= dns_trust_ultimate);
664                 remaining.length -= 1;
665                 remaining.base += 1;
666
667                 raw = remaining.base;
668                 count = raw[0] * 256 + raw[1];
669                 INSIST(count > 0);
670                 raw += 2;
671                 sigregion.length = raw[0] * 256 + raw[1];
672                 raw += 2;
673                 sigregion.base = raw;
674                 dns_rdata_reset(&rdata);
675                 dns_rdata_fromregion(&rdata, rdataset->rdclass,
676                                      dns_rdatatype_rrsig, &sigregion);
677                 (void)dns_rdata_tostruct(&rdata, &rrsig, NULL);
678                 if (rrsig.covered == covers) {
679                         isc_buffer_remainingregion(&source, &remaining);
680                         break;
681                 }
682
683                 result = dns_rdataset_next(&clone);
684                 dns_rdata_reset(&rdata);
685         }
686         dns_rdataset_disassociate(&clone);
687         if (result == ISC_R_NOMORE)
688                 return (ISC_R_NOTFOUND);
689         if (result != ISC_R_SUCCESS)
690                 return (result);
691
692         INSIST(remaining.length != 0);
693
694         rdataset->methods = &rdataset_methods;
695         rdataset->rdclass = ncacherdataset->rdclass;
696         rdataset->type = dns_rdatatype_rrsig;
697         rdataset->covers = covers;
698         rdataset->ttl = ncacherdataset->ttl;
699         rdataset->trust = trust;
700         rdataset->private1 = NULL;
701         rdataset->private2 = NULL;
702
703         rdataset->private3 = remaining.base;
704
705         /*
706          * Reset iterator state.
707          */
708         rdataset->privateuint4 = 0;
709         rdataset->private5 = NULL;
710         rdataset->private6 = NULL;
711         return (ISC_R_SUCCESS);
712 }
713
714 void
715 dns_ncache_current(dns_rdataset_t *ncacherdataset, dns_name_t *found,
716                    dns_rdataset_t *rdataset)
717 {
718         dns_rdata_t rdata = DNS_RDATA_INIT;
719         dns_trust_t trust;
720         isc_region_t remaining, sigregion;
721         isc_buffer_t source;
722         dns_name_t tname;
723         dns_rdatatype_t type;
724         unsigned int count;
725         dns_rdata_rrsig_t rrsig;
726         unsigned char *raw;
727
728         REQUIRE(ncacherdataset != NULL);
729         REQUIRE(ncacherdataset->type == 0);
730         REQUIRE(found != NULL);
731         REQUIRE(!dns_rdataset_isassociated(rdataset));
732
733         dns_rdataset_current(ncacherdataset, &rdata);
734         isc_buffer_init(&source, rdata.data, rdata.length);
735         isc_buffer_add(&source, rdata.length);
736
737         dns_name_init(&tname, NULL);
738         isc_buffer_remainingregion(&source, &remaining);
739         dns_name_fromregion(found, &remaining);
740         INSIST(remaining.length >= found->length);
741         isc_buffer_forward(&source, found->length);
742         remaining.length -= found->length;
743
744         INSIST(remaining.length >= 5);
745         type = isc_buffer_getuint16(&source);
746         trust = isc_buffer_getuint8(&source);
747         INSIST(trust <= dns_trust_ultimate);
748         isc_buffer_remainingregion(&source, &remaining);
749
750         rdataset->methods = &rdataset_methods;
751         rdataset->rdclass = ncacherdataset->rdclass;
752         rdataset->type = type;
753         if (type == dns_rdatatype_rrsig) {
754                 /*
755                  * Extract covers from RRSIG.
756                  */
757                 raw = remaining.base;
758                 count = raw[0] * 256 + raw[1];
759                 INSIST(count > 0);
760                 raw += 2;
761                 sigregion.length = raw[0] * 256 + raw[1];
762                 raw += 2;
763                 sigregion.base = raw;
764                 dns_rdata_reset(&rdata);
765                 dns_rdata_fromregion(&rdata, rdataset->rdclass,
766                                      rdataset->type, &sigregion);
767                 (void)dns_rdata_tostruct(&rdata, &rrsig, NULL);
768                 rdataset->covers = rrsig.covered;
769         } else
770                 rdataset->covers = 0;
771         rdataset->ttl = ncacherdataset->ttl;
772         rdataset->trust = trust;
773         rdataset->private1 = NULL;
774         rdataset->private2 = NULL;
775
776         rdataset->private3 = remaining.base;
777
778         /*
779          * Reset iterator state.
780          */
781         rdataset->privateuint4 = 0;
782         rdataset->private5 = NULL;
783         rdataset->private6 = NULL;
784 }