]> The Tcpdump Group git mirrors - tcpdump/blob - print-lwres.c
add lwres dissector
[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 #ifndef lint
31 static const char rcsid[] =
32 "@(#) $Header: /tcpdump/master/tcpdump/print-lwres.c,v 1.1 2001-01-29 09:18:50 itojun Exp $ (LBL)";
33 #endif
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/time.h>
41
42 #include <netinet/in.h>
43
44 #ifdef NOERROR
45 #undef NOERROR /* Solaris sucks */
46 #endif
47 #ifdef NOERROR
48 #undef T_UNSPEC /* SINIX does too */
49 #endif
50 #include "nameser.h"
51
52 #include <stdio.h>
53 #include <string.h>
54
55 #include "interface.h"
56 #include "addrtoname.h"
57 #include "extract.h" /* must come after interface.h */
58
59 /* BIND9 lib/lwres/include/lwres */
60 typedef u_int32_t lwres_uint32_t;
61 typedef u_int16_t lwres_uint16_t;
62 typedef u_int8_t lwres_uint8_t;
63
64 struct lwres_lwpacket {
65 lwres_uint32_t length;
66 lwres_uint16_t version;
67 lwres_uint16_t pktflags;
68 lwres_uint32_t serial;
69 lwres_uint32_t opcode;
70 lwres_uint32_t result;
71 lwres_uint32_t recvlength;
72 lwres_uint16_t authtype;
73 lwres_uint16_t authlength;
74 };
75
76 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
77 #define LWRES_FLAG_SECUREDATA 0x00000002U
78
79 /*
80 * no-op
81 */
82 #define LWRES_OPCODE_NOOP 0x00000000U
83
84 typedef struct {
85 /* public */
86 lwres_uint16_t datalength;
87 unsigned char *data;
88 } lwres_nooprequest_t;
89
90 typedef struct {
91 /* public */
92 lwres_uint16_t datalength;
93 unsigned char *data;
94 } lwres_noopresponse_t;
95
96 /*
97 * get addresses by name
98 */
99 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
100
101 typedef struct {
102 /* public */
103 lwres_uint32_t flags;
104 lwres_uint32_t addrtypes;
105 lwres_uint16_t namelen;
106 /* name follows */
107 } lwres_gabnrequest_t;
108
109 typedef struct {
110 /* public */
111 lwres_uint32_t flags;
112 lwres_uint16_t naliases;
113 lwres_uint16_t naddrs;
114 char *realname;
115 char **aliases;
116 lwres_uint16_t realnamelen;
117 lwres_uint16_t *aliaslen;
118 #if 0
119 lwres_addrlist_t addrs;
120 /* if base != NULL, it will be freed when this structure is freed. */
121 void *base;
122 size_t baselen;
123 #endif
124 } lwres_gabnresponse_t;
125
126 /*
127 * get name by address
128 */
129 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
130 typedef struct {
131 /* public */
132 lwres_uint32_t flags;
133 #if 0
134 lwres_addr_t addr;
135 #endif
136 } lwres_gnbarequest_t;
137
138 typedef struct {
139 /* public */
140 lwres_uint32_t flags;
141 lwres_uint16_t naliases;
142 #if 0
143 char *realname;
144 char **aliases;
145 lwres_uint16_t realnamelen;
146 lwres_uint16_t *aliaslen;
147 /* if base != NULL, it will be freed when this structure is freed. */
148 void *base;
149 size_t baselen;
150 #endif
151 } lwres_gnbaresponse_t;
152
153 /*
154 * get rdata by name
155 */
156 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
157
158 typedef struct {
159 /* public */
160 lwres_uint32_t flags;
161 lwres_uint16_t rdclass;
162 lwres_uint16_t rdtype;
163 lwres_uint16_t namelen;
164 #if 0
165 char *name;
166 #endif
167 } lwres_grbnrequest_t;
168
169 typedef struct {
170 /* public */
171 lwres_uint32_t flags;
172 lwres_uint16_t rdclass;
173 lwres_uint16_t rdtype;
174 lwres_uint32_t ttl;
175 lwres_uint16_t nrdatas;
176 lwres_uint16_t nsigs;
177 #if 0
178 char *realname;
179 lwres_uint16_t realnamelen;
180 unsigned char **rdatas;
181 lwres_uint16_t *rdatalen;
182 unsigned char **sigs;
183 lwres_uint16_t *siglen;
184 /* if base != NULL, it will be freed when this structure is freed. */
185 void *base;
186 size_t baselen;
187 #endif
188 } lwres_grbnresponse_t;
189
190 #define LWRDATA_VALIDATED 0x00000001
191
192 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
193 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
194
195 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
196 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
197
198 struct vstr {
199 u_int32_t v;
200 const char *s;
201 };
202
203 struct vstr opcode[] = {
204 { LWRES_OPCODE_NOOP, "noop", },
205 { LWRES_OPCODE_GETADDRSBYNAME, "getaddrsbyname", },
206 { LWRES_OPCODE_GETNAMEBYADDR, "getnamebyaddr", },
207 { LWRES_OPCODE_GETRDATABYNAME, "getrdatabyname", },
208 { 0, NULL, },
209 };
210
211 static const char *vtostr(struct vstr *, u_int32_t, const char *);
212
213 static const char *
214 vtostr(p, v, lastresort)
215 struct vstr *p;
216 u_int32_t v;
217 const char *lastresort;
218 {
219
220 while (p->s) {
221 if (v == p->v)
222 return p->s;
223 p++;
224 }
225
226 return lastresort;
227 }
228
229 void
230 lwres_print(register const u_char *bp, u_int length)
231 {
232 const struct lwres_lwpacket *np;
233 u_int32_t v;
234 const char *s;
235 #if 0
236 u_int32_t l;
237 lwres_gabnrequest_t *gabn;
238 int i;
239 #endif
240
241 np = (const struct lwres_lwpacket *)bp;
242 TCHECK(np->authlength);
243
244 printf(" lwres");
245 if (vflag)
246 printf(" v%u", ntohs(np->version));
247
248 /* opcode */
249 v = (u_int32_t)ntohl(np->opcode);
250 s = vtostr(opcode, v, NULL);
251 if (s) {
252 printf(" %s", s);
253 if (vflag)
254 printf("(0x%x)", v);
255 } else
256 printf(" 0x%x", v);
257
258 /* flag bit */
259 v = ntohs(np->pktflags);
260 if (v) {
261 printf("[%s%s]",
262 (v & LWRES_FLAG_TRUSTNOTREQUIRED) ? "t" : "",
263 (v & LWRES_FLAG_SECUREDATA) ? "S" : "");
264 }
265
266 if (vflag > 1) {
267 printf(" ("); /*)*/
268 printf("serial:0x%x", (unsigned int)ntohl(np->serial));
269 printf(" result:0x%x", (unsigned int)ntohl(np->result));
270 printf(" recvlen:%u", (unsigned int)ntohl(np->recvlength));
271 printf(" authtype:0x%x", ntohs(np->authtype));
272 printf(" authlen:%u", ntohs(np->authlength));
273 /*(*/
274 printf(")");
275 }
276
277 #if 0 /*not completed yet*/
278 /* per-opcode content */
279 switch (ntohl(np->opcode)) {
280 case LWRES_OPCODE_NOOP:
281 break;
282 case LWRES_OPCODE_GETADDRSBYNAME:
283 gabn = (lwres_gabnrequest_t *)(np + 1);
284 TCHECK(gabn->namelen);
285 /* XXX gabn points to packed struct */
286 s = (const char *)&gabn->namelen + sizeof(gabn->namelen);
287 l = ntohs(gabn->namelen);
288 if (s + l > (const char *)snapend)
289 goto trunc;
290
291 printf(" flags:0x%x", (unsigned int)ntohl(gabn->flags));
292 v = (u_int32_t)ntohl(gabn->addrtypes);
293 switch (v & (LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6)) {
294 case LWRES_ADDRTYPE_V4:
295 printf(" IPv4");
296 break;
297 case LWRES_ADDRTYPE_V6:
298 printf(" IPv6");
299 break;
300 case LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6:
301 printf(" IPv4/6");
302 break;
303 }
304 if (v & ~(LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6))
305 printf("[0x%x]", v);
306 printf(" ");
307 for (i = 0; i < l; i++)
308 safeputchar(s[i]);
309 break;
310 case LWRES_OPCODE_GETNAMEBYADDR:
311 break;
312 case LWRES_OPCODE_GETRDATABYNAME:
313 break;
314 default:
315 break;
316 }
317 #endif
318
319 /* length mismatch */
320 if (ntohl(np->length) != length)
321 printf(" [len: %u != %u]", (unsigned int)ntohl(np->length), length);
322 return;
323
324 trunc:
325 printf("[|lwres]");
326 return;
327 }