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