]>
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.71 2001-01-20 07:22:21 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"
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 struct enamemem
*e_nxt
;
104 struct enamemem enametable
[HASHNAMESIZE
];
105 struct enamemem nsaptable
[HASHNAMESIZE
];
111 struct protoidmem
*p_nxt
;
114 struct protoidmem protoidtable
[HASHNAMESIZE
];
117 * A faster replacement for inet_ntoa().
120 intoa(u_int32_t addr
)
125 static char buf
[sizeof(".xxx.xxx.xxx.xxx")];
128 cp
= &buf
[sizeof buf
];
134 *--cp
= byte
% 10 + '0';
137 *--cp
= byte
% 10 + '0';
149 static u_int32_t f_netmask
;
150 static u_int32_t f_localnet
;
151 static u_int32_t netmask
;
154 * "getname" is written in this atrocious way to make sure we don't
155 * wait forever while trying to get hostnames from yp.
162 nohostname(int signo
)
164 longjmp(getname_env
, 1);
168 * Return a name for the IP address pointed to by ap. This address
169 * is assumed to be in network byte order.
172 getname(const u_char
*ap
)
174 register struct hostent
*hp
;
176 static struct hnamemem
*p
; /* static for longjmp() */
179 addr
= *(const u_int32_t
*)ap
;
181 memcpy(&addr
, ap
, sizeof(addr
));
183 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
184 for (; p
->nxt
; p
= p
->nxt
) {
189 p
->nxt
= newhnamemem();
192 * Only print names when:
193 * (1) -n was not given.
194 * (2) Address is foreign and -f was given. (If -f was not
195 * give, f_netmask and f_local are 0 and the test
197 * (3) -a was given or the host portion is not all ones
198 * nor all zeros (i.e. not a network or broadcast address)
201 (addr
& f_netmask
) == f_localnet
&&
203 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))) {
204 if (!setjmp(getname_env
)) {
205 (void)setsignal(SIGALRM
, nohostname
);
207 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
212 p
->name
= strdup(hp
->h_name
);
214 /* Remove domain qualifications */
215 dotp
= strchr(p
->name
, '.');
223 p
->name
= strdup(intoa(addr
));
229 * Return a name for the IP6 address pointed to by ap. This address
230 * is assumed to be in network byte order.
233 getname6(const u_char
*ap
)
235 register struct hostent
*hp
;
236 struct in6_addr addr
;
237 static struct h6namemem
*p
; /* static for longjmp() */
239 char ntop_buf
[INET6_ADDRSTRLEN
];
241 memcpy(&addr
, ap
, sizeof(addr
));
242 p
= &h6nametable
[*(u_int16_t
*)&addr
.s6_addr
[14] & (HASHNAMESIZE
-1)];
243 for (; p
->nxt
; p
= p
->nxt
) {
244 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
248 p
->nxt
= newh6namemem();
251 * Only print names when:
252 * (1) -n was not given.
253 * (2) Address is foreign and -f was given. (If -f was not
254 * give, f_netmask and f_local are 0 and the test
256 * (3) -a was given or the host portion is not all ones
257 * nor all zeros (i.e. not a network or broadcast address)
262 (addr
& f_netmask
) == f_localnet
&&
264 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))
267 if (!setjmp(getname_env
)) {
268 (void)setsignal(SIGALRM
, nohostname
);
270 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET6
);
275 p
->name
= strdup(hp
->h_name
);
277 /* Remove domain qualifications */
278 dotp
= strchr(p
->name
, '.');
286 cp
= (char *)inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
));
287 p
->name
= strdup(cp
);
292 static char hex
[] = "0123456789abcdef";
295 /* Find the hash node that corresponds the ether address 'ep' */
297 static inline struct enamemem
*
298 lookup_emem(const u_char
*ep
)
300 register u_int i
, j
, k
;
303 k
= (ep
[0] << 8) | ep
[1];
304 j
= (ep
[2] << 8) | ep
[3];
305 i
= (ep
[4] << 8) | ep
[5];
307 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
309 if (tp
->e_addr0
== i
&&
318 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
319 if (tp
->e_nxt
== NULL
)
320 error("lookup_emem: calloc");
325 /* Find the hash node that corresponds the NSAP 'nsap' */
327 static inline struct enamemem
*
328 lookup_nsap(register const u_char
*nsap
)
330 register u_int i
, j
, k
;
333 const u_char
*ensap
= nsap
+ nlen
- 6;
336 k
= (ensap
[0] << 8) | ensap
[1];
337 j
= (ensap
[2] << 8) | ensap
[3];
338 i
= (ensap
[4] << 8) | ensap
[5];
343 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
345 if (tp
->e_addr0
== i
&&
348 tp
->e_nsap
[0] == nlen
&&
349 memcmp((char *)&(nsap
[1]),
350 (char *)&(tp
->e_nsap
[1]), nlen
) == 0)
357 tp
->e_nsap
= (u_char
*)malloc(nlen
+ 1);
358 if (tp
->e_nsap
== NULL
)
359 error("lookup_nsap: malloc");
360 memcpy((char *)tp
->e_nsap
, (char *)nsap
, nlen
+ 1);
361 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
362 if (tp
->e_nxt
== NULL
)
363 error("lookup_nsap: calloc");
368 /* Find the hash node that corresponds the protoid 'pi'. */
370 static inline struct protoidmem
*
371 lookup_protoid(const u_char
*pi
)
374 struct protoidmem
*tp
;
376 /* 5 octets won't be aligned */
377 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
378 j
= (pi
[3] << 8) + pi
[4];
379 /* XXX should be endian-insensitive, but do big-endian testing XXX */
381 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
383 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
389 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
390 if (tp
->p_nxt
== NULL
)
391 error("lookup_protoid: calloc");
397 etheraddr_string(register const u_char
*ep
)
401 register struct enamemem
*tp
;
402 char buf
[sizeof("00:00:00:00:00:00")];
404 tp
= lookup_emem(ep
);
407 #ifdef HAVE_ETHER_NTOHOST
410 if (ether_ntohost(buf
, (struct ether_addr
*)ep
) == 0) {
411 tp
->e_name
= strdup(buf
);
417 if ((j
= *ep
>> 4) != 0)
419 *cp
++ = hex
[*ep
++ & 0xf];
420 for (i
= 5; (int)--i
>= 0;) {
422 if ((j
= *ep
>> 4) != 0)
424 *cp
++ = hex
[*ep
++ & 0xf];
427 tp
->e_name
= strdup(buf
);
432 etherproto_string(u_short port
)
435 register struct hnamemem
*tp
;
436 register u_int32_t i
= port
;
437 char buf
[sizeof("0000")];
439 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
444 tp
->nxt
= newhnamemem();
448 *cp
++ = hex
[port
>> 12 & 0xf];
449 *cp
++ = hex
[port
>> 8 & 0xf];
450 *cp
++ = hex
[port
>> 4 & 0xf];
451 *cp
++ = hex
[port
& 0xf];
453 tp
->name
= strdup(buf
);
458 protoid_string(register const u_char
*pi
)
462 register struct protoidmem
*tp
;
463 char buf
[sizeof("00:00:00:00:00")];
465 tp
= lookup_protoid(pi
);
470 if ((j
= *pi
>> 4) != 0)
472 *cp
++ = hex
[*pi
++ & 0xf];
473 for (i
= 4; (int)--i
>= 0;) {
475 if ((j
= *pi
>> 4) != 0)
477 *cp
++ = hex
[*pi
++ & 0xf];
480 tp
->p_name
= strdup(buf
);
485 llcsap_string(u_char sap
)
487 register struct hnamemem
*tp
;
488 register u_int32_t i
= sap
;
489 char buf
[sizeof("sap 00")];
491 for (tp
= &llcsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
496 tp
->nxt
= newhnamemem();
498 snprintf(buf
, sizeof(buf
), "sap %02x", sap
& 0xff);
499 tp
->name
= strdup(buf
);
504 isonsap_string(const u_char
*nsap
)
506 register u_int i
, nlen
= nsap
[0];
508 register struct enamemem
*tp
;
510 tp
= lookup_nsap(nsap
);
514 tp
->e_name
= cp
= (char *)malloc(nlen
* 2 + 2);
516 error("isonsap_string: malloc");
520 for (i
= nlen
; (int)--i
>= 0;) {
521 *cp
++ = hex
[*nsap
>> 4];
522 *cp
++ = hex
[*nsap
++ & 0xf];
529 tcpport_string(u_short port
)
531 register struct hnamemem
*tp
;
532 register u_int32_t i
= port
;
533 char buf
[sizeof("00000")];
535 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
540 tp
->nxt
= newhnamemem();
542 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
543 tp
->name
= strdup(buf
);
548 udpport_string(register u_short port
)
550 register struct hnamemem
*tp
;
551 register u_int32_t i
= port
;
552 char buf
[sizeof("00000")];
554 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
559 tp
->nxt
= newhnamemem();
561 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
562 tp
->name
= strdup(buf
);
570 register struct hnamemem
*table
;
572 char buf
[sizeof("0000000000")];
574 while ((sv
= getservent()) != NULL
) {
575 int port
= ntohs(sv
->s_port
);
576 i
= port
& (HASHNAMESIZE
-1);
577 if (strcmp(sv
->s_proto
, "tcp") == 0)
578 table
= &tporttable
[i
];
579 else if (strcmp(sv
->s_proto
, "udp") == 0)
580 table
= &uporttable
[i
];
587 (void)snprintf(buf
, sizeof(buf
), "%d", port
);
588 table
->name
= strdup(buf
);
590 table
->name
= strdup(sv
->s_name
);
592 table
->nxt
= newhnamemem();
597 /*XXX from libbpfc.a */
598 extern struct eproto
{
604 init_eprotoarray(void)
607 register struct hnamemem
*table
;
609 for (i
= 0; eproto_db
[i
].s
; i
++) {
610 int j
= ntohs(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
611 table
= &eprototable
[j
];
614 table
->name
= eproto_db
[i
].s
;
615 table
->addr
= ntohs(eproto_db
[i
].p
);
616 table
->nxt
= newhnamemem();
621 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
625 init_protoidarray(void)
628 register struct protoidmem
*tp
;
634 for (i
= 0; eproto_db
[i
].s
; i
++) {
635 u_short etype
= htons(eproto_db
[i
].p
);
637 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
638 tp
= lookup_protoid(protoid
);
639 tp
->p_name
= strdup(eproto_db
[i
].s
);
643 static struct etherlist
{
647 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
648 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
652 * Initialize the ethers hash table. We take two different approaches
653 * depending on whether or not the system provides the ethers name
654 * service. If it does, we just wire in a few names at startup,
655 * and etheraddr_string() fills in the table on demand. If it doesn't,
656 * then we suck in the entire /etc/ethers file at startup. The idea
657 * is that parsing the local file will be fast, but spinning through
658 * all the ethers entries via NIS & next_etherent might be very slow.
660 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
661 * since the pcap module already does name-to-address translation,
662 * it's already does most of the work for the ethernet address-to-name
663 * translation, so we just pcap_next_etherent as a convenience.
666 init_etherarray(void)
668 register struct etherlist
*el
;
669 register struct enamemem
*tp
;
670 #ifdef HAVE_ETHER_NTOHOST
673 register struct pcap_etherent
*ep
;
676 /* Suck in entire ethers file */
677 fp
= fopen(PCAP_ETHERS_FILE
, "r");
679 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
680 tp
= lookup_emem(ep
->addr
);
681 tp
->e_name
= strdup(ep
->name
);
687 /* Hardwire some ethernet names */
688 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
689 tp
= lookup_emem(el
->addr
);
690 /* Don't override existing name */
691 if (tp
->e_name
!= NULL
)
694 #ifdef HAVE_ETHER_NTOHOST
695 /* Use yp/nis version of name if available */
696 if (ether_ntohost(name
, (struct ether_addr
*)el
->addr
) == 0) {
697 tp
->e_name
= strdup(name
);
701 tp
->e_name
= el
->name
;
705 static struct tok llcsap_db
[] = {
706 { LLCSAP_NULL
, "null" },
707 { LLCSAP_8021B_I
, "802.1b-gsap" },
708 { LLCSAP_8021B_G
, "802.1b-isap" },
709 { LLCSAP_IP
, "ip-sap" },
710 { LLCSAP_PROWAYNM
, "proway-nm" },
711 { LLCSAP_8021D
, "802.1d" },
712 { LLCSAP_RS511
, "eia-rs511" },
713 { LLCSAP_ISO8208
, "x.25/llc2" },
714 { LLCSAP_PROWAY
, "proway" },
715 { LLCSAP_ISONS
, "iso-clns" },
716 { LLCSAP_GLOBAL
, "global" },
721 init_llcsaparray(void)
724 register struct hnamemem
*table
;
726 for (i
= 0; llcsap_db
[i
].s
!= NULL
; i
++) {
727 table
= &llcsaptable
[llcsap_db
[i
].v
];
730 table
->name
= llcsap_db
[i
].s
;
731 table
->addr
= llcsap_db
[i
].v
;
732 table
->nxt
= newhnamemem();
737 * Initialize the address to name translation machinery. We map all
738 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
739 * to prevent blocking on the nameserver). localnet is the IP address
740 * of the local network. mask is its subnet mask.
743 init_addrtoname(u_int32_t localnet
, u_int32_t mask
)
747 f_localnet
= localnet
;
752 * Simplest way to suppress names.
764 dnaddr_string(u_short dnaddr
)
766 register struct hnamemem
*tp
;
768 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= 0;
770 if (tp
->addr
== dnaddr
)
774 tp
->nxt
= newhnamemem();
776 tp
->name
= dnnum_string(dnaddr
);
778 tp
->name
= dnname_string(dnaddr
);
783 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
787 register struct hnamemem
*p
;
788 static struct hnamemem
*ptr
= NULL
;
789 static u_int num
= 0;
793 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
795 error("newhnamemem: calloc");
803 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
807 register struct h6namemem
*p
;
808 static struct h6namemem
*ptr
= NULL
;
809 static u_int num
= 0;
813 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
815 error("newh6namemem: calloc");