]>
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.
33 * To quote the MSDN page for getaddrinfo() at
35 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
37 * "Support for getaddrinfo on Windows 2000 and older versions
38 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
39 * later. To execute an application that uses this function on earlier
40 * versions of Windows, then you need to include the Ws2tcpip.h and
41 * Wspiapi.h files. When the Wspiapi.h include file is added, the
42 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
43 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
44 * function is implemented in such a way that if the Ws2_32.dll or the
45 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
46 * Preview for Windows 2000) does not include getaddrinfo, then a
47 * version of getaddrinfo is implemented inline based on code in the
48 * Wspiapi.h header file. This inline code will be used on older Windows
49 * platforms that do not natively support the getaddrinfo function."
51 * We use getaddrinfo(), so we include Wspiapi.h here.
56 #include <sys/param.h>
57 #include <sys/types.h>
58 #include <sys/socket.h>
61 #include <netinet/in.h>
63 #ifdef HAVE_ETHER_HOSTTON
64 #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
66 * OK, just include <net/ethernet.h>.
68 #include <net/ethernet.h>
69 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
71 * OK, just include <netinet/ether.h>
73 #include <netinet/ether.h>
74 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
76 * OK, just include <sys/ethernet.h>
78 #include <sys/ethernet.h>
79 #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
81 * OK, just include <arpa/inet.h>
83 #include <arpa/inet.h>
84 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
86 * OK, include <netinet/if_ether.h>, after all the other stuff we
87 * need to include or define for its benefit.
89 #define NEED_NETINET_IF_ETHER_H
92 * We'll have to declare it ourselves.
93 * If <netinet/if_ether.h> defines struct ether_addr, include
94 * it. Otherwise, define it ourselves.
96 #ifdef HAVE_STRUCT_ETHER_ADDR
97 #define NEED_NETINET_IF_ETHER_H
98 #else /* HAVE_STRUCT_ETHER_ADDR */
100 unsigned char ether_addr_octet
[6];
102 #endif /* HAVE_STRUCT_ETHER_ADDR */
103 #endif /* what declares ether_hostton() */
105 #ifdef NEED_NETINET_IF_ETHER_H
106 #include <net/if.h> /* Needed on some platforms */
107 #include <netinet/in.h> /* Needed on some platforms */
108 #include <netinet/if_ether.h>
109 #endif /* NEED_NETINET_IF_ETHER_H */
111 #ifndef HAVE_DECL_ETHER_HOSTTON
113 * No header declares it, so declare it ourselves.
115 extern int ether_hostton(const char *, struct ether_addr
*);
116 #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
117 #endif /* HAVE_ETHER_HOSTTON */
119 #include <arpa/inet.h>
128 #include "pcap-int.h"
130 #include "diag-control.h"
133 #include <pcap/namedb.h>
134 #include "nametoaddr.h"
136 #include "thread-local.h"
138 #ifdef HAVE_OS_PROTO_H
139 #include "os-proto.h"
143 #define NTOHL(x) (x) = ntohl(x)
144 #define NTOHS(x) (x) = ntohs(x)
148 * Convert host name to internet address.
149 * Return 0 upon failure.
150 * XXX - not thread-safe; don't use it inside libpcap.
153 pcap_nametoaddr(const char *name
)
156 static bpf_u_int32
*hlist
[2];
162 * gethostbyname() is deprecated on Windows, perhaps because
163 * it's not thread-safe, or because it doesn't support IPv6,
166 * We deprecate pcap_nametoaddr() on all platforms because
167 * it's not thread-safe; we supply it for backwards compatibility,
168 * so suppress the deprecation warning. We could, I guess,
169 * use getaddrinfo() and construct the array ourselves, but
170 * that's probably not worth the effort, as that wouldn't make
171 * this thread-safe - we can't change the API to require that
172 * our caller free the address array, so we still have to reuse
176 if ((hp
= gethostbyname(name
)) != NULL
) {
179 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
183 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
185 return (bpf_u_int32
**)hp
->h_addr_list
;
193 pcap_nametoaddrinfo(const char *name
)
195 struct addrinfo hints
, *res
;
198 memset(&hints
, 0, sizeof(hints
));
199 hints
.ai_family
= PF_UNSPEC
;
200 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
201 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
202 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
210 * Convert net name to internet address.
211 * Return 0 upon failure.
212 * XXX - not guaranteed to be thread-safe! See below for platforms
213 * on which it is thread-safe and on which it isn't.
215 #if defined(_WIN32) || defined(__CYGWIN__)
217 pcap_nametonetaddr(const char *name _U_
)
220 * There's no "getnetbyname()" on Windows.
222 * XXX - I guess we could use the BSD code to read
223 * C:\Windows\System32\drivers\etc/networks, assuming
224 * that's its home on all the versions of Windows
225 * we use, but that file probably just has the loopback
226 * network on 127/24 on 99 44/100% of Windows machines.
228 * (Heck, these days it probably just has that on 99 44/100%
229 * of *UN*X* machines.)
235 pcap_nametonetaddr(const char *name
)
241 #if defined(HAVE_LINUX_GETNETBYNAME_R)
243 * We have Linux's reentrant getnetbyname_r().
245 struct netent result_buf
;
246 char buf
[1024]; /* arbitrary size */
251 * Apparently, the man page at
253 * http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
257 * If the function call successfully obtains a network record,
258 * then *result is set pointing to result_buf; otherwise, *result
261 * and, in fact, at least in some versions of GNU libc, it does
262 * *not* always get set if getnetbyname_r() succeeds.
265 err
= getnetbyname_r(name
, &result_buf
, buf
, sizeof buf
, &np
,
269 * XXX - dynamically allocate the buffer, and make it
270 * bigger if we get ERANGE back?
274 #elif defined(HAVE_SOLARIS_GETNETBYNAME_R)
276 * We have Solaris's reentrant getnetbyname_r().
278 struct netent result_buf
;
279 char buf
[1024]; /* arbitrary size */
281 np
= getnetbyname_r(name
, &result_buf
, buf
, (int)sizeof buf
);
282 #elif defined(HAVE_AIX_GETNETBYNAME_R)
284 * We have AIX's reentrant getnetbyname_r().
286 struct netent result_buf
;
287 struct netent_data net_data
;
289 if (getnetbyname_r(name
, &result_buf
, &net_data
) == -1)
295 * We don't have any getnetbyname_r(); either we have a
296 * getnetbyname() that uses thread-specific data, in which
297 * case we're thread-safe (sufficiently recent FreeBSD,
298 * sufficiently recent Darwin-based OS, sufficiently recent
299 * HP-UX, or we have the
300 * traditional getnetbyname() (everything else, including
301 * current NetBSD and OpenBSD), in which case we're not
304 np
= getnetbyname(name
);
314 * Convert a port name to its port and protocol numbers.
315 * We assume only TCP or UDP.
316 * Return 0 upon failure.
319 pcap_nametoport(const char *name
, int *port
, int *proto
)
321 struct addrinfo hints
, *res
, *ai
;
323 struct sockaddr_in
*in4
;
325 struct sockaddr_in6
*in6
;
331 * We check for both TCP and UDP in case there are
334 memset(&hints
, 0, sizeof(hints
));
335 hints
.ai_family
= PF_UNSPEC
;
336 hints
.ai_socktype
= SOCK_STREAM
;
337 hints
.ai_protocol
= IPPROTO_TCP
;
338 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
340 if (error
!= EAI_NONAME
&&
341 error
!= EAI_SERVICE
) {
343 * This is a real error, not just "there's
344 * no such service name".
345 * XXX - this doesn't return an error string.
351 * OK, we found it. Did it find anything?
353 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
355 * Does it have an address?
357 if (ai
->ai_addr
!= NULL
) {
359 * Yes. Get a port number; we're done.
361 if (ai
->ai_addr
->sa_family
== AF_INET
) {
362 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
363 tcp_port
= ntohs(in4
->sin_port
);
367 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
368 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
369 tcp_port
= ntohs(in6
->sin6_port
);
378 memset(&hints
, 0, sizeof(hints
));
379 hints
.ai_family
= PF_UNSPEC
;
380 hints
.ai_socktype
= SOCK_DGRAM
;
381 hints
.ai_protocol
= IPPROTO_UDP
;
382 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
384 if (error
!= EAI_NONAME
&&
385 error
!= EAI_SERVICE
) {
387 * This is a real error, not just "there's
388 * no such service name".
389 * XXX - this doesn't return an error string.
395 * OK, we found it. Did it find anything?
397 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
399 * Does it have an address?
401 if (ai
->ai_addr
!= NULL
) {
403 * Yes. Get a port number; we're done.
405 if (ai
->ai_addr
->sa_family
== AF_INET
) {
406 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
407 udp_port
= ntohs(in4
->sin_port
);
411 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
412 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
413 udp_port
= ntohs(in6
->sin6_port
);
423 * We need to check /etc/services for ambiguous entries.
424 * If we find an ambiguous entry, and it has the
425 * same port number, change the proto to PROTO_UNDEF
426 * so both TCP and UDP will be checked.
430 *proto
= IPPROTO_TCP
;
432 if (udp_port
== tcp_port
)
433 *proto
= PROTO_UNDEF
;
436 /* Can't handle ambiguous names that refer
437 to different port numbers. */
438 warning("ambiguous port %s in /etc/services",
446 *proto
= IPPROTO_UDP
;
453 * Convert a string in the form PPP-PPP, where correspond to ports, to
454 * a starting and ending port in a port range.
455 * Return 0 on failure.
458 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
463 if ((cpy
= strdup(name
)) == NULL
)
466 if ((off
= strchr(cpy
, '-')) == NULL
) {
473 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
479 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
485 if (*proto
!= save_proto
)
486 *proto
= PROTO_UNDEF
;
492 * XXX - not guaranteed to be thread-safe! See below for platforms
493 * on which it is thread-safe and on which it isn't.
496 pcap_nametoproto(const char *str
)
499 #if defined(HAVE_LINUX_GETNETBYNAME_R)
501 * We have Linux's reentrant getprotobyname_r().
503 struct protoent result_buf
;
504 char buf
[1024]; /* arbitrary size */
507 err
= getprotobyname_r(str
, &result_buf
, buf
, sizeof buf
, &p
);
510 * XXX - dynamically allocate the buffer, and make it
511 * bigger if we get ERANGE back?
515 #elif defined(HAVE_SOLARIS_GETNETBYNAME_R)
517 * We have Solaris's reentrant getprotobyname_r().
519 struct protoent result_buf
;
520 char buf
[1024]; /* arbitrary size */
522 p
= getprotobyname_r(str
, &result_buf
, buf
, (int)sizeof buf
);
523 #elif defined(HAVE_AIX_GETNETBYNAME_R)
525 * We have AIX's reentrant getprotobyname_r().
527 struct protoent result_buf
;
528 struct protoent_data proto_data
;
530 if (getprotobyname_r(str
, &result_buf
, &proto_data
) == -1)
536 * We don't have any getprotobyname_r(); either we have a
537 * getprotobyname() that uses thread-specific data, in which
538 * case we're thread-safe (sufficiently recent FreeBSD,
539 * sufficiently recent Darwin-based OS, sufficiently recent
540 * HP-UX, Windows), or we have
541 * the traditional getprotobyname() (everything else, including
542 * current NetBSD and OpenBSD), in which case we're not
545 p
= getprotobyname(str
);
553 #include "ethertype.h"
561 * Static data base of ether protocol types.
562 * tcpdump used to import this, and it's declared as an export on
563 * Debian, at least, so make it a public symbol, even though we
564 * don't officially export it by declaring it in a header file.
565 * (Programs *should* do this themselves, as tcpdump now does.)
567 * We declare it here, right before defining it, to squelch any
568 * warnings we might get from compilers about the lack of a
571 PCAP_API
struct eproto eproto_db
[];
572 PCAP_API_DEF
struct eproto eproto_db
[] = {
573 { "aarp", ETHERTYPE_AARP
},
574 { "arp", ETHERTYPE_ARP
},
575 { "atalk", ETHERTYPE_ATALK
},
576 { "decnet", ETHERTYPE_DN
},
577 { "ip", ETHERTYPE_IP
},
579 { "ip6", ETHERTYPE_IPV6
},
581 { "lat", ETHERTYPE_LAT
},
582 { "loopback", ETHERTYPE_LOOPBACK
},
583 { "mopdl", ETHERTYPE_MOPDL
},
584 { "moprc", ETHERTYPE_MOPRC
},
585 { "rarp", ETHERTYPE_REVARP
},
586 { "sca", ETHERTYPE_SCA
},
591 pcap_nametoeproto(const char *s
)
593 struct eproto
*p
= eproto_db
;
596 if (strcmp(p
->s
, s
) == 0)
605 /* Static data base of LLC values. */
606 static struct eproto llc_db
[] = {
607 { "iso", LLCSAP_ISONS
},
608 { "stp", LLCSAP_8021D
},
609 { "ipx", LLCSAP_IPX
},
610 { "netbeui", LLCSAP_NETBEUI
},
615 pcap_nametollc(const char *s
)
617 struct eproto
*p
= llc_db
;
620 if (strcmp(p
->s
, s
) == 0)
627 /* Hex digit to 8-bit unsigned integer. */
631 if (c
>= '0' && c
<= '9')
632 return (u_char
)(c
- '0');
633 else if (c
>= 'a' && c
<= 'f')
634 return (u_char
)(c
- 'a' + 10);
636 return (u_char
)(c
- 'A' + 10);
640 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
649 while (*s
&& *s
!= '.') {
651 /* The result will be > 255 */
654 n
= n
* 10 + *s
++ - '0';
669 * If 's' is not a string that is a well-formed DECnet address (aa.nnnn),
670 * return zero. Otherwise parse the address into the low 16 bits of 'addr'
671 * and return a non-zero. The binary DECnet address consists of a 6-bit area
672 * number and a 10-bit node number; neither area 0 nor node 0 are valid for
673 * normal addressing purposes, but either can appear on the wire.
676 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
679 #define AREAMASK 0176000
680 #define NODEMASK 01777
682 /* Initialize to squelch a compiler warning only. */
683 u_int node
= 0, area
= 0;
688 * --> START --> AREA --> DOT --> NODE -->
691 * +--------> INVALID <------+
704 if (PCAP_ISDIGIT(*s
)) {
716 if (PCAP_ISDIGIT(*s
)) {
717 area
= area
* 10 + *s
- '0';
718 if (area
<= AREAMASK
>> AREASHIFT
)
724 if (PCAP_ISDIGIT(*s
)) {
732 if (PCAP_ISDIGIT(*s
)) {
733 node
= node
* 10 + *s
- '0';
734 if (node
<= NODEMASK
)
745 * This condition is false if the string comes from the lexer, but
746 * let's not depend on that.
748 if (fsm_state
!= NODE
)
751 *addr
= area
<< AREASHIFT
| node
;
756 * Convert 's', which can have the one of the forms:
758 * "xx:xx:xx:xx:xx:xx"
759 * "xx.xx.xx.xx.xx.xx"
760 * "xx-xx-xx-xx-xx-xx"
764 * (or various mixes of ':', '.', and '-') into a new
765 * ethernet address. Assumes 's' is well formed.
768 pcap_ether_aton(const char *s
)
770 register u_char
*ep
, *e
;
773 e
= ep
= (u_char
*)malloc(6);
778 if (*s
== ':' || *s
== '.' || *s
== '-')
781 if (PCAP_ISXDIGIT(*s
)) {
791 #ifndef HAVE_ETHER_HOSTTON
795 * This should be thread-safe, as we define the static variables
796 * we use to be thread-local, and as pcap_next_etherent() does so
800 pcap_ether_hostton(const char *name
)
802 register struct pcap_etherent
*ep
;
804 static thread_local
FILE *fp
= NULL
;
805 static thread_local
int init
= 0;
808 fp
= fopen(PCAP_ETHERS_FILE
, "r");
812 } else if (fp
== NULL
)
817 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
818 if (strcmp(ep
->name
, name
) == 0) {
819 ap
= (u_char
*)malloc(6);
821 memcpy(ap
, ep
->addr
, 6);
831 * Use the OS-supplied routine.
832 * This *should* be thread-safe; the API doesn't have a static buffer.
835 pcap_ether_hostton(const char *name
)
842 * In AIX 7.1 and 7.2: int ether_hostton(char *, struct ether_addr *);
844 pcapint_strlcpy(namebuf
, name
, sizeof(namebuf
));
846 if (ether_hostton(namebuf
, (struct ether_addr
*)a
) == 0) {
847 ap
= (u_char
*)malloc(6);
849 memcpy((char *)ap
, (char *)a
, 6);