public override byte[] GetProtocolPacketBytes(byte[] payLoad)
        {
            var offset = 0;

            var ipv6Packet = new byte[Ipv6HeaderLength + payLoad.Length];

            ipv6Packet[offset++] = (byte)((Version << 4) | ((TrafficClass >> 4) & 0xF));

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

            Console.WriteLine("Next header = {0}", NextHeader);

            byte[] byteValue = BitConverter.GetBytes(ipPayloadLength);
            Array.Copy(byteValue, 0, ipv6Packet, offset, byteValue.Length);
            offset += byteValue.Length;

            ipv6Packet[offset++] = NextHeader;
            ipv6Packet[offset++] = 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);
        }
Ejemplo n.º 2
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);
        }
        public Stream ToStream()
        {
            MemoryStream mem = null;

            if (null != Packet)
            {
                if (0 <= Packet.Length)
                {
                    mem          = new MemoryStream(Packet);
                    mem.Position = 0;
                    return(mem);
                } /* Packet has data. */
            }     /* Packet is not null. */

            mem = new MemoryStream();
            using (var writer = new BinaryWriter(mem))
            {
                // Combine Versiona and IHL nibbles and write complete byte.
                writer.Write(Version.AddLowNibble(IHL));
                // Write TOS byte.
                writer.Write(TOS);
                // Write total length bytes.
                writer.Write(TotalLength);
                // Write identification bytes.
                writer.Write(Identification);

                // Create 2 byte long data type and assign flags to it.
                ushort s = (ushort)Flags.ToByte();
                // Combine and write the flags and the fragment offset.
                writer.Write((ushort)(s + FragmentOffset));
                // Write ttl byte.
                writer.Write(TTL);
                // Write protocl byte.
                writer.Write((byte)Protocol);
                // Write checksum bytes.
                writer.Write(HeaderChecksum);
                // Write source address bytes.
                writer.Write(SourceAddress.GetAddressBytes());
                // Write destination address bytes.
                writer.Write(DestinationAddress.GetAddressBytes());
                // Write options and padding bytes.
                writer.Write(OptionsAndPadding);
                // Write data payload bytes.
                writer.Write(Data);
            }
            return(mem);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Converts <c>MPLSPackage</c> to bytes
        /// </summary>
        /// <returns>Converted package</returns>
        public byte[] ToBytes()
        {
            List <byte> result = new List <byte>();

            PacketLength = HeaderLength + Payload.Length;

            result.AddRange(LabelStack.GetBytes());
            result.AddRange(BitConverter.GetBytes(ID));
            result.AddRange(BitConverter.GetBytes(PacketLength));
            result.AddRange(BitConverter.GetBytes(TTL));
            result.AddRange(SourceAddress.GetAddressBytes());
            result.AddRange(DestAddress.GetAddressBytes());
            result.AddRange(BitConverter.GetBytes(Port));
            result.AddRange(Encoding.ASCII.GetBytes(Payload ?? ""));

            return(result.ToArray());
        }
        public override byte[] GetProtocolPacketBytes(byte[] payLoad, ushort crc)
        {
            var index = 0;

            var ipv4Packet = new byte[Ipv4HeaderLength + payLoad.Length];

            ipv4Packet[index++] = (byte)((Version << 4) | ipLength);
            ipv4Packet[index++] = TypeOfService;

            byte[] byteValue = BitConverter.GetBytes(ipTotalLength);
            Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
            index += byteValue.Length;

            byteValue = BitConverter.GetBytes(ipId);
            Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
            index += byteValue.Length;

            byteValue = BitConverter.GetBytes(ipOffset);
            Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
            index += byteValue.Length;

            ipv4Packet[index++] = Ttl;
            ipv4Packet[index++] = Protocol;
            ipv4Packet[index++] = 0;
            ipv4Packet[index++] = 0;

            byteValue = SourceAddress.GetAddressBytes();
            Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
            index += byteValue.Length;

            byteValue = DestinationAddress.GetAddressBytes();
            Array.Copy(byteValue, 0, ipv4Packet, index, byteValue.Length);
            index += byteValue.Length;

            Array.Copy(payLoad, 0, ipv4Packet, index, payLoad.Length);
            index += payLoad.Length;

            Checksum = crc;

            byteValue = BitConverter.GetBytes(ipChecksum);
            Array.Copy(byteValue, 0, ipv4Packet, 10, byteValue.Length);

            return(ipv4Packet);
        }
Ejemplo n.º 6
0
        public byte[] PacketToBytes()
        {
            List <byte> packetbytes = new List <byte>();

            PacketLength = PacketHeaderLength + Payload.Length;

            packetbytes.AddRange(BitConverter.GetBytes(PacketLength));
            packetbytes.AddRange(BitConverter.GetBytes(ID));
            packetbytes.AddRange(SourceAddress.GetAddressBytes());
            packetbytes.AddRange(DestinationAddress.GetAddressBytes());
            packetbytes.AddRange(BitConverter.GetBytes(Frequency));
            packetbytes.AddRange(BitConverter.GetBytes(Bandwith));
            packetbytes.AddRange(BitConverter.GetBytes(BitRate));
            packetbytes.AddRange(BitConverter.GetBytes(Performance));
            packetbytes.AddRange(BitConverter.GetBytes(Port));
            packetbytes.AddRange(BitConverter.GetBytes(slotWindow.FirstSlot));
            packetbytes.AddRange(BitConverter.GetBytes(slotWindow.NumberofSlots));
            packetbytes.AddRange(Encoding.ASCII.GetBytes(Payload ?? ""));
            return(packetbytes.ToArray());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Serializes and encrypts the message into a byte array.
        /// </summary>
        /// <param name="sharedKey">The shared encryption key.</param>
        /// <returns>The serialized message.</returns>
        public byte[] ToArray(SymmetricKey sharedKey)
        {
            using (var ms = new EnhancedMemoryStream(2048))
            {
                var addressBytes = SourceAddress.GetAddressBytes();

                if (addressBytes.Length != 4)
                {
                    throw new InvalidOperationException("UdpBroadcastMessage supports only IPv4 address.");
                }

                ms.WriteInt32Le(Magic);
                ms.WriteInt64Le(TimeStampUtc.Ticks);
                ms.WriteBytesNoLen(SourceAddress.GetAddressBytes());
                ms.WriteByte((byte)MessageType);
                ms.WriteByte((byte)BroadcastGroup);
                ms.WriteBytes16(Payload);
                ms.WriteBytesNoLen(Crypto.GetSalt4());

                return(Crypto.Encrypt(ms.ToArray(), sharedKey));
            }
        }
Ejemplo n.º 8
0
        public byte[] convertToBytes()
        {
            List <byte> package_in_Bytes = new List <byte>();
            // const int headerLength=24+label
            int headerLength = 24 + labelStack.GetLengthOfStack();

            package_length = payload.Length + headerLength;

            package_in_Bytes.AddRange(labelStack.GetStackInBytes());           //length of stack
            package_in_Bytes.AddRange(BitConverter.GetBytes(messageID));       // 4 bytes
            package_in_Bytes.AddRange(BitConverter.GetBytes(package_length));  //4bytes
            package_in_Bytes.AddRange(BitConverter.GetBytes(TTL));             // 2 bytes
            package_in_Bytes.AddRange(SourceAddress.GetAddressBytes());        //4 bytes
            package_in_Bytes.AddRange(DestinationAddress.GetAddressBytes());   // 4 bytes
            package_in_Bytes.AddRange(BitConverter.GetBytes(Port));            // 2 bytes
            package_in_Bytes.AddRange(CurrentNodeIP.GetAddressBytes());        //4 bytes
            package_in_Bytes.AddRange(Encoding.ASCII.GetBytes(payload ?? "")); // length of payload

            //package_length = package_in_Bytes.Count;
            //Console.WriteLine("Package length: " +package_length);
            return(package_in_Bytes.ToArray());
        }
        public Stream ToStream()
        {
            MemoryStream mem = null;

            if (null != Packet)
            {
                if (0 <= Packet.Length)
                {
                    mem          = new MemoryStream(Packet);
                    mem.Position = 0;
                    return(mem);
                } /* Packet has data. */
            }     /* Packet is not null. */


            byte b;

            byte[] buffer;
            mem = new MemoryStream();
            using (var writer = new BinaryWriter(mem))
            {
                b = Version.AddLowNibble(TrafficClass.HighNibble());
                writer.Write(b);
                b = TrafficClass.LowNibble().AddLowNibble(FlowLabel.GetBits(0, 4).ToByte());
                writer.Write(b);
                writer.Write((ushort)FlowLabel.GetBits(4, 12).ToInt());
                writer.Write(PayloadLength);
                writer.Write(NextHeader);
                writer.Write(HopLimit);
                writer.Write(SourceAddress.GetAddressBytes());
                writer.Write(DestinationAddress.GetAddressBytes());
                writer.Write(Data);
                buffer = mem.ToArray();
            }
            return(mem);
        }