]>
The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
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> /* concession to AIX */
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 */
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>
138 #include "pcap-int.h"
141 #include <pcap/namedb.h>
142 #include "nametoaddr.h"
144 #ifdef HAVE_OS_PROTO_H
145 #include "os-proto.h"
149 #define NTOHL(x) (x) = ntohl(x)
150 #define NTOHS(x) (x) = ntohs(x)
153 static inline int xdtoi(int);
156 * Convert host name to internet address.
157 * Return 0 upon failure.
160 pcap_nametoaddr(const char *name
)
163 static bpf_u_int32
*hlist
[2];
168 if ((hp
= gethostbyname(name
)) != NULL
) {
170 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
174 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
176 return (bpf_u_int32
**)hp
->h_addr_list
;
185 pcap_nametoaddrinfo(const char *name
)
187 struct addrinfo hints
, *res
;
190 memset(&hints
, 0, sizeof(hints
));
191 hints
.ai_family
= PF_UNSPEC
;
192 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
193 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
194 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
203 * Convert net name to internet address.
204 * Return 0 upon failure.
207 pcap_nametonetaddr(const char *name
)
212 if ((np
= getnetbyname(name
)) != NULL
)
218 * There's no "getnetbyname()" on Windows.
220 * XXX - I guess we could use the BSD code to read
221 * C:\Windows\System32\drivers\etc/networks, assuming
222 * that's its home on all the versions of Windows
223 * we use, but that file probably just has the loopback
224 * network on 127/24 on 99 44/100% of Windows machines.
226 * (Heck, these days it probably just has that on 99 44/100%
227 * of *UN*X* machines.)
234 * Convert a port name to its port and protocol numbers.
235 * We assume only TCP or UDP.
236 * Return 0 upon failure.
239 pcap_nametoport(const char *name
, int *port
, int *proto
)
246 * We need to check /etc/services for ambiguous entries.
247 * If we find the ambiguous entry, and it has the
248 * same port number, change the proto to PROTO_UNDEF
249 * so both TCP and UDP will be checked.
251 sp
= getservbyname(name
, "tcp");
252 if (sp
!= NULL
) tcp_port
= ntohs(sp
->s_port
);
253 sp
= getservbyname(name
, "udp");
254 if (sp
!= NULL
) udp_port
= ntohs(sp
->s_port
);
257 *proto
= IPPROTO_TCP
;
259 if (udp_port
== tcp_port
)
260 *proto
= PROTO_UNDEF
;
263 /* Can't handle ambiguous names that refer
264 to different port numbers. */
265 warning("ambiguous port %s in /etc/services",
273 *proto
= IPPROTO_UDP
;
276 #if defined(ultrix) || defined(__osf__)
277 /* Special hack in case NFS isn't in /etc/services */
278 if (strcmp(name
, "nfs") == 0) {
280 *proto
= PROTO_UNDEF
;
288 * Convert a string in the form PPP-PPP, where correspond to ports, to
289 * a starting and ending port in a port range.
290 * Return 0 on failure.
293 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
299 if (sscanf(name
, "%d-%d", &p1
, &p2
) != 2) {
300 if ((cpy
= strdup(name
)) == NULL
)
303 if ((off
= strchr(cpy
, '-')) == NULL
) {
310 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
316 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
322 if (*proto
!= save_proto
)
323 *proto
= PROTO_UNDEF
;
327 *proto
= PROTO_UNDEF
;
334 pcap_nametoproto(const char *str
)
338 p
= getprotobyname(str
);
345 #include "ethertype.h"
353 * Static data base of ether protocol types.
354 * tcpdump used to import this, and it's declared as an export on
355 * Debian, at least, so make it a public symbol, even though we
356 * don't officially export it by declaring it in a header file.
357 * (Programs *should* do this themselves, as tcpdump now does.)
359 PCAP_API_DEF
struct eproto eproto_db
[] = {
360 { "pup", ETHERTYPE_PUP
},
361 { "xns", ETHERTYPE_NS
},
362 { "ip", ETHERTYPE_IP
},
364 { "ip6", ETHERTYPE_IPV6
},
366 { "arp", ETHERTYPE_ARP
},
367 { "rarp", ETHERTYPE_REVARP
},
368 { "sprite", ETHERTYPE_SPRITE
},
369 { "mopdl", ETHERTYPE_MOPDL
},
370 { "moprc", ETHERTYPE_MOPRC
},
371 { "decnet", ETHERTYPE_DN
},
372 { "lat", ETHERTYPE_LAT
},
373 { "sca", ETHERTYPE_SCA
},
374 { "lanbridge", ETHERTYPE_LANBRIDGE
},
375 { "vexp", ETHERTYPE_VEXP
},
376 { "vprod", ETHERTYPE_VPROD
},
377 { "atalk", ETHERTYPE_ATALK
},
378 { "atalkarp", ETHERTYPE_AARP
},
379 { "loopback", ETHERTYPE_LOOPBACK
},
380 { "decdts", ETHERTYPE_DECDTS
},
381 { "decdns", ETHERTYPE_DECDNS
},
386 pcap_nametoeproto(const char *s
)
388 struct eproto
*p
= eproto_db
;
391 if (strcmp(p
->s
, s
) == 0)
400 /* Static data base of LLC values. */
401 static struct eproto llc_db
[] = {
402 { "iso", LLCSAP_ISONS
},
403 { "stp", LLCSAP_8021D
},
404 { "ipx", LLCSAP_IPX
},
405 { "netbeui", LLCSAP_NETBEUI
},
410 pcap_nametollc(const char *s
)
412 struct eproto
*p
= llc_db
;
415 if (strcmp(p
->s
, s
) == 0)
422 /* Hex digit to integer. */
436 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
445 while (*s
&& *s
!= '.')
446 n
= n
* 10 + *s
++ - '0';
458 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
461 #define AREAMASK 0176000
462 #define NODEMASK 01777
466 if (sscanf(s
, "%d.%d", &area
, &node
) != 2)
469 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
470 *addr
|= (node
& NODEMASK
);
476 * Convert 's', which can have the one of the forms:
478 * "xx:xx:xx:xx:xx:xx"
479 * "xx.xx.xx.xx.xx.xx"
480 * "xx-xx-xx-xx-xx-xx"
484 * (or various mixes of ':', '.', and '-') into a new
485 * ethernet address. Assumes 's' is well formed.
488 pcap_ether_aton(const char *s
)
490 register u_char
*ep
, *e
;
493 e
= ep
= (u_char
*)malloc(6);
498 if (*s
== ':' || *s
== '.' || *s
== '-')
501 if (isxdigit((unsigned char)*s
)) {
511 #ifndef HAVE_ETHER_HOSTTON
514 pcap_ether_hostton(const char *name
)
516 register struct pcap_etherent
*ep
;
518 static FILE *fp
= NULL
;
523 if (pcap_fopen(&fp
, PCAP_ETHERS_FILE
, "r") != 0)
525 } else if (fp
== NULL
)
530 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
531 if (strcmp(ep
->name
, name
) == 0) {
532 ap
= (u_char
*)malloc(6);
534 memcpy(ap
, ep
->addr
, 6);
543 /* Use the os supplied routines */
545 pcap_ether_hostton(const char *name
)
551 if (ether_hostton(name
, (struct ether_addr
*)a
) == 0) {
552 ap
= (u_char
*)malloc(6);
554 memcpy((char *)ap
, (char *)a
, 6);
561 __pcap_nametodnaddr(const char *name
, u_short
*res
)
564 struct nodeent
*getnodebyname();
567 nep
= getnodebyname(name
);
568 if (nep
== ((struct nodeent
*)0))
571 memcpy((char *)res
, (char *)nep
->n_addr
, sizeof(unsigned short));