How to get DNS TXT record in iOS app
1 answer
Here's something that I threw together a bit quickly. I am not very familiar with TXT records, so you can test it with a few scenarios, but this demonstrates the basic concept. You can change it to return TTL values if you need. You want to add -lresolv
to your linker flags and import headers resolv.h
.
#include <resolv.h>
static NSArray *fetchTXTRecords(NSString *domain)
{
// declare buffers / return array
NSMutableArray *answers = [NSMutableArray new];
u_char answer[1024];
ns_msg msg;
ns_rr rr;
// initialize resolver
res_init();
// send query. res_query returns the length of the answer, or -1 if the query failed
int rlen = res_query([domain cStringUsingEncoding:NSUTF8StringEncoding], ns_c_in, ns_t_txt, answer, sizeof(answer));
if(rlen == -1)
{
return nil;
}
// parse the entire message
if(ns_initparse(answer, rlen, &msg) < 0)
{
return nil;
}
// get the number of messages returned
int rrmax = rrmax = ns_msg_count(msg, ns_s_an);
// iterate over each message
for(int i = 0; i < rrmax; i++)
{
// parse the answer section of the message
if(ns_parserr(&msg, ns_s_an, i, &rr))
{
return nil;
}
// obtain the record data
const u_char *rd = ns_rr_rdata(rr);
// the first byte is the length of the data
size_t length = rd[0];
// create and save a string from the C string
NSString *record = [[NSString alloc] initWithBytes:(rd + 1) length:length encoding:NSUTF8StringEncoding];
[answers addObject:record];
}
return answers;
}
The usage is pretty straight forward:
NSArray *records = fetchTXTRecords(@"google.com");
NSLog(@"%@", records);
// outputs:
// (
// "v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"
// )
+6
source to share