]>
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) || !HAVE_DECL_ETHER_HOSTTON */
124 #endif /* HAVE_ETHER_HOSTTON */
126 #include <arpa/inet.h>
136 #include "pcap-int.h"
139 #include <pcap/namedb.h>
140 #include "nametoaddr.h"
142 #ifdef HAVE_OS_PROTO_H
143 #include "os-proto.h"
147 #define NTOHL(x) (x) = ntohl(x)
148 #define NTOHS(x) (x) = ntohs(x)
151 static inline int xdtoi(int);
154 * Convert host name to internet address.
155 * Return 0 upon failure.
158 pcap_nametoaddr(const char *name
)
161 static bpf_u_int32
*hlist
[2];
166 if ((hp
= gethostbyname(name
)) != NULL
) {
168 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
172 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
174 return (bpf_u_int32
**)hp
->h_addr_list
;
183 pcap_nametoaddrinfo(const char *name
)
185 struct addrinfo hints
, *res
;
188 memset(&hints
, 0, sizeof(hints
));
189 hints
.ai_family
= PF_UNSPEC
;
190 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
191 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
192 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
201 * Convert net name to internet address.
202 * Return 0 upon failure.
205 pcap_nametonetaddr(const char *name
)
210 if ((np
= getnetbyname(name
)) != NULL
)
216 * There's no "getnetbyname()" on Windows.
218 * XXX - I guess we could use the BSD code to read
219 * C:\Windows\System32\drivers\etc/networks, assuming
220 * that's its home on all the versions of Windows
221 * we use, but that file probably just has the loopback
222 * network on 127/24 on 99 44/100% of Windows machines.
224 * (Heck, these days it probably just has that on 99 44/100%
225 * of *UN*X* machines.)
232 * Convert a port name to its port and protocol numbers.
233 * We assume only TCP or UDP.
234 * Return 0 upon failure.
237 pcap_nametoport(const char *name
, int *port
, int *proto
)
244 * We need to check /etc/services for ambiguous entries.
245 * If we find the ambiguous entry, and it has the
246 * same port number, change the proto to PROTO_UNDEF
247 * so both TCP and UDP will be checked.
249 sp
= getservbyname(name
, "tcp");
250 if (sp
!= NULL
) tcp_port
= ntohs(sp
->s_port
);
251 sp
= getservbyname(name
, "udp");
252 if (sp
!= NULL
) udp_port
= ntohs(sp
->s_port
);
255 *proto
= IPPROTO_TCP
;
257 if (udp_port
== tcp_port
)
258 *proto
= PROTO_UNDEF
;
261 /* Can't handle ambiguous names that refer
262 to different port numbers. */
263 warning("ambiguous port %s in /etc/services",
271 *proto
= IPPROTO_UDP
;
274 #if defined(ultrix) || defined(__osf__)
275 /* Special hack in case NFS isn't in /etc/services */
276 if (strcmp(name
, "nfs") == 0) {
278 *proto
= PROTO_UNDEF
;
286 * Convert a string in the form PPP-PPP, where correspond to ports, to
287 * a starting and ending port in a port range.
288 * Return 0 on failure.
291 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
297 if (sscanf(name
, "%d-%d", &p1
, &p2
) != 2) {
298 if ((cpy
= strdup(name
)) == NULL
)
301 if ((off
= strchr(cpy
, '-')) == NULL
) {
308 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
314 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
320 if (*proto
!= save_proto
)
321 *proto
= PROTO_UNDEF
;
325 *proto
= PROTO_UNDEF
;
332 pcap_nametoproto(const char *str
)
336 p
= getprotobyname(str
);
343 #include "ethertype.h"
351 * Static data base of ether protocol types.
352 * tcpdump used to import this, and it's declared as an export on
353 * Debian, at least, so make it a public symbol, even though we
354 * don't officially export it by declaring it in a header file.
355 * (Programs *should* do this themselves, as tcpdump now does.)
357 PCAP_API_DEF
struct eproto eproto_db
[] = {
358 { "pup", ETHERTYPE_PUP
},
359 { "xns", ETHERTYPE_NS
},
360 { "ip", ETHERTYPE_IP
},
362 { "ip6", ETHERTYPE_IPV6
},
364 { "arp", ETHERTYPE_ARP
},
365 { "rarp", ETHERTYPE_REVARP
},
366 { "sprite", ETHERTYPE_SPRITE
},
367 { "mopdl", ETHERTYPE_MOPDL
},
368 { "moprc", ETHERTYPE_MOPRC
},
369 { "decnet", ETHERTYPE_DN
},
370 { "lat", ETHERTYPE_LAT
},
371 { "sca", ETHERTYPE_SCA
},
372 { "lanbridge", ETHERTYPE_LANBRIDGE
},
373 { "vexp", ETHERTYPE_VEXP
},
374 { "vprod", ETHERTYPE_VPROD
},
375 { "atalk", ETHERTYPE_ATALK
},
376 { "atalkarp", ETHERTYPE_AARP
},
377 { "loopback", ETHERTYPE_LOOPBACK
},
378 { "decdts", ETHERTYPE_DECDTS
},
379 { "decdns", ETHERTYPE_DECDNS
},
384 pcap_nametoeproto(const char *s
)
386 struct eproto
*p
= eproto_db
;
389 if (strcmp(p
->s
, s
) == 0)
398 /* Static data base of LLC values. */
399 static struct eproto llc_db
[] = {
400 { "iso", LLCSAP_ISONS
},
401 { "stp", LLCSAP_8021D
},
402 { "ipx", LLCSAP_IPX
},
403 { "netbeui", LLCSAP_NETBEUI
},
408 pcap_nametollc(const char *s
)
410 struct eproto
*p
= llc_db
;
413 if (strcmp(p
->s
, s
) == 0)
420 /* Hex digit to integer. */
434 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
443 while (*s
&& *s
!= '.')
444 n
= n
* 10 + *s
++ - '0';
456 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
459 #define AREAMASK 0176000
460 #define NODEMASK 01777
464 if (sscanf(s
, "%d.%d", &area
, &node
) != 2)
467 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
468 *addr
|= (node
& NODEMASK
);
474 * Convert 's', which can have the one of the forms:
476 * "xx:xx:xx:xx:xx:xx"
477 * "xx.xx.xx.xx.xx.xx"
478 * "xx-xx-xx-xx-xx-xx"
482 * (or various mixes of ':', '.', and '-') into a new
483 * ethernet address. Assumes 's' is well formed.
486 pcap_ether_aton(const char *s
)
488 register u_char
*ep
, *e
;
491 e
= ep
= (u_char
*)malloc(6);
496 if (*s
== ':' || *s
== '.' || *s
== '-')
499 if (isxdigit((unsigned char)*s
)) {
509 #ifndef HAVE_ETHER_HOSTTON
512 pcap_ether_hostton(const char *name
)
514 register struct pcap_etherent
*ep
;
516 static FILE *fp
= NULL
;
520 fp
= fopen(PCAP_ETHERS_FILE
, "r");
524 } else if (fp
== NULL
)
529 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
530 if (strcmp(ep
->name
, name
) == 0) {
531 ap
= (u_char
*)malloc(6);
533 memcpy(ap
, ep
->addr
, 6);
542 /* Use the os supplied routines */
544 pcap_ether_hostton(const char *name
)
550 if (ether_hostton(name
, (struct ether_addr
*)a
) == 0) {
551 ap
= (u_char
*)malloc(6);
553 memcpy((char *)ap
, (char *)a
, 6);
560 __pcap_nametodnaddr(const char *name
, u_short
*res
)
563 struct nodeent
*getnodebyname();
566 nep
= getnodebyname(name
);
567 if (nep
== ((struct nodeent
*)0))
570 memcpy((char *)res
, (char *)nep
->n_addr
, sizeof(unsigned short));