2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Internet, ethernet, port, and protocol string to address
22 * and address to string conversion routines
30 #include <libcasper.h>
31 #include <casper/cap_dns.h>
32 #endif /* HAVE_CASPER */
34 #include <netdissect-stdinc.h>
36 #ifdef USE_ETHER_NTOHOST
37 #ifdef HAVE_NETINET_IF_ETHER_H
38 struct mbuf
; /* Squelch compiler warnings on some platforms for */
39 struct rtentry
; /* declarations in <net/if.h> */
40 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
41 #include <netinet/if_ether.h>
42 #endif /* HAVE_NETINET_IF_ETHER_H */
43 #ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
44 #include <netinet/ether.h>
45 #endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
47 #if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
48 #ifndef HAVE_STRUCT_ETHER_ADDR
50 unsigned char ether_addr_octet
[6];
53 extern int ether_ntohost(char *, const struct ether_addr
*);
56 #endif /* USE_ETHER_NTOHOST */
59 #include <pcap-namedb.h>
60 #ifndef HAVE_GETSERVENT
61 #include <getservent.h>
68 #include "netdissect.h"
69 #include "addrtoname.h"
70 #include "addrtostr.h"
71 #include "ethertype.h"
76 #ifndef ETHER_ADDR_LEN
77 #define ETHER_ADDR_LEN 6
81 * hash tables for whatever-to-name translations
83 * ndo_error() called on strdup(3) failure
86 #define HASHNAMESIZE 4096
94 static struct hnamemem hnametable
[HASHNAMESIZE
];
95 static struct hnamemem tporttable
[HASHNAMESIZE
];
96 static struct hnamemem uporttable
[HASHNAMESIZE
];
97 static struct hnamemem eprototable
[HASHNAMESIZE
];
98 static struct hnamemem dnaddrtable
[HASHNAMESIZE
];
99 static struct hnamemem ipxsaptable
[HASHNAMESIZE
];
103 * fake gethostbyaddr for Win2k/XP
104 * gethostbyaddr() returns incorrect value when AF_INET6 is passed
107 * h_name in struct hostent is only valid.
109 static struct hostent
*
110 win32_gethostbyaddr(const char *addr
, int len
, int type
)
112 static struct hostent host
;
113 static char hostbuf
[NI_MAXHOST
];
114 char hname
[NI_MAXHOST
];
115 struct sockaddr_in6 addr6
;
117 host
.h_name
= hostbuf
;
120 return gethostbyaddr(addr
, len
, type
);
123 memset(&addr6
, 0, sizeof(addr6
));
124 addr6
.sin6_family
= AF_INET6
;
125 memcpy(&addr6
.sin6_addr
, addr
, len
);
126 if (getnameinfo((struct sockaddr
*)&addr6
, sizeof(addr6
),
127 hname
, sizeof(hname
), NULL
, 0, 0)) {
130 strcpy(host
.h_name
, hname
);
138 #define gethostbyaddr win32_gethostbyaddr
142 struct in6_addr addr
;
144 struct h6namemem
*nxt
;
147 static struct h6namemem h6nametable
[HASHNAMESIZE
];
154 u_char
*e_nsap
; /* used only for nsaptable[] */
155 struct enamemem
*e_nxt
;
158 static struct enamemem enametable
[HASHNAMESIZE
];
159 static struct enamemem nsaptable
[HASHNAMESIZE
];
167 unsigned int bs_nbytes
;
168 struct bsnamemem
*bs_nxt
;
171 static struct bsnamemem bytestringtable
[HASHNAMESIZE
];
177 struct protoidmem
*p_nxt
;
180 static struct protoidmem protoidtable
[HASHNAMESIZE
];
183 * A faster replacement for inet_ntoa().
191 static char buf
[sizeof(".xxx.xxx.xxx.xxx")];
194 cp
= buf
+ sizeof(buf
);
200 *--cp
= byte
% 10 + '0';
203 *--cp
= byte
% 10 + '0';
215 static uint32_t f_netmask
;
216 static uint32_t f_localnet
;
218 extern cap_channel_t
*capdns
;
222 * Return a name for the IP address pointed to by ap. This address
223 * is assumed to be in network byte order.
225 * NOTE: ap is *NOT* necessarily part of the packet data (not even if
226 * this is being called with the "ipaddr_string()" macro), so you
227 * *CANNOT* use the ND_TCHECK{2}/ND_TTEST{2} macros on it. Furthermore,
228 * even in cases where it *is* part of the packet data, the caller
229 * would still have to check for a null return value, even if it's
230 * just printing the return value with "%s" - not all versions of
231 * printf print "(null)" with "%s" and a null pointer, some of them
232 * don't check for a null pointer and crash in that case.
234 * The callers of this routine should, before handing this routine
235 * a pointer to packet data, be sure that the data is present in
236 * the packet buffer. They should probably do those checks anyway,
237 * as other data at that layer might not be IP addresses, and it
238 * also needs to check whether they're present in the packet buffer.
241 getname(netdissect_options
*ndo
, const u_char
*ap
)
243 register struct hostent
*hp
;
247 memcpy(&addr
, ap
, sizeof(addr
));
248 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
249 for (; p
->nxt
; p
= p
->nxt
) {
254 p
->nxt
= newhnamemem(ndo
);
257 * Print names unless:
259 * (2) Address is foreign and -f was given. (If -f was not
260 * given, f_netmask and f_localnet are 0 and the test
263 if (!ndo
->ndo_nflag
&&
264 (addr
& f_netmask
) == f_localnet
) {
266 if (capdns
!= NULL
) {
267 hp
= cap_gethostbyaddr(capdns
, (char *)&addr
, 4,
271 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
275 p
->name
= strdup(hp
->h_name
);
277 (*ndo
->ndo_error
)(ndo
,
278 "getname: strdup(hp->h_name)");
279 if (ndo
->ndo_Nflag
) {
280 /* Remove domain qualifications */
281 dotp
= strchr(p
->name
, '.');
288 p
->name
= strdup(intoa(addr
));
290 (*ndo
->ndo_error
)(ndo
, "getname: strdup(intoa(addr))");
295 * Return a name for the IP6 address pointed to by ap. This address
296 * is assumed to be in network byte order.
299 getname6(netdissect_options
*ndo
, const u_char
*ap
)
301 register struct hostent
*hp
;
303 struct in6_addr addr
;
304 struct for_hash_addr
{
310 register const char *cp
;
311 char ntop_buf
[INET6_ADDRSTRLEN
];
313 memcpy(&addr
, ap
, sizeof(addr
));
314 p
= &h6nametable
[addr
.addra
.d
& (HASHNAMESIZE
-1)];
315 for (; p
->nxt
; p
= p
->nxt
) {
316 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
320 p
->nxt
= newh6namemem(ndo
);
323 * Do not print names if -n was given.
325 if (!ndo
->ndo_nflag
) {
327 if (capdns
!= NULL
) {
328 hp
= cap_gethostbyaddr(capdns
, (char *)&addr
,
329 sizeof(addr
), AF_INET6
);
332 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
),
337 p
->name
= strdup(hp
->h_name
);
339 (*ndo
->ndo_error
)(ndo
,
340 "getname6: strdup(hp->h_name)");
341 if (ndo
->ndo_Nflag
) {
342 /* Remove domain qualifications */
343 dotp
= strchr(p
->name
, '.');
350 cp
= addrtostr6(ap
, ntop_buf
, sizeof(ntop_buf
));
351 p
->name
= strdup(cp
);
353 (*ndo
->ndo_error
)(ndo
, "getname6: strdup(cp)");
357 static const char hex
[16] = {
358 '0', '1', '2', '3', '4', '5', '6', '7',
359 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
362 /* Find the hash node that corresponds the ether address 'ep' */
364 static inline struct enamemem
*
365 lookup_emem(netdissect_options
*ndo
, const u_char
*ep
)
367 register u_int i
, j
, k
;
370 k
= (ep
[0] << 8) | ep
[1];
371 j
= (ep
[2] << 8) | ep
[3];
372 i
= (ep
[4] << 8) | ep
[5];
374 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
376 if (tp
->e_addr0
== i
&&
385 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
386 if (tp
->e_nxt
== NULL
)
387 (*ndo
->ndo_error
)(ndo
, "lookup_emem: calloc");
393 * Find the hash node that corresponds to the bytestring 'bs'
397 static inline struct bsnamemem
*
398 lookup_bytestring(netdissect_options
*ndo
, register const u_char
*bs
,
399 const unsigned int nlen
)
401 struct bsnamemem
*tp
;
402 register u_int i
, j
, k
;
405 k
= (bs
[0] << 8) | bs
[1];
406 j
= (bs
[2] << 8) | bs
[3];
407 i
= (bs
[4] << 8) | bs
[5];
408 } else if (nlen
>= 4) {
409 k
= (bs
[0] << 8) | bs
[1];
410 j
= (bs
[2] << 8) | bs
[3];
415 tp
= &bytestringtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
417 if (nlen
== tp
->bs_nbytes
&&
421 memcmp((const char *)bs
, (const char *)(tp
->bs_bytes
), nlen
) == 0)
430 tp
->bs_bytes
= (u_char
*) calloc(1, nlen
);
431 if (tp
->bs_bytes
== NULL
)
432 (*ndo
->ndo_error
)(ndo
, "lookup_bytestring: calloc");
434 memcpy(tp
->bs_bytes
, bs
, nlen
);
435 tp
->bs_nbytes
= nlen
;
436 tp
->bs_nxt
= (struct bsnamemem
*)calloc(1, sizeof(*tp
));
437 if (tp
->bs_nxt
== NULL
)
438 (*ndo
->ndo_error
)(ndo
, "lookup_bytestring: calloc");
443 /* Find the hash node that corresponds the NSAP 'nsap' */
445 static inline struct enamemem
*
446 lookup_nsap(netdissect_options
*ndo
, register const u_char
*nsap
,
447 register u_int nsap_length
)
449 register u_int i
, j
, k
;
453 if (nsap_length
> 6) {
454 ensap
= nsap
+ nsap_length
- 6;
455 k
= (ensap
[0] << 8) | ensap
[1];
456 j
= (ensap
[2] << 8) | ensap
[3];
457 i
= (ensap
[4] << 8) | ensap
[5];
462 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
464 if (nsap_length
== tp
->e_nsap
[0] &&
468 memcmp((const char *)nsap
,
469 (char *)&(tp
->e_nsap
[1]), nsap_length
) == 0)
476 tp
->e_nsap
= (u_char
*)malloc(nsap_length
+ 1);
477 if (tp
->e_nsap
== NULL
)
478 (*ndo
->ndo_error
)(ndo
, "lookup_nsap: malloc");
479 tp
->e_nsap
[0] = (u_char
)nsap_length
; /* guaranteed < ISONSAP_MAX_LENGTH */
480 memcpy((char *)&tp
->e_nsap
[1], (const char *)nsap
, nsap_length
);
481 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
482 if (tp
->e_nxt
== NULL
)
483 (*ndo
->ndo_error
)(ndo
, "lookup_nsap: calloc");
488 /* Find the hash node that corresponds the protoid 'pi'. */
490 static inline struct protoidmem
*
491 lookup_protoid(netdissect_options
*ndo
, const u_char
*pi
)
494 struct protoidmem
*tp
;
496 /* 5 octets won't be aligned */
497 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
498 j
= (pi
[3] << 8) + pi
[4];
499 /* XXX should be endian-insensitive, but do big-endian testing XXX */
501 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
503 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
509 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
510 if (tp
->p_nxt
== NULL
)
511 (*ndo
->ndo_error
)(ndo
, "lookup_protoid: calloc");
517 etheraddr_string(netdissect_options
*ndo
, register const u_char
*ep
)
521 register struct enamemem
*tp
;
525 tp
= lookup_emem(ndo
, ep
);
528 #ifdef USE_ETHER_NTOHOST
529 if (!ndo
->ndo_nflag
) {
532 if (ether_ntohost(buf2
, (const struct ether_addr
*)ep
) == 0) {
533 tp
->e_name
= strdup(buf2
);
534 if (tp
->e_name
== NULL
)
535 (*ndo
->ndo_error
)(ndo
,
536 "etheraddr_string: strdup(buf2)");
542 oui
= EXTRACT_BE_U_3(ep
);
543 *cp
++ = hex
[*ep
>> 4 ];
544 *cp
++ = hex
[*ep
++ & 0xf];
545 for (i
= 5; --i
>= 0;) {
547 *cp
++ = hex
[*ep
>> 4 ];
548 *cp
++ = hex
[*ep
++ & 0xf];
551 if (!ndo
->ndo_nflag
) {
552 snprintf(cp
, BUFSIZE
- (2 + 5*3), " (oui %s)",
553 tok2str(oui_values
, "Unknown", oui
));
556 tp
->e_name
= strdup(buf
);
557 if (tp
->e_name
== NULL
)
558 (*ndo
->ndo_error
)(ndo
, "etheraddr_string: strdup(buf)");
563 le64addr_string(netdissect_options
*ndo
, const u_char
*ep
)
565 const unsigned int len
= 8;
568 register struct bsnamemem
*tp
;
571 tp
= lookup_bytestring(ndo
, ep
, len
);
573 return (tp
->bs_name
);
576 for (i
= len
; i
> 0 ; --i
) {
577 *cp
++ = hex
[*(ep
+ i
- 1) >> 4];
578 *cp
++ = hex
[*(ep
+ i
- 1) & 0xf];
585 tp
->bs_name
= strdup(buf
);
586 if (tp
->bs_name
== NULL
)
587 (*ndo
->ndo_error
)(ndo
, "le64addr_string: strdup(buf)");
589 return (tp
->bs_name
);
593 linkaddr_string(netdissect_options
*ndo
, const u_char
*ep
,
594 const unsigned int type
, const unsigned int len
)
598 register struct bsnamemem
*tp
;
603 if (type
== LINKADDR_ETHER
&& len
== ETHER_ADDR_LEN
)
604 return (etheraddr_string(ndo
, ep
));
606 if (type
== LINKADDR_FRELAY
)
607 return (q922_string(ndo
, ep
, len
));
609 tp
= lookup_bytestring(ndo
, ep
, len
);
611 return (tp
->bs_name
);
613 tp
->bs_name
= cp
= (char *)malloc(len
*3);
614 if (tp
->bs_name
== NULL
)
615 (*ndo
->ndo_error
)(ndo
, "linkaddr_string: malloc");
616 *cp
++ = hex
[*ep
>> 4];
617 *cp
++ = hex
[*ep
++ & 0xf];
618 for (i
= len
-1; i
> 0 ; --i
) {
620 *cp
++ = hex
[*ep
>> 4];
621 *cp
++ = hex
[*ep
++ & 0xf];
624 return (tp
->bs_name
);
628 etherproto_string(netdissect_options
*ndo
, u_short port
)
631 register struct hnamemem
*tp
;
632 register uint32_t i
= port
;
633 char buf
[sizeof("0000")];
635 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
640 tp
->nxt
= newhnamemem(ndo
);
644 *cp
++ = hex
[port
>> 12 & 0xf];
645 *cp
++ = hex
[port
>> 8 & 0xf];
646 *cp
++ = hex
[port
>> 4 & 0xf];
647 *cp
++ = hex
[port
& 0xf];
649 tp
->name
= strdup(buf
);
650 if (tp
->name
== NULL
)
651 (*ndo
->ndo_error
)(ndo
, "etherproto_string: strdup(buf)");
655 #define ISONSAP_MAX_LENGTH 20
657 isonsap_string(netdissect_options
*ndo
, const u_char
*nsap
,
658 register u_int nsap_length
)
660 register u_int nsap_idx
;
662 register struct enamemem
*tp
;
664 if (nsap_length
< 1 || nsap_length
> ISONSAP_MAX_LENGTH
)
665 return ("isonsap_string: illegal length");
667 tp
= lookup_nsap(ndo
, nsap
, nsap_length
);
671 tp
->e_name
= cp
= (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
673 (*ndo
->ndo_error
)(ndo
, "isonsap_string: malloc");
675 for (nsap_idx
= 0; nsap_idx
< nsap_length
; nsap_idx
++) {
676 *cp
++ = hex
[*nsap
>> 4];
677 *cp
++ = hex
[*nsap
++ & 0xf];
678 if (((nsap_idx
& 1) == 0) &&
679 (nsap_idx
+ 1 < nsap_length
)) {
688 tcpport_string(netdissect_options
*ndo
, u_short port
)
690 register struct hnamemem
*tp
;
691 register uint32_t i
= port
;
692 char buf
[sizeof("00000")];
694 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
699 tp
->nxt
= newhnamemem(ndo
);
701 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
702 tp
->name
= strdup(buf
);
703 if (tp
->name
== NULL
)
704 (*ndo
->ndo_error
)(ndo
, "tcpport_string: strdup(buf)");
709 udpport_string(netdissect_options
*ndo
, register u_short port
)
711 register struct hnamemem
*tp
;
712 register uint32_t i
= port
;
713 char buf
[sizeof("00000")];
715 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
720 tp
->nxt
= newhnamemem(ndo
);
722 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
723 tp
->name
= strdup(buf
);
724 if (tp
->name
== NULL
)
725 (*ndo
->ndo_error
)(ndo
, "udpport_string: strdup(buf)");
730 ipxsap_string(netdissect_options
*ndo
, u_short port
)
733 register struct hnamemem
*tp
;
734 register uint32_t i
= port
;
735 char buf
[sizeof("0000")];
737 for (tp
= &ipxsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
742 tp
->nxt
= newhnamemem(ndo
);
746 *cp
++ = hex
[port
>> 12 & 0xf];
747 *cp
++ = hex
[port
>> 8 & 0xf];
748 *cp
++ = hex
[port
>> 4 & 0xf];
749 *cp
++ = hex
[port
& 0xf];
751 tp
->name
= strdup(buf
);
752 if (tp
->name
== NULL
)
753 (*ndo
->ndo_error
)(ndo
, "ipxsap_string: strdup(buf)");
758 init_servarray(netdissect_options
*ndo
)
761 register struct hnamemem
*table
;
763 char buf
[sizeof("0000000000")];
765 while ((sv
= getservent()) != NULL
) {
766 int port
= ntohs(sv
->s_port
);
767 i
= port
& (HASHNAMESIZE
-1);
768 if (strcmp(sv
->s_proto
, "tcp") == 0)
769 table
= &tporttable
[i
];
770 else if (strcmp(sv
->s_proto
, "udp") == 0)
771 table
= &uporttable
[i
];
777 if (ndo
->ndo_nflag
) {
778 (void)snprintf(buf
, sizeof(buf
), "%d", port
);
779 table
->name
= strdup(buf
);
781 table
->name
= strdup(sv
->s_name
);
782 if (table
->name
== NULL
)
783 (*ndo
->ndo_error
)(ndo
, "init_servarray: strdup");
786 table
->nxt
= newhnamemem(ndo
);
791 static const struct eproto
{
795 { "pup", ETHERTYPE_PUP
},
796 { "xns", ETHERTYPE_NS
},
797 { "ip", ETHERTYPE_IP
},
798 { "ip6", ETHERTYPE_IPV6
},
799 { "arp", ETHERTYPE_ARP
},
800 { "rarp", ETHERTYPE_REVARP
},
801 { "sprite", ETHERTYPE_SPRITE
},
802 { "mopdl", ETHERTYPE_MOPDL
},
803 { "moprc", ETHERTYPE_MOPRC
},
804 { "decnet", ETHERTYPE_DN
},
805 { "lat", ETHERTYPE_LAT
},
806 { "sca", ETHERTYPE_SCA
},
807 { "lanbridge", ETHERTYPE_LANBRIDGE
},
808 { "vexp", ETHERTYPE_VEXP
},
809 { "vprod", ETHERTYPE_VPROD
},
810 { "atalk", ETHERTYPE_ATALK
},
811 { "atalkarp", ETHERTYPE_AARP
},
812 { "loopback", ETHERTYPE_LOOPBACK
},
813 { "decdts", ETHERTYPE_DECDTS
},
814 { "decdns", ETHERTYPE_DECDNS
},
819 init_eprotoarray(netdissect_options
*ndo
)
822 register struct hnamemem
*table
;
824 for (i
= 0; eproto_db
[i
].s
; i
++) {
825 int j
= htons(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
826 table
= &eprototable
[j
];
829 table
->name
= eproto_db
[i
].s
;
830 table
->addr
= htons(eproto_db
[i
].p
);
831 table
->nxt
= newhnamemem(ndo
);
835 static const struct protoidlist
{
836 const u_char protoid
[5];
839 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
840 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
841 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
842 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
843 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
844 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
848 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
852 init_protoidarray(netdissect_options
*ndo
)
855 register struct protoidmem
*tp
;
856 const struct protoidlist
*pl
;
862 for (i
= 0; eproto_db
[i
].s
; i
++) {
863 u_short etype
= htons(eproto_db
[i
].p
);
865 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
866 tp
= lookup_protoid(ndo
, protoid
);
867 tp
->p_name
= strdup(eproto_db
[i
].s
);
868 if (tp
->p_name
== NULL
)
869 (*ndo
->ndo_error
)(ndo
,
870 "init_protoidarray: strdup(eproto_db[i].s)");
872 /* Hardwire some SNAP proto ID names */
873 for (pl
= protoidlist
; pl
->name
!= NULL
; ++pl
) {
874 tp
= lookup_protoid(ndo
, pl
->protoid
);
875 /* Don't override existing name */
876 if (tp
->p_name
!= NULL
)
879 tp
->p_name
= pl
->name
;
883 static const struct etherlist
{
884 const u_char addr
[6];
887 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
888 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
892 * Initialize the ethers hash table. We take two different approaches
893 * depending on whether or not the system provides the ethers name
894 * service. If it does, we just wire in a few names at startup,
895 * and etheraddr_string() fills in the table on demand. If it doesn't,
896 * then we suck in the entire /etc/ethers file at startup. The idea
897 * is that parsing the local file will be fast, but spinning through
898 * all the ethers entries via NIS & next_etherent might be very slow.
900 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
901 * since the pcap module already does name-to-address translation,
902 * it's already does most of the work for the ethernet address-to-name
903 * translation, so we just pcap_next_etherent as a convenience.
906 init_etherarray(netdissect_options
*ndo
)
908 register const struct etherlist
*el
;
909 register struct enamemem
*tp
;
910 #ifdef USE_ETHER_NTOHOST
913 register struct pcap_etherent
*ep
;
916 /* Suck in entire ethers file */
917 fp
= fopen(PCAP_ETHERS_FILE
, "r");
919 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
920 tp
= lookup_emem(ndo
, ep
->addr
);
921 tp
->e_name
= strdup(ep
->name
);
922 if (tp
->e_name
== NULL
)
923 (*ndo
->ndo_error
)(ndo
,
924 "init_etherarray: strdup(ep->addr)");
930 /* Hardwire some ethernet names */
931 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
932 tp
= lookup_emem(ndo
, el
->addr
);
933 /* Don't override existing name */
934 if (tp
->e_name
!= NULL
)
937 #ifdef USE_ETHER_NTOHOST
939 * Use YP/NIS version of name if available.
941 if (ether_ntohost(name
, (const struct ether_addr
*)el
->addr
) == 0) {
942 tp
->e_name
= strdup(name
);
943 if (tp
->e_name
== NULL
)
944 (*ndo
->ndo_error
)(ndo
,
945 "init_etherarray: strdup(name)");
949 tp
->e_name
= el
->name
;
953 static const struct tok ipxsap_db
[] = {
954 { 0x0000, "Unknown" },
956 { 0x0002, "User Group" },
957 { 0x0003, "PrintQueue" },
958 { 0x0004, "FileServer" },
959 { 0x0005, "JobServer" },
960 { 0x0006, "Gateway" },
961 { 0x0007, "PrintServer" },
962 { 0x0008, "ArchiveQueue" },
963 { 0x0009, "ArchiveServer" },
964 { 0x000a, "JobQueue" },
965 { 0x000b, "Administration" },
966 { 0x000F, "Novell TI-RPC" },
967 { 0x0017, "Diagnostics" },
968 { 0x0020, "NetBIOS" },
969 { 0x0021, "NAS SNA Gateway" },
970 { 0x0023, "NACS AsyncGateway" },
971 { 0x0024, "RemoteBridge/RoutingService" },
972 { 0x0026, "BridgeServer" },
973 { 0x0027, "TCP/IP Gateway" },
974 { 0x0028, "Point-to-point X.25 BridgeServer" },
975 { 0x0029, "3270 Gateway" },
976 { 0x002a, "CHI Corp" },
977 { 0x002c, "PC Chalkboard" },
978 { 0x002d, "TimeSynchServer" },
979 { 0x002e, "ARCserve5.0/PalindromeBackup" },
980 { 0x0045, "DI3270 Gateway" },
981 { 0x0047, "AdvertisingPrintServer" },
982 { 0x004a, "NetBlazerModems" },
983 { 0x004b, "BtrieveVAP" },
984 { 0x004c, "NetwareSQL" },
985 { 0x004d, "XtreeNetwork" },
986 { 0x0050, "BtrieveVAP4.11" },
987 { 0x0052, "QuickLink" },
988 { 0x0053, "PrintQueueUser" },
989 { 0x0058, "Multipoint X.25 Router" },
990 { 0x0060, "STLB/NLM" },
991 { 0x0064, "ARCserve" },
992 { 0x0066, "ARCserve3.0" },
993 { 0x0072, "WAN CopyUtility" },
994 { 0x007a, "TES-NetwareVMS" },
995 { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
996 { 0x0095, "DDA OBGYN" },
997 { 0x0098, "NetwareAccessServer" },
998 { 0x009a, "Netware for VMS II/NamedPipeServer" },
999 { 0x009b, "NetwareAccessServer" },
1000 { 0x009e, "PortableNetwareServer/SunLinkNVT" },
1001 { 0x00a1, "PowerchuteAPC UPS" },
1002 { 0x00aa, "LAWserve" },
1003 { 0x00ac, "CompaqIDA StatusMonitor" },
1004 { 0x0100, "PIPE STAIL" },
1005 { 0x0102, "LAN ProtectBindery" },
1006 { 0x0103, "OracleDataBaseServer" },
1007 { 0x0107, "Netware386/RSPX RemoteConsole" },
1008 { 0x010f, "NovellSNA Gateway" },
1009 { 0x0111, "TestServer" },
1010 { 0x0112, "HP PrintServer" },
1011 { 0x0114, "CSA MUX" },
1012 { 0x0115, "CSA LCA" },
1013 { 0x0116, "CSA CM" },
1014 { 0x0117, "CSA SMA" },
1015 { 0x0118, "CSA DBA" },
1016 { 0x0119, "CSA NMA" },
1017 { 0x011a, "CSA SSA" },
1018 { 0x011b, "CSA STATUS" },
1019 { 0x011e, "CSA APPC" },
1020 { 0x0126, "SNA TEST SSA Profile" },
1021 { 0x012a, "CSA TRACE" },
1022 { 0x012b, "NetwareSAA" },
1023 { 0x012e, "IKARUS VirusScan" },
1024 { 0x0130, "CommunicationsExecutive" },
1025 { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
1026 { 0x0135, "NetwareNamingServicesProfile" },
1027 { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
1028 { 0x0141, "LAN SpoolServer" },
1029 { 0x0152, "IRMALAN Gateway" },
1030 { 0x0154, "NamedPipeServer" },
1031 { 0x0166, "NetWareManagement" },
1032 { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
1033 { 0x0173, "Compaq" },
1034 { 0x0174, "Compaq SNMP Agent" },
1035 { 0x0175, "Compaq" },
1036 { 0x0180, "XTreeServer/XTreeTools" },
1037 { 0x018A, "NASI ServicesBroadcastServer" },
1038 { 0x01b0, "GARP Gateway" },
1039 { 0x01b1, "Binfview" },
1040 { 0x01bf, "IntelLanDeskManager" },
1041 { 0x01ca, "AXTEC" },
1042 { 0x01cb, "ShivaNetModem/E" },
1043 { 0x01cc, "ShivaLanRover/E" },
1044 { 0x01cd, "ShivaLanRover/T" },
1045 { 0x01ce, "ShivaUniversal" },
1046 { 0x01d8, "CastelleFAXPressServer" },
1047 { 0x01da, "CastelleLANPressPrintServer" },
1048 { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
1049 { 0x01f0, "LEGATO" },
1050 { 0x01f5, "LEGATO" },
1051 { 0x0233, "NMS Agent/NetwareManagementAgent" },
1052 { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
1053 { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
1054 { 0x023a, "LANtern" },
1055 { 0x023c, "MAVERICK" },
1056 { 0x023f, "NovellSMDR" },
1057 { 0x024e, "NetwareConnect" },
1058 { 0x024f, "NASI ServerBroadcast Cisco" },
1059 { 0x026a, "NMS ServiceConsole" },
1060 { 0x026b, "TimeSynchronizationServer Netware 4.x" },
1061 { 0x0278, "DirectoryServer Netware 4.x" },
1062 { 0x027b, "NetwareManagementAgent" },
1063 { 0x0280, "Novell File and Printer Sharing Service for PC" },
1064 { 0x0304, "NovellSAA Gateway" },
1065 { 0x0308, "COM/VERMED" },
1066 { 0x030a, "GalacticommWorldgroupServer" },
1067 { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1068 { 0x0320, "AttachmateGateway" },
1069 { 0x0327, "MicrosoftDiagnostiocs" },
1070 { 0x0328, "WATCOM SQL Server" },
1071 { 0x0335, "MultiTechSystems MultisynchCommServer" },
1072 { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1073 { 0x0355, "ArcadaBackupExec" },
1074 { 0x0358, "MSLCD1" },
1075 { 0x0361, "NETINELO" },
1076 { 0x037e, "Powerchute UPS Monitoring" },
1077 { 0x037f, "ViruSafeNotify" },
1078 { 0x0386, "HP Bridge" },
1079 { 0x0387, "HP Hub" },
1080 { 0x0394, "NetWare SAA Gateway" },
1081 { 0x039b, "LotusNotes" },
1082 { 0x03b7, "CertusAntiVirus" },
1083 { 0x03c4, "ARCserve4.0" },
1084 { 0x03c7, "LANspool3.5" },
1085 { 0x03d7, "LexmarkPrinterServer" },
1086 { 0x03d8, "LexmarkXLE PrinterServer" },
1087 { 0x03dd, "BanyanENS NetwareClient" },
1088 { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1089 { 0x03e1, "UnivelUnixware" },
1090 { 0x03e4, "UnivelUnixware" },
1091 { 0x03fc, "IntelNetport" },
1092 { 0x03fd, "PrintServerQueue" },
1093 { 0x040A, "ipnServer" },
1094 { 0x040D, "LVERRMAN" },
1095 { 0x040E, "LVLIC" },
1096 { 0x0414, "NET Silicon (DPI)/Kyocera" },
1097 { 0x0429, "SiteLockVirus" },
1098 { 0x0432, "UFHELPR???" },
1099 { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1100 { 0x0444, "MicrosoftNT SNA Server" },
1101 { 0x0448, "Oracle" },
1102 { 0x044c, "ARCserve5.01" },
1103 { 0x0457, "CanonGP55" },
1104 { 0x045a, "QMS Printers" },
1105 { 0x045b, "DellSCSI Array" },
1106 { 0x0491, "NetBlazerModems" },
1107 { 0x04ac, "OnTimeScheduler" },
1108 { 0x04b0, "CD-Net" },
1109 { 0x0513, "EmulexNQA" },
1110 { 0x0520, "SiteLockChecks" },
1111 { 0x0529, "SiteLockChecks" },
1112 { 0x052d, "CitrixOS2 AppServer" },
1113 { 0x0535, "Tektronix" },
1114 { 0x0536, "Milan" },
1115 { 0x055d, "Attachmate SNA gateway" },
1116 { 0x056b, "IBM8235 ModemServer" },
1117 { 0x056c, "ShivaLanRover/E PLUS" },
1118 { 0x056d, "ShivaLanRover/T PLUS" },
1119 { 0x0580, "McAfeeNetShield" },
1120 { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1121 { 0x05BA, "CompatibleSystemsRouters" },
1122 { 0x05BE, "CheyenneHierarchicalStorageManager" },
1123 { 0x0606, "JCWatermarkImaging" },
1124 { 0x060c, "AXISNetworkPrinter" },
1125 { 0x0610, "AdaptecSCSIManagement" },
1126 { 0x0621, "IBM AntiVirus" },
1127 { 0x0640, "Windows95 RemoteRegistryService" },
1128 { 0x064e, "MicrosoftIIS" },
1129 { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1130 { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1131 { 0x076C, "Xerox" },
1132 { 0x079b, "ShivaLanRover/E 115" },
1133 { 0x079c, "ShivaLanRover/T 115" },
1134 { 0x07B4, "CubixWorldDesk" },
1135 { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1136 { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1137 { 0x0810, "ELAN License Server Demo" },
1138 { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1139 { 0x086a, "ISSC Collector" },
1140 { 0x087f, "ISSC DAS AgentAIX" },
1141 { 0x0880, "Intel Netport PRO" },
1142 { 0x0881, "Intel Netport PRO" },
1143 { 0x0b29, "SiteLock" },
1144 { 0x0c29, "SiteLockApplications" },
1145 { 0x0c2c, "LicensingServer" },
1146 { 0x2101, "PerformanceTechnologyInstantInternet" },
1147 { 0x2380, "LAI SiteLock" },
1148 { 0x238c, "MeetingMaker" },
1149 { 0x4808, "SiteLockServer/SiteLockMetering" },
1150 { 0x5555, "SiteLockUser" },
1151 { 0x6312, "Tapeware" },
1152 { 0x6f00, "RabbitGateway" },
1153 { 0x7703, "MODEM" },
1154 { 0x8002, "NetPortPrinters" },
1155 { 0x8008, "WordPerfectNetworkVersion" },
1156 { 0x85BE, "Cisco EIGRP" },
1157 { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1158 { 0x9000, "McAfeeNetShield" },
1159 { 0x9604, "CSA-NT_MON" },
1160 { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1161 { 0xf11f, "SiteLockMetering" },
1162 { 0xf1ff, "SiteLock" },
1163 { 0xf503, "Microsoft SQL Server" },
1164 { 0xF905, "IBM TimeAndPlace" },
1165 { 0xfbfb, "TopCallIII FaxServer" },
1166 { 0xffff, "AnyService/Wildcard" },
1171 init_ipxsaparray(netdissect_options
*ndo
)
1174 register struct hnamemem
*table
;
1176 for (i
= 0; ipxsap_db
[i
].s
!= NULL
; i
++) {
1177 int j
= htons(ipxsap_db
[i
].v
) & (HASHNAMESIZE
-1);
1178 table
= &ipxsaptable
[j
];
1181 table
->name
= ipxsap_db
[i
].s
;
1182 table
->addr
= htons(ipxsap_db
[i
].v
);
1183 table
->nxt
= newhnamemem(ndo
);
1188 * Initialize the address to name translation machinery. We map all
1189 * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
1190 * (i.e., to prevent blocking on the nameserver). localnet is the IP address
1191 * of the local network. mask is its subnet mask.
1194 init_addrtoname(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
)
1196 if (ndo
->ndo_fflag
) {
1197 f_localnet
= localnet
;
1202 * Simplest way to suppress names.
1206 init_etherarray(ndo
);
1207 init_servarray(ndo
);
1208 init_eprotoarray(ndo
);
1209 init_protoidarray(ndo
);
1210 init_ipxsaparray(ndo
);
1214 dnaddr_string(netdissect_options
*ndo
, u_short dnaddr
)
1216 register struct hnamemem
*tp
;
1218 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= NULL
;
1220 if (tp
->addr
== dnaddr
)
1224 tp
->nxt
= newhnamemem(ndo
);
1226 tp
->name
= dnnum_string(ndo
, dnaddr
);
1228 tp
->name
= dnname_string(ndo
, dnaddr
);
1233 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1235 newhnamemem(netdissect_options
*ndo
)
1237 register struct hnamemem
*p
;
1238 static struct hnamemem
*ptr
= NULL
;
1239 static u_int num
= 0;
1243 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
1245 (*ndo
->ndo_error
)(ndo
, "newhnamemem: calloc");
1252 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1254 newh6namemem(netdissect_options
*ndo
)
1256 register struct h6namemem
*p
;
1257 static struct h6namemem
*ptr
= NULL
;
1258 static u_int num
= 0;
1262 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
1264 (*ndo
->ndo_error
)(ndo
, "newh6namemem: calloc");
1271 /* Represent TCI part of the 802.1Q 4-octet tag as text. */
1273 ieee8021q_tci_string(const uint16_t tci
)
1275 static char buf
[128];
1276 snprintf(buf
, sizeof(buf
), "vlan %u, p %u%s",
1279 (tci
& 0x1000) ? ", DEI" : "");