]>
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.62 1999-10-30 05:11:06 itojun Exp $ (LBL)";
29 #include <sys/types.h>
30 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
43 #include <netinet6/ip6.h>
46 #include <arpa/inet.h>
51 #include <pcap-namedb.h>
64 #include "interface.h"
65 #include "addrtoname.h"
68 #include "setsignal.h"
71 static RETSIGTYPE
nohostname(int);
74 * hash tables for whatever-to-name translations
77 #define HASHNAMESIZE 4096
85 struct hnamemem hnametable
[HASHNAMESIZE
];
86 struct hnamemem tporttable
[HASHNAMESIZE
];
87 struct hnamemem uporttable
[HASHNAMESIZE
];
88 struct hnamemem eprototable
[HASHNAMESIZE
];
89 struct hnamemem dnaddrtable
[HASHNAMESIZE
];
90 struct hnamemem llcsaptable
[HASHNAMESIZE
];
96 struct h6namemem
*nxt
;
99 struct h6namemem h6nametable
[HASHNAMESIZE
];
107 u_char
*e_nsap
; /* used only for nsaptable[] */
108 struct enamemem
*e_nxt
;
111 struct enamemem enametable
[HASHNAMESIZE
];
112 struct enamemem nsaptable
[HASHNAMESIZE
];
118 struct protoidmem
*p_nxt
;
121 struct protoidmem protoidtable
[HASHNAMESIZE
];
124 * A faster replacement for inet_ntoa().
127 intoa(u_int32_t addr
)
132 static char buf
[sizeof(".xxx.xxx.xxx.xxx")];
135 cp
= &buf
[sizeof buf
];
141 *--cp
= byte
% 10 + '0';
144 *--cp
= byte
% 10 + '0';
156 static u_int32_t f_netmask
;
157 static u_int32_t f_localnet
;
158 static u_int32_t netmask
;
161 * "getname" is written in this atrocious way to make sure we don't
162 * wait forever while trying to get hostnames from yp.
169 nohostname(int signo
)
171 longjmp(getname_env
, 1);
175 * Return a name for the IP address pointed to by ap. This address
176 * is assumed to be in network byte order.
179 getname(const u_char
*ap
)
181 register struct hostent
*hp
;
183 static struct hnamemem
*p
; /* static for longjmp() */
186 addr
= *(const u_int32_t
*)ap
;
188 memcpy(&addr
, ap
, sizeof(addr
));
190 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
191 for (; p
->nxt
; p
= p
->nxt
) {
196 p
->nxt
= newhnamemem();
199 * Only print names when:
200 * (1) -n was not given.
201 * (2) Address is foreign and -f was given. (If -f was not
202 * give, f_netmask and f_local are 0 and the test
204 * (3) -a was given or the host portion is not all ones
205 * nor all zeros (i.e. not a network or broadcast address)
208 (addr
& f_netmask
) == f_localnet
&&
210 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))) {
211 if (!setjmp(getname_env
)) {
212 (void)setsignal(SIGALRM
, nohostname
);
214 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
219 p
->name
= savestr(hp
->h_name
);
221 /* Remove domain qualifications */
222 dotp
= strchr(p
->name
, '.');
230 p
->name
= savestr(intoa(addr
));
236 * Return a name for the IP6 address pointed to by ap. This address
237 * is assumed to be in network byte order.
240 getname6(const u_char
*ap
)
242 register struct hostent
*hp
;
243 struct in6_addr addr
;
244 static struct h6namemem
*p
; /* static for longjmp() */
246 char ntop_buf
[INET6_ADDRSTRLEN
];
248 memcpy(&addr
, ap
, sizeof(addr
));
249 p
= &h6nametable
[*(u_int16_t
*)&addr
.s6_addr
[15] & (HASHNAMESIZE
-1)];
250 for (; p
->nxt
; p
= p
->nxt
) {
251 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
255 p
->nxt
= newh6namemem();
258 * Only print names when:
259 * (1) -n was not given.
260 * (2) Address is foreign and -f was given. (If -f was not
261 * give, f_netmask and f_local are 0 and the test
263 * (3) -a was given or the host portion is not all ones
264 * nor all zeros (i.e. not a network or broadcast address)
269 (addr
& f_netmask
) == f_localnet
&&
271 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))
274 if (!setjmp(getname_env
)) {
275 (void)setsignal(SIGALRM
, nohostname
);
277 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET6
);
282 p
->name
= savestr(hp
->h_name
);
284 /* Remove domain qualifications */
285 dotp
= strchr(p
->name
, '.');
293 cp
= (char *)inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
));
294 p
->name
= savestr(cp
);
299 static char hex
[] = "0123456789abcdef";
302 /* Find the hash node that corresponds the ether address 'ep' */
304 static inline struct enamemem
*
305 lookup_emem(const u_char
*ep
)
307 register u_int i
, j
, k
;
310 k
= (ep
[0] << 8) | ep
[1];
311 j
= (ep
[2] << 8) | ep
[3];
312 i
= (ep
[4] << 8) | ep
[5];
314 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
316 if (tp
->e_addr0
== i
&&
325 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
326 if (tp
->e_nxt
== NULL
)
327 error("lookup_emem: calloc");
332 /* Find the hash node that corresponds the NSAP 'nsap' */
334 static inline struct enamemem
*
335 lookup_nsap(register const u_char
*nsap
)
337 register u_int i
, j
, k
;
340 const u_char
*ensap
= nsap
+ nlen
- 6;
343 k
= (ensap
[0] << 8) | ensap
[1];
344 j
= (ensap
[2] << 8) | ensap
[3];
345 i
= (ensap
[4] << 8) | ensap
[5];
350 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
352 if (tp
->e_addr0
== i
&&
355 tp
->e_nsap
[0] == nlen
&&
356 memcmp((char *)&(nsap
[1]),
357 (char *)&(tp
->e_nsap
[1]), nlen
) == 0)
364 tp
->e_nsap
= (u_char
*)malloc(nlen
+ 1);
365 if (tp
->e_nsap
== NULL
)
366 error("lookup_nsap: malloc");
367 memcpy((char *)tp
->e_nsap
, (char *)nsap
, nlen
+ 1);
368 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
369 if (tp
->e_nxt
== NULL
)
370 error("lookup_nsap: calloc");
375 /* Find the hash node that corresponds the protoid 'pi'. */
377 static inline struct protoidmem
*
378 lookup_protoid(const u_char
*pi
)
381 struct protoidmem
*tp
;
383 /* 5 octets won't be aligned */
384 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
385 j
= (pi
[3] << 8) + pi
[4];
386 /* XXX should be endian-insensitive, but do big-endian testing XXX */
388 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
390 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
396 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
397 if (tp
->p_nxt
== NULL
)
398 error("lookup_protoid: calloc");
404 etheraddr_string(register const u_char
*ep
)
408 register struct enamemem
*tp
;
409 char buf
[sizeof("00:00:00:00:00:00")];
411 tp
= lookup_emem(ep
);
414 #ifdef HAVE_ETHER_NTOHOST
417 if (ether_ntohost(buf
, (struct ether_addr
*)ep
) == 0) {
418 tp
->e_name
= savestr(buf
);
424 if ((j
= *ep
>> 4) != 0)
426 *cp
++ = hex
[*ep
++ & 0xf];
427 for (i
= 5; (int)--i
>= 0;) {
429 if ((j
= *ep
>> 4) != 0)
431 *cp
++ = hex
[*ep
++ & 0xf];
434 tp
->e_name
= savestr(buf
);
439 etherproto_string(u_short port
)
442 register struct hnamemem
*tp
;
443 register u_int32_t i
= port
;
444 char buf
[sizeof("0000")];
446 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
451 tp
->nxt
= newhnamemem();
455 *cp
++ = hex
[port
>> 12 & 0xf];
456 *cp
++ = hex
[port
>> 8 & 0xf];
457 *cp
++ = hex
[port
>> 4 & 0xf];
458 *cp
++ = hex
[port
& 0xf];
460 tp
->name
= savestr(buf
);
465 protoid_string(register const u_char
*pi
)
469 register struct protoidmem
*tp
;
470 char buf
[sizeof("00:00:00:00:00")];
472 tp
= lookup_protoid(pi
);
477 if ((j
= *pi
>> 4) != 0)
479 *cp
++ = hex
[*pi
++ & 0xf];
480 for (i
= 4; (int)--i
>= 0;) {
482 if ((j
= *pi
>> 4) != 0)
484 *cp
++ = hex
[*pi
++ & 0xf];
487 tp
->p_name
= savestr(buf
);
492 llcsap_string(u_char sap
)
495 register struct hnamemem
*tp
;
496 register u_int32_t i
= sap
;
497 char buf
[sizeof("sap 00")];
499 for (tp
= &llcsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
504 tp
->nxt
= newhnamemem();
507 (void)strcpy(cp
, "sap ");
509 *cp
++ = hex
[sap
>> 4 & 0xf];
510 *cp
++ = hex
[sap
& 0xf];
512 tp
->name
= savestr(buf
);
517 isonsap_string(const u_char
*nsap
)
519 register u_int i
, nlen
= nsap
[0];
521 register struct enamemem
*tp
;
523 tp
= lookup_nsap(nsap
);
527 tp
->e_name
= cp
= (char *)malloc(nlen
* 2 + 2);
529 error("isonsap_string: malloc");
533 for (i
= nlen
; (int)--i
>= 0;) {
534 *cp
++ = hex
[*nsap
>> 4];
535 *cp
++ = hex
[*nsap
++ & 0xf];
542 tcpport_string(u_short port
)
544 register struct hnamemem
*tp
;
545 register u_int32_t i
= port
;
546 char buf
[sizeof("00000")];
548 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
553 tp
->nxt
= newhnamemem();
555 (void)sprintf(buf
, "%u", i
);
556 tp
->name
= savestr(buf
);
561 udpport_string(register u_short port
)
563 register struct hnamemem
*tp
;
564 register u_int32_t i
= port
;
565 char buf
[sizeof("00000")];
567 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
572 tp
->nxt
= newhnamemem();
574 (void)sprintf(buf
, "%u", i
);
575 tp
->name
= savestr(buf
);
583 register struct hnamemem
*table
;
585 char buf
[sizeof("0000000000")];
587 while ((sv
= getservent()) != NULL
) {
588 int port
= ntohs(sv
->s_port
);
589 i
= port
& (HASHNAMESIZE
-1);
590 if (strcmp(sv
->s_proto
, "tcp") == 0)
591 table
= &tporttable
[i
];
592 else if (strcmp(sv
->s_proto
, "udp") == 0)
593 table
= &uporttable
[i
];
600 (void)sprintf(buf
, "%d", port
);
601 table
->name
= savestr(buf
);
603 table
->name
= savestr(sv
->s_name
);
605 table
->nxt
= newhnamemem();
610 /*XXX from libbpfc.a */
611 extern struct eproto
{
617 init_eprotoarray(void)
620 register struct hnamemem
*table
;
622 for (i
= 0; eproto_db
[i
].s
; i
++) {
623 int j
= ntohs(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
624 table
= &eprototable
[j
];
627 table
->name
= eproto_db
[i
].s
;
628 table
->addr
= ntohs(eproto_db
[i
].p
);
629 table
->nxt
= newhnamemem();
634 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
638 init_protoidarray(void)
641 register struct protoidmem
*tp
;
647 for (i
= 0; eproto_db
[i
].s
; i
++) {
648 u_short etype
= htons(eproto_db
[i
].p
);
650 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
651 tp
= lookup_protoid(protoid
);
652 tp
->p_name
= savestr(eproto_db
[i
].s
);
656 static struct etherlist
{
660 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
661 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
665 * Initialize the ethers hash table. We take two different approaches
666 * depending on whether or not the system provides the ethers name
667 * service. If it does, we just wire in a few names at startup,
668 * and etheraddr_string() fills in the table on demand. If it doesn't,
669 * then we suck in the entire /etc/ethers file at startup. The idea
670 * is that parsing the local file will be fast, but spinning through
671 * all the ethers entries via NIS & next_etherent might be very slow.
673 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
674 * since the pcap module already does name-to-address translation,
675 * it's already does most of the work for the ethernet address-to-name
676 * translation, so we just pcap_next_etherent as a convenience.
679 init_etherarray(void)
681 register struct etherlist
*el
;
682 register struct enamemem
*tp
;
683 #ifdef HAVE_ETHER_NTOHOST
686 register struct pcap_etherent
*ep
;
689 /* Suck in entire ethers file */
690 fp
= fopen(PCAP_ETHERS_FILE
, "r");
692 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
693 tp
= lookup_emem(ep
->addr
);
694 tp
->e_name
= savestr(ep
->name
);
700 /* Hardwire some ethernet names */
701 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
702 tp
= lookup_emem(el
->addr
);
703 /* Don't override existing name */
704 if (tp
->e_name
!= NULL
)
707 #ifdef HAVE_ETHER_NTOHOST
708 /* Use yp/nis version of name if available */
709 if (ether_ntohost(name
, (struct ether_addr
*)el
->addr
) == 0) {
710 tp
->e_name
= savestr(name
);
714 tp
->e_name
= el
->name
;
718 static struct tok llcsap_db
[] = {
719 { LLCSAP_NULL
, "null" },
720 { LLCSAP_8021B_I
, "802.1b-gsap" },
721 { LLCSAP_8021B_G
, "802.1b-isap" },
722 { LLCSAP_IP
, "ip-sap" },
723 { LLCSAP_PROWAYNM
, "proway-nm" },
724 { LLCSAP_8021D
, "802.1d" },
725 { LLCSAP_RS511
, "eia-rs511" },
726 { LLCSAP_ISO8208
, "x.25/llc2" },
727 { LLCSAP_PROWAY
, "proway" },
728 { LLCSAP_ISONS
, "iso-clns" },
729 { LLCSAP_GLOBAL
, "global" },
734 init_llcsaparray(void)
737 register struct hnamemem
*table
;
739 for (i
= 0; llcsap_db
[i
].s
!= NULL
; i
++) {
740 table
= &llcsaptable
[llcsap_db
[i
].v
];
743 table
->name
= llcsap_db
[i
].s
;
744 table
->addr
= llcsap_db
[i
].v
;
745 table
->nxt
= newhnamemem();
750 * Initialize the address to name translation machinery. We map all
751 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
752 * to prevent blocking on the nameserver). localnet is the IP address
753 * of the local network. mask is its subnet mask.
756 init_addrtoname(u_int32_t localnet
, u_int32_t mask
)
760 f_localnet
= localnet
;
765 * Simplest way to suppress names.
777 dnaddr_string(u_short dnaddr
)
779 register struct hnamemem
*tp
;
781 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= 0;
783 if (tp
->addr
== dnaddr
)
787 tp
->nxt
= newhnamemem();
789 tp
->name
= dnnum_string(dnaddr
);
791 tp
->name
= dnname_string(dnaddr
);
796 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
800 register struct hnamemem
*p
;
801 static struct hnamemem
*ptr
= NULL
;
802 static u_int num
= 0;
806 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
808 error("newhnamemem: calloc");
816 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
820 register struct h6namemem
*p
;
821 static struct h6namemem
*ptr
= NULL
;
822 static u_int num
= 0;
826 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
828 error("newh6namemem: calloc");