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