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