[英]How do I implement Traceroute using UDP?
显然,ICMP不是创建Traceroute的唯一方法。 这个和这个答案表明可以发送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您可以在此处查看有关Cisco UPD跟踪路由实施的详细答案。 它相当全面,可以轻松地针对特定的UDP端口。 您不会从目标获得UDP数据包。 而是,您收到ICMP答复以指示未收到流量。 您发出的UDP数据包包含一个随机响应端口号,并且主机跟踪哪些应用程序使用了哪些端口。 当ICMP响应被发回时,它被发送到主机IP和UDP标头中包含的响应端口。 然后,您的主机将看到该端口,并知道该端口已绑定到您的应用程序。 然后,它将数据包传递到您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.