Last active
April 14, 2017 09:27
-
-
Save zulkis/7eb85c28dd9738f5a824daf0421600a1 to your computer and use it in GitHub Desktop.
Grabbing DNS addresses from iOS as a CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <resolv.h> | |
#include <netdb.h> | |
@implementation C2GDNSResolver { | |
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)) { // Flag values for getnameinfo() | |
[addresses addObject:[NSString stringWithUTF8String:hostBuffer]]; | |
} | |
} | |
} | |
return [addresses componentsJoinedByString:@","]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment