objective c - How to get DNS TXT Record in iOS app -
objective c - How to get DNS TXT Record in iOS app -
i want inspect txt records server own app.
is possible? if yes, how can done?
thanks!
here's threw quickly. i'm not familiar txt records, might want test few different scenarios, demonstrates basic concept. can modify homecoming ttl values if need well. you'll want add together -lresolv
linker flags, , import resolv.h
headers.
#include <resolv.h> static nsarray *fetchtxtrecords(nsstring *domain) { // declare buffers / homecoming array nsmutablearray *answers = [nsmutablearray new]; u_char answer[1024]; ns_msg msg; ns_rr rr; // initialize resolver res_init(); // send query. res_query returns length of answer, or -1 if query failed int rlen = res_query([domain cstringusingencoding:nsutf8stringencoding], ns_c_in, ns_t_txt, answer, sizeof(answer)); if(rlen == -1) { homecoming nil; } // parse entire message if(ns_initparse(answer, rlen, &msg) < 0) { homecoming nil; } // number of messages returned int rrmax = rrmax = ns_msg_count(msg, ns_s_an); // iterate on each message for(int = 0; < rrmax; i++) { // parse reply section of message if(ns_parserr(&msg, ns_s_an, i, &rr)) { homecoming nil; } // obtain record info const u_char *rd = ns_rr_rdata(rr); // first byte length of info size_t length = rd[0]; // create , save string c string nsstring *record = [[nsstring alloc] initwithbytes:(rd + 1) length:length encoding:nsutf8stringencoding]; [answers addobject:record]; } homecoming answers; }
usage 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" // )
ios objective-c
Comments
Post a Comment