Maurício Linhares
Maurício Linhares

Reputation: 40333

How do I get the currently connected network interface name using Cocoa or Foundation?

I need to know the network interface name of the currently connected network interface, as in en0, lo0 and so on.

Is there a Cocoa/Foundation function that is going to give me this information?

Upvotes: 2

Views: 9520

Answers (4)

RhodanV5500
RhodanV5500

Reputation: 1107

Ported the sample code of @ambientlight to iOS 13:

public func interfaceNames() -> [String] {

        let MAX_INTERFACES = 128;

        var interfaceNames = [String]()
        let interfaceNamePtr = UnsafeMutablePointer<Int8>.allocate(capacity: Int(Int(IF_NAMESIZE)))
        for interfaceIndex in 1...MAX_INTERFACES {
            if (if_indextoname(UInt32(interfaceIndex), interfaceNamePtr) != nil){
                let interfaceName = String(cString: interfaceNamePtr)
                interfaceNames.append(interfaceName)
            } else {
                break
            }
        }

        interfaceNamePtr.deallocate()
        return interfaceNames
    }

Most likely leaking memory - Use with caution.

Output:

▿ 20 elements
  - 0 : "lo0"
  - 1 : "pdp_ip0"
  - 2 : "pdp_ip1"
  - 3 : "pdp_ip2"
  - 4 : "pdp_ip3"
  - 5 : "pdp_ip5"
  - 6 : "pdp_ip4"
  - 7 : "pdp_ip6"
  - 8 : "pdp_ip7"
  - 9 : "ap1"
  - 10 : "en0"
  - 11 : "en1"
  - 12 : "en2"
  - 13 : "ipsec0"
  - 14 : "ipsec1"
  - 15 : "ipsec2"
  - 16 : "ipsec3"
  - 17 : "awdl0"
  - 18 : "utun0"
  - 19 : "utun1"

Upvotes: 0

ambientlight
ambientlight

Reputation: 7332

Alternatively you can also utilize if_indextoname() to get available interface names. Here is how Swift implementation would look like:

public func interfaceNames() -> [String] {

    let MAX_INTERFACES = 128;

    var interfaceNames = [String]()
    let interfaceNamePtr = UnsafeMutablePointer<Int8>.alloc(Int(IF_NAMESIZE))
    for interfaceIndex in 1...MAX_INTERFACES {
        if (if_indextoname(UInt32(interfaceIndex), interfaceNamePtr) != nil){
            if let interfaceName = String.fromCString(interfaceNamePtr) {
                interfaceNames.append(interfaceName)
            }
        } else {
            break
        }
    }

    interfaceNamePtr.dealloc(Int(IF_NAMESIZE))
    return interfaceNames
}

Upvotes: 2

Rembrandt Q. Einstein
Rembrandt Q. Einstein

Reputation: 1121

Since iOS works slightly differently to OSX, we had luck using the following code based on Davyd's answer to see the names of all available network interfaces on an iPhone: (also see here for full documentation on ifaddrs)

#include <ifaddrs.h>

struct ifaddrs* interfaces = NULL;
struct ifaddrs* temp_addr = NULL;

// retrieve the current interfaces - returns 0 on success
NSInteger success = getifaddrs(&interfaces);
if (success == 0)
{
    // Loop through linked list of interfaces
    temp_addr = interfaces;
    while (temp_addr != NULL)
    {
            NSString* name = [NSString stringWithUTF8String:temp_addr->ifa_name];
            NSLog(@"interface name: %@", name);

        temp_addr = temp_addr->ifa_next;
    }
}

// Free memory
freeifaddrs(interfaces);

Upvotes: 3

Davyd Geyl
Davyd Geyl

Reputation: 4623

You can cycle through network interfaces and get their names, IP addresses, etc.

#include <ifaddrs.h>
// you may need to include other headers

struct ifaddrs* interfaces = NULL;
struct ifaddrs* temp_addr = NULL;

// retrieve the current interfaces - returns 0 on success
NSInteger success = getifaddrs(&interfaces);
if (success == 0)
{
    // Loop through linked list of interfaces
    temp_addr = interfaces;
    while (temp_addr != NULL)
    {
      if (temp_addr->ifa_addr->sa_family == AF_INET) // internetwork only
      {
        NSString* name = [NSString stringWithUTF8String:temp_addr->ifa_name];
        NSString* address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
        NSLog(@"interface name: %@; address: %@", name, address);
      }

      temp_addr = temp_addr->ifa_next;
    }
}

// Free memory
freeifaddrs(interfaces);

There are many other flags and data in the above structures, I hope you will find what you are looking for.

Upvotes: 10

Related Questions