How to find the hardware interface type on iOS and Mac OS X?

I am writing code that heuristically determines the likelihood that a service will reside on a network interface. The hardware I'm looking for does not implement SSDP or mDNS, so I have to look for it manually.

The device connects to the network via WiFi, so most likely I will find it through the WiFi interface. However, it is possible that the Mac will be connected to the Wi-Fi bridge via Ethernet, so this can very well be resolved through this.

To avoid unnecessary requests and to be a generally good citizen of the network, I would like to be smart with which interface to try first.

I can get a list of interfaces on my computer without problems, but this is not useful: en0- wired ethernet on my iMac, but WiFi on my Macbook.

Bonus points if this works on iOS as well, although it is rarely possible to use a USB USB adapter with it.

+4
source share
3 answers

Use frame SystemConfiguration:

import Foundation
import SystemConfiguration

for interface in SCNetworkInterfaceCopyAll() as NSArray {
    if let name = SCNetworkInterfaceGetBSDName(interface as! SCNetworkInterface),
       let type = SCNetworkInterfaceGetInterfaceType(interface as! SCNetworkInterface) {
            print("Interface \(name) is of type \(type)")
    }
}

On my system, this prints:

Interface en0 is of type IEEE80211
Interface en3 is of type Ethernet
Interface en1 is of type Ethernet
Interface en2 is of type Ethernet
Interface bridge0 is of type Bridge
+3
source

Not much of a Mac developer, but iOSwe can use the class Reachabilityprovided by Apple.

Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus status = [reachability currentReachabilityStatus];

if(status == NotReachable) 
{
    //No Connection
}
else if (status == ReachableViaWiFi)
{
    //WiFi Connection
}
else if (status == ReachableViaWWAN) 
{
    //Carrier Connection
}
0
source

C: ( IP)

@implementation NetworkInterfaces

+(void)display{
    struct ifaddrs *ifap, *ifa;
    struct sockaddr_in *sa;
    char *addr;

    getifaddrs (&ifap);
    for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
        if (ifa->ifa_addr->sa_family==AF_INET) {
            sa = (struct sockaddr_in *) ifa->ifa_addr;
            addr = inet_ntoa(sa->sin_addr);
            printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr);
        }
    }

    freeifaddrs(ifap);
}
@end

( AppDelegate):

()

NetworkInterfaces.display()

(ObjC)   [ NetworkInterfaces];

0

All Articles