]>
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.
30 #include <sys/types.h>
31 #include <netdnet/dnetdb.h>
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 #ifdef HAVE_ETHER_HOSTTON
50 * XXX - do we need any of this if <netinet/if_ether.h> doesn't declare
53 #ifdef HAVE_NETINET_IF_ETHER_H
54 struct mbuf
; /* Squelch compiler warnings on some platforms for */
55 struct rtentry
; /* declarations in <net/if.h> */
56 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
57 #include <netinet/if_ether.h>
58 #endif /* HAVE_NETINET_IF_ETHER_H */
59 #ifdef NETINET_ETHER_H_DECLARES_ETHER_HOSTTON
60 #include <netinet/ether.h>
61 #endif /* NETINET_ETHER_H_DECLARES_ETHER_HOSTTON */
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 * Convert a string in the form PPP-PPP, where correspond to ports, to
214 * a starting and ending port in a port range.
215 * Return 0 on failure.
218 pcap_nametoportrange(const char *name
, int *port1
, int *port2
, int *proto
)
224 if (sscanf(name
, "%d-%d", &p1
, &p2
) != 2) {
225 if ((cpy
= strdup(name
)) == NULL
)
228 if ((off
= strchr(cpy
, '-')) == NULL
) {
235 if (pcap_nametoport(cpy
, port1
, proto
) == 0) {
241 if (pcap_nametoport(off
+ 1, port2
, proto
) == 0) {
247 if (*proto
!= save_proto
)
248 *proto
= PROTO_UNDEF
;
252 *proto
= PROTO_UNDEF
;
259 pcap_nametoproto(const char *str
)
263 p
= getprotobyname(str
);
270 #include "ethertype.h"
277 /* Static data base of ether protocol types. */
278 struct eproto eproto_db
[] = {
279 { "pup", ETHERTYPE_PUP
},
280 { "xns", ETHERTYPE_NS
},
281 { "ip", ETHERTYPE_IP
},
283 { "ip6", ETHERTYPE_IPV6
},
285 { "arp", ETHERTYPE_ARP
},
286 { "rarp", ETHERTYPE_REVARP
},
287 { "sprite", ETHERTYPE_SPRITE
},
288 { "mopdl", ETHERTYPE_MOPDL
},
289 { "moprc", ETHERTYPE_MOPRC
},
290 { "decnet", ETHERTYPE_DN
},
291 { "lat", ETHERTYPE_LAT
},
292 { "sca", ETHERTYPE_SCA
},
293 { "lanbridge", ETHERTYPE_LANBRIDGE
},
294 { "vexp", ETHERTYPE_VEXP
},
295 { "vprod", ETHERTYPE_VPROD
},
296 { "atalk", ETHERTYPE_ATALK
},
297 { "atalkarp", ETHERTYPE_AARP
},
298 { "loopback", ETHERTYPE_LOOPBACK
},
299 { "decdts", ETHERTYPE_DECDTS
},
300 { "decdns", ETHERTYPE_DECDNS
},
305 pcap_nametoeproto(const char *s
)
307 struct eproto
*p
= eproto_db
;
310 if (strcmp(p
->s
, s
) == 0)
319 /* Static data base of LLC values. */
320 static struct eproto llc_db
[] = {
321 { "iso", LLCSAP_ISONS
},
322 { "stp", LLCSAP_8021D
},
323 { "ipx", LLCSAP_IPX
},
324 { "netbeui", LLCSAP_NETBEUI
},
329 pcap_nametollc(const char *s
)
331 struct eproto
*p
= llc_db
;
334 if (strcmp(p
->s
, s
) == 0)
341 /* Hex digit to integer. */
355 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
364 while (*s
&& *s
!= '.')
365 n
= n
* 10 + *s
++ - '0';
377 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
380 #define AREAMASK 0176000
381 #define NODEMASK 01777
385 if (sscanf(s
, "%d.%d", &area
, &node
) != 2)
386 bpf_error("malformed decnet address '%s'", s
);
388 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
389 *addr
|= (node
& NODEMASK
);
395 * Convert 's', which can have the one of the forms:
397 * "xx:xx:xx:xx:xx:xx"
398 * "xx.xx.xx.xx.xx.xx"
399 * "xx-xx-xx-xx-xx-xx"
403 * (or various mixes of ':', '.', and '-') into a new
404 * ethernet address. Assumes 's' is well formed.
407 pcap_ether_aton(const char *s
)
409 register u_char
*ep
, *e
;
412 e
= ep
= (u_char
*)malloc(6);
417 if (*s
== ':' || *s
== '.' || *s
== '-')
420 if (isxdigit((unsigned char)*s
)) {
430 #ifndef HAVE_ETHER_HOSTTON
433 pcap_ether_hostton(const char *name
)
435 register struct pcap_etherent
*ep
;
437 static FILE *fp
= NULL
;
441 fp
= fopen(PCAP_ETHERS_FILE
, "r");
445 } else if (fp
== NULL
)
450 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
451 if (strcmp(ep
->name
, name
) == 0) {
452 ap
= (u_char
*)malloc(6);
454 memcpy(ap
, ep
->addr
, 6);
464 #if !defined(HAVE_DECL_ETHER_HOSTTON) || !HAVE_DECL_ETHER_HOSTTON
465 #ifndef HAVE_STRUCT_ETHER_ADDR
467 unsigned char ether_addr_octet
[6];
470 extern int ether_hostton(const char *, struct ether_addr
*);
473 /* Use the os supplied routines */
475 pcap_ether_hostton(const char *name
)
481 if (ether_hostton(name
, (struct ether_addr
*)a
) == 0) {
482 ap
= (u_char
*)malloc(6);
484 memcpy((char *)ap
, (char *)a
, 6);
491 __pcap_nametodnaddr(const char *name
)
494 struct nodeent
*getnodebyname();
498 nep
= getnodebyname(name
);
499 if (nep
== ((struct nodeent
*)0))
500 bpf_error("unknown decnet host name '%s'\n", name
);
502 memcpy((char *)&res
, (char *)nep
->n_addr
, sizeof(unsigned short));
505 bpf_error("decnet name support not included, '%s' cannot be translated\n",