Libinfo-173.tar.gz
[apple/libinfo.git] / netinfo.subproj / sys_interfaces.c
1 /*
2 * Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <ifaddrs.h>
30 #include "sys_interfaces.h"
31
32 __private_extern__ interface_list_t *
33 sys_interfaces(void)
34 {
35 interface_list_t *my_interfaces = NULL;
36 interface_t *iface;
37 struct ifaddrs *ifa, *p;
38
39 if (getifaddrs(&ifa) < 0) return NULL;
40
41 my_interfaces = (interface_list_t *)malloc(sizeof(interface_list_t));
42 my_interfaces->count = 0;
43 my_interfaces->interface = NULL;
44
45 for (p = ifa; p != NULL; p = p->ifa_next)
46 {
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;
50
51 my_interfaces->count++;
52 if (my_interfaces->count == 1)
53 {
54 my_interfaces->interface = (interface_t *)malloc(sizeof(interface_t));
55 }
56 else
57 {
58 my_interfaces->interface = (interface_t *)realloc(my_interfaces->interface, my_interfaces->count * sizeof(interface_t));
59 }
60
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);
69 }
70
71 freeifaddrs(ifa);
72
73 return my_interfaces;
74 }
75
76 __private_extern__ void
77 sys_interfaces_release(interface_list_t *l)
78 {
79 int i;
80
81 if (l == NULL) return;
82
83 for (i = 0; i < l->count; i++)
84 {
85 if (l->interface[i].name != NULL) free(l->interface[i].name);
86 }
87
88 free(l->interface);
89 free(l);
90 }
91
92 __private_extern__ int
93 sys_is_my_address(interface_list_t *l, struct in_addr *a)
94 {
95 int i;
96
97 if (l == NULL) return 0;
98
99 for (i = 0; i < l->count; i++)
100 {
101 if (a->s_addr == l->interface[i].addr.s_addr) return 1;
102 }
103 return 0;
104 }
105
106 __private_extern__ int
107 sys_is_my_network(interface_list_t *l, struct in_addr *a)
108 {
109 int i;
110
111 if (l == NULL) return 0;
112
113 for (i = 0; i < l->count; i++)
114 {
115 if ((a->s_addr & l->interface[i].mask.s_addr) ==
116 l->interface[i].netaddr.s_addr) return 1;
117
118 }
119 return 0;
120 }