]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCNetworkSignature.c
configd-293.4.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCNetworkSignature.c
1 /*
2 * Copyright (c) 2006 Apple Computer, 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 /*
25 * SCNetworkSignature.c
26 * - implementation of SCNetworkSignatureRef API that allows access to
27 network identification information
28 *
29 */
30 /*
31 * Modification History
32 *
33 * November 6, 2006 Dieter Siegmund (dieter@apple.com)
34 * - initial revision
35 */
36
37
38 #include <netinet/in.h>
39 #include <CoreFoundation/CFDictionary.h>
40 #include <CoreFoundation/CFString.h>
41 #include <CoreFoundation/CFArray.h>
42 #include <CoreFoundation/CFRuntime.h>
43 #include <SystemConfiguration/SCDynamicStore.h>
44 #include <SystemConfiguration/SCValidation.h>
45 #include <SystemConfiguration/SCPrivate.h>
46 #include "SCNetworkSignature.h"
47 #include "SCNetworkSignaturePrivate.h"
48
49 const char * kSCNetworkSignatureActiveChangedNotifyName = NETWORK_ID_KEY ".active";
50
51
52 #pragma mark SCNetworkSignature support routines
53
54 static __inline__ SCDynamicStoreRef
55 store_create(CFAllocatorRef alloc)
56 {
57 return (SCDynamicStoreCreate(alloc, CFSTR("SCNetworkSignature"),
58 NULL, NULL));
59 }
60
61 static CFDictionaryRef
62 store_copy_id_dict(CFAllocatorRef alloc, SCDynamicStoreRef store)
63 {
64 CFDictionaryRef id_dict = NULL;
65 Boolean release_store = FALSE;
66
67 if (store == NULL) {
68 store = store_create(alloc);
69 if (store == NULL) {
70 goto done;
71 }
72 release_store = TRUE;
73 }
74 id_dict = SCDynamicStoreCopyValue(store,
75 kSCNetworkIdentificationStoreKey);
76 if (isA_CFDictionary(id_dict) == NULL) {
77 if (id_dict != NULL) {
78 CFRelease(id_dict);
79 id_dict = NULL;
80 }
81 goto done;
82 }
83 done:
84 if (release_store) {
85 CFRelease(store);
86 }
87 return (id_dict);
88 }
89
90 #pragma -
91
92 #pragma mark SCNetworkSignature APIs
93
94 CFStringRef
95 SCNetworkSignatureCopyActiveIdentifierForAddress(CFAllocatorRef alloc,
96 const struct sockaddr * addr)
97 {
98 CFDictionaryRef id_dict = NULL;
99 CFStringRef ident = NULL;
100 struct sockaddr_in * sin_p;
101
102
103 /* only accept 0.0.0.0 (i.e. default) for now */
104 sin_p = (struct sockaddr_in *)addr;
105 if (addr == NULL
106 || addr->sa_family != AF_INET
107 || addr->sa_len != sizeof(struct sockaddr_in)
108 || sin_p->sin_addr.s_addr != 0) {
109 _SCErrorSet(kSCStatusInvalidArgument);
110 goto done;
111 }
112 id_dict = store_copy_id_dict(alloc, NULL);
113 if (id_dict == NULL) {
114 _SCErrorSet(kSCStatusFailed);
115 goto done;
116 }
117 ident = CFDictionaryGetValue(id_dict, kStoreKeyPrimaryIPv4Identifier);
118 if (isA_CFString(ident) != NULL) {
119 CFRetain(ident);
120 }
121 else {
122 _SCErrorSet(kSCStatusFailed);
123 }
124 done:
125 if (id_dict != NULL) {
126 CFRelease(id_dict);
127 }
128 return (ident);
129 }
130
131 CFArrayRef /* of CFStringRef's */
132 SCNetworkSignatureCopyActiveIdentifiers(CFAllocatorRef alloc)
133 {
134 CFArrayRef active = NULL;
135 int i;
136 int count = 0;
137 CFDictionaryRef id_dict = NULL;
138
139 id_dict = store_copy_id_dict(alloc, NULL);
140 if (id_dict == NULL) {
141 goto done;
142 }
143 active = CFDictionaryGetValue(id_dict, kStoreKeyActiveIdentifiers);
144 if (isA_CFArray(active) != NULL) {
145 count = CFArrayGetCount(active);
146 }
147 if (count == 0) {
148 active = NULL;
149 goto done;
150 }
151 for (i = 0; i < count; i++) {
152 CFStringRef ident = CFArrayGetValueAtIndex(active, i);
153
154 if (isA_CFString(ident) == NULL) {
155 active = NULL;
156 goto done;
157 }
158 }
159 CFRetain(active);
160
161 done:
162 if (id_dict != NULL) {
163 CFRelease(id_dict);
164 }
165 if (active == NULL) {
166 _SCErrorSet(kSCStatusFailed);
167 }
168 return (active);
169 }
170
171 #pragma mark -