point-source / dart_ping

Multi-platform network ping utility for Dart
30 stars 13 forks source link

Fix #35: TTL and time are inverted on Windows #36

Closed ckerens closed 1 year ago

ckerens commented 1 year ago

The time and TTL capture groups were in the wrong order for Windows ping output. I converted the regex capture groups to named groups and also updated the regex to capture the Windows time<1ms output.

ping_parser.dart:

            var seq = match.groupNames.contains('seq') 
              ? match.namedGroup('seq') 
              : null;
            var ttl = match.namedGroup('ttl');
            var time = match.namedGroup('time');
            sink.add(
              PingData(
                response: PingResponse(
                  ip: match.namedGroup('ip'),
          // Summary
          if (data.contains(summaryStr)) {
            final match = summaryRgx.firstMatch(data);
            var tx = match?.namedGroup('tx');
            var rx = match?.namedGroup('rx');
            String? time;
            if ((match?.groupCount ?? 0) > 2) {
              time = match?.namedGroup('time');
            }

windows_ping.dart:

        responseStr: RegExp(r'Reply from'),
        responseRgx: RegExp(r'from (?<ip>.*): bytes=(?:\d+) time(?:=|<)(?<time>\d+)ms TTL=(?<ttl>\d+)'),
        summaryStr: RegExp(r'Lost'),
        summaryRgx: RegExp(r'Sent = (?<tx>\d+), Received = (?<rx>\d+), Lost = (?:\d+)'),

Ping command output:

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Console output:

Pinging 127.0.0.1
PingResponse(seq:null, ip:127.0.0.1, ttl:128, time:1.0 ms)
PingSummary(transmitted:1, received:1)
point-source commented 1 year ago

Thank you so much! This PR looks excellent and I must admit that I had missed issue #36 on accident. Thank you for covering this. I will review, merge, and publish asap.