print-domain.c 18 KB

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