]> The Tcpdump Group git mirrors - tcpdump/blob - addrtoname.c
updates for 3.7.
[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.86 2001-11-25 01:48:46 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 struct mbuf;
38 struct rtentry;
39 #include <net/if.h>
40
41 #include <netinet/in.h>
42 #ifdef HAVE_NETINET_IF_ETHER_H
43 #include <netinet/if_ether.h>
44 #endif
45
46 #include <arpa/inet.h>
47
48 #include <ctype.h>
49 #include <netdb.h>
50 #include <pcap.h>
51 #include <pcap-namedb.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57
58 #include "interface.h"
59 #include "addrtoname.h"
60 #include "llc.h"
61 #include "setsignal.h"
62
63 /*
64 * hash tables for whatever-to-name translations
65 *
66 * XXX there has to be error checks against strdup(3) failure
67 */
68
69 #define HASHNAMESIZE 4096
70
71 struct hnamemem {
72 u_int32_t addr;
73 const char *name;
74 struct hnamemem *nxt;
75 };
76
77 struct hnamemem hnametable[HASHNAMESIZE];
78 struct hnamemem tporttable[HASHNAMESIZE];
79 struct hnamemem uporttable[HASHNAMESIZE];
80 struct hnamemem eprototable[HASHNAMESIZE];
81 struct hnamemem dnaddrtable[HASHNAMESIZE];
82 struct hnamemem llcsaptable[HASHNAMESIZE];
83 struct hnamemem ipxsaptable[HASHNAMESIZE];
84
85 #ifdef INET6
86 struct h6namemem {
87 struct in6_addr addr;
88 char *name;
89 struct h6namemem *nxt;
90 };
91
92 struct h6namemem h6nametable[HASHNAMESIZE];
93 #endif /* INET6 */
94
95 struct enamemem {
96 u_short e_addr0;
97 u_short e_addr1;
98 u_short e_addr2;
99 const char *e_name;
100 u_char *e_nsap; /* used only for nsaptable[] */
101 #define e_bs e_nsap /* for bytestringtable */
102 struct enamemem *e_nxt;
103 };
104
105 struct enamemem enametable[HASHNAMESIZE];
106 struct enamemem nsaptable[HASHNAMESIZE];
107 struct enamemem bytestringtable[HASHNAMESIZE];
108
109 struct protoidmem {
110 u_int32_t p_oui;
111 u_short p_proto;
112 const char *p_name;
113 struct protoidmem *p_nxt;
114 };
115
116 struct protoidmem protoidtable[HASHNAMESIZE];
117
118 /*
119 * A faster replacement for inet_ntoa().
120 */
121 const char *
122 intoa(u_int32_t addr)
123 {
124 register char *cp;
125 register u_int byte;
126 register int n;
127 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
128
129 NTOHL(addr);
130 cp = &buf[sizeof buf];
131 *--cp = '\0';
132
133 n = 4;
134 do {
135 byte = addr & 0xff;
136 *--cp = byte % 10 + '0';
137 byte /= 10;
138 if (byte > 0) {
139 *--cp = byte % 10 + '0';
140 byte /= 10;
141 if (byte > 0)
142 *--cp = byte + '0';
143 }
144 *--cp = '.';
145 addr >>= 8;
146 } while (--n > 0);
147
148 return cp + 1;
149 }
150
151 static u_int32_t f_netmask;
152 static u_int32_t f_localnet;
153 static u_int32_t netmask;
154
155 /*
156 * Return a name for the IP address pointed to by ap. This address
157 * is assumed to be in network byte order.
158 */
159 const char *
160 getname(const u_char *ap)
161 {
162 register struct hostent *hp;
163 u_int32_t addr;
164 static struct hnamemem *p; /* static for longjmp() */
165
166 memcpy(&addr, ap, sizeof(addr));
167 p = &hnametable[addr & (HASHNAMESIZE-1)];
168 for (; p->nxt; p = p->nxt) {
169 if (p->addr == addr)
170 return (p->name);
171 }
172 p->addr = addr;
173 p->nxt = newhnamemem();
174
175 /*
176 * Only print names when:
177 * (1) -n was not given.
178 * (2) Address is foreign and -f was given. (If -f was not
179 * give, f_netmask and f_local are 0 and the test
180 * evaluates to true)
181 * (3) -a was given or the host portion is not all ones
182 * nor all zeros (i.e. not a network or broadcast address)
183 */
184 if (!nflag &&
185 (addr & f_netmask) == f_localnet &&
186 (aflag ||
187 !((addr & ~netmask) == 0 || (addr | netmask) == 0xffffffff))) {
188 hp = gethostbyaddr((char *)&addr, 4, AF_INET);
189 if (hp) {
190 char *dotp;
191
192 p->name = strdup(hp->h_name);
193 if (Nflag) {
194 /* Remove domain qualifications */
195 dotp = strchr(p->name, '.');
196 if (dotp)
197 *dotp = '\0';
198 }
199 return (p->name);
200 }
201 }
202 p->name = strdup(intoa(addr));
203 return (p->name);
204 }
205
206 #ifdef INET6
207 /*
208 * Return a name for the IP6 address pointed to by ap. This address
209 * is assumed to be in network byte order.
210 */
211 const char *
212 getname6(const u_char *ap)
213 {
214 register struct hostent *hp;
215 struct in6_addr addr;
216 static struct h6namemem *p; /* static for longjmp() */
217 register const char *cp;
218 char ntop_buf[INET6_ADDRSTRLEN];
219
220 memcpy(&addr, ap, sizeof(addr));
221 p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
222 for (; p->nxt; p = p->nxt) {
223 if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
224 return (p->name);
225 }
226 p->addr = addr;
227 p->nxt = newh6namemem();
228
229 /*
230 * Only print names when:
231 * (1) -n was not given.
232 * (2) Address is foreign and -f was given. (If -f was not
233 * give, f_netmask and f_local are 0 and the test
234 * evaluates to true)
235 * (3) -a was given or the host portion is not all ones
236 * nor all zeros (i.e. not a network or broadcast address)
237 */
238 if (!nflag
239 #if 0
240 &&
241 (addr & f_netmask) == f_localnet &&
242 (aflag ||
243 !((addr & ~netmask) == 0 || (addr | netmask) == 0xffffffff))
244 #endif
245 ) {
246 hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
247 if (hp) {
248 char *dotp;
249
250 p->name = strdup(hp->h_name);
251 if (Nflag) {
252 /* Remove domain qualifications */
253 dotp = strchr(p->name, '.');
254 if (dotp)
255 *dotp = '\0';
256 }
257 return (p->name);
258 }
259 }
260 cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
261 p->name = strdup(cp);
262 return (p->name);
263 }
264 #endif /* INET6 */
265
266 static char hex[] = "0123456789abcdef";
267
268
269 /* Find the hash node that corresponds the ether address 'ep' */
270
271 static inline struct enamemem *
272 lookup_emem(const u_char *ep)
273 {
274 register u_int i, j, k;
275 struct enamemem *tp;
276
277 k = (ep[0] << 8) | ep[1];
278 j = (ep[2] << 8) | ep[3];
279 i = (ep[4] << 8) | ep[5];
280
281 tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
282 while (tp->e_nxt)
283 if (tp->e_addr0 == i &&
284 tp->e_addr1 == j &&
285 tp->e_addr2 == k)
286 return tp;
287 else
288 tp = tp->e_nxt;
289 tp->e_addr0 = i;
290 tp->e_addr1 = j;
291 tp->e_addr2 = k;
292 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
293 if (tp->e_nxt == NULL)
294 error("lookup_emem: calloc");
295
296 return tp;
297 }
298
299 /*
300 * Find the hash node that corresponds to the bytestring 'bs'
301 * with length 'nlen'
302 */
303
304 static inline struct enamemem *
305 lookup_bytestring(register const u_char *bs, const unsigned int nlen)
306 {
307 struct enamemem *tp;
308 register u_int i, j, k;
309
310 if (nlen >= 6) {
311 k = (bs[0] << 8) | bs[1];
312 j = (bs[2] << 8) | bs[3];
313 i = (bs[4] << 8) | bs[5];
314 } else if (nlen >= 4) {
315 k = (bs[0] << 8) | bs[1];
316 j = (bs[2] << 8) | bs[3];
317 i = 0;
318 } else
319 i = j = k = 0;
320
321 tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
322 while (tp->e_nxt)
323 if (tp->e_addr0 == i &&
324 tp->e_addr1 == j &&
325 tp->e_addr2 == k &&
326 memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
327 return tp;
328 else
329 tp = tp->e_nxt;
330
331 tp->e_addr0 = i;
332 tp->e_addr1 = j;
333 tp->e_addr2 = k;
334
335 tp->e_bs = (u_char *) calloc(1, nlen + 1);
336 memcpy(tp->e_bs, bs, nlen);
337 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
338 if (tp->e_nxt == NULL)
339 error("lookup_bytestring: calloc");
340
341 return tp;
342 }
343
344 /* Find the hash node that corresponds the NSAP 'nsap' */
345
346 static inline struct enamemem *
347 lookup_nsap(register const u_char *nsap)
348 {
349 register u_int i, j, k;
350 unsigned int nlen = *nsap;
351 struct enamemem *tp;
352 const u_char *ensap = nsap + nlen - 6;
353
354 if (nlen > 6) {
355 k = (ensap[0] << 8) | ensap[1];
356 j = (ensap[2] << 8) | ensap[3];
357 i = (ensap[4] << 8) | ensap[5];
358 }
359 else
360 i = j = k = 0;
361
362 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
363 while (tp->e_nxt)
364 if (tp->e_addr0 == i &&
365 tp->e_addr1 == j &&
366 tp->e_addr2 == k &&
367 tp->e_nsap[0] == nlen &&
368 memcmp((const char *)&(nsap[1]),
369 (char *)&(tp->e_nsap[1]), nlen) == 0)
370 return tp;
371 else
372 tp = tp->e_nxt;
373 tp->e_addr0 = i;
374 tp->e_addr1 = j;
375 tp->e_addr2 = k;
376 tp->e_nsap = (u_char *)malloc(nlen + 1);
377 if (tp->e_nsap == NULL)
378 error("lookup_nsap: malloc");
379 memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
380 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
381 if (tp->e_nxt == NULL)
382 error("lookup_nsap: calloc");
383
384 return tp;
385 }
386
387 /* Find the hash node that corresponds the protoid 'pi'. */
388
389 static inline struct protoidmem *
390 lookup_protoid(const u_char *pi)
391 {
392 register u_int i, j;
393 struct protoidmem *tp;
394
395 /* 5 octets won't be aligned */
396 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
397 j = (pi[3] << 8) + pi[4];
398 /* XXX should be endian-insensitive, but do big-endian testing XXX */
399
400 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
401 while (tp->p_nxt)
402 if (tp->p_oui == i && tp->p_proto == j)
403 return tp;
404 else
405 tp = tp->p_nxt;
406 tp->p_oui = i;
407 tp->p_proto = j;
408 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
409 if (tp->p_nxt == NULL)
410 error("lookup_protoid: calloc");
411
412 return tp;
413 }
414
415 const char *
416 etheraddr_string(register const u_char *ep)
417 {
418 register u_int i, j;
419 register char *cp;
420 register struct enamemem *tp;
421 char buf[sizeof("00:00:00:00:00:00")];
422
423 tp = lookup_emem(ep);
424 if (tp->e_name)
425 return (tp->e_name);
426 #ifdef USE_ETHER_NTOHOST
427 if (!nflag) {
428 char buf[128];
429 if (ether_ntohost(buf, (const struct ether_addr *)ep) == 0) {
430 tp->e_name = strdup(buf);
431 return (tp->e_name);
432 }
433 }
434 #endif
435 cp = buf;
436 if ((j = *ep >> 4) != 0)
437 *cp++ = hex[j];
438 *cp++ = hex[*ep++ & 0xf];
439 for (i = 5; (int)--i >= 0;) {
440 *cp++ = ':';
441 if ((j = *ep >> 4) != 0)
442 *cp++ = hex[j];
443 *cp++ = hex[*ep++ & 0xf];
444 }
445 *cp = '\0';
446 tp->e_name = strdup(buf);
447 return (tp->e_name);
448 }
449
450 const char *
451 linkaddr_string(const u_char *ep, const unsigned int len)
452 {
453 register u_int i, j;
454 register char *cp;
455 register struct enamemem *tp;
456
457 if (len == 6) /* XXX not totally correct... */
458 return etheraddr_string(ep);
459
460 tp = lookup_bytestring(ep, len);
461 if (tp->e_name)
462 return (tp->e_name);
463
464 tp->e_name = cp = (char *)malloc(len*3);
465 if (tp->e_name == NULL)
466 error("linkaddr_string: malloc");
467 if ((j = *ep >> 4) != 0)
468 *cp++ = hex[j];
469 *cp++ = hex[*ep++ & 0xf];
470 for (i = len-1; i > 0 ; --i) {
471 *cp++ = ':';
472 if ((j = *ep >> 4) != 0)
473 *cp++ = hex[j];
474 *cp++ = hex[*ep++ & 0xf];
475 }
476 *cp = '\0';
477 return (tp->e_name);
478 }
479
480 const char *
481 etherproto_string(u_short port)
482 {
483 register char *cp;
484 register struct hnamemem *tp;
485 register u_int32_t i = port;
486 char buf[sizeof("0000")];
487
488 for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
489 if (tp->addr == i)
490 return (tp->name);
491
492 tp->addr = i;
493 tp->nxt = newhnamemem();
494
495 cp = buf;
496 NTOHS(port);
497 *cp++ = hex[port >> 12 & 0xf];
498 *cp++ = hex[port >> 8 & 0xf];
499 *cp++ = hex[port >> 4 & 0xf];
500 *cp++ = hex[port & 0xf];
501 *cp++ = '\0';
502 tp->name = strdup(buf);
503 return (tp->name);
504 }
505
506 const char *
507 protoid_string(register const u_char *pi)
508 {
509 register u_int i, j;
510 register char *cp;
511 register struct protoidmem *tp;
512 char buf[sizeof("00:00:00:00:00")];
513
514 tp = lookup_protoid(pi);
515 if (tp->p_name)
516 return tp->p_name;
517
518 cp = buf;
519 if ((j = *pi >> 4) != 0)
520 *cp++ = hex[j];
521 *cp++ = hex[*pi++ & 0xf];
522 for (i = 4; (int)--i >= 0;) {
523 *cp++ = ':';
524 if ((j = *pi >> 4) != 0)
525 *cp++ = hex[j];
526 *cp++ = hex[*pi++ & 0xf];
527 }
528 *cp = '\0';
529 tp->p_name = strdup(buf);
530 return (tp->p_name);
531 }
532
533 const char *
534 llcsap_string(u_char sap)
535 {
536 register struct hnamemem *tp;
537 register u_int32_t i = sap;
538 char buf[sizeof("sap 00")];
539
540 for (tp = &llcsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
541 if (tp->addr == i)
542 return (tp->name);
543
544 tp->addr = i;
545 tp->nxt = newhnamemem();
546
547 snprintf(buf, sizeof(buf), "sap %02x", sap & 0xff);
548 tp->name = strdup(buf);
549 return (tp->name);
550 }
551
552 const char *
553 isonsap_string(const u_char *nsap)
554 {
555 register u_int i, nlen = nsap[0];
556 register char *cp;
557 register struct enamemem *tp;
558
559 tp = lookup_nsap(nsap);
560 if (tp->e_name)
561 return tp->e_name;
562
563 tp->e_name = cp = (char *)malloc(nlen * 2 + 2);
564 if (cp == NULL)
565 error("isonsap_string: malloc");
566
567 nsap++;
568 *cp++ = '/';
569 for (i = nlen; (int)--i >= 0;) {
570 *cp++ = hex[*nsap >> 4];
571 *cp++ = hex[*nsap++ & 0xf];
572 }
573 *cp = '\0';
574 return (tp->e_name);
575 }
576
577 const char *
578 tcpport_string(u_short port)
579 {
580 register struct hnamemem *tp;
581 register u_int32_t i = port;
582 char buf[sizeof("00000")];
583
584 for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
585 if (tp->addr == i)
586 return (tp->name);
587
588 tp->addr = i;
589 tp->nxt = newhnamemem();
590
591 (void)snprintf(buf, sizeof(buf), "%u", i);
592 tp->name = strdup(buf);
593 return (tp->name);
594 }
595
596 const char *
597 udpport_string(register u_short port)
598 {
599 register struct hnamemem *tp;
600 register u_int32_t i = port;
601 char buf[sizeof("00000")];
602
603 for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
604 if (tp->addr == i)
605 return (tp->name);
606
607 tp->addr = i;
608 tp->nxt = newhnamemem();
609
610 (void)snprintf(buf, sizeof(buf), "%u", i);
611 tp->name = strdup(buf);
612 return (tp->name);
613 }
614
615 const char *
616 ipxsap_string(u_short port)
617 {
618 register char *cp;
619 register struct hnamemem *tp;
620 register u_int32_t i = port;
621 char buf[sizeof("0000")];
622
623 for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
624 if (tp->addr == i)
625 return (tp->name);
626
627 tp->addr = i;
628 tp->nxt = newhnamemem();
629
630 cp = buf;
631 NTOHS(port);
632 *cp++ = hex[port >> 12 & 0xf];
633 *cp++ = hex[port >> 8 & 0xf];
634 *cp++ = hex[port >> 4 & 0xf];
635 *cp++ = hex[port & 0xf];
636 *cp++ = '\0';
637 tp->name = strdup(buf);
638 return (tp->name);
639 }
640
641 static void
642 init_servarray(void)
643 {
644 struct servent *sv;
645 register struct hnamemem *table;
646 register int i;
647 char buf[sizeof("0000000000")];
648
649 while ((sv = getservent()) != NULL) {
650 int port = ntohs(sv->s_port);
651 i = port & (HASHNAMESIZE-1);
652 if (strcmp(sv->s_proto, "tcp") == 0)
653 table = &tporttable[i];
654 else if (strcmp(sv->s_proto, "udp") == 0)
655 table = &uporttable[i];
656 else
657 continue;
658
659 while (table->name)
660 table = table->nxt;
661 if (nflag) {
662 (void)snprintf(buf, sizeof(buf), "%d", port);
663 table->name = strdup(buf);
664 } else
665 table->name = strdup(sv->s_name);
666 table->addr = port;
667 table->nxt = newhnamemem();
668 }
669 endservent();
670 }
671
672 /*XXX from libbpfc.a */
673 extern struct eproto {
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 */