]> The Tcpdump Group git mirrors - tcpdump/blob - addrtoname.c
KNF
[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[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.95 2003-06-05 13:36:33 risso 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 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
107 hname, sizeof(hname), NULL, 0, 0)) {
108 return NULL;
109 } else {
110 strcpy(host.h_name, hname);
111 return &host;
112 }
113 break;
114 default:
115 return NULL;
116 }
117 }
118 #define gethostbyaddr win32_gethostbyaddr
119 #endif /* INET6 & WIN32*/
120
121 #ifdef INET6
122 struct h6namemem {
123 struct in6_addr addr;
124 char *name;
125 struct h6namemem *nxt;
126 };
127
128 struct h6namemem h6nametable[HASHNAMESIZE];
129 #endif /* INET6 */
130
131 struct enamemem {
132 u_short e_addr0;
133 u_short e_addr1;
134 u_short e_addr2;
135 const char *e_name;
136 u_char *e_nsap; /* used only for nsaptable[] */
137 #define e_bs e_nsap /* for bytestringtable */
138 struct enamemem *e_nxt;
139 };
140
141 struct enamemem enametable[HASHNAMESIZE];
142 struct enamemem nsaptable[HASHNAMESIZE];
143 struct enamemem bytestringtable[HASHNAMESIZE];
144
145 struct protoidmem {
146 u_int32_t p_oui;
147 u_short p_proto;
148 const char *p_name;
149 struct protoidmem *p_nxt;
150 };
151
152 struct protoidmem protoidtable[HASHNAMESIZE];
153
154 /*
155 * A faster replacement for inet_ntoa().
156 */
157 const char *
158 intoa(u_int32_t addr)
159 {
160 register char *cp;
161 register u_int byte;
162 register int n;
163 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
164
165 NTOHL(addr);
166 cp = &buf[sizeof buf];
167 *--cp = '\0';
168
169 n = 4;
170 do {
171 byte = addr & 0xff;
172 *--cp = byte % 10 + '0';
173 byte /= 10;
174 if (byte > 0) {
175 *--cp = byte % 10 + '0';
176 byte /= 10;
177 if (byte > 0)
178 *--cp = byte + '0';
179 }
180 *--cp = '.';
181 addr >>= 8;
182 } while (--n > 0);
183
184 return cp + 1;
185 }
186
187 static u_int32_t f_netmask;
188 static u_int32_t f_localnet;
189 static u_int32_t netmask;
190
191 /*
192 * Return a name for the IP address pointed to by ap. This address
193 * is assumed to be in network byte order.
194 */
195 const char *
196 getname(const u_char *ap)
197 {
198 register struct hostent *hp;
199 u_int32_t addr;
200 static struct hnamemem *p; /* static for longjmp() */
201
202 memcpy(&addr, ap, sizeof(addr));
203 p = &hnametable[addr & (HASHNAMESIZE-1)];
204 for (; p->nxt; p = p->nxt) {
205 if (p->addr == addr)
206 return (p->name);
207 }
208 p->addr = addr;
209 p->nxt = newhnamemem();
210
211 /*
212 * Only print names when:
213 * (1) -n was not given.
214 * (2) Address is foreign and -f was given. (If -f was not
215 * give, f_netmask and f_local are 0 and the test
216 * evaluates to true)
217 * (3) -a was given or the host portion is not all ones
218 * nor all zeros (i.e. not a network or broadcast address)
219 */
220 if (!nflag &&
221 (addr & f_netmask) == f_localnet &&
222 (aflag ||
223 !((addr & ~netmask) == 0 || (addr | netmask) == 0xffffffff))) {
224 hp = gethostbyaddr((char *)&addr, 4, AF_INET);
225 if (hp) {
226 char *dotp;
227
228 p->name = strdup(hp->h_name);
229 if (Nflag) {
230 /* Remove domain qualifications */
231 dotp = strchr(p->name, '.');
232 if (dotp)
233 *dotp = '\0';
234 }
235 return (p->name);
236 }
237 }
238 p->name = strdup(intoa(addr));
239 return (p->name);
240 }
241
242 #ifdef INET6
243 /*
244 * Return a name for the IP6 address pointed to by ap. This address
245 * is assumed to be in network byte order.
246 */
247 const char *
248 getname6(const u_char *ap)
249 {
250 register struct hostent *hp;
251 struct in6_addr addr;
252 static struct h6namemem *p; /* static for longjmp() */
253 register const char *cp;
254 char ntop_buf[INET6_ADDRSTRLEN];
255
256 memcpy(&addr, ap, sizeof(addr));
257 p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
258 for (; p->nxt; p = p->nxt) {
259 if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
260 return (p->name);
261 }
262 p->addr = addr;
263 p->nxt = newh6namemem();
264
265 /*
266 * Only print names when:
267 * (1) -n was not given.
268 * (2) Address is foreign and -f was given. (If -f was not
269 * give, f_netmask and f_local are 0 and the test
270 * evaluates to true)
271 * (3) -a was given or the host portion is not all ones
272 * nor all zeros (i.e. not a network or broadcast address)
273 */
274 if (!nflag
275 #if 0
276 &&
277 (addr & f_netmask) == f_localnet &&
278 (aflag ||
279 !((addr & ~netmask) == 0 || (addr | netmask) == 0xffffffff))
280 #endif
281 ) {
282 hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
283 if (hp) {
284 char *dotp;
285
286 p->name = strdup(hp->h_name);
287 if (Nflag) {
288 /* Remove domain qualifications */
289 dotp = strchr(p->name, '.');
290 if (dotp)
291 *dotp = '\0';
292 }
293 return (p->name);
294 }
295 }
296 cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
297 p->name = strdup(cp);
298 return (p->name);
299 }
300 #endif /* INET6 */
301
302 static char hex[] = "0123456789abcdef";
303
304
305 /* Find the hash node that corresponds the ether address 'ep' */
306
307 static inline struct enamemem *
308 lookup_emem(const u_char *ep)
309 {
310 register u_int i, j, k;
311 struct enamemem *tp;
312
313 k = (ep[0] << 8) | ep[1];
314 j = (ep[2] << 8) | ep[3];
315 i = (ep[4] << 8) | ep[5];
316
317 tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
318 while (tp->e_nxt)
319 if (tp->e_addr0 == i &&
320 tp->e_addr1 == j &&
321 tp->e_addr2 == k)
322 return tp;
323 else
324 tp = tp->e_nxt;
325 tp->e_addr0 = i;
326 tp->e_addr1 = j;
327 tp->e_addr2 = k;
328 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
329 if (tp->e_nxt == NULL)
330 error("lookup_emem: calloc");
331
332 return tp;
333 }
334
335 /*
336 * Find the hash node that corresponds to the bytestring 'bs'
337 * with length 'nlen'
338 */
339
340 static inline struct enamemem *
341 lookup_bytestring(register const u_char *bs, const unsigned int nlen)
342 {
343 struct enamemem *tp;
344 register u_int i, j, k;
345
346 if (nlen >= 6) {
347 k = (bs[0] << 8) | bs[1];
348 j = (bs[2] << 8) | bs[3];
349 i = (bs[4] << 8) | bs[5];
350 } else if (nlen >= 4) {
351 k = (bs[0] << 8) | bs[1];
352 j = (bs[2] << 8) | bs[3];
353 i = 0;
354 } else
355 i = j = k = 0;
356
357 tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
358 while (tp->e_nxt)
359 if (tp->e_addr0 == i &&
360 tp->e_addr1 == j &&
361 tp->e_addr2 == k &&
362 memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
363 return tp;
364 else
365 tp = tp->e_nxt;
366
367 tp->e_addr0 = i;
368 tp->e_addr1 = j;
369 tp->e_addr2 = k;
370
371 tp->e_bs = (u_char *) calloc(1, nlen + 1);
372 memcpy(tp->e_bs, bs, nlen);
373 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
374 if (tp->e_nxt == NULL)
375 error("lookup_bytestring: calloc");
376
377 return tp;
378 }
379
380 /* Find the hash node that corresponds the NSAP 'nsap' */
381
382 static inline struct enamemem *
383 lookup_nsap(register const u_char *nsap)
384 {
385 register u_int i, j, k;
386 unsigned int nlen = *nsap;
387 struct enamemem *tp;
388 const u_char *ensap = nsap + nlen - 6;
389
390 if (nlen > 6) {
391 k = (ensap[0] << 8) | ensap[1];
392 j = (ensap[2] << 8) | ensap[3];
393 i = (ensap[4] << 8) | ensap[5];
394 }
395 else
396 i = j = k = 0;
397
398 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
399 while (tp->e_nxt)
400 if (tp->e_addr0 == i &&
401 tp->e_addr1 == j &&
402 tp->e_addr2 == k &&
403 tp->e_nsap[0] == nlen &&
404 memcmp((const char *)&(nsap[1]),
405 (char *)&(tp->e_nsap[1]), nlen) == 0)
406 return tp;
407 else
408 tp = tp->e_nxt;
409 tp->e_addr0 = i;
410 tp->e_addr1 = j;
411 tp->e_addr2 = k;
412 tp->e_nsap = (u_char *)malloc(nlen + 1);
413 if (tp->e_nsap == NULL)
414 error("lookup_nsap: malloc");
415 memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
416 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
417 if (tp->e_nxt == NULL)
418 error("lookup_nsap: calloc");
419
420 return tp;
421 }
422
423 /* Find the hash node that corresponds the protoid 'pi'. */
424
425 static inline struct protoidmem *
426 lookup_protoid(const u_char *pi)
427 {
428 register u_int i, j;
429 struct protoidmem *tp;
430
431 /* 5 octets won't be aligned */
432 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
433 j = (pi[3] << 8) + pi[4];
434 /* XXX should be endian-insensitive, but do big-endian testing XXX */
435
436 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
437 while (tp->p_nxt)
438 if (tp->p_oui == i && tp->p_proto == j)
439 return tp;
440 else
441 tp = tp->p_nxt;
442 tp->p_oui = i;
443 tp->p_proto = j;
444 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
445 if (tp->p_nxt == NULL)
446 error("lookup_protoid: calloc");
447
448 return tp;
449 }
450
451 const char *
452 etheraddr_string(register const u_char *ep)
453 {
454 register u_int i;
455 register char *cp;
456 register struct enamemem *tp;
457 char buf[sizeof("00:00:00:00:00:00")];
458
459 tp = lookup_emem(ep);
460 if (tp->e_name)
461 return (tp->e_name);
462 #ifdef USE_ETHER_NTOHOST
463 if (!nflag) {
464 char buf2[128];
465 if (ether_ntohost(buf2, (const struct ether_addr *)ep) == 0) {
466 tp->e_name = strdup(buf2);
467 return (tp->e_name);
468 }
469 }
470 #endif
471 cp = buf;
472 *cp++ = hex[*ep >> 4 ];
473 *cp++ = hex[*ep++ & 0xf];
474 for (i = 5; (int)--i >= 0;) {
475 *cp++ = ':';
476 *cp++ = hex[*ep >> 4 ];
477 *cp++ = hex[*ep++ & 0xf];
478 }
479 *cp = '\0';
480 tp->e_name = strdup(buf);
481 return (tp->e_name);
482 }
483
484 const char *
485 linkaddr_string(const u_char *ep, const unsigned int len)
486 {
487 register u_int i, j;
488 register char *cp;
489 register struct enamemem *tp;
490
491 if (len == 6) /* XXX not totally correct... */
492 return etheraddr_string(ep);
493
494 tp = lookup_bytestring(ep, len);
495 if (tp->e_name)
496 return (tp->e_name);
497
498 tp->e_name = cp = (char *)malloc(len*3);
499 if (tp->e_name == NULL)
500 error("linkaddr_string: malloc");
501 if ((j = *ep >> 4) != 0)
502 *cp++ = hex[j];
503 *cp++ = hex[*ep++ & 0xf];
504 for (i = len-1; i > 0 ; --i) {
505 *cp++ = ':';
506 if ((j = *ep >> 4) != 0)
507 *cp++ = hex[j];
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 netmask = mask;
1118 if (fflag) {
1119 f_localnet = localnet;
1120 f_netmask = mask;
1121 }
1122 if (nflag)
1123 /*
1124 * Simplest way to suppress names.
1125 */
1126 return;
1127
1128 init_etherarray();
1129 init_servarray();
1130 init_eprotoarray();
1131 init_llcsaparray();
1132 init_protoidarray();
1133 init_ipxsaparray();
1134 }
1135
1136 const char *
1137 dnaddr_string(u_short dnaddr)
1138 {
1139 register struct hnamemem *tp;
1140
1141 for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
1142 tp = tp->nxt)
1143 if (tp->addr == dnaddr)
1144 return (tp->name);
1145
1146 tp->addr = dnaddr;
1147 tp->nxt = newhnamemem();
1148 if (nflag)
1149 tp->name = dnnum_string(dnaddr);
1150 else
1151 tp->name = dnname_string(dnaddr);
1152
1153 return(tp->name);
1154 }
1155
1156 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1157 struct hnamemem *
1158 newhnamemem(void)
1159 {
1160 register struct hnamemem *p;
1161 static struct hnamemem *ptr = NULL;
1162 static u_int num = 0;
1163
1164 if (num <= 0) {
1165 num = 64;
1166 ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1167 if (ptr == NULL)
1168 error("newhnamemem: calloc");
1169 }
1170 --num;
1171 p = ptr++;
1172 return (p);
1173 }
1174
1175 #ifdef INET6
1176 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1177 struct h6namemem *
1178 newh6namemem(void)
1179 {
1180 register struct h6namemem *p;
1181 static struct h6namemem *ptr = NULL;
1182 static u_int num = 0;
1183
1184 if (num <= 0) {
1185 num = 64;
1186 ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1187 if (ptr == NULL)
1188 error("newh6namemem: calloc");
1189 }
1190 --num;
1191 p = ptr++;
1192 return (p);
1193 }
1194 #endif /* INET6 */