/// <summary>
        /// Converts bytes to <c>MPLSPackage</c>
        /// </summary>
        /// <param name="bytes">Received stream of bytes</param>
        /// <returns>Converted package</returns>
        /// <exception cref="InvalidMPLSPackageException">Thrown when a stream of bytes is not a valid package</exception>
        public static MPLSPackage FromBytes(byte[] bytes)
        {
            try
            {
                MPLSPackage package = new MPLSPackage();
                package.LabelStack = LabelStack.FromBytes(bytes);
                var stackLength = package.LabelStack.GetLength();

                package.ID           = BitConverter.ToInt32(bytes, stackLength);
                package.PacketLength = BitConverter.ToInt32(bytes, stackLength + 4);
                package.TTL          = (ushort)((bytes[stackLength + 9] << 8) + bytes[stackLength + 8]);

                package.SourceAddress = new IPAddress(new byte[]
                                                      { bytes[stackLength + 10], bytes[stackLength + 11], bytes[stackLength + 12], bytes[stackLength + 13] });
                package.DestAddress = new IPAddress(new byte[]
                {
                    bytes[stackLength + 14], bytes[stackLength + 15], bytes[stackLength + 16], bytes[stackLength + 17]
                });

                package.Port = (ushort)((bytes[stackLength + 19] << 8) + bytes[stackLength + 18]);

                var usefulPayload = new List <byte>();
                usefulPayload.AddRange(bytes.ToList()
                                       .GetRange(stackLength + 20, package.PacketLength - HeaderLength));

                package.Payload = Encoding.ASCII.GetString(usefulPayload.ToArray());
                return(package);
            }
            catch (Exception)
            {
                throw new InvalidMPLSPackageException();
            }
        }
Exemple #2
0
        /// <summary>
        /// Converts a stream of bytes to a valid LabelStack.
        /// </summary>
        /// <param name="bytes">A stream of bytes that will be converted.</param>
        /// <returns>Valid label stack</returns>
        public static LabelStack FromBytes(byte[] bytes)
        {
            LabelStack labelStack = new LabelStack();

            if (bytes[0] == StackNotEmptyFlag)
            {
                var index = 1;
                while (true)
                {
                    var label = new Label
                    {
                        Number = (short)((bytes[index + 1] << 8) + bytes[index]),
                        TTL    = bytes[index + 2]
                    };
                    var bottomOfStack = bytes[index + 3];

                    labelStack.Labels.Push(label);
                    if (bottomOfStack == 0xff)
                    {
                        break;
                    }

                    index = index + 4;
                }
            }
            return(labelStack);
        }
        public static LabelStack returnToStack(byte [] bytes)
        {
            LabelStack my_Stack = new LabelStack();
            int        id       = 1;

            if (bytes[0] == stackNotEmpty) // look at flag
            {
                while (true)
                {
                    Label label = new Label //construction of label: | label_Number 2 bytes | TTL 1 byte | INFO 1 byte |
                    {
                        labelNumber = (ushort)((bytes[id + 1] << 8) + bytes[id]),
                        TTL         = bytes[id + 2]
                    };

                    my_Stack.labels.Push(label);
                    if (bytes[id + 3] == 0xff) // check the bottom of Stack
                    {
                        break;
                    }

                    id = id + 4; // size of label=4 bytes
                }
            }

            return(my_Stack);
        }
Exemple #4
0
        // package looks like : | length of label stack | message ID 1 BYTE | TTL 1 byte | Source Address 4 bytes | Dest Address 4 bytes | Port 2 bytes | CurrentNode 4 bytes| length of payload |
        public static Package returnToPackage(byte[] bytes)
        {
            Package myPackage = new Package();

            myPackage.labelStack = LabelStack.returnToStack(bytes);

            var length_of_stack = myPackage.labelStack.GetLengthOfStack();

            int headerLength = 24 + length_of_stack;

            //myPackage.package_length = headerLength + myPackage.payload.Length;
            myPackage.messageID          = BitConverter.ToInt32(bytes, length_of_stack);
            myPackage.package_length     = BitConverter.ToInt32(bytes, length_of_stack + 4);
            myPackage.TTL                = (ushort)((bytes[length_of_stack + 9] << 8) + bytes[length_of_stack + 8]);
            myPackage.SourceAddress      = new IPAddress(new byte[] { bytes[length_of_stack + 10], bytes[length_of_stack + 11], bytes[length_of_stack + 12], bytes[length_of_stack + 13] });
            myPackage.DestinationAddress = new IPAddress(new byte[] { bytes[length_of_stack + 14], bytes[length_of_stack + 15], bytes[length_of_stack + 16], bytes[length_of_stack + 17] });
            myPackage.Port               = (ushort)((bytes[length_of_stack + 19] << 8) + bytes[length_of_stack + 18]);
            myPackage.CurrentNodeIP      = new IPAddress(new byte[] { bytes[length_of_stack + 20], bytes[length_of_stack + 21], bytes[length_of_stack + 22], bytes[length_of_stack + 23] });

            List <byte> listForPayload = new List <byte>();

            listForPayload.AddRange(bytes.ToList()
                                    .GetRange(length_of_stack + 24, myPackage.package_length - headerLength));

            myPackage.payload = Encoding.ASCII.GetString(listForPayload.ToArray());
            //Console.WriteLine("Your payload: " +myPackage.payload);
            return(myPackage);
        }
 /// <summary>
 /// Peeks a top label from <c>LabelStack</c> without popping it.
 /// </summary>
 /// <returns>Peeked top labeled or null, if <c>LabelStack</c> is empty</returns>
 public Label PeekTopLabel()
 {
     if (LabelStack.IsEmpty())
     {
         return(null);
     }
     return(LabelStack.Labels.Peek());
 }
 /// <summary>
 /// Generates a message to be shown by a host or node, after receiving/sending package.
 /// </summary>
 /// <returns>Generated string</returns>
 public override string ToString()
 {
     if (LabelStack.IsEmpty())
     {
         return($"[ID={ID}, LabelStack={LabelStack}, Payload={Payload}, TTLStack={TTL}, Source={SourceAddress}, Destination={DestAddress}]");
     }
     else
     {
         var TTLStack = string.Join(", ", LabelStack.Labels.ToList().Select(label => label.TTL)) + ", " + TTL;
         return($"[ID={ID}, LabelStack={LabelStack}, Payload={Payload}, TTLStack={TTLStack}, Source={SourceAddress}, Destination={DestAddress}]");
     }
 }
        /// <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());
        }
 /// <summary>
 /// Initalizes MPLS package and sets default value of TTL.
 /// </summary>
 public MPLSPackage()
 {
     LabelStack = new LabelStack();
     TTL        = DefaultTTL;
 }
Exemple #9
0
 public Package()
 {
     labelStack = new LabelStack();
     TTL        = 255; // default value
 }