]>
The Tcpdump Group git mirrors - tcpdump/blob - addrtoname.c
8d22ce071942ebdf41f5d6338673fa299df08e77
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.85 2001-11-15 08:06:37 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
66 * XXX there has to be error checks against strdup(3) failure
69 #define HASHNAMESIZE 4096
77 struct hnamemem hnametable
[HASHNAMESIZE
];
78 struct hnamemem tporttable
[HASHNAMESIZE
];
79 struct hnamemem uporttable
[HASHNAMESIZE
];
80 struct hnamemem eprototable
[HASHNAMESIZE
];
81 struct hnamemem dnaddrtable
[HASHNAMESIZE
];
82 struct hnamemem llcsaptable
[HASHNAMESIZE
];
88 struct h6namemem
*nxt
;
91 struct h6namemem h6nametable
[HASHNAMESIZE
];
99 u_char
*e_nsap
; /* used only for nsaptable[] */
100 #define e_bs e_nsap /* for bytestringtable */
101 struct enamemem
*e_nxt
;
104 struct enamemem enametable
[HASHNAMESIZE
];
105 struct enamemem nsaptable
[HASHNAMESIZE
];
106 struct enamemem bytestringtable
[HASHNAMESIZE
];
112 struct protoidmem
*p_nxt
;
115 struct protoidmem protoidtable
[HASHNAMESIZE
];
118 * A faster replacement for inet_ntoa().
121 intoa(u_int32_t addr
)
126 static char buf
[sizeof(".xxx.xxx.xxx.xxx")];
129 cp
= &buf
[sizeof buf
];
135 *--cp
= byte
% 10 + '0';
138 *--cp
= byte
% 10 + '0';
150 static u_int32_t f_netmask
;
151 static u_int32_t f_localnet
;
152 static u_int32_t netmask
;
155 * Return a name for the IP address pointed to by ap. This address
156 * is assumed to be in network byte order.
159 getname(const u_char
*ap
)
161 register struct hostent
*hp
;
163 static struct hnamemem
*p
; /* static for longjmp() */
165 memcpy(&addr
, ap
, sizeof(addr
));
166 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
167 for (; p
->nxt
; p
= p
->nxt
) {
172 p
->nxt
= newhnamemem();
175 * Only print names when:
176 * (1) -n was not given.
177 * (2) Address is foreign and -f was given. (If -f was not
178 * give, f_netmask and f_local are 0 and the test
180 * (3) -a was given or the host portion is not all ones
181 * nor all zeros (i.e. not a network or broadcast address)
184 (addr
& f_netmask
) == f_localnet
&&
186 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))) {
187 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
191 p
->name
= strdup(hp
->h_name
);
193 /* Remove domain qualifications */
194 dotp
= strchr(p
->name
, '.');
201 p
->name
= strdup(intoa(addr
));
207 * Return a name for the IP6 address pointed to by ap. This address
208 * is assumed to be in network byte order.
211 getname6(const u_char
*ap
)
213 register struct hostent
*hp
;
214 struct in6_addr addr
;
215 static struct h6namemem
*p
; /* static for longjmp() */
216 register const char *cp
;
217 char ntop_buf
[INET6_ADDRSTRLEN
];
219 memcpy(&addr
, ap
, sizeof(addr
));
220 p
= &h6nametable
[*(u_int16_t
*)&addr
.s6_addr
[14] & (HASHNAMESIZE
-1)];
221 for (; p
->nxt
; p
= p
->nxt
) {
222 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
226 p
->nxt
= newh6namemem();
229 * Only print names when:
230 * (1) -n was not given.
231 * (2) Address is foreign and -f was given. (If -f was not
232 * give, f_netmask and f_local are 0 and the test
234 * (3) -a was given or the host portion is not all ones
235 * nor all zeros (i.e. not a network or broadcast address)
240 (addr
& f_netmask
) == f_localnet
&&
242 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))
245 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET6
);
249 p
->name
= strdup(hp
->h_name
);
251 /* Remove domain qualifications */
252 dotp
= strchr(p
->name
, '.');
259 cp
= inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
));
260 p
->name
= strdup(cp
);
265 static char hex
[] = "0123456789abcdef";
268 /* Find the hash node that corresponds the ether address 'ep' */
270 static inline struct enamemem
*
271 lookup_emem(const u_char
*ep
)
273 register u_int i
, j
, k
;
276 k
= (ep
[0] << 8) | ep
[1];
277 j
= (ep
[2] << 8) | ep
[3];
278 i
= (ep
[4] << 8) | ep
[5];
280 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
282 if (tp
->e_addr0
== i
&&
291 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
292 if (tp
->e_nxt
== NULL
)
293 error("lookup_emem: calloc");
299 * Find the hash node that corresponds to the bytestring 'bs'
303 static inline struct enamemem
*
304 lookup_bytestring(register const u_char
*bs
, const unsigned int nlen
)
307 register u_int i
, j
, k
;
310 k
= (bs
[0] << 8) | bs
[1];
311 j
= (bs
[2] << 8) | bs
[3];
312 i
= (bs
[4] << 8) | bs
[5];
313 } else if (nlen
>= 4) {
314 k
= (bs
[0] << 8) | bs
[1];
315 j
= (bs
[2] << 8) | bs
[3];
320 tp
= &bytestringtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
322 if (tp
->e_addr0
== i
&&
325 memcmp((const char *)bs
, (const char *)(tp
->e_bs
), nlen
) == 0)
334 tp
->e_bs
= (u_char
*) calloc(1, nlen
+ 1);
335 memcpy(tp
->e_bs
, bs
, nlen
);
336 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
337 if (tp
->e_nxt
== NULL
)
338 error("lookup_bytestring: calloc");
343 /* Find the hash node that corresponds the NSAP 'nsap' */
345 static inline struct enamemem
*
346 lookup_nsap(register const u_char
*nsap
)
348 register u_int i
, j
, k
;
349 unsigned int nlen
= *nsap
;
351 const u_char
*ensap
= nsap
+ nlen
- 6;
354 k
= (ensap
[0] << 8) | ensap
[1];
355 j
= (ensap
[2] << 8) | ensap
[3];
356 i
= (ensap
[4] << 8) | ensap
[5];
361 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
363 if (tp
->e_addr0
== i
&&
366 tp
->e_nsap
[0] == nlen
&&
367 memcmp((const char *)&(nsap
[1]),
368 (char *)&(tp
->e_nsap
[1]), nlen
) == 0)
375 tp
->e_nsap
= (u_char
*)malloc(nlen
+ 1);
376 if (tp
->e_nsap
== NULL
)
377 error("lookup_nsap: malloc");
378 memcpy((char *)tp
->e_nsap
, (const char *)nsap
, nlen
+ 1);
379 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
380 if (tp
->e_nxt
== NULL
)
381 error("lookup_nsap: calloc");
386 /* Find the hash node that corresponds the protoid 'pi'. */
388 static inline struct protoidmem
*
389 lookup_protoid(const u_char
*pi
)
392 struct protoidmem
*tp
;
394 /* 5 octets won't be aligned */
395 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
396 j
= (pi
[3] << 8) + pi
[4];
397 /* XXX should be endian-insensitive, but do big-endian testing XXX */
399 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
401 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
407 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
408 if (tp
->p_nxt
== NULL
)
409 error("lookup_protoid: calloc");
415 etheraddr_string(register const u_char
*ep
)
419 register struct enamemem
*tp
;
420 char buf
[sizeof("00:00:00:00:00:00")];
422 tp
= lookup_emem(ep
);
425 #ifdef USE_ETHER_NTOHOST
428 if (ether_ntohost(buf
, (const struct ether_addr
*)ep
) == 0) {
429 tp
->e_name
= strdup(buf
);
435 if ((j
= *ep
>> 4) != 0)
437 *cp
++ = hex
[*ep
++ & 0xf];
438 for (i
= 5; (int)--i
>= 0;) {
440 if ((j
= *ep
>> 4) != 0)
442 *cp
++ = hex
[*ep
++ & 0xf];
445 tp
->e_name
= strdup(buf
);
450 linkaddr_string(const u_char
*ep
, const unsigned int len
)
454 register struct enamemem
*tp
;
456 if (len
== 6) /* XXX not totally correct... */
457 return etheraddr_string(ep
);
459 tp
= lookup_bytestring(ep
, len
);
463 tp
->e_name
= cp
= (char *)malloc(len
*3);
464 if (tp
->e_name
== NULL
)
465 error("linkaddr_string: malloc");
466 if ((j
= *ep
>> 4) != 0)
468 *cp
++ = hex
[*ep
++ & 0xf];
469 for (i
= len
-1; i
> 0 ; --i
) {
471 if ((j
= *ep
>> 4) != 0)
473 *cp
++ = hex
[*ep
++ & 0xf];
480 etherproto_string(u_short port
)
483 register struct hnamemem
*tp
;
484 register u_int32_t i
= port
;
485 char buf
[sizeof("0000")];
487 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
492 tp
->nxt
= newhnamemem();
496 *cp
++ = hex
[port
>> 12 & 0xf];
497 *cp
++ = hex
[port
>> 8 & 0xf];
498 *cp
++ = hex
[port
>> 4 & 0xf];
499 *cp
++ = hex
[port
& 0xf];
501 tp
->name
= strdup(buf
);
506 protoid_string(register const u_char
*pi
)
510 register struct protoidmem
*tp
;
511 char buf
[sizeof("00:00:00:00:00")];
513 tp
= lookup_protoid(pi
);
518 if ((j
= *pi
>> 4) != 0)
520 *cp
++ = hex
[*pi
++ & 0xf];
521 for (i
= 4; (int)--i
>= 0;) {
523 if ((j
= *pi
>> 4) != 0)
525 *cp
++ = hex
[*pi
++ & 0xf];
528 tp
->p_name
= strdup(buf
);
533 llcsap_string(u_char sap
)
535 register struct hnamemem
*tp
;
536 register u_int32_t i
= sap
;
537 char buf
[sizeof("sap 00")];
539 for (tp
= &llcsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
544 tp
->nxt
= newhnamemem();
546 snprintf(buf
, sizeof(buf
), "sap %02x", sap
& 0xff);
547 tp
->name
= strdup(buf
);
552 isonsap_string(const u_char
*nsap
)
554 register u_int i
, nlen
= nsap
[0];
556 register struct enamemem
*tp
;
558 tp
= lookup_nsap(nsap
);
562 tp
->e_name
= cp
= (char *)malloc(nlen
* 2 + 2);
564 error("isonsap_string: malloc");
568 for (i
= nlen
; (int)--i
>= 0;) {
569 *cp
++ = hex
[*nsap
>> 4];
570 *cp
++ = hex
[*nsap
++ & 0xf];
577 tcpport_string(u_short port
)
579 register struct hnamemem
*tp
;
580 register u_int32_t i
= port
;
581 char buf
[sizeof("00000")];
583 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
588 tp
->nxt
= newhnamemem();
590 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
591 tp
->name
= strdup(buf
);
596 udpport_string(register u_short port
)
598 register struct hnamemem
*tp
;
599 register u_int32_t i
= port
;
600 char buf
[sizeof("00000")];
602 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
607 tp
->nxt
= newhnamemem();
609 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
610 tp
->name
= strdup(buf
);
618 register struct hnamemem
*table
;
620 char buf
[sizeof("0000000000")];
622 while ((sv
= getservent()) != NULL
) {
623 int port
= ntohs(sv
->s_port
);
624 i
= port
& (HASHNAMESIZE
-1);
625 if (strcmp(sv
->s_proto
, "tcp") == 0)
626 table
= &tporttable
[i
];
627 else if (strcmp(sv
->s_proto
, "udp") == 0)
628 table
= &uporttable
[i
];
635 (void)snprintf(buf
, sizeof(buf
), "%d", port
);
636 table
->name
= strdup(buf
);
638 table
->name
= strdup(sv
->s_name
);
640 table
->nxt
= newhnamemem();
645 /*XXX from libbpfc.a */
646 extern struct eproto
{
652 init_eprotoarray(void)
655 register struct hnamemem
*table
;
657 for (i
= 0; eproto_db
[i
].s
; i
++) {
658 int j
= ntohs(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
659 table
= &eprototable
[j
];
662 table
->name
= eproto_db
[i
].s
;
663 table
->addr
= ntohs(eproto_db
[i
].p
);
664 table
->nxt
= newhnamemem();
668 static struct protoidlist
{
669 const u_char protoid
[5];
672 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
673 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
674 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
675 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
676 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
677 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
681 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
685 init_protoidarray(void)
688 register struct protoidmem
*tp
;
689 struct protoidlist
*pl
;
695 for (i
= 0; eproto_db
[i
].s
; i
++) {
696 u_short etype
= htons(eproto_db
[i
].p
);
698 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
699 tp
= lookup_protoid(protoid
);
700 tp
->p_name
= strdup(eproto_db
[i
].s
);
702 /* Hardwire some SNAP proto ID names */
703 for (pl
= protoidlist
; pl
->name
!= NULL
; ++pl
) {
704 tp
= lookup_protoid(pl
->protoid
);
705 /* Don't override existing name */
706 if (tp
->p_name
!= NULL
)
709 tp
->p_name
= pl
->name
;
713 static struct etherlist
{
714 const u_char addr
[6];
717 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
718 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
722 * Initialize the ethers hash table. We take two different approaches
723 * depending on whether or not the system provides the ethers name
724 * service. If it does, we just wire in a few names at startup,
725 * and etheraddr_string() fills in the table on demand. If it doesn't,
726 * then we suck in the entire /etc/ethers file at startup. The idea
727 * is that parsing the local file will be fast, but spinning through
728 * all the ethers entries via NIS & next_etherent might be very slow.
730 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
731 * since the pcap module already does name-to-address translation,
732 * it's already does most of the work for the ethernet address-to-name
733 * translation, so we just pcap_next_etherent as a convenience.
736 init_etherarray(void)
738 register struct etherlist
*el
;
739 register struct enamemem
*tp
;
740 #ifdef USE_ETHER_NTOHOST
743 register struct pcap_etherent
*ep
;
746 /* Suck in entire ethers file */
747 fp
= fopen(PCAP_ETHERS_FILE
, "r");
749 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
750 tp
= lookup_emem(ep
->addr
);
751 tp
->e_name
= strdup(ep
->name
);
757 /* Hardwire some ethernet names */
758 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
759 tp
= lookup_emem(el
->addr
);
760 /* Don't override existing name */
761 if (tp
->e_name
!= NULL
)
764 #ifdef USE_ETHER_NTOHOST
765 /* Use yp/nis version of name if available */
766 if (ether_ntohost(name
, (const struct ether_addr
*)el
->addr
) == 0) {
767 tp
->e_name
= strdup(name
);
771 tp
->e_name
= el
->name
;
775 static struct tok llcsap_db
[] = {
776 { LLCSAP_NULL
, "null" },
777 { LLCSAP_8021B_I
, "802.1b-gsap" },
778 { LLCSAP_8021B_G
, "802.1b-isap" },
779 { LLCSAP_IP
, "ip-sap" },
780 { LLCSAP_PROWAYNM
, "proway-nm" },
781 { LLCSAP_8021D
, "802.1d" },
782 { LLCSAP_RS511
, "eia-rs511" },
783 { LLCSAP_ISO8208
, "x.25/llc2" },
784 { LLCSAP_PROWAY
, "proway" },
785 { LLCSAP_SNAP
, "snap" },
786 { LLCSAP_IPX
, "IPX" },
787 { LLCSAP_NETBEUI
, "netbeui" },
788 { LLCSAP_ISONS
, "iso-clns" },
789 { LLCSAP_GLOBAL
, "global" },
794 init_llcsaparray(void)
797 register struct hnamemem
*table
;
799 for (i
= 0; llcsap_db
[i
].s
!= NULL
; i
++) {
800 table
= &llcsaptable
[llcsap_db
[i
].v
];
803 table
->name
= llcsap_db
[i
].s
;
804 table
->addr
= llcsap_db
[i
].v
;
805 table
->nxt
= newhnamemem();
810 * Initialize the address to name translation machinery. We map all
811 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
812 * to prevent blocking on the nameserver). localnet is the IP address
813 * of the local network. mask is its subnet mask.
816 init_addrtoname(u_int32_t localnet
, u_int32_t mask
)
820 f_localnet
= localnet
;
825 * Simplest way to suppress names.
837 dnaddr_string(u_short dnaddr
)
839 register struct hnamemem
*tp
;
841 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= 0;
843 if (tp
->addr
== dnaddr
)
847 tp
->nxt
= newhnamemem();
849 tp
->name
= dnnum_string(dnaddr
);
851 tp
->name
= dnname_string(dnaddr
);
856 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
860 register struct hnamemem
*p
;
861 static struct hnamemem
*ptr
= NULL
;
862 static u_int num
= 0;
866 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
868 error("newhnamemem: calloc");
876 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
880 register struct h6namemem
*p
;
881 static struct h6namemem
*ptr
= NULL
;
882 static u_int num
= 0;
886 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
888 error("newh6namemem: calloc");