]>
The Tcpdump Group git mirrors - libpcap/blob - nametoaddr.c
61286662adb5ccd8a0e4e1e15a229fb24b0b7cb1
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.66 2002-08-02 03:44:20 guy Exp $ (LBL)";
35 #include <pcap-stdinc.h>
42 #include <sys/param.h>
43 #include <sys/types.h> /* concession to AIX */
44 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #ifdef HAVE_ETHER_HOSTTON
49 #ifdef HAVE_NETINET_IF_ETHER_H
50 struct mbuf
; /* Squelch compiler warnings on some platforms for */
51 struct rtentry
; /* declarations in <net/if.h> */
52 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
53 #include <netinet/if_ether.h>
54 #endif /* HAVE_NETINET_IF_ETHER_H */
55 #endif /* HAVE_ETHER_HOSTTON */
56 #include <arpa/inet.h>
69 #include <pcap-namedb.h>
71 #ifdef HAVE_OS_PROTO_H
76 #define NTOHL(x) (x) = ntohl(x)
77 #define NTOHS(x) (x) = ntohs(x)
80 static inline int xdtoi(int);
83 * Convert host name to internet address.
84 * Return 0 upon failure.
87 pcap_nametoaddr(const char *name
)
90 static bpf_u_int32
*hlist
[2];
95 if ((hp
= gethostbyname(name
)) != NULL
) {
97 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
101 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
103 return (bpf_u_int32
**)hp
->h_addr_list
;
112 pcap_nametoaddrinfo(const char *name
)
114 struct addrinfo hints
, *res
;
117 memset(&hints
, 0, sizeof(hints
));
118 hints
.ai_family
= PF_UNSPEC
;
119 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
120 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
129 * Convert net name to internet address.
130 * Return 0 upon failure.
133 pcap_nametonetaddr(const char *name
)
138 if ((np
= getnetbyname(name
)) != NULL
)
144 * There's no "getnetbyname()" on Windows.
151 * Convert a port name to its port and protocol numbers.
152 * We assume only TCP or UDP.
153 * Return 0 upon failure.
156 pcap_nametoport(const char *name
, int *port
, int *proto
)
161 sp
= getservbyname(name
, (char *)0);
165 *proto
= pcap_nametoproto(sp
->s_proto
);
167 * We need to check /etc/services for ambiguous entries.
168 * If we find the ambiguous entry, and it has the
169 * same port number, change the proto to PROTO_UNDEF
170 * so both TCP and UDP will be checked.
172 if (*proto
== IPPROTO_TCP
)
177 sp
= getservbyname(name
, other
);
181 if (*port
!= sp
->s_port
)
182 /* Can't handle ambiguous names that refer
183 to different port numbers. */
184 warning("ambiguous port %s in /etc/services",
187 *proto
= PROTO_UNDEF
;
191 #if defined(ultrix) || defined(__osf__)
192 /* Special hack in case NFS isn't in /etc/services */
193 if (strcmp(name
, "nfs") == 0) {
195 *proto
= PROTO_UNDEF
;
203 pcap_nametoproto(const char *str
)
207 p
= getprotobyname(str
);
214 #include "ethertype.h"
221 /* Static data base of ether protocol types. */
222 struct eproto eproto_db
[] = {
223 { "pup", ETHERTYPE_PUP
},
224 { "xns", ETHERTYPE_NS
},
225 { "ip", ETHERTYPE_IP
},
227 { "ip6", ETHERTYPE_IPV6
},
229 { "arp", ETHERTYPE_ARP
},
230 { "rarp", ETHERTYPE_REVARP
},
231 { "sprite", ETHERTYPE_SPRITE
},
232 { "mopdl", ETHERTYPE_MOPDL
},
233 { "moprc", ETHERTYPE_MOPRC
},
234 { "decnet", ETHERTYPE_DN
},
235 { "lat", ETHERTYPE_LAT
},
236 { "sca", ETHERTYPE_SCA
},
237 { "lanbridge", ETHERTYPE_LANBRIDGE
},
238 { "vexp", ETHERTYPE_VEXP
},
239 { "vprod", ETHERTYPE_VPROD
},
240 { "atalk", ETHERTYPE_ATALK
},
241 { "atalkarp", ETHERTYPE_AARP
},
242 { "loopback", ETHERTYPE_LOOPBACK
},
243 { "decdts", ETHERTYPE_DECDTS
},
244 { "decdns", ETHERTYPE_DECDNS
},
249 pcap_nametoeproto(const char *s
)
251 struct eproto
*p
= eproto_db
;
254 if (strcmp(p
->s
, s
) == 0)
261 /* Hex digit to integer. */
275 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
284 while (*s
&& *s
!= '.')
285 n
= n
* 10 + *s
++ - '0';
297 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
300 #define AREAMASK 0176000
301 #define NODEMASK 01777
305 if (sscanf((char *)s
, "%d.%d", &area
, &node
) != 2)
306 bpf_error("malformed decnet address '%s'", s
);
308 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
309 *addr
|= (node
& NODEMASK
);
315 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
316 * ethernet address. Assumes 's' is well formed.
319 pcap_ether_aton(const char *s
)
321 register u_char
*ep
, *e
;
324 e
= ep
= (u_char
*)malloc(6);
330 if (isxdigit((unsigned char)*s
)) {
340 #ifndef HAVE_ETHER_HOSTTON
343 pcap_ether_hostton(const char *name
)
345 register struct pcap_etherent
*ep
;
347 static FILE *fp
= NULL
;
351 fp
= fopen(PCAP_ETHERS_FILE
, "r");
355 } else if (fp
== NULL
)
360 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
361 if (strcmp(ep
->name
, name
) == 0) {
362 ap
= (u_char
*)malloc(6);
364 memcpy(ap
, ep
->addr
, 6);
375 * XXX - perhaps this should, instead, be declared in "lbl/os-XXX.h" files,
376 * for those OS versions that don't declare it, rather than being declared
377 * here? That way, for example, we could declare it on FreeBSD 2.x (which
378 * doesn't declare it), but not on FreeBSD 3.x (which declares it like
379 * this) or FreeBSD 4.x (which declares it with its first argument as
380 * "const char *", so no matter how we declare it here, it'll fail to
381 * compile on one of 3.x or 4.x).
383 #if !defined(sgi) && !defined(__NetBSD__) && !defined(__FreeBSD__)
384 extern int ether_hostton(char *, struct ether_addr
*);
387 /* Use the os supplied routines */
389 pcap_ether_hostton(const char *name
)
395 if (ether_hostton((char *)name
, (struct ether_addr
*)a
) == 0) {
396 ap
= (u_char
*)malloc(6);
398 memcpy((char *)ap
, (char *)a
, 6);
405 __pcap_nametodnaddr(const char *name
)
408 struct nodeent
*getnodebyname();
412 nep
= getnodebyname(name
);
413 if (nep
== ((struct nodeent
*)0))
414 bpf_error("unknown decnet host name '%s'\n", name
);
416 memcpy((char *)&res
, (char *)nep
->n_addr
, sizeof(unsigned short));
419 bpf_error("decnet name support not included, '%s' cannot be translated\n",