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