问题
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.
回答1:
I used this on OS X and worked for me, (link to ibresolv.dylib as well)
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <resolv.h>
#include <dns.h>
- (NSString *) getDNSAddressesStr
{
NSMutableString *addressStr = [[NSMutableString alloc]initWithString:@"DNS Addresses \n"];
res_state res = malloc(sizeof(struct __res_state));
int result = res_ninit(res);
if ( result == 0 )
{
for ( int i = 0; i < res->nscount; i++ )
{
NSString *s = [NSString stringWithUTF8String : inet_ntoa(res->nsaddr_list[i].sin_addr)];
[addressStr appendFormat:@"%@\n",s];
NSLog(@"%@",s);
}
}
else
[addressStr appendString:@" res_init result != 0"];
return addressStr;
}
}
回答2:
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)
回答3:
ObjC version with IPV6 support if Swift is not in your project:
#include <resolv.h>
#include <netdb.h>
@implementation DNSResolver {
res_state _state;
}
- (instancetype)init {
self = [super init];
if (self) {
_state = malloc(sizeof(struct __res_state));
if (EXIT_SUCCESS != res_ninit(_state)) {
free(_state);
return nil;
}
}
return self;
}
- (void)dealloc {
res_ndestroy(_state);
free(_state);
}
#pragma mark - Public
- (NSString *)getDNSAddressesCSV
{
NSMutableArray *addresses = [NSMutableArray new];
union res_sockaddr_union servers[NI_MAXSERV];
int serversFound = res_9_getservers(_state, servers, NI_MAXSERV);
char hostBuffer[NI_MAXHOST];
for (int i = 0; i < serversFound; i ++) {
union res_sockaddr_union s = servers[i];
if (s.sin.sin_len > 0) {
if (EXIT_SUCCESS == getnameinfo((struct sockaddr *)&s.sin, // Pointer to your struct sockaddr
(socklen_t)s.sin.sin_len, // Size of this struct
(char *)&hostBuffer, // Pointer to hostname string
sizeof(hostBuffer), // Size of this string
nil, // Pointer to service name string
0, // Size of this string
NI_NUMERICHOST)) { // Flags given
[addresses addObject:[NSString stringWithUTF8String:hostBuffer]];
}
}
}
return [addresses componentsJoinedByString:@","];
}
@end
回答4:
I think a free() is missing:
free(res);
ps for xCode 7 add "libresolv.tbd" to "link with Binary Library"
回答5:
This is Swift3 variant of the accepted answer:
func dnsServers() -> [in_addr_t] {
var result = __res_9_state()
guard res_9_ninit(&result) == 0 else { return [] }
let sockets = result.nsaddr_list // note: nsaddr_list is a tuple, not an array
let addresses = [sockets.0.sin_addr.s_addr, sockets.1.sin_addr.s_addr, sockets.2.sin_addr.s_addr]
let filtered = addresses.filter { $0 != nil }
return filtered
}
As usual, link libresolv.9.tbd
and add #import <resolv.h>
to bridging header
来源:https://stackoverflow.com/questions/31256024/get-dns-server-ip-from-iphone-settings