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