In a macOS app, I'm trying to get the gateway / router and DNS servers for a network interface. The interface could be identified by its BSD name, or using System Configuration with a SCNetworkInterfaceRef
or a SCNetworkServiceRef
. (Whichever is appropriate.)
Specifically, I want to get the same information that would be displayed to the user if they open the Network system preferences and select this connection. (Note that its gateway is called "router" in the Network system preferences, and I'm using the two terms interchangeably for the purposes of this question.)
This app is written in Objective-C, but a Swift solution is fine too as it's generally straightforward to port it to Objective-C.
I've figured out how to do this, given a
SCNetworkServiceRef
. These two functions will return the gateway and DNS servers for the specified network: