Search code examples
iosswiftcore-foundation

Convert NSData to sockaddr struct in swift


I'm trying to do a simple DNS lookup in swift. So far, here is the code that I have:

let hostRef = CFHostCreateWithName(kCFAllocatorDefault, "google.com").takeRetainedValue()
var resolved = CFHostStartInfoResolution(hostRef, CFHostInfoType.Addresses, nil)
let addresses = CFHostGetAddressing(hostRef, &resolved).takeRetainedValue() as NSArray

At this point, each element in the "addresses" NSArray is a CFDataRef object wrapping a sockaddr struct.

Since CFDataRef can be toll-free bridged to NSData, I can loop through them like so:

for address: AnyObject in addresses {
  println(address)  // address is of type NSData.
}

So far so good (I think). This prints out valid looking data when I run it in a unit test. Here is where I get stuck though. For the life of me, I can't figure out how to convert the bytes in the NSData object into a sockaddr struct.

How can I convert address.bytes, which is of type COpaquePointer?, into a c struct? Any help appreciated. I'm banging my head against the wall trying to figure this out.


Solution

  • For a simpler solution using getnameinfo, see Martin's answer here: How can I get a real IP address from DNS query in Swift?

    Updated for Swift 5 / IPv6:

    The objects returned by CFHostGetAddressing can be bridged to Swift as Data, and cast to in_addr/in6_addr by using withUnsafeBytes and assumingMemoryBound(to:).

    Here's a complete example that uses inet_ntop to convert IPv4/IPv6 addresses to strings:

    import CFNetwork
    import Foundation
    
    protocol NetworkAddress {
        static var family: Int32 { get }
        static var maxStringLength: Int32 { get }
    }
    extension in_addr: NetworkAddress {
        static let family = AF_INET
        static let maxStringLength = INET_ADDRSTRLEN
    }
    extension in6_addr: NetworkAddress {
        static let family = AF_INET6
        static let maxStringLength = INET6_ADDRSTRLEN
    }
    
    extension String {
        init<A: NetworkAddress>(address: A) {
            // allocate a temporary buffer large enough to hold the string
            var buf = ContiguousArray<Int8>(repeating: 0, count: Int(A.maxStringLength))
            self = withUnsafePointer(to: address) { rawAddr in
                buf.withUnsafeMutableBufferPointer {
                    String(cString: inet_ntop(A.family, rawAddr, $0.baseAddress, UInt32($0.count)))
                }
            }
        }
    }
    
    func addressToString(data: Data) -> String? {
        return data.withUnsafeBytes {
            let family = $0.baseAddress!.assumingMemoryBound(to: sockaddr_storage.self).pointee.ss_family
            // family determines which address type to cast to (IPv4 vs IPv6)
            if family == numericCast(AF_INET) {
                return String(address: $0.baseAddress!.assumingMemoryBound(to: sockaddr_in.self).pointee.sin_addr)
            } else if family == numericCast(AF_INET6) {
                return String(address: $0.baseAddress!.assumingMemoryBound(to: sockaddr_in6.self).pointee.sin6_addr)
            }
            return nil
        }
    }
    
    let host = CFHostCreateWithName(kCFAllocatorDefault, "google.com" as CFString).takeRetainedValue()
    var resolved = DarwinBoolean(CFHostStartInfoResolution(host, .addresses, nil))
    let addresses = CFHostGetAddressing(host, &resolved)?.takeUnretainedValue() as! [Data]?
    
    print(addresses?.compactMap(addressToString))
    

    You can use the NSData method getBytes(_, length:) method and pass the sockaddr struct to the inout parameter using the prefix & operator:

    var data: NSData ...
    var address: sockaddr ...
    
    data.getBytes(&address, length: MemoryLayout<sockaddr>.size)
    

    Updated for Swift 3:

    let host = CFHostCreateWithName(kCFAllocatorDefault, "google.com" as CFString).takeRetainedValue()
    var resolved = DarwinBoolean(CFHostStartInfoResolution(host, .addresses, nil))
    let addresses = CFHostGetAddressing(host, &resolved)?.takeUnretainedValue() as! [NSData]?
    
    if let data = addresses?.first {
        var storage = sockaddr_storage()
        data.getBytes(&storage, length: MemoryLayout<sockaddr_storage>.size)
    
        if Int32(storage.ss_family) == AF_INET {
            let addr4 = withUnsafePointer(to: &storage) {
                $0.withMemoryRebound(to: sockaddr_in.self, capacity: 1) {
                    $0.pointee
                }
            }
    
            // prints 74.125.239.132
            print(String(cString: inet_ntoa(addr4.sin_addr), encoding: .ascii))
        }
    }
    


    Updated 6/3/2015: Now that C structs can be easily zero-initialized, this becomes much simpler:

    let host = CFHostCreateWithName(kCFAllocatorDefault, "google.com").takeRetainedValue()
    var resolved = CFHostStartInfoResolution(host, .Addresses, nil)
    let addresses = CFHostGetAddressing(host, &resolved)?.takeUnretainedValue() as! [NSData]?
    
    if let data = addresses?.first {
        var storage = sockaddr_storage()
        data.getBytes(&storage, length: sizeof(sockaddr_storage))
    
        if Int32(storage.ss_family) == AF_INET {
            let addr4 = withUnsafePointer(&storage) { UnsafePointer<sockaddr_in>($0).memory }
    
            // prints 74.125.239.132
            println(String(CString: inet_ntoa(addr4.sin_addr), encoding: NSASCIIStringEncoding))
        }
    }
    


    Unfortunately this requires sockaddr to be initialized first. To avoid that, you could do something like this:

    func makeWithUnsafePointer<T>(body: UnsafePointer<T> -> ()) -> T {
        let ptr = UnsafePointer<T>.alloc(sizeof(T))
        body(ptr)
        return ptr.move()
    }
    
    let addr: sockaddr = makeWithUnsafePointer {
        data.getBytes($0 as UnsafePointer<sockaddr>, length: sizeof(sockaddr))
    }
    

    Or this:

    func makeWithUninitialized<T>(body: inout T -> ()) -> T {
        let ptr = UnsafePointer<T>.alloc(sizeof(T))
        body(&ptr.memory)
        return ptr.move()
    }
    
    let addr = makeWithUninitialized { (inout addr: sockaddr) in
        data.getBytes(&addr, length: sizeof(sockaddr))
    }
    

    For more discussion, see Swift: Pass Uninitialized C Structure to Imported C function