Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get DNS server IP from iphone settings

I try to find a way how to get my DNS server ip which appear under settings->wi-fi->details->DNS. I don't know if apple allow to get this info programmatically.

like image 353
Patryk Grabowski Avatar asked Jul 06 '15 21:07

Patryk Grabowski


Video Answer


1 Answers

This is (IMHO) a better Swift3+ variant, which is an extract of my Swift wrapper for libresolv as used by dig for iOS, and correctly handles IPV6 name servers:

open class Resolver {

    fileprivate var state = __res_9_state()

    public init() {
        res_9_ninit(&state)
    }

    deinit() {
        res_9_ndestroy(&state)
    }

    public final func getservers() -> [res_9_sockaddr_union] {

        let maxServers = 10
        var servers = [res_9_sockaddr_union](repeating: res_9_sockaddr_union(), count: maxServers)
        let found = Int(res_9_getservers(&state, &servers, Int32(maxServers)))

        // filter is to remove the erroneous empty entry when there's no real servers
       return Array(servers[0 ..< found]).filter() { $0.sin.sin_len > 0 }
    }
}

Converting a res_9_sockaddr_union to its string value can be done with this:

extension Resolver {
    public static func getnameinfo(_ s: res_9_sockaddr_union) -> String {
        var s = s
        var hostBuffer = [CChar](repeating: 0, count: Int(NI_MAXHOST))

        len sinlen = socklen_t(s.sin.sin_len)
        let _ = withUnsafePointer(to: &s) {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
                Darwin.getnameinfo($0, sinlen,
                                   &hostBuffer, socklen_t(hostBuffer.count),
                                   nil, 0,
                                   NI_NUMERICHOST)
            }
        }

        return String(cString: hostBuffer)
    }
}

which all combined can get a String[] containing the list of servers like this:

let servers = Resolver().getservers().map(Resolver.getnameinfo)
like image 132
Alnitak Avatar answered Oct 05 '22 00:10

Alnitak