]>
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.76 2001-06-18 09:12:27 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 static RETSIGTYPE
nohostname(int);
67 * hash tables for whatever-to-name translations
70 #define HASHNAMESIZE 4096
78 struct hnamemem hnametable
[HASHNAMESIZE
];
79 struct hnamemem tporttable
[HASHNAMESIZE
];
80 struct hnamemem uporttable
[HASHNAMESIZE
];
81 struct hnamemem eprototable
[HASHNAMESIZE
];
82 struct hnamemem dnaddrtable
[HASHNAMESIZE
];
83 struct hnamemem llcsaptable
[HASHNAMESIZE
];
89 struct h6namemem
*nxt
;
92 struct h6namemem h6nametable
[HASHNAMESIZE
];
100 u_char
*e_nsap
; /* used only for nsaptable[] */
101 #define e_bs e_nsap /* for bytestringtable */
102 struct enamemem
*e_nxt
;
105 struct enamemem enametable
[HASHNAMESIZE
];
106 struct enamemem nsaptable
[HASHNAMESIZE
];
107 struct enamemem bytestringtable
[HASHNAMESIZE
];
113 struct protoidmem
*p_nxt
;
116 struct protoidmem protoidtable
[HASHNAMESIZE
];
119 * A faster replacement for inet_ntoa().
122 intoa(u_int32_t addr
)
127 static char buf
[sizeof(".xxx.xxx.xxx.xxx")];
130 cp
= &buf
[sizeof buf
];
136 *--cp
= byte
% 10 + '0';
139 *--cp
= byte
% 10 + '0';
151 static u_int32_t f_netmask
;
152 static u_int32_t f_localnet
;
153 static u_int32_t netmask
;
156 * "getname" is written in this atrocious way to make sure we don't
157 * wait forever while trying to get hostnames from yp.
164 nohostname(int signo
)
166 longjmp(getname_env
, 1);
170 * Return a name for the IP address pointed to by ap. This address
171 * is assumed to be in network byte order.
174 getname(const u_char
*ap
)
176 register struct hostent
*hp
;
178 static struct hnamemem
*p
; /* static for longjmp() */
181 addr
= *(const u_int32_t
*)ap
;
183 memcpy(&addr
, ap
, sizeof(addr
));
185 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
186 for (; p
->nxt
; p
= p
->nxt
) {
191 p
->nxt
= newhnamemem();
194 * Only print names when:
195 * (1) -n was not given.
196 * (2) Address is foreign and -f was given. (If -f was not
197 * give, f_netmask and f_local are 0 and the test
199 * (3) -a was given or the host portion is not all ones
200 * nor all zeros (i.e. not a network or broadcast address)
203 (addr
& f_netmask
) == f_localnet
&&
205 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))) {
206 if (!setjmp(getname_env
)) {
207 (void)setsignal(SIGALRM
, nohostname
);
209 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
214 p
->name
= strdup(hp
->h_name
);
216 /* Remove domain qualifications */
217 dotp
= strchr(p
->name
, '.');
225 p
->name
= strdup(intoa(addr
));
231 * Return a name for the IP6 address pointed to by ap. This address
232 * is assumed to be in network byte order.
235 getname6(const u_char
*ap
)
237 register struct hostent
*hp
;
238 struct in6_addr addr
;
239 static struct h6namemem
*p
; /* static for longjmp() */
241 char ntop_buf
[INET6_ADDRSTRLEN
];
243 memcpy(&addr
, ap
, sizeof(addr
));
244 p
= &h6nametable
[*(u_int16_t
*)&addr
.s6_addr
[14] & (HASHNAMESIZE
-1)];
245 for (; p
->nxt
; p
= p
->nxt
) {
246 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
250 p
->nxt
= newh6namemem();
253 * Only print names when:
254 * (1) -n was not given.
255 * (2) Address is foreign and -f was given. (If -f was not
256 * give, f_netmask and f_local are 0 and the test
258 * (3) -a was given or the host portion is not all ones
259 * nor all zeros (i.e. not a network or broadcast address)
264 (addr
& f_netmask
) == f_localnet
&&
266 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))
269 if (!setjmp(getname_env
)) {
270 (void)setsignal(SIGALRM
, nohostname
);
272 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET6
);
277 p
->name
= strdup(hp
->h_name
);
279 /* Remove domain qualifications */
280 dotp
= strchr(p
->name
, '.');
288 cp
= (char *)inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
));
289 p
->name
= strdup(cp
);
294 static char hex
[] = "0123456789abcdef";
297 /* Find the hash node that corresponds the ether address 'ep' */
299 static inline struct enamemem
*
300 lookup_emem(const u_char
*ep
)
302 register u_int i
, j
, k
;
305 k
= (ep
[0] << 8) | ep
[1];
306 j
= (ep
[2] << 8) | ep
[3];
307 i
= (ep
[4] << 8) | ep
[5];
309 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
311 if (tp
->e_addr0
== i
&&
320 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
321 if (tp
->e_nxt
== NULL
)
322 error("lookup_emem: calloc");
328 * Find the hash node that corresponds to the bytestring 'bs'
332 static inline struct enamemem
*
333 lookup_bytestring(register const u_char
*bs
, const int nlen
)
336 register u_int i
, j
, k
;
339 k
= (bs
[0] << 8) | bs
[1];
340 j
= (bs
[2] << 8) | bs
[3];
341 i
= (bs
[4] << 8) | bs
[5];
342 } else if (nlen
>= 4) {
343 k
= (bs
[0] << 8) | bs
[1];
344 j
= (bs
[2] << 8) | bs
[3];
349 tp
= &bytestringtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
351 if (tp
->e_addr0
== i
&&
354 bcmp((char *)bs
, (char *)(tp
->e_bs
), nlen
) == 0)
363 tp
->e_bs
= (u_char
*) calloc(1, nlen
+ 1);
364 bcopy(bs
, tp
->e_bs
, nlen
);
365 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
366 if (tp
->e_nxt
== NULL
)
367 error("lookup_bytestring: calloc");
372 /* Find the hash node that corresponds the NSAP 'nsap' */
374 static inline struct enamemem
*
375 lookup_nsap(register const u_char
*nsap
)
377 register u_int i
, j
, k
;
380 const u_char
*ensap
= nsap
+ nlen
- 6;
383 k
= (ensap
[0] << 8) | ensap
[1];
384 j
= (ensap
[2] << 8) | ensap
[3];
385 i
= (ensap
[4] << 8) | ensap
[5];
390 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
392 if (tp
->e_addr0
== i
&&
395 tp
->e_nsap
[0] == nlen
&&
396 memcmp((char *)&(nsap
[1]),
397 (char *)&(tp
->e_nsap
[1]), nlen
) == 0)
404 tp
->e_nsap
= (u_char
*)malloc(nlen
+ 1);
405 if (tp
->e_nsap
== NULL
)
406 error("lookup_nsap: malloc");
407 memcpy((char *)tp
->e_nsap
, (char *)nsap
, nlen
+ 1);
408 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
409 if (tp
->e_nxt
== NULL
)
410 error("lookup_nsap: calloc");
415 /* Find the hash node that corresponds the protoid 'pi'. */
417 static inline struct protoidmem
*
418 lookup_protoid(const u_char
*pi
)
421 struct protoidmem
*tp
;
423 /* 5 octets won't be aligned */
424 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
425 j
= (pi
[3] << 8) + pi
[4];
426 /* XXX should be endian-insensitive, but do big-endian testing XXX */
428 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
430 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
436 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
437 if (tp
->p_nxt
== NULL
)
438 error("lookup_protoid: calloc");
444 etheraddr_string(register const u_char
*ep
)
448 register struct enamemem
*tp
;
449 char buf
[sizeof("00:00:00:00:00:00")];
451 tp
= lookup_emem(ep
);
454 #ifdef USE_ETHER_NTOHOST
457 if (ether_ntohost(buf
, (struct ether_addr
*)ep
) == 0) {
458 tp
->e_name
= strdup(buf
);
464 if ((j
= *ep
>> 4) != 0)
466 *cp
++ = hex
[*ep
++ & 0xf];
467 for (i
= 5; (int)--i
>= 0;) {
469 if ((j
= *ep
>> 4) != 0)
471 *cp
++ = hex
[*ep
++ & 0xf];
474 tp
->e_name
= strdup(buf
);
479 linkaddr_string(const u_char
*ep
, const int len
)
483 register struct enamemem
*tp
;
485 if (len
== 6) /* XXX not totally correct... */
486 return etheraddr_string(ep
);
488 tp
= lookup_bytestring(ep
, len
);
492 tp
->e_name
= cp
= (char *)malloc(len
*3);
493 if (tp
->e_name
== NULL
)
494 error("linkaddr_string: malloc");
495 if ((j
= *ep
>> 4) != 0)
497 *cp
++ = hex
[*ep
++ & 0xf];
498 for (i
= len
-1; i
> 0 ; --i
) {
500 if ((j
= *ep
>> 4) != 0)
502 *cp
++ = hex
[*ep
++ & 0xf];
509 etherproto_string(u_short port
)
512 register struct hnamemem
*tp
;
513 register u_int32_t i
= port
;
514 char buf
[sizeof("0000")];
516 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
521 tp
->nxt
= newhnamemem();
525 *cp
++ = hex
[port
>> 12 & 0xf];
526 *cp
++ = hex
[port
>> 8 & 0xf];
527 *cp
++ = hex
[port
>> 4 & 0xf];
528 *cp
++ = hex
[port
& 0xf];
530 tp
->name
= strdup(buf
);
535 protoid_string(register const u_char
*pi
)
539 register struct protoidmem
*tp
;
540 char buf
[sizeof("00:00:00:00:00")];
542 tp
= lookup_protoid(pi
);
547 if ((j
= *pi
>> 4) != 0)
549 *cp
++ = hex
[*pi
++ & 0xf];
550 for (i
= 4; (int)--i
>= 0;) {
552 if ((j
= *pi
>> 4) != 0)
554 *cp
++ = hex
[*pi
++ & 0xf];
557 tp
->p_name
= strdup(buf
);
562 llcsap_string(u_char sap
)
564 register struct hnamemem
*tp
;
565 register u_int32_t i
= sap
;
566 char buf
[sizeof("sap 00")];
568 for (tp
= &llcsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
573 tp
->nxt
= newhnamemem();
575 snprintf(buf
, sizeof(buf
), "sap %02x", sap
& 0xff);
576 tp
->name
= strdup(buf
);
581 isonsap_string(const u_char
*nsap
)
583 register u_int i
, nlen
= nsap
[0];
585 register struct enamemem
*tp
;
587 tp
= lookup_nsap(nsap
);
591 tp
->e_name
= cp
= (char *)malloc(nlen
* 2 + 2);
593 error("isonsap_string: malloc");
597 for (i
= nlen
; (int)--i
>= 0;) {
598 *cp
++ = hex
[*nsap
>> 4];
599 *cp
++ = hex
[*nsap
++ & 0xf];
606 tcpport_string(u_short port
)
608 register struct hnamemem
*tp
;
609 register u_int32_t i
= port
;
610 char buf
[sizeof("00000")];
612 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
617 tp
->nxt
= newhnamemem();
619 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
620 tp
->name
= strdup(buf
);
625 udpport_string(register u_short port
)
627 register struct hnamemem
*tp
;
628 register u_int32_t i
= port
;
629 char buf
[sizeof("00000")];
631 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
636 tp
->nxt
= newhnamemem();
638 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
639 tp
->name
= strdup(buf
);
647 register struct hnamemem
*table
;
649 char buf
[sizeof("0000000000")];
651 while ((sv
= getservent()) != NULL
) {
652 int port
= ntohs(sv
->s_port
);
653 i
= port
& (HASHNAMESIZE
-1);
654 if (strcmp(sv
->s_proto
, "tcp") == 0)
655 table
= &tporttable
[i
];
656 else if (strcmp(sv
->s_proto
, "udp") == 0)
657 table
= &uporttable
[i
];
664 (void)snprintf(buf
, sizeof(buf
), "%d", port
);
665 table
->name
= strdup(buf
);
667 table
->name
= strdup(sv
->s_name
);
669 table
->nxt
= newhnamemem();
674 /*XXX from libbpfc.a */
675 extern struct eproto
{
681 init_eprotoarray(void)
684 register struct hnamemem
*table
;
686 for (i
= 0; eproto_db
[i
].s
; i
++) {
687 int j
= ntohs(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
688 table
= &eprototable
[j
];
691 table
->name
= eproto_db
[i
].s
;
692 table
->addr
= ntohs(eproto_db
[i
].p
);
693 table
->nxt
= newhnamemem();
697 static struct protoidlist
{
701 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
702 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
703 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
704 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
705 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
706 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
710 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
714 init_protoidarray(void)
717 register struct protoidmem
*tp
;
718 struct protoidlist
*pl
;
724 for (i
= 0; eproto_db
[i
].s
; i
++) {
725 u_short etype
= htons(eproto_db
[i
].p
);
727 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
728 tp
= lookup_protoid(protoid
);
729 tp
->p_name
= strdup(eproto_db
[i
].s
);
731 /* Hardwire some SNAP proto ID names */
732 for (pl
= protoidlist
; pl
->name
!= NULL
; ++pl
) {
733 tp
= lookup_protoid(pl
->protoid
);
734 /* Don't override existing name */
735 if (tp
->p_name
!= NULL
)
738 tp
->p_name
= pl
->name
;
742 static struct etherlist
{
746 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
747 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
751 * Initialize the ethers hash table. We take two different approaches
752 * depending on whether or not the system provides the ethers name
753 * service. If it does, we just wire in a few names at startup,
754 * and etheraddr_string() fills in the table on demand. If it doesn't,
755 * then we suck in the entire /etc/ethers file at startup. The idea
756 * is that parsing the local file will be fast, but spinning through
757 * all the ethers entries via NIS & next_etherent might be very slow.
759 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
760 * since the pcap module already does name-to-address translation,
761 * it's already does most of the work for the ethernet address-to-name
762 * translation, so we just pcap_next_etherent as a convenience.
765 init_etherarray(void)
767 register struct etherlist
*el
;
768 register struct enamemem
*tp
;
769 #ifdef USE_ETHER_NTOHOST
772 register struct pcap_etherent
*ep
;
775 /* Suck in entire ethers file */
776 fp
= fopen(PCAP_ETHERS_FILE
, "r");
778 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
779 tp
= lookup_emem(ep
->addr
);
780 tp
->e_name
= strdup(ep
->name
);
786 /* Hardwire some ethernet names */
787 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
788 tp
= lookup_emem(el
->addr
);
789 /* Don't override existing name */
790 if (tp
->e_name
!= NULL
)
793 #ifdef USE_ETHER_NTOHOST
794 /* Use yp/nis version of name if available */
795 if (ether_ntohost(name
, (struct ether_addr
*)el
->addr
) == 0) {
796 tp
->e_name
= strdup(name
);
800 tp
->e_name
= el
->name
;
804 static struct tok llcsap_db
[] = {
805 { LLCSAP_NULL
, "null" },
806 { LLCSAP_8021B_I
, "802.1b-gsap" },
807 { LLCSAP_8021B_G
, "802.1b-isap" },
808 { LLCSAP_IP
, "ip-sap" },
809 { LLCSAP_PROWAYNM
, "proway-nm" },
810 { LLCSAP_8021D
, "802.1d" },
811 { LLCSAP_RS511
, "eia-rs511" },
812 { LLCSAP_ISO8208
, "x.25/llc2" },
813 { LLCSAP_PROWAY
, "proway" },
814 { LLCSAP_SNAP
, "snap" },
815 { LLCSAP_IPX
, "IPX" },
816 { LLCSAP_NETBEUI
, "netbeui" },
817 { LLCSAP_ISONS
, "iso-clns" },
818 { LLCSAP_GLOBAL
, "global" },
823 init_llcsaparray(void)
826 register struct hnamemem
*table
;
828 for (i
= 0; llcsap_db
[i
].s
!= NULL
; i
++) {
829 table
= &llcsaptable
[llcsap_db
[i
].v
];
832 table
->name
= llcsap_db
[i
].s
;
833 table
->addr
= llcsap_db
[i
].v
;
834 table
->nxt
= newhnamemem();
839 * Initialize the address to name translation machinery. We map all
840 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
841 * to prevent blocking on the nameserver). localnet is the IP address
842 * of the local network. mask is its subnet mask.
845 init_addrtoname(u_int32_t localnet
, u_int32_t mask
)
849 f_localnet
= localnet
;
854 * Simplest way to suppress names.
866 dnaddr_string(u_short dnaddr
)
868 register struct hnamemem
*tp
;
870 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= 0;
872 if (tp
->addr
== dnaddr
)
876 tp
->nxt
= newhnamemem();
878 tp
->name
= dnnum_string(dnaddr
);
880 tp
->name
= dnname_string(dnaddr
);
885 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
889 register struct hnamemem
*p
;
890 static struct hnamemem
*ptr
= NULL
;
891 static u_int num
= 0;
895 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
897 error("newhnamemem: calloc");
905 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
909 register struct h6namemem
*p
;
910 static struct h6namemem
*ptr
= NULL
;
911 static u_int num
= 0;
915 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
917 error("newh6namemem: calloc");