2 * Copyright (c) 2006 Apple Computer, 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@
25 * SCNetworkSignature.c
26 * - implementation of SCNetworkSignatureRef API that allows access to
27 network identification information
31 * Modification History
33 * November 6, 2006 Dieter Siegmund (dieter@apple.com)
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"
49 const char * kSCNetworkSignatureActiveChangedNotifyName
= NETWORK_ID_KEY
".active";
52 #pragma mark SCNetworkSignature support routines
54 static __inline__ SCDynamicStoreRef
55 store_create(CFAllocatorRef alloc
)
57 return (SCDynamicStoreCreate(alloc
, CFSTR("SCNetworkSignature"),
61 static CFDictionaryRef
62 store_copy_id_dict(CFAllocatorRef alloc
, SCDynamicStoreRef store
)
64 CFDictionaryRef id_dict
= NULL
;
65 Boolean release_store
= FALSE
;
68 store
= store_create(alloc
);
74 id_dict
= SCDynamicStoreCopyValue(store
,
75 kSCNetworkIdentificationStoreKey
);
76 if (isA_CFDictionary(id_dict
) == NULL
) {
77 if (id_dict
!= NULL
) {
92 #pragma mark SCNetworkSignature APIs
95 SCNetworkSignatureCopyActiveIdentifierForAddress(CFAllocatorRef alloc
,
96 const struct sockaddr
* addr
)
98 CFDictionaryRef id_dict
= NULL
;
99 CFStringRef ident
= NULL
;
100 struct sockaddr_in
* sin_p
;
103 /* only accept 0.0.0.0 (i.e. default) for now */
104 sin_p
= (struct sockaddr_in
*)addr
;
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
);
112 id_dict
= store_copy_id_dict(alloc
, NULL
);
113 if (id_dict
== NULL
) {
114 _SCErrorSet(kSCStatusFailed
);
117 ident
= CFDictionaryGetValue(id_dict
, kStoreKeyPrimaryIPv4Identifier
);
118 if (isA_CFString(ident
) != NULL
) {
122 _SCErrorSet(kSCStatusFailed
);
125 if (id_dict
!= NULL
) {
131 CFArrayRef
/* of CFStringRef's */
132 SCNetworkSignatureCopyActiveIdentifiers(CFAllocatorRef alloc
)
134 CFArrayRef active
= NULL
;
137 CFDictionaryRef id_dict
= NULL
;
139 id_dict
= store_copy_id_dict(alloc
, NULL
);
140 if (id_dict
== NULL
) {
143 active
= CFDictionaryGetValue(id_dict
, kStoreKeyActiveIdentifiers
);
144 if (isA_CFArray(active
) != NULL
) {
145 count
= CFArrayGetCount(active
);
151 for (i
= 0; i
< count
; i
++) {
152 CFStringRef ident
= CFArrayGetValueAtIndex(active
, i
);
154 if (isA_CFString(ident
) == NULL
) {
162 if (id_dict
!= NULL
) {
165 if (active
== NULL
) {
166 _SCErrorSet(kSCStatusFailed
);