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