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