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);
......
......@@ -45,14 +45,19 @@ 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)
};
{
Source = random.NextMacAddress(),
Destination = random.NextMacAddress(),
EtherType = etherType
};
}
public static EthernetLayer NextEthernetLayer(this Random random)
{
return random.NextEthernetLayer(random.NextEnum(EthernetType.None));
}
public static MacAddress NextMacAddress(this Random random)
......
......@@ -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