Ejemplo n.º 1
0
        public void Send(byte[] buffer, int length)
        {
            // make sure that a default destination IPEndpoint has been configured.
            if ((destinationIpAddress == IPv6Any) || (destinationPort == IPPortAny))
            {
                throw new SocketsException("Socket is not connected.");
            }

            UdpDatagram udpDatagram = new UdpDatagram();

            udpDatagram.DestinationPort = destinationPort;
            udpDatagram.SourcePort      = sourcePort;
            udpDatagram.AddPayload(buffer);
            udpDatagram.Checksum = 0;


            IPv6Packet packetUDP = new IPv6Packet();

            packetUDP.SourceAddress      = sourceIpAddress;
            packetUDP.DestinationAddress = destinationIpAddress;
            packetUDP.NextHeader         = IPv6Protocol.Udp;
            packetUDP.Payload            = udpDatagram;
            packetUDP.PayloadLength      = udpDatagram.Length;


            IPv6PseudoHeader ipv6PseudoHeader = new IPv6PseudoHeader(packetUDP.SourceAddress, packetUDP.DestinationAddress, packetUDP.PayloadLength, (byte)packetUDP.NextHeader);
            ushort           checkSum         = ipv6PseudoHeader.GetCheckSum();

            checkSum = NetUtilities.ComputeChecksum(checkSum, udpDatagram.ToBytes(), true);

            udpDatagram.Checksum = checkSum;

            NetworkingInterface.Send(packetUDP.ToBytes());
        }
Ejemplo n.º 2
0
        public ushort GetCheckSum()
        {
            if (pseudoHeader != null)
            {
                return(NetUtilities.ComputeChecksum(0, pseudoHeader, false));
            }

            throw new InvalidOperationException("Empty packet array.");
        }
Ejemplo n.º 3
0
        public static void PacketHandler(IPv6Packet ipv6Packet)
        {
            Icmpv6Packet icmpv6Handler = (Icmpv6Packet)ipv6Packet.Payload;

            if (icmpv6Handler.Icmpv6MessageType == Icmpv6MessageType.EchoRequest)
            {
                IPv6Packet packetEchoReply = new IPv6Packet();
                packetEchoReply.SourceAddress      = ipv6Packet.DestinationAddress;
                packetEchoReply.DestinationAddress = ipv6Packet.SourceAddress;
                packetEchoReply.NextHeader         = ipv6Packet.NextHeader;
                packetEchoReply.Flow          = ipv6Packet.Flow;
                packetEchoReply.HopLimit      = ipv6Packet.HopLimit;
                packetEchoReply.PayloadLength = ipv6Packet.PayloadLength;
                packetEchoReply.TrafficClass  = ipv6Packet.TrafficClass;
                packetEchoReply.Version       = ipv6Packet.Version;


                icmpv6Handler.Icmpv6MessageType = Icmpv6MessageType.EchoReply;
                Icmpv6EchoReply icmpv6EchoReply = new Icmpv6EchoReply((Icmpv6EchoRequest)icmpv6Handler.IcmpMessage);
                icmpv6Handler.IcmpMessage = icmpv6EchoReply;

                IPv6PseudoHeader ipv6PseudoHeader = new IPv6PseudoHeader(packetEchoReply.SourceAddress, packetEchoReply.DestinationAddress, packetEchoReply.PayloadLength, (byte)packetEchoReply.NextHeader);
                ushort           checkSum         = ipv6PseudoHeader.GetCheckSum();

                byte[] icmpData = icmpv6Handler.ToBytes();
                icmpData[2] = 0;
                icmpData[3] = 0;

                checkSum = NetUtilities.ComputeChecksum(checkSum, icmpData, true);

                icmpv6Handler.Checksum = checkSum;

                packetEchoReply.Payload = icmpv6Handler;

                NetworkInterface.Send(packetEchoReply.ToBytes());
            }
            else if (icmpv6Handler.Icmpv6MessageType == Icmpv6MessageType.EchoReply)
            {
                if (isResponseTimeout)
                {
                    return;
                }

                TimeSpan elapsed = DateTime.UtcNow - pingStart;

                replyTime = (short)elapsed.TotalMilliseconds;

//#if NETMF
//                replyTime = (short)elapsed.Milliseconds;
//#else
//                replyTime = (short)elapsed.TotalMilliseconds;
//#endif

                pingTimeout.Set();
            }
        }
