samiyr/SwiftyPing

Works fine with Wifi but not 4G

Opened this issue · 6 comments

Hello,

The following code works fine on Wifi but throws PingError.hostNotFound exception on 4G.
That makes no sense though, because the same host IP works on Wifi, but not on 4G.

do {
    let ping = try SwiftyPing(host: serverIP, configuration: PingConfiguration(interval: 0.5, with: 1), queue: DispatchQueue.global())
    ping.finished = { (result) in
        completed(true, result.roundtrip?.average ?? 0.0)
    }
    ping.targetCount = count
    try ping.startPinging()
} catch let error {
    print("Error: \(error.localizedDescription)")
    completed(false, 0.0)
}

I'm actually able to reproduce it with 4G.

I get the PingError error 10 (hostNotFound) when trying to ping IPv4 addresses. The same address works fine with Wifi.

I don't get this issue when pinging domain names on 4G.

I know this project isn't maintained anymore, which is a shame. But for the record I reopen this and leave it here.

I was able to pinpoint it exactlly.

In method getIPv4AddressFromHost() there is this block, where if the condition fails, it throws a PingError.hostNotFound error:

for address in addresses {
    let addrin = address.socketAddress
    if address.count >= MemoryLayout<sockaddr>.size && addrin.sa_family == UInt8(AF_INET) {
        data = address
        break
    }
}
               
if data?.count == 0 || data == nil {
    throw PingError.hostNotFound
}

The part that fails is addrin.sa_family == UInt8(AF_INET) as you see from the screenshot below:

Screenshot 2022-06-17 at 11 09 36

And then because data remains nil, it throws that exception.

But when on Wifi we have different values that pass this conditional correctly:

Screenshot 2022-06-17 at 11 13 54

I had the same issue, but I resolved the issue by changing the initialisation.

Throws error PingError.hostNotFound

SwiftPing(host: "ipAddress", 
          config: DAPingConfiguratio(interval: 0.5, with: 5),
          queue: .global())

Works:

SwiftPing(ipv4Address: "ipAddress", 
          config: DAPingConfiguratio(interval: 0.5, with: 5),
          queue: .global())

This is true, it works on Wifi but not on 4G. In my case, on 4g, the error is always timeout

houmie commented

I had the same issue, but I resolved the issue by changing the initialisation.

Throws error PingError.hostNotFound

SwiftPing(host: "ipAddress", 
          config: DAPingConfiguratio(interval: 0.5, with: 5),
          queue: .global())

Works:

SwiftPing(ipv4Address: "ipAddress", 
          config: DAPingConfiguratio(interval: 0.5, with: 5),
          queue: .global())

@PetterVangBraka But you assume that IPv4 is available. What if I want to ping a domain name, which can have many IPv4 addresses? for example facebook.com?

houmie commented

This is true, it works on Wifi but not on 4G. In my case, on 4g, the error is always timeout

@f-gonzalez Have you found a different solution for this? I'm still looking. A shame this repo has been abandoned.