xref: /openbsd-src/lib/libpcap/nametoaddr.c (revision 9f33d8a69f338ef247968fdba5f70289dcd14c89)
1 /*	$OpenBSD: nametoaddr.c,v 1.11 2005/03/28 06:19:58 tedu Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
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/param.h>
28 #include <sys/types.h>				/* concession to AIX */
29 #include <sys/socket.h>
30 #include <sys/time.h>
31 
32 struct mbuf;
33 struct rtentry;
34 
35 #include <net/if.h>
36 #include <netinet/in.h>
37 #include <netinet/if_ether.h>
38 #include <arpa/inet.h>
39 #ifdef INET6
40 #include <netdb.h>
41 #include <sys/socket.h>
42 #endif /*INET6*/
43 
44 #include <ctype.h>
45 #include <errno.h>
46 #include <stdlib.h>
47 #include <memory.h>
48 #include <netdb.h>
49 #include <stdio.h>
50 
51 #include "pcap-int.h"
52 
53 #include "gencode.h"
54 #include <pcap-namedb.h>
55 
56 #ifdef HAVE_OS_PROTO_H
57 #include "os-proto.h"
58 #endif
59 
60 #ifndef NTOHL
61 #define NTOHL(x) (x) = ntohl(x)
62 #define NTOHS(x) (x) = ntohs(x)
63 #endif
64 
65 static __inline int xdtoi(int);
66 
67 /*
68  *  Convert host name to internet address.
69  *  Return 0 upon failure.
70  */
71 bpf_u_int32 **
72 pcap_nametoaddr(const char *name)
73 {
74 #ifndef h_addr
75 	static bpf_u_int32 *hlist[2];
76 #endif
77 	bpf_u_int32 **p;
78 	struct hostent *hp;
79 
80 	if ((hp = gethostbyname(name)) != NULL) {
81 #ifndef h_addr
82 		hlist[0] = (bpf_u_int32 *)hp->h_addr;
83 		NTOHL(hp->h_addr);
84 		return hlist;
85 #else
86 		for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
87 			NTOHL(**p);
88 		return (bpf_u_int32 **)hp->h_addr_list;
89 #endif
90 	}
91 	else
92 		return 0;
93 }
94 
95 #ifdef INET6
96 struct addrinfo *
97 pcap_nametoaddrinfo(const char *name)
98 {
99 	struct addrinfo hints, *res;
100 	int error;
101 
102 	memset(&hints, 0, sizeof(hints));
103 	hints.ai_family = PF_UNSPEC;
104 	hints.ai_socktype = SOCK_STREAM;	/*not really*/
105 	error = getaddrinfo(name, NULL, &hints, &res);
106 	if (error)
107 		return NULL;
108 	else
109 		return res;
110 }
111 #endif /*INET6*/
112 
113 /*
114  *  Convert net name to internet address.
115  *  Return 0 upon failure.
116  */
117 bpf_u_int32
118 pcap_nametonetaddr(const char *name)
119 {
120 	struct netent *np;
121 
122 	if ((np = getnetbyname(name)) != NULL)
123 		return np->n_net;
124 	else
125 		return 0;
126 }
127 
128 /*
129  * Convert a port name to its port and protocol numbers.
130  * We assume only TCP or UDP.
131  * Return 0 upon failure.
132  */
133 int
134 pcap_nametoport(const char *name, int *port, int *proto)
135 {
136 	struct servent *sp;
137 	char *other;
138 
139 	sp = getservbyname(name, (char *)0);
140 	if (sp != NULL) {
141 		NTOHS(sp->s_port);
142 		*port = sp->s_port;
143 		*proto = pcap_nametoproto(sp->s_proto);
144 		/*
145 		 * We need to check /etc/services for ambiguous entries.
146 		 * If we find the ambiguous entry, and it has the
147 		 * same port number, change the proto to PROTO_UNDEF
148 		 * so both TCP and UDP will be checked.
149 		 */
150 		if (*proto == IPPROTO_TCP)
151 			other = "udp";
152 		else
153 			other = "tcp";
154 
155 		sp = getservbyname(name, other);
156 		if (sp != 0) {
157 			NTOHS(sp->s_port);
158 #ifdef notdef
159 			if (*port != sp->s_port)
160 				/* Can't handle ambiguous names that refer
161 				   to different port numbers. */
162 				warning("ambiguous port %s in /etc/services",
163 					name);
164 #endif
165 			*proto = PROTO_UNDEF;
166 		}
167 		return 1;
168 	}
169 #if defined(ultrix) || defined(__osf__)
170 	/* Special hack in case NFS isn't in /etc/services */
171 	if (strcmp(name, "nfs") == 0) {
172 		*port = 2049;
173 		*proto = PROTO_UNDEF;
174 		return 1;
175 	}
176 #endif
177 	return 0;
178 }
179 
180 int
181 pcap_nametoproto(const char *str)
182 {
183 	struct protoent *p;
184 
185 	p = getprotobyname(str);
186 	if (p != 0)
187 		return p->p_proto;
188 	else
189 		return PROTO_UNDEF;
190 }
191 
192 #include "ethertype.h"
193 
194 struct eproto {
195 	char *s;
196 	u_short p;
197 };
198 
199 /* Static data base of ether protocol types. */
200 struct eproto eproto_db[] = {
201 	{ "pup", ETHERTYPE_PUP },
202 	{ "xns", ETHERTYPE_NS },
203 	{ "ip", ETHERTYPE_IP },
204 #ifdef INET6
205 	{ "ip6", ETHERTYPE_IPV6 },
206 #endif
207 	{ "arp", ETHERTYPE_ARP },
208 	{ "rarp", ETHERTYPE_REVARP },
209 	{ "sprite", ETHERTYPE_SPRITE },
210 	{ "mopdl", ETHERTYPE_MOPDL },
211 	{ "moprc", ETHERTYPE_MOPRC },
212 	{ "decnet", ETHERTYPE_DN },
213 	{ "lat", ETHERTYPE_LAT },
214 	{ "sca", ETHERTYPE_SCA },
215 	{ "lanbridge", ETHERTYPE_LANBRIDGE },
216 	{ "vexp", ETHERTYPE_VEXP },
217 	{ "vprod", ETHERTYPE_VPROD },
218 	{ "atalk", ETHERTYPE_ATALK },
219 	{ "atalkarp", ETHERTYPE_AARP },
220 	{ "loopback", ETHERTYPE_LOOPBACK },
221 	{ "decdts", ETHERTYPE_DECDTS },
222 	{ "decdns", ETHERTYPE_DECDNS },
223 	{ (char *)0, 0 }
224 };
225 
226 int
227 pcap_nametoeproto(const char *s)
228 {
229 	struct eproto *p = eproto_db;
230 
231 	while (p->s != 0) {
232 		if (strcmp(p->s, s) == 0)
233 			return p->p;
234 		p += 1;
235 	}
236 	return PROTO_UNDEF;
237 }
238 
239 /* Hex digit to integer. */
240 static __inline int
241 xdtoi(c)
242 	register int c;
243 {
244 	if (isdigit(c))
245 		return c - '0';
246 	else if (islower(c))
247 		return c - 'a' + 10;
248 	else
249 		return c - 'A' + 10;
250 }
251 
252 int
253 __pcap_atoin(const char *s, bpf_u_int32 *addr)
254 {
255 	u_int n;
256 	int len;
257 
258 	*addr = 0;
259 	len = 0;
260 	while (1) {
261 		n = 0;
262 		while (*s && *s != '.')
263 			n = n * 10 + *s++ - '0';
264 		*addr <<= 8;
265 		*addr |= n & 0xff;
266 		len += 8;
267 		if (*s == '\0')
268 			return len;
269 		++s;
270 	}
271 	/* NOTREACHED */
272 }
273 
274 int
275 __pcap_atodn(const char *s, bpf_u_int32 *addr)
276 {
277 #define AREASHIFT 10
278 #define AREAMASK 0176000
279 #define NODEMASK 01777
280 
281 	u_int node, area;
282 
283 	if (sscanf((char *)s, "%d.%d", &area, &node) != 2)
284 		bpf_error("malformed decnet address '%s'", s);
285 
286 	*addr = (area << AREASHIFT) & AREAMASK;
287 	*addr |= (node & NODEMASK);
288 
289 	return(32);
290 }
291 
292 /*
293  * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
294  * ethernet address.  Assumes 's' is well formed.
295  */
296 u_char *
297 pcap_ether_aton(const char *s)
298 {
299 	register u_char *ep, *e;
300 	register u_int d;
301 
302 	e = ep = (u_char *)malloc(6);
303 
304 	while (*s) {
305 		if (*s == ':')
306 			s += 1;
307 		d = xdtoi(*s++);
308 		if (isxdigit(*s)) {
309 			d <<= 4;
310 			d |= xdtoi(*s++);
311 		}
312 		*ep++ = d;
313 	}
314 
315 	return (e);
316 }
317 
318 #ifndef HAVE_ETHER_HOSTTON
319 /* Roll our own */
320 u_char *
321 pcap_ether_hostton(const char *name)
322 {
323 	register struct pcap_etherent *ep;
324 	register u_char *ap;
325 	static FILE *fp = NULL;
326 	static init = 0;
327 
328 	if (!init) {
329 		fp = fopen(PCAP_ETHERS_FILE, "r");
330 		++init;
331 		if (fp == NULL)
332 			return (NULL);
333 	} else if (fp == NULL)
334 		return (NULL);
335 	else
336 		rewind(fp);
337 
338 	while ((ep = pcap_next_etherent(fp)) != NULL) {
339 		if (strcmp(ep->name, name) == 0) {
340 			ap = (u_char *)malloc(6);
341 			if (ap != NULL) {
342 				memcpy(ap, ep->addr, 6);
343 				return (ap);
344 			}
345 			break;
346 		}
347 	}
348 	return (NULL);
349 }
350 #else
351 
352 /* Use the os supplied routines */
353 u_char *
354 pcap_ether_hostton(const char *name)
355 {
356 	register u_char *ap;
357 	u_char a[6];
358 
359 	ap = NULL;
360 	if (ether_hostton(name, (struct ether_addr *)a) == 0) {
361 		ap = (u_char *)malloc(6);
362 		if (ap != NULL)
363 			memcpy((char *)ap, (char *)a, 6);
364 	}
365 	return (ap);
366 }
367 #endif
368 
369 u_short
370 __pcap_nametodnaddr(const char *name)
371 {
372 #ifdef	DECNETLIB
373 	struct nodeent *getnodebyname();
374 	struct nodeent *nep;
375 	unsigned short res;
376 
377 	nep = getnodebyname(name);
378 	if (nep == ((struct nodeent *)0))
379 		bpf_error("unknown decnet host name '%s'\n", name);
380 
381 	memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
382 	return(res);
383 #else
384 	bpf_error("decnet name support not included, '%s' cannot be translated\n",
385 		name);
386 	/* NOTREACHED */
387 #ifdef lint
388 	/*
389 	 * Arguably, lint should assume that functions which don't return
390 	 * (i.e. that contain no return statements and whose ends are
391 	 * unreachable) actually return a value, so callers won't get
392 	 * warnings for using that value (since they won't actually
393 	 * be doing so).  However, most lints don't seem to do that...
394 	 */
395 	return (0);
396 #endif
397 #endif
398 }
399