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