]>
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.70 2001-01-17 18:27:36 guy 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"
62 #include "setsignal.h"
65 static RETSIGTYPE
nohostname(int);
68 * hash tables for whatever-to-name translations
71 #define HASHNAMESIZE 4096
79 struct hnamemem hnametable
[HASHNAMESIZE
];
80 struct hnamemem tporttable
[HASHNAMESIZE
];
81 struct hnamemem uporttable
[HASHNAMESIZE
];
82 struct hnamemem eprototable
[HASHNAMESIZE
];
83 struct hnamemem dnaddrtable
[HASHNAMESIZE
];
84 struct hnamemem llcsaptable
[HASHNAMESIZE
];
90 struct h6namemem
*nxt
;
93 struct h6namemem h6nametable
[HASHNAMESIZE
];
101 u_char
*e_nsap
; /* used only for nsaptable[] */
102 struct enamemem
*e_nxt
;
105 struct enamemem enametable
[HASHNAMESIZE
];
106 struct enamemem nsaptable
[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 * "getname" is written in this atrocious way to make sure we don't
156 * wait forever while trying to get hostnames from yp.
163 nohostname(int signo
)
165 longjmp(getname_env
, 1);
169 * Return a name for the IP address pointed to by ap. This address
170 * is assumed to be in network byte order.
173 getname(const u_char
*ap
)
175 register struct hostent
*hp
;
177 static struct hnamemem
*p
; /* static for longjmp() */
180 addr
= *(const u_int32_t
*)ap
;
182 memcpy(&addr
, ap
, sizeof(addr
));
184 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
185 for (; p
->nxt
; p
= p
->nxt
) {
190 p
->nxt
= newhnamemem();
193 * Only print names when:
194 * (1) -n was not given.
195 * (2) Address is foreign and -f was given. (If -f was not
196 * give, f_netmask and f_local are 0 and the test
198 * (3) -a was given or the host portion is not all ones
199 * nor all zeros (i.e. not a network or broadcast address)
202 (addr
& f_netmask
) == f_localnet
&&
204 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))) {
205 if (!setjmp(getname_env
)) {
206 (void)setsignal(SIGALRM
, nohostname
);
208 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
213 p
->name
= savestr(hp
->h_name
);
215 /* Remove domain qualifications */
216 dotp
= strchr(p
->name
, '.');
224 p
->name
= savestr(intoa(addr
));
230 * Return a name for the IP6 address pointed to by ap. This address
231 * is assumed to be in network byte order.
234 getname6(const u_char
*ap
)
236 register struct hostent
*hp
;
237 struct in6_addr addr
;
238 static struct h6namemem
*p
; /* static for longjmp() */
240 char ntop_buf
[INET6_ADDRSTRLEN
];
242 memcpy(&addr
, ap
, sizeof(addr
));
243 p
= &h6nametable
[*(u_int16_t
*)&addr
.s6_addr
[14] & (HASHNAMESIZE
-1)];
244 for (; p
->nxt
; p
= p
->nxt
) {
245 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
249 p
->nxt
= newh6namemem();
252 * Only print names when:
253 * (1) -n was not given.
254 * (2) Address is foreign and -f was given. (If -f was not
255 * give, f_netmask and f_local are 0 and the test
257 * (3) -a was given or the host portion is not all ones
258 * nor all zeros (i.e. not a network or broadcast address)
263 (addr
& f_netmask
) == f_localnet
&&
265 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))
268 if (!setjmp(getname_env
)) {
269 (void)setsignal(SIGALRM
, nohostname
);
271 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET6
);
276 p
->name
= savestr(hp
->h_name
);
278 /* Remove domain qualifications */
279 dotp
= strchr(p
->name
, '.');
287 cp
= (char *)inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
));
288 p
->name
= savestr(cp
);
293 static char hex
[] = "0123456789abcdef";
296 /* Find the hash node that corresponds the ether address 'ep' */
298 static inline struct enamemem
*
299 lookup_emem(const u_char
*ep
)
301 register u_int i
, j
, k
;
304 k
= (ep
[0] << 8) | ep
[1];
305 j
= (ep
[2] << 8) | ep
[3];
306 i
= (ep
[4] << 8) | ep
[5];
308 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
310 if (tp
->e_addr0
== i
&&
319 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
320 if (tp
->e_nxt
== NULL
)
321 error("lookup_emem: calloc");
326 /* Find the hash node that corresponds the NSAP 'nsap' */
328 static inline struct enamemem
*
329 lookup_nsap(register const u_char
*nsap
)
331 register u_int i
, j
, k
;
334 const u_char
*ensap
= nsap
+ nlen
- 6;
337 k
= (ensap
[0] << 8) | ensap
[1];
338 j
= (ensap
[2] << 8) | ensap
[3];
339 i
= (ensap
[4] << 8) | ensap
[5];
344 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
346 if (tp
->e_addr0
== i
&&
349 tp
->e_nsap
[0] == nlen
&&
350 memcmp((char *)&(nsap
[1]),
351 (char *)&(tp
->e_nsap
[1]), nlen
) == 0)
358 tp
->e_nsap
= (u_char
*)malloc(nlen
+ 1);
359 if (tp
->e_nsap
== NULL
)
360 error("lookup_nsap: malloc");
361 memcpy((char *)tp
->e_nsap
, (char *)nsap
, nlen
+ 1);
362 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
363 if (tp
->e_nxt
== NULL
)
364 error("lookup_nsap: calloc");
369 /* Find the hash node that corresponds the protoid 'pi'. */
371 static inline struct protoidmem
*
372 lookup_protoid(const u_char
*pi
)
375 struct protoidmem
*tp
;
377 /* 5 octets won't be aligned */
378 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
379 j
= (pi
[3] << 8) + pi
[4];
380 /* XXX should be endian-insensitive, but do big-endian testing XXX */
382 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
384 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
390 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
391 if (tp
->p_nxt
== NULL
)
392 error("lookup_protoid: calloc");
398 etheraddr_string(register const u_char
*ep
)
402 register struct enamemem
*tp
;
403 char buf
[sizeof("00:00:00:00:00:00")];
405 tp
= lookup_emem(ep
);
408 #ifdef HAVE_ETHER_NTOHOST
411 if (ether_ntohost(buf
, (struct ether_addr
*)ep
) == 0) {
412 tp
->e_name
= savestr(buf
);
418 if ((j
= *ep
>> 4) != 0)
420 *cp
++ = hex
[*ep
++ & 0xf];
421 for (i
= 5; (int)--i
>= 0;) {
423 if ((j
= *ep
>> 4) != 0)
425 *cp
++ = hex
[*ep
++ & 0xf];
428 tp
->e_name
= savestr(buf
);
433 etherproto_string(u_short port
)
436 register struct hnamemem
*tp
;
437 register u_int32_t i
= port
;
438 char buf
[sizeof("0000")];
440 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
445 tp
->nxt
= newhnamemem();
449 *cp
++ = hex
[port
>> 12 & 0xf];
450 *cp
++ = hex
[port
>> 8 & 0xf];
451 *cp
++ = hex
[port
>> 4 & 0xf];
452 *cp
++ = hex
[port
& 0xf];
454 tp
->name
= savestr(buf
);
459 protoid_string(register const u_char
*pi
)
463 register struct protoidmem
*tp
;
464 char buf
[sizeof("00:00:00:00:00")];
466 tp
= lookup_protoid(pi
);
471 if ((j
= *pi
>> 4) != 0)
473 *cp
++ = hex
[*pi
++ & 0xf];
474 for (i
= 4; (int)--i
>= 0;) {
476 if ((j
= *pi
>> 4) != 0)
478 *cp
++ = hex
[*pi
++ & 0xf];
481 tp
->p_name
= savestr(buf
);
486 llcsap_string(u_char sap
)
488 register struct hnamemem
*tp
;
489 register u_int32_t i
= sap
;
490 char buf
[sizeof("sap 00")];
492 for (tp
= &llcsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
497 tp
->nxt
= newhnamemem();
499 snprintf(buf
, sizeof(buf
), "sap %02x", sap
& 0xff);
500 tp
->name
= savestr(buf
);
505 isonsap_string(const u_char
*nsap
)
507 register u_int i
, nlen
= nsap
[0];
509 register struct enamemem
*tp
;
511 tp
= lookup_nsap(nsap
);
515 tp
->e_name
= cp
= (char *)malloc(nlen
* 2 + 2);
517 error("isonsap_string: malloc");
521 for (i
= nlen
; (int)--i
>= 0;) {
522 *cp
++ = hex
[*nsap
>> 4];
523 *cp
++ = hex
[*nsap
++ & 0xf];
530 tcpport_string(u_short port
)
532 register struct hnamemem
*tp
;
533 register u_int32_t i
= port
;
534 char buf
[sizeof("00000")];
536 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
541 tp
->nxt
= newhnamemem();
543 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
544 tp
->name
= savestr(buf
);
549 udpport_string(register u_short port
)
551 register struct hnamemem
*tp
;
552 register u_int32_t i
= port
;
553 char buf
[sizeof("00000")];
555 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
560 tp
->nxt
= newhnamemem();
562 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
563 tp
->name
= savestr(buf
);
571 register struct hnamemem
*table
;
573 char buf
[sizeof("0000000000")];
575 while ((sv
= getservent()) != NULL
) {
576 int port
= ntohs(sv
->s_port
);
577 i
= port
& (HASHNAMESIZE
-1);
578 if (strcmp(sv
->s_proto
, "tcp") == 0)
579 table
= &tporttable
[i
];
580 else if (strcmp(sv
->s_proto
, "udp") == 0)
581 table
= &uporttable
[i
];
588 (void)snprintf(buf
, sizeof(buf
), "%d", port
);
589 table
->name
= savestr(buf
);
591 table
->name
= savestr(sv
->s_name
);
593 table
->nxt
= newhnamemem();
598 /*XXX from libbpfc.a */
599 extern struct eproto
{
605 init_eprotoarray(void)
608 register struct hnamemem
*table
;
610 for (i
= 0; eproto_db
[i
].s
; i
++) {
611 int j
= ntohs(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
612 table
= &eprototable
[j
];
615 table
->name
= eproto_db
[i
].s
;
616 table
->addr
= ntohs(eproto_db
[i
].p
);
617 table
->nxt
= newhnamemem();
622 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
626 init_protoidarray(void)
629 register struct protoidmem
*tp
;
635 for (i
= 0; eproto_db
[i
].s
; i
++) {
636 u_short etype
= htons(eproto_db
[i
].p
);
638 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
639 tp
= lookup_protoid(protoid
);
640 tp
->p_name
= savestr(eproto_db
[i
].s
);
644 static struct etherlist
{
648 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
649 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
653 * Initialize the ethers hash table. We take two different approaches
654 * depending on whether or not the system provides the ethers name
655 * service. If it does, we just wire in a few names at startup,
656 * and etheraddr_string() fills in the table on demand. If it doesn't,
657 * then we suck in the entire /etc/ethers file at startup. The idea
658 * is that parsing the local file will be fast, but spinning through
659 * all the ethers entries via NIS & next_etherent might be very slow.
661 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
662 * since the pcap module already does name-to-address translation,
663 * it's already does most of the work for the ethernet address-to-name
664 * translation, so we just pcap_next_etherent as a convenience.
667 init_etherarray(void)
669 register struct etherlist
*el
;
670 register struct enamemem
*tp
;
671 #ifdef HAVE_ETHER_NTOHOST
674 register struct pcap_etherent
*ep
;
677 /* Suck in entire ethers file */
678 fp
= fopen(PCAP_ETHERS_FILE
, "r");
680 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
681 tp
= lookup_emem(ep
->addr
);
682 tp
->e_name
= savestr(ep
->name
);
688 /* Hardwire some ethernet names */
689 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
690 tp
= lookup_emem(el
->addr
);
691 /* Don't override existing name */
692 if (tp
->e_name
!= NULL
)
695 #ifdef HAVE_ETHER_NTOHOST
696 /* Use yp/nis version of name if available */
697 if (ether_ntohost(name
, (struct ether_addr
*)el
->addr
) == 0) {
698 tp
->e_name
= savestr(name
);
702 tp
->e_name
= el
->name
;
706 static struct tok llcsap_db
[] = {
707 { LLCSAP_NULL
, "null" },
708 { LLCSAP_8021B_I
, "802.1b-gsap" },
709 { LLCSAP_8021B_G
, "802.1b-isap" },
710 { LLCSAP_IP
, "ip-sap" },
711 { LLCSAP_PROWAYNM
, "proway-nm" },
712 { LLCSAP_8021D
, "802.1d" },
713 { LLCSAP_RS511
, "eia-rs511" },
714 { LLCSAP_ISO8208
, "x.25/llc2" },
715 { LLCSAP_PROWAY
, "proway" },
716 { LLCSAP_ISONS
, "iso-clns" },
717 { LLCSAP_GLOBAL
, "global" },
722 init_llcsaparray(void)
725 register struct hnamemem
*table
;
727 for (i
= 0; llcsap_db
[i
].s
!= NULL
; i
++) {
728 table
= &llcsaptable
[llcsap_db
[i
].v
];
731 table
->name
= llcsap_db
[i
].s
;
732 table
->addr
= llcsap_db
[i
].v
;
733 table
->nxt
= newhnamemem();
738 * Initialize the address to name translation machinery. We map all
739 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
740 * to prevent blocking on the nameserver). localnet is the IP address
741 * of the local network. mask is its subnet mask.
744 init_addrtoname(u_int32_t localnet
, u_int32_t mask
)
748 f_localnet
= localnet
;
753 * Simplest way to suppress names.
765 dnaddr_string(u_short dnaddr
)
767 register struct hnamemem
*tp
;
769 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= 0;
771 if (tp
->addr
== dnaddr
)
775 tp
->nxt
= newhnamemem();
777 tp
->name
= dnnum_string(dnaddr
);
779 tp
->name
= dnname_string(dnaddr
);
784 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
788 register struct hnamemem
*p
;
789 static struct hnamemem
*ptr
= NULL
;
790 static u_int num
= 0;
794 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
796 error("newhnamemem: calloc");
804 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
808 register struct h6namemem
*p
;
809 static struct h6namemem
*ptr
= NULL
;
810 static u_int num
= 0;
814 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
816 error("newh6namemem: calloc");