]>
The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
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.73 2003-12-24 09:14:21 itojun 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 hints
.ai_protocol
= IPPROTO_TCP
; /*not really*/
128 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
137 * Convert net name to internet address.
138 * Return 0 upon failure.
141 pcap_nametonetaddr(const char *name
)
146 if ((np
= getnetbyname(name
)) != NULL
)
152 * There's no "getnetbyname()" on Windows.
159 * Convert a port name to its port and protocol numbers.
160 * We assume only TCP or UDP.
161 * Return 0 upon failure.
164 pcap_nametoport(const char *name
, int *port
, int *proto
)
171 * We need to check /etc/services for ambiguous entries.
172 * If we find the ambiguous entry, and it has the
173 * same port number, change the proto to PROTO_UNDEF
174 * so both TCP and UDP will be checked.
176 sp
= getservbyname(name
, "tcp");
177 if (sp
!= NULL
) tcp_port
= ntohs(sp
->s_port
);
178 sp
= getservbyname(name
, "udp");
179 if (sp
!= NULL
) udp_port
= ntohs(sp
->s_port
);
182 *proto
= IPPROTO_TCP
;
184 if (udp_port
== tcp_port
)
185 *proto
= PROTO_UNDEF
;
188 /* Can't handle ambiguous names that refer
189 to different port numbers. */
190 warning("ambiguous port %s in /etc/services",
198 *proto
= IPPROTO_UDP
;
201 #if defined(ultrix) || defined(__osf__)
202 /* Special hack in case NFS isn't in /etc/services */
203 if (strcmp(name
, "nfs") == 0) {
205 *proto
= PROTO_UNDEF
;
213 pcap_nametoproto(const char *str
)
217 p
= getprotobyname(str
);
224 #include "ethertype.h"
231 /* Static data base of ether protocol types. */
232 struct eproto eproto_db
[] = {
233 { "pup", ETHERTYPE_PUP
},
234 { "xns", ETHERTYPE_NS
},
235 { "ip", ETHERTYPE_IP
},
237 { "ip6", ETHERTYPE_IPV6
},
239 { "arp", ETHERTYPE_ARP
},
240 { "rarp", ETHERTYPE_REVARP
},
241 { "sprite", ETHERTYPE_SPRITE
},
242 { "mopdl", ETHERTYPE_MOPDL
},
243 { "moprc", ETHERTYPE_MOPRC
},
244 { "decnet", ETHERTYPE_DN
},
245 { "lat", ETHERTYPE_LAT
},
246 { "sca", ETHERTYPE_SCA
},
247 { "lanbridge", ETHERTYPE_LANBRIDGE
},
248 { "vexp", ETHERTYPE_VEXP
},
249 { "vprod", ETHERTYPE_VPROD
},
250 { "atalk", ETHERTYPE_ATALK
},
251 { "atalkarp", ETHERTYPE_AARP
},
252 { "loopback", ETHERTYPE_LOOPBACK
},
253 { "decdts", ETHERTYPE_DECDTS
},
254 { "decdns", ETHERTYPE_DECDNS
},
259 pcap_nametoeproto(const char *s
)
261 struct eproto
*p
= eproto_db
;
264 if (strcmp(p
->s
, s
) == 0)
271 /* Hex digit to integer. */
285 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
294 while (*s
&& *s
!= '.')
295 n
= n
* 10 + *s
++ - '0';
307 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
310 #define AREAMASK 0176000
311 #define NODEMASK 01777
315 if (sscanf((char *)s
, "%d.%d", &area
, &node
) != 2)
316 bpf_error("malformed decnet address '%s'", s
);
318 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
319 *addr
|= (node
& NODEMASK
);
325 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
326 * ethernet address. Assumes 's' is well formed.
329 pcap_ether_aton(const char *s
)
331 register u_char
*ep
, *e
;
334 e
= ep
= (u_char
*)malloc(6);
340 if (isxdigit((unsigned char)*s
)) {
350 #ifndef HAVE_ETHER_HOSTTON
353 pcap_ether_hostton(const char *name
)
355 register struct pcap_etherent
*ep
;
357 static FILE *fp
= NULL
;
361 fp
= fopen(PCAP_ETHERS_FILE
, "r");
365 } else if (fp
== NULL
)
370 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
371 if (strcmp(ep
->name
, name
) == 0) {
372 ap
= (u_char
*)malloc(6);
374 memcpy(ap
, ep
->addr
, 6);
385 * XXX - perhaps this should, instead, be declared in "lbl/os-XXX.h" files,
386 * for those OS versions that don't declare it, rather than being declared
387 * here? That way, for example, we could declare it on FreeBSD 2.x (which
388 * doesn't declare it), but not on FreeBSD 3.x (which declares it like
389 * this) or FreeBSD 4.x (which declares it with its first argument as
390 * "const char *", so no matter how we declare it here, it'll fail to
391 * compile on one of 3.x or 4.x).
393 #if !defined(sgi) && !defined(__NetBSD__) && !defined(__FreeBSD__) && \
395 extern int ether_hostton(char *, struct ether_addr
*);
398 /* Use the os supplied routines */
400 pcap_ether_hostton(const char *name
)
406 if (ether_hostton((char *)name
, (struct ether_addr
*)a
) == 0) {
407 ap
= (u_char
*)malloc(6);
409 memcpy((char *)ap
, (char *)a
, 6);
416 __pcap_nametodnaddr(const char *name
)
419 struct nodeent
*getnodebyname();
423 nep
= getnodebyname(name
);
424 if (nep
== ((struct nodeent
*)0))
425 bpf_error("unknown decnet host name '%s'\n", name
);
427 memcpy((char *)&res
, (char *)nep
->n_addr
, sizeof(unsigned short));
430 bpf_error("decnet name support not included, '%s' cannot be translated\n",