Ejemplo n.º 1
0
        /// <summary>
        /// Creates a string representation of the IpPacket object.
        /// </summary>
        public override string ToString()
        {
            var s = new StringBuilder();

            s.Append("IP version: ");
            s.AppendLine(IpVersion.ToString());
            s.Append("Internet Header Length ");
            s.AppendLine(InternetHeaderLength.ToString());
            s.Append("DSCP value ");
            s.AppendLine(DscpValue.ToString());
            s.Append("ECN value ");
            s.AppendLine(ExplicitCongestionNotice.ToString());
            s.Append("IP packet length ");
            s.AppendLine(IpPacketLength.ToString());
            s.Append("ID/Fragment Group ");
            s.AppendLine(FragmentGroupId.ToString());
            s.Append("IP header flags ");
            s.AppendLine(IpHeaderFlags.ToString());
            s.Append("Fragment offset ");
            s.AppendLine(FragmentOffset.ToString());
            s.Append("TTL ");
            s.AppendLine(TimeToLive.ToString());
            s.Append("Protocol Number ");
            s.AppendLine(ProtocolNumber.ToString());
            s.Append("Header Checksum ");
            s.AppendLine(PacketHeaderChecksum.ToString());
            s.Append("Source IP ");
            s.AppendLine(SourceIpAddress.ToString());
            s.Append("Destination IP ");
            s.AppendLine(DestinationIpAddress.ToString());
            if (IpOptions != null)
            {
                s.Append("Length of IP options ");
                s.AppendLine(IpOptions.Length.ToString());
            }
            s.Append("Packet Data Length ");
            s.AppendLine(PacketData.Length.ToString());
            s.Append("Size of data buffer processed ");
            s.AppendLine(DataBuffer.Length.ToString());

            return(s.ToString());
        }
Ejemplo 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}[IPv4Packet: SourceAddress={2}, DestinationAddress={3}, HeaderLength={4}, Protocol={5}, TimeToLive={6}]{1}",
                                    color,
                                    colorEscape,
                                    SourceAddress,
                                    DestinationAddress,
                                    HeaderLength,
                                    Protocol,
                                    TimeToLive);
            }

            if (outputFormat == StringOutputType.Verbose || outputFormat == StringOutputType.VerboseColored)
            {
                // collect the properties and their value
                Dictionary <string, string> properties = new Dictionary <string, string>();
                properties.Add("version", Version.ToString());
                // FIXME: Header length output is incorrect
                properties.Add("header length", HeaderLength + " bytes");
                string diffServices = Convert.ToString(DifferentiatedServices, 2).PadLeft(8, '0').Insert(4, " ");
                properties.Add("differentiated services", "0x" + DifferentiatedServices.ToString("x").PadLeft(2, '0'));
                properties.Add("", diffServices.Substring(0, 7) + ".. = [" + (DifferentiatedServices >> 2) + "] code point");
                properties.Add(" ", ".... .." + diffServices[6] + ". = [" + diffServices[6] + "] ECN");
                properties.Add("  ", ".... ..." + diffServices[7] + " = [" + diffServices[7] + "] ECE");
                properties.Add("total length", TotalLength.ToString());
                properties.Add("identification", "0x" + Id.ToString("x") + " (" + Id + ")");
                string flags = Convert.ToString(FragmentFlags, 2).PadLeft(8, '0').Substring(5, 3);
                properties.Add("flags", "0x" + FragmentFlags.ToString("x").PadLeft(2, '0'));
                properties.Add("   ", flags[0] + ".. = [" + flags[0] + "] reserved");
                properties.Add("    ", "." + flags[1] + ". = [" + flags[1] + "] don't fragment");
                properties.Add("     ", ".." + flags[2] + " = [" + flags[2] + "] more fragments");
                properties.Add("fragment offset", FragmentOffset.ToString());
                properties.Add("time to live", TimeToLive.ToString());
                properties.Add("protocol", Protocol.ToString() + " (0x" + Protocol.ToString("x") + ")");
                properties.Add("header checksum", "0x" + Checksum.ToString("x") + " [" + (ValidChecksum ? "valid" : "invalid") + "]");
                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:  ******* IPv4 - \"Internet Protocol (Version 4)\" - 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());
        }