Commit b2293f3d authored by Brickner_cp's avatar Brickner_cp

ICMP

parent 1e1408a0
...@@ -224,13 +224,7 @@ namespace PcapDotNet.Core.Test ...@@ -224,13 +224,7 @@ namespace PcapDotNet.Core.Test
random.NextBytes(hardwareAddressLength), random.NextBytes(protocolAddressLength)); random.NextBytes(hardwareAddressLength), random.NextBytes(protocolAddressLength));
case PacketType.IpV4: case PacketType.IpV4:
return PacketBuilder.EthernetIpV4(packetTimestamp, return PacketBuilder2.Build(packetTimestamp, random.NextEthernetLayer(EthernetType.None), random.NextIpV4Layer(), random.NextPayloadLayer(random.Next(100)));
random.NextMacAddress(), random.NextMacAddress(),
random.NextByte(), random.NextUShort(), random.NextIpV4Fragmentation(),
random.NextByte(),
random.NextEnum<IpV4Protocol>(),
random.NextIpV4Address(), random.NextIpV4Address(), random.NextIpV4Options(),
random.NextDatagram(random.Next(100)));
case PacketType.Igmp: case PacketType.Igmp:
return CreateRandomIgmpPacket(packetTimestamp, random); return CreateRandomIgmpPacket(packetTimestamp, random);
......
...@@ -45,14 +45,19 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -45,14 +45,19 @@ namespace PcapDotNet.Packets.TestUtils
// Ethernet // Ethernet
public static EthernetLayer NextEthernetLayer(this Random random) public static EthernetLayer NextEthernetLayer(this Random random, EthernetType etherType)
{ {
return new EthernetLayer return new EthernetLayer
{ {
Source = random.NextMacAddress(), Source = random.NextMacAddress(),
Destination = random.NextMacAddress(), Destination = random.NextMacAddress(),
EtherType = random.NextEnum(EthernetType.None) EtherType = etherType
}; };
}
public static EthernetLayer NextEthernetLayer(this Random random)
{
return random.NextEthernetLayer(random.NextEnum(EthernetType.None));
} }
public static MacAddress NextMacAddress(this Random random) public static MacAddress NextMacAddress(this Random random)
......
...@@ -215,6 +215,7 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -215,6 +215,7 @@ namespace PcapDotNet.Packets.IpV4
Fragmentation = Fragmentation, Fragmentation = Fragmentation,
Ttl = Ttl, Ttl = Ttl,
Protocol = Protocol, Protocol = Protocol,
HeaderChecksum = HeaderChecksum,
Source = Source, Source = Source,
Destination = Destination, Destination = Destination,
Options = Options, Options = Options,
...@@ -312,7 +313,7 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -312,7 +313,7 @@ namespace PcapDotNet.Packets.IpV4
internal static void WriteHeader(byte[] buffer, int offset, internal static void WriteHeader(byte[] buffer, int offset,
byte typeOfService, ushort identification, byte typeOfService, ushort identification,
IpV4Fragmentation fragmentation, IpV4Fragmentation fragmentation,
byte ttl, IpV4Protocol protocol, byte ttl, IpV4Protocol protocol, ushort? checksum,
IpV4Address source, IpV4Address destination, IpV4Address source, IpV4Address destination,
IpV4Options options, int payloadLength) IpV4Options options, int payloadLength)
{ {
...@@ -330,7 +331,9 @@ namespace PcapDotNet.Packets.IpV4 ...@@ -330,7 +331,9 @@ namespace PcapDotNet.Packets.IpV4
buffer.Write(offset + Offset.Destination, destination, Endianity.Big); buffer.Write(offset + Offset.Destination, destination, Endianity.Big);
options.Write(buffer, offset + Offset.Options); options.Write(buffer, offset + Offset.Options);
buffer.Write(offset + Offset.HeaderChecksum, Sum16BitsToChecksum(Sum16Bits(buffer, offset, headerLength)), Endianity.Big); if (checksum == null)
checksum = Sum16BitsToChecksum(Sum16Bits(buffer, offset, headerLength));
buffer.Write(offset + Offset.HeaderChecksum, checksum.Value, Endianity.Big);
} }
internal static void WriteTransportChecksum(byte[] buffer, int offset, int headerLength, ushort transportLength, int transportChecksumOffset, bool isChecksumOptional) internal static void WriteTransportChecksum(byte[] buffer, int offset, int headerLength, ushort transportLength, int transportChecksumOffset, bool isChecksumOptional)
......
...@@ -90,25 +90,25 @@ namespace PcapDotNet.Packets ...@@ -90,25 +90,25 @@ namespace PcapDotNet.Packets
/// <param name="ipV4Options">The IPv4 options.</param> /// <param name="ipV4Options">The IPv4 options.</param>
/// <param name="ipV4Payload">The IPv4 payload.</param> /// <param name="ipV4Payload">The IPv4 payload.</param>
/// <returns>A packet with an IPv4 over Ethernet datagram.</returns> /// <returns>A packet with an IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4(DateTime timestamp, // public static Packet EthernetIpV4(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination, // MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation, // byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl, IpV4Protocol ipV4Protocol, // byte ipV4Ttl, IpV4Protocol ipV4Protocol,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress, // IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options, // IpV4Options ipV4Options,
Datagram ipV4Payload) // Datagram ipV4Payload)
{ // {
int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength; // int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ipHeaderLength + ipV4Payload.Length]; // byte[] buffer = new byte[EthernetDatagram.HeaderLength + ipHeaderLength + ipV4Payload.Length];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4); // EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength, // IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, // ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, ipV4Protocol, // ipV4Ttl, ipV4Protocol,
ipV4SourceAddress, ipV4DestinationAddress, // ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, ipV4Payload.Length); // ipV4Options, ipV4Payload.Length);
ipV4Payload.Write(buffer, EthernetDatagram.HeaderLength + ipHeaderLength); // ipV4Payload.Write(buffer, EthernetDatagram.HeaderLength + ipHeaderLength);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet); // return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
} // }
/// <summary> /// <summary>
/// Builds an ICMP over IPv4 over Ethernet packet. /// Builds an ICMP over IPv4 over Ethernet packet.
...@@ -244,14 +244,14 @@ namespace PcapDotNet.Packets ...@@ -244,14 +244,14 @@ namespace PcapDotNet.Packets
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4); EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength, IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetControlMessageProtocol, ipV4Ttl, IpV4Protocol.InternetControlMessageProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress, ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, ipPayloadLength); ipV4Options, ipPayloadLength);
IcmpDatagram.WriteHeader(buffer, icmpOffset, IcmpMessageType.DestinationUnreachable, icmpCode, icmpValueAccordingToType); 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,
icmpIpV4Ttl, icmpIpV4Protocol, icmpIpV4Ttl, icmpIpV4Protocol, null,
icmpIpV4SourceAddress, icmpIpV4DestinationAddress, icmpIpV4SourceAddress, icmpIpV4DestinationAddress,
icmpIpV4Options, icmpIpV4Payload.Length); icmpIpV4Options, icmpIpV4Payload.Length);
icmpIpV4Payload.Write(buffer, icmpOffset + IcmpDatagram.HeaderLength + icmpIpHeaderLength); icmpIpV4Payload.Write(buffer, icmpOffset + IcmpDatagram.HeaderLength + icmpIpHeaderLength);
...@@ -498,7 +498,7 @@ namespace PcapDotNet.Packets ...@@ -498,7 +498,7 @@ namespace PcapDotNet.Packets
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4); EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength, IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress, ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, igmpLength); ipV4Options, igmpLength);
IgmpDatagram.WriteQueryVersion3(buffer, EthernetDatagram.HeaderLength + ipHeaderLength, IgmpDatagram.WriteQueryVersion3(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
...@@ -536,7 +536,7 @@ namespace PcapDotNet.Packets ...@@ -536,7 +536,7 @@ namespace PcapDotNet.Packets
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4); EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength, IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress, ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, igmpLength); ipV4Options, igmpLength);
IgmpDatagram.WriteReportVersion3(buffer, EthernetDatagram.HeaderLength + ipHeaderLength, IgmpDatagram.WriteReportVersion3(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
...@@ -557,7 +557,7 @@ namespace PcapDotNet.Packets ...@@ -557,7 +557,7 @@ namespace PcapDotNet.Packets
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4); EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength, IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress, ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, IgmpDatagram.HeaderLength); ipV4Options, IgmpDatagram.HeaderLength);
IgmpDatagram.WriteHeader(buffer, EthernetDatagram.HeaderLength + ipHeaderLength, IgmpDatagram.WriteHeader(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
...@@ -601,7 +601,7 @@ namespace PcapDotNet.Packets ...@@ -601,7 +601,7 @@ namespace PcapDotNet.Packets
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength, IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.Udp, ipV4Ttl, IpV4Protocol.Udp, null,
ipV4SourceAddress, ipV4DestinationAddress, ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, transportLength); ipV4Options, transportLength);
...@@ -661,7 +661,7 @@ namespace PcapDotNet.Packets ...@@ -661,7 +661,7 @@ namespace PcapDotNet.Packets
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength, IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.Tcp, ipV4Ttl, IpV4Protocol.Tcp, null,
ipV4SourceAddress, ipV4DestinationAddress, ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, transportLength); ipV4Options, transportLength);
......
...@@ -185,6 +185,7 @@ namespace PcapDotNet.Packets ...@@ -185,6 +185,7 @@ namespace PcapDotNet.Packets
Fragmentation = IpV4Fragmentation.None; Fragmentation = IpV4Fragmentation.None;
Ttl = 0; Ttl = 0;
Protocol = null; Protocol = null;
HeaderChecksum = null;
Source = IpV4Address.Zero; Source = IpV4Address.Zero;
Destination = IpV4Address.Zero; Destination = IpV4Address.Zero;
Options = IpV4Options.None; Options = IpV4Options.None;
...@@ -200,6 +201,8 @@ namespace PcapDotNet.Packets ...@@ -200,6 +201,8 @@ namespace PcapDotNet.Packets
public IpV4Protocol? Protocol { get; set; } public IpV4Protocol? Protocol { get; set; }
public ushort? HeaderChecksum { get; set; }
public IpV4Address Source { get; set; } public IpV4Address Source { get; set; }
public IpV4Address Destination { get; set; } public IpV4Address Destination { get; set; }
...@@ -238,7 +241,7 @@ namespace PcapDotNet.Packets ...@@ -238,7 +241,7 @@ namespace PcapDotNet.Packets
IpV4Datagram.WriteHeader(buffer, offset, IpV4Datagram.WriteHeader(buffer, offset,
TypeOfService, Identification, Fragmentation, TypeOfService, Identification, Fragmentation,
Ttl, protocol, Ttl, protocol, HeaderChecksum,
Source, Destination, Source, Destination,
Options, payloadLength); Options, payloadLength);
} }
...@@ -1080,6 +1083,11 @@ namespace PcapDotNet.Packets ...@@ -1080,6 +1083,11 @@ namespace PcapDotNet.Packets
public class PacketBuilder2 public class PacketBuilder2
{ {
public static Packet Build(DateTime timestamp, params ILayer[] layers)
{
return new PacketBuilder2(layers).Build(timestamp);
}
public PacketBuilder2(params ILayer[] layers) public PacketBuilder2(params ILayer[] layers)
{ {
if (layers.Length == 0) if (layers.Length == 0)
......
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