+static CFStringRef
+copy_default_set_name(Boolean loc)
+{
+ CFStringRef name;
+ static CFStringRef non_localized = NULL;
+ static CFStringRef localized = NULL;
+
+ if (!loc) {
+ static dispatch_once_t once;
+
+ dispatch_once(&once, ^{
+ CFBundleRef bundle;
+
+ bundle = _SC_CFBundleGet();
+ if (bundle != NULL) {
+ non_localized = _SC_CFBundleCopyNonLocalizedString(bundle,
+ CFSTR("DEFAULT_SET_NAME"),
+ DEFAULT_SET_NAME,
+ NULL);
+ }
+ });
+ name = non_localized;
+ } else {
+ static dispatch_once_t once;
+
+ dispatch_once(&once, ^{
+ CFBundleRef bundle;
+
+ bundle = _SC_CFBundleGet();
+ if (bundle != NULL) {
+ localized = CFBundleCopyLocalizedString(bundle,
+ CFSTR("DEFAULT_SET_NAME"),
+ DEFAULT_SET_NAME,
+ NULL);
+ }
+ });
+ name = localized;
+ }
+
+ if (name == NULL) {
+ // if bundle or localized names not available
+ name = DEFAULT_SET_NAME;
+ }
+
+ CFRetain(name);
+ return name;
+}
+
+
+#define PREVENT_DUPLICATE_SERVICE_NAMES
+#ifdef PREVENT_DUPLICATE_SERVICE_NAMES
+static CFStringRef
+copy_next_name(CFStringRef name)
+{
+ CFArrayRef components;
+ CFIndex n;
+ CFMutableArrayRef newComponents;
+ SInt32 suffix = 2;
+
+ if (name == NULL) {
+ return NULL;
+ }
+
+ components = CFStringCreateArrayBySeparatingStrings(NULL, name, CFSTR(" "));
+ if (components != NULL) {
+ newComponents = CFArrayCreateMutableCopy(NULL, 0, components);
+ CFRelease(components);
+ } else {
+ newComponents = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
+ CFArrayAppendValue(newComponents, name);
+ }
+
+ n = CFArrayGetCount(newComponents);
+ if (n > 1) {
+ CFStringRef str;
+
+ str = CFArrayGetValueAtIndex(newComponents, n - 1);
+ suffix = CFStringGetIntValue(str);
+ if (suffix++ > 0) {
+ CFArrayRemoveValueAtIndex(newComponents, n - 1);
+ } else {
+ suffix = 2;
+ }
+ }
+
+ name = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), (int)suffix);
+ CFArrayAppendValue(newComponents, name);
+ CFRelease(name);
+
+ name = CFStringCreateByCombiningStrings(NULL, newComponents, CFSTR(" "));
+ CFRelease(newComponents);
+
+ return name;
+}
+
+
+static Boolean
+ensure_unique_service_name(SCNetworkServiceRef service)
+{
+ SCNetworkInterfaceRef interface;
+ CFStringRef name;
+ Boolean ok = TRUE;
+
+ interface = SCNetworkServiceGetInterface(service);
+
+ name = SCNetworkServiceGetName(service);
+ if (name != NULL) {
+ CFRetain(name);
+ }
+
+ while (TRUE) {
+ CFStringRef newName;
+
+ ok = SCNetworkServiceSetName(service, name);
+ if (ok) {
+ break;
+ }
+
+ if (SCError() != kSCStatusKeyExists) {
+ SC_log(LOG_INFO, "could not update service name for \"%@\": %s",
+ SCNetworkInterfaceGetLocalizedDisplayName(interface),
+ SCErrorString(SCError()));
+ break;
+ }
+
+ newName = copy_next_name(name);
+ if (newName == NULL) {
+ SC_log(LOG_INFO, "could not create unique name for \"%@\": %s",
+ SCNetworkInterfaceGetLocalizedDisplayName(interface),
+ SCErrorString(SCError()));
+ break;
+ }
+
+ // try again with the "new" name
+ if (name != NULL) {
+ CFRelease(name);
+ }
+ name = newName;
+ }
+
+ if (name != NULL) {
+ CFRelease(name);
+ }
+
+ return ok;
+}
+#endif // PREVENT_DUPLICATE_SERVICE_NAMES
+
+