Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pcap-Net
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Pcap-Net
Commits
1e1408a0
Commit
1e1408a0
authored
Nov 28, 2009
by
Brickner_cp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ICMP
parent
457e35b8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
322 additions
and
431 deletions
+322
-431
TimeSpanExtensions.cs
PcapDotNet/src/PcapDotNet.Base/TimeSpanExtensions.cs
+5
-0
ArpTests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/ArpTests.cs
+9
-36
EthernetTests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/EthernetTests.cs
+8
-21
IcmpTests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/IcmpTests.cs
+4
-28
IgmpTests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/IgmpTests.cs
+10
-170
IpV4Tests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/IpV4Tests.cs
+30
-70
TcpTests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/TcpTests.cs
+34
-77
RandomPacketsExtensions.cs
...c/PcapDotNet.Packets.TestUtils/RandomPacketsExtensions.cs
+75
-0
ArpDatagram.cs
PcapDotNet/src/PcapDotNet.Packets/Arp/ArpDatagram.cs
+19
-4
IgmpDatagram.cs
PcapDotNet/src/PcapDotNet.Packets/Igmp/IgmpDatagram.cs
+77
-0
PacketBuilder2.cs
PcapDotNet/src/PcapDotNet.Packets/PacketBuilder2.cs
+28
-17
TcpDatagram.cs
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpDatagram.cs
+23
-8
No files found.
PcapDotNet/src/PcapDotNet.Base/TimeSpanExtensions.cs
View file @
1e1408a0
...
...
@@ -19,5 +19,10 @@ namespace PcapDotNet.Base
{
return
TimeSpan
.
FromTicks
((
long
)(
timeSpan
.
Ticks
/
value
));
}
public
static
TimeSpan
Multiply
(
this
TimeSpan
timeSpan
,
double
value
)
{
return
TimeSpan
.
FromTicks
((
long
)(
timeSpan
.
Ticks
*
value
));
}
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Packets.Test/ArpTests.cs
View file @
1e1408a0
...
...
@@ -57,34 +57,14 @@ namespace PcapDotNet.Packets.Test
for
(
int
i
=
0
;
i
!=
1000
;
++
i
)
{
MacAddress
ethernetSource
=
random
.
NextMacAddress
();
EthernetType
ethernetType
=
random
.
NextEnum
<
EthernetType
>();
ArpOperation
arpOperation
=
random
.
NextEnum
<
ArpOperation
>();
byte
hardwareAddressLength
=
random
.
NextByte
();
byte
protocolAddressLength
=
random
.
NextByte
();
byte
[]
arpSenderHardwareAddress
=
random
.
NextBytes
(
hardwareAddressLength
);
byte
[]
arpSenderProtocolAddress
=
random
.
NextBytes
(
protocolAddressLength
);
byte
[]
arpTargetHardwareAddress
=
random
.
NextBytes
(
hardwareAddressLength
);
byte
[]
arpTargetProtocolAddress
=
random
.
NextBytes
(
protocolAddressLength
);
EthernetLayer
ethernetLayer
=
new
EthernetLayer
{
Source
=
ethernetSource
,
};
Packet
packet
=
new
PacketBuilder2
(
new
EthernetLayer
{
Source
=
ethernetSource
,
},
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);
ArpLayer
arpLayer
=
random
.
NextArpLayer
();
Packet
packet
=
new
PacketBuilder2
(
ethernetLayer
,
arpLayer
).
Build
(
DateTime
.
Now
);
Assert
.
IsTrue
(
packet
.
IsValid
,
"IsValid"
);
...
...
@@ -96,16 +76,9 @@ namespace PcapDotNet.Packets.Test
Assert
.
AreEqual
(
EthernetType
.
Arp
,
packet
.
Ethernet
.
EtherType
,
"Ethernet EtherType"
);
// Arp
Assert
.
AreEqual
(
ArpDatagram
.
HeaderBaseLength
+
2
*
hardwareAddressLength
+
2
*
protocolAddressLength
,
packet
.
Ethernet
.
Arp
.
Length
,
"Arp length"
);
Assert
.
AreEqual
(
ArpDatagram
.
HeaderBaseLength
+
2
*
arpLayer
.
SenderHardwareAddress
.
Count
+
2
*
arpLayer
.
SenderProtocolAddress
.
Count
,
packet
.
Ethernet
.
Arp
.
Length
,
"Arp length"
);
Assert
.
AreEqual
(
ArpHardwareType
.
Ethernet
,
packet
.
Ethernet
.
Arp
.
HardwareType
,
"Arp hardware type"
);
Assert
.
AreEqual
(
ethernetType
,
packet
.
Ethernet
.
Arp
.
ProtocolType
,
"Arp protocol type"
);
Assert
.
AreEqual
(
hardwareAddressLength
,
packet
.
Ethernet
.
Arp
.
HardwareLength
,
"Arp hardware length"
);
Assert
.
AreEqual
(
protocolAddressLength
,
packet
.
Ethernet
.
Arp
.
ProtocolLength
,
"Arp protocol length"
);
Assert
.
AreEqual
(
arpOperation
,
packet
.
Ethernet
.
Arp
.
Operation
,
"Arp operation"
);
MoreAssert
.
AreSequenceEqual
(
arpSenderHardwareAddress
,
packet
.
Ethernet
.
Arp
.
SenderHardwareAddress
,
"Arp SenderHardwareAddress"
);
MoreAssert
.
AreSequenceEqual
(
arpSenderProtocolAddress
,
packet
.
Ethernet
.
Arp
.
SenderProtocolAddress
,
"Arp SenderProtocolAddress"
);
MoreAssert
.
AreSequenceEqual
(
arpTargetHardwareAddress
,
packet
.
Ethernet
.
Arp
.
TargetHardwareAddress
,
"Arp TargetHardwareAddress"
);
MoreAssert
.
AreSequenceEqual
(
arpTargetProtocolAddress
,
packet
.
Ethernet
.
Arp
.
TargetProtocolAddress
,
"Arp TargetProtocolAddress"
);
Assert
.
AreEqual
(
arpLayer
,
packet
.
Ethernet
.
Arp
.
ExtractLayer
(),
"ARP Layer"
);
}
}
...
...
PcapDotNet/src/PcapDotNet.Packets.Test/EthernetTests.cs
View file @
1e1408a0
...
...
@@ -66,33 +66,20 @@ namespace PcapDotNet.Packets.Test
for
(
int
i
=
0
;
i
!=
1000
;
++
i
)
{
MacAddress
ethernetSource
=
random
.
NextMacAddress
();
MacAddress
ethernetDestination
=
random
.
NextMacAddress
();
EthernetType
ethernetType
=
random
.
NextEnum
(
EthernetType
.
None
);
EthernetLayer
ethernetLayer
=
random
.
NextEthernetLayer
();
int
ethernetPayloadLength
=
random
.
Next
(
1500
);
Datagram
ethernetPayload
=
random
.
NextDatagram
(
ethernetPayloadLength
);
Packet
packet
=
new
PacketBuilder2
(
new
EthernetLayer
{
Source
=
ethernetSource
,
Destination
=
ethernetDestination
,
EtherType
=
ethernetType
},
new
PayloadLayer
{
Data
=
ethernetPayload
})
.
Build
(
DateTime
.
Now
);
PayloadLayer
payloadLayer
=
new
PayloadLayer
{
Data
=
random
.
NextDatagram
(
ethernetPayloadLength
),
};
Packet
packet
=
new
PacketBuilder2
(
ethernetLayer
,
payloadLayer
).
Build
(
DateTime
.
Now
);
// Ethernet
Assert
.
AreEqual
(
packet
.
Length
-
EthernetDatagram
.
HeaderLength
,
packet
.
Ethernet
.
PayloadLength
,
"PayloadLength"
);
Assert
.
AreEqual
(
ethernet
Source
,
packet
.
Ethernet
.
Source
,
"Ethernet Source
"
);
Assert
.
AreEqual
(
ethernet
Layer
,
packet
.
Ethernet
.
ExtractLayer
(),
"Ethernet Layer
"
);
Assert
.
AreNotEqual
(
2
,
packet
.
Ethernet
.
Source
,
"Ethernet Source"
);
Assert
.
AreEqual
(
ethernetDestination
,
packet
.
Ethernet
.
Destination
,
"Ethernet Destination"
);
Assert
.
AreEqual
(
ethernetType
,
packet
.
Ethernet
.
EtherType
,
"Ethernet Type"
);
Assert
.
AreEqual
(
ethernetPayload
,
packet
.
Ethernet
.
Payload
);
Assert
.
AreEqual
(
payloadLayer
.
Data
,
packet
.
Ethernet
.
Payload
);
}
}
}
...
...
PcapDotNet/src/PcapDotNet.Packets.Test/IcmpTests.cs
View file @
1e1408a0
...
...
@@ -76,18 +76,7 @@ namespace PcapDotNet.Packets.Test
Random
random
=
new
Random
();
IpV4Layer
ipV4Layer
=
new
IpV4Layer
{
TypeOfService
=
random
.
NextByte
(),
Identification
=
random
.
NextUShort
(),
Ttl
=
random
.
NextByte
(),
Fragmentation
=
random
.
NextIpV4Fragmentation
(),
Source
=
random
.
NextIpV4Address
(),
Destination
=
random
.
NextIpV4Address
(),
Options
=
random
.
NextIpV4Options
()
};
IpV4Layer
ipV4Layer
=
random
.
NextIpV4Layer
(
null
);
for
(
int
i
=
0
;
i
!=
1000
;
++
i
)
{
IcmpLayer
icmpLayer
=
random
.
NextIcmpLayer
();
...
...
@@ -129,22 +118,9 @@ namespace PcapDotNet.Packets.Test
PayloadLayer
icmpIpV4PayloadLayer
=
null
;
if
(
isIpV4Payload
)
{
icmpIpV4Layer
=
new
IpV4Layer
{
TypeOfService
=
random
.
NextByte
(),
Identification
=
random
.
NextUShort
(),
Fragmentation
=
random
.
NextIpV4Fragmentation
(),
Ttl
=
random
.
NextByte
(),
Protocol
=
random
.
NextEnum
<
IpV4Protocol
>(),
Source
=
random
.
NextIpV4Address
(),
Destination
=
random
.
NextIpV4Address
(),
Options
=
random
.
NextIpV4Options
(),
};
icmpIpV4PayloadLayer
=
new
PayloadLayer
{
Data
=
random
.
NextDatagram
(
random
.
Next
(
200
))
};
icmpIpV4Layer
=
random
.
NextIpV4Layer
();
icmpIpV4PayloadLayer
=
random
.
NextPayloadLayer
(
random
.
Next
(
200
));
packetBuilder2
=
new
PacketBuilder2
(
ethernetLayer
,
ipV4Layer
,
icmpLayer
,
icmpIpV4Layer
,
icmpIpV4PayloadLayer
);
}
...
...
PcapDotNet/src/PcapDotNet.Packets.Test/IgmpTests.cs
View file @
1e1408a0
This diff is collapsed.
Click to expand it.
PcapDotNet/src/PcapDotNet.Packets.Test/IpV4Tests.cs
View file @
1e1408a0
...
...
@@ -104,96 +104,56 @@ namespace PcapDotNet.Packets.Test
MacAddress
ethernetDestination
=
new
MacAddress
(
"A0:A1:A2:A3:A4:A5"
);
const
EthernetType
ethernetType
=
EthernetType
.
IpV4
;
EthernetLayer
ethernetLayer
=
new
EthernetLayer
{
Source
=
ethernetSource
,
Destination
=
ethernetDestination
,
EtherType
=
ethernetType
};
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
!=
1000
;
++
i
)
{
byte
ipV4TypeOfService
=
random
.
NextByte
();
ushort
ipV4Identification
=
random
.
NextUShort
();
byte
ipV4Ttl
=
random
.
NextByte
();
IpV4FragmentationOptions
ipV4FragmentationOptions
=
random
.
NextEnum
<
IpV4FragmentationOptions
>();
ushort
ipV4FragmentationOffset
=
(
ushort
)(
random
.
NextUShort
(
ushort
.
MaxValue
/
8
)
*
8
);
IpV4Fragmentation
ipV4Fragmentation
=
new
IpV4Fragmentation
(
ipV4FragmentationOptions
,
ipV4FragmentationOffset
);
IpV4Protocol
ipV4Protocol
=
random
.
NextEnum
<
IpV4Protocol
>();
IpV4Address
ipV4Source
=
new
IpV4Address
(
random
.
NextUInt
());
IpV4Address
ipV4Destination
=
new
IpV4Address
(
random
.
NextUInt
());
IpV4Options
ipV4Options
=
random
.
NextIpV4Options
();
byte
[]
ipV4PayloadBuffer
=
new
byte
[
random
.
Next
(
0
,
50
*
1024
)];
random
.
NextBytes
(
ipV4PayloadBuffer
);
Datagram
ipV4Payload
=
new
Datagram
(
ipV4PayloadBuffer
);
Packet
packet
=
new
PacketBuilder2
(
new
EthernetLayer
{
Source
=
ethernetSource
,
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
||
ipV4Protocol
==
IpV4Protocol
.
Tcp
||
ipV4Protocol
==
IpV4Protocol
.
InternetGroupManagementProtocol
||
IpV4Layer
ipV4Layer
=
random
.
NextIpV4Layer
();
PayloadLayer
payloadLayer
=
random
.
NextPayloadLayer
(
random
.
Next
(
0
,
50
*
1024
));
Packet
packet
=
new
PacketBuilder2
(
ethernetLayer
,
ipV4Layer
,
payloadLayer
).
Build
(
DateTime
.
Now
);
Assert
.
IsTrue
(
ipV4Layer
.
Protocol
==
IpV4Protocol
.
Udp
||
ipV4Layer
.
Protocol
==
IpV4Protocol
.
Tcp
||
ipV4Layer
.
Protocol
==
IpV4Protocol
.
InternetGroupManagementProtocol
||
ipV4Layer
.
Protocol
==
IpV4Protocol
.
InternetControlMessageProtocol
||
packet
.
IsValid
,
"IsValid"
);
// Ethernet
Assert
.
AreEqual
(
packet
.
Length
-
EthernetDatagram
.
HeaderLength
,
packet
.
Ethernet
.
PayloadLength
,
"PayloadLength"
);
Assert
.
AreEqual
(
ethernetSource
,
packet
.
Ethernet
.
Source
,
"Ethernet Source"
);
Assert
.
AreEqual
(
ethernetDestination
,
packet
.
Ethernet
.
Destination
,
"Ethernet Destination"
);
Assert
.
AreEqual
(
ethernetType
,
packet
.
Ethernet
.
EtherType
,
"Ethernet Type"
);
Assert
.
AreEqual
(
ethernetLayer
,
packet
.
Ethernet
.
ExtractLayer
(),
"Ethernet Layer"
);
// IpV4
Assert
.
AreEqual
(
IpV4Datagram
.
HeaderMinimumLength
+
ipV4Options
.
BytesLength
,
packet
.
Ethernet
.
IpV4
.
HeaderLength
,
"IP HeaderLength
"
);
Assert
.
AreEqual
(
ipV4TypeOfService
,
packet
.
Ethernet
.
IpV4
.
TypeOfService
,
"IP TypeOfService
"
);
Assert
.
AreEqual
(
ipV4Layer
,
packet
.
Ethernet
.
IpV4
.
ExtractLayer
(),
"IP Layer
"
);
Assert
.
AreEqual
(
IpV4Datagram
.
HeaderMinimumLength
+
ipV4Layer
.
Options
.
BytesLength
,
packet
.
Ethernet
.
IpV4
.
HeaderLength
,
"IP HeaderLength
"
);
Assert
.
AreEqual
(
packet
.
Length
-
EthernetDatagram
.
HeaderLength
,
packet
.
Ethernet
.
IpV4
.
TotalLength
,
"IP TotalLength"
);
Assert
.
AreEqual
(
ipV4Identification
,
packet
.
Ethernet
.
IpV4
.
Identification
,
"IP Identification"
);
Assert
.
AreEqual
(
ipV4Fragmentation
,
packet
.
Ethernet
.
IpV4
.
Fragmentation
,
"IP Fragmentation"
);
Assert
.
AreNotEqual
(
2
,
packet
.
Ethernet
.
IpV4
.
Fragmentation
,
"IP Fragmentation"
);
Assert
.
IsTrue
(
ipV4Fragmentation
==
packet
.
Ethernet
.
IpV4
.
Fragmentation
,
"IP Fragmentation"
);
Assert
.
IsFalse
(
ipV4Fragmentation
!=
packet
.
Ethernet
.
IpV4
.
Fragmentation
,
"IP Fragmentation"
);
Assert
.
AreEqual
(
ipV4Fragmentation
.
GetHashCode
(),
packet
.
Ethernet
.
IpV4
.
Fragmentation
.
GetHashCode
(),
"IP Fragmentation"
);
Assert
.
AreEqual
(
ipV4Fragmentation
.
Options
,
packet
.
Ethernet
.
IpV4
.
Fragmentation
.
Options
,
"IP Fragmentation"
);
Assert
.
AreEqual
(
ipV4Fragmentation
.
Offset
,
packet
.
Ethernet
.
IpV4
.
Fragmentation
.
Offset
,
"IP Fragmentation"
);
if
(
ipV4Fragmentation
.
Equals
(
IpV4Fragmentation
.
None
))
Assert
.
IsTrue
(
ipV4
Layer
.
Fragmentation
==
packet
.
Ethernet
.
IpV4
.
Fragmentation
,
"IP Fragmentation"
);
Assert
.
IsFalse
(
ipV4
Layer
.
Fragmentation
!=
packet
.
Ethernet
.
IpV4
.
Fragmentation
,
"IP Fragmentation"
);
Assert
.
AreEqual
(
ipV4
Layer
.
Fragmentation
.
GetHashCode
(),
packet
.
Ethernet
.
IpV4
.
Fragmentation
.
GetHashCode
(),
"IP Fragmentation"
);
Assert
.
AreEqual
(
ipV4
Layer
.
Fragmentation
.
Options
,
packet
.
Ethernet
.
IpV4
.
Fragmentation
.
Options
,
"IP Fragmentation"
);
Assert
.
AreEqual
(
ipV4
Layer
.
Fragmentation
.
Offset
,
packet
.
Ethernet
.
IpV4
.
Fragmentation
.
Offset
,
"IP Fragmentation"
);
if
(
ipV4
Layer
.
Fragmentation
.
Equals
(
IpV4Fragmentation
.
None
))
{
Assert
.
AreEqual
(
IpV4FragmentationOptions
.
None
,
packet
.
Ethernet
.
IpV4
.
Fragmentation
.
Options
,
"IP Fragmentation"
);
Assert
.
AreEqual
(
0
,
packet
.
Ethernet
.
IpV4
.
Fragmentation
.
Offset
,
"IP Fragmentation"
);
}
Assert
.
AreEqual
(
ipV4Ttl
,
packet
.
Ethernet
.
IpV4
.
Ttl
,
"IP Ttl"
);
Assert
.
AreEqual
(
ipV4Protocol
,
packet
.
Ethernet
.
IpV4
.
Protocol
,
"IP Protocol"
);
// Assert.AreEqual(0x9010, packet.Ethernet.IpV4.HeaderChecksum, "IP HeaderChecksum");
Assert
.
AreEqual
(
true
,
packet
.
Ethernet
.
IpV4
.
IsHeaderChecksumCorrect
,
"IP HeaderChecksumCorrect"
);
Assert
.
AreEqual
(
ipV4Source
,
packet
.
Ethernet
.
IpV4
.
Source
,
"IP Source"
);
Assert
.
AreEqual
(
ipV4Destination
,
packet
.
Ethernet
.
IpV4
.
Destination
,
"IP Destination"
);
Assert
.
AreEqual
(
ipV4Options
,
packet
.
Ethernet
.
IpV4
.
Options
,
"IP Options"
);
Assert
.
AreNotEqual
(
null
,
packet
.
Ethernet
.
IpV4
.
Options
,
"IP Options"
);
Assert
.
AreNotEqual
(
new
IpV4Options
(
new
IpV4OptionUnknown
(
0
,
new
byte
[
35
])),
packet
.
Ethernet
.
IpV4
.
Options
,
"IP Options"
);
Assert
.
AreEqual
(
ipV4Options
.
GetHashCode
(),
packet
.
Ethernet
.
IpV4
.
Options
.
GetHashCode
(),
"IP Options HashCode"
);
Assert
.
AreEqual
(
ipV4
Layer
.
Options
.
GetHashCode
(),
packet
.
Ethernet
.
IpV4
.
Options
.
GetHashCode
(),
"IP Options HashCode"
);
Assert
.
IsNotNull
(
packet
.
Ethernet
.
IpV4
.
Options
.
ToString
());
for
(
int
optionIndex
=
0
;
optionIndex
!=
ipV4Options
.
Count
;
++
optionIndex
)
for
(
int
optionIndex
=
0
;
optionIndex
!=
ipV4
Layer
.
Options
.
Count
;
++
optionIndex
)
{
IpV4Option
option
=
ipV4Options
[
optionIndex
];
IpV4Option
option
=
ipV4
Layer
.
Options
[
optionIndex
];
Assert
.
AreEqual
(
option
,
packet
.
Ethernet
.
IpV4
.
Options
[
optionIndex
]);
Assert
.
IsFalse
(
option
.
Equals
(
null
));
}
...
...
@@ -205,7 +165,7 @@ namespace PcapDotNet.Packets.Test
else
Assert
.
IsNull
(
packet
.
Ethernet
.
IpV4
.
Transport
);
Assert
.
AreEqual
(
ipV4Payload
,
packet
.
Ethernet
.
IpV4
.
Payload
,
"IP Payload"
);
Assert
.
AreEqual
(
payloadLayer
,
packet
.
Ethernet
.
IpV4
.
Payload
.
ExtractLayer
()
,
"IP Payload"
);
}
}
...
...
PcapDotNet/src/PcapDotNet.Packets.Test/TcpTests.cs
View file @
1e1408a0
This diff is collapsed.
Click to expand it.
PcapDotNet/src/PcapDotNet.Packets.TestUtils/RandomPacketsExtensions.cs
View file @
1e1408a0
...
...
@@ -4,6 +4,7 @@ using System.Linq;
using
System.Text
;
using
PcapDotNet.Base
;
using
PcapDotNet.Packets
;
using
PcapDotNet.Packets.Arp
;
using
PcapDotNet.Packets.Ethernet
;
using
PcapDotNet.Packets.Icmp
;
using
PcapDotNet.Packets.Igmp
;
...
...
@@ -34,8 +35,26 @@ namespace PcapDotNet.Packets.TestUtils
return
new
Packet
(
buffer
,
DateTime
.
Now
,
random
.
NextDataLinkKind
());
}
public
static
PayloadLayer
NextPayloadLayer
(
this
Random
random
,
int
length
)
{
return
new
PayloadLayer
{
Data
=
random
.
NextDatagram
(
length
)
};
}
// Ethernet
public
static
EthernetLayer
NextEthernetLayer
(
this
Random
random
)
{
return
new
EthernetLayer
{
Source
=
random
.
NextMacAddress
(),
Destination
=
random
.
NextMacAddress
(),
EtherType
=
random
.
NextEnum
(
EthernetType
.
None
)
};
}
public
static
MacAddress
NextMacAddress
(
this
Random
random
)
{
return
new
MacAddress
(
random
.
NextUInt48
());
...
...
@@ -71,8 +90,46 @@ namespace PcapDotNet.Packets.TestUtils
return
random
.
NextEthernetPacket
(
packetSize
,
DateTime
.
Now
,
random
.
NextMacAddress
(),
random
.
NextMacAddress
());
}
// ARP
public
static
ArpLayer
NextArpLayer
(
this
Random
random
)
{
byte
hardwareAddressLength
=
random
.
NextByte
();
byte
protocolAddressLength
=
random
.
NextByte
();
return
new
ArpLayer
{
ProtocolType
=
random
.
NextEnum
<
EthernetType
>(),
Operation
=
random
.
NextEnum
<
ArpOperation
>(),
SenderHardwareAddress
=
random
.
NextBytes
(
hardwareAddressLength
),
SenderProtocolAddress
=
random
.
NextBytes
(
protocolAddressLength
),
TargetHardwareAddress
=
random
.
NextBytes
(
hardwareAddressLength
),
TargetProtocolAddress
=
random
.
NextBytes
(
protocolAddressLength
)
};
}
// IPv4
public
static
IpV4Layer
NextIpV4Layer
(
this
Random
random
,
IpV4Protocol
?
protocol
)
{
return
new
IpV4Layer
{
TypeOfService
=
random
.
NextByte
(),
Identification
=
random
.
NextUShort
(),
Ttl
=
random
.
NextByte
(),
Protocol
=
protocol
,
Fragmentation
=
random
.
NextIpV4Fragmentation
(),
Source
=
random
.
NextIpV4Address
(),
Destination
=
random
.
NextIpV4Address
(),
Options
=
random
.
NextIpV4Options
()
};
}
public
static
IpV4Layer
NextIpV4Layer
(
this
Random
random
)
{
return
random
.
NextIpV4Layer
(
random
.
NextEnum
<
IpV4Protocol
>());
}
public
static
IpV4Address
NextIpV4Address
(
this
Random
random
)
{
return
new
IpV4Address
(
random
.
NextUInt
());
...
...
@@ -254,6 +311,24 @@ namespace PcapDotNet.Packets.TestUtils
return
new
IpV4Options
(
options
);
}
// TCP
public
static
TcpLayer
NextTcpLayer
(
this
Random
random
)
{
return
new
TcpLayer
{
SourcePort
=
random
.
NextUShort
(),
DestinationPort
=
random
.
NextUShort
(),
SequenceNumber
=
random
.
NextUInt
(),
AcknowledgmentNumber
=
random
.
NextUInt
(),
ControlBits
=
random
.
NextFlags
<
TcpControlBits
>(),
Window
=
random
.
NextUShort
(),
UrgentPointer
=
random
.
NextUShort
(),
Options
=
random
.
NextTcpOptions
(),
};
}
public
static
TcpOptionUnknown
NextTcpOptionUnknown
(
this
Random
random
,
int
maximumOptionLength
)
{
TcpOptionType
unknownOptionType
;
...
...
PcapDotNet/src/PcapDotNet.Packets/Arp/ArpDatagram.cs
View file @
1e1408a0
using
System
;
using
System.Collections.Generic
;
using
System.Collections.ObjectModel
;
using
System.Linq
;
using
PcapDotNet.Packets.Ethernet
;
using
PcapDotNet.Packets.IpV4
;
...
...
@@ -146,6 +148,19 @@ namespace PcapDotNet.Packets.Arp
get
{
return
ReadIpV4Address
(
OffsetTargetProtocolAddress
,
Endianity
.
Big
);
}
}
public
override
ILayer
ExtractLayer
()
{
return
new
ArpLayer
{
ProtocolType
=
ProtocolType
,
Operation
=
Operation
,
SenderHardwareAddress
=
SenderHardwareAddress
,
SenderProtocolAddress
=
SenderProtocolAddress
,
TargetHardwareAddress
=
TargetHardwareAddress
,
TargetProtocolAddress
=
TargetProtocolAddress
};
}
/// <summary>
/// The datagram is valid if the length is correct according to the header.
/// </summary>
...
...
@@ -166,13 +181,13 @@ namespace PcapDotNet.Packets.Arp
internal
static
void
WriteHeader
(
byte
[]
buffer
,
int
offset
,
ArpHardwareType
hardwareType
,
EthernetType
protocolType
,
ArpOperation
operation
,
byte
[]
senderHardwareAddress
,
byte
[]
senderProtocolAddress
,
byte
[]
targetHardwareAddress
,
byte
[]
targetProtocolAddress
)
IList
<
byte
>
senderHardwareAddress
,
IList
<
byte
>
senderProtocolAddress
,
IList
<
byte
>
targetHardwareAddress
,
IList
<
byte
>
targetProtocolAddress
)
{
buffer
.
Write
(
ref
offset
,
(
ushort
)
hardwareType
,
Endianity
.
Big
);
buffer
.
Write
(
ref
offset
,
(
ushort
)
protocolType
,
Endianity
.
Big
);
buffer
.
Write
(
ref
offset
,
(
byte
)
senderHardwareAddress
.
Length
);
buffer
.
Write
(
ref
offset
,
(
byte
)
senderProtocolAddress
.
Length
);
buffer
.
Write
(
ref
offset
,
(
byte
)
senderHardwareAddress
.
Count
);
buffer
.
Write
(
ref
offset
,
(
byte
)
senderProtocolAddress
.
Count
);
buffer
.
Write
(
ref
offset
,
(
ushort
)
operation
,
Endianity
.
Big
);
buffer
.
Write
(
ref
offset
,
senderHardwareAddress
);
buffer
.
Write
(
ref
offset
,
senderProtocolAddress
);
...
...
PcapDotNet/src/PcapDotNet.Packets/Igmp/IgmpDatagram.cs
View file @
1e1408a0
...
...
@@ -480,6 +480,83 @@ namespace PcapDotNet.Packets.Igmp
}
}
public
override
ILayer
ExtractLayer
()
{
// IgmpMessageType igmpMessageType = random.NextEnum(IgmpMessageType.None);
// IgmpQueryVersion igmpQueryVersion = IgmpQueryVersion.None;
// TimeSpan igmpMaxResponseTime = random.NextTimeSpan(TimeSpan.FromSeconds(0.1), TimeSpan.FromSeconds(256 * 0.1) - TimeSpan.FromTicks(1));
// IpV4Address igmpGroupAddress = random.NextIpV4Address();
// bool? igmpIsSuppressRouterSideProcessing = null;
// byte? igmpQueryRobustnessVariable = null;
// TimeSpan? igmpQueryInterval = null;
// IpV4Address[] igmpSourceAddresses = null;
// IgmpGroupRecord[] igmpGroupRecords = null;
switch
(
MessageType
)
{
case
IgmpMessageType
.
MembershipQuery
:
switch
(
QueryVersion
)
{
case
IgmpQueryVersion
.
Version1
:
return
new
IgmpQueryVersion1Layer
{
GroupAddress
=
GroupAddress
};
case
IgmpQueryVersion
.
Version2
:
return
new
IgmpQueryVersion2Layer
{
MaxResponseTime
=
MaxResponseTime
,
GroupAddress
=
GroupAddress
};
case
IgmpQueryVersion
.
Version3
:
return
new
IgmpQueryVersion3Layer
{
MaxResponseTime
=
MaxResponseTime
,
GroupAddress
=
GroupAddress
,
IsSuppressRouterSideProcessing
=
IsSuppressRouterSideProcessing
,
QueryRobustnessVariable
=
QueryRobustnessVariable
,
QueryInterval
=
QueryInterval
,
SourceAddresses
=
SourceAddresses
};
default
:
throw
new
InvalidOperationException
(
"Invalid Query Version "
+
QueryVersion
);
}
case
IgmpMessageType
.
MembershipReportVersion1
:
return
new
IgmpReportVersion1Layer
{
GroupAddress
=
GroupAddress
};
case
IgmpMessageType
.
MembershipReportVersion2
:
return
new
IgmpReportVersion2Layer
{
MaxResponseTime
=
MaxResponseTime
,
GroupAddress
=
GroupAddress
};
case
IgmpMessageType
.
LeaveGroupVersion2
:
return
new
IgmpLeaveGroupVersion2Layer
{
MaxResponseTime
=
MaxResponseTime
,
GroupAddress
=
GroupAddress
};
case
IgmpMessageType
.
MembershipReportVersion3
:
return
new
IgmpReportVersion3Layer
{
GroupRecords
=
GroupRecords
.
Select
(
record
=>
record
.
ToGroupRecord
()).
ToList
()
};
default
:
throw
new
InvalidOperationException
(
"Invalid message type "
+
MessageType
);
}
}
internal
IgmpDatagram
(
byte
[]
buffer
,
int
offset
,
int
length
)
:
base
(
buffer
,
offset
,
length
)
{
...
...
PcapDotNet/src/PcapDotNet.Packets/PacketBuilder2.cs
View file @
1e1408a0
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
PcapDotNet.Base
;
using
PcapDotNet.Packets.Arp
;
using
PcapDotNet.Packets.Ethernet
;
using
PcapDotNet.Packets.Icmp
;
...
...
@@ -137,6 +138,11 @@ namespace PcapDotNet.Packets
{
return
base
.
Equals
(
other
)
&&
Equals
(
other
as
EthernetLayer
);
}
public
override
string
ToString
()
{
return
Source
+
" -> "
+
Destination
+
" ("
+
EtherType
+
")"
;
}
}
public
class
PayloadLayer
:
SimpleLayer
...
...
@@ -261,6 +267,11 @@ namespace PcapDotNet.Packets
{
return
base
.
Equals
(
other
)
&&
Equals
(
other
as
IpV4Layer
);
}
public
override
string
ToString
()
{
return
Source
+
" -> "
+
Destination
+
" ("
+
Protocol
+
")"
;
}
}
public
abstract
class
TransportLayer
:
Layer
,
IIpV4NextTransportLayer
...
...
@@ -341,7 +352,7 @@ namespace PcapDotNet.Packets
SequenceNumber
==
other
.
SequenceNumber
&&
AcknowledgmentNumber
==
other
.
AcknowledgmentNumber
&&
ControlBits
==
other
.
ControlBits
&&
Window
==
other
.
Window
&&
UrgentPointer
==
other
.
UrgentPointer
&&
Options
==
other
.
Options
;
Options
.
Equals
(
other
.
Options
)
;
}
public
override
sealed
bool
Equals
(
TransportLayer
other
)
...
...
@@ -391,13 +402,13 @@ namespace PcapDotNet.Packets
public
ArpOperation
Operation
{
get
;
set
;
}
public
byte
[]
SenderHardwareAddress
{
get
;
set
;
}
public
IList
<
byte
>
SenderHardwareAddress
{
get
;
set
;
}
public
byte
[]
SenderProtocolAddress
{
get
;
set
;
}
public
IList
<
byte
>
SenderProtocolAddress
{
get
;
set
;
}
public
byte
[]
TargetHardwareAddress
{
get
;
set
;
}
public
IList
<
byte
>
TargetHardwareAddress
{
get
;
set
;
}
public
byte
[]
TargetProtocolAddress
{
get
;
set
;
}
public
IList
<
byte
>
TargetProtocolAddress
{
get
;
set
;
}
public
EthernetType
PreviousLayerEtherType
{
...
...
@@ -411,7 +422,7 @@ namespace PcapDotNet.Packets
public
override
int
Length
{
get
{
return
ArpDatagram
.
GetHeaderLength
(
SenderHardwareAddress
.
Length
,
SenderProtocolAddress
.
Length
);
}
get
{
return
ArpDatagram
.
GetHeaderLength
(
SenderHardwareAddress
.
Count
,
SenderProtocolAddress
.
Count
);
}
}
public
override
void
Write
(
byte
[]
buffer
,
int
offset
,
int
payloadLength
,
ILayer
previousLayer
,
ILayer
nextLayer
)
...
...
@@ -423,15 +434,15 @@ namespace PcapDotNet.Packets
if
(
arpPreviousLayer
==
null
)
throw
new
ArgumentException
(
"The layer before the ARP layer must be an ARP previous layer and can't be "
+
previousLayer
.
GetType
());
if
(
SenderHardwareAddress
.
Length
!=
TargetHardwareAddress
.
Length
)
if
(
SenderHardwareAddress
.
Count
!=
TargetHardwareAddress
.
Count
)
{
throw
new
ArgumentException
(
"Sender hardware address length is "
+
SenderHardwareAddress
.
Length
+
" bytes "
+
"while target hardware address length is "
+
TargetHardwareAddress
.
Length
+
" bytes"
);
throw
new
ArgumentException
(
"Sender hardware address length is "
+
SenderHardwareAddress
.
Count
+
" bytes "
+
"while target hardware address length is "
+
TargetHardwareAddress
.
Count
+
" bytes"
);
}
if
(
SenderProtocolAddress
.
Length
!=
TargetProtocolAddress
.
Length
)
if
(
SenderProtocolAddress
.
Count
!=
TargetProtocolAddress
.
Count
)
{
throw
new
ArgumentException
(
"Sender protocol address length is "
+
SenderProtocolAddress
.
Length
+
" bytes "
+
"while target protocol address length is "
+
TargetProtocolAddress
.
Length
+
" bytes"
);
throw
new
ArgumentException
(
"Sender protocol address length is "
+
SenderProtocolAddress
.
Count
+
" bytes "
+
"while target protocol address length is "
+
TargetProtocolAddress
.
Count
+
" bytes"
);
}
ArpDatagram
.
WriteHeader
(
buffer
,
offset
,
...
...
@@ -474,7 +485,7 @@ namespace PcapDotNet.Packets
return
other
!=
null
&&
MessageType
==
other
.
MessageType
&&
QueryVersion
==
other
.
QueryVersion
&&
MaxResponseTimeValue
=
=
other
.
MaxResponseTimeValue
;
MaxResponseTimeValue
.
Divide
(
2
)
<=
other
.
MaxResponseTimeValue
&&
MaxResponseTimeValue
.
Multiply
(
2
)
>
=
other
.
MaxResponseTimeValue
;
}
public
sealed
override
bool
Equals
(
Layer
other
)
...
...
@@ -573,11 +584,11 @@ namespace PcapDotNet.Packets
public
TimeSpan
QueryInterval
{
get
;
set
;}
public
I
pV4Address
[]
SourceAddresses
{
get
;
set
;}
public
I
List
<
IpV4Address
>
SourceAddresses
{
get
;
set
;}
public
override
int
Length
{
get
{
return
IgmpDatagram
.
GetQueryVersion3Length
(
SourceAddresses
.
Length
);
}
get
{
return
IgmpDatagram
.
GetQueryVersion3Length
(
SourceAddresses
.
Count
);
}
}
protected
override
void
Write
(
byte
[]
buffer
,
int
offset
)
...
...
@@ -611,7 +622,7 @@ namespace PcapDotNet.Packets
GroupAddress
==
other
.
GroupAddress
&&
IsSuppressRouterSideProcessing
==
other
.
IsSuppressRouterSideProcessing
&&
QueryRobustnessVariable
==
other
.
QueryRobustnessVariable
&&
QueryInterval
=
=
other
.
QueryInterval
&&
QueryInterval
.
Divide
(
2
)
<=
other
.
QueryInterval
&&
QueryInterval
.
Multiply
(
2
)
>
=
other
.
QueryInterval
&&
SourceAddresses
.
SequenceEqual
(
other
.
SourceAddresses
);
}
...
...
@@ -647,7 +658,7 @@ namespace PcapDotNet.Packets
public
class
IgmpReportVersion3Layer
:
IgmpLayer
{
public
I
gmpGroupRecord
[]
GroupRecords
{
get
;
set
;}
public
I
List
<
IgmpGroupRecord
>
GroupRecords
{
get
;
set
;}
public
override
int
Length
{
...
...
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpDatagram.cs
View file @
1e1408a0
...
...
@@ -166,14 +166,6 @@ namespace PcapDotNet.Packets.Transport
}
}
/// <summary>
/// The payload of the TCP datagram.
/// </summary>
public
Datagram
Payload
{
get
{
return
new
Datagram
(
Buffer
,
StartOffset
+
HeaderLength
,
PayloadLength
);
}
}
/// <summary>
/// The length of the TCP payload.
/// </summary>
...
...
@@ -246,6 +238,29 @@ namespace PcapDotNet.Packets.Transport
get
{
return
(
ControlBits
&
TcpControlBits
.
Fin
)
==
TcpControlBits
.
Fin
;
}
}
public
override
ILayer
ExtractLayer
()
{
return
new
TcpLayer
{
SourcePort
=
SourcePort
,
DestinationPort
=
DestinationPort
,
SequenceNumber
=
SequenceNumber
,
AcknowledgmentNumber
=
AcknowledgmentNumber
,
ControlBits
=
ControlBits
,
Window
=
Window
,
UrgentPointer
=
UrgentPointer
,
Options
=
Options
};
}
/// <summary>
/// The payload of the TCP datagram.
/// </summary>
public
Datagram
Payload
{
get
{
return
new
Datagram
(
Buffer
,
StartOffset
+
HeaderLength
,
PayloadLength
);
}
}
/// <summary>
/// The datagram is valid if the length is correct according to the header and the options are valid.
/// </summary>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment