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