Commit b2293f3d authored by Brickner_cp's avatar Brickner_cp

ICMP

parent 1e1408a0
......@@ -224,13 +224,7 @@ namespace PcapDotNet.Core.Test
random.NextBytes(hardwareAddressLength), random.NextBytes(protocolAddressLength));
case PacketType.IpV4:
return PacketBuilder.EthernetIpV4(packetTimestamp,
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)));
return PacketBuilder2.Build(packetTimestamp, random.NextEthernetLayer(EthernetType.None), random.NextIpV4Layer(), random.NextPayloadLayer(random.Next(100)));
case PacketType.Igmp:
return CreateRandomIgmpPacket(packetTimestamp, random);
......
......@@ -165,7 +165,7 @@ namespace PcapDotNet.Packets.Test
else
Assert.IsNull(packet.Ethernet.IpV4.Transport);
Assert.AreEqual(payloadLayer, packet.Ethernet.IpV4.Payload.ExtractLayer(), "IP Payload");
Assert.AreEqual(payloadLayer.Data, packet.Ethernet.IpV4.Payload, "IP Payload");
}
}
......@@ -192,11 +192,27 @@ namespace PcapDotNet.Packets.Test
[TestMethod]
public void IpV4OptionTimestampFactoryCreateInstanceErrorTest()
{
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionTimestampOnly(0, 0)),
Datagram.Empty);
Packet packet = PacketBuilder2.Build(DateTime.Now,
new EthernetLayer(),
new IpV4Layer
{
Protocol = IpV4Protocol.Argus,
Options = new IpV4Options(new IpV4OptionTimestampOnly(0, 0)),
});
// public static Packet EthernetIpV4(DateTime timestamp,
// MacAddress ethernetSource, MacAddress ethernetDestination,
// byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
// byte ipV4Ttl, IpV4Protocol ipV4Protocol,
// IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
// IpV4Options ipV4Options,
// Datagram ipV4Payload)
// PacketBuilder.EthernetIpV4(DateTime.Now,
// new MacAddress(), new MacAddress(),
// 0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
// new IpV4Options(new IpV4OptionTimestampOnly(0, 0)),
// Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
......@@ -246,11 +262,18 @@ namespace PcapDotNet.Packets.Test
public void IpV4OptionRouteTryReadErrorTest()
{
// Small Length
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionLooseSourceRouting()),
Datagram.Empty);
Packet packet = PacketBuilder2.Build(DateTime.Now,
new EthernetLayer(),
new IpV4Layer
{
Protocol = 0,
Options = new IpV4Options(new IpV4OptionLooseSourceRouting())
});
// PacketBuilder.EthernetIpV4(DateTime.Now,
// new MacAddress(), new MacAddress(),
// 0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
// new IpV4Options(new IpV4OptionLooseSourceRouting()),
// Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
byte[] buffer = packet.Buffer;
......@@ -371,11 +394,19 @@ namespace PcapDotNet.Packets.Test
public void IpV4OptionBasicSecurityCreateInstanceErrorTest()
{
// Invalid Length
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionBasicSecurity()),
Datagram.Empty);
Packet packet = PacketBuilder2.Build(DateTime.Now,
new EthernetLayer(),
new IpV4Layer
{
Protocol = 0,
Options = new IpV4Options(new IpV4OptionBasicSecurity()),
});
// Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
// new MacAddress(), new MacAddress(),
// 0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
// new IpV4Options(new IpV4OptionBasicSecurity()),
// Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
byte[] buffer = packet.Buffer;
......@@ -384,11 +415,19 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
// Invalid classification level
packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Secret)),
Datagram.Empty);
packet = PacketBuilder2.Build(DateTime.Now,
new EthernetLayer(),
new IpV4Layer
{
Protocol = 0,
Options = new IpV4Options(new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Secret)),
});
// packet = PacketBuilder.EthernetIpV4(DateTime.Now,
// new MacAddress(), new MacAddress(),
// 0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
// new IpV4Options(new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Secret)),
// Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
buffer = packet.Buffer;
......@@ -397,11 +436,18 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(packet.Ethernet.IpV4.Options.IsValid);
// Invalid protection authorities bytes
packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
new IpV4Options(new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Confidential, IpV4OptionSecurityProtectionAuthorities.Nsa, 5)),
Datagram.Empty);
packet = PacketBuilder2.Build(DateTime.Now,
new EthernetLayer(),
new IpV4Layer
{
Protocol = 0,
Options = new IpV4Options(new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Confidential, IpV4OptionSecurityProtectionAuthorities.Nsa, 5)),
});
// packet = PacketBuilder.EthernetIpV4(DateTime.Now,
// new MacAddress(), new MacAddress(),
// 0, 0, new IpV4Fragmentation(), 0, 0, new IpV4Address(), new IpV4Address(),
// new IpV4Options(new IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel.Confidential, IpV4OptionSecurityProtectionAuthorities.Nsa, 5)),
// Datagram.Empty);
Assert.IsTrue(packet.Ethernet.IpV4.Options.IsValid);
buffer = packet.Buffer;
......@@ -457,11 +503,27 @@ namespace PcapDotNet.Packets.Test
[TestMethod]
public void IpV4DatagramInvalidShortTest()
{
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(1), new MacAddress(2),
0, 1, new IpV4Fragmentation(IpV4FragmentationOptions.MoreFragments, 8), 1,
IpV4Protocol.WidebandExpak, new IpV4Address(1), new IpV4Address(2), new IpV4Options(),
Datagram.Empty);
Packet packet = PacketBuilder2.Build(DateTime.Now,
new EthernetLayer
{
Source = new MacAddress(1),
Destination = new MacAddress(2),
},
new IpV4Layer
{
Identification = 1,
Fragmentation = new IpV4Fragmentation(IpV4FragmentationOptions.MoreFragments, 8),
Ttl = 1,
Protocol = IpV4Protocol.WidebandExpak,
Source = new IpV4Address(1),
Destination = new IpV4Address(2),
});
// Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
// new MacAddress(1), new MacAddress(2),
// 0, 1, new IpV4Fragmentation(IpV4FragmentationOptions.MoreFragments, 8), 1,
// IpV4Protocol.WidebandExpak, new IpV4Address(1), new IpV4Address(2), new IpV4Options(),
// Datagram.Empty);
Assert.IsTrue(packet.IsValid);
byte[] badPacketBuffer = new byte[packet.Length - 5];
......@@ -473,10 +535,14 @@ namespace PcapDotNet.Packets.Test
[TestMethod]
public void IpV4DatagramInvalidHeaderChecksumTest()
{
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
new MacAddress(), new MacAddress(),
0, 0, new IpV4Fragmentation(), 0, 0, IpV4Address.Zero, IpV4Address.Zero, IpV4Options.None,
Datagram.Empty);
Packet packet = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer
{
Protocol = 0,
});
// Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
// new MacAddress(), new MacAddress(),
// 0, 0, new IpV4Fragmentation(), 0, 0, IpV4Address.Zero, IpV4Address.Zero, IpV4Options.None,
// Datagram.Empty);
Assert.IsTrue(packet.IsValid);
......
......@@ -45,16 +45,21 @@ namespace PcapDotNet.Packets.TestUtils
// Ethernet
public static EthernetLayer NextEthernetLayer(this Random random)
public static EthernetLayer NextEthernetLayer(this Random random, EthernetType etherType)
{
return new EthernetLayer
{
Source = 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)
{
return new MacAddress(random.NextUInt48());
......
......@@ -215,6 +215,7 @@ namespace PcapDotNet.Packets.IpV4
Fragmentation = Fragmentation,
Ttl = Ttl,
Protocol = Protocol,
HeaderChecksum = HeaderChecksum,
Source = Source,
Destination = Destination,
Options = Options,
......@@ -312,7 +313,7 @@ namespace PcapDotNet.Packets.IpV4
internal static void WriteHeader(byte[] buffer, int offset,
byte typeOfService, ushort identification,
IpV4Fragmentation fragmentation,
byte ttl, IpV4Protocol protocol,
byte ttl, IpV4Protocol protocol, ushort? checksum,
IpV4Address source, IpV4Address destination,
IpV4Options options, int payloadLength)
{
......@@ -330,7 +331,9 @@ namespace PcapDotNet.Packets.IpV4
buffer.Write(offset + Offset.Destination, destination, Endianity.Big);
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)
......
......@@ -90,25 +90,25 @@ namespace PcapDotNet.Packets
/// <param name="ipV4Options">The IPv4 options.</param>
/// <param name="ipV4Payload">The IPv4 payload.</param>
/// <returns>A packet with an IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl, IpV4Protocol ipV4Protocol,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
Datagram ipV4Payload)
{
int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ipHeaderLength + ipV4Payload.Length];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, ipV4Protocol,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, ipV4Payload.Length);
ipV4Payload.Write(buffer, EthernetDatagram.HeaderLength + ipHeaderLength);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
}
// public static Packet EthernetIpV4(DateTime timestamp,
// MacAddress ethernetSource, MacAddress ethernetDestination,
// byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
// byte ipV4Ttl, IpV4Protocol ipV4Protocol,
// IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
// IpV4Options ipV4Options,
// Datagram ipV4Payload)
// {
// int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
// byte[] buffer = new byte[EthernetDatagram.HeaderLength + ipHeaderLength + ipV4Payload.Length];
// EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
// IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
// ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
// ipV4Ttl, ipV4Protocol,
// ipV4SourceAddress, ipV4DestinationAddress,
// ipV4Options, ipV4Payload.Length);
// ipV4Payload.Write(buffer, EthernetDatagram.HeaderLength + ipHeaderLength);
// return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
// }
/// <summary>
/// Builds an ICMP over IPv4 over Ethernet packet.
......@@ -244,14 +244,14 @@ namespace PcapDotNet.Packets
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetControlMessageProtocol,
ipV4Ttl, IpV4Protocol.InternetControlMessageProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, ipPayloadLength);
IcmpDatagram.WriteHeader(buffer, icmpOffset, IcmpMessageType.DestinationUnreachable, icmpCode, icmpValueAccordingToType);
IpV4Datagram.WriteHeader(buffer, icmpOffset + IcmpDatagram.HeaderLength,
icmpIpV4TypeOfService, icmpIpV4Identification,
icmpIpV4Fragmentation,
icmpIpV4Ttl, icmpIpV4Protocol,
icmpIpV4Ttl, icmpIpV4Protocol, null,
icmpIpV4SourceAddress, icmpIpV4DestinationAddress,
icmpIpV4Options, icmpIpV4Payload.Length);
icmpIpV4Payload.Write(buffer, icmpOffset + IcmpDatagram.HeaderLength + icmpIpHeaderLength);
......@@ -498,7 +498,7 @@ namespace PcapDotNet.Packets
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, igmpLength);
IgmpDatagram.WriteQueryVersion3(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
......@@ -536,7 +536,7 @@ namespace PcapDotNet.Packets
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, igmpLength);
IgmpDatagram.WriteReportVersion3(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
......@@ -557,7 +557,7 @@ namespace PcapDotNet.Packets
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, IgmpDatagram.HeaderLength);
IgmpDatagram.WriteHeader(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
......@@ -601,7 +601,7 @@ namespace PcapDotNet.Packets
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.Udp,
ipV4Ttl, IpV4Protocol.Udp, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, transportLength);
......@@ -661,7 +661,7 @@ namespace PcapDotNet.Packets
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.Tcp,
ipV4Ttl, IpV4Protocol.Tcp, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, transportLength);
......
......@@ -185,6 +185,7 @@ namespace PcapDotNet.Packets
Fragmentation = IpV4Fragmentation.None;
Ttl = 0;
Protocol = null;
HeaderChecksum = null;
Source = IpV4Address.Zero;
Destination = IpV4Address.Zero;
Options = IpV4Options.None;
......@@ -200,6 +201,8 @@ namespace PcapDotNet.Packets
public IpV4Protocol? Protocol { get; set; }
public ushort? HeaderChecksum { get; set; }
public IpV4Address Source { get; set; }
public IpV4Address Destination { get; set; }
......@@ -238,7 +241,7 @@ namespace PcapDotNet.Packets
IpV4Datagram.WriteHeader(buffer, offset,
TypeOfService, Identification, Fragmentation,
Ttl, protocol,
Ttl, protocol, HeaderChecksum,
Source, Destination,
Options, payloadLength);
}
......@@ -1080,6 +1083,11 @@ namespace PcapDotNet.Packets
public class PacketBuilder2
{
public static Packet Build(DateTime timestamp, params ILayer[] layers)
{
return new PacketBuilder2(layers).Build(timestamp);
}
public PacketBuilder2(params ILayer[] layers)
{
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