示例#1
0
        public byte[] ToBytes()
        {
            byte[] byteValue;
            byte[] payload    = Payload.ToBytes();
            byte[] ipv6Packet = new byte[Ipv6HeaderLength + payload.Length];
            int    offset     = 0;

            ipv6Packet[offset++] = (byte)((Version << 4) | ((TrafficClass >> 4) & 0xF));
            ipv6Packet[offset++] = (byte)((uint)((TrafficClass << 4) & 0xF0) | (uint)((Flow >> 16) & 0xF));
            ipv6Packet[offset++] = (byte)((Flow >> 8) & 0xFF);
            ipv6Packet[offset++] = (byte)(Flow & 0xFF);


            byteValue = NetUtilities.FromLittleEndian(PayloadLength);
            Array.Copy(byteValue, 0, ipv6Packet, offset, byteValue.Length);
            offset += byteValue.Length;

            ipv6Packet[offset++] = (byte)NextHeader;
            ipv6Packet[offset++] = (byte)HopLimit;

            byteValue = SourceAddress.GetAddressBytes();
            Array.Copy(byteValue, 0, ipv6Packet, offset, byteValue.Length);
            offset += byteValue.Length;

            byteValue = DestinationAddress.GetAddressBytes();
            Array.Copy(byteValue, 0, ipv6Packet, offset, byteValue.Length);
            offset += byteValue.Length;

            Array.Copy(payload, 0, ipv6Packet, offset, payload.Length);

            return(ipv6Packet);
        }
示例#2
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);
        }