]>
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.
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
38 #if defined(__linux__) && defined(HAVE_ETHER_HOSTTON)
40 #if ! defined(__GLIBC__) && ! defined(__UCLIBC__)
42 * In musl libc (which does not identify itself) ether_hostton() is
43 * present and does not work.
45 #undef HAVE_ETHER_HOSTTON
47 #endif // defined(__linux__) && defined(HAVE_ETHER_HOSTTON)
49 #ifdef HAVE_ETHER_HOSTTON
50 #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
52 * OK, just include <net/ethernet.h>.
54 #include <net/ethernet.h>
55 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
57 * OK, just include <netinet/ether.h>
59 #include <netinet/ether.h>
60 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
62 * OK, just include <sys/ethernet.h>
64 #include <sys/ethernet.h>
65 #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
67 * OK, just include <arpa/inet.h>
69 #include <arpa/inet.h>
70 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
72 * OK, include <netinet/if_ether.h>, after all the other stuff we
73 * need to include or define for its benefit.
75 #define NEED_NETINET_IF_ETHER_H
78 * We'll have to declare it ourselves.
79 * If <netinet/if_ether.h> defines struct ether_addr, include
80 * it. Otherwise, define it ourselves.
82 #ifdef HAVE_STRUCT_ETHER_ADDR
83 #define NEED_NETINET_IF_ETHER_H
84 #else /* HAVE_STRUCT_ETHER_ADDR */
86 unsigned char ether_addr_octet
[6];
88 #endif /* HAVE_STRUCT_ETHER_ADDR */
89 #endif /* what declares ether_hostton() */
91 #ifdef NEED_NETINET_IF_ETHER_H
92 #include <net/if.h> /* Needed on some platforms */
93 #include <netinet/in.h> /* Needed on some platforms */
94 #include <netinet/if_ether.h>
95 #endif /* NEED_NETINET_IF_ETHER_H */
97 #ifndef HAVE_DECL_ETHER_HOSTTON
99 * No header declares it, so declare it ourselves.
101 extern int ether_hostton(const char *, struct ether_addr
*);
102 #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
103 #endif /* HAVE_ETHER_HOSTTON */
105 #include <arpa/inet.h>
114 #include "pcap-int.h"
116 #include "diag-control.h"
119 #include <pcap/namedb.h>
120 #include "nametoaddr.h"
122 #include "thread-local.h"
124 #ifdef HAVE_OS_PROTO_H
125 #include "os-proto.h"
129 #define NTOHL(x) (x) = ntohl(x)
133 * Convert host name to internet address.
134 * Return 0 upon failure.
135 * XXX - not thread-safe; don't use it inside libpcap.
138 pcap_nametoaddr(const char *name
)
141 static bpf_u_int32
*hlist
[2];
147 * gethostbyname() is deprecated on Windows, perhaps because
148 * it's not thread-safe, or because it doesn't support IPv6,
151 * We deprecate pcap_nametoaddr() on all platforms because
152 * it's not thread-safe; we supply it for backwards compatibility,
153 * so suppress the deprecation warning. We could, I guess,
154 * use getaddrinfo() and construct the array ourselves, but
155 * that's probably not worth the effort, as that wouldn't make
156 * this thread-safe - we can't change the API to require that
157 * our caller free the address array, so we still have to reuse
161 if ((hp
= gethostbyname(name
)) != NULL
) {
164 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
168 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
170 return (bpf_u_int32
**)hp
->h_addr_list
;
178 pcap_nametoaddrinfo(const char *name
)
180 struct addrinfo hints
, *res
;
183 memset(&hints
, 0, sizeof(hints
));
184 hints
.ai_family
= PF_UNSPEC
;
185 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
186 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
187 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
195 * Convert net name to internet address.
196 * Return 0 upon failure.
197 * XXX - not guaranteed to be thread-safe! See below for platforms
198 * on which it is thread-safe and on which it isn't.
200 #if defined(_WIN32) || defined(__CYGWIN__)
202 pcap_nametonetaddr(const char *name _U_
)
205 * There's no "getnetbyname()" on Windows.
207 * XXX - I guess we could use the BSD code to read
208 * C:\Windows\System32\drivers\etc/networks, assuming
209 * that's its home on all the versions of Windows
210 * we use, but that file probably just has the loopback
211 * network on 127/24 on 99 44/100% of Windows machines.
213 * (Heck, these days it probably just has that on 99 44/100%
214 * of *UN*X* machines.)
220 pcap_nametonetaddr(const char *name
)
226 #if defined(HAVE_LINUX_GETNETBYNAME_R)
228 * We have Linux's reentrant getnetbyname_r().
230 struct netent result_buf
;
231 char buf
[1024]; /* arbitrary size */
236 * Apparently, the man page at
238 * http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
242 * If the function call successfully obtains a network record,
243 * then *result is set pointing to result_buf; otherwise, *result
246 * and, in fact, at least in some versions of GNU libc, it does
247 * *not* always get set if getnetbyname_r() succeeds.
250 err
= getnetbyname_r(name
, &result_buf
, buf
, sizeof buf
, &np
,
254 * XXX - dynamically allocate the buffer, and make it
255 * bigger if we get ERANGE back?
259 #elif defined(HAVE_SOLARIS_GETNETBYNAME_R)
261 * We have Solaris's reentrant getnetbyname_r().
263 struct netent result_buf
;
264 char buf
[1024]; /* arbitrary size */
266 np
= getnetbyname_r(name
, &result_buf
, buf
, (int)sizeof buf
);
267 #elif defined(HAVE_AIX_GETNETBYNAME_R)
269 * We have AIX's reentrant getnetbyname_r().
271 struct netent result_buf
;
272 struct netent_data net_data
;
274 if (getnetbyname_r(name
, &result_buf
, &net_data
) == -1)
280 * We don't have any getnetbyname_r(); either we have a
281 * getnetbyname() that uses thread-specific data, in which
282 * case we're thread-safe (sufficiently recent FreeBSD,
283 * sufficiently recent Darwin-based OS, sufficiently recent
284 * HP-UX, or we have the
285 * traditional getnetbyname() (everything else, including
286 * current NetBSD and OpenBSD), in which case we're not
289 np
= getnetbyname(name
);
299 * Convert a port name to its port and protocol numbers.
300 * We assume only TCP or UDP.
301 * Return 0 upon failure.
304 pcap_nametoport(const char *name
, int *port
, int *proto
)
306 struct addrinfo hints
, *res
, *ai
;
308 struct sockaddr_in
*in4
;
309 struct sockaddr_in6
*in6
;
314 * We check for both TCP and UDP in case there are
317 memset(&hints
, 0, sizeof(hints
));
318 hints
.ai_family
= PF_UNSPEC
;
319 hints
.ai_socktype
= SOCK_STREAM
;
320 hints
.ai_protocol
= IPPROTO_TCP
;
321 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
323 if (error
!= EAI_NONAME
&&
324 error
!= EAI_SERVICE
) {
326 * This is a real error, not just "there's
327 * no such service name".
328 * XXX - this doesn't return an error string.
334 * OK, we found it. Did it find anything?
336 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
338 * Does it have an address?
340 if (ai
->ai_addr
!= NULL
) {
342 * Yes. Get a port number; we're done.
344 if (ai
->ai_addr
->sa_family
== AF_INET
) {
345 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
346 tcp_port
= ntohs(in4
->sin_port
);
349 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
350 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
351 tcp_port
= ntohs(in6
->sin6_port
);
359 memset(&hints
, 0, sizeof(hints
));
360 hints
.ai_family
= PF_UNSPEC
;
361 hints
.ai_socktype
= SOCK_DGRAM
;
362 hints
.ai_protocol
= IPPROTO_UDP
;
363 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
365 if (error
!= EAI_NONAME
&&
366 error
!= EAI_SERVICE
) {
368 * This is a real error, not just "there's
369 * no such service name".
370 * XXX - this doesn't return an error string.
376 * OK, we found it. Did it find anything?
378 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
380 * Does it have an address?
382 if (ai
->ai_addr
!= NULL
) {
384 * Yes. Get a port number; we're done.
386 if (ai
->ai_addr
->sa_family
== AF_INET
) {
387 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
388 udp_port
= ntohs(in4
->sin_port
);
391 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
392 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
393 udp_port
= ntohs(in6
->sin6_port
);
402 * We need to check /etc/services for ambiguous entries.
403 * If we find an ambiguous entry, and it has the
404 * same port number, change the proto to PROTO_UNDEF
405 * so both TCP and UDP will be checked.
409 *proto
= IPPROTO_TCP
;
411 if (udp_port
== tcp_port
)
412 *proto
= PROTO_UNDEF
;
415 /* Can't handle ambiguous names that refer
416 to different port numbers. */
417 warning("ambiguous port %s in /etc/services",
425 *proto
= IPPROTO_UDP
;
432 * Convert a string in the form PPP-PPP, where correspond to ports, to
433 * a starting and ending port in a port range.
434 * Return 0 on failure.
437 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
442 if ((cpy
= strdup(name
)) == NULL
)
445 if ((off
= strchr(cpy
, '-')) == NULL
) {
452 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
458 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
464 if (*proto
!= save_proto
)
465 *proto
= PROTO_UNDEF
;
471 * XXX - not guaranteed to be thread-safe! See below for platforms
472 * on which it is thread-safe and on which it isn't.
475 pcap_nametoproto(const char *str
)
478 #if defined(HAVE_LINUX_GETPROTOBYNAME_R)
480 * We have Linux's reentrant getprotobyname_r().
482 struct protoent result_buf
;
483 char buf
[1024]; // "...1024 bytes should be sufficient for most applications."
486 err
= getprotobyname_r(str
, &result_buf
, buf
, sizeof buf
, &p
);
488 * As far as GNU libc implementation goes, an "error" means the
489 * protocol database could not be searched, which could mean err ==
490 * ERANGE if the buffer is too small or ENOENT if the protocols(5)
491 * file does not exist (the man page does not document the latter
492 * eventuality). If the database has been searched normally and the
493 * requested protocol name was not found, it is not an "error" and
496 * This notwithstanding, p == NULL iff a record was not found for any
497 * reason (whether an "error" or not), which is the same semantics as
498 * in every other HAVE_xxxxx branch of this block. The final check
499 * after the block will catch that if necessary.
503 * XXX - dynamically allocate the buffer, and make it
504 * bigger if we get ERANGE back?
508 #elif defined(HAVE_SOLARIS_GETPROTOBYNAME_R)
510 * We have Solaris's reentrant getprotobyname_r().
512 struct protoent result_buf
;
513 char buf
[1024]; // "...must be at least 1024 bytes."
515 p
= getprotobyname_r(str
, &result_buf
, buf
, (int)sizeof buf
);
516 #elif defined(HAVE_AIX_GETPROTOBYNAME_R)
518 * We have AIX's reentrant getprotobyname_r().
520 struct protoent result_buf
;
521 // "The structure must be zero-filled before it is used..." (OpenBSD).
522 struct protoent_data proto_data
= {0};
524 if (getprotobyname_r(str
, &result_buf
, &proto_data
) == -1)
530 * We don't have any getprotobyname_r(); either we have a
531 * getprotobyname() that uses thread-specific data, in which
532 * case we're thread-safe (sufficiently recent FreeBSD,
533 * sufficiently recent Darwin-based OS, sufficiently recent
534 * HP-UX, Windows), or we have
535 * the traditional getprotobyname() (everything else, including
536 * current NetBSD and OpenBSD), in which case we're not
539 p
= getprotobyname(str
);
547 #include "ethertype.h"
555 * Static data base of ether protocol types.
556 * tcpdump used to import this, and it's declared as an export on
557 * Debian, at least, so make it a public symbol, even though we
558 * don't officially export it by declaring it in a header file.
559 * (Programs *should* do this themselves, as tcpdump now does.)
561 * We declare it here, right before defining it, to squelch any
562 * warnings we might get from compilers about the lack of a
565 PCAP_API
struct eproto eproto_db
[];
566 PCAP_API_DEF
struct eproto eproto_db
[] = {
567 { "aarp", ETHERTYPE_AARP
},
568 { "arp", ETHERTYPE_ARP
},
569 { "atalk", ETHERTYPE_ATALK
},
570 { "decnet", ETHERTYPE_DN
},
571 { "ip", ETHERTYPE_IP
},
572 { "ip6", ETHERTYPE_IPV6
},
573 { "lat", ETHERTYPE_LAT
},
574 { "loopback", ETHERTYPE_LOOPBACK
},
575 { "mopdl", ETHERTYPE_MOPDL
},
576 { "moprc", ETHERTYPE_MOPRC
},
577 { "rarp", ETHERTYPE_REVARP
},
578 { "sca", ETHERTYPE_SCA
},
583 pcap_nametoeproto(const char *s
)
585 struct eproto
*p
= eproto_db
;
588 if (strcmp(p
->s
, s
) == 0)
597 /* Static data base of LLC values. */
598 static struct eproto llc_db
[] = {
599 { "iso", LLCSAP_ISONS
},
600 { "stp", LLCSAP_8021D
},
601 { "ipx", LLCSAP_IPX
},
602 { "netbeui", LLCSAP_NETBEUI
},
607 pcap_nametollc(const char *s
)
609 struct eproto
*p
= llc_db
;
612 if (strcmp(p
->s
, s
) == 0)
619 /* Hex digit to 8-bit unsigned integer. */
621 pcapint_xdtoi(const u_char c
)
623 if (c
>= '0' && c
<= '9')
624 return (u_char
)(c
- '0');
625 else if (c
>= 'a' && c
<= 'f')
626 return (u_char
)(c
- 'a' + 10);
628 return (u_char
)(c
- 'A' + 10);
632 pcapint_atoin(const char *s
, bpf_u_int32
*addr
)
641 while (*s
&& *s
!= '.') {
643 /* The result will be > 255 */
646 n
= n
* 10 + *s
++ - '0';
661 * If 's' is not a string that is a well-formed DECnet address (aa.nnnn),
662 * return zero. Otherwise parse the address into the low 16 bits of 'addr'
663 * and return a non-zero. The binary DECnet address consists of a 6-bit area
664 * number and a 10-bit node number; neither area 0 nor node 0 are valid for
665 * normal addressing purposes, but either can appear on the wire.
668 pcapint_atodn(const char *s
, bpf_u_int32
*addr
)
671 #define AREAMASK 0176000
672 #define NODEMASK 01777
674 /* Initialize to squelch a compiler warning only. */
675 u_int node
= 0, area
= 0;
680 * --> START --> AREA --> DOT --> NODE -->
683 * +--------> INVALID <------+
696 if (PCAP_ISDIGIT(*s
)) {
708 if (PCAP_ISDIGIT(*s
)) {
709 area
= area
* 10 + *s
- '0';
710 if (area
<= AREAMASK
>> AREASHIFT
)
716 if (PCAP_ISDIGIT(*s
)) {
724 if (PCAP_ISDIGIT(*s
)) {
725 node
= node
* 10 + *s
- '0';
726 if (node
<= NODEMASK
)
737 * This condition is false if the string comes from the lexer, but
738 * let's not depend on that.
740 if (fsm_state
!= NODE
)
743 *addr
= area
<< AREASHIFT
| node
;
748 * libpcap ARCnet address format is "^\$[0-9a-fA-F]{1,2}$" in regexp syntax.
749 * Iff the given string is a well-formed ARCnet address, parse the string,
750 * store the 8-bit unsigned value into the provided integer and return 1.
751 * Otherwise return 0.
753 * --> START -- $ --> DOLLAR -- [0-9a-fA-F] --> HEX1 -- \0 -->-+
755 * [.] [.] [0-9a-fA-F] |
758 * (invalid) <--------+-<---------------[.]-- HEX2 -- \0 -->-+--> (valid)
761 pcapint_atoan(const char *s
, uint8_t *addr
)
779 if (! PCAP_ISXDIGIT(*s
))
781 tmp
= pcapint_xdtoi(*s
);
785 if (! PCAP_ISXDIGIT(*s
))
788 tmp
|= pcapint_xdtoi(*s
);
796 if (fsm_state
== HEX1
|| fsm_state
== HEX2
) {
806 * Convert 's', which can have the one of the forms:
808 * "xx:xx:xx:xx:xx:xx"
809 * "xx.xx.xx.xx.xx.xx"
810 * "xx-xx-xx-xx-xx-xx"
814 * (or various mixes of ':', '.', and '-') into a new
815 * ethernet address. Assumes 's' is well formed.
818 pcap_ether_aton(const char *s
)
820 register u_char
*ep
, *e
;
823 e
= ep
= (u_char
*)malloc(6);
828 if (*s
== ':' || *s
== '.' || *s
== '-')
830 d
= pcapint_xdtoi(*s
++);
831 if (PCAP_ISXDIGIT(*s
)) {
833 d
|= pcapint_xdtoi(*s
++);
841 #ifndef HAVE_ETHER_HOSTTON
845 * This should be thread-safe, as we define the static variables
846 * we use to be thread-local, and as pcap_next_etherent() does so
850 pcap_ether_hostton(const char *name
)
852 register struct pcap_etherent
*ep
;
854 static thread_local
FILE *fp
= NULL
;
855 static thread_local
int init
= 0;
858 fp
= fopen(PCAP_ETHERS_FILE
, "r");
862 } else if (fp
== NULL
)
867 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
868 if (strcmp(ep
->name
, name
) == 0) {
869 ap
= (u_char
*)malloc(6);
871 memcpy(ap
, ep
->addr
, 6);
881 * Use the OS-supplied routine.
882 * This *should* be thread-safe; the API doesn't have a static buffer.
885 pcap_ether_hostton(const char *name
)
892 * In AIX 7.1 and 7.2: int ether_hostton(char *, struct ether_addr *);
894 pcapint_strlcpy(namebuf
, name
, sizeof(namebuf
));
896 if (ether_hostton(namebuf
, (struct ether_addr
*)a
) == 0) {
897 ap
= (u_char
*)malloc(6);
899 memcpy((char *)ap
, (char *)a
, 6);