Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Problem with pinging ip or hostnames... The problem is that always receive Status : TTL Expire even when host is not reachable,doesnt exist, or other ...

code is this :

PingReply Reply = await pingo.SendPingAsync(text_ip.Text, 4000, buffer, options);
            Toast.MakeText(Activity, Reply.Status.ToString(), ToastLength.Long).Show();
            switch (Reply.Status)
            {
                case IPStatus.Success:

                    Toast.MakeText(Activity, Reply.Status.ToString(), ToastLength.Long).Show();
                    break;

                case IPStatus.TimedOut:

                    Toast.MakeText(Activity, Reply.Status.ToString(), ToastLength.Long).Show();
                    break;

                case IPStatus.TimeExceeded:

                    Toast.MakeText(Activity, Reply.Status.ToString(), ToastLength.Long).Show();
                    break;

                case IPStatus.DestinationHostUnreachable:


                   Toast.MakeText(Activity, Reply.Status.ToString(), ToastLength.Long).Show();
                    break;

                case IPStatus.DestinationNetworkUnreachable:

                    Toast.MakeText(Activity, Reply.Status.ToString(), ToastLength.Long).Show();
                    break;

                case IPStatus.DestinationUnreachable:

                    Toast.MakeText(Activity, Reply.Status.ToString(), ToastLength.Long).Show();
                    break;
            }



For example when add a ip adress that is not assigned to any device and is doesnt return : Destination Host Unreachable or something else , but only ttl expired !

Whats wrong ?

What I have tried:

Tried to set ttl to 64 hops , 128 , and many other options, but is the same thing. i'm trying to ping a modem that im connected with Wireless , the default gateway adress is : 192.168.1.1 . When ping this everything is ok , but for example if ping a ip that is not assigned somewhere for example 192.168.1.123 it always return ttl expired , when it should be : Destination Host Unreachable . Thank you for your time . In opossite adb shell ping 192.168.1.123 returns : Destination host unreachable
Posted
Updated 13-Mar-17 0:12am
v2

1 solution

You are trying to ping a system in the same network. So there is no router involved that might return a Destination Host Unreachable. There is no other device reacting on the echo request besides the adressed one and if that is not present a time out will occur.

When using the ADB shell, the ping command is executed on your Android device which is probably located in a different subnet (WLAN). Then a router is involved (forwarding packages from WLAN to LAN) and may generate an unreachable message.

[EDIT]
I overlooked that the problem is that TTL expired is returned instead of a time out.

When using Mono, the used sources can be found at mono/Ping.cs at master · mono/mono · GitHub[^].
That calls the system ping command and checks the exit code:
C#
if (!ping.WaitForExit (timeout) || (ping.HasExited && ping.ExitCode == 2))
    status = IPStatus.TimedOut;
else if (ping.ExitCode == 0)
    status = IPStatus.Success;
else if (ping.ExitCode == 1)
    status = IPStatus.TtlExpired;

So TTL expired is set when there was no timeout, no error or success (exit code 2 or 0), and the exit code was 1.
See ping(8) - Linux man page[^] for the cases of returning 1:
Quote:
If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

So a return value of 1 indicates that no response has been received (for various reasons) and the Mono implementation sets the TTL expired status in this case. I would expect that a time out status would be returned in such cases. But it seems that the timeout detection of the Mono implementation is not triggered here (e.g. because the ping command exits upon on it's own timeout before).

So there are two possible solutions:

  1. Treat a TTL expired as host not alive (but this will then ignore ignore real TTL expired detections)
  2. Check the Mono sources to see if the internal timeout value can be lowered (or the value passed to the ping command can be enhanced) and send a bug report

[/EDIT]
 
Share this answer
 
v2
Comments
Emin Kokalari 13-Mar-17 10:15am    
Ok ok .... but the prob is not with Destination Host Unreachable ... I just said it as exaple, in fact every ping request that i made it returns Ttl Expired if not success , even if it should be Time Out , or Unreachable , Or Cannot Resolve , or something else
Jochen Arndt 13-Mar-17 11:05am    
OK. Now I understand. You got TTL expired but it should be time out (or destination unreachable).

When there are no routers involved, you should not get TTL expired. Possible reasons might be a TTL of zero (you have set different values) or you have routers involved producing a circular route (wrong routing configuration somewhere; probably on the system running your code).

Where are you executing your code (PC or an Android device)?

To check this with Windows open a command terminal and type
tracert 192.168.1.123
That should show circular loops.

If it is on an Android device it might be also a bug in Mono.
See https://github.com/mono/mono/blob/master/mcs/class/System/System.Net.NetworkInformation/Ping.cs

A quick look shows that TTL expired is returned when the exit code is 1 (line 339). That might be the reason. I will add this to my answer.
Emin Kokalari 13-Mar-17 12:23pm    
I didn't see that line exaclty in the ping class at mono project. Anyway thank you , that's exactly what i looked for . Thank you again !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900