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