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