在C#中发送和接收ipv6数据包

时间:2019-05-04 07:49:27

标签: c# sockets ipv6

我无法接收到对IPV6数据包的确认

我已经连接,发送IPV6数据包,但是遇到绑定和接收功能 我认为这是IP地址问题或其他

在绑定函数的情况下,它给出错误: 请求的地址在其上下文中无效

在接收功能的情况下进入等待状态

public void PacketSend(int version)
    {

            // Default/initial values, the source should be a spoofed IP :-)

            IPAddress sourceAddress = IPAddress.Parse(GetIPAddress(version).Values.FirstOrDefault()),
            destAddress = IPAddress.Parse("[2001:db8:abcd::85]");
            IPAddress bindAddress;          
            bindAddress = IPAddress.IPv6Any;
            ushort sourcePort = 5150,
            destPort = 84;
            int messageSize = 16,
            sendCount = 5;
            // Start building the headers

            Console.WriteLine("Building the packet header...");

            byte[] builtPacket, payLoad = new byte[messageSize];

            UdpHeader udpPacket = new UdpHeader();

            ArrayList headerList = new ArrayList();

            Socket rawSocket = null;

            SocketOptionLevel socketLevel = SocketOptionLevel.IP;



            // Initialize the payload

            Console.WriteLine("Initialize the payload...");

            for (int i = 0; i < payLoad.Length; i++)

                payLoad[i] = (byte)'#';



            // Fill out the UDP header first

            Console.WriteLine("Filling out the UDP header...");

            udpPacket.SourcePort = sourcePort;

            udpPacket.DestinationPort = destPort;

            udpPacket.Length = (ushort)(UdpHeader.UdpHeaderLength + messageSize);

            udpPacket.Checksum = 0;



                        ProtocolHeaderDef.Ipv6Header ipv6Packet = new ProtocolHeaderDef.Ipv6Header();



                // Build the IPv6 header

                Console.WriteLine("Building the IPv6 header...");

                ipv6Packet.Version = 6;

                ipv6Packet.TrafficClass = 1;

                ipv6Packet.Flow = 2;

                ipv6Packet.HopLimit = 2;

                ipv6Packet.NextHeader = (byte)ProtocolType.Udp;

                ipv6Packet.PayloadLength = (ushort)(UdpHeader.UdpHeaderLength + payLoad.Length);

                ipv6Packet.SourceAddress = sourceAddress;

                ipv6Packet.DestinationAddress = destAddress;



                // Set the IPv6 header in the UDP header since it is required to calculate the

                //    pseudo header checksum

                Console.WriteLine("Setting the IPv6 header for pseudo header checksum...");

                udpPacket.ipv6PacketHeader = ipv6Packet;



                // Add the IPv6 header to the list of headers - headers should be added in the order

                //    they appear in the packet (i.e. IP first then UDP)

                Console.WriteLine("Adding the IPv6 header to the list of header, encapsulating packet...");

                headerList.Add(ipv6Packet);

                socketLevel = SocketOptionLevel.IPv6;





            // Add the UDP header to list of headers after the IP header has been added

            Console.WriteLine("Adding the UDP header to the list of header, after IP header...");

            headerList.Add(udpPacket);



            // Convert the header classes into the binary on-the-wire representation

            Console.WriteLine("Converting the header classes into the binary...");

            builtPacket = udpPacket.BuildPacket(headerList, payLoad);



            // Create the raw socket for this packet

            Console.WriteLine("Creating the raw socket using Socket()...");

            rawSocket = new Socket(sourceAddress.AddressFamily, SocketType.Raw, ProtocolType.Udp);



            // Bind the socket to the interface specified

            Console.WriteLine("Binding the socket to the specified interface using Bind()...");

            rawSocket.Bind(new IPEndPoint(bindAddress, 0));



            // Set the HeaderIncluded option since we include the IP header

            Console.WriteLine("Setting the HeaderIncluded option for IP header...");

            rawSocket.SetSocketOption(socketLevel, SocketOptionName.HeaderIncluded, 1);

            try
            {

                // Send the packet!

                Console.WriteLine("Sending the packet...");

                for (int i = 0; i < sendCount; i++)
                {
                    int rc = rawSocket.SendTo(builtPacket, new IPEndPoint(destAddress, destPort));
                    Console.WriteLine("send {0} bytes to {1}", rc, destAddress.ToString());
                }

                //IPEndPoint object will allow us to read datagrams sent from any source.
                EndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0);
                byte[] rec = new byte[128];
                var receiveBytes = rawSocket.ReceiveFrom(rec, ref RemoteIpEndPoint);
                //string returnData = Encoding.ASCII.GetString(receiveBytes);
            }

            catch (SocketException err)
            {

                Console.WriteLine("Socket error occurred: {0}", err.Message);



            }

            finally
            {

                // Close the socket

                Console.WriteLine("Closing the socket...");

                rawSocket.Close();

            }
        }

我想在c#中发送和接收IPV6数据包 我有IPV6地址,例如[2001:db8:abcd :: 7]:84

0 个答案:

没有答案