/* This file is part of PacketDotNet PacketDotNet is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PacketDotNet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PacketDotNet. If not, see . */ /* * Copyright 2009 Chris Morgan */ using System; using System.Collections.Generic; using System.Text; using MiscUtil.Conversion; using PacketDotNet.Utils; namespace PacketDotNet { /// /// An ICMP packet. /// See http://en.wikipedia.org/wiki/ICMPv6 /// [Serializable] public class ICMPv6Packet : InternetPacket { // NOTE: No need to warn about lack of use, the compiler won't // put any calls to 'log' here but we need 'log' to exist to compile #pragma warning disable 0169, 0649 private static readonly ILogInactive log; #pragma warning restore 0169, 0649 /// /// The Type value /// virtual public ICMPv6Types Type { get { var val = header.Bytes[header.Offset + ICMPv6Fields.TypePosition]; if(Enum.IsDefined(typeof(ICMPv6Types), val)) return (ICMPv6Types)val; else throw new ArgumentOutOfRangeException("Type of \"" + val + "\" is not defined in ICMPv6Types"); } set { header.Bytes[header.Offset + ICMPv6Fields.TypePosition] = (byte)value; } } /// Fetch the ICMP code virtual public byte Code { get { return header.Bytes[header.Offset + ICMPv6Fields.CodePosition]; } set { header.Bytes[header.Offset + ICMPv6Fields.CodePosition] = (byte)value; } } /// /// Checksum value /// public ushort Checksum { get { return EndianBitConverter.Big.ToUInt16(header.Bytes, header.Offset + ICMPv6Fields.ChecksumPosition); } set { var theValue = value; EndianBitConverter.Big.CopyBytes(theValue, header.Bytes, header.Offset + ICMPv6Fields.ChecksumPosition); } } /// /// Constructor /// /// /// A /// public ICMPv6Packet(ByteArraySegment bas) { log.Debug(""); header = new ByteArraySegment(bas); RandomUtils.EnsurePacketLength(this, ICMPv6Fields.HeaderLength, header.Length); } /// /// Constructor with parent packet /// /// /// A /// /// /// A /// public ICMPv6Packet(ByteArraySegment bas, Packet ParentPacket) : this(bas) { this.ParentPacket = ParentPacket; } /// /// Used to prevent a recursive stack overflow /// when recalculating in UpdateCalculatedValues() /// private bool skipUpdating = false; /// /// Recalculate the checksum /// public override void UpdateCalculatedValues () { if(skipUpdating) return; // prevent us from entering this routine twice // by setting this flag, the act of retrieving the Bytes // property will cause this routine to be called which will // retrieve Bytes recursively and overflow the stack skipUpdating = true; // start with this packet with a zeroed out checksum field Checksum = 0; var originalBytes = Bytes; var ipv6Parent = ParentPacket as IPv6Packet; var bytesToChecksum = ipv6Parent.AttachPseudoIPHeader(originalBytes); // calculate the one's complement sum of the tcp header Checksum = (ushort)ChecksumUtils.OnesComplementSum(bytesToChecksum); // clear the skip variable skipUpdating = false; } /// Fetch ascii escape sequence of the color associated with this packet type. override public System.String Color { get { return AnsiEscapeSequences.LightBlue; } } /// 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}[ICMPPacket: Type={2}, Code={3}]{1}", color, colorEscape, Type, Code); } if(outputFormat == StringOutputType.Verbose || outputFormat == StringOutputType.VerboseColored) { // collect the properties and their value Dictionary properties = new Dictionary(); properties.Add("type", Type.ToString() + " (" + (int)Type + ")"); properties.Add("code", Code.ToString()); // TODO: Implement a checksum verification for ICMPv6 properties.Add("checksum", "0x" + Checksum.ToString("x")); // TODO: Implement ICMPv6 Option fields here? // calculate the padding needed to right-justify the property names int padLength = Utils.RandomUtils.LongestStringLength(new List(properties.Keys)); // build the output string buffer.AppendLine("ICMP: ******* ICMPv6 - \"Internet Control Message Protocol (Version 6)\"- offset=? length=" + TotalPacketLength); buffer.AppendLine("ICMP:"); foreach (var property in properties) { buffer.AppendLine("ICMP: " + property.Key.PadLeft(padLength) + " = " + property.Value); } buffer.AppendLine("ICMP:"); } // append the base string output buffer.Append(base.ToString(outputFormat)); return buffer.ToString(); } /// /// Returns the ICMPv6Packet inside of Packet p or null if /// there is no encapsulated ICMPv6Packet /// /// /// A /// /// /// A /// [Obsolete("Use Packet.Extract() instead")] public static ICMPv6Packet GetEncapsulated(Packet p) { log.Debug(""); if(p is InternetLinkLayerPacket) { var payload = InternetLinkLayerPacket.GetInnerPayload((InternetLinkLayerPacket)p); if(payload is IpPacket) { var payload2 = payload.PayloadPacket; if(payload2 is ICMPv6Packet) { return (ICMPv6Packet)payload2; } } } return null; } } }