/* Turns a service ID or name into a vendor type, or preserves type */ static CFStringRef nc_copy_vendor_type (CFStringRef input) { SCNetworkInterfaceRef child; SCNetworkInterfaceRef interface; CFStringRef output_name = input; SCNetworkServiceRef service = NULL; CFStringRef type; if (input == NULL) { goto done; } service = nc_copy_service(NULL, input); if (service != NULL) { interface = SCNetworkServiceGetInterface(service); child = SCNetworkInterfaceGetInterface(interface); type = SCNetworkInterfaceGetInterfaceType(interface); /* Must be of type VPN */ if (!CFEqual(type, kSCNetworkInterfaceTypeVPN)) { output_name = NULL; goto done; } output_name = SCNetworkInterfaceGetInterfaceType(child); goto done; } done : if (output_name != NULL) CFRetain(output_name); my_CFRelease(&service); return output_name; }
/* * Function: copy_service * Purpose: * Check the global list of SCNetworkServiceRefs for one that is configured * over the specifed SCNetworkInterfaceRef. The assumption here is that * a previous call has already checked for such a service in the current * set. */ STATIC SCNetworkServiceRef copy_service(SCPreferencesRef prefs, SCNetworkInterfaceRef net_if) { int count = 0; int i; SCNetworkServiceRef service = NULL; CFArrayRef list; list = SCNetworkServiceCopyAll(prefs); if (list != NULL) { count = CFArrayGetCount(list); } if (count == 0) { goto done; } for (i = 0; i < count; i++) { SCNetworkInterfaceRef this_if; SCNetworkServiceRef this_service; this_service = (SCNetworkServiceRef)CFArrayGetValueAtIndex(list, i); this_if = SCNetworkServiceGetInterface(this_service); if (this_if == NULL) { continue; } if (CFEqual(this_if, net_if)) { service = this_service; CFRetain(service); break; } } done: my_CFRelease(&list); return (service); }
/* * Function: copy_configured_interface_names * * Purpose: * Return the BSD interface name of all configured interfaces. If * 'entity' is non-NULL, also check that the interface has the specified * extended configuration. */ STATIC CFArrayRef /* of CFStringRef */ copy_configured_interface_names(SCPreferencesRef prefs, CFStringRef entity_name) { SCNetworkSetRef current_set = NULL; int count; int i; CFMutableArrayRef ret_names = NULL; CFRange ret_names_range = { 0 , 0 }; CFArrayRef services = NULL; if (prefs == NULL) { goto done; } current_set = SCNetworkSetCopyCurrent(prefs); if (current_set == NULL) { goto done; } services = SCNetworkSetCopyServices(current_set); if (services == NULL) { goto done; } count = CFArrayGetCount(services); for (i = 0; i < count; i++) { CFStringRef this_if_name; SCNetworkInterfaceRef this_if; SCNetworkServiceRef s; s = (SCNetworkServiceRef)CFArrayGetValueAtIndex(services, i); this_if = SCNetworkServiceGetInterface(s); if (this_if == NULL) { continue; } if (entity_name != NULL && (SCNetworkInterfaceGetExtendedConfiguration(this_if, entity_name) == NULL)) { /* interface doesn't have specified entity */ continue; } this_if_name = SCNetworkInterfaceGetBSDName(this_if); if (this_if_name == NULL) { continue; } if (ret_names == NULL || CFArrayContainsValue(ret_names, ret_names_range, this_if_name) == FALSE) { if (ret_names == NULL) { ret_names = CFArrayCreateMutable(NULL, count, &kCFTypeArrayCallBacks); } CFArrayAppendValue(ret_names, this_if_name); ret_names_range.length++; } } done: my_CFRelease(¤t_set); my_CFRelease(&services); return (ret_names); }
/* * Function: copy_interface * Purpose: * Get a reference to an SCNetworkInterfaceRef for the specified * interface name. First try to get an interface configured in the * current set. If that fails, copy a service configured over the * specified interface, and add it to the current set. * * Return the interface, service, and set for the caller to release. */ STATIC SCNetworkInterfaceRef copy_interface(SCPreferencesRef prefs, CFStringRef if_name, SCNetworkSetRef * ret_set_p, SCNetworkServiceRef * ret_service_p) { SCNetworkSetRef current_set = NULL; SCNetworkServiceRef service = NULL; SCNetworkInterfaceRef net_if; SCNetworkInterfaceRef ret = NULL; /* if the interface is part of a service/set, we're done */ net_if = copy_configured_interface(prefs, if_name); if (net_if != NULL) { ret = net_if; CFRetain(ret); goto done; } /* interface isn't part of a service/set, make it so */ net_if = copy_present_interface(if_name); if (net_if == NULL) { goto done; } /* find the service in any set */ service = copy_service(prefs, net_if); if (service == NULL) { EAPLOG(LOG_ERR, "EAPOLClientConfiguration: can't get service"); goto done; } /* add the service to the current set */ current_set = SCNetworkSetCopyCurrent(prefs); if (current_set == NULL) { EAPLOG(LOG_ERR, "EAPOLClientConfiguration: can't get current set"); goto done; } if (SCNetworkSetAddService(current_set, service) == FALSE) { EAPLOG(LOG_ERR, "EAPOLClientConfiguration: failed to add dummy service"); goto done; } /* return this SCNetworkInterfaceRef since it's bound to the prefs */ ret = SCNetworkServiceGetInterface(service); CFRetain(ret); done: my_CFRelease(&net_if); if (ret == NULL) { my_CFRelease(&service); my_CFRelease(¤t_set); } *ret_service_p = service; *ret_set_p = current_set; return (ret); }
Boolean SCNetworkSetRemoveService(SCNetworkSetRef set, SCNetworkServiceRef service) { SCNetworkInterfaceRef interface; CFArrayRef interface_config = NULL; Boolean ok; CFStringRef path; int sc_status = kSCStatusOK; SCNetworkServicePrivateRef servicePrivate = (SCNetworkServicePrivateRef)service; SCNetworkSetPrivateRef setPrivate = (SCNetworkSetPrivateRef)set; if (!isA_SCNetworkSet(set)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; } if (!isA_SCNetworkService(service) || (servicePrivate->prefs == NULL)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; } // remove service from ServiceOrder _serviceOrder_remove(set, service); // get the [deep] interface configuration settings interface = SCNetworkServiceGetInterface(service); if (interface != NULL) { interface_config = __SCNetworkInterfaceCopyDeepConfiguration(set, interface); if (interface_config != NULL) { // remove the interface configuration from all sets which contain this service. __SCNetworkInterfaceSetDeepConfiguration(set, interface, NULL); } } // remove the link between "set" and the "service" path = SCPreferencesPathKeyCreateSetNetworkServiceEntity(NULL, setPrivate->setID, servicePrivate->serviceID, NULL); ok = SCPreferencesPathRemoveValue(setPrivate->prefs, path); if (!ok) { sc_status = SCError(); // preserve the error } CFRelease(path); // push the [deep] interface configuration [back] into all sets which contain the service. if (interface_config != NULL) { __SCNetworkInterfaceSetDeepConfiguration(set, interface, interface_config); } if (interface_config != NULL) CFRelease(interface_config); if (!ok) { _SCErrorSet(sc_status); } return ok; }
static int _serviceOrder(SCNetworkServiceRef service) { SCNetworkInterfaceRef interface; interface = SCNetworkServiceGetInterface(service); if ((interface == NULL) || _SCNetworkServiceIsVPN(service)) { return 100000; // if unknown or VPN interface, sort last } return __SCNetworkInterfaceOrder(interface); }
/* ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- */ static void nc_get_service_type_and_subtype(SCNetworkServiceRef service, CFStringRef *iftype, CFStringRef *ifsubtype) { SCNetworkInterfaceRef interface = SCNetworkServiceGetInterface(service); SCNetworkInterfaceRef child = SCNetworkInterfaceGetInterface(interface); *iftype = SCNetworkInterfaceGetInterfaceType(interface); *ifsubtype = NULL; if (CFEqual(*iftype, kSCNetworkInterfaceTypePPP) || CFEqual(*iftype, kSCNetworkInterfaceTypeVPN)) { *ifsubtype = (child != NULL) ? SCNetworkInterfaceGetInterfaceType(child) : NULL; } }
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 CFArrayRef updateServices(CFArrayRef services, SCNetworkInterfaceRef interface) { CFStringRef bsdName; CFIndex i; CFIndex n; CFMutableArrayRef newServices; if (services == NULL) { return NULL; } bsdName = SCNetworkInterfaceGetBSDName(interface); newServices = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); n = CFArrayGetCount(services); for (i = 0; i < n; i++) { SCNetworkInterfaceRef interface; CFStringRef interfaceName; SCNetworkServiceRef newService; SCNetworkServiceRef service; CFStringRef serviceID; SCNetworkServicePrivateRef servicePrivate; service = CFArrayGetValueAtIndex(services, i); interface = SCNetworkServiceGetInterface(service); interfaceName = SCNetworkInterfaceGetBSDName(interface); if (!_SC_CFEqual(interfaceName, bsdName)) { // if not a match, retain CFArrayAppendValue(newServices, service); continue; } // if a match, update serviceID = SCNetworkServiceGetServiceID(service); servicePrivate = (SCNetworkServicePrivateRef)service; newService = SCNetworkServiceCopy(servicePrivate->prefs, serviceID); if (newService != NULL) { CFArrayAppendValue(newServices, newService); CFRelease(newService); } } return newServices; }
static void __show_service_interface(SCNetworkServiceRef service, const char *prefix) { CFStringRef description; SCNetworkInterfaceRef interface; interface = SCNetworkServiceGetInterface(service); if (interface == NULL) { return; } description = _interface_description(interface); SCPrint(TRUE, stdout, CFSTR("%s%@\n"), prefix, description); CFRelease(description); return; }
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 SCNetworkInterfaceRef copy_configured_interface(SCPreferencesRef prefs, CFStringRef if_name) { SCNetworkSetRef current_set = NULL; int count; int i; SCNetworkInterfaceRef ret_if = NULL; CFArrayRef services = NULL; current_set = SCNetworkSetCopyCurrent(prefs); if (current_set == NULL) { goto done; } services = SCNetworkSetCopyServices(current_set); if (services == NULL) { goto done; } count = CFArrayGetCount(services); for (i = 0; i < count; i++) { CFStringRef this_if_name; SCNetworkInterfaceRef this_if; SCNetworkServiceRef s; s = (SCNetworkServiceRef)CFArrayGetValueAtIndex(services, i); this_if = SCNetworkServiceGetInterface(s); if (this_if == NULL) { continue; } this_if_name = SCNetworkInterfaceGetBSDName(this_if); if (this_if_name == NULL) { continue; } if (CFEqual(this_if_name, if_name)) { ret_if = this_if; CFRetain(ret_if); break; } } done: my_CFRelease(¤t_set); my_CFRelease(&services); return (ret_if); }
vector<InterfaceInfo> OSXPlatform::interfaces() { vector<InterfaceInfo> result; CFStringRef name = CFSTR("com.codebutler.firesheep.backend"); SCPreferencesRef prefs = SCPreferencesCreate(NULL, name, NULL); SCNetworkSetRef set = SCNetworkSetCopyCurrent(prefs); CFArrayRef services = SCNetworkSetCopyServices(set); int arraySize = CFArrayGetCount(services); for (int i = 0; i < arraySize; i++) { SCNetworkServiceRef service = (SCNetworkServiceRef) CFArrayGetValueAtIndex(services, i); if (SCNetworkServiceGetEnabled(service)) { SCNetworkInterfaceRef iface = SCNetworkServiceGetInterface(service); CFStringRef serviceName = SCNetworkServiceGetName(service); char cServiceName[(CFStringGetLength(serviceName) * 4) + 1]; CFStringGetCString(serviceName, cServiceName, sizeof(cServiceName), kCFStringEncodingUTF8); CFStringRef type = SCNetworkInterfaceGetInterfaceType(iface); if (CFStringCompare(type, CFSTR("Ethernet"), 0) == kCFCompareEqualTo || CFStringCompare(type, CFSTR("IEEE80211"), 0) == kCFCompareEqualTo) { char cType[(CFStringGetLength(type) * 4) + 1]; CFStringGetCString(type, cType, sizeof(cType), kCFStringEncodingUTF8); CFStringRef bsdName = SCNetworkInterfaceGetBSDName(iface); char cBsdName[(CFStringGetLength(bsdName) * 4) + 1]; CFStringGetCString(bsdName, cBsdName, sizeof(cBsdName), kCFStringEncodingUTF8); InterfaceInfo info((string(cBsdName)), (string(cServiceName)), (string(cType))); result.push_back(info); } } } CFRelease(services); CFRelease(set); CFRelease(prefs); return result; }
__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; }
Boolean SCNetworkSetAddService(SCNetworkSetRef set, SCNetworkServiceRef service) { SCNetworkInterfaceRef interface; CFArrayRef interface_config = NULL; CFStringRef link; Boolean ok; CFStringRef path; SCNetworkServicePrivateRef servicePrivate = (SCNetworkServicePrivateRef)service; SCNetworkSetPrivateRef setPrivate = (SCNetworkSetPrivateRef)set; if (!isA_SCNetworkSet(set)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; } if (!isA_SCNetworkService(service) || (servicePrivate->prefs == NULL)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; } // make sure that we do not add an orphaned network service if its // associated interface is a member of a bond or bridge. interface = SCNetworkServiceGetInterface(service); if ((interface != NULL) && __SCNetworkInterfaceIsMember(servicePrivate->prefs, interface)) { _SCErrorSet(kSCStatusKeyExists); return FALSE; } //#define PREVENT_DUPLICATE_SETS #ifdef PREVENT_DUPLICATE_SETS CFArrayRef sets; // ensure that each service is only a member of ONE set sets = SCNetworkSetCopyAll(setPrivate->prefs); if (sets != NULL) { CFIndex i; CFIndex n; n = CFArrayGetCount(sets); for (i = 0; i < n; i++) { Boolean found; CFArrayRef services; SCNetworkSetRef set; set = CFArrayGetValueAtIndex(sets, i); services = SCNetworkSetCopyServices(set); found = CFArrayContainsValue(services, CFRangeMake(0, CFArrayGetCount(services)), service); CFRelease(services); if (found) { CFRelease(sets); _SCErrorSet(kSCStatusKeyExists); return FALSE; } } CFRelease(sets); } #endif /* PREVENT_DUPLICATE_SETS */ // get the [deep] interface configuration settings interface = SCNetworkServiceGetInterface(service); if (interface != NULL) { interface_config = __SCNetworkInterfaceCopyDeepConfiguration(set, interface); } // create the link between "set" and the "service" path = SCPreferencesPathKeyCreateSetNetworkServiceEntity(NULL, // allocator setPrivate->setID, // set servicePrivate->serviceID, // service NULL); // entity link = SCPreferencesPathKeyCreateNetworkServiceEntity(NULL, // allocator servicePrivate->serviceID, // service NULL); // entity ok = SCPreferencesPathSetLink(setPrivate->prefs, path, link); #ifdef PREVENT_DUPLICATE_SERVICE_NAMES if (ok) { ok = ensure_unique_service_name(service); if (!ok) { // if we could not ensure a unique name, remove the (just added) // link between the "set" and the "service" (void) SCPreferencesPathRemoveValue(setPrivate->prefs, path); } } #endif // PREVENT_DUPLICATE_SERVICE_NAMES CFRelease(path); CFRelease(link); if (!ok) { goto done; } // push the [deep] interface configuration into all sets which contain this service. if (interface != NULL) { __SCNetworkInterfaceSetDeepConfiguration(set, interface, interface_config); } // add service to ServiceOrder _serviceOrder_add(set, service); // mark set as no longer "new" setPrivate->established = TRUE; done : if (interface_config != NULL) CFRelease(interface_config); return ok; }
__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 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; }
static SCNetworkServiceRef _find_service(char *match) { Boolean allowIndex = TRUE; CFIndex i; CFIndex n; CFStringRef select_name = NULL; SCNetworkServiceRef selected = NULL; if (services == NULL) { if (net_set == NULL) { SCPrint(TRUE, stdout, CFSTR("set not selected\n")); return NULL; } services = SCNetworkSetCopyServices(net_set); if (services == NULL) { SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); return NULL; } allowIndex = FALSE; } // try to select the service by its serviceID select_name = CFStringCreateWithCString(NULL, match, kCFStringEncodingUTF8); n = CFArrayGetCount(services); for (i = 0; i < n; i++) { SCNetworkServiceRef service; CFStringRef serviceID; service = CFArrayGetValueAtIndex(services, i); serviceID = SCNetworkServiceGetServiceID(service); if (CFEqual(select_name, serviceID)) { selected = service; goto done; } } // try to select the service by its name for (i = 0; i < n; i++) { SCNetworkServiceRef service; CFStringRef serviceName; service = CFArrayGetValueAtIndex(services, i); serviceName = SCNetworkServiceGetName(service); if ((serviceName != NULL) && CFEqual(select_name, serviceName)) { if (selected == NULL) { selected = service; } else { // if multiple services match selected = NULL; SCPrint(TRUE, stdout, CFSTR("multiple services match\n")); goto done; } } } if (selected != NULL) { goto done; } // try to select the service by its name (case insensitive) for (i = 0; i < n; i++) { SCNetworkServiceRef service; CFStringRef serviceName; service = CFArrayGetValueAtIndex(services, i); serviceName = SCNetworkServiceGetName(service); if ((serviceName != NULL) && CFStringCompare(select_name, serviceName, kCFCompareCaseInsensitive) == kCFCompareEqualTo) { if (selected == NULL) { selected = service; } else { // if multiple services match selected = NULL; SCPrint(TRUE, stdout, CFSTR("multiple services match\n")); goto done; } } } if (selected != NULL) { goto done; } // try to select the service by its [BSD] interface name for (i = 0; i < n; i++) { SCNetworkInterfaceRef interface; CFStringRef interfaceName = NULL; SCNetworkServiceRef service; service = CFArrayGetValueAtIndex(services, i); interface = SCNetworkServiceGetInterface(service); while ((interface != NULL) && (interfaceName == NULL)) { interfaceName = SCNetworkInterfaceGetBSDName(interface); if (interfaceName == NULL) { interface = SCNetworkInterfaceGetInterface(interface); } } if (interfaceName == NULL) { continue; } if (CFStringCompare(select_name, interfaceName, kCFCompareCaseInsensitive) == kCFCompareEqualTo) { if (selected == NULL) { selected = service; } else { // if multiple services match selected = NULL; SCPrint(TRUE, stdout, CFSTR("multiple services match\n")); goto done; } } } if (selected != NULL) { goto done; } // try to select the service by its index if (allowIndex) { char *end; char *str = match; long val; errno = 0; val = strtol(str, &end, 10); if ((*str != '\0') && (*end == '\0') && (errno == 0)) { if ((val > 0) && (val <= n)) { selected = CFArrayGetValueAtIndex(services, val - 1); } } } if (selected != NULL) { goto done; } SCPrint(TRUE, stdout, CFSTR("no match, which service?\n")); done : if (select_name != NULL) CFRelease(select_name); return selected; }