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 #include <sys/param.h>
42 #include <sys/types.h> /* concession to AIX */
43 #include <sys/socket.h>
46 #include <netinet/in.h>
50 #ifdef HAVE_ETHER_HOSTTON
52 * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
55 #ifdef HAVE_NETINET_IF_ETHER_H
56 struct mbuf; /* Squelch compiler warnings on some platforms for */
57 struct rtentry; /* declarations in <net/if.h> */
58 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
59 #include <netinet/if_ether.h>
60 #endif /* HAVE_NETINET_IF_ETHER_H */
61 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
62 #include <netinet/ether.h>
63 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
64 #endif /* HAVE_ETHER_HOSTTON */
65 #include <arpa/inet.h>
78 #include <pcap/namedb.h>
80 #ifdef HAVE_OS_PROTO_H
85 #define NTOHL(x) (x) = ntohl(x)
86 #define NTOHS(x) (x) = ntohs(x)
89 static inline int xdtoi(int);
92 * Convert host name to internet address.
93 * Return 0 upon failure.
96 pcap_nametoaddr(const char *name)
99 static bpf_u_int32 *hlist[2];
104 if ((hp = gethostbyname(name)) != NULL) {
106 hlist[0] = (bpf_u_int32 *)hp->h_addr;
110 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
112 return (bpf_u_int32 **)hp->h_addr_list;
121 pcap_nametoaddrinfo(const char *name)
123 struct addrinfo hints, *res;
126 memset(&hints, 0, sizeof(hints));
127 hints.ai_family = PF_UNSPEC;
128 hints.ai_socktype = SOCK_STREAM; /*not really*/
129 hints.ai_protocol = IPPROTO_TCP; /*not really*/
130 error = getaddrinfo(name, NULL, &hints, &res);
139 * Convert net name to internet address.
140 * Return 0 upon failure.
143 pcap_nametonetaddr(const char *name)
148 if ((np = getnetbyname(name)) != NULL)
154 * There's no "getnetbyname()" on Windows.
161 * Convert a port name to its port and protocol numbers.
162 * We assume only TCP or UDP.
163 * Return 0 upon failure.
166 pcap_nametoport(const char *name, int *port, int *proto)
173 * We need to check /etc/services for ambiguous entries.
174 * If we find the ambiguous entry, and it has the
175 * same port number, change the proto to PROTO_UNDEF
176 * so both TCP and UDP will be checked.
178 sp = getservbyname(name, "tcp");
179 if (sp != NULL) tcp_port = ntohs(sp->s_port);
180 sp = getservbyname(name, "udp");
181 if (sp != NULL) udp_port = ntohs(sp->s_port);
184 *proto = IPPROTO_TCP;
186 if (udp_port == tcp_port)
187 *proto = PROTO_UNDEF;
190 /* Can't handle ambiguous names that refer
191 to different port numbers. */
192 warning("ambiguous port %s in /etc/services",
200 *proto = IPPROTO_UDP;
203 #if defined(ultrix) || defined(__osf__)
204 /* Special hack in case NFS isn't in /etc/services */
205 if (strcmp(name, "nfs") == 0) {
207 *proto = PROTO_UNDEF;
215 * Convert a string in the form PPP-PPP, where correspond to ports, to
216 * a starting and ending port in a port range.
217 * Return 0 on failure.
220 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
226 if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
227 if ((cpy = strdup(name)) == NULL)
230 if ((off = strchr(cpy, '-')) == NULL) {
237 if (pcap_nametoport(cpy, port1, proto) == 0) {
243 if (pcap_nametoport(off + 1, port2, proto) == 0) {
249 if (*proto != save_proto)
250 *proto = PROTO_UNDEF;
254 *proto = PROTO_UNDEF;
261 pcap_nametoproto(const char *str)
265 p = getprotobyname(str);
272 #include "ethertype.h"
279 /* Static data base of ether protocol types. */
280 struct eproto eproto_db[] = {
282 /* The FreeBSD elf linker generates a request to copy this array
283 * (including its size) when you link with -lpcap. In order to
284 * not bump the major version number of this libpcap.so, we need
285 * to ensure that the array stays the same size. Since PUP is
286 * likely never seen in real life any more, it's the first to
287 * be sacrificed (in favor of ip6).
289 { "pup", ETHERTYPE_PUP },
291 { "xns", ETHERTYPE_NS },
292 { "ip", ETHERTYPE_IP },
294 { "ip6", ETHERTYPE_IPV6 },
296 { "arp", ETHERTYPE_ARP },
297 { "rarp", ETHERTYPE_REVARP },
298 { "sprite", ETHERTYPE_SPRITE },
299 { "mopdl", ETHERTYPE_MOPDL },
300 { "moprc", ETHERTYPE_MOPRC },
301 { "decnet", ETHERTYPE_DN },
302 { "lat", ETHERTYPE_LAT },
303 { "sca", ETHERTYPE_SCA },
304 { "lanbridge", ETHERTYPE_LANBRIDGE },
305 { "vexp", ETHERTYPE_VEXP },
306 { "vprod", ETHERTYPE_VPROD },
307 { "atalk", ETHERTYPE_ATALK },
308 { "atalkarp", ETHERTYPE_AARP },
309 { "loopback", ETHERTYPE_LOOPBACK },
310 { "decdts", ETHERTYPE_DECDTS },
311 { "decdns", ETHERTYPE_DECDNS },
316 pcap_nametoeproto(const char *s)
318 struct eproto *p = eproto_db;
321 if (strcmp(p->s, s) == 0)
330 /* Static data base of LLC values. */
331 static struct eproto llc_db[] = {
332 { "iso", LLCSAP_ISONS },
333 { "stp", LLCSAP_8021D },
334 { "ipx", LLCSAP_IPX },
335 { "netbeui", LLCSAP_NETBEUI },
340 pcap_nametollc(const char *s)
342 struct eproto *p = llc_db;
345 if (strcmp(p->s, s) == 0)
352 /* Hex digit to integer. */
366 __pcap_atoin(const char *s, bpf_u_int32 *addr)
375 while (*s && *s != '.')
376 n = n * 10 + *s++ - '0';
388 __pcap_atodn(const char *s, bpf_u_int32 *addr)
391 #define AREAMASK 0176000
392 #define NODEMASK 01777
396 if (sscanf(s, "%d.%d", &area, &node) != 2)
397 bpf_error("malformed decnet address '%s'", s);
399 *addr = (area << AREASHIFT) & AREAMASK;
400 *addr |= (node & NODEMASK);
406 * Convert 's', which can have the one of the forms:
408 * "xx:xx:xx:xx:xx:xx"
409 * "xx.xx.xx.xx.xx.xx"
410 * "xx-xx-xx-xx-xx-xx"
414 * (or various mixes of ':', '.', and '-') into a new
415 * ethernet address. Assumes 's' is well formed.
418 pcap_ether_aton(const char *s)
420 register u_char *ep, *e;
423 e = ep = (u_char *)malloc(6);
428 if (*s == ':' || *s == '.' || *s == '-')
431 if (isxdigit((unsigned char)*s)) {
441 #ifndef HAVE_ETHER_HOSTTON
444 pcap_ether_hostton(const char *name)
446 register struct pcap_etherent *ep;
448 static FILE *fp = NULL;
452 fp = fopen(PCAP_ETHERS_FILE, "r");
456 } else if (fp == NULL)
461 while ((ep = pcap_next_etherent(fp)) != NULL) {
462 if (strcmp(ep->name, name) == 0) {
463 ap = (u_char *)malloc(6);
465 memcpy(ap, ep->addr, 6);
475 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
476 #ifndef HAVE_STRUCT_ETHER_ADDR
478 unsigned char ether_addr_octet[6];
481 extern int ether_hostton(const char *, struct ether_addr *);
484 /* Use the os supplied routines */
486 pcap_ether_hostton(const char *name)
492 if (ether_hostton(name, (struct ether_addr *)a) == 0) {
493 ap = (u_char *)malloc(6);
495 memcpy((char *)ap, (char *)a, 6);
502 __pcap_nametodnaddr(const char *name)
505 struct nodeent *getnodebyname();
509 nep = getnodebyname(name);
510 if (nep == ((struct nodeent *)0))
511 bpf_error("unknown decnet host name '%s'\n", name);
513 memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
516 bpf_error("decnet name support not included, '%s' cannot be translated\n",