]>
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
[] =
27 "@(#) $Header: /tcpdump/master/libpcap/nametoaddr.c,v 1.60 2001-07-28 22:56:35 guy Exp $ (LBL)";
34 #include <sys/param.h>
35 #include <sys/types.h> /* concession to AIX */
36 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #ifdef HAVE_NETINET_IF_ETHER_H
44 #include <netinet/if_ether.h>
46 #include <arpa/inet.h>
49 #include <sys/socket.h>
62 #include <pcap-namedb.h>
64 #ifdef HAVE_OS_PROTO_H
69 #define NTOHL(x) (x) = ntohl(x)
70 #define NTOHS(x) (x) = ntohs(x)
73 static inline int xdtoi(int);
76 * Convert host name to internet address.
77 * Return 0 upon failure.
80 pcap_nametoaddr(const char *name
)
83 static bpf_u_int32
*hlist
[2];
88 if ((hp
= gethostbyname(name
)) != NULL
) {
90 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
94 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
96 return (bpf_u_int32
**)hp
->h_addr_list
;
105 pcap_nametoaddrinfo(const char *name
)
107 struct addrinfo hints
, *res
;
110 memset(&hints
, 0, sizeof(hints
));
111 hints
.ai_family
= PF_UNSPEC
;
112 hints
.ai_socktype
= SOCK_STREAM
; /*not really*/
113 error
= getaddrinfo(name
, NULL
, &hints
, &res
);
122 * Convert net name to internet address.
123 * Return 0 upon failure.
126 pcap_nametonetaddr(const char *name
)
130 if ((np
= getnetbyname(name
)) != NULL
)
137 * Convert a port name to its port and protocol numbers.
138 * We assume only TCP or UDP.
139 * Return 0 upon failure.
142 pcap_nametoport(const char *name
, int *port
, int *proto
)
147 sp
= getservbyname(name
, (char *)0);
151 *proto
= pcap_nametoproto(sp
->s_proto
);
153 * We need to check /etc/services for ambiguous entries.
154 * If we find the ambiguous entry, and it has the
155 * same port number, change the proto to PROTO_UNDEF
156 * so both TCP and UDP will be checked.
158 if (*proto
== IPPROTO_TCP
)
163 sp
= getservbyname(name
, other
);
167 if (*port
!= sp
->s_port
)
168 /* Can't handle ambiguous names that refer
169 to different port numbers. */
170 warning("ambiguous port %s in /etc/services",
173 *proto
= PROTO_UNDEF
;
177 #if defined(ultrix) || defined(__osf__)
178 /* Special hack in case NFS isn't in /etc/services */
179 if (strcmp(name
, "nfs") == 0) {
181 *proto
= PROTO_UNDEF
;
189 pcap_nametoproto(const char *str
)
193 p
= getprotobyname(str
);
200 #include "ethertype.h"
207 /* Static data base of ether protocol types. */
208 struct eproto eproto_db
[] = {
209 { "pup", ETHERTYPE_PUP
},
210 { "xns", ETHERTYPE_NS
},
211 { "ip", ETHERTYPE_IP
},
213 { "ip6", ETHERTYPE_IPV6
},
215 { "arp", ETHERTYPE_ARP
},
216 { "rarp", ETHERTYPE_REVARP
},
217 { "sprite", ETHERTYPE_SPRITE
},
218 { "mopdl", ETHERTYPE_MOPDL
},
219 { "moprc", ETHERTYPE_MOPRC
},
220 { "decnet", ETHERTYPE_DN
},
221 { "lat", ETHERTYPE_LAT
},
222 { "sca", ETHERTYPE_SCA
},
223 { "lanbridge", ETHERTYPE_LANBRIDGE
},
224 { "vexp", ETHERTYPE_VEXP
},
225 { "vprod", ETHERTYPE_VPROD
},
226 { "atalk", ETHERTYPE_ATALK
},
227 { "atalkarp", ETHERTYPE_AARP
},
228 { "loopback", ETHERTYPE_LOOPBACK
},
229 { "decdts", ETHERTYPE_DECDTS
},
230 { "decdns", ETHERTYPE_DECDNS
},
235 pcap_nametoeproto(const char *s
)
237 struct eproto
*p
= eproto_db
;
240 if (strcmp(p
->s
, s
) == 0)
247 /* Hex digit to integer. */
261 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
270 while (*s
&& *s
!= '.')
271 n
= n
* 10 + *s
++ - '0';
283 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
286 #define AREAMASK 0176000
287 #define NODEMASK 01777
291 if (sscanf((char *)s
, "%d.%d", &area
, &node
) != 2)
292 bpf_error("malformed decnet address '%s'", s
);
294 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
295 *addr
|= (node
& NODEMASK
);
301 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
302 * ethernet address. Assumes 's' is well formed.
305 pcap_ether_aton(const char *s
)
307 register u_char
*ep
, *e
;
310 e
= ep
= (u_char
*)malloc(6);
316 if (isxdigit((unsigned char)*s
)) {
326 #ifndef HAVE_ETHER_HOSTTON
329 pcap_ether_hostton(const char *name
)
331 register struct pcap_etherent
*ep
;
333 static FILE *fp
= NULL
;
337 fp
= fopen(PCAP_ETHERS_FILE
, "r");
341 } else if (fp
== NULL
)
346 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
347 if (strcmp(ep
->name
, name
) == 0) {
348 ap
= (u_char
*)malloc(6);
350 memcpy(ap
, ep
->addr
, 6);
361 * XXX - perhaps this should, instead, be declared in "lbl/os-XXX.h" files,
362 * for those OS versions that don't declare it, rather than being declared
363 * here? That way, for example, we could declare it on FreeBSD 2.x (which
364 * doesn't declare it), but not on FreeBSD 3.x (which declares it like
365 * this) or FreeBSD 4.x (which declares it with its first argument as
366 * "const char *", so no matter how we declare it here, it'll fail to
367 * compile on one of 3.x or 4.x).
369 #if !defined(sgi) && !defined(__NetBSD__) && !defined(__FreeBSD__)
370 extern int ether_hostton(char *, struct ether_addr
*);
373 /* Use the os supplied routines */
375 pcap_ether_hostton(const char *name
)
381 if (ether_hostton((char *)name
, (struct ether_addr
*)a
) == 0) {
382 ap
= (u_char
*)malloc(6);
384 memcpy((char *)ap
, (char *)a
, 6);
391 __pcap_nametodnaddr(const char *name
)
394 struct nodeent
*getnodebyname();
398 nep
= getnodebyname(name
);
399 if (nep
== ((struct nodeent
*)0))
400 bpf_error("unknown decnet host name '%s'\n", name
);
402 memcpy((char *)&res
, (char *)nep
->n_addr
, sizeof(unsigned short));
405 bpf_error("decnet name support not included, '%s' cannot be translated\n",