xref: /freebsd/contrib/libpcap/nametoaddr.c (revision 57e22627f9459b9dfd6043b32e02ecfcf205e176)
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 
258e1481d1SBill Fenner #ifdef HAVE_CONFIG_H
26b00ab754SHans Petter Selasky #include <config.h>
278cf6c252SPaul Traina #endif
288cf6c252SPaul Traina 
29a0ee43a1SRui Paulo #ifdef DECNETLIB
30a0ee43a1SRui Paulo #include <sys/types.h>
31a0ee43a1SRui Paulo #include <netdnet/dnetdb.h>
32a0ee43a1SRui Paulo #endif
33a0ee43a1SRui Paulo 
34ada6f083SXin LI #ifdef _WIN32
35b00ab754SHans Petter Selasky   #include <winsock2.h>
36b00ab754SHans Petter Selasky   #include <ws2tcpip.h>
37eb9f0330SBruce M Simpson 
38ada6f083SXin LI   #ifdef INET6
39ada6f083SXin LI     /*
40ada6f083SXin LI      * To quote the MSDN page for getaddrinfo() at
41ada6f083SXin LI      *
42ada6f083SXin LI      *    https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
43ada6f083SXin LI      *
44ada6f083SXin LI      * "Support for getaddrinfo on Windows 2000 and older versions
45ada6f083SXin LI      * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
46ada6f083SXin LI      * later. To execute an application that uses this function on earlier
47ada6f083SXin LI      * versions of Windows, then you need to include the Ws2tcpip.h and
48ada6f083SXin LI      * Wspiapi.h files. When the Wspiapi.h include file is added, the
49ada6f083SXin LI      * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
50ada6f083SXin LI      * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
51ada6f083SXin LI      * function is implemented in such a way that if the Ws2_32.dll or the
52ada6f083SXin LI      * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
53ada6f083SXin LI      * Preview for Windows 2000) does not include getaddrinfo, then a
54ada6f083SXin LI      * version of getaddrinfo is implemented inline based on code in the
55ada6f083SXin LI      * Wspiapi.h header file. This inline code will be used on older Windows
56ada6f083SXin LI      * platforms that do not natively support the getaddrinfo function."
57ada6f083SXin LI      *
58b00ab754SHans Petter Selasky      * We use getaddrinfo(), so we include Wspiapi.h here.
59ada6f083SXin LI      */
60b00ab754SHans Petter Selasky     #include <wspiapi.h>
61b00ab754SHans Petter Selasky   #endif /* INET6 */
62ada6f083SXin LI #else /* _WIN32 */
638cf6c252SPaul Traina   #include <sys/param.h>
64b00ab754SHans Petter Selasky   #include <sys/types.h>
658cf6c252SPaul Traina   #include <sys/socket.h>
66628d2ac1SGarrett Wollman   #include <sys/time.h>
678cf6c252SPaul Traina 
688cf6c252SPaul Traina   #include <netinet/in.h>
69eb9f0330SBruce M Simpson 
70eb9f0330SBruce M Simpson   #ifdef HAVE_ETHER_HOSTTON
71b00ab754SHans Petter Selasky     #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
72c761ebcbSSam Leffler       /*
73b00ab754SHans Petter Selasky        * OK, just include <net/ethernet.h>.
74c761ebcbSSam Leffler        */
75b00ab754SHans Petter Selasky       #include <net/ethernet.h>
76b00ab754SHans Petter Selasky     #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
77b00ab754SHans Petter Selasky       /*
78b00ab754SHans Petter Selasky        * OK, just include <netinet/ether.h>
79b00ab754SHans Petter Selasky        */
80c761ebcbSSam Leffler       #include <netinet/ether.h>
81b00ab754SHans Petter Selasky     #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
82b00ab754SHans Petter Selasky       /*
83b00ab754SHans Petter Selasky        * OK, just include <sys/ethernet.h>
84b00ab754SHans Petter Selasky        */
85b00ab754SHans Petter Selasky       #include <sys/ethernet.h>
86b00ab754SHans Petter Selasky     #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
87b00ab754SHans Petter Selasky       /*
88b00ab754SHans Petter Selasky        * OK, just include <arpa/inet.h>
89b00ab754SHans Petter Selasky        */
90b00ab754SHans Petter Selasky       #include <arpa/inet.h>
91b00ab754SHans Petter Selasky     #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
92b00ab754SHans Petter Selasky       /*
93b00ab754SHans Petter Selasky        * OK, include <netinet/if_ether.h>, after all the other stuff we
94b00ab754SHans Petter Selasky        * need to include or define for its benefit.
95b00ab754SHans Petter Selasky        */
96b00ab754SHans Petter Selasky       #define NEED_NETINET_IF_ETHER_H
97b00ab754SHans Petter Selasky     #else
98b00ab754SHans Petter Selasky       /*
99b00ab754SHans Petter Selasky        * We'll have to declare it ourselves.
100b00ab754SHans Petter Selasky        * If <netinet/if_ether.h> defines struct ether_addr, include
101b00ab754SHans Petter Selasky        * it.  Otherwise, define it ourselves.
102b00ab754SHans Petter Selasky        */
103b00ab754SHans Petter Selasky       #ifdef HAVE_STRUCT_ETHER_ADDR
104b00ab754SHans Petter Selasky         #define NEED_NETINET_IF_ETHER_H
105b00ab754SHans Petter Selasky       #else /* HAVE_STRUCT_ETHER_ADDR */
106b00ab754SHans Petter Selasky 	struct ether_addr {
107b00ab754SHans Petter Selasky 		unsigned char ether_addr_octet[6];
108b00ab754SHans Petter Selasky 	};
109b00ab754SHans Petter Selasky       #endif /* HAVE_STRUCT_ETHER_ADDR */
110b00ab754SHans Petter Selasky     #endif /* what declares ether_hostton() */
111b00ab754SHans Petter Selasky 
112b00ab754SHans Petter Selasky     #ifdef NEED_NETINET_IF_ETHER_H
113b00ab754SHans Petter Selasky       #include <net/if.h>	/* Needed on some platforms */
114b00ab754SHans Petter Selasky       #include <netinet/in.h>	/* Needed on some platforms */
115b00ab754SHans Petter Selasky       #include <netinet/if_ether.h>
116b00ab754SHans Petter Selasky     #endif /* NEED_NETINET_IF_ETHER_H */
117b00ab754SHans Petter Selasky 
118b00ab754SHans Petter Selasky     #ifndef HAVE_DECL_ETHER_HOSTTON
119b00ab754SHans Petter Selasky       /*
120b00ab754SHans Petter Selasky        * No header declares it, so declare it ourselves.
121b00ab754SHans Petter Selasky        */
122b00ab754SHans Petter Selasky       extern int ether_hostton(const char *, struct ether_addr *);
123b00ab754SHans Petter Selasky     #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
124eb9f0330SBruce M Simpson   #endif /* HAVE_ETHER_HOSTTON */
125b00ab754SHans Petter Selasky 
1268cf6c252SPaul Traina   #include <arpa/inet.h>
127fae05455SBill Fenner   #include <netdb.h>
128ada6f083SXin LI #endif /* _WIN32 */
1298cf6c252SPaul Traina 
1308cf6c252SPaul Traina #include <ctype.h>
1318cf6c252SPaul Traina #include <errno.h>
1328cf6c252SPaul Traina #include <stdlib.h>
1334238c2cdSSam Leffler #include <string.h>
1348cf6c252SPaul Traina #include <stdio.h>
1358cf6c252SPaul Traina 
1368cf6c252SPaul Traina #include "pcap-int.h"
1378cf6c252SPaul Traina 
1388cf6c252SPaul Traina #include "gencode.h"
139a8e07101SRui Paulo #include <pcap/namedb.h>
140ada6f083SXin LI #include "nametoaddr.h"
1418cf6c252SPaul Traina 
1428cf6c252SPaul Traina #ifdef HAVE_OS_PROTO_H
1438cf6c252SPaul Traina #include "os-proto.h"
1448cf6c252SPaul Traina #endif
1458cf6c252SPaul Traina 
1468cf6c252SPaul Traina #ifndef NTOHL
1478cf6c252SPaul Traina #define NTOHL(x) (x) = ntohl(x)
1488cf6c252SPaul Traina #define NTOHS(x) (x) = ntohs(x)
1498cf6c252SPaul Traina #endif
1508cf6c252SPaul Traina 
1518cf6c252SPaul Traina /*
1528cf6c252SPaul Traina  *  Convert host name to internet address.
1538cf6c252SPaul Traina  *  Return 0 upon failure.
154b00ab754SHans Petter Selasky  *  XXX - not thread-safe; don't use it inside libpcap.
1558cf6c252SPaul Traina  */
1568cf6c252SPaul Traina bpf_u_int32 **
1578cf6c252SPaul Traina pcap_nametoaddr(const char *name)
1588cf6c252SPaul Traina {
1598cf6c252SPaul Traina #ifndef h_addr
1608cf6c252SPaul Traina 	static bpf_u_int32 *hlist[2];
1618cf6c252SPaul Traina #endif
1628cf6c252SPaul Traina 	bpf_u_int32 **p;
1638cf6c252SPaul Traina 	struct hostent *hp;
1648cf6c252SPaul Traina 
1658cf6c252SPaul Traina 	if ((hp = gethostbyname(name)) != NULL) {
1668cf6c252SPaul Traina #ifndef h_addr
1678cf6c252SPaul Traina 		hlist[0] = (bpf_u_int32 *)hp->h_addr;
1688cf6c252SPaul Traina 		NTOHL(hp->h_addr);
1698cf6c252SPaul Traina 		return hlist;
1708cf6c252SPaul Traina #else
1718cf6c252SPaul Traina 		for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
1728cf6c252SPaul Traina 			NTOHL(**p);
1738cf6c252SPaul Traina 		return (bpf_u_int32 **)hp->h_addr_list;
1748cf6c252SPaul Traina #endif
1758cf6c252SPaul Traina 	}
1768cf6c252SPaul Traina 	else
1778cf6c252SPaul Traina 		return 0;
1788cf6c252SPaul Traina }
1798e1481d1SBill Fenner 
180fae05455SBill Fenner struct addrinfo *
1818e1481d1SBill Fenner pcap_nametoaddrinfo(const char *name)
182fae05455SBill Fenner {
183fae05455SBill Fenner 	struct addrinfo hints, *res;
184fae05455SBill Fenner 	int error;
185fae05455SBill Fenner 
186fae05455SBill Fenner 	memset(&hints, 0, sizeof(hints));
187fae05455SBill Fenner 	hints.ai_family = PF_UNSPEC;
188fae05455SBill Fenner 	hints.ai_socktype = SOCK_STREAM;	/*not really*/
189c761ebcbSSam Leffler 	hints.ai_protocol = IPPROTO_TCP;	/*not really*/
190fae05455SBill Fenner 	error = getaddrinfo(name, NULL, &hints, &res);
191fae05455SBill Fenner 	if (error)
192fae05455SBill Fenner 		return NULL;
193fae05455SBill Fenner 	else
194fae05455SBill Fenner 		return res;
195fae05455SBill Fenner }
1968cf6c252SPaul Traina 
1978cf6c252SPaul Traina /*
1988cf6c252SPaul Traina  *  Convert net name to internet address.
1998cf6c252SPaul Traina  *  Return 0 upon failure.
200b00ab754SHans Petter Selasky  *  XXX - not guaranteed to be thread-safe!  See below for platforms
201b00ab754SHans Petter Selasky  *  on which it is thread-safe and on which it isn't.
2028cf6c252SPaul Traina  */
2038cf6c252SPaul Traina bpf_u_int32
2048cf6c252SPaul Traina pcap_nametonetaddr(const char *name)
2058cf6c252SPaul Traina {
206b00ab754SHans Petter Selasky #ifdef _WIN32
207eb9f0330SBruce M Simpson 	/*
208eb9f0330SBruce M Simpson 	 * There's no "getnetbyname()" on Windows.
209ada6f083SXin LI 	 *
210ada6f083SXin LI 	 * XXX - I guess we could use the BSD code to read
211ada6f083SXin LI 	 * C:\Windows\System32\drivers\etc/networks, assuming
212ada6f083SXin LI 	 * that's its home on all the versions of Windows
213ada6f083SXin LI 	 * we use, but that file probably just has the loopback
214ada6f083SXin LI 	 * network on 127/24 on 99 44/100% of Windows machines.
215ada6f083SXin LI 	 *
216ada6f083SXin LI 	 * (Heck, these days it probably just has that on 99 44/100%
217ada6f083SXin LI 	 * of *UN*X* machines.)
218eb9f0330SBruce M Simpson 	 */
219eb9f0330SBruce M Simpson 	return 0;
220b00ab754SHans Petter Selasky #else
221b00ab754SHans Petter Selasky 	/*
222b00ab754SHans Petter Selasky 	 * UN*X.
223b00ab754SHans Petter Selasky 	 */
224b00ab754SHans Petter Selasky 	struct netent *np;
225b00ab754SHans Petter Selasky   #if defined(HAVE_LINUX_GETNETBYNAME_R)
226b00ab754SHans Petter Selasky 	/*
227b00ab754SHans Petter Selasky 	 * We have Linux's reentrant getnetbyname_r().
228b00ab754SHans Petter Selasky 	 */
229b00ab754SHans Petter Selasky 	struct netent result_buf;
230b00ab754SHans Petter Selasky 	char buf[1024];	/* arbitrary size */
231b00ab754SHans Petter Selasky 	int h_errnoval;
232b00ab754SHans Petter Selasky 	int err;
233b00ab754SHans Petter Selasky 
234*57e22627SCy Schubert 	/*
235*57e22627SCy Schubert 	 * Apparently, the man page at
236*57e22627SCy Schubert 	 *
237*57e22627SCy Schubert 	 *    http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
238*57e22627SCy Schubert 	 *
239*57e22627SCy Schubert 	 * lies when it says
240*57e22627SCy Schubert 	 *
241*57e22627SCy Schubert 	 *    If the function call successfully obtains a network record,
242*57e22627SCy Schubert 	 *    then *result is set pointing to result_buf; otherwise, *result
243*57e22627SCy Schubert 	 *    is set to NULL.
244*57e22627SCy Schubert 	 *
245*57e22627SCy Schubert 	 * and, in fact, at least in some versions of GNU libc, it does
246*57e22627SCy Schubert 	 * *not* always get set if getnetbyname_r() succeeds.
247*57e22627SCy Schubert 	 */
248*57e22627SCy Schubert 	np = NULL;
249b00ab754SHans Petter Selasky  	err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
250b00ab754SHans Petter Selasky 	    &h_errnoval);
251b00ab754SHans Petter Selasky 	if (err != 0) {
252b00ab754SHans Petter Selasky 		/*
253b00ab754SHans Petter Selasky 		 * XXX - dynamically allocate the buffer, and make it
254b00ab754SHans Petter Selasky 		 * bigger if we get ERANGE back?
255b00ab754SHans Petter Selasky 		 */
256b00ab754SHans Petter Selasky 		return 0;
257b00ab754SHans Petter Selasky 	}
258b00ab754SHans Petter Selasky   #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
259b00ab754SHans Petter Selasky 	/*
260b00ab754SHans Petter Selasky 	 * We have Solaris's and IRIX's reentrant getnetbyname_r().
261b00ab754SHans Petter Selasky 	 */
262b00ab754SHans Petter Selasky 	struct netent result_buf;
263b00ab754SHans Petter Selasky 	char buf[1024];	/* arbitrary size */
264b00ab754SHans Petter Selasky 
265b00ab754SHans Petter Selasky 	np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
266b00ab754SHans Petter Selasky   #elif defined(HAVE_AIX_GETNETBYNAME_R)
267b00ab754SHans Petter Selasky 	/*
268b00ab754SHans Petter Selasky 	 * We have AIX's reentrant getnetbyname_r().
269b00ab754SHans Petter Selasky 	 */
270b00ab754SHans Petter Selasky 	struct netent result_buf;
271b00ab754SHans Petter Selasky 	struct netent_data net_data;
272b00ab754SHans Petter Selasky 
273b00ab754SHans Petter Selasky 	if (getnetbyname_r(name, &result_buf, &net_data) == -1)
274b00ab754SHans Petter Selasky 		np = NULL;
275b00ab754SHans Petter Selasky 	else
276b00ab754SHans Petter Selasky 		np = &result_buf;
277b00ab754SHans Petter Selasky   #else
278b00ab754SHans Petter Selasky  	/*
279b00ab754SHans Petter Selasky  	 * We don't have any getnetbyname_r(); either we have a
280b00ab754SHans Petter Selasky  	 * getnetbyname() that uses thread-specific data, in which
281b00ab754SHans Petter Selasky  	 * case we're thread-safe (sufficiently recent FreeBSD,
282b00ab754SHans Petter Selasky  	 * sufficiently recent Darwin-based OS, sufficiently recent
283b00ab754SHans Petter Selasky  	 * HP-UX, sufficiently recent Tru64 UNIX), or we have the
284b00ab754SHans Petter Selasky  	 * traditional getnetbyname() (everything else, including
285b00ab754SHans Petter Selasky  	 * current NetBSD and OpenBSD), in which case we're not
286b00ab754SHans Petter Selasky  	 * thread-safe.
287b00ab754SHans Petter Selasky  	 */
288b00ab754SHans Petter Selasky 	np = getnetbyname(name);
289eb9f0330SBruce M Simpson   #endif
290b00ab754SHans Petter Selasky 	if (np != NULL)
291b00ab754SHans Petter Selasky 		return np->n_net;
292b00ab754SHans Petter Selasky 	else
293b00ab754SHans Petter Selasky 		return 0;
294b00ab754SHans Petter Selasky #endif /* _WIN32 */
2958cf6c252SPaul Traina }
2968cf6c252SPaul Traina 
2978cf6c252SPaul Traina /*
2988cf6c252SPaul Traina  * Convert a port name to its port and protocol numbers.
2998cf6c252SPaul Traina  * We assume only TCP or UDP.
3008cf6c252SPaul Traina  * Return 0 upon failure.
3018cf6c252SPaul Traina  */
3028cf6c252SPaul Traina int
3038cf6c252SPaul Traina pcap_nametoport(const char *name, int *port, int *proto)
3048cf6c252SPaul Traina {
305b00ab754SHans Petter Selasky 	struct addrinfo hints, *res, *ai;
306b00ab754SHans Petter Selasky 	int error;
307b00ab754SHans Petter Selasky 	struct sockaddr_in *in4;
308b00ab754SHans Petter Selasky #ifdef INET6
309b00ab754SHans Petter Selasky 	struct sockaddr_in6 *in6;
310b00ab754SHans Petter Selasky #endif
311eb9f0330SBruce M Simpson 	int tcp_port = -1;
312eb9f0330SBruce M Simpson 	int udp_port = -1;
3138cf6c252SPaul Traina 
3148cf6c252SPaul Traina 	/*
315b00ab754SHans Petter Selasky 	 * We check for both TCP and UDP in case there are
316b00ab754SHans Petter Selasky 	 * ambiguous entries.
317b00ab754SHans Petter Selasky 	 */
318b00ab754SHans Petter Selasky 	memset(&hints, 0, sizeof(hints));
319b00ab754SHans Petter Selasky 	hints.ai_family = PF_UNSPEC;
320b00ab754SHans Petter Selasky 	hints.ai_socktype = SOCK_STREAM;
321b00ab754SHans Petter Selasky 	hints.ai_protocol = IPPROTO_TCP;
322b00ab754SHans Petter Selasky 	error = getaddrinfo(NULL, name, &hints, &res);
323b00ab754SHans Petter Selasky 	if (error != 0) {
324*57e22627SCy Schubert 		if (error != EAI_NONAME &&
325*57e22627SCy Schubert 		    error != EAI_SERVICE) {
326b00ab754SHans Petter Selasky 			/*
327b00ab754SHans Petter Selasky 			 * This is a real error, not just "there's
328b00ab754SHans Petter Selasky 			 * no such service name".
329b00ab754SHans Petter Selasky 			 * XXX - this doesn't return an error string.
330b00ab754SHans Petter Selasky 			 */
331b00ab754SHans Petter Selasky 			return 0;
332b00ab754SHans Petter Selasky 		}
333b00ab754SHans Petter Selasky 	} else {
334b00ab754SHans Petter Selasky 		/*
335b00ab754SHans Petter Selasky 		 * OK, we found it.  Did it find anything?
336b00ab754SHans Petter Selasky 		 */
337b00ab754SHans Petter Selasky 		for (ai = res; ai != NULL; ai = ai->ai_next) {
338b00ab754SHans Petter Selasky 			/*
339b00ab754SHans Petter Selasky 			 * Does it have an address?
340b00ab754SHans Petter Selasky 			 */
341b00ab754SHans Petter Selasky 			if (ai->ai_addr != NULL) {
342b00ab754SHans Petter Selasky 				/*
343b00ab754SHans Petter Selasky 				 * Yes.  Get a port number; we're done.
344b00ab754SHans Petter Selasky 				 */
345b00ab754SHans Petter Selasky 				if (ai->ai_addr->sa_family == AF_INET) {
346b00ab754SHans Petter Selasky 					in4 = (struct sockaddr_in *)ai->ai_addr;
347b00ab754SHans Petter Selasky 					tcp_port = ntohs(in4->sin_port);
348b00ab754SHans Petter Selasky 					break;
349b00ab754SHans Petter Selasky 				}
350b00ab754SHans Petter Selasky #ifdef INET6
351b00ab754SHans Petter Selasky 				if (ai->ai_addr->sa_family == AF_INET6) {
352b00ab754SHans Petter Selasky 					in6 = (struct sockaddr_in6 *)ai->ai_addr;
353b00ab754SHans Petter Selasky 					tcp_port = ntohs(in6->sin6_port);
354b00ab754SHans Petter Selasky 					break;
355b00ab754SHans Petter Selasky 				}
356b00ab754SHans Petter Selasky #endif
357b00ab754SHans Petter Selasky 			}
358b00ab754SHans Petter Selasky 		}
359b00ab754SHans Petter Selasky 		freeaddrinfo(res);
360b00ab754SHans Petter Selasky 	}
361b00ab754SHans Petter Selasky 
362b00ab754SHans Petter Selasky 	memset(&hints, 0, sizeof(hints));
363b00ab754SHans Petter Selasky 	hints.ai_family = PF_UNSPEC;
364b00ab754SHans Petter Selasky 	hints.ai_socktype = SOCK_DGRAM;
365b00ab754SHans Petter Selasky 	hints.ai_protocol = IPPROTO_UDP;
366b00ab754SHans Petter Selasky 	error = getaddrinfo(NULL, name, &hints, &res);
367b00ab754SHans Petter Selasky 	if (error != 0) {
368*57e22627SCy Schubert 		if (error != EAI_NONAME &&
369*57e22627SCy Schubert 		    error != EAI_SERVICE) {
370b00ab754SHans Petter Selasky 			/*
371b00ab754SHans Petter Selasky 			 * This is a real error, not just "there's
372b00ab754SHans Petter Selasky 			 * no such service name".
373b00ab754SHans Petter Selasky 			 * XXX - this doesn't return an error string.
374b00ab754SHans Petter Selasky 			 */
375b00ab754SHans Petter Selasky 			return 0;
376b00ab754SHans Petter Selasky 		}
377b00ab754SHans Petter Selasky 	} else {
378b00ab754SHans Petter Selasky 		/*
379b00ab754SHans Petter Selasky 		 * OK, we found it.  Did it find anything?
380b00ab754SHans Petter Selasky 		 */
381b00ab754SHans Petter Selasky 		for (ai = res; ai != NULL; ai = ai->ai_next) {
382b00ab754SHans Petter Selasky 			/*
383b00ab754SHans Petter Selasky 			 * Does it have an address?
384b00ab754SHans Petter Selasky 			 */
385b00ab754SHans Petter Selasky 			if (ai->ai_addr != NULL) {
386b00ab754SHans Petter Selasky 				/*
387b00ab754SHans Petter Selasky 				 * Yes.  Get a port number; we're done.
388b00ab754SHans Petter Selasky 				 */
389b00ab754SHans Petter Selasky 				if (ai->ai_addr->sa_family == AF_INET) {
390b00ab754SHans Petter Selasky 					in4 = (struct sockaddr_in *)ai->ai_addr;
391b00ab754SHans Petter Selasky 					udp_port = ntohs(in4->sin_port);
392b00ab754SHans Petter Selasky 					break;
393b00ab754SHans Petter Selasky 				}
394b00ab754SHans Petter Selasky #ifdef INET6
395b00ab754SHans Petter Selasky 				if (ai->ai_addr->sa_family == AF_INET6) {
396b00ab754SHans Petter Selasky 					in6 = (struct sockaddr_in6 *)ai->ai_addr;
397b00ab754SHans Petter Selasky 					udp_port = ntohs(in6->sin6_port);
398b00ab754SHans Petter Selasky 					break;
399b00ab754SHans Petter Selasky 				}
400b00ab754SHans Petter Selasky #endif
401b00ab754SHans Petter Selasky 			}
402b00ab754SHans Petter Selasky 		}
403b00ab754SHans Petter Selasky 		freeaddrinfo(res);
404b00ab754SHans Petter Selasky 	}
405b00ab754SHans Petter Selasky 
406b00ab754SHans Petter Selasky 	/*
4078cf6c252SPaul Traina 	 * We need to check /etc/services for ambiguous entries.
408b00ab754SHans Petter Selasky 	 * If we find an ambiguous entry, and it has the
4098cf6c252SPaul Traina 	 * same port number, change the proto to PROTO_UNDEF
4108cf6c252SPaul Traina 	 * so both TCP and UDP will be checked.
4118cf6c252SPaul Traina 	 */
412eb9f0330SBruce M Simpson 	if (tcp_port >= 0) {
413eb9f0330SBruce M Simpson 		*port = tcp_port;
414eb9f0330SBruce M Simpson 		*proto = IPPROTO_TCP;
415eb9f0330SBruce M Simpson 		if (udp_port >= 0) {
416eb9f0330SBruce M Simpson 			if (udp_port == tcp_port)
417eb9f0330SBruce M Simpson 				*proto = PROTO_UNDEF;
4188cf6c252SPaul Traina #ifdef notdef
419eb9f0330SBruce M Simpson 			else
4208cf6c252SPaul Traina 				/* Can't handle ambiguous names that refer
4218cf6c252SPaul Traina 				   to different port numbers. */
4228cf6c252SPaul Traina 				warning("ambiguous port %s in /etc/services",
4238cf6c252SPaul Traina 					name);
4248cf6c252SPaul Traina #endif
4258cf6c252SPaul Traina 		}
4268cf6c252SPaul Traina 		return 1;
4278cf6c252SPaul Traina 	}
428eb9f0330SBruce M Simpson 	if (udp_port >= 0) {
429eb9f0330SBruce M Simpson 		*port = udp_port;
430eb9f0330SBruce M Simpson 		*proto = IPPROTO_UDP;
431eb9f0330SBruce M Simpson 		return 1;
432eb9f0330SBruce M Simpson 	}
4338cf6c252SPaul Traina #if defined(ultrix) || defined(__osf__)
4348cf6c252SPaul Traina 	/* Special hack in case NFS isn't in /etc/services */
4358cf6c252SPaul Traina 	if (strcmp(name, "nfs") == 0) {
4368cf6c252SPaul Traina 		*port = 2049;
4378cf6c252SPaul Traina 		*proto = PROTO_UNDEF;
4388cf6c252SPaul Traina 		return 1;
4398cf6c252SPaul Traina 	}
4408cf6c252SPaul Traina #endif
4418cf6c252SPaul Traina 	return 0;
4428cf6c252SPaul Traina }
4438cf6c252SPaul Traina 
4444238c2cdSSam Leffler /*
4454238c2cdSSam Leffler  * Convert a string in the form PPP-PPP, where correspond to ports, to
4464238c2cdSSam Leffler  * a starting and ending port in a port range.
4474238c2cdSSam Leffler  * Return 0 on failure.
4484238c2cdSSam Leffler  */
4494238c2cdSSam Leffler int
4504238c2cdSSam Leffler pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
4514238c2cdSSam Leffler {
4524238c2cdSSam Leffler 	u_int p1, p2;
4534238c2cdSSam Leffler 	char *off, *cpy;
4544238c2cdSSam Leffler 	int save_proto;
4554238c2cdSSam Leffler 
4564238c2cdSSam Leffler 	if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
4574238c2cdSSam Leffler 		if ((cpy = strdup(name)) == NULL)
4584238c2cdSSam Leffler 			return 0;
4594238c2cdSSam Leffler 
4604238c2cdSSam Leffler 		if ((off = strchr(cpy, '-')) == NULL) {
4614238c2cdSSam Leffler 			free(cpy);
4624238c2cdSSam Leffler 			return 0;
4634238c2cdSSam Leffler 		}
4644238c2cdSSam Leffler 
4654238c2cdSSam Leffler 		*off = '\0';
4664238c2cdSSam Leffler 
4674238c2cdSSam Leffler 		if (pcap_nametoport(cpy, port1, proto) == 0) {
4684238c2cdSSam Leffler 			free(cpy);
4694238c2cdSSam Leffler 			return 0;
4704238c2cdSSam Leffler 		}
4714238c2cdSSam Leffler 		save_proto = *proto;
4724238c2cdSSam Leffler 
4734238c2cdSSam Leffler 		if (pcap_nametoport(off + 1, port2, proto) == 0) {
4744238c2cdSSam Leffler 			free(cpy);
4754238c2cdSSam Leffler 			return 0;
4764238c2cdSSam Leffler 		}
477edc89b24SXin LI 		free(cpy);
4784238c2cdSSam Leffler 
4794238c2cdSSam Leffler 		if (*proto != save_proto)
4804238c2cdSSam Leffler 			*proto = PROTO_UNDEF;
4814238c2cdSSam Leffler 	} else {
4824238c2cdSSam Leffler 		*port1 = p1;
4834238c2cdSSam Leffler 		*port2 = p2;
4844238c2cdSSam Leffler 		*proto = PROTO_UNDEF;
4854238c2cdSSam Leffler 	}
4864238c2cdSSam Leffler 
4874238c2cdSSam Leffler 	return 1;
4884238c2cdSSam Leffler }
4894238c2cdSSam Leffler 
490b00ab754SHans Petter Selasky /*
491b00ab754SHans Petter Selasky  * XXX - not guaranteed to be thread-safe!  See below for platforms
492b00ab754SHans Petter Selasky  * on which it is thread-safe and on which it isn't.
493b00ab754SHans Petter Selasky  */
4948cf6c252SPaul Traina int
4958cf6c252SPaul Traina pcap_nametoproto(const char *str)
4968cf6c252SPaul Traina {
4978cf6c252SPaul Traina 	struct protoent *p;
498b00ab754SHans Petter Selasky   #if defined(HAVE_LINUX_GETNETBYNAME_R)
499b00ab754SHans Petter Selasky 	/*
500b00ab754SHans Petter Selasky 	 * We have Linux's reentrant getprotobyname_r().
501b00ab754SHans Petter Selasky 	 */
502b00ab754SHans Petter Selasky 	struct protoent result_buf;
503b00ab754SHans Petter Selasky 	char buf[1024];	/* arbitrary size */
504b00ab754SHans Petter Selasky 	int err;
5058cf6c252SPaul Traina 
506b00ab754SHans Petter Selasky 	err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
507b00ab754SHans Petter Selasky 	if (err != 0) {
508b00ab754SHans Petter Selasky 		/*
509b00ab754SHans Petter Selasky 		 * XXX - dynamically allocate the buffer, and make it
510b00ab754SHans Petter Selasky 		 * bigger if we get ERANGE back?
511b00ab754SHans Petter Selasky 		 */
512b00ab754SHans Petter Selasky 		return 0;
513b00ab754SHans Petter Selasky 	}
514b00ab754SHans Petter Selasky   #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
515b00ab754SHans Petter Selasky 	/*
516b00ab754SHans Petter Selasky 	 * We have Solaris's and IRIX's reentrant getprotobyname_r().
517b00ab754SHans Petter Selasky 	 */
518b00ab754SHans Petter Selasky 	struct protoent result_buf;
519b00ab754SHans Petter Selasky 	char buf[1024];	/* arbitrary size */
520b00ab754SHans Petter Selasky 
521b00ab754SHans Petter Selasky 	p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
522b00ab754SHans Petter Selasky   #elif defined(HAVE_AIX_GETNETBYNAME_R)
523b00ab754SHans Petter Selasky 	/*
524b00ab754SHans Petter Selasky 	 * We have AIX's reentrant getprotobyname_r().
525b00ab754SHans Petter Selasky 	 */
526b00ab754SHans Petter Selasky 	struct protoent result_buf;
527b00ab754SHans Petter Selasky 	struct protoent_data proto_data;
528b00ab754SHans Petter Selasky 
529b00ab754SHans Petter Selasky 	if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
530b00ab754SHans Petter Selasky 		p = NULL;
531b00ab754SHans Petter Selasky 	else
532b00ab754SHans Petter Selasky 		p = &result_buf;
533b00ab754SHans Petter Selasky   #else
534b00ab754SHans Petter Selasky  	/*
535b00ab754SHans Petter Selasky  	 * We don't have any getprotobyname_r(); either we have a
536b00ab754SHans Petter Selasky  	 * getprotobyname() that uses thread-specific data, in which
537b00ab754SHans Petter Selasky  	 * case we're thread-safe (sufficiently recent FreeBSD,
538b00ab754SHans Petter Selasky  	 * sufficiently recent Darwin-based OS, sufficiently recent
539b00ab754SHans Petter Selasky  	 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
540b00ab754SHans Petter Selasky 	 * the traditional getprotobyname() (everything else, including
541b00ab754SHans Petter Selasky  	 * current NetBSD and OpenBSD), in which case we're not
542b00ab754SHans Petter Selasky  	 * thread-safe.
543b00ab754SHans Petter Selasky  	 */
5448cf6c252SPaul Traina 	p = getprotobyname(str);
545b00ab754SHans Petter Selasky   #endif
5468cf6c252SPaul Traina 	if (p != 0)
5478cf6c252SPaul Traina 		return p->p_proto;
5488cf6c252SPaul Traina 	else
5498cf6c252SPaul Traina 		return PROTO_UNDEF;
5508cf6c252SPaul Traina }
5518cf6c252SPaul Traina 
5528cf6c252SPaul Traina #include "ethertype.h"
5538cf6c252SPaul Traina 
5548cf6c252SPaul Traina struct eproto {
5555357e0feSMax Laier 	const char *s;
5568cf6c252SPaul Traina 	u_short p;
5578cf6c252SPaul Traina };
5588cf6c252SPaul Traina 
559ada6f083SXin LI /*
560ada6f083SXin LI  * Static data base of ether protocol types.
561ada6f083SXin LI  * tcpdump used to import this, and it's declared as an export on
562ada6f083SXin LI  * Debian, at least, so make it a public symbol, even though we
563ada6f083SXin LI  * don't officially export it by declaring it in a header file.
564ada6f083SXin LI  * (Programs *should* do this themselves, as tcpdump now does.)
565b00ab754SHans Petter Selasky  *
566b00ab754SHans Petter Selasky  * We declare it here, right before defining it, to squelch any
567b00ab754SHans Petter Selasky  * warnings we might get from compilers about the lack of a
568b00ab754SHans Petter Selasky  * declaration.
569ada6f083SXin LI  */
570b00ab754SHans Petter Selasky PCAP_API struct eproto eproto_db[];
571ada6f083SXin LI PCAP_API_DEF struct eproto eproto_db[] = {
5728cf6c252SPaul Traina 	{ "pup", ETHERTYPE_PUP },
5738cf6c252SPaul Traina 	{ "xns", ETHERTYPE_NS },
5748cf6c252SPaul Traina 	{ "ip", ETHERTYPE_IP },
575fae05455SBill Fenner #ifdef INET6
576fae05455SBill Fenner 	{ "ip6", ETHERTYPE_IPV6 },
577fae05455SBill Fenner #endif
5788cf6c252SPaul Traina 	{ "arp", ETHERTYPE_ARP },
5798cf6c252SPaul Traina 	{ "rarp", ETHERTYPE_REVARP },
5808cf6c252SPaul Traina 	{ "sprite", ETHERTYPE_SPRITE },
5818cf6c252SPaul Traina 	{ "mopdl", ETHERTYPE_MOPDL },
5828cf6c252SPaul Traina 	{ "moprc", ETHERTYPE_MOPRC },
5838cf6c252SPaul Traina 	{ "decnet", ETHERTYPE_DN },
5848cf6c252SPaul Traina 	{ "lat", ETHERTYPE_LAT },
5858cf6c252SPaul Traina 	{ "sca", ETHERTYPE_SCA },
5868cf6c252SPaul Traina 	{ "lanbridge", ETHERTYPE_LANBRIDGE },
5878cf6c252SPaul Traina 	{ "vexp", ETHERTYPE_VEXP },
5888cf6c252SPaul Traina 	{ "vprod", ETHERTYPE_VPROD },
5898cf6c252SPaul Traina 	{ "atalk", ETHERTYPE_ATALK },
5908cf6c252SPaul Traina 	{ "atalkarp", ETHERTYPE_AARP },
5918cf6c252SPaul Traina 	{ "loopback", ETHERTYPE_LOOPBACK },
5928cf6c252SPaul Traina 	{ "decdts", ETHERTYPE_DECDTS },
5938cf6c252SPaul Traina 	{ "decdns", ETHERTYPE_DECDNS },
5948cf6c252SPaul Traina 	{ (char *)0, 0 }
5958cf6c252SPaul Traina };
5968cf6c252SPaul Traina 
5978cf6c252SPaul Traina int
5988cf6c252SPaul Traina pcap_nametoeproto(const char *s)
5998cf6c252SPaul Traina {
6008cf6c252SPaul Traina 	struct eproto *p = eproto_db;
6018cf6c252SPaul Traina 
6028cf6c252SPaul Traina 	while (p->s != 0) {
6038cf6c252SPaul Traina 		if (strcmp(p->s, s) == 0)
6048cf6c252SPaul Traina 			return p->p;
6058cf6c252SPaul Traina 		p += 1;
6068cf6c252SPaul Traina 	}
6078cf6c252SPaul Traina 	return PROTO_UNDEF;
6088cf6c252SPaul Traina }
6098cf6c252SPaul Traina 
610c761ebcbSSam Leffler #include "llc.h"
611c761ebcbSSam Leffler 
612c761ebcbSSam Leffler /* Static data base of LLC values. */
613c761ebcbSSam Leffler static struct eproto llc_db[] = {
614c761ebcbSSam Leffler 	{ "iso", LLCSAP_ISONS },
615c761ebcbSSam Leffler 	{ "stp", LLCSAP_8021D },
616c761ebcbSSam Leffler 	{ "ipx", LLCSAP_IPX },
617c761ebcbSSam Leffler 	{ "netbeui", LLCSAP_NETBEUI },
618c761ebcbSSam Leffler 	{ (char *)0, 0 }
619c761ebcbSSam Leffler };
620c761ebcbSSam Leffler 
621c761ebcbSSam Leffler int
622c761ebcbSSam Leffler pcap_nametollc(const char *s)
623c761ebcbSSam Leffler {
624c761ebcbSSam Leffler 	struct eproto *p = llc_db;
625c761ebcbSSam Leffler 
626c761ebcbSSam Leffler 	while (p->s != 0) {
627c761ebcbSSam Leffler 		if (strcmp(p->s, s) == 0)
628c761ebcbSSam Leffler 			return p->p;
629c761ebcbSSam Leffler 		p += 1;
630c761ebcbSSam Leffler 	}
631c761ebcbSSam Leffler 	return PROTO_UNDEF;
632c761ebcbSSam Leffler }
633c761ebcbSSam Leffler 
634b00ab754SHans Petter Selasky /* Hex digit to 8-bit unsigned integer. */
635b00ab754SHans Petter Selasky static inline u_char
636b00ab754SHans Petter Selasky xdtoi(u_char c)
6378cf6c252SPaul Traina {
6388cf6c252SPaul Traina 	if (isdigit(c))
639b00ab754SHans Petter Selasky 		return (u_char)(c - '0');
6408cf6c252SPaul Traina 	else if (islower(c))
641b00ab754SHans Petter Selasky 		return (u_char)(c - 'a' + 10);
6428cf6c252SPaul Traina 	else
643b00ab754SHans Petter Selasky 		return (u_char)(c - 'A' + 10);
6448cf6c252SPaul Traina }
6458cf6c252SPaul Traina 
6468cf6c252SPaul Traina int
6478cf6c252SPaul Traina __pcap_atoin(const char *s, bpf_u_int32 *addr)
6488cf6c252SPaul Traina {
6498cf6c252SPaul Traina 	u_int n;
6508cf6c252SPaul Traina 	int len;
6518cf6c252SPaul Traina 
6528cf6c252SPaul Traina 	*addr = 0;
6538cf6c252SPaul Traina 	len = 0;
654b00ab754SHans Petter Selasky 	for (;;) {
6558cf6c252SPaul Traina 		n = 0;
6568cf6c252SPaul Traina 		while (*s && *s != '.')
6578cf6c252SPaul Traina 			n = n * 10 + *s++ - '0';
6588cf6c252SPaul Traina 		*addr <<= 8;
6598cf6c252SPaul Traina 		*addr |= n & 0xff;
6608cf6c252SPaul Traina 		len += 8;
6618cf6c252SPaul Traina 		if (*s == '\0')
6628cf6c252SPaul Traina 			return len;
6638cf6c252SPaul Traina 		++s;
6648cf6c252SPaul Traina 	}
6658cf6c252SPaul Traina 	/* NOTREACHED */
6668cf6c252SPaul Traina }
6678cf6c252SPaul Traina 
6688cf6c252SPaul Traina int
6698cf6c252SPaul Traina __pcap_atodn(const char *s, bpf_u_int32 *addr)
6708cf6c252SPaul Traina {
6718cf6c252SPaul Traina #define AREASHIFT 10
6728cf6c252SPaul Traina #define AREAMASK 0176000
6738cf6c252SPaul Traina #define NODEMASK 01777
6748cf6c252SPaul Traina 
6758cf6c252SPaul Traina 	u_int node, area;
6768cf6c252SPaul Traina 
6775357e0feSMax Laier 	if (sscanf(s, "%d.%d", &area, &node) != 2)
678ada6f083SXin LI 		return(0);
6798cf6c252SPaul Traina 
6808cf6c252SPaul Traina 	*addr = (area << AREASHIFT) & AREAMASK;
6818cf6c252SPaul Traina 	*addr |= (node & NODEMASK);
6828cf6c252SPaul Traina 
6838cf6c252SPaul Traina 	return(32);
6848cf6c252SPaul Traina }
6858cf6c252SPaul Traina 
6868cf6c252SPaul Traina /*
687a8e07101SRui Paulo  * Convert 's', which can have the one of the forms:
688a8e07101SRui Paulo  *
689a8e07101SRui Paulo  *	"xx:xx:xx:xx:xx:xx"
690a8e07101SRui Paulo  *	"xx.xx.xx.xx.xx.xx"
691a8e07101SRui Paulo  *	"xx-xx-xx-xx-xx-xx"
692a8e07101SRui Paulo  *	"xxxx.xxxx.xxxx"
693a8e07101SRui Paulo  *	"xxxxxxxxxxxx"
694a8e07101SRui Paulo  *
695a8e07101SRui Paulo  * (or various mixes of ':', '.', and '-') into a new
6968cf6c252SPaul Traina  * ethernet address.  Assumes 's' is well formed.
6978cf6c252SPaul Traina  */
6988cf6c252SPaul Traina u_char *
6998cf6c252SPaul Traina pcap_ether_aton(const char *s)
7008cf6c252SPaul Traina {
7018cf6c252SPaul Traina 	register u_char *ep, *e;
702b00ab754SHans Petter Selasky 	register u_char d;
7038cf6c252SPaul Traina 
7048cf6c252SPaul Traina 	e = ep = (u_char *)malloc(6);
705681ed54cSXin LI 	if (e == NULL)
706681ed54cSXin LI 		return (NULL);
7078cf6c252SPaul Traina 
7088cf6c252SPaul Traina 	while (*s) {
709a8e07101SRui Paulo 		if (*s == ':' || *s == '.' || *s == '-')
7108cf6c252SPaul Traina 			s += 1;
7118cf6c252SPaul Traina 		d = xdtoi(*s++);
712c0653930SBill Fenner 		if (isxdigit((unsigned char)*s)) {
7138cf6c252SPaul Traina 			d <<= 4;
7148cf6c252SPaul Traina 			d |= xdtoi(*s++);
7158cf6c252SPaul Traina 		}
7168cf6c252SPaul Traina 		*ep++ = d;
7178cf6c252SPaul Traina 	}
7188cf6c252SPaul Traina 
7198cf6c252SPaul Traina 	return (e);
7208cf6c252SPaul Traina }
7218cf6c252SPaul Traina 
7228cf6c252SPaul Traina #ifndef HAVE_ETHER_HOSTTON
723b00ab754SHans Petter Selasky /*
724b00ab754SHans Petter Selasky  * Roll our own.
725b00ab754SHans Petter Selasky  * XXX - not thread-safe, because pcap_next_etherent() isn't thread-
726b00ab754SHans Petter Selasky  * safe!  Needs a mutex or a thread-safe pcap_next_etherent().
727b00ab754SHans Petter Selasky  */
7288cf6c252SPaul Traina u_char *
7298cf6c252SPaul Traina pcap_ether_hostton(const char *name)
7308cf6c252SPaul Traina {
7318cf6c252SPaul Traina 	register struct pcap_etherent *ep;
7328cf6c252SPaul Traina 	register u_char *ap;
7338cf6c252SPaul Traina 	static FILE *fp = NULL;
7348e1481d1SBill Fenner 	static int init = 0;
7358cf6c252SPaul Traina 
7368cf6c252SPaul Traina 	if (!init) {
7378cf6c252SPaul Traina 		fp = fopen(PCAP_ETHERS_FILE, "r");
7388cf6c252SPaul Traina 		++init;
7398cf6c252SPaul Traina 		if (fp == NULL)
7408cf6c252SPaul Traina 			return (NULL);
7418cf6c252SPaul Traina 	} else if (fp == NULL)
7428cf6c252SPaul Traina 		return (NULL);
7438cf6c252SPaul Traina 	else
7448cf6c252SPaul Traina 		rewind(fp);
7458cf6c252SPaul Traina 
7468cf6c252SPaul Traina 	while ((ep = pcap_next_etherent(fp)) != NULL) {
7478cf6c252SPaul Traina 		if (strcmp(ep->name, name) == 0) {
7488cf6c252SPaul Traina 			ap = (u_char *)malloc(6);
7498cf6c252SPaul Traina 			if (ap != NULL) {
7508cf6c252SPaul Traina 				memcpy(ap, ep->addr, 6);
7518cf6c252SPaul Traina 				return (ap);
7528cf6c252SPaul Traina 			}
7538cf6c252SPaul Traina 			break;
7548cf6c252SPaul Traina 		}
7558cf6c252SPaul Traina 	}
7568cf6c252SPaul Traina 	return (NULL);
7578cf6c252SPaul Traina }
7588cf6c252SPaul Traina #else
759b00ab754SHans Petter Selasky /*
760b00ab754SHans Petter Selasky  * Use the OS-supplied routine.
761b00ab754SHans Petter Selasky  * This *should* be thread-safe; the API doesn't have a static buffer.
762b00ab754SHans Petter Selasky  */
7638cf6c252SPaul Traina u_char *
7648cf6c252SPaul Traina pcap_ether_hostton(const char *name)
7658cf6c252SPaul Traina {
7668cf6c252SPaul Traina 	register u_char *ap;
7678cf6c252SPaul Traina 	u_char a[6];
7688cf6c252SPaul Traina 
7698cf6c252SPaul Traina 	ap = NULL;
7705357e0feSMax Laier 	if (ether_hostton(name, (struct ether_addr *)a) == 0) {
7718cf6c252SPaul Traina 		ap = (u_char *)malloc(6);
7728cf6c252SPaul Traina 		if (ap != NULL)
773dfd1ee14SBill Fenner 			memcpy((char *)ap, (char *)a, 6);
7748cf6c252SPaul Traina 	}
7758cf6c252SPaul Traina 	return (ap);
7768cf6c252SPaul Traina }
7778cf6c252SPaul Traina #endif
7788cf6c252SPaul Traina 
779b00ab754SHans Petter Selasky /*
780b00ab754SHans Petter Selasky  * XXX - not guaranteed to be thread-safe!
781b00ab754SHans Petter Selasky  */
782ada6f083SXin LI int
783b00ab754SHans Petter Selasky #ifdef	DECNETLIB
784ada6f083SXin LI __pcap_nametodnaddr(const char *name, u_short *res)
7858cf6c252SPaul Traina {
7868cf6c252SPaul Traina 	struct nodeent *getnodebyname();
7878cf6c252SPaul Traina 	struct nodeent *nep;
7888cf6c252SPaul Traina 
7898cf6c252SPaul Traina 	nep = getnodebyname(name);
7908cf6c252SPaul Traina 	if (nep == ((struct nodeent *)0))
791ada6f083SXin LI 		return(0);
7928cf6c252SPaul Traina 
793ada6f083SXin LI 	memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
794ada6f083SXin LI 	return(1);
7958cf6c252SPaul Traina #else
796b00ab754SHans Petter Selasky __pcap_nametodnaddr(const char *name _U_, u_short *res _U_)
797b00ab754SHans Petter Selasky {
798c0653930SBill Fenner 	return(0);
7998cf6c252SPaul Traina #endif
8008cf6c252SPaul Traina }
801