どうやら、Traceroute を作成する方法は ICMP だけではありません。This and this answer は、UDP パケット (またはその他のパケット) を低い TTL で送信し、ICMP メッセージを待つことが可能であることを示しています。
これをC#で実装するにはどうすればよいですか? System.IO.Sockets? TCP オブジェクト? 誰でも簡単/最良の方法を知っていますか?
更新 1:
次のコードは、TTL に到達したときに正しく例外をスローするようです。返された UDP パケットから情報を抽出するにはどうすればよいですか?
受信している UDP パケットが自分宛てのものであること (ホスト上の他のアプリケーションではないこと) を確認するにはどうすればよいですか?
public void PingUDPAsync(IPAddress _destination, short ttl)
{
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient(21000);
udpClient.Ttl = ttl;
// udpClient.DontFragment = true;
try
{
udpClient.Connect(_destination, 21000);
// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
udpClient.Send(sendBytes, sendBytes.Length);
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());
udpClient.Close();
}
catch (SocketException socketException)
{
Console.WriteLine(socketException.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}