Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Works fine with Wifi but not 4G #38

Open
houmie opened this issue Mar 23, 2022 · 6 comments
Open

Works fine with Wifi but not 4G #38

houmie opened this issue Mar 23, 2022 · 6 comments

Comments

@houmie
Copy link

houmie commented Mar 23, 2022

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)
}
@houmie houmie closed this as completed Jun 15, 2022
@houmie
Copy link
Author

houmie commented Jun 17, 2022

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.

@houmie houmie reopened this Jun 17, 2022
@houmie
Copy link
Author

houmie commented Jun 17, 2022

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

@PetterVangBraka
Copy link

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())

@f-gonzalez
Copy link

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

@houmie
Copy link
Author

houmie commented Sep 13, 2023

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
Copy link
Author

houmie commented Sep 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants