diff options
Diffstat (limited to 'c/ping.c')
| -rw-r--r-- | c/ping.c | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -14,6 +14,19 @@ static void put_ip(unsigned char *ip) { for (i = 0; i < 4; i++) { putu(ip[i]); if (i < 3) putc('.'); } } +/* wait up to ~2s for an echo reply without blocking in the kernel: a lost + reply (some hosts rate-limit ICMP) must cost one "timeout" line, not + minutes wedged inside net_recv's pump-counted wait. */ +static unsigned char wait_reply(unsigned char s) { + unsigned char t, r; + for (t = 0; t < 40; t++) { + r = net_recv_nb(s, rbuf, (unsigned char)sizeof(rbuf)); + if (r != 0xFE) return r; /* reply (or error) */ + msleep(50); + } + return 0xFF; /* ~2s with no reply */ +} + void main(void) { char *arg; unsigned char s, seq, got = 0, i; @@ -35,7 +48,7 @@ void main(void) { puts("PING "); put_ip(dst); nl(); for (seq = 1; seq <= 4; seq++) { net_send(s, payload, 4); - if (net_recv(s, rbuf, (unsigned char)sizeof(rbuf)) != 0xFF) { + if (wait_reply(s) != 0xFF) { got++; puts(" reply from "); put_ip(dst); puts(" seq="); putu(seq); nl(); } else { |
