]>
git.saurik.com Git - apple/network_cmds.git/blob - pcap/nametoaddr.c
3ce8008ed80035b5cb6be822e6475c61113aa1df
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: nametoaddr.c,v 1.5 1996/09/16 02:33:06 tholo Exp $ */
27 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that: (1) source code distributions
32 * retain the above copyright notice and this paragraph in its entirety, (2)
33 * distributions including binary code include the above copyright notice and
34 * this paragraph in its entirety in the documentation or other materials
35 * provided with the distribution, and (3) all advertising materials mentioning
36 * features or use of this software display the following acknowledgement:
37 * ``This product includes software developed by the University of California,
38 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
39 * the University nor the names of its contributors may be used to endorse
40 * or promote products derived from this software without specific prior
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
43 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 * Name to id translation routines used by the scanner.
47 * These functions are not time critical.
52 "@(#) Header: nametoaddr.c,v 1.38 96/06/17 02:42:50 leres Exp (LBL)";
55 #include <sys/param.h>
56 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 #include <arpa/inet.h>
74 #include <pcap-namedb.h>
77 #ifdef HAVE_OS_PROTO_H
85 #define NTOHL(x) (x) = ntohl(x)
86 #define NTOHS(x) (x) = ntohs(x)
89 static __inline
int xdtoi(int);
92 * Convert host name to internet address.
93 * Return 0 upon failure.
96 pcap_nametoaddr(const char *name
)
99 static bpf_u_int32
*hlist
[2];
104 if ((hp
= gethostbyname(name
)) != NULL
) {
106 hlist
[0] = (bpf_u_int32
*)hp
->h_addr
;
110 for (p
= (bpf_u_int32
**)hp
->h_addr_list
; *p
; ++p
)
112 return (bpf_u_int32
**)hp
->h_addr_list
;
120 * Convert net name to internet address.
121 * Return 0 upon failure.
124 pcap_nametonetaddr(const char *name
)
128 if ((np
= getnetbyname(name
)) != NULL
)
135 * Convert a port name to its port and protocol numbers.
136 * We assume only TCP or UDP.
137 * Return 0 upon failure.
140 pcap_nametoport(const char *name
, int *port
, int *proto
)
145 sp
= getservbyname(name
, (char *)0);
149 *proto
= pcap_nametoproto(sp
->s_proto
);
151 * We need to check /etc/services for ambiguous entries.
152 * If we find the ambiguous entry, and it has the
153 * same port number, change the proto to PROTO_UNDEF
154 * so both TCP and UDP will be checked.
156 if (*proto
== IPPROTO_TCP
)
161 sp
= getservbyname(name
, other
);
165 if (*port
!= sp
->s_port
)
166 /* Can't handle ambiguous names that refer
167 to different port numbers. */
168 warning("ambiguous port %s in /etc/services",
171 *proto
= PROTO_UNDEF
;
175 #if defined(ultrix) || defined(__osf__)
176 /* Special hack in case NFS isn't in /etc/services */
177 if (strcmp(name
, "nfs") == 0) {
179 *proto
= PROTO_UNDEF
;
187 pcap_nametoproto(const char *str
)
191 p
= getprotobyname(str
);
198 #include "ethertype.h"
205 /* Static data base of ether protocol types. */
206 struct eproto eproto_db
[] = {
207 { "pup", ETHERTYPE_PUP
},
208 { "xns", ETHERTYPE_NS
},
209 { "ip", ETHERTYPE_IP
},
210 { "arp", ETHERTYPE_ARP
},
211 { "rarp", ETHERTYPE_REVARP
},
212 { "sprite", ETHERTYPE_SPRITE
},
213 { "mopdl", ETHERTYPE_MOPDL
},
214 { "moprc", ETHERTYPE_MOPRC
},
215 { "decnet", ETHERTYPE_DN
},
216 { "lat", ETHERTYPE_LAT
},
217 { "lanbridge", ETHERTYPE_LANBRIDGE
},
218 { "vexp", ETHERTYPE_VEXP
},
219 { "vprod", ETHERTYPE_VPROD
},
220 { "atalk", ETHERTYPE_ATALK
},
221 { "atalkarp", ETHERTYPE_AARP
},
222 { "loopback", ETHERTYPE_LOOPBACK
},
223 { "decdts", ETHERTYPE_DECDTS
},
224 { "decdns", ETHERTYPE_DECDNS
},
229 pcap_nametoeproto(const char *s
)
231 struct eproto
*p
= eproto_db
;
234 if (strcmp(p
->s
, s
) == 0)
241 /* Hex digit to integer. */
255 __pcap_atoin(const char *s
)
257 bpf_u_int32 addr
= 0;
262 while (*s
&& *s
!= '.')
263 n
= n
* 10 + *s
++ - '0';
274 __pcap_atodn(const char *s
)
277 #define AREAMASK 0176000
278 #define NODEMASK 01777
280 bpf_u_int32 addr
= 0;
283 if (sscanf((char *)s
, "%d.%d", &area
, &node
) != 2)
284 bpf_error("malformed decnet address '%s'", s
);
286 addr
= (area
<< AREASHIFT
) & AREAMASK
;
287 addr
|= (node
& NODEMASK
);
293 * Convert 's' which has the form "xx:xx:xx:xx:xx:xx" into a new
294 * ethernet address. Assumes 's' is well formed.
297 pcap_ether_aton(const char *s
)
299 register u_char
*ep
, *e
;
302 e
= ep
= (u_char
*)malloc(6);
318 #ifndef HAVE_ETHER_HOSTTON
321 pcap_ether_hostton(const char *name
)
323 register struct pcap_etherent
*ep
;
325 static FILE *fp
= NULL
;
329 fp
= fopen(PCAP_ETHERS_FILE
, "r");
333 } else if (fp
== NULL
)
338 while ((ep
= pcap_next_etherent(fp
)) != NULL
) {
339 if (strcmp(ep
->name
, name
) == 0) {
340 ap
= (u_char
*)malloc(6);
342 memcpy(ap
, ep
->addr
, 6);
353 extern int ether_hostton(char *, struct ether_addr
*);
356 /* Use the os supplied routines */
358 pcap_ether_hostton(const char *name
)
364 if (ether_hostton((char *)name
, (struct ether_addr
*)a
) == 0) {
365 ap
= (u_char
*)malloc(6);
374 __pcap_nametodnaddr(const char *name
)
377 struct nodeent
*getnodebyname();
381 nep
= getnodebyname(name
);
382 if (nep
== ((struct nodeent
*)0))
383 bpf_error("unknown decnet host name '%s'\n", name
);
385 memcpy((char *)&res
, (char *)nep
->n_addr
, sizeof(unsigned short));
388 bpf_error("decnet name support not included, '%s' cannot be translated\n",