]> The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
Making "extracted_ethertype" static to "print-ether.c" broke other
[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 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.55 2000-10-10 05:05:07 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33
34 #include <netinet/in.h>
35
36 #ifdef NOERROR
37 #undef NOERROR /* Solaris sucks */
38 #endif
39 #ifdef NOERROR
40 #undef T_UNSPEC /* SINIX does too */
41 #endif
42 #include "nameser.h"
43
44 #include <stdio.h>
45 #include <string.h>
46
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "extract.h" /* must come after interface.h */
50
51 /* Compatibility */
52 #ifndef T_TXT
53 #define T_TXT 16 /* text strings */
54 #endif
55 #ifndef T_RP
56 #define T_RP 17 /* responsible person */
57 #endif
58 #ifndef T_AFSDB
59 #define T_AFSDB 18 /* AFS cell database */
60 #endif
61 #ifndef T_X25
62 #define T_X25 19 /* X_25 calling address */
63 #endif
64 #ifndef T_ISDN
65 #define T_ISDN 20 /* ISDN calling address */
66 #endif
67 #ifndef T_RT
68 #define T_RT 21 /* router */
69 #endif
70 #ifndef T_NSAP
71 #define T_NSAP 22 /* NSAP address */
72 #endif
73 #ifndef T_NSAP_PTR
74 #define T_NSAP_PTR 23 /* reverse NSAP lookup (deprecated) */
75 #endif
76 #ifndef T_SIG
77 #define T_SIG 24 /* security signature */
78 #endif
79 #ifndef T_KEY
80 #define T_KEY 25 /* security key */
81 #endif
82 #ifndef T_PX
83 #define T_PX 26 /* X.400 mail mapping */
84 #endif
85 #ifndef T_GPOS
86 #define T_GPOS 27 /* geographical position (withdrawn) */
87 #endif
88 #ifndef T_AAAA
89 #define T_AAAA 28 /* IP6 Address */
90 #endif
91 #ifndef T_LOC
92 #define T_LOC 29 /* Location Information */
93 #endif
94 #ifndef T_NXT
95 #define T_NXT 30 /* Next Valid Name in Zone */
96 #endif
97 #ifndef T_EID
98 #define T_EID 31 /* Endpoint identifier */
99 #endif
100 #ifndef T_NIMLOC
101 #define T_NIMLOC 32 /* Nimrod locator */
102 #endif
103 #ifndef T_SRV
104 #define T_SRV 33 /* Server selection */
105 #endif
106 #ifndef T_ATMA
107 #define T_ATMA 34 /* ATM Address */
108 #endif
109 #ifndef T_NAPTR
110 #define T_NAPTR 35 /* Naming Authority PoinTeR */
111 #endif
112 #ifndef T_A6
113 #define T_A6 38 /* IP6 address (ipngwg-dns-lookups) */
114 #endif
115
116 #ifndef T_UNSPEC
117 #define T_UNSPEC 103 /* Unspecified format (binary data) */
118 #endif
119 #ifndef T_UNSPECA
120 #define T_UNSPECA 104 /* "unspecified ascii". Ugly MIT hack */
121 #endif
122
123 #ifndef C_CHAOS
124 #define C_CHAOS 3 /* for chaos net (MIT) */
125 #endif
126 #ifndef C_HS
127 #define C_HS 4 /* for Hesiod name server (MIT) (XXX) */
128 #endif
129
130 static char *ns_ops[] = {
131 "", " inv_q", " stat", " op3", " notify", " op5", " op6", " op7",
132 " op8", " updataA", " updateD", " updateDA",
133 " updateM", " updateMA", " zoneInit", " zoneRef",
134 };
135
136 static char *ns_resp[] = {
137 "", " FormErr", " ServFail", " NXDomain",
138 " NotImp", " Refused", " Resp6", " Resp7",
139 " Resp8", " Resp9", " Resp10", " Resp11",
140 " Resp12", " Resp13", " Resp14", " NoChange",
141 };
142
143 /* skip over a domain name */
144 static const u_char *
145 ns_nskip(register const u_char *cp, register const u_char *bp)
146 {
147 register u_char i;
148
149 if (((i = *cp++) & INDIR_MASK) == INDIR_MASK)
150 return (cp + 1);
151 while (i && cp < snapend) {
152 cp += i;
153 i = *cp++;
154 }
155 return (cp);
156 }
157
158 /* print a <domain-name> */
159 static const u_char *
160 ns_nprint(register const u_char *cp, register const u_char *bp)
161 {
162 register u_int i;
163 register const u_char *rp;
164 register int compress;
165 int chars_processed;
166 int data_size = snapend - bp;
167
168 i = *cp++;
169 chars_processed = 1;
170 rp = cp + i;
171 if ((i & INDIR_MASK) == INDIR_MASK) {
172 rp = cp + 1;
173 compress = 1;
174 } else
175 compress = 0;
176 if (i != 0)
177 while (i && cp < snapend) {
178 if ((i & INDIR_MASK) == INDIR_MASK) {
179 cp = bp + (((i << 8) | *cp) & 0x3fff);
180 i = *cp++;
181 chars_processed++;
182
183 /*
184 * If we've looked at every character in
185 * the message, this pointer will make
186 * us look at some character again,
187 * which means we're looping.
188 */
189 if (chars_processed >= data_size) {
190 fn_printn(cp, 6, (u_char *)"<LOOP>");
191 if (!compress)
192 rp += i + 1;
193 return (rp);
194 }
195 continue;
196 }
197 if (fn_printn(cp, i, snapend))
198 break;
199 cp += i;
200 chars_processed += i;
201 putchar('.');
202 i = *cp++;
203 chars_processed++;
204 if (!compress)
205 rp += i + 1;
206 }
207 else
208 putchar('.');
209 return (rp);
210 }
211
212 /* print a <character-string> */
213 static const u_char *
214 ns_cprint(register const u_char *cp, register const u_char *bp)
215 {
216 register u_int i;
217
218 i = *cp++;
219 (void)fn_printn(cp, i, snapend);
220 return (cp + i);
221 }
222
223 static struct tok type2str[] = {
224 { T_A, "A" },
225 { T_NS, "NS" },
226 { T_MD, "MD" },
227 { T_MF, "MF" },
228 { T_CNAME, "CNAME" },
229 { T_SOA, "SOA" },
230 { T_MB, "MB" },
231 { T_MG, "MG" },
232 { T_MR, "MR" },
233 { T_NULL, "NULL" },
234 { T_WKS, "WKS" },
235 { T_PTR, "PTR" },
236 { T_HINFO, "HINFO" },
237 { T_MINFO, "MINFO" },
238 { T_MX, "MX" },
239 { T_TXT, "TXT" },
240 { T_RP, "RP" },
241 { T_AFSDB, "AFSDB" },
242 { T_X25, "X25" },
243 { T_ISDN, "ISDN" },
244 { T_RT, "RT" },
245 { T_NSAP, "NSAP" },
246 { T_NSAP_PTR, "NSAP_PTR" },
247 { T_SIG, "SIG" },
248 { T_KEY, "KEY" },
249 { T_PX, "PX" },
250 { T_GPOS, "GPOS" },
251 { T_AAAA, "AAAA" },
252 { T_LOC, "LOC " },
253 { T_NXT, "NXT " },
254 { T_EID, "EID " },
255 { T_NIMLOC, "NIMLOC " },
256 { T_SRV, "SRV " },
257 { T_ATMA, "ATMA " },
258 { T_NAPTR, "NAPTR " },
259 { T_A6, "A6 " },
260 #ifndef T_UINFO
261 #define T_UINFO 100
262 #endif
263 { T_UINFO, "UINFO" },
264 #ifndef T_UID
265 #define T_UID 101
266 #endif
267 { T_UID, "UID" },
268 #ifndef T_GID
269 #define T_GID 102
270 #endif
271 { T_GID, "GID" },
272 { T_UNSPEC, "UNSPEC" },
273 { T_UNSPECA, "UNSPECA" },
274 { T_AXFR, "AXFR" },
275 { T_MAILB, "MAILB" },
276 { T_MAILA, "MAILA" },
277 { T_ANY, "ANY" },
278 { 0, NULL }
279 };
280
281 static struct tok class2str[] = {
282 { C_IN, "IN" }, /* Not used */
283 { C_CHAOS, "CHAOS)" },
284 { C_HS, "HS" },
285 { C_ANY, "ANY" },
286 { 0, NULL }
287 };
288
289 /* print a query */
290 static void
291 ns_qprint(register const u_char *cp, register const u_char *bp)
292 {
293 register const u_char *np = cp;
294 register u_int i;
295
296 cp = ns_nskip(cp, bp);
297
298 if (cp + 4 > snapend)
299 return;
300
301 /* print the qtype and qclass (if it's not IN) */
302 i = *cp++ << 8;
303 i |= *cp++;
304 printf(" %s", tok2str(type2str, "Type%d", i));
305 i = *cp++ << 8;
306 i |= *cp++;
307 if (i != C_IN)
308 printf(" %s", tok2str(class2str, "(Class %d)", i));
309
310 fputs("? ", stdout);
311 ns_nprint(np, bp);
312 }
313
314 /* print a reply */
315 static const u_char *
316 ns_rprint(register const u_char *cp, register const u_char *bp)
317 {
318 register u_int i;
319 register u_short typ, len;
320 register const u_char *rp;
321
322 if (vflag) {
323 putchar(' ');
324 cp = ns_nprint(cp, bp);
325 } else
326 cp = ns_nskip(cp, bp);
327
328 if (cp + 10 > snapend)
329 return (snapend);
330
331 /* print the type/qtype and class (if it's not IN) */
332 typ = *cp++ << 8;
333 typ |= *cp++;
334 i = *cp++ << 8;
335 i |= *cp++;
336 if (i != C_IN)
337 printf(" %s", tok2str(class2str, "(Class %d)", i));
338
339 /* ignore ttl */
340 cp += 4;
341
342 len = *cp++ << 8;
343 len |= *cp++;
344
345 rp = cp + len;
346
347 printf(" %s", tok2str(type2str, "Type%d", typ));
348 switch (typ) {
349
350 case T_A:
351 printf(" %s", ipaddr_string(cp));
352 break;
353
354 case T_NS:
355 case T_CNAME:
356 case T_PTR:
357 #ifdef T_DNAME
358 case T_DNAME: /*XXX not checked as there's no server support yet*/
359 #endif
360 putchar(' ');
361 (void)ns_nprint(cp, bp);
362 break;
363
364 case T_MX:
365 putchar(' ');
366 (void)ns_nprint(cp + 2, bp);
367 printf(" %d", EXTRACT_16BITS(cp));
368 break;
369
370 case T_TXT:
371 putchar(' ');
372 (void)ns_cprint(cp, bp);
373 break;
374
375 #ifdef INET6
376 case T_AAAA:
377 printf(" %s", ip6addr_string(cp));
378 break;
379
380 case T_A6: /*XXX not checked as there's no server support yet*/
381 {
382 struct in6_addr a;
383 int pbyte;
384
385 pbyte = (*cp + 7) / 8;
386 memset(&a, 0, sizeof(a));
387 memcpy(&a, cp + 1, pbyte);
388 printf(" %u %s ", *cp, ip6addr_string(&a));
389 (void)ns_nprint(cp + 1 + pbyte, bp);
390 break;
391 }
392 #endif /*INET6*/
393
394 case T_UNSPECA: /* One long string */
395 printf(" %.*s", len, cp);
396 break;
397 }
398 return (rp); /* XXX This isn't always right */
399 }
400
401 void
402 ns_print(register const u_char *bp, u_int length)
403 {
404 register const HEADER *np;
405 register int qdcount, ancount, nscount, arcount;
406 register const u_char *cp;
407
408 np = (const HEADER *)bp;
409 /* get the byte-order right */
410 qdcount = ntohs(np->qdcount);
411 ancount = ntohs(np->ancount);
412 nscount = ntohs(np->nscount);
413 arcount = ntohs(np->arcount);
414
415 if (DNS_QR(np)) {
416 /* this is a response */
417 printf(" %d%s%s%s%s%s%s",
418 ntohs(np->id),
419 ns_ops[DNS_OPCODE(np)],
420 ns_resp[DNS_RCODE(np)],
421 DNS_AA(np)? "*" : "",
422 DNS_RA(np)? "" : "-",
423 DNS_TC(np)? "|" : "",
424 DNS_CD(np)? "%" : "");
425
426 if (qdcount != 1)
427 printf(" [%dq]", qdcount);
428 /* Print QUESTION section on -vv */
429 if (vflag > 1) {
430 fputs(" q: ", stdout);
431 cp = ns_nprint((const u_char *)(np + 1), bp);
432 } else
433 cp = ns_nskip((const u_char *)(np + 1), bp);
434 printf(" %d/%d/%d", ancount, nscount, arcount);
435 if (ancount--) {
436 cp = ns_rprint(cp + 4, bp);
437 while (ancount-- && cp < snapend) {
438 putchar(',');
439 cp = ns_rprint(cp, bp);
440 }
441 }
442 }
443 else {
444 /* this is a request */
445 printf(" %d%s%s%s",
446 ntohs(np->id),
447 ns_ops[DNS_OPCODE(np)],
448 DNS_RD(np)? "+" : "",
449 DNS_AD(np)? "$" : "");
450
451 /* any weirdness? */
452 if (*(((u_short *)np)+1) & htons(0x6cf))
453 printf(" [b2&3=0x%x]", ntohs(*(((u_short *)np)+1)));
454
455 if (DNS_OPCODE(np) == IQUERY) {
456 if (qdcount)
457 printf(" [%dq]", qdcount);
458 if (ancount != 1)
459 printf(" [%da]", ancount);
460 }
461 else {
462 if (ancount)
463 printf(" [%da]", ancount);
464 if (qdcount != 1)
465 printf(" [%dq]", qdcount);
466 }
467 if (nscount)
468 printf(" [%dn]", nscount);
469 if (arcount)
470 printf(" [%dau]", arcount);
471
472 ns_qprint((const u_char *)(np + 1), (const u_char *)np);
473 }
474 printf(" (%d)", length);
475 }