GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libpcap/nametoaddr.c Lines: 0 83 0.0 %
Date: 2017-11-13 Branches: 0 48 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: nametoaddr.c,v 1.21 2016/12/02 02:37:30 dlg 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
static const 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
	{ "lldp", ETHERTYPE_LLDP },
206
	{ "sprite", ETHERTYPE_SPRITE },
207
	{ "mopdl", ETHERTYPE_MOPDL },
208
	{ "moprc", ETHERTYPE_MOPRC },
209
	{ "decnet", ETHERTYPE_DN },
210
	{ "lat", ETHERTYPE_LAT },
211
	{ "sca", ETHERTYPE_SCA },
212
	{ "lanbridge", ETHERTYPE_LANBRIDGE },
213
	{ "vexp", ETHERTYPE_VEXP },
214
	{ "vprod", ETHERTYPE_VPROD },
215
	{ "atalk", ETHERTYPE_ATALK },
216
	{ "atalkarp", ETHERTYPE_AARP },
217
	{ "loopback", ETHERTYPE_LOOPBACK },
218
	{ "decdts", ETHERTYPE_DECDTS },
219
	{ "decdns", ETHERTYPE_DECDNS },
220
	{ (char *)0, 0 }
221
};
222
/* Accessor for tcpdump */
223
const struct eproto * const eproto_db = _eproto_db;
224
225
int
226
pcap_nametoeproto(const char *s)
227
{
228
	const struct eproto *p = _eproto_db;
229
230
	while (p->s != 0) {
231
		if (strcmp(p->s, s) == 0)
232
			return p->p;
233
		p += 1;
234
	}
235
	return PROTO_UNDEF;
236
}
237
238
#include "llc.h"
239
240
/* Static data base of LLC values. */
241
static const struct eproto llc_db[] = {
242
	{ "stp", LLCSAP_8021D },
243
	{ (char *)0, 0 }
244
};
245
246
int
247
pcap_nametollc(const char *s)
248
{
249
	const struct eproto *p = llc_db;
250
251
	while (p->s != 0) {
252
		if (strcmp(p->s, s) == 0)
253
			return p->p;
254
		p += 1;
255
	}
256
	return PROTO_UNDEF;
257
}
258
259
/* Hex digit to integer. */
260
static __inline int
261
xdtoi(c)
262
	int c;
263
{
264
	if (isdigit(c))
265
		return c - '0';
266
	else if (islower(c))
267
		return c - 'a' + 10;
268
	else
269
		return c - 'A' + 10;
270
}
271
272
int
273
__pcap_atoin(const char *s, bpf_u_int32 *addr)
274
{
275
	u_int n;
276
	int len;
277
278
	*addr = 0;
279
	len = 0;
280
	while (1) {
281
		n = 0;
282
		while (*s && *s != '.')
283
			n = n * 10 + *s++ - '0';
284
		*addr <<= 8;
285
		*addr |= n & 0xff;
286
		len += 8;
287
		if (*s == '\0')
288
			return len;
289
		++s;
290
	}
291
	/* NOTREACHED */
292
}
293
294
int
295
__pcap_atodn(const char *s, bpf_u_int32 *addr)
296
{
297
#define AREASHIFT 10
298
#define AREAMASK 0176000
299
#define NODEMASK 01777
300
301
	u_int node, area;
302
303
	if (sscanf((char *)s, "%d.%d", &area, &node) != 2)
304
		bpf_error("malformed decnet address '%s'", s);
305
306
	*addr = (area << AREASHIFT) & AREAMASK;
307
	*addr |= (node & NODEMASK);
308
309
	return(32);
310
}
311
312
/*
313
 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
314
 * ethernet address.  Assumes 's' is well formed.
315
 */
316
u_char *
317
pcap_ether_aton(const char *s)
318
{
319
	u_char *ep, *e;
320
	u_int d;
321
322
	e = ep = malloc(6);
323
	if (e == NULL)
324
		bpf_error("malloc");
325
326
	while (*s) {
327
		if (*s == ':')
328
			s += 1;
329
		d = xdtoi(*s++);
330
		if (isxdigit((unsigned char)*s)) {
331
			d <<= 4;
332
			d |= xdtoi(*s++);
333
		}
334
		*ep++ = d;
335
	}
336
337
	return (e);
338
}
339
340
#ifndef HAVE_ETHER_HOSTTON
341
/* Roll our own */
342
u_char *
343
pcap_ether_hostton(const char *name)
344
{
345
	struct pcap_etherent *ep;
346
	u_char *ap;
347
	static FILE *fp = NULL;
348
	static init = 0;
349
350
	if (!init) {
351
		fp = fopen(PCAP_ETHERS_FILE, "r");
352
		++init;
353
		if (fp == NULL)
354
			return (NULL);
355
	} else if (fp == NULL)
356
		return (NULL);
357
	else
358
		rewind(fp);
359
360
	while ((ep = pcap_next_etherent(fp)) != NULL) {
361
		if (strcmp(ep->name, name) == 0) {
362
			ap = malloc(6);
363
			if (ap != NULL) {
364
				memcpy(ap, ep->addr, 6);
365
				return (ap);
366
			}
367
			break;
368
		}
369
	}
370
	return (NULL);
371
}
372
#else
373
374
/* Use the os supplied routines */
375
u_char *
376
pcap_ether_hostton(const char *name)
377
{
378
	u_char *ap;
379
	u_char a[6];
380
381
	ap = NULL;
382
	if (ether_hostton(name, (struct ether_addr *)a) == 0) {
383
		ap = malloc(6);
384
		if (ap != NULL)
385
			memcpy((char *)ap, (char *)a, 6);
386
	}
387
	return (ap);
388
}
389
#endif
390
391
u_short
392
__pcap_nametodnaddr(const char *name)
393
{
394
#ifdef	DECNETLIB
395
	struct nodeent *getnodebyname();
396
	struct nodeent *nep;
397
	unsigned short res;
398
399
	nep = getnodebyname(name);
400
	if (nep == ((struct nodeent *)0))
401
		bpf_error("unknown decnet host name '%s'\n", name);
402
403
	memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
404
	return(res);
405
#else
406
	bpf_error("decnet name support not included, '%s' cannot be translated\n",
407
		name);
408
	/* NOTREACHED */
409
#endif
410
}