2

Im my code i am trying to use SCNetworkSetGetServiceOrder to get network service order. like below

Ethernet
FireWire
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge

i am using below code

    -(void)getserviceorder
    {
           CFArrayRef          interfaces;
            CFIndex            interfaceCount;
            CFIndex            interfaceIndex;
            CFMutableArrayRef  result;
            CFArrayRef *portArray;
            portArray = NULL;
            result = NULL;
            interfaces = NULL;
                if (SCNetworkInterfaceCopyAll != NULL) {
                interfaces = SCNetworkInterfaceCopyAll();
                interfaceCount = CFArrayGetCount(interfaces);

            for (interfaceIndex = 0; interfaceIndex < interfaceCount;
                 interfaceIndex++) {
                SCNetworkInterfaceRef  thisInterface;
                SCNetworkSetRef set =         CFArrayGetValueAtIndex(interfaces,interfaceIndex);
                assert(thisInterface != NULL);

               NSLog(@"SCNetworkSetGetServiceOrder is %@",SCNetworkSetGetServiceOrder(set));

            }
                CFRelease(interfaces);
}

But it always returns null. Is this proper way or how can i use SCNetworkSetGetServiceOrder method to get the service order. What should be the value of (SCNetworkSetRef set), which is the parameter of SCNetworkSetGetServiceOrder method . where can i learn more about this

1 Answers1

2

For example, like this (in Swift):

import SystemConfiguration

let scpref = SCPreferencesCreate(kCFAllocatorDefault, "iflist" as CFString, nil)

let netset = SCNetworkSetCopyCurrent(scpref!)
let netservs = SCNetworkSetGetServiceOrder(netset!)! as NSArray

for id in netservs {
    print("id \(id)")
    let serv = SCNetworkServiceCopy(scpref!, id as! CFString)!
    if SCNetworkServiceGetEnabled(serv) {
        if let interface = SCNetworkServiceGetInterface(serv) {
            let serviceName = SCNetworkServiceGetName(serv)
            print(serviceName as! String)
        }
    }
}
  • 1
    Very nice... Any idea about reordering the network set, like WiFi first later Ethernet and other network cards? Tried using `SCNetworkSetSetServiceOrder(netset!, newSet)` but with no luck – Chethan Shetty Jan 25 '18 at 10:56
  • Any idea how to get the virtual interfaces services too? e.g. utun network services. – Adrian Maire Jul 16 '21 at 07:40