Skip to content

Instantly share code, notes, and snippets.

@lzell
Created June 21, 2016 21:52
Show Gist options
  • Save lzell/a5a967e627c7c79ba57f10873f5bede6 to your computer and use it in GitHub Desktop.
Save lzell/a5a967e627c7c79ba57f10873f5bede6 to your computer and use it in GitHub Desktop.
DNS resolve multicast os x
var res : __res_9_state = __res_9_state()
res_9_ninit(&res)
var addr: in_addr = in_addr()
inet_aton("224.0.0.251", &addr)
res.nsaddr_list.0.sin_addr = addr
res.nsaddr_list.0.sin_family = sa_family_t(AF_INET)
res.nsaddr_list.0.sin_port = in_port_t(5353).bigEndian
res.nscount = 1
let start = NSDate()
var answer = [CUnsignedChar](count: 1024, repeatedValue: 0)
print("About to query....")
// So this takes a LONG time on first bluetooth connect. I don't know why. The service is found almost instantly.
// Could try forcing IPV6. Something to do w/ link local.
let nBytesRead : CInt = res_9_nquery(&res, hosttarget, Int32(ns_c_any.rawValue), Int32(ns_t_a.rawValue), &answer, Int32(answer.count))
print("bytes read: \(nBytesRead)")
var handle: __ns_msg = __ns_msg()
res_9_ns_initparse(answer, nBytesRead, &handle)
// ns_msg_count is defined in nameser.h, but it's a C macro. Expanding it here:
assert(ns_s_an.rawValue == 1, "ns_s_an constant changed")
let answerCount = handle._counts.1
if answerCount > 0 {
var ip = [CChar](count: 256, repeatedValue: 0)
var rr : __ns_rr = __ns_rr()
if(res_9_ns_parserr(&handle, ns_s_an, 0, &rr) == 0) {
// Here we can't use ns_rr_rdata, so expanding the macro in nameser.h
let v1 : UnsafePointer<u_char> = (rr.rdata + 0)
// Cast between UnsafePointer<u_char> and UnsafePointer<in_addr>
let v99 : UnsafePointer<in_addr> = UnsafePointer<in_addr>(v1)
strcpy(&ip, inet_ntoa(v99.memory))
print(String.fromCString(ip))
}
}
print("Done in \(NSDate().timeIntervalSinceDate(start))")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment