Help getting Network info (SWIFT)

Can someone please help point me in the right direction. I am trying to do the following in SWIFT for iOS:

#1 Get Gateway/Router IP of current wifi connection.
#2 Get hostname of a given IP
#3 print 1 and 2 to debug console.

Get Gateway/Router IP of current wifi connection.

This is quite tricky. On macOS you can do this using a routing socket but this is not supported on iOS. The only way I know to do this that I fully support is via traceroute, and that’s not fun to implement.

Get hostname of a given IP

That’s not possible in general — there’s no guarantee that a specific IP has a single DNS name, it might have zero or more than one — but I presume you’re asking about doing reverse DNS. If so, you can do that using CFHost, which is relatively easy to call from Swift. See CFHostSample for an example.



Taking a step back, what are you trying to do here? The reason I ask is that most folks who ask questions like this end up running into roadblocks because of recent privacy-focused changes to the system. If you can provide information about your high-level goal, I may be able to steer you around those roadblocks (or perhaps tell you that there’s no way around and thus you need to rethink your destination).

See the Local Network Privacy FAQ for more background on this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Here is more info on the end goal.

I have several locations, I would like to make an app where when I get on site and join the wifi it looks up the gatway IP address for that location. Once I have the gateway IP I can change the last number on the IP and get the hostname for that IP. This static IP has a hostname containing the location number of the site I'm at. With location number I can then just simply ping known devices by hostname as they are all named with location in the hostname.

Once I have the gateway IP and Hostname of a device thats on the network I can then display to the user status info (online/offline) for many devices at this site that should all have static IP's that match the gateway IP but differ by only the last set of numbers in the IP. or by hostname with the location number obtained.


I know this is possible, there is currently an app called Network Analyzer that displays the gatway IP.



This will just be a personal tool to let me walk onto a site and just instantly be shown any known devices at the location that are offline. The app would do this from getting the gatway IP and figuring out the rest by standardized IP's and Hostnames at each location.

FYI (gatway and device IP are different subnets thats why I cant simply use the device IP)
I was able to get the Gateway IP, now I'm just trying to get a host name for given IP address.

This will just be a personal tool

That’s good background.

I was able to get the Gateway IP

How are you doing that?

I'm just trying to get a host name for given IP address.

Did you look at CFHostSample?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
I'm getting Gateway IP with FGRoute CocoaPod.

Yes I looked at CFHostSample still not having any luck getting the HostName for a given IP.

For now I'm just passing the IP to a PHP page on the same network and having it return everything i need like hostname in JSON. I would like to get it working in the app itself without having to request the info from an external PHP server.

I'm getting Gateway IP with FGRoute CocoaPod.

Yeah, don’t do that. This is using a routing socket but, as I mentioned earlier, routing sockets are not supported on iOS.

Note If you look at the code you’ll see it includes its own copy of <route.h>, which is never a good sign.

Yes I looked at CFHostSample still not having any luck getting the
HostName for a given IP.

What went wrong?

For context, I did the following:
  1. Dowloaded the sample.

  2. Opened it in Xcode 12.4 on macOS 11.1.

  3. Selected the CFHostSampleSwift scheme.

  4. Updated the Swift Language Version build setting to Swift 5.

  5. Fixed the resulting build errors (which wasn’t hard; there’s only a few errors and the fix-it suggestions are helpful).

  6. Ran it with the arguments -a 184.72.216.47.

  7. It printed:

.

Code Block
184.72.216.47 -> ec2-184-72-216-47.compute-1.amazonaws.com


Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Not supported in iOS?

its working on my iPhone 12...

its working on my iPhone 12...

Not supported as in Developer Technical Support (DTS), the group I work for, does not support it. The reason we don’t support it is that we can’t promise long-term compatibility. If Apple was prepared to stand by this API on iOS, the <route.h> header would be in the iOS SDK.

There are plenty of things that work today but are not supported. Sometimes such things break. If you build your product around unsupported stuff, and that stuff breaks, you have a problem. If you’re lucky there will be an alternative and ‘all’ you have to do is ship an emergency update. If you’re unlucky, there’s no alternative and you have to pull a feature out from underneath your users.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"

Can't we use iOS 13 NWPath gateways API to get a router/gateway IP adress?

Possibly. At the time I posted the above I wasn’t aware of that API )-:

Having said that, I recently tried using gateways and ran into a problem, namely that the property gets depopulated when the path goes to .ready. That is, if I start a connection and then, on each state change, get the gateways property from the currentPath), the property is populated while the connection is in the .preparing state but then gets depopulated when the connection goes to the .ready state. Needless to say, this is a bug :-(r. 74269834).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Help getting Network info (SWIFT)
 
 
Q