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>
35 #include <pcap-stdinc.h>
39 * To quote the MSDN page for getaddrinfo() at
41 * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
43 * "Support for getaddrinfo on Windows 2000 and older versions
44 * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
45 * later. To execute an application that uses this function on earlier
46 * versions of Windows, then you need to include the Ws2tcpip.h and
47 * Wspiapi.h files. When the Wspiapi.h include file is added, the
48 * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
49 * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
50 * function is implemented in such a way that if the Ws2_32.dll or the
51 * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
52 * Preview for Windows 2000) does not include getaddrinfo, then a
53 * version of getaddrinfo is implemented inline based on code in the
54 * Wspiapi.h header file. This inline code will be used on older Windows
55 * platforms that do not natively support the getaddrinfo function."
57 * We use getaddrinfo(), so we include Wspiapi.h here. pcap-stdinc.h
58 * includes Ws2tcpip.h, so we don't need to include it ourselves.
65 #include <sys/param.h>
66 #include <sys/types.h> /* concession to AIX */
67 #include <sys/socket.h>
70 #include <netinet/in.h>
74 #ifdef HAVE_ETHER_HOSTTON
76 * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
79 #ifdef HAVE_NETINET_IF_ETHER_H
80 struct mbuf
; /* Squelch compiler warnings on some platforms for */
81 struct rtentry
; /* declarations in <net/if.h> */
82 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
83 #include <netinet/if_ether.h>
84 #endif /* HAVE_NETINET_IF_ETHER_H */
85 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
86 #include <netinet/ether.h>
87 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
88 #endif /* HAVE_ETHER_HOSTTON */
89 #include <arpa/inet.h>
102 #include <pcap/namedb.h>
103 #include "nametoaddr.h"
105 #ifdef HAVE_OS_PROTO_H
106 #include "os-proto.h"
110 #define NTOHL(x) (x) = ntohl(x)
111 #define NTOHS(x) (x) = ntohs(x)
114 static inline int xdtoi(int);
117 * Convert host name to internet address.
118 * Return 0 upon failure.
121 pcap_nametoaddr(const char *name
)
124 static bpf_u_int32
*hlist
[2];
129 if ((hp
= gethostbyname(name
)) != NULL
) {
131 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
135 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
137 return (bpf_u_int32
**)hp
->h_addr_list
;
146 pcap_nametoaddrinfo(const char *name
)
148 struct addrinfo hints
, *res
;
151 memset(&hints
, 0, sizeof(hints
));
152 hints
.ai_family
= PF_UNSPEC
;
153 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
154 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
155 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
164 * Convert net name to internet address.
165 * Return 0 upon failure.
168 pcap_nametonetaddr(const char *name
)
173 if ((np
= getnetbyname(name
)) != NULL
)
179 * There's no "getnetbyname()" on Windows.
181 * XXX - I guess we could use the BSD code to read
182 * C:\Windows\System32\drivers\etc/networks, assuming
183 * that's its home on all the versions of Windows
184 * we use, but that file probably just has the loopback
185 * network on 127/24 on 99 44/100% of Windows machines.
187 * (Heck, these days it probably just has that on 99 44/100%
188 * of *UN*X* machines.)
195 * Convert a port name to its port and protocol numbers.
196 * We assume only TCP or UDP.
197 * Return 0 upon failure.
200 pcap_nametoport(const char *name
, int *port
, int *proto
)
207 * We need to check /etc/services for ambiguous entries.
208 * If we find the ambiguous entry, and it has the
209 * same port number, change the proto to PROTO_UNDEF
210 * so both TCP and UDP will be checked.
212 sp
= getservbyname(name
, "tcp");
213 if (sp
!= NULL
) tcp_port
= ntohs(sp
->s_port
);
214 sp
= getservbyname(name
, "udp");
215 if (sp
!= NULL
) udp_port
= ntohs(sp
->s_port
);
218 *proto
= IPPROTO_TCP
;
220 if (udp_port
== tcp_port
)
221 *proto
= PROTO_UNDEF
;
224 /* Can't handle ambiguous names that refer
225 to different port numbers. */
226 warning("ambiguous port %s in /etc/services",
234 *proto
= IPPROTO_UDP
;
237 #if defined(ultrix) || defined(__osf__)
238 /* Special hack in case NFS isn't in /etc/services */
239 if (strcmp(name
, "nfs") == 0) {
241 *proto
= PROTO_UNDEF
;
249 * Convert a string in the form PPP-PPP, where correspond to ports, to
250 * a starting and ending port in a port range.
251 * Return 0 on failure.
254 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
260 if (sscanf(name
, "%d-%d", &p1
, &p2
) != 2) {
261 if ((cpy
= strdup(name
)) == NULL
)
264 if ((off
= strchr(cpy
, '-')) == NULL
) {
271 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
277 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
283 if (*proto
!= save_proto
)
284 *proto
= PROTO_UNDEF
;
288 *proto
= PROTO_UNDEF
;
295 pcap_nametoproto(const char *str
)
299 p
= getprotobyname(str
);
306 #include "ethertype.h"
314 * Static data base of ether protocol types.
315 * tcpdump used to import this, and it's declared as an export on
316 * Debian, at least, so make it a public symbol, even though we
317 * don't officially export it by declaring it in a header file.
318 * (Programs *should* do this themselves, as tcpdump now does.)
320 PCAP_API_DEF
struct eproto eproto_db
[] = {
321 { "pup", ETHERTYPE_PUP
},
322 { "xns", ETHERTYPE_NS
},
323 { "ip", ETHERTYPE_IP
},
325 { "ip6", ETHERTYPE_IPV6
},
327 { "arp", ETHERTYPE_ARP
},
328 { "rarp", ETHERTYPE_REVARP
},
329 { "sprite", ETHERTYPE_SPRITE
},
330 { "mopdl", ETHERTYPE_MOPDL
},
331 { "moprc", ETHERTYPE_MOPRC
},
332 { "decnet", ETHERTYPE_DN
},
333 { "lat", ETHERTYPE_LAT
},
334 { "sca", ETHERTYPE_SCA
},
335 { "lanbridge", ETHERTYPE_LANBRIDGE
},
336 { "vexp", ETHERTYPE_VEXP
},
337 { "vprod", ETHERTYPE_VPROD
},
338 { "atalk", ETHERTYPE_ATALK
},
339 { "atalkarp", ETHERTYPE_AARP
},
340 { "loopback", ETHERTYPE_LOOPBACK
},
341 { "decdts", ETHERTYPE_DECDTS
},
342 { "decdns", ETHERTYPE_DECDNS
},
347 pcap_nametoeproto(const char *s
)
349 struct eproto
*p
= eproto_db
;
352 if (strcmp(p
->s
, s
) == 0)
361 /* Static data base of LLC values. */
362 static struct eproto llc_db
[] = {
363 { "iso", LLCSAP_ISONS
},
364 { "stp", LLCSAP_8021D
},
365 { "ipx", LLCSAP_IPX
},
366 { "netbeui", LLCSAP_NETBEUI
},
371 pcap_nametollc(const char *s
)
373 struct eproto
*p
= llc_db
;
376 if (strcmp(p
->s
, s
) == 0)
383 /* Hex digit to integer. */
397 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
406 while (*s
&& *s
!= '.')
407 n
= n
* 10 + *s
++ - '0';
419 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
422 #define AREAMASK 0176000
423 #define NODEMASK 01777
427 if (sscanf(s
, "%d.%d", &area
, &node
) != 2)
430 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
431 *addr
|= (node
& NODEMASK
);
437 * Convert 's', which can have the one of the forms:
439 * "xx:xx:xx:xx:xx:xx"
440 * "xx.xx.xx.xx.xx.xx"
441 * "xx-xx-xx-xx-xx-xx"
445 * (or various mixes of ':', '.', and '-') into a new
446 * ethernet address. Assumes 's' is well formed.
449 pcap_ether_aton(const char *s
)
451 register u_char
*ep
, *e
;
454 e
= ep
= (u_char
*)malloc(6);
459 if (*s
== ':' || *s
== '.' || *s
== '-')
462 if (isxdigit((unsigned char)*s
)) {
472 #ifndef HAVE_ETHER_HOSTTON
475 pcap_ether_hostton(const char *name
)
477 register struct pcap_etherent
*ep
;
479 static FILE *fp
= NULL
;
483 fp
= fopen(PCAP_ETHERS_FILE
, "r");
487 } else if (fp
== NULL
)
492 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
493 if (strcmp(ep
->name
, name
) == 0) {
494 ap
= (u_char
*)malloc(6);
496 memcpy(ap
, ep
->addr
, 6);
506 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
507 #ifndef HAVE_STRUCT_ETHER_ADDR
509 unsigned char ether_addr_octet
[6];
512 extern int ether_hostton(const char *, struct ether_addr
*);
515 /* Use the os supplied routines */
517 pcap_ether_hostton(const char *name
)
523 if (ether_hostton(name
, (struct ether_addr
*)a
) == 0) {
524 ap
= (u_char
*)malloc(6);
526 memcpy((char *)ap
, (char *)a
, 6);
533 __pcap_nametodnaddr(const char *name
, u_short
*res
)
536 struct nodeent
*getnodebyname();
539 nep
= getnodebyname(name
);
540 if (nep
== ((struct nodeent
*)0))
543 memcpy((char *)res
, (char *)nep
->n_addr
, sizeof(unsigned short));