0

I am using the following code to get the network adapters of my macOS System:

private func getAdapters() -> [Adapter]
    {
        var adapters: [Adapter] = []

        var addresses: UnsafeMutablePointer<ifaddrs>?
        guard getifaddrs(&addresses) == 0 else { return adapters }
        guard let firstAddr = addresses else { return adapters }

        for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next })
        {
            let address = ptr.pointee
            let rawData = address.ifa_data
            let name = address.ifa_name
            let socket: sockaddr = address.ifa_addr.pointee

            // Set up some filters
            let loopback = (address.ifa_flags & UInt32(IFF_LOOPBACK)) != 0
            let up = (address.ifa_flags & UInt32(IFF_UP)) != 0
            let p2p = (address.ifa_flags & UInt32(IFF_POINTOPOINT)) != 0

            if rawData != nil && name != nil && socket.sa_family == UInt8(AF_LINK) && !loopback && up && !p2p
            {
                let adapterName = String(utf8String: UnsafePointer<CChar>(name!))
                let adapter = Adapter(name: adapterName!)
                adapters.append(adapter)
            }
        }

        freeifaddrs(addresses)

        return adapters
    }

Now I am looking for a way to figure out, which of those adapters is the "active" one, i.e. which one is connected to the internet.

I want to get the adapter that has the "green dot" in the network settings. How can I get this information?

Regards, Sascha

inexcitus
  • 2,471
  • 2
  • 26
  • 41
  • What about checking for IFF_RUNNING interfaces, as in http://stackoverflow.com/a/25627545/1187415? – Martin R Mar 19 '17 at 19:14
  • Hello, I already tried that (but removed the code) because I still get every adapter on my machine, i.e. every adapter has the IF_RUNNING flag. – inexcitus Mar 19 '17 at 19:16
  • I have another question: why does none of my adapters have the AF_INET or AF_INET6 Flag? They only have the AF_LINK flag. – inexcitus Mar 20 '17 at 06:23

0 Answers0