我正在为课程制作IPv6 Traceroute程序。我正在使用MacOs 10.13.5 .NET Core SDK版本2.1.302。我无法将ICMP数据包发送到facebook.com。我的无线路由器和ISP都支持IPv6。当我转到“高级网络信息”时,看到“ TCP / IP”选项卡下有一个IPv6地址。
当我尝试发送数据包(甚至是空字节数组)时,出现以下错误:
Unhandled Exception: System.Net.Sockets.SocketException: Unknown error: -1
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint remoteEP)
at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 size, SocketFlags socketFlags, EndPoint remoteEP)
我有程序可以与IPv4连接并正常工作。由于Unix不支持TTL选项,因此我无法使用Ping类。由于保留了Raw套接字类型,因此指示我在使用IPv4 ping时使用Dgram套接字类型。
我的代码:
IPAddress[] ipv6Addresses = Array.FindAll(Dns.GetHostEntry("facebook.com").AddressList,a => a.AddressFamily == AddressFamily.InterNetworkV6);
IPEndPoint iep = new IPEndPoint(ipv6Addresses[0], 80);
EndPoint ep = (iep);
//Construct the packet
int packetSize = 0;
IcmpPacket packet = new IcmpPacket();
packet.Type = 8;
packet.SubCode = 0;
packet.CheckSum = UInt16.Parse("0");
packet.Identifier = UInt16.Parse("45");
packet.SequenceNumber = UInt16.Parse("0");
int packetData = 32;
packet.Data = new byte[packetData];
//Initialize the Packet Data
for (int a = 0; a < packetData; a++)
{
packet.Data[a] = Convert.ToByte('a');
}
packetSize = packetData + 8;
//Serialize the packet
packet.GenerateCheckSum();
Byte[] sendbuf = new Byte[packetSize];
sendbuf = packet.Serialize();
Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.IcmpV6);
//socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.HopLimit, 1);
socket.SendTo(packet.Serialize(), packetSize, SocketFlags.None, iep);