2 * Copyright (C) 2001 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 static const char rcsid
[] _U_
=
32 "@(#) $Header: /tcpdump/master/tcpdump/print-lwres.c,v 1.13 2004-03-24 01:54:29 guy Exp $ (LBL)";
39 #include <tcpdump-stdinc.h>
46 #include "interface.h"
47 #include "addrtoname.h"
48 #include "extract.h" /* must come after interface.h */
50 /* BIND9 lib/lwres/include/lwres */
51 typedef u_int32_t lwres_uint32_t
;
52 typedef u_int16_t lwres_uint16_t
;
53 typedef u_int8_t lwres_uint8_t
;
55 struct lwres_lwpacket
{
56 lwres_uint32_t length
;
57 lwres_uint16_t version
;
58 lwres_uint16_t pktflags
;
59 lwres_uint32_t serial
;
60 lwres_uint32_t opcode
;
61 lwres_uint32_t result
;
62 lwres_uint32_t recvlength
;
63 lwres_uint16_t authtype
;
64 lwres_uint16_t authlength
;
67 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
69 #define LWRES_LWPACKETVERSION_0 0
71 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
72 #define LWRES_FLAG_SECUREDATA 0x00000002U
77 #define LWRES_OPCODE_NOOP 0x00000000U
81 lwres_uint16_t datalength
;
83 } lwres_nooprequest_t
;
87 lwres_uint16_t datalength
;
89 } lwres_noopresponse_t
;
92 * get addresses by name
94 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
96 typedef struct lwres_addr lwres_addr_t
;
99 lwres_uint32_t family
;
100 lwres_uint16_t length
;
106 lwres_uint32_t flags
;
107 lwres_uint32_t addrtypes
;
108 lwres_uint16_t namelen
;
110 } lwres_gabnrequest_t
;
114 lwres_uint32_t flags
;
115 lwres_uint16_t naliases
;
116 lwres_uint16_t naddrs
;
117 lwres_uint16_t realnamelen
;
118 /* aliases follows */
120 /* realname follows */
121 } lwres_gabnresponse_t
;
124 * get name by address
126 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
129 lwres_uint32_t flags
;
131 /* addr body follows */
132 } lwres_gnbarequest_t
;
136 lwres_uint32_t flags
;
137 lwres_uint16_t naliases
;
138 lwres_uint16_t realnamelen
;
139 /* aliases follows */
140 /* realname follows */
141 } lwres_gnbaresponse_t
;
146 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
150 lwres_uint32_t flags
;
151 lwres_uint16_t rdclass
;
152 lwres_uint16_t rdtype
;
153 lwres_uint16_t namelen
;
155 } lwres_grbnrequest_t
;
159 lwres_uint32_t flags
;
160 lwres_uint16_t rdclass
;
161 lwres_uint16_t rdtype
;
163 lwres_uint16_t nrdatas
;
164 lwres_uint16_t nsigs
;
165 /* realname here (len + name) */
166 /* rdata here (len + name) */
167 /* signatures here (len + name) */
168 } lwres_grbnresponse_t
;
170 #define LWRDATA_VALIDATED 0x00000001
172 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
173 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
175 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
176 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
178 struct tok opcode
[] = {
179 { LWRES_OPCODE_NOOP
, "noop", },
180 { LWRES_OPCODE_GETADDRSBYNAME
, "getaddrsbyname", },
181 { LWRES_OPCODE_GETNAMEBYADDR
, "getnamebyaddr", },
182 { LWRES_OPCODE_GETRDATABYNAME
, "getrdatabyname", },
187 extern struct tok ns_type2str
[];
188 extern struct tok ns_class2str
[];
190 static int lwres_printname(size_t, const char *);
191 static int lwres_printnamelen(const char *);
192 static int lwres_printbinlen(const char *);
193 static int lwres_printaddr(lwres_addr_t
*);
196 lwres_printname(size_t l
, const char *p0
)
202 /* + 1 for terminating \0 */
203 if (p
+ l
+ 1 > (const char *)snapend
)
207 for (i
= 0; i
< l
; i
++)
209 p
++; /* skip terminating \0 */
218 lwres_printnamelen(const char *p
)
223 if (p
+ 2 > (const char *)snapend
)
225 l
= EXTRACT_16BITS(p
);
226 advance
= lwres_printname(l
, p
+ 2);
236 lwres_printbinlen(const char *p0
)
243 if (p
+ 2 > (const char *)snapend
)
245 l
= EXTRACT_16BITS(p
);
246 if (p
+ 2 + l
> (const char *)snapend
)
249 for (i
= 0; i
< l
; i
++)
250 printf("%02x", *p
++);
258 lwres_printaddr(lwres_addr_t
*ap
)
265 l
= EXTRACT_16BITS(&ap
->length
);
266 /* XXX ap points to packed struct */
267 p
= (const char *)&ap
->length
+ sizeof(ap
->length
);
270 switch (EXTRACT_32BITS(&ap
->family
)) {
274 printf(" %s", ipaddr_string(p
));
275 p
+= sizeof(struct in_addr
);
281 printf(" %s", ip6addr_string(p
));
282 p
+= sizeof(struct in6_addr
);
286 printf(" %u/", EXTRACT_32BITS(&ap
->family
));
287 for (i
= 0; i
< l
; i
++)
288 printf("%02x", *p
++);
291 return p
- (const char *)ap
;
298 lwres_print(register const u_char
*bp
, u_int length
)
300 const struct lwres_lwpacket
*np
;
307 np
= (const struct lwres_lwpacket
*)bp
;
308 TCHECK(np
->authlength
);
311 v
= EXTRACT_16BITS(&np
->version
);
312 if (vflag
|| v
!= LWRES_LWPACKETVERSION_0
)
314 if (v
!= LWRES_LWPACKETVERSION_0
) {
315 s
= (const char *)np
+ EXTRACT_32BITS(&np
->length
);
319 response
= EXTRACT_16BITS(&np
->pktflags
) & LWRES_LWPACKETFLAG_RESPONSE
;
321 /* opcode and pktflags */
322 v
= EXTRACT_32BITS(&np
->opcode
);
323 s
= tok2str(opcode
, "#0x%x", v
);
324 printf(" %s%s", s
, response
? "" : "?");
327 v
= EXTRACT_16BITS(&np
->pktflags
);
328 if (v
& ~LWRES_LWPACKETFLAG_RESPONSE
)
333 printf("serial:0x%x", EXTRACT_32BITS(&np
->serial
));
334 printf(" result:0x%x", EXTRACT_32BITS(&np
->result
));
335 printf(" recvlen:%u", EXTRACT_32BITS(&np
->recvlength
));
336 /* BIND910: not used */
338 printf(" authtype:0x%x", EXTRACT_16BITS(&np
->authtype
));
339 printf(" authlen:%u", EXTRACT_16BITS(&np
->authlength
));
345 /* per-opcode content */
350 lwres_gabnrequest_t
*gabn
;
351 lwres_gnbarequest_t
*gnba
;
352 lwres_grbnrequest_t
*grbn
;
359 switch (EXTRACT_32BITS(&np
->opcode
)) {
360 case LWRES_OPCODE_NOOP
:
362 case LWRES_OPCODE_GETADDRSBYNAME
:
363 gabn
= (lwres_gabnrequest_t
*)(np
+ 1);
364 TCHECK(gabn
->namelen
);
365 /* XXX gabn points to packed struct */
366 s
= (const char *)&gabn
->namelen
+
367 sizeof(gabn
->namelen
);
368 l
= EXTRACT_16BITS(&gabn
->namelen
);
370 /* BIND910: not used */
372 printf(" flags:0x%x",
373 EXTRACT_32BITS(&gabn
->flags
));
376 v
= EXTRACT_32BITS(&gabn
->addrtypes
);
377 switch (v
& (LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
)) {
378 case LWRES_ADDRTYPE_V4
:
381 case LWRES_ADDRTYPE_V6
:
384 case LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
:
388 if (v
& ~(LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
))
391 advance
= lwres_printname(l
, s
);
396 case LWRES_OPCODE_GETNAMEBYADDR
:
397 gnba
= (lwres_gnbarequest_t
*)(np
+ 1);
400 /* BIND910: not used */
402 printf(" flags:0x%x",
403 EXTRACT_32BITS(&gnba
->flags
));
406 s
= (const char *)&gnba
->addr
;
408 advance
= lwres_printaddr(&gnba
->addr
);
413 case LWRES_OPCODE_GETRDATABYNAME
:
414 /* XXX no trace, not tested */
415 grbn
= (lwres_grbnrequest_t
*)(np
+ 1);
416 TCHECK(grbn
->namelen
);
418 /* BIND910: not used */
420 printf(" flags:0x%x",
421 EXTRACT_32BITS(&grbn
->flags
));
424 printf(" %s", tok2str(ns_type2str
, "Type%d",
425 EXTRACT_16BITS(&grbn
->rdtype
)));
426 if (EXTRACT_16BITS(&grbn
->rdclass
) != C_IN
) {
427 printf(" %s", tok2str(ns_class2str
, "Class%d",
428 EXTRACT_16BITS(&grbn
->rdclass
)));
431 /* XXX grbn points to packed struct */
432 s
= (const char *)&grbn
->namelen
+
433 sizeof(grbn
->namelen
);
434 l
= EXTRACT_16BITS(&grbn
->namelen
);
436 advance
= lwres_printname(l
, s
);
449 lwres_gabnresponse_t
*gabn
;
450 lwres_gnbaresponse_t
*gnba
;
451 lwres_grbnresponse_t
*grbn
;
459 switch (EXTRACT_32BITS(&np
->opcode
)) {
460 case LWRES_OPCODE_NOOP
:
462 case LWRES_OPCODE_GETADDRSBYNAME
:
463 gabn
= (lwres_gabnresponse_t
*)(np
+ 1);
464 TCHECK(gabn
->realnamelen
);
465 /* XXX gabn points to packed struct */
466 s
= (const char *)&gabn
->realnamelen
+
467 sizeof(gabn
->realnamelen
);
468 l
= EXTRACT_16BITS(&gabn
->realnamelen
);
470 /* BIND910: not used */
472 printf(" flags:0x%x",
473 EXTRACT_32BITS(&gabn
->flags
));
476 printf(" %u/%u", EXTRACT_16BITS(&gabn
->naliases
),
477 EXTRACT_16BITS(&gabn
->naddrs
));
479 advance
= lwres_printname(l
, s
);
485 na
= EXTRACT_16BITS(&gabn
->naliases
);
486 for (i
= 0; i
< na
; i
++) {
487 advance
= lwres_printnamelen(s
);
494 na
= EXTRACT_16BITS(&gabn
->naddrs
);
495 for (i
= 0; i
< na
; i
++) {
496 advance
= lwres_printaddr((lwres_addr_t
*)s
);
502 case LWRES_OPCODE_GETNAMEBYADDR
:
503 gnba
= (lwres_gnbaresponse_t
*)(np
+ 1);
504 TCHECK(gnba
->realnamelen
);
505 /* XXX gnba points to packed struct */
506 s
= (const char *)&gnba
->realnamelen
+
507 sizeof(gnba
->realnamelen
);
508 l
= EXTRACT_16BITS(&gnba
->realnamelen
);
510 /* BIND910: not used */
512 printf(" flags:0x%x",
513 EXTRACT_32BITS(&gnba
->flags
));
516 printf(" %u", EXTRACT_16BITS(&gnba
->naliases
));
518 advance
= lwres_printname(l
, s
);
524 na
= EXTRACT_16BITS(&gnba
->naliases
);
525 for (i
= 0; i
< na
; i
++) {
526 advance
= lwres_printnamelen(s
);
532 case LWRES_OPCODE_GETRDATABYNAME
:
533 /* XXX no trace, not tested */
534 grbn
= (lwres_grbnresponse_t
*)(np
+ 1);
537 /* BIND910: not used */
539 printf(" flags:0x%x",
540 EXTRACT_32BITS(&grbn
->flags
));
543 printf(" %s", tok2str(ns_type2str
, "Type%d",
544 EXTRACT_16BITS(&grbn
->rdtype
)));
545 if (EXTRACT_16BITS(&grbn
->rdclass
) != C_IN
) {
546 printf(" %s", tok2str(ns_class2str
, "Class%d",
547 EXTRACT_16BITS(&grbn
->rdclass
)));
550 relts_print(EXTRACT_32BITS(&grbn
->ttl
));
551 printf(" %u/%u", EXTRACT_16BITS(&grbn
->nrdatas
),
552 EXTRACT_16BITS(&grbn
->nsigs
));
554 /* XXX grbn points to packed struct */
555 s
= (const char *)&grbn
->nsigs
+ sizeof(grbn
->nsigs
);
557 advance
= lwres_printnamelen(s
);
563 na
= EXTRACT_16BITS(&grbn
->nrdatas
);
564 for (i
= 0; i
< na
; i
++) {
565 /* XXX should decode resource data */
566 advance
= lwres_printbinlen(s
);
573 na
= EXTRACT_16BITS(&grbn
->nsigs
);
574 for (i
= 0; i
< na
; i
++) {
575 /* XXX how should we print it? */
576 advance
= lwres_printbinlen(s
);
589 /* length mismatch */
590 if (EXTRACT_32BITS(&np
->length
) != length
) {
591 printf(" [len: %u != %u]", EXTRACT_32BITS(&np
->length
),
594 if (!unsupported
&& s
< (const char *)np
+ EXTRACT_32BITS(&np
->length
))