141c99275SPeter Avalos /*
241c99275SPeter Avalos * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
341c99275SPeter Avalos * The Regents of the University of California. All rights reserved.
441c99275SPeter Avalos *
541c99275SPeter Avalos * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos * modification, are permitted provided that: (1) source code distributions
741c99275SPeter Avalos * retain the above copyright notice and this paragraph in its entirety, (2)
841c99275SPeter Avalos * distributions including binary code include the above copyright notice and
941c99275SPeter Avalos * this paragraph in its entirety in the documentation or other materials
1041c99275SPeter Avalos * provided with the distribution, and (3) all advertising materials mentioning
1141c99275SPeter Avalos * features or use of this software display the following acknowledgement:
1241c99275SPeter Avalos * ``This product includes software developed by the University of California,
1341c99275SPeter Avalos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1441c99275SPeter Avalos * the University nor the names of its contributors may be used to endorse
1541c99275SPeter Avalos * or promote products derived from this software without specific prior
1641c99275SPeter Avalos * written permission.
1741c99275SPeter Avalos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1841c99275SPeter Avalos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1941c99275SPeter Avalos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2041c99275SPeter Avalos *
2141c99275SPeter Avalos * Internet, ethernet, port, and protocol string to address
2241c99275SPeter Avalos * and address to string conversion routines
2341c99275SPeter Avalos */
2441c99275SPeter Avalos
2541c99275SPeter Avalos #ifdef HAVE_CONFIG_H
26*ed775ee7SAntonio Huete Jimenez #include <config.h>
2741c99275SPeter Avalos #endif
2841c99275SPeter Avalos
29*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_CASPER
30*ed775ee7SAntonio Huete Jimenez #include <libcasper.h>
31*ed775ee7SAntonio Huete Jimenez #include <casper/cap_dns.h>
32*ed775ee7SAntonio Huete Jimenez #endif /* HAVE_CASPER */
3341c99275SPeter Avalos
34*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
35*ed775ee7SAntonio Huete Jimenez
36*ed775ee7SAntonio Huete Jimenez #ifdef _WIN32
37*ed775ee7SAntonio Huete Jimenez /*
38*ed775ee7SAntonio Huete Jimenez * We have our own ether_ntohost(), reading from the system's
39*ed775ee7SAntonio Huete Jimenez * Ethernet address file.
40*ed775ee7SAntonio Huete Jimenez */
41*ed775ee7SAntonio Huete Jimenez #include "missing/win_ether_ntohost.h"
42*ed775ee7SAntonio Huete Jimenez #else
4341c99275SPeter Avalos #ifdef USE_ETHER_NTOHOST
44*ed775ee7SAntonio Huete Jimenez #if defined(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
45*ed775ee7SAntonio Huete Jimenez /*
46*ed775ee7SAntonio Huete Jimenez * OK, just include <net/ethernet.h>.
47*ed775ee7SAntonio Huete Jimenez */
48*ed775ee7SAntonio Huete Jimenez #include <net/ethernet.h>
49*ed775ee7SAntonio Huete Jimenez #elif defined(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
50*ed775ee7SAntonio Huete Jimenez /*
51*ed775ee7SAntonio Huete Jimenez * OK, just include <netinet/ether.h>
52*ed775ee7SAntonio Huete Jimenez */
5341c99275SPeter Avalos #include <netinet/ether.h>
54*ed775ee7SAntonio Huete Jimenez #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
55*ed775ee7SAntonio Huete Jimenez /*
56*ed775ee7SAntonio Huete Jimenez * OK, just include <sys/ethernet.h>
57*ed775ee7SAntonio Huete Jimenez */
58*ed775ee7SAntonio Huete Jimenez #include <sys/ethernet.h>
59*ed775ee7SAntonio Huete Jimenez #elif defined(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
60*ed775ee7SAntonio Huete Jimenez /*
61*ed775ee7SAntonio Huete Jimenez * OK, just include <arpa/inet.h>
62*ed775ee7SAntonio Huete Jimenez */
63*ed775ee7SAntonio Huete Jimenez #include <arpa/inet.h>
64*ed775ee7SAntonio Huete Jimenez #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
65*ed775ee7SAntonio Huete Jimenez /*
66*ed775ee7SAntonio Huete Jimenez * OK, include <netinet/if_ether.h>, after all the other stuff we
67*ed775ee7SAntonio Huete Jimenez * need to include or define for its benefit.
68*ed775ee7SAntonio Huete Jimenez */
69*ed775ee7SAntonio Huete Jimenez #define NEED_NETINET_IF_ETHER_H
70*ed775ee7SAntonio Huete Jimenez #else
71*ed775ee7SAntonio Huete Jimenez /*
72*ed775ee7SAntonio Huete Jimenez * We'll have to declare it ourselves.
73*ed775ee7SAntonio Huete Jimenez * If <netinet/if_ether.h> defines struct ether_addr, include
74*ed775ee7SAntonio Huete Jimenez * it. Otherwise, define it ourselves.
75*ed775ee7SAntonio Huete Jimenez */
76*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_STRUCT_ETHER_ADDR
77*ed775ee7SAntonio Huete Jimenez #define NEED_NETINET_IF_ETHER_H
78*ed775ee7SAntonio Huete Jimenez #else /* HAVE_STRUCT_ETHER_ADDR */
7941c99275SPeter Avalos struct ether_addr {
80*ed775ee7SAntonio Huete Jimenez /* Beware FreeBSD calls this "octet". */
81*ed775ee7SAntonio Huete Jimenez unsigned char ether_addr_octet[MAC_ADDR_LEN];
8241c99275SPeter Avalos };
83*ed775ee7SAntonio Huete Jimenez #endif /* HAVE_STRUCT_ETHER_ADDR */
84*ed775ee7SAntonio Huete Jimenez #endif /* what declares ether_ntohost() */
8541c99275SPeter Avalos
86*ed775ee7SAntonio Huete Jimenez #ifdef NEED_NETINET_IF_ETHER_H
87*ed775ee7SAntonio Huete Jimenez #include <net/if.h> /* Needed on some platforms */
88*ed775ee7SAntonio Huete Jimenez #include <netinet/in.h> /* Needed on some platforms */
89*ed775ee7SAntonio Huete Jimenez #include <netinet/if_ether.h>
90*ed775ee7SAntonio Huete Jimenez #endif /* NEED_NETINET_IF_ETHER_H */
91*ed775ee7SAntonio Huete Jimenez
92*ed775ee7SAntonio Huete Jimenez #ifndef HAVE_DECL_ETHER_NTOHOST
93*ed775ee7SAntonio Huete Jimenez /*
94*ed775ee7SAntonio Huete Jimenez * No header declares it, so declare it ourselves.
95*ed775ee7SAntonio Huete Jimenez */
96*ed775ee7SAntonio Huete Jimenez extern int ether_ntohost(char *, const struct ether_addr *);
97*ed775ee7SAntonio Huete Jimenez #endif /* !defined(HAVE_DECL_ETHER_NTOHOST) */
9841c99275SPeter Avalos #endif /* USE_ETHER_NTOHOST */
99*ed775ee7SAntonio Huete Jimenez #endif /* _WIN32 */
10041c99275SPeter Avalos
10141c99275SPeter Avalos #include <pcap.h>
10241c99275SPeter Avalos #include <pcap-namedb.h>
103*ed775ee7SAntonio Huete Jimenez #ifndef HAVE_GETSERVENT
104*ed775ee7SAntonio Huete Jimenez #include <getservent.h>
105*ed775ee7SAntonio Huete Jimenez #endif
10641c99275SPeter Avalos #include <signal.h>
10741c99275SPeter Avalos #include <stdio.h>
10841c99275SPeter Avalos #include <string.h>
10941c99275SPeter Avalos #include <stdlib.h>
11041c99275SPeter Avalos
111411677aeSAaron LI #include "netdissect.h"
11241c99275SPeter Avalos #include "addrtoname.h"
113411677aeSAaron LI #include "addrtostr.h"
114411677aeSAaron LI #include "ethertype.h"
11541c99275SPeter Avalos #include "llc.h"
11641c99275SPeter Avalos #include "extract.h"
11741c99275SPeter Avalos #include "oui.h"
11841c99275SPeter Avalos
11941c99275SPeter Avalos /*
12041c99275SPeter Avalos * hash tables for whatever-to-name translations
12141c99275SPeter Avalos *
122*ed775ee7SAntonio Huete Jimenez * ndo_error() called on strdup(3) failure with S_ERR_ND_MEM_ALLOC status
12341c99275SPeter Avalos */
12441c99275SPeter Avalos
12541c99275SPeter Avalos #define HASHNAMESIZE 4096
12641c99275SPeter Avalos
12741c99275SPeter Avalos struct hnamemem {
128411677aeSAaron LI uint32_t addr;
12941c99275SPeter Avalos const char *name;
13041c99275SPeter Avalos struct hnamemem *nxt;
13141c99275SPeter Avalos };
13241c99275SPeter Avalos
13327bfbee1SPeter Avalos static struct hnamemem hnametable[HASHNAMESIZE];
13427bfbee1SPeter Avalos static struct hnamemem tporttable[HASHNAMESIZE];
13527bfbee1SPeter Avalos static struct hnamemem uporttable[HASHNAMESIZE];
13627bfbee1SPeter Avalos static struct hnamemem eprototable[HASHNAMESIZE];
13727bfbee1SPeter Avalos static struct hnamemem dnaddrtable[HASHNAMESIZE];
13827bfbee1SPeter Avalos static struct hnamemem ipxsaptable[HASHNAMESIZE];
13941c99275SPeter Avalos
140411677aeSAaron LI #ifdef _WIN32
14141c99275SPeter Avalos /*
14241c99275SPeter Avalos * fake gethostbyaddr for Win2k/XP
14341c99275SPeter Avalos * gethostbyaddr() returns incorrect value when AF_INET6 is passed
14441c99275SPeter Avalos * to 3rd argument.
14541c99275SPeter Avalos *
14641c99275SPeter Avalos * h_name in struct hostent is only valid.
14741c99275SPeter Avalos */
14841c99275SPeter Avalos static struct hostent *
win32_gethostbyaddr(const char * addr,int len,int type)14941c99275SPeter Avalos win32_gethostbyaddr(const char *addr, int len, int type)
15041c99275SPeter Avalos {
15141c99275SPeter Avalos static struct hostent host;
15241c99275SPeter Avalos static char hostbuf[NI_MAXHOST];
15341c99275SPeter Avalos char hname[NI_MAXHOST];
15441c99275SPeter Avalos struct sockaddr_in6 addr6;
15541c99275SPeter Avalos
15641c99275SPeter Avalos host.h_name = hostbuf;
15741c99275SPeter Avalos switch (type) {
15841c99275SPeter Avalos case AF_INET:
15941c99275SPeter Avalos return gethostbyaddr(addr, len, type);
16041c99275SPeter Avalos break;
16141c99275SPeter Avalos case AF_INET6:
16241c99275SPeter Avalos memset(&addr6, 0, sizeof(addr6));
16341c99275SPeter Avalos addr6.sin6_family = AF_INET6;
16441c99275SPeter Avalos memcpy(&addr6.sin6_addr, addr, len);
16541c99275SPeter Avalos if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
16641c99275SPeter Avalos hname, sizeof(hname), NULL, 0, 0)) {
16741c99275SPeter Avalos return NULL;
16841c99275SPeter Avalos } else {
169411677aeSAaron LI strlcpy(host.h_name, hname, NI_MAXHOST);
17041c99275SPeter Avalos return &host;
17141c99275SPeter Avalos }
17241c99275SPeter Avalos break;
17341c99275SPeter Avalos default:
17441c99275SPeter Avalos return NULL;
17541c99275SPeter Avalos }
17641c99275SPeter Avalos }
17741c99275SPeter Avalos #define gethostbyaddr win32_gethostbyaddr
178411677aeSAaron LI #endif /* _WIN32 */
17941c99275SPeter Avalos
18041c99275SPeter Avalos struct h6namemem {
181*ed775ee7SAntonio Huete Jimenez nd_ipv6 addr;
18241c99275SPeter Avalos char *name;
18341c99275SPeter Avalos struct h6namemem *nxt;
18441c99275SPeter Avalos };
18541c99275SPeter Avalos
18627bfbee1SPeter Avalos static struct h6namemem h6nametable[HASHNAMESIZE];
18741c99275SPeter Avalos
18841c99275SPeter Avalos struct enamemem {
18941c99275SPeter Avalos u_short e_addr0;
19041c99275SPeter Avalos u_short e_addr1;
19141c99275SPeter Avalos u_short e_addr2;
19241c99275SPeter Avalos const char *e_name;
19341c99275SPeter Avalos u_char *e_nsap; /* used only for nsaptable[] */
19441c99275SPeter Avalos struct enamemem *e_nxt;
19541c99275SPeter Avalos };
19641c99275SPeter Avalos
19727bfbee1SPeter Avalos static struct enamemem enametable[HASHNAMESIZE];
19827bfbee1SPeter Avalos static struct enamemem nsaptable[HASHNAMESIZE];
199411677aeSAaron LI
200411677aeSAaron LI struct bsnamemem {
201411677aeSAaron LI u_short bs_addr0;
202411677aeSAaron LI u_short bs_addr1;
203411677aeSAaron LI u_short bs_addr2;
204411677aeSAaron LI const char *bs_name;
205411677aeSAaron LI u_char *bs_bytes;
206411677aeSAaron LI unsigned int bs_nbytes;
207411677aeSAaron LI struct bsnamemem *bs_nxt;
208411677aeSAaron LI };
209411677aeSAaron LI
210411677aeSAaron LI static struct bsnamemem bytestringtable[HASHNAMESIZE];
21141c99275SPeter Avalos
21241c99275SPeter Avalos struct protoidmem {
213411677aeSAaron LI uint32_t p_oui;
21441c99275SPeter Avalos u_short p_proto;
21541c99275SPeter Avalos const char *p_name;
21641c99275SPeter Avalos struct protoidmem *p_nxt;
21741c99275SPeter Avalos };
21841c99275SPeter Avalos
21927bfbee1SPeter Avalos static struct protoidmem protoidtable[HASHNAMESIZE];
22041c99275SPeter Avalos
22141c99275SPeter Avalos /*
22241c99275SPeter Avalos * A faster replacement for inet_ntoa().
22341c99275SPeter Avalos */
22441c99275SPeter Avalos const char *
intoa(uint32_t addr)225411677aeSAaron LI intoa(uint32_t addr)
22641c99275SPeter Avalos {
227*ed775ee7SAntonio Huete Jimenez char *cp;
228*ed775ee7SAntonio Huete Jimenez u_int byte;
229*ed775ee7SAntonio Huete Jimenez int n;
23041c99275SPeter Avalos static char buf[sizeof(".xxx.xxx.xxx.xxx")];
23141c99275SPeter Avalos
232*ed775ee7SAntonio Huete Jimenez addr = ntohl(addr);
23341c99275SPeter Avalos cp = buf + sizeof(buf);
23441c99275SPeter Avalos *--cp = '\0';
23541c99275SPeter Avalos
23641c99275SPeter Avalos n = 4;
23741c99275SPeter Avalos do {
23841c99275SPeter Avalos byte = addr & 0xff;
239*ed775ee7SAntonio Huete Jimenez *--cp = (char)(byte % 10) + '0';
24041c99275SPeter Avalos byte /= 10;
24141c99275SPeter Avalos if (byte > 0) {
242*ed775ee7SAntonio Huete Jimenez *--cp = (char)(byte % 10) + '0';
24341c99275SPeter Avalos byte /= 10;
24441c99275SPeter Avalos if (byte > 0)
245*ed775ee7SAntonio Huete Jimenez *--cp = (char)byte + '0';
24641c99275SPeter Avalos }
24741c99275SPeter Avalos *--cp = '.';
24841c99275SPeter Avalos addr >>= 8;
24941c99275SPeter Avalos } while (--n > 0);
25041c99275SPeter Avalos
25141c99275SPeter Avalos return cp + 1;
25241c99275SPeter Avalos }
25341c99275SPeter Avalos
254411677aeSAaron LI static uint32_t f_netmask;
255411677aeSAaron LI static uint32_t f_localnet;
256*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_CASPER
257*ed775ee7SAntonio Huete Jimenez extern cap_channel_t *capdns;
258*ed775ee7SAntonio Huete Jimenez #endif
25941c99275SPeter Avalos
26041c99275SPeter Avalos /*
26141c99275SPeter Avalos * Return a name for the IP address pointed to by ap. This address
26241c99275SPeter Avalos * is assumed to be in network byte order.
26341c99275SPeter Avalos *
264*ed775ee7SAntonio Huete Jimenez * NOTE: ap is *NOT* necessarily part of the packet data, so you
265*ed775ee7SAntonio Huete Jimenez * *CANNOT* use the ND_TCHECK_* or ND_TTEST_* macros on it. Furthermore,
26641c99275SPeter Avalos * even in cases where it *is* part of the packet data, the caller
26741c99275SPeter Avalos * would still have to check for a null return value, even if it's
26841c99275SPeter Avalos * just printing the return value with "%s" - not all versions of
26941c99275SPeter Avalos * printf print "(null)" with "%s" and a null pointer, some of them
27041c99275SPeter Avalos * don't check for a null pointer and crash in that case.
27141c99275SPeter Avalos *
27241c99275SPeter Avalos * The callers of this routine should, before handing this routine
27341c99275SPeter Avalos * a pointer to packet data, be sure that the data is present in
27441c99275SPeter Avalos * the packet buffer. They should probably do those checks anyway,
27541c99275SPeter Avalos * as other data at that layer might not be IP addresses, and it
27641c99275SPeter Avalos * also needs to check whether they're present in the packet buffer.
27741c99275SPeter Avalos */
27841c99275SPeter Avalos const char *
ipaddr_string(netdissect_options * ndo,const u_char * ap)279*ed775ee7SAntonio Huete Jimenez ipaddr_string(netdissect_options *ndo, const u_char *ap)
28041c99275SPeter Avalos {
281*ed775ee7SAntonio Huete Jimenez struct hostent *hp;
282411677aeSAaron LI uint32_t addr;
283411677aeSAaron LI struct hnamemem *p;
28441c99275SPeter Avalos
28541c99275SPeter Avalos memcpy(&addr, ap, sizeof(addr));
28641c99275SPeter Avalos p = &hnametable[addr & (HASHNAMESIZE-1)];
28741c99275SPeter Avalos for (; p->nxt; p = p->nxt) {
28841c99275SPeter Avalos if (p->addr == addr)
28941c99275SPeter Avalos return (p->name);
29041c99275SPeter Avalos }
29141c99275SPeter Avalos p->addr = addr;
292411677aeSAaron LI p->nxt = newhnamemem(ndo);
29341c99275SPeter Avalos
29441c99275SPeter Avalos /*
29541c99275SPeter Avalos * Print names unless:
29641c99275SPeter Avalos * (1) -n was given.
29741c99275SPeter Avalos * (2) Address is foreign and -f was given. (If -f was not
29841c99275SPeter Avalos * given, f_netmask and f_localnet are 0 and the test
29941c99275SPeter Avalos * evaluates to true)
30041c99275SPeter Avalos */
301411677aeSAaron LI if (!ndo->ndo_nflag &&
30241c99275SPeter Avalos (addr & f_netmask) == f_localnet) {
303*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_CASPER
304*ed775ee7SAntonio Huete Jimenez if (capdns != NULL) {
305*ed775ee7SAntonio Huete Jimenez hp = cap_gethostbyaddr(capdns, (char *)&addr, 4,
306*ed775ee7SAntonio Huete Jimenez AF_INET);
307*ed775ee7SAntonio Huete Jimenez } else
308*ed775ee7SAntonio Huete Jimenez #endif
30941c99275SPeter Avalos hp = gethostbyaddr((char *)&addr, 4, AF_INET);
31041c99275SPeter Avalos if (hp) {
31141c99275SPeter Avalos char *dotp;
31241c99275SPeter Avalos
31341c99275SPeter Avalos p->name = strdup(hp->h_name);
314411677aeSAaron LI if (p->name == NULL)
315*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
316*ed775ee7SAntonio Huete Jimenez "%s: strdup(hp->h_name)", __func__);
317411677aeSAaron LI if (ndo->ndo_Nflag) {
31841c99275SPeter Avalos /* Remove domain qualifications */
31941c99275SPeter Avalos dotp = strchr(p->name, '.');
32041c99275SPeter Avalos if (dotp)
32141c99275SPeter Avalos *dotp = '\0';
32241c99275SPeter Avalos }
32341c99275SPeter Avalos return (p->name);
32441c99275SPeter Avalos }
32541c99275SPeter Avalos }
32641c99275SPeter Avalos p->name = strdup(intoa(addr));
327411677aeSAaron LI if (p->name == NULL)
328*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
329*ed775ee7SAntonio Huete Jimenez "%s: strdup(intoa(addr))", __func__);
33041c99275SPeter Avalos return (p->name);
33141c99275SPeter Avalos }
33241c99275SPeter Avalos
33341c99275SPeter Avalos /*
33441c99275SPeter Avalos * Return a name for the IP6 address pointed to by ap. This address
33541c99275SPeter Avalos * is assumed to be in network byte order.
33641c99275SPeter Avalos */
33741c99275SPeter Avalos const char *
ip6addr_string(netdissect_options * ndo,const u_char * ap)338*ed775ee7SAntonio Huete Jimenez ip6addr_string(netdissect_options *ndo, const u_char *ap)
33941c99275SPeter Avalos {
340*ed775ee7SAntonio Huete Jimenez struct hostent *hp;
341411677aeSAaron LI union {
342*ed775ee7SAntonio Huete Jimenez nd_ipv6 addr;
343411677aeSAaron LI struct for_hash_addr {
344411677aeSAaron LI char fill[14];
345411677aeSAaron LI uint16_t d;
346411677aeSAaron LI } addra;
347411677aeSAaron LI } addr;
348411677aeSAaron LI struct h6namemem *p;
349*ed775ee7SAntonio Huete Jimenez const char *cp;
35041c99275SPeter Avalos char ntop_buf[INET6_ADDRSTRLEN];
35141c99275SPeter Avalos
35241c99275SPeter Avalos memcpy(&addr, ap, sizeof(addr));
353411677aeSAaron LI p = &h6nametable[addr.addra.d & (HASHNAMESIZE-1)];
35441c99275SPeter Avalos for (; p->nxt; p = p->nxt) {
35541c99275SPeter Avalos if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
35641c99275SPeter Avalos return (p->name);
35741c99275SPeter Avalos }
358*ed775ee7SAntonio Huete Jimenez memcpy(p->addr, addr.addr, sizeof(nd_ipv6));
359411677aeSAaron LI p->nxt = newh6namemem(ndo);
36041c99275SPeter Avalos
36141c99275SPeter Avalos /*
36241c99275SPeter Avalos * Do not print names if -n was given.
36341c99275SPeter Avalos */
364411677aeSAaron LI if (!ndo->ndo_nflag) {
365*ed775ee7SAntonio Huete Jimenez #ifdef HAVE_CASPER
366*ed775ee7SAntonio Huete Jimenez if (capdns != NULL) {
367*ed775ee7SAntonio Huete Jimenez hp = cap_gethostbyaddr(capdns, (char *)&addr,
368*ed775ee7SAntonio Huete Jimenez sizeof(addr), AF_INET6);
369*ed775ee7SAntonio Huete Jimenez } else
370*ed775ee7SAntonio Huete Jimenez #endif
371*ed775ee7SAntonio Huete Jimenez hp = gethostbyaddr((char *)&addr, sizeof(addr),
372*ed775ee7SAntonio Huete Jimenez AF_INET6);
37341c99275SPeter Avalos if (hp) {
37441c99275SPeter Avalos char *dotp;
37541c99275SPeter Avalos
37641c99275SPeter Avalos p->name = strdup(hp->h_name);
377411677aeSAaron LI if (p->name == NULL)
378*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
379*ed775ee7SAntonio Huete Jimenez "%s: strdup(hp->h_name)", __func__);
380411677aeSAaron LI if (ndo->ndo_Nflag) {
38141c99275SPeter Avalos /* Remove domain qualifications */
38241c99275SPeter Avalos dotp = strchr(p->name, '.');
38341c99275SPeter Avalos if (dotp)
38441c99275SPeter Avalos *dotp = '\0';
38541c99275SPeter Avalos }
38641c99275SPeter Avalos return (p->name);
38741c99275SPeter Avalos }
38841c99275SPeter Avalos }
389411677aeSAaron LI cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
39041c99275SPeter Avalos p->name = strdup(cp);
391411677aeSAaron LI if (p->name == NULL)
392*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
393*ed775ee7SAntonio Huete Jimenez "%s: strdup(cp)", __func__);
39441c99275SPeter Avalos return (p->name);
39541c99275SPeter Avalos }
39641c99275SPeter Avalos
397*ed775ee7SAntonio Huete Jimenez static const char hex[16] = {
398*ed775ee7SAntonio Huete Jimenez '0', '1', '2', '3', '4', '5', '6', '7',
399*ed775ee7SAntonio Huete Jimenez '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
400*ed775ee7SAntonio Huete Jimenez };
40141c99275SPeter Avalos
402*ed775ee7SAntonio Huete Jimenez /*
403*ed775ee7SAntonio Huete Jimenez * Convert an octet to two hex digits.
404*ed775ee7SAntonio Huete Jimenez *
405*ed775ee7SAntonio Huete Jimenez * Coverity appears either:
406*ed775ee7SAntonio Huete Jimenez *
407*ed775ee7SAntonio Huete Jimenez * not to believe the C standard when it asserts that a uint8_t is
408*ed775ee7SAntonio Huete Jimenez * exactly 8 bits in size;
409*ed775ee7SAntonio Huete Jimenez *
410*ed775ee7SAntonio Huete Jimenez * not to believe that an unsigned type of exactly 8 bits has a value
411*ed775ee7SAntonio Huete Jimenez * in the range of 0 to 255;
412*ed775ee7SAntonio Huete Jimenez *
413*ed775ee7SAntonio Huete Jimenez * not to believe that, for a range of unsigned values, if you shift
414*ed775ee7SAntonio Huete Jimenez * one of those values right by 4 bits, the maximum result value is
415*ed775ee7SAntonio Huete Jimenez * the maximum value shifted right by 4 bits, with no stray 1's shifted
416*ed775ee7SAntonio Huete Jimenez * in;
417*ed775ee7SAntonio Huete Jimenez *
418*ed775ee7SAntonio Huete Jimenez * not to believe that 255 >> 4 is 15;
419*ed775ee7SAntonio Huete Jimenez *
420*ed775ee7SAntonio Huete Jimenez * so it gets upset that we're taking a "tainted" unsigned value, shifting
421*ed775ee7SAntonio Huete Jimenez * it right 4 bits, and using it as an index into a 16-element array.
422*ed775ee7SAntonio Huete Jimenez *
423*ed775ee7SAntonio Huete Jimenez * So we do a stupid pointless masking of the result of the shift with
424*ed775ee7SAntonio Huete Jimenez * 0xf, to hammer the point home to Coverity.
425*ed775ee7SAntonio Huete Jimenez */
426*ed775ee7SAntonio Huete Jimenez static inline char *
octet_to_hex(char * cp,uint8_t octet)427*ed775ee7SAntonio Huete Jimenez octet_to_hex(char *cp, uint8_t octet)
428*ed775ee7SAntonio Huete Jimenez {
429*ed775ee7SAntonio Huete Jimenez *cp++ = hex[(octet >> 4) & 0xf];
430*ed775ee7SAntonio Huete Jimenez *cp++ = hex[(octet >> 0) & 0xf];
431*ed775ee7SAntonio Huete Jimenez return (cp);
432*ed775ee7SAntonio Huete Jimenez }
43341c99275SPeter Avalos
43441c99275SPeter Avalos /* Find the hash node that corresponds the ether address 'ep' */
43541c99275SPeter Avalos
436*ed775ee7SAntonio Huete Jimenez static struct enamemem *
lookup_emem(netdissect_options * ndo,const u_char * ep)437411677aeSAaron LI lookup_emem(netdissect_options *ndo, const u_char *ep)
43841c99275SPeter Avalos {
439*ed775ee7SAntonio Huete Jimenez u_int i, j, k;
44041c99275SPeter Avalos struct enamemem *tp;
44141c99275SPeter Avalos
44241c99275SPeter Avalos k = (ep[0] << 8) | ep[1];
44341c99275SPeter Avalos j = (ep[2] << 8) | ep[3];
44441c99275SPeter Avalos i = (ep[4] << 8) | ep[5];
44541c99275SPeter Avalos
44641c99275SPeter Avalos tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
44741c99275SPeter Avalos while (tp->e_nxt)
44841c99275SPeter Avalos if (tp->e_addr0 == i &&
44941c99275SPeter Avalos tp->e_addr1 == j &&
45041c99275SPeter Avalos tp->e_addr2 == k)
45141c99275SPeter Avalos return tp;
45241c99275SPeter Avalos else
45341c99275SPeter Avalos tp = tp->e_nxt;
454*ed775ee7SAntonio Huete Jimenez tp->e_addr0 = (u_short)i;
455*ed775ee7SAntonio Huete Jimenez tp->e_addr1 = (u_short)j;
456*ed775ee7SAntonio Huete Jimenez tp->e_addr2 = (u_short)k;
45741c99275SPeter Avalos tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
45841c99275SPeter Avalos if (tp->e_nxt == NULL)
459*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
46041c99275SPeter Avalos
46141c99275SPeter Avalos return tp;
46241c99275SPeter Avalos }
46341c99275SPeter Avalos
46441c99275SPeter Avalos /*
46541c99275SPeter Avalos * Find the hash node that corresponds to the bytestring 'bs'
46641c99275SPeter Avalos * with length 'nlen'
46741c99275SPeter Avalos */
46841c99275SPeter Avalos
469*ed775ee7SAntonio Huete Jimenez static struct bsnamemem *
lookup_bytestring(netdissect_options * ndo,const u_char * bs,const unsigned int nlen)470*ed775ee7SAntonio Huete Jimenez lookup_bytestring(netdissect_options *ndo, const u_char *bs,
471411677aeSAaron LI const unsigned int nlen)
47241c99275SPeter Avalos {
473411677aeSAaron LI struct bsnamemem *tp;
474*ed775ee7SAntonio Huete Jimenez u_int i, j, k;
47541c99275SPeter Avalos
47641c99275SPeter Avalos if (nlen >= 6) {
47741c99275SPeter Avalos k = (bs[0] << 8) | bs[1];
47841c99275SPeter Avalos j = (bs[2] << 8) | bs[3];
47941c99275SPeter Avalos i = (bs[4] << 8) | bs[5];
48041c99275SPeter Avalos } else if (nlen >= 4) {
48141c99275SPeter Avalos k = (bs[0] << 8) | bs[1];
48241c99275SPeter Avalos j = (bs[2] << 8) | bs[3];
48341c99275SPeter Avalos i = 0;
48441c99275SPeter Avalos } else
48541c99275SPeter Avalos i = j = k = 0;
48641c99275SPeter Avalos
48741c99275SPeter Avalos tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
488411677aeSAaron LI while (tp->bs_nxt)
489411677aeSAaron LI if (nlen == tp->bs_nbytes &&
490411677aeSAaron LI tp->bs_addr0 == i &&
491411677aeSAaron LI tp->bs_addr1 == j &&
492411677aeSAaron LI tp->bs_addr2 == k &&
493411677aeSAaron LI memcmp((const char *)bs, (const char *)(tp->bs_bytes), nlen) == 0)
49441c99275SPeter Avalos return tp;
49541c99275SPeter Avalos else
496411677aeSAaron LI tp = tp->bs_nxt;
49741c99275SPeter Avalos
498*ed775ee7SAntonio Huete Jimenez tp->bs_addr0 = (u_short)i;
499*ed775ee7SAntonio Huete Jimenez tp->bs_addr1 = (u_short)j;
500*ed775ee7SAntonio Huete Jimenez tp->bs_addr2 = (u_short)k;
50141c99275SPeter Avalos
502411677aeSAaron LI tp->bs_bytes = (u_char *) calloc(1, nlen);
503411677aeSAaron LI if (tp->bs_bytes == NULL)
504*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
505*ed775ee7SAntonio Huete Jimenez "%s: calloc", __func__);
506411677aeSAaron LI
507411677aeSAaron LI memcpy(tp->bs_bytes, bs, nlen);
508411677aeSAaron LI tp->bs_nbytes = nlen;
509411677aeSAaron LI tp->bs_nxt = (struct bsnamemem *)calloc(1, sizeof(*tp));
510411677aeSAaron LI if (tp->bs_nxt == NULL)
511*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
512*ed775ee7SAntonio Huete Jimenez "%s: calloc", __func__);
51341c99275SPeter Avalos
51441c99275SPeter Avalos return tp;
51541c99275SPeter Avalos }
51641c99275SPeter Avalos
51741c99275SPeter Avalos /* Find the hash node that corresponds the NSAP 'nsap' */
51841c99275SPeter Avalos
519*ed775ee7SAntonio Huete Jimenez static struct enamemem *
lookup_nsap(netdissect_options * ndo,const u_char * nsap,u_int nsap_length)520*ed775ee7SAntonio Huete Jimenez lookup_nsap(netdissect_options *ndo, const u_char *nsap,
521*ed775ee7SAntonio Huete Jimenez u_int nsap_length)
52241c99275SPeter Avalos {
523*ed775ee7SAntonio Huete Jimenez u_int i, j, k;
52441c99275SPeter Avalos struct enamemem *tp;
525411677aeSAaron LI const u_char *ensap;
52641c99275SPeter Avalos
527411677aeSAaron LI if (nsap_length > 6) {
528411677aeSAaron LI ensap = nsap + nsap_length - 6;
52941c99275SPeter Avalos k = (ensap[0] << 8) | ensap[1];
53041c99275SPeter Avalos j = (ensap[2] << 8) | ensap[3];
53141c99275SPeter Avalos i = (ensap[4] << 8) | ensap[5];
53241c99275SPeter Avalos }
53341c99275SPeter Avalos else
53441c99275SPeter Avalos i = j = k = 0;
53541c99275SPeter Avalos
53641c99275SPeter Avalos tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
53741c99275SPeter Avalos while (tp->e_nxt)
538411677aeSAaron LI if (nsap_length == tp->e_nsap[0] &&
539411677aeSAaron LI tp->e_addr0 == i &&
54041c99275SPeter Avalos tp->e_addr1 == j &&
54141c99275SPeter Avalos tp->e_addr2 == k &&
542411677aeSAaron LI memcmp((const char *)nsap,
543411677aeSAaron LI (char *)&(tp->e_nsap[1]), nsap_length) == 0)
54441c99275SPeter Avalos return tp;
54541c99275SPeter Avalos else
54641c99275SPeter Avalos tp = tp->e_nxt;
547*ed775ee7SAntonio Huete Jimenez tp->e_addr0 = (u_short)i;
548*ed775ee7SAntonio Huete Jimenez tp->e_addr1 = (u_short)j;
549*ed775ee7SAntonio Huete Jimenez tp->e_addr2 = (u_short)k;
550411677aeSAaron LI tp->e_nsap = (u_char *)malloc(nsap_length + 1);
55141c99275SPeter Avalos if (tp->e_nsap == NULL)
552*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: malloc", __func__);
553411677aeSAaron LI tp->e_nsap[0] = (u_char)nsap_length; /* guaranteed < ISONSAP_MAX_LENGTH */
554411677aeSAaron LI memcpy((char *)&tp->e_nsap[1], (const char *)nsap, nsap_length);
55541c99275SPeter Avalos tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
55641c99275SPeter Avalos if (tp->e_nxt == NULL)
557*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
55841c99275SPeter Avalos
55941c99275SPeter Avalos return tp;
56041c99275SPeter Avalos }
56141c99275SPeter Avalos
56241c99275SPeter Avalos /* Find the hash node that corresponds the protoid 'pi'. */
56341c99275SPeter Avalos
564*ed775ee7SAntonio Huete Jimenez static struct protoidmem *
lookup_protoid(netdissect_options * ndo,const u_char * pi)565411677aeSAaron LI lookup_protoid(netdissect_options *ndo, const u_char *pi)
56641c99275SPeter Avalos {
567*ed775ee7SAntonio Huete Jimenez u_int i, j;
56841c99275SPeter Avalos struct protoidmem *tp;
56941c99275SPeter Avalos
57041c99275SPeter Avalos /* 5 octets won't be aligned */
57141c99275SPeter Avalos i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
57241c99275SPeter Avalos j = (pi[3] << 8) + pi[4];
57341c99275SPeter Avalos /* XXX should be endian-insensitive, but do big-endian testing XXX */
57441c99275SPeter Avalos
57541c99275SPeter Avalos tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
57641c99275SPeter Avalos while (tp->p_nxt)
57741c99275SPeter Avalos if (tp->p_oui == i && tp->p_proto == j)
57841c99275SPeter Avalos return tp;
57941c99275SPeter Avalos else
58041c99275SPeter Avalos tp = tp->p_nxt;
58141c99275SPeter Avalos tp->p_oui = i;
582*ed775ee7SAntonio Huete Jimenez tp->p_proto = (u_short)j;
58341c99275SPeter Avalos tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
58441c99275SPeter Avalos if (tp->p_nxt == NULL)
585*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
58641c99275SPeter Avalos
58741c99275SPeter Avalos return tp;
58841c99275SPeter Avalos }
58941c99275SPeter Avalos
59041c99275SPeter Avalos const char *
etheraddr_string(netdissect_options * ndo,const uint8_t * ep)591*ed775ee7SAntonio Huete Jimenez etheraddr_string(netdissect_options *ndo, const uint8_t *ep)
59241c99275SPeter Avalos {
593*ed775ee7SAntonio Huete Jimenez int i;
594*ed775ee7SAntonio Huete Jimenez char *cp;
595*ed775ee7SAntonio Huete Jimenez struct enamemem *tp;
59641c99275SPeter Avalos int oui;
59741c99275SPeter Avalos char buf[BUFSIZE];
59841c99275SPeter Avalos
599411677aeSAaron LI tp = lookup_emem(ndo, ep);
60041c99275SPeter Avalos if (tp->e_name)
60141c99275SPeter Avalos return (tp->e_name);
60241c99275SPeter Avalos #ifdef USE_ETHER_NTOHOST
603411677aeSAaron LI if (!ndo->ndo_nflag) {
60441c99275SPeter Avalos char buf2[BUFSIZE];
605*ed775ee7SAntonio Huete Jimenez /*
606*ed775ee7SAntonio Huete Jimenez * This is a non-const copy of ep for ether_ntohost(), which
607*ed775ee7SAntonio Huete Jimenez * has its second argument non-const in OpenBSD. Also saves a
608*ed775ee7SAntonio Huete Jimenez * type cast.
609*ed775ee7SAntonio Huete Jimenez */
610*ed775ee7SAntonio Huete Jimenez struct ether_addr ea;
61141c99275SPeter Avalos
612*ed775ee7SAntonio Huete Jimenez memcpy (&ea, ep, MAC_ADDR_LEN);
613*ed775ee7SAntonio Huete Jimenez if (ether_ntohost(buf2, &ea) == 0) {
61441c99275SPeter Avalos tp->e_name = strdup(buf2);
615411677aeSAaron LI if (tp->e_name == NULL)
616*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
617*ed775ee7SAntonio Huete Jimenez "%s: strdup(buf2)", __func__);
61841c99275SPeter Avalos return (tp->e_name);
61941c99275SPeter Avalos }
62041c99275SPeter Avalos }
62141c99275SPeter Avalos #endif
62241c99275SPeter Avalos cp = buf;
623*ed775ee7SAntonio Huete Jimenez oui = EXTRACT_BE_U_3(ep);
624*ed775ee7SAntonio Huete Jimenez cp = octet_to_hex(cp, *ep++);
62541c99275SPeter Avalos for (i = 5; --i >= 0;) {
62641c99275SPeter Avalos *cp++ = ':';
627*ed775ee7SAntonio Huete Jimenez cp = octet_to_hex(cp, *ep++);
62841c99275SPeter Avalos }
62941c99275SPeter Avalos
630411677aeSAaron LI if (!ndo->ndo_nflag) {
63141c99275SPeter Avalos snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
63241c99275SPeter Avalos tok2str(oui_values, "Unknown", oui));
63341c99275SPeter Avalos } else
63441c99275SPeter Avalos *cp = '\0';
63541c99275SPeter Avalos tp->e_name = strdup(buf);
636411677aeSAaron LI if (tp->e_name == NULL)
637*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
638*ed775ee7SAntonio Huete Jimenez "%s: strdup(buf)", __func__);
63941c99275SPeter Avalos return (tp->e_name);
64041c99275SPeter Avalos }
64141c99275SPeter Avalos
64241c99275SPeter Avalos const char *
le64addr_string(netdissect_options * ndo,const uint8_t * ep)643*ed775ee7SAntonio Huete Jimenez le64addr_string(netdissect_options *ndo, const uint8_t *ep)
64427bfbee1SPeter Avalos {
64527bfbee1SPeter Avalos const unsigned int len = 8;
646*ed775ee7SAntonio Huete Jimenez u_int i;
647*ed775ee7SAntonio Huete Jimenez char *cp;
648*ed775ee7SAntonio Huete Jimenez struct bsnamemem *tp;
64927bfbee1SPeter Avalos char buf[BUFSIZE];
65027bfbee1SPeter Avalos
651411677aeSAaron LI tp = lookup_bytestring(ndo, ep, len);
652411677aeSAaron LI if (tp->bs_name)
653411677aeSAaron LI return (tp->bs_name);
65427bfbee1SPeter Avalos
65527bfbee1SPeter Avalos cp = buf;
65627bfbee1SPeter Avalos for (i = len; i > 0 ; --i) {
657*ed775ee7SAntonio Huete Jimenez cp = octet_to_hex(cp, *(ep + i - 1));
65827bfbee1SPeter Avalos *cp++ = ':';
65927bfbee1SPeter Avalos }
66027bfbee1SPeter Avalos cp --;
66127bfbee1SPeter Avalos
66227bfbee1SPeter Avalos *cp = '\0';
66327bfbee1SPeter Avalos
664411677aeSAaron LI tp->bs_name = strdup(buf);
665411677aeSAaron LI if (tp->bs_name == NULL)
666*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
667*ed775ee7SAntonio Huete Jimenez "%s: strdup(buf)", __func__);
66827bfbee1SPeter Avalos
669411677aeSAaron LI return (tp->bs_name);
67027bfbee1SPeter Avalos }
67127bfbee1SPeter Avalos
67227bfbee1SPeter Avalos const char *
linkaddr_string(netdissect_options * ndo,const uint8_t * ep,const unsigned int type,const unsigned int len)673*ed775ee7SAntonio Huete Jimenez linkaddr_string(netdissect_options *ndo, const uint8_t *ep,
674411677aeSAaron LI const unsigned int type, const unsigned int len)
67541c99275SPeter Avalos {
676*ed775ee7SAntonio Huete Jimenez u_int i;
677*ed775ee7SAntonio Huete Jimenez char *cp;
678*ed775ee7SAntonio Huete Jimenez struct bsnamemem *tp;
67941c99275SPeter Avalos
68027bfbee1SPeter Avalos if (len == 0)
68127bfbee1SPeter Avalos return ("<empty>");
682ea7b4bf5SPeter Avalos
683*ed775ee7SAntonio Huete Jimenez if (type == LINKADDR_ETHER && len == MAC_ADDR_LEN)
684411677aeSAaron LI return (etheraddr_string(ndo, ep));
68527bfbee1SPeter Avalos
68627bfbee1SPeter Avalos if (type == LINKADDR_FRELAY)
687411677aeSAaron LI return (q922_string(ndo, ep, len));
68841c99275SPeter Avalos
689411677aeSAaron LI tp = lookup_bytestring(ndo, ep, len);
690411677aeSAaron LI if (tp->bs_name)
691411677aeSAaron LI return (tp->bs_name);
69241c99275SPeter Avalos
693411677aeSAaron LI tp->bs_name = cp = (char *)malloc(len*3);
694411677aeSAaron LI if (tp->bs_name == NULL)
695*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
696*ed775ee7SAntonio Huete Jimenez "%s: malloc", __func__);
697*ed775ee7SAntonio Huete Jimenez cp = octet_to_hex(cp, *ep++);
69841c99275SPeter Avalos for (i = len-1; i > 0 ; --i) {
69941c99275SPeter Avalos *cp++ = ':';
700*ed775ee7SAntonio Huete Jimenez cp = octet_to_hex(cp, *ep++);
70141c99275SPeter Avalos }
70241c99275SPeter Avalos *cp = '\0';
703411677aeSAaron LI return (tp->bs_name);
70441c99275SPeter Avalos }
70541c99275SPeter Avalos
70641c99275SPeter Avalos #define ISONSAP_MAX_LENGTH 20
70741c99275SPeter Avalos const char *
isonsap_string(netdissect_options * ndo,const uint8_t * nsap,u_int nsap_length)708*ed775ee7SAntonio Huete Jimenez isonsap_string(netdissect_options *ndo, const uint8_t *nsap,
709*ed775ee7SAntonio Huete Jimenez u_int nsap_length)
71041c99275SPeter Avalos {
711*ed775ee7SAntonio Huete Jimenez u_int nsap_idx;
712*ed775ee7SAntonio Huete Jimenez char *cp;
713*ed775ee7SAntonio Huete Jimenez struct enamemem *tp;
71441c99275SPeter Avalos
71541c99275SPeter Avalos if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
71641c99275SPeter Avalos return ("isonsap_string: illegal length");
71741c99275SPeter Avalos
718411677aeSAaron LI tp = lookup_nsap(ndo, nsap, nsap_length);
71941c99275SPeter Avalos if (tp->e_name)
72041c99275SPeter Avalos return tp->e_name;
72141c99275SPeter Avalos
72241c99275SPeter Avalos tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
72341c99275SPeter Avalos if (cp == NULL)
724*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
725*ed775ee7SAntonio Huete Jimenez "%s: malloc", __func__);
72641c99275SPeter Avalos
72741c99275SPeter Avalos for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
728*ed775ee7SAntonio Huete Jimenez cp = octet_to_hex(cp, *nsap++);
72941c99275SPeter Avalos if (((nsap_idx & 1) == 0) &&
73041c99275SPeter Avalos (nsap_idx + 1 < nsap_length)) {
73141c99275SPeter Avalos *cp++ = '.';
73241c99275SPeter Avalos }
73341c99275SPeter Avalos }
73441c99275SPeter Avalos *cp = '\0';
73541c99275SPeter Avalos return (tp->e_name);
73641c99275SPeter Avalos }
73741c99275SPeter Avalos
73841c99275SPeter Avalos const char *
tcpport_string(netdissect_options * ndo,u_short port)739411677aeSAaron LI tcpport_string(netdissect_options *ndo, u_short port)
74041c99275SPeter Avalos {
741*ed775ee7SAntonio Huete Jimenez struct hnamemem *tp;
742*ed775ee7SAntonio Huete Jimenez uint32_t i = port;
74341c99275SPeter Avalos char buf[sizeof("00000")];
74441c99275SPeter Avalos
74541c99275SPeter Avalos for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
74641c99275SPeter Avalos if (tp->addr == i)
74741c99275SPeter Avalos return (tp->name);
74841c99275SPeter Avalos
74941c99275SPeter Avalos tp->addr = i;
750411677aeSAaron LI tp->nxt = newhnamemem(ndo);
75141c99275SPeter Avalos
75241c99275SPeter Avalos (void)snprintf(buf, sizeof(buf), "%u", i);
75341c99275SPeter Avalos tp->name = strdup(buf);
754411677aeSAaron LI if (tp->name == NULL)
755*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
756*ed775ee7SAntonio Huete Jimenez "%s: strdup(buf)", __func__);
75741c99275SPeter Avalos return (tp->name);
75841c99275SPeter Avalos }
75941c99275SPeter Avalos
76041c99275SPeter Avalos const char *
udpport_string(netdissect_options * ndo,u_short port)761*ed775ee7SAntonio Huete Jimenez udpport_string(netdissect_options *ndo, u_short port)
76241c99275SPeter Avalos {
763*ed775ee7SAntonio Huete Jimenez struct hnamemem *tp;
764*ed775ee7SAntonio Huete Jimenez uint32_t i = port;
76541c99275SPeter Avalos char buf[sizeof("00000")];
76641c99275SPeter Avalos
76741c99275SPeter Avalos for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
76841c99275SPeter Avalos if (tp->addr == i)
76941c99275SPeter Avalos return (tp->name);
77041c99275SPeter Avalos
77141c99275SPeter Avalos tp->addr = i;
772411677aeSAaron LI tp->nxt = newhnamemem(ndo);
77341c99275SPeter Avalos
77441c99275SPeter Avalos (void)snprintf(buf, sizeof(buf), "%u", i);
77541c99275SPeter Avalos tp->name = strdup(buf);
776411677aeSAaron LI if (tp->name == NULL)
777*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
778*ed775ee7SAntonio Huete Jimenez "%s: strdup(buf)", __func__);
77941c99275SPeter Avalos return (tp->name);
78041c99275SPeter Avalos }
78141c99275SPeter Avalos
78241c99275SPeter Avalos const char *
ipxsap_string(netdissect_options * ndo,u_short port)783411677aeSAaron LI ipxsap_string(netdissect_options *ndo, u_short port)
78441c99275SPeter Avalos {
785*ed775ee7SAntonio Huete Jimenez char *cp;
786*ed775ee7SAntonio Huete Jimenez struct hnamemem *tp;
787*ed775ee7SAntonio Huete Jimenez uint32_t i = port;
78841c99275SPeter Avalos char buf[sizeof("0000")];
78941c99275SPeter Avalos
79041c99275SPeter Avalos for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
79141c99275SPeter Avalos if (tp->addr == i)
79241c99275SPeter Avalos return (tp->name);
79341c99275SPeter Avalos
79441c99275SPeter Avalos tp->addr = i;
795411677aeSAaron LI tp->nxt = newhnamemem(ndo);
79641c99275SPeter Avalos
79741c99275SPeter Avalos cp = buf;
798*ed775ee7SAntonio Huete Jimenez port = ntohs(port);
79941c99275SPeter Avalos *cp++ = hex[port >> 12 & 0xf];
80041c99275SPeter Avalos *cp++ = hex[port >> 8 & 0xf];
80141c99275SPeter Avalos *cp++ = hex[port >> 4 & 0xf];
80241c99275SPeter Avalos *cp++ = hex[port & 0xf];
80341c99275SPeter Avalos *cp++ = '\0';
80441c99275SPeter Avalos tp->name = strdup(buf);
805411677aeSAaron LI if (tp->name == NULL)
806*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
807*ed775ee7SAntonio Huete Jimenez "%s: strdup(buf)", __func__);
80841c99275SPeter Avalos return (tp->name);
80941c99275SPeter Avalos }
81041c99275SPeter Avalos
81141c99275SPeter Avalos static void
init_servarray(netdissect_options * ndo)812411677aeSAaron LI init_servarray(netdissect_options *ndo)
81341c99275SPeter Avalos {
81441c99275SPeter Avalos struct servent *sv;
815*ed775ee7SAntonio Huete Jimenez struct hnamemem *table;
816*ed775ee7SAntonio Huete Jimenez int i;
81741c99275SPeter Avalos char buf[sizeof("0000000000")];
81841c99275SPeter Avalos
81941c99275SPeter Avalos while ((sv = getservent()) != NULL) {
82041c99275SPeter Avalos int port = ntohs(sv->s_port);
82141c99275SPeter Avalos i = port & (HASHNAMESIZE-1);
82241c99275SPeter Avalos if (strcmp(sv->s_proto, "tcp") == 0)
82341c99275SPeter Avalos table = &tporttable[i];
82441c99275SPeter Avalos else if (strcmp(sv->s_proto, "udp") == 0)
82541c99275SPeter Avalos table = &uporttable[i];
82641c99275SPeter Avalos else
82741c99275SPeter Avalos continue;
82841c99275SPeter Avalos
82941c99275SPeter Avalos while (table->name)
83041c99275SPeter Avalos table = table->nxt;
831411677aeSAaron LI if (ndo->ndo_nflag) {
83241c99275SPeter Avalos (void)snprintf(buf, sizeof(buf), "%d", port);
83341c99275SPeter Avalos table->name = strdup(buf);
83441c99275SPeter Avalos } else
83541c99275SPeter Avalos table->name = strdup(sv->s_name);
836411677aeSAaron LI if (table->name == NULL)
837*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
838*ed775ee7SAntonio Huete Jimenez "%s: strdup", __func__);
839411677aeSAaron LI
84041c99275SPeter Avalos table->addr = port;
841411677aeSAaron LI table->nxt = newhnamemem(ndo);
84241c99275SPeter Avalos }
84341c99275SPeter Avalos endservent();
84441c99275SPeter Avalos }
84541c99275SPeter Avalos
846411677aeSAaron LI static const struct eproto {
84741c99275SPeter Avalos const char *s;
84841c99275SPeter Avalos u_short p;
849411677aeSAaron LI } eproto_db[] = {
850*ed775ee7SAntonio Huete Jimenez { "aarp", ETHERTYPE_AARP },
851*ed775ee7SAntonio Huete Jimenez { "arp", ETHERTYPE_ARP },
852*ed775ee7SAntonio Huete Jimenez { "atalk", ETHERTYPE_ATALK },
853*ed775ee7SAntonio Huete Jimenez { "decnet", ETHERTYPE_DN },
854411677aeSAaron LI { "ip", ETHERTYPE_IP },
855411677aeSAaron LI { "ip6", ETHERTYPE_IPV6 },
856*ed775ee7SAntonio Huete Jimenez { "lat", ETHERTYPE_LAT },
857*ed775ee7SAntonio Huete Jimenez { "loopback", ETHERTYPE_LOOPBACK },
858411677aeSAaron LI { "mopdl", ETHERTYPE_MOPDL },
859411677aeSAaron LI { "moprc", ETHERTYPE_MOPRC },
860*ed775ee7SAntonio Huete Jimenez { "rarp", ETHERTYPE_REVARP },
861411677aeSAaron LI { "sca", ETHERTYPE_SCA },
862411677aeSAaron LI { (char *)0, 0 }
863411677aeSAaron LI };
86441c99275SPeter Avalos
86541c99275SPeter Avalos static void
init_eprotoarray(netdissect_options * ndo)866411677aeSAaron LI init_eprotoarray(netdissect_options *ndo)
86741c99275SPeter Avalos {
868*ed775ee7SAntonio Huete Jimenez int i;
869*ed775ee7SAntonio Huete Jimenez struct hnamemem *table;
87041c99275SPeter Avalos
87141c99275SPeter Avalos for (i = 0; eproto_db[i].s; i++) {
87241c99275SPeter Avalos int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
87341c99275SPeter Avalos table = &eprototable[j];
87441c99275SPeter Avalos while (table->name)
87541c99275SPeter Avalos table = table->nxt;
87641c99275SPeter Avalos table->name = eproto_db[i].s;
87741c99275SPeter Avalos table->addr = htons(eproto_db[i].p);
878411677aeSAaron LI table->nxt = newhnamemem(ndo);
87941c99275SPeter Avalos }
88041c99275SPeter Avalos }
88141c99275SPeter Avalos
88227bfbee1SPeter Avalos static const struct protoidlist {
88341c99275SPeter Avalos const u_char protoid[5];
88441c99275SPeter Avalos const char *name;
88541c99275SPeter Avalos } protoidlist[] = {
88641c99275SPeter Avalos {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
88741c99275SPeter Avalos {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
88841c99275SPeter Avalos {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
88941c99275SPeter Avalos {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
89041c99275SPeter Avalos {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
89141c99275SPeter Avalos {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
89241c99275SPeter Avalos };
89341c99275SPeter Avalos
89441c99275SPeter Avalos /*
89541c99275SPeter Avalos * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
89641c99275SPeter Avalos * types.
89741c99275SPeter Avalos */
89841c99275SPeter Avalos static void
init_protoidarray(netdissect_options * ndo)899411677aeSAaron LI init_protoidarray(netdissect_options *ndo)
90041c99275SPeter Avalos {
901*ed775ee7SAntonio Huete Jimenez int i;
902*ed775ee7SAntonio Huete Jimenez struct protoidmem *tp;
90327bfbee1SPeter Avalos const struct protoidlist *pl;
90441c99275SPeter Avalos u_char protoid[5];
90541c99275SPeter Avalos
90641c99275SPeter Avalos protoid[0] = 0;
90741c99275SPeter Avalos protoid[1] = 0;
90841c99275SPeter Avalos protoid[2] = 0;
90941c99275SPeter Avalos for (i = 0; eproto_db[i].s; i++) {
91041c99275SPeter Avalos u_short etype = htons(eproto_db[i].p);
91141c99275SPeter Avalos
91241c99275SPeter Avalos memcpy((char *)&protoid[3], (char *)&etype, 2);
913411677aeSAaron LI tp = lookup_protoid(ndo, protoid);
91441c99275SPeter Avalos tp->p_name = strdup(eproto_db[i].s);
915411677aeSAaron LI if (tp->p_name == NULL)
916*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
917*ed775ee7SAntonio Huete Jimenez "%s: strdup(eproto_db[i].s)", __func__);
91841c99275SPeter Avalos }
91941c99275SPeter Avalos /* Hardwire some SNAP proto ID names */
92041c99275SPeter Avalos for (pl = protoidlist; pl->name != NULL; ++pl) {
921411677aeSAaron LI tp = lookup_protoid(ndo, pl->protoid);
92241c99275SPeter Avalos /* Don't override existing name */
92341c99275SPeter Avalos if (tp->p_name != NULL)
92441c99275SPeter Avalos continue;
92541c99275SPeter Avalos
92641c99275SPeter Avalos tp->p_name = pl->name;
92741c99275SPeter Avalos }
92841c99275SPeter Avalos }
92941c99275SPeter Avalos
93027bfbee1SPeter Avalos static const struct etherlist {
931*ed775ee7SAntonio Huete Jimenez const nd_mac_addr addr;
93241c99275SPeter Avalos const char *name;
93341c99275SPeter Avalos } etherlist[] = {
93441c99275SPeter Avalos {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
93541c99275SPeter Avalos {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
93641c99275SPeter Avalos };
93741c99275SPeter Avalos
93841c99275SPeter Avalos /*
93941c99275SPeter Avalos * Initialize the ethers hash table. We take two different approaches
94041c99275SPeter Avalos * depending on whether or not the system provides the ethers name
94141c99275SPeter Avalos * service. If it does, we just wire in a few names at startup,
94241c99275SPeter Avalos * and etheraddr_string() fills in the table on demand. If it doesn't,
94341c99275SPeter Avalos * then we suck in the entire /etc/ethers file at startup. The idea
94441c99275SPeter Avalos * is that parsing the local file will be fast, but spinning through
94541c99275SPeter Avalos * all the ethers entries via NIS & next_etherent might be very slow.
94641c99275SPeter Avalos *
94741c99275SPeter Avalos * XXX pcap_next_etherent doesn't belong in the pcap interface, but
94841c99275SPeter Avalos * since the pcap module already does name-to-address translation,
94941c99275SPeter Avalos * it's already does most of the work for the ethernet address-to-name
95041c99275SPeter Avalos * translation, so we just pcap_next_etherent as a convenience.
95141c99275SPeter Avalos */
95241c99275SPeter Avalos static void
init_etherarray(netdissect_options * ndo)953411677aeSAaron LI init_etherarray(netdissect_options *ndo)
95441c99275SPeter Avalos {
955*ed775ee7SAntonio Huete Jimenez const struct etherlist *el;
956*ed775ee7SAntonio Huete Jimenez struct enamemem *tp;
95741c99275SPeter Avalos #ifdef USE_ETHER_NTOHOST
95841c99275SPeter Avalos char name[256];
95941c99275SPeter Avalos #else
960*ed775ee7SAntonio Huete Jimenez struct pcap_etherent *ep;
961*ed775ee7SAntonio Huete Jimenez FILE *fp;
96241c99275SPeter Avalos
96341c99275SPeter Avalos /* Suck in entire ethers file */
96441c99275SPeter Avalos fp = fopen(PCAP_ETHERS_FILE, "r");
96541c99275SPeter Avalos if (fp != NULL) {
96641c99275SPeter Avalos while ((ep = pcap_next_etherent(fp)) != NULL) {
967411677aeSAaron LI tp = lookup_emem(ndo, ep->addr);
96841c99275SPeter Avalos tp->e_name = strdup(ep->name);
969411677aeSAaron LI if (tp->e_name == NULL)
970*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
971*ed775ee7SAntonio Huete Jimenez "%s: strdup(ep->addr)", __func__);
97241c99275SPeter Avalos }
97341c99275SPeter Avalos (void)fclose(fp);
97441c99275SPeter Avalos }
97541c99275SPeter Avalos #endif
97641c99275SPeter Avalos
97741c99275SPeter Avalos /* Hardwire some ethernet names */
97841c99275SPeter Avalos for (el = etherlist; el->name != NULL; ++el) {
979411677aeSAaron LI tp = lookup_emem(ndo, el->addr);
98041c99275SPeter Avalos /* Don't override existing name */
98141c99275SPeter Avalos if (tp->e_name != NULL)
98241c99275SPeter Avalos continue;
98341c99275SPeter Avalos
98441c99275SPeter Avalos #ifdef USE_ETHER_NTOHOST
98541c99275SPeter Avalos /*
98641c99275SPeter Avalos * Use YP/NIS version of name if available.
98741c99275SPeter Avalos */
988*ed775ee7SAntonio Huete Jimenez /* Same workaround as in etheraddr_string(). */
989*ed775ee7SAntonio Huete Jimenez struct ether_addr ea;
990*ed775ee7SAntonio Huete Jimenez memcpy (&ea, el->addr, MAC_ADDR_LEN);
991*ed775ee7SAntonio Huete Jimenez if (ether_ntohost(name, &ea) == 0) {
99241c99275SPeter Avalos tp->e_name = strdup(name);
993411677aeSAaron LI if (tp->e_name == NULL)
994*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
995*ed775ee7SAntonio Huete Jimenez "%s: strdup(name)", __func__);
99641c99275SPeter Avalos continue;
99741c99275SPeter Avalos }
99841c99275SPeter Avalos #endif
99941c99275SPeter Avalos tp->e_name = el->name;
100041c99275SPeter Avalos }
100141c99275SPeter Avalos }
100241c99275SPeter Avalos
1003*ed775ee7SAntonio Huete Jimenez static const struct ipxsap_ent {
1004*ed775ee7SAntonio Huete Jimenez uint16_t v;
1005*ed775ee7SAntonio Huete Jimenez const char *s;
1006*ed775ee7SAntonio Huete Jimenez } ipxsap_db[] = {
100741c99275SPeter Avalos { 0x0000, "Unknown" },
100841c99275SPeter Avalos { 0x0001, "User" },
100941c99275SPeter Avalos { 0x0002, "User Group" },
101041c99275SPeter Avalos { 0x0003, "PrintQueue" },
101141c99275SPeter Avalos { 0x0004, "FileServer" },
101241c99275SPeter Avalos { 0x0005, "JobServer" },
101341c99275SPeter Avalos { 0x0006, "Gateway" },
101441c99275SPeter Avalos { 0x0007, "PrintServer" },
101541c99275SPeter Avalos { 0x0008, "ArchiveQueue" },
101641c99275SPeter Avalos { 0x0009, "ArchiveServer" },
101741c99275SPeter Avalos { 0x000a, "JobQueue" },
101841c99275SPeter Avalos { 0x000b, "Administration" },
101941c99275SPeter Avalos { 0x000F, "Novell TI-RPC" },
102041c99275SPeter Avalos { 0x0017, "Diagnostics" },
102141c99275SPeter Avalos { 0x0020, "NetBIOS" },
102241c99275SPeter Avalos { 0x0021, "NAS SNA Gateway" },
102341c99275SPeter Avalos { 0x0023, "NACS AsyncGateway" },
102441c99275SPeter Avalos { 0x0024, "RemoteBridge/RoutingService" },
102541c99275SPeter Avalos { 0x0026, "BridgeServer" },
102641c99275SPeter Avalos { 0x0027, "TCP/IP Gateway" },
102741c99275SPeter Avalos { 0x0028, "Point-to-point X.25 BridgeServer" },
102841c99275SPeter Avalos { 0x0029, "3270 Gateway" },
102941c99275SPeter Avalos { 0x002a, "CHI Corp" },
103041c99275SPeter Avalos { 0x002c, "PC Chalkboard" },
103141c99275SPeter Avalos { 0x002d, "TimeSynchServer" },
103241c99275SPeter Avalos { 0x002e, "ARCserve5.0/PalindromeBackup" },
103341c99275SPeter Avalos { 0x0045, "DI3270 Gateway" },
103441c99275SPeter Avalos { 0x0047, "AdvertisingPrintServer" },
103541c99275SPeter Avalos { 0x004a, "NetBlazerModems" },
103641c99275SPeter Avalos { 0x004b, "BtrieveVAP" },
103741c99275SPeter Avalos { 0x004c, "NetwareSQL" },
103841c99275SPeter Avalos { 0x004d, "XtreeNetwork" },
103941c99275SPeter Avalos { 0x0050, "BtrieveVAP4.11" },
104041c99275SPeter Avalos { 0x0052, "QuickLink" },
104141c99275SPeter Avalos { 0x0053, "PrintQueueUser" },
104241c99275SPeter Avalos { 0x0058, "Multipoint X.25 Router" },
104341c99275SPeter Avalos { 0x0060, "STLB/NLM" },
104441c99275SPeter Avalos { 0x0064, "ARCserve" },
104541c99275SPeter Avalos { 0x0066, "ARCserve3.0" },
104641c99275SPeter Avalos { 0x0072, "WAN CopyUtility" },
104741c99275SPeter Avalos { 0x007a, "TES-NetwareVMS" },
104841c99275SPeter Avalos { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
104941c99275SPeter Avalos { 0x0095, "DDA OBGYN" },
105041c99275SPeter Avalos { 0x0098, "NetwareAccessServer" },
105141c99275SPeter Avalos { 0x009a, "Netware for VMS II/NamedPipeServer" },
105241c99275SPeter Avalos { 0x009b, "NetwareAccessServer" },
105341c99275SPeter Avalos { 0x009e, "PortableNetwareServer/SunLinkNVT" },
105441c99275SPeter Avalos { 0x00a1, "PowerchuteAPC UPS" },
105541c99275SPeter Avalos { 0x00aa, "LAWserve" },
105641c99275SPeter Avalos { 0x00ac, "CompaqIDA StatusMonitor" },
105741c99275SPeter Avalos { 0x0100, "PIPE STAIL" },
105841c99275SPeter Avalos { 0x0102, "LAN ProtectBindery" },
105941c99275SPeter Avalos { 0x0103, "OracleDataBaseServer" },
106041c99275SPeter Avalos { 0x0107, "Netware386/RSPX RemoteConsole" },
106141c99275SPeter Avalos { 0x010f, "NovellSNA Gateway" },
106241c99275SPeter Avalos { 0x0111, "TestServer" },
106341c99275SPeter Avalos { 0x0112, "HP PrintServer" },
106441c99275SPeter Avalos { 0x0114, "CSA MUX" },
106541c99275SPeter Avalos { 0x0115, "CSA LCA" },
106641c99275SPeter Avalos { 0x0116, "CSA CM" },
106741c99275SPeter Avalos { 0x0117, "CSA SMA" },
106841c99275SPeter Avalos { 0x0118, "CSA DBA" },
106941c99275SPeter Avalos { 0x0119, "CSA NMA" },
107041c99275SPeter Avalos { 0x011a, "CSA SSA" },
107141c99275SPeter Avalos { 0x011b, "CSA STATUS" },
107241c99275SPeter Avalos { 0x011e, "CSA APPC" },
107341c99275SPeter Avalos { 0x0126, "SNA TEST SSA Profile" },
107441c99275SPeter Avalos { 0x012a, "CSA TRACE" },
107541c99275SPeter Avalos { 0x012b, "NetwareSAA" },
107641c99275SPeter Avalos { 0x012e, "IKARUS VirusScan" },
107741c99275SPeter Avalos { 0x0130, "CommunicationsExecutive" },
107841c99275SPeter Avalos { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
107941c99275SPeter Avalos { 0x0135, "NetwareNamingServicesProfile" },
108041c99275SPeter Avalos { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
108141c99275SPeter Avalos { 0x0141, "LAN SpoolServer" },
108241c99275SPeter Avalos { 0x0152, "IRMALAN Gateway" },
108341c99275SPeter Avalos { 0x0154, "NamedPipeServer" },
108441c99275SPeter Avalos { 0x0166, "NetWareManagement" },
108541c99275SPeter Avalos { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
108641c99275SPeter Avalos { 0x0173, "Compaq" },
108741c99275SPeter Avalos { 0x0174, "Compaq SNMP Agent" },
108841c99275SPeter Avalos { 0x0175, "Compaq" },
108941c99275SPeter Avalos { 0x0180, "XTreeServer/XTreeTools" },
109041c99275SPeter Avalos { 0x018A, "NASI ServicesBroadcastServer" },
109141c99275SPeter Avalos { 0x01b0, "GARP Gateway" },
109241c99275SPeter Avalos { 0x01b1, "Binfview" },
109341c99275SPeter Avalos { 0x01bf, "IntelLanDeskManager" },
109441c99275SPeter Avalos { 0x01ca, "AXTEC" },
109541c99275SPeter Avalos { 0x01cb, "ShivaNetModem/E" },
109641c99275SPeter Avalos { 0x01cc, "ShivaLanRover/E" },
109741c99275SPeter Avalos { 0x01cd, "ShivaLanRover/T" },
109841c99275SPeter Avalos { 0x01ce, "ShivaUniversal" },
109941c99275SPeter Avalos { 0x01d8, "CastelleFAXPressServer" },
110041c99275SPeter Avalos { 0x01da, "CastelleLANPressPrintServer" },
110141c99275SPeter Avalos { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
110241c99275SPeter Avalos { 0x01f0, "LEGATO" },
110341c99275SPeter Avalos { 0x01f5, "LEGATO" },
110441c99275SPeter Avalos { 0x0233, "NMS Agent/NetwareManagementAgent" },
110541c99275SPeter Avalos { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
110641c99275SPeter Avalos { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
110741c99275SPeter Avalos { 0x023a, "LANtern" },
110841c99275SPeter Avalos { 0x023c, "MAVERICK" },
110941c99275SPeter Avalos { 0x023f, "NovellSMDR" },
111041c99275SPeter Avalos { 0x024e, "NetwareConnect" },
111141c99275SPeter Avalos { 0x024f, "NASI ServerBroadcast Cisco" },
111241c99275SPeter Avalos { 0x026a, "NMS ServiceConsole" },
111341c99275SPeter Avalos { 0x026b, "TimeSynchronizationServer Netware 4.x" },
111441c99275SPeter Avalos { 0x0278, "DirectoryServer Netware 4.x" },
111541c99275SPeter Avalos { 0x027b, "NetwareManagementAgent" },
111641c99275SPeter Avalos { 0x0280, "Novell File and Printer Sharing Service for PC" },
111741c99275SPeter Avalos { 0x0304, "NovellSAA Gateway" },
111841c99275SPeter Avalos { 0x0308, "COM/VERMED" },
111941c99275SPeter Avalos { 0x030a, "GalacticommWorldgroupServer" },
112041c99275SPeter Avalos { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
112141c99275SPeter Avalos { 0x0320, "AttachmateGateway" },
112241c99275SPeter Avalos { 0x0327, "MicrosoftDiagnostiocs" },
112341c99275SPeter Avalos { 0x0328, "WATCOM SQL Server" },
112441c99275SPeter Avalos { 0x0335, "MultiTechSystems MultisynchCommServer" },
112541c99275SPeter Avalos { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
112641c99275SPeter Avalos { 0x0355, "ArcadaBackupExec" },
112741c99275SPeter Avalos { 0x0358, "MSLCD1" },
112841c99275SPeter Avalos { 0x0361, "NETINELO" },
112941c99275SPeter Avalos { 0x037e, "Powerchute UPS Monitoring" },
113041c99275SPeter Avalos { 0x037f, "ViruSafeNotify" },
113141c99275SPeter Avalos { 0x0386, "HP Bridge" },
113241c99275SPeter Avalos { 0x0387, "HP Hub" },
113341c99275SPeter Avalos { 0x0394, "NetWare SAA Gateway" },
113441c99275SPeter Avalos { 0x039b, "LotusNotes" },
113541c99275SPeter Avalos { 0x03b7, "CertusAntiVirus" },
113641c99275SPeter Avalos { 0x03c4, "ARCserve4.0" },
113741c99275SPeter Avalos { 0x03c7, "LANspool3.5" },
113841c99275SPeter Avalos { 0x03d7, "LexmarkPrinterServer" },
113941c99275SPeter Avalos { 0x03d8, "LexmarkXLE PrinterServer" },
114041c99275SPeter Avalos { 0x03dd, "BanyanENS NetwareClient" },
114141c99275SPeter Avalos { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
114241c99275SPeter Avalos { 0x03e1, "UnivelUnixware" },
114341c99275SPeter Avalos { 0x03e4, "UnivelUnixware" },
114441c99275SPeter Avalos { 0x03fc, "IntelNetport" },
114541c99275SPeter Avalos { 0x03fd, "PrintServerQueue" },
114641c99275SPeter Avalos { 0x040A, "ipnServer" },
114741c99275SPeter Avalos { 0x040D, "LVERRMAN" },
114841c99275SPeter Avalos { 0x040E, "LVLIC" },
114941c99275SPeter Avalos { 0x0414, "NET Silicon (DPI)/Kyocera" },
115041c99275SPeter Avalos { 0x0429, "SiteLockVirus" },
115141c99275SPeter Avalos { 0x0432, "UFHELPR???" },
115241c99275SPeter Avalos { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
115341c99275SPeter Avalos { 0x0444, "MicrosoftNT SNA Server" },
115441c99275SPeter Avalos { 0x0448, "Oracle" },
115541c99275SPeter Avalos { 0x044c, "ARCserve5.01" },
115641c99275SPeter Avalos { 0x0457, "CanonGP55" },
115741c99275SPeter Avalos { 0x045a, "QMS Printers" },
115841c99275SPeter Avalos { 0x045b, "DellSCSI Array" },
115941c99275SPeter Avalos { 0x0491, "NetBlazerModems" },
116041c99275SPeter Avalos { 0x04ac, "OnTimeScheduler" },
116141c99275SPeter Avalos { 0x04b0, "CD-Net" },
116241c99275SPeter Avalos { 0x0513, "EmulexNQA" },
116341c99275SPeter Avalos { 0x0520, "SiteLockChecks" },
116441c99275SPeter Avalos { 0x0529, "SiteLockChecks" },
116541c99275SPeter Avalos { 0x052d, "CitrixOS2 AppServer" },
116641c99275SPeter Avalos { 0x0535, "Tektronix" },
116741c99275SPeter Avalos { 0x0536, "Milan" },
116841c99275SPeter Avalos { 0x055d, "Attachmate SNA gateway" },
116941c99275SPeter Avalos { 0x056b, "IBM8235 ModemServer" },
117041c99275SPeter Avalos { 0x056c, "ShivaLanRover/E PLUS" },
117141c99275SPeter Avalos { 0x056d, "ShivaLanRover/T PLUS" },
117241c99275SPeter Avalos { 0x0580, "McAfeeNetShield" },
117341c99275SPeter Avalos { 0x05B8, "NLM to workstation communication (Revelation Software)" },
117441c99275SPeter Avalos { 0x05BA, "CompatibleSystemsRouters" },
117541c99275SPeter Avalos { 0x05BE, "CheyenneHierarchicalStorageManager" },
117641c99275SPeter Avalos { 0x0606, "JCWatermarkImaging" },
117741c99275SPeter Avalos { 0x060c, "AXISNetworkPrinter" },
117841c99275SPeter Avalos { 0x0610, "AdaptecSCSIManagement" },
117941c99275SPeter Avalos { 0x0621, "IBM AntiVirus" },
118041c99275SPeter Avalos { 0x0640, "Windows95 RemoteRegistryService" },
118141c99275SPeter Avalos { 0x064e, "MicrosoftIIS" },
118241c99275SPeter Avalos { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
118341c99275SPeter Avalos { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
118441c99275SPeter Avalos { 0x076C, "Xerox" },
118541c99275SPeter Avalos { 0x079b, "ShivaLanRover/E 115" },
118641c99275SPeter Avalos { 0x079c, "ShivaLanRover/T 115" },
118741c99275SPeter Avalos { 0x07B4, "CubixWorldDesk" },
118841c99275SPeter Avalos { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
118941c99275SPeter Avalos { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
119041c99275SPeter Avalos { 0x0810, "ELAN License Server Demo" },
119141c99275SPeter Avalos { 0x0824, "ShivaLanRoverAccessSwitch/E" },
119241c99275SPeter Avalos { 0x086a, "ISSC Collector" },
119341c99275SPeter Avalos { 0x087f, "ISSC DAS AgentAIX" },
119441c99275SPeter Avalos { 0x0880, "Intel Netport PRO" },
119541c99275SPeter Avalos { 0x0881, "Intel Netport PRO" },
119641c99275SPeter Avalos { 0x0b29, "SiteLock" },
119741c99275SPeter Avalos { 0x0c29, "SiteLockApplications" },
119841c99275SPeter Avalos { 0x0c2c, "LicensingServer" },
119941c99275SPeter Avalos { 0x2101, "PerformanceTechnologyInstantInternet" },
120041c99275SPeter Avalos { 0x2380, "LAI SiteLock" },
120141c99275SPeter Avalos { 0x238c, "MeetingMaker" },
120241c99275SPeter Avalos { 0x4808, "SiteLockServer/SiteLockMetering" },
120341c99275SPeter Avalos { 0x5555, "SiteLockUser" },
120441c99275SPeter Avalos { 0x6312, "Tapeware" },
120541c99275SPeter Avalos { 0x6f00, "RabbitGateway" },
120641c99275SPeter Avalos { 0x7703, "MODEM" },
120741c99275SPeter Avalos { 0x8002, "NetPortPrinters" },
120841c99275SPeter Avalos { 0x8008, "WordPerfectNetworkVersion" },
120941c99275SPeter Avalos { 0x85BE, "Cisco EIGRP" },
121041c99275SPeter Avalos { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
121141c99275SPeter Avalos { 0x9000, "McAfeeNetShield" },
121241c99275SPeter Avalos { 0x9604, "CSA-NT_MON" },
121341c99275SPeter Avalos { 0xb6a8, "OceanIsleReachoutRemoteControl" },
121441c99275SPeter Avalos { 0xf11f, "SiteLockMetering" },
121541c99275SPeter Avalos { 0xf1ff, "SiteLock" },
121641c99275SPeter Avalos { 0xf503, "Microsoft SQL Server" },
121741c99275SPeter Avalos { 0xF905, "IBM TimeAndPlace" },
121841c99275SPeter Avalos { 0xfbfb, "TopCallIII FaxServer" },
121941c99275SPeter Avalos { 0xffff, "AnyService/Wildcard" },
122041c99275SPeter Avalos { 0, (char *)0 }
122141c99275SPeter Avalos };
122241c99275SPeter Avalos
122341c99275SPeter Avalos static void
init_ipxsaparray(netdissect_options * ndo)1224411677aeSAaron LI init_ipxsaparray(netdissect_options *ndo)
122541c99275SPeter Avalos {
1226*ed775ee7SAntonio Huete Jimenez int i;
1227*ed775ee7SAntonio Huete Jimenez struct hnamemem *table;
122841c99275SPeter Avalos
122941c99275SPeter Avalos for (i = 0; ipxsap_db[i].s != NULL; i++) {
1230*ed775ee7SAntonio Huete Jimenez u_int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
123141c99275SPeter Avalos table = &ipxsaptable[j];
123241c99275SPeter Avalos while (table->name)
123341c99275SPeter Avalos table = table->nxt;
123441c99275SPeter Avalos table->name = ipxsap_db[i].s;
123541c99275SPeter Avalos table->addr = htons(ipxsap_db[i].v);
1236411677aeSAaron LI table->nxt = newhnamemem(ndo);
123741c99275SPeter Avalos }
123841c99275SPeter Avalos }
123941c99275SPeter Avalos
124041c99275SPeter Avalos /*
124141c99275SPeter Avalos * Initialize the address to name translation machinery. We map all
1242411677aeSAaron LI * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
1243411677aeSAaron LI * (i.e., to prevent blocking on the nameserver). localnet is the IP address
124441c99275SPeter Avalos * of the local network. mask is its subnet mask.
124541c99275SPeter Avalos */
124641c99275SPeter Avalos void
init_addrtoname(netdissect_options * ndo,uint32_t localnet,uint32_t mask)1247411677aeSAaron LI init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
124841c99275SPeter Avalos {
1249411677aeSAaron LI if (ndo->ndo_fflag) {
125041c99275SPeter Avalos f_localnet = localnet;
125141c99275SPeter Avalos f_netmask = mask;
125241c99275SPeter Avalos }
1253411677aeSAaron LI if (ndo->ndo_nflag)
125441c99275SPeter Avalos /*
125541c99275SPeter Avalos * Simplest way to suppress names.
125641c99275SPeter Avalos */
125741c99275SPeter Avalos return;
125841c99275SPeter Avalos
1259411677aeSAaron LI init_etherarray(ndo);
1260411677aeSAaron LI init_servarray(ndo);
1261411677aeSAaron LI init_eprotoarray(ndo);
1262411677aeSAaron LI init_protoidarray(ndo);
1263411677aeSAaron LI init_ipxsaparray(ndo);
126441c99275SPeter Avalos }
126541c99275SPeter Avalos
126641c99275SPeter Avalos const char *
dnaddr_string(netdissect_options * ndo,u_short dnaddr)1267411677aeSAaron LI dnaddr_string(netdissect_options *ndo, u_short dnaddr)
126841c99275SPeter Avalos {
1269*ed775ee7SAntonio Huete Jimenez struct hnamemem *tp;
127041c99275SPeter Avalos
1271411677aeSAaron LI for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != NULL;
127241c99275SPeter Avalos tp = tp->nxt)
127341c99275SPeter Avalos if (tp->addr == dnaddr)
127441c99275SPeter Avalos return (tp->name);
127541c99275SPeter Avalos
127641c99275SPeter Avalos tp->addr = dnaddr;
1277411677aeSAaron LI tp->nxt = newhnamemem(ndo);
1278411677aeSAaron LI tp->name = dnnum_string(ndo, dnaddr);
127941c99275SPeter Avalos
128041c99275SPeter Avalos return(tp->name);
128141c99275SPeter Avalos }
128241c99275SPeter Avalos
128341c99275SPeter Avalos /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
128441c99275SPeter Avalos struct hnamemem *
newhnamemem(netdissect_options * ndo)1285411677aeSAaron LI newhnamemem(netdissect_options *ndo)
128641c99275SPeter Avalos {
1287*ed775ee7SAntonio Huete Jimenez struct hnamemem *p;
128841c99275SPeter Avalos static struct hnamemem *ptr = NULL;
128941c99275SPeter Avalos static u_int num = 0;
129041c99275SPeter Avalos
129141c99275SPeter Avalos if (num <= 0) {
129241c99275SPeter Avalos num = 64;
129341c99275SPeter Avalos ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
129441c99275SPeter Avalos if (ptr == NULL)
1295*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1296*ed775ee7SAntonio Huete Jimenez "%s: calloc", __func__);
129741c99275SPeter Avalos }
129841c99275SPeter Avalos --num;
129941c99275SPeter Avalos p = ptr++;
130041c99275SPeter Avalos return (p);
130141c99275SPeter Avalos }
130241c99275SPeter Avalos
130341c99275SPeter Avalos /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
130441c99275SPeter Avalos struct h6namemem *
newh6namemem(netdissect_options * ndo)1305411677aeSAaron LI newh6namemem(netdissect_options *ndo)
130641c99275SPeter Avalos {
1307*ed775ee7SAntonio Huete Jimenez struct h6namemem *p;
130841c99275SPeter Avalos static struct h6namemem *ptr = NULL;
130941c99275SPeter Avalos static u_int num = 0;
131041c99275SPeter Avalos
131141c99275SPeter Avalos if (num <= 0) {
131241c99275SPeter Avalos num = 64;
131341c99275SPeter Avalos ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
131441c99275SPeter Avalos if (ptr == NULL)
1315*ed775ee7SAntonio Huete Jimenez (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1316*ed775ee7SAntonio Huete Jimenez "%s: calloc", __func__);
131741c99275SPeter Avalos }
131841c99275SPeter Avalos --num;
131941c99275SPeter Avalos p = ptr++;
132041c99275SPeter Avalos return (p);
132141c99275SPeter Avalos }
1322411677aeSAaron LI
1323411677aeSAaron LI /* Represent TCI part of the 802.1Q 4-octet tag as text. */
1324411677aeSAaron LI const char *
ieee8021q_tci_string(const uint16_t tci)1325411677aeSAaron LI ieee8021q_tci_string(const uint16_t tci)
1326411677aeSAaron LI {
1327411677aeSAaron LI static char buf[128];
1328411677aeSAaron LI snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
1329411677aeSAaron LI tci & 0xfff,
1330411677aeSAaron LI tci >> 13,
1331411677aeSAaron LI (tci & 0x1000) ? ", DEI" : "");
1332411677aeSAaron LI return buf;
1333411677aeSAaron LI }
1334