#1 Get Gateway/Router IP of current wifi connection.
#2 Get hostname of a given IP
#3 print 1 and 2 to debug console.
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 Gateway/Router IP of current wifi connection.
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.Get hostname of a given IP
That’s good background.This will just be a personal tool
How are you doing that?I was able to get the Gateway IP
Did you look at CFHostSample?I'm just trying to get a host name for given IP address.
Yeah, don’t do that. This is using a routing socket but, as I mentioned earlier, routing sockets are not supported on iOS.I'm getting Gateway IP with FGRoute CocoaPod.
What went wrong?Yes I looked at CFHostSample still not having any luck getting the
HostName for a given IP.
Dowloaded the sample.
Opened it in Xcode 12.4 on macOS 11.1.
Selected the CFHostSampleSwift scheme.
Updated the Swift Language Version build setting to Swift 5.
Fixed the resulting build errors (which wasn’t hard; there’s only a few errors and the fix-it suggestions are helpful).
Ran it with the arguments -a 184.72.216.47.
It printed:
Code Block 184.72.216.47 -> ec2-184-72-216-47.compute-1.amazonaws.com
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.its working on my iPhone 12...
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"