2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * January 8, 2001 Allan Nathanson <ajn@apple.com>
36 #include <SystemConfiguration/SystemConfiguration.h>
37 #include <SystemConfiguration/SCValidation.h>
38 #include <SystemConfiguration/SCPrivate.h>
41 SCDynamicStoreKeyCreateComputerName(CFAllocatorRef allocator
)
43 return SCDynamicStoreKeyCreate(allocator
,
45 kSCDynamicStoreDomainSetup
,
51 SCDynamicStoreCopyComputerName(SCDynamicStoreRef store
,
52 CFStringEncoding
*nameEncoding
)
54 CFDictionaryRef dict
= NULL
;
56 CFStringRef name
= NULL
;
57 Boolean tempSession
= FALSE
;
60 store
= SCDynamicStoreCreate(NULL
,
61 CFSTR("SCDynamicStoreCopyComputerName"),
65 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
71 key
= SCDynamicStoreKeyCreateComputerName(NULL
);
72 dict
= SCDynamicStoreCopyValue(store
, key
);
77 if (!isA_CFDictionary(dict
)) {
78 _SCErrorSet(kSCStatusNoKey
);
82 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropSystemComputerName
));
84 _SCErrorSet(kSCStatusNoKey
);
92 num
= CFDictionaryGetValue(dict
,
93 kSCPropSystemComputerNameEncoding
);
94 if (isA_CFNumber(num
)) {
95 CFNumberGetValue(num
, kCFNumberIntType
, nameEncoding
);
97 *nameEncoding
= CFStringGetSystemEncoding();
103 if (tempSession
) CFRelease(store
);
104 if (dict
) CFRelease(dict
);
110 SCPreferencesSetComputerName(SCPreferencesRef session
,
112 CFStringEncoding encoding
)
114 CFDictionaryRef dict
;
115 CFMutableDictionaryRef newDict
= NULL
;
118 CFStringRef path
= NULL
;
120 if (!isA_CFString(name
)) {
121 _SCErrorSet(kSCStatusInvalidArgument
);
125 path
= CFStringCreateWithFormat(NULL
,
131 dict
= SCPreferencesPathGetValue(session
, path
);
133 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
135 newDict
= CFDictionaryCreateMutable(NULL
,
137 &kCFTypeDictionaryKeyCallBacks
,
138 &kCFTypeDictionaryValueCallBacks
);
141 CFDictionarySetValue(newDict
, kSCPropSystemComputerName
, name
);
143 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &encoding
);
144 CFDictionarySetValue(newDict
, kSCPropSystemComputerNameEncoding
, num
);
147 ok
= SCPreferencesPathSetValue(session
, path
, newDict
);
149 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesPathSetValue() failed"));
152 if (path
) CFRelease(path
);
153 if (newDict
) CFRelease(newDict
);
160 SCDynamicStoreKeyCreateHostNames(CFAllocatorRef allocator
)
162 return SCDynamicStoreKeyCreate(allocator
,
164 kSCDynamicStoreDomainSetup
,
171 SCDynamicStoreCopyLocalHostName(SCDynamicStoreRef store
)
173 CFDictionaryRef dict
= NULL
;
175 CFStringRef name
= NULL
;
176 Boolean tempSession
= FALSE
;
179 store
= SCDynamicStoreCreate(NULL
,
180 CFSTR("SCDynamicStoreCopyLocalHostName"),
184 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
190 key
= SCDynamicStoreKeyCreateHostNames(NULL
);
191 dict
= SCDynamicStoreCopyValue(store
, key
);
196 if (!isA_CFDictionary(dict
)) {
197 _SCErrorSet(kSCStatusNoKey
);
201 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropNetLocalHostName
));
203 _SCErrorSet(kSCStatusNoKey
);
210 if (tempSession
) CFRelease(store
);
211 if (dict
) CFRelease(dict
);
217 _SC_stringIsValidDNSName(const char *name
)
220 int len
= strlen(name
);
228 for (scan
= name
, i
= 0; i
< len
; i
++, scan
++) {
230 char next
= *(scan
+ 1);
232 if (prev
== '.' || prev
== '\0') {
233 if (isalnum(ch
) == 0) {
234 /* a label must begin with a letter or digit */
237 } else if (next
== '\0' || next
== '.') {
238 if (isalnum(ch
) == 0) {
239 /* a label must end with a letter or digit */
242 } else if (isalnum(ch
) == 0) {
246 if (prev
== '.' || prev
== '-') {
247 /* a label cannot begin or end with a hyphen */
252 /* an invalid character */
264 _SC_CFStringIsValidDNSName(CFStringRef name
)
266 Boolean clean
= FALSE
;
269 if (!isA_CFString(name
)) {
273 str
= _SC_cfstring_to_cstring(name
, NULL
, 0, kCFStringEncodingASCII
);
278 clean
= _SC_stringIsValidDNSName(str
);
280 if (str
) CFAllocatorDeallocate(NULL
, str
);
286 SCPreferencesSetLocalHostName(SCPreferencesRef session
,
289 CFDictionaryRef dict
;
290 CFMutableDictionaryRef newDict
= NULL
;
292 CFStringRef path
= NULL
;
297 if (!isA_CFString(name
)) {
298 _SCErrorSet(kSCStatusInvalidArgument
);
302 len
= CFStringGetLength(name
);
304 if (!_SC_CFStringIsValidDNSName(name
)) {
305 _SCErrorSet(kSCStatusInvalidArgument
);
309 if (CFStringFindWithOptions(name
, CFSTR("."), CFRangeMake(0, len
), 0, NULL
)) {
310 _SCErrorSet(kSCStatusInvalidArgument
);
318 path
= CFStringCreateWithFormat(NULL
,
325 dict
= SCPreferencesPathGetValue(session
, path
);
327 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
329 newDict
= CFDictionaryCreateMutable(NULL
,
331 &kCFTypeDictionaryKeyCallBacks
,
332 &kCFTypeDictionaryValueCallBacks
);
336 CFDictionarySetValue(newDict
, kSCPropNetLocalHostName
, name
);
338 CFDictionaryRemoveValue(newDict
, kSCPropNetLocalHostName
);
341 if (CFDictionaryGetCount(newDict
) > 0) {
342 ok
= SCPreferencesPathSetValue(session
, path
, newDict
);
344 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesPathSetValue() failed"));
347 ok
= SCPreferencesPathRemoveValue(session
, path
);
349 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesPathRemoveValue() failed"));
353 if (path
) CFRelease(path
);
354 if (newDict
) CFRelease(newDict
);