1

显然 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());
        }

    }
4

2 回答 2

1

是的,System.Net.Sockets应该为您提供发送/接收 UDP/TCP 数据包所需的所有原始对象。大量在线文档和示例,您在问题中包含的两篇文章非常有趣并且是一个很好的起点:)

于 2011-09-01T20:30:58.097 回答
0

https://learningnetwork.cisco.com/thread/87497 您可以在此处查看详细了解思科 UPD 跟踪路由实施的答案。它相当全面,可以轻松适应特定的 UDP 端口。您不会从目标获得 UDP 数据包。相反,您会收到 ICMP 回复以指示未收到流量。您发起的 UDP 数据包包含一个随机响应端口号,并且您的主机跟踪哪些应用程序使用了哪些端口。当 ICMP 响应被发回时,它被发送到主机 IP 和包含在 UDP 头中的响应端口。然后您的主机将看到该端口并知道它已绑定到您的应用程序。然后它将数据包传递给您的应用程序。

于 2019-02-10T20:10:52.137 回答