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