xref: /freebsd/contrib/libpcap/nametoaddr.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
18cf6c252SPaul Traina /*
2dfd1ee14SBill Fenner  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
38cf6c252SPaul Traina  *	The Regents of the University of California.  All rights reserved.
48cf6c252SPaul Traina  *
58cf6c252SPaul Traina  * Redistribution and use in source and binary forms, with or without
68cf6c252SPaul Traina  * modification, are permitted provided that: (1) source code distributions
78cf6c252SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
88cf6c252SPaul Traina  * distributions including binary code include the above copyright notice and
98cf6c252SPaul Traina  * this paragraph in its entirety in the documentation or other materials
108cf6c252SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
118cf6c252SPaul Traina  * features or use of this software display the following acknowledgement:
128cf6c252SPaul Traina  * ``This product includes software developed by the University of California,
138cf6c252SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
148cf6c252SPaul Traina  * the University nor the names of its contributors may be used to endorse
158cf6c252SPaul Traina  * or promote products derived from this software without specific prior
168cf6c252SPaul Traina  * written permission.
178cf6c252SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
188cf6c252SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
198cf6c252SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
208cf6c252SPaul Traina  *
218cf6c252SPaul Traina  * Name to id translation routines used by the scanner.
228cf6c252SPaul Traina  * These functions are not time critical.
238cf6c252SPaul Traina  */
248cf6c252SPaul Traina 
25b00ab754SHans Petter Selasky #include <config.h>
268cf6c252SPaul Traina 
27a0ee43a1SRui Paulo #ifdef DECNETLIB
28a0ee43a1SRui Paulo #include <sys/types.h>
29a0ee43a1SRui Paulo #include <netdnet/dnetdb.h>
30a0ee43a1SRui Paulo #endif
31a0ee43a1SRui Paulo 
32ada6f083SXin LI #ifdef _WIN32
33b00ab754SHans Petter Selasky   #include <winsock2.h>
34b00ab754SHans Petter Selasky   #include <ws2tcpip.h>
35eb9f0330SBruce M Simpson 
36ada6f083SXin LI   #ifdef INET6
37ada6f083SXin LI     /*
38ada6f083SXin LI      * To quote the MSDN page for getaddrinfo() at
39ada6f083SXin LI      *
40ada6f083SXin LI      *    https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
41ada6f083SXin LI      *
42ada6f083SXin LI      * "Support for getaddrinfo on Windows 2000 and older versions
43ada6f083SXin LI      * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
44ada6f083SXin LI      * later. To execute an application that uses this function on earlier
45ada6f083SXin LI      * versions of Windows, then you need to include the Ws2tcpip.h and
46ada6f083SXin LI      * Wspiapi.h files. When the Wspiapi.h include file is added, the
47ada6f083SXin LI      * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
48ada6f083SXin LI      * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
49ada6f083SXin LI      * function is implemented in such a way that if the Ws2_32.dll or the
50ada6f083SXin LI      * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
51ada6f083SXin LI      * Preview for Windows 2000) does not include getaddrinfo, then a
52ada6f083SXin LI      * version of getaddrinfo is implemented inline based on code in the
53ada6f083SXin LI      * Wspiapi.h header file. This inline code will be used on older Windows
54ada6f083SXin LI      * platforms that do not natively support the getaddrinfo function."
55ada6f083SXin LI      *
56b00ab754SHans Petter Selasky      * We use getaddrinfo(), so we include Wspiapi.h here.
57ada6f083SXin LI      */
58b00ab754SHans Petter Selasky     #include <wspiapi.h>
59b00ab754SHans Petter Selasky   #endif /* INET6 */
60ada6f083SXin LI #else /* _WIN32 */
618cf6c252SPaul Traina   #include <sys/param.h>
62b00ab754SHans Petter Selasky   #include <sys/types.h>
638cf6c252SPaul Traina   #include <sys/socket.h>
64628d2ac1SGarrett Wollman   #include <sys/time.h>
658cf6c252SPaul Traina 
668cf6c252SPaul Traina   #include <netinet/in.h>
67eb9f0330SBruce M Simpson 
68eb9f0330SBruce M Simpson   #ifdef HAVE_ETHER_HOSTTON
69b00ab754SHans Petter Selasky     #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
70c761ebcbSSam Leffler       /*
71b00ab754SHans Petter Selasky        * OK, just include <net/ethernet.h>.
72c761ebcbSSam Leffler        */
73b00ab754SHans Petter Selasky       #include <net/ethernet.h>
74b00ab754SHans Petter Selasky     #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
75b00ab754SHans Petter Selasky       /*
76b00ab754SHans Petter Selasky        * OK, just include <netinet/ether.h>
77b00ab754SHans Petter Selasky        */
78c761ebcbSSam Leffler       #include <netinet/ether.h>
79b00ab754SHans Petter Selasky     #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
80b00ab754SHans Petter Selasky       /*
81b00ab754SHans Petter Selasky        * OK, just include <sys/ethernet.h>
82b00ab754SHans Petter Selasky        */
83b00ab754SHans Petter Selasky       #include <sys/ethernet.h>
84b00ab754SHans Petter Selasky     #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
85b00ab754SHans Petter Selasky       /*
86b00ab754SHans Petter Selasky        * OK, just include <arpa/inet.h>
87b00ab754SHans Petter Selasky        */
88b00ab754SHans Petter Selasky       #include <arpa/inet.h>
89b00ab754SHans Petter Selasky     #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
90b00ab754SHans Petter Selasky       /*
91b00ab754SHans Petter Selasky        * OK, include <netinet/if_ether.h>, after all the other stuff we
92b00ab754SHans Petter Selasky        * need to include or define for its benefit.
93b00ab754SHans Petter Selasky        */
94b00ab754SHans Petter Selasky       #define NEED_NETINET_IF_ETHER_H
95b00ab754SHans Petter Selasky     #else
96b00ab754SHans Petter Selasky       /*
97b00ab754SHans Petter Selasky        * We'll have to declare it ourselves.
98b00ab754SHans Petter Selasky        * If <netinet/if_ether.h> defines struct ether_addr, include
99b00ab754SHans Petter Selasky        * it.  Otherwise, define it ourselves.
100b00ab754SHans Petter Selasky        */
101b00ab754SHans Petter Selasky       #ifdef HAVE_STRUCT_ETHER_ADDR
102b00ab754SHans Petter Selasky         #define NEED_NETINET_IF_ETHER_H
103b00ab754SHans Petter Selasky       #else /* HAVE_STRUCT_ETHER_ADDR */
104b00ab754SHans Petter Selasky 	struct ether_addr {
105b00ab754SHans Petter Selasky 		unsigned char ether_addr_octet[6];
106b00ab754SHans Petter Selasky 	};
107b00ab754SHans Petter Selasky       #endif /* HAVE_STRUCT_ETHER_ADDR */
108b00ab754SHans Petter Selasky     #endif /* what declares ether_hostton() */
109b00ab754SHans Petter Selasky 
110b00ab754SHans Petter Selasky     #ifdef NEED_NETINET_IF_ETHER_H
111b00ab754SHans Petter Selasky       #include <net/if.h>	/* Needed on some platforms */
112b00ab754SHans Petter Selasky       #include <netinet/in.h>	/* Needed on some platforms */
113b00ab754SHans Petter Selasky       #include <netinet/if_ether.h>
114b00ab754SHans Petter Selasky     #endif /* NEED_NETINET_IF_ETHER_H */
115b00ab754SHans Petter Selasky 
116b00ab754SHans Petter Selasky     #ifndef HAVE_DECL_ETHER_HOSTTON
117b00ab754SHans Petter Selasky       /*
118b00ab754SHans Petter Selasky        * No header declares it, so declare it ourselves.
119b00ab754SHans Petter Selasky        */
120b00ab754SHans Petter Selasky       extern int ether_hostton(const char *, struct ether_addr *);
121b00ab754SHans Petter Selasky     #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
122eb9f0330SBruce M Simpson   #endif /* HAVE_ETHER_HOSTTON */
123b00ab754SHans Petter Selasky 
1248cf6c252SPaul Traina   #include <arpa/inet.h>
125fae05455SBill Fenner   #include <netdb.h>
126ada6f083SXin LI #endif /* _WIN32 */
1278cf6c252SPaul Traina 
1288cf6c252SPaul Traina #include <errno.h>
1298cf6c252SPaul Traina #include <stdlib.h>
1304238c2cdSSam Leffler #include <string.h>
1318cf6c252SPaul Traina #include <stdio.h>
1328cf6c252SPaul Traina 
1338cf6c252SPaul Traina #include "pcap-int.h"
1348cf6c252SPaul Traina 
1356f9cba8fSJoseph Mingrone #include "diag-control.h"
1366f9cba8fSJoseph Mingrone 
1378cf6c252SPaul Traina #include "gencode.h"
138a8e07101SRui Paulo #include <pcap/namedb.h>
139ada6f083SXin LI #include "nametoaddr.h"
1408cf6c252SPaul Traina 
141*afdbf109SJoseph Mingrone #include "thread-local.h"
142*afdbf109SJoseph Mingrone 
1438cf6c252SPaul Traina #ifdef HAVE_OS_PROTO_H
1448cf6c252SPaul Traina #include "os-proto.h"
1458cf6c252SPaul Traina #endif
1468cf6c252SPaul Traina 
1478cf6c252SPaul Traina #ifndef NTOHL
1488cf6c252SPaul Traina #define NTOHL(x) (x) = ntohl(x)
1498cf6c252SPaul Traina #define NTOHS(x) (x) = ntohs(x)
1508cf6c252SPaul Traina #endif
1518cf6c252SPaul Traina 
1528cf6c252SPaul Traina /*
1538cf6c252SPaul Traina  *  Convert host name to internet address.
1548cf6c252SPaul Traina  *  Return 0 upon failure.
155b00ab754SHans Petter Selasky  *  XXX - not thread-safe; don't use it inside libpcap.
1568cf6c252SPaul Traina  */
1578cf6c252SPaul Traina bpf_u_int32 **
pcap_nametoaddr(const char * name)1588cf6c252SPaul Traina pcap_nametoaddr(const char *name)
1598cf6c252SPaul Traina {
1608cf6c252SPaul Traina #ifndef h_addr
1618cf6c252SPaul Traina 	static bpf_u_int32 *hlist[2];
1628cf6c252SPaul Traina #endif
1638cf6c252SPaul Traina 	bpf_u_int32 **p;
1648cf6c252SPaul Traina 	struct hostent *hp;
1658cf6c252SPaul Traina 
1666f9cba8fSJoseph Mingrone 	/*
1676f9cba8fSJoseph Mingrone 	 * gethostbyname() is deprecated on Windows, perhaps because
1686f9cba8fSJoseph Mingrone 	 * it's not thread-safe, or because it doesn't support IPv6,
1696f9cba8fSJoseph Mingrone 	 * or both.
1706f9cba8fSJoseph Mingrone 	 *
1716f9cba8fSJoseph Mingrone 	 * We deprecate pcap_nametoaddr() on all platforms because
1726f9cba8fSJoseph Mingrone 	 * it's not thread-safe; we supply it for backwards compatibility,
1736f9cba8fSJoseph Mingrone 	 * so suppress the deprecation warning.  We could, I guess,
1746f9cba8fSJoseph Mingrone 	 * use getaddrinfo() and construct the array ourselves, but
1756f9cba8fSJoseph Mingrone 	 * that's probably not worth the effort, as that wouldn't make
1766f9cba8fSJoseph Mingrone 	 * this thread-safe - we can't change the API to require that
1776f9cba8fSJoseph Mingrone 	 * our caller free the address array, so we still have to reuse
1786f9cba8fSJoseph Mingrone 	 * a local array.
1796f9cba8fSJoseph Mingrone 	 */
1806f9cba8fSJoseph Mingrone DIAG_OFF_DEPRECATION
1818cf6c252SPaul Traina 	if ((hp = gethostbyname(name)) != NULL) {
1826f9cba8fSJoseph Mingrone DIAG_ON_DEPRECATION
1838cf6c252SPaul Traina #ifndef h_addr
1848cf6c252SPaul Traina 		hlist[0] = (bpf_u_int32 *)hp->h_addr;
1858cf6c252SPaul Traina 		NTOHL(hp->h_addr);
1868cf6c252SPaul Traina 		return hlist;
1878cf6c252SPaul Traina #else
1888cf6c252SPaul Traina 		for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
1898cf6c252SPaul Traina 			NTOHL(**p);
1908cf6c252SPaul Traina 		return (bpf_u_int32 **)hp->h_addr_list;
1918cf6c252SPaul Traina #endif
1928cf6c252SPaul Traina 	}
1938cf6c252SPaul Traina 	else
1948cf6c252SPaul Traina 		return 0;
1958cf6c252SPaul Traina }
1968e1481d1SBill Fenner 
197fae05455SBill Fenner struct addrinfo *
pcap_nametoaddrinfo(const char * name)1988e1481d1SBill Fenner pcap_nametoaddrinfo(const char *name)
199fae05455SBill Fenner {
200fae05455SBill Fenner 	struct addrinfo hints, *res;
201fae05455SBill Fenner 	int error;
202fae05455SBill Fenner 
203fae05455SBill Fenner 	memset(&hints, 0, sizeof(hints));
204fae05455SBill Fenner 	hints.ai_family = PF_UNSPEC;
205fae05455SBill Fenner 	hints.ai_socktype = SOCK_STREAM;	/*not really*/
206c761ebcbSSam Leffler 	hints.ai_protocol = IPPROTO_TCP;	/*not really*/
207fae05455SBill Fenner 	error = getaddrinfo(name, NULL, &hints, &res);
208fae05455SBill Fenner 	if (error)
209fae05455SBill Fenner 		return NULL;
210fae05455SBill Fenner 	else
211fae05455SBill Fenner 		return res;
212fae05455SBill Fenner }
2138cf6c252SPaul Traina 
2148cf6c252SPaul Traina /*
2158cf6c252SPaul Traina  *  Convert net name to internet address.
2168cf6c252SPaul Traina  *  Return 0 upon failure.
217b00ab754SHans Petter Selasky  *  XXX - not guaranteed to be thread-safe!  See below for platforms
218b00ab754SHans Petter Selasky  *  on which it is thread-safe and on which it isn't.
2198cf6c252SPaul Traina  */
2206f9cba8fSJoseph Mingrone #if defined(_WIN32) || defined(__CYGWIN__)
2218cf6c252SPaul Traina bpf_u_int32
pcap_nametonetaddr(const char * name _U_)2226f9cba8fSJoseph Mingrone pcap_nametonetaddr(const char *name _U_)
2238cf6c252SPaul Traina {
224eb9f0330SBruce M Simpson 	/*
225eb9f0330SBruce M Simpson 	 * There's no "getnetbyname()" on Windows.
226ada6f083SXin LI 	 *
227ada6f083SXin LI 	 * XXX - I guess we could use the BSD code to read
228ada6f083SXin LI 	 * C:\Windows\System32\drivers\etc/networks, assuming
229ada6f083SXin LI 	 * that's its home on all the versions of Windows
230ada6f083SXin LI 	 * we use, but that file probably just has the loopback
231ada6f083SXin LI 	 * network on 127/24 on 99 44/100% of Windows machines.
232ada6f083SXin LI 	 *
233ada6f083SXin LI 	 * (Heck, these days it probably just has that on 99 44/100%
234ada6f083SXin LI 	 * of *UN*X* machines.)
235eb9f0330SBruce M Simpson 	 */
236eb9f0330SBruce M Simpson 	return 0;
2376f9cba8fSJoseph Mingrone }
2386f9cba8fSJoseph Mingrone #else /* _WIN32 */
2396f9cba8fSJoseph Mingrone bpf_u_int32
pcap_nametonetaddr(const char * name)2406f9cba8fSJoseph Mingrone pcap_nametonetaddr(const char *name)
2416f9cba8fSJoseph Mingrone {
242b00ab754SHans Petter Selasky 	/*
243b00ab754SHans Petter Selasky 	 * UN*X.
244b00ab754SHans Petter Selasky 	 */
245b00ab754SHans Petter Selasky 	struct netent *np;
246b00ab754SHans Petter Selasky   #if defined(HAVE_LINUX_GETNETBYNAME_R)
247b00ab754SHans Petter Selasky 	/*
248b00ab754SHans Petter Selasky 	 * We have Linux's reentrant getnetbyname_r().
249b00ab754SHans Petter Selasky 	 */
250b00ab754SHans Petter Selasky 	struct netent result_buf;
251b00ab754SHans Petter Selasky 	char buf[1024];	/* arbitrary size */
252b00ab754SHans Petter Selasky 	int h_errnoval;
253b00ab754SHans Petter Selasky 	int err;
254b00ab754SHans Petter Selasky 
25557e22627SCy Schubert 	/*
25657e22627SCy Schubert 	 * Apparently, the man page at
25757e22627SCy Schubert 	 *
25857e22627SCy Schubert 	 *    http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
25957e22627SCy Schubert 	 *
26057e22627SCy Schubert 	 * lies when it says
26157e22627SCy Schubert 	 *
26257e22627SCy Schubert 	 *    If the function call successfully obtains a network record,
26357e22627SCy Schubert 	 *    then *result is set pointing to result_buf; otherwise, *result
26457e22627SCy Schubert 	 *    is set to NULL.
26557e22627SCy Schubert 	 *
26657e22627SCy Schubert 	 * and, in fact, at least in some versions of GNU libc, it does
26757e22627SCy Schubert 	 * *not* always get set if getnetbyname_r() succeeds.
26857e22627SCy Schubert 	 */
26957e22627SCy Schubert 	np = NULL;
270b00ab754SHans Petter Selasky 	err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
271b00ab754SHans Petter Selasky 	    &h_errnoval);
272b00ab754SHans Petter Selasky 	if (err != 0) {
273b00ab754SHans Petter Selasky 		/*
274b00ab754SHans Petter Selasky 		 * XXX - dynamically allocate the buffer, and make it
275b00ab754SHans Petter Selasky 		 * bigger if we get ERANGE back?
276b00ab754SHans Petter Selasky 		 */
277b00ab754SHans Petter Selasky 		return 0;
278b00ab754SHans Petter Selasky 	}
279b00ab754SHans Petter Selasky   #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
280b00ab754SHans Petter Selasky 	/*
281b00ab754SHans Petter Selasky 	 * We have Solaris's and IRIX's reentrant getnetbyname_r().
282b00ab754SHans Petter Selasky 	 */
283b00ab754SHans Petter Selasky 	struct netent result_buf;
284b00ab754SHans Petter Selasky 	char buf[1024];	/* arbitrary size */
285b00ab754SHans Petter Selasky 
286b00ab754SHans Petter Selasky 	np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
287b00ab754SHans Petter Selasky   #elif defined(HAVE_AIX_GETNETBYNAME_R)
288b00ab754SHans Petter Selasky 	/*
289b00ab754SHans Petter Selasky 	 * We have AIX's reentrant getnetbyname_r().
290b00ab754SHans Petter Selasky 	 */
291b00ab754SHans Petter Selasky 	struct netent result_buf;
292b00ab754SHans Petter Selasky 	struct netent_data net_data;
293b00ab754SHans Petter Selasky 
294b00ab754SHans Petter Selasky 	if (getnetbyname_r(name, &result_buf, &net_data) == -1)
295b00ab754SHans Petter Selasky 		np = NULL;
296b00ab754SHans Petter Selasky 	else
297b00ab754SHans Petter Selasky 		np = &result_buf;
298b00ab754SHans Petter Selasky   #else
299b00ab754SHans Petter Selasky 	/*
300b00ab754SHans Petter Selasky 	 * We don't have any getnetbyname_r(); either we have a
301b00ab754SHans Petter Selasky 	 * getnetbyname() that uses thread-specific data, in which
302b00ab754SHans Petter Selasky 	 * case we're thread-safe (sufficiently recent FreeBSD,
303b00ab754SHans Petter Selasky 	 * sufficiently recent Darwin-based OS, sufficiently recent
304b00ab754SHans Petter Selasky 	 * HP-UX, sufficiently recent Tru64 UNIX), or we have the
305b00ab754SHans Petter Selasky 	 * traditional getnetbyname() (everything else, including
306b00ab754SHans Petter Selasky 	 * current NetBSD and OpenBSD), in which case we're not
307b00ab754SHans Petter Selasky 	 * thread-safe.
308b00ab754SHans Petter Selasky 	 */
309b00ab754SHans Petter Selasky 	np = getnetbyname(name);
310eb9f0330SBruce M Simpson   #endif
311b00ab754SHans Petter Selasky 	if (np != NULL)
312b00ab754SHans Petter Selasky 		return np->n_net;
313b00ab754SHans Petter Selasky 	else
314b00ab754SHans Petter Selasky 		return 0;
3158cf6c252SPaul Traina }
3166f9cba8fSJoseph Mingrone #endif /* _WIN32 */
3178cf6c252SPaul Traina 
3188cf6c252SPaul Traina /*
3198cf6c252SPaul Traina  * Convert a port name to its port and protocol numbers.
3208cf6c252SPaul Traina  * We assume only TCP or UDP.
3218cf6c252SPaul Traina  * Return 0 upon failure.
3228cf6c252SPaul Traina  */
3238cf6c252SPaul Traina int
pcap_nametoport(const char * name,int * port,int * proto)3248cf6c252SPaul Traina pcap_nametoport(const char *name, int *port, int *proto)
3258cf6c252SPaul Traina {
326b00ab754SHans Petter Selasky 	struct addrinfo hints, *res, *ai;
327b00ab754SHans Petter Selasky 	int error;
328b00ab754SHans Petter Selasky 	struct sockaddr_in *in4;
329b00ab754SHans Petter Selasky #ifdef INET6
330b00ab754SHans Petter Selasky 	struct sockaddr_in6 *in6;
331b00ab754SHans Petter Selasky #endif
332eb9f0330SBruce M Simpson 	int tcp_port = -1;
333eb9f0330SBruce M Simpson 	int udp_port = -1;
3348cf6c252SPaul Traina 
3358cf6c252SPaul Traina 	/*
336b00ab754SHans Petter Selasky 	 * We check for both TCP and UDP in case there are
337b00ab754SHans Petter Selasky 	 * ambiguous entries.
338b00ab754SHans Petter Selasky 	 */
339b00ab754SHans Petter Selasky 	memset(&hints, 0, sizeof(hints));
340b00ab754SHans Petter Selasky 	hints.ai_family = PF_UNSPEC;
341b00ab754SHans Petter Selasky 	hints.ai_socktype = SOCK_STREAM;
342b00ab754SHans Petter Selasky 	hints.ai_protocol = IPPROTO_TCP;
343b00ab754SHans Petter Selasky 	error = getaddrinfo(NULL, name, &hints, &res);
344b00ab754SHans Petter Selasky 	if (error != 0) {
34557e22627SCy Schubert 		if (error != EAI_NONAME &&
34657e22627SCy Schubert 		    error != EAI_SERVICE) {
347b00ab754SHans Petter Selasky 			/*
348b00ab754SHans Petter Selasky 			 * This is a real error, not just "there's
349b00ab754SHans Petter Selasky 			 * no such service name".
350b00ab754SHans Petter Selasky 			 * XXX - this doesn't return an error string.
351b00ab754SHans Petter Selasky 			 */
352b00ab754SHans Petter Selasky 			return 0;
353b00ab754SHans Petter Selasky 		}
354b00ab754SHans Petter Selasky 	} else {
355b00ab754SHans Petter Selasky 		/*
356b00ab754SHans Petter Selasky 		 * OK, we found it.  Did it find anything?
357b00ab754SHans Petter Selasky 		 */
358b00ab754SHans Petter Selasky 		for (ai = res; ai != NULL; ai = ai->ai_next) {
359b00ab754SHans Petter Selasky 			/*
360b00ab754SHans Petter Selasky 			 * Does it have an address?
361b00ab754SHans Petter Selasky 			 */
362b00ab754SHans Petter Selasky 			if (ai->ai_addr != NULL) {
363b00ab754SHans Petter Selasky 				/*
364b00ab754SHans Petter Selasky 				 * Yes.  Get a port number; we're done.
365b00ab754SHans Petter Selasky 				 */
366b00ab754SHans Petter Selasky 				if (ai->ai_addr->sa_family == AF_INET) {
367b00ab754SHans Petter Selasky 					in4 = (struct sockaddr_in *)ai->ai_addr;
368b00ab754SHans Petter Selasky 					tcp_port = ntohs(in4->sin_port);
369b00ab754SHans Petter Selasky 					break;
370b00ab754SHans Petter Selasky 				}
371b00ab754SHans Petter Selasky #ifdef INET6
372b00ab754SHans Petter Selasky 				if (ai->ai_addr->sa_family == AF_INET6) {
373b00ab754SHans Petter Selasky 					in6 = (struct sockaddr_in6 *)ai->ai_addr;
374b00ab754SHans Petter Selasky 					tcp_port = ntohs(in6->sin6_port);
375b00ab754SHans Petter Selasky 					break;
376b00ab754SHans Petter Selasky 				}
377b00ab754SHans Petter Selasky #endif
378b00ab754SHans Petter Selasky 			}
379b00ab754SHans Petter Selasky 		}
380b00ab754SHans Petter Selasky 		freeaddrinfo(res);
381b00ab754SHans Petter Selasky 	}
382b00ab754SHans Petter Selasky 
383b00ab754SHans Petter Selasky 	memset(&hints, 0, sizeof(hints));
384b00ab754SHans Petter Selasky 	hints.ai_family = PF_UNSPEC;
385b00ab754SHans Petter Selasky 	hints.ai_socktype = SOCK_DGRAM;
386b00ab754SHans Petter Selasky 	hints.ai_protocol = IPPROTO_UDP;
387b00ab754SHans Petter Selasky 	error = getaddrinfo(NULL, name, &hints, &res);
388b00ab754SHans Petter Selasky 	if (error != 0) {
38957e22627SCy Schubert 		if (error != EAI_NONAME &&
39057e22627SCy Schubert 		    error != EAI_SERVICE) {
391b00ab754SHans Petter Selasky 			/*
392b00ab754SHans Petter Selasky 			 * This is a real error, not just "there's
393b00ab754SHans Petter Selasky 			 * no such service name".
394b00ab754SHans Petter Selasky 			 * XXX - this doesn't return an error string.
395b00ab754SHans Petter Selasky 			 */
396b00ab754SHans Petter Selasky 			return 0;
397b00ab754SHans Petter Selasky 		}
398b00ab754SHans Petter Selasky 	} else {
399b00ab754SHans Petter Selasky 		/*
400b00ab754SHans Petter Selasky 		 * OK, we found it.  Did it find anything?
401b00ab754SHans Petter Selasky 		 */
402b00ab754SHans Petter Selasky 		for (ai = res; ai != NULL; ai = ai->ai_next) {
403b00ab754SHans Petter Selasky 			/*
404b00ab754SHans Petter Selasky 			 * Does it have an address?
405b00ab754SHans Petter Selasky 			 */
406b00ab754SHans Petter Selasky 			if (ai->ai_addr != NULL) {
407b00ab754SHans Petter Selasky 				/*
408b00ab754SHans Petter Selasky 				 * Yes.  Get a port number; we're done.
409b00ab754SHans Petter Selasky 				 */
410b00ab754SHans Petter Selasky 				if (ai->ai_addr->sa_family == AF_INET) {
411b00ab754SHans Petter Selasky 					in4 = (struct sockaddr_in *)ai->ai_addr;
412b00ab754SHans Petter Selasky 					udp_port = ntohs(in4->sin_port);
413b00ab754SHans Petter Selasky 					break;
414b00ab754SHans Petter Selasky 				}
415b00ab754SHans Petter Selasky #ifdef INET6
416b00ab754SHans Petter Selasky 				if (ai->ai_addr->sa_family == AF_INET6) {
417b00ab754SHans Petter Selasky 					in6 = (struct sockaddr_in6 *)ai->ai_addr;
418b00ab754SHans Petter Selasky 					udp_port = ntohs(in6->sin6_port);
419b00ab754SHans Petter Selasky 					break;
420b00ab754SHans Petter Selasky 				}
421b00ab754SHans Petter Selasky #endif
422b00ab754SHans Petter Selasky 			}
423b00ab754SHans Petter Selasky 		}
424b00ab754SHans Petter Selasky 		freeaddrinfo(res);
425b00ab754SHans Petter Selasky 	}
426b00ab754SHans Petter Selasky 
427b00ab754SHans Petter Selasky 	/*
4288cf6c252SPaul Traina 	 * We need to check /etc/services for ambiguous entries.
429b00ab754SHans Petter Selasky 	 * If we find an ambiguous entry, and it has the
4308cf6c252SPaul Traina 	 * same port number, change the proto to PROTO_UNDEF
4318cf6c252SPaul Traina 	 * so both TCP and UDP will be checked.
4328cf6c252SPaul Traina 	 */
433eb9f0330SBruce M Simpson 	if (tcp_port >= 0) {
434eb9f0330SBruce M Simpson 		*port = tcp_port;
435eb9f0330SBruce M Simpson 		*proto = IPPROTO_TCP;
436eb9f0330SBruce M Simpson 		if (udp_port >= 0) {
437eb9f0330SBruce M Simpson 			if (udp_port == tcp_port)
438eb9f0330SBruce M Simpson 				*proto = PROTO_UNDEF;
4398cf6c252SPaul Traina #ifdef notdef
440eb9f0330SBruce M Simpson 			else
4418cf6c252SPaul Traina 				/* Can't handle ambiguous names that refer
4428cf6c252SPaul Traina 				   to different port numbers. */
4438cf6c252SPaul Traina 				warning("ambiguous port %s in /etc/services",
4448cf6c252SPaul Traina 					name);
4458cf6c252SPaul Traina #endif
4468cf6c252SPaul Traina 		}
4478cf6c252SPaul Traina 		return 1;
4488cf6c252SPaul Traina 	}
449eb9f0330SBruce M Simpson 	if (udp_port >= 0) {
450eb9f0330SBruce M Simpson 		*port = udp_port;
451eb9f0330SBruce M Simpson 		*proto = IPPROTO_UDP;
452eb9f0330SBruce M Simpson 		return 1;
453eb9f0330SBruce M Simpson 	}
4548cf6c252SPaul Traina #if defined(ultrix) || defined(__osf__)
4558cf6c252SPaul Traina 	/* Special hack in case NFS isn't in /etc/services */
4568cf6c252SPaul Traina 	if (strcmp(name, "nfs") == 0) {
4578cf6c252SPaul Traina 		*port = 2049;
4588cf6c252SPaul Traina 		*proto = PROTO_UNDEF;
4598cf6c252SPaul Traina 		return 1;
4608cf6c252SPaul Traina 	}
4618cf6c252SPaul Traina #endif
4628cf6c252SPaul Traina 	return 0;
4638cf6c252SPaul Traina }
4648cf6c252SPaul Traina 
4654238c2cdSSam Leffler /*
4664238c2cdSSam Leffler  * Convert a string in the form PPP-PPP, where correspond to ports, to
4674238c2cdSSam Leffler  * a starting and ending port in a port range.
4684238c2cdSSam Leffler  * Return 0 on failure.
4694238c2cdSSam Leffler  */
4704238c2cdSSam Leffler int
pcap_nametoportrange(const char * name,int * port1,int * port2,int * proto)4714238c2cdSSam Leffler pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
4724238c2cdSSam Leffler {
4734238c2cdSSam Leffler 	char *off, *cpy;
4744238c2cdSSam Leffler 	int save_proto;
4754238c2cdSSam Leffler 
4764238c2cdSSam Leffler 	if ((cpy = strdup(name)) == NULL)
4774238c2cdSSam Leffler 		return 0;
4784238c2cdSSam Leffler 
4794238c2cdSSam Leffler 	if ((off = strchr(cpy, '-')) == NULL) {
4804238c2cdSSam Leffler 		free(cpy);
4814238c2cdSSam Leffler 		return 0;
4824238c2cdSSam Leffler 	}
4834238c2cdSSam Leffler 
4844238c2cdSSam Leffler 	*off = '\0';
4854238c2cdSSam Leffler 
4864238c2cdSSam Leffler 	if (pcap_nametoport(cpy, port1, proto) == 0) {
4874238c2cdSSam Leffler 		free(cpy);
4884238c2cdSSam Leffler 		return 0;
4894238c2cdSSam Leffler 	}
4904238c2cdSSam Leffler 	save_proto = *proto;
4914238c2cdSSam Leffler 
4924238c2cdSSam Leffler 	if (pcap_nametoport(off + 1, port2, proto) == 0) {
4934238c2cdSSam Leffler 		free(cpy);
4944238c2cdSSam Leffler 		return 0;
4954238c2cdSSam Leffler 	}
496edc89b24SXin LI 	free(cpy);
4974238c2cdSSam Leffler 
4984238c2cdSSam Leffler 	if (*proto != save_proto)
4994238c2cdSSam Leffler 		*proto = PROTO_UNDEF;
5004238c2cdSSam Leffler 
5014238c2cdSSam Leffler 	return 1;
5024238c2cdSSam Leffler }
5034238c2cdSSam Leffler 
504b00ab754SHans Petter Selasky /*
505b00ab754SHans Petter Selasky  * XXX - not guaranteed to be thread-safe!  See below for platforms
506b00ab754SHans Petter Selasky  * on which it is thread-safe and on which it isn't.
507b00ab754SHans Petter Selasky  */
5088cf6c252SPaul Traina int
pcap_nametoproto(const char * str)5098cf6c252SPaul Traina pcap_nametoproto(const char *str)
5108cf6c252SPaul Traina {
5118cf6c252SPaul Traina 	struct protoent *p;
512b00ab754SHans Petter Selasky   #if defined(HAVE_LINUX_GETNETBYNAME_R)
513b00ab754SHans Petter Selasky 	/*
514b00ab754SHans Petter Selasky 	 * We have Linux's reentrant getprotobyname_r().
515b00ab754SHans Petter Selasky 	 */
516b00ab754SHans Petter Selasky 	struct protoent result_buf;
517b00ab754SHans Petter Selasky 	char buf[1024];	/* arbitrary size */
518b00ab754SHans Petter Selasky 	int err;
5198cf6c252SPaul Traina 
520b00ab754SHans Petter Selasky 	err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
521b00ab754SHans Petter Selasky 	if (err != 0) {
522b00ab754SHans Petter Selasky 		/*
523b00ab754SHans Petter Selasky 		 * XXX - dynamically allocate the buffer, and make it
524b00ab754SHans Petter Selasky 		 * bigger if we get ERANGE back?
525b00ab754SHans Petter Selasky 		 */
526b00ab754SHans Petter Selasky 		return 0;
527b00ab754SHans Petter Selasky 	}
528b00ab754SHans Petter Selasky   #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
529b00ab754SHans Petter Selasky 	/*
530b00ab754SHans Petter Selasky 	 * We have Solaris's and IRIX's reentrant getprotobyname_r().
531b00ab754SHans Petter Selasky 	 */
532b00ab754SHans Petter Selasky 	struct protoent result_buf;
533b00ab754SHans Petter Selasky 	char buf[1024];	/* arbitrary size */
534b00ab754SHans Petter Selasky 
535b00ab754SHans Petter Selasky 	p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
536b00ab754SHans Petter Selasky   #elif defined(HAVE_AIX_GETNETBYNAME_R)
537b00ab754SHans Petter Selasky 	/*
538b00ab754SHans Petter Selasky 	 * We have AIX's reentrant getprotobyname_r().
539b00ab754SHans Petter Selasky 	 */
540b00ab754SHans Petter Selasky 	struct protoent result_buf;
541b00ab754SHans Petter Selasky 	struct protoent_data proto_data;
542b00ab754SHans Petter Selasky 
543b00ab754SHans Petter Selasky 	if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
544b00ab754SHans Petter Selasky 		p = NULL;
545b00ab754SHans Petter Selasky 	else
546b00ab754SHans Petter Selasky 		p = &result_buf;
547b00ab754SHans Petter Selasky   #else
548b00ab754SHans Petter Selasky 	/*
549b00ab754SHans Petter Selasky 	 * We don't have any getprotobyname_r(); either we have a
550b00ab754SHans Petter Selasky 	 * getprotobyname() that uses thread-specific data, in which
551b00ab754SHans Petter Selasky 	 * case we're thread-safe (sufficiently recent FreeBSD,
552b00ab754SHans Petter Selasky 	 * sufficiently recent Darwin-based OS, sufficiently recent
553b00ab754SHans Petter Selasky 	 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
554b00ab754SHans Petter Selasky 	 * the traditional getprotobyname() (everything else, including
555b00ab754SHans Petter Selasky 	 * current NetBSD and OpenBSD), in which case we're not
556b00ab754SHans Petter Selasky 	 * thread-safe.
557b00ab754SHans Petter Selasky 	 */
5588cf6c252SPaul Traina 	p = getprotobyname(str);
559b00ab754SHans Petter Selasky   #endif
5608cf6c252SPaul Traina 	if (p != 0)
5618cf6c252SPaul Traina 		return p->p_proto;
5628cf6c252SPaul Traina 	else
5638cf6c252SPaul Traina 		return PROTO_UNDEF;
5648cf6c252SPaul Traina }
5658cf6c252SPaul Traina 
5668cf6c252SPaul Traina #include "ethertype.h"
5678cf6c252SPaul Traina 
5688cf6c252SPaul Traina struct eproto {
5695357e0feSMax Laier 	const char *s;
5708cf6c252SPaul Traina 	u_short p;
5718cf6c252SPaul Traina };
5728cf6c252SPaul Traina 
573ada6f083SXin LI /*
574ada6f083SXin LI  * Static data base of ether protocol types.
575ada6f083SXin LI  * tcpdump used to import this, and it's declared as an export on
576ada6f083SXin LI  * Debian, at least, so make it a public symbol, even though we
577ada6f083SXin LI  * don't officially export it by declaring it in a header file.
578ada6f083SXin LI  * (Programs *should* do this themselves, as tcpdump now does.)
579b00ab754SHans Petter Selasky  *
580b00ab754SHans Petter Selasky  * We declare it here, right before defining it, to squelch any
581b00ab754SHans Petter Selasky  * warnings we might get from compilers about the lack of a
582b00ab754SHans Petter Selasky  * declaration.
583ada6f083SXin LI  */
584b00ab754SHans Petter Selasky PCAP_API struct eproto eproto_db[];
585ada6f083SXin LI PCAP_API_DEF struct eproto eproto_db[] = {
5866f9cba8fSJoseph Mingrone 	{ "aarp", ETHERTYPE_AARP },
5876f9cba8fSJoseph Mingrone 	{ "arp", ETHERTYPE_ARP },
5886f9cba8fSJoseph Mingrone 	{ "atalk", ETHERTYPE_ATALK },
5896f9cba8fSJoseph Mingrone 	{ "decnet", ETHERTYPE_DN },
5908cf6c252SPaul Traina 	{ "ip", ETHERTYPE_IP },
591fae05455SBill Fenner #ifdef INET6
592fae05455SBill Fenner 	{ "ip6", ETHERTYPE_IPV6 },
593fae05455SBill Fenner #endif
5946f9cba8fSJoseph Mingrone 	{ "lat", ETHERTYPE_LAT },
5956f9cba8fSJoseph Mingrone 	{ "loopback", ETHERTYPE_LOOPBACK },
5968cf6c252SPaul Traina 	{ "mopdl", ETHERTYPE_MOPDL },
5978cf6c252SPaul Traina 	{ "moprc", ETHERTYPE_MOPRC },
5986f9cba8fSJoseph Mingrone 	{ "rarp", ETHERTYPE_REVARP },
5998cf6c252SPaul Traina 	{ "sca", ETHERTYPE_SCA },
6008cf6c252SPaul Traina 	{ (char *)0, 0 }
6018cf6c252SPaul Traina };
6028cf6c252SPaul Traina 
6038cf6c252SPaul Traina int
pcap_nametoeproto(const char * s)6048cf6c252SPaul Traina pcap_nametoeproto(const char *s)
6058cf6c252SPaul Traina {
6068cf6c252SPaul Traina 	struct eproto *p = eproto_db;
6078cf6c252SPaul Traina 
6088cf6c252SPaul Traina 	while (p->s != 0) {
6098cf6c252SPaul Traina 		if (strcmp(p->s, s) == 0)
6108cf6c252SPaul Traina 			return p->p;
6118cf6c252SPaul Traina 		p += 1;
6128cf6c252SPaul Traina 	}
6138cf6c252SPaul Traina 	return PROTO_UNDEF;
6148cf6c252SPaul Traina }
6158cf6c252SPaul Traina 
616c761ebcbSSam Leffler #include "llc.h"
617c761ebcbSSam Leffler 
618c761ebcbSSam Leffler /* Static data base of LLC values. */
619c761ebcbSSam Leffler static struct eproto llc_db[] = {
620c761ebcbSSam Leffler 	{ "iso", LLCSAP_ISONS },
621c761ebcbSSam Leffler 	{ "stp", LLCSAP_8021D },
622c761ebcbSSam Leffler 	{ "ipx", LLCSAP_IPX },
623c761ebcbSSam Leffler 	{ "netbeui", LLCSAP_NETBEUI },
624c761ebcbSSam Leffler 	{ (char *)0, 0 }
625c761ebcbSSam Leffler };
626c761ebcbSSam Leffler 
627c761ebcbSSam Leffler int
pcap_nametollc(const char * s)628c761ebcbSSam Leffler pcap_nametollc(const char *s)
629c761ebcbSSam Leffler {
630c761ebcbSSam Leffler 	struct eproto *p = llc_db;
631c761ebcbSSam Leffler 
632c761ebcbSSam Leffler 	while (p->s != 0) {
633c761ebcbSSam Leffler 		if (strcmp(p->s, s) == 0)
634c761ebcbSSam Leffler 			return p->p;
635c761ebcbSSam Leffler 		p += 1;
636c761ebcbSSam Leffler 	}
637c761ebcbSSam Leffler 	return PROTO_UNDEF;
638c761ebcbSSam Leffler }
639c761ebcbSSam Leffler 
640b00ab754SHans Petter Selasky /* Hex digit to 8-bit unsigned integer. */
641b00ab754SHans Petter Selasky static inline u_char
xdtoi(u_char c)642b00ab754SHans Petter Selasky xdtoi(u_char c)
6438cf6c252SPaul Traina {
6446f9cba8fSJoseph Mingrone 	if (c >= '0' && c <= '9')
645b00ab754SHans Petter Selasky 		return (u_char)(c - '0');
6466f9cba8fSJoseph Mingrone 	else if (c >= 'a' && c <= 'f')
647b00ab754SHans Petter Selasky 		return (u_char)(c - 'a' + 10);
6488cf6c252SPaul Traina 	else
649b00ab754SHans Petter Selasky 		return (u_char)(c - 'A' + 10);
6508cf6c252SPaul Traina }
6518cf6c252SPaul Traina 
6528cf6c252SPaul Traina int
__pcap_atoin(const char * s,bpf_u_int32 * addr)6538cf6c252SPaul Traina __pcap_atoin(const char *s, bpf_u_int32 *addr)
6548cf6c252SPaul Traina {
6558cf6c252SPaul Traina 	u_int n;
6568cf6c252SPaul Traina 	int len;
6578cf6c252SPaul Traina 
6588cf6c252SPaul Traina 	*addr = 0;
6598cf6c252SPaul Traina 	len = 0;
660b00ab754SHans Petter Selasky 	for (;;) {
6618cf6c252SPaul Traina 		n = 0;
66231bfaf17SCy Schubert 		while (*s && *s != '.') {
66331bfaf17SCy Schubert 			if (n > 25) {
66431bfaf17SCy Schubert 				/* The result will be > 255 */
66531bfaf17SCy Schubert 				return -1;
66631bfaf17SCy Schubert 			}
6678cf6c252SPaul Traina 			n = n * 10 + *s++ - '0';
66831bfaf17SCy Schubert 		}
66931bfaf17SCy Schubert 		if (n > 255)
67031bfaf17SCy Schubert 			return -1;
6718cf6c252SPaul Traina 		*addr <<= 8;
6728cf6c252SPaul Traina 		*addr |= n & 0xff;
6738cf6c252SPaul Traina 		len += 8;
6748cf6c252SPaul Traina 		if (*s == '\0')
6758cf6c252SPaul Traina 			return len;
6768cf6c252SPaul Traina 		++s;
6778cf6c252SPaul Traina 	}
6788cf6c252SPaul Traina 	/* NOTREACHED */
6798cf6c252SPaul Traina }
6808cf6c252SPaul Traina 
6818cf6c252SPaul Traina int
__pcap_atodn(const char * s,bpf_u_int32 * addr)6828cf6c252SPaul Traina __pcap_atodn(const char *s, bpf_u_int32 *addr)
6838cf6c252SPaul Traina {
6848cf6c252SPaul Traina #define AREASHIFT 10
6858cf6c252SPaul Traina #define AREAMASK 0176000
6868cf6c252SPaul Traina #define NODEMASK 01777
6878cf6c252SPaul Traina 
6888cf6c252SPaul Traina 	u_int node, area;
6898cf6c252SPaul Traina 
6905357e0feSMax Laier 	if (sscanf(s, "%d.%d", &area, &node) != 2)
691ada6f083SXin LI 		return(0);
6928cf6c252SPaul Traina 
6938cf6c252SPaul Traina 	*addr = (area << AREASHIFT) & AREAMASK;
6948cf6c252SPaul Traina 	*addr |= (node & NODEMASK);
6958cf6c252SPaul Traina 
6968cf6c252SPaul Traina 	return(32);
6978cf6c252SPaul Traina }
6988cf6c252SPaul Traina 
6998cf6c252SPaul Traina /*
700a8e07101SRui Paulo  * Convert 's', which can have the one of the forms:
701a8e07101SRui Paulo  *
702a8e07101SRui Paulo  *	"xx:xx:xx:xx:xx:xx"
703a8e07101SRui Paulo  *	"xx.xx.xx.xx.xx.xx"
704a8e07101SRui Paulo  *	"xx-xx-xx-xx-xx-xx"
705a8e07101SRui Paulo  *	"xxxx.xxxx.xxxx"
706a8e07101SRui Paulo  *	"xxxxxxxxxxxx"
707a8e07101SRui Paulo  *
708a8e07101SRui Paulo  * (or various mixes of ':', '.', and '-') into a new
7098cf6c252SPaul Traina  * ethernet address.  Assumes 's' is well formed.
7108cf6c252SPaul Traina  */
7118cf6c252SPaul Traina u_char *
pcap_ether_aton(const char * s)7128cf6c252SPaul Traina pcap_ether_aton(const char *s)
7138cf6c252SPaul Traina {
7148cf6c252SPaul Traina 	register u_char *ep, *e;
715b00ab754SHans Petter Selasky 	register u_char d;
7168cf6c252SPaul Traina 
7178cf6c252SPaul Traina 	e = ep = (u_char *)malloc(6);
718681ed54cSXin LI 	if (e == NULL)
719681ed54cSXin LI 		return (NULL);
7208cf6c252SPaul Traina 
7218cf6c252SPaul Traina 	while (*s) {
722a8e07101SRui Paulo 		if (*s == ':' || *s == '.' || *s == '-')
7238cf6c252SPaul Traina 			s += 1;
7248cf6c252SPaul Traina 		d = xdtoi(*s++);
7256f9cba8fSJoseph Mingrone 		if (PCAP_ISXDIGIT(*s)) {
7268cf6c252SPaul Traina 			d <<= 4;
7278cf6c252SPaul Traina 			d |= xdtoi(*s++);
7288cf6c252SPaul Traina 		}
7298cf6c252SPaul Traina 		*ep++ = d;
7308cf6c252SPaul Traina 	}
7318cf6c252SPaul Traina 
7328cf6c252SPaul Traina 	return (e);
7338cf6c252SPaul Traina }
7348cf6c252SPaul Traina 
7358cf6c252SPaul Traina #ifndef HAVE_ETHER_HOSTTON
736b00ab754SHans Petter Selasky /*
737b00ab754SHans Petter Selasky  * Roll our own.
738*afdbf109SJoseph Mingrone  *
739*afdbf109SJoseph Mingrone  * This should be thread-safe, as we define the static variables
740*afdbf109SJoseph Mingrone  * we use to be thread-local, and as pcap_next_etherent() does so
741*afdbf109SJoseph Mingrone  * as well.
742b00ab754SHans Petter Selasky  */
7438cf6c252SPaul Traina u_char *
pcap_ether_hostton(const char * name)7448cf6c252SPaul Traina pcap_ether_hostton(const char *name)
7458cf6c252SPaul Traina {
7468cf6c252SPaul Traina 	register struct pcap_etherent *ep;
7478cf6c252SPaul Traina 	register u_char *ap;
748*afdbf109SJoseph Mingrone 	static thread_local FILE *fp = NULL;
749*afdbf109SJoseph Mingrone 	static thread_local int init = 0;
7508cf6c252SPaul Traina 
7518cf6c252SPaul Traina 	if (!init) {
7528cf6c252SPaul Traina 		fp = fopen(PCAP_ETHERS_FILE, "r");
7538cf6c252SPaul Traina 		++init;
7548cf6c252SPaul Traina 		if (fp == NULL)
7558cf6c252SPaul Traina 			return (NULL);
7568cf6c252SPaul Traina 	} else if (fp == NULL)
7578cf6c252SPaul Traina 		return (NULL);
7588cf6c252SPaul Traina 	else
7598cf6c252SPaul Traina 		rewind(fp);
7608cf6c252SPaul Traina 
7618cf6c252SPaul Traina 	while ((ep = pcap_next_etherent(fp)) != NULL) {
7628cf6c252SPaul Traina 		if (strcmp(ep->name, name) == 0) {
7638cf6c252SPaul Traina 			ap = (u_char *)malloc(6);
7648cf6c252SPaul Traina 			if (ap != NULL) {
7658cf6c252SPaul Traina 				memcpy(ap, ep->addr, 6);
7668cf6c252SPaul Traina 				return (ap);
7678cf6c252SPaul Traina 			}
7688cf6c252SPaul Traina 			break;
7698cf6c252SPaul Traina 		}
7708cf6c252SPaul Traina 	}
7718cf6c252SPaul Traina 	return (NULL);
7728cf6c252SPaul Traina }
7738cf6c252SPaul Traina #else
774b00ab754SHans Petter Selasky /*
775b00ab754SHans Petter Selasky  * Use the OS-supplied routine.
776b00ab754SHans Petter Selasky  * This *should* be thread-safe; the API doesn't have a static buffer.
777b00ab754SHans Petter Selasky  */
7788cf6c252SPaul Traina u_char *
pcap_ether_hostton(const char * name)7798cf6c252SPaul Traina pcap_ether_hostton(const char *name)
7808cf6c252SPaul Traina {
7818cf6c252SPaul Traina 	register u_char *ap;
7828cf6c252SPaul Traina 	u_char a[6];
7836f9cba8fSJoseph Mingrone 	char namebuf[1024];
7848cf6c252SPaul Traina 
7856f9cba8fSJoseph Mingrone 	/*
7866f9cba8fSJoseph Mingrone 	 * In AIX 7.1 and 7.2: int ether_hostton(char *, struct ether_addr *);
7876f9cba8fSJoseph Mingrone 	 */
788*afdbf109SJoseph Mingrone 	pcapint_strlcpy(namebuf, name, sizeof(namebuf));
7898cf6c252SPaul Traina 	ap = NULL;
7906f9cba8fSJoseph Mingrone 	if (ether_hostton(namebuf, (struct ether_addr *)a) == 0) {
7918cf6c252SPaul Traina 		ap = (u_char *)malloc(6);
7928cf6c252SPaul Traina 		if (ap != NULL)
793dfd1ee14SBill Fenner 			memcpy((char *)ap, (char *)a, 6);
7948cf6c252SPaul Traina 	}
7958cf6c252SPaul Traina 	return (ap);
7968cf6c252SPaul Traina }
7978cf6c252SPaul Traina #endif
7988cf6c252SPaul Traina 
799b00ab754SHans Petter Selasky /*
800b00ab754SHans Petter Selasky  * XXX - not guaranteed to be thread-safe!
801b00ab754SHans Petter Selasky  */
802ada6f083SXin LI int
803b00ab754SHans Petter Selasky #ifdef	DECNETLIB
__pcap_nametodnaddr(const char * name,u_short * res)804ada6f083SXin LI __pcap_nametodnaddr(const char *name, u_short *res)
8058cf6c252SPaul Traina {
8068cf6c252SPaul Traina 	struct nodeent *getnodebyname();
8078cf6c252SPaul Traina 	struct nodeent *nep;
8088cf6c252SPaul Traina 
8098cf6c252SPaul Traina 	nep = getnodebyname(name);
8108cf6c252SPaul Traina 	if (nep == ((struct nodeent *)0))
811ada6f083SXin LI 		return(0);
8128cf6c252SPaul Traina 
813ada6f083SXin LI 	memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
814ada6f083SXin LI 	return(1);
8158cf6c252SPaul Traina #else
816b00ab754SHans Petter Selasky __pcap_nametodnaddr(const char *name _U_, u_short *res _U_)
817b00ab754SHans Petter Selasky {
818c0653930SBill Fenner 	return(0);
8198cf6c252SPaul Traina #endif
8208cf6c252SPaul Traina }
821