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