Commit 17d66d12 authored by Brickner_cp's avatar Brickner_cp

ICMP

parent 1b776f01
using System;
namespace PcapDotNet.Packets.Icmp
{
public class IcmpDatagram : Datagram
{
/// <summary>
/// The number of bytes the ICMP header takes.
/// </summary>
public const int HeaderLength = 8;
private static class Offset
{
public const int Type = 0;
public const int Code = 1;
public const int Checksum = 2;
public const int Variable = 4;
}
/// <summary>
/// The value of this field determines the format of the remaining data.
/// </summary>
public IcmpType Type
{
get { return (IcmpType)this[Offset.Type]; }
}
public byte Code
{
get { return this[Offset.Code]; }
}
public IcmpTypeAndCode TypeAndCode
{
get { return (IcmpTypeAndCode)ReadUShort(Offset.Type, Endianity.Big); }
}
/// <summary>
/// The checksum is the 16-bit ones's complement of the one's complement sum of the ICMP message starting with the ICMP Type.
/// For computing the checksum, the checksum field should be zero.
/// This checksum may be replaced in the future.
/// </summary>
public ushort Checksum
{
get { return ReadUShort(Offset.Checksum, Endianity.Big); }
}
/// <summary>
/// True iff the checksum value is correct according to the datagram data.
/// </summary>
public bool IsChecksumCorrect
{
get
{
if (_isChecksumCorrect == null)
_isChecksumCorrect = (CalculateChecksum() == Checksum);
return _isChecksumCorrect.Value;
}
}
public uint Variable
{
get { return ReadUInt(Offset.Variable, Endianity.Big); }
}
public Datagram Payload
{
get { return new Datagram(Buffer, StartOffset + HeaderLength, Length - HeaderLength); }
}
internal IcmpDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length)
{
}
protected override bool CalculateIsValid()
{
if (Length < HeaderLength || !IsChecksumCorrect)
return false;
switch (Type)
{
default:
return false;
}
}
private ushort CalculateChecksum()
{
uint sum = Sum16Bits(Buffer, StartOffset, Math.Min(Offset.Checksum, Length)) +
Sum16Bits(Buffer, StartOffset + Offset.Checksum + sizeof(ushort), Length - Offset.Checksum - sizeof(ushort));
return Sum16BitsToChecksum(sum);
}
private bool? _isChecksumCorrect;
}
}
\ No newline at end of file
namespace PcapDotNet.Packets.Icmp
{
public enum IcmpType : byte
{
/// <summary>
/// RFC 792
///
/// <para>
/// If, according to the information in the gateway's routing tables,
/// the network specified in the internet destination field of a datagram is unreachable, e.g., the distance to the network is infinity,
/// the gateway may send a destination unreachable message to the internet source host of the datagram.
/// In addition, in some networks, the gateway may be able to determine if the internet destination host is unreachable.
/// Gateways in these networks may send destination unreachable messages to the source host when the destination host is unreachable.
/// </para>
///
/// <para>
/// If, in the destination host, the IP module cannot deliver the datagram because the indicated protocol module or process port is not active,
/// the destination host may send a destination unreachable message to the source host.
/// </para>
///
/// <para>
/// Another case is when a datagram must be fragmented to be forwarded by a gateway yet the Don't Fragment flag is on.
/// In this case the gateway must discard the datagram and may return a destination unreachable message.
/// </para>
///
/// <para>
/// Codes 0, 1, 4, and 5 may be received from a gateway.
/// Codes 2 and 3 may be received from a host.
/// </para>
/// </summary>
DestinationUnreachable = 0x03
}
}
\ No newline at end of file
namespace PcapDotNet.Packets.Icmp
{
public enum IcmpTypeAndCode : ushort
{
/// <summary>
/// RFC 792.
/// If, according to the information in the gateway's routing tables,
/// the network specified in the internet destination field of a datagram is unreachable,
/// e.g., the distance to the network is infinity,
/// the gateway may send a destination unreachable message to the internet source host of the datagram.
/// </summary>
DestinationUnreachableNetUnreachable = 0x0300,
/// <summary>
/// RFC 792.
/// In some networks, the gateway may be able to determine if the internet destination host is unreachable.
/// Gateways in these networks may send destination unreachable messages to the source host when the destination host is unreachable.
/// </summary>
DestinationUnreachableHostUnreachable = 0x0301,
/// <summary>
/// RFC 792.
/// If, in the destination host, the IP module cannot deliver the datagram because the indicated protocol module is not active,
/// the destination host may send a destination unreachable message to the source host.
/// </summary>
DestinationUnreachableProtocolUnreachable = 0x0302,
/// <summary>
/// RFC 792.
/// If, in the destination host, the IP module cannot deliver the datagram because the indicated process port is not active,
/// the destination host may send a destination unreachable message to the source host.
/// </summary>
DestinationUnreachablePortUnreachable = 0x0303,
/// <summary>
/// RFC 792.
/// A datagram must be fragmented to be forwarded by a gateway yet the Don't Fragment flag is on.
/// In this case the gateway must discard the datagram and may return a destination unreachable message.
/// </summary>
DestinationUnreachableFragmentationNeededAndDontFragmentSet = 0x0304,
DestinationUnreachableSourceRouteFailed = 0x0305,
}
}
\ No newline at end of file
...@@ -621,7 +621,7 @@ namespace PcapDotNet.Packets.Igmp ...@@ -621,7 +621,7 @@ namespace PcapDotNet.Packets.Igmp
private ushort CalculateChecksum() private ushort CalculateChecksum()
{ {
uint sum = Sum16Bits(Buffer, StartOffset, Math.Min(Offset.Checksum, Length)) + uint sum = Sum16Bits(Buffer, StartOffset, Math.Min(Offset.Checksum, Length)) +
Sum16Bits(Buffer, StartOffset + Offset.Checksum + 2, Length - Offset.Checksum - 2); Sum16Bits(Buffer, StartOffset + Offset.Checksum + sizeof(ushort), Length - Offset.Checksum - sizeof(ushort));
return Sum16BitsToChecksum(sum); return Sum16BitsToChecksum(sum);
} }
......
...@@ -70,6 +70,9 @@ ...@@ -70,6 +70,9 @@
<Compile Include="Ethernet\EthernetDatagram.cs" /> <Compile Include="Ethernet\EthernetDatagram.cs" />
<Compile Include="Ethernet\EthernetType.cs" /> <Compile Include="Ethernet\EthernetType.cs" />
<Compile Include="Ethernet\MacAddress.cs" /> <Compile Include="Ethernet\MacAddress.cs" />
<Compile Include="Icmp\IcmpDatagram.cs" />
<Compile Include="Icmp\IcmpType.cs" />
<Compile Include="Icmp\IcmpTypeAndCode.cs" />
<Compile Include="IDataLink.cs" /> <Compile Include="IDataLink.cs" />
<Compile Include="Igmp\IgmpDatagram.cs" /> <Compile Include="Igmp\IgmpDatagram.cs" />
<Compile Include="Igmp\IgmpGroupRecord.cs" /> <Compile Include="Igmp\IgmpGroupRecord.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment