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