如何从Swift的DNS查询获取真正的IP地址? [英] How can I get a real IP address from DNS query in Swift?

查看:158
本文介绍了如何从Swift的DNS查询获取真正的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Swift 中的DNS查询中获取IP连接(如192.168.0.1或87.12.56.50)。我尝试100次100种不同的方法...没有什么帮助我,所以我必须要求帮助(我希望这里有一些民间的);
这是我的代码到目前为止:

I want to get the IP dress (like 192.168.0.1 or 87.12.56.50) from DNS query in Swift. I tried 100 times with 100 diffrent methods ... Nothing helped me, so I'll have to ask for help (I hope there are alots of folk here ;)) This is my code so far:

let host = CFHostCreateWithName(nil,"subdomain.of.stackoverflow.com").takeUnretainedValue();
CFHostStartInfoResolution(host, .Addresses, nil);
var success: Boolean = 0;
let addresses = CFHostGetAddressing(host, &success).takeUnretainedValue() as NSArray;
if(addresses.count > 0){
   let theAddress = addresses[0] as NSData;
   println(theAddress);
}

OK ...这些是我试图实现的代码的链接,没有成功:
https://gist.github.com/mikeash/bca3a341db74221625f5 < br>
如何在iOS上执行DNS查询 < br>
从NSData创建Swift中的数组对象

CFHostGetAddressing()是否支持ipv6 DNS条目?

执行简单的DNS查找Swift

所以如果我们完成了对这个问题的无意义的负面投票,我们可以尝试找到一个解决方案并帮助别人(li ke我)

OK ... These are the links for the code I tried to implement without success: https://gist.github.com/mikeash/bca3a341db74221625f5
How to perform DNS query on iOS
Create an Array in Swift from an NSData Object
Does CFHostGetAddressing() support ipv6 DNS entries?
Do a simple DNS lookup in Swift
So if we're done with nonsensical negative voting for this question we can try to find a solution and help other people (like me)

推荐答案

您的代码将地址作为套接字地址结构检索。
getnameinfo()可用于将地址转换为数字IP字符串
(代码从 https://stackoverflow.com/a/25627545/1187415
现已更新为 Swift 2 ):

Your code retrieves the address as a "socket address" structure. getnameinfo() can be used to convert the address into a numerical IP string (code recycled from https://stackoverflow.com/a/25627545/1187415, now updated to Swift 2):

let host = CFHostCreateWithName(nil,"www.google.com").takeRetainedValue()
CFHostStartInfoResolution(host, .Addresses, nil)
var success: DarwinBoolean = false
if let addresses = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray?,
    let theAddress = addresses.firstObject as? NSData {
    var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
    if getnameinfo(UnsafePointer(theAddress.bytes), socklen_t(theAddress.length),
        &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
            if let numAddress = String.fromCString(hostname) {
                print(numAddress)
            }
    }
}

输出(示例): 173.194。 112.147

请注意第一行中 takeRetainedValue()的用法,因为
CFHostCreateWithName()在其名称中具有创建,因此返回一个(+1)保留的
对象。

Note also the usage of takeRetainedValue() in the first line, because CFHostCreateWithName() has "Create" in its name the therefore returns a (+1) retained object.

Swift 3 / Xcode 8的更新:

let host = CFHostCreateWithName(nil,"www.google.com" as CFString).takeRetainedValue()
CFHostStartInfoResolution(host, .addresses, nil)
var success: DarwinBoolean = false
if let addresses = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray?,
    let theAddress = addresses.firstObject as? NSData {
    var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
    if getnameinfo(theAddress.bytes.assumingMemoryBound(to: sockaddr.self), socklen_t(theAddress.length),
                   &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
        let numAddress = String(cString: hostname)
        print(numAddress)
    }
}

或者,要获取所有 IP地址对于主持人:

Or, to get all IP addresses for the host:

let host = CFHostCreateWithName(nil,"www.google.com" as CFString).takeRetainedValue()
CFHostStartInfoResolution(host, .addresses, nil)
var success: DarwinBoolean = false
if let addresses = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray? {
    for case let theAddress as NSData in addresses {
        var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
        if getnameinfo(theAddress.bytes.assumingMemoryBound(to: sockaddr.self), socklen_t(theAddress.length),
                       &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
            let numAddress = String(cString: hostname)
            print(numAddress)
        }
    }
}

这篇关于如何从Swift的DNS查询获取真正的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