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);
        }
Exemplo n.º 2
0
        /// <summary cref="Packet.ToString(StringOutputType)" />
        public override string ToString(StringOutputType outputFormat)
        {
            var    buffer      = new StringBuilder();
            string color       = "";
            string colorEscape = "";

            if (outputFormat == StringOutputType.Colored || outputFormat == StringOutputType.VerboseColored)
            {
                color       = Color;
                colorEscape = AnsiEscapeSequences.Reset;
            }

            if (outputFormat == StringOutputType.Normal || outputFormat == StringOutputType.Colored)
            {
                // build the output string
                buffer.AppendFormat("{0}[IPv6Packet: SourceAddress={2}, DestinationAddress={3}, NextHeader={4}]{1}",
                                    color,
                                    colorEscape,
                                    SourceAddress,
                                    DestinationAddress,
                                    NextHeader);
            }

            if (outputFormat == StringOutputType.Verbose || outputFormat == StringOutputType.VerboseColored)
            {
                // collect the properties and their value
                Dictionary <string, string> properties = new Dictionary <string, string>();
                string ipVersion = Convert.ToString((int)Version, 2).PadLeft(4, '0');
                properties.Add("version", ipVersion + " .... .... .... .... .... .... .... = " + (int)Version);
                string trafficClass = Convert.ToString(TrafficClass, 2).PadLeft(8, '0').Insert(4, " ");
                properties.Add("traffic class", ".... " + trafficClass + " .... .... .... .... .... = 0x" + TrafficClass.ToString("x").PadLeft(8, '0'));
                string flowLabel = Convert.ToString(FlowLabel, 2).PadLeft(20, '0').Insert(16, " ").Insert(12, " ").Insert(8, " ").Insert(4, " ");
                properties.Add("flow label", ".... .... .... " + flowLabel + " = 0x" + FlowLabel.ToString("x").PadLeft(8, '0'));
                properties.Add("payload length", PayloadLength.ToString());
                properties.Add("next header", NextHeader.ToString() + " (0x" + NextHeader.ToString("x") + ")");
                properties.Add("hop limit", HopLimit.ToString());
                properties.Add("source", SourceAddress.ToString());
                properties.Add("destination", DestinationAddress.ToString());

                // calculate the padding needed to right-justify the property names
                int padLength = Utils.RandomUtils.LongestStringLength(new List <string>(properties.Keys));

                // build the output string
                buffer.AppendLine("IP:  ******* IP - \"Internet Protocol (Version 6)\" - offset=? length=" + TotalPacketLength);
                buffer.AppendLine("IP:");
                foreach (var property in properties)
                {
                    if (property.Key.Trim() != "")
                    {
                        buffer.AppendLine("IP: " + property.Key.PadLeft(padLength) + " = " + property.Value);
                    }
                    else
                    {
                        buffer.AppendLine("IP: " + property.Key.PadLeft(padLength) + "   " + property.Value);
                    }
                }
                buffer.AppendLine("IP");
            }

            // append the base class output
            buffer.Append(base.ToString(outputFormat));

            return(buffer.ToString());
        }