Last active
November 8, 2016 04:47
-
-
Save networkextension/bddb1316222b5d7de1f663ea02f0da9c to your computer and use it in GitHub Desktop.
quary dns use swift 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func query(domain:String) ->[String] { | |
| var results:[String] = [] | |
| //dispatch_async(q) { [weak self] in | |
| let host = CFHostCreateWithName(nil,domain as CFString).takeRetainedValue() | |
| //NSLog("getIPFromDNS %@", hostName) | |
| //let d = NSDate() | |
| CFHostStartInfoResolution(host, .addresses, nil) | |
| var success: DarwinBoolean = false | |
| if let addresses = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray? { | |
| for s in addresses{ | |
| let theAddress = s as! NSData | |
| var hostname = [CChar](repeating: 0, count: Int(256)) | |
| print(theAddress) | |
| let p = theAddress as Data | |
| let value = p.withUnsafeBytes { (ptr: UnsafePointer<sockaddr>) in | |
| return ptr | |
| } | |
| if getnameinfo(value, socklen_t(theAddress.length), | |
| &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 { | |
| let numAddress = String(cString:hostname) | |
| results.append(numAddress) | |
| } | |
| } | |
| } | |
| return results | |
| } | |
| print(query(domain: "www.freebsdchina.org")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment