2 * Copyright (c) 2000-2005 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 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * January 8, 2001 Allan Nathanson <ajn@apple.com>
34 #include <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCValidation.h>
36 #include <SystemConfiguration/SCPrivate.h>
39 SCDynamicStoreKeyCreateComputerName(CFAllocatorRef allocator
)
41 return SCDynamicStoreKeyCreate(allocator
,
43 kSCDynamicStoreDomainSetup
,
49 SCDynamicStoreCopyComputerName(SCDynamicStoreRef store
,
50 CFStringEncoding
*nameEncoding
)
54 CFStringRef name
= NULL
;
55 Boolean tempSession
= FALSE
;
58 store
= SCDynamicStoreCreate(NULL
,
59 CFSTR("SCDynamicStoreCopyComputerName"),
68 key
= SCDynamicStoreKeyCreateComputerName(NULL
);
69 dict
= SCDynamicStoreCopyValue(store
, key
);
74 if (!isA_CFDictionary(dict
)) {
75 _SCErrorSet(kSCStatusNoKey
);
79 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropSystemComputerName
));
81 _SCErrorSet(kSCStatusNoKey
);
86 if (nameEncoding
!= NULL
) {
89 num
= CFDictionaryGetValue(dict
,
90 kSCPropSystemComputerNameEncoding
);
91 if (isA_CFNumber(num
)) {
92 CFNumberGetValue(num
, kCFNumberIntType
, nameEncoding
);
94 *nameEncoding
= CFStringGetSystemEncoding();
100 if (tempSession
) CFRelease(store
);
101 if (dict
!= NULL
) CFRelease(dict
);
107 SCPreferencesSetComputerName(SCPreferencesRef prefs
,
109 CFStringEncoding encoding
)
111 CFDictionaryRef dict
;
112 CFMutableDictionaryRef newDict
;
117 if (!isA_CFString(name
)) {
118 _SCErrorSet(kSCStatusInvalidArgument
);
122 path
= CFStringCreateWithFormat(NULL
,
128 dict
= SCPreferencesPathGetValue(prefs
, path
);
130 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
132 newDict
= CFDictionaryCreateMutable(NULL
,
134 &kCFTypeDictionaryKeyCallBacks
,
135 &kCFTypeDictionaryValueCallBacks
);
138 CFDictionarySetValue(newDict
, kSCPropSystemComputerName
, name
);
140 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &encoding
);
141 CFDictionarySetValue(newDict
, kSCPropSystemComputerNameEncoding
, num
);
144 ok
= SCPreferencesPathSetValue(prefs
, path
, newDict
);
153 #ifndef kSCPropNetHostName
154 #define kSCPropNetHostName CFSTR("HostName")
159 SCPreferencesGetHostName(SCPreferencesRef prefs
)
161 CFDictionaryRef dict
;
165 path
= CFStringCreateWithFormat(NULL
,
170 dict
= SCPreferencesPathGetValue(prefs
, path
);
173 if (!isA_CFDictionary(dict
)) {
174 _SCErrorSet(kSCStatusNoKey
);
178 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropNetHostName
));
180 _SCErrorSet(kSCStatusNoKey
);
189 SCPreferencesSetHostName(SCPreferencesRef prefs
,
192 CFDictionaryRef dict
;
193 CFMutableDictionaryRef newDict
;
200 if (!isA_CFString(name
)) {
201 _SCErrorSet(kSCStatusInvalidArgument
);
205 len
= CFStringGetLength(name
);
211 path
= CFStringCreateWithFormat(NULL
,
217 dict
= SCPreferencesPathGetValue(prefs
, path
);
219 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
221 newDict
= CFDictionaryCreateMutable(NULL
,
223 &kCFTypeDictionaryKeyCallBacks
,
224 &kCFTypeDictionaryValueCallBacks
);
228 CFDictionarySetValue(newDict
, kSCPropNetHostName
, name
);
230 CFDictionaryRemoveValue(newDict
, kSCPropNetHostName
);
233 if (CFDictionaryGetCount(newDict
) > 0) {
234 ok
= SCPreferencesPathSetValue(prefs
, path
, newDict
);
236 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
247 SCDynamicStoreKeyCreateHostNames(CFAllocatorRef allocator
)
249 return SCDynamicStoreKeyCreate(allocator
,
251 kSCDynamicStoreDomainSetup
,
258 SCDynamicStoreCopyLocalHostName(SCDynamicStoreRef store
)
260 CFDictionaryRef dict
;
262 CFStringRef name
= NULL
;
263 Boolean tempSession
= FALSE
;
266 store
= SCDynamicStoreCreate(NULL
,
267 CFSTR("SCDynamicStoreCopyLocalHostName"),
276 key
= SCDynamicStoreKeyCreateHostNames(NULL
);
277 dict
= SCDynamicStoreCopyValue(store
, key
);
282 if (!isA_CFDictionary(dict
)) {
283 _SCErrorSet(kSCStatusNoKey
);
287 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropNetLocalHostName
));
289 _SCErrorSet(kSCStatusNoKey
);
296 if (tempSession
) CFRelease(store
);
297 if (dict
!= NULL
) CFRelease(dict
);
303 _SC_stringIsValidDNSName(const char *name
)
306 int len
= strlen(name
);
314 for (scan
= name
, i
= 0; i
< len
; i
++, scan
++) {
316 char next
= *(scan
+ 1);
318 if (prev
== '.' || prev
== '\0') {
319 if (isalnum(ch
) == 0) {
320 /* a label must begin with a letter or digit */
323 } else if (next
== '\0' || next
== '.') {
324 if (isalnum(ch
) == 0) {
325 /* a label must end with a letter or digit */
328 } else if (isalnum(ch
) == 0) {
332 if (prev
== '.' || prev
== '-') {
333 /* a label cannot begin or end with a hyphen */
338 /* an invalid character */
350 _SC_CFStringIsValidDNSName(CFStringRef name
)
352 Boolean clean
= FALSE
;
355 if (!isA_CFString(name
)) {
359 str
= _SC_cfstring_to_cstring(name
, NULL
, 0, kCFStringEncodingASCII
);
364 clean
= _SC_stringIsValidDNSName(str
);
366 if (str
!= NULL
) CFAllocatorDeallocate(NULL
, str
);
372 SCPreferencesSetLocalHostName(SCPreferencesRef prefs
,
375 CFDictionaryRef dict
;
376 CFMutableDictionaryRef newDict
;
383 if (!isA_CFString(name
)) {
384 _SCErrorSet(kSCStatusInvalidArgument
);
388 len
= CFStringGetLength(name
);
390 if (!_SC_CFStringIsValidDNSName(name
)) {
391 _SCErrorSet(kSCStatusInvalidArgument
);
395 if (CFStringFindWithOptions(name
, CFSTR("."), CFRangeMake(0, len
), 0, NULL
)) {
396 _SCErrorSet(kSCStatusInvalidArgument
);
404 path
= CFStringCreateWithFormat(NULL
,
411 dict
= SCPreferencesPathGetValue(prefs
, path
);
413 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
415 newDict
= CFDictionaryCreateMutable(NULL
,
417 &kCFTypeDictionaryKeyCallBacks
,
418 &kCFTypeDictionaryValueCallBacks
);
422 CFDictionarySetValue(newDict
, kSCPropNetLocalHostName
, name
);
424 CFDictionaryRemoveValue(newDict
, kSCPropNetLocalHostName
);
427 if (CFDictionaryGetCount(newDict
) > 0) {
428 ok
= SCPreferencesPathSetValue(prefs
, path
, newDict
);
430 ok
= SCPreferencesPathRemoveValue(prefs
, path
);