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