]> The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
Merge branch 'master' of https://github.com/rixed/libpcap into rixed-master
[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>
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 /* what declares ether_hostton() */
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 "pcap-int.h"
137
138 #include "diag-control.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 /*
154 * Convert host name to internet address.
155 * Return 0 upon failure.
156 * XXX - not thread-safe; don't use it inside libpcap.
157 */
158 bpf_u_int32 **
159 pcap_nametoaddr(const char *name)
160 {
161 #ifndef h_addr
162 static bpf_u_int32 *hlist[2];
163 #endif
164 bpf_u_int32 **p;
165 struct hostent *hp;
166
167 /*
168 * gethostbyname() is deprecated on Windows, perhaps because
169 * it's not thread-safe, or because it doesn't support IPv6,
170 * or both.
171 *
172 * We deprecate pcap_nametoaddr() on all platforms because
173 * it's not thread-safe; we supply it for backwards compatibility,
174 * so suppress the deprecation warning. We could, I guess,
175 * use getaddrinfo() and construct the array ourselves, but
176 * that's probably not worth the effort, as that wouldn't make
177 * this thread-safe - we can't change the API to require that
178 * our caller free the address array, so we still have to reuse
179 * a local array.
180 */
181 DIAG_OFF_DEPRECATION
182 if ((hp = gethostbyname(name)) != NULL) {
183 DIAG_ON_DEPRECATION
184 #ifndef h_addr
185 hlist[0] = (bpf_u_int32 *)hp->h_addr;
186 NTOHL(hp->h_addr);
187 return hlist;
188 #else
189 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
190 NTOHL(**p);
191 return (bpf_u_int32 **)hp->h_addr_list;
192 #endif
193 }
194 else
195 return 0;
196 }
197
198 struct addrinfo *
199 pcap_nametoaddrinfo(const char *name)
200 {
201 struct addrinfo hints, *res;
202 int error;
203
204 memset(&hints, 0, sizeof(hints));
205 hints.ai_family = PF_UNSPEC;
206 hints.ai_socktype = SOCK_STREAM; /*not really*/
207 hints.ai_protocol = IPPROTO_TCP; /*not really*/
208 error = getaddrinfo(name, NULL, &hints, &res);
209 if (error)
210 return NULL;
211 else
212 return res;
213 }
214
215 /*
216 * Convert net name to internet address.
217 * Return 0 upon failure.
218 * XXX - not guaranteed to be thread-safe! See below for platforms
219 * on which it is thread-safe and on which it isn't.
220 */
221 bpf_u_int32
222 pcap_nametonetaddr(const char *name)
223 {
224 #ifdef _WIN32
225 /*
226 * There's no "getnetbyname()" on Windows.
227 *
228 * XXX - I guess we could use the BSD code to read
229 * C:\Windows\System32\drivers\etc/networks, assuming
230 * that's its home on all the versions of Windows
231 * we use, but that file probably just has the loopback
232 * network on 127/24 on 99 44/100% of Windows machines.
233 *
234 * (Heck, these days it probably just has that on 99 44/100%
235 * of *UN*X* machines.)
236 */
237 return 0;
238 #else
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 #endif /* _WIN32 */
313 }
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 u_int p1, p2;
471 char *off, *cpy;
472 int save_proto;
473
474 if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
475 if ((cpy = strdup(name)) == NULL)
476 return 0;
477
478 if ((off = strchr(cpy, '-')) == NULL) {
479 free(cpy);
480 return 0;
481 }
482
483 *off = '\0';
484
485 if (pcap_nametoport(cpy, port1, proto) == 0) {
486 free(cpy);
487 return 0;
488 }
489 save_proto = *proto;
490
491 if (pcap_nametoport(off + 1, port2, proto) == 0) {
492 free(cpy);
493 return 0;
494 }
495 free(cpy);
496
497 if (*proto != save_proto)
498 *proto = PROTO_UNDEF;
499 } else {
500 *port1 = p1;
501 *port2 = p2;
502 *proto = PROTO_UNDEF;
503 }
504
505 return 1;
506 }
507
508 /*
509 * XXX - not guaranteed to be thread-safe! See below for platforms
510 * on which it is thread-safe and on which it isn't.
511 */
512 int
513 pcap_nametoproto(const char *str)
514 {
515 struct protoent *p;
516 #if defined(HAVE_LINUX_GETNETBYNAME_R)
517 /*
518 * We have Linux's reentrant getprotobyname_r().
519 */
520 struct protoent result_buf;
521 char buf[1024]; /* arbitrary size */
522 int err;
523
524 err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
525 if (err != 0) {
526 /*
527 * XXX - dynamically allocate the buffer, and make it
528 * bigger if we get ERANGE back?
529 */
530 return 0;
531 }
532 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
533 /*
534 * We have Solaris's and IRIX's reentrant getprotobyname_r().
535 */
536 struct protoent result_buf;
537 char buf[1024]; /* arbitrary size */
538
539 p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
540 #elif defined(HAVE_AIX_GETNETBYNAME_R)
541 /*
542 * We have AIX's reentrant getprotobyname_r().
543 */
544 struct protoent result_buf;
545 struct protoent_data proto_data;
546
547 if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
548 p = NULL;
549 else
550 p = &result_buf;
551 #else
552 /*
553 * We don't have any getprotobyname_r(); either we have a
554 * getprotobyname() that uses thread-specific data, in which
555 * case we're thread-safe (sufficiently recent FreeBSD,
556 * sufficiently recent Darwin-based OS, sufficiently recent
557 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
558 * the traditional getprotobyname() (everything else, including
559 * current NetBSD and OpenBSD), in which case we're not
560 * thread-safe.
561 */
562 p = getprotobyname(str);
563 #endif
564 if (p != 0)
565 return p->p_proto;
566 else
567 return PROTO_UNDEF;
568 }
569
570 #include "ethertype.h"
571
572 struct eproto {
573 const char *s;
574 u_short p;
575 };
576
577 /*
578 * Static data base of ether protocol types.
579 * tcpdump used to import this, and it's declared as an export on
580 * Debian, at least, so make it a public symbol, even though we
581 * don't officially export it by declaring it in a header file.
582 * (Programs *should* do this themselves, as tcpdump now does.)
583 *
584 * We declare it here, right before defining it, to squelch any
585 * warnings we might get from compilers about the lack of a
586 * declaration.
587 */
588 PCAP_API struct eproto eproto_db[];
589 PCAP_API_DEF struct eproto eproto_db[] = {
590 { "pup", ETHERTYPE_PUP },
591 { "xns", ETHERTYPE_NS },
592 { "ip", ETHERTYPE_IP },
593 #ifdef INET6
594 { "ip6", ETHERTYPE_IPV6 },
595 #endif
596 { "arp", ETHERTYPE_ARP },
597 { "rarp", ETHERTYPE_REVARP },
598 { "sprite", ETHERTYPE_SPRITE },
599 { "mopdl", ETHERTYPE_MOPDL },
600 { "moprc", ETHERTYPE_MOPRC },
601 { "decnet", ETHERTYPE_DN },
602 { "lat", ETHERTYPE_LAT },
603 { "sca", ETHERTYPE_SCA },
604 { "lanbridge", ETHERTYPE_LANBRIDGE },
605 { "vexp", ETHERTYPE_VEXP },
606 { "vprod", ETHERTYPE_VPROD },
607 { "atalk", ETHERTYPE_ATALK },
608 { "atalkarp", ETHERTYPE_AARP },
609 { "loopback", ETHERTYPE_LOOPBACK },
610 { "decdts", ETHERTYPE_DECDTS },
611 { "decdns", ETHERTYPE_DECDNS },
612 { (char *)0, 0 }
613 };
614
615 int
616 pcap_nametoeproto(const char *s)
617 {
618 struct eproto *p = eproto_db;
619
620 while (p->s != 0) {
621 if (strcmp(p->s, s) == 0)
622 return p->p;
623 p += 1;
624 }
625 return PROTO_UNDEF;
626 }
627
628 #include "llc.h"
629
630 /* Static data base of LLC values. */
631 static struct eproto llc_db[] = {
632 { "iso", LLCSAP_ISONS },
633 { "stp", LLCSAP_8021D },
634 { "ipx", LLCSAP_IPX },
635 { "netbeui", LLCSAP_NETBEUI },
636 { (char *)0, 0 }
637 };
638
639 int
640 pcap_nametollc(const char *s)
641 {
642 struct eproto *p = llc_db;
643
644 while (p->s != 0) {
645 if (strcmp(p->s, s) == 0)
646 return p->p;
647 p += 1;
648 }
649 return PROTO_UNDEF;
650 }
651
652 /* Hex digit to 8-bit unsigned integer. */
653 static inline u_char
654 xdtoi(u_char c)
655 {
656 if (isdigit(c))
657 return (u_char)(c - '0');
658 else if (islower(c))
659 return (u_char)(c - 'a' + 10);
660 else
661 return (u_char)(c - 'A' + 10);
662 }
663
664 int
665 __pcap_atoin(const char *s, bpf_u_int32 *addr)
666 {
667 u_int n;
668 int len;
669
670 *addr = 0;
671 len = 0;
672 for (;;) {
673 n = 0;
674 while (*s && *s != '.')
675 n = n * 10 + *s++ - '0';
676 *addr <<= 8;
677 *addr |= n & 0xff;
678 len += 8;
679 if (*s == '\0')
680 return len;
681 ++s;
682 }
683 /* NOTREACHED */
684 }
685
686 int
687 __pcap_atodn(const char *s, bpf_u_int32 *addr)
688 {
689 #define AREASHIFT 10
690 #define AREAMASK 0176000
691 #define NODEMASK 01777
692
693 u_int node, area;
694
695 if (sscanf(s, "%d.%d", &area, &node) != 2)
696 return(0);
697
698 *addr = (area << AREASHIFT) & AREAMASK;
699 *addr |= (node & NODEMASK);
700
701 return(32);
702 }
703
704 /*
705 * Convert 's', which can have the one of the forms:
706 *
707 * "xx:xx:xx:xx:xx:xx"
708 * "xx.xx.xx.xx.xx.xx"
709 * "xx-xx-xx-xx-xx-xx"
710 * "xxxx.xxxx.xxxx"
711 * "xxxxxxxxxxxx"
712 *
713 * (or various mixes of ':', '.', and '-') into a new
714 * ethernet address. Assumes 's' is well formed.
715 */
716 u_char *
717 pcap_ether_aton(const char *s)
718 {
719 register u_char *ep, *e;
720 register u_char d;
721
722 e = ep = (u_char *)malloc(6);
723 if (e == NULL)
724 return (NULL);
725
726 while (*s) {
727 if (*s == ':' || *s == '.' || *s == '-')
728 s += 1;
729 d = xdtoi(*s++);
730 if (isxdigit((unsigned char)*s)) {
731 d <<= 4;
732 d |= xdtoi(*s++);
733 }
734 *ep++ = d;
735 }
736
737 return (e);
738 }
739
740 #ifndef HAVE_ETHER_HOSTTON
741 /*
742 * Roll our own.
743 * XXX - not thread-safe, because pcap_next_etherent() isn't thread-
744 * safe! Needs a mutex or a thread-safe pcap_next_etherent().
745 */
746 u_char *
747 pcap_ether_hostton(const char *name)
748 {
749 register struct pcap_etherent *ep;
750 register u_char *ap;
751 static FILE *fp = NULL;
752 static int init = 0;
753
754 if (!init) {
755 fp = fopen(PCAP_ETHERS_FILE, "r");
756 ++init;
757 if (fp == NULL)
758 return (NULL);
759 } else if (fp == NULL)
760 return (NULL);
761 else
762 rewind(fp);
763
764 while ((ep = pcap_next_etherent(fp)) != NULL) {
765 if (strcmp(ep->name, name) == 0) {
766 ap = (u_char *)malloc(6);
767 if (ap != NULL) {
768 memcpy(ap, ep->addr, 6);
769 return (ap);
770 }
771 break;
772 }
773 }
774 return (NULL);
775 }
776 #else
777 /*
778 * Use the OS-supplied routine.
779 * This *should* be thread-safe; the API doesn't have a static buffer.
780 */
781 u_char *
782 pcap_ether_hostton(const char *name)
783 {
784 register u_char *ap;
785 u_char a[6];
786
787 ap = NULL;
788 if (ether_hostton(name, (struct ether_addr *)a) == 0) {
789 ap = (u_char *)malloc(6);
790 if (ap != NULL)
791 memcpy((char *)ap, (char *)a, 6);
792 }
793 return (ap);
794 }
795 #endif
796
797 /*
798 * XXX - not guaranteed to be thread-safe!
799 */
800 int
801 #ifdef DECNETLIB
802 __pcap_nametodnaddr(const char *name, u_short *res)
803 {
804 struct nodeent *getnodebyname();
805 struct nodeent *nep;
806
807 nep = getnodebyname(name);
808 if (nep == ((struct nodeent *)0))
809 return(0);
810
811 memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
812 return(1);
813 #else
814 __pcap_nametodnaddr(const char *name _U_, u_short *res _U_)
815 {
816 return(0);
817 #endif
818 }