Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. - (void)updateDnsRecords
  2. { NSLog(@"DNS update");
  3. DNSServiceRef sdRef;
  4. DNSServiceErrorType err;
  5.  
  6. NSTimeInterval remainingTime = 3.0;
  7. NSDate* startTime = [NSDate date];
  8.  
  9. err = DNSServiceQueryRecord(&sdRef, 0, 0,
  10. "_sip._tls.mydomain.com",
  11. kDNSServiceType_SRV,
  12. kDNSServiceClass_IN,
  13. processDnsReply,
  14. &remainingTime);
  15.  
  16. // This is necessary so we don't hang forever if there are no results
  17. int dns_sd_fd = DNSServiceRefSockFD(sdRef);
  18. int nfds = dns_sd_fd + 1;
  19. fd_set readfds;
  20. int result;
  21.  
  22. while (remainingTime > 0)
  23. {
  24. FD_ZERO(&readfds);
  25. FD_SET(dns_sd_fd, &readfds);
  26.  
  27. struct timeval tv;
  28. tv.tv_sec = (time_t)remainingTime;
  29. tv.tv_usec = (remainingTime - tv.tv_sec) * 1000000;
  30.  
  31. result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv);
  32. if (result == 1)
  33. {
  34. if (FD_ISSET(dns_sd_fd, &readfds))
  35. {
  36. err = DNSServiceProcessResult(sdRef);
  37. if (err != kDNSServiceErr_NoError)
  38. {
  39. NSLog(@"There was an error reading the DNS SRV records.");
  40. break;
  41. }
  42. }
  43. }
  44. else if (result == 0)
  45. {
  46. NSLog(@"DNS SRV select() timed out");
  47. break;
  48. }
  49. else
  50. {
  51. if (errno == EINTR)
  52. {
  53. NSLog(@"DNS SRV select() interrupted, retry.");
  54. }
  55. else
  56. {
  57. NSLog(@"DNS SRV select() returned %d errno %d %s.", result, errno, strerror(errno));
  58. break;
  59. }
  60. }
  61.  
  62. NSTimeInterval elapsed = [[NSDate date] timeIntervalSinceDate:startTime];
  63. remainingTime -= elapsed;
  64. }
  65.  
  66. DNSServiceRefDeallocate(sdRef);
  67. }
  68. static void processDnsReply(DNSServiceRef sdRef,
  69. DNSServiceFlags flags,
  70. uint32_t interfaceIndex,
  71. DNSServiceErrorType errorCode,
  72. const char* fullname,
  73. uint16_t rrtype,
  74. uint16_t rrclass,
  75. uint16_t rdlen,
  76. const void* rdata,
  77. uint32_t ttl,
  78. void* context
  79. )
  80. {
  81.  
  82. NSTimeInterval* remainingTime = (NSTimeInterval*)context;
  83.  
  84. // If a timeout occurs the value of the errorCode argument will be
  85. // kDNSServiceErr_Timeout.
  86. if (errorCode != kDNSServiceErr_NoError)
  87. {
  88. return;
  89. }
  90.  
  91. // The flags argument will have the kDNSServiceFlagsAdd bit set if the
  92. // callback is being invoked when a record is received in response to
  93. // the query.
  94. //
  95. // If kDNSServiceFlagsAdd bit is clear then callback is being invoked
  96. // because the record has expired, in which case the ttl argument will
  97. // be 0.
  98. if ((flags & kDNSServiceFlagsMoreComing) == 0)
  99. {
  100. *remainingTime = 0;
  101. }
  102.  
  103. // Record parsing code below was copied from Apple SRVResolver sample.
  104. NSMutableData * rrData = [NSMutableData data];
  105. dns_resource_record_t * rr;
  106. uint8_t u8;
  107. uint16_t u16;
  108. uint32_t u32;
  109.  
  110.  
  111. u8 = 0;
  112. [rrData appendBytes:&u8 length:sizeof(u8)];
  113. u16 = htons(kDNSServiceType_SRV);
  114. [rrData appendBytes:&u16 length:sizeof(u16)];
  115. u16 = htons(kDNSServiceClass_IN);
  116. [rrData appendBytes:&u16 length:sizeof(u16)];
  117. u32 = htonl(666);
  118. [rrData appendBytes:&u32 length:sizeof(u32)];
  119. u16 = htons(rdlen);
  120. [rrData appendBytes:&u16 length:sizeof(u16)];
  121. [rrData appendBytes:rdata length:rdlen];
  122.  
  123. rr = dns_parse_resource_record([rrData bytes], (uint32_t) [rrData length]);
  124.  
  125. // If the parse is successful, add the results.
  126. if (rr != NULL)
  127. {
  128. NSString *target;
  129.  
  130. target = [NSString stringWithCString:rr->data.SRV->target encoding:NSASCIIStringEncoding];
  131. if (target != nil)
  132. {
  133. uint16_t priority = rr->data.SRV->priority;
  134. uint16_t weight = rr->data.SRV->weight;
  135. uint16_t port = rr->data.SRV->port;
  136. char *adresse = rr->data.SRV->target;
  137.  
  138. NSLog(@"Prio:%hu",priority);
  139. NSLog(@"weight:%hu",weight);
  140. NSLog(@"port:%hu",port);
  141. NSLog(@"adresse:%s",adresse);
  142. NSLog(@"fullname:%s",fullname);
  143.  
  144. //[[FailoverWebInterface sharedInterface] addDnsServer:target priority:priority weight:weight port:port ttl:ttl]; // You'll have to do this in with your own method.
  145. }
  146. }
  147.  
  148. dns_free_resource_record(rr);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement