Commit bcd8ec16 authored by Brickner_cp's avatar Brickner_cp

New PacketBuilder design

parent 8122647a
...@@ -66,11 +66,25 @@ namespace PcapDotNet.Packets.Test ...@@ -66,11 +66,25 @@ namespace PcapDotNet.Packets.Test
byte[] arpTargetHardwareAddress = random.NextBytes(hardwareAddressLength); byte[] arpTargetHardwareAddress = random.NextBytes(hardwareAddressLength);
byte[] arpTargetProtocolAddress = random.NextBytes(protocolAddressLength); byte[] arpTargetProtocolAddress = random.NextBytes(protocolAddressLength);
Packet packet = PacketBuilder.EthernetArp(DateTime.Now, Packet packet = new PacketBuilder2(new EthernetLayer
ethernetSource, {
ethernetType, arpOperation, Source = ethernetSource,
arpSenderHardwareAddress, arpSenderProtocolAddress, },
arpTargetHardwareAddress, arpTargetProtocolAddress); new ArpLayer
{
ProtocolType = ethernetType,
Operation=arpOperation,
SenderHardwareAddress = arpSenderHardwareAddress,
SenderProtocolAddress = arpSenderProtocolAddress,
TargetHardwareAddress = arpTargetHardwareAddress,
TargetProtocolAddress = arpTargetProtocolAddress
})
.Build(DateTime.Now);
// Packet packet = PacketBuilder.EthernetArp(DateTime.Now,
// ethernetSource,
// ethernetType, arpOperation,
// arpSenderHardwareAddress, arpSenderProtocolAddress,
// arpTargetHardwareAddress, arpTargetProtocolAddress);
Assert.IsTrue(packet.IsValid, "IsValid"); Assert.IsTrue(packet.IsValid, "IsValid");
......
...@@ -72,10 +72,18 @@ namespace PcapDotNet.Packets.Test ...@@ -72,10 +72,18 @@ namespace PcapDotNet.Packets.Test
int ethernetPayloadLength = random.Next(1500); int ethernetPayloadLength = random.Next(1500);
Datagram ethernetPayload = random.NextDatagram(ethernetPayloadLength); Datagram ethernetPayload = random.NextDatagram(ethernetPayloadLength);
Packet packet = PacketBuilder.Ethernet(DateTime.Now, Packet packet =
ethernetSource, ethernetDestination, ethernetType, new PacketBuilder2(new EthernetLayer
ethernetPayload); {
Source = ethernetSource,
Destination = ethernetDestination,
EtherType = ethernetType
},
new PayloadLayer
{
Data = ethernetPayload
})
.Build(DateTime.Now);
// Ethernet // Ethernet
Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength"); Assert.AreEqual(packet.Length - EthernetDatagram.HeaderLength, packet.Ethernet.PayloadLength, "PayloadLength");
......
...@@ -123,11 +123,33 @@ namespace PcapDotNet.Packets.Test ...@@ -123,11 +123,33 @@ namespace PcapDotNet.Packets.Test
random.NextBytes(ipV4PayloadBuffer); random.NextBytes(ipV4PayloadBuffer);
Datagram ipV4Payload = new Datagram(ipV4PayloadBuffer); Datagram ipV4Payload = new Datagram(ipV4PayloadBuffer);
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now, Packet packet =
ethernetSource, ethernetDestination, new PacketBuilder2(new EthernetLayer
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4Ttl, ipV4Protocol, {
ipV4Source, ipV4Destination, ipV4Options, Source = ethernetSource,
ipV4Payload); Destination = ethernetDestination,
},
new IpV4Layer
{
TypeOfService = ipV4TypeOfService,
Identification = ipV4Identification,
Fragmentation = ipV4Fragmentation,
Ttl = ipV4Ttl,
Protocol = ipV4Protocol,
Source = ipV4Source,
Destination = ipV4Destination,
Options = ipV4Options,
},
new PayloadLayer
{
Data = ipV4Payload
}
).Build(DateTime.Now);
// Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
// ethernetSource, ethernetDestination,
// ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4Ttl, ipV4Protocol,
// ipV4Source, ipV4Destination, ipV4Options,
// ipV4Payload);
Assert.IsTrue(ipV4Protocol == IpV4Protocol.Udp || Assert.IsTrue(ipV4Protocol == IpV4Protocol.Udp ||
......
...@@ -92,13 +92,44 @@ namespace PcapDotNet.Packets.Test ...@@ -92,13 +92,44 @@ namespace PcapDotNet.Packets.Test
TcpOptions tcpOptions = random.NextTcpOptions(); TcpOptions tcpOptions = random.NextTcpOptions();
Datagram tcpPayload = random.NextDatagram(random.Next(60000)); Datagram tcpPayload = random.NextDatagram(random.Next(60000));
Packet packet = PacketBuilder.EthernetIpV4Tcp(DateTime.Now, Packet packet = new PacketBuilder2(new EthernetLayer
ethernetSource, ethernetDestination, {
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4Ttl, Source = ethernetSource,
ipV4Source, ipV4Destination, ipV4Options, Destination = ethernetDestination
tcpSourcePort, tcpDestinationPort, tcpSequenceNumber, tcpAcknowledgmentNumber, tcpControlBits, tcpWindow, tcpUrgentPointer, },
tcpOptions, new IpV4Layer
tcpPayload); {
TypeOfService = ipV4TypeOfService,
Identification = ipV4Identification,
Fragmentation = ipV4Fragmentation,
Ttl = ipV4Ttl,
Source = ipV4Source,
Destination = ipV4Destination,
Options = ipV4Options,
},
new TcpLayer
{
SourcePort = tcpSourcePort,
DestinationPort = tcpDestinationPort,
SequenceNumber = tcpSequenceNumber,
AcknowledgmentNumber = tcpAcknowledgmentNumber,
ControlBits = tcpControlBits,
Window = tcpWindow,
UrgentPointer = tcpUrgentPointer,
Options = tcpOptions,
},
new PayloadLayer
{
Data = tcpPayload
}
).Build(DateTime.Now);
// Packet packet = PacketBuilder.EthernetIpV4Tcp(DateTime.Now,
// ethernetSource, ethernetDestination,
// ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4Ttl,
// ipV4Source, ipV4Destination, ipV4Options,
// tcpSourcePort, tcpDestinationPort, tcpSequenceNumber, tcpAcknowledgmentNumber, tcpControlBits, tcpWindow, tcpUrgentPointer,
// tcpOptions,
// tcpPayload);
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
......
...@@ -86,12 +86,38 @@ namespace PcapDotNet.Packets.Test ...@@ -86,12 +86,38 @@ namespace PcapDotNet.Packets.Test
bool udpCalculateChecksum = random.NextBool(); bool udpCalculateChecksum = random.NextBool();
Datagram udpPayload = random.NextDatagram(random.Next(60000)); Datagram udpPayload = random.NextDatagram(random.Next(60000));
Packet packet = PacketBuilder.EthernetIpV4Udp(DateTime.Now, Packet packet = new PacketBuilder2(new EthernetLayer
ethernetSource, ethernetDestination, {
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4Ttl, Source = ethernetSource,
ipV4Source, ipV4Destination, ipV4Options, Destination = ethernetDestination,
udpSourcePort, udpDestinationPort, udpCalculateChecksum, },
udpPayload); new IpV4Layer
{
TypeOfService = ipV4TypeOfService,
Identification = ipV4Identification,
Fragmentation = ipV4Fragmentation,
Ttl = ipV4Ttl,
Source = ipV4Source,
Destination = ipV4Destination,
Options = ipV4Options,
},
new UdpLayer
{
SourcePort = udpSourcePort,
DestinationPort = udpDestinationPort,
CalculateChecksum = udpCalculateChecksum,
},
new PayloadLayer
{
Data = udpPayload
})
.Build(DateTime.Now);
// Packet packet = PacketBuilder.EthernetIpV4Udp(DateTime.Now,
// ethernetSource, ethernetDestination,
// ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4Ttl,
// ipV4Source, ipV4Destination, ipV4Options,
// udpSourcePort, udpDestinationPort, udpCalculateChecksum,
// udpPayload);
Assert.IsTrue(packet.IsValid); Assert.IsTrue(packet.IsValid);
......
...@@ -14,6 +14,11 @@ namespace PcapDotNet.Packets.Ethernet ...@@ -14,6 +14,11 @@ namespace PcapDotNet.Packets.Ethernet
/// </summary> /// </summary>
public const int SizeOf = UInt48.SizeOf; public const int SizeOf = UInt48.SizeOf;
public static MacAddress Zero
{
get { return _zero; }
}
/// <summary> /// <summary>
/// Constructs the address from a 48 bit integer. /// Constructs the address from a 48 bit integer.
/// </summary> /// </summary>
...@@ -105,6 +110,7 @@ namespace PcapDotNet.Packets.Ethernet ...@@ -105,6 +110,7 @@ namespace PcapDotNet.Packets.Ethernet
(byte)(_value)); (byte)(_value));
} }
private static readonly MacAddress _zero;
private readonly UInt48 _value; private readonly UInt48 _value;
} }
} }
\ No newline at end of file
This diff is collapsed.
...@@ -134,6 +134,7 @@ ...@@ -134,6 +134,7 @@
<Compile Include="OptionTypeRegistrationAttribute.cs" /> <Compile Include="OptionTypeRegistrationAttribute.cs" />
<Compile Include="Packet.cs" /> <Compile Include="Packet.cs" />
<Compile Include="PacketBuilder.cs" /> <Compile Include="PacketBuilder.cs" />
<Compile Include="PacketBuilder2.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Transport\TcpDatagram.cs" /> <Compile Include="Transport\TcpDatagram.cs" />
<Compile Include="Transport\TcpControlBits.cs" /> <Compile Include="Transport\TcpControlBits.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment