point-source / dart_ping

Multi-platform network ping utility for Dart
31 stars 14 forks source link

Does not catch/report "Network is unreachable" errors on Android #20

Open fernando-s97 opened 2 years ago

fernando-s97 commented 2 years ago
Future<bool> _ping() async {
    try {
      final results = await Ping(ipAddress, count: 4).stream.toList();

      if (results.isEmpty) {
        return false;
      }

      final summary = results.last.summary!;
      final successRate = summary.received / summary.transmitted;
      return successRate > 0.5;
    } catch (e) {
      return false;
    }
  }

The code above always return immediately, doesn't matter if I set the count to 4 or 40. The result is always empty and immediate.

I'm using an Android 11 and the ipAddress is from an iot device (without internet) that I'm connected to.

Also, when using emulated terminals I get "Network is unreachable", but my app can properly communicate with the device.

point-source commented 2 years ago

Are you by chance using a language other than English for your Android system/OS primary language? Have you tried debugging or printing out any exceptions that get caught? It seems to me that your code would fail silently in the event of an exception being thrown.

fernando-s97 commented 2 years ago

No. English is the primary language.

I added a breakpoint in both return false; statements, and the only that is reached is the one from if (results.isEmpty).

One thing that I noticed, is that when I also ping with Termux (after connecting with my app), I also get Network is unreachable. Therefore, there's a chance this isn't a bug in the library.

point-source commented 2 years ago

Well, I would say it's probably a bug that it isn't returning anything at all. It should at least return or throw the error but I need to figure out how to reproduce this and so far I haven't been able to.

fernando-s97 commented 2 years ago

I could keep this code for some time in my app and try to find what's happening, but I need to know where should I add some logs.

point-source commented 2 years ago

A good place to start would be to try the raw command like so:

Future<bool> _ping() async {
    try {
      final ping = Ping(ipAddress, count: 4);

      print(ping.command); // Outputs the command that will be sent to the shell/ping process

      final results = await ping.stream.toList();

      if (results.isEmpty) {
        return false;
      }

      final summary = results.last.summary!;
      final successRate = summary.received / summary.transmitted;
      return successRate > 0.5;
    } catch (e) {
      return false;
    }
  }

Then, copy that command and run it in a terminal emulator on your Android device or emulator and see what it does. If it outputs an error that isn't being caught by dart_ping, then copy that error here as a comment so I can add parsing for it.

fernando-s97 commented 2 years ago

Ok. I'll try to do this tomorrow morning

On Tue, 15 Feb 2022 at 21:58, PointSource @.***> wrote:

A good place to start would be to try the raw command like so:

Future _ping() async { try { final ping = Ping(ipAddress, count: 4);

  print(ping.command); // Outputs the command that will be send to the shell/ping process

  final results = await ping.stream.toList();

  if (results.isEmpty) {
    return false;
  }

  final summary = results.last.summary!;
  final successRate = summary.received / summary.transmitted;
  return successRate > 0.5;
} catch (e) {
  return false;
}

}

Then, copy that command and run it in a terminal emulator on your Android device or emulator and see what it does. If it outputs an error that isn't being caught by dart_ping, then copy that error here as a comment so I can add parsing for it.

— Reply to this email directly, view it on GitHub https://github.com/point-source/dart_ping/issues/20#issuecomment-1040958966, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE3FQASE6KQPNHPDY3RXBH3U3LZDPANCNFSM5OEXR5YA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

fernando-s97 commented 2 years ago

The output of ping -O -n -W 2 -i 1 -t 255 -c 4 10.200.116.1 is connect: Network is unreachable

point-source commented 2 years ago

Okay. Looks like I need to see why dart_ping isn't catching that error and reporting it back to you. Nonetheless, the fact that you can't ping it due to the network being unreachable is a separate issue between your devices and not related to this package. I will work on getting dart_ping to be more informative about this. Thanks

ckerens commented 1 year ago

I was able to reproduce this error by statically assigning an IP address outside of the subnet. The errors I get are Destination Host Unreachable or Network is unreachable depending on whether or not the Wi-Fi is connected.

I can submit a PR for this. I'll add new ErrorType.unreachable and the regex to the parser.