source: rtems-libbsd/freebsd/contrib/tcpdump/addrtoname.c @ 24631e2

5
Last change on this file since 24631e2 was 24631e2, checked in by Sebastian Huber <sebastian.huber@…>, on 03/11/19 at 08:33:28

tcpdump: Move static variables to special section

This fixes some issues if tcpdump is invoked a second time.

  • Property mode set to 100644
File size: 33.8 KB
RevLine 
[8440506]1#include <machine/rtems-bsd-user-space.h>
[4525674]2
[1043048]3#ifdef __rtems__
4#include <machine/rtems-bsd-program.h>
5#include "rtems-bsd-tcpdump-namespace.h"
6#endif /* __rtems__ */
[8440506]7/*
8 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
9 *      The Regents of the University of California.  All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that: (1) source code distributions
13 * retain the above copyright notice and this paragraph in its entirety, (2)
14 * distributions including binary code include the above copyright notice and
15 * this paragraph in its entirety in the documentation or other materials
16 * provided with the distribution, and (3) all advertising materials mentioning
17 * features or use of this software display the following acknowledgement:
18 * ``This product includes software developed by the University of California,
19 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
20 * the University nor the names of its contributors may be used to endorse
21 * or promote products derived from this software without specific prior
22 * written permission.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 *  Internet, ethernet, port, and protocol string to address
28 *  and address to string conversion routines
29 */
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
[bb80d9d]35#ifdef HAVE_CASPER
[1043048]36#include <libcasper.h>
37#include <casper/cap_dns.h>
[bb80d9d]38#endif /* HAVE_CASPER */
[1043048]39
40#include <netdissect-stdinc.h>
[8440506]41
42#ifdef USE_ETHER_NTOHOST
43#ifdef HAVE_NETINET_IF_ETHER_H
44struct mbuf;            /* Squelch compiler warnings on some platforms for */
45struct rtentry;         /* declarations in <net/if.h> */
46#include <net/if.h>     /* for "struct ifnet" in "struct arpcom" on Solaris */
47#include <netinet/if_ether.h>
48#endif /* HAVE_NETINET_IF_ETHER_H */
49#ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
50#include <netinet/ether.h>
51#endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
52
53#if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
54#ifndef HAVE_STRUCT_ETHER_ADDR
55struct ether_addr {
56        unsigned char ether_addr_octet[6];
57};
58#endif
59extern int ether_ntohost(char *, const struct ether_addr *);
60#endif
61
62#endif /* USE_ETHER_NTOHOST */
63
64#include <pcap.h>
65#include <pcap-namedb.h>
66#include <signal.h>
67#include <stdio.h>
68#include <string.h>
69#include <stdlib.h>
70
[1043048]71#include "netdissect.h"
[8440506]72#include "addrtoname.h"
[1043048]73#include "addrtostr.h"
74#include "ethertype.h"
[8440506]75#include "llc.h"
76#include "setsignal.h"
77#include "extract.h"
78#include "oui.h"
79
80#ifndef ETHER_ADDR_LEN
81#define ETHER_ADDR_LEN  6
82#endif
83
84/*
85 * hash tables for whatever-to-name translations
86 *
[1043048]87 * ndo_error() called on strdup(3) failure
[8440506]88 */
89
90#define HASHNAMESIZE 4096
91
92struct hnamemem {
[1043048]93        uint32_t addr;
[8440506]94        const char *name;
95        struct hnamemem *nxt;
96};
97
98static struct hnamemem hnametable[HASHNAMESIZE];
99static struct hnamemem tporttable[HASHNAMESIZE];
100static struct hnamemem uporttable[HASHNAMESIZE];
101static struct hnamemem eprototable[HASHNAMESIZE];
102static struct hnamemem dnaddrtable[HASHNAMESIZE];
103static struct hnamemem ipxsaptable[HASHNAMESIZE];
104
[1043048]105#ifdef _WIN32
[8440506]106/*
107 * fake gethostbyaddr for Win2k/XP
108 * gethostbyaddr() returns incorrect value when AF_INET6 is passed
109 * to 3rd argument.
110 *
111 * h_name in struct hostent is only valid.
112 */
113static struct hostent *
114win32_gethostbyaddr(const char *addr, int len, int type)
115{
116        static struct hostent host;
117        static char hostbuf[NI_MAXHOST];
118        char hname[NI_MAXHOST];
119        struct sockaddr_in6 addr6;
120
121        host.h_name = hostbuf;
122        switch (type) {
123        case AF_INET:
124                return gethostbyaddr(addr, len, type);
125                break;
126        case AF_INET6:
127                memset(&addr6, 0, sizeof(addr6));
128                addr6.sin6_family = AF_INET6;
129                memcpy(&addr6.sin6_addr, addr, len);
130                if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
131                    hname, sizeof(hname), NULL, 0, 0)) {
132                        return NULL;
133                } else {
134                        strcpy(host.h_name, hname);
135                        return &host;
136                }
137                break;
138        default:
139                return NULL;
140        }
141}
142#define gethostbyaddr win32_gethostbyaddr
[1043048]143#endif /* _WIN32 */
[8440506]144
145struct h6namemem {
146        struct in6_addr addr;
147        char *name;
148        struct h6namemem *nxt;
149};
150
151static struct h6namemem h6nametable[HASHNAMESIZE];
152
153struct enamemem {
154        u_short e_addr0;
155        u_short e_addr1;
156        u_short e_addr2;
157        const char *e_name;
158        u_char *e_nsap;                 /* used only for nsaptable[] */
159        struct enamemem *e_nxt;
160};
161
162static struct enamemem enametable[HASHNAMESIZE];
163static struct enamemem nsaptable[HASHNAMESIZE];
[18fa92c]164
165struct bsnamemem {
166        u_short bs_addr0;
167        u_short bs_addr1;
168        u_short bs_addr2;
169        const char *bs_name;
170        u_char *bs_bytes;
171        unsigned int bs_nbytes;
172        struct bsnamemem *bs_nxt;
173};
174
175static struct bsnamemem bytestringtable[HASHNAMESIZE];
[8440506]176
177struct protoidmem {
[1043048]178        uint32_t p_oui;
[8440506]179        u_short p_proto;
180        const char *p_name;
181        struct protoidmem *p_nxt;
182};
183
184static struct protoidmem protoidtable[HASHNAMESIZE];
185
186/*
187 * A faster replacement for inet_ntoa().
188 */
189const char *
[1043048]190intoa(uint32_t addr)
[8440506]191{
192        register char *cp;
193        register u_int byte;
194        register int n;
195        static char buf[sizeof(".xxx.xxx.xxx.xxx")];
196
197        NTOHL(addr);
198        cp = buf + sizeof(buf);
199        *--cp = '\0';
200
201        n = 4;
202        do {
203                byte = addr & 0xff;
204                *--cp = byte % 10 + '0';
205                byte /= 10;
206                if (byte > 0) {
207                        *--cp = byte % 10 + '0';
208                        byte /= 10;
209                        if (byte > 0)
210                                *--cp = byte + '0';
211                }
212                *--cp = '.';
213                addr >>= 8;
214        } while (--n > 0);
215
216        return cp + 1;
217}
218
[1043048]219static uint32_t f_netmask;
220static uint32_t f_localnet;
[bb80d9d]221#ifdef HAVE_CASPER
[1043048]222extern cap_channel_t *capdns;
223#endif
[8440506]224
225/*
226 * Return a name for the IP address pointed to by ap.  This address
227 * is assumed to be in network byte order.
228 *
229 * NOTE: ap is *NOT* necessarily part of the packet data (not even if
230 * this is being called with the "ipaddr_string()" macro), so you
[1043048]231 * *CANNOT* use the ND_TCHECK{2}/ND_TTEST{2} macros on it.  Furthermore,
[8440506]232 * even in cases where it *is* part of the packet data, the caller
233 * would still have to check for a null return value, even if it's
234 * just printing the return value with "%s" - not all versions of
235 * printf print "(null)" with "%s" and a null pointer, some of them
236 * don't check for a null pointer and crash in that case.
237 *
238 * The callers of this routine should, before handing this routine
239 * a pointer to packet data, be sure that the data is present in
240 * the packet buffer.  They should probably do those checks anyway,
241 * as other data at that layer might not be IP addresses, and it
242 * also needs to check whether they're present in the packet buffer.
243 */
244const char *
[1043048]245getname(netdissect_options *ndo, const u_char *ap)
[8440506]246{
247        register struct hostent *hp;
[1043048]248        uint32_t addr;
249        struct hnamemem *p;
[8440506]250
251        memcpy(&addr, ap, sizeof(addr));
252        p = &hnametable[addr & (HASHNAMESIZE-1)];
253        for (; p->nxt; p = p->nxt) {
254                if (p->addr == addr)
255                        return (p->name);
256        }
257        p->addr = addr;
[1043048]258        p->nxt = newhnamemem(ndo);
[8440506]259
260        /*
261         * Print names unless:
262         *      (1) -n was given.
263         *      (2) Address is foreign and -f was given. (If -f was not
264         *          given, f_netmask and f_localnet are 0 and the test
265         *          evaluates to true)
266         */
[1043048]267        if (!ndo->ndo_nflag &&
[8440506]268            (addr & f_netmask) == f_localnet) {
[bb80d9d]269#ifdef HAVE_CASPER
[1043048]270                if (capdns != NULL) {
271                        hp = cap_gethostbyaddr(capdns, (char *)&addr, 4,
272                            AF_INET);
273                } else
274#endif
275                        hp = gethostbyaddr((char *)&addr, 4, AF_INET);
[8440506]276                if (hp) {
277                        char *dotp;
278
279                        p->name = strdup(hp->h_name);
[1043048]280                        if (p->name == NULL)
281                                (*ndo->ndo_error)(ndo,
282                                                  "getname: strdup(hp->h_name)");
283                        if (ndo->ndo_Nflag) {
[8440506]284                                /* Remove domain qualifications */
285                                dotp = strchr(p->name, '.');
286                                if (dotp)
287                                        *dotp = '\0';
288                        }
289                        return (p->name);
290                }
291        }
292        p->name = strdup(intoa(addr));
[1043048]293        if (p->name == NULL)
294                (*ndo->ndo_error)(ndo, "getname: strdup(intoa(addr))");
[8440506]295        return (p->name);
296}
297
298/*
299 * Return a name for the IP6 address pointed to by ap.  This address
300 * is assumed to be in network byte order.
301 */
302const char *
[1043048]303getname6(netdissect_options *ndo, const u_char *ap)
[8440506]304{
305        register struct hostent *hp;
[1043048]306        union {
307                struct in6_addr addr;
308                struct for_hash_addr {
309                        char fill[14];
310                        uint16_t d;
311                } addra;
312        } addr;
313        struct h6namemem *p;
[8440506]314        register const char *cp;
315        char ntop_buf[INET6_ADDRSTRLEN];
316
317        memcpy(&addr, ap, sizeof(addr));
[1043048]318        p = &h6nametable[addr.addra.d & (HASHNAMESIZE-1)];
[8440506]319        for (; p->nxt; p = p->nxt) {
320                if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
321                        return (p->name);
322        }
[1043048]323        p->addr = addr.addr;
324        p->nxt = newh6namemem(ndo);
[8440506]325
326        /*
327         * Do not print names if -n was given.
328         */
[1043048]329        if (!ndo->ndo_nflag) {
[bb80d9d]330#ifdef HAVE_CASPER
[1043048]331                if (capdns != NULL) {
332                        hp = cap_gethostbyaddr(capdns, (char *)&addr,
333                            sizeof(addr), AF_INET6);
334                } else
335#endif
336                        hp = gethostbyaddr((char *)&addr, sizeof(addr),
337                            AF_INET6);
[8440506]338                if (hp) {
339                        char *dotp;
340
341                        p->name = strdup(hp->h_name);
[1043048]342                        if (p->name == NULL)
343                                (*ndo->ndo_error)(ndo,
344                                                  "getname6: strdup(hp->h_name)");
345                        if (ndo->ndo_Nflag) {
[8440506]346                                /* Remove domain qualifications */
347                                dotp = strchr(p->name, '.');
348                                if (dotp)
349                                        *dotp = '\0';
350                        }
351                        return (p->name);
352                }
353        }
[1043048]354        cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
[8440506]355        p->name = strdup(cp);
[1043048]356        if (p->name == NULL)
357                (*ndo->ndo_error)(ndo, "getname6: strdup(cp)");
[8440506]358        return (p->name);
359}
360
[18fa92c]361static const char hex[16] = "0123456789abcdef";
[8440506]362
363
364/* Find the hash node that corresponds the ether address 'ep' */
365
366static inline struct enamemem *
[1043048]367lookup_emem(netdissect_options *ndo, const u_char *ep)
[8440506]368{
369        register u_int i, j, k;
370        struct enamemem *tp;
371
372        k = (ep[0] << 8) | ep[1];
373        j = (ep[2] << 8) | ep[3];
374        i = (ep[4] << 8) | ep[5];
375
376        tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
377        while (tp->e_nxt)
378                if (tp->e_addr0 == i &&
379                    tp->e_addr1 == j &&
380                    tp->e_addr2 == k)
381                        return tp;
382                else
383                        tp = tp->e_nxt;
384        tp->e_addr0 = i;
385        tp->e_addr1 = j;
386        tp->e_addr2 = k;
387        tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
388        if (tp->e_nxt == NULL)
[1043048]389                (*ndo->ndo_error)(ndo, "lookup_emem: calloc");
[8440506]390
391        return tp;
392}
393
394/*
395 * Find the hash node that corresponds to the bytestring 'bs'
396 * with length 'nlen'
397 */
398
[18fa92c]399static inline struct bsnamemem *
[1043048]400lookup_bytestring(netdissect_options *ndo, register const u_char *bs,
401                  const unsigned int nlen)
[8440506]402{
[18fa92c]403        struct bsnamemem *tp;
[8440506]404        register u_int i, j, k;
405
406        if (nlen >= 6) {
407                k = (bs[0] << 8) | bs[1];
408                j = (bs[2] << 8) | bs[3];
409                i = (bs[4] << 8) | bs[5];
410        } else if (nlen >= 4) {
411                k = (bs[0] << 8) | bs[1];
412                j = (bs[2] << 8) | bs[3];
413                i = 0;
414        } else
415                i = j = k = 0;
416
417        tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
[18fa92c]418        while (tp->bs_nxt)
419                if (nlen == tp->bs_nbytes &&
420                    tp->bs_addr0 == i &&
421                    tp->bs_addr1 == j &&
422                    tp->bs_addr2 == k &&
423                    memcmp((const char *)bs, (const char *)(tp->bs_bytes), nlen) == 0)
[8440506]424                        return tp;
425                else
[18fa92c]426                        tp = tp->bs_nxt;
[8440506]427
[18fa92c]428        tp->bs_addr0 = i;
429        tp->bs_addr1 = j;
430        tp->bs_addr2 = k;
[8440506]431
[18fa92c]432        tp->bs_bytes = (u_char *) calloc(1, nlen);
433        if (tp->bs_bytes == NULL)
[1043048]434                (*ndo->ndo_error)(ndo, "lookup_bytestring: calloc");
[8440506]435
[18fa92c]436        memcpy(tp->bs_bytes, bs, nlen);
437        tp->bs_nbytes = nlen;
438        tp->bs_nxt = (struct bsnamemem *)calloc(1, sizeof(*tp));
439        if (tp->bs_nxt == NULL)
[1043048]440                (*ndo->ndo_error)(ndo, "lookup_bytestring: calloc");
[8440506]441
442        return tp;
443}
444
445/* Find the hash node that corresponds the NSAP 'nsap' */
446
447static inline struct enamemem *
[1043048]448lookup_nsap(netdissect_options *ndo, register const u_char *nsap,
449            register u_int nsap_length)
[8440506]450{
451        register u_int i, j, k;
452        struct enamemem *tp;
[1043048]453        const u_char *ensap;
[8440506]454
[1043048]455        if (nsap_length > 6) {
456                ensap = nsap + nsap_length - 6;
[8440506]457                k = (ensap[0] << 8) | ensap[1];
458                j = (ensap[2] << 8) | ensap[3];
459                i = (ensap[4] << 8) | ensap[5];
460        }
461        else
462                i = j = k = 0;
463
464        tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
465        while (tp->e_nxt)
[18fa92c]466                if (nsap_length == tp->e_nsap[0] &&
467                    tp->e_addr0 == i &&
[8440506]468                    tp->e_addr1 == j &&
469                    tp->e_addr2 == k &&
[18fa92c]470                    memcmp((const char *)nsap,
[1043048]471                        (char *)&(tp->e_nsap[1]), nsap_length) == 0)
[8440506]472                        return tp;
473                else
474                        tp = tp->e_nxt;
475        tp->e_addr0 = i;
476        tp->e_addr1 = j;
477        tp->e_addr2 = k;
[1043048]478        tp->e_nsap = (u_char *)malloc(nsap_length + 1);
[8440506]479        if (tp->e_nsap == NULL)
[1043048]480                (*ndo->ndo_error)(ndo, "lookup_nsap: malloc");
481        tp->e_nsap[0] = (u_char)nsap_length;    /* guaranteed < ISONSAP_MAX_LENGTH */
482        memcpy((char *)&tp->e_nsap[1], (const char *)nsap, nsap_length);
[8440506]483        tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
484        if (tp->e_nxt == NULL)
[1043048]485                (*ndo->ndo_error)(ndo, "lookup_nsap: calloc");
[8440506]486
487        return tp;
488}
489
490/* Find the hash node that corresponds the protoid 'pi'. */
491
492static inline struct protoidmem *
[1043048]493lookup_protoid(netdissect_options *ndo, const u_char *pi)
[8440506]494{
495        register u_int i, j;
496        struct protoidmem *tp;
497
498        /* 5 octets won't be aligned */
499        i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
500        j =   (pi[3] << 8) + pi[4];
501        /* XXX should be endian-insensitive, but do big-endian testing  XXX */
502
503        tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
504        while (tp->p_nxt)
505                if (tp->p_oui == i && tp->p_proto == j)
506                        return tp;
507                else
508                        tp = tp->p_nxt;
509        tp->p_oui = i;
510        tp->p_proto = j;
511        tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
512        if (tp->p_nxt == NULL)
[1043048]513                (*ndo->ndo_error)(ndo, "lookup_protoid: calloc");
[8440506]514
515        return tp;
516}
517
518const char *
[1043048]519etheraddr_string(netdissect_options *ndo, register const u_char *ep)
[8440506]520{
521        register int i;
522        register char *cp;
523        register struct enamemem *tp;
524        int oui;
525        char buf[BUFSIZE];
526
[1043048]527        tp = lookup_emem(ndo, ep);
[8440506]528        if (tp->e_name)
529                return (tp->e_name);
530#ifdef USE_ETHER_NTOHOST
[1043048]531        if (!ndo->ndo_nflag) {
[8440506]532                char buf2[BUFSIZE];
533
[1043048]534                if (ether_ntohost(buf2, (const struct ether_addr *)ep) == 0) {
[8440506]535                        tp->e_name = strdup(buf2);
[1043048]536                        if (tp->e_name == NULL)
537                                (*ndo->ndo_error)(ndo,
538                                                  "etheraddr_string: strdup(buf2)");
[8440506]539                        return (tp->e_name);
540                }
541        }
542#endif
543        cp = buf;
544        oui = EXTRACT_24BITS(ep);
545        *cp++ = hex[*ep >> 4 ];
546        *cp++ = hex[*ep++ & 0xf];
547        for (i = 5; --i >= 0;) {
548                *cp++ = ':';
549                *cp++ = hex[*ep >> 4 ];
550                *cp++ = hex[*ep++ & 0xf];
551        }
552
[1043048]553        if (!ndo->ndo_nflag) {
[8440506]554                snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
555                    tok2str(oui_values, "Unknown", oui));
556        } else
557                *cp = '\0';
558        tp->e_name = strdup(buf);
[1043048]559        if (tp->e_name == NULL)
560                (*ndo->ndo_error)(ndo, "etheraddr_string: strdup(buf)");
[8440506]561        return (tp->e_name);
562}
563
564const char *
[1043048]565le64addr_string(netdissect_options *ndo, const u_char *ep)
[8440506]566{
567        const unsigned int len = 8;
568        register u_int i;
569        register char *cp;
[18fa92c]570        register struct bsnamemem *tp;
[8440506]571        char buf[BUFSIZE];
572
[1043048]573        tp = lookup_bytestring(ndo, ep, len);
[18fa92c]574        if (tp->bs_name)
575                return (tp->bs_name);
[8440506]576
577        cp = buf;
578        for (i = len; i > 0 ; --i) {
579                *cp++ = hex[*(ep + i - 1) >> 4];
580                *cp++ = hex[*(ep + i - 1) & 0xf];
581                *cp++ = ':';
582        }
583        cp --;
584
585        *cp = '\0';
586
[18fa92c]587        tp->bs_name = strdup(buf);
588        if (tp->bs_name == NULL)
[1043048]589                (*ndo->ndo_error)(ndo, "le64addr_string: strdup(buf)");
[8440506]590
[18fa92c]591        return (tp->bs_name);
[8440506]592}
593
594const char *
[1043048]595linkaddr_string(netdissect_options *ndo, const u_char *ep,
596                const unsigned int type, const unsigned int len)
[8440506]597{
598        register u_int i;
599        register char *cp;
[18fa92c]600        register struct bsnamemem *tp;
[8440506]601
602        if (len == 0)
603                return ("<empty>");
604
605        if (type == LINKADDR_ETHER && len == ETHER_ADDR_LEN)
[1043048]606                return (etheraddr_string(ndo, ep));
[8440506]607
608        if (type == LINKADDR_FRELAY)
[1043048]609                return (q922_string(ndo, ep, len));
[8440506]610
[1043048]611        tp = lookup_bytestring(ndo, ep, len);
[18fa92c]612        if (tp->bs_name)
613                return (tp->bs_name);
[8440506]614
[18fa92c]615        tp->bs_name = cp = (char *)malloc(len*3);
616        if (tp->bs_name == NULL)
[1043048]617                (*ndo->ndo_error)(ndo, "linkaddr_string: malloc");
[8440506]618        *cp++ = hex[*ep >> 4];
619        *cp++ = hex[*ep++ & 0xf];
620        for (i = len-1; i > 0 ; --i) {
621                *cp++ = ':';
622                *cp++ = hex[*ep >> 4];
623                *cp++ = hex[*ep++ & 0xf];
624        }
625        *cp = '\0';
[18fa92c]626        return (tp->bs_name);
[8440506]627}
628
629const char *
[1043048]630etherproto_string(netdissect_options *ndo, u_short port)
[8440506]631{
632        register char *cp;
633        register struct hnamemem *tp;
[1043048]634        register uint32_t i = port;
[8440506]635        char buf[sizeof("0000")];
636
637        for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
638                if (tp->addr == i)
639                        return (tp->name);
640
641        tp->addr = i;
[1043048]642        tp->nxt = newhnamemem(ndo);
[8440506]643
644        cp = buf;
645        NTOHS(port);
646        *cp++ = hex[port >> 12 & 0xf];
647        *cp++ = hex[port >> 8 & 0xf];
648        *cp++ = hex[port >> 4 & 0xf];
649        *cp++ = hex[port & 0xf];
650        *cp++ = '\0';
651        tp->name = strdup(buf);
[1043048]652        if (tp->name == NULL)
653                (*ndo->ndo_error)(ndo, "etherproto_string: strdup(buf)");
[8440506]654        return (tp->name);
655}
656
657const char *
[1043048]658protoid_string(netdissect_options *ndo, register const u_char *pi)
[8440506]659{
660        register u_int i, j;
661        register char *cp;
662        register struct protoidmem *tp;
663        char buf[sizeof("00:00:00:00:00")];
664
[1043048]665        tp = lookup_protoid(ndo, pi);
[8440506]666        if (tp->p_name)
667                return tp->p_name;
668
669        cp = buf;
670        if ((j = *pi >> 4) != 0)
671                *cp++ = hex[j];
672        *cp++ = hex[*pi++ & 0xf];
673        for (i = 4; (int)--i >= 0;) {
674                *cp++ = ':';
675                if ((j = *pi >> 4) != 0)
676                        *cp++ = hex[j];
677                *cp++ = hex[*pi++ & 0xf];
678        }
679        *cp = '\0';
680        tp->p_name = strdup(buf);
[1043048]681        if (tp->p_name == NULL)
682                (*ndo->ndo_error)(ndo, "protoid_string: strdup(buf)");
[8440506]683        return (tp->p_name);
684}
685
686#define ISONSAP_MAX_LENGTH 20
687const char *
[1043048]688isonsap_string(netdissect_options *ndo, const u_char *nsap,
689               register u_int nsap_length)
[8440506]690{
691        register u_int nsap_idx;
692        register char *cp;
693        register struct enamemem *tp;
694
695        if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
696                return ("isonsap_string: illegal length");
697
[1043048]698        tp = lookup_nsap(ndo, nsap, nsap_length);
[8440506]699        if (tp->e_name)
700                return tp->e_name;
701
702        tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
703        if (cp == NULL)
[1043048]704                (*ndo->ndo_error)(ndo, "isonsap_string: malloc");
[8440506]705
706        for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
707                *cp++ = hex[*nsap >> 4];
708                *cp++ = hex[*nsap++ & 0xf];
709                if (((nsap_idx & 1) == 0) &&
710                     (nsap_idx + 1 < nsap_length)) {
711                        *cp++ = '.';
712                }
713        }
714        *cp = '\0';
715        return (tp->e_name);
716}
717
718const char *
[1043048]719tcpport_string(netdissect_options *ndo, u_short port)
[8440506]720{
721        register struct hnamemem *tp;
[1043048]722        register uint32_t i = port;
[8440506]723        char buf[sizeof("00000")];
724
725        for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
726                if (tp->addr == i)
727                        return (tp->name);
728
729        tp->addr = i;
[1043048]730        tp->nxt = newhnamemem(ndo);
[8440506]731
732        (void)snprintf(buf, sizeof(buf), "%u", i);
733        tp->name = strdup(buf);
[1043048]734        if (tp->name == NULL)
735                (*ndo->ndo_error)(ndo, "tcpport_string: strdup(buf)");
[8440506]736        return (tp->name);
737}
738
739const char *
[1043048]740udpport_string(netdissect_options *ndo, register u_short port)
[8440506]741{
742        register struct hnamemem *tp;
[1043048]743        register uint32_t i = port;
[8440506]744        char buf[sizeof("00000")];
745
746        for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
747                if (tp->addr == i)
748                        return (tp->name);
749
750        tp->addr = i;
[1043048]751        tp->nxt = newhnamemem(ndo);
[8440506]752
753        (void)snprintf(buf, sizeof(buf), "%u", i);
754        tp->name = strdup(buf);
[1043048]755        if (tp->name == NULL)
756                (*ndo->ndo_error)(ndo, "udpport_string: strdup(buf)");
[8440506]757        return (tp->name);
758}
759
760const char *
[1043048]761ipxsap_string(netdissect_options *ndo, u_short port)
[8440506]762{
763        register char *cp;
764        register struct hnamemem *tp;
[1043048]765        register uint32_t i = port;
[8440506]766        char buf[sizeof("0000")];
767
768        for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
769                if (tp->addr == i)
770                        return (tp->name);
771
772        tp->addr = i;
[1043048]773        tp->nxt = newhnamemem(ndo);
[8440506]774
775        cp = buf;
776        NTOHS(port);
777        *cp++ = hex[port >> 12 & 0xf];
778        *cp++ = hex[port >> 8 & 0xf];
779        *cp++ = hex[port >> 4 & 0xf];
780        *cp++ = hex[port & 0xf];
781        *cp++ = '\0';
782        tp->name = strdup(buf);
[1043048]783        if (tp->name == NULL)
784                (*ndo->ndo_error)(ndo, "ipxsap_string: strdup(buf)");
[8440506]785        return (tp->name);
786}
787
788static void
[1043048]789init_servarray(netdissect_options *ndo)
[8440506]790{
791        struct servent *sv;
792        register struct hnamemem *table;
793        register int i;
794        char buf[sizeof("0000000000")];
795
796        while ((sv = getservent()) != NULL) {
797                int port = ntohs(sv->s_port);
798                i = port & (HASHNAMESIZE-1);
799                if (strcmp(sv->s_proto, "tcp") == 0)
800                        table = &tporttable[i];
801                else if (strcmp(sv->s_proto, "udp") == 0)
802                        table = &uporttable[i];
803                else
804                        continue;
805
806                while (table->name)
807                        table = table->nxt;
[1043048]808                if (ndo->ndo_nflag) {
[8440506]809                        (void)snprintf(buf, sizeof(buf), "%d", port);
810                        table->name = strdup(buf);
811                } else
812                        table->name = strdup(sv->s_name);
[1043048]813                if (table->name == NULL)
814                        (*ndo->ndo_error)(ndo, "init_servarray: strdup");
815
[8440506]816                table->addr = port;
[1043048]817                table->nxt = newhnamemem(ndo);
[8440506]818        }
819        endservent();
820}
821
[1043048]822static const struct eproto {
[8440506]823        const char *s;
824        u_short p;
[1043048]825} eproto_db[] = {
826        { "pup", ETHERTYPE_PUP },
827        { "xns", ETHERTYPE_NS },
828        { "ip", ETHERTYPE_IP },
829        { "ip6", ETHERTYPE_IPV6 },
830        { "arp", ETHERTYPE_ARP },
831        { "rarp", ETHERTYPE_REVARP },
832        { "sprite", ETHERTYPE_SPRITE },
833        { "mopdl", ETHERTYPE_MOPDL },
834        { "moprc", ETHERTYPE_MOPRC },
835        { "decnet", ETHERTYPE_DN },
836        { "lat", ETHERTYPE_LAT },
837        { "sca", ETHERTYPE_SCA },
838        { "lanbridge", ETHERTYPE_LANBRIDGE },
839        { "vexp", ETHERTYPE_VEXP },
840        { "vprod", ETHERTYPE_VPROD },
841        { "atalk", ETHERTYPE_ATALK },
842        { "atalkarp", ETHERTYPE_AARP },
843        { "loopback", ETHERTYPE_LOOPBACK },
844        { "decdts", ETHERTYPE_DECDTS },
845        { "decdns", ETHERTYPE_DECDNS },
846        { (char *)0, 0 }
847};
[8440506]848
849static void
[1043048]850init_eprotoarray(netdissect_options *ndo)
[8440506]851{
852        register int i;
853        register struct hnamemem *table;
854
855        for (i = 0; eproto_db[i].s; i++) {
856                int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
857                table = &eprototable[j];
858                while (table->name)
859                        table = table->nxt;
860                table->name = eproto_db[i].s;
861                table->addr = htons(eproto_db[i].p);
[1043048]862                table->nxt = newhnamemem(ndo);
[8440506]863        }
864}
865
866static const struct protoidlist {
867        const u_char protoid[5];
868        const char *name;
869} protoidlist[] = {
870        {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
871        {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
872        {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
873        {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
874        {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
875        {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
876};
877
878/*
879 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
880 * types.
881 */
882static void
[1043048]883init_protoidarray(netdissect_options *ndo)
[8440506]884{
885        register int i;
886        register struct protoidmem *tp;
887        const struct protoidlist *pl;
888        u_char protoid[5];
889
890        protoid[0] = 0;
891        protoid[1] = 0;
892        protoid[2] = 0;
893        for (i = 0; eproto_db[i].s; i++) {
894                u_short etype = htons(eproto_db[i].p);
895
896                memcpy((char *)&protoid[3], (char *)&etype, 2);
[1043048]897                tp = lookup_protoid(ndo, protoid);
[8440506]898                tp->p_name = strdup(eproto_db[i].s);
[1043048]899                if (tp->p_name == NULL)
900                        (*ndo->ndo_error)(ndo,
901                                          "init_protoidarray: strdup(eproto_db[i].s)");
[8440506]902        }
903        /* Hardwire some SNAP proto ID names */
904        for (pl = protoidlist; pl->name != NULL; ++pl) {
[1043048]905                tp = lookup_protoid(ndo, pl->protoid);
[8440506]906                /* Don't override existing name */
907                if (tp->p_name != NULL)
908                        continue;
909
910                tp->p_name = pl->name;
911        }
912}
913
914static const struct etherlist {
915        const u_char addr[6];
916        const char *name;
917} etherlist[] = {
918        {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
919        {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
920};
921
922/*
923 * Initialize the ethers hash table.  We take two different approaches
924 * depending on whether or not the system provides the ethers name
925 * service.  If it does, we just wire in a few names at startup,
926 * and etheraddr_string() fills in the table on demand.  If it doesn't,
927 * then we suck in the entire /etc/ethers file at startup.  The idea
928 * is that parsing the local file will be fast, but spinning through
929 * all the ethers entries via NIS & next_etherent might be very slow.
930 *
931 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
932 * since the pcap module already does name-to-address translation,
933 * it's already does most of the work for the ethernet address-to-name
934 * translation, so we just pcap_next_etherent as a convenience.
935 */
936static void
[1043048]937init_etherarray(netdissect_options *ndo)
[8440506]938{
939        register const struct etherlist *el;
940        register struct enamemem *tp;
941#ifdef USE_ETHER_NTOHOST
942        char name[256];
943#else
944        register struct pcap_etherent *ep;
945        register FILE *fp;
946
947        /* Suck in entire ethers file */
948        fp = fopen(PCAP_ETHERS_FILE, "r");
949        if (fp != NULL) {
950                while ((ep = pcap_next_etherent(fp)) != NULL) {
[1043048]951                        tp = lookup_emem(ndo, ep->addr);
[8440506]952                        tp->e_name = strdup(ep->name);
[1043048]953                        if (tp->e_name == NULL)
954                                (*ndo->ndo_error)(ndo,
955                                                  "init_etherarray: strdup(ep->addr)");
[8440506]956                }
957                (void)fclose(fp);
958        }
959#endif
960
961        /* Hardwire some ethernet names */
962        for (el = etherlist; el->name != NULL; ++el) {
[1043048]963                tp = lookup_emem(ndo, el->addr);
[8440506]964                /* Don't override existing name */
965                if (tp->e_name != NULL)
966                        continue;
967
968#ifdef USE_ETHER_NTOHOST
969                /*
970                 * Use YP/NIS version of name if available.
971                 */
[1043048]972                if (ether_ntohost(name, (const struct ether_addr *)el->addr) == 0) {
[8440506]973                        tp->e_name = strdup(name);
[1043048]974                        if (tp->e_name == NULL)
975                                (*ndo->ndo_error)(ndo,
976                                                  "init_etherarray: strdup(name)");
[8440506]977                        continue;
978                }
979#endif
980                tp->e_name = el->name;
981        }
982}
983
984static const struct tok ipxsap_db[] = {
985        { 0x0000, "Unknown" },
986        { 0x0001, "User" },
987        { 0x0002, "User Group" },
988        { 0x0003, "PrintQueue" },
989        { 0x0004, "FileServer" },
990        { 0x0005, "JobServer" },
991        { 0x0006, "Gateway" },
992        { 0x0007, "PrintServer" },
993        { 0x0008, "ArchiveQueue" },
994        { 0x0009, "ArchiveServer" },
995        { 0x000a, "JobQueue" },
996        { 0x000b, "Administration" },
997        { 0x000F, "Novell TI-RPC" },
998        { 0x0017, "Diagnostics" },
999        { 0x0020, "NetBIOS" },
1000        { 0x0021, "NAS SNA Gateway" },
1001        { 0x0023, "NACS AsyncGateway" },
1002        { 0x0024, "RemoteBridge/RoutingService" },
1003        { 0x0026, "BridgeServer" },
1004        { 0x0027, "TCP/IP Gateway" },
1005        { 0x0028, "Point-to-point X.25 BridgeServer" },
1006        { 0x0029, "3270 Gateway" },
1007        { 0x002a, "CHI Corp" },
1008        { 0x002c, "PC Chalkboard" },
1009        { 0x002d, "TimeSynchServer" },
1010        { 0x002e, "ARCserve5.0/PalindromeBackup" },
1011        { 0x0045, "DI3270 Gateway" },
1012        { 0x0047, "AdvertisingPrintServer" },
1013        { 0x004a, "NetBlazerModems" },
1014        { 0x004b, "BtrieveVAP" },
1015        { 0x004c, "NetwareSQL" },
1016        { 0x004d, "XtreeNetwork" },
1017        { 0x0050, "BtrieveVAP4.11" },
1018        { 0x0052, "QuickLink" },
1019        { 0x0053, "PrintQueueUser" },
1020        { 0x0058, "Multipoint X.25 Router" },
1021        { 0x0060, "STLB/NLM" },
1022        { 0x0064, "ARCserve" },
1023        { 0x0066, "ARCserve3.0" },
1024        { 0x0072, "WAN CopyUtility" },
1025        { 0x007a, "TES-NetwareVMS" },
1026        { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
1027        { 0x0095, "DDA OBGYN" },
1028        { 0x0098, "NetwareAccessServer" },
1029        { 0x009a, "Netware for VMS II/NamedPipeServer" },
1030        { 0x009b, "NetwareAccessServer" },
1031        { 0x009e, "PortableNetwareServer/SunLinkNVT" },
1032        { 0x00a1, "PowerchuteAPC UPS" },
1033        { 0x00aa, "LAWserve" },
1034        { 0x00ac, "CompaqIDA StatusMonitor" },
1035        { 0x0100, "PIPE STAIL" },
1036        { 0x0102, "LAN ProtectBindery" },
1037        { 0x0103, "OracleDataBaseServer" },
1038        { 0x0107, "Netware386/RSPX RemoteConsole" },
1039        { 0x010f, "NovellSNA Gateway" },
1040        { 0x0111, "TestServer" },
1041        { 0x0112, "HP PrintServer" },
1042        { 0x0114, "CSA MUX" },
1043        { 0x0115, "CSA LCA" },
1044        { 0x0116, "CSA CM" },
1045        { 0x0117, "CSA SMA" },
1046        { 0x0118, "CSA DBA" },
1047        { 0x0119, "CSA NMA" },
1048        { 0x011a, "CSA SSA" },
1049        { 0x011b, "CSA STATUS" },
1050        { 0x011e, "CSA APPC" },
1051        { 0x0126, "SNA TEST SSA Profile" },
1052        { 0x012a, "CSA TRACE" },
1053        { 0x012b, "NetwareSAA" },
1054        { 0x012e, "IKARUS VirusScan" },
1055        { 0x0130, "CommunicationsExecutive" },
1056        { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
1057        { 0x0135, "NetwareNamingServicesProfile" },
1058        { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
1059        { 0x0141, "LAN SpoolServer" },
1060        { 0x0152, "IRMALAN Gateway" },
1061        { 0x0154, "NamedPipeServer" },
1062        { 0x0166, "NetWareManagement" },
1063        { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
1064        { 0x0173, "Compaq" },
1065        { 0x0174, "Compaq SNMP Agent" },
1066        { 0x0175, "Compaq" },
1067        { 0x0180, "XTreeServer/XTreeTools" },
1068        { 0x018A, "NASI ServicesBroadcastServer" },
1069        { 0x01b0, "GARP Gateway" },
1070        { 0x01b1, "Binfview" },
1071        { 0x01bf, "IntelLanDeskManager" },
1072        { 0x01ca, "AXTEC" },
1073        { 0x01cb, "ShivaNetModem/E" },
1074        { 0x01cc, "ShivaLanRover/E" },
1075        { 0x01cd, "ShivaLanRover/T" },
1076        { 0x01ce, "ShivaUniversal" },
1077        { 0x01d8, "CastelleFAXPressServer" },
1078        { 0x01da, "CastelleLANPressPrintServer" },
1079        { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
1080        { 0x01f0, "LEGATO" },
1081        { 0x01f5, "LEGATO" },
1082        { 0x0233, "NMS Agent/NetwareManagementAgent" },
1083        { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
1084        { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
1085        { 0x023a, "LANtern" },
1086        { 0x023c, "MAVERICK" },
1087        { 0x023f, "NovellSMDR" },
1088        { 0x024e, "NetwareConnect" },
1089        { 0x024f, "NASI ServerBroadcast Cisco" },
1090        { 0x026a, "NMS ServiceConsole" },
1091        { 0x026b, "TimeSynchronizationServer Netware 4.x" },
1092        { 0x0278, "DirectoryServer Netware 4.x" },
1093        { 0x027b, "NetwareManagementAgent" },
1094        { 0x0280, "Novell File and Printer Sharing Service for PC" },
1095        { 0x0304, "NovellSAA Gateway" },
1096        { 0x0308, "COM/VERMED" },
1097        { 0x030a, "GalacticommWorldgroupServer" },
1098        { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1099        { 0x0320, "AttachmateGateway" },
1100        { 0x0327, "MicrosoftDiagnostiocs" },
1101        { 0x0328, "WATCOM SQL Server" },
1102        { 0x0335, "MultiTechSystems MultisynchCommServer" },
1103        { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1104        { 0x0355, "ArcadaBackupExec" },
1105        { 0x0358, "MSLCD1" },
1106        { 0x0361, "NETINELO" },
1107        { 0x037e, "Powerchute UPS Monitoring" },
1108        { 0x037f, "ViruSafeNotify" },
1109        { 0x0386, "HP Bridge" },
1110        { 0x0387, "HP Hub" },
1111        { 0x0394, "NetWare SAA Gateway" },
1112        { 0x039b, "LotusNotes" },
1113        { 0x03b7, "CertusAntiVirus" },
1114        { 0x03c4, "ARCserve4.0" },
1115        { 0x03c7, "LANspool3.5" },
1116        { 0x03d7, "LexmarkPrinterServer" },
1117        { 0x03d8, "LexmarkXLE PrinterServer" },
1118        { 0x03dd, "BanyanENS NetwareClient" },
1119        { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1120        { 0x03e1, "UnivelUnixware" },
1121        { 0x03e4, "UnivelUnixware" },
1122        { 0x03fc, "IntelNetport" },
1123        { 0x03fd, "PrintServerQueue" },
1124        { 0x040A, "ipnServer" },
1125        { 0x040D, "LVERRMAN" },
1126        { 0x040E, "LVLIC" },
1127        { 0x0414, "NET Silicon (DPI)/Kyocera" },
1128        { 0x0429, "SiteLockVirus" },
1129        { 0x0432, "UFHELPR???" },
1130        { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1131        { 0x0444, "MicrosoftNT SNA Server" },
1132        { 0x0448, "Oracle" },
1133        { 0x044c, "ARCserve5.01" },
1134        { 0x0457, "CanonGP55" },
1135        { 0x045a, "QMS Printers" },
1136        { 0x045b, "DellSCSI Array" },
1137        { 0x0491, "NetBlazerModems" },
1138        { 0x04ac, "OnTimeScheduler" },
1139        { 0x04b0, "CD-Net" },
1140        { 0x0513, "EmulexNQA" },
1141        { 0x0520, "SiteLockChecks" },
1142        { 0x0529, "SiteLockChecks" },
1143        { 0x052d, "CitrixOS2 AppServer" },
1144        { 0x0535, "Tektronix" },
1145        { 0x0536, "Milan" },
1146        { 0x055d, "Attachmate SNA gateway" },
1147        { 0x056b, "IBM8235 ModemServer" },
1148        { 0x056c, "ShivaLanRover/E PLUS" },
1149        { 0x056d, "ShivaLanRover/T PLUS" },
1150        { 0x0580, "McAfeeNetShield" },
1151        { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1152        { 0x05BA, "CompatibleSystemsRouters" },
1153        { 0x05BE, "CheyenneHierarchicalStorageManager" },
1154        { 0x0606, "JCWatermarkImaging" },
1155        { 0x060c, "AXISNetworkPrinter" },
1156        { 0x0610, "AdaptecSCSIManagement" },
1157        { 0x0621, "IBM AntiVirus" },
1158        { 0x0640, "Windows95 RemoteRegistryService" },
1159        { 0x064e, "MicrosoftIIS" },
1160        { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1161        { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1162        { 0x076C, "Xerox" },
1163        { 0x079b, "ShivaLanRover/E 115" },
1164        { 0x079c, "ShivaLanRover/T 115" },
1165        { 0x07B4, "CubixWorldDesk" },
1166        { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1167        { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1168        { 0x0810, "ELAN License Server Demo" },
1169        { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1170        { 0x086a, "ISSC Collector" },
1171        { 0x087f, "ISSC DAS AgentAIX" },
1172        { 0x0880, "Intel Netport PRO" },
1173        { 0x0881, "Intel Netport PRO" },
1174        { 0x0b29, "SiteLock" },
1175        { 0x0c29, "SiteLockApplications" },
1176        { 0x0c2c, "LicensingServer" },
1177        { 0x2101, "PerformanceTechnologyInstantInternet" },
1178        { 0x2380, "LAI SiteLock" },
1179        { 0x238c, "MeetingMaker" },
1180        { 0x4808, "SiteLockServer/SiteLockMetering" },
1181        { 0x5555, "SiteLockUser" },
1182        { 0x6312, "Tapeware" },
1183        { 0x6f00, "RabbitGateway" },
1184        { 0x7703, "MODEM" },
1185        { 0x8002, "NetPortPrinters" },
1186        { 0x8008, "WordPerfectNetworkVersion" },
1187        { 0x85BE, "Cisco EIGRP" },
1188        { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1189        { 0x9000, "McAfeeNetShield" },
1190        { 0x9604, "CSA-NT_MON" },
1191        { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1192        { 0xf11f, "SiteLockMetering" },
1193        { 0xf1ff, "SiteLock" },
1194        { 0xf503, "Microsoft SQL Server" },
1195        { 0xF905, "IBM TimeAndPlace" },
1196        { 0xfbfb, "TopCallIII FaxServer" },
1197        { 0xffff, "AnyService/Wildcard" },
1198        { 0, (char *)0 }
1199};
1200
1201static void
[1043048]1202init_ipxsaparray(netdissect_options *ndo)
[8440506]1203{
1204        register int i;
1205        register struct hnamemem *table;
1206
1207        for (i = 0; ipxsap_db[i].s != NULL; i++) {
1208                int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1209                table = &ipxsaptable[j];
1210                while (table->name)
1211                        table = table->nxt;
1212                table->name = ipxsap_db[i].s;
1213                table->addr = htons(ipxsap_db[i].v);
[1043048]1214                table->nxt = newhnamemem(ndo);
[8440506]1215        }
1216}
1217
1218/*
1219 * Initialize the address to name translation machinery.  We map all
[1043048]1220 * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
1221 * (i.e., to prevent blocking on the nameserver).  localnet is the IP address
[8440506]1222 * of the local network.  mask is its subnet mask.
1223 */
1224void
[1043048]1225init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
[8440506]1226{
[1043048]1227        if (ndo->ndo_fflag) {
[8440506]1228                f_localnet = localnet;
1229                f_netmask = mask;
1230        }
[1043048]1231        if (ndo->ndo_nflag)
[8440506]1232                /*
1233                 * Simplest way to suppress names.
1234                 */
1235                return;
1236
[1043048]1237        init_etherarray(ndo);
1238        init_servarray(ndo);
1239        init_eprotoarray(ndo);
1240        init_protoidarray(ndo);
1241        init_ipxsaparray(ndo);
[8440506]1242}
1243
1244const char *
[1043048]1245dnaddr_string(netdissect_options *ndo, u_short dnaddr)
[8440506]1246{
1247        register struct hnamemem *tp;
1248
[1043048]1249        for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != NULL;
[8440506]1250             tp = tp->nxt)
1251                if (tp->addr == dnaddr)
1252                        return (tp->name);
1253
1254        tp->addr = dnaddr;
[1043048]1255        tp->nxt = newhnamemem(ndo);
1256        if (ndo->ndo_nflag)
1257                tp->name = dnnum_string(ndo, dnaddr);
[8440506]1258        else
[1043048]1259                tp->name = dnname_string(ndo, dnaddr);
[8440506]1260
1261        return(tp->name);
1262}
1263
1264/* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1265struct hnamemem *
[1043048]1266newhnamemem(netdissect_options *ndo)
[8440506]1267{
1268        register struct hnamemem *p;
[24631e2]1269#ifdef __rtems__
1270        __section(".rtemsrwset.bsd_prog_tcpdump.content")
1271#endif /* __rtems__ */
[8440506]1272        static struct hnamemem *ptr = NULL;
[24631e2]1273#ifdef __rtems__
1274        __section(".rtemsrwset.bsd_prog_tcpdump.content")
1275#endif /* __rtems__ */
[8440506]1276        static u_int num = 0;
1277
1278        if (num  <= 0) {
1279                num = 64;
1280                ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1281                if (ptr == NULL)
[1043048]1282                        (*ndo->ndo_error)(ndo, "newhnamemem: calloc");
[8440506]1283        }
1284        --num;
1285        p = ptr++;
1286        return (p);
1287}
1288
1289/* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1290struct h6namemem *
[1043048]1291newh6namemem(netdissect_options *ndo)
[8440506]1292{
1293        register struct h6namemem *p;
[24631e2]1294#ifdef __rtems__
1295        __section(".rtemsrwset.bsd_prog_tcpdump.content")
1296#endif /* __rtems__ */
[8440506]1297        static struct h6namemem *ptr = NULL;
[24631e2]1298#ifdef __rtems__
1299        __section(".rtemsrwset.bsd_prog_tcpdump.content")
1300#endif /* __rtems__ */
[8440506]1301        static u_int num = 0;
1302
1303        if (num  <= 0) {
1304                num = 64;
1305                ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1306                if (ptr == NULL)
[1043048]1307                        (*ndo->ndo_error)(ndo, "newh6namemem: calloc");
[8440506]1308        }
1309        --num;
1310        p = ptr++;
1311        return (p);
1312}
[1043048]1313
1314/* Represent TCI part of the 802.1Q 4-octet tag as text. */
1315const char *
1316ieee8021q_tci_string(const uint16_t tci)
1317{
1318        static char buf[128];
1319        snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
1320                 tci & 0xfff,
1321                 tci >> 13,
1322                 (tci & 0x1000) ? ", DEI" : "");
1323        return buf;
1324}
1325#ifdef __rtems__
1326#include "rtems-bsd-tcpdump-addrtoname-data.h"
1327#endif /* __rtems__ */
Note: See TracBrowser for help on using the repository browser.