]>
The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
d699c87f263e7a2c72a76f3640599ba0d73f537d
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.63 2002-04-09 07:41:19 guy Exp $ (LBL)";
34 #include <sys/param.h>
35 #include <sys/types.h> /* concession to AIX */
36 #include <sys/socket.h>
39 #include <netinet/in.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 */
48 #include <arpa/inet.h>
60 #include <pcap-namedb.h>
62 #ifdef HAVE_OS_PROTO_H
67 #define NTOHL(x) (x) = ntohl(x)
68 #define NTOHS(x) (x) = ntohs(x)
71 static inline int xdtoi(int);
74 * Convert host name to internet address.
75 * Return 0 upon failure.
78 pcap_nametoaddr(const char *name
)
81 static bpf_u_int32
*hlist
[2];
86 if ((hp
= gethostbyname(name
)) != NULL
) {
88 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
92 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
94 return (bpf_u_int32
**)hp
->h_addr_list
;
103 pcap_nametoaddrinfo(const char *name
)
105 struct addrinfo hints
, *res
;
108 memset(&hints
, 0, sizeof(hints
));
109 hints
.ai_family
= PF_UNSPEC
;
110 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
111 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
120 * Convert net name to internet address.
121 * Return 0 upon failure.
124 pcap_nametonetaddr(const char *name
)
128 if ((np
= getnetbyname(name
)) != NULL
)
135 * Convert a port name to its port and protocol numbers.
136 * We assume only TCP or UDP.
137 * Return 0 upon failure.
140 pcap_nametoport(const char *name
, int *port
, int *proto
)
145 sp
= getservbyname(name
, (char *)0);
149 *proto
= pcap_nametoproto(sp
->s_proto
);
151 * We need to check /etc/services for ambiguous entries.
152 * If we find the ambiguous entry, and it has the
153 * same port number, change the proto to PROTO_UNDEF
154 * so both TCP and UDP will be checked.
156 if (*proto
== IPPROTO_TCP
)
161 sp
= getservbyname(name
, other
);
165 if (*port
!= sp
->s_port
)
166 /* Can't handle ambiguous names that refer
167 to different port numbers. */
168 warning("ambiguous port %s in /etc/services",
171 *proto
= PROTO_UNDEF
;
175 #if defined(ultrix) || defined(__osf__)
176 /* Special hack in case NFS isn't in /etc/services */
177 if (strcmp(name
, "nfs") == 0) {
179 *proto
= PROTO_UNDEF
;
187 pcap_nametoproto(const char *str
)
191 p
= getprotobyname(str
);
198 #include "ethertype.h"
205 /* Static data base of ether protocol types. */
206 struct eproto eproto_db
[] = {
207 { "pup", ETHERTYPE_PUP
},
208 { "xns", ETHERTYPE_NS
},
209 { "ip", ETHERTYPE_IP
},
211 { "ip6", ETHERTYPE_IPV6
},
213 { "arp", ETHERTYPE_ARP
},
214 { "rarp", ETHERTYPE_REVARP
},
215 { "sprite", ETHERTYPE_SPRITE
},
216 { "mopdl", ETHERTYPE_MOPDL
},
217 { "moprc", ETHERTYPE_MOPRC
},
218 { "decnet", ETHERTYPE_DN
},
219 { "lat", ETHERTYPE_LAT
},
220 { "sca", ETHERTYPE_SCA
},
221 { "lanbridge", ETHERTYPE_LANBRIDGE
},
222 { "vexp", ETHERTYPE_VEXP
},
223 { "vprod", ETHERTYPE_VPROD
},
224 { "atalk", ETHERTYPE_ATALK
},
225 { "atalkarp", ETHERTYPE_AARP
},
226 { "loopback", ETHERTYPE_LOOPBACK
},
227 { "decdts", ETHERTYPE_DECDTS
},
228 { "decdns", ETHERTYPE_DECDNS
},
233 pcap_nametoeproto(const char *s
)
235 struct eproto
*p
= eproto_db
;
238 if (strcmp(p
->s
, s
) == 0)
245 /* Hex digit to integer. */
259 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
268 while (*s
&& *s
!= '.')
269 n
= n
* 10 + *s
++ - '0';
281 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
284 #define AREAMASK 0176000
285 #define NODEMASK 01777
289 if (sscanf((char *)s
, "%d.%d", &area
, &node
) != 2)
290 bpf_error("malformed decnet address '%s'", s
);
292 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
293 *addr
|= (node
& NODEMASK
);
299 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
300 * ethernet address. Assumes 's' is well formed.
303 pcap_ether_aton(const char *s
)
305 register u_char
*ep
, *e
;
308 e
= ep
= (u_char
*)malloc(6);
314 if (isxdigit((unsigned char)*s
)) {
324 #ifndef HAVE_ETHER_HOSTTON
327 pcap_ether_hostton(const char *name
)
329 register struct pcap_etherent
*ep
;
331 static FILE *fp
= NULL
;
335 fp
= fopen(PCAP_ETHERS_FILE
, "r");
339 } else if (fp
== NULL
)
344 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
345 if (strcmp(ep
->name
, name
) == 0) {
346 ap
= (u_char
*)malloc(6);
348 memcpy(ap
, ep
->addr
, 6);
359 * XXX - perhaps this should, instead, be declared in "lbl/os-XXX.h" files,
360 * for those OS versions that don't declare it, rather than being declared
361 * here? That way, for example, we could declare it on FreeBSD 2.x (which
362 * doesn't declare it), but not on FreeBSD 3.x (which declares it like
363 * this) or FreeBSD 4.x (which declares it with its first argument as
364 * "const char *", so no matter how we declare it here, it'll fail to
365 * compile on one of 3.x or 4.x).
367 #if !defined(sgi) && !defined(__NetBSD__) && !defined(__FreeBSD__)
368 extern int ether_hostton(char *, struct ether_addr
*);
371 /* Use the os supplied routines */
373 pcap_ether_hostton(const char *name
)
379 if (ether_hostton((char *)name
, (struct ether_addr
*)a
) == 0) {
380 ap
= (u_char
*)malloc(6);
382 memcpy((char *)ap
, (char *)a
, 6);
389 __pcap_nametodnaddr(const char *name
)
392 struct nodeent
*getnodebyname();
396 nep
= getnodebyname(name
);
397 if (nep
== ((struct nodeent
*)0))
398 bpf_error("unknown decnet host name '%s'\n", name
);
400 memcpy((char *)&res
, (char *)nep
->n_addr
, sizeof(unsigned short));
403 bpf_error("decnet name support not included, '%s' cannot be translated\n",