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