显然ICMP不是创建一个路由跟踪的唯一途径。 This和this回答表明它可能具有低TTL发送UDP数据包(或任何其他),并等待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());
}
}
是的,应该的System.Net.Sockets为您提供所有你需要发送/接收UDP / TCP数据包的原始对象。文档和示例的大量在线,你在你的问题包括两篇文章都非常有趣,一个很好的起点:)
https://learningnetwork.cisco.com/thread/87497您可以检查出这里的答案是进入思科的UPD traceroute的实现细节。这是相当全面,可以很容易地适应于针对特定UDP端口。你没有得到一个UDP数据包从目标回。相反,你会得到一个ICMP回复指示交通没有收到。您发起的UDP数据包有一个包含随机响应端口号和主机跟踪端口是由什么应用程序使用了什么。当ICMP应答被发送回,它被发送到主机的IP和包括在UDP报头的响应端口。然后,您的主机将看到港口和知道它绑定到应用程序。然后,将数据包送给你的应用程序。