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; }
static void nc_print_VPN_service(SCNetworkServiceRef service) { SCNetworkInterfaceRef interface = NULL; CFStringRef display_name = NULL; CFStringRef display_name_padded = NULL; CFStringRef service_id = NULL; CFStringRef service_name = NULL; CFStringRef service_name_padded = NULL; CFStringRef service_status = NULL; CFStringRef service_status_padded = NULL; CFStringRef sub_type = NULL; CFStringRef type = NULL; nc_get_service_type_and_subtype(service, &type, &sub_type); service_name = SCNetworkServiceGetName(service); service_name_padded = copy_padded_string(service_name, 32, CFSTR("\""), CFSTR("\"")); service_id = SCNetworkServiceGetServiceID(service); interface = SCNetworkServiceGetInterface(service); display_name = SCNetworkInterfaceGetLocalizedDisplayName(interface); display_name_padded = copy_padded_string(display_name, 18, NULL, NULL); service_status = copy_VPN_status(service); service_status_padded = copy_padded_string(service_status, 16, CFSTR("("), CFSTR(")")); SCPrint(TRUE, stdout, CFSTR("%@ %@ %@ %@ %@ [%@%@%@]\n"), SCNetworkServiceGetEnabled(service) ? CFSTR("*") : CFSTR(" "), service_status_padded, service_id, display_name_padded, service_name_padded, type, (sub_type == NULL) ? CFSTR("") : CFSTR(":"), (sub_type == NULL) ? CFSTR("") : sub_type); CFRelease(display_name_padded); CFRelease(service_name_padded); CFRelease(service_status_padded); my_CFRelease(&service_status); }
static Boolean __SCNetworkSetEstablishDefaultConfigurationForInterfaces(SCNetworkSetRef set, CFArrayRef interfaces, Boolean excludeHidden) { CFSetRef excluded = NULL; CFIndex i; CFIndex n = 0; Boolean ok = TRUE; CFArrayRef services; SCNetworkSetPrivateRef setPrivate = (SCNetworkSetPrivateRef)set; Boolean updated = FALSE; Boolean updatedIFs = FALSE; #if TARGET_OS_IPHONE CFArrayRef orphans = NULL; CFArrayRef sets; sets = SCNetworkSetCopyAll(setPrivate->prefs); if (sets != NULL) { if (CFArrayGetCount(sets) == 1) { services = SCNetworkSetCopyServices(set); if (services != NULL) { n = CFArrayGetCount(services); CFRelease(services); } if ((n == 0) && CFEqual(set, CFArrayGetValueAtIndex(sets, 0))) { // after a "Reset Network Settings" we need to find (and // add back) any VPN services that were orphaned. orphans = SCNetworkServiceCopyAll(setPrivate->prefs); } } CFRelease(sets); } #endif // TARGET_OS_IPHONE // copy network services services = copyServices(set); // copy network interfaces to be excluded excluded = copyExcludedInterfaces(setPrivate->prefs); #if !TARGET_OS_IPHONE // look for interfaces that should auto-magically be added // to an Ethernet bridge n = (interfaces != NULL) ? CFArrayGetCount(interfaces) : 0; for (i = 0; i < n; i++) { SCBridgeInterfaceRef bridge = NULL; SCNetworkInterfaceRef interface; interface = CFArrayGetValueAtIndex(interfaces, i); if (excludeHidden && skipInterface(interface)) { // if not auto-configure continue; } if ((excluded != NULL) && CFSetContainsValue(excluded, interface)) { // if this interface is a member of a Bond or Bridge continue; } if (__SCNetworkServiceExistsForInterface(services, interface)) { // if this is not a new interface continue; } if (_SCNetworkInterfaceIsBuiltin(interface) && _SCNetworkInterfaceIsThunderbolt(interface) && !isA_SCBridgeInterface(interface)) { // add built-in Thunderbolt interfaces to bridge bridge = copyAutoBridgeInterface(setPrivate->prefs, CFSTR("thunderbolt-bridge")); } if (bridge != NULL) { CFIndex bridgeIndex; CFArrayRef members; CFMutableArrayRef newMembers; CFMutableSetRef newExcluded; CFMutableArrayRef newInterfaces; CFArrayRef newServices; // track the bridge interface (if it's in our list) bridgeIndex = CFArrayGetFirstIndexOfValue(interfaces, CFRangeMake(0, CFArrayGetCount(interfaces)), bridge); // add new member interface members = SCBridgeInterfaceGetMemberInterfaces(bridge); if ((members != NULL) && (CFArrayGetCount(members) > 0)) { newMembers = CFArrayCreateMutableCopy(NULL, 0, members); updated = TRUE; // if we're updating an existing bridge } else { newMembers = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); } CFArrayAppendValue(newMembers, interface); ok = SCBridgeInterfaceSetMemberInterfaces(bridge, newMembers); CFRelease(newMembers); if (!ok) { SC_log(LOG_INFO, "could not update bridge with \"%@\": %s", SCNetworkInterfaceGetLocalizedDisplayName(interface), SCErrorString(SCError())); CFRelease(bridge); continue; } // exclude the new member interface newExcluded = CFSetCreateMutableCopy(NULL, 0, excluded); CFRelease(excluded); CFSetAddValue(newExcluded, interface); excluded = newExcluded; // update the list of interfaces to include the [new or updated] bridge newInterfaces = CFArrayCreateMutableCopy(NULL, 0, interfaces); if (bridgeIndex != kCFNotFound) { CFArraySetValueAtIndex(newInterfaces, bridgeIndex, bridge); } else { CFArrayAppendValue(newInterfaces, bridge); } if (updatedIFs) { CFRelease(interfaces); } interfaces = newInterfaces; updatedIFs = TRUE; // refresh [existing] services newServices = updateServices(services, bridge); if (newServices != NULL) { CFRelease(services); services = newServices; } CFRelease(bridge); } } #endif // !TARGET_OS_IPHONE n = (interfaces != NULL) ? CFArrayGetCount(interfaces) : 0; for (i = 0; i < n; i++) { SCNetworkInterfaceRef interface; CFMutableArrayRef interface_list; interface = CFArrayGetValueAtIndex(interfaces, i); if (excludeHidden && skipInterface(interface)) { // if not auto-configure continue; } if ((excluded != NULL) && CFSetContainsValue(excluded, interface)) { // if this interface is a member of a Bond or Bridge continue; } if (__SCNetworkServiceExistsForInterface(services, interface)) { // if this is not a new interface continue; } interface_list = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); CFArrayAppendValue(interface_list, interface); while (ok && (CFArrayGetCount(interface_list) > 0)) { CFArrayRef protocol_types; interface = CFArrayGetValueAtIndex(interface_list, 0); protocol_types = SCNetworkInterfaceGetSupportedProtocolTypes(interface); if ((protocol_types != NULL) && (CFArrayGetCount(protocol_types) > 0)) { SCNetworkServiceRef service; service = SCNetworkServiceCreate(setPrivate->prefs, interface); if (service == NULL) { SC_log(LOG_INFO, "could not create service for \"%@\": %s", SCNetworkInterfaceGetLocalizedDisplayName(interface), SCErrorString(SCError())); ok = FALSE; goto nextInterface; } ok = SCNetworkServiceEstablishDefaultConfiguration(service); if (!ok) { SC_log(LOG_INFO, "could not estabish default configuration for \"%@\": %s", SCNetworkInterfaceGetLocalizedDisplayName(interface), SCErrorString(SCError())); SCNetworkServiceRemove(service); CFRelease(service); goto nextInterface; } ok = SCNetworkSetAddService(set, service); if (!ok) { SC_log(LOG_INFO, "could not add service for \"%@\": %s", SCNetworkInterfaceGetLocalizedDisplayName(interface), SCErrorString(SCError())); SCNetworkServiceRemove(service); CFRelease(service); goto nextInterface; } CFRelease(service); updated = TRUE; } else { add_supported_interfaces(interface_list, interface); } nextInterface : CFArrayRemoveValueAtIndex(interface_list, 0); } CFRelease(interface_list); } if (updatedIFs) CFRelease(interfaces); if (services != NULL) CFRelease(services); if (excluded != NULL) CFRelease(excluded); #if TARGET_OS_IPHONE if (orphans != NULL) { if (ok && updated) { CFIndex i; CFIndex n = CFArrayGetCount(orphans); for (i = 0; i < n; i++) { SCNetworkServiceRef service; service = CFArrayGetValueAtIndex(orphans, i); if (_SCNetworkServiceIsVPN(service)) { ok = SCNetworkSetAddService(set, service); if (!ok) { break; } } } } CFRelease(orphans); } #endif // TARGET_OS_IPHONE if (ok && !updated) { // if no changes were made _SCErrorSet(kSCStatusOK); } return updated; }
__private_extern__ void show_service(int argc, char **argv) { SCNetworkInterfaceRef interface; CFArrayRef protocols; SCNetworkServiceRef service; CFStringRef serviceName; SCNetworkServicePrimaryRank serviceRank; if (argc == 1) { service = _find_service(argv[0]); } else { if (net_service != NULL) { service = net_service; } else { SCPrint(TRUE, stdout, CFSTR("service not selected\n")); return; } } if (service == NULL) { return; } SCPrint(TRUE, stdout, CFSTR("service id = %@\n"), SCNetworkServiceGetServiceID(service)); serviceName = SCNetworkServiceGetName(service); SCPrint(TRUE, stdout, CFSTR("name = %@\n"), (serviceName != NULL) ? serviceName : CFSTR("")); serviceRank = SCNetworkServiceGetPrimaryRank(service); switch (serviceRank) { case kSCNetworkServicePrimaryRankDefault : // nothing to report break; case kSCNetworkServicePrimaryRankFirst : SCPrint(TRUE, stdout, CFSTR("primary rank = FIRST\n")); break; case kSCNetworkServicePrimaryRankLast : SCPrint(TRUE, stdout, CFSTR("primary rank = LAST\n")); break; case kSCNetworkServicePrimaryRankNever : SCPrint(TRUE, stdout, CFSTR("primary rank = NEVER\n")); break; case kSCNetworkServicePrimaryRankScoped : SCPrint(TRUE, stdout, CFSTR("primary rank = SCOPED\n")); break; default : SCPrint(TRUE, stdout, CFSTR("primary rank = %d\n"), serviceRank); break; } interface = SCNetworkServiceGetInterface(service); if (interface != NULL) { CFStringRef interfaceName; interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); if (interfaceName != NULL) { CFRetain(interfaceName); } else { interfaceName = _interface_description(interface); } if (interfaceName != NULL) { SCPrint(TRUE, stdout, CFSTR("interface = %@\n"), interfaceName); CFRelease(interfaceName); } } else { SCPrint(TRUE, stdout, CFSTR("\n No interface!\n\n")); } protocols = SCNetworkServiceCopyProtocols(service); if (protocols != NULL) { CFIndex n; n = CFArrayGetCount(protocols); if (n > 1) { CFMutableArrayRef sorted; sorted = CFArrayCreateMutableCopy(NULL, 0, protocols); CFArraySortValues(sorted, CFRangeMake(0, n), _compare_protocols, NULL); CFRelease(protocols); protocols = sorted; } if (n > 0) { CFIndex i; SCPrint(TRUE, stdout, CFSTR("configured protocols = ")); for (i = 0; i < n; i++) { SCNetworkProtocolRef protocol; protocol = CFArrayGetValueAtIndex(protocols, i); SCPrint(TRUE, stdout, CFSTR("%s%@"), (i == 0) ? "" : ", ", SCNetworkProtocolGetProtocolType(protocol)); } SCPrint(TRUE, stdout, CFSTR("\n")); __show_service_protocols(service, " ", FALSE); } else { SCPrint(TRUE, stdout, CFSTR("no configured protocols\n")); } CFRelease(protocols); } if (_sc_debug) { SCPrint(TRUE, stdout, CFSTR("\n%@\n"), service); } return; }
__private_extern__ void select_service(int argc, char **argv) { SCNetworkInterfaceRef interface; SCNetworkServiceRef service; CFStringRef serviceName; service = _find_service(argv[0]); if (service == NULL) { return; } if (net_service != NULL) CFRelease(net_service); net_service = CFRetain(service); serviceName = SCNetworkServiceGetName(service); if (serviceName != NULL) { SCPrint(TRUE, stdout, CFSTR("service \"%@\" selected\n"), serviceName); } else { SCPrint(TRUE, stdout, CFSTR("service ID \"%@\" selected\n"), SCNetworkServiceGetServiceID(service)); } interface = SCNetworkServiceGetInterface(service); if (interface != NULL) { CFStringRef interfaceName; if (net_interface != NULL) CFRelease(net_interface); net_interface = CFRetain(interface); interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); if (interfaceName == NULL) { interfaceName = SCNetworkInterfaceGetBSDName(interface); } if (interfaceName == NULL) { interfaceName = SCNetworkInterfaceGetInterfaceType(interface); } SCPrint(TRUE, stdout, CFSTR("& interface \"%@\" selected\n"), interfaceName); } else { if (net_interface != NULL) { CFRelease(net_interface); net_interface = NULL; SCPrint(TRUE, stdout, CFSTR("& no interface selected\n")); } } if (protocols != NULL) { CFRelease(protocols); protocols = NULL; } if (net_protocol != NULL) { CFRelease(net_protocol); net_protocol = NULL; SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n")); } return; }
__private_extern__ void create_service(int argc, char **argv) { SCNetworkInterfaceRef interface; CFStringRef interfaceName; Boolean ok; SCNetworkServiceRef service = NULL; CFStringRef serviceName; CFStringRef setName; CFArrayRef supported; if (prefs == NULL) { SCPrint(TRUE, stdout, CFSTR("network configuration not open\n")); return; } if (net_set == NULL) { SCPrint(TRUE, stdout, CFSTR("set not selected\n")); return; } if (argc < 1) { if (net_interface == NULL) { SCPrint(TRUE, stdout, CFSTR("no network interface selected\n")); return; } interface = net_interface; } else { int nArgs; interface = _find_interface(argc, argv, &nArgs); argv += nArgs; argc -= nArgs; } if (interface == NULL) { return; } supported = SCNetworkInterfaceGetSupportedProtocolTypes(interface); if (supported == NULL) { SCPrint(TRUE, stdout, CFSTR("no network protocols are supported over this interface\n")); return; } service = SCNetworkServiceCreate(prefs, interface); if (service == NULL) { SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); goto done; } if ((argc > 0) && (strlen(argv[0]) > 0)) { Boolean ok; serviceName = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8); // argv++; // argc--; ok = SCNetworkServiceSetName(service, serviceName); CFRelease(serviceName); if (!ok) { SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); (void)SCNetworkServiceRemove(service); goto done; } } ok = SCNetworkServiceEstablishDefaultConfiguration(service); if (!ok) { SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); (void)SCNetworkServiceRemove(service); goto done; } ok = SCNetworkSetAddService(net_set, service); if (!ok) { SCPrint(TRUE, stdout, CFSTR("service not created: %s\n"), SCErrorString(SCError())); (void)SCNetworkServiceRemove(service); goto done; } _prefs_changed = TRUE; if (net_service != NULL) CFRelease(net_service); net_service = CFRetain(service); serviceName = SCNetworkServiceGetName(service); if (serviceName != NULL) { SCPrint(TRUE, stdout, CFSTR("service \"%@\" (%@) created and selected\n"), serviceName, SCNetworkServiceGetServiceID(service)); } else { SCPrint(TRUE, stdout, CFSTR("service ID \"%@\" created and selected\n"), SCNetworkServiceGetServiceID(service)); } setName = SCNetworkSetGetName(net_set); if (setName != NULL) { SCPrint(TRUE, stdout, CFSTR("& added to set \"%@\"\n"), setName); } else { SCPrint(TRUE, stdout, CFSTR("& added to set ID \"%@\"\n"), SCNetworkSetGetSetID(net_set)); } if (net_interface != NULL) CFRelease(net_interface); net_interface = SCNetworkServiceGetInterface(net_service); if (net_interface != NULL) { CFRetain(net_interface); } interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); if (interfaceName == NULL) { interfaceName = SCNetworkInterfaceGetBSDName(interface); } if (interfaceName == NULL) { interfaceName = SCNetworkInterfaceGetInterfaceType(interface); } SCPrint(TRUE, stdout, CFSTR("& interface \"%@\" selected\n"), interfaceName); if (protocols != NULL) { CFRelease(protocols); protocols = NULL; } if (net_protocol != NULL) { CFRelease(net_protocol); net_protocol = NULL; SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n")); } if (services != NULL) { CFRelease(services); services = NULL; } done : if (service != NULL) CFRelease(service); return; }