]>
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.83 2001-09-17 21:57:50 fenner 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() */
180 memcpy(&addr
, ap
, sizeof(addr
));
181 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
182 for (; p
->nxt
; p
= p
->nxt
) {
187 p
->nxt
= newhnamemem();
190 * Only print names when:
191 * (1) -n was not given.
192 * (2) Address is foreign and -f was given. (If -f was not
193 * give, f_netmask and f_local are 0 and the test
195 * (3) -a was given or the host portion is not all ones
196 * nor all zeros (i.e. not a network or broadcast address)
199 (addr
& f_netmask
) == f_localnet
&&
201 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))) {
202 if (!setjmp(getname_env
)) {
203 (void)setsignal(SIGALRM
, nohostname
);
205 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
210 p
->name
= strdup(hp
->h_name
);
212 /* Remove domain qualifications */
213 dotp
= strchr(p
->name
, '.');
221 p
->name
= strdup(intoa(addr
));
227 * Return a name for the IP6 address pointed to by ap. This address
228 * is assumed to be in network byte order.
231 getname6(const u_char
*ap
)
233 register struct hostent
*hp
;
234 struct in6_addr addr
;
235 static struct h6namemem
*p
; /* static for longjmp() */
236 register const char *cp
;
237 char ntop_buf
[INET6_ADDRSTRLEN
];
239 memcpy(&addr
, ap
, sizeof(addr
));
240 p
= &h6nametable
[*(u_int16_t
*)&addr
.s6_addr
[14] & (HASHNAMESIZE
-1)];
241 for (; p
->nxt
; p
= p
->nxt
) {
242 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
246 p
->nxt
= newh6namemem();
249 * Only print names when:
250 * (1) -n was not given.
251 * (2) Address is foreign and -f was given. (If -f was not
252 * give, f_netmask and f_local are 0 and the test
254 * (3) -a was given or the host portion is not all ones
255 * nor all zeros (i.e. not a network or broadcast address)
260 (addr
& f_netmask
) == f_localnet
&&
262 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))
265 if (!setjmp(getname_env
)) {
266 (void)setsignal(SIGALRM
, nohostname
);
268 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET6
);
273 p
->name
= strdup(hp
->h_name
);
275 /* Remove domain qualifications */
276 dotp
= strchr(p
->name
, '.');
284 cp
= inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
));
285 p
->name
= strdup(cp
);
290 static char hex
[] = "0123456789abcdef";
293 /* Find the hash node that corresponds the ether address 'ep' */
295 static inline struct enamemem
*
296 lookup_emem(const u_char
*ep
)
298 register u_int i
, j
, k
;
301 k
= (ep
[0] << 8) | ep
[1];
302 j
= (ep
[2] << 8) | ep
[3];
303 i
= (ep
[4] << 8) | ep
[5];
305 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
307 if (tp
->e_addr0
== i
&&
316 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
317 if (tp
->e_nxt
== NULL
)
318 error("lookup_emem: calloc");
324 * Find the hash node that corresponds to the bytestring 'bs'
328 static inline struct enamemem
*
329 lookup_bytestring(register const u_char
*bs
, const unsigned int nlen
)
332 register u_int i
, j
, k
;
335 k
= (bs
[0] << 8) | bs
[1];
336 j
= (bs
[2] << 8) | bs
[3];
337 i
= (bs
[4] << 8) | bs
[5];
338 } else if (nlen
>= 4) {
339 k
= (bs
[0] << 8) | bs
[1];
340 j
= (bs
[2] << 8) | bs
[3];
345 tp
= &bytestringtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
347 if (tp
->e_addr0
== i
&&
350 memcmp((const char *)bs
, (const char *)(tp
->e_bs
), nlen
) == 0)
359 tp
->e_bs
= (u_char
*) calloc(1, nlen
+ 1);
360 memcpy(tp
->e_bs
, bs
, nlen
);
361 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
362 if (tp
->e_nxt
== NULL
)
363 error("lookup_bytestring: calloc");
368 /* Find the hash node that corresponds the NSAP 'nsap' */
370 static inline struct enamemem
*
371 lookup_nsap(register const u_char
*nsap
)
373 register u_int i
, j
, k
;
374 unsigned int nlen
= *nsap
;
376 const u_char
*ensap
= nsap
+ nlen
- 6;
379 k
= (ensap
[0] << 8) | ensap
[1];
380 j
= (ensap
[2] << 8) | ensap
[3];
381 i
= (ensap
[4] << 8) | ensap
[5];
386 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
388 if (tp
->e_addr0
== i
&&
391 tp
->e_nsap
[0] == nlen
&&
392 memcmp((const char *)&(nsap
[1]),
393 (char *)&(tp
->e_nsap
[1]), nlen
) == 0)
400 tp
->e_nsap
= (u_char
*)malloc(nlen
+ 1);
401 if (tp
->e_nsap
== NULL
)
402 error("lookup_nsap: malloc");
403 memcpy((char *)tp
->e_nsap
, (const char *)nsap
, nlen
+ 1);
404 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
405 if (tp
->e_nxt
== NULL
)
406 error("lookup_nsap: calloc");
411 /* Find the hash node that corresponds the protoid 'pi'. */
413 static inline struct protoidmem
*
414 lookup_protoid(const u_char
*pi
)
417 struct protoidmem
*tp
;
419 /* 5 octets won't be aligned */
420 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
421 j
= (pi
[3] << 8) + pi
[4];
422 /* XXX should be endian-insensitive, but do big-endian testing XXX */
424 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
426 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
432 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
433 if (tp
->p_nxt
== NULL
)
434 error("lookup_protoid: calloc");
440 etheraddr_string(register const u_char
*ep
)
444 register struct enamemem
*tp
;
445 char buf
[sizeof("00:00:00:00:00:00")];
447 tp
= lookup_emem(ep
);
450 #ifdef USE_ETHER_NTOHOST
453 if (ether_ntohost(buf
, (const struct ether_addr
*)ep
) == 0) {
454 tp
->e_name
= strdup(buf
);
460 if ((j
= *ep
>> 4) != 0)
462 *cp
++ = hex
[*ep
++ & 0xf];
463 for (i
= 5; (int)--i
>= 0;) {
465 if ((j
= *ep
>> 4) != 0)
467 *cp
++ = hex
[*ep
++ & 0xf];
470 tp
->e_name
= strdup(buf
);
475 linkaddr_string(const u_char
*ep
, const unsigned int len
)
479 register struct enamemem
*tp
;
481 if (len
== 6) /* XXX not totally correct... */
482 return etheraddr_string(ep
);
484 tp
= lookup_bytestring(ep
, len
);
488 tp
->e_name
= cp
= (char *)malloc(len
*3);
489 if (tp
->e_name
== NULL
)
490 error("linkaddr_string: malloc");
491 if ((j
= *ep
>> 4) != 0)
493 *cp
++ = hex
[*ep
++ & 0xf];
494 for (i
= len
-1; i
> 0 ; --i
) {
496 if ((j
= *ep
>> 4) != 0)
498 *cp
++ = hex
[*ep
++ & 0xf];
505 etherproto_string(u_short port
)
508 register struct hnamemem
*tp
;
509 register u_int32_t i
= port
;
510 char buf
[sizeof("0000")];
512 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
517 tp
->nxt
= newhnamemem();
521 *cp
++ = hex
[port
>> 12 & 0xf];
522 *cp
++ = hex
[port
>> 8 & 0xf];
523 *cp
++ = hex
[port
>> 4 & 0xf];
524 *cp
++ = hex
[port
& 0xf];
526 tp
->name
= strdup(buf
);
531 protoid_string(register const u_char
*pi
)
535 register struct protoidmem
*tp
;
536 char buf
[sizeof("00:00:00:00:00")];
538 tp
= lookup_protoid(pi
);
543 if ((j
= *pi
>> 4) != 0)
545 *cp
++ = hex
[*pi
++ & 0xf];
546 for (i
= 4; (int)--i
>= 0;) {
548 if ((j
= *pi
>> 4) != 0)
550 *cp
++ = hex
[*pi
++ & 0xf];
553 tp
->p_name
= strdup(buf
);
558 llcsap_string(u_char sap
)
560 register struct hnamemem
*tp
;
561 register u_int32_t i
= sap
;
562 char buf
[sizeof("sap 00")];
564 for (tp
= &llcsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
569 tp
->nxt
= newhnamemem();
571 snprintf(buf
, sizeof(buf
), "sap %02x", sap
& 0xff);
572 tp
->name
= strdup(buf
);
577 isonsap_string(const u_char
*nsap
)
579 register u_int i
, nlen
= nsap
[0];
581 register struct enamemem
*tp
;
583 tp
= lookup_nsap(nsap
);
587 tp
->e_name
= cp
= (char *)malloc(nlen
* 2 + 2);
589 error("isonsap_string: malloc");
593 for (i
= nlen
; (int)--i
>= 0;) {
594 *cp
++ = hex
[*nsap
>> 4];
595 *cp
++ = hex
[*nsap
++ & 0xf];
602 tcpport_string(u_short port
)
604 register struct hnamemem
*tp
;
605 register u_int32_t i
= port
;
606 char buf
[sizeof("00000")];
608 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
613 tp
->nxt
= newhnamemem();
615 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
616 tp
->name
= strdup(buf
);
621 udpport_string(register u_short port
)
623 register struct hnamemem
*tp
;
624 register u_int32_t i
= port
;
625 char buf
[sizeof("00000")];
627 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
632 tp
->nxt
= newhnamemem();
634 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
635 tp
->name
= strdup(buf
);
643 register struct hnamemem
*table
;
645 char buf
[sizeof("0000000000")];
647 while ((sv
= getservent()) != NULL
) {
648 int port
= ntohs(sv
->s_port
);
649 i
= port
& (HASHNAMESIZE
-1);
650 if (strcmp(sv
->s_proto
, "tcp") == 0)
651 table
= &tporttable
[i
];
652 else if (strcmp(sv
->s_proto
, "udp") == 0)
653 table
= &uporttable
[i
];
660 (void)snprintf(buf
, sizeof(buf
), "%d", port
);
661 table
->name
= strdup(buf
);
663 table
->name
= strdup(sv
->s_name
);
665 table
->nxt
= newhnamemem();
670 /*XXX from libbpfc.a */
671 extern struct eproto
{
677 init_eprotoarray(void)
680 register struct hnamemem
*table
;
682 for (i
= 0; eproto_db
[i
].s
; i
++) {
683 int j
= ntohs(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
684 table
= &eprototable
[j
];
687 table
->name
= eproto_db
[i
].s
;
688 table
->addr
= ntohs(eproto_db
[i
].p
);
689 table
->nxt
= newhnamemem();
693 static struct protoidlist
{
694 const u_char protoid
[5];
697 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
698 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
699 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
700 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
701 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
702 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
706 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
710 init_protoidarray(void)
713 register struct protoidmem
*tp
;
714 struct protoidlist
*pl
;
720 for (i
= 0; eproto_db
[i
].s
; i
++) {
721 u_short etype
= htons(eproto_db
[i
].p
);
723 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
724 tp
= lookup_protoid(protoid
);
725 tp
->p_name
= strdup(eproto_db
[i
].s
);
727 /* Hardwire some SNAP proto ID names */
728 for (pl
= protoidlist
; pl
->name
!= NULL
; ++pl
) {
729 tp
= lookup_protoid(pl
->protoid
);
730 /* Don't override existing name */
731 if (tp
->p_name
!= NULL
)
734 tp
->p_name
= pl
->name
;
738 static struct etherlist
{
739 const u_char addr
[6];
742 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
743 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
747 * Initialize the ethers hash table. We take two different approaches
748 * depending on whether or not the system provides the ethers name
749 * service. If it does, we just wire in a few names at startup,
750 * and etheraddr_string() fills in the table on demand. If it doesn't,
751 * then we suck in the entire /etc/ethers file at startup. The idea
752 * is that parsing the local file will be fast, but spinning through
753 * all the ethers entries via NIS & next_etherent might be very slow.
755 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
756 * since the pcap module already does name-to-address translation,
757 * it's already does most of the work for the ethernet address-to-name
758 * translation, so we just pcap_next_etherent as a convenience.
761 init_etherarray(void)
763 register struct etherlist
*el
;
764 register struct enamemem
*tp
;
765 #ifdef USE_ETHER_NTOHOST
768 register struct pcap_etherent
*ep
;
771 /* Suck in entire ethers file */
772 fp
= fopen(PCAP_ETHERS_FILE
, "r");
774 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
775 tp
= lookup_emem(ep
->addr
);
776 tp
->e_name
= strdup(ep
->name
);
782 /* Hardwire some ethernet names */
783 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
784 tp
= lookup_emem(el
->addr
);
785 /* Don't override existing name */
786 if (tp
->e_name
!= NULL
)
789 #ifdef USE_ETHER_NTOHOST
790 /* Use yp/nis version of name if available */
791 if (ether_ntohost(name
, (const struct ether_addr
*)el
->addr
) == 0) {
792 tp
->e_name
= strdup(name
);
796 tp
->e_name
= el
->name
;
800 static struct tok llcsap_db
[] = {
801 { LLCSAP_NULL
, "null" },
802 { LLCSAP_8021B_I
, "802.1b-gsap" },
803 { LLCSAP_8021B_G
, "802.1b-isap" },
804 { LLCSAP_IP
, "ip-sap" },
805 { LLCSAP_PROWAYNM
, "proway-nm" },
806 { LLCSAP_8021D
, "802.1d" },
807 { LLCSAP_RS511
, "eia-rs511" },
808 { LLCSAP_ISO8208
, "x.25/llc2" },
809 { LLCSAP_PROWAY
, "proway" },
810 { LLCSAP_SNAP
, "snap" },
811 { LLCSAP_IPX
, "IPX" },
812 { LLCSAP_NETBEUI
, "netbeui" },
813 { LLCSAP_ISONS
, "iso-clns" },
814 { LLCSAP_GLOBAL
, "global" },
819 init_llcsaparray(void)
822 register struct hnamemem
*table
;
824 for (i
= 0; llcsap_db
[i
].s
!= NULL
; i
++) {
825 table
= &llcsaptable
[llcsap_db
[i
].v
];
828 table
->name
= llcsap_db
[i
].s
;
829 table
->addr
= llcsap_db
[i
].v
;
830 table
->nxt
= newhnamemem();
835 * Initialize the address to name translation machinery. We map all
836 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
837 * to prevent blocking on the nameserver). localnet is the IP address
838 * of the local network. mask is its subnet mask.
841 init_addrtoname(u_int32_t localnet
, u_int32_t mask
)
845 f_localnet
= localnet
;
850 * Simplest way to suppress names.
862 dnaddr_string(u_short dnaddr
)
864 register struct hnamemem
*tp
;
866 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= 0;
868 if (tp
->addr
== dnaddr
)
872 tp
->nxt
= newhnamemem();
874 tp
->name
= dnnum_string(dnaddr
);
876 tp
->name
= dnname_string(dnaddr
);
881 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
885 register struct hnamemem
*p
;
886 static struct hnamemem
*ptr
= NULL
;
887 static u_int num
= 0;
891 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
893 error("newhnamemem: calloc");
901 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
905 register struct h6namemem
*p
;
906 static struct h6namemem
*ptr
= NULL
;
907 static u_int num
= 0;
911 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
913 error("newh6namemem: calloc");