xref: /dragonfly/contrib/libpcap/nametoaddr.c (revision cfd1aba3)
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 
25 #ifndef lint
26 static const char rcsid[] _U_ =
27     "@(#) $Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.83 2008-02-06 10:21:30 guy Exp $ (LBL)";
28 #endif
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 <pcap-stdinc.h>
41 
42 #else /* WIN32 */
43 
44 #include <sys/param.h>
45 #include <sys/types.h>				/* concession to AIX */
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 
49 #include <netinet/in.h>
50 #endif /* WIN32 */
51 
52 #ifndef WIN32
53 #ifdef HAVE_ETHER_HOSTTON
54 /*
55  * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
56  * ether_hostton()?
57  */
58 #ifdef HAVE_NETINET_IF_ETHER_H
59 struct mbuf;		/* Squelch compiler warnings on some platforms for */
60 struct rtentry;		/* declarations in <net/if.h> */
61 #include <net/if.h>	/* for "struct ifnet" in "struct arpcom" on Solaris */
62 #include <netinet/if_ether.h>
63 #endif /* HAVE_NETINET_IF_ETHER_H */
64 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
65 #include <netinet/ether.h>
66 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
67 #endif /* HAVE_ETHER_HOSTTON */
68 #include <arpa/inet.h>
69 #include <netdb.h>
70 #endif /* WIN32 */
71 
72 #include <ctype.h>
73 #include <errno.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <stdio.h>
77 
78 #include "pcap-int.h"
79 
80 #include "gencode.h"
81 #include <pcap/namedb.h>
82 
83 #ifdef HAVE_OS_PROTO_H
84 #include "os-proto.h"
85 #endif
86 
87 #ifndef NTOHL
88 #define NTOHL(x) (x) = ntohl(x)
89 #define NTOHS(x) (x) = ntohs(x)
90 #endif
91 
92 static inline int xdtoi(int);
93 
94 /*
95  *  Convert host name to internet address.
96  *  Return 0 upon failure.
97  */
98 bpf_u_int32 **
99 pcap_nametoaddr(const char *name)
100 {
101 #ifndef h_addr
102 	static bpf_u_int32 *hlist[2];
103 #endif
104 	bpf_u_int32 **p;
105 	struct hostent *hp;
106 
107 	if ((hp = gethostbyname(name)) != NULL) {
108 #ifndef h_addr
109 		hlist[0] = (bpf_u_int32 *)hp->h_addr;
110 		NTOHL(hp->h_addr);
111 		return hlist;
112 #else
113 		for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
114 			NTOHL(**p);
115 		return (bpf_u_int32 **)hp->h_addr_list;
116 #endif
117 	}
118 	else
119 		return 0;
120 }
121 
122 #ifdef INET6
123 struct addrinfo *
124 pcap_nametoaddrinfo(const char *name)
125 {
126 	struct addrinfo hints, *res;
127 	int error;
128 
129 	memset(&hints, 0, sizeof(hints));
130 	hints.ai_family = PF_UNSPEC;
131 	hints.ai_socktype = SOCK_STREAM;	/*not really*/
132 	hints.ai_protocol = IPPROTO_TCP;	/*not really*/
133 	error = getaddrinfo(name, NULL, &hints, &res);
134 	if (error)
135 		return NULL;
136 	else
137 		return res;
138 }
139 #endif /*INET6*/
140 
141 /*
142  *  Convert net name to internet address.
143  *  Return 0 upon failure.
144  */
145 bpf_u_int32
146 pcap_nametonetaddr(const char *name)
147 {
148 #ifndef WIN32
149 	struct netent *np;
150 
151 	if ((np = getnetbyname(name)) != NULL)
152 		return np->n_net;
153 	else
154 		return 0;
155 #else
156 	/*
157 	 * There's no "getnetbyname()" on Windows.
158 	 */
159 	return 0;
160 #endif
161 }
162 
163 /*
164  * Convert a port name to its port and protocol numbers.
165  * We assume only TCP or UDP.
166  * Return 0 upon failure.
167  */
168 int
169 pcap_nametoport(const char *name, int *port, int *proto)
170 {
171 	struct servent *sp;
172 	int tcp_port = -1;
173 	int udp_port = -1;
174 
175 	/*
176 	 * We need to check /etc/services for ambiguous entries.
177 	 * If we find the ambiguous entry, and it has the
178 	 * same port number, change the proto to PROTO_UNDEF
179 	 * so both TCP and UDP will be checked.
180 	 */
181 	sp = getservbyname(name, "tcp");
182 	if (sp != NULL) tcp_port = ntohs(sp->s_port);
183 	sp = getservbyname(name, "udp");
184 	if (sp != NULL) udp_port = ntohs(sp->s_port);
185 	if (tcp_port >= 0) {
186 		*port = tcp_port;
187 		*proto = IPPROTO_TCP;
188 		if (udp_port >= 0) {
189 			if (udp_port == tcp_port)
190 				*proto = PROTO_UNDEF;
191 #ifdef notdef
192 			else
193 				/* Can't handle ambiguous names that refer
194 				   to different port numbers. */
195 				warning("ambiguous port %s in /etc/services",
196 					name);
197 #endif
198 		}
199 		return 1;
200 	}
201 	if (udp_port >= 0) {
202 		*port = udp_port;
203 		*proto = IPPROTO_UDP;
204 		return 1;
205 	}
206 #if defined(ultrix) || defined(__osf__)
207 	/* Special hack in case NFS isn't in /etc/services */
208 	if (strcmp(name, "nfs") == 0) {
209 		*port = 2049;
210 		*proto = PROTO_UNDEF;
211 		return 1;
212 	}
213 #endif
214 	return 0;
215 }
216 
217 /*
218  * Convert a string in the form PPP-PPP, where correspond to ports, to
219  * a starting and ending port in a port range.
220  * Return 0 on failure.
221  */
222 int
223 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
224 {
225 	u_int p1, p2;
226 	char *off, *cpy;
227 	int save_proto;
228 
229 	if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
230 		if ((cpy = strdup(name)) == NULL)
231 			return 0;
232 
233 		if ((off = strchr(cpy, '-')) == NULL) {
234 			free(cpy);
235 			return 0;
236 		}
237 
238 		*off = '\0';
239 
240 		if (pcap_nametoport(cpy, port1, proto) == 0) {
241 			free(cpy);
242 			return 0;
243 		}
244 		save_proto = *proto;
245 
246 		if (pcap_nametoport(off + 1, port2, proto) == 0) {
247 			free(cpy);
248 			return 0;
249 		}
250 
251 		if (*proto != save_proto)
252 			*proto = PROTO_UNDEF;
253 	} else {
254 		*port1 = p1;
255 		*port2 = p2;
256 		*proto = PROTO_UNDEF;
257 	}
258 
259 	return 1;
260 }
261 
262 int
263 pcap_nametoproto(const char *str)
264 {
265 	struct protoent *p;
266 
267 	p = getprotobyname(str);
268 	if (p != 0)
269 		return p->p_proto;
270 	else
271 		return PROTO_UNDEF;
272 }
273 
274 #include "ethertype.h"
275 
276 struct eproto {
277 	const char *s;
278 	u_short p;
279 };
280 
281 /* Static data base of ether protocol types. */
282 struct eproto eproto_db[] = {
283 	{ "pup", ETHERTYPE_PUP },
284 	{ "xns", ETHERTYPE_NS },
285 	{ "ip", ETHERTYPE_IP },
286 #ifdef INET6
287 	{ "ip6", ETHERTYPE_IPV6 },
288 #endif
289 	{ "arp", ETHERTYPE_ARP },
290 	{ "rarp", ETHERTYPE_REVARP },
291 	{ "sprite", ETHERTYPE_SPRITE },
292 	{ "mopdl", ETHERTYPE_MOPDL },
293 	{ "moprc", ETHERTYPE_MOPRC },
294 	{ "decnet", ETHERTYPE_DN },
295 	{ "lat", ETHERTYPE_LAT },
296 	{ "sca", ETHERTYPE_SCA },
297 	{ "lanbridge", ETHERTYPE_LANBRIDGE },
298 	{ "vexp", ETHERTYPE_VEXP },
299 	{ "vprod", ETHERTYPE_VPROD },
300 	{ "atalk", ETHERTYPE_ATALK },
301 	{ "atalkarp", ETHERTYPE_AARP },
302 	{ "loopback", ETHERTYPE_LOOPBACK },
303 	{ "decdts", ETHERTYPE_DECDTS },
304 	{ "decdns", ETHERTYPE_DECDNS },
305 	{ (char *)0, 0 }
306 };
307 
308 int
309 pcap_nametoeproto(const char *s)
310 {
311 	struct eproto *p = eproto_db;
312 
313 	while (p->s != 0) {
314 		if (strcmp(p->s, s) == 0)
315 			return p->p;
316 		p += 1;
317 	}
318 	return PROTO_UNDEF;
319 }
320 
321 #include "llc.h"
322 
323 /* Static data base of LLC values. */
324 static struct eproto llc_db[] = {
325 	{ "iso", LLCSAP_ISONS },
326 	{ "stp", LLCSAP_8021D },
327 	{ "ipx", LLCSAP_IPX },
328 	{ "netbeui", LLCSAP_NETBEUI },
329 	{ (char *)0, 0 }
330 };
331 
332 int
333 pcap_nametollc(const char *s)
334 {
335 	struct eproto *p = llc_db;
336 
337 	while (p->s != 0) {
338 		if (strcmp(p->s, s) == 0)
339 			return p->p;
340 		p += 1;
341 	}
342 	return PROTO_UNDEF;
343 }
344 
345 /* Hex digit to integer. */
346 static inline int
347 xdtoi(c)
348 	register int c;
349 {
350 	if (isdigit(c))
351 		return c - '0';
352 	else if (islower(c))
353 		return c - 'a' + 10;
354 	else
355 		return c - 'A' + 10;
356 }
357 
358 int
359 __pcap_atoin(const char *s, bpf_u_int32 *addr)
360 {
361 	u_int n;
362 	int len;
363 
364 	*addr = 0;
365 	len = 0;
366 	while (1) {
367 		n = 0;
368 		while (*s && *s != '.')
369 			n = n * 10 + *s++ - '0';
370 		*addr <<= 8;
371 		*addr |= n & 0xff;
372 		len += 8;
373 		if (*s == '\0')
374 			return len;
375 		++s;
376 	}
377 	/* NOTREACHED */
378 }
379 
380 int
381 __pcap_atodn(const char *s, bpf_u_int32 *addr)
382 {
383 #define AREASHIFT 10
384 #define AREAMASK 0176000
385 #define NODEMASK 01777
386 
387 	u_int node, area;
388 
389 	if (sscanf(s, "%d.%d", &area, &node) != 2)
390 		bpf_error("malformed decnet address '%s'", s);
391 
392 	*addr = (area << AREASHIFT) & AREAMASK;
393 	*addr |= (node & NODEMASK);
394 
395 	return(32);
396 }
397 
398 /*
399  * Convert 's', which can have the one of the forms:
400  *
401  *	"xx:xx:xx:xx:xx:xx"
402  *	"xx.xx.xx.xx.xx.xx"
403  *	"xx-xx-xx-xx-xx-xx"
404  *	"xxxx.xxxx.xxxx"
405  *	"xxxxxxxxxxxx"
406  *
407  * (or various mixes of ':', '.', and '-') into a new
408  * ethernet address.  Assumes 's' is well formed.
409  */
410 u_char *
411 pcap_ether_aton(const char *s)
412 {
413 	register u_char *ep, *e;
414 	register u_int d;
415 
416 	e = ep = (u_char *)malloc(6);
417 
418 	while (*s) {
419 		if (*s == ':' || *s == '.' || *s == '-')
420 			s += 1;
421 		d = xdtoi(*s++);
422 		if (isxdigit((unsigned char)*s)) {
423 			d <<= 4;
424 			d |= xdtoi(*s++);
425 		}
426 		*ep++ = d;
427 	}
428 
429 	return (e);
430 }
431 
432 #ifndef HAVE_ETHER_HOSTTON
433 /* Roll our own */
434 u_char *
435 pcap_ether_hostton(const char *name)
436 {
437 	register struct pcap_etherent *ep;
438 	register u_char *ap;
439 	static FILE *fp = NULL;
440 	static int init = 0;
441 
442 	if (!init) {
443 		fp = fopen(PCAP_ETHERS_FILE, "r");
444 		++init;
445 		if (fp == NULL)
446 			return (NULL);
447 	} else if (fp == NULL)
448 		return (NULL);
449 	else
450 		rewind(fp);
451 
452 	while ((ep = pcap_next_etherent(fp)) != NULL) {
453 		if (strcmp(ep->name, name) == 0) {
454 			ap = (u_char *)malloc(6);
455 			if (ap != NULL) {
456 				memcpy(ap, ep->addr, 6);
457 				return (ap);
458 			}
459 			break;
460 		}
461 	}
462 	return (NULL);
463 }
464 #else
465 
466 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
467 #ifndef HAVE_STRUCT_ETHER_ADDR
468 struct ether_addr {
469 	unsigned char ether_addr_octet[6];
470 };
471 #endif
472 extern int ether_hostton(const char *, struct ether_addr *);
473 #endif
474 
475 /* Use the os supplied routines */
476 u_char *
477 pcap_ether_hostton(const char *name)
478 {
479 	register u_char *ap;
480 	u_char a[6];
481 
482 	ap = NULL;
483 	if (ether_hostton(name, (struct ether_addr *)a) == 0) {
484 		ap = (u_char *)malloc(6);
485 		if (ap != NULL)
486 			memcpy((char *)ap, (char *)a, 6);
487 	}
488 	return (ap);
489 }
490 #endif
491 
492 u_short
493 __pcap_nametodnaddr(const char *name)
494 {
495 #ifdef	DECNETLIB
496 	struct nodeent *getnodebyname();
497 	struct nodeent *nep;
498 	unsigned short res;
499 
500 	nep = getnodebyname(name);
501 	if (nep == ((struct nodeent *)0))
502 		bpf_error("unknown decnet host name '%s'\n", name);
503 
504 	memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
505 	return(res);
506 #else
507 	bpf_error("decnet name support not included, '%s' cannot be translated\n",
508 		name);
509 	return(0);
510 #endif
511 }
512