2 * Copyright (c) 2001 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.1 (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@
29 #include "sys_interfaces.h"
31 __private_extern__ interface_list_t
*
32 _libinfo_ni_sys_interfaces(void)
34 interface_list_t
*my_interfaces
= NULL
;
36 struct ifaddrs
*ifa
, *p
;
38 if (getifaddrs(&ifa
) < 0) return NULL
;
40 my_interfaces
= (interface_list_t
*)malloc(sizeof(interface_list_t
));
41 my_interfaces
->count
= 0;
42 my_interfaces
->interface
= NULL
;
44 for (p
= ifa
; p
!= NULL
; p
= p
->ifa_next
)
46 if (p
->ifa_addr
== NULL
) continue;
47 if ((p
->ifa_flags
& IFF_UP
) == 0) continue;
48 if (p
->ifa_addr
->sa_family
!= AF_INET
) continue;
50 my_interfaces
->count
++;
51 if (my_interfaces
->count
== 1)
53 my_interfaces
->interface
= (interface_t
*)malloc(sizeof(interface_t
));
57 my_interfaces
->interface
= (interface_t
*)realloc(my_interfaces
->interface
, my_interfaces
->count
* sizeof(interface_t
));
60 iface
= &(my_interfaces
->interface
[my_interfaces
->count
- 1]);
61 memset(iface
, 0, sizeof(interface_t
));
62 iface
->name
= strdup(p
->ifa_name
);
63 iface
->flags
= p
->ifa_flags
;
64 iface
->addr
.s_addr
= ((struct sockaddr_in
*)(p
->ifa_addr
))->sin_addr
.s_addr
;
65 iface
->mask
.s_addr
= ((struct sockaddr_in
*)(p
->ifa_netmask
))->sin_addr
.s_addr
;
66 iface
->netaddr
.s_addr
= iface
->addr
.s_addr
& iface
->mask
.s_addr
;
67 iface
->bcast
.s_addr
= iface
->netaddr
.s_addr
| (~iface
->mask
.s_addr
);
75 __private_extern__
void
76 _libinfo_ni_sys_interfaces_release(interface_list_t
*l
)
80 if (l
== NULL
) return;
82 for (i
= 0; i
< l
->count
; i
++)
84 if (l
->interface
[i
].name
!= NULL
) free(l
->interface
[i
].name
);
91 __private_extern__
int
92 _libinfo_ni_sys_is_my_address(interface_list_t
*l
, struct in_addr
*a
)
96 if (l
== NULL
) return 0;
98 for (i
= 0; i
< l
->count
; i
++)
100 if (a
->s_addr
== l
->interface
[i
].addr
.s_addr
) return 1;
105 __private_extern__
int
106 _libinfo_ni_sys_is_my_network(interface_list_t
*l
, struct in_addr
*a
)
110 if (l
== NULL
) return 0;
112 for (i
= 0; i
< l
->count
; i
++)
114 if ((a
->s_addr
& l
->interface
[i
].mask
.s_addr
) ==
115 l
->interface
[i
].netaddr
.s_addr
) return 1;