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.
26 static const char rcsid[] _U_ =
27 "@(#) $Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.68.2.3 2003/11/19 18:13:48 guy Exp $ (LBL)";
35 #include <pcap-stdinc.h>
39 #include <sys/param.h>
40 #include <sys/types.h> /* concession to AIX */
41 #include <sys/socket.h>
44 #include <netinet/in.h>
48 * XXX - why was this included even on UNIX?
55 #ifdef HAVE_ETHER_HOSTTON
56 #ifdef HAVE_NETINET_IF_ETHER_H
57 struct mbuf; /* Squelch compiler warnings on some platforms for */
58 struct rtentry; /* declarations in <net/if.h> */
59 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
60 #include <netinet/if_ether.h>
61 #endif /* HAVE_NETINET_IF_ETHER_H */
62 #endif /* HAVE_ETHER_HOSTTON */
63 #include <arpa/inet.h>
76 #include <pcap-namedb.h>
78 #ifdef HAVE_OS_PROTO_H
83 #define NTOHL(x) (x) = ntohl(x)
84 #define NTOHS(x) (x) = ntohs(x)
87 static inline int xdtoi(int);
90 * Convert host name to internet address.
91 * Return 0 upon failure.
94 pcap_nametoaddr(const char *name)
97 static bpf_u_int32 *hlist[2];
102 if ((hp = gethostbyname(name)) != NULL) {
104 hlist[0] = (bpf_u_int32 *)hp->h_addr;
108 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
110 return (bpf_u_int32 **)hp->h_addr_list;
119 pcap_nametoaddrinfo(const char *name)
121 struct addrinfo hints, *res;
124 memset(&hints, 0, sizeof(hints));
125 hints.ai_family = PF_UNSPEC;
126 hints.ai_socktype = SOCK_STREAM; /*not really*/
127 error = getaddrinfo(name, NULL, &hints, &res);
136 * Convert net name to internet address.
137 * Return 0 upon failure.
140 pcap_nametonetaddr(const char *name)
145 if ((np = getnetbyname(name)) != NULL)
151 * There's no "getnetbyname()" on Windows.
158 * Convert a port name to its port and protocol numbers.
159 * We assume only TCP or UDP.
160 * Return 0 upon failure.
163 pcap_nametoport(const char *name, int *port, int *proto)
170 * We need to check /etc/services for ambiguous entries.
171 * If we find the ambiguous entry, and it has the
172 * same port number, change the proto to PROTO_UNDEF
173 * so both TCP and UDP will be checked.
175 sp = getservbyname(name, "tcp");
176 if (sp != NULL) tcp_port = ntohs(sp->s_port);
177 sp = getservbyname(name, "udp");
178 if (sp != NULL) udp_port = ntohs(sp->s_port);
181 *proto = IPPROTO_TCP;
183 if (udp_port == tcp_port)
184 *proto = PROTO_UNDEF;
187 /* Can't handle ambiguous names that refer
188 to different port numbers. */
189 warning("ambiguous port %s in /etc/services",
197 *proto = IPPROTO_UDP;
200 #if defined(ultrix) || defined(__osf__)
201 /* Special hack in case NFS isn't in /etc/services */
202 if (strcmp(name, "nfs") == 0) {
204 *proto = PROTO_UNDEF;
212 pcap_nametoproto(const char *str)
216 p = getprotobyname(str);
223 #include "ethertype.h"
230 /* Static data base of ether protocol types. */
231 struct eproto eproto_db[] = {
232 { "pup", ETHERTYPE_PUP },
233 { "xns", ETHERTYPE_NS },
234 { "ip", ETHERTYPE_IP },
236 { "ip6", ETHERTYPE_IPV6 },
238 { "arp", ETHERTYPE_ARP },
239 { "rarp", ETHERTYPE_REVARP },
240 { "sprite", ETHERTYPE_SPRITE },
241 { "mopdl", ETHERTYPE_MOPDL },
242 { "moprc", ETHERTYPE_MOPRC },
243 { "decnet", ETHERTYPE_DN },
244 { "lat", ETHERTYPE_LAT },
245 { "sca", ETHERTYPE_SCA },
246 { "lanbridge", ETHERTYPE_LANBRIDGE },
247 { "vexp", ETHERTYPE_VEXP },
248 { "vprod", ETHERTYPE_VPROD },
249 { "atalk", ETHERTYPE_ATALK },
250 { "atalkarp", ETHERTYPE_AARP },
251 { "loopback", ETHERTYPE_LOOPBACK },
252 { "decdts", ETHERTYPE_DECDTS },
253 { "decdns", ETHERTYPE_DECDNS },
258 pcap_nametoeproto(const char *s)
260 struct eproto *p = eproto_db;
263 if (strcmp(p->s, s) == 0)
270 /* Hex digit to integer. */
284 __pcap_atoin(const char *s, bpf_u_int32 *addr)
293 while (*s && *s != '.')
294 n = n * 10 + *s++ - '0';
306 __pcap_atodn(const char *s, bpf_u_int32 *addr)
309 #define AREAMASK 0176000
310 #define NODEMASK 01777
314 if (sscanf((char *)s, "%d.%d", &area, &node) != 2)
315 bpf_error("malformed decnet address '%s'", s);
317 *addr = (area << AREASHIFT) & AREAMASK;
318 *addr |= (node & NODEMASK);
324 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
325 * ethernet address. Assumes 's' is well formed.
328 pcap_ether_aton(const char *s)
330 register u_char *ep, *e;
333 e = ep = (u_char *)malloc(6);
339 if (isxdigit((unsigned char)*s)) {
349 #ifndef HAVE_ETHER_HOSTTON
352 pcap_ether_hostton(const char *name)
354 register struct pcap_etherent *ep;
356 static FILE *fp = NULL;
360 fp = fopen(PCAP_ETHERS_FILE, "r");
364 } else if (fp == NULL)
369 while ((ep = pcap_next_etherent(fp)) != NULL) {
370 if (strcmp(ep->name, name) == 0) {
371 ap = (u_char *)malloc(6);
373 memcpy(ap, ep->addr, 6);
384 * XXX - perhaps this should, instead, be declared in "lbl/os-XXX.h" files,
385 * for those OS versions that don't declare it, rather than being declared
386 * here? That way, for example, we could declare it on FreeBSD 2.x (which
387 * doesn't declare it), but not on FreeBSD 3.x (which declares it like
388 * this) or FreeBSD 4.x (which declares it with its first argument as
389 * "const char *", so no matter how we declare it here, it'll fail to
390 * compile on one of 3.x or 4.x).
392 #if !defined(sgi) && !defined(__NetBSD__) && !defined(__FreeBSD__) && \
394 extern int ether_hostton(char *, struct ether_addr *);
397 /* Use the os supplied routines */
399 pcap_ether_hostton(const char *name)
405 if (ether_hostton((char *)name, (struct ether_addr *)a) == 0) {
406 ap = (u_char *)malloc(6);
408 memcpy((char *)ap, (char *)a, 6);
415 __pcap_nametodnaddr(const char *name)
418 struct nodeent *getnodebyname();
422 nep = getnodebyname(name);
423 if (nep == ((struct nodeent *)0))
424 bpf_error("unknown decnet host name '%s'\n", name);
426 memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
429 bpf_error("decnet name support not included, '%s' cannot be translated\n",