]>
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 #ifdef HAVE_ETHER_HOSTTON
39 #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
41 * OK, just include <net/ethernet.h>.
43 #include <net/ethernet.h>
44 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
46 * OK, just include <netinet/ether.h>
48 #include <netinet/ether.h>
49 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
51 * OK, just include <sys/ethernet.h>
53 #include <sys/ethernet.h>
54 #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
56 * OK, just include <arpa/inet.h>
58 #include <arpa/inet.h>
59 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
61 * OK, include <netinet/if_ether.h>, after all the other stuff we
62 * need to include or define for its benefit.
64 #define NEED_NETINET_IF_ETHER_H
67 * We'll have to declare it ourselves.
68 * If <netinet/if_ether.h> defines struct ether_addr, include
69 * it. Otherwise, define it ourselves.
71 #ifdef HAVE_STRUCT_ETHER_ADDR
72 #define NEED_NETINET_IF_ETHER_H
73 #else /* HAVE_STRUCT_ETHER_ADDR */
75 unsigned char ether_addr_octet
[6];
77 #endif /* HAVE_STRUCT_ETHER_ADDR */
78 #endif /* what declares ether_hostton() */
80 #ifdef NEED_NETINET_IF_ETHER_H
81 #include <net/if.h> /* Needed on some platforms */
82 #include <netinet/in.h> /* Needed on some platforms */
83 #include <netinet/if_ether.h>
84 #endif /* NEED_NETINET_IF_ETHER_H */
86 #ifndef HAVE_DECL_ETHER_HOSTTON
88 * No header declares it, so declare it ourselves.
90 extern int ether_hostton(const char *, struct ether_addr
*);
91 #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
92 #endif /* HAVE_ETHER_HOSTTON */
94 #include <arpa/inet.h>
103 #include "pcap-int.h"
105 #include "diag-control.h"
108 #include <pcap/namedb.h>
109 #include "nametoaddr.h"
111 #include "thread-local.h"
113 #ifdef HAVE_OS_PROTO_H
114 #include "os-proto.h"
118 #define NTOHL(x) (x) = ntohl(x)
119 #define NTOHS(x) (x) = ntohs(x)
123 * Convert host name to internet address.
124 * Return 0 upon failure.
125 * XXX - not thread-safe; don't use it inside libpcap.
128 pcap_nametoaddr(const char *name
)
131 static bpf_u_int32
*hlist
[2];
137 * gethostbyname() is deprecated on Windows, perhaps because
138 * it's not thread-safe, or because it doesn't support IPv6,
141 * We deprecate pcap_nametoaddr() on all platforms because
142 * it's not thread-safe; we supply it for backwards compatibility,
143 * so suppress the deprecation warning. We could, I guess,
144 * use getaddrinfo() and construct the array ourselves, but
145 * that's probably not worth the effort, as that wouldn't make
146 * this thread-safe - we can't change the API to require that
147 * our caller free the address array, so we still have to reuse
151 if ((hp
= gethostbyname(name
)) != NULL
) {
154 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
158 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
160 return (bpf_u_int32
**)hp
->h_addr_list
;
168 pcap_nametoaddrinfo(const char *name
)
170 struct addrinfo hints
, *res
;
173 memset(&hints
, 0, sizeof(hints
));
174 hints
.ai_family
= PF_UNSPEC
;
175 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
176 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
177 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
185 * Convert net name to internet address.
186 * Return 0 upon failure.
187 * XXX - not guaranteed to be thread-safe! See below for platforms
188 * on which it is thread-safe and on which it isn't.
190 #if defined(_WIN32) || defined(__CYGWIN__)
192 pcap_nametonetaddr(const char *name _U_
)
195 * There's no "getnetbyname()" on Windows.
197 * XXX - I guess we could use the BSD code to read
198 * C:\Windows\System32\drivers\etc/networks, assuming
199 * that's its home on all the versions of Windows
200 * we use, but that file probably just has the loopback
201 * network on 127/24 on 99 44/100% of Windows machines.
203 * (Heck, these days it probably just has that on 99 44/100%
204 * of *UN*X* machines.)
210 pcap_nametonetaddr(const char *name
)
216 #if defined(HAVE_LINUX_GETNETBYNAME_R)
218 * We have Linux's reentrant getnetbyname_r().
220 struct netent result_buf
;
221 char buf
[1024]; /* arbitrary size */
226 * Apparently, the man page at
228 * http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
232 * If the function call successfully obtains a network record,
233 * then *result is set pointing to result_buf; otherwise, *result
236 * and, in fact, at least in some versions of GNU libc, it does
237 * *not* always get set if getnetbyname_r() succeeds.
240 err
= getnetbyname_r(name
, &result_buf
, buf
, sizeof buf
, &np
,
244 * XXX - dynamically allocate the buffer, and make it
245 * bigger if we get ERANGE back?
249 #elif defined(HAVE_SOLARIS_GETNETBYNAME_R)
251 * We have Solaris's reentrant getnetbyname_r().
253 struct netent result_buf
;
254 char buf
[1024]; /* arbitrary size */
256 np
= getnetbyname_r(name
, &result_buf
, buf
, (int)sizeof buf
);
257 #elif defined(HAVE_AIX_GETNETBYNAME_R)
259 * We have AIX's reentrant getnetbyname_r().
261 struct netent result_buf
;
262 struct netent_data net_data
;
264 if (getnetbyname_r(name
, &result_buf
, &net_data
) == -1)
270 * We don't have any getnetbyname_r(); either we have a
271 * getnetbyname() that uses thread-specific data, in which
272 * case we're thread-safe (sufficiently recent FreeBSD,
273 * sufficiently recent Darwin-based OS, sufficiently recent
274 * HP-UX, or we have the
275 * traditional getnetbyname() (everything else, including
276 * current NetBSD and OpenBSD), in which case we're not
279 np
= getnetbyname(name
);
289 * Convert a port name to its port and protocol numbers.
290 * We assume only TCP or UDP.
291 * Return 0 upon failure.
294 pcap_nametoport(const char *name
, int *port
, int *proto
)
296 struct addrinfo hints
, *res
, *ai
;
298 struct sockaddr_in
*in4
;
300 struct sockaddr_in6
*in6
;
306 * We check for both TCP and UDP in case there are
309 memset(&hints
, 0, sizeof(hints
));
310 hints
.ai_family
= PF_UNSPEC
;
311 hints
.ai_socktype
= SOCK_STREAM
;
312 hints
.ai_protocol
= IPPROTO_TCP
;
313 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
315 if (error
!= EAI_NONAME
&&
316 error
!= EAI_SERVICE
) {
318 * This is a real error, not just "there's
319 * no such service name".
320 * XXX - this doesn't return an error string.
326 * OK, we found it. Did it find anything?
328 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
330 * Does it have an address?
332 if (ai
->ai_addr
!= NULL
) {
334 * Yes. Get a port number; we're done.
336 if (ai
->ai_addr
->sa_family
== AF_INET
) {
337 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
338 tcp_port
= ntohs(in4
->sin_port
);
342 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
343 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
344 tcp_port
= ntohs(in6
->sin6_port
);
353 memset(&hints
, 0, sizeof(hints
));
354 hints
.ai_family
= PF_UNSPEC
;
355 hints
.ai_socktype
= SOCK_DGRAM
;
356 hints
.ai_protocol
= IPPROTO_UDP
;
357 error
= getaddrinfo(NULL
, name
, &hints
, &res
);
359 if (error
!= EAI_NONAME
&&
360 error
!= EAI_SERVICE
) {
362 * This is a real error, not just "there's
363 * no such service name".
364 * XXX - this doesn't return an error string.
370 * OK, we found it. Did it find anything?
372 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
374 * Does it have an address?
376 if (ai
->ai_addr
!= NULL
) {
378 * Yes. Get a port number; we're done.
380 if (ai
->ai_addr
->sa_family
== AF_INET
) {
381 in4
= (struct sockaddr_in
*)ai
->ai_addr
;
382 udp_port
= ntohs(in4
->sin_port
);
386 if (ai
->ai_addr
->sa_family
== AF_INET6
) {
387 in6
= (struct sockaddr_in6
*)ai
->ai_addr
;
388 udp_port
= ntohs(in6
->sin6_port
);
398 * We need to check /etc/services for ambiguous entries.
399 * If we find an ambiguous entry, and it has the
400 * same port number, change the proto to PROTO_UNDEF
401 * so both TCP and UDP will be checked.
405 *proto
= IPPROTO_TCP
;
407 if (udp_port
== tcp_port
)
408 *proto
= PROTO_UNDEF
;
411 /* Can't handle ambiguous names that refer
412 to different port numbers. */
413 warning("ambiguous port %s in /etc/services",
421 *proto
= IPPROTO_UDP
;
428 * Convert a string in the form PPP-PPP, where correspond to ports, to
429 * a starting and ending port in a port range.
430 * Return 0 on failure.
433 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
438 if ((cpy
= strdup(name
)) == NULL
)
441 if ((off
= strchr(cpy
, '-')) == NULL
) {
448 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
454 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
460 if (*proto
!= save_proto
)
461 *proto
= PROTO_UNDEF
;
467 * XXX - not guaranteed to be thread-safe! See below for platforms
468 * on which it is thread-safe and on which it isn't.
471 pcap_nametoproto(const char *str
)
474 #if defined(HAVE_LINUX_GETPROTOBYNAME_R)
476 * We have Linux's reentrant getprotobyname_r().
478 struct protoent result_buf
;
479 char buf
[1024]; /* arbitrary size */
482 err
= getprotobyname_r(str
, &result_buf
, buf
, sizeof buf
, &p
);
485 * XXX - dynamically allocate the buffer, and make it
486 * bigger if we get ERANGE back?
490 #elif defined(HAVE_SOLARIS_GETPROTOBYNAME_R)
492 * We have Solaris's reentrant getprotobyname_r().
494 struct protoent result_buf
;
495 char buf
[1024]; /* arbitrary size */
497 p
= getprotobyname_r(str
, &result_buf
, buf
, (int)sizeof buf
);
498 #elif defined(HAVE_AIX_GETPROTOBYNAME_R)
500 * We have AIX's reentrant getprotobyname_r().
502 struct protoent result_buf
;
503 // "The structure must be zero-filled before it is used..." (OpenBSD).
504 struct protoent_data proto_data
= {0};
506 if (getprotobyname_r(str
, &result_buf
, &proto_data
) == -1)
512 * We don't have any getprotobyname_r(); either we have a
513 * getprotobyname() that uses thread-specific data, in which
514 * case we're thread-safe (sufficiently recent FreeBSD,
515 * sufficiently recent Darwin-based OS, sufficiently recent
516 * HP-UX, Windows), or we have
517 * the traditional getprotobyname() (everything else, including
518 * current NetBSD and OpenBSD), in which case we're not
521 p
= getprotobyname(str
);
529 #include "ethertype.h"
537 * Static data base of ether protocol types.
538 * tcpdump used to import this, and it's declared as an export on
539 * Debian, at least, so make it a public symbol, even though we
540 * don't officially export it by declaring it in a header file.
541 * (Programs *should* do this themselves, as tcpdump now does.)
543 * We declare it here, right before defining it, to squelch any
544 * warnings we might get from compilers about the lack of a
547 PCAP_API
struct eproto eproto_db
[];
548 PCAP_API_DEF
struct eproto eproto_db
[] = {
549 { "aarp", ETHERTYPE_AARP
},
550 { "arp", ETHERTYPE_ARP
},
551 { "atalk", ETHERTYPE_ATALK
},
552 { "decnet", ETHERTYPE_DN
},
553 { "ip", ETHERTYPE_IP
},
555 { "ip6", ETHERTYPE_IPV6
},
557 { "lat", ETHERTYPE_LAT
},
558 { "loopback", ETHERTYPE_LOOPBACK
},
559 { "mopdl", ETHERTYPE_MOPDL
},
560 { "moprc", ETHERTYPE_MOPRC
},
561 { "rarp", ETHERTYPE_REVARP
},
562 { "sca", ETHERTYPE_SCA
},
567 pcap_nametoeproto(const char *s
)
569 struct eproto
*p
= eproto_db
;
572 if (strcmp(p
->s
, s
) == 0)
581 /* Static data base of LLC values. */
582 static struct eproto llc_db
[] = {
583 { "iso", LLCSAP_ISONS
},
584 { "stp", LLCSAP_8021D
},
585 { "ipx", LLCSAP_IPX
},
586 { "netbeui", LLCSAP_NETBEUI
},
591 pcap_nametollc(const char *s
)
593 struct eproto
*p
= llc_db
;
596 if (strcmp(p
->s
, s
) == 0)
603 /* Hex digit to 8-bit unsigned integer. */
605 pcapint_xdtoi(const u_char c
)
607 if (c
>= '0' && c
<= '9')
608 return (u_char
)(c
- '0');
609 else if (c
>= 'a' && c
<= 'f')
610 return (u_char
)(c
- 'a' + 10);
612 return (u_char
)(c
- 'A' + 10);
616 pcapint_atoin(const char *s
, bpf_u_int32
*addr
)
625 while (*s
&& *s
!= '.') {
627 /* The result will be > 255 */
630 n
= n
* 10 + *s
++ - '0';
645 * If 's' is not a string that is a well-formed DECnet address (aa.nnnn),
646 * return zero. Otherwise parse the address into the low 16 bits of 'addr'
647 * and return a non-zero. The binary DECnet address consists of a 6-bit area
648 * number and a 10-bit node number; neither area 0 nor node 0 are valid for
649 * normal addressing purposes, but either can appear on the wire.
652 pcapint_atodn(const char *s
, bpf_u_int32
*addr
)
655 #define AREAMASK 0176000
656 #define NODEMASK 01777
658 /* Initialize to squelch a compiler warning only. */
659 u_int node
= 0, area
= 0;
664 * --> START --> AREA --> DOT --> NODE -->
667 * +--------> INVALID <------+
680 if (PCAP_ISDIGIT(*s
)) {
692 if (PCAP_ISDIGIT(*s
)) {
693 area
= area
* 10 + *s
- '0';
694 if (area
<= AREAMASK
>> AREASHIFT
)
700 if (PCAP_ISDIGIT(*s
)) {
708 if (PCAP_ISDIGIT(*s
)) {
709 node
= node
* 10 + *s
- '0';
710 if (node
<= NODEMASK
)
721 * This condition is false if the string comes from the lexer, but
722 * let's not depend on that.
724 if (fsm_state
!= NODE
)
727 *addr
= area
<< AREASHIFT
| node
;
732 * libpcap ARCnet address format is "^\$[0-9a-fA-F]{1,2}$" in regexp syntax.
733 * Iff the given string is a well-formed ARCnet address, parse the string,
734 * store the 8-bit unsigned value into the provided integer and return 1.
735 * Otherwise return 0.
737 * --> START -- $ --> DOLLAR -- [0-9a-fA-F] --> HEX1 -- \0 -->-+
739 * [.] [.] [0-9a-fA-F] |
742 * (invalid) <--------+-<---------------[.]-- HEX2 -- \0 -->-+--> (valid)
745 pcapint_atoan(const char *s
, uint8_t *addr
)
763 if (! PCAP_ISXDIGIT(*s
))
765 tmp
= pcapint_xdtoi(*s
);
769 if (! PCAP_ISXDIGIT(*s
))
772 tmp
|= pcapint_xdtoi(*s
);
780 if (fsm_state
== HEX1
|| fsm_state
== HEX2
) {
790 * Convert 's', which can have the one of the forms:
792 * "xx:xx:xx:xx:xx:xx"
793 * "xx.xx.xx.xx.xx.xx"
794 * "xx-xx-xx-xx-xx-xx"
798 * (or various mixes of ':', '.', and '-') into a new
799 * ethernet address. Assumes 's' is well formed.
802 pcap_ether_aton(const char *s
)
804 register u_char
*ep
, *e
;
807 e
= ep
= (u_char
*)malloc(6);
812 if (*s
== ':' || *s
== '.' || *s
== '-')
814 d
= pcapint_xdtoi(*s
++);
815 if (PCAP_ISXDIGIT(*s
)) {
817 d
|= pcapint_xdtoi(*s
++);
825 #ifndef HAVE_ETHER_HOSTTON
829 * This should be thread-safe, as we define the static variables
830 * we use to be thread-local, and as pcap_next_etherent() does so
834 pcap_ether_hostton(const char *name
)
836 register struct pcap_etherent
*ep
;
838 static thread_local
FILE *fp
= NULL
;
839 static thread_local
int init
= 0;
842 fp
= fopen(PCAP_ETHERS_FILE
, "r");
846 } else if (fp
== NULL
)
851 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
852 if (strcmp(ep
->name
, name
) == 0) {
853 ap
= (u_char
*)malloc(6);
855 memcpy(ap
, ep
->addr
, 6);
865 * Use the OS-supplied routine.
866 * This *should* be thread-safe; the API doesn't have a static buffer.
869 pcap_ether_hostton(const char *name
)
876 * In AIX 7.1 and 7.2: int ether_hostton(char *, struct ether_addr *);
878 pcapint_strlcpy(namebuf
, name
, sizeof(namebuf
));
880 if (ether_hostton(namebuf
, (struct ether_addr
*)a
) == 0) {
881 ap
= (u_char
*)malloc(6);
883 memcpy((char *)ap
, (char *)a
, 6);