2 * Copyright (c) 2000-2002 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
)
52 CFDictionaryRef dict
= NULL
;
54 CFStringRef name
= NULL
;
55 Boolean tempSession
= FALSE
;
58 store
= SCDynamicStoreCreate(NULL
,
59 CFSTR("SCDynamicStoreCopyComputerName"),
63 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
69 key
= SCDynamicStoreKeyCreateComputerName(NULL
);
70 dict
= SCDynamicStoreCopyValue(store
, key
);
75 if (!isA_CFDictionary(dict
)) {
76 _SCErrorSet(kSCStatusNoKey
);
80 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropSystemComputerName
));
82 _SCErrorSet(kSCStatusNoKey
);
90 num
= CFDictionaryGetValue(dict
,
91 kSCPropSystemComputerNameEncoding
);
92 if (isA_CFNumber(num
)) {
93 CFNumberGetValue(num
, kCFNumberIntType
, nameEncoding
);
95 *nameEncoding
= CFStringGetSystemEncoding();
101 if (tempSession
) CFRelease(store
);
102 if (dict
) CFRelease(dict
);
108 SCPreferencesSetComputerName(SCPreferencesRef session
,
110 CFStringEncoding encoding
)
112 CFDictionaryRef dict
;
113 CFMutableDictionaryRef newDict
= NULL
;
116 CFStringRef path
= NULL
;
118 if (!isA_CFString(name
)) {
119 _SCErrorSet(kSCStatusInvalidArgument
);
123 path
= CFStringCreateWithFormat(NULL
,
129 dict
= SCPreferencesPathGetValue(session
, path
);
131 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
133 newDict
= CFDictionaryCreateMutable(NULL
,
135 &kCFTypeDictionaryKeyCallBacks
,
136 &kCFTypeDictionaryValueCallBacks
);
139 CFDictionarySetValue(newDict
, kSCPropSystemComputerName
, name
);
141 num
= CFNumberCreate(NULL
, kCFNumberIntType
, &encoding
);
142 CFDictionarySetValue(newDict
, kSCPropSystemComputerNameEncoding
, num
);
145 ok
= SCPreferencesPathSetValue(session
, path
, newDict
);
147 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesPathSetValue() failed"));
150 if (path
) CFRelease(path
);
151 if (newDict
) CFRelease(newDict
);
158 SCDynamicStoreKeyCreateHostNames(CFAllocatorRef allocator
)
160 return SCDynamicStoreKeyCreate(allocator
,
162 kSCDynamicStoreDomainSetup
,
169 SCDynamicStoreCopyLocalHostName(SCDynamicStoreRef store
)
171 CFDictionaryRef dict
= NULL
;
173 CFStringRef name
= NULL
;
174 Boolean tempSession
= FALSE
;
177 store
= SCDynamicStoreCreate(NULL
,
178 CFSTR("SCDynamicStoreCopyLocalHostName"),
182 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
188 key
= SCDynamicStoreKeyCreateHostNames(NULL
);
189 dict
= SCDynamicStoreCopyValue(store
, key
);
194 if (!isA_CFDictionary(dict
)) {
195 _SCErrorSet(kSCStatusNoKey
);
199 name
= isA_CFString(CFDictionaryGetValue(dict
, kSCPropNetLocalHostName
));
201 _SCErrorSet(kSCStatusNoKey
);
208 if (tempSession
) CFRelease(store
);
209 if (dict
) CFRelease(dict
);
215 _SC_stringIsValidDNSName(const char *name
)
218 int len
= strlen(name
);
226 for (scan
= name
, i
= 0; i
< len
; i
++, scan
++) {
228 char next
= *(scan
+ 1);
230 if (prev
== '.' || prev
== '\0') {
231 if (isalnum(ch
) == 0) {
232 /* a label must begin with a letter or digit */
235 } else if (next
== '\0' || next
== '.') {
236 if (isalnum(ch
) == 0) {
237 /* a label must end with a letter or digit */
240 } else if (isalnum(ch
) == 0) {
244 if (prev
== '.' || prev
== '-') {
245 /* a label cannot begin or end with a hyphen */
250 /* an invalid character */
262 _SC_CFStringIsValidDNSName(CFStringRef name
)
264 Boolean clean
= FALSE
;
267 if (!isA_CFString(name
)) {
271 str
= _SC_cfstring_to_cstring(name
, NULL
, 0, kCFStringEncodingASCII
);
276 clean
= _SC_stringIsValidDNSName(str
);
278 if (str
) CFAllocatorDeallocate(NULL
, str
);
284 SCPreferencesSetLocalHostName(SCPreferencesRef session
,
287 CFDictionaryRef dict
;
288 CFMutableDictionaryRef newDict
= NULL
;
290 CFStringRef path
= NULL
;
295 if (!isA_CFString(name
)) {
296 _SCErrorSet(kSCStatusInvalidArgument
);
300 len
= CFStringGetLength(name
);
302 if (!_SC_CFStringIsValidDNSName(name
)) {
303 _SCErrorSet(kSCStatusInvalidArgument
);
307 if (CFStringFindWithOptions(name
, CFSTR("."), CFRangeMake(0, len
), 0, NULL
)) {
308 _SCErrorSet(kSCStatusInvalidArgument
);
316 path
= CFStringCreateWithFormat(NULL
,
323 dict
= SCPreferencesPathGetValue(session
, path
);
325 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
327 newDict
= CFDictionaryCreateMutable(NULL
,
329 &kCFTypeDictionaryKeyCallBacks
,
330 &kCFTypeDictionaryValueCallBacks
);
334 CFDictionarySetValue(newDict
, kSCPropNetLocalHostName
, name
);
336 CFDictionaryRemoveValue(newDict
, kSCPropNetLocalHostName
);
339 if (CFDictionaryGetCount(newDict
) > 0) {
340 ok
= SCPreferencesPathSetValue(session
, path
, newDict
);
342 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesPathSetValue() failed"));
345 ok
= SCPreferencesPathRemoveValue(session
, path
);
347 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCPreferencesPathRemoveValue() failed"));
351 if (path
) CFRelease(path
);
352 if (newDict
) CFRelease(newDict
);