]>
The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
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 * Name to id translation routines used by the scanner.
22 * These functions are not time critical.
30 #include <sys/types.h>
31 #include <netdnet/dnetdb.h>
40 * To quote the MSDN page for getaddrinfo() at
42 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
44 * "Support for getaddrinfo on Windows 2000 and older versions
45 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
46 * later. To execute an application that uses this function on earlier
47 * versions of Windows, then you need to include the Ws2tcpip.h and
48 * Wspiapi.h files. When the Wspiapi.h include file is added, the
49 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
50 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
51 * function is implemented in such a way that if the Ws2_32.dll or the
52 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
53 * Preview for Windows 2000) does not include getaddrinfo, then a
54 * version of getaddrinfo is implemented inline based on code in the
55 * Wspiapi.h header file. This inline code will be used on older Windows
56 * platforms that do not natively support the getaddrinfo function."
58 * We use getaddrinfo(), so we include Wspiapi.h here.
63 #include <sys/param.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
68 #include <netinet/in.h>
70 #ifdef HAVE_ETHER_HOSTTON
71 #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
73 * OK, just include <net/ethernet.h>.
75 #include <net/ethernet.h>
76 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
78 * OK, just include <netinet/ether.h>
80 #include <netinet/ether.h>
81 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
83 * OK, just include <sys/ethernet.h>
85 #include <sys/ethernet.h>
86 #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
88 * OK, just include <arpa/inet.h>
90 #include <arpa/inet.h>
91 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
93 * OK, include <netinet/if_ether.h>, after all the other stuff we
94 * need to include or define for its benefit.
96 #define NEED_NETINET_IF_ETHER_H
99 * We'll have to declare it ourselves.
100 * If <netinet/if_ether.h> defines struct ether_addr, include
101 * it. Otherwise, define it ourselves.
103 #ifdef HAVE_STRUCT_ETHER_ADDR
104 #define NEED_NETINET_IF_ETHER_H
105 #else /* HAVE_STRUCT_ETHER_ADDR */
107 unsigned char ether_addr_octet
[6];
109 #endif /* HAVE_STRUCT_ETHER_ADDR */
110 #endif /* what declares ether_hostton() */
112 #ifdef NEED_NETINET_IF_ETHER_H
113 #include <net/if.h> /* Needed on some platforms */
114 #include <netinet/in.h> /* Needed on some platforms */
115 #include <netinet/if_ether.h>
116 #endif /* NEED_NETINET_IF_ETHER_H */
118 #ifndef HAVE_DECL_ETHER_HOSTTON
120 * No header declares it, so declare it ourselves.
122 extern int ether_hostton(const char *, struct ether_addr
*);
123 #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
124 #endif /* HAVE_ETHER_HOSTTON */
126 #include <arpa/inet.h>
136 #include "pcap-int.h"
138 #include "diag-control.h"
141 #include <pcap/namedb.h>
142 #include "nametoaddr.h"
144 #ifdef HAVE_OS_PROTO_H
145 #include "os-proto.h"
149 #define NTOHL(x) (x) = ntohl(x)
150 #define NTOHS(x) (x) = ntohs(x)
154 * Convert host name to internet address.
155 * Return 0 upon failure.
156 * XXX - not thread-safe; don't use it inside libpcap.
159 pcap_nametoaddr(const char *name
)
162 static bpf_u_int32
*hlist
[2];
168 * gethostbyname() is deprecated on Windows, perhaps because
169 * it's not thread-safe, or because it doesn't support IPv6,
172 * We deprecate pcap_nametoaddr() on all platforms because
173 * it's not thread-safe; we supply it for backwards compatibility,
174 * so suppress the deprecation warning. We could, I guess,
175 * use getaddrinfo() and construct the array ourselves, but
176 * that's probably not worth the effort, as that wouldn't make
177 * this thread-safe - we can't change the API to require that
178 * our caller free the address array, so we still have to reuse
182 if ((hp
= gethostbyname(name
)) != NULL
) {
185 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
189 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
191 return (bpf_u_int32
**)hp
->h_addr_list
;
199 pcap_nametoaddrinfo(const char *name
)
201 struct addrinfo hints
, *res
;
204 memset(&hints
, 0, sizeof(hints
));
205 hints
.ai_family
= PF_UNSPEC
;
206 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
207 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
208 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
216 * Convert net name to internet address.
217 * Return 0 upon failure.
218 * XXX - not guaranteed to be thread-safe! See below for platforms
219 * on which it is thread-safe and on which it isn't.
222 pcap_nametonetaddr(const char *name
)
226 * There's no "getnetbyname()" on Windows.
228 * XXX - I guess we could use the BSD code to read
229 * C:\Windows\System32\drivers\etc/networks, assuming
230 * that's its home on all the versions of Windows
231 * we use, but that file probably just has the loopback
232 * network on 127/24 on 99 44/100% of Windows machines.
234 * (Heck, these days it probably just has that on 99 44/100%
235 * of *UN*X* machines.)
242 struct netent
*np
= NULL
;
243 #if defined(HAVE_LINUX_GETNETBYNAME_R)
245 * We have Linux's reentrant getnetbyname_r().
247 struct netent result_buf
;
248 char buf
[1024]; /* arbitrary size */
252 err
= getnetbyname_r(name
, &result_buf
, buf
, sizeof buf
, &np
,
256 * XXX - dynamically allocate the buffer, and make it
257 * bigger if we get ERANGE back?
261 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
263 * We have Solaris's and IRIX's reentrant getnetbyname_r().
265 struct netent result_buf
;
266 char buf
[1024]; /* arbitrary size */
268 np
= getnetbyname_r(name
, &result_buf
, buf
, (int)sizeof buf
);
269 #elif defined(HAVE_AIX_GETNETBYNAME_R)
271 * We have AIX's reentrant getnetbyname_r().
273 struct netent result_buf
;
274 struct netent_data net_data
;
276 if (getnetbyname_r(name
, &result_buf
, &net_data
) == -1)
282 * We don't have any getnetbyname_r(); either we have a
283 * getnetbyname() that uses thread-specific data, in which
284 * case we're thread-safe (sufficiently recent FreeBSD,
285 * sufficiently recent Darwin-based OS, sufficiently recent
286 * HP-UX, sufficiently recent Tru64 UNIX), or we have the
287 * traditional getnetbyname() (everything else, including
288 * current NetBSD and OpenBSD), in which case we're not
291 np
= getnetbyname(name
);
301 * Convert a port name to its port and protocol numbers.
302 * We assume only TCP or UDP.
303 * Return 0 upon failure.
306 pcap_nametoport(const char *name
, int *port
, int *proto
)
308 struct addrinfo hints
, *res
, *ai
;
310 struct sockaddr_in
*in4
;
312 struct sockaddr_in6
*in6
;
318 * We check for both TCP and UDP in case there are
321 memset(&hints
, 0, sizeof(hints
));
322 hints
.ai_family
= PF_UNSPEC
;
323 hints
.ai_socktype
= SOCK_STREAM
;
324 hints
.ai_protocol
= IPPROTO_TCP
;
325 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
327 if (error
!= EAI_NONAME
&&
328 error
!= EAI_SERVICE
) {
330 * This is a real error, not just "there's
331 * no such service name".
332 * XXX - this doesn't return an error string.
338 * OK, we found it. Did it find anything?
340 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
342 * Does it have an address?
344 if (ai
->ai_addr
!= NULL
) {
346 * Yes. Get a port number; we're done.
348 if (ai
->ai_addr
->sa_family
== AF_INET
) {
349 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
350 tcp_port
= ntohs(in4
->sin_port
);
354 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
355 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
356 tcp_port
= ntohs(in6
->sin6_port
);
365 memset(&hints
, 0, sizeof(hints
));
366 hints
.ai_family
= PF_UNSPEC
;
367 hints
.ai_socktype
= SOCK_DGRAM
;
368 hints
.ai_protocol
= IPPROTO_UDP
;
369 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
371 if (error
!= EAI_NONAME
&&
372 error
!= EAI_SERVICE
) {
374 * This is a real error, not just "there's
375 * no such service name".
376 * XXX - this doesn't return an error string.
382 * OK, we found it. Did it find anything?
384 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
386 * Does it have an address?
388 if (ai
->ai_addr
!= NULL
) {
390 * Yes. Get a port number; we're done.
392 if (ai
->ai_addr
->sa_family
== AF_INET
) {
393 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
394 udp_port
= ntohs(in4
->sin_port
);
398 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
399 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
400 udp_port
= ntohs(in6
->sin6_port
);
410 * We need to check /etc/services for ambiguous entries.
411 * If we find an ambiguous entry, and it has the
412 * same port number, change the proto to PROTO_UNDEF
413 * so both TCP and UDP will be checked.
417 *proto
= IPPROTO_TCP
;
419 if (udp_port
== tcp_port
)
420 *proto
= PROTO_UNDEF
;
423 /* Can't handle ambiguous names that refer
424 to different port numbers. */
425 warning("ambiguous port %s in /etc/services",
433 *proto
= IPPROTO_UDP
;
436 #if defined(ultrix) || defined(__osf__)
437 /* Special hack in case NFS isn't in /etc/services */
438 if (strcmp(name
, "nfs") == 0) {
440 *proto
= PROTO_UNDEF
;
448 * Convert a string in the form PPP-PPP, where correspond to ports, to
449 * a starting and ending port in a port range.
450 * Return 0 on failure.
453 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
459 if (sscanf(name
, "%d-%d", &p1
, &p2
) != 2) {
460 if ((cpy
= strdup(name
)) == NULL
)
463 if ((off
= strchr(cpy
, '-')) == NULL
) {
470 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
476 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
482 if (*proto
!= save_proto
)
483 *proto
= PROTO_UNDEF
;
487 *proto
= PROTO_UNDEF
;
494 * XXX - not guaranteed to be thread-safe! See below for platforms
495 * on which it is thread-safe and on which it isn't.
498 pcap_nametoproto(const char *str
)
501 #if defined(HAVE_LINUX_GETNETBYNAME_R)
503 * We have Linux's reentrant getprotobyname_r().
505 struct protoent result_buf
;
506 char buf
[1024]; /* arbitrary size */
509 err
= getprotobyname_r(str
, &result_buf
, buf
, sizeof buf
, &p
);
512 * XXX - dynamically allocate the buffer, and make it
513 * bigger if we get ERANGE back?
517 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
519 * We have Solaris's and IRIX's reentrant getprotobyname_r().
521 struct protoent result_buf
;
522 char buf
[1024]; /* arbitrary size */
524 p
= getprotobyname_r(str
, &result_buf
, buf
, (int)sizeof buf
);
525 #elif defined(HAVE_AIX_GETNETBYNAME_R)
527 * We have AIX's reentrant getprotobyname_r().
529 struct protoent result_buf
;
530 struct protoent_data proto_data
;
532 if (getprotobyname_r(str
, &result_buf
, &proto_data
) == -1)
538 * We don't have any getprotobyname_r(); either we have a
539 * getprotobyname() that uses thread-specific data, in which
540 * case we're thread-safe (sufficiently recent FreeBSD,
541 * sufficiently recent Darwin-based OS, sufficiently recent
542 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
543 * the traditional getprotobyname() (everything else, including
544 * current NetBSD and OpenBSD), in which case we're not
547 p
= getprotobyname(str
);
555 #include "ethertype.h"
563 * Static data base of ether protocol types.
564 * tcpdump used to import this, and it's declared as an export on
565 * Debian, at least, so make it a public symbol, even though we
566 * don't officially export it by declaring it in a header file.
567 * (Programs *should* do this themselves, as tcpdump now does.)
569 * We declare it here, right before defining it, to squelch any
570 * warnings we might get from compilers about the lack of a
573 PCAP_API
struct eproto eproto_db
[];
574 PCAP_API_DEF
struct eproto eproto_db
[] = {
575 { "pup", ETHERTYPE_PUP
},
576 { "xns", ETHERTYPE_NS
},
577 { "ip", ETHERTYPE_IP
},
579 { "ip6", ETHERTYPE_IPV6
},
581 { "arp", ETHERTYPE_ARP
},
582 { "rarp", ETHERTYPE_REVARP
},
583 { "sprite", ETHERTYPE_SPRITE
},
584 { "mopdl", ETHERTYPE_MOPDL
},
585 { "moprc", ETHERTYPE_MOPRC
},
586 { "decnet", ETHERTYPE_DN
},
587 { "lat", ETHERTYPE_LAT
},
588 { "sca", ETHERTYPE_SCA
},
589 { "lanbridge", ETHERTYPE_LANBRIDGE
},
590 { "vexp", ETHERTYPE_VEXP
},
591 { "vprod", ETHERTYPE_VPROD
},
592 { "atalk", ETHERTYPE_ATALK
},
593 { "atalkarp", ETHERTYPE_AARP
},
594 { "loopback", ETHERTYPE_LOOPBACK
},
595 { "decdts", ETHERTYPE_DECDTS
},
596 { "decdns", ETHERTYPE_DECDNS
},
601 pcap_nametoeproto(const char *s
)
603 struct eproto
*p
= eproto_db
;
606 if (strcmp(p
->s
, s
) == 0)
615 /* Static data base of LLC values. */
616 static struct eproto llc_db
[] = {
617 { "iso", LLCSAP_ISONS
},
618 { "stp", LLCSAP_8021D
},
619 { "ipx", LLCSAP_IPX
},
620 { "netbeui", LLCSAP_NETBEUI
},
625 pcap_nametollc(const char *s
)
627 struct eproto
*p
= llc_db
;
630 if (strcmp(p
->s
, s
) == 0)
637 /* Hex digit to 8-bit unsigned integer. */
642 return (u_char
)(c
- '0');
644 return (u_char
)(c
- 'a' + 10);
646 return (u_char
)(c
- 'A' + 10);
650 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
659 while (*s
&& *s
!= '.')
660 n
= n
* 10 + *s
++ - '0';
672 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
675 #define AREAMASK 0176000
676 #define NODEMASK 01777
680 if (sscanf(s
, "%d.%d", &area
, &node
) != 2)
683 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
684 *addr
|= (node
& NODEMASK
);
690 * Convert 's', which can have the one of the forms:
692 * "xx:xx:xx:xx:xx:xx"
693 * "xx.xx.xx.xx.xx.xx"
694 * "xx-xx-xx-xx-xx-xx"
698 * (or various mixes of ':', '.', and '-') into a new
699 * ethernet address. Assumes 's' is well formed.
702 pcap_ether_aton(const char *s
)
704 register u_char
*ep
, *e
;
707 e
= ep
= (u_char
*)malloc(6);
712 if (*s
== ':' || *s
== '.' || *s
== '-')
715 if (isxdigit((unsigned char)*s
)) {
725 #ifndef HAVE_ETHER_HOSTTON
728 * XXX - not thread-safe, because pcap_next_etherent() isn't thread-
729 * safe! Needs a mutex or a thread-safe pcap_next_etherent().
732 pcap_ether_hostton(const char *name
)
734 register struct pcap_etherent
*ep
;
736 static FILE *fp
= NULL
;
740 fp
= fopen(PCAP_ETHERS_FILE
, "r");
744 } else if (fp
== NULL
)
749 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
750 if (strcmp(ep
->name
, name
) == 0) {
751 ap
= (u_char
*)malloc(6);
753 memcpy(ap
, ep
->addr
, 6);
763 * Use the OS-supplied routine.
764 * This *should* be thread-safe; the API doesn't have a static buffer.
767 pcap_ether_hostton(const char *name
)
773 if (ether_hostton(name
, (struct ether_addr
*)a
) == 0) {
774 ap
= (u_char
*)malloc(6);
776 memcpy((char *)ap
, (char *)a
, 6);
783 * XXX - not guaranteed to be thread-safe!
787 __pcap_nametodnaddr(const char *name
, u_short
*res
)
789 struct nodeent
*getnodebyname();
792 nep
= getnodebyname(name
);
793 if (nep
== ((struct nodeent
*)0))
796 memcpy((char *)res
, (char *)nep
->n_addr
, sizeof(unsigned short));
799 __pcap_nametodnaddr(const char *name _U_
, u_short
*res _U_
)