2 * Copyright (c) 2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _NETWORK_INFORMATION_PRIV_H_
25 #define _NETWORK_INFORMATION_PRIV_H_
30 #include <netinet/in.h>
31 #include <sys/socket.h>
33 #include "network_information.h"
36 sa_family_t nwi_af_list
[2];
38 #define NWI_IFSTATE_FLAGS_NOT_IN_LIST 0x8
40 typedef uint32_t Rank
;
42 typedef struct _nwi_ifstate
{
43 char ifname
[IFNAMSIZ
];
45 nwi_ifstate_t af_alias
;
50 struct in6_addr iaddr6
;
58 *+---------------------------------------------+
61 *----------------------------------------------+
64 *|---------------------------------------------+
67 *|---------------------------------------------+
70 *|---------------------------------------------+
71 *| ipv6_start |-------+
73 *|---------------------------------------------+ |ipv6_start stores the index of the start of the v6 list.
76 *|---------------------------------------------+ |
77 *| IPv4 nwi_ifstates | |
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 |<------+ |
88 *|---------------------------------------------+
89 *| Sentinel nwi_ifstates |
90 *| flags =NWI_IFSTATE_FLAGS_RANK_NEVER) |
92 *|---------------------------------------------+
95 typedef struct _nwi_state
{
96 uint64_t generation_count
;
102 nwi_ifstate nwi_ifstates
[0];
105 static __inline__
int
106 uint32_cmp(uint32_t a
, uint32_t b
)
122 static __inline__
int
123 RankCompare(Rank a
, Rank b
)
125 return (uint32_cmp(a
, b
));
129 * Function: nwi_state_get_ifstate_count
131 * Return the number of ifstate elements for the specified address family
132 * 'af'. 'af' is either AF_INET or AF_INET6.
134 * Returns zero if there are no elements.
138 nwi_state_get_ifstate_count(nwi_state_t state
, int af
)
140 return (af
== AF_INET
)?state
->ipv4_count
:state
->ipv6_count
;
144 * The ifstate list is sorted in order of decreasing priority, with the
145 * highest priority element appearing at index zero.
147 * If 'idx' is outside of the bounds of the corresponding array, returns NULL.
151 nwi_state_get_ifstate_with_index(nwi_state_t state
, int af
, int idx
)
153 nwi_ifstate_t nwi_ifstate
= NULL
;
156 if (idx
> nwi_state_get_ifstate_count(state
, af
)) {
157 return (nwi_ifstate
);
160 if (af
== AF_INET6
) {
161 i_idx
= idx
+ state
->ipv6_start
;
164 return &state
->nwi_ifstates
[i_idx
];
168 * Function: nwi_state_get_ifstate_with_name
170 * Return the ifstate for the specified ifstate for the specified address
171 * family 'af'. 'af' is either AF_INET or AF_INET6.
173 * Returns NULL if no such information exists.
177 nwi_state_get_ifstate_with_name(nwi_state_t state
,
178 int af
, const char * name
)
182 nwi_ifstate_t ifstate
= NULL
;
188 count
= (af
== AF_INET
)
189 ?state
->ipv4_count
:state
->ipv6_count
;
192 while (idx
< count
) {
193 ifstate
= nwi_state_get_ifstate_with_index(state
, af
, idx
);
194 if (ifstate
== NULL
) {
198 nwi_ifstate_get_ifname(ifstate
)) == 0) {
208 nwi_state_new(nwi_state_t old_state
, int elems
);
212 nwi_state_copy_priv(nwi_state_t old_state
);
216 nwi_insert_ifstate(nwi_state_t state
, const char* ifname
, int af
,
217 uint64_t flags
, Rank rank
,
222 nwi_state_clear(nwi_state_t state
, int af
);
226 nwi_state_set_last(nwi_state_t state
, int af
);
230 nwi_state_diff(nwi_state_t old_state
, nwi_state_t new_state
);
234 nwi_ifstate_get_address(nwi_ifstate_t ifstate
);
238 nwi_ifstate_get_diff_str(nwi_ifstate_t ifstate
);
242 _nwi_state_store(nwi_state_t state
);
246 _nwi_state_copy(void);
250 _nwi_state_dump(int level
, nwi_state_t state
);