]> git.saurik.com Git - apple/configd.git/blob - nwi/network_information_priv.h
configd-453.16.tar.gz
[apple/configd.git] / nwi / network_information_priv.h
1 /*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef _NETWORK_INFORMATION_PRIV_H_
25 #define _NETWORK_INFORMATION_PRIV_H_
26
27 #include <net/if.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32
33 #include "network_information.h"
34
35 __private_extern__
36 sa_family_t nwi_af_list[2];
37
38 #define NWI_IFSTATE_FLAGS_NOT_IN_LIST 0x8
39
40 typedef uint32_t Rank;
41
42 typedef struct _nwi_ifstate {
43 char ifname[IFNAMSIZ];
44 uint64_t flags;
45 nwi_ifstate_t af_alias;
46 Rank rank;
47 int af;
48 union {
49 struct in_addr iaddr;
50 struct in6_addr iaddr6;
51 };
52 const char* diff_ch;
53 } nwi_ifstate;
54
55 /*
56 * nwi_state
57 *
58 *+---------------------------------------------+
59 *| generation_count |
60 *| |
61 *----------------------------------------------+
62 *| size |
63 *| |
64 *|---------------------------------------------+
65 *| ipv4_count |
66 *| |
67 *|---------------------------------------------+
68 *| ipv6_count |
69 *| |
70 *|---------------------------------------------+
71 *| ipv6_start |-------+
72 *| | |
73 *|---------------------------------------------+ |ipv6_start stores the index of the start of the v6 list.
74 *| ref | |
75 *| | |
76 *|---------------------------------------------+ |
77 *| IPv4 nwi_ifstates | |
78 *| |<------|-------+
79 *| ... | | |
80 *|---------------------------------------------+ | |
81 *| Sentinel nwi_ifstates | | |
82 *| flags =NWI_IFSTATE_FLAGS_RANK_NEVER) | | | af_alias points to the same ifstate in the
83 *| | | | opposite (v4 -> v6 and vice versa) af list.
84 *|---------------------------------------------+ | |
85 *| IPv6 nwi_ifstates |<------+ |
86 *| |<--------------+
87 *| ... |
88 *|---------------------------------------------+
89 *| Sentinel nwi_ifstates |
90 *| flags =NWI_IFSTATE_FLAGS_RANK_NEVER) |
91 *| |
92 *|---------------------------------------------+
93 *
94 */
95 typedef struct _nwi_state {
96 uint64_t generation_count;
97 uint32_t size;
98 uint32_t ipv4_count;
99 uint32_t ipv6_count;
100 uint32_t ipv6_start;
101 uint32_t ref;
102 nwi_ifstate nwi_ifstates[0];
103 } nwi_state;
104
105 static __inline__ int
106 uint32_cmp(uint32_t a, uint32_t b)
107 {
108 int ret;
109
110 if (a == b) {
111 ret = 0;
112 }
113 else if (a < b) {
114 ret = -1;
115 }
116 else {
117 ret = 1;
118 }
119 return (ret);
120 }
121
122 static __inline__ int
123 RankCompare(Rank a, Rank b)
124 {
125 return (uint32_cmp(a, b));
126 }
127
128 /*
129 * Function: nwi_state_get_ifstate_count
130 * Purpose:
131 * Return the number of ifstate elements for the specified address family
132 * 'af'. 'af' is either AF_INET or AF_INET6.
133 *
134 * Returns zero if there are no elements.
135 */
136 static __inline__
137 int
138 nwi_state_get_ifstate_count(nwi_state_t state, int af)
139 {
140 return (af == AF_INET)?state->ipv4_count:state->ipv6_count;
141 }
142
143 /*
144 * The ifstate list is sorted in order of decreasing priority, with the
145 * highest priority element appearing at index zero.
146 *
147 * If 'idx' is outside of the bounds of the corresponding array, returns NULL.
148 */
149 static __inline__
150 nwi_ifstate_t
151 nwi_state_get_ifstate_with_index(nwi_state_t state, int af, int idx)
152 {
153 nwi_ifstate_t nwi_ifstate = NULL;
154 int i_idx = idx;
155
156 if (idx > nwi_state_get_ifstate_count(state, af)) {
157 return (nwi_ifstate);
158 }
159
160 if (af == AF_INET6) {
161 i_idx = idx + state->ipv6_start;
162 }
163
164 return &state->nwi_ifstates[i_idx];
165 }
166
167 /*
168 * Function: nwi_state_get_ifstate_with_name
169 * Purpose:
170 * Return the ifstate for the specified ifstate for the specified address
171 * family 'af'. 'af' is either AF_INET or AF_INET6.
172 *
173 * Returns NULL if no such information exists.
174 */
175 static __inline__
176 nwi_ifstate_t
177 nwi_state_get_ifstate_with_name(nwi_state_t state,
178 int af, const char * name)
179 {
180 int idx = 0;
181 int count;
182 nwi_ifstate_t ifstate = NULL;
183
184 if (state == NULL) {
185 return ifstate;
186 }
187
188 count = (af == AF_INET)
189 ?state->ipv4_count:state->ipv6_count;
190
191
192 while (idx < count) {
193 ifstate = nwi_state_get_ifstate_with_index(state, af, idx);
194 if (ifstate == NULL) {
195 break;
196 }
197 if (strcmp(name,
198 nwi_ifstate_get_ifname(ifstate)) == 0) {
199 return (ifstate);
200 }
201 idx++;
202 }
203 return (NULL);
204 }
205
206 __private_extern__
207 nwi_state_t
208 nwi_state_new(nwi_state_t old_state, int elems);
209
210 __private_extern__
211 nwi_state_t
212 nwi_state_copy_priv(nwi_state_t old_state);
213
214 __private_extern__
215 void
216 nwi_insert_ifstate(nwi_state_t state, const char* ifname, int af,
217 uint64_t flags, Rank rank,
218 void * ifa);
219
220 __private_extern__
221 void
222 nwi_state_clear(nwi_state_t state, int af);
223
224 __private_extern__
225 void
226 nwi_state_set_last(nwi_state_t state, int af);
227
228 __private_extern__
229 nwi_state_t
230 nwi_state_diff(nwi_state_t old_state, nwi_state_t new_state);
231
232 __private_extern__
233 void *
234 nwi_ifstate_get_address(nwi_ifstate_t ifstate);
235
236 __private_extern__
237 const char *
238 nwi_ifstate_get_diff_str(nwi_ifstate_t ifstate);
239
240 __private_extern__
241 _Bool
242 _nwi_state_store(nwi_state_t state);
243
244 __private_extern__
245 nwi_state_t
246 _nwi_state_copy(void);
247
248 __private_extern__
249 void
250 _nwi_state_dump(int level, nwi_state_t state);
251
252 #endif