]> The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
More pcap_ to pcapint_ for non-API routines.
[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 _WIN32
30 #include <winsock2.h>
31 #include <ws2tcpip.h>
32
33 #ifdef INET6
34 /*
35 * To quote the MSDN page for getaddrinfo() at
36 *
37 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
38 *
39 * "Support for getaddrinfo on Windows 2000 and older versions
40 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
41 * later. To execute an application that uses this function on earlier
42 * versions of Windows, then you need to include the Ws2tcpip.h and
43 * Wspiapi.h files. When the Wspiapi.h include file is added, the
44 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
45 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
46 * function is implemented in such a way that if the Ws2_32.dll or the
47 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
48 * Preview for Windows 2000) does not include getaddrinfo, then a
49 * version of getaddrinfo is implemented inline based on code in the
50 * Wspiapi.h header file. This inline code will be used on older Windows
51 * platforms that do not natively support the getaddrinfo function."
52 *
53 * We use getaddrinfo(), so we include Wspiapi.h here.
54 */
55 #include <wspiapi.h>
56 #endif /* INET6 */
57 #else /* _WIN32 */
58 #include <sys/param.h>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/time.h>
62
63 #include <netinet/in.h>
64
65 #ifdef HAVE_ETHER_HOSTTON
66 #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
67 /*
68 * OK, just include <net/ethernet.h>.
69 */
70 #include <net/ethernet.h>
71 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
72 /*
73 * OK, just include <netinet/ether.h>
74 */
75 #include <netinet/ether.h>
76 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
77 /*
78 * OK, just include <sys/ethernet.h>
79 */
80 #include <sys/ethernet.h>
81 #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
82 /*
83 * OK, just include <arpa/inet.h>
84 */
85 #include <arpa/inet.h>
86 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
87 /*
88 * OK, include <netinet/if_ether.h>, after all the other stuff we
89 * need to include or define for its benefit.
90 */
91 #define NEED_NETINET_IF_ETHER_H
92 #else
93 /*
94 * We'll have to declare it ourselves.
95 * If <netinet/if_ether.h> defines struct ether_addr, include
96 * it. Otherwise, define it ourselves.
97 */
98 #ifdef HAVE_STRUCT_ETHER_ADDR
99 #define NEED_NETINET_IF_ETHER_H
100 #else /* HAVE_STRUCT_ETHER_ADDR */
101 struct ether_addr {
102 unsigned char ether_addr_octet[6];
103 };
104 #endif /* HAVE_STRUCT_ETHER_ADDR */
105 #endif /* what declares ether_hostton() */
106
107 #ifdef NEED_NETINET_IF_ETHER_H
108 #include <net/if.h> /* Needed on some platforms */
109 #include <netinet/in.h> /* Needed on some platforms */
110 #include <netinet/if_ether.h>
111 #endif /* NEED_NETINET_IF_ETHER_H */
112
113 #ifndef HAVE_DECL_ETHER_HOSTTON
114 /*
115 * No header declares it, so declare it ourselves.
116 */
117 extern int ether_hostton(const char *, struct ether_addr *);
118 #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
119 #endif /* HAVE_ETHER_HOSTTON */
120
121 #include <arpa/inet.h>
122 #include <netdb.h>
123 #endif /* _WIN32 */
124
125 #include <errno.h>
126 #include <stdlib.h>
127 #include <string.h>
128 #include <stdio.h>
129
130 #include "pcap-int.h"
131
132 #include "diag-control.h"
133
134 #include "gencode.h"
135 #include <pcap/namedb.h>
136 #include "nametoaddr.h"
137
138 #include "thread-local.h"
139
140 #ifdef HAVE_OS_PROTO_H
141 #include "os-proto.h"
142 #endif
143
144 #ifndef NTOHL
145 #define NTOHL(x) (x) = ntohl(x)
146 #define NTOHS(x) (x) = ntohs(x)
147 #endif
148
149 /*
150 * Convert host name to internet address.
151 * Return 0 upon failure.
152 * XXX - not thread-safe; don't use it inside libpcap.
153 */
154 bpf_u_int32 **
155 pcap_nametoaddr(const char *name)
156 {
157 #ifndef h_addr
158 static bpf_u_int32 *hlist[2];
159 #endif
160 bpf_u_int32 **p;
161 struct hostent *hp;
162
163 /*
164 * gethostbyname() is deprecated on Windows, perhaps because
165 * it's not thread-safe, or because it doesn't support IPv6,
166 * or both.
167 *
168 * We deprecate pcap_nametoaddr() on all platforms because
169 * it's not thread-safe; we supply it for backwards compatibility,
170 * so suppress the deprecation warning. We could, I guess,
171 * use getaddrinfo() and construct the array ourselves, but
172 * that's probably not worth the effort, as that wouldn't make
173 * this thread-safe - we can't change the API to require that
174 * our caller free the address array, so we still have to reuse
175 * a local array.
176 */
177 DIAG_OFF_DEPRECATION
178 if ((hp = gethostbyname(name)) != NULL) {
179 DIAG_ON_DEPRECATION
180 #ifndef h_addr
181 hlist[0] = (bpf_u_int32 *)hp->h_addr;
182 NTOHL(hp->h_addr);
183 return hlist;
184 #else
185 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
186 NTOHL(**p);
187 return (bpf_u_int32 **)hp->h_addr_list;
188 #endif
189 }
190 else
191 return 0;
192 }
193
194 struct addrinfo *
195 pcap_nametoaddrinfo(const char *name)
196 {
197 struct addrinfo hints, *res;
198 int error;
199
200 memset(&hints, 0, sizeof(hints));
201 hints.ai_family = PF_UNSPEC;
202 hints.ai_socktype = SOCK_STREAM; /*not really*/
203 hints.ai_protocol = IPPROTO_TCP; /*not really*/
204 error = getaddrinfo(name, NULL, &hints, &res);
205 if (error)
206 return NULL;
207 else
208 return res;
209 }
210
211 /*
212 * Convert net name to internet address.
213 * Return 0 upon failure.
214 * XXX - not guaranteed to be thread-safe! See below for platforms
215 * on which it is thread-safe and on which it isn't.
216 */
217 #if defined(_WIN32) || defined(__CYGWIN__)
218 bpf_u_int32
219 pcap_nametonetaddr(const char *name _U_)
220 {
221 /*
222 * There's no "getnetbyname()" on Windows.
223 *
224 * XXX - I guess we could use the BSD code to read
225 * C:\Windows\System32\drivers\etc/networks, assuming
226 * that's its home on all the versions of Windows
227 * we use, but that file probably just has the loopback
228 * network on 127/24 on 99 44/100% of Windows machines.
229 *
230 * (Heck, these days it probably just has that on 99 44/100%
231 * of *UN*X* machines.)
232 */
233 return 0;
234 }
235 #else /* _WIN32 */
236 bpf_u_int32
237 pcap_nametonetaddr(const char *name)
238 {
239 /*
240 * UN*X.
241 */
242 struct netent *np;
243 #if defined(HAVE_LINUX_GETNETBYNAME_R)
244 /*
245 * We have Linux's reentrant getnetbyname_r().
246 */
247 struct netent result_buf;
248 char buf[1024]; /* arbitrary size */
249 int h_errnoval;
250 int err;
251
252 /*
253 * Apparently, the man page at
254 *
255 * http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
256 *
257 * lies when it says
258 *
259 * If the function call successfully obtains a network record,
260 * then *result is set pointing to result_buf; otherwise, *result
261 * is set to NULL.
262 *
263 * and, in fact, at least in some versions of GNU libc, it does
264 * *not* always get set if getnetbyname_r() succeeds.
265 */
266 np = NULL;
267 err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
268 &h_errnoval);
269 if (err != 0) {
270 /*
271 * XXX - dynamically allocate the buffer, and make it
272 * bigger if we get ERANGE back?
273 */
274 return 0;
275 }
276 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
277 /*
278 * We have Solaris's and IRIX's reentrant getnetbyname_r().
279 */
280 struct netent result_buf;
281 char buf[1024]; /* arbitrary size */
282
283 np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
284 #elif defined(HAVE_AIX_GETNETBYNAME_R)
285 /*
286 * We have AIX's reentrant getnetbyname_r().
287 */
288 struct netent result_buf;
289 struct netent_data net_data;
290
291 if (getnetbyname_r(name, &result_buf, &net_data) == -1)
292 np = NULL;
293 else
294 np = &result_buf;
295 #else
296 /*
297 * We don't have any getnetbyname_r(); either we have a
298 * getnetbyname() that uses thread-specific data, in which
299 * case we're thread-safe (sufficiently recent FreeBSD,
300 * sufficiently recent Darwin-based OS, sufficiently recent
301 * HP-UX, sufficiently recent Tru64 UNIX), or we have the
302 * traditional getnetbyname() (everything else, including
303 * current NetBSD and OpenBSD), in which case we're not
304 * thread-safe.
305 */
306 np = getnetbyname(name);
307 #endif
308 if (np != NULL)
309 return np->n_net;
310 else
311 return 0;
312 }
313 #endif /* _WIN32 */
314
315 /*
316 * Convert a port name to its port and protocol numbers.
317 * We assume only TCP or UDP.
318 * Return 0 upon failure.
319 */
320 int
321 pcap_nametoport(const char *name, int *port, int *proto)
322 {
323 struct addrinfo hints, *res, *ai;
324 int error;
325 struct sockaddr_in *in4;
326 #ifdef INET6
327 struct sockaddr_in6 *in6;
328 #endif
329 int tcp_port = -1;
330 int udp_port = -1;
331
332 /*
333 * We check for both TCP and UDP in case there are
334 * ambiguous entries.
335 */
336 memset(&hints, 0, sizeof(hints));
337 hints.ai_family = PF_UNSPEC;
338 hints.ai_socktype = SOCK_STREAM;
339 hints.ai_protocol = IPPROTO_TCP;
340 error = getaddrinfo(NULL, name, &hints, &res);
341 if (error != 0) {
342 if (error != EAI_NONAME &&
343 error != EAI_SERVICE) {
344 /*
345 * This is a real error, not just "there's
346 * no such service name".
347 * XXX - this doesn't return an error string.
348 */
349 return 0;
350 }
351 } else {
352 /*
353 * OK, we found it. Did it find anything?
354 */
355 for (ai = res; ai != NULL; ai = ai->ai_next) {
356 /*
357 * Does it have an address?
358 */
359 if (ai->ai_addr != NULL) {
360 /*
361 * Yes. Get a port number; we're done.
362 */
363 if (ai->ai_addr->sa_family == AF_INET) {
364 in4 = (struct sockaddr_in *)ai->ai_addr;
365 tcp_port = ntohs(in4->sin_port);
366 break;
367 }
368 #ifdef INET6
369 if (ai->ai_addr->sa_family == AF_INET6) {
370 in6 = (struct sockaddr_in6 *)ai->ai_addr;
371 tcp_port = ntohs(in6->sin6_port);
372 break;
373 }
374 #endif
375 }
376 }
377 freeaddrinfo(res);
378 }
379
380 memset(&hints, 0, sizeof(hints));
381 hints.ai_family = PF_UNSPEC;
382 hints.ai_socktype = SOCK_DGRAM;
383 hints.ai_protocol = IPPROTO_UDP;
384 error = getaddrinfo(NULL, name, &hints, &res);
385 if (error != 0) {
386 if (error != EAI_NONAME &&
387 error != EAI_SERVICE) {
388 /*
389 * This is a real error, not just "there's
390 * no such service name".
391 * XXX - this doesn't return an error string.
392 */
393 return 0;
394 }
395 } else {
396 /*
397 * OK, we found it. Did it find anything?
398 */
399 for (ai = res; ai != NULL; ai = ai->ai_next) {
400 /*
401 * Does it have an address?
402 */
403 if (ai->ai_addr != NULL) {
404 /*
405 * Yes. Get a port number; we're done.
406 */
407 if (ai->ai_addr->sa_family == AF_INET) {
408 in4 = (struct sockaddr_in *)ai->ai_addr;
409 udp_port = ntohs(in4->sin_port);
410 break;
411 }
412 #ifdef INET6
413 if (ai->ai_addr->sa_family == AF_INET6) {
414 in6 = (struct sockaddr_in6 *)ai->ai_addr;
415 udp_port = ntohs(in6->sin6_port);
416 break;
417 }
418 #endif
419 }
420 }
421 freeaddrinfo(res);
422 }
423
424 /*
425 * We need to check /etc/services for ambiguous entries.
426 * If we find an ambiguous entry, and it has the
427 * same port number, change the proto to PROTO_UNDEF
428 * so both TCP and UDP will be checked.
429 */
430 if (tcp_port >= 0) {
431 *port = tcp_port;
432 *proto = IPPROTO_TCP;
433 if (udp_port >= 0) {
434 if (udp_port == tcp_port)
435 *proto = PROTO_UNDEF;
436 #ifdef notdef
437 else
438 /* Can't handle ambiguous names that refer
439 to different port numbers. */
440 warning("ambiguous port %s in /etc/services",
441 name);
442 #endif
443 }
444 return 1;
445 }
446 if (udp_port >= 0) {
447 *port = udp_port;
448 *proto = IPPROTO_UDP;
449 return 1;
450 }
451 #if defined(ultrix) || defined(__osf__)
452 /* Special hack in case NFS isn't in /etc/services */
453 if (strcmp(name, "nfs") == 0) {
454 *port = 2049;
455 *proto = PROTO_UNDEF;
456 return 1;
457 }
458 #endif
459 return 0;
460 }
461
462 /*
463 * Convert a string in the form PPP-PPP, where correspond to ports, to
464 * a starting and ending port in a port range.
465 * Return 0 on failure.
466 */
467 int
468 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
469 {
470 char *off, *cpy;
471 int save_proto;
472
473 if ((cpy = strdup(name)) == NULL)
474 return 0;
475
476 if ((off = strchr(cpy, '-')) == NULL) {
477 free(cpy);
478 return 0;
479 }
480
481 *off = '\0';
482
483 if (pcap_nametoport(cpy, port1, proto) == 0) {
484 free(cpy);
485 return 0;
486 }
487 save_proto = *proto;
488
489 if (pcap_nametoport(off + 1, port2, proto) == 0) {
490 free(cpy);
491 return 0;
492 }
493 free(cpy);
494
495 if (*proto != save_proto)
496 *proto = PROTO_UNDEF;
497
498 return 1;
499 }
500
501 /*
502 * XXX - not guaranteed to be thread-safe! See below for platforms
503 * on which it is thread-safe and on which it isn't.
504 */
505 int
506 pcap_nametoproto(const char *str)
507 {
508 struct protoent *p;
509 #if defined(HAVE_LINUX_GETNETBYNAME_R)
510 /*
511 * We have Linux's reentrant getprotobyname_r().
512 */
513 struct protoent result_buf;
514 char buf[1024]; /* arbitrary size */
515 int err;
516
517 err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
518 if (err != 0) {
519 /*
520 * XXX - dynamically allocate the buffer, and make it
521 * bigger if we get ERANGE back?
522 */
523 return 0;
524 }
525 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
526 /*
527 * We have Solaris's and IRIX's reentrant getprotobyname_r().
528 */
529 struct protoent result_buf;
530 char buf[1024]; /* arbitrary size */
531
532 p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
533 #elif defined(HAVE_AIX_GETNETBYNAME_R)
534 /*
535 * We have AIX's reentrant getprotobyname_r().
536 */
537 struct protoent result_buf;
538 struct protoent_data proto_data;
539
540 if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
541 p = NULL;
542 else
543 p = &result_buf;
544 #else
545 /*
546 * We don't have any getprotobyname_r(); either we have a
547 * getprotobyname() that uses thread-specific data, in which
548 * case we're thread-safe (sufficiently recent FreeBSD,
549 * sufficiently recent Darwin-based OS, sufficiently recent
550 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
551 * the traditional getprotobyname() (everything else, including
552 * current NetBSD and OpenBSD), in which case we're not
553 * thread-safe.
554 */
555 p = getprotobyname(str);
556 #endif
557 if (p != 0)
558 return p->p_proto;
559 else
560 return PROTO_UNDEF;
561 }
562
563 #include "ethertype.h"
564
565 struct eproto {
566 const char *s;
567 u_short p;
568 };
569
570 /*
571 * Static data base of ether protocol types.
572 * tcpdump used to import this, and it's declared as an export on
573 * Debian, at least, so make it a public symbol, even though we
574 * don't officially export it by declaring it in a header file.
575 * (Programs *should* do this themselves, as tcpdump now does.)
576 *
577 * We declare it here, right before defining it, to squelch any
578 * warnings we might get from compilers about the lack of a
579 * declaration.
580 */
581 PCAP_API struct eproto eproto_db[];
582 PCAP_API_DEF struct eproto eproto_db[] = {
583 { "aarp", ETHERTYPE_AARP },
584 { "arp", ETHERTYPE_ARP },
585 { "atalk", ETHERTYPE_ATALK },
586 { "decnet", ETHERTYPE_DN },
587 { "ip", ETHERTYPE_IP },
588 #ifdef INET6
589 { "ip6", ETHERTYPE_IPV6 },
590 #endif
591 { "lat", ETHERTYPE_LAT },
592 { "loopback", ETHERTYPE_LOOPBACK },
593 { "mopdl", ETHERTYPE_MOPDL },
594 { "moprc", ETHERTYPE_MOPRC },
595 { "rarp", ETHERTYPE_REVARP },
596 { "sca", ETHERTYPE_SCA },
597 { (char *)0, 0 }
598 };
599
600 int
601 pcap_nametoeproto(const char *s)
602 {
603 struct eproto *p = eproto_db;
604
605 while (p->s != 0) {
606 if (strcmp(p->s, s) == 0)
607 return p->p;
608 p += 1;
609 }
610 return PROTO_UNDEF;
611 }
612
613 #include "llc.h"
614
615 /* Static data base of LLC values. */
616 static struct eproto llc_db[] = {
617 { "iso", LLCSAP_ISONS },
618 { "stp", LLCSAP_8021D },
619 { "ipx", LLCSAP_IPX },
620 { "netbeui", LLCSAP_NETBEUI },
621 { (char *)0, 0 }
622 };
623
624 int
625 pcap_nametollc(const char *s)
626 {
627 struct eproto *p = llc_db;
628
629 while (p->s != 0) {
630 if (strcmp(p->s, s) == 0)
631 return p->p;
632 p += 1;
633 }
634 return PROTO_UNDEF;
635 }
636
637 /* Hex digit to 8-bit unsigned integer. */
638 static inline u_char
639 xdtoi(u_char c)
640 {
641 if (c >= '0' && c <= '9')
642 return (u_char)(c - '0');
643 else if (c >= 'a' && c <= 'f')
644 return (u_char)(c - 'a' + 10);
645 else
646 return (u_char)(c - 'A' + 10);
647 }
648
649 int
650 __pcap_atoin(const char *s, bpf_u_int32 *addr)
651 {
652 u_int n;
653 int len;
654
655 *addr = 0;
656 len = 0;
657 for (;;) {
658 n = 0;
659 while (*s && *s != '.') {
660 if (n > 25) {
661 /* The result will be > 255 */
662 return -1;
663 }
664 n = n * 10 + *s++ - '0';
665 }
666 if (n > 255)
667 return -1;
668 *addr <<= 8;
669 *addr |= n & 0xff;
670 len += 8;
671 if (*s == '\0')
672 return len;
673 ++s;
674 }
675 /* NOTREACHED */
676 }
677
678 /*
679 * If 's' is not a string that is a well-formed DECnet address (aa.nnnn),
680 * return zero. Otherwise parse the address into the low 16 bits of 'addr'
681 * and return a non-zero. The binary DECnet address consists of a 6-bit area
682 * number and a 10-bit node number; neither area 0 nor node 0 are valid for
683 * normal addressing purposes, but either can appear on the wire.
684 */
685 int
686 __pcap_atodn(const char *s, bpf_u_int32 *addr)
687 {
688 #define AREASHIFT 10
689 #define AREAMASK 0176000
690 #define NODEMASK 01777
691
692 /* Initialize to squelch a compiler warning only. */
693 u_int node = 0, area = 0;
694 /*
695 * +--+ +--+
696 * | | | |
697 * v | v |
698 * --> START --> AREA --> DOT --> NODE -->
699 * | | | |
700 * | v v |
701 * +--------> INVALID <------+
702 */
703 enum {
704 START,
705 AREA,
706 DOT,
707 NODE,
708 INVALID
709 } fsm_state = START;
710
711 while (*s) {
712 switch (fsm_state) {
713 case START:
714 if (PCAP_ISDIGIT(*s)) {
715 area = *s - '0';
716 fsm_state = AREA;
717 break;
718 }
719 fsm_state = INVALID;
720 break;
721 case AREA:
722 if (*s == '.') {
723 fsm_state = DOT;
724 break;
725 }
726 if (PCAP_ISDIGIT(*s)) {
727 area = area * 10 + *s - '0';
728 if (area <= AREAMASK >> AREASHIFT)
729 break;
730 }
731 fsm_state = INVALID;
732 break;
733 case DOT:
734 if (PCAP_ISDIGIT(*s)) {
735 node = *s - '0';
736 fsm_state = NODE;
737 break;
738 }
739 fsm_state = INVALID;
740 break;
741 case NODE:
742 if (PCAP_ISDIGIT(*s)) {
743 node = node * 10 + *s - '0';
744 if (node <= NODEMASK)
745 break;
746 }
747 fsm_state = INVALID;
748 break;
749 case INVALID:
750 return 0;
751 } /* switch */
752 s++;
753 } /* while */
754 /*
755 * This condition is false if the string comes from the lexer, but
756 * let's not depend on that.
757 */
758 if (fsm_state != NODE)
759 return 0;
760
761 *addr = area << AREASHIFT | node;
762 return(32);
763 }
764
765 /*
766 * Convert 's', which can have the one of the forms:
767 *
768 * "xx:xx:xx:xx:xx:xx"
769 * "xx.xx.xx.xx.xx.xx"
770 * "xx-xx-xx-xx-xx-xx"
771 * "xxxx.xxxx.xxxx"
772 * "xxxxxxxxxxxx"
773 *
774 * (or various mixes of ':', '.', and '-') into a new
775 * ethernet address. Assumes 's' is well formed.
776 */
777 u_char *
778 pcap_ether_aton(const char *s)
779 {
780 register u_char *ep, *e;
781 register u_char d;
782
783 e = ep = (u_char *)malloc(6);
784 if (e == NULL)
785 return (NULL);
786
787 while (*s) {
788 if (*s == ':' || *s == '.' || *s == '-')
789 s += 1;
790 d = xdtoi(*s++);
791 if (PCAP_ISXDIGIT(*s)) {
792 d <<= 4;
793 d |= xdtoi(*s++);
794 }
795 *ep++ = d;
796 }
797
798 return (e);
799 }
800
801 #ifndef HAVE_ETHER_HOSTTON
802 /*
803 * Roll our own.
804 *
805 * This should be thread-safe, as we define the static variables
806 * we use to be thread-local, and as pcap_next_etherent() does so
807 * as well.
808 */
809 u_char *
810 pcap_ether_hostton(const char *name)
811 {
812 register struct pcap_etherent *ep;
813 register u_char *ap;
814 static thread_local FILE *fp = NULL;
815 static thread_local int init = 0;
816
817 if (!init) {
818 fp = fopen(PCAP_ETHERS_FILE, "r");
819 ++init;
820 if (fp == NULL)
821 return (NULL);
822 } else if (fp == NULL)
823 return (NULL);
824 else
825 rewind(fp);
826
827 while ((ep = pcap_next_etherent(fp)) != NULL) {
828 if (strcmp(ep->name, name) == 0) {
829 ap = (u_char *)malloc(6);
830 if (ap != NULL) {
831 memcpy(ap, ep->addr, 6);
832 return (ap);
833 }
834 break;
835 }
836 }
837 return (NULL);
838 }
839 #else
840 /*
841 * Use the OS-supplied routine.
842 * This *should* be thread-safe; the API doesn't have a static buffer.
843 */
844 u_char *
845 pcap_ether_hostton(const char *name)
846 {
847 register u_char *ap;
848 u_char a[6];
849 char namebuf[1024];
850
851 /*
852 * In AIX 7.1 and 7.2: int ether_hostton(char *, struct ether_addr *);
853 */
854 pcapint_strlcpy(namebuf, name, sizeof(namebuf));
855 ap = NULL;
856 if (ether_hostton(namebuf, (struct ether_addr *)a) == 0) {
857 ap = (u_char *)malloc(6);
858 if (ap != NULL)
859 memcpy((char *)ap, (char *)a, 6);
860 }
861 return (ap);
862 }
863 #endif