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