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