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