]>
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.69 2000-10-07 05:33:25 itojun Exp $ (LBL)";
33 #include <sys/types.h>
34 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
44 #include <arpa/inet.h>
49 #include <pcap-namedb.h>
56 #include "interface.h"
57 #include "addrtoname.h"
60 #include "setsignal.h"
63 static RETSIGTYPE
nohostname(int);
66 * hash tables for whatever-to-name translations
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 struct enamemem
*e_nxt
;
103 struct enamemem enametable
[HASHNAMESIZE
];
104 struct enamemem nsaptable
[HASHNAMESIZE
];
110 struct protoidmem
*p_nxt
;
113 struct protoidmem protoidtable
[HASHNAMESIZE
];
116 * A faster replacement for inet_ntoa().
119 intoa(u_int32_t addr
)
124 static char buf
[sizeof(".xxx.xxx.xxx.xxx")];
127 cp
= &buf
[sizeof buf
];
133 *--cp
= byte
% 10 + '0';
136 *--cp
= byte
% 10 + '0';
148 static u_int32_t f_netmask
;
149 static u_int32_t f_localnet
;
150 static u_int32_t netmask
;
153 * "getname" is written in this atrocious way to make sure we don't
154 * wait forever while trying to get hostnames from yp.
161 nohostname(int signo
)
163 longjmp(getname_env
, 1);
167 * Return a name for the IP address pointed to by ap. This address
168 * is assumed to be in network byte order.
171 getname(const u_char
*ap
)
173 register struct hostent
*hp
;
175 static struct hnamemem
*p
; /* static for longjmp() */
178 addr
= *(const u_int32_t
*)ap
;
180 memcpy(&addr
, ap
, sizeof(addr
));
182 p
= &hnametable
[addr
& (HASHNAMESIZE
-1)];
183 for (; p
->nxt
; p
= p
->nxt
) {
188 p
->nxt
= newhnamemem();
191 * Only print names when:
192 * (1) -n was not given.
193 * (2) Address is foreign and -f was given. (If -f was not
194 * give, f_netmask and f_local are 0 and the test
196 * (3) -a was given or the host portion is not all ones
197 * nor all zeros (i.e. not a network or broadcast address)
200 (addr
& f_netmask
) == f_localnet
&&
202 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))) {
203 if (!setjmp(getname_env
)) {
204 (void)setsignal(SIGALRM
, nohostname
);
206 hp
= gethostbyaddr((char *)&addr
, 4, AF_INET
);
211 p
->name
= savestr(hp
->h_name
);
213 /* Remove domain qualifications */
214 dotp
= strchr(p
->name
, '.');
222 p
->name
= savestr(intoa(addr
));
228 * Return a name for the IP6 address pointed to by ap. This address
229 * is assumed to be in network byte order.
232 getname6(const u_char
*ap
)
234 register struct hostent
*hp
;
235 struct in6_addr addr
;
236 static struct h6namemem
*p
; /* static for longjmp() */
238 char ntop_buf
[INET6_ADDRSTRLEN
];
240 memcpy(&addr
, ap
, sizeof(addr
));
241 p
= &h6nametable
[*(u_int16_t
*)&addr
.s6_addr
[14] & (HASHNAMESIZE
-1)];
242 for (; p
->nxt
; p
= p
->nxt
) {
243 if (memcmp(&p
->addr
, &addr
, sizeof(addr
)) == 0)
247 p
->nxt
= newh6namemem();
250 * Only print names when:
251 * (1) -n was not given.
252 * (2) Address is foreign and -f was given. (If -f was not
253 * give, f_netmask and f_local are 0 and the test
255 * (3) -a was given or the host portion is not all ones
256 * nor all zeros (i.e. not a network or broadcast address)
261 (addr
& f_netmask
) == f_localnet
&&
263 !((addr
& ~netmask
) == 0 || (addr
| netmask
) == 0xffffffff))
266 if (!setjmp(getname_env
)) {
267 (void)setsignal(SIGALRM
, nohostname
);
269 hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET6
);
274 p
->name
= savestr(hp
->h_name
);
276 /* Remove domain qualifications */
277 dotp
= strchr(p
->name
, '.');
285 cp
= (char *)inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
));
286 p
->name
= savestr(cp
);
291 static char hex
[] = "0123456789abcdef";
294 /* Find the hash node that corresponds the ether address 'ep' */
296 static inline struct enamemem
*
297 lookup_emem(const u_char
*ep
)
299 register u_int i
, j
, k
;
302 k
= (ep
[0] << 8) | ep
[1];
303 j
= (ep
[2] << 8) | ep
[3];
304 i
= (ep
[4] << 8) | ep
[5];
306 tp
= &enametable
[(i
^ j
) & (HASHNAMESIZE
-1)];
308 if (tp
->e_addr0
== i
&&
317 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
318 if (tp
->e_nxt
== NULL
)
319 error("lookup_emem: calloc");
324 /* Find the hash node that corresponds the NSAP 'nsap' */
326 static inline struct enamemem
*
327 lookup_nsap(register const u_char
*nsap
)
329 register u_int i
, j
, k
;
332 const u_char
*ensap
= nsap
+ nlen
- 6;
335 k
= (ensap
[0] << 8) | ensap
[1];
336 j
= (ensap
[2] << 8) | ensap
[3];
337 i
= (ensap
[4] << 8) | ensap
[5];
342 tp
= &nsaptable
[(i
^ j
) & (HASHNAMESIZE
-1)];
344 if (tp
->e_addr0
== i
&&
347 tp
->e_nsap
[0] == nlen
&&
348 memcmp((char *)&(nsap
[1]),
349 (char *)&(tp
->e_nsap
[1]), nlen
) == 0)
356 tp
->e_nsap
= (u_char
*)malloc(nlen
+ 1);
357 if (tp
->e_nsap
== NULL
)
358 error("lookup_nsap: malloc");
359 memcpy((char *)tp
->e_nsap
, (char *)nsap
, nlen
+ 1);
360 tp
->e_nxt
= (struct enamemem
*)calloc(1, sizeof(*tp
));
361 if (tp
->e_nxt
== NULL
)
362 error("lookup_nsap: calloc");
367 /* Find the hash node that corresponds the protoid 'pi'. */
369 static inline struct protoidmem
*
370 lookup_protoid(const u_char
*pi
)
373 struct protoidmem
*tp
;
375 /* 5 octets won't be aligned */
376 i
= (((pi
[0] << 8) + pi
[1]) << 8) + pi
[2];
377 j
= (pi
[3] << 8) + pi
[4];
378 /* XXX should be endian-insensitive, but do big-endian testing XXX */
380 tp
= &protoidtable
[(i
^ j
) & (HASHNAMESIZE
-1)];
382 if (tp
->p_oui
== i
&& tp
->p_proto
== j
)
388 tp
->p_nxt
= (struct protoidmem
*)calloc(1, sizeof(*tp
));
389 if (tp
->p_nxt
== NULL
)
390 error("lookup_protoid: calloc");
396 etheraddr_string(register const u_char
*ep
)
400 register struct enamemem
*tp
;
401 char buf
[sizeof("00:00:00:00:00:00")];
403 tp
= lookup_emem(ep
);
406 #ifdef HAVE_ETHER_NTOHOST
409 if (ether_ntohost(buf
, (struct ether_addr
*)ep
) == 0) {
410 tp
->e_name
= savestr(buf
);
416 if ((j
= *ep
>> 4) != 0)
418 *cp
++ = hex
[*ep
++ & 0xf];
419 for (i
= 5; (int)--i
>= 0;) {
421 if ((j
= *ep
>> 4) != 0)
423 *cp
++ = hex
[*ep
++ & 0xf];
426 tp
->e_name
= savestr(buf
);
431 etherproto_string(u_short port
)
434 register struct hnamemem
*tp
;
435 register u_int32_t i
= port
;
436 char buf
[sizeof("0000")];
438 for (tp
= &eprototable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
443 tp
->nxt
= newhnamemem();
447 *cp
++ = hex
[port
>> 12 & 0xf];
448 *cp
++ = hex
[port
>> 8 & 0xf];
449 *cp
++ = hex
[port
>> 4 & 0xf];
450 *cp
++ = hex
[port
& 0xf];
452 tp
->name
= savestr(buf
);
457 protoid_string(register const u_char
*pi
)
461 register struct protoidmem
*tp
;
462 char buf
[sizeof("00:00:00:00:00")];
464 tp
= lookup_protoid(pi
);
469 if ((j
= *pi
>> 4) != 0)
471 *cp
++ = hex
[*pi
++ & 0xf];
472 for (i
= 4; (int)--i
>= 0;) {
474 if ((j
= *pi
>> 4) != 0)
476 *cp
++ = hex
[*pi
++ & 0xf];
479 tp
->p_name
= savestr(buf
);
484 llcsap_string(u_char sap
)
486 register struct hnamemem
*tp
;
487 register u_int32_t i
= sap
;
488 char buf
[sizeof("sap 00")];
490 for (tp
= &llcsaptable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
495 tp
->nxt
= newhnamemem();
497 snprintf(buf
, sizeof(buf
), "sap %02x", sap
& 0xff);
498 tp
->name
= savestr(buf
);
503 isonsap_string(const u_char
*nsap
)
505 register u_int i
, nlen
= nsap
[0];
507 register struct enamemem
*tp
;
509 tp
= lookup_nsap(nsap
);
513 tp
->e_name
= cp
= (char *)malloc(nlen
* 2 + 2);
515 error("isonsap_string: malloc");
519 for (i
= nlen
; (int)--i
>= 0;) {
520 *cp
++ = hex
[*nsap
>> 4];
521 *cp
++ = hex
[*nsap
++ & 0xf];
528 tcpport_string(u_short port
)
530 register struct hnamemem
*tp
;
531 register u_int32_t i
= port
;
532 char buf
[sizeof("00000")];
534 for (tp
= &tporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
539 tp
->nxt
= newhnamemem();
541 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
542 tp
->name
= savestr(buf
);
547 udpport_string(register u_short port
)
549 register struct hnamemem
*tp
;
550 register u_int32_t i
= port
;
551 char buf
[sizeof("00000")];
553 for (tp
= &uporttable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
558 tp
->nxt
= newhnamemem();
560 (void)snprintf(buf
, sizeof(buf
), "%u", i
);
561 tp
->name
= savestr(buf
);
569 register struct hnamemem
*table
;
571 char buf
[sizeof("0000000000")];
573 while ((sv
= getservent()) != NULL
) {
574 int port
= ntohs(sv
->s_port
);
575 i
= port
& (HASHNAMESIZE
-1);
576 if (strcmp(sv
->s_proto
, "tcp") == 0)
577 table
= &tporttable
[i
];
578 else if (strcmp(sv
->s_proto
, "udp") == 0)
579 table
= &uporttable
[i
];
586 (void)snprintf(buf
, sizeof(buf
), "%d", port
);
587 table
->name
= savestr(buf
);
589 table
->name
= savestr(sv
->s_name
);
591 table
->nxt
= newhnamemem();
596 /*XXX from libbpfc.a */
597 extern struct eproto
{
603 init_eprotoarray(void)
606 register struct hnamemem
*table
;
608 for (i
= 0; eproto_db
[i
].s
; i
++) {
609 int j
= ntohs(eproto_db
[i
].p
) & (HASHNAMESIZE
-1);
610 table
= &eprototable
[j
];
613 table
->name
= eproto_db
[i
].s
;
614 table
->addr
= ntohs(eproto_db
[i
].p
);
615 table
->nxt
= newhnamemem();
620 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
624 init_protoidarray(void)
627 register struct protoidmem
*tp
;
633 for (i
= 0; eproto_db
[i
].s
; i
++) {
634 u_short etype
= htons(eproto_db
[i
].p
);
636 memcpy((char *)&protoid
[3], (char *)&etype
, 2);
637 tp
= lookup_protoid(protoid
);
638 tp
->p_name
= savestr(eproto_db
[i
].s
);
642 static struct etherlist
{
646 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
647 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL
}
651 * Initialize the ethers hash table. We take two different approaches
652 * depending on whether or not the system provides the ethers name
653 * service. If it does, we just wire in a few names at startup,
654 * and etheraddr_string() fills in the table on demand. If it doesn't,
655 * then we suck in the entire /etc/ethers file at startup. The idea
656 * is that parsing the local file will be fast, but spinning through
657 * all the ethers entries via NIS & next_etherent might be very slow.
659 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
660 * since the pcap module already does name-to-address translation,
661 * it's already does most of the work for the ethernet address-to-name
662 * translation, so we just pcap_next_etherent as a convenience.
665 init_etherarray(void)
667 register struct etherlist
*el
;
668 register struct enamemem
*tp
;
669 #ifdef HAVE_ETHER_NTOHOST
672 register struct pcap_etherent
*ep
;
675 /* Suck in entire ethers file */
676 fp
= fopen(PCAP_ETHERS_FILE
, "r");
678 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
679 tp
= lookup_emem(ep
->addr
);
680 tp
->e_name
= savestr(ep
->name
);
686 /* Hardwire some ethernet names */
687 for (el
= etherlist
; el
->name
!= NULL
; ++el
) {
688 tp
= lookup_emem(el
->addr
);
689 /* Don't override existing name */
690 if (tp
->e_name
!= NULL
)
693 #ifdef HAVE_ETHER_NTOHOST
694 /* Use yp/nis version of name if available */
695 if (ether_ntohost(name
, (struct ether_addr
*)el
->addr
) == 0) {
696 tp
->e_name
= savestr(name
);
700 tp
->e_name
= el
->name
;
704 static struct tok llcsap_db
[] = {
705 { LLCSAP_NULL
, "null" },
706 { LLCSAP_8021B_I
, "802.1b-gsap" },
707 { LLCSAP_8021B_G
, "802.1b-isap" },
708 { LLCSAP_IP
, "ip-sap" },
709 { LLCSAP_PROWAYNM
, "proway-nm" },
710 { LLCSAP_8021D
, "802.1d" },
711 { LLCSAP_RS511
, "eia-rs511" },
712 { LLCSAP_ISO8208
, "x.25/llc2" },
713 { LLCSAP_PROWAY
, "proway" },
714 { LLCSAP_ISONS
, "iso-clns" },
715 { LLCSAP_GLOBAL
, "global" },
720 init_llcsaparray(void)
723 register struct hnamemem
*table
;
725 for (i
= 0; llcsap_db
[i
].s
!= NULL
; i
++) {
726 table
= &llcsaptable
[llcsap_db
[i
].v
];
729 table
->name
= llcsap_db
[i
].s
;
730 table
->addr
= llcsap_db
[i
].v
;
731 table
->nxt
= newhnamemem();
736 * Initialize the address to name translation machinery. We map all
737 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
738 * to prevent blocking on the nameserver). localnet is the IP address
739 * of the local network. mask is its subnet mask.
742 init_addrtoname(u_int32_t localnet
, u_int32_t mask
)
746 f_localnet
= localnet
;
751 * Simplest way to suppress names.
763 dnaddr_string(u_short dnaddr
)
765 register struct hnamemem
*tp
;
767 for (tp
= &dnaddrtable
[dnaddr
& (HASHNAMESIZE
-1)]; tp
->nxt
!= 0;
769 if (tp
->addr
== dnaddr
)
773 tp
->nxt
= newhnamemem();
775 tp
->name
= dnnum_string(dnaddr
);
777 tp
->name
= dnname_string(dnaddr
);
782 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
786 register struct hnamemem
*p
;
787 static struct hnamemem
*ptr
= NULL
;
788 static u_int num
= 0;
792 ptr
= (struct hnamemem
*)calloc(num
, sizeof (*ptr
));
794 error("newhnamemem: calloc");
802 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
806 register struct h6namemem
*p
;
807 static struct h6namemem
*ptr
= NULL
;
808 static u_int num
= 0;
812 ptr
= (struct h6namemem
*)calloc(num
, sizeof (*ptr
));
814 error("newh6namemem: calloc");