2

I’m having some issues to get the default gateway IP on iOS using swift. I found an application on apple store called 'Net Analyzer' that can get the default gateway IP as you can see on the image below.

enter image description here

On the screen shot you can see the Default Gateway IP, The DNS Server IP and the device IP address. Can anyone give me a clue how to do it?

Currently I can get the device interfaces to get the device IP, but I want to get the default gateway that the device is connected.

I'm using the current code to get the interfaces addresses

func getIFAddresses() -> [String] {
        var addresses = [String]()

        // Get list of all interfaces on the local machine:
        var ifaddr : UnsafeMutablePointer<ifaddrs>?
        guard getifaddrs(&ifaddr) == 0 else { return [] }
        guard let firstAddr = ifaddr else { return [] }

        // For each interface ...
        for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
            let flags = Int32(ptr.pointee.ifa_flags)
            let addr = ptr.pointee.ifa_addr.pointee

            // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
            if ((flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING)) &&
                (addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6)) {

                // Convert interface address to a human readable string:
                var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                if (getnameinfo(ptr.pointee.ifa_addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                                nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                    let address = String(cString: hostname)
                    addresses.append(address)
                }
            }
        }

        freeifaddrs(ifaddr)
        return addresses
    }

By the way, is there any way to diff the Wi-Fi interface from the Mobile Network interface?

Mailson
  • 63
  • 2
  • 8

1 Answers1

1

Getting the Gateway

If you have the ability to be in iOS ≥ 13, you can use the Network framework.

The NWPath you get from the NWPathMonitor.pathUpdateHandler will have the NWPath.gateways variable set to the gateways IPs.

let monitor = NWPathMonitor(requiredInterfaceType: .wifi)
monitor.pathUpdateHandler = { path in
    print(path.gateways)
}
monitor.start(queue: DispatchQueue(label: "nwpathmonitor.queue"))

Diff the Wi-Fi interface from the Mobile Network Interface

The ptr.pointee.ifa_name contains the interface name. The interface name will be en0 for the WiFi interface, and pdp_ip0 for the cell interface. It is a cString so let interfaceName = String(cString: ptr.pointee.ifa_name) will convert it to a swift String.

Popmedic
  • 1,791
  • 1
  • 16
  • 21
  • Thanks, that is what I want. Is there similar handy method/approach when iOS < 12, otherwise we need to deal with the network interface. – Zhou Haibo Oct 13 '20 at 10:07
  • The truly awesome `Network` framework was not added till 12 :( < 12 needs to use the inet functions in the other answers. – Popmedic Oct 14 '20 at 12:10
  • The `gateways` property is iOS13+. – Avi Apr 20 '21 at 08:18
  • @Popmedic Thanks. Can we also get IP address of the connected Access Point in iOS? – Akshada-Systematix May 29 '21 at 14:43
  • @Akshada-Systematix The external IP address of the Access Point? This would be your best bet, be careful with the load balancer: https://stackoverflow.com/questions/27234861/correct-way-of-getting-clients-ip-addresses-from-http-request – Popmedic Jun 02 '21 at 17:30
  • @Popmedic : Thanks, but it seems the solution is not for iOS specifically. Can you please help me with this in iOS? – Akshada-Systematix Jun 04 '21 at 16:49
  • @akshada-systematix sorry I do not feel your question is related to this question, please start a new thread with your question, I am sure someone will help you. – Popmedic Jun 05 '21 at 23:27
  • @Popmedic: I already posted this question in separate thread: https://stackoverflow.com/questions/67706948/how-to-get-the-ip-address-of-router-in-swift-ios I have redirected to this question from one of the answers I got there. So, Can you please help me with my doubts? – Akshada-Systematix Jun 07 '21 at 08:58
  • @Popmedic: I just want to know how I can get ip address of dhcp server iOS Swift? – Akshada-Systematix Jun 07 '21 at 17:11
  • @Akshada-Systematix There is no Swift way to do this that I know about. The simple way is to make a http call to a service like [whatismyipaddress](https://whatismyipaddress.com/), or create your own servers that does it. [This post shows the technique using objective c.](https://stackoverflow.com/questions/10361023/get-public-ip-in-objective-c/14448134) – Popmedic Jun 17 '21 at 12:40
  • @Akshada-Systematix https://gist.github.com/popmedic/cbdfa9da7287d4dd085d758301a1e64f – Popmedic Jun 30 '21 at 19:52