2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * Name to id translation routines used by the scanner.
22 * These functions are not time critical.
30 #include <sys/types.h>
31 #include <netdnet/dnetdb.h>
40 * To quote the MSDN page for getaddrinfo() at
42 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
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."
58 * We use getaddrinfo(), so we include Wspiapi.h here.
63 #include <sys/param.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
68 #include <netinet/in.h>
70 #ifdef HAVE_ETHER_HOSTTON
71 #if defined(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
73 * OK, just include <net/ethernet.h>.
75 #include <net/ethernet.h>
76 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
78 * OK, just include <netinet/ether.h>
80 #include <netinet/ether.h>
81 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
83 * OK, just include <sys/ethernet.h>
85 #include <sys/ethernet.h>
86 #elif defined(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
88 * OK, just include <arpa/inet.h>
90 #include <arpa/inet.h>
91 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
93 * OK, include <netinet/if_ether.h>, after all the other stuff we
94 * need to include or define for its benefit.
96 #define NEED_NETINET_IF_ETHER_H
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.
103 #ifdef HAVE_STRUCT_ETHER_ADDR
104 #define NEED_NETINET_IF_ETHER_H
105 #else /* HAVE_STRUCT_ETHER_ADDR */
107 unsigned char ether_addr_octet[6];
109 #endif /* HAVE_STRUCT_ETHER_ADDR */
110 #endif /* what declares ether_hostton() */
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 */
118 #ifndef HAVE_DECL_ETHER_HOSTTON
120 * No header declares it, so declare it ourselves.
122 extern int ether_hostton(const char *, struct ether_addr *);
123 #endif /* !defined(HAVE_DECL_ETHER_HOSTTON) */
124 #endif /* HAVE_ETHER_HOSTTON */
126 #include <arpa/inet.h>
136 #include "pcap-int.h"
139 #include <pcap/namedb.h>
140 #include "nametoaddr.h"
142 #ifdef HAVE_OS_PROTO_H
143 #include "os-proto.h"
147 #define NTOHL(x) (x) = ntohl(x)
148 #define NTOHS(x) (x) = ntohs(x)
152 * Convert host name to internet address.
153 * Return 0 upon failure.
154 * XXX - not thread-safe; don't use it inside libpcap.
157 pcap_nametoaddr(const char *name)
160 static bpf_u_int32 *hlist[2];
165 if ((hp = gethostbyname(name)) != NULL) {
167 hlist[0] = (bpf_u_int32 *)hp->h_addr;
171 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
173 return (bpf_u_int32 **)hp->h_addr_list;
181 pcap_nametoaddrinfo(const char *name)
183 struct addrinfo hints, *res;
186 memset(&hints, 0, sizeof(hints));
187 hints.ai_family = PF_UNSPEC;
188 hints.ai_socktype = SOCK_STREAM; /*not really*/
189 hints.ai_protocol = IPPROTO_TCP; /*not really*/
190 error = getaddrinfo(name, NULL, &hints, &res);
198 * Convert net name to internet address.
199 * Return 0 upon failure.
200 * XXX - not guaranteed to be thread-safe! See below for platforms
201 * on which it is thread-safe and on which it isn't.
204 pcap_nametonetaddr(const char *name)
208 * There's no "getnetbyname()" on Windows.
210 * XXX - I guess we could use the BSD code to read
211 * C:\Windows\System32\drivers\etc/networks, assuming
212 * that's its home on all the versions of Windows
213 * we use, but that file probably just has the loopback
214 * network on 127/24 on 99 44/100% of Windows machines.
216 * (Heck, these days it probably just has that on 99 44/100%
217 * of *UN*X* machines.)
225 #if defined(HAVE_LINUX_GETNETBYNAME_R)
227 * We have Linux's reentrant getnetbyname_r().
229 struct netent result_buf;
230 char buf[1024]; /* arbitrary size */
235 * Apparently, the man page at
237 * http://man7.org/linux/man-pages/man3/getnetbyname_r.3.html
241 * If the function call successfully obtains a network record,
242 * then *result is set pointing to result_buf; otherwise, *result
245 * and, in fact, at least in some versions of GNU libc, it does
246 * *not* always get set if getnetbyname_r() succeeds.
249 err = getnetbyname_r(name, &result_buf, buf, sizeof buf, &np,
253 * XXX - dynamically allocate the buffer, and make it
254 * bigger if we get ERANGE back?
258 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
260 * We have Solaris's and IRIX's reentrant getnetbyname_r().
262 struct netent result_buf;
263 char buf[1024]; /* arbitrary size */
265 np = getnetbyname_r(name, &result_buf, buf, (int)sizeof buf);
266 #elif defined(HAVE_AIX_GETNETBYNAME_R)
268 * We have AIX's reentrant getnetbyname_r().
270 struct netent result_buf;
271 struct netent_data net_data;
273 if (getnetbyname_r(name, &result_buf, &net_data) == -1)
279 * We don't have any getnetbyname_r(); either we have a
280 * getnetbyname() that uses thread-specific data, in which
281 * case we're thread-safe (sufficiently recent FreeBSD,
282 * sufficiently recent Darwin-based OS, sufficiently recent
283 * HP-UX, sufficiently recent Tru64 UNIX), or we have the
284 * traditional getnetbyname() (everything else, including
285 * current NetBSD and OpenBSD), in which case we're not
288 np = getnetbyname(name);
298 * Convert a port name to its port and protocol numbers.
299 * We assume only TCP or UDP.
300 * Return 0 upon failure.
303 pcap_nametoport(const char *name, int *port, int *proto)
305 struct addrinfo hints, *res, *ai;
307 struct sockaddr_in *in4;
309 struct sockaddr_in6 *in6;
315 * We check for both TCP and UDP in case there are
318 memset(&hints, 0, sizeof(hints));
319 hints.ai_family = PF_UNSPEC;
320 hints.ai_socktype = SOCK_STREAM;
321 hints.ai_protocol = IPPROTO_TCP;
322 error = getaddrinfo(NULL, name, &hints, &res);
324 if (error != EAI_NONAME &&
325 error != EAI_SERVICE) {
327 * This is a real error, not just "there's
328 * no such service name".
329 * XXX - this doesn't return an error string.
335 * OK, we found it. Did it find anything?
337 for (ai = res; ai != NULL; ai = ai->ai_next) {
339 * Does it have an address?
341 if (ai->ai_addr != NULL) {
343 * Yes. Get a port number; we're done.
345 if (ai->ai_addr->sa_family == AF_INET) {
346 in4 = (struct sockaddr_in *)ai->ai_addr;
347 tcp_port = ntohs(in4->sin_port);
351 if (ai->ai_addr->sa_family == AF_INET6) {
352 in6 = (struct sockaddr_in6 *)ai->ai_addr;
353 tcp_port = ntohs(in6->sin6_port);
362 memset(&hints, 0, sizeof(hints));
363 hints.ai_family = PF_UNSPEC;
364 hints.ai_socktype = SOCK_DGRAM;
365 hints.ai_protocol = IPPROTO_UDP;
366 error = getaddrinfo(NULL, name, &hints, &res);
368 if (error != EAI_NONAME &&
369 error != EAI_SERVICE) {
371 * This is a real error, not just "there's
372 * no such service name".
373 * XXX - this doesn't return an error string.
379 * OK, we found it. Did it find anything?
381 for (ai = res; ai != NULL; ai = ai->ai_next) {
383 * Does it have an address?
385 if (ai->ai_addr != NULL) {
387 * Yes. Get a port number; we're done.
389 if (ai->ai_addr->sa_family == AF_INET) {
390 in4 = (struct sockaddr_in *)ai->ai_addr;
391 udp_port = ntohs(in4->sin_port);
395 if (ai->ai_addr->sa_family == AF_INET6) {
396 in6 = (struct sockaddr_in6 *)ai->ai_addr;
397 udp_port = ntohs(in6->sin6_port);
407 * We need to check /etc/services for ambiguous entries.
408 * If we find an ambiguous entry, and it has the
409 * same port number, change the proto to PROTO_UNDEF
410 * so both TCP and UDP will be checked.
414 *proto = IPPROTO_TCP;
416 if (udp_port == tcp_port)
417 *proto = PROTO_UNDEF;
420 /* Can't handle ambiguous names that refer
421 to different port numbers. */
422 warning("ambiguous port %s in /etc/services",
430 *proto = IPPROTO_UDP;
433 #if defined(ultrix) || defined(__osf__)
434 /* Special hack in case NFS isn't in /etc/services */
435 if (strcmp(name, "nfs") == 0) {
437 *proto = PROTO_UNDEF;
445 * Convert a string in the form PPP-PPP, where correspond to ports, to
446 * a starting and ending port in a port range.
447 * Return 0 on failure.
450 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
456 if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
457 if ((cpy = strdup(name)) == NULL)
460 if ((off = strchr(cpy, '-')) == NULL) {
467 if (pcap_nametoport(cpy, port1, proto) == 0) {
473 if (pcap_nametoport(off + 1, port2, proto) == 0) {
479 if (*proto != save_proto)
480 *proto = PROTO_UNDEF;
484 *proto = PROTO_UNDEF;
491 * XXX - not guaranteed to be thread-safe! See below for platforms
492 * on which it is thread-safe and on which it isn't.
495 pcap_nametoproto(const char *str)
498 #if defined(HAVE_LINUX_GETNETBYNAME_R)
500 * We have Linux's reentrant getprotobyname_r().
502 struct protoent result_buf;
503 char buf[1024]; /* arbitrary size */
506 err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
509 * XXX - dynamically allocate the buffer, and make it
510 * bigger if we get ERANGE back?
514 #elif defined(HAVE_SOLARIS_IRIX_GETNETBYNAME_R)
516 * We have Solaris's and IRIX's reentrant getprotobyname_r().
518 struct protoent result_buf;
519 char buf[1024]; /* arbitrary size */
521 p = getprotobyname_r(str, &result_buf, buf, (int)sizeof buf);
522 #elif defined(HAVE_AIX_GETNETBYNAME_R)
524 * We have AIX's reentrant getprotobyname_r().
526 struct protoent result_buf;
527 struct protoent_data proto_data;
529 if (getprotobyname_r(str, &result_buf, &proto_data) == -1)
535 * We don't have any getprotobyname_r(); either we have a
536 * getprotobyname() that uses thread-specific data, in which
537 * case we're thread-safe (sufficiently recent FreeBSD,
538 * sufficiently recent Darwin-based OS, sufficiently recent
539 * HP-UX, sufficiently recent Tru64 UNIX, Windows), or we have
540 * the traditional getprotobyname() (everything else, including
541 * current NetBSD and OpenBSD), in which case we're not
544 p = getprotobyname(str);
552 #include "ethertype.h"
560 * Static data base of ether protocol types.
561 * tcpdump used to import this, and it's declared as an export on
562 * Debian, at least, so make it a public symbol, even though we
563 * don't officially export it by declaring it in a header file.
564 * (Programs *should* do this themselves, as tcpdump now does.)
566 * We declare it here, right before defining it, to squelch any
567 * warnings we might get from compilers about the lack of a
570 PCAP_API struct eproto eproto_db[];
571 PCAP_API_DEF struct eproto eproto_db[] = {
572 { "pup", ETHERTYPE_PUP },
573 { "xns", ETHERTYPE_NS },
574 { "ip", ETHERTYPE_IP },
576 { "ip6", ETHERTYPE_IPV6 },
578 { "arp", ETHERTYPE_ARP },
579 { "rarp", ETHERTYPE_REVARP },
580 { "sprite", ETHERTYPE_SPRITE },
581 { "mopdl", ETHERTYPE_MOPDL },
582 { "moprc", ETHERTYPE_MOPRC },
583 { "decnet", ETHERTYPE_DN },
584 { "lat", ETHERTYPE_LAT },
585 { "sca", ETHERTYPE_SCA },
586 { "lanbridge", ETHERTYPE_LANBRIDGE },
587 { "vexp", ETHERTYPE_VEXP },
588 { "vprod", ETHERTYPE_VPROD },
589 { "atalk", ETHERTYPE_ATALK },
590 { "atalkarp", ETHERTYPE_AARP },
591 { "loopback", ETHERTYPE_LOOPBACK },
592 { "decdts", ETHERTYPE_DECDTS },
593 { "decdns", ETHERTYPE_DECDNS },
598 pcap_nametoeproto(const char *s)
600 struct eproto *p = eproto_db;
603 if (strcmp(p->s, s) == 0)
612 /* Static data base of LLC values. */
613 static struct eproto llc_db[] = {
614 { "iso", LLCSAP_ISONS },
615 { "stp", LLCSAP_8021D },
616 { "ipx", LLCSAP_IPX },
617 { "netbeui", LLCSAP_NETBEUI },
622 pcap_nametollc(const char *s)
624 struct eproto *p = llc_db;
627 if (strcmp(p->s, s) == 0)
634 /* Hex digit to 8-bit unsigned integer. */
639 return (u_char)(c - '0');
641 return (u_char)(c - 'a' + 10);
643 return (u_char)(c - 'A' + 10);
647 __pcap_atoin(const char *s, bpf_u_int32 *addr)
656 while (*s && *s != '.') {
658 /* The result will be > 255 */
661 n = n * 10 + *s++ - '0';
676 __pcap_atodn(const char *s, bpf_u_int32 *addr)
679 #define AREAMASK 0176000
680 #define NODEMASK 01777
684 if (sscanf(s, "%d.%d", &area, &node) != 2)
687 *addr = (area << AREASHIFT) & AREAMASK;
688 *addr |= (node & NODEMASK);
694 * Convert 's', which can have the one of the forms:
696 * "xx:xx:xx:xx:xx:xx"
697 * "xx.xx.xx.xx.xx.xx"
698 * "xx-xx-xx-xx-xx-xx"
702 * (or various mixes of ':', '.', and '-') into a new
703 * ethernet address. Assumes 's' is well formed.
706 pcap_ether_aton(const char *s)
708 register u_char *ep, *e;
711 e = ep = (u_char *)malloc(6);
716 if (*s == ':' || *s == '.' || *s == '-')
719 if (isxdigit((unsigned char)*s)) {
729 #ifndef HAVE_ETHER_HOSTTON
732 * XXX - not thread-safe, because pcap_next_etherent() isn't thread-
733 * safe! Needs a mutex or a thread-safe pcap_next_etherent().
736 pcap_ether_hostton(const char *name)
738 register struct pcap_etherent *ep;
740 static FILE *fp = NULL;
744 fp = fopen(PCAP_ETHERS_FILE, "r");
748 } else if (fp == NULL)
753 while ((ep = pcap_next_etherent(fp)) != NULL) {
754 if (strcmp(ep->name, name) == 0) {
755 ap = (u_char *)malloc(6);
757 memcpy(ap, ep->addr, 6);
767 * Use the OS-supplied routine.
768 * This *should* be thread-safe; the API doesn't have a static buffer.
771 pcap_ether_hostton(const char *name)
777 if (ether_hostton(name, (struct ether_addr *)a) == 0) {
778 ap = (u_char *)malloc(6);
780 memcpy((char *)ap, (char *)a, 6);
787 * XXX - not guaranteed to be thread-safe!
791 __pcap_nametodnaddr(const char *name, u_short *res)
793 struct nodeent *getnodebyname();
796 nep = getnodebyname(name);
797 if (nep == ((struct nodeent *)0))
800 memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));
803 __pcap_nametodnaddr(const char *name _U_, u_short *res _U_)