xref: /freebsd/contrib/libpcap/nametoaddr.c (revision b0b1dbdd)
1 /*
2  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * Name to id translation routines used by the scanner.
22  * These functions are not time critical.
23  *
24  * $FreeBSD$
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #ifdef DECNETLIB
32 #include <sys/types.h>
33 #include <netdnet/dnetdb.h>
34 #endif
35 
36 #ifdef _WIN32
37 #include <pcap-stdinc.h>
38 
39 #ifdef INET6
40 /*
41  * To quote the MSDN page for getaddrinfo() at
42  *
43  *    https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
44  *
45  * "Support for getaddrinfo on Windows 2000 and older versions
46  * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
47  * later. To execute an application that uses this function on earlier
48  * versions of Windows, then you need to include the Ws2tcpip.h and
49  * Wspiapi.h files. When the Wspiapi.h include file is added, the
50  * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
51  * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
52  * function is implemented in such a way that if the Ws2_32.dll or the
53  * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
54  * Preview for Windows 2000) does not include getaddrinfo, then a
55  * version of getaddrinfo is implemented inline based on code in the
56  * Wspiapi.h header file. This inline code will be used on older Windows
57  * platforms that do not natively support the getaddrinfo function."
58  *
59  * We use getaddrinfo(), so we include Wspiapi.h here.  pcap-stdinc.h
60  * includes Ws2tcpip.h, so we don't need to include it ourselves.
61  */
62 #include <Wspiapi.h>
63 #endif
64 
65 #else /* _WIN32 */
66 
67 #include <sys/param.h>
68 #include <sys/types.h>				/* concession to AIX */
69 #include <sys/socket.h>
70 #include <sys/time.h>
71 
72 #include <netinet/in.h>
73 #endif /* _WIN32 */
74 
75 #ifndef _WIN32
76 #ifdef HAVE_ETHER_HOSTTON
77 /*
78  * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
79  * ether_hostton()?
80  */
81 #ifdef HAVE_NETINET_IF_ETHER_H
82 struct mbuf;		/* Squelch compiler warnings on some platforms for */
83 struct rtentry;		/* declarations in <net/if.h> */
84 #include <net/if.h>	/* for "struct ifnet" in "struct arpcom" on Solaris */
85 #include <netinet/if_ether.h>
86 #endif /* HAVE_NETINET_IF_ETHER_H */
87 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
88 #include <netinet/ether.h>
89 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
90 #endif /* HAVE_ETHER_HOSTTON */
91 #include <arpa/inet.h>
92 #include <netdb.h>
93 #endif /* _WIN32 */
94 
95 #include <ctype.h>
96 #include <errno.h>
97 #include <stdlib.h>
98 #include <string.h>
99 #include <stdio.h>
100 
101 #include "pcap-int.h"
102 
103 #include "gencode.h"
104 #include <pcap/namedb.h>
105 #include "nametoaddr.h"
106 
107 #ifdef HAVE_OS_PROTO_H
108 #include "os-proto.h"
109 #endif
110 
111 #ifndef NTOHL
112 #define NTOHL(x) (x) = ntohl(x)
113 #define NTOHS(x) (x) = ntohs(x)
114 #endif
115 
116 static inline int xdtoi(int);
117 
118 /*
119  *  Convert host name to internet address.
120  *  Return 0 upon failure.
121  */
122 bpf_u_int32 **
123 pcap_nametoaddr(const char *name)
124 {
125 #ifndef h_addr
126 	static bpf_u_int32 *hlist[2];
127 #endif
128 	bpf_u_int32 **p;
129 	struct hostent *hp;
130 
131 	if ((hp = gethostbyname(name)) != NULL) {
132 #ifndef h_addr
133 		hlist[0] = (bpf_u_int32 *)hp->h_addr;
134 		NTOHL(hp->h_addr);
135 		return hlist;
136 #else
137 		for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
138 			NTOHL(**p);
139 		return (bpf_u_int32 **)hp->h_addr_list;
140 #endif
141 	}
142 	else
143 		return 0;
144 }
145 
146 #ifdef INET6
147 struct addrinfo *
148 pcap_nametoaddrinfo(const char *name)
149 {
150 	struct addrinfo hints, *res;
151 	int error;
152 
153 	memset(&hints, 0, sizeof(hints));
154 	hints.ai_family = PF_UNSPEC;
155 	hints.ai_socktype = SOCK_STREAM;	/*not really*/
156 	hints.ai_protocol = IPPROTO_TCP;	/*not really*/
157 	error = getaddrinfo(name, NULL, &hints, &res);
158 	if (error)
159 		return NULL;
160 	else
161 		return res;
162 }
163 #endif /*INET6*/
164 
165 /*
166  *  Convert net name to internet address.
167  *  Return 0 upon failure.
168  */
169 bpf_u_int32
170 pcap_nametonetaddr(const char *name)
171 {
172 #ifndef _WIN32
173 	struct netent *np;
174 
175 	if ((np = getnetbyname(name)) != NULL)
176 		return np->n_net;
177 	else
178 		return 0;
179 #else
180 	/*
181 	 * There's no "getnetbyname()" on Windows.
182 	 *
183 	 * XXX - I guess we could use the BSD code to read
184 	 * C:\Windows\System32\drivers\etc/networks, assuming
185 	 * that's its home on all the versions of Windows
186 	 * we use, but that file probably just has the loopback
187 	 * network on 127/24 on 99 44/100% of Windows machines.
188 	 *
189 	 * (Heck, these days it probably just has that on 99 44/100%
190 	 * of *UN*X* machines.)
191 	 */
192 	return 0;
193 #endif
194 }
195 
196 /*
197  * Convert a port name to its port and protocol numbers.
198  * We assume only TCP or UDP.
199  * Return 0 upon failure.
200  */
201 int
202 pcap_nametoport(const char *name, int *port, int *proto)
203 {
204 	struct servent *sp;
205 	int tcp_port = -1;
206 	int udp_port = -1;
207 
208 	/*
209 	 * We need to check /etc/services for ambiguous entries.
210 	 * If we find the ambiguous entry, and it has the
211 	 * same port number, change the proto to PROTO_UNDEF
212 	 * so both TCP and UDP will be checked.
213 	 */
214 	sp = getservbyname(name, "tcp");
215 	if (sp != NULL) tcp_port = ntohs(sp->s_port);
216 	sp = getservbyname(name, "udp");
217 	if (sp != NULL) udp_port = ntohs(sp->s_port);
218 	if (tcp_port >= 0) {
219 		*port = tcp_port;
220 		*proto = IPPROTO_TCP;
221 		if (udp_port >= 0) {
222 			if (udp_port == tcp_port)
223 				*proto = PROTO_UNDEF;
224 #ifdef notdef
225 			else
226 				/* Can't handle ambiguous names that refer
227 				   to different port numbers. */
228 				warning("ambiguous port %s in /etc/services",
229 					name);
230 #endif
231 		}
232 		return 1;
233 	}
234 	if (udp_port >= 0) {
235 		*port = udp_port;
236 		*proto = IPPROTO_UDP;
237 		return 1;
238 	}
239 #if defined(ultrix) || defined(__osf__)
240 	/* Special hack in case NFS isn't in /etc/services */
241 	if (strcmp(name, "nfs") == 0) {
242 		*port = 2049;
243 		*proto = PROTO_UNDEF;
244 		return 1;
245 	}
246 #endif
247 	return 0;
248 }
249 
250 /*
251  * Convert a string in the form PPP-PPP, where correspond to ports, to
252  * a starting and ending port in a port range.
253  * Return 0 on failure.
254  */
255 int
256 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
257 {
258 	u_int p1, p2;
259 	char *off, *cpy;
260 	int save_proto;
261 
262 	if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
263 		if ((cpy = strdup(name)) == NULL)
264 			return 0;
265 
266 		if ((off = strchr(cpy, '-')) == NULL) {
267 			free(cpy);
268 			return 0;
269 		}
270 
271 		*off = '\0';
272 
273 		if (pcap_nametoport(cpy, port1, proto) == 0) {
274 			free(cpy);
275 			return 0;
276 		}
277 		save_proto = *proto;
278 
279 		if (pcap_nametoport(off + 1, port2, proto) == 0) {
280 			free(cpy);
281 			return 0;
282 		}
283 		free(cpy);
284 
285 		if (*proto != save_proto)
286 			*proto = PROTO_UNDEF;
287 	} else {
288 		*port1 = p1;
289 		*port2 = p2;
290 		*proto = PROTO_UNDEF;
291 	}
292 
293 	return 1;
294 }
295 
296 int
297 pcap_nametoproto(const char *str)
298 {
299 	struct protoent *p;
300 
301 	p = getprotobyname(str);
302 	if (p != 0)
303 		return p->p_proto;
304 	else
305 		return PROTO_UNDEF;
306 }
307 
308 #include "ethertype.h"
309 
310 struct eproto {
311 	const char *s;
312 	u_short p;
313 };
314 
315 /*
316  * Static data base of ether protocol types.
317  * tcpdump used to import this, and it's declared as an export on
318  * Debian, at least, so make it a public symbol, even though we
319  * don't officially export it by declaring it in a header file.
320  * (Programs *should* do this themselves, as tcpdump now does.)
321  */
322 PCAP_API_DEF struct eproto eproto_db[] = {
323 #if 0
324 	/* The FreeBSD elf linker generates a request to copy this array
325 	 * (including its size) when you link with -lpcap.  In order to
326 	 * not bump the major version number of this libpcap.so, we need
327 	 * to ensure that the array stays the same size.  Since PUP is
328 	 * likely never seen in real life any more, it's the first to
329 	 * be sacrificed (in favor of ip6).
330 	 */
331 	{ "pup", ETHERTYPE_PUP },
332 #endif
333 	{ "xns", ETHERTYPE_NS },
334 	{ "ip", ETHERTYPE_IP },
335 #ifdef INET6
336 	{ "ip6", ETHERTYPE_IPV6 },
337 #endif
338 	{ "arp", ETHERTYPE_ARP },
339 	{ "rarp", ETHERTYPE_REVARP },
340 	{ "sprite", ETHERTYPE_SPRITE },
341 	{ "mopdl", ETHERTYPE_MOPDL },
342 	{ "moprc", ETHERTYPE_MOPRC },
343 	{ "decnet", ETHERTYPE_DN },
344 	{ "lat", ETHERTYPE_LAT },
345 	{ "sca", ETHERTYPE_SCA },
346 	{ "lanbridge", ETHERTYPE_LANBRIDGE },
347 	{ "vexp", ETHERTYPE_VEXP },
348 	{ "vprod", ETHERTYPE_VPROD },
349 	{ "atalk", ETHERTYPE_ATALK },
350 	{ "atalkarp", ETHERTYPE_AARP },
351 	{ "loopback", ETHERTYPE_LOOPBACK },
352 	{ "decdts", ETHERTYPE_DECDTS },
353 	{ "decdns", ETHERTYPE_DECDNS },
354 	{ (char *)0, 0 }
355 };
356 
357 int
358 pcap_nametoeproto(const char *s)
359 {
360 	struct eproto *p = eproto_db;
361 
362 	while (p->s != 0) {
363 		if (strcmp(p->s, s) == 0)
364 			return p->p;
365 		p += 1;
366 	}
367 	return PROTO_UNDEF;
368 }
369 
370 #include "llc.h"
371 
372 /* Static data base of LLC values. */
373 static struct eproto llc_db[] = {
374 	{ "iso", LLCSAP_ISONS },
375 	{ "stp", LLCSAP_8021D },
376 	{ "ipx", LLCSAP_IPX },
377 	{ "netbeui", LLCSAP_NETBEUI },
378 	{ (char *)0, 0 }
379 };
380 
381 int
382 pcap_nametollc(const char *s)
383 {
384 	struct eproto *p = llc_db;
385 
386 	while (p->s != 0) {
387 		if (strcmp(p->s, s) == 0)
388 			return p->p;
389 		p += 1;
390 	}
391 	return PROTO_UNDEF;
392 }
393 
394 /* Hex digit to integer. */
395 static inline int
396 xdtoi(c)
397 	register int c;
398 {
399 	if (isdigit(c))
400 		return c - '0';
401 	else if (islower(c))
402 		return c - 'a' + 10;
403 	else
404 		return c - 'A' + 10;
405 }
406 
407 int
408 __pcap_atoin(const char *s, bpf_u_int32 *addr)
409 {
410 	u_int n;
411 	int len;
412 
413 	*addr = 0;
414 	len = 0;
415 	while (1) {
416 		n = 0;
417 		while (*s && *s != '.')
418 			n = n * 10 + *s++ - '0';
419 		*addr <<= 8;
420 		*addr |= n & 0xff;
421 		len += 8;
422 		if (*s == '\0')
423 			return len;
424 		++s;
425 	}
426 	/* NOTREACHED */
427 }
428 
429 int
430 __pcap_atodn(const char *s, bpf_u_int32 *addr)
431 {
432 #define AREASHIFT 10
433 #define AREAMASK 0176000
434 #define NODEMASK 01777
435 
436 	u_int node, area;
437 
438 	if (sscanf(s, "%d.%d", &area, &node) != 2)
439 		return(0);
440 
441 	*addr = (area << AREASHIFT) & AREAMASK;
442 	*addr |= (node & NODEMASK);
443 
444 	return(32);
445 }
446 
447 /*
448  * Convert 's', which can have the one of the forms:
449  *
450  *	"xx:xx:xx:xx:xx:xx"
451  *	"xx.xx.xx.xx.xx.xx"
452  *	"xx-xx-xx-xx-xx-xx"
453  *	"xxxx.xxxx.xxxx"
454  *	"xxxxxxxxxxxx"
455  *
456  * (or various mixes of ':', '.', and '-') into a new
457  * ethernet address.  Assumes 's' is well formed.
458  */
459 u_char *
460 pcap_ether_aton(const char *s)
461 {
462 	register u_char *ep, *e;
463 	register u_int d;
464 
465 	e = ep = (u_char *)malloc(6);
466 	if (e == NULL)
467 		return (NULL);
468 
469 	while (*s) {
470 		if (*s == ':' || *s == '.' || *s == '-')
471 			s += 1;
472 		d = xdtoi(*s++);
473 		if (isxdigit((unsigned char)*s)) {
474 			d <<= 4;
475 			d |= xdtoi(*s++);
476 		}
477 		*ep++ = d;
478 	}
479 
480 	return (e);
481 }
482 
483 #ifndef HAVE_ETHER_HOSTTON
484 /* Roll our own */
485 u_char *
486 pcap_ether_hostton(const char *name)
487 {
488 	register struct pcap_etherent *ep;
489 	register u_char *ap;
490 	static FILE *fp = NULL;
491 	static int init = 0;
492 
493 	if (!init) {
494 		fp = fopen(PCAP_ETHERS_FILE, "r");
495 		++init;
496 		if (fp == NULL)
497 			return (NULL);
498 	} else if (fp == NULL)
499 		return (NULL);
500 	else
501 		rewind(fp);
502 
503 	while ((ep = pcap_next_etherent(fp)) != NULL) {
504 		if (strcmp(ep->name, name) == 0) {
505 			ap = (u_char *)malloc(6);
506 			if (ap != NULL) {
507 				memcpy(ap, ep->addr, 6);
508 				return (ap);
509 			}
510 			break;
511 		}
512 	}
513 	return (NULL);
514 }
515 #else
516 
517 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
518 #ifndef HAVE_STRUCT_ETHER_ADDR
519 struct ether_addr {
520 	unsigned char ether_addr_octet[6];
521 };
522 #endif
523 extern int ether_hostton(const char *, struct ether_addr *);
524 #endif
525 
526 /* Use the os supplied routines */
527 u_char *
528 pcap_ether_hostton(const char *name)
529 {
530 	register u_char *ap;
531 	u_char a[6];
532 
533 	ap = NULL;
534 	if (ether_hostton(name, (struct ether_addr *)a) == 0) {
535 		ap = (u_char *)malloc(6);
536 		if (ap != NULL)
537 			memcpy((char *)ap, (char *)a, 6);
538 	}
539 	return (ap);
540 }
541 #endif
542 
543 int
544 __pcap_nametodnaddr(const char *name, u_short *res)
545 {
546 #ifdef	DECNETLIB
547 	struct nodeent *getnodebyname();
548 	struct nodeent *nep;
549 
550 	nep = getnodebyname(name);
551 	if (nep == ((struct nodeent *)0))
552 		return(0);
553 
554 	memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
555 	return(1);
556 #else
557 	return(0);
558 #endif
559 }
560