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