2 * Copyright (c) 2000-2007 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@
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>
35 #include <CoreFoundation/CoreFoundation.h>
36 #include <CoreFoundation/CFStringDefaultEncoding.h> // for __CFStringGetUserDefaultEncoding
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCValidation.h>
39 #include <SystemConfiguration/SCPrivate.h>
42 #pragma mark ComputerName
46 SCDynamicStoreKeyCreateComputerName(CFAllocatorRef allocator
)
48 return SCDynamicStoreKeyCreate(allocator
,
50 kSCDynamicStoreDomainSetup
,
56 SCDynamicStoreCopyComputerName(SCDynamicStoreRef store
,
57 CFStringEncoding
*nameEncoding
)
61 CFStringRef name
= NULL
;
62 Boolean tempSession
= FALSE
;
65 store
= SCDynamicStoreCreate(NULL
,
66 CFSTR("SCDynamicStoreCopyComputerName"),
75 key
= SCDynamicStoreKeyCreateComputerName(NULL
);
76 dict
= SCDynamicStoreCopyValue(store
, key
);
81 if (!isA_CFDictionary(dict
)) {
82 _SCErrorSet(kSCStatusNoKey
);
86 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropSystemComputerName
));
88 _SCErrorSet(kSCStatusNoKey
);
93 if (nameEncoding
!= NULL
) {
96 num
= CFDictionaryGetValue(dict
,
97 kSCPropSystemComputerNameEncoding
);
98 if (isA_CFNumber(num
)) {
99 CFNumberGetValue(num
, kCFNumberIntType
, nameEncoding
);
101 *nameEncoding
= CFStringGetSystemEncoding();
107 if (tempSession
) CFRelease(store
);
108 if (dict
!= NULL
) CFRelease(dict
);
114 SCPreferencesSetComputerName(SCPreferencesRef prefs
,
116 CFStringEncoding encoding
)
118 CFDictionaryRef dict
;
119 CFMutableDictionaryRef newDict
;
124 if (!isA_CFString(name
)) {
125 _SCErrorSet(kSCStatusInvalidArgument
);
129 path
= CFStringCreateWithFormat(NULL
,
135 dict
= SCPreferencesPathGetValue(prefs
, path
);
137 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
139 newDict
= CFDictionaryCreateMutable(NULL
,
141 &kCFTypeDictionaryKeyCallBacks
,
142 &kCFTypeDictionaryValueCallBacks
);
145 CFDictionarySetValue(newDict
, kSCPropSystemComputerName
, name
);
147 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &encoding
);
148 CFDictionarySetValue(newDict
, kSCPropSystemComputerNameEncoding
, num
);
151 CFDictionaryRemoveValue(newDict
, kSCPropSystemComputerNameRegion
);
152 if (encoding
== kCFStringEncodingMacRoman
) {
153 UInt32 userEncoding
= 0;
154 UInt32 userRegion
= 0;
156 __CFStringGetUserDefaultEncoding(&userEncoding
, &userRegion
);
157 if ((userEncoding
== kCFStringEncodingMacRoman
) && (userRegion
!= 0)) {
158 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &userRegion
);
159 CFDictionarySetValue(newDict
, kSCPropSystemComputerNameRegion
, num
);
164 ok
= SCPreferencesPathSetValue(prefs
, path
, newDict
);
174 #pragma mark HostName
178 SCPreferencesGetHostName(SCPreferencesRef prefs
)
180 CFDictionaryRef dict
;
184 path
= CFStringCreateWithFormat(NULL
,
189 dict
= SCPreferencesPathGetValue(prefs
, path
);
192 if (!isA_CFDictionary(dict
)) {
193 _SCErrorSet(kSCStatusNoKey
);
197 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropSystemHostName
));
199 _SCErrorSet(kSCStatusNoKey
);
208 SCPreferencesSetHostName(SCPreferencesRef prefs
,
211 CFDictionaryRef dict
;
212 CFMutableDictionaryRef newDict
;
219 if (!isA_CFString(name
)) {
220 _SCErrorSet(kSCStatusInvalidArgument
);
224 len
= CFStringGetLength(name
);
230 path
= CFStringCreateWithFormat(NULL
,
236 dict
= SCPreferencesPathGetValue(prefs
, path
);
238 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
240 newDict
= CFDictionaryCreateMutable(NULL
,
242 &kCFTypeDictionaryKeyCallBacks
,
243 &kCFTypeDictionaryValueCallBacks
);
247 CFDictionarySetValue(newDict
, kSCPropSystemHostName
, name
);
249 CFDictionaryRemoveValue(newDict
, kSCPropSystemHostName
);
252 if (CFDictionaryGetCount(newDict
) > 0) {
253 ok
= SCPreferencesPathSetValue(prefs
, path
, newDict
);
255 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
266 #pragma mark LocalHostName
270 SCDynamicStoreKeyCreateHostNames(CFAllocatorRef allocator
)
272 return SCDynamicStoreKeyCreate(allocator
,
274 kSCDynamicStoreDomainSetup
,
281 SCDynamicStoreCopyLocalHostName(SCDynamicStoreRef store
)
283 CFDictionaryRef dict
;
285 CFStringRef name
= NULL
;
286 Boolean tempSession
= FALSE
;
289 store
= SCDynamicStoreCreate(NULL
,
290 CFSTR("SCDynamicStoreCopyLocalHostName"),
299 key
= SCDynamicStoreKeyCreateHostNames(NULL
);
300 dict
= SCDynamicStoreCopyValue(store
, key
);
305 if (!isA_CFDictionary(dict
)) {
306 _SCErrorSet(kSCStatusNoKey
);
310 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropNetLocalHostName
));
312 _SCErrorSet(kSCStatusNoKey
);
319 if (tempSession
) CFRelease(store
);
320 if (dict
!= NULL
) CFRelease(dict
);
326 _SC_stringIsValidDNSName(const char *name
)
329 int len
= strlen(name
);
337 for (scan
= name
, i
= 0; i
< len
; i
++, scan
++) {
339 char next
= *(scan
+ 1);
341 if (prev
== '.' || prev
== '\0') {
342 if (isalnum(ch
) == 0) {
343 /* a label must begin with a letter or digit */
346 } else if (next
== '\0' || next
== '.') {
347 if (isalnum(ch
) == 0) {
348 /* a label must end with a letter or digit */
351 } else if (isalnum(ch
) == 0) {
355 if (prev
== '.' || prev
== '-') {
356 /* a label cannot begin or end with a hyphen */
361 /* an invalid character */
374 _SC_CFStringIsValidDNSName(CFStringRef name
)
376 Boolean clean
= FALSE
;
379 if (!isA_CFString(name
)) {
383 str
= _SC_cfstring_to_cstring(name
, NULL
, 0, kCFStringEncodingASCII
);
388 clean
= _SC_stringIsValidDNSName(str
);
390 if (str
!= NULL
) CFAllocatorDeallocate(NULL
, str
);
396 SCPreferencesSetLocalHostName(SCPreferencesRef prefs
,
399 CFDictionaryRef dict
;
400 CFMutableDictionaryRef newDict
;
407 if (!isA_CFString(name
)) {
408 _SCErrorSet(kSCStatusInvalidArgument
);
412 len
= CFStringGetLength(name
);
414 if (!_SC_CFStringIsValidDNSName(name
)) {
415 _SCErrorSet(kSCStatusInvalidArgument
);
419 if (CFStringFindWithOptions(name
, CFSTR("."), CFRangeMake(0, len
), 0, NULL
)) {
420 _SCErrorSet(kSCStatusInvalidArgument
);
428 path
= CFStringCreateWithFormat(NULL
,
435 dict
= SCPreferencesPathGetValue(prefs
, path
);
437 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
439 newDict
= CFDictionaryCreateMutable(NULL
,
441 &kCFTypeDictionaryKeyCallBacks
,
442 &kCFTypeDictionaryValueCallBacks
);
446 CFDictionarySetValue(newDict
, kSCPropNetLocalHostName
, name
);
448 CFDictionaryRemoveValue(newDict
, kSCPropNetLocalHostName
);
451 if (CFDictionaryGetCount(newDict
) > 0) {
452 ok
= SCPreferencesPathSetValue(prefs
, path
, newDict
);
454 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
465 _SC_CFStringIsValidNetBIOSName(CFStringRef name
)
467 if (!isA_CFString(name
)) {
471 if (CFStringGetLength(name
) > 15) {