]>
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.
35 * To quote the MSDN page for getaddrinfo() at
37 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
39 * "Support for getaddrinfo on Windows 2000 and older versions
40 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
41 * later. To execute an application that uses this function on earlier
42 * versions of Windows, then you need to include the Ws2tcpip.h and
43 * Wspiapi.h files. When the Wspiapi.h include file is added, the
44 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
45 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
46 * function is implemented in such a way that if the Ws2_32.dll or the
47 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
48 * Preview for Windows 2000) does not include getaddrinfo, then a
49 * version of getaddrinfo is implemented inline based on code in the
50 * Wspiapi.h header file. This inline code will be used on older Windows
51 * platforms that do not natively support the getaddrinfo function."
53 * We use getaddrinfo(), so we include Wspiapi.h here.
58 #include <sys/param.h>
59 #include <sys/types.h>
60 #include <sys/socket.h>
63 #include <netinet/in.h>
65 #ifdef HAVE_ETHER_HOSTTON
66 #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
68 * OK, just include <net/ethernet.h>.
70 #include <net/ethernet.h>
71 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
73 * OK, just include <netinet/ether.h>
75 #include <netinet/ether.h>
76 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
78 * OK, just include <sys/ethernet.h>
80 #include <sys/ethernet.h>
81 #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
83 * OK, just include <arpa/inet.h>
85 #include <arpa/inet.h>
86 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
88 * OK, include <netinet/if_ether.h>, after all the other stuff we
89 * need to include or define for its benefit.
91 #define NEED_NETINET_IF_ETHER_H
94 * We'll have to declare it ourselves.
95 * If <netinet/if_ether.h> defines struct ether_addr, include
96 * it. Otherwise, define it ourselves.
98 #ifdef HAVE_STRUCT_ETHER_ADDR
99 #define NEED_NETINET_IF_ETHER_H
100 #else /* HAVE_STRUCT_ETHER_ADDR */
102 unsigned char ether_addr_octet
[6];
104 #endif /* HAVE_STRUCT_ETHER_ADDR */
105 #endif /* what declares ether_hostton() */
107 #ifdef NEED_NETINET_IF_ETHER_H
108 #include <net/if.h> /* Needed on some platforms */
109 #include <netinet/in.h> /* Needed on some platforms */
110 #include <netinet/if_ether.h>
111 #endif /* NEED_NETINET_IF_ETHER_H */
113 #ifndef HAVE_DECL_ETHER_HOSTTON
115 * No header declares it, so declare it ourselves.
117 extern int ether_hostton(const char *, struct ether_addr
*);
118 #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
119 #endif /* HAVE_ETHER_HOSTTON */
121 #include <arpa/inet.h>
130 #include "pcap-int.h"
132 #include "diag-control.h"
135 #include <pcap/namedb.h>
136 #include "nametoaddr.h"
138 #include "thread-local.h"
140 #ifdef HAVE_OS_PROTO_H
141 #include "os-proto.h"
145 #define NTOHL(x) (x) = ntohl(x)
146 #define NTOHS(x) (x) = ntohs(x)
150 * Convert host name to internet address.
151 * Return 0 upon failure.
152 * XXX - not thread-safe; don't use it inside libpcap.
155 pcap_nametoaddr(const char *name
)
158 static bpf_u_int32
*hlist
[2];
164 * gethostbyname() is deprecated on Windows, perhaps because
165 * it's not thread-safe, or because it doesn't support IPv6,
168 * We deprecate pcap_nametoaddr() on all platforms because
169 * it's not thread-safe; we supply it for backwards compatibility,
170 * so suppress the deprecation warning. We could, I guess,
171 * use getaddrinfo() and construct the array ourselves, but
172 * that's probably not worth the effort, as that wouldn't make
173 * this thread-safe - we can't change the API to require that
174 * our caller free the address array, so we still have to reuse
178 if ((hp
= gethostbyname(name
)) != NULL
) {
181 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
185 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
187 return (bpf_u_int32
**)hp
->h_addr_list
;
195 pcap_nametoaddrinfo(const char *name
)
197 struct addrinfo hints
, *res
;
200 memset(&hints
, 0, sizeof(hints
));
201 hints
.ai_family
= PF_UNSPEC
;
202 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
203 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
204 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
212 * Convert net name to internet address.
213 * Return 0 upon failure.
214 * XXX - not guaranteed to be thread-safe! See below for platforms
215 * on which it is thread-safe and on which it isn't.
217 #if defined(_WIN32) || defined(__CYGWIN__)
219 pcap_nametonetaddr(const char *name _U_
)
222 * There's no "getnetbyname()" on Windows.
224 * XXX - I guess we could use the BSD code to read
225 * C:\Windows\System32\drivers\etc/networks, assuming
226 * that's its home on all the versions of Windows
227 * we use, but that file probably just has the loopback
228 * network on 127/24 on 99 44/100% of Windows machines.
230 * (Heck, these days it probably just has that on 99 44/100%
231 * of *UN*X* machines.)
237 pcap_nametonetaddr(const char *name
)
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 */
253 * Apparently, the man page at
255 * http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
259 * If the function call successfully obtains a network record,
260 * then *result is set pointing to result_buf; otherwise, *result
263 * and, in fact, at least in some versions of GNU libc, it does
264 * *not* always get set if getnetbyname_r() succeeds.
267 err
= getnetbyname_r(name
, &result_buf
, buf
, sizeof buf
, &np
,
271 * XXX - dynamically allocate the buffer, and make it
272 * bigger if we get ERANGE back?
276 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
278 * We have Solaris's and IRIX's reentrant getnetbyname_r().
280 struct netent result_buf
;
281 char buf
[1024]; /* arbitrary size */
283 np
= getnetbyname_r(name
, &result_buf
, buf
, (int)sizeof buf
);
284 #elif defined(HAVE_AIX_GETNETBYNAME_R)
286 * We have AIX's reentrant getnetbyname_r().
288 struct netent result_buf
;
289 struct netent_data net_data
;
291 if (getnetbyname_r(name
, &result_buf
, &net_data
) == -1)
297 * We don't have any getnetbyname_r(); either we have a
298 * getnetbyname() that uses thread-specific data, in which
299 * case we're thread-safe (sufficiently recent FreeBSD,
300 * sufficiently recent Darwin-based OS, sufficiently recent
301 * HP-UX, sufficiently recent Tru64 UNIX), or we have the
302 * traditional getnetbyname() (everything else, including
303 * current NetBSD and OpenBSD), in which case we're not
306 np
= getnetbyname(name
);
316 * Convert a port name to its port and protocol numbers.
317 * We assume only TCP or UDP.
318 * Return 0 upon failure.
321 pcap_nametoport(const char *name
, int *port
, int *proto
)
323 struct addrinfo hints
, *res
, *ai
;
325 struct sockaddr_in
*in4
;
327 struct sockaddr_in6
*in6
;
333 * We check for both TCP and UDP in case there are
336 memset(&hints
, 0, sizeof(hints
));
337 hints
.ai_family
= PF_UNSPEC
;
338 hints
.ai_socktype
= SOCK_STREAM
;
339 hints
.ai_protocol
= IPPROTO_TCP
;
340 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
342 if (error
!= EAI_NONAME
&&
343 error
!= EAI_SERVICE
) {
345 * This is a real error, not just "there's
346 * no such service name".
347 * XXX - this doesn't return an error string.
353 * OK, we found it. Did it find anything?
355 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
357 * Does it have an address?
359 if (ai
->ai_addr
!= NULL
) {
361 * Yes. Get a port number; we're done.
363 if (ai
->ai_addr
->sa_family
== AF_INET
) {
364 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
365 tcp_port
= ntohs(in4
->sin_port
);
369 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
370 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
371 tcp_port
= ntohs(in6
->sin6_port
);
380 memset(&hints
, 0, sizeof(hints
));
381 hints
.ai_family
= PF_UNSPEC
;
382 hints
.ai_socktype
= SOCK_DGRAM
;
383 hints
.ai_protocol
= IPPROTO_UDP
;
384 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
386 if (error
!= EAI_NONAME
&&
387 error
!= EAI_SERVICE
) {
389 * This is a real error, not just "there's
390 * no such service name".
391 * XXX - this doesn't return an error string.
397 * OK, we found it. Did it find anything?
399 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
401 * Does it have an address?
403 if (ai
->ai_addr
!= NULL
) {
405 * Yes. Get a port number; we're done.
407 if (ai
->ai_addr
->sa_family
== AF_INET
) {
408 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
409 udp_port
= ntohs(in4
->sin_port
);
413 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
414 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
415 udp_port
= ntohs(in6
->sin6_port
);
425 * We need to check /etc/services for ambiguous entries.
426 * If we find an ambiguous entry, and it has the
427 * same port number, change the proto to PROTO_UNDEF
428 * so both TCP and UDP will be checked.
432 *proto
= IPPROTO_TCP
;
434 if (udp_port
== tcp_port
)
435 *proto
= PROTO_UNDEF
;
438 /* Can't handle ambiguous names that refer
439 to different port numbers. */
440 warning("ambiguous port %s in /etc/services",
448 *proto
= IPPROTO_UDP
;
451 #if defined(ultrix) || defined(__osf__)
452 /* Special hack in case NFS isn't in /etc/services */
453 if (strcmp(name
, "nfs") == 0) {
455 *proto
= PROTO_UNDEF
;
463 * Convert a string in the form PPP-PPP, where correspond to ports, to
464 * a starting and ending port in a port range.
465 * Return 0 on failure.
468 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
473 if ((cpy
= strdup(name
)) == NULL
)
476 if ((off
= strchr(cpy
, '-')) == NULL
) {
483 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
489 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
495 if (*proto
!= save_proto
)
496 *proto
= PROTO_UNDEF
;
502 * XXX - not guaranteed to be thread-safe! See below for platforms
503 * on which it is thread-safe and on which it isn't.
506 pcap_nametoproto(const char *str
)
509 #if defined(HAVE_LINUX_GETNETBYNAME_R)
511 * We have Linux's reentrant getprotobyname_r().
513 struct protoent result_buf
;
514 char buf
[1024]; /* arbitrary size */
517 err
= getprotobyname_r(str
, &result_buf
, buf
, sizeof buf
, &p
);
520 * XXX - dynamically allocate the buffer, and make it
521 * bigger if we get ERANGE back?
525 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
527 * We have Solaris's and IRIX's reentrant getprotobyname_r().
529 struct protoent result_buf
;
530 char buf
[1024]; /* arbitrary size */
532 p
= getprotobyname_r(str
, &result_buf
, buf
, (int)sizeof buf
);
533 #elif defined(HAVE_AIX_GETNETBYNAME_R)
535 * We have AIX's reentrant getprotobyname_r().
537 struct protoent result_buf
;
538 struct protoent_data proto_data
;
540 if (getprotobyname_r(str
, &result_buf
, &proto_data
) == -1)
546 * We don't have any getprotobyname_r(); either we have a
547 * getprotobyname() that uses thread-specific data, in which
548 * case we're thread-safe (sufficiently recent FreeBSD,
549 * sufficiently recent Darwin-based OS, sufficiently recent
550 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
551 * the traditional getprotobyname() (everything else, including
552 * current NetBSD and OpenBSD), in which case we're not
555 p
= getprotobyname(str
);
563 #include "ethertype.h"
571 * Static data base of ether protocol types.
572 * tcpdump used to import this, and it's declared as an export on
573 * Debian, at least, so make it a public symbol, even though we
574 * don't officially export it by declaring it in a header file.
575 * (Programs *should* do this themselves, as tcpdump now does.)
577 * We declare it here, right before defining it, to squelch any
578 * warnings we might get from compilers about the lack of a
581 PCAP_API
struct eproto eproto_db
[];
582 PCAP_API_DEF
struct eproto eproto_db
[] = {
583 { "aarp", ETHERTYPE_AARP
},
584 { "arp", ETHERTYPE_ARP
},
585 { "atalk", ETHERTYPE_ATALK
},
586 { "decnet", ETHERTYPE_DN
},
587 { "ip", ETHERTYPE_IP
},
589 { "ip6", ETHERTYPE_IPV6
},
591 { "lat", ETHERTYPE_LAT
},
592 { "loopback", ETHERTYPE_LOOPBACK
},
593 { "mopdl", ETHERTYPE_MOPDL
},
594 { "moprc", ETHERTYPE_MOPRC
},
595 { "rarp", ETHERTYPE_REVARP
},
596 { "sca", ETHERTYPE_SCA
},
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. */
641 if (c
>= '0' && c
<= '9')
642 return (u_char
)(c
- '0');
643 else if (c
>= 'a' && c
<= 'f')
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
!= '.') {
661 /* The result will be > 255 */
664 n
= n
* 10 + *s
++ - '0';
679 * If 's' is not a string that is a well-formed DECnet address (aa.nnnn),
680 * return zero. Otherwise parse the address into the low 16 bits of 'addr'
681 * and return a non-zero. The binary DECnet address consists of a 6-bit area
682 * number and a 10-bit node number; neither area 0 nor node 0 are valid for
683 * normal addressing purposes, but either can appear on the wire.
686 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
689 #define AREAMASK 0176000
690 #define NODEMASK 01777
692 /* Initialize to squelch a compiler warning only. */
693 u_int node
= 0, area
= 0;
698 * --> START --> AREA --> DOT --> NODE -->
701 * +--------> INVALID <------+
714 if (PCAP_ISDIGIT(*s
)) {
726 if (PCAP_ISDIGIT(*s
)) {
727 area
= area
* 10 + *s
- '0';
728 if (area
<= AREAMASK
>> AREASHIFT
)
734 if (PCAP_ISDIGIT(*s
)) {
742 if (PCAP_ISDIGIT(*s
)) {
743 node
= node
* 10 + *s
- '0';
744 if (node
<= NODEMASK
)
755 * This condition is false if the string comes from the lexer, but
756 * let's not depend on that.
758 if (fsm_state
!= NODE
)
761 *addr
= area
<< AREASHIFT
| node
;
766 * Convert 's', which can have the one of the forms:
768 * "xx:xx:xx:xx:xx:xx"
769 * "xx.xx.xx.xx.xx.xx"
770 * "xx-xx-xx-xx-xx-xx"
774 * (or various mixes of ':', '.', and '-') into a new
775 * ethernet address. Assumes 's' is well formed.
778 pcap_ether_aton(const char *s
)
780 register u_char
*ep
, *e
;
783 e
= ep
= (u_char
*)malloc(6);
788 if (*s
== ':' || *s
== '.' || *s
== '-')
791 if (PCAP_ISXDIGIT(*s
)) {
801 #ifndef HAVE_ETHER_HOSTTON
805 * This should be thread-safe, as we define the static variables
806 * we use to be thread-local, and as pcap_next_etherent() does so
810 pcap_ether_hostton(const char *name
)
812 register struct pcap_etherent
*ep
;
814 static thread_local
FILE *fp
= NULL
;
815 static thread_local
int init
= 0;
818 fp
= fopen(PCAP_ETHERS_FILE
, "r");
822 } else if (fp
== NULL
)
827 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
828 if (strcmp(ep
->name
, name
) == 0) {
829 ap
= (u_char
*)malloc(6);
831 memcpy(ap
, ep
->addr
, 6);
841 * Use the OS-supplied routine.
842 * This *should* be thread-safe; the API doesn't have a static buffer.
845 pcap_ether_hostton(const char *name
)
852 * In AIX 7.1 and 7.2: int ether_hostton(char *, struct ether_addr *);
854 pcapint_strlcpy(namebuf
, name
, sizeof(namebuf
));
856 if (ether_hostton(namebuf
, (struct ether_addr
*)a
) == 0) {
857 ap
= (u_char
*)malloc(6);
859 memcpy((char *)ap
, (char *)a
, 6);