Commit d0ed8367 authored by Brickner_cp's avatar Brickner_cp

PacketBuilder new design

parent cd86ed94
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<TestRunConfiguration name="Local Test Run" id="be17286c-6d1c-43dd-b068-e83867abf9c5" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2006"> <TestRunConfiguration name="Local Test Run" id="be17286c-6d1c-43dd-b068-e83867abf9c5" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
<Description>This is a default test run configuration for a local test run.</Description> <Description>This is a default test run configuration for a local test run.</Description>
<CodeCoverage keyFile="PcapDotNet.snk" /> <CodeCoverage enabled="true" keyFile="PcapDotNet.snk">
<Regular>
<CodeCoverageItem binaryFile="C:\TFS\tfs06.codeplex.com\PcapDotNet\PcapDotNet\bin\Debug\PcapDotNet.Base.dll" pdbFile="C:\TFS\tfs06.codeplex.com\PcapDotNet\PcapDotNet\bin\Debug\PcapDotNet.Base.pdb" instrumentInPlace="true" />
<CodeCoverageItem binaryFile="c:\TFS\tfs06.codeplex.com\PcapDotNet\PcapDotNet\bin\Debug\PcapDotNet.Core.dll" pdbFile="c:\TFS\tfs06.codeplex.com\PcapDotNet\PcapDotNet\bin\Debug\PcapDotNet.Core.pdb" instrumentInPlace="true" />
<CodeCoverageItem binaryFile="C:\TFS\tfs06.codeplex.com\PcapDotNet\PcapDotNet\bin\Debug\PcapDotNet.Core.Extensions.dll" pdbFile="C:\TFS\tfs06.codeplex.com\PcapDotNet\PcapDotNet\bin\Debug\PcapDotNet.Core.Extensions.pdb" instrumentInPlace="true" />
<CodeCoverageItem binaryFile="C:\TFS\tfs06.codeplex.com\PcapDotNet\PcapDotNet\bin\Debug\PcapDotNet.Packets.dll" pdbFile="C:\TFS\tfs06.codeplex.com\PcapDotNet\PcapDotNet\bin\Debug\PcapDotNet.Packets.pdb" instrumentInPlace="true" />
</Regular>
</CodeCoverage>
<TestTypeSpecific> <TestTypeSpecific>
<WebTestRunConfiguration testTypeId="4e7599fa-5ecb-43e9-a887-cd63cf72d207"> <WebTestRunConfiguration testTypeId="4e7599fa-5ecb-43e9-a887-cd63cf72d207">
<Browser name="Internet Explorer 7.0"> <Browser name="Internet Explorer 7.0">
......
...@@ -64,8 +64,17 @@ namespace PcapDotNet.Core.Test ...@@ -64,8 +64,17 @@ namespace PcapDotNet.Core.Test
{ {
string filename = Path.GetTempPath() + @"dump.pcap"; string filename = Path.GetTempPath() + @"dump.pcap";
Packet expectedPacket = PacketBuilder.Ethernet(DateTime.Now, new MacAddress(1), new MacAddress(2), EthernetType.QInQ, Packet expectedPacket = PacketBuilder2.Build(DateTime.Now,
new Datagram(new byte[] {1, 2, 3})); new EthernetLayer
{
Source = new MacAddress(1),
Destination = new MacAddress(2),
EtherType = EthernetType.QInQ,
},
new PayloadLayer
{
Data = new Datagram(new byte[] {1, 2, 3})
});
PacketDumpFile.Dump(filename, new PcapDataLink(DataLinkKind.Ethernet), PacketDevice.DefaultSnapshotLength, PacketDumpFile.Dump(filename, new PcapDataLink(DataLinkKind.Ethernet), PacketDevice.DefaultSnapshotLength,
new[] {expectedPacket}); new[] {expectedPacket});
......
...@@ -112,145 +112,45 @@ namespace PcapDotNet.Core.Test ...@@ -112,145 +112,45 @@ namespace PcapDotNet.Core.Test
Tcp Tcp
} }
private static Packet CreateRandomIgmpPacket(DateTime packetTimestamp, Random random)
{
switch (random.NextEnum(IgmpMessageType.None))
{
case IgmpMessageType.MembershipQuery:
switch (random.NextEnum(IgmpQueryVersion.None, IgmpQueryVersion.Unknown))
{
case IgmpQueryVersion.Version1:
return PacketBuilder.EthernetIpV4IgmpQueryVersion1(packetTimestamp,
random.NextMacAddress(), random.NextMacAddress(),
random.NextByte(), random.NextUShort(),
IpV4Fragmentation.None, random.NextByte(),
random.NextIpV4Address(), random.NextIpV4Address(),
random.NextIpV4Options(),
random.NextIpV4Address());
case IgmpQueryVersion.Version2:
return PacketBuilder.EthernetIpV4IgmpQueryVersion2(packetTimestamp,
random.NextMacAddress(), random.NextMacAddress(),
random.NextByte(), random.NextUShort(),
IpV4Fragmentation.None, random.NextByte(),
random.NextIpV4Address(), random.NextIpV4Address(),
random.NextIpV4Options(),
random.NextTimeSpan(TimeSpan.FromSeconds(0.1),
IgmpDatagram.MaxMaxResponseTime),
random.NextIpV4Address());
case IgmpQueryVersion.Version3:
return PacketBuilder.EthernetIpV4IgmpQueryVersion3(packetTimestamp,
random.NextMacAddress(), random.NextMacAddress(),
random.NextByte(), random.NextUShort(),
IpV4Fragmentation.None, random.NextByte(),
random.NextIpV4Address(), random.NextIpV4Address(),
random.NextIpV4Options(),
random.NextTimeSpan(TimeSpan.FromSeconds(0.1),
IgmpDatagram.MaxVersion3MaxResponseTime),
random.NextIpV4Address(), random.NextBool(),
random.NextByte(IgmpDatagram.MaxQueryRobustnessVariable + 1),
random.NextTimeSpan(TimeSpan.Zero,
IgmpDatagram.MaxQueryInterval),
random.NextIpV4Addresses(random.Next(100)));
default:
throw new InvalidOperationException();
}
case IgmpMessageType.LeaveGroupVersion2:
return PacketBuilder.EthernetIpV4IgmpLeaveGroupVersion2(packetTimestamp,
random.NextMacAddress(), random.NextMacAddress(),
random.NextByte(), random.NextUShort(),
IpV4Fragmentation.None, random.NextByte(),
random.NextIpV4Address(), random.NextIpV4Address(),
random.NextIpV4Options(),
random.NextTimeSpan(TimeSpan.FromSeconds(0.1),
IgmpDatagram.MaxMaxResponseTime),
random.NextIpV4Address());
case IgmpMessageType.MembershipReportVersion1:
return PacketBuilder.EthernetIpV4IgmpReportVersion1(packetTimestamp,
random.NextMacAddress(), random.NextMacAddress(),
random.NextByte(), random.NextUShort(),
IpV4Fragmentation.None, random.NextByte(),
random.NextIpV4Address(), random.NextIpV4Address(),
random.NextIpV4Options(),
random.NextIpV4Address());
case IgmpMessageType.MembershipReportVersion2:
return PacketBuilder.EthernetIpV4IgmpReportVersion2(packetTimestamp,
random.NextMacAddress(), random.NextMacAddress(),
random.NextByte(), random.NextUShort(),
IpV4Fragmentation.None, random.NextByte(),
random.NextIpV4Address(), random.NextIpV4Address(),
random.NextIpV4Options(),
random.NextTimeSpan(TimeSpan.FromSeconds(0.1),
IgmpDatagram.MaxMaxResponseTime),
random.NextIpV4Address());
case IgmpMessageType.MembershipReportVersion3:
return PacketBuilder.EthernetIpV4IgmpReportVersion3(packetTimestamp,
random.NextMacAddress(), random.NextMacAddress(),
random.NextByte(), random.NextUShort(),
IpV4Fragmentation.None, random.NextByte(),
random.NextIpV4Address(), random.NextIpV4Address(),
random.NextIpV4Options(),
random.NextIgmpGroupRecords(random.Next(10)));
default:
throw new InvalidOperationException();
}
}
private static Packet CreateRandomPacket(Random random) private static Packet CreateRandomPacket(Random random)
{ {
DateTime packetTimestamp = DateTime packetTimestamp =
random.NextDateTime(PacketTimestamp.MinimumPacketTimestamp, PacketTimestamp.MaximumPacketTimestamp).ToUniversalTime().ToLocalTime(); random.NextDateTime(PacketTimestamp.MinimumPacketTimestamp, PacketTimestamp.MaximumPacketTimestamp).ToUniversalTime().ToLocalTime();
EthernetLayer ethernetLayer = random.NextEthernetLayer();
IpV4Layer ipV4Layer = random.NextIpV4Layer();
PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
switch (random.NextEnum<PacketType>()) switch (random.NextEnum<PacketType>())
{ {
case PacketType.Ethernet: case PacketType.Ethernet:
return PacketBuilder.Ethernet(packetTimestamp, return PacketBuilder2.Build(DateTime.Now, ethernetLayer, payloadLayer);
random.NextMacAddress(), random.NextMacAddress(), random.NextEnum<EthernetType>(),
random.NextDatagram(random.Next(100)));
case PacketType.Arp: case PacketType.Arp:
byte hardwareAddressLength = random.NextByte(); ethernetLayer.Destination = MacAddress.Zero;
byte protocolAddressLength = random.NextByte(); return PacketBuilder2.Build(packetTimestamp, ethernetLayer, random.NextArpLayer());
return PacketBuilder.EthernetArp(packetTimestamp,
random.NextMacAddress(),
random.NextEnum<EthernetType>(), random.NextEnum<ArpOperation>(),
random.NextBytes(hardwareAddressLength), random.NextBytes(protocolAddressLength),
random.NextBytes(hardwareAddressLength), random.NextBytes(protocolAddressLength));
case PacketType.IpV4: case PacketType.IpV4:
return PacketBuilder2.Build(packetTimestamp, random.NextEthernetLayer(EthernetType.None), random.NextIpV4Layer(), random.NextPayloadLayer(random.Next(100))); return PacketBuilder2.Build(packetTimestamp, ethernetLayer, ipV4Layer, payloadLayer);
case PacketType.Igmp: case PacketType.Igmp:
return CreateRandomIgmpPacket(packetTimestamp, random); ethernetLayer.EtherType = EthernetType.None;
ipV4Layer.Protocol = null;
return PacketBuilder2.Build(packetTimestamp, ipV4Layer, random.NextIgmpLayer());
case PacketType.Udp: case PacketType.Udp:
return PacketBuilder.EthernetIpV4Udp(packetTimestamp, ethernetLayer.EtherType = EthernetType.None;
random.NextMacAddress(), random.NextMacAddress(), ipV4Layer.Protocol = null;
random.NextByte(), random.NextUShort(), if (random.NextBool())
random.NextBool() ? IpV4Fragmentation.None : random.NextIpV4Fragmentation(), ipV4Layer.Fragmentation = IpV4Fragmentation.None;
random.NextByte(), return PacketBuilder2.Build(packetTimestamp, ethernetLayer, ipV4Layer, random.NextUdpLayer(), payloadLayer);
random.NextIpV4Address(), random.NextIpV4Address(), random.NextIpV4Options(),
random.NextUShort(), random.NextUShort(), random.NextBool(),
random.NextDatagram(random.Next(100)));
case PacketType.Tcp: case PacketType.Tcp:
return PacketBuilder.EthernetIpV4Tcp(packetTimestamp, ethernetLayer.EtherType = EthernetType.None;
random.NextMacAddress(), random.NextMacAddress(), ipV4Layer.Protocol = null;
random.NextByte(), random.NextUShort(), if (random.NextBool())
random.NextBool() ? IpV4Fragmentation.None : random.NextIpV4Fragmentation(), ipV4Layer.Fragmentation = IpV4Fragmentation.None;
random.NextByte(), return PacketBuilder2.Build(packetTimestamp, ethernetLayer, ipV4Layer, random.NextUdpLayer(), payloadLayer);
random.NextIpV4Address(), random.NextIpV4Address(), random.NextIpV4Options(),
random.NextUShort(), random.NextUShort(), random.NextUInt(), random.NextUInt(),
random.NextFlags<TcpControlBits>(),
random.NextUShort(), random.NextUShort(),
random.NextTcpOptions(),
random.NextDatagram(random.Next(100)));
default: default:
throw new InvalidOperationException(); throw new InvalidOperationException();
......
...@@ -85,11 +85,20 @@ namespace PcapDotNet.Packets.Test ...@@ -85,11 +85,20 @@ namespace PcapDotNet.Packets.Test
[TestMethod] [TestMethod]
public void ArpProtocolIpV4Address() public void ArpProtocolIpV4Address()
{ {
Packet packet = PacketBuilder.EthernetArp(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now,
new MacAddress(), new EthernetLayer
EthernetType.QInQ, ArpOperation.Request, {
new byte[8], new byte[] {1,2,3,4}, Source = new MacAddress(),
new byte[8], new byte[] {11,22,33,44}); EtherType = EthernetType.QInQ
},
new ArpLayer
{
Operation = ArpOperation.Request,
SenderHardwareAddress = new byte[8],
SenderProtocolAddress = new byte[]{1,2,3,4},
TargetHardwareAddress = new byte[8],
TargetProtocolAddress = new byte[]{11,22,33,44}
});
Assert.AreEqual(new IpV4Address("1.2.3.4"), packet.Ethernet.Arp.SenderProtocolIpV4Address); Assert.AreEqual(new IpV4Address("1.2.3.4"), packet.Ethernet.Arp.SenderProtocolIpV4Address);
Assert.AreEqual(new IpV4Address("11.22.33.44"), packet.Ethernet.Arp.TargetProtocolIpV4Address); Assert.AreEqual(new IpV4Address("11.22.33.44"), packet.Ethernet.Arp.TargetProtocolIpV4Address);
...@@ -99,8 +108,20 @@ namespace PcapDotNet.Packets.Test ...@@ -99,8 +108,20 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(ArgumentException))] [ExpectedException(typeof(ArgumentException))]
public void ArpIncosistentSenderAddressSizeTest() public void ArpIncosistentSenderAddressSizeTest()
{ {
Packet packet = PacketBuilder.EthernetArp(DateTime.Now, new MacAddress(), EthernetType.IpV4, ArpOperation.Request, Packet packet = PacketBuilder2.Build(DateTime.Now,
new byte[4], new byte[6], new byte[5], new byte[6]); new EthernetLayer
{
Source = new MacAddress(),
EtherType = EthernetType.IpV4
},
new ArpLayer
{
Operation = ArpOperation.Request,
SenderHardwareAddress = new byte[4],
SenderProtocolAddress = new byte[6],
TargetHardwareAddress = new byte[5],
TargetProtocolAddress = new byte[6]
});
Assert.IsNull(packet); Assert.IsNull(packet);
Assert.Fail(); Assert.Fail();
} }
...@@ -109,8 +130,20 @@ namespace PcapDotNet.Packets.Test ...@@ -109,8 +130,20 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(ArgumentException))] [ExpectedException(typeof(ArgumentException))]
public void ArpIncosistentTargetAddressSizeTest() public void ArpIncosistentTargetAddressSizeTest()
{ {
Packet packet = PacketBuilder.EthernetArp(DateTime.Now, new MacAddress(), EthernetType.IpV4, ArpOperation.Request, Packet packet = PacketBuilder2.Build(DateTime.Now,
new byte[4], new byte[6], new byte[4], new byte[7]); new EthernetLayer
{
Source = new MacAddress(),
EtherType = EthernetType.IpV4
},
new ArpLayer
{
Operation = ArpOperation.Request,
SenderHardwareAddress = new byte[4],
SenderProtocolAddress = new byte[6],
TargetHardwareAddress = new byte[4],
TargetProtocolAddress = new byte[7]
});
Assert.IsNull(packet); Assert.IsNull(packet);
Assert.Fail(); Assert.Fail();
} }
......
...@@ -107,12 +107,12 @@ namespace PcapDotNet.Packets.Test ...@@ -107,12 +107,12 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(ArgumentOutOfRangeException))] [ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IgmpQueryVersion3SmallMaxResponseTimeTest() public void IgmpQueryVersion3SmallMaxResponseTimeTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4IgmpQueryVersion3(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpQueryVersion3Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, MaxResponseTime = TimeSpan.FromSeconds(-1),
TimeSpan.FromSeconds(-1), IpV4Address.Zero, false, 0, TimeSpan.FromSeconds(1), QueryInterval = TimeSpan.FromSeconds(1)
new IpV4Address[] {}); });
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.Fail(); Assert.Fail();
...@@ -122,12 +122,12 @@ namespace PcapDotNet.Packets.Test ...@@ -122,12 +122,12 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(ArgumentOutOfRangeException))] [ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IgmpQueryVersion3BigMaxResponseTimeTest() public void IgmpQueryVersion3BigMaxResponseTimeTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4IgmpQueryVersion3(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpQueryVersion3Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, MaxResponseTime = TimeSpan.FromHours(1),
TimeSpan.FromHours(1), IpV4Address.Zero, false, 0, TimeSpan.FromSeconds(1), QueryInterval = TimeSpan.FromSeconds(1)
new IpV4Address[] { }); });
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.Fail(); Assert.Fail();
...@@ -137,12 +137,12 @@ namespace PcapDotNet.Packets.Test ...@@ -137,12 +137,12 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(ArgumentOutOfRangeException))] [ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IgmpQueryVersion3SmallQueryIntervalTest() public void IgmpQueryVersion3SmallQueryIntervalTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4IgmpQueryVersion3(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpQueryVersion3Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, MaxResponseTime = TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1), IpV4Address.Zero, false, 0, TimeSpan.FromSeconds(-1), QueryInterval = TimeSpan.FromSeconds(-1)
new IpV4Address[] { }); });
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.Fail(); Assert.Fail();
...@@ -152,12 +152,12 @@ namespace PcapDotNet.Packets.Test ...@@ -152,12 +152,12 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(ArgumentOutOfRangeException))] [ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IgmpQueryVersion3BigQueryIntervalTest() public void IgmpQueryVersion3BigQueryIntervalTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4IgmpQueryVersion3(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpQueryVersion3Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, MaxResponseTime = TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1), IpV4Address.Zero, false, 0, TimeSpan.FromHours(9), QueryInterval = TimeSpan.FromHours(9)
new IpV4Address[] { }); });
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.Fail(); Assert.Fail();
...@@ -167,11 +167,12 @@ namespace PcapDotNet.Packets.Test ...@@ -167,11 +167,12 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(ArgumentOutOfRangeException))] [ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IgmpQueryVersion2SmallMaxResponseTimeTest() public void IgmpQueryVersion2SmallMaxResponseTimeTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4IgmpQueryVersion2(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now,
new MacAddress(), new MacAddress(), new EthernetLayer(), new IpV4Layer(),
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, new IgmpQueryVersion2Layer
IpV4Options.None, {
TimeSpan.FromSeconds(-1), IpV4Address.Zero); MaxResponseTime = TimeSpan.FromSeconds(-1)
});
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.Fail(); Assert.Fail();
...@@ -181,11 +182,11 @@ namespace PcapDotNet.Packets.Test ...@@ -181,11 +182,11 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(ArgumentOutOfRangeException))] [ExpectedException(typeof(ArgumentOutOfRangeException))]
public void IgmpQueryVersion2BigMaxResponseTimeTest() public void IgmpQueryVersion2BigMaxResponseTimeTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4IgmpQueryVersion2(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpQueryVersion2Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, MaxResponseTime = TimeSpan.FromMinutes(5)
TimeSpan.FromMinutes(5), IpV4Address.Zero); });
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.Fail(); Assert.Fail();
...@@ -194,11 +195,12 @@ namespace PcapDotNet.Packets.Test ...@@ -194,11 +195,12 @@ namespace PcapDotNet.Packets.Test
[TestMethod] [TestMethod]
public void IgmpInvalidTest() public void IgmpInvalidTest()
{ {
Packet queryVersion2 = PacketBuilder.EthernetIpV4IgmpQueryVersion2(DateTime.Now, Packet queryVersion2 = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpQueryVersion2Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, MaxResponseTime = TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1), IpV4Address.Zero); });
Assert.IsTrue(queryVersion2.IsValid); Assert.IsTrue(queryVersion2.IsValid);
Assert.IsTrue(queryVersion2.Ethernet.IpV4.Igmp.IsChecksumCorrect); Assert.IsTrue(queryVersion2.Ethernet.IpV4.Igmp.IsChecksumCorrect);
...@@ -217,12 +219,12 @@ namespace PcapDotNet.Packets.Test ...@@ -217,12 +219,12 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(badChecksumPacket.IsValid); Assert.IsFalse(badChecksumPacket.IsValid);
// Big query version 3 // Big query version 3
Packet queryVersion3 = PacketBuilder.EthernetIpV4IgmpQueryVersion3(DateTime.Now, Packet queryVersion3 = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpQueryVersion3Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, MaxResponseTime = TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1), IpV4Address.Zero, false, 0, QueryInterval = TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(1), new IpV4Address[] {}); });
buffer = new byte[queryVersion3.Length + 1]; buffer = new byte[queryVersion3.Length + 1];
queryVersion3.Buffer.BlockCopy(0, buffer, 0, queryVersion3.Length); queryVersion3.Buffer.BlockCopy(0, buffer, 0, queryVersion3.Length);
Packet bigQueryVersion3 = new Packet(buffer, queryVersion3.Timestamp, queryVersion3.DataLink); Packet bigQueryVersion3 = new Packet(buffer, queryVersion3.Timestamp, queryVersion3.DataLink);
...@@ -230,11 +232,8 @@ namespace PcapDotNet.Packets.Test ...@@ -230,11 +232,8 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(bigQueryVersion3.IsValid); Assert.IsFalse(bigQueryVersion3.IsValid);
// Big report version 1 // Big report version 1
Packet reportVersion1 = PacketBuilder.EthernetIpV4IgmpReportVersion1(DateTime.Now, Packet reportVersion1 = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), new IgmpReportVersion1Layer());
new MacAddress(), new MacAddress(),
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero,
IpV4Options.None,
IpV4Address.Zero);
buffer = new byte[reportVersion1.Length + 1]; buffer = new byte[reportVersion1.Length + 1];
reportVersion1.Buffer.BlockCopy(0, buffer, 0, reportVersion1.Length); reportVersion1.Buffer.BlockCopy(0, buffer, 0, reportVersion1.Length);
Packet bigReportVersion1 = new Packet(buffer, reportVersion1.Timestamp, reportVersion1.DataLink); Packet bigReportVersion1 = new Packet(buffer, reportVersion1.Timestamp, reportVersion1.DataLink);
...@@ -251,11 +250,12 @@ namespace PcapDotNet.Packets.Test ...@@ -251,11 +250,12 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(nonZeroMaxResponseCodeReportVersion1.IsValid); Assert.IsFalse(nonZeroMaxResponseCodeReportVersion1.IsValid);
// Big report version 2 // Big report version 2
Packet reportVersion2 = PacketBuilder.EthernetIpV4IgmpReportVersion2(DateTime.Now, Packet reportVersion2 = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpReportVersion2Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, MaxResponseTime = TimeSpan.FromSeconds(1)
TimeSpan.FromSeconds(1), IpV4Address.Zero); });
buffer = new byte[reportVersion2.Length + 1]; buffer = new byte[reportVersion2.Length + 1];
reportVersion2.Buffer.BlockCopy(0, buffer, 0, reportVersion2.Length); reportVersion2.Buffer.BlockCopy(0, buffer, 0, reportVersion2.Length);
Packet bigReportVersion2 = new Packet(buffer, reportVersion2.Timestamp, reportVersion2.DataLink); Packet bigReportVersion2 = new Packet(buffer, reportVersion2.Timestamp, reportVersion2.DataLink);
...@@ -263,16 +263,17 @@ namespace PcapDotNet.Packets.Test ...@@ -263,16 +263,17 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(bigReportVersion2.IsValid); Assert.IsFalse(bigReportVersion2.IsValid);
// non zero max response code report version 3 // non zero max response code report version 3
Packet reportVersion3 = PacketBuilder.EthernetIpV4IgmpReportVersion3(DateTime.Now, Packet reportVersion3 = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new MacAddress(), new MacAddress(), new IgmpReportVersion3Layer
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero, {
IpV4Options.None, GroupRecords = new[]
new[] {
{ new IgmpGroupRecord(
new IgmpGroupRecord( IgmpRecordType.CurrentStateRecordModeIsExclude,
IgmpRecordType.CurrentStateRecordModeIsExclude, IpV4Address.Zero, new List<IpV4Address>(), Datagram.Empty)
IpV4Address.Zero, new List<IpV4Address>(), Datagram.Empty) }
}); });
buffer = new byte[reportVersion3.Length]; buffer = new byte[reportVersion3.Length];
reportVersion3.Buffer.BlockCopy(0, buffer, 0, buffer.Length); reportVersion3.Buffer.BlockCopy(0, buffer, 0, buffer.Length);
buffer.Write(EthernetDatagram.HeaderLength + IpV4Datagram.HeaderMinimumLength + 1, 1); buffer.Write(EthernetDatagram.HeaderLength + IpV4Datagram.HeaderMinimumLength + 1, 1);
...@@ -302,11 +303,8 @@ namespace PcapDotNet.Packets.Test ...@@ -302,11 +303,8 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(InvalidOperationException))] [ExpectedException(typeof(InvalidOperationException))]
public void IgmpIllegalReportVersionTest() public void IgmpIllegalReportVersionTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4IgmpReportVersion1(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), new IgmpReportVersion1Layer());
new MacAddress(), new MacAddress(),
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero,
IpV4Options.None,
IpV4Address.Zero);
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.AreEqual(1, packet.Ethernet.IpV4.Igmp.Version); Assert.AreEqual(1, packet.Ethernet.IpV4.Igmp.Version);
...@@ -323,11 +321,8 @@ namespace PcapDotNet.Packets.Test ...@@ -323,11 +321,8 @@ namespace PcapDotNet.Packets.Test
[ExpectedException(typeof(InvalidOperationException))] [ExpectedException(typeof(InvalidOperationException))]
public void IgmpIllegalQueryVersionTest() public void IgmpIllegalQueryVersionTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4IgmpQueryVersion1(DateTime.Now, Packet packet = PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), new IgmpQueryVersion1Layer());
new MacAddress(), new MacAddress(),
0, 0, IpV4Fragmentation.None, 0, IpV4Address.Zero, IpV4Address.Zero,
IpV4Options.None,
IpV4Address.Zero);
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.AreEqual(1, packet.Ethernet.IpV4.Igmp.Version); Assert.AreEqual(1, packet.Ethernet.IpV4.Igmp.Version);
......
...@@ -160,11 +160,14 @@ namespace PcapDotNet.Packets.Test ...@@ -160,11 +160,14 @@ namespace PcapDotNet.Packets.Test
[TestMethod] [TestMethod]
public void TcpOptionMd5SignatureCreateInstanceErrorDataLengthTest() public void TcpOptionMd5SignatureCreateInstanceErrorDataLengthTest()
{ {
Packet packet = PacketBuilder.EthernetIpV4Tcp(DateTime.Now, Packet packet =
new MacAddress(), new MacAddress(), PacketBuilder2.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
0, 0, new IpV4Fragmentation(), 0, new IpV4Address(), new IpV4Address(), new IpV4Options(), new TcpLayer
0, 0, 0, 0, new TcpControlBits(), 0, 0, {
new TcpOptions(new TcpOptionMd5Signature(new byte[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16})), Datagram.Empty); Options =
new TcpOptions(
new TcpOptionMd5Signature(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}))
});
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Options.IsValid); Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Options.IsValid);
......
...@@ -75,9 +75,14 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -75,9 +75,14 @@ namespace PcapDotNet.Packets.TestUtils
if (packetSize < EthernetDatagram.HeaderLength) if (packetSize < EthernetDatagram.HeaderLength)
throw new ArgumentOutOfRangeException("packetSize", packetSize, "Must be at least the ethernet header length (" + EthernetDatagram.HeaderLength + ")"); throw new ArgumentOutOfRangeException("packetSize", packetSize, "Must be at least the ethernet header length (" + EthernetDatagram.HeaderLength + ")");
return PacketBuilder.Ethernet(timestamp, return PacketBuilder2.Build(timestamp,
ethernetSource, ethernetDestination, random.NextEthernetType(), new EthernetLayer
random.NextDatagram(packetSize - EthernetDatagram.HeaderLength)); {
Source = ethernetSource,
Destination = ethernetDestination,
EtherType = random.NextEthernetType()
},
random.NextPayloadLayer(packetSize - EthernetDatagram.HeaderLength));
} }
public static Packet NextEthernetPacket(this Random random, int packetSize, DateTime timestamp, string ethernetSource, string ethernetDestination) public static Packet NextEthernetPacket(this Random random, int packetSize, DateTime timestamp, string ethernetSource, string ethernetDestination)
......
...@@ -13,670 +13,4 @@ namespace PcapDotNet.Packets ...@@ -13,670 +13,4 @@ namespace PcapDotNet.Packets
/// <summary> /// <summary>
/// The class to use to build all the packets. /// The class to use to build all the packets.
/// </summary> /// </summary>
public static class PacketBuilder
{
/// <summary>
/// Builds an 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="ethernetType">The Ethernet type.</param>
/// <param name="ethernetPayload">The Ethernet payload.</param>
/// <returns>A packet with an Ethernet datagram.</returns>
public static Packet Ethernet(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination, EthernetType ethernetType,
Datagram ethernetPayload)
{
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ethernetPayload.Length];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, ethernetType);
ethernetPayload.Write(buffer, EthernetDatagram.HeaderLength);
return new Packet(buffer, timestamp, new DataLink(DataLinkKind.Ethernet));
}
/// <summary>
/// Builds an ARP over Ethernet packet.
/// The ethernet destination will be ethernet broadcast.
/// </summary>
/// <param name="timestamp">The packet timestamp.</param>
/// <param name="ethernetSource">The Ethernet source mac address.</param>
/// <param name="arpProtocolType">Each protocol is assigned a number used in this field.</param>
/// <param name="arpOperation">Specifies the operation the sender is performing.</param>
/// <param name="arpSenderHardwareAddress">Hardware address of the sender.</param>
/// <param name="arpSenderProtocolAddress">Protocol address of the sender.</param>
/// <param name="arpTargetHardwareAddress">Hardware address of the intended receiver. This field is ignored in requests.</param>
/// <param name="arpTargetProtocolAddress">Protocol address of the intended receiver.</param>
/// <returns>A packet with an ARP over Ethernet datagram.</returns>
/// <exception cref="ArgumentException">The sender hardware or protocol addresses have different length.</exception>
public static Packet EthernetArp(DateTime timestamp,
MacAddress ethernetSource,
EthernetType arpProtocolType, ArpOperation arpOperation,
byte[] arpSenderHardwareAddress, byte[] arpSenderProtocolAddress,
byte[] arpTargetHardwareAddress, byte[] arpTargetProtocolAddress)
{
if (arpSenderHardwareAddress.Length != arpTargetHardwareAddress.Length)
{
throw new ArgumentException("Sender hardware address length is " + arpSenderHardwareAddress.Length + " bytes " +
"while target hardware address length is " + arpTargetHardwareAddress.Length + " bytes");
}
if (arpSenderProtocolAddress.Length != arpTargetProtocolAddress.Length)
{
throw new ArgumentException("Sender protocol address length is " + arpSenderProtocolAddress.Length + " bytes " +
"while target protocol address length is " + arpTargetProtocolAddress.Length + " bytes");
}
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ArpDatagram.GetHeaderLength(arpSenderHardwareAddress.Length, arpSenderProtocolAddress.Length)];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, EthernetDatagram.BroadcastAddress, EthernetType.Arp);
ArpDatagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ArpHardwareType.Ethernet, arpProtocolType, arpOperation,
arpSenderHardwareAddress, arpSenderProtocolAddress, arpTargetHardwareAddress, arpTargetProtocolAddress);
return new Packet(buffer, timestamp, new DataLink(DataLinkKind.Ethernet));
}
/// <summary>
/// Builds an 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="ipV4Protocol">The IPv4 Protocol.</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="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);
// }
/// <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 EthernetIpV4IcmpDestinationUnreachable(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);
}
/// <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 icmpIpHeaderLength = IpV4Datagram.HeaderMinimumLength + icmpIpV4Options.BytesLength;
int ipPayloadLength = IcmpDatagram.HeaderLength + icmpIpHeaderLength + icmpIpV4Payload.Length;
int icmpOffset = EthernetDatagram.HeaderLength + ipHeaderLength;
byte[] buffer = new byte[icmpOffset + ipPayloadLength];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
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, null,
icmpIpV4SourceAddress, icmpIpV4DestinationAddress,
icmpIpV4Options, icmpIpV4Payload.Length);
icmpIpV4Payload.Write(buffer, icmpOffset + IcmpDatagram.HeaderLength + icmpIpHeaderLength);
IcmpDatagram.WriteChecksum(buffer, icmpOffset, ipPayloadLength, null);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
}
/// <summary>
/// Builds an IGMP query version 1 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="igmpGroupAddress">
/// The Group Address field is set to zero when sending a General Query,
/// and set to the IP multicast address being queried when sending a Group-Specific Query or Group-and-Source-Specific Query.
/// In a Membership Report of version 1 or 2 or Leave Group message, the group address field holds the IP multicast group address of the group being reported or left.
/// In a Membership Report of version 3 this field is meaningless.
/// </param>
/// <returns>A packet with an IGMP query version 1 over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IgmpQueryVersion1(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
IpV4Address igmpGroupAddress)
{
return EthernetIpV4Igmp(timestamp,
ethernetSource, ethernetDestination,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options,
IgmpMessageType.MembershipQuery, TimeSpan.Zero, igmpGroupAddress);
}
/// <summary>
/// Builds an IGMP report version 1 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="igmpGroupAddress">
/// The Group Address field is set to zero when sending a General Query,
/// and set to the IP multicast address being queried when sending a Group-Specific Query or Group-and-Source-Specific Query.
/// In a Membership Report of version 1 or 2 or Leave Group message, the group address field holds the IP multicast group address of the group being reported or left.
/// In a Membership Report of version 3 this field is meaningless.
/// </param>
/// <returns>A packet with an IGMP report version 1 over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IgmpReportVersion1(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
IpV4Address igmpGroupAddress)
{
return EthernetIpV4Igmp(timestamp,
ethernetSource, ethernetDestination,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options,
IgmpMessageType.MembershipReportVersion1, TimeSpan.Zero, igmpGroupAddress);
}
/// <summary>
/// Builds an IGMP query version 2 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="igmpMaxResponseTime">The actual time allowed.</param>
/// <param name="igmpGroupAddress">
/// The Group Address field is set to zero when sending a General Query,
/// and set to the IP multicast address being queried when sending a Group-Specific Query or Group-and-Source-Specific Query.
/// In a Membership Report of version 1 or 2 or Leave Group message, the group address field holds the IP multicast group address of the group being reported or left.
/// In a Membership Report of version 3 this field is meaningless.
/// </param>
/// <returns>A packet with an IGMP query version 2 over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IgmpQueryVersion2(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
TimeSpan igmpMaxResponseTime, IpV4Address igmpGroupAddress)
{
return EthernetIpV4Igmp(timestamp,
ethernetSource, ethernetDestination,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options,
IgmpMessageType.MembershipQuery, igmpMaxResponseTime, igmpGroupAddress);
}
/// <summary>
/// Builds an IGMP report version 2 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="igmpMaxResponseTime">The actual time allowed.</param>
/// <param name="igmpGroupAddress">
/// The Group Address field is set to zero when sending a General Query,
/// and set to the IP multicast address being queried when sending a Group-Specific Query or Group-and-Source-Specific Query.
/// In a Membership Report of version 1 or 2 or Leave Group message, the group address field holds the IP multicast group address of the group being reported or left.
/// In a Membership Report of version 3 this field is meaningless.
/// </param>
/// <returns>A packet with an IGMP report version 2 over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IgmpReportVersion2(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
TimeSpan igmpMaxResponseTime, IpV4Address igmpGroupAddress)
{
return EthernetIpV4Igmp(timestamp,
ethernetSource, ethernetDestination,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options,
IgmpMessageType.MembershipReportVersion2, igmpMaxResponseTime, igmpGroupAddress);
}
/// <summary>
/// Builds an IGMP leave group version 2 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="igmpMaxResponseTime">The actual time allowed.</param>
/// <param name="igmpGroupAddress">
/// The Group Address field is set to zero when sending a General Query,
/// and set to the IP multicast address being queried when sending a Group-Specific Query or Group-and-Source-Specific Query.
/// In a Membership Report of version 1 or 2 or Leave Group message, the group address field holds the IP multicast group address of the group being reported or left.
/// In a Membership Report of version 3 this field is meaningless.
/// </param>
/// <returns>A packet with an IGMP leave group version 2 over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IgmpLeaveGroupVersion2(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
TimeSpan igmpMaxResponseTime, IpV4Address igmpGroupAddress)
{
return EthernetIpV4Igmp(timestamp,
ethernetSource, ethernetDestination,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options,
IgmpMessageType.LeaveGroupVersion2, igmpMaxResponseTime, igmpGroupAddress);
}
/// <summary>
/// Builds an IGMP query version 3 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="igmpMaxResponseTime">The actual time allowed.</param>
/// <param name="igmpGroupAddress">
/// The Group Address field is set to zero when sending a General Query,
/// and set to the IP multicast address being queried when sending a Group-Specific Query or Group-and-Source-Specific Query.
/// </param>
/// <param name="igmpIsSuppressRouterSideProcessing">
/// When set to one, the S Flag indicates to any receiving multicast routers that they are to suppress the normal timer updates they perform upon hearing a Query.
/// It does not, however, suppress the querier election or the normal "host-side" processing of a Query
/// that a router may be required to perform as a consequence of itself being a group member.
/// </param>
/// <param name="igmpQueryRobustnessVariable">
/// If non-zero, the QRV field contains the [Robustness Variable] value used by the querier, i.e., the sender of the Query.
/// If the querier's [Robustness Variable] exceeds 7, the maximum value of the QRV field, the QRV is set to zero.
/// Routers adopt the QRV value from the most recently received Query as their own [Robustness Variable] value,
/// unless that most recently received QRV was zero, in which case the receivers use the default [Robustness Variable] value or a statically configured value.
/// </param>
/// <param name="igmpQueryInterval">Interval, called the Querier's Query Interval (QQI).</param>
/// <param name="igmpSourceAddresses">
/// The Source Address [i] fields are a vector of n IP unicast addresses,
/// where n is the value in the Number of Sources (N) field.
/// </param>
/// <returns>A packet with an IGMP query version 3 over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IgmpQueryVersion3(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
TimeSpan igmpMaxResponseTime, IpV4Address igmpGroupAddress,
bool igmpIsSuppressRouterSideProcessing, byte igmpQueryRobustnessVariable,
TimeSpan igmpQueryInterval, IEnumerable<IpV4Address> igmpSourceAddresses)
{
int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
int igmpLength = IgmpDatagram.GetQueryVersion3Length(igmpSourceAddresses.Count());
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ipHeaderLength + igmpLength];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, igmpLength);
IgmpDatagram.WriteQueryVersion3(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
igmpMaxResponseTime, igmpGroupAddress, igmpIsSuppressRouterSideProcessing, igmpQueryRobustnessVariable,
igmpQueryInterval, igmpSourceAddresses);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
}
/// <summary>
/// Builds an IGMP report version 3 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="igmpGroupRecords">Each Group Record is a block of fields containing information pertaining to the sender's membership in a single multicast group on the interface from which the Report is sent.</param>
/// <returns>A packet with an IGMP report version 3 over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4IgmpReportVersion3(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
IEnumerable<IgmpGroupRecord> igmpGroupRecords)
{
int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
int igmpLength = IgmpDatagram.GetReportVersion3Length(igmpGroupRecords);
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ipHeaderLength + igmpLength];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, igmpLength);
IgmpDatagram.WriteReportVersion3(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
igmpGroupRecords);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
}
private static Packet EthernetIpV4Igmp(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
IgmpMessageType igmpMessageType, TimeSpan igmpMaxResponseTime, IpV4Address igmpGroupAddress)
{
int ipHeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
byte[] buffer = new byte[EthernetDatagram.HeaderLength + ipHeaderLength + IgmpDatagram.HeaderLength];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.InternetGroupManagementProtocol, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, IgmpDatagram.HeaderLength);
IgmpDatagram.WriteHeader(buffer, EthernetDatagram.HeaderLength + ipHeaderLength,
igmpMessageType, igmpMaxResponseTime, igmpGroupAddress);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
}
/// <summary>
/// Builds a UDP 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="udpSourcePort">The source udp port.</param>
/// <param name="udpDestinationPort">The destination udp port.</param>
/// <param name="udpCalculateChecksum">Whether to calculate udp checksum or leave it empty (UDP checksum is optional).</param>
/// <param name="udpPayload">The payload of UDP datagram.</param>
/// <returns>A packet with a UDP over IPv4 over Ethernet datagram.</returns>
public static Packet EthernetIpV4Udp(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
ushort udpSourcePort, ushort udpDestinationPort, bool udpCalculateChecksum,
Datagram udpPayload)
{
int ipV4HeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
int transportLength = UdpDatagram.HeaderLength + udpPayload.Length;
int ethernetIpV4HeadersLength = EthernetDatagram.HeaderLength + ipV4HeaderLength;
byte[] buffer = new byte[ethernetIpV4HeadersLength + transportLength];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.Udp, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, transportLength);
UdpDatagram.WriteHeader(buffer, ethernetIpV4HeadersLength, udpSourcePort, udpDestinationPort, udpPayload.Length);
udpPayload.Write(buffer, ethernetIpV4HeadersLength + UdpDatagram.HeaderLength);
if (udpCalculateChecksum)
IpV4Datagram.WriteTransportChecksum(buffer, EthernetDatagram.HeaderLength, ipV4HeaderLength, (ushort)transportLength, UdpDatagram.Offset.Checksum, true, null);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
}
/// <summary>
/// Builds a UDP 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="tcpSourcePort">The source TCP port.</param>
/// <param name="tcpDestinationPort">The destination TCP port.</param>
/// <param name="tcpSequenceNumber">The TCP sequence number.</param>
/// <param name="tcpAcknowledgmentNumber">The TCP ack number.</param>
/// <param name="tcpControlBits">The TCP flags.</param>
/// <param name="tcpWindow">The TCP window size.</param>
/// <param name="tcpUrgentPointer">The TCP urgent pointer value.</param>
/// <param name="tcpOptions">The TCP options.</param>
/// <param name="tcpPayload">The payload of UDP datagram.</param>
/// <returns>A packet with a UDP over IPv4 over Ethernet datagram.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "pointer")]
public static Packet EthernetIpV4Tcp(DateTime timestamp,
MacAddress ethernetSource, MacAddress ethernetDestination,
byte ipV4TypeOfService, ushort ipV4Identification, IpV4Fragmentation ipV4Fragmentation,
byte ipV4Ttl,
IpV4Address ipV4SourceAddress, IpV4Address ipV4DestinationAddress,
IpV4Options ipV4Options,
ushort tcpSourcePort, ushort tcpDestinationPort,
uint tcpSequenceNumber, uint tcpAcknowledgmentNumber,
TcpControlBits tcpControlBits, ushort tcpWindow, ushort tcpUrgentPointer,
TcpOptions tcpOptions,
Datagram tcpPayload)
{
int ipV4HeaderLength = IpV4Datagram.HeaderMinimumLength + ipV4Options.BytesLength;
int tcpHeaderLength = TcpDatagram.HeaderMinimumLength + tcpOptions.BytesLength;
int transportLength = tcpHeaderLength + tcpPayload.Length;
int ethernetIpV4HeadersLength = EthernetDatagram.HeaderLength + ipV4HeaderLength;
byte[] buffer = new byte[ethernetIpV4HeadersLength + transportLength];
EthernetDatagram.WriteHeader(buffer, 0, ethernetSource, ethernetDestination, EthernetType.IpV4);
IpV4Datagram.WriteHeader(buffer, EthernetDatagram.HeaderLength,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation,
ipV4Ttl, IpV4Protocol.Tcp, null,
ipV4SourceAddress, ipV4DestinationAddress,
ipV4Options, transportLength);
TcpDatagram.WriteHeader(buffer, ethernetIpV4HeadersLength,
tcpSourcePort, tcpDestinationPort,
tcpSequenceNumber, tcpAcknowledgmentNumber,
tcpControlBits, tcpWindow, tcpUrgentPointer,
tcpOptions);
tcpPayload.Write(buffer, ethernetIpV4HeadersLength + tcpHeaderLength);
IpV4Datagram.WriteTransportChecksum(buffer, EthernetDatagram.HeaderLength, ipV4HeaderLength, (ushort)transportLength,
TcpDatagram.Offset.Checksum, false, null);
return new Packet(buffer, timestamp, DataLinkKind.Ethernet);
}
}
} }
\ No newline at end of file
...@@ -592,6 +592,11 @@ namespace PcapDotNet.Packets ...@@ -592,6 +592,11 @@ namespace PcapDotNet.Packets
public class IgmpQueryVersion3Layer : IgmpLayer, IIgmpLayerWithGroupAddress public class IgmpQueryVersion3Layer : IgmpLayer, IIgmpLayerWithGroupAddress
{ {
public IgmpQueryVersion3Layer()
{
SourceAddresses = new List<IpV4Address>();
}
public TimeSpan MaxResponseTime { get; set; } public TimeSpan MaxResponseTime { get; set; }
public IpV4Address GroupAddress { get; set; } public IpV4Address GroupAddress { get; set; }
public bool IsSuppressRouterSideProcessing { get; set; } public bool IsSuppressRouterSideProcessing { get; set; }
......
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