]> The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
Use more the EXTRACT_U_1() macro (40/n)
[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 "nameser.h"
31
32 #include <string.h>
33
34 #include "netdissect.h"
35 #include "addrtoname.h"
36 #include "addrtostr.h"
37 #include "extract.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 register const u_char *cp)
56 {
57 register 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(*bitp);
108 ND_PRINT((ndo, "%02x", EXTRACT_U_1(bitp)));
109 }
110 if (b > 4) {
111 ND_TCHECK(*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(*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 register 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 register const u_char *cp, register const u_char *bp)
155 {
156 register u_int i, l;
157 register const u_char *rp = NULL;
158 register 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 register const u_char *cp)
245 {
246 register 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 register const u_char *cp, register const u_char *bp, int is_mdns)
335 {
336 register const u_char *np = cp;
337 register 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 register const u_char *cp, register const u_char *bp, int is_mdns)
370 {
371 register u_int i, class, opt_flags = 0;
372 register u_short typ, len;
373 register 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_TTEST2(*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_TTEST2(*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_TTEST2(*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_TTEST2(*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_TTEST2(*(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_TTEST2(*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 register const u_char *bp, u_int length, int is_mdns)
589 {
590 register const HEADER *np;
591 register int qdcount, ancount, nscount, arcount;
592 register const u_char *cp;
593 uint16_t b2;
594
595 np = (const HEADER *)bp;
596 ND_TCHECK(*np);
597 /* get the byte-order right */
598 qdcount = EXTRACT_BE_U_2(&np->qdcount);
599 ancount = EXTRACT_BE_U_2(&np->ancount);
600 nscount = EXTRACT_BE_U_2(&np->nscount);
601 arcount = EXTRACT_BE_U_2(&np->arcount);
602
603 if (DNS_QR(np)) {
604 /* this is a response */
605 ND_PRINT((ndo, "%d%s%s%s%s%s%s",
606 EXTRACT_BE_U_2(&np->id),
607 ns_ops[DNS_OPCODE(np)],
608 ns_resp[DNS_RCODE(np)],
609 DNS_AA(np)? "*" : "",
610 DNS_RA(np)? "" : "-",
611 DNS_TC(np)? "|" : "",
612 DNS_AD(np)? "$" : ""));
613
614 if (qdcount != 1)
615 ND_PRINT((ndo, " [%dq]", qdcount));
616 /* Print QUESTION section on -vv */
617 cp = (const u_char *)(np + 1);
618 while (qdcount--) {
619 if (qdcount < EXTRACT_BE_U_2(&np->qdcount) - 1)
620 ND_PRINT((ndo, ","));
621 if (ndo->ndo_vflag > 1) {
622 ND_PRINT((ndo, " q:"));
623 if ((cp = ns_qprint(ndo, cp, bp, is_mdns)) == NULL)
624 goto trunc;
625 } else {
626 if ((cp = ns_nskip(ndo, cp)) == NULL)
627 goto trunc;
628 cp += 4; /* skip QTYPE and QCLASS */
629 }
630 }
631 ND_PRINT((ndo, " %d/%d/%d", ancount, nscount, arcount));
632 if (ancount--) {
633 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
634 goto trunc;
635 while (cp < ndo->ndo_snapend && ancount--) {
636 ND_PRINT((ndo, ","));
637 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
638 goto trunc;
639 }
640 }
641 if (ancount > 0)
642 goto trunc;
643 /* Print NS and AR sections on -vv */
644 if (ndo->ndo_vflag > 1) {
645 if (cp < ndo->ndo_snapend && nscount--) {
646 ND_PRINT((ndo, " ns:"));
647 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
648 goto trunc;
649 while (cp < ndo->ndo_snapend && nscount--) {
650 ND_PRINT((ndo, ","));
651 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
652 goto trunc;
653 }
654 }
655 if (nscount > 0)
656 goto trunc;
657 if (cp < ndo->ndo_snapend && arcount--) {
658 ND_PRINT((ndo, " ar:"));
659 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
660 goto trunc;
661 while (cp < ndo->ndo_snapend && arcount--) {
662 ND_PRINT((ndo, ","));
663 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
664 goto trunc;
665 }
666 }
667 if (arcount > 0)
668 goto trunc;
669 }
670 }
671 else {
672 /* this is a request */
673 ND_PRINT((ndo, "%d%s%s%s", EXTRACT_BE_U_2(&np->id), ns_ops[DNS_OPCODE(np)],
674 DNS_RD(np) ? "+" : "",
675 DNS_CD(np) ? "%" : ""));
676
677 /* any weirdness? */
678 b2 = EXTRACT_BE_U_2(((const u_short *)np) + 1);
679 if (b2 & 0x6cf)
680 ND_PRINT((ndo, " [b2&3=0x%x]", b2));
681
682 if (DNS_OPCODE(np) == IQUERY) {
683 if (qdcount)
684 ND_PRINT((ndo, " [%dq]", qdcount));
685 if (ancount != 1)
686 ND_PRINT((ndo, " [%da]", ancount));
687 }
688 else {
689 if (ancount)
690 ND_PRINT((ndo, " [%da]", ancount));
691 if (qdcount != 1)
692 ND_PRINT((ndo, " [%dq]", qdcount));
693 }
694 if (nscount)
695 ND_PRINT((ndo, " [%dn]", nscount));
696 if (arcount)
697 ND_PRINT((ndo, " [%dau]", arcount));
698
699 cp = (const u_char *)(np + 1);
700 if (qdcount--) {
701 cp = ns_qprint(ndo, cp, (const u_char *)np, is_mdns);
702 if (!cp)
703 goto trunc;
704 while (cp < ndo->ndo_snapend && qdcount--) {
705 cp = ns_qprint(ndo, (const u_char *)cp,
706 (const u_char *)np,
707 is_mdns);
708 if (!cp)
709 goto trunc;
710 }
711 }
712 if (qdcount > 0)
713 goto trunc;
714
715 /* Print remaining sections on -vv */
716 if (ndo->ndo_vflag > 1) {
717 if (ancount--) {
718 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
719 goto trunc;
720 while (cp < ndo->ndo_snapend && ancount--) {
721 ND_PRINT((ndo, ","));
722 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
723 goto trunc;
724 }
725 }
726 if (ancount > 0)
727 goto trunc;
728 if (cp < ndo->ndo_snapend && nscount--) {
729 ND_PRINT((ndo, " ns:"));
730 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
731 goto trunc;
732 while (nscount-- && cp < ndo->ndo_snapend) {
733 ND_PRINT((ndo, ","));
734 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
735 goto trunc;
736 }
737 }
738 if (nscount > 0)
739 goto trunc;
740 if (cp < ndo->ndo_snapend && arcount--) {
741 ND_PRINT((ndo, " ar:"));
742 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
743 goto trunc;
744 while (cp < ndo->ndo_snapend && arcount--) {
745 ND_PRINT((ndo, ","));
746 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
747 goto trunc;
748 }
749 }
750 if (arcount > 0)
751 goto trunc;
752 }
753 }
754 ND_PRINT((ndo, " (%d)", length));
755 return;
756
757 trunc:
758 ND_PRINT((ndo, "[|domain]"));
759 }