Commit 88bbdf85 authored by Brickner_cp's avatar Brickner_cp

ICMP

parent 18b85375
...@@ -86,10 +86,6 @@ namespace PcapDotNet.Packets.Test ...@@ -86,10 +86,6 @@ namespace PcapDotNet.Packets.Test
IcmpMessageType icmpMessageType = random.NextEnum<IcmpMessageType>(); IcmpMessageType icmpMessageType = random.NextEnum<IcmpMessageType>();
Packet packet; Packet packet;
switch (icmpMessageType)
{
case IcmpMessageType.DestinationUnreachable:
IcmpCodeDestinationUnrechable code = random.NextEnum<IcmpCodeDestinationUnrechable>();
byte icmpIpV4TypeOfService = random.NextByte(); byte icmpIpV4TypeOfService = random.NextByte();
ushort icmpIpV4Identification = random.NextUShort(); ushort icmpIpV4Identification = random.NextUShort();
byte icmpIpV4Ttl = random.NextByte(); byte icmpIpV4Ttl = random.NextByte();
...@@ -99,6 +95,10 @@ namespace PcapDotNet.Packets.Test ...@@ -99,6 +95,10 @@ namespace PcapDotNet.Packets.Test
IpV4Address icmpIpV4Destination = new IpV4Address(random.NextUInt()); IpV4Address icmpIpV4Destination = new IpV4Address(random.NextUInt());
IpV4Options icmpIpV4Options = random.NextIpV4Options(); IpV4Options icmpIpV4Options = random.NextIpV4Options();
Datagram icmpIpV4Payload = random.NextDatagram(random.Next(200)); Datagram icmpIpV4Payload = random.NextDatagram(random.Next(200));
switch (icmpMessageType)
{
case IcmpMessageType.DestinationUnreachable:
IcmpCodeDestinationUnrechable code = random.NextEnum<IcmpCodeDestinationUnrechable>();
packet = PacketBuilder.EthernetIpV4IcmpDestinationUnreachable(DateTime.Now, packet = PacketBuilder.EthernetIpV4IcmpDestinationUnreachable(DateTime.Now,
ethernetSource, ethernetDestination, ethernetSource, ethernetDestination,
...@@ -113,6 +113,37 @@ namespace PcapDotNet.Packets.Test ...@@ -113,6 +113,37 @@ namespace PcapDotNet.Packets.Test
icmpIpV4Options, icmpIpV4Payload); icmpIpV4Options, icmpIpV4Payload);
break; break;
// case IcmpMessageType.TimeExceeded:
// PacketBuilder.EthernetIpV4IcmpTimeExceeded(DateTime.Now,
// ethernetSource, ethernetDestination,
// ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
// ipV4Ttl,
// ipV4Source, ipV4Destination, ipV4Options,
// code,
// icmpIpV4TypeOfService, icmpIpV4Identification,
// icmpIpV4Fragmentation,
// icmpIpV4Ttl, icmpIpV4Protocol, icmpIpV4Source,
// icmpIpV4Destination,
// icmpIpV4Options, icmpIpV4Payload);
// case IcmpMessageType.ParameterProblem:
// case IcmpMessageType.SourceQuench:
// case IcmpMessageType.Redirect:
// case IcmpMessageType.Echo:
// case IcmpMessageType.EchoReply:
// case IcmpMessageType.Timestamp:
// case IcmpMessageType.TimestampReply:
// case IcmpMessageType.InformationRequest:
// case IcmpMessageType.InformationReply:
// case IcmpMessageType.RouterAdvertisement:
// case IcmpMessageType.RouterSolicitation:
// case IcmpMessageType.AddressMaskRequest:
// case IcmpMessageType.AddressMaskReply:
// case IcmpMessageType.Traceroute:
// case IcmpMessageType.ConversionFailed:
// case IcmpMessageType.DomainNameRequest:
// case IcmpMessageType.DomainNameReply:
// case IcmpMessageType.SecurityFailures:
default: default:
throw new InvalidOperationException("Invalid icmpMessageType " + icmpMessageType); throw new InvalidOperationException("Invalid icmpMessageType " + icmpMessageType);
} }
......
namespace PcapDotNet.Packets.Icmp
{
/// <summary>
/// RFC 792.
/// </summary>
public enum IcmpCodeDestinationUnrechable : byte
{
/// <summary>
/// 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 = 0x00,
/// <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 = 0x01,
/// <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 = 0x02,
/// <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 = 0x03,
/// <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 = 0x04,
/// <summary>
/// RFC 792.
/// </summary>
DestinationUnreachableSourceRouteFailed = 0x05,
}
}
\ No newline at end of file
...@@ -312,6 +312,5 @@ namespace PcapDotNet.Packets.Icmp ...@@ -312,6 +312,5 @@ namespace PcapDotNet.Packets.Icmp
/// RFC 2521. /// RFC 2521.
/// </summary> /// </summary>
SecurityFailures = 0x28, SecurityFailures = 0x28,
} }
} }
\ No newline at end of file
namespace PcapDotNet.Packets.Icmp namespace PcapDotNet.Packets.Icmp
{ {
/// <summary>
/// RFC 792.
/// </summary>
public enum IcmpCodeDestinationUnrechable : byte
{
/// <summary>
/// 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 = 0x00,
/// <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 = 0x01,
/// <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 = 0x02,
/// <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 = 0x03,
/// <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 = 0x04,
/// <summary>
/// RFC 792.
/// </summary>
DestinationUnreachableSourceRouteFailed = 0x05,
}
public enum IcmpMessageTypeAndCode : ushort public enum IcmpMessageTypeAndCode : ushort
{ {
/// <summary> /// <summary>
......
...@@ -123,7 +123,17 @@ namespace PcapDotNet.Packets ...@@ -123,7 +123,17 @@ namespace PcapDotNet.Packets
/// <param name="ipV4SourceAddress">The IPv4 source address.</param> /// <param name="ipV4SourceAddress">The IPv4 source address.</param>
/// <param name="ipV4DestinationAddress">The IPv4 destination address.</param> /// <param name="ipV4DestinationAddress">The IPv4 destination address.</param>
/// <param name="ipV4Options">The IPv4 options.</param> /// <param name="ipV4Options">The IPv4 options.</param>
/// <returns>A packet with an IPv4 over Ethernet datagram.</returns> /// <param name="icmpCode">The ICMP code.</param>
/// <param name="icmpIpV4TypeOfService">The IPv4 over the ICMP's Type of Service.</param>
/// <param name="icmpIpV4Identification">The IPv4 over the ICMP's Identification.</param>
/// <param name="icmpIpV4Fragmentation">The IPv4 over the ICMP's Fragmentation.</param>
/// <param name="icmpIpV4Ttl">The IPv4 over the ICMP's TTL.</param>
/// <param name="icmpIpV4Protocol">The IPv4 over the ICMP's Protocol.</param>
/// <param name="icmpIpV4SourceAddress">The IPv4 over the ICMP's source address.</param>
/// <param name="icmpIpV4DestinationAddress">The IPv4 over the ICMP's destination address.</param>
/// <param name="icmpIpV4Options">The IPv4 over the ICMP's options.</param>
/// <param name="icmpIpV4Payload">The IPv4 over the ICMP's payload.</param>
/// <returns>A packet with an ICMP Destination Unreachable over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IcmpDestinationUnreachable(DateTime timestamp, public static Packet EthernetIpV4IcmpDestinationUnreachable(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination, MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, byte ipV4TypeOfService, ushort ipV4Identification,
...@@ -138,6 +148,92 @@ namespace PcapDotNet.Packets ...@@ -138,6 +148,92 @@ namespace PcapDotNet.Packets
IpV4Address icmpIpV4SourceAddress, IpV4Address icmpIpV4DestinationAddress, IpV4Address icmpIpV4SourceAddress, IpV4Address icmpIpV4DestinationAddress,
IpV4Options icmpIpV4Options, IpV4Options icmpIpV4Options,
Datagram icmpIpV4Payload) Datagram icmpIpV4Payload)
{
return EthernetIpV4IcmpWithIpV4Payload(timestamp,
ethernetSource, ethernetDestination,
ipV4TypeOfService, ipV4Identification,
ipV4Fragmentation,
ipV4Ttl,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options,
(byte)icmpCode, 0,
icmpIpV4TypeOfService, icmpIpV4Identification,
icmpIpV4Fragmentation,
icmpIpV4Ttl, icmpIpV4Protocol,
icmpIpV4SourceAddress, icmpIpV4DestinationAddress,
icmpIpV4Options,
icmpIpV4Payload);
}
/// <summary>
/// Builds an ICMP over IPv4 over Ethernet packet.
/// </summary>
/// <param name="timestamp">The packet timestamp.</param>
/// <param name="ethernetSource">The ethernet source mac address.</param>
/// <param name="ethernetDestination">The ethernet destination mac address.</param>
/// <param name="ipV4TypeOfService">The IPv4 Type of Service.</param>
/// <param name="ipV4Identification">The IPv4 Identification.</param>
/// <param name="ipV4Fragmentation">The IPv4 Fragmentation.</param>
/// <param name="ipV4Ttl">The IPv4 TTL.</param>
/// <param name="ipV4SourceAddress">The IPv4 source address.</param>
/// <param name="ipV4DestinationAddress">The IPv4 destination address.</param>
/// <param name="ipV4Options">The IPv4 options.</param>
/// <param name="icmpCode">The ICMP code.</param>
/// <param name="icmpIpV4TypeOfService">The IPv4 over the ICMP's Type of Service.</param>
/// <param name="icmpIpV4Identification">The IPv4 over the ICMP's Identification.</param>
/// <param name="icmpIpV4Fragmentation">The IPv4 over the ICMP's Fragmentation.</param>
/// <param name="icmpIpV4Ttl">The IPv4 over the ICMP's TTL.</param>
/// <param name="icmpIpV4Protocol">The IPv4 over the ICMP's Protocol.</param>
/// <param name="icmpIpV4SourceAddress">The IPv4 over the ICMP's source address.</param>
/// <param name="icmpIpV4DestinationAddress">The IPv4 over the ICMP's destination address.</param>
/// <param name="icmpIpV4Options">The IPv4 over the ICMP's options.</param>
/// <param name="icmpIpV4Payload">The IPv4 over the ICMP's payload.</param>
/// <returns>A packet with an ICMP Destination Unreachable over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IcmpTimeExceeded(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification,
IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
IcmpCodeDestinationUnrechable icmpCode,
byte icmpIpV4TypeOfService, ushort icmpIpV4Identification,
IpV4Fragmentation icmpIpV4Fragmentation,
byte icmpIpV4Ttl, IpV4Protocol icmpIpV4Protocol,
IpV4Address icmpIpV4SourceAddress, IpV4Address icmpIpV4DestinationAddress,
IpV4Options icmpIpV4Options,
Datagram icmpIpV4Payload)
{
return EthernetIpV4IcmpWithIpV4Payload(timestamp,
ethernetSource, ethernetDestination,
ipV4TypeOfService, ipV4Identification,
ipV4Fragmentation,
ipV4Ttl,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options,
(byte)icmpCode, 0,
icmpIpV4TypeOfService, icmpIpV4Identification,
icmpIpV4Fragmentation,
icmpIpV4Ttl, icmpIpV4Protocol,
icmpIpV4SourceAddress, icmpIpV4DestinationAddress,
icmpIpV4Options,
icmpIpV4Payload);
}
private static Packet EthernetIpV4IcmpWithIpV4Payload(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification,
IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
byte icmpCode, uint icmpValueAccordingToType,
byte icmpIpV4TypeOfService, ushort icmpIpV4Identification,
IpV4Fragmentation icmpIpV4Fragmentation,
byte icmpIpV4Ttl, IpV4Protocol icmpIpV4Protocol,
IpV4Address icmpIpV4SourceAddress, IpV4Address icmpIpV4DestinationAddress,
IpV4Options icmpIpV4Options,
Datagram icmpIpV4Payload)
{ {
int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength; int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
int icmpIpHeaderLength = IpV4Datagram.HeaderMinimumLength + icmpIpV4Options.BytesLength; int icmpIpHeaderLength = IpV4Datagram.HeaderMinimumLength + icmpIpV4Options.BytesLength;
...@@ -151,7 +247,7 @@ namespace PcapDotNet.Packets ...@@ -151,7 +247,7 @@ namespace PcapDotNet.Packets
ipV4Ttl, IpV4Protocol.InternetControlMessageProtocol, ipV4Ttl, IpV4Protocol.InternetControlMessageProtocol,
ipV4SourceAddress, ipV4DestinationAddress, ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, ipPayloadLength); ipV4Options, ipPayloadLength);
IcmpDatagram.WriteHeader(buffer, icmpOffset, IcmpMessageType.DestinationUnreachable, (byte)icmpCode, 0); IcmpDatagram.WriteHeader(buffer, icmpOffset, IcmpMessageType.DestinationUnreachable, icmpCode, icmpValueAccordingToType);
IpV4Datagram.WriteHeader(buffer, icmpOffset + IcmpDatagram.HeaderLength, IpV4Datagram.WriteHeader(buffer, icmpOffset + IcmpDatagram.HeaderLength,
icmpIpV4TypeOfService, icmpIpV4Identification, icmpIpV4TypeOfService, icmpIpV4Identification,
icmpIpV4Fragmentation, icmpIpV4Fragmentation,
...@@ -163,7 +259,6 @@ namespace PcapDotNet.Packets ...@@ -163,7 +259,6 @@ namespace PcapDotNet.Packets
return new Packet(buffer, timestamp, DataLinkKind.Ethernet); return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
} }
/// <summary> /// <summary>
/// Builds an IGMP query version 1 over IPv4 over Ethernet packet. /// Builds an IGMP query version 1 over IPv4 over Ethernet packet.
/// </summary> /// </summary>
...@@ -578,7 +673,8 @@ namespace PcapDotNet.Packets ...@@ -578,7 +673,8 @@ namespace PcapDotNet.Packets
tcpPayload.Write(buffer, ethernetIpV4HeadersLength + tcpHeaderLength); tcpPayload.Write(buffer, ethernetIpV4HeadersLength + tcpHeaderLength);
IpV4Datagram.WriteTransportChecksum(buffer, EthernetDatagram.HeaderLength, ipV4HeaderLength, (ushort)transportLength, TcpDatagram.Offset.Checksum, false); IpV4Datagram.WriteTransportChecksum(buffer, EthernetDatagram.HeaderLength, ipV4HeaderLength, (ushort)transportLength,
TcpDatagram.Offset.Checksum, false);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet); return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion> <ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{8A184AF5-E46C-482C-81A3-76D8CE290104}</ProjectGuid> <ProjectGuid>{8A184AF5-E46C-482C-81A3-76D8CE290104}</ProjectGuid>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
...@@ -71,6 +71,7 @@ ...@@ -71,6 +71,7 @@
<Compile Include="Ethernet\EthernetType.cs" /> <Compile Include="Ethernet\EthernetType.cs" />
<Compile Include="Ethernet\MacAddress.cs" /> <Compile Include="Ethernet\MacAddress.cs" />
<Compile Include="Icmp\IcmpAddressMaskDatagram.cs" /> <Compile Include="Icmp\IcmpAddressMaskDatagram.cs" />
<Compile Include="Icmp\IcmpCodeDestinationUnrechable.cs" />
<Compile Include="Icmp\IcmpConversionFailedDatagram.cs" /> <Compile Include="Icmp\IcmpConversionFailedDatagram.cs" />
<Compile Include="Icmp\IcmpDatagram.cs" /> <Compile Include="Icmp\IcmpDatagram.cs" />
<Compile Include="Icmp\IcmpEchoDatagram.cs" /> <Compile Include="Icmp\IcmpEchoDatagram.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