xref: /netbsd/external/bsd/libpcap/dist/nametoaddr.c (revision d5dfe396)
1 /*	$NetBSD: nametoaddr.c,v 1.5 2019/10/01 16:02:11 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  *
23  * Name to id translation routines used by the scanner.
24  * These functions are not time critical.
25  */
26 
27 #include <sys/cdefs.h>
28 __RCSID("$NetBSD: nametoaddr.c,v 1.5 2019/10/01 16:02:11 christos Exp $");
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #ifdef DECNETLIB
35 #include <sys/types.h>
36 #include <netdnet/dnetdb.h>
37 #endif
38 
39 #ifdef _WIN32
40   #include <winsock2.h>
41   #include <ws2tcpip.h>
42 
43   #ifdef INET6
44     /*
45      * To quote the MSDN page for getaddrinfo() at
46      *
47      *    https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
48      *
49      * "Support for getaddrinfo on Windows 2000 and older versions
50      * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
51      * later. To execute an application that uses this function on earlier
52      * versions of Windows, then you need to include the Ws2tcpip.h and
53      * Wspiapi.h files. When the Wspiapi.h include file is added, the
54      * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
55      * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
56      * function is implemented in such a way that if the Ws2_32.dll or the
57      * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
58      * Preview for Windows 2000) does not include getaddrinfo, then a
59      * version of getaddrinfo is implemented inline based on code in the
60      * Wspiapi.h header file. This inline code will be used on older Windows
61      * platforms that do not natively support the getaddrinfo function."
62      *
63      * We use getaddrinfo(), so we include Wspiapi.h here.
64      */
65     #include <wspiapi.h>
66   #endif /* INET6 */
67 #else /* _WIN32 */
68   #include <sys/param.h>
69   #include <sys/types.h>
70   #include <sys/socket.h>
71   #include <sys/time.h>
72 
73   #include <netinet/in.h>
74 
75   #ifdef HAVE_ETHER_HOSTTON
76     #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
77       /*
78        * OK, just include <net/ethernet.h>.
79        */
80       #include <net/ethernet.h>
81     #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
82       /*
83        * OK, just include <netinet/ether.h>
84        */
85       #include <netinet/ether.h>
86     #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
87       /*
88        * OK, just include <sys/ethernet.h>
89        */
90       #include <sys/ethernet.h>
91     #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
92       /*
93        * OK, just include <arpa/inet.h>
94        */
95       #include <arpa/inet.h>
96     #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
97       /*
98        * OK, include <netinet/if_ether.h>, after all the other stuff we
99        * need to include or define for its benefit.
100        */
101       #define NEED_NETINET_IF_ETHER_H
102     #else
103       /*
104        * We'll have to declare it ourselves.
105        * If <netinet/if_ether.h> defines struct ether_addr, include
106        * it.  Otherwise, define it ourselves.
107        */
108       #ifdef HAVE_STRUCT_ETHER_ADDR
109         #define NEED_NETINET_IF_ETHER_H
110       #else /* HAVE_STRUCT_ETHER_ADDR */
111 	struct ether_addr {
112 		unsigned char ether_addr_octet[6];
113 	};
114       #endif /* HAVE_STRUCT_ETHER_ADDR */
115     #endif /* what declares ether_hostton() */
116 
117     #ifdef NEED_NETINET_IF_ETHER_H
118       #include <net/if.h>	/* Needed on some platforms */
119       #include <netinet/in.h>	/* Needed on some platforms */
120       #include <netinet/if_ether.h>
121     #endif /* NEED_NETINET_IF_ETHER_H */
122 
123     #ifndef HAVE_DECL_ETHER_HOSTTON
124       /*
125        * No header declares it, so declare it ourselves.
126        */
127       extern int ether_hostton(const char *, struct ether_addr *);
128     #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
129   #endif /* HAVE_ETHER_HOSTTON */
130 
131   #include <arpa/inet.h>
132   #include <netdb.h>
133 #endif /* _WIN32 */
134 
135 #include <ctype.h>
136 #include <errno.h>
137 #include <stdlib.h>
138 #include <string.h>
139 #include <stdio.h>
140 
141 #include "pcap-int.h"
142 
143 #include "gencode.h"
144 #include <pcap/namedb.h>
145 #include "nametoaddr.h"
146 
147 #ifdef HAVE_OS_PROTO_H
148 #include "os-proto.h"
149 #endif
150 
151 #ifndef NTOHL
152 #define NTOHL(x) (x) = ntohl(x)
153 #define NTOHS(x) (x) = ntohs(x)
154 #endif
155 
156 /*
157  *  Convert host name to internet address.
158  *  Return 0 upon failure.
159  *  XXX - not thread-safe; don't use it inside libpcap.
160  */
161 bpf_u_int32 **
pcap_nametoaddr(const char * name)162 pcap_nametoaddr(const char *name)
163 {
164 #ifndef h_addr
165 	static bpf_u_int32 *hlist[2];
166 #endif
167 	bpf_u_int32 **p;
168 	struct hostent *hp;
169 
170 	if ((hp = gethostbyname(name)) != NULL) {
171 #ifndef h_addr
172 		hlist[0] = (bpf_u_int32 *)hp->h_addr;
173 		NTOHL(hp->h_addr);
174 		return hlist;
175 #else
176 		for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
177 			NTOHL(**p);
178 		return (bpf_u_int32 **)hp->h_addr_list;
179 #endif
180 	}
181 	else
182 		return 0;
183 }
184 
185 struct addrinfo *
pcap_nametoaddrinfo(const char * name)186 pcap_nametoaddrinfo(const char *name)
187 {
188 	struct addrinfo hints, *res;
189 	int error;
190 
191 	memset(&hints, 0, sizeof(hints));
192 	hints.ai_family = PF_UNSPEC;
193 	hints.ai_socktype = SOCK_STREAM;	/*not really*/
194 	hints.ai_protocol = IPPROTO_TCP;	/*not really*/
195 	error = getaddrinfo(name, NULL, &hints, &res);
196 	if (error)
197 		return NULL;
198 	else
199 		return res;
200 }
201 
202 /*
203  *  Convert net name to internet address.
204  *  Return 0 upon failure.
205  *  XXX - not guaranteed to be thread-safe!  See below for platforms
206  *  on which it is thread-safe and on which it isn't.
207  */
208 bpf_u_int32
pcap_nametonetaddr(const char * name)209 pcap_nametonetaddr(const char *name)
210 {
211 #ifdef _WIN32
212 	/*
213 	 * There's no "getnetbyname()" on Windows.
214 	 *
215 	 * XXX - I guess we could use the BSD code to read
216 	 * C:\Windows\System32\drivers\etc/networks, assuming
217 	 * that's its home on all the versions of Windows
218 	 * we use, but that file probably just has the loopback
219 	 * network on 127/24 on 99 44/100% of Windows machines.
220 	 *
221 	 * (Heck, these days it probably just has that on 99 44/100%
222 	 * of *UN*X* machines.)
223 	 */
224 	return 0;
225 #else
226 	/*
227 	 * UN*X.
228 	 */
229 	struct netent *np;
230   #if defined(HAVE_LINUX_GETNETBYNAME_R)
231 	/*
232 	 * We have Linux's reentrant getnetbyname_r().
233 	 */
234 	struct netent result_buf;
235 	char buf[1024];	/* arbitrary size */
236 	int h_errnoval;
237 	int err;
238 
239 	/*
240 	 * Apparently, the man page at
241 	 *
242 	 *    http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
243 	 *
244 	 * lies when it says
245 	 *
246 	 *    If the function call successfully obtains a network record,
247 	 *    then *result is set pointing to result_buf; otherwise, *result
248 	 *    is set to NULL.
249 	 *
250 	 * and, in fact, at least in some versions of GNU libc, it does
251 	 * *not* always get set if getnetbyname_r() succeeds.
252 	 */
253 	np = NULL;
254  	err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
255 	    &h_errnoval);
256 	if (err != 0) {
257 		/*
258 		 * XXX - dynamically allocate the buffer, and make it
259 		 * bigger if we get ERANGE back?
260 		 */
261 		return 0;
262 	}
263   #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
264 	/*
265 	 * We have Solaris's and IRIX's reentrant getnetbyname_r().
266 	 */
267 	struct netent result_buf;
268 	char buf[1024];	/* arbitrary size */
269 
270 	np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
271   #elif defined(HAVE_AIX_GETNETBYNAME_R)
272 	/*
273 	 * We have AIX's reentrant getnetbyname_r().
274 	 */
275 	struct netent result_buf;
276 	struct netent_data net_data;
277 
278 	if (getnetbyname_r(name, &result_buf, &net_data) == -1)
279 		np = NULL;
280 	else
281 		np = &result_buf;
282   #else
283  	/*
284  	 * We don't have any getnetbyname_r(); either we have a
285  	 * getnetbyname() that uses thread-specific data, in which
286  	 * case we're thread-safe (sufficiently recent FreeBSD,
287  	 * sufficiently recent Darwin-based OS, sufficiently recent
288  	 * HP-UX, sufficiently recent Tru64 UNIX), or we have the
289  	 * traditional getnetbyname() (everything else, including
290  	 * current NetBSD and OpenBSD), in which case we're not
291  	 * thread-safe.
292  	 */
293 	np = getnetbyname(name);
294   #endif
295 	if (np != NULL)
296 		return np->n_net;
297 	else
298 		return 0;
299 #endif /* _WIN32 */
300 }
301 
302 /*
303  * Convert a port name to its port and protocol numbers.
304  * We assume only TCP or UDP.
305  * Return 0 upon failure.
306  */
307 int
pcap_nametoport(const char * name,int * port,int * proto)308 pcap_nametoport(const char *name, int *port, int *proto)
309 {
310 	struct addrinfo hints, *res, *ai;
311 	int error;
312 	struct sockaddr_in *in4;
313 #ifdef INET6
314 	struct sockaddr_in6 *in6;
315 #endif
316 	int tcp_port = -1;
317 	int udp_port = -1;
318 
319 	/*
320 	 * We check for both TCP and UDP in case there are
321 	 * ambiguous entries.
322 	 */
323 	memset(&hints, 0, sizeof(hints));
324 	hints.ai_family = PF_UNSPEC;
325 	hints.ai_socktype = SOCK_STREAM;
326 	hints.ai_protocol = IPPROTO_TCP;
327 	error = getaddrinfo(NULL, name, &hints, &res);
328 	if (error != 0) {
329 		if (error != EAI_NONAME &&
330 		    error != EAI_SERVICE) {
331 			/*
332 			 * This is a real error, not just "there's
333 			 * no such service name".
334 			 * XXX - this doesn't return an error string.
335 			 */
336 			return 0;
337 		}
338 	} else {
339 		/*
340 		 * OK, we found it.  Did it find anything?
341 		 */
342 		for (ai = res; ai != NULL; ai = ai->ai_next) {
343 			/*
344 			 * Does it have an address?
345 			 */
346 			if (ai->ai_addr != NULL) {
347 				/*
348 				 * Yes.  Get a port number; we're done.
349 				 */
350 				if (ai->ai_addr->sa_family == AF_INET) {
351 					in4 = (struct sockaddr_in *)ai->ai_addr;
352 					tcp_port = ntohs(in4->sin_port);
353 					break;
354 				}
355 #ifdef INET6
356 				if (ai->ai_addr->sa_family == AF_INET6) {
357 					in6 = (struct sockaddr_in6 *)ai->ai_addr;
358 					tcp_port = ntohs(in6->sin6_port);
359 					break;
360 				}
361 #endif
362 			}
363 		}
364 		freeaddrinfo(res);
365 	}
366 
367 	memset(&hints, 0, sizeof(hints));
368 	hints.ai_family = PF_UNSPEC;
369 	hints.ai_socktype = SOCK_DGRAM;
370 	hints.ai_protocol = IPPROTO_UDP;
371 	error = getaddrinfo(NULL, name, &hints, &res);
372 	if (error != 0) {
373 		if (error != EAI_NONAME &&
374 		    error != EAI_SERVICE) {
375 			/*
376 			 * This is a real error, not just "there's
377 			 * no such service name".
378 			 * XXX - this doesn't return an error string.
379 			 */
380 			return 0;
381 		}
382 	} else {
383 		/*
384 		 * OK, we found it.  Did it find anything?
385 		 */
386 		for (ai = res; ai != NULL; ai = ai->ai_next) {
387 			/*
388 			 * Does it have an address?
389 			 */
390 			if (ai->ai_addr != NULL) {
391 				/*
392 				 * Yes.  Get a port number; we're done.
393 				 */
394 				if (ai->ai_addr->sa_family == AF_INET) {
395 					in4 = (struct sockaddr_in *)ai->ai_addr;
396 					udp_port = ntohs(in4->sin_port);
397 					break;
398 				}
399 #ifdef INET6
400 				if (ai->ai_addr->sa_family == AF_INET6) {
401 					in6 = (struct sockaddr_in6 *)ai->ai_addr;
402 					udp_port = ntohs(in6->sin6_port);
403 					break;
404 				}
405 #endif
406 			}
407 		}
408 		freeaddrinfo(res);
409 	}
410 
411 	/*
412 	 * We need to check /etc/services for ambiguous entries.
413 	 * If we find an ambiguous entry, and it has the
414 	 * same port number, change the proto to PROTO_UNDEF
415 	 * so both TCP and UDP will be checked.
416 	 */
417 	if (tcp_port >= 0) {
418 		*port = tcp_port;
419 		*proto = IPPROTO_TCP;
420 		if (udp_port >= 0) {
421 			if (udp_port == tcp_port)
422 				*proto = PROTO_UNDEF;
423 #ifdef notdef
424 			else
425 				/* Can't handle ambiguous names that refer
426 				   to different port numbers. */
427 				warning("ambiguous port %s in /etc/services",
428 					name);
429 #endif
430 		}
431 		return 1;
432 	}
433 	if (udp_port >= 0) {
434 		*port = udp_port;
435 		*proto = IPPROTO_UDP;
436 		return 1;
437 	}
438 #if defined(ultrix) || defined(__osf__)
439 	/* Special hack in case NFS isn't in /etc/services */
440 	if (strcmp(name, "nfs") == 0) {
441 		*port = 2049;
442 		*proto = PROTO_UNDEF;
443 		return 1;
444 	}
445 #endif
446 	return 0;
447 }
448 
449 /*
450  * Convert a string in the form PPP-PPP, where correspond to ports, to
451  * a starting and ending port in a port range.
452  * Return 0 on failure.
453  */
454 int
pcap_nametoportrange(const char * name,int * port1,int * port2,int * proto)455 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
456 {
457 	u_int p1, p2;
458 	char *off, *cpy;
459 	int save_proto;
460 
461 	if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
462 		if ((cpy = strdup(name)) == NULL)
463 			return 0;
464 
465 		if ((off = strchr(cpy, '-')) == NULL) {
466 			free(cpy);
467 			return 0;
468 		}
469 
470 		*off = '\0';
471 
472 		if (pcap_nametoport(cpy, port1, proto) == 0) {
473 			free(cpy);
474 			return 0;
475 		}
476 		save_proto = *proto;
477 
478 		if (pcap_nametoport(off + 1, port2, proto) == 0) {
479 			free(cpy);
480 			return 0;
481 		}
482 		free(cpy);
483 
484 		if (*proto != save_proto)
485 			*proto = PROTO_UNDEF;
486 	} else {
487 		*port1 = p1;
488 		*port2 = p2;
489 		*proto = PROTO_UNDEF;
490 	}
491 
492 	return 1;
493 }
494 
495 /*
496  * XXX - not guaranteed to be thread-safe!  See below for platforms
497  * on which it is thread-safe and on which it isn't.
498  */
499 int
pcap_nametoproto(const char * str)500 pcap_nametoproto(const char *str)
501 {
502 	struct protoent *p;
503   #if defined(HAVE_LINUX_GETNETBYNAME_R)
504 	/*
505 	 * We have Linux's reentrant getprotobyname_r().
506 	 */
507 	struct protoent result_buf;
508 	char buf[1024];	/* arbitrary size */
509 	int err;
510 
511 	err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
512 	if (err != 0) {
513 		/*
514 		 * XXX - dynamically allocate the buffer, and make it
515 		 * bigger if we get ERANGE back?
516 		 */
517 		return 0;
518 	}
519   #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
520 	/*
521 	 * We have Solaris's and IRIX's reentrant getprotobyname_r().
522 	 */
523 	struct protoent result_buf;
524 	char buf[1024];	/* arbitrary size */
525 
526 	p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
527   #elif defined(HAVE_AIX_GETNETBYNAME_R)
528 	/*
529 	 * We have AIX's reentrant getprotobyname_r().
530 	 */
531 	struct protoent result_buf;
532 	struct protoent_data proto_data;
533 
534 	if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
535 		p = NULL;
536 	else
537 		p = &result_buf;
538   #else
539  	/*
540  	 * We don't have any getprotobyname_r(); either we have a
541  	 * getprotobyname() that uses thread-specific data, in which
542  	 * case we're thread-safe (sufficiently recent FreeBSD,
543  	 * sufficiently recent Darwin-based OS, sufficiently recent
544  	 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
545 	 * the traditional getprotobyname() (everything else, including
546  	 * current NetBSD and OpenBSD), in which case we're not
547  	 * thread-safe.
548  	 */
549 	p = getprotobyname(str);
550   #endif
551 	if (p != 0)
552 		return p->p_proto;
553 	else
554 		return PROTO_UNDEF;
555 }
556 
557 #include "ethertype.h"
558 
559 struct eproto {
560 	const char *s;
561 	u_short p;
562 };
563 
564 /*
565  * Static data base of ether protocol types.
566  * tcpdump used to import this, and it's declared as an export on
567  * Debian, at least, so make it a public symbol, even though we
568  * don't officially export it by declaring it in a header file.
569  * (Programs *should* do this themselves, as tcpdump now does.)
570  *
571  * We declare it here, right before defining it, to squelch any
572  * warnings we might get from compilers about the lack of a
573  * declaration.
574  */
575 PCAP_API struct eproto eproto_db[];
576 PCAP_API_DEF struct eproto eproto_db[] = {
577 	{ "pup", ETHERTYPE_PUP },
578 	{ "xns", ETHERTYPE_NS },
579 	{ "ip", ETHERTYPE_IP },
580 #ifdef INET6
581 	{ "ip6", ETHERTYPE_IPV6 },
582 #endif
583 	{ "arp", ETHERTYPE_ARP },
584 	{ "rarp", ETHERTYPE_REVARP },
585 	{ "sprite", ETHERTYPE_SPRITE },
586 	{ "mopdl", ETHERTYPE_MOPDL },
587 	{ "moprc", ETHERTYPE_MOPRC },
588 	{ "decnet", ETHERTYPE_DN },
589 	{ "lat", ETHERTYPE_LAT },
590 	{ "sca", ETHERTYPE_SCA },
591 	{ "lanbridge", ETHERTYPE_LANBRIDGE },
592 	{ "vexp", ETHERTYPE_VEXP },
593 	{ "vprod", ETHERTYPE_VPROD },
594 	{ "atalk", ETHERTYPE_ATALK },
595 	{ "atalkarp", ETHERTYPE_AARP },
596 	{ "loopback", ETHERTYPE_LOOPBACK },
597 	{ "decdts", ETHERTYPE_DECDTS },
598 	{ "decdns", ETHERTYPE_DECDNS },
599 	{ (char *)0, 0 }
600 };
601 
602 int
pcap_nametoeproto(const char * s)603 pcap_nametoeproto(const char *s)
604 {
605 	struct eproto *p = eproto_db;
606 
607 	while (p->s != 0) {
608 		if (strcmp(p->s, s) == 0)
609 			return p->p;
610 		p += 1;
611 	}
612 	return PROTO_UNDEF;
613 }
614 
615 #include "llc.h"
616 
617 /* Static data base of LLC values. */
618 static struct eproto llc_db[] = {
619 	{ "iso", LLCSAP_ISONS },
620 	{ "stp", LLCSAP_8021D },
621 	{ "ipx", LLCSAP_IPX },
622 	{ "netbeui", LLCSAP_NETBEUI },
623 	{ (char *)0, 0 }
624 };
625 
626 int
pcap_nametollc(const char * s)627 pcap_nametollc(const char *s)
628 {
629 	struct eproto *p = llc_db;
630 
631 	while (p->s != 0) {
632 		if (strcmp(p->s, s) == 0)
633 			return p->p;
634 		p += 1;
635 	}
636 	return PROTO_UNDEF;
637 }
638 
639 /* Hex digit to 8-bit unsigned integer. */
640 static inline u_char
xdtoi(u_char c)641 xdtoi(u_char c)
642 {
643 	if (isdigit(c))
644 		return (u_char)(c - '0');
645 	else if (islower(c))
646 		return (u_char)(c - 'a' + 10);
647 	else
648 		return (u_char)(c - 'A' + 10);
649 }
650 
651 int
__pcap_atoin(const char * s,bpf_u_int32 * addr)652 __pcap_atoin(const char *s, bpf_u_int32 *addr)
653 {
654 	u_int n;
655 	int len;
656 
657 	*addr = 0;
658 	len = 0;
659 	for (;;) {
660 		n = 0;
661 		while (*s && *s != '.')
662 			n = n * 10 + *s++ - '0';
663 		*addr <<= 8;
664 		*addr |= n & 0xff;
665 		len += 8;
666 		if (*s == '\0')
667 			return len;
668 		++s;
669 	}
670 	/* NOTREACHED */
671 }
672 
673 int
__pcap_atodn(const char * s,bpf_u_int32 * addr)674 __pcap_atodn(const char *s, bpf_u_int32 *addr)
675 {
676 #define AREASHIFT 10
677 #define AREAMASK 0176000
678 #define NODEMASK 01777
679 
680 	u_int node, area;
681 
682 	if (sscanf(s, "%d.%d", &area, &node) != 2)
683 		return(0);
684 
685 	*addr = (area << AREASHIFT) & AREAMASK;
686 	*addr |= (node & NODEMASK);
687 
688 	return(32);
689 }
690 
691 /*
692  * Convert 's', which can have the one of the forms:
693  *
694  *	"xx:xx:xx:xx:xx:xx"
695  *	"xx.xx.xx.xx.xx.xx"
696  *	"xx-xx-xx-xx-xx-xx"
697  *	"xxxx.xxxx.xxxx"
698  *	"xxxxxxxxxxxx"
699  *
700  * (or various mixes of ':', '.', and '-') into a new
701  * ethernet address.  Assumes 's' is well formed.
702  */
703 u_char *
pcap_ether_aton(const char * s)704 pcap_ether_aton(const char *s)
705 {
706 	register u_char *ep, *e;
707 	register u_char d;
708 
709 	e = ep = (u_char *)malloc(6);
710 	if (e == NULL)
711 		return (NULL);
712 
713 	while (*s) {
714 		if (*s == ':' || *s == '.' || *s == '-')
715 			s += 1;
716 		d = xdtoi(*s++);
717 		if (isxdigit((unsigned char)*s)) {
718 			d <<= 4;
719 			d |= xdtoi(*s++);
720 		}
721 		*ep++ = d;
722 	}
723 
724 	return (e);
725 }
726 
727 #ifndef HAVE_ETHER_HOSTTON
728 /*
729  * Roll our own.
730  * XXX - not thread-safe, because pcap_next_etherent() isn't thread-
731  * safe!  Needs a mutex or a thread-safe pcap_next_etherent().
732  */
733 u_char *
pcap_ether_hostton(const char * name)734 pcap_ether_hostton(const char *name)
735 {
736 	register struct pcap_etherent *ep;
737 	register u_char *ap;
738 	static FILE *fp = NULL;
739 	static int init = 0;
740 
741 	if (!init) {
742 		fp = fopen(PCAP_ETHERS_FILE, "r");
743 		++init;
744 		if (fp == NULL)
745 			return (NULL);
746 	} else if (fp == NULL)
747 		return (NULL);
748 	else
749 		rewind(fp);
750 
751 	while ((ep = pcap_next_etherent(fp)) != NULL) {
752 		if (strcmp(ep->name, name) == 0) {
753 			ap = (u_char *)malloc(6);
754 			if (ap != NULL) {
755 				memcpy(ap, ep->addr, 6);
756 				return (ap);
757 			}
758 			break;
759 		}
760 	}
761 	return (NULL);
762 }
763 #else
764 /*
765  * Use the OS-supplied routine.
766  * This *should* be thread-safe; the API doesn't have a static buffer.
767  */
768 u_char *
pcap_ether_hostton(const char * name)769 pcap_ether_hostton(const char *name)
770 {
771 	register u_char *ap;
772 	u_char a[6];
773 
774 	ap = NULL;
775 	if (ether_hostton(name, (struct ether_addr *)a) == 0) {
776 		ap = (u_char *)malloc(6);
777 		if (ap != NULL)
778 			memcpy((char *)ap, (char *)a, 6);
779 	}
780 	return (ap);
781 }
782 #endif
783 
784 /*
785  * XXX - not guaranteed to be thread-safe!
786  */
787 int
788 #ifdef	DECNETLIB
__pcap_nametodnaddr(const char * name,u_short * res)789 __pcap_nametodnaddr(const char *name, u_short *res)
790 {
791 	struct nodeent *getnodebyname();
792 	struct nodeent *nep;
793 
794 	nep = getnodebyname(name);
795 	if (nep == ((struct nodeent *)0))
796 		return(0);
797 
798 	memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
799 	return(1);
800 #else
801 __pcap_nametodnaddr(const char *name _U_, u_short *res _U_)
802 {
803 	return(0);
804 #endif
805 }
806