miken.mkndev
miken.mkndev

Reputation: 1951

Pure Swift TCP Connection

I am trying to build a class that can create a TCP connection using pure Swift code. Everything compiles file, but when the code below runs I get an EXC_BAD_ACCESS crash when calling the getaddrinfo function.

The crash occurs when trying to reference the servInfo variable in the last parameter of the getaddrinfo function. I have looked around the web and it appears the way I am declaring the UnsafeMutablePointer variable is correct as it needs to be set by the getaddrinfo function.

Does anyone have any ideas what's going on? I am building using the latest Swift 3 snapshot.

import Darwin
import Foundation

class SwiftTcp {

    //MARK: - Properties

    var serviceAddress = "0.0.0.0"
    var servicePort: Int = 8080

    //MARK: - Read-only properties

    private(set) var status: Int32 = 0

    //MARK: - Private variables

    // protocol configuration
    private var hints = addrinfo(
        ai_flags: AI_PASSIVE,
        ai_family: AF_UNSPEC,
        ai_socktype: SOCK_STREAM,
        ai_protocol: 0,
        ai_addrlen: 0,
        ai_canonname: nil,
        ai_addr: nil,
        ai_next: nil)

    // used to set the protocol info needed to make a socket connection
    private var servInfo: UnsafeMutablePointer<addrinfo> = nil

    //MARK: - Initialization

    deinit {
        freeaddrinfo(servInfo)
    }

    init() {

        SwiftLog.trace("[SwiftTcp][Connect] Initialize...")

        // get protocol info
        status = getaddrinfo(
            nil,
            UnsafePointer<Int8>(bitPattern: servicePort),
            &hints,
            &servInfo)

        if (status != 0) {
            let err = String(UTF8String: gai_strerror(status)) ?? "Unknown error code"
            SwiftLog.error("Unable to get address information: Error \(err)")
            return
        }

        #if DEBUG

            var info = servInfo

            repeat {

                let (clientIp, service) = sockaddrDescription(info.memory.ai_addr)
                let message = "HostIp: " + (clientIp ?? "?") + " at port: " + (service ?? "?")
                SwiftLog.debug(message)

                // get next or nil
                info = nil
                info = info.memory.ai_next

            } while info != nil

        #endif
    }

    func connect() {
        // do nothing
    }

    //MARK: - Private Helpers

    private func sockaddrDescription(addr: UnsafePointer<sockaddr>) -> (String?, String?) {

        var host : String?
        var service : String?

        var hostBuffer = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
        var serviceBuffer = [CChar](count: Int(NI_MAXSERV), repeatedValue: 0)

        if getnameinfo(
            addr,
            socklen_t(addr.memory.sa_len),
            &hostBuffer,
            socklen_t(hostBuffer.count),
            &serviceBuffer,
            socklen_t(serviceBuffer.count),
            NI_NUMERICHOST | NI_NUMERICSERV)

            == 0 {

            host = String(UTF8String: hostBuffer)
            service = String(UTF8String: serviceBuffer)
        }
        return (host, service)

    }

}

Upvotes: 0

Views: 686

Answers (1)

Martin R
Martin R

Reputation: 539685

The second argument in

status = getaddrinfo(
        nil,
        UnsafePointer<Int8>(bitPattern: servicePort),
        &hints,
        &servInfo)

makes no sense and causes the crash, as you are casting the number 8080 to a pointer.

getaddrinfo() expects a C string here (UnsafePointer<Int8> in Swift), but you can simply pass a Swift string, which is automatically converted (as explained in String value to UnsafePointer<UInt8> function parameter behavior):

let servicePort = "8080"
status = getaddrinfo(
        nil,
        servicePort,
        &hints,
        &servInfo)

Upvotes: 2

Related Questions