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.
28 static const char rcsid[] _U_ =
29 "@(#) $Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.77 2005/03/27 22:26:25 guy Exp $ (LBL)";
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 * XXX - why was this included even on UNIX?
57 #ifdef HAVE_ETHER_HOSTTON
59 * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
62 #ifdef HAVE_NETINET_IF_ETHER_H
63 struct mbuf; /* Squelch compiler warnings on some platforms for */
64 struct rtentry; /* declarations in <net/if.h> */
65 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
66 #include <netinet/if_ether.h>
67 #endif /* HAVE_NETINET_IF_ETHER_H */
68 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
69 #include <netinet/ether.h>
70 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
71 #endif /* HAVE_ETHER_HOSTTON */
72 #include <arpa/inet.h>
85 #include <pcap-namedb.h>
87 #ifdef HAVE_OS_PROTO_H
92 #define NTOHL(x) (x) = ntohl(x)
93 #define NTOHS(x) (x) = ntohs(x)
96 static inline int xdtoi(int);
99 * Convert host name to internet address.
100 * Return 0 upon failure.
103 pcap_nametoaddr(const char *name)
106 static bpf_u_int32 *hlist[2];
111 if ((hp = gethostbyname(name)) != NULL) {
113 hlist[0] = (bpf_u_int32 *)hp->h_addr;
117 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
119 return (bpf_u_int32 **)hp->h_addr_list;
128 pcap_nametoaddrinfo(const char *name)
130 struct addrinfo hints, *res;
133 memset(&hints, 0, sizeof(hints));
134 hints.ai_family = PF_UNSPEC;
135 hints.ai_socktype = SOCK_STREAM; /*not really*/
136 hints.ai_protocol = IPPROTO_TCP; /*not really*/
137 error = getaddrinfo(name, NULL, &hints, &res);
146 * Convert net name to internet address.
147 * Return 0 upon failure.
150 pcap_nametonetaddr(const char *name)
155 if ((np = getnetbyname(name)) != NULL)
161 * There's no "getnetbyname()" on Windows.
168 * Convert a port name to its port and protocol numbers.
169 * We assume only TCP or UDP.
170 * Return 0 upon failure.
173 pcap_nametoport(const char *name, int *port, int *proto)
180 * We need to check /etc/services for ambiguous entries.
181 * If we find the ambiguous entry, and it has the
182 * same port number, change the proto to PROTO_UNDEF
183 * so both TCP and UDP will be checked.
185 sp = getservbyname(name, "tcp");
186 if (sp != NULL) tcp_port = ntohs(sp->s_port);
187 sp = getservbyname(name, "udp");
188 if (sp != NULL) udp_port = ntohs(sp->s_port);
191 *proto = IPPROTO_TCP;
193 if (udp_port == tcp_port)
194 *proto = PROTO_UNDEF;
197 /* Can't handle ambiguous names that refer
198 to different port numbers. */
199 warning("ambiguous port %s in /etc/services",
207 *proto = IPPROTO_UDP;
210 #if defined(ultrix) || defined(__osf__)
211 /* Special hack in case NFS isn't in /etc/services */
212 if (strcmp(name, "nfs") == 0) {
214 *proto = PROTO_UNDEF;
222 pcap_nametoproto(const char *str)
226 p = getprotobyname(str);
233 #include "ethertype.h"
240 /* Static data base of ether protocol types. */
241 struct eproto eproto_db[] = {
243 /* The FreeBSD elf linker generates a request to copy this array
244 * (including its size) when you link with -lpcap. In order to
245 * not bump the major version number of this libpcap.so, we need
246 * to ensure that the array stays the same size. Since PUP is
247 * likely never seen in real life any more, it's the first to
248 * be sacrificed (in favor of ip6).
250 { "pup", ETHERTYPE_PUP },
252 { "xns", ETHERTYPE_NS },
253 { "ip", ETHERTYPE_IP },
255 { "ip6", ETHERTYPE_IPV6 },
257 { "arp", ETHERTYPE_ARP },
258 { "rarp", ETHERTYPE_REVARP },
259 { "sprite", ETHERTYPE_SPRITE },
260 { "mopdl", ETHERTYPE_MOPDL },
261 { "moprc", ETHERTYPE_MOPRC },
262 { "decnet", ETHERTYPE_DN },
263 { "lat", ETHERTYPE_LAT },
264 { "sca", ETHERTYPE_SCA },
265 { "lanbridge", ETHERTYPE_LANBRIDGE },
266 { "vexp", ETHERTYPE_VEXP },
267 { "vprod", ETHERTYPE_VPROD },
268 { "atalk", ETHERTYPE_ATALK },
269 { "atalkarp", ETHERTYPE_AARP },
270 { "loopback", ETHERTYPE_LOOPBACK },
271 { "decdts", ETHERTYPE_DECDTS },
272 { "decdns", ETHERTYPE_DECDNS },
277 pcap_nametoeproto(const char *s)
279 struct eproto *p = eproto_db;
282 if (strcmp(p->s, s) == 0)
291 /* Static data base of LLC values. */
292 static struct eproto llc_db[] = {
293 { "iso", LLCSAP_ISONS },
294 { "stp", LLCSAP_8021D },
295 { "ipx", LLCSAP_IPX },
296 { "netbeui", LLCSAP_NETBEUI },
301 pcap_nametollc(const char *s)
303 struct eproto *p = llc_db;
306 if (strcmp(p->s, s) == 0)
313 /* Hex digit to integer. */
327 __pcap_atoin(const char *s, bpf_u_int32 *addr)
336 while (*s && *s != '.')
337 n = n * 10 + *s++ - '0';
349 __pcap_atodn(const char *s, bpf_u_int32 *addr)
352 #define AREAMASK 0176000
353 #define NODEMASK 01777
357 if (sscanf((char *)s, "%d.%d", &area, &node) != 2)
358 bpf_error("malformed decnet address '%s'", s);
360 *addr = (area << AREASHIFT) & AREAMASK;
361 *addr |= (node & NODEMASK);
367 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
368 * ethernet address. Assumes 's' is well formed.
371 pcap_ether_aton(const char *s)
373 register u_char *ep, *e;
376 e = ep = (u_char *)malloc(6);
382 if (isxdigit((unsigned char)*s)) {
392 #ifndef HAVE_ETHER_HOSTTON
395 pcap_ether_hostton(const char *name)
397 register struct pcap_etherent *ep;
399 static FILE *fp = NULL;
403 fp = fopen(PCAP_ETHERS_FILE, "r");
407 } else if (fp == NULL)
412 while ((ep = pcap_next_etherent(fp)) != NULL) {
413 if (strcmp(ep->name, name) == 0) {
414 ap = (u_char *)malloc(6);
416 memcpy(ap, ep->addr, 6);
426 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
427 extern int ether_hostton(const char *, struct ether_addr *);
430 /* Use the os supplied routines */
432 pcap_ether_hostton(const char *name)
438 if (ether_hostton((char *)name, (struct ether_addr *)a) == 0) {
439 ap = (u_char *)malloc(6);
441 memcpy((char *)ap, (char *)a, 6);
448 __pcap_nametodnaddr(const char *name)
451 struct nodeent *getnodebyname();
455 nep = getnodebyname(name);
456 if (nep == ((struct nodeent *)0))
457 bpf_error("unknown decnet host name '%s'\n", name);
459 memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
462 bpf_error("decnet name support not included, '%s' cannot be translated\n",