]>
git.saurik.com Git - apple/network_cmds.git/blob - pcap/inet.c
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: inet.c,v 1.6 1997/01/24 19:17:25 deraadt Exp $ */
27 * Copyright (c) 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 the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the Computer Systems
41 * Engineering Group at Lawrence Berkeley Laboratory.
42 * 4. Neither the name of the University nor of the Laboratory may be used
43 * to endorse or promote products derived from this software without
44 * specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 "@(#) Header: inet.c,v 1.16 96/06/23 14:28:22 leres Exp (LBL)";
64 #include <sys/param.h>
66 #include <sys/ioctl.h>
67 #include <sys/socket.h>
68 #ifdef HAVE_SYS_SOCKIO_H
69 #include <sys/sockio.h>
78 #include <netinet/in.h>
89 #ifdef HAVE_OS_PROTO_H
95 /* Not all systems have IFF_LOOPBACK */
97 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
99 #define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
103 * Return the name of a network interface attached to the system, or NULL
104 * if none can be found. The interface must be configured up; the
105 * lowest unit number is preferred; loopback is ignored.
108 pcap_lookupdev(errbuf
)
109 register char *errbuf
;
111 register int fd
, minunit
, n
;
112 register char *cp
, *ibuf
= NULL
;
113 register struct ifreq
*ifrp
, *ifend
, *ifnext
, *mp
;
116 static char device
[sizeof(ifrp
->ifr_name
) + 1];
119 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
121 (void)sprintf(errbuf
, "socket: %s", pcap_strerror(errno
));
126 ifc
.ifc_buf
= ibuf
= realloc(ibuf
, len
);
131 if (ioctl(fd
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
136 if (ifc
.ifc_len
+ sizeof(ifr
) < len
)
141 ifrp
= (struct ifreq
*)ibuf
;
142 ifend
= (struct ifreq
*)((char *)ibuf
+ ifc
.ifc_len
);
146 for (; ifrp
< ifend
; ifrp
= ifnext
) {
147 #if BSD - 0 >= 199006
148 n
= ifrp
->ifr_addr
.sa_len
+ sizeof(ifrp
->ifr_name
);
149 if (n
< sizeof(*ifrp
))
152 ifnext
= (struct ifreq
*)((char *)ifrp
+ n
);
153 if (ifrp
->ifr_addr
.sa_family
!= AF_INET
)
159 * Need a template to preserve address info that is
160 * used below to locate the next entry. (Otherwise,
161 * SIOCGIFFLAGS stomps over it because the requests
162 * are returned in a union.)
164 strncpy(ifr
.ifr_name
, ifrp
->ifr_name
, sizeof(ifr
.ifr_name
));
165 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
166 (void)sprintf(errbuf
, "SIOCGIFFLAGS: %s",
167 pcap_strerror(errno
));
173 /* Must be up and not the loopback */
174 if ((ifr
.ifr_flags
& IFF_UP
) == 0 || ISLOOPBACK(&ifr
))
177 for (cp
= ifrp
->ifr_name
; !isdigit(*cp
); ++cp
)
188 (void)strcpy(errbuf
, "no suitable device found");
192 (void)strncpy(device
, mp
->ifr_name
, sizeof(device
) - 1);
193 device
[sizeof(device
) - 1] = '\0';
198 pcap_lookupnet(device
, netp
, maskp
, errbuf
)
199 register char *device
;
200 register bpf_u_int32
*netp
, *maskp
;
201 register char *errbuf
;
204 register struct sockaddr_in
*sin
;
207 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
209 (void)sprintf(errbuf
, "socket: %s", pcap_strerror(errno
));
212 memset(&ifr
, 0, sizeof(ifr
));
214 /* XXX Work around Linux kernel bug */
215 ifr
.ifr_addr
.sa_family
= AF_INET
;
217 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
218 if (ioctl(fd
, SIOCGIFADDR
, (char *)&ifr
) < 0) {
219 (void)sprintf(errbuf
, "SIOCGIFADDR: %s: %s",
220 device
, pcap_strerror(errno
));
224 sin
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
225 *netp
= sin
->sin_addr
.s_addr
;
226 if (ioctl(fd
, SIOCGIFNETMASK
, (char *)&ifr
) < 0) {
227 (void)sprintf(errbuf
, "SIOCGIFNETMASK: %s: %s",
228 device
, pcap_strerror(errno
));
233 *maskp
= sin
->sin_addr
.s_addr
;
235 if (IN_CLASSA(*netp
))
236 *maskp
= IN_CLASSA_NET
;
237 else if (IN_CLASSB(*netp
))
238 *maskp
= IN_CLASSB_NET
;
239 else if (IN_CLASSC(*netp
))
240 *maskp
= IN_CLASSC_NET
;
242 (void)sprintf(errbuf
, "inet class for 0x%x unknown",