public IPv6Header(byte[] packet) { Packet = packet; byte b; byte[] buffer; using (var mem = new MemoryStream(packet)) using (var reader = new BinaryReader(mem)) { // Get first byte. b = reader.ReadByte(); // Get version from high nibble of first byte. Version = b.HighNibble(); // Get traffic class from low nibble of first byte. TrafficClass = b.LowNibble(); // Get next byte. b = reader.ReadByte(); // Add high nibble of second byte to traffic class. TrafficClass += b.HighNibble(); // Get flow label from low nibble of byte. FlowLabel = b.LowNibble(); // Add next 2 bytes to flow label. FlowLabel += reader.ReadUInt16(); // Get payload length from next to bytes. PayloadLength = reader.ReadUInt16(); // Get next header from next byte. NextHeader = reader.ReadByte(); Protocol p = Protocol.UNDEFINED; if (Enum.TryParse(NextHeader.ToString(), out p)) { if (Enum.IsDefined(typeof(Protocol), p)) { NextProtocolOrHeader = p; } /* Check if protocol value is defined. */ } /* Check if value can be parsed to Protocol enum. */ // Get hop limit from next byte. HopLimit = reader.ReadByte(); // Get source address from next 16 bytes. buffer = reader.ReadBytes(16); SourceAddress = new IPAddress(buffer); // Get destination address from next 16 bytes. buffer = reader.ReadBytes(16); DestinationAddress = new IPAddress(buffer); // Get extended header or next protocol. buffer = reader.ReadBytes(PayloadLength); Data = buffer; } }
/// <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()); }