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