]> The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
Use nd_ types, add EXTRACT_ calls.
[tcpdump] / print-domain.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 /* \summary: Domain Name System (DNS) printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <netdissect-stdinc.h>
29
30 #include <string.h>
31
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "addrtostr.h"
35 #include "extract.h"
36
37 #include "nameser.h"
38
39 static const char *ns_ops[] = {
40 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
41 " op8", " updateA", " updateD", " updateDA",
42 " updateM", " updateMA", " zoneInit", " zoneRef",
43 };
44
45 static const char *ns_resp[] = {
46 "", " FormErr", " ServFail", " NXDomain",
47 " NotImp", " Refused", " YXDomain", " YXRRSet",
48 " NXRRSet", " NotAuth", " NotZone", " Resp11",
49 " Resp12", " Resp13", " Resp14", " NoChange",
50 };
51
52 /* skip over a domain name */
53 static const u_char *
54 ns_nskip(netdissect_options *ndo,
55 const u_char *cp)
56 {
57 u_char i;
58
59 if (!ND_TTEST_1(cp))
60 return (NULL);
61 i = EXTRACT_U_1(cp);
62 cp++;
63 while (i) {
64 if ((i & INDIR_MASK) == INDIR_MASK)
65 return (cp + 1);
66 if ((i & INDIR_MASK) == EDNS0_MASK) {
67 int bitlen, bytelen;
68
69 if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
70 return(NULL); /* unknown ELT */
71 if (!ND_TTEST_1(cp))
72 return (NULL);
73 if ((bitlen = EXTRACT_U_1(cp)) == 0)
74 bitlen = 256;
75 cp++;
76 bytelen = (bitlen + 7) / 8;
77 cp += bytelen;
78 } else
79 cp += i;
80 if (!ND_TTEST_1(cp))
81 return (NULL);
82 i = EXTRACT_U_1(cp);
83 cp++;
84 }
85 return (cp);
86 }
87
88 /* print a <domain-name> */
89 static const u_char *
90 blabel_print(netdissect_options *ndo,
91 const u_char *cp)
92 {
93 int bitlen, slen, b;
94 const u_char *bitp, *lim;
95 char tc;
96
97 if (!ND_TTEST_1(cp))
98 return(NULL);
99 if ((bitlen = EXTRACT_U_1(cp)) == 0)
100 bitlen = 256;
101 slen = (bitlen + 3) / 4;
102 lim = cp + 1 + slen;
103
104 /* print the bit string as a hex string */
105 ND_PRINT((ndo, "\\[x"));
106 for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) {
107 ND_TCHECK_1(bitp);
108 ND_PRINT((ndo, "%02x", EXTRACT_U_1(bitp)));
109 }
110 if (b > 4) {
111 ND_TCHECK_1(bitp);
112 tc = EXTRACT_U_1(bitp);
113 bitp++;
114 ND_PRINT((ndo, "%02x", tc & (0xff << (8 - b))));
115 } else if (b > 0) {
116 ND_TCHECK_1(bitp);
117 tc = EXTRACT_U_1(bitp);
118 bitp++;
119 ND_PRINT((ndo, "%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
120 }
121 ND_PRINT((ndo, "/%d]", bitlen));
122 return lim;
123 trunc:
124 ND_PRINT((ndo, ".../%d]", bitlen));
125 return NULL;
126 }
127
128 static int
129 labellen(netdissect_options *ndo,
130 const u_char *cp)
131 {
132 u_int i;
133
134 if (!ND_TTEST_1(cp))
135 return(-1);
136 i = EXTRACT_U_1(cp);
137 if ((i & INDIR_MASK) == EDNS0_MASK) {
138 int bitlen, elt;
139 if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL) {
140 ND_PRINT((ndo, "<ELT %d>", elt));
141 return(-1);
142 }
143 if (!ND_TTEST_1(cp + 1))
144 return(-1);
145 if ((bitlen = EXTRACT_U_1(cp + 1)) == 0)
146 bitlen = 256;
147 return(((bitlen + 7) / 8) + 1);
148 } else
149 return(i);
150 }
151
152 const u_char *
153 ns_nprint(netdissect_options *ndo,
154 const u_char *cp, const u_char *bp)
155 {
156 u_int i, l;
157 const u_char *rp = NULL;
158 int compress = 0;
159 int elt;
160 u_int offset, max_offset;
161
162 if ((l = labellen(ndo, cp)) == (u_int)-1)
163 return(NULL);
164 if (!ND_TTEST_1(cp))
165 return(NULL);
166 max_offset = (u_int)(cp - bp);
167 i = EXTRACT_U_1(cp);
168 cp++;
169 if ((i & INDIR_MASK) != INDIR_MASK) {
170 compress = 0;
171 rp = cp + l;
172 }
173
174 if (i != 0)
175 while (i && cp < ndo->ndo_snapend) {
176 if ((i & INDIR_MASK) == INDIR_MASK) {
177 if (!compress) {
178 rp = cp + 1;
179 compress = 1;
180 }
181 if (!ND_TTEST_1(cp))
182 return(NULL);
183 offset = (((i << 8) | EXTRACT_U_1(cp)) & 0x3fff);
184 /*
185 * This must move backwards in the packet.
186 * No RFC explicitly says that, but BIND's
187 * name decompression code requires it,
188 * as a way of preventing infinite loops
189 * and other bad behavior, and it's probably
190 * what was intended (compress by pointing
191 * to domain name suffixes already seen in
192 * the packet).
193 */
194 if (offset >= max_offset) {
195 ND_PRINT((ndo, "<BAD PTR>"));
196 return(NULL);
197 }
198 max_offset = offset;
199 cp = bp + offset;
200 if ((l = labellen(ndo, cp)) == (u_int)-1)
201 return(NULL);
202 if (!ND_TTEST_1(cp))
203 return(NULL);
204 i = EXTRACT_U_1(cp);
205 cp++;
206 continue;
207 }
208 if ((i & INDIR_MASK) == EDNS0_MASK) {
209 elt = (i & ~INDIR_MASK);
210 switch(elt) {
211 case EDNS0_ELT_BITLABEL:
212 if (blabel_print(ndo, cp) == NULL)
213 return (NULL);
214 break;
215 default:
216 /* unknown ELT */
217 ND_PRINT((ndo, "<ELT %d>", elt));
218 return(NULL);
219 }
220 } else {
221 if (fn_printn(ndo, cp, l, ndo->ndo_snapend))
222 return(NULL);
223 }
224
225 cp += l;
226 ND_PRINT((ndo, "."));
227 if ((l = labellen(ndo, cp)) == (u_int)-1)
228 return(NULL);
229 if (!ND_TTEST_1(cp))
230 return(NULL);
231 i = EXTRACT_U_1(cp);
232 cp++;
233 if (!compress)
234 rp += l + 1;
235 }
236 else
237 ND_PRINT((ndo, "."));
238 return (rp);
239 }
240
241 /* print a <character-string> */
242 static const u_char *
243 ns_cprint(netdissect_options *ndo,
244 const u_char *cp)
245 {
246 u_int i;
247
248 if (!ND_TTEST_1(cp))
249 return (NULL);
250 i = EXTRACT_U_1(cp);
251 cp++;
252 if (fn_printn(ndo, cp, i, ndo->ndo_snapend))
253 return (NULL);
254 return (cp + i);
255 }
256
257 /* http://www.iana.org/assignments/dns-parameters */
258 const struct tok ns_type2str[] = {
259 { T_A, "A" }, /* RFC 1035 */
260 { T_NS, "NS" }, /* RFC 1035 */
261 { T_MD, "MD" }, /* RFC 1035 */
262 { T_MF, "MF" }, /* RFC 1035 */
263 { T_CNAME, "CNAME" }, /* RFC 1035 */
264 { T_SOA, "SOA" }, /* RFC 1035 */
265 { T_MB, "MB" }, /* RFC 1035 */
266 { T_MG, "MG" }, /* RFC 1035 */
267 { T_MR, "MR" }, /* RFC 1035 */
268 { T_NULL, "NULL" }, /* RFC 1035 */
269 { T_WKS, "WKS" }, /* RFC 1035 */
270 { T_PTR, "PTR" }, /* RFC 1035 */
271 { T_HINFO, "HINFO" }, /* RFC 1035 */
272 { T_MINFO, "MINFO" }, /* RFC 1035 */
273 { T_MX, "MX" }, /* RFC 1035 */
274 { T_TXT, "TXT" }, /* RFC 1035 */
275 { T_RP, "RP" }, /* RFC 1183 */
276 { T_AFSDB, "AFSDB" }, /* RFC 1183 */
277 { T_X25, "X25" }, /* RFC 1183 */
278 { T_ISDN, "ISDN" }, /* RFC 1183 */
279 { T_RT, "RT" }, /* RFC 1183 */
280 { T_NSAP, "NSAP" }, /* RFC 1706 */
281 { T_NSAP_PTR, "NSAP_PTR" },
282 { T_SIG, "SIG" }, /* RFC 2535 */
283 { T_KEY, "KEY" }, /* RFC 2535 */
284 { T_PX, "PX" }, /* RFC 2163 */
285 { T_GPOS, "GPOS" }, /* RFC 1712 */
286 { T_AAAA, "AAAA" }, /* RFC 1886 */
287 { T_LOC, "LOC" }, /* RFC 1876 */
288 { T_NXT, "NXT" }, /* RFC 2535 */
289 { T_EID, "EID" }, /* Nimrod */
290 { T_NIMLOC, "NIMLOC" }, /* Nimrod */
291 { T_SRV, "SRV" }, /* RFC 2782 */
292 { T_ATMA, "ATMA" }, /* ATM Forum */
293 { T_NAPTR, "NAPTR" }, /* RFC 2168, RFC 2915 */
294 { T_KX, "KX" }, /* RFC 2230 */
295 { T_CERT, "CERT" }, /* RFC 2538 */
296 { T_A6, "A6" }, /* RFC 2874 */
297 { T_DNAME, "DNAME" }, /* RFC 2672 */
298 { T_SINK, "SINK" },
299 { T_OPT, "OPT" }, /* RFC 2671 */
300 { T_APL, "APL" }, /* RFC 3123 */
301 { T_DS, "DS" }, /* RFC 4034 */
302 { T_SSHFP, "SSHFP" }, /* RFC 4255 */
303 { T_IPSECKEY, "IPSECKEY" }, /* RFC 4025 */
304 { T_RRSIG, "RRSIG" }, /* RFC 4034 */
305 { T_NSEC, "NSEC" }, /* RFC 4034 */
306 { T_DNSKEY, "DNSKEY" }, /* RFC 4034 */
307 { T_SPF, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
308 { T_UINFO, "UINFO" },
309 { T_UID, "UID" },
310 { T_GID, "GID" },
311 { T_UNSPEC, "UNSPEC" },
312 { T_UNSPECA, "UNSPECA" },
313 { T_TKEY, "TKEY" }, /* RFC 2930 */
314 { T_TSIG, "TSIG" }, /* RFC 2845 */
315 { T_IXFR, "IXFR" }, /* RFC 1995 */
316 { T_AXFR, "AXFR" }, /* RFC 1035 */
317 { T_MAILB, "MAILB" }, /* RFC 1035 */
318 { T_MAILA, "MAILA" }, /* RFC 1035 */
319 { T_ANY, "ANY" },
320 { 0, NULL }
321 };
322
323 const struct tok ns_class2str[] = {
324 { C_IN, "IN" }, /* Not used */
325 { C_CHAOS, "CHAOS" },
326 { C_HS, "HS" },
327 { C_ANY, "ANY" },
328 { 0, NULL }
329 };
330
331 /* print a query */
332 static const u_char *
333 ns_qprint(netdissect_options *ndo,
334 const u_char *cp, const u_char *bp, int is_mdns)
335 {
336 const u_char *np = cp;
337 u_int i, class;
338
339 cp = ns_nskip(ndo, cp);
340
341 if (cp == NULL || !ND_TTEST_4(cp))
342 return(NULL);
343
344 /* print the qtype */
345 i = EXTRACT_BE_U_2(cp);
346 cp += 2;
347 ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d", i)));
348 /* print the qclass (if it's not IN) */
349 i = EXTRACT_BE_U_2(cp);
350 cp += 2;
351 if (is_mdns)
352 class = (i & ~C_QU);
353 else
354 class = i;
355 if (class != C_IN)
356 ND_PRINT((ndo, " %s", tok2str(ns_class2str, "(Class %d)", class)));
357 if (is_mdns) {
358 ND_PRINT((ndo, i & C_QU ? " (QU)" : " (QM)"));
359 }
360
361 ND_PRINT((ndo, "? "));
362 cp = ns_nprint(ndo, np, bp);
363 return(cp ? cp + 4 : NULL);
364 }
365
366 /* print a reply */
367 static const u_char *
368 ns_rprint(netdissect_options *ndo,
369 const u_char *cp, const u_char *bp, int is_mdns)
370 {
371 u_int i, class, opt_flags = 0;
372 u_short typ, len;
373 const u_char *rp;
374
375 if (ndo->ndo_vflag) {
376 ND_PRINT((ndo, " "));
377 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
378 return NULL;
379 } else
380 cp = ns_nskip(ndo, cp);
381
382 if (cp == NULL || !ND_TTEST_LEN(cp, 10))
383 return (ndo->ndo_snapend);
384
385 /* print the type/qtype */
386 typ = EXTRACT_BE_U_2(cp);
387 cp += 2;
388 /* print the class (if it's not IN and the type isn't OPT) */
389 i = EXTRACT_BE_U_2(cp);
390 cp += 2;
391 if (is_mdns)
392 class = (i & ~C_CACHE_FLUSH);
393 else
394 class = i;
395 if (class != C_IN && typ != T_OPT)
396 ND_PRINT((ndo, " %s", tok2str(ns_class2str, "(Class %d)", class)));
397 if (is_mdns) {
398 if (i & C_CACHE_FLUSH)
399 ND_PRINT((ndo, " (Cache flush)"));
400 }
401
402 if (typ == T_OPT) {
403 /* get opt flags */
404 cp += 2;
405 opt_flags = EXTRACT_BE_U_2(cp);
406 /* ignore rest of ttl field */
407 cp += 2;
408 } else if (ndo->ndo_vflag > 2) {
409 /* print ttl */
410 ND_PRINT((ndo, " ["));
411 unsigned_relts_print(ndo, EXTRACT_BE_U_4(cp));
412 ND_PRINT((ndo, "]"));
413 cp += 4;
414 } else {
415 /* ignore ttl */
416 cp += 4;
417 }
418
419 len = EXTRACT_BE_U_2(cp);
420 cp += 2;
421
422 rp = cp + len;
423
424 ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d", typ)));
425 if (rp > ndo->ndo_snapend)
426 return(NULL);
427
428 switch (typ) {
429 case T_A:
430 if (!ND_TTEST_LEN(cp, sizeof(struct in_addr)))
431 return(NULL);
432 ND_PRINT((ndo, " %s", intoa(htonl(EXTRACT_BE_U_4(cp)))));
433 break;
434
435 case T_NS:
436 case T_CNAME:
437 case T_PTR:
438 #ifdef T_DNAME
439 case T_DNAME:
440 #endif
441 ND_PRINT((ndo, " "));
442 if (ns_nprint(ndo, cp, bp) == NULL)
443 return(NULL);
444 break;
445
446 case T_SOA:
447 if (!ndo->ndo_vflag)
448 break;
449 ND_PRINT((ndo, " "));
450 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
451 return(NULL);
452 ND_PRINT((ndo, " "));
453 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
454 return(NULL);
455 if (!ND_TTEST_LEN(cp, 5 * 4))
456 return(NULL);
457 ND_PRINT((ndo, " %u", EXTRACT_BE_U_4(cp)));
458 cp += 4;
459 ND_PRINT((ndo, " %u", EXTRACT_BE_U_4(cp)));
460 cp += 4;
461 ND_PRINT((ndo, " %u", EXTRACT_BE_U_4(cp)));
462 cp += 4;
463 ND_PRINT((ndo, " %u", EXTRACT_BE_U_4(cp)));
464 cp += 4;
465 ND_PRINT((ndo, " %u", EXTRACT_BE_U_4(cp)));
466 cp += 4;
467 break;
468 case T_MX:
469 ND_PRINT((ndo, " "));
470 if (!ND_TTEST_2(cp))
471 return(NULL);
472 if (ns_nprint(ndo, cp + 2, bp) == NULL)
473 return(NULL);
474 ND_PRINT((ndo, " %d", EXTRACT_BE_U_2(cp)));
475 break;
476
477 case T_TXT:
478 while (cp < rp) {
479 ND_PRINT((ndo, " \""));
480 cp = ns_cprint(ndo, cp);
481 if (cp == NULL)
482 return(NULL);
483 ND_PRINT((ndo, "\""));
484 }
485 break;
486
487 case T_SRV:
488 ND_PRINT((ndo, " "));
489 if (!ND_TTEST_6(cp))
490 return(NULL);
491 if (ns_nprint(ndo, cp + 6, bp) == NULL)
492 return(NULL);
493 ND_PRINT((ndo, ":%d %d %d", EXTRACT_BE_U_2(cp + 4),
494 EXTRACT_BE_U_2(cp), EXTRACT_BE_U_2(cp + 2)));
495 break;
496
497 case T_AAAA:
498 {
499 char ntop_buf[INET6_ADDRSTRLEN];
500
501 if (!ND_TTEST_LEN(cp, sizeof(struct in6_addr)))
502 return(NULL);
503 ND_PRINT((ndo, " %s",
504 addrtostr6(cp, ntop_buf, sizeof(ntop_buf))));
505
506 break;
507 }
508
509 case T_A6:
510 {
511 struct in6_addr a;
512 int pbit, pbyte;
513 char ntop_buf[INET6_ADDRSTRLEN];
514
515 if (!ND_TTEST_1(cp))
516 return(NULL);
517 pbit = EXTRACT_U_1(cp);
518 pbyte = (pbit & ~7) / 8;
519 if (pbit > 128) {
520 ND_PRINT((ndo, " %u(bad plen)", pbit));
521 break;
522 } else if (pbit < 128) {
523 if (!ND_TTEST_LEN(cp + 1, sizeof(a) - pbyte))
524 return(NULL);
525 memset(&a, 0, sizeof(a));
526 memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte);
527 ND_PRINT((ndo, " %u %s", pbit,
528 addrtostr6(&a, ntop_buf, sizeof(ntop_buf))));
529 }
530 if (pbit > 0) {
531 ND_PRINT((ndo, " "));
532 if (ns_nprint(ndo, cp + 1 + sizeof(a) - pbyte, bp) == NULL)
533 return(NULL);
534 }
535 break;
536 }
537
538 case T_OPT:
539 ND_PRINT((ndo, " UDPsize=%u", class));
540 if (opt_flags & 0x8000)
541 ND_PRINT((ndo, " DO"));
542 break;
543
544 case T_UNSPECA: /* One long string */
545 if (!ND_TTEST_LEN(cp, len))
546 return(NULL);
547 if (fn_printn(ndo, cp, len, ndo->ndo_snapend))
548 return(NULL);
549 break;
550
551 case T_TSIG:
552 {
553 if (cp + len > ndo->ndo_snapend)
554 return(NULL);
555 if (!ndo->ndo_vflag)
556 break;
557 ND_PRINT((ndo, " "));
558 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
559 return(NULL);
560 cp += 6;
561 if (!ND_TTEST_2(cp))
562 return(NULL);
563 ND_PRINT((ndo, " fudge=%u", EXTRACT_BE_U_2(cp)));
564 cp += 2;
565 if (!ND_TTEST_2(cp))
566 return(NULL);
567 ND_PRINT((ndo, " maclen=%u", EXTRACT_BE_U_2(cp)));
568 cp += 2 + EXTRACT_BE_U_2(cp);
569 if (!ND_TTEST_2(cp))
570 return(NULL);
571 ND_PRINT((ndo, " origid=%u", EXTRACT_BE_U_2(cp)));
572 cp += 2;
573 if (!ND_TTEST_2(cp))
574 return(NULL);
575 ND_PRINT((ndo, " error=%u", EXTRACT_BE_U_2(cp)));
576 cp += 2;
577 if (!ND_TTEST_2(cp))
578 return(NULL);
579 ND_PRINT((ndo, " otherlen=%u", EXTRACT_BE_U_2(cp)));
580 cp += 2;
581 }
582 }
583 return (rp); /* XXX This isn't always right */
584 }
585
586 void
587 domain_print(netdissect_options *ndo,
588 const u_char *bp, u_int length, int is_mdns)
589 {
590 const HEADER *np;
591 uint16_t flags;
592 u_int qdcount, ancount, nscount, arcount;
593 u_int i;
594 const u_char *cp;
595 uint16_t b2;
596
597 np = (const HEADER *)bp;
598 ND_TCHECK(*np);
599 flags = EXTRACT_BE_U_2(np->flags);
600 /* get the byte-order right */
601 qdcount = EXTRACT_BE_U_2(np->qdcount);
602 ancount = EXTRACT_BE_U_2(np->ancount);
603 nscount = EXTRACT_BE_U_2(np->nscount);
604 arcount = EXTRACT_BE_U_2(np->arcount);
605
606 if (DNS_QR(flags)) {
607 /* this is a response */
608 ND_PRINT((ndo, "%d%s%s%s%s%s%s",
609 EXTRACT_BE_U_2(np->id),
610 ns_ops[DNS_OPCODE(flags)],
611 ns_resp[DNS_RCODE(flags)],
612 DNS_AA(flags)? "*" : "",
613 DNS_RA(flags)? "" : "-",
614 DNS_TC(flags)? "|" : "",
615 DNS_AD(flags)? "$" : ""));
616
617 if (qdcount != 1)
618 ND_PRINT((ndo, " [%uq]", qdcount));
619 /* Print QUESTION section on -vv */
620 cp = (const u_char *)(np + 1);
621 for (i = 0; i < qdcount; i++) {
622 if (i != 0)
623 ND_PRINT((ndo, ","));
624 if (ndo->ndo_vflag > 1) {
625 ND_PRINT((ndo, " q:"));
626 if ((cp = ns_qprint(ndo, cp, bp, is_mdns)) == NULL)
627 goto trunc;
628 } else {
629 if ((cp = ns_nskip(ndo, cp)) == NULL)
630 goto trunc;
631 cp += 4; /* skip QTYPE and QCLASS */
632 }
633 }
634 ND_PRINT((ndo, " %u/%u/%u", ancount, nscount, arcount));
635 if (ancount) {
636 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
637 goto trunc;
638 ancount--;
639 while (cp < ndo->ndo_snapend && ancount) {
640 ND_PRINT((ndo, ","));
641 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
642 goto trunc;
643 ancount--;
644 }
645 }
646 if (ancount)
647 goto trunc;
648 /* Print NS and AR sections on -vv */
649 if (ndo->ndo_vflag > 1) {
650 if (cp < ndo->ndo_snapend && nscount) {
651 ND_PRINT((ndo, " ns:"));
652 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
653 goto trunc;
654 nscount--;
655 while (cp < ndo->ndo_snapend && nscount) {
656 ND_PRINT((ndo, ","));
657 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
658 goto trunc;
659 nscount--;
660 }
661 }
662 if (nscount)
663 goto trunc;
664 if (cp < ndo->ndo_snapend && arcount) {
665 ND_PRINT((ndo, " ar:"));
666 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
667 goto trunc;
668 arcount--;
669 while (cp < ndo->ndo_snapend && arcount) {
670 ND_PRINT((ndo, ","));
671 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
672 goto trunc;
673 arcount--;
674 }
675 }
676 if (arcount)
677 goto trunc;
678 }
679 }
680 else {
681 /* this is a request */
682 ND_PRINT((ndo, "%d%s%s%s", EXTRACT_BE_U_2(np->id), ns_ops[DNS_OPCODE(flags)],
683 DNS_RD(flags) ? "+" : "",
684 DNS_CD(flags) ? "%" : ""));
685
686 /* any weirdness? */
687 b2 = EXTRACT_BE_U_2(((const u_short *)np) + 1);
688 if (b2 & 0x6cf)
689 ND_PRINT((ndo, " [b2&3=0x%x]", b2));
690
691 if (DNS_OPCODE(flags) == IQUERY) {
692 if (qdcount)
693 ND_PRINT((ndo, " [%uq]", qdcount));
694 if (ancount != 1)
695 ND_PRINT((ndo, " [%ua]", ancount));
696 }
697 else {
698 if (ancount)
699 ND_PRINT((ndo, " [%ua]", ancount));
700 if (qdcount != 1)
701 ND_PRINT((ndo, " [%uq]", qdcount));
702 }
703 if (nscount)
704 ND_PRINT((ndo, " [%un]", nscount));
705 if (arcount)
706 ND_PRINT((ndo, " [%uau]", arcount));
707
708 cp = (const u_char *)(np + 1);
709 if (qdcount) {
710 cp = ns_qprint(ndo, cp, (const u_char *)np, is_mdns);
711 if (!cp)
712 goto trunc;
713 qdcount--;
714 while (cp < ndo->ndo_snapend && qdcount) {
715 cp = ns_qprint(ndo, (const u_char *)cp,
716 (const u_char *)np,
717 is_mdns);
718 if (!cp)
719 goto trunc;
720 qdcount--;
721 }
722 }
723 if (qdcount)
724 goto trunc;
725
726 /* Print remaining sections on -vv */
727 if (ndo->ndo_vflag > 1) {
728 if (ancount) {
729 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
730 goto trunc;
731 ancount--;
732 while (cp < ndo->ndo_snapend && ancount) {
733 ND_PRINT((ndo, ","));
734 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
735 goto trunc;
736 ancount--;
737 }
738 }
739 if (ancount)
740 goto trunc;
741 if (cp < ndo->ndo_snapend && nscount) {
742 ND_PRINT((ndo, " ns:"));
743 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
744 goto trunc;
745 nscount--;
746 while (cp < ndo->ndo_snapend && nscount) {
747 ND_PRINT((ndo, ","));
748 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
749 goto trunc;
750 nscount--;
751 }
752 }
753 if (nscount > 0)
754 goto trunc;
755 if (cp < ndo->ndo_snapend && arcount) {
756 ND_PRINT((ndo, " ar:"));
757 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
758 goto trunc;
759 arcount--;
760 while (cp < ndo->ndo_snapend && arcount) {
761 ND_PRINT((ndo, ","));
762 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
763 goto trunc;
764 arcount--;
765 }
766 }
767 if (arcount)
768 goto trunc;
769 }
770 }
771 ND_PRINT((ndo, " (%d)", length));
772 return;
773
774 trunc:
775 ND_PRINT((ndo, "[|domain]"));
776 }