]>
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.48 1999-10-07 23:46:40 mcr Exp $ (LBL)";
30 #include <sys/param.h>
31 #include <sys/types.h> /* concession to AIX */
32 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43 #include <arpa/inet.h>
55 #include <pcap-namedb.h>
58 #ifdef HAVE_OS_PROTO_H
63 #define NTOHL(x) (x) = ntohl(x)
64 #define NTOHS(x) (x) = ntohs(x)
67 static inline int xdtoi(int);
70 * Convert host name to internet address.
71 * Return 0 upon failure.
74 pcap_nametoaddr(const char *name
)
77 static bpf_u_int32
*hlist
[2];
82 if ((hp
= gethostbyname(name
)) != NULL
) {
84 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
88 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
90 return (bpf_u_int32
**)hp
->h_addr_list
;
98 * Convert net name to internet address.
99 * Return 0 upon failure.
102 pcap_nametonetaddr(const char *name
)
106 if ((np
= getnetbyname(name
)) != NULL
)
113 * Convert a port name to its port and protocol numbers.
114 * We assume only TCP or UDP.
115 * Return 0 upon failure.
118 pcap_nametoport(const char *name
, int *port
, int *proto
)
123 sp
= getservbyname(name
, (char *)0);
127 *proto
= pcap_nametoproto(sp
->s_proto
);
129 * We need to check /etc/services for ambiguous entries.
130 * If we find the ambiguous entry, and it has the
131 * same port number, change the proto to PROTO_UNDEF
132 * so both TCP and UDP will be checked.
134 if (*proto
== IPPROTO_TCP
)
139 sp
= getservbyname(name
, other
);
143 if (*port
!= sp
->s_port
)
144 /* Can't handle ambiguous names that refer
145 to different port numbers. */
146 warning("ambiguous port %s in /etc/services",
149 *proto
= PROTO_UNDEF
;
153 #if defined(ultrix) || defined(__osf__)
154 /* Special hack in case NFS isn't in /etc/services */
155 if (strcmp(name
, "nfs") == 0) {
157 *proto
= PROTO_UNDEF
;
165 pcap_nametoproto(const char *str
)
169 p
= getprotobyname(str
);
176 #include "ethertype.h"
183 /* Static data base of ether protocol types. */
184 struct eproto eproto_db
[] = {
185 { "pup", ETHERTYPE_PUP
},
186 { "xns", ETHERTYPE_NS
},
187 { "ip", ETHERTYPE_IP
},
188 { "arp", ETHERTYPE_ARP
},
189 { "rarp", ETHERTYPE_REVARP
},
190 { "sprite", ETHERTYPE_SPRITE
},
191 { "mopdl", ETHERTYPE_MOPDL
},
192 { "moprc", ETHERTYPE_MOPRC
},
193 { "decnet", ETHERTYPE_DN
},
194 { "lat", ETHERTYPE_LAT
},
195 { "sca", ETHERTYPE_SCA
},
196 { "lanbridge", ETHERTYPE_LANBRIDGE
},
197 { "vexp", ETHERTYPE_VEXP
},
198 { "vprod", ETHERTYPE_VPROD
},
199 { "atalk", ETHERTYPE_ATALK
},
200 { "atalkarp", ETHERTYPE_AARP
},
201 { "loopback", ETHERTYPE_LOOPBACK
},
202 { "decdts", ETHERTYPE_DECDTS
},
203 { "decdns", ETHERTYPE_DECDNS
},
208 pcap_nametoeproto(const char *s
)
210 struct eproto
*p
= eproto_db
;
213 if (strcmp(p
->s
, s
) == 0)
220 /* Hex digit to integer. */
234 __pcap_atoin(const char *s
, bpf_u_int32
*addr
)
243 while (*s
&& *s
!= '.')
244 n
= n
* 10 + *s
++ - '0';
256 __pcap_atodn(const char *s
, bpf_u_int32
*addr
)
259 #define AREAMASK 0176000
260 #define NODEMASK 01777
264 if (sscanf((char *)s
, "%d.%d", &area
, &node
) != 2)
265 bpf_error("malformed decnet address '%s'", s
);
267 *addr
= (area
<< AREASHIFT
) & AREAMASK
;
268 *addr
|= (node
& NODEMASK
);
274 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
275 * ethernet address. Assumes 's' is well formed.
278 pcap_ether_aton(const char *s
)
280 register u_char
*ep
, *e
;
283 e
= ep
= (u_char
*)malloc(6);
299 #ifndef HAVE_ETHER_HOSTTON
302 pcap_ether_hostton(const char *name
)
304 register struct pcap_etherent
*ep
;
306 static FILE *fp
= NULL
;
310 fp
= fopen(PCAP_ETHERS_FILE
, "r");
314 } else if (fp
== NULL
)
319 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
320 if (strcmp(ep
->name
, name
) == 0) {
321 ap
= (u_char
*)malloc(6);
323 memcpy(ap
, ep
->addr
, 6);
334 extern int ether_hostton(char *, struct ether_addr
*);
337 /* Use the os supplied routines */
339 pcap_ether_hostton(const char *name
)
345 if (ether_hostton((char *)name
, (struct ether_addr
*)a
) == 0) {
346 ap
= (u_char
*)malloc(6);
348 memcpy((char *)ap
, (char *)a
, 6);
355 __pcap_nametodnaddr(const char *name
)
358 struct nodeent
*getnodebyname();
362 nep
= getnodebyname(name
);
363 if (nep
== ((struct nodeent
*)0))
364 bpf_error("unknown decnet host name '%s'\n", name
);
366 memcpy((char *)&res
, (char *)nep
->n_addr
, sizeof(unsigned short));
369 bpf_error("decnet name support not included, '%s' cannot be translated\n",