]>
The Tcpdump Group git mirrors - tcpdump/blob - addrtoname.c
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * Internet, ethernet, port, and protocol string to address
22 * and address to string conversion routines
25 static const char rcsid
[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.84 2001-11-05 02:02:41 itojun Exp $ (LBL)";
33 #include <sys/types.h>
34 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #ifdef HAVE_NETINET_IF_ETHER_H
43 #include <netinet/if_ether.h>
46 #include <arpa/inet.h>
51 #include <pcap-namedb.h>
58 #include "interface.h"
59 #include "addrtoname.h"
61 #include "setsignal.h"
64 * hash tables for whatever-to-name translations
67 #define HASHNAMESIZE 4096
75 struct hnamemem hnametable
[HASHNAMESIZE
];
76 struct hnamemem tporttable
[HASHNAMESIZE
];
77 struct hnamemem uporttable
[HASHNAMESIZE
];
78 struct hnamemem eprototable
[HASHNAMESIZE
];
79 struct hnamemem dnaddrtable
[HASHNAMESIZE
];
80 struct hnamemem llcsaptable
[HASHNAMESIZE
];
86 struct h6namemem
*nxt
;
89 struct h6namemem h6nametable
[HASHNAMESIZE
];
97 u_char
*e_nsap
; /* used only for nsaptable[] */
98 #define e_bs e_nsap /* for bytestringtable */
99 struct enamemem
*e_nxt
;
102 struct enamemem enametable
[HASHNAMESIZE
];
103 struct enamemem nsaptable
[HASHNAMESIZE
];
104 struct enamemem bytestringtable
[HASHNAMESIZE
];
110 struct protoidmem
*p_nxt
;
113 struct protoidmem protoidtable
[HASHNAMESIZE
];
116 * A faster replacement for inet_ntoa().
119 intoa(u_int32_t addr
)
124 static char buf
[sizeof(".xxx.xxx.xxx.xxx")];
127 cp
= &buf
[sizeof buf
];
133 *--cp
= byte
% 10 + '0';
136 *--cp
= byte
% 10 + '0';
148 static u_int32_t f_netmask
;
149 static u_int32_t f_localnet
;
150 static u_int32_t netmask
;
153 * Return a name for the IP address pointed to by ap. This address
154 * is assumed to be in network byte order.
157 getname(const u_char
*ap
)
159 register struct hostent
*hp
;
161 static struct hnamemem
*p
; /* static for longjmp() */
163 memcpy(&addr
, ap
, sizeof(addr
));
164 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
165 for (; p
->nxt
; p
= p
->nxt
) {
170 p
->nxt
= newhnamemem();
173 * Only print names when:
174 * (1) -n was not given.
175 * (2) Address is foreign and -f was given. (If -f was not
176 * give, f_netmask and f_local are 0 and the test
178 * (3) -a was given or the host portion is not all ones
179 * nor all zeros (i.e. not a network or broadcast address)
182 (addr
& f_netmask
) == f_localnet
&&
184 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))) {
185 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
189 p
->name
= strdup(hp
->h_name
);
191 /* Remove domain qualifications */
192 dotp
= strchr(p
->name
, '.');
199 p
->name
= strdup(intoa(addr
));
205 * Return a name for the IP6 address pointed to by ap. This address
206 * is assumed to be in network byte order.
209 getname6(const u_char
*ap
)
211 register struct hostent
*hp
;
212 struct in6_addr addr
;
213 static struct h6namemem
*p
; /* static for longjmp() */
214 register const char *cp
;
215 char ntop_buf
[INET6_ADDRSTRLEN
];
217 memcpy(&addr
, ap
, sizeof(addr
));
218 p
= &h6nametable
[*(u_int16_t
*)&addr
.s6_addr
[14] & (HASHNAMESIZE
-1)];
219 for (; p
->nxt
; p
= p
->nxt
) {
220 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
224 p
->nxt
= newh6namemem();
227 * Only print names when:
228 * (1) -n was not given.
229 * (2) Address is foreign and -f was given. (If -f was not
230 * give, f_netmask and f_local are 0 and the test
232 * (3) -a was given or the host portion is not all ones
233 * nor all zeros (i.e. not a network or broadcast address)
238 (addr
& f_netmask
) == f_localnet
&&
240 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))
243 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET6
);
247 p
->name
= strdup(hp
->h_name
);
249 /* Remove domain qualifications */
250 dotp
= strchr(p
->name
, '.');
257 cp
= inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
));
258 p
->name
= strdup(cp
);
263 static char hex
[] = "0123456789abcdef";
266 /* Find the hash node that corresponds the ether address 'ep' */
268 static inline struct enamemem
*
269 lookup_emem(const u_char
*ep
)
271 register u_int i
, j
, k
;
274 k
= (ep
[0] << 8) | ep
[1];
275 j
= (ep
[2] << 8) | ep
[3];
276 i
= (ep
[4] << 8) | ep
[5];
278 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
280 if (tp
->e_addr0
== i
&&
289 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
290 if (tp
->e_nxt
== NULL
)
291 error("lookup_emem: calloc");
297 * Find the hash node that corresponds to the bytestring 'bs'
301 static inline struct enamemem
*
302 lookup_bytestring(register const u_char
*bs
, const unsigned int nlen
)
305 register u_int i
, j
, k
;
308 k
= (bs
[0] << 8) | bs
[1];
309 j
= (bs
[2] << 8) | bs
[3];
310 i
= (bs
[4] << 8) | bs
[5];
311 } else if (nlen
>= 4) {
312 k
= (bs
[0] << 8) | bs
[1];
313 j
= (bs
[2] << 8) | bs
[3];
318 tp
= &bytestringtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
320 if (tp
->e_addr0
== i
&&
323 memcmp((const char *)bs
, (const char *)(tp
->e_bs
), nlen
) == 0)
332 tp
->e_bs
= (u_char
*) calloc(1, nlen
+ 1);
333 memcpy(tp
->e_bs
, bs
, nlen
);
334 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
335 if (tp
->e_nxt
== NULL
)
336 error("lookup_bytestring: calloc");
341 /* Find the hash node that corresponds the NSAP 'nsap' */
343 static inline struct enamemem
*
344 lookup_nsap(register const u_char
*nsap
)
346 register u_int i
, j
, k
;
347 unsigned int nlen
= *nsap
;
349 const u_char
*ensap
= nsap
+ nlen
- 6;
352 k
= (ensap
[0] << 8) | ensap
[1];
353 j
= (ensap
[2] << 8) | ensap
[3];
354 i
= (ensap
[4] << 8) | ensap
[5];
359 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
361 if (tp
->e_addr0
== i
&&
364 tp
->e_nsap
[0] == nlen
&&
365 memcmp((const char *)&(nsap
[1]),
366 (char *)&(tp
->e_nsap
[1]), nlen
) == 0)
373 tp
->e_nsap
= (u_char
*)malloc(nlen
+ 1);
374 if (tp
->e_nsap
== NULL
)
375 error("lookup_nsap: malloc");
376 memcpy((char *)tp
->e_nsap
, (const char *)nsap
, nlen
+ 1);
377 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
378 if (tp
->e_nxt
== NULL
)
379 error("lookup_nsap: calloc");
384 /* Find the hash node that corresponds the protoid 'pi'. */
386 static inline struct protoidmem
*
387 lookup_protoid(const u_char
*pi
)
390 struct protoidmem
*tp
;
392 /* 5 octets won't be aligned */
393 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
394 j
= (pi
[3] << 8) + pi
[4];
395 /* XXX should be endian-insensitive, but do big-endian testing XXX */
397 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
399 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
405 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
406 if (tp
->p_nxt
== NULL
)
407 error("lookup_protoid: calloc");
413 etheraddr_string(register const u_char
*ep
)
417 register struct enamemem
*tp
;
418 char buf
[sizeof("00:00:00:00:00:00")];
420 tp
= lookup_emem(ep
);
423 #ifdef USE_ETHER_NTOHOST
426 if (ether_ntohost(buf
, (const struct ether_addr
*)ep
) == 0) {
427 tp
->e_name
= strdup(buf
);
433 if ((j
= *ep
>> 4) != 0)
435 *cp
++ = hex
[*ep
++ & 0xf];
436 for (i
= 5; (int)--i
>= 0;) {
438 if ((j
= *ep
>> 4) != 0)
440 *cp
++ = hex
[*ep
++ & 0xf];
443 tp
->e_name
= strdup(buf
);
448 linkaddr_string(const u_char
*ep
, const unsigned int len
)
452 register struct enamemem
*tp
;
454 if (len
== 6) /* XXX not totally correct... */
455 return etheraddr_string(ep
);
457 tp
= lookup_bytestring(ep
, len
);
461 tp
->e_name
= cp
= (char *)malloc(len
*3);
462 if (tp
->e_name
== NULL
)
463 error("linkaddr_string: malloc");
464 if ((j
= *ep
>> 4) != 0)
466 *cp
++ = hex
[*ep
++ & 0xf];
467 for (i
= len
-1; i
> 0 ; --i
) {
469 if ((j
= *ep
>> 4) != 0)
471 *cp
++ = hex
[*ep
++ & 0xf];
478 etherproto_string(u_short port
)
481 register struct hnamemem
*tp
;
482 register u_int32_t i
= port
;
483 char buf
[sizeof("0000")];
485 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
490 tp
->nxt
= newhnamemem();
494 *cp
++ = hex
[port
>> 12 & 0xf];
495 *cp
++ = hex
[port
>> 8 & 0xf];
496 *cp
++ = hex
[port
>> 4 & 0xf];
497 *cp
++ = hex
[port
& 0xf];
499 tp
->name
= strdup(buf
);
504 protoid_string(register const u_char
*pi
)
508 register struct protoidmem
*tp
;
509 char buf
[sizeof("00:00:00:00:00")];
511 tp
= lookup_protoid(pi
);
516 if ((j
= *pi
>> 4) != 0)
518 *cp
++ = hex
[*pi
++ & 0xf];
519 for (i
= 4; (int)--i
>= 0;) {
521 if ((j
= *pi
>> 4) != 0)
523 *cp
++ = hex
[*pi
++ & 0xf];
526 tp
->p_name
= strdup(buf
);
531 llcsap_string(u_char sap
)
533 register struct hnamemem
*tp
;
534 register u_int32_t i
= sap
;
535 char buf
[sizeof("sap 00")];
537 for (tp
= &llcsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
542 tp
->nxt
= newhnamemem();
544 snprintf(buf
, sizeof(buf
), "sap %02x", sap
& 0xff);
545 tp
->name
= strdup(buf
);
550 isonsap_string(const u_char
*nsap
)
552 register u_int i
, nlen
= nsap
[0];
554 register struct enamemem
*tp
;
556 tp
= lookup_nsap(nsap
);
560 tp
->e_name
= cp
= (char *)malloc(nlen
* 2 + 2);
562 error("isonsap_string: malloc");
566 for (i
= nlen
; (int)--i
>= 0;) {
567 *cp
++ = hex
[*nsap
>> 4];
568 *cp
++ = hex
[*nsap
++ & 0xf];
575 tcpport_string(u_short port
)
577 register struct hnamemem
*tp
;
578 register u_int32_t i
= port
;
579 char buf
[sizeof("00000")];
581 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
586 tp
->nxt
= newhnamemem();
588 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
589 tp
->name
= strdup(buf
);
594 udpport_string(register u_short port
)
596 register struct hnamemem
*tp
;
597 register u_int32_t i
= port
;
598 char buf
[sizeof("00000")];
600 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
605 tp
->nxt
= newhnamemem();
607 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
608 tp
->name
= strdup(buf
);
616 register struct hnamemem
*table
;
618 char buf
[sizeof("0000000000")];
620 while ((sv
= getservent()) != NULL
) {
621 int port
= ntohs(sv
->s_port
);
622 i
= port
& (HASHNAMESIZE
-1);
623 if (strcmp(sv
->s_proto
, "tcp") == 0)
624 table
= &tporttable
[i
];
625 else if (strcmp(sv
->s_proto
, "udp") == 0)
626 table
= &uporttable
[i
];
633 (void)snprintf(buf
, sizeof(buf
), "%d", port
);
634 table
->name
= strdup(buf
);
636 table
->name
= strdup(sv
->s_name
);
638 table
->nxt
= newhnamemem();
643 /*XXX from libbpfc.a */
644 extern struct eproto
{
650 init_eprotoarray(void)
653 register struct hnamemem
*table
;
655 for (i
= 0; eproto_db
[i
].s
; i
++) {
656 int j
= ntohs(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
657 table
= &eprototable
[j
];
660 table
->name
= eproto_db
[i
].s
;
661 table
->addr
= ntohs(eproto_db
[i
].p
);
662 table
->nxt
= newhnamemem();
666 static struct protoidlist
{
667 const u_char protoid
[5];
670 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
671 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
672 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
673 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
674 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
675 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
679 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
683 init_protoidarray(void)
686 register struct protoidmem
*tp
;
687 struct protoidlist
*pl
;
693 for (i
= 0; eproto_db
[i
].s
; i
++) {
694 u_short etype
= htons(eproto_db
[i
].p
);
696 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
697 tp
= lookup_protoid(protoid
);
698 tp
->p_name
= strdup(eproto_db
[i
].s
);
700 /* Hardwire some SNAP proto ID names */
701 for (pl
= protoidlist
; pl
->name
!= NULL
; ++pl
) {
702 tp
= lookup_protoid(pl
->protoid
);
703 /* Don't override existing name */
704 if (tp
->p_name
!= NULL
)
707 tp
->p_name
= pl
->name
;
711 static struct etherlist
{
712 const u_char addr
[6];
715 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
716 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
720 * Initialize the ethers hash table. We take two different approaches
721 * depending on whether or not the system provides the ethers name
722 * service. If it does, we just wire in a few names at startup,
723 * and etheraddr_string() fills in the table on demand. If it doesn't,
724 * then we suck in the entire /etc/ethers file at startup. The idea
725 * is that parsing the local file will be fast, but spinning through
726 * all the ethers entries via NIS & next_etherent might be very slow.
728 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
729 * since the pcap module already does name-to-address translation,
730 * it's already does most of the work for the ethernet address-to-name
731 * translation, so we just pcap_next_etherent as a convenience.
734 init_etherarray(void)
736 register struct etherlist
*el
;
737 register struct enamemem
*tp
;
738 #ifdef USE_ETHER_NTOHOST
741 register struct pcap_etherent
*ep
;
744 /* Suck in entire ethers file */
745 fp
= fopen(PCAP_ETHERS_FILE
, "r");
747 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
748 tp
= lookup_emem(ep
->addr
);
749 tp
->e_name
= strdup(ep
->name
);
755 /* Hardwire some ethernet names */
756 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
757 tp
= lookup_emem(el
->addr
);
758 /* Don't override existing name */
759 if (tp
->e_name
!= NULL
)
762 #ifdef USE_ETHER_NTOHOST
763 /* Use yp/nis version of name if available */
764 if (ether_ntohost(name
, (const struct ether_addr
*)el
->addr
) == 0) {
765 tp
->e_name
= strdup(name
);
769 tp
->e_name
= el
->name
;
773 static struct tok llcsap_db
[] = {
774 { LLCSAP_NULL
, "null" },
775 { LLCSAP_8021B_I
, "802.1b-gsap" },
776 { LLCSAP_8021B_G
, "802.1b-isap" },
777 { LLCSAP_IP
, "ip-sap" },
778 { LLCSAP_PROWAYNM
, "proway-nm" },
779 { LLCSAP_8021D
, "802.1d" },
780 { LLCSAP_RS511
, "eia-rs511" },
781 { LLCSAP_ISO8208
, "x.25/llc2" },
782 { LLCSAP_PROWAY
, "proway" },
783 { LLCSAP_SNAP
, "snap" },
784 { LLCSAP_IPX
, "IPX" },
785 { LLCSAP_NETBEUI
, "netbeui" },
786 { LLCSAP_ISONS
, "iso-clns" },
787 { LLCSAP_GLOBAL
, "global" },
792 init_llcsaparray(void)
795 register struct hnamemem
*table
;
797 for (i
= 0; llcsap_db
[i
].s
!= NULL
; i
++) {
798 table
= &llcsaptable
[llcsap_db
[i
].v
];
801 table
->name
= llcsap_db
[i
].s
;
802 table
->addr
= llcsap_db
[i
].v
;
803 table
->nxt
= newhnamemem();
808 * Initialize the address to name translation machinery. We map all
809 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
810 * to prevent blocking on the nameserver). localnet is the IP address
811 * of the local network. mask is its subnet mask.
814 init_addrtoname(u_int32_t localnet
, u_int32_t mask
)
818 f_localnet
= localnet
;
823 * Simplest way to suppress names.
835 dnaddr_string(u_short dnaddr
)
837 register struct hnamemem
*tp
;
839 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= 0;
841 if (tp
->addr
== dnaddr
)
845 tp
->nxt
= newhnamemem();
847 tp
->name
= dnnum_string(dnaddr
);
849 tp
->name
= dnname_string(dnaddr
);
854 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
858 register struct hnamemem
*p
;
859 static struct hnamemem
*ptr
= NULL
;
860 static u_int num
= 0;
864 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
866 error("newhnamemem: calloc");
874 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
878 register struct h6namemem
*p
;
879 static struct h6namemem
*ptr
= NULL
;
880 static u_int num
= 0;
884 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
886 error("newh6namemem: calloc");