]> The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
Fix typpo.
[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 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #ifdef DECNETLIB
30 #include <sys/types.h>
31 #include <netdnet/dnetdb.h>
32 #endif
33
34 #ifdef _WIN32
35 #include <winsock2.h>
36 #include <ws2tcpip.h>
37
38 #ifdef INET6
39 /*
40 * To quote the MSDN page for getaddrinfo() at
41 *
42 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
43 *
44 * "Support for getaddrinfo on Windows 2000 and older versions
45 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
46 * later. To execute an application that uses this function on earlier
47 * versions of Windows, then you need to include the Ws2tcpip.h and
48 * Wspiapi.h files. When the Wspiapi.h include file is added, the
49 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
50 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
51 * function is implemented in such a way that if the Ws2_32.dll or the
52 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
53 * Preview for Windows 2000) does not include getaddrinfo, then a
54 * version of getaddrinfo is implemented inline based on code in the
55 * Wspiapi.h header file. This inline code will be used on older Windows
56 * platforms that do not natively support the getaddrinfo function."
57 *
58 * We use getaddrinfo(), so we include Wspiapi.h here.
59 */
60 #include <wspiapi.h>
61 #endif /* INET6 */
62 #else /* _WIN32 */
63 #include <sys/param.h>
64 #include <sys/types.h> /* concession to AIX */
65 #include <sys/socket.h>
66 #include <sys/time.h>
67
68 #include <netinet/in.h>
69
70 #ifdef HAVE_ETHER_HOSTTON
71 #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
72 /*
73 * OK, just include <net/ethernet.h>.
74 */
75 #include <net/ethernet.h>
76 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
77 /*
78 * OK, just include <netinet/ether.h>
79 */
80 #include <netinet/ether.h>
81 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
82 /*
83 * OK, just include <sys/ethernet.h>
84 */
85 #include <sys/ethernet.h>
86 #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
87 /*
88 * OK, just include <arpa/inet.h>
89 */
90 #include <arpa/inet.h>
91 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
92 /*
93 * OK, include <netinet/if_ether.h>, after all the other stuff we
94 * need to include or define for its benefit.
95 */
96 #define NEED_NETINET_IF_ETHER_H
97 #else
98 /*
99 * We'll have to declare it ourselves.
100 * If <netinet/if_ether.h> defines struct ether_addr, include
101 * it. Otherwise, define it ourselves.
102 */
103 #ifdef HAVE_STRUCT_ETHER_ADDR
104 #define NEED_NETINET_IF_ETHER_H
105 #else /* HAVE_STRUCT_ETHER_ADDR */
106 struct ether_addr {
107 unsigned char ether_addr_octet[6];
108 };
109 #endif /* HAVE_STRUCT_ETHER_ADDR */
110 #endif
111
112 #ifdef NEED_NETINET_IF_ETHER_H
113 #include <net/if.h> /* Needed on some platforms */
114 #include <netinet/in.h> /* Needed on some platforms */
115 #include <netinet/if_ether.h>
116 #endif /* NEED_NETINET_IF_ETHER_H */
117
118 #ifndef HAVE_DECL_ETHER_HOSTTON
119 /*
120 * No header declares it, so declare it ourselves.
121 */
122 extern int ether_hostton(const char *, struct ether_addr *);
123 #endif /* defined(HAVE_DECL_ETHER_HOSTTON) */
124 #endif /* HAVE_ETHER_HOSTTON */
125
126 #include <arpa/inet.h>
127 #include <netdb.h>
128 #endif /* _WIN32 */
129
130 #include <ctype.h>
131 #include <errno.h>
132 #include <stdlib.h>
133 #include <string.h>
134 #include <stdio.h>
135
136 #include "fopen.h"
137
138 #include "pcap-int.h"
139
140 #include "gencode.h"
141 #include <pcap/namedb.h>
142 #include "nametoaddr.h"
143
144 #ifdef HAVE_OS_PROTO_H
145 #include "os-proto.h"
146 #endif
147
148 #ifndef NTOHL
149 #define NTOHL(x) (x) = ntohl(x)
150 #define NTOHS(x) (x) = ntohs(x)
151 #endif
152
153 static inline int xdtoi(int);
154
155 /*
156 * Convert host name to internet address.
157 * Return 0 upon failure.
158 */
159 bpf_u_int32 **
160 pcap_nametoaddr(const char *name)
161 {
162 #ifndef h_addr
163 static bpf_u_int32 *hlist[2];
164 #endif
165 bpf_u_int32 **p;
166 struct hostent *hp;
167
168 if ((hp = gethostbyname(name)) != NULL) {
169 #ifndef h_addr
170 hlist[0] = (bpf_u_int32 *)hp->h_addr;
171 NTOHL(hp->h_addr);
172 return hlist;
173 #else
174 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
175 NTOHL(**p);
176 return (bpf_u_int32 **)hp->h_addr_list;
177 #endif
178 }
179 else
180 return 0;
181 }
182
183 #ifdef INET6
184 struct addrinfo *
185 pcap_nametoaddrinfo(const char *name)
186 {
187 struct addrinfo hints, *res;
188 int error;
189
190 memset(&hints, 0, sizeof(hints));
191 hints.ai_family = PF_UNSPEC;
192 hints.ai_socktype = SOCK_STREAM; /*not really*/
193 hints.ai_protocol = IPPROTO_TCP; /*not really*/
194 error = getaddrinfo(name, NULL, &hints, &res);
195 if (error)
196 return NULL;
197 else
198 return res;
199 }
200 #endif /*INET6*/
201
202 /*
203 * Convert net name to internet address.
204 * Return 0 upon failure.
205 */
206 bpf_u_int32
207 pcap_nametonetaddr(const char *name)
208 {
209 #ifndef _WIN32
210 struct netent *np;
211
212 if ((np = getnetbyname(name)) != NULL)
213 return np->n_net;
214 else
215 return 0;
216 #else
217 /*
218 * There's no "getnetbyname()" on Windows.
219 *
220 * XXX - I guess we could use the BSD code to read
221 * C:\Windows\System32\drivers\etc/networks, assuming
222 * that's its home on all the versions of Windows
223 * we use, but that file probably just has the loopback
224 * network on 127/24 on 99 44/100% of Windows machines.
225 *
226 * (Heck, these days it probably just has that on 99 44/100%
227 * of *UN*X* machines.)
228 */
229 return 0;
230 #endif
231 }
232
233 /*
234 * Convert a port name to its port and protocol numbers.
235 * We assume only TCP or UDP.
236 * Return 0 upon failure.
237 */
238 int
239 pcap_nametoport(const char *name, int *port, int *proto)
240 {
241 struct servent *sp;
242 int tcp_port = -1;
243 int udp_port = -1;
244
245 /*
246 * We need to check /etc/services for ambiguous entries.
247 * If we find the ambiguous entry, and it has the
248 * same port number, change the proto to PROTO_UNDEF
249 * so both TCP and UDP will be checked.
250 */
251 sp = getservbyname(name, "tcp");
252 if (sp != NULL) tcp_port = ntohs(sp->s_port);
253 sp = getservbyname(name, "udp");
254 if (sp != NULL) udp_port = ntohs(sp->s_port);
255 if (tcp_port >= 0) {
256 *port = tcp_port;
257 *proto = IPPROTO_TCP;
258 if (udp_port >= 0) {
259 if (udp_port == tcp_port)
260 *proto = PROTO_UNDEF;
261 #ifdef notdef
262 else
263 /* Can't handle ambiguous names that refer
264 to different port numbers. */
265 warning("ambiguous port %s in /etc/services",
266 name);
267 #endif
268 }
269 return 1;
270 }
271 if (udp_port >= 0) {
272 *port = udp_port;
273 *proto = IPPROTO_UDP;
274 return 1;
275 }
276 #if defined(ultrix) || defined(__osf__)
277 /* Special hack in case NFS isn't in /etc/services */
278 if (strcmp(name, "nfs") == 0) {
279 *port = 2049;
280 *proto = PROTO_UNDEF;
281 return 1;
282 }
283 #endif
284 return 0;
285 }
286
287 /*
288 * Convert a string in the form PPP-PPP, where correspond to ports, to
289 * a starting and ending port in a port range.
290 * Return 0 on failure.
291 */
292 int
293 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
294 {
295 u_int p1, p2;
296 char *off, *cpy;
297 int save_proto;
298
299 if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
300 if ((cpy = strdup(name)) == NULL)
301 return 0;
302
303 if ((off = strchr(cpy, '-')) == NULL) {
304 free(cpy);
305 return 0;
306 }
307
308 *off = '\0';
309
310 if (pcap_nametoport(cpy, port1, proto) == 0) {
311 free(cpy);
312 return 0;
313 }
314 save_proto = *proto;
315
316 if (pcap_nametoport(off + 1, port2, proto) == 0) {
317 free(cpy);
318 return 0;
319 }
320 free(cpy);
321
322 if (*proto != save_proto)
323 *proto = PROTO_UNDEF;
324 } else {
325 *port1 = p1;
326 *port2 = p2;
327 *proto = PROTO_UNDEF;
328 }
329
330 return 1;
331 }
332
333 int
334 pcap_nametoproto(const char *str)
335 {
336 struct protoent *p;
337
338 p = getprotobyname(str);
339 if (p != 0)
340 return p->p_proto;
341 else
342 return PROTO_UNDEF;
343 }
344
345 #include "ethertype.h"
346
347 struct eproto {
348 const char *s;
349 u_short p;
350 };
351
352 /*
353 * Static data base of ether protocol types.
354 * tcpdump used to import this, and it's declared as an export on
355 * Debian, at least, so make it a public symbol, even though we
356 * don't officially export it by declaring it in a header file.
357 * (Programs *should* do this themselves, as tcpdump now does.)
358 */
359 PCAP_API_DEF struct eproto eproto_db[] = {
360 { "pup", ETHERTYPE_PUP },
361 { "xns", ETHERTYPE_NS },
362 { "ip", ETHERTYPE_IP },
363 #ifdef INET6
364 { "ip6", ETHERTYPE_IPV6 },
365 #endif
366 { "arp", ETHERTYPE_ARP },
367 { "rarp", ETHERTYPE_REVARP },
368 { "sprite", ETHERTYPE_SPRITE },
369 { "mopdl", ETHERTYPE_MOPDL },
370 { "moprc", ETHERTYPE_MOPRC },
371 { "decnet", ETHERTYPE_DN },
372 { "lat", ETHERTYPE_LAT },
373 { "sca", ETHERTYPE_SCA },
374 { "lanbridge", ETHERTYPE_LANBRIDGE },
375 { "vexp", ETHERTYPE_VEXP },
376 { "vprod", ETHERTYPE_VPROD },
377 { "atalk", ETHERTYPE_ATALK },
378 { "atalkarp", ETHERTYPE_AARP },
379 { "loopback", ETHERTYPE_LOOPBACK },
380 { "decdts", ETHERTYPE_DECDTS },
381 { "decdns", ETHERTYPE_DECDNS },
382 { (char *)0, 0 }
383 };
384
385 int
386 pcap_nametoeproto(const char *s)
387 {
388 struct eproto *p = eproto_db;
389
390 while (p->s != 0) {
391 if (strcmp(p->s, s) == 0)
392 return p->p;
393 p += 1;
394 }
395 return PROTO_UNDEF;
396 }
397
398 #include "llc.h"
399
400 /* Static data base of LLC values. */
401 static struct eproto llc_db[] = {
402 { "iso", LLCSAP_ISONS },
403 { "stp", LLCSAP_8021D },
404 { "ipx", LLCSAP_IPX },
405 { "netbeui", LLCSAP_NETBEUI },
406 { (char *)0, 0 }
407 };
408
409 int
410 pcap_nametollc(const char *s)
411 {
412 struct eproto *p = llc_db;
413
414 while (p->s != 0) {
415 if (strcmp(p->s, s) == 0)
416 return p->p;
417 p += 1;
418 }
419 return PROTO_UNDEF;
420 }
421
422 /* Hex digit to integer. */
423 static inline int
424 xdtoi(c)
425 register int c;
426 {
427 if (isdigit(c))
428 return c - '0';
429 else if (islower(c))
430 return c - 'a' + 10;
431 else
432 return c - 'A' + 10;
433 }
434
435 int
436 __pcap_atoin(const char *s, bpf_u_int32 *addr)
437 {
438 u_int n;
439 int len;
440
441 *addr = 0;
442 len = 0;
443 while (1) {
444 n = 0;
445 while (*s && *s != '.')
446 n = n * 10 + *s++ - '0';
447 *addr <<= 8;
448 *addr |= n & 0xff;
449 len += 8;
450 if (*s == '\0')
451 return len;
452 ++s;
453 }
454 /* NOTREACHED */
455 }
456
457 int
458 __pcap_atodn(const char *s, bpf_u_int32 *addr)
459 {
460 #define AREASHIFT 10
461 #define AREAMASK 0176000
462 #define NODEMASK 01777
463
464 u_int node, area;
465
466 if (sscanf(s, "%d.%d", &area, &node) != 2)
467 return(0);
468
469 *addr = (area << AREASHIFT) & AREAMASK;
470 *addr |= (node & NODEMASK);
471
472 return(32);
473 }
474
475 /*
476 * Convert 's', which can have the one of the forms:
477 *
478 * "xx:xx:xx:xx:xx:xx"
479 * "xx.xx.xx.xx.xx.xx"
480 * "xx-xx-xx-xx-xx-xx"
481 * "xxxx.xxxx.xxxx"
482 * "xxxxxxxxxxxx"
483 *
484 * (or various mixes of ':', '.', and '-') into a new
485 * ethernet address. Assumes 's' is well formed.
486 */
487 u_char *
488 pcap_ether_aton(const char *s)
489 {
490 register u_char *ep, *e;
491 register u_int d;
492
493 e = ep = (u_char *)malloc(6);
494 if (e == NULL)
495 return (NULL);
496
497 while (*s) {
498 if (*s == ':' || *s == '.' || *s == '-')
499 s += 1;
500 d = xdtoi(*s++);
501 if (isxdigit((unsigned char)*s)) {
502 d <<= 4;
503 d |= xdtoi(*s++);
504 }
505 *ep++ = d;
506 }
507
508 return (e);
509 }
510
511 #ifndef HAVE_ETHER_HOSTTON
512 /* Roll our own */
513 u_char *
514 pcap_ether_hostton(const char *name)
515 {
516 register struct pcap_etherent *ep;
517 register u_char *ap;
518 static FILE *fp = NULL;
519 static int init = 0;
520
521 if (!init) {
522 ++init;
523 if (pcap_fopen(&fp, PCAP_ETHERS_FILE, "r") != 0)
524 return (NULL);
525 } else if (fp == NULL)
526 return (NULL);
527 else
528 rewind(fp);
529
530 while ((ep = pcap_next_etherent(fp)) != NULL) {
531 if (strcmp(ep->name, name) == 0) {
532 ap = (u_char *)malloc(6);
533 if (ap != NULL) {
534 memcpy(ap, ep->addr, 6);
535 return (ap);
536 }
537 break;
538 }
539 }
540 return (NULL);
541 }
542 #else
543 /* Use the os supplied routines */
544 u_char *
545 pcap_ether_hostton(const char *name)
546 {
547 register u_char *ap;
548 u_char a[6];
549
550 ap = NULL;
551 if (ether_hostton(name, (struct ether_addr *)a) == 0) {
552 ap = (u_char *)malloc(6);
553 if (ap != NULL)
554 memcpy((char *)ap, (char *)a, 6);
555 }
556 return (ap);
557 }
558 #endif
559
560 int
561 __pcap_nametodnaddr(const char *name, u_short *res)
562 {
563 #ifdef DECNETLIB
564 struct nodeent *getnodebyname();
565 struct nodeent *nep;
566
567 nep = getnodebyname(name);
568 if (nep == ((struct nodeent *)0))
569 return(0);
570
571 memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
572 return(1);
573 #else
574 return(0);
575 #endif
576 }