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