]>
The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
b220f8b79a864bea007f62e8fdb0ca3b31626a1b
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
[] =
27 "@(#) $Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.65 2002-08-01 08:33:03 risso Exp $ (LBL)";
34 #include <pcap-stdinc.h>
40 #ifdef HAVE_ETHER_HOSTTON
41 #ifdef HAVE_NETINET_IF_ETHER_H
42 struct mbuf
; /* Squelch compiler warnings on some platforms for */
43 struct rtentry
; /* declarations in <net/if.h> */
44 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
45 #include <netinet/if_ether.h>
46 #endif /* HAVE_NETINET_IF_ETHER_H */
47 #endif /* HAVE_ETHER_HOSTTON */
58 #include <pcap-namedb.h>
60 #ifdef HAVE_OS_PROTO_H
65 #define NTOHL(x) (x) = ntohl(x)
66 #define NTOHS(x) (x) = ntohs(x)
69 static inline int xdtoi(int);
72 * Convert host name to internet address.
73 * Return 0 upon failure.
76 pcap_nametoaddr(const char *name
)
79 static bpf_u_int32
*hlist
[2];
84 if ((hp
= gethostbyname(name
)) != NULL
) {
86 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
90 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
92 return (bpf_u_int32
**)hp
->h_addr_list
;
101 pcap_nametoaddrinfo(const char *name
)
103 struct addrinfo hints
, *res
;
106 memset(&hints
, 0, sizeof(hints
));
107 hints
.ai_family
= PF_UNSPEC
;
108 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
109 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
118 * Convert net name to internet address.
119 * Return 0 upon failure.
122 pcap_nametonetaddr(const char *name
)
127 if ((np
= getnetbyname(name
)) != NULL
)
133 * There's no "getnetbyname()" on Windows.
140 * Convert a port name to its port and protocol numbers.
141 * We assume only TCP or UDP.
142 * Return 0 upon failure.
145 pcap_nametoport(const char *name
, int *port
, int *proto
)
150 sp
= getservbyname(name
, (char *)0);
154 *proto
= pcap_nametoproto(sp
->s_proto
);
156 * We need to check /etc/services for ambiguous entries.
157 * If we find the ambiguous entry, and it has the
158 * same port number, change the proto to PROTO_UNDEF
159 * so both TCP and UDP will be checked.
161 if (*proto
== IPPROTO_TCP
)
166 sp
= getservbyname(name
, other
);
170 if (*port
!= sp
->s_port
)
171 /* Can't handle ambiguous names that refer
172 to different port numbers. */
173 warning("ambiguous port %s in /etc/services",
176 *proto
= PROTO_UNDEF
;
180 #if defined(ultrix) || defined(__osf__)
181 /* Special hack in case NFS isn't in /etc/services */
182 if (strcmp(name
, "nfs") == 0) {
184 *proto
= PROTO_UNDEF
;
192 pcap_nametoproto(const char *str
)
196 p
= getprotobyname(str
);
203 #include "ethertype.h"
210 /* Static data base of ether protocol types. */
211 struct eproto eproto_db
[] = {
212 { "pup", ETHERTYPE_PUP
},
213 { "xns", ETHERTYPE_NS
},
214 { "ip", ETHERTYPE_IP
},
216 { "ip6", ETHERTYPE_IPV6
},
218 { "arp", ETHERTYPE_ARP
},
219 { "rarp", ETHERTYPE_REVARP
},
220 { "sprite", ETHERTYPE_SPRITE
},
221 { "mopdl", ETHERTYPE_MOPDL
},
222 { "moprc", ETHERTYPE_MOPRC
},
223 { "decnet", ETHERTYPE_DN
},
224 { "lat", ETHERTYPE_LAT
},
225 { "sca", ETHERTYPE_SCA
},
226 { "lanbridge", ETHERTYPE_LANBRIDGE
},
227 { "vexp", ETHERTYPE_VEXP
},
228 { "vprod", ETHERTYPE_VPROD
},
229 { "atalk", ETHERTYPE_ATALK
},
230 { "atalkarp", ETHERTYPE_AARP
},
231 { "loopback", ETHERTYPE_LOOPBACK
},
232 { "decdts", ETHERTYPE_DECDTS
},
233 { "decdns", ETHERTYPE_DECDNS
},
238 pcap_nametoeproto(const char *s
)
240 struct eproto
*p
= eproto_db
;
243 if (strcmp(p
->s
, s
) == 0)
250 /* Hex digit to integer. */
264 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
273 while (*s
&& *s
!= '.')
274 n
= n
* 10 + *s
++ - '0';
286 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
289 #define AREAMASK 0176000
290 #define NODEMASK 01777
294 if (sscanf((char *)s
, "%d.%d", &area
, &node
) != 2)
295 bpf_error("malformed decnet address '%s'", s
);
297 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
298 *addr
|= (node
& NODEMASK
);
304 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
305 * ethernet address. Assumes 's' is well formed.
308 pcap_ether_aton(const char *s
)
310 register u_char
*ep
, *e
;
313 e
= ep
= (u_char
*)malloc(6);
319 if (isxdigit((unsigned char)*s
)) {
329 #ifndef HAVE_ETHER_HOSTTON
332 pcap_ether_hostton(const char *name
)
334 register struct pcap_etherent
*ep
;
336 static FILE *fp
= NULL
;
340 fp
= fopen(PCAP_ETHERS_FILE
, "r");
344 } else if (fp
== NULL
)
349 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
350 if (strcmp(ep
->name
, name
) == 0) {
351 ap
= (u_char
*)malloc(6);
353 memcpy(ap
, ep
->addr
, 6);
364 * XXX - perhaps this should, instead, be declared in "lbl/os-XXX.h" files,
365 * for those OS versions that don't declare it, rather than being declared
366 * here? That way, for example, we could declare it on FreeBSD 2.x (which
367 * doesn't declare it), but not on FreeBSD 3.x (which declares it like
368 * this) or FreeBSD 4.x (which declares it with its first argument as
369 * "const char *", so no matter how we declare it here, it'll fail to
370 * compile on one of 3.x or 4.x).
372 #if !defined(sgi) && !defined(__NetBSD__) && !defined(__FreeBSD__)
373 extern int ether_hostton(char *, struct ether_addr
*);
376 /* Use the os supplied routines */
378 pcap_ether_hostton(const char *name
)
384 if (ether_hostton((char *)name
, (struct ether_addr
*)a
) == 0) {
385 ap
= (u_char
*)malloc(6);
387 memcpy((char *)ap
, (char *)a
, 6);
394 __pcap_nametodnaddr(const char *name
)
397 struct nodeent
*getnodebyname();
401 nep
= getnodebyname(name
);
402 if (nep
== ((struct nodeent
*)0))
403 bpf_error("unknown decnet host name '%s'\n", name
);
405 memcpy((char *)&res
, (char *)nep
->n_addr
, sizeof(unsigned short));
408 bpf_error("decnet name support not included, '%s' cannot be translated\n",