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