453jerry/NetDiagnosis

User Error

Closed this issue · 4 comments

The ping method in the release environment is always wrong but in the debug environment it is fine
result=failed(Error Domain=NSPOSIXErrorDomain Code=57 "Socket is not connected")

Hi, I've built with the Release configuration and tested pinging both IPv6 and IPv4 addresses. But didn't get this error.

My XCode version is 14.3.1.

Screenshot 2023-06-10 at 17 18 46
let remoteAddr = IPAddr.create("13.107.21.200", addressFamily: .ipv4)
let pinger = try! Pinger.init(remoteAddr: remoteAddr)
pinger.ping { result in
    print(result)
}

\\ Ouput: 
\\ pong(NetDiagnosis.Pinger.Response(len: 64, from: 13.107.21.200, hopLimit: 116, sequence: 0, identifier: 15649, rtt: 0.033390045166015625))
let remoteAddr = IPAddr.create("2620:1ec:c11::200", addressFamily: .ipv6)
let pinger = try! Pinger.init(remoteAddr: remoteAddr)
pinger.ping { result in
    print(result)
}

\\ Output (IPv6 Env):
\\ pong(NetDiagnosis.Pinger.Response(len: 1024, from: 2620:1ec:c11::200, hopLimit: 51, sequence: 0, identifier: 26605, rtt: 0.059056997299194336))

\\ Output (IPv4 Env):
\\ failed(Error Domain=NSPOSIXErrorDomain Code=65 "No route to host")

So could you share more infomation such as target address , OS and net enviroment.

WeChat2c31550b9c43a03e0e015591b6c05d29
I deleted the Pinger+Trace.swift file

This is my test code
NetDemo.zip

Hi, this issue has been confirmed. The reason for this is incorrect use of assertion in IPAddr.create(_ addrss: String, addressFamily: AddressFamily) .

public static func create(_ addrss: String, addressFamily: AddressFamily) -> IPAddr {
        switch addressFamily {
        case .ipv4:
            var addr = in_addr()
            assert(inet_pton(AF_INET, addrss, &addr) == 1)
            return .ipv4(addr)
        case .ipv6:
            var addr = in6_addr()
            assert(inet_pton(AF_INET6, addrss, &addr) == 1)
            return .ipv6(addr)
        }
    }

In -O builds (the default for Xcode’s Release configuration), condition is not evaluated, and there are no effects.
https://developer.apple.com/documentation/swift/assert(_:_:file:line:)

I tried it and it's OK. Thank you