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