Ejemplo n.º 4
0
        public static short SendEchoRequest(IPAddress destionationIPAddress)
        {
            IPv6PseudoHeader pv6PseudoHeader = new IPv6PseudoHeader(NetworkInterface.IPAddress, destionationIPAddress, 0x10, 58);
            ushort           checkSum        = pv6PseudoHeader.GetCheckSum();

            byte[] packet = new byte[16];
            packet[0] = 0x80;
            packet[1] = 0x00;
            packet[2] = 0x00; //Checksum
            packet[3] = 0x00; //Checksum

            packet[4] = 0x00;
            packet[5] = 0x01;
            packet[6] = 0x00;
            packet[7] = 0x0a;

            packet[8]  = 0x02;
            packet[9]  = 0xcd;
            packet[10] = 0x21;
            packet[11] = 0xf2;
            packet[12] = 0x00;
            packet[13] = 0x00;
            packet[14] = 0x00;
            packet[15] = 0x00;

            checkSum = NetUtilities.ComputeChecksum(checkSum, packet, true);

            Icmpv6Packet icmpv6Packet = new Icmpv6Packet();
            int          packetIndex  = 0;

            icmpv6Packet.FromBytes(packet, ref packetIndex);
            icmpv6Packet.Checksum = checkSum;

            IPv6Packet packetEchoRequest = new IPv6Packet();

            packetEchoRequest.DestinationAddress = destionationIPAddress;
            packetEchoRequest.SourceAddress      = NetworkInterface.IPAddress;
            packetEchoRequest.PayloadLength      = 0x10;
            packetEchoRequest.NextHeader         = IPv6Protocol.ICMPv6;
            packetEchoRequest.Payload            = icmpv6Packet;

            pingStart         = DateTime.UtcNow;
            isResponseTimeout = false;

            replyTime = -1;

            NetworkInterface.SendAndWait(packetEchoRequest.ToBytes());
            pingTimeout.WaitOne(5000, false);

            if (replyTime == -1)
            {
                isResponseTimeout = true;
            }

            return(replyTime);
        }
Ejemplo n.º 5
0
        public bool FromBytes(byte[] ipv6Packet)
        {
            byte[] addressBytes = new byte[16];
            uint   tempVal = 0, tempVal2 = 0;

            // Ensure byte array is large enough to contain an IPv6 header
            if (ipv6Packet.Length < Ipv6HeaderLength)
            {
                return(false);
            }

            tempVal = ipv6Packet[0];
            tempVal = (tempVal >> 4) & 0xF;
            Version = (byte)tempVal;

            tempVal      = ipv6Packet[0];
            tempVal      = (tempVal & 0xF) >> 4;
            TrafficClass = (byte)(tempVal | (uint)((ipv6Packet[1] >> 4) & 0xF));

            tempVal2      = ipv6Packet[1];
            tempVal2      = (tempVal2 & 0xF) << 16;
            tempVal       = ipv6Packet[2];
            tempVal       = tempVal << 8;
            Flow          = tempVal2 | tempVal | ipv6Packet[3];
            PayloadLength = NetUtilities.ToLittleEndian(BitConverter.ToUInt16(ipv6Packet, 4));
            NextHeader    = (IPv6Protocol)ipv6Packet[6];
            HopLimit      = ipv6Packet[7];

            Array.Copy(ipv6Packet, 8, addressBytes, 0, 16);
            SourceAddress = new IPv6Address(addressBytes);

            Array.Copy(ipv6Packet, 24, addressBytes, 0, 16);
            DestinationAddress = new IPv6Address(addressBytes);

            packetIndex += Ipv6HeaderLength;

            IPv6PseudoHeader ipv6PseudoHeader = new IPv6PseudoHeader(SourceAddress, DestinationAddress, PayloadLength, (byte)NextHeader);
            ushort           checkSum         = ipv6PseudoHeader.GetCheckSum();

            if (ipv6Packet.Length > packetIndex)
            {
                switch (NextHeader)
                {
                case IPv6Protocol.ICMPv6:

                    Icmpv6Packet icmpv6packet = new Icmpv6Packet();
                    if ((icmpv6packet.FromBytes(ipv6Packet, ref packetIndex)) == false)
                    {
                        return(false);
                    }
                    Payload = icmpv6packet;

                    checkSum = NetUtilities.ComputeChecksum(checkSum, Payload.ToBytes(), true);

                    if (checkSum != 0)
                    {
                        return(false);
                    }

                    break;

                case IPv6Protocol.Udp:

                    UdpDatagram udpDatagram = new UdpDatagram();
                    if ((udpDatagram.FromBytes(ipv6Packet, ref packetIndex)) == false)
                    {
                        return(false);
                    }
                    Payload = udpDatagram;

                    checkSum = NetUtilities.ComputeChecksum(checkSum, Payload.ToBytes(), true);

                    if (checkSum != 0)
                    {
                        return(false);
                    }
                    break;
                }
            }
            return(true);
        }