source: rtems-libbsd/freebsd/contrib/tcpdump/print-lwres.c @ 8440506

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 8440506 was 8440506, checked in by Chris Johns <chrisj@…>, on 06/15/15 at 07:42:23

Add tcpdump and libpcap.

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