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.83 2008-02-06 10:21:30 guy Exp $ (LBL)";
35 #include <sys/types.h>
36 #include <netdnet/dnetdb.h>
40 #include <pcap-stdinc.h>
44 #include <sys/param.h>
45 #include <sys/types.h> /* concession to AIX */
46 #include <sys/socket.h>
49 #include <netinet/in.h>
53 #ifdef HAVE_ETHER_HOSTTON
55 * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
58 #ifdef HAVE_NETINET_IF_ETHER_H
59 struct mbuf; /* Squelch compiler warnings on some platforms for */
60 struct rtentry; /* declarations in <net/if.h> */
61 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
62 #include <netinet/if_ether.h>
63 #endif /* HAVE_NETINET_IF_ETHER_H */
64 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
65 #include <netinet/ether.h>
66 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
67 #endif /* HAVE_ETHER_HOSTTON */
68 #include <arpa/inet.h>
81 #include <pcap/namedb.h>
83 #ifdef HAVE_OS_PROTO_H
88 #define NTOHL(x) (x) = ntohl(x)
89 #define NTOHS(x) (x) = ntohs(x)
92 static inline int xdtoi(int);
95 * Convert host name to internet address.
96 * Return 0 upon failure.
99 pcap_nametoaddr(const char *name)
102 static bpf_u_int32 *hlist[2];
107 if ((hp = gethostbyname(name)) != NULL) {
109 hlist[0] = (bpf_u_int32 *)hp->h_addr;
113 for (p = (bpf_u_int32 **)hp->h_addr_list; *p; ++p)
115 return (bpf_u_int32 **)hp->h_addr_list;
124 pcap_nametoaddrinfo(const char *name)
126 struct addrinfo hints, *res;
129 memset(&hints, 0, sizeof(hints));
130 hints.ai_family = PF_UNSPEC;
131 hints.ai_socktype = SOCK_STREAM; /*not really*/
132 hints.ai_protocol = IPPROTO_TCP; /*not really*/
133 error = getaddrinfo(name, NULL, &hints, &res);
142 * Convert net name to internet address.
143 * Return 0 upon failure.
146 pcap_nametonetaddr(const char *name)
151 if ((np = getnetbyname(name)) != NULL)
157 * There's no "getnetbyname()" on Windows.
164 * Convert a port name to its port and protocol numbers.
165 * We assume only TCP or UDP.
166 * Return 0 upon failure.
169 pcap_nametoport(const char *name, int *port, int *proto)
176 * We need to check /etc/services for ambiguous entries.
177 * If we find the ambiguous entry, and it has the
178 * same port number, change the proto to PROTO_UNDEF
179 * so both TCP and UDP will be checked.
181 sp = getservbyname(name, "tcp");
182 if (sp != NULL) tcp_port = ntohs(sp->s_port);
183 sp = getservbyname(name, "udp");
184 if (sp != NULL) udp_port = ntohs(sp->s_port);
187 *proto = IPPROTO_TCP;
189 if (udp_port == tcp_port)
190 *proto = PROTO_UNDEF;
193 /* Can't handle ambiguous names that refer
194 to different port numbers. */
195 warning("ambiguous port %s in /etc/services",
203 *proto = IPPROTO_UDP;
206 #if defined(ultrix) || defined(__osf__)
207 /* Special hack in case NFS isn't in /etc/services */
208 if (strcmp(name, "nfs") == 0) {
210 *proto = PROTO_UNDEF;
218 * Convert a string in the form PPP-PPP, where correspond to ports, to
219 * a starting and ending port in a port range.
220 * Return 0 on failure.
223 pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto)
229 if (sscanf(name, "%d-%d", &p1, &p2) != 2) {
230 if ((cpy = strdup(name)) == NULL)
233 if ((off = strchr(cpy, '-')) == NULL) {
240 if (pcap_nametoport(cpy, port1, proto) == 0) {
246 if (pcap_nametoport(off + 1, port2, proto) == 0) {
252 if (*proto != save_proto)
253 *proto = PROTO_UNDEF;
257 *proto = PROTO_UNDEF;
264 pcap_nametoproto(const char *str)
268 p = getprotobyname(str);
275 #include "ethertype.h"
282 /* Static data base of ether protocol types. */
283 struct eproto eproto_db[] = {
284 { "pup", ETHERTYPE_PUP },
285 { "xns", ETHERTYPE_NS },
286 { "ip", ETHERTYPE_IP },
288 { "ip6", ETHERTYPE_IPV6 },
290 { "arp", ETHERTYPE_ARP },
291 { "rarp", ETHERTYPE_REVARP },
292 { "sprite", ETHERTYPE_SPRITE },
293 { "mopdl", ETHERTYPE_MOPDL },
294 { "moprc", ETHERTYPE_MOPRC },
295 { "decnet", ETHERTYPE_DN },
296 { "lat", ETHERTYPE_LAT },
297 { "sca", ETHERTYPE_SCA },
298 { "lanbridge", ETHERTYPE_LANBRIDGE },
299 { "vexp", ETHERTYPE_VEXP },
300 { "vprod", ETHERTYPE_VPROD },
301 { "atalk", ETHERTYPE_ATALK },
302 { "atalkarp", ETHERTYPE_AARP },
303 { "loopback", ETHERTYPE_LOOPBACK },
304 { "decdts", ETHERTYPE_DECDTS },
305 { "decdns", ETHERTYPE_DECDNS },
310 pcap_nametoeproto(const char *s)
312 struct eproto *p = eproto_db;
315 if (strcmp(p->s, s) == 0)
324 /* Static data base of LLC values. */
325 static struct eproto llc_db[] = {
326 { "iso", LLCSAP_ISONS },
327 { "stp", LLCSAP_8021D },
328 { "ipx", LLCSAP_IPX },
329 { "netbeui", LLCSAP_NETBEUI },
334 pcap_nametollc(const char *s)
336 struct eproto *p = llc_db;
339 if (strcmp(p->s, s) == 0)
346 /* Hex digit to integer. */
360 __pcap_atoin(const char *s, bpf_u_int32 *addr)
369 while (*s && *s != '.')
370 n = n * 10 + *s++ - '0';
382 __pcap_atodn(const char *s, bpf_u_int32 *addr)
385 #define AREAMASK 0176000
386 #define NODEMASK 01777
390 if (sscanf(s, "%d.%d", &area, &node) != 2)
391 bpf_error("malformed decnet address '%s'", s);
393 *addr = (area << AREASHIFT) & AREAMASK;
394 *addr |= (node & NODEMASK);
400 * Convert 's', which can have the one of the forms:
402 * "xx:xx:xx:xx:xx:xx"
403 * "xx.xx.xx.xx.xx.xx"
404 * "xx-xx-xx-xx-xx-xx"
408 * (or various mixes of ':', '.', and '-') into a new
409 * ethernet address. Assumes 's' is well formed.
412 pcap_ether_aton(const char *s)
414 register u_char *ep, *e;
417 e = ep = (u_char *)malloc(6);
420 if (*s == ':' || *s == '.' || *s == '-')
423 if (isxdigit((unsigned char)*s)) {
433 #ifndef HAVE_ETHER_HOSTTON
436 pcap_ether_hostton(const char *name)
438 register struct pcap_etherent *ep;
440 static FILE *fp = NULL;
444 fp = fopen(PCAP_ETHERS_FILE, "r");
448 } else if (fp == NULL)
453 while ((ep = pcap_next_etherent(fp)) != NULL) {
454 if (strcmp(ep->name, name) == 0) {
455 ap = (u_char *)malloc(6);
457 memcpy(ap, ep->addr, 6);
467 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
468 #ifndef HAVE_STRUCT_ETHER_ADDR
470 unsigned char ether_addr_octet[6];
473 extern int ether_hostton(const char *, struct ether_addr *);
476 /* Use the os supplied routines */
478 pcap_ether_hostton(const char *name)
484 if (ether_hostton(name, (struct ether_addr *)a) == 0) {
485 ap = (u_char *)malloc(6);
487 memcpy((char *)ap, (char *)a, 6);
494 __pcap_nametodnaddr(const char *name)
497 struct nodeent *getnodebyname();
501 nep = getnodebyname(name);
502 if (nep == ((struct nodeent *)0))
503 bpf_error("unknown decnet host name '%s'\n", name);
505 memcpy((char *)&res, (char *)nep->n_addr, sizeof(unsigned short));
508 bpf_error("decnet name support not included, '%s' cannot be translated\n",