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.
32 #include <sys/types.h>
33 #include <netdnet/dnetdb.h>
37 #include <pcap-stdinc.h>
41 * To quote the MSDN page for getaddrinfo() at
43 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
45 * "Support for getaddrinfo on Windows 2000 and older versions
46 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
47 * later. To execute an application that uses this function on earlier
48 * versions of Windows, then you need to include the Ws2tcpip.h and
49 * Wspiapi.h files. When the Wspiapi.h include file is added, the
50 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
51 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
52 * function is implemented in such a way that if the Ws2_32.dll or the
53 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
54 * Preview for Windows 2000) does not include getaddrinfo, then a
55 * version of getaddrinfo is implemented inline based on code in the
56 * Wspiapi.h header file. This inline code will be used on older Windows
57 * platforms that do not natively support the getaddrinfo function."
59 * We use getaddrinfo(), so we include Wspiapi.h here. pcap-stdinc.h
60 * includes Ws2tcpip.h, so we don't need to include it ourselves.
67 #include <sys/param.h>
68 #include <sys/types.h> /* concession to AIX */
69 #include <sys/socket.h>
72 #include <netinet/in.h>
76 #ifdef HAVE_ETHER_HOSTTON
78 * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
81 #ifdef HAVE_NETINET_IF_ETHER_H
82 struct mbuf; /* Squelch compiler warnings on some platforms for */
83 struct rtentry; /* declarations in <net/if.h> */
84 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
85 #include <netinet/if_ether.h>
86 #endif /* HAVE_NETINET_IF_ETHER_H */
87 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
88 #include <netinet/ether.h>
89 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
90 #endif /* HAVE_ETHER_HOSTTON */
91 #include <arpa/inet.h>
101 #include "pcap-int.h"
104 #include <pcap/namedb.h>
105 #include "nametoaddr.h"
107 #ifdef HAVE_OS_PROTO_H
108 #include "os-proto.h"
112 #define NTOHL(x) (x) = ntohl(x)
113 #define NTOHS(x) (x) = ntohs(x)
116 static inline int xdtoi(int);
119 * Convert host name to internet address.
120 * Return 0 upon failure.
123 pcap_nametoaddr(const char *name)
126 static bpf_u_int32 *hlist[2];
131 if ((hp = gethostbyname(name)) != NULL) {
133 hlist[0] = (bpf_u_int32 *)hp->h_addr;
137 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
139 return (bpf_u_int32 **)hp->h_addr_list;
148 pcap_nametoaddrinfo(const char *name)
150 struct addrinfo hints, *res;
153 memset(&hints, 0, sizeof(hints));
154 hints.ai_family = PF_UNSPEC;
155 hints.ai_socktype = SOCK_STREAM; /*not really*/
156 hints.ai_protocol = IPPROTO_TCP; /*not really*/
157 error = getaddrinfo(name, NULL, &hints, &res);
166 * Convert net name to internet address.
167 * Return 0 upon failure.
170 pcap_nametonetaddr(const char *name)
175 if ((np = getnetbyname(name)) != NULL)
181 * There's no "getnetbyname()" on Windows.
183 * XXX - I guess we could use the BSD code to read
184 * C:\Windows\System32\drivers\etc/networks, assuming
185 * that's its home on all the versions of Windows
186 * we use, but that file probably just has the loopback
187 * network on 127/24 on 99 44/100% of Windows machines.
189 * (Heck, these days it probably just has that on 99 44/100%
190 * of *UN*X* machines.)
197 * Convert a port name to its port and protocol numbers.
198 * We assume only TCP or UDP.
199 * Return 0 upon failure.
202 pcap_nametoport(const char *name, int *port, int *proto)
209 * We need to check /etc/services for ambiguous entries.
210 * If we find the ambiguous entry, and it has the
211 * same port number, change the proto to PROTO_UNDEF
212 * so both TCP and UDP will be checked.
214 sp = getservbyname(name, "tcp");
215 if (sp != NULL) tcp_port = ntohs(sp->s_port);
216 sp = getservbyname(name, "udp");
217 if (sp != NULL) udp_port = ntohs(sp->s_port);
220 *proto = IPPROTO_TCP;
222 if (udp_port == tcp_port)
223 *proto = PROTO_UNDEF;
226 /* Can't handle ambiguous names that refer
227 to different port numbers. */
228 warning("ambiguous port %s in /etc/services",
236 *proto = IPPROTO_UDP;
239 #if defined(ultrix) || defined(__osf__)
240 /* Special hack in case NFS isn't in /etc/services */
241 if (strcmp(name, "nfs") == 0) {
243 *proto = PROTO_UNDEF;
251 * Convert a string in the form PPP-PPP, where correspond to ports, to
252 * a starting and ending port in a port range.
253 * Return 0 on failure.
256 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
262 if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
263 if ((cpy = strdup(name)) == NULL)
266 if ((off = strchr(cpy, '-')) == NULL) {
273 if (pcap_nametoport(cpy, port1, proto) == 0) {
279 if (pcap_nametoport(off + 1, port2, proto) == 0) {
285 if (*proto != save_proto)
286 *proto = PROTO_UNDEF;
290 *proto = PROTO_UNDEF;
297 pcap_nametoproto(const char *str)
301 p = getprotobyname(str);
308 #include "ethertype.h"
316 * Static data base of ether protocol types.
317 * tcpdump used to import this, and it's declared as an export on
318 * Debian, at least, so make it a public symbol, even though we
319 * don't officially export it by declaring it in a header file.
320 * (Programs *should* do this themselves, as tcpdump now does.)
322 PCAP_API_DEF struct eproto eproto_db[] = {
324 /* The FreeBSD elf linker generates a request to copy this array
325 * (including its size) when you link with -lpcap. In order to
326 * not bump the major version number of this libpcap.so, we need
327 * to ensure that the array stays the same size. Since PUP is
328 * likely never seen in real life any more, it's the first to
329 * be sacrificed (in favor of ip6).
331 { "pup", ETHERTYPE_PUP },
333 { "xns", ETHERTYPE_NS },
334 { "ip", ETHERTYPE_IP },
336 { "ip6", ETHERTYPE_IPV6 },
338 { "arp", ETHERTYPE_ARP },
339 { "rarp", ETHERTYPE_REVARP },
340 { "sprite", ETHERTYPE_SPRITE },
341 { "mopdl", ETHERTYPE_MOPDL },
342 { "moprc", ETHERTYPE_MOPRC },
343 { "decnet", ETHERTYPE_DN },
344 { "lat", ETHERTYPE_LAT },
345 { "sca", ETHERTYPE_SCA },
346 { "lanbridge", ETHERTYPE_LANBRIDGE },
347 { "vexp", ETHERTYPE_VEXP },
348 { "vprod", ETHERTYPE_VPROD },
349 { "atalk", ETHERTYPE_ATALK },
350 { "atalkarp", ETHERTYPE_AARP },
351 { "loopback", ETHERTYPE_LOOPBACK },
352 { "decdts", ETHERTYPE_DECDTS },
353 { "decdns", ETHERTYPE_DECDNS },
358 pcap_nametoeproto(const char *s)
360 struct eproto *p = eproto_db;
363 if (strcmp(p->s, s) == 0)
372 /* Static data base of LLC values. */
373 static struct eproto llc_db[] = {
374 { "iso", LLCSAP_ISONS },
375 { "stp", LLCSAP_8021D },
376 { "ipx", LLCSAP_IPX },
377 { "netbeui", LLCSAP_NETBEUI },
382 pcap_nametollc(const char *s)
384 struct eproto *p = llc_db;
387 if (strcmp(p->s, s) == 0)
394 /* Hex digit to integer. */
408 __pcap_atoin(const char *s, bpf_u_int32 *addr)
417 while (*s && *s != '.')
418 n = n * 10 + *s++ - '0';
430 __pcap_atodn(const char *s, bpf_u_int32 *addr)
433 #define AREAMASK 0176000
434 #define NODEMASK 01777
438 if (sscanf(s, "%d.%d", &area, &node) != 2)
441 *addr = (area << AREASHIFT) & AREAMASK;
442 *addr |= (node & NODEMASK);
448 * Convert 's', which can have the one of the forms:
450 * "xx:xx:xx:xx:xx:xx"
451 * "xx.xx.xx.xx.xx.xx"
452 * "xx-xx-xx-xx-xx-xx"
456 * (or various mixes of ':', '.', and '-') into a new
457 * ethernet address. Assumes 's' is well formed.
460 pcap_ether_aton(const char *s)
462 register u_char *ep, *e;
465 e = ep = (u_char *)malloc(6);
470 if (*s == ':' || *s == '.' || *s == '-')
473 if (isxdigit((unsigned char)*s)) {
483 #ifndef HAVE_ETHER_HOSTTON
486 pcap_ether_hostton(const char *name)
488 register struct pcap_etherent *ep;
490 static FILE *fp = NULL;
494 fp = fopen(PCAP_ETHERS_FILE, "r");
498 } else if (fp == NULL)
503 while ((ep = pcap_next_etherent(fp)) != NULL) {
504 if (strcmp(ep->name, name) == 0) {
505 ap = (u_char *)malloc(6);
507 memcpy(ap, ep->addr, 6);
517 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
518 #ifndef HAVE_STRUCT_ETHER_ADDR
520 unsigned char ether_addr_octet[6];
523 extern int ether_hostton(const char *, struct ether_addr *);
526 /* Use the os supplied routines */
528 pcap_ether_hostton(const char *name)
534 if (ether_hostton(name, (struct ether_addr *)a) == 0) {
535 ap = (u_char *)malloc(6);
537 memcpy((char *)ap, (char *)a, 6);
544 __pcap_nametodnaddr(const char *name, u_short *res)
547 struct nodeent *getnodebyname();
550 nep = getnodebyname(name);
551 if (nep == ((struct nodeent *)0))
554 memcpy((char *)res, (char *)nep->n_addr, sizeof(unsigned short));