2 * Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
30 #include "sys_interfaces.h"
32 __private_extern__ interface_list_t
*
35 interface_list_t
*my_interfaces
= NULL
;
37 struct ifaddrs
*ifa
, *p
;
39 if (getifaddrs(&ifa
) < 0) return NULL
;
41 my_interfaces
= (interface_list_t
*)malloc(sizeof(interface_list_t
));
42 my_interfaces
->count
= 0;
43 my_interfaces
->interface
= NULL
;
45 for (p
= ifa
; p
!= NULL
; p
= p
->ifa_next
)
47 if (p
->ifa_addr
== NULL
) continue;
48 if ((p
->ifa_flags
& IFF_UP
) == 0) continue;
49 if (p
->ifa_addr
->sa_family
!= AF_INET
) continue;
51 my_interfaces
->count
++;
52 if (my_interfaces
->count
== 1)
54 my_interfaces
->interface
= (interface_t
*)malloc(sizeof(interface_t
));
58 my_interfaces
->interface
= (interface_t
*)realloc(my_interfaces
->interface
, my_interfaces
->count
* sizeof(interface_t
));
61 iface
= &(my_interfaces
->interface
[my_interfaces
->count
- 1]);
62 memset(iface
, 0, sizeof(interface_t
));
63 iface
->name
= strdup(p
->ifa_name
);
64 iface
->flags
= p
->ifa_flags
;
65 iface
->addr
.s_addr
= ((struct sockaddr_in
*)(p
->ifa_addr
))->sin_addr
.s_addr
;
66 iface
->mask
.s_addr
= ((struct sockaddr_in
*)(p
->ifa_netmask
))->sin_addr
.s_addr
;
67 iface
->netaddr
.s_addr
= iface
->addr
.s_addr
& iface
->mask
.s_addr
;
68 iface
->bcast
.s_addr
= iface
->netaddr
.s_addr
| (~iface
->mask
.s_addr
);
76 __private_extern__
void
77 sys_interfaces_release(interface_list_t
*l
)
81 if (l
== NULL
) return;
83 for (i
= 0; i
< l
->count
; i
++)
85 if (l
->interface
[i
].name
!= NULL
) free(l
->interface
[i
].name
);
92 __private_extern__
int
93 sys_is_my_address(interface_list_t
*l
, struct in_addr
*a
)
97 if (l
== NULL
) return 0;
99 for (i
= 0; i
< l
->count
; i
++)
101 if (a
->s_addr
== l
->interface
[i
].addr
.s_addr
) return 1;
106 __private_extern__
int
107 sys_is_my_network(interface_list_t
*l
, struct in_addr
*a
)
111 if (l
== NULL
) return 0;
113 for (i
= 0; i
< l
->count
; i
++)
115 if ((a
->s_addr
& l
->interface
[i
].mask
.s_addr
) ==
116 l
->interface
[i
].netaddr
.s_addr
) return 1;