]> The Tcpdump Group git mirrors - tcpdump/blob - print-lwres.c
remove tcpdump's own CVS keywords
[tcpdump] / print-lwres.c
1 /*
2 * Copyright (C) 2001 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <tcpdump-stdinc.h>
35
36 #include "nameser.h"
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h" /* must come after interface.h */
44
45 /* BIND9 lib/lwres/include/lwres */
46 typedef u_int32_t lwres_uint32_t;
47 typedef u_int16_t lwres_uint16_t;
48 typedef u_int8_t lwres_uint8_t;
49
50 struct lwres_lwpacket {
51 lwres_uint32_t length;
52 lwres_uint16_t version;
53 lwres_uint16_t pktflags;
54 lwres_uint32_t serial;
55 lwres_uint32_t opcode;
56 lwres_uint32_t result;
57 lwres_uint32_t recvlength;
58 lwres_uint16_t authtype;
59 lwres_uint16_t authlength;
60 };
61
62 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
63
64 #define LWRES_LWPACKETVERSION_0 0
65
66 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
67 #define LWRES_FLAG_SECUREDATA 0x00000002U
68
69 /*
70 * no-op
71 */
72 #define LWRES_OPCODE_NOOP 0x00000000U
73
74 typedef struct {
75 /* public */
76 lwres_uint16_t datalength;
77 /* data follows */
78 } lwres_nooprequest_t;
79
80 typedef struct {
81 /* public */
82 lwres_uint16_t datalength;
83 /* data follows */
84 } lwres_noopresponse_t;
85
86 /*
87 * get addresses by name
88 */
89 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
90
91 typedef struct lwres_addr lwres_addr_t;
92
93 struct lwres_addr {
94 lwres_uint32_t family;
95 lwres_uint16_t length;
96 /* address folows */
97 };
98
99 typedef struct {
100 /* public */
101 lwres_uint32_t flags;
102 lwres_uint32_t addrtypes;
103 lwres_uint16_t namelen;
104 /* name follows */
105 } lwres_gabnrequest_t;
106
107 typedef struct {
108 /* public */
109 lwres_uint32_t flags;
110 lwres_uint16_t naliases;
111 lwres_uint16_t naddrs;
112 lwres_uint16_t realnamelen;
113 /* aliases follows */
114 /* addrs follows */
115 /* realname follows */
116 } lwres_gabnresponse_t;
117
118 /*
119 * get name by address
120 */
121 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
122 typedef struct {
123 /* public */
124 lwres_uint32_t flags;
125 lwres_addr_t addr;
126 /* addr body follows */
127 } lwres_gnbarequest_t;
128
129 typedef struct {
130 /* public */
131 lwres_uint32_t flags;
132 lwres_uint16_t naliases;
133 lwres_uint16_t realnamelen;
134 /* aliases follows */
135 /* realname follows */
136 } lwres_gnbaresponse_t;
137
138 /*
139 * get rdata by name
140 */
141 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
142
143 typedef struct {
144 /* public */
145 lwres_uint32_t flags;
146 lwres_uint16_t rdclass;
147 lwres_uint16_t rdtype;
148 lwres_uint16_t namelen;
149 /* name follows */
150 } lwres_grbnrequest_t;
151
152 typedef struct {
153 /* public */
154 lwres_uint32_t flags;
155 lwres_uint16_t rdclass;
156 lwres_uint16_t rdtype;
157 lwres_uint32_t ttl;
158 lwres_uint16_t nrdatas;
159 lwres_uint16_t nsigs;
160 /* realname here (len + name) */
161 /* rdata here (len + name) */
162 /* signatures here (len + name) */
163 } lwres_grbnresponse_t;
164
165 #define LWRDATA_VALIDATED 0x00000001
166
167 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
168 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
169
170 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
171 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
172
173 static const struct tok opcode[] = {
174 { LWRES_OPCODE_NOOP, "noop", },
175 { LWRES_OPCODE_GETADDRSBYNAME, "getaddrsbyname", },
176 { LWRES_OPCODE_GETNAMEBYADDR, "getnamebyaddr", },
177 { LWRES_OPCODE_GETRDATABYNAME, "getrdatabyname", },
178 { 0, NULL, },
179 };
180
181 /* print-domain.c */
182 extern const struct tok ns_type2str[];
183 extern const struct tok ns_class2str[];
184
185 static int lwres_printname(size_t, const char *);
186 static int lwres_printnamelen(const char *);
187 static int lwres_printbinlen(const char *);
188 static int lwres_printaddr(lwres_addr_t *);
189
190 static int
191 lwres_printname(size_t l, const char *p0)
192 {
193 const char *p;
194 size_t i;
195
196 p = p0;
197 /* + 1 for terminating \0 */
198 if (p + l + 1 > (const char *)snapend)
199 goto trunc;
200
201 printf(" ");
202 for (i = 0; i < l; i++)
203 safeputchar(*p++);
204 p++; /* skip terminating \0 */
205
206 return p - p0;
207
208 trunc:
209 return -1;
210 }
211
212 static int
213 lwres_printnamelen(const char *p)
214 {
215 u_int16_t l;
216 int advance;
217
218 if (p + 2 > (const char *)snapend)
219 goto trunc;
220 l = EXTRACT_16BITS(p);
221 advance = lwres_printname(l, p + 2);
222 if (advance < 0)
223 goto trunc;
224 return 2 + advance;
225
226 trunc:
227 return -1;
228 }
229
230 static int
231 lwres_printbinlen(const char *p0)
232 {
233 const char *p;
234 u_int16_t l;
235 int i;
236
237 p = p0;
238 if (p + 2 > (const char *)snapend)
239 goto trunc;
240 l = EXTRACT_16BITS(p);
241 if (p + 2 + l > (const char *)snapend)
242 goto trunc;
243 p += 2;
244 for (i = 0; i < l; i++)
245 printf("%02x", *p++);
246 return p - p0;
247
248 trunc:
249 return -1;
250 }
251
252 static int
253 lwres_printaddr(lwres_addr_t *ap)
254 {
255 u_int16_t l;
256 const char *p;
257 int i;
258
259 TCHECK(ap->length);
260 l = EXTRACT_16BITS(&ap->length);
261 /* XXX ap points to packed struct */
262 p = (const char *)&ap->length + sizeof(ap->length);
263 TCHECK2(*p, l);
264
265 switch (EXTRACT_32BITS(&ap->family)) {
266 case 1: /* IPv4 */
267 if (l < 4)
268 return -1;
269 printf(" %s", ipaddr_string(p));
270 p += sizeof(struct in_addr);
271 break;
272 #ifdef INET6
273 case 2: /* IPv6 */
274 if (l < 16)
275 return -1;
276 printf(" %s", ip6addr_string(p));
277 p += sizeof(struct in6_addr);
278 break;
279 #endif
280 default:
281 printf(" %u/", EXTRACT_32BITS(&ap->family));
282 for (i = 0; i < l; i++)
283 printf("%02x", *p++);
284 }
285
286 return p - (const char *)ap;
287
288 trunc:
289 return -1;
290 }
291
292 void
293 lwres_print(register const u_char *bp, u_int length)
294 {
295 const struct lwres_lwpacket *np;
296 u_int32_t v;
297 const char *s;
298 int response;
299 int advance;
300 int unsupported = 0;
301
302 np = (const struct lwres_lwpacket *)bp;
303 TCHECK(np->authlength);
304
305 printf(" lwres");
306 v = EXTRACT_16BITS(&np->version);
307 if (vflag || v != LWRES_LWPACKETVERSION_0)
308 printf(" v%u", v);
309 if (v != LWRES_LWPACKETVERSION_0) {
310 s = (const char *)np + EXTRACT_32BITS(&np->length);
311 goto tail;
312 }
313
314 response = EXTRACT_16BITS(&np->pktflags) & LWRES_LWPACKETFLAG_RESPONSE;
315
316 /* opcode and pktflags */
317 v = EXTRACT_32BITS(&np->opcode);
318 s = tok2str(opcode, "#0x%x", v);
319 printf(" %s%s", s, response ? "" : "?");
320
321 /* pktflags */
322 v = EXTRACT_16BITS(&np->pktflags);
323 if (v & ~LWRES_LWPACKETFLAG_RESPONSE)
324 printf("[0x%x]", v);
325
326 if (vflag > 1) {
327 printf(" ("); /*)*/
328 printf("serial:0x%x", EXTRACT_32BITS(&np->serial));
329 printf(" result:0x%x", EXTRACT_32BITS(&np->result));
330 printf(" recvlen:%u", EXTRACT_32BITS(&np->recvlength));
331 /* BIND910: not used */
332 if (vflag > 2) {
333 printf(" authtype:0x%x", EXTRACT_16BITS(&np->authtype));
334 printf(" authlen:%u", EXTRACT_16BITS(&np->authlength));
335 }
336 /*(*/
337 printf(")");
338 }
339
340 /* per-opcode content */
341 if (!response) {
342 /*
343 * queries
344 */
345 lwres_gabnrequest_t *gabn;
346 lwres_gnbarequest_t *gnba;
347 lwres_grbnrequest_t *grbn;
348 u_int32_t l;
349
350 gabn = NULL;
351 gnba = NULL;
352 grbn = NULL;
353
354 switch (EXTRACT_32BITS(&np->opcode)) {
355 case LWRES_OPCODE_NOOP:
356 break;
357 case LWRES_OPCODE_GETADDRSBYNAME:
358 gabn = (lwres_gabnrequest_t *)(np + 1);
359 TCHECK(gabn->namelen);
360 /* XXX gabn points to packed struct */
361 s = (const char *)&gabn->namelen +
362 sizeof(gabn->namelen);
363 l = EXTRACT_16BITS(&gabn->namelen);
364
365 /* BIND910: not used */
366 if (vflag > 2) {
367 printf(" flags:0x%x",
368 EXTRACT_32BITS(&gabn->flags));
369 }
370
371 v = EXTRACT_32BITS(&gabn->addrtypes);
372 switch (v & (LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6)) {
373 case LWRES_ADDRTYPE_V4:
374 printf(" IPv4");
375 break;
376 case LWRES_ADDRTYPE_V6:
377 printf(" IPv6");
378 break;
379 case LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6:
380 printf(" IPv4/6");
381 break;
382 }
383 if (v & ~(LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6))
384 printf("[0x%x]", v);
385
386 advance = lwres_printname(l, s);
387 if (advance < 0)
388 goto trunc;
389 s += advance;
390 break;
391 case LWRES_OPCODE_GETNAMEBYADDR:
392 gnba = (lwres_gnbarequest_t *)(np + 1);
393 TCHECK(gnba->addr);
394
395 /* BIND910: not used */
396 if (vflag > 2) {
397 printf(" flags:0x%x",
398 EXTRACT_32BITS(&gnba->flags));
399 }
400
401 s = (const char *)&gnba->addr;
402
403 advance = lwres_printaddr(&gnba->addr);
404 if (advance < 0)
405 goto trunc;
406 s += advance;
407 break;
408 case LWRES_OPCODE_GETRDATABYNAME:
409 /* XXX no trace, not tested */
410 grbn = (lwres_grbnrequest_t *)(np + 1);
411 TCHECK(grbn->namelen);
412
413 /* BIND910: not used */
414 if (vflag > 2) {
415 printf(" flags:0x%x",
416 EXTRACT_32BITS(&grbn->flags));
417 }
418
419 printf(" %s", tok2str(ns_type2str, "Type%d",
420 EXTRACT_16BITS(&grbn->rdtype)));
421 if (EXTRACT_16BITS(&grbn->rdclass) != C_IN) {
422 printf(" %s", tok2str(ns_class2str, "Class%d",
423 EXTRACT_16BITS(&grbn->rdclass)));
424 }
425
426 /* XXX grbn points to packed struct */
427 s = (const char *)&grbn->namelen +
428 sizeof(grbn->namelen);
429 l = EXTRACT_16BITS(&grbn->namelen);
430
431 advance = lwres_printname(l, s);
432 if (advance < 0)
433 goto trunc;
434 s += advance;
435 break;
436 default:
437 unsupported++;
438 break;
439 }
440 } else {
441 /*
442 * responses
443 */
444 lwres_gabnresponse_t *gabn;
445 lwres_gnbaresponse_t *gnba;
446 lwres_grbnresponse_t *grbn;
447 u_int32_t l, na;
448 u_int32_t i;
449
450 gabn = NULL;
451 gnba = NULL;
452 grbn = NULL;
453
454 switch (EXTRACT_32BITS(&np->opcode)) {
455 case LWRES_OPCODE_NOOP:
456 break;
457 case LWRES_OPCODE_GETADDRSBYNAME:
458 gabn = (lwres_gabnresponse_t *)(np + 1);
459 TCHECK(gabn->realnamelen);
460 /* XXX gabn points to packed struct */
461 s = (const char *)&gabn->realnamelen +
462 sizeof(gabn->realnamelen);
463 l = EXTRACT_16BITS(&gabn->realnamelen);
464
465 /* BIND910: not used */
466 if (vflag > 2) {
467 printf(" flags:0x%x",
468 EXTRACT_32BITS(&gabn->flags));
469 }
470
471 printf(" %u/%u", EXTRACT_16BITS(&gabn->naliases),
472 EXTRACT_16BITS(&gabn->naddrs));
473
474 advance = lwres_printname(l, s);
475 if (advance < 0)
476 goto trunc;
477 s += advance;
478
479 /* aliases */
480 na = EXTRACT_16BITS(&gabn->naliases);
481 for (i = 0; i < na; i++) {
482 advance = lwres_printnamelen(s);
483 if (advance < 0)
484 goto trunc;
485 s += advance;
486 }
487
488 /* addrs */
489 na = EXTRACT_16BITS(&gabn->naddrs);
490 for (i = 0; i < na; i++) {
491 advance = lwres_printaddr((lwres_addr_t *)s);
492 if (advance < 0)
493 goto trunc;
494 s += advance;
495 }
496 break;
497 case LWRES_OPCODE_GETNAMEBYADDR:
498 gnba = (lwres_gnbaresponse_t *)(np + 1);
499 TCHECK(gnba->realnamelen);
500 /* XXX gnba points to packed struct */
501 s = (const char *)&gnba->realnamelen +
502 sizeof(gnba->realnamelen);
503 l = EXTRACT_16BITS(&gnba->realnamelen);
504
505 /* BIND910: not used */
506 if (vflag > 2) {
507 printf(" flags:0x%x",
508 EXTRACT_32BITS(&gnba->flags));
509 }
510
511 printf(" %u", EXTRACT_16BITS(&gnba->naliases));
512
513 advance = lwres_printname(l, s);
514 if (advance < 0)
515 goto trunc;
516 s += advance;
517
518 /* aliases */
519 na = EXTRACT_16BITS(&gnba->naliases);
520 for (i = 0; i < na; i++) {
521 advance = lwres_printnamelen(s);
522 if (advance < 0)
523 goto trunc;
524 s += advance;
525 }
526 break;
527 case LWRES_OPCODE_GETRDATABYNAME:
528 /* XXX no trace, not tested */
529 grbn = (lwres_grbnresponse_t *)(np + 1);
530 TCHECK(grbn->nsigs);
531
532 /* BIND910: not used */
533 if (vflag > 2) {
534 printf(" flags:0x%x",
535 EXTRACT_32BITS(&grbn->flags));
536 }
537
538 printf(" %s", tok2str(ns_type2str, "Type%d",
539 EXTRACT_16BITS(&grbn->rdtype)));
540 if (EXTRACT_16BITS(&grbn->rdclass) != C_IN) {
541 printf(" %s", tok2str(ns_class2str, "Class%d",
542 EXTRACT_16BITS(&grbn->rdclass)));
543 }
544 printf(" TTL ");
545 relts_print(EXTRACT_32BITS(&grbn->ttl));
546 printf(" %u/%u", EXTRACT_16BITS(&grbn->nrdatas),
547 EXTRACT_16BITS(&grbn->nsigs));
548
549 /* XXX grbn points to packed struct */
550 s = (const char *)&grbn->nsigs+ sizeof(grbn->nsigs);
551
552 advance = lwres_printnamelen(s);
553 if (advance < 0)
554 goto trunc;
555 s += advance;
556
557 /* rdatas */
558 na = EXTRACT_16BITS(&grbn->nrdatas);
559 for (i = 0; i < na; i++) {
560 /* XXX should decode resource data */
561 advance = lwres_printbinlen(s);
562 if (advance < 0)
563 goto trunc;
564 s += advance;
565 }
566
567 /* sigs */
568 na = EXTRACT_16BITS(&grbn->nsigs);
569 for (i = 0; i < na; i++) {
570 /* XXX how should we print it? */
571 advance = lwres_printbinlen(s);
572 if (advance < 0)
573 goto trunc;
574 s += advance;
575 }
576 break;
577 default:
578 unsupported++;
579 break;
580 }
581 }
582
583 tail:
584 /* length mismatch */
585 if (EXTRACT_32BITS(&np->length) != length) {
586 printf(" [len: %u != %u]", EXTRACT_32BITS(&np->length),
587 length);
588 }
589 if (!unsupported && s < (const char *)np + EXTRACT_32BITS(&np->length))
590 printf("[extra]");
591 return;
592
593 trunc:
594 printf("[|lwres]");
595 return;
596 }