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