]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/libpcap/nametoaddr.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / 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  * $FreeBSD$
25  */
26
27 #ifndef lint
28 static const char rcsid[] _U_ =
29     "@(#) $Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.82.2.1 2008/02/06 10:21:47 guy Exp $ (LBL)";
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #ifdef WIN32
37 #include <pcap-stdinc.h>
38
39 #else /* WIN32 */
40
41 #include <sys/param.h>
42 #include <sys/types.h>                          /* concession to AIX */
43 #include <sys/socket.h>
44 #include <sys/time.h>
45
46 #include <netinet/in.h>
47 #endif /* WIN32 */
48
49 /*
50  * XXX - why was this included even on UNIX?
51  */
52 #ifdef __MINGW32__
53 #include "IP6_misc.h"
54 #endif
55
56 #ifndef WIN32
57 #ifdef HAVE_ETHER_HOSTTON
58 /*
59  * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
60  * ether_hostton()?
61  */
62 #ifdef HAVE_NETINET_IF_ETHER_H
63 struct mbuf;            /* Squelch compiler warnings on some platforms for */
64 struct rtentry;         /* declarations in <net/if.h> */
65 #include <net/if.h>     /* for "struct ifnet" in "struct arpcom" on Solaris */
66 #include <netinet/if_ether.h>
67 #endif /* HAVE_NETINET_IF_ETHER_H */
68 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
69 #include <netinet/ether.h>
70 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
71 #endif /* HAVE_ETHER_HOSTTON */
72 #include <arpa/inet.h>
73 #include <netdb.h>
74 #endif /* WIN32 */
75
76 #include <ctype.h>
77 #include <errno.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <stdio.h>
81
82 #include "pcap-int.h"
83
84 #include "gencode.h"
85 #include <pcap/namedb.h>
86
87 #ifdef HAVE_OS_PROTO_H
88 #include "os-proto.h"
89 #endif
90
91 #ifndef NTOHL
92 #define NTOHL(x) (x) = ntohl(x)
93 #define NTOHS(x) (x) = ntohs(x)
94 #endif
95
96 static inline int xdtoi(int);
97
98 /*
99  *  Convert host name to internet address.
100  *  Return 0 upon failure.
101  */
102 bpf_u_int32 **
103 pcap_nametoaddr(const char *name)
104 {
105 #ifndef h_addr
106         static bpf_u_int32 *hlist[2];
107 #endif
108         bpf_u_int32 **p;
109         struct hostent *hp;
110
111         if ((hp = gethostbyname(name)) != NULL) {
112 #ifndef h_addr
113                 hlist[0] = (bpf_u_int32 *)hp->h_addr;
114                 NTOHL(hp->h_addr);
115                 return hlist;
116 #else
117                 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
118                         NTOHL(**p);
119                 return (bpf_u_int32 **)hp->h_addr_list;
120 #endif
121         }
122         else
123                 return 0;
124 }
125
126 #ifdef INET6
127 struct addrinfo *
128 pcap_nametoaddrinfo(const char *name)
129 {
130         struct addrinfo hints, *res;
131         int error;
132
133         memset(&hints, 0, sizeof(hints));
134         hints.ai_family = PF_UNSPEC;
135         hints.ai_socktype = SOCK_STREAM;        /*not really*/
136         hints.ai_protocol = IPPROTO_TCP;        /*not really*/
137         error = getaddrinfo(name, NULL, &hints, &res);
138         if (error)
139                 return NULL;
140         else
141                 return res;
142 }
143 #endif /*INET6*/
144
145 /*
146  *  Convert net name to internet address.
147  *  Return 0 upon failure.
148  */
149 bpf_u_int32
150 pcap_nametonetaddr(const char *name)
151 {
152 #ifndef WIN32
153         struct netent *np;
154
155         if ((np = getnetbyname(name)) != NULL)
156                 return np->n_net;
157         else
158                 return 0;
159 #else
160         /*
161          * There's no "getnetbyname()" on Windows.
162          */
163         return 0;
164 #endif
165 }
166
167 /*
168  * Convert a port name to its port and protocol numbers.
169  * We assume only TCP or UDP.
170  * Return 0 upon failure.
171  */
172 int
173 pcap_nametoport(const char *name, int *port, int *proto)
174 {
175         struct servent *sp;
176         int tcp_port = -1;
177         int udp_port = -1;
178
179         /*
180          * We need to check /etc/services for ambiguous entries.
181          * If we find the ambiguous entry, and it has the
182          * same port number, change the proto to PROTO_UNDEF
183          * so both TCP and UDP will be checked.
184          */
185         sp = getservbyname(name, "tcp");
186         if (sp != NULL) tcp_port = ntohs(sp->s_port);
187         sp = getservbyname(name, "udp");
188         if (sp != NULL) udp_port = ntohs(sp->s_port);
189         if (tcp_port >= 0) {
190                 *port = tcp_port;
191                 *proto = IPPROTO_TCP;
192                 if (udp_port >= 0) {
193                         if (udp_port == tcp_port)
194                                 *proto = PROTO_UNDEF;
195 #ifdef notdef
196                         else
197                                 /* Can't handle ambiguous names that refer
198                                    to different port numbers. */
199                                 warning("ambiguous port %s in /etc/services",
200                                         name);
201 #endif
202                 }
203                 return 1;
204         }
205         if (udp_port >= 0) {
206                 *port = udp_port;
207                 *proto = IPPROTO_UDP;
208                 return 1;
209         }
210 #if defined(ultrix) || defined(__osf__)
211         /* Special hack in case NFS isn't in /etc/services */
212         if (strcmp(name, "nfs") == 0) {
213                 *port = 2049;
214                 *proto = PROTO_UNDEF;
215                 return 1;
216         }
217 #endif
218         return 0;
219 }
220
221 /*
222  * Convert a string in the form PPP-PPP, where correspond to ports, to
223  * a starting and ending port in a port range.
224  * Return 0 on failure.
225  */
226 int
227 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
228 {
229         u_int p1, p2;
230         char *off, *cpy;
231         int save_proto;
232
233         if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
234                 if ((cpy = strdup(name)) == NULL)
235                         return 0;
236
237                 if ((off = strchr(cpy, '-')) == NULL) {
238                         free(cpy);
239                         return 0;
240                 }
241
242                 *off = '\0';
243
244                 if (pcap_nametoport(cpy, port1, proto) == 0) {
245                         free(cpy);
246                         return 0;
247                 }
248                 save_proto = *proto;
249
250                 if (pcap_nametoport(off + 1, port2, proto) == 0) {
251                         free(cpy);
252                         return 0;
253                 }
254
255                 if (*proto != save_proto)
256                         *proto = PROTO_UNDEF;
257         } else {
258                 *port1 = p1;
259                 *port2 = p2;
260                 *proto = PROTO_UNDEF;
261         }
262
263         return 1;
264 }
265
266 int
267 pcap_nametoproto(const char *str)
268 {
269         struct protoent *p;
270
271         p = getprotobyname(str);
272         if (p != 0)
273                 return p->p_proto;
274         else
275                 return PROTO_UNDEF;
276 }
277
278 #include "ethertype.h"
279
280 struct eproto {
281         const char *s;
282         u_short p;
283 };
284
285 /* Static data base of ether protocol types. */
286 struct eproto eproto_db[] = {
287 #if 0
288         /* The FreeBSD elf linker generates a request to copy this array
289          * (including its size) when you link with -lpcap.  In order to
290          * not bump the major version number of this libpcap.so, we need
291          * to ensure that the array stays the same size.  Since PUP is
292          * likely never seen in real life any more, it's the first to
293          * be sacrificed (in favor of ip6).
294          */
295         { "pup", ETHERTYPE_PUP },
296 #endif
297         { "xns", ETHERTYPE_NS },
298         { "ip", ETHERTYPE_IP },
299 #ifdef INET6
300         { "ip6", ETHERTYPE_IPV6 },
301 #endif
302         { "arp", ETHERTYPE_ARP },
303         { "rarp", ETHERTYPE_REVARP },
304         { "sprite", ETHERTYPE_SPRITE },
305         { "mopdl", ETHERTYPE_MOPDL },
306         { "moprc", ETHERTYPE_MOPRC },
307         { "decnet", ETHERTYPE_DN },
308         { "lat", ETHERTYPE_LAT },
309         { "sca", ETHERTYPE_SCA },
310         { "lanbridge", ETHERTYPE_LANBRIDGE },
311         { "vexp", ETHERTYPE_VEXP },
312         { "vprod", ETHERTYPE_VPROD },
313         { "atalk", ETHERTYPE_ATALK },
314         { "atalkarp", ETHERTYPE_AARP },
315         { "loopback", ETHERTYPE_LOOPBACK },
316         { "decdts", ETHERTYPE_DECDTS },
317         { "decdns", ETHERTYPE_DECDNS },
318         { (char *)0, 0 }
319 };
320
321 int
322 pcap_nametoeproto(const char *s)
323 {
324         struct eproto *p = eproto_db;
325
326         while (p->s != 0) {
327                 if (strcmp(p->s, s) == 0)
328                         return p->p;
329                 p += 1;
330         }
331         return PROTO_UNDEF;
332 }
333
334 #include "llc.h"
335
336 /* Static data base of LLC values. */
337 static struct eproto llc_db[] = {
338         { "iso", LLCSAP_ISONS },
339         { "stp", LLCSAP_8021D },
340         { "ipx", LLCSAP_IPX },
341         { "netbeui", LLCSAP_NETBEUI },
342         { (char *)0, 0 }
343 };
344
345 int
346 pcap_nametollc(const char *s)
347 {
348         struct eproto *p = llc_db;
349
350         while (p->s != 0) {
351                 if (strcmp(p->s, s) == 0)
352                         return p->p;
353                 p += 1;
354         }
355         return PROTO_UNDEF;
356 }
357
358 /* Hex digit to integer. */
359 static inline int
360 xdtoi(c)
361         register int c;
362 {
363         if (isdigit(c))
364                 return c - '0';
365         else if (islower(c))
366                 return c - 'a' + 10;
367         else
368                 return c - 'A' + 10;
369 }
370
371 int
372 __pcap_atoin(const char *s, bpf_u_int32 *addr)
373 {
374         u_int n;
375         int len;
376
377         *addr = 0;
378         len = 0;
379         while (1) {
380                 n = 0;
381                 while (*s && *s != '.')
382                         n = n * 10 + *s++ - '0';
383                 *addr <<= 8;
384                 *addr |= n & 0xff;
385                 len += 8;
386                 if (*s == '\0')
387                         return len;
388                 ++s;
389         }
390         /* NOTREACHED */
391 }
392
393 int
394 __pcap_atodn(const char *s, bpf_u_int32 *addr)
395 {
396 #define AREASHIFT 10
397 #define AREAMASK 0176000
398 #define NODEMASK 01777
399
400         u_int node, area;
401
402         if (sscanf(s, "%d.%d", &area, &node) != 2)
403                 bpf_error("malformed decnet address '%s'", s);
404
405         *addr = (area << AREASHIFT) & AREAMASK;
406         *addr |= (node & NODEMASK);
407
408         return(32);
409 }
410
411 /*
412  * Convert 's', which can have the one of the forms:
413  *
414  *      "xx:xx:xx:xx:xx:xx"
415  *      "xx.xx.xx.xx.xx.xx"
416  *      "xx-xx-xx-xx-xx-xx"
417  *      "xxxx.xxxx.xxxx"
418  *      "xxxxxxxxxxxx"
419  *
420  * (or various mixes of ':', '.', and '-') into a new
421  * ethernet address.  Assumes 's' is well formed.
422  */
423 u_char *
424 pcap_ether_aton(const char *s)
425 {
426         register u_char *ep, *e;
427         register u_int d;
428
429         e = ep = (u_char *)malloc(6);
430
431         while (*s) {
432                 if (*s == ':' || *s == '.' || *s == '-')
433                         s += 1;
434                 d = xdtoi(*s++);
435                 if (isxdigit((unsigned char)*s)) {
436                         d <<= 4;
437                         d |= xdtoi(*s++);
438                 }
439                 *ep++ = d;
440         }
441
442         return (e);
443 }
444
445 #ifndef HAVE_ETHER_HOSTTON
446 /* Roll our own */
447 u_char *
448 pcap_ether_hostton(const char *name)
449 {
450         register struct pcap_etherent *ep;
451         register u_char *ap;
452         static FILE *fp = NULL;
453         static int init = 0;
454
455         if (!init) {
456                 fp = fopen(PCAP_ETHERS_FILE, "r");
457                 ++init;
458                 if (fp == NULL)
459                         return (NULL);
460         } else if (fp == NULL)
461                 return (NULL);
462         else
463                 rewind(fp);
464
465         while ((ep = pcap_next_etherent(fp)) != NULL) {
466                 if (strcmp(ep->name, name) == 0) {
467                         ap = (u_char *)malloc(6);
468                         if (ap != NULL) {
469                                 memcpy(ap, ep->addr, 6);
470                                 return (ap);
471                         }
472                         break;
473                 }
474         }
475         return (NULL);
476 }
477 #else
478
479 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
480 #ifndef HAVE_STRUCT_ETHER_ADDR
481 struct ether_addr {
482         unsigned char ether_addr_octet[6];
483 };
484 #endif
485 extern int ether_hostton(const char *, struct ether_addr *);
486 #endif
487
488 /* Use the os supplied routines */
489 u_char *
490 pcap_ether_hostton(const char *name)
491 {
492         register u_char *ap;
493         u_char a[6];
494
495         ap = NULL;
496         if (ether_hostton(name, (struct ether_addr *)a) == 0) {
497                 ap = (u_char *)malloc(6);
498                 if (ap != NULL)
499                         memcpy((char *)ap, (char *)a, 6);
500         }
501         return (ap);
502 }
503 #endif
504
505 u_short
506 __pcap_nametodnaddr(const char *name)
507 {
508 #ifdef  DECNETLIB
509         struct nodeent *getnodebyname();
510         struct nodeent *nep;
511         unsigned short res;
512
513         nep = getnodebyname(name);
514         if (nep == ((struct nodeent *)0))
515                 bpf_error("unknown decnet host name '%s'\n", name);
516
517         memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
518         return(res);
519 #else
520         bpf_error("decnet name support not included, '%s' cannot be translated\n",
521                 name);
522         return(0);
523 #endif
524 }