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