Query DNS to find NAPTR in iOS - ios

I have been having a lot of trouble finding a way to query the DNS to find the NAPTR in iOS. There seem to be many relatively simple ways to resolve to an IP, but I specifically need to find all NAPTR records in a DNS lookup. I'd prefer to do so without having to bring in any external libraries if at all possible. If anyone has been able to do this (or something similar that I can extrapolate from) I'd appreciate any pointers.
All code must function in iOS 5.0+

I ended up using DNSServiceQueryRecord.
DNSServiceRef sdRef;
DNSServiceQueryRecord(&sdRef, 0, 0,
"google.com",
kDNSServiceType_NAPTR,
kDNSServiceClass_IN,
callback,
NULL);
DNSServiceProcessResult(sdRef);
DNSServiceRefDeallocate(sdRef);
In actual use, I found that there was an issue where the app would hang indefinitely if there were no results, so I ended up having to adjust my code to add a timeout on the result.
/*
Attempt to fetch the NAPTR from the stored server address. Since iOS will continue waiting
until told directly to stop (even if there is no result) we must set our own timeout on the
request (set to 5 seconds).
On success, the callback function is called. On timeout, the kSRVLookupComplete notification
is sent.
*/
- (void)attemptNAPTRFetch {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DNSServiceRef sdRef;
DNSServiceErrorType err;
err = DNSServiceQueryRecord(&sdRef, 0, 0,
[server cStringUsingEncoding:[NSString defaultCStringEncoding]],
kDNSServiceType_NAPTR,
kDNSServiceClass_IN,
callback,
NULL);
// This stuff is necessary so we don't hang forever if there are no results
int dns_sd_fd = DNSServiceRefSockFD(sdRef);
int nfds = dns_sd_fd + 1;
fd_set readfds;
struct timeval tv;
int result;
int stopNow = 0;
int timeOut = 5; // Timeout in seconds
while (!stopNow) {
FD_ZERO(&readfds);
FD_SET(dns_sd_fd, &readfds);
tv.tv_sec = timeOut;
tv.tv_usec = 0;
result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
if (result > 0) {
if(FD_ISSET(dns_sd_fd, &readfds)) {
err = DNSServiceProcessResult(sdRef);
if (err != kDNSServiceErr_NoError){
NSLog(#"There was an error");
}
stopNow = 1;
}
}
else {
printf("select() returned %d errno %d %s\n", result, errno, strerror(errno));
if (errno != EINTR) {
stopNow = 1;
postNotification(kSRVLookupComplete, nil);
}
}
}
DNSServiceRefDeallocate(sdRef);
});
}
Then, for the callback:
static void callback(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *fullname,
uint16_t rrtype,
uint16_t rrclass,
uint16_t rdlen,
const void *rdata,
uint32_t ttl,
void *context)
{
uint16_t order, pref;
char flag;
NSMutableString *service = [[NSMutableString alloc] init];
NSMutableString *replacement = [[NSMutableString alloc] init];
const char *data = (const char*)rdata;
order = data[1];
pref = data[3];
flag = data[5];
int i = 7;
while (data[i] != 0){
[service appendString:[NSString stringWithFormat:#"%c", data[i]]];
i++;
}
i += 2;
while(data[i] != 0){
if(data[i] >= 32 && data[i] <= 127)
[replacement appendString:[NSString stringWithFormat:#"%c", data[i]]];
else
[replacement appendString:#"."];
i++;
}
NSLog(#"\nOrder: %i\nPreference: %i\nFlag: %c\nService: %#\nReplacement: %#\n", order, pref, flag, service, replacement);
}
This seems to do the trick for me. You would of course do any other necessary work using all the parsed data in the callback or store the data somewhere to be used later.

Related

How to get CNAME for Host name/ip adress

I have been able to get ip addresses from host name and vice versa using CFHost class but not able to get CNAME from Host name.
Anybody having any idea how to resolve the host name to get its CNAME.
I found out the solution for it. Hope it may help someone looking for it:
+ (void) getCNAME {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DNSServiceRef serviceRef;
DNSServiceErrorType error;
error = DNSServiceQueryRecord(&serviceRef, 0, 0, "apple.com", kDNSServiceType_CNAME,
kDNSServiceClass_IN, queryCallback, NULL);
if (error != kDNSServiceErr_NoError){
NSLog(#"DNS Service error");
}
DNSServiceProcessResult(serviceRef);
DNSServiceRefDeallocate(serviceRef);
});}
static void queryCallback(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *fullname,
uint16_t rrtype,
uint16_t rrclass,
uint16_t rdlen,
const void *rdata,
uint32_t ttl,
void *context) {
if (errorCode == kDNSServiceErr_NoError && rdlen > 1) {
NSMutableData *txtData = [NSMutableData dataWithCapacity:rdlen];
for (uint16_t i = 1; i < rdlen; i += 256) {
[txtData appendBytes:rdata + i length:MIN(rdlen - i, 255)];
}
NSString *theTXT = [[NSString alloc] initWithBytes:txtData.bytes length:txtData.length encoding:NSASCIIStringEncoding];
NSLog(#"CNAME: %#", theTXT);
}}

How to get CNAME of a domain in iOS

I would like to resolve a domain and get its CNAME and ip info, like the following:
example.com -> cname.com -> ip address
I searched a lot and know how to get the ip address, e.g. by CFHost. But I still don't know how to get other information, like its CNAME.
Should I do this with CFHost or something else?
You can use below code for your purpose
+ (void) getCNAME {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DNSServiceRef serviceRef;
DNSServiceErrorType error;
error = DNSServiceQueryRecord(&serviceRef, 0, 0, "apple.com", kDNSServiceType_CNAME,
kDNSServiceClass_IN, queryCallback, NULL);
if (error != kDNSServiceErr_NoError){
NSLog(#"DNS Service error");
}
DNSServiceProcessResult(serviceRef);
DNSServiceRefDeallocate(serviceRef);
});}
static void queryCallback(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *fullname,
uint16_t rrtype,
uint16_t rrclass,
uint16_t rdlen,
const void *rdata,
uint32_t ttl,
void *context) {
if (errorCode == kDNSServiceErr_NoError && rdlen > 1) {
NSMutableData *txtData = [NSMutableData dataWithCapacity:rdlen];
for (uint16_t i = 1; i < rdlen; i += 256) {
[txtData appendBytes:rdata + i length:MIN(rdlen - i, 255)];
}
NSString *theTXT = [[NSString alloc] initWithBytes:txtData.bytes length:txtData.length encoding:NSASCIIStringEncoding];
NSLog(#"CNAME: %#", theTXT);
}}

iOS Resolve SRV Record without Domain

I am trying to resolve a SRV Record with my iOS App.
I have come so far that I indeed can resolve the DNS record.
This works only if the SRV Record Contains a valid Domain Ending.
Each Customer has its on domain so I can't hard Code the Domain.
Furthermore I don't want the User to specify a Domain Name. This would make the SRV record useless.
I hope Somebody can explain to me how the resolve an DNS Record without the Domain. Or Resolve the Domain first an the do the SRV query. Thanks a lot.
This Works: _sip._tls.mydomain.com
This Should be the Goal: _sip._tls
And please do not give me a suggestion for how to resolve _sip._tls
this is only a placeholder. My real SRV record is a custom record like "_myApplication._tcp" THX
- (void)updateDnsRecords
{
NSLog(#"DNS update");
DNSServiceRef sdRef;
DNSServiceErrorType err;
NSTimeInterval remainingTime = 3.0;
NSDate* startTime = [NSDate date];
err = DNSServiceQueryRecord(&sdRef, 0, 0,
"_sip._tls.mydomain.com",
kDNSServiceType_SRV,
kDNSServiceClass_IN,
processDnsReply,
&remainingTime);
// This is necessary so we don't hang forever if there are no results
int dns_sd_fd = DNSServiceRefSockFD(sdRef);
int nfds = dns_sd_fd + 1;
fd_set readfds;
int result;
while (remainingTime > 0)
{
FD_ZERO(&readfds);
FD_SET(dns_sd_fd, &readfds);
struct timeval tv;
tv.tv_sec = (time_t)remainingTime;
tv.tv_usec = (remainingTime - tv.tv_sec) * 1000000;
result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
if (result == 1)
{
if (FD_ISSET(dns_sd_fd, &readfds))
{
err = DNSServiceProcessResult(sdRef);
if (err != kDNSServiceErr_NoError)
{
NSLog(#"There was an error reading the DNS SRV records.");
break;
}
}
}
else if (result == 0)
{
NSLog(#"DNS SRV select() timed out");
break;
}
else
{
if (errno == EINTR)
{
NSLog(#"DNS SRV select() interrupted, retry.");
}
else
{
NSLog(#"DNS SRV select() returned %d errno %d %s.", result, errno, strerror(errno));
break;
}
}
NSTimeInterval elapsed = [[NSDate date] timeIntervalSinceDate:startTime];
remainingTime -= elapsed;
}
DNSServiceRefDeallocate(sdRef);
}
static void processDnsReply(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char* fullname,
uint16_t rrtype,
uint16_t rrclass,
uint16_t rdlen,
const void* rdata,
uint32_t ttl,
void* context
)
{
NSTimeInterval* remainingTime = (NSTimeInterval*)context;
// If a timeout occurs the value of the errorCode argument will be
// kDNSServiceErr_Timeout.
if (errorCode != kDNSServiceErr_NoError)
{
return;
}
// The flags argument will have the kDNSServiceFlagsAdd bit set if the
// callback is being invoked when a record is received in response to
// the query.
//
// If kDNSServiceFlagsAdd bit is clear then callback is being invoked
// because the record has expired, in which case the ttl argument will
// be 0.
if ((flags & kDNSServiceFlagsMoreComing) == 0)
{
*remainingTime = 0;
}
// Record parsing code below was copied from Apple SRVResolver sample.
NSMutableData * rrData = [NSMutableData data];
dns_resource_record_t * rr;
uint8_t u8;
uint16_t u16;
uint32_t u32;
u8 = 0;
[rrData appendBytes:&u8 length:sizeof(u8)];
u16 = htons(kDNSServiceType_SRV);
[rrData appendBytes:&u16 length:sizeof(u16)];
u16 = htons(kDNSServiceClass_IN);
[rrData appendBytes:&u16 length:sizeof(u16)];
u32 = htonl(666);
[rrData appendBytes:&u32 length:sizeof(u32)];
u16 = htons(rdlen);
[rrData appendBytes:&u16 length:sizeof(u16)];
[rrData appendBytes:rdata length:rdlen];
rr = dns_parse_resource_record([rrData bytes], (uint32_t) [rrData length]);
// If the parse is successful, add the results.
if (rr != NULL)
{
NSString *target;
target = [NSString stringWithCString:rr->data.SRV->target encoding:NSASCIIStringEncoding];
if (target != nil)
{
uint16_t priority = rr->data.SRV->priority;
uint16_t weight = rr->data.SRV->weight;
uint16_t port = rr->data.SRV->port;
char *adresse = rr->data.SRV->target;
NSLog(#"Prio:%hu",priority);
NSLog(#"weight:%hu",weight);
NSLog(#"port:%hu",port);
NSLog(#"adresse:%s",adresse);
NSLog(#"fullname:%s",fullname);
//[[FailoverWebInterface sharedInterface] addDnsServer:target priority:priority weight:weight port:port ttl:ttl]; // You'll have to do this in with your own method.
}
}
dns_free_resource_record(rr);
}

iOS Network Stack "What kind of network am I"? [duplicate]

How to get Wi-Fi encryption mode in iOS without private libraries?
The code from the answer above has been posted originally on this website: http://www.codeproject.com/Articles/621213/Non-Standard-Way-to-Get-Inaccessible-Data-from-iOS
By the way, for this code to work you need to include the appropriate header files with #include <mach/mach.h> so that your compiler recognizes NDR_record_t ndr.
However, this whole setup did not actually return me the encryption mode of the current WiFi, but rather the configuration of AirPort (the variable key in the code from above needs to be set to NSString *key = #"Setup:/Network/Interface/en0/AirPort"; before). I tried different values instead of AirPort which I got from running $scutil in the Terminal of my Mac (such as Setup:/Network/Interface/en0/IPv4 or Setup:/Network/Interface/en0/Modem or from this website)
Hope that helps someone having similar issues...
For iOS 5:
aslmsg asl, message;
aslresponse searchResult;
int i;
const char *key, *val;
NSMutableArray *result_dicts = [NSMutableArray array];
asl = asl_new(ASL_TYPE_QUERY);
if (!asl)
{
DDLogCError(#"Failed creating ASL query");
}
asl_set_query(asl, "Sender", "kernel", ASL_QUERY_OP_EQUAL);
asl_set_query(asl, "Message", "AppleBCMWLAN Joined BSS:", ASL_QUERY_OP_PREFIX|ASL_QUERY_OP_EQUAL);
searchResult = asl_search(NULL, asl);
while (NULL != (message = aslresponse_next(searchResult)))
{
NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
for (i = 0; (NULL != (key = asl_key(message, i))); i++)
{
NSString *keyString = [NSString stringWithUTF8String:(char *)key];
val = asl_get(message, key);
NSString *string = [NSString stringWithUTF8String:val];
[tmpDict setObject:string forKey:keyString];
}
[result_dicts addObject:tmpDict];
}
aslresponse_free(searchResult);
asl_free(asl);
For iOS 6:
#define kMachPortConfigd "com.apple.SystemConfiguration.configd"
-(NSDictionary *)getSCdata:(NSString *)key
{
if(SYSTEM_VERSION_LESS_THAN(#"6.0"))
{
// It does not work on iOS 5.*
return nil;
}
struct send_body {mach_msg_header_t header; int count; UInt8 *addr; CFIndex size0; int flags; NDR_record_t ndr; CFIndex size; int retB; int rcB; int f24; int f28;};
mach_port_t bootstrapport = MACH_PORT_NULL;
mach_port_t configport = MACH_PORT_NULL;
mach_msg_header_t *msg;
mach_msg_return_t msg_return;
struct send_body send_msg;
// Make request
CFDataRef extRepr;
extRepr = CFStringCreateExternalRepresentation(NULL, (__bridge CFStringRef)(key), kCFStringEncodingUTF8, 0);
// Connect to Mach MIG port of configd
task_get_bootstrap_port(mach_task_self(), &bootstrapport);
bootstrap_look_up2(bootstrapport, kMachPortConfigd, &configport, 0, 8LL);
// Make request
send_msg.count = 1;
send_msg.addr = (UInt8*)CFDataGetBytePtr(extRepr);
send_msg.size0 = CFDataGetLength(extRepr);
send_msg.size = CFDataGetLength(extRepr);
send_msg.flags = 0x1000100u;
send_msg.ndr = NDR_record;
// Make message header
msg = &(send_msg.header);
msg->msgh_bits = 0x80001513u;
msg->msgh_remote_port = configport;
msg->msgh_local_port = mig_get_reply_port();
msg->msgh_id = 20010;
// Request server
msg_return = mach_msg(msg, 3, 0x34u, 0x44u, msg->msgh_local_port, 0, 0);
if(msg_return)
{
if (msg_return - 0x10000002u >= 2 && msg_return != 0x10000010 )
{
mig_dealloc_reply_port(msg->msgh_local_port);
}
else
{
mig_put_reply_port(msg->msgh_local_port);
}
}
else if ( msg->msgh_id != 71 && msg->msgh_id == 20110 && msg->msgh_bits <= -1 )
{
if ((send_msg.flags & 0xFF000000) == 0x1000000)
{
CFDataRef deserializedData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, send_msg.addr,send_msg.size0, kCFAllocatorNull);
CFPropertyListRef proplist = CFPropertyListCreateWithData(kCFAllocatorDefault, deserializedData, kCFPropertyListImmutable, NULL, NULL);
mig_dealloc_reply_port(msg->msgh_local_port);
mach_port_deallocate(mach_task_self(), bootstrapport);
mach_port_deallocate(mach_task_self(), configport);
mach_msg_destroy(msg);
NSDictionary *property_list = (__bridge NSDictionary*)proplist;
if(proplist)
CFRelease(proplist);
CFRelease(deserializedData);
CFRelease(extRepr);
return property_list;
}
}
mig_dealloc_reply_port(msg->msgh_local_port);
mach_port_deallocate(mach_task_self(), bootstrapport);
mach_port_deallocate(mach_task_self(), configport);
mach_msg_destroy(msg);
CFRelease(extRepr);
return nil;
}

Transferring a file through TCP in GCDAsyncSocket

I am currently writing a game in which I intend to transfer a .caf sound file from the iPhone to my C++ server. Right now I am getting an EXC_BAD_ACCESS when I message the sendGameUpdateWithFile function, and I really have no idea why.
Although this is not related to any uni assignments, please keep in mind that I am a CS student and not (yet) a professional network programmer, so judge my code thereafter.
Here's the code I use to transmit the data to the server (which crashes right now):
- (void) sendGameUpdateWithFile:(NSString*)filePath gameID:(NSInteger)gameID {
NSMutableData* data = [[NSMutableData alloc] init];
data = [NSMutableData dataWithContentsOfFile:filePath];
fileheadPacket head;
head.msgtype = 0x12;
strncpy(head.data1, [myUsername cStringUsingEncoding:NSASCIIStringEncoding], [myUsername length]);
int followingPackets = (([data length] % 1024 == 0) || ([data length] < 1024))? ([data length]/1024) : ([data length]/1024)+1;
head.following = followingPackets;
head.fileid = gameID;
head.size = sizeof(packet);
[mySock writeData:[NSData dataWithBytes:&head length:sizeof(packet)] withTimeout:-1 tag:7];
filePacket sendPackets[followingPackets];
for(int i = 0; i < followingPackets; i++){
NSRange thisRange;
thisRange.location = i*1024;
thisRange.length = (i+1)*1024;
filePacket tmp;
tmp.msgtype = 0x13;
tmp.size = sizeof(filePacket);
memset(tmp.data1, 0, sizeof(tmp.data1));
[data getBytes:tmp.fileBuffer range:thisRange];
strncpy((char*)&sendPackets[i], (char*)&tmp, sizeof(tmp));
}
for(int i = 0; i < followingPackets; i++){
[mySock writeData:[NSData dataWithBytes:&sendPackets[i] length:sizeof(filePacket)] withTimeout:-1 tag:3];
}
}
The structs I use for data look like this:
typedef struct file_packet {
int msgtype:8;
int size:16;
int nul:8;
int following:24;
int emp:8;
char data1[64];
char fileBuffer[1024];
} filePacket;
typedef struct filehead_packet {
int msgtype:8;
int size:16;
int nul:8;
int following:24;
int emp:8;
char data1[64];
int fileid;
char rest[60];
} fileheadPacket;
The server expects the given msgtypes -- the problem lies on the client side.

Resources