]>
The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
8fc2e8d9f2bfad3f648377f42cba23612d7d0ce6
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>
86 * NetBSD insists on having <net/if.h> pull in <net/dlt.h>, so that
87 * including <net/if.h> defines a bunch of stuff we later define in our
88 * pcap/dlt.h; those definitions will be different, if they haven't
89 * picked up all of our changes yet.
91 * Undefine some of the offending items.
94 #undef DLT_USB /* we now define it as DLT_FREEBSD_USB */
95 #undef DLT_MATCHING_MAX /* we may define more DLT_ values than they do */
98 #endif /* HAVE_NETINET_IF_ETHER_H */
99 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
100 #include <netinet/ether.h>
101 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
102 #endif /* HAVE_ETHER_HOSTTON */
103 #include <arpa/inet.h>
113 #include "pcap-int.h"
116 #include <pcap/namedb.h>
117 #include "nametoaddr.h"
119 #ifdef HAVE_OS_PROTO_H
120 #include "os-proto.h"
124 #define NTOHL(x) (x) = ntohl(x)
125 #define NTOHS(x) (x) = ntohs(x)
128 static inline int xdtoi(int);
131 * Convert host name to internet address.
132 * Return 0 upon failure.
135 pcap_nametoaddr(const char *name
)
138 static bpf_u_int32
*hlist
[2];
143 if ((hp
= gethostbyname(name
)) != NULL
) {
145 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
149 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
151 return (bpf_u_int32
**)hp
->h_addr_list
;
160 pcap_nametoaddrinfo(const char *name
)
162 struct addrinfo hints
, *res
;
165 memset(&hints
, 0, sizeof(hints
));
166 hints
.ai_family
= PF_UNSPEC
;
167 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
168 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
169 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
178 * Convert net name to internet address.
179 * Return 0 upon failure.
182 pcap_nametonetaddr(const char *name
)
187 if ((np
= getnetbyname(name
)) != NULL
)
193 * There's no "getnetbyname()" on Windows.
195 * XXX - I guess we could use the BSD code to read
196 * C:\Windows\System32\drivers\etc/networks, assuming
197 * that's its home on all the versions of Windows
198 * we use, but that file probably just has the loopback
199 * network on 127/24 on 99 44/100% of Windows machines.
201 * (Heck, these days it probably just has that on 99 44/100%
202 * of *UN*X* machines.)
209 * Convert a port name to its port and protocol numbers.
210 * We assume only TCP or UDP.
211 * Return 0 upon failure.
214 pcap_nametoport(const char *name
, int *port
, int *proto
)
221 * We need to check /etc/services for ambiguous entries.
222 * If we find the ambiguous entry, and it has the
223 * same port number, change the proto to PROTO_UNDEF
224 * so both TCP and UDP will be checked.
226 sp
= getservbyname(name
, "tcp");
227 if (sp
!= NULL
) tcp_port
= ntohs(sp
->s_port
);
228 sp
= getservbyname(name
, "udp");
229 if (sp
!= NULL
) udp_port
= ntohs(sp
->s_port
);
232 *proto
= IPPROTO_TCP
;
234 if (udp_port
== tcp_port
)
235 *proto
= PROTO_UNDEF
;
238 /* Can't handle ambiguous names that refer
239 to different port numbers. */
240 warning("ambiguous port %s in /etc/services",
248 *proto
= IPPROTO_UDP
;
251 #if defined(ultrix) || defined(__osf__)
252 /* Special hack in case NFS isn't in /etc/services */
253 if (strcmp(name
, "nfs") == 0) {
255 *proto
= PROTO_UNDEF
;
263 * Convert a string in the form PPP-PPP, where correspond to ports, to
264 * a starting and ending port in a port range.
265 * Return 0 on failure.
268 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
274 if (sscanf(name
, "%d-%d", &p1
, &p2
) != 2) {
275 if ((cpy
= strdup(name
)) == NULL
)
278 if ((off
= strchr(cpy
, '-')) == NULL
) {
285 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
291 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
297 if (*proto
!= save_proto
)
298 *proto
= PROTO_UNDEF
;
302 *proto
= PROTO_UNDEF
;
309 pcap_nametoproto(const char *str
)
313 p
= getprotobyname(str
);
320 #include "ethertype.h"
328 * Static data base of ether protocol types.
329 * tcpdump used to import this, and it's declared as an export on
330 * Debian, at least, so make it a public symbol, even though we
331 * don't officially export it by declaring it in a header file.
332 * (Programs *should* do this themselves, as tcpdump now does.)
334 PCAP_API_DEF
struct eproto eproto_db
[] = {
335 { "pup", ETHERTYPE_PUP
},
336 { "xns", ETHERTYPE_NS
},
337 { "ip", ETHERTYPE_IP
},
339 { "ip6", ETHERTYPE_IPV6
},
341 { "arp", ETHERTYPE_ARP
},
342 { "rarp", ETHERTYPE_REVARP
},
343 { "sprite", ETHERTYPE_SPRITE
},
344 { "mopdl", ETHERTYPE_MOPDL
},
345 { "moprc", ETHERTYPE_MOPRC
},
346 { "decnet", ETHERTYPE_DN
},
347 { "lat", ETHERTYPE_LAT
},
348 { "sca", ETHERTYPE_SCA
},
349 { "lanbridge", ETHERTYPE_LANBRIDGE
},
350 { "vexp", ETHERTYPE_VEXP
},
351 { "vprod", ETHERTYPE_VPROD
},
352 { "atalk", ETHERTYPE_ATALK
},
353 { "atalkarp", ETHERTYPE_AARP
},
354 { "loopback", ETHERTYPE_LOOPBACK
},
355 { "decdts", ETHERTYPE_DECDTS
},
356 { "decdns", ETHERTYPE_DECDNS
},
361 pcap_nametoeproto(const char *s
)
363 struct eproto
*p
= eproto_db
;
366 if (strcmp(p
->s
, s
) == 0)
375 /* Static data base of LLC values. */
376 static struct eproto llc_db
[] = {
377 { "iso", LLCSAP_ISONS
},
378 { "stp", LLCSAP_8021D
},
379 { "ipx", LLCSAP_IPX
},
380 { "netbeui", LLCSAP_NETBEUI
},
385 pcap_nametollc(const char *s
)
387 struct eproto
*p
= llc_db
;
390 if (strcmp(p
->s
, s
) == 0)
397 /* Hex digit to integer. */
411 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
420 while (*s
&& *s
!= '.')
421 n
= n
* 10 + *s
++ - '0';
433 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
436 #define AREAMASK 0176000
437 #define NODEMASK 01777
441 if (sscanf(s
, "%d.%d", &area
, &node
) != 2)
444 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
445 *addr
|= (node
& NODEMASK
);
451 * Convert 's', which can have the one of the forms:
453 * "xx:xx:xx:xx:xx:xx"
454 * "xx.xx.xx.xx.xx.xx"
455 * "xx-xx-xx-xx-xx-xx"
459 * (or various mixes of ':', '.', and '-') into a new
460 * ethernet address. Assumes 's' is well formed.
463 pcap_ether_aton(const char *s
)
465 register u_char
*ep
, *e
;
468 e
= ep
= (u_char
*)malloc(6);
473 if (*s
== ':' || *s
== '.' || *s
== '-')
476 if (isxdigit((unsigned char)*s
)) {
486 #ifndef HAVE_ETHER_HOSTTON
489 pcap_ether_hostton(const char *name
)
491 register struct pcap_etherent
*ep
;
493 static FILE *fp
= NULL
;
497 fp
= fopen(PCAP_ETHERS_FILE
, "r");
501 } else if (fp
== NULL
)
506 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
507 if (strcmp(ep
->name
, name
) == 0) {
508 ap
= (u_char
*)malloc(6);
510 memcpy(ap
, ep
->addr
, 6);
520 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
521 #ifndef HAVE_STRUCT_ETHER_ADDR
523 unsigned char ether_addr_octet
[6];
526 extern int ether_hostton(const char *, struct ether_addr
*);
529 /* Use the os supplied routines */
531 pcap_ether_hostton(const char *name
)
537 if (ether_hostton(name
, (struct ether_addr
*)a
) == 0) {
538 ap
= (u_char
*)malloc(6);
540 memcpy((char *)ap
, (char *)a
, 6);
547 __pcap_nametodnaddr(const char *name
, u_short
*res
)
550 struct nodeent
*getnodebyname();
553 nep
= getnodebyname(name
);
554 if (nep
== ((struct nodeent
*)0))
557 memcpy((char *)res
, (char *)nep
->n_addr
, sizeof(unsigned short));