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
8f749c59
Commit
8f749c59
authored
Aug 15, 2009
by
Brickner_cp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TCP.
Fixed DateTime randomization.
parent
7fc31eea
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
624 additions
and
43 deletions
+624
-43
PcapDotNet.4.5.resharper
PcapDotNet/src/PcapDotNet.4.5.resharper
+4
-0
TupleTests.cs
PcapDotNet/src/PcapDotNet.Base.Test/TupleTests.cs
+2
-1
MorePropertyInfo.cs
PcapDotNet/src/PcapDotNet.Base/MorePropertyInfo.cs
+12
-0
PcapDotNet.Base.csproj
PcapDotNet/src/PcapDotNet.Base/PcapDotNet.Base.csproj
+1
-0
MoreIpV4Option.cs
PcapDotNet/src/PcapDotNet.Core.Test/MoreIpV4Option.cs
+4
-1
WiresharkCompareTests.cs
PcapDotNet/src/PcapDotNet.Core.Test/WiresharkCompareTests.cs
+154
-17
IpV4Tests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/IpV4Tests.cs
+1
-1
PcapDotNet.Packets.Test.csproj
...rc/PcapDotNet.Packets.Test/PcapDotNet.Packets.Test.csproj
+1
-0
TcpTests.cs
PcapDotNet/src/PcapDotNet.Packets.Test/TcpTests.cs
+130
-0
IpV4Datagram.cs
PcapDotNet/src/PcapDotNet.Packets/IpV4/IpV4Datagram.cs
+33
-11
PacketBuilder.cs
PcapDotNet/src/PcapDotNet.Packets/PacketBuilder.cs
+58
-1
PcapDotNet.Packets.csproj
PcapDotNet/src/PcapDotNet.Packets/PcapDotNet.Packets.csproj
+2
-0
TcpDatagram.cs
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpDatagram.cs
+101
-6
TcpFlags.cs
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpFlags.cs
+27
-0
TcpOption.cs
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpOption.cs
+6
-0
TcpOptions.cs
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpOptions.cs
+42
-0
TransportDatagram.cs
...Net/src/PcapDotNet.Packets/Transport/TransportDatagram.cs
+8
-0
UdpDatagram.cs
PcapDotNet/src/PcapDotNet.Packets/Transport/UdpDatagram.cs
+12
-4
MoreRandom.cs
PcapDotNet/src/PcapDotNet.TestUtils/MoreRandom.cs
+26
-1
No files found.
PcapDotNet/src/PcapDotNet.4.5.resharper
View file @
8f749c59
...
...
@@ -71,6 +71,10 @@
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="StaticReadonly" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="EnumMember" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="Other" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateConstants" />
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PrivateInstanceFields" />
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PrivateStaticFields" />
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PrivateStaticReadonly" />
</Naming2>
</CodeStyleSettings>
</Configuration>
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Base.Test/TupleTests.cs
View file @
8f749c59
...
...
@@ -139,9 +139,10 @@ namespace PcapDotNet.Base.Test
{
const
long
ticks
=
633737178954260865
;
Assert
.
AreEqual
(
"Jerusalem Daylight Time"
,
TimeZone
.
CurrentTimeZone
.
DaylightName
);
Assert
.
AreEqual
(
"Israel Standard Time"
,
TimeZoneInfo
.
Local
.
Id
);
DateTime
dateTime
=
new
DateTime
(
ticks
,
DateTimeKind
.
Local
);
// Assert.AreEqual(dateTime.ToLocalTime(), dateTime.ToUniversalTime().ToLocalTime(
));
Assert
.
IsTrue
(
TimeZoneInfo
.
Local
.
IsInvalidTime
(
dateTime
));
}
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Base/MorePropertyInfo.cs
0 → 100644
View file @
8f749c59
using
System.Reflection
;
namespace
PcapDotNet.Base
{
public
static
class
MorePropertyInfo
{
public
static
object
GetValue
(
this
PropertyInfo
propertyInfo
,
object
obj
)
{
return
propertyInfo
.
GetValue
(
obj
,
null
);
}
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Base/PcapDotNet.Base.csproj
View file @
8f749c59
...
...
@@ -57,6 +57,7 @@
<ItemGroup>
<Compile
Include=
"MoreIEnumerable.cs"
/>
<Compile
Include=
"MoreIList.cs"
/>
<Compile
Include=
"MorePropertyInfo.cs"
/>
<Compile
Include=
"Tuple.cs"
/>
<Compile
Include=
"UInt24.cs"
/>
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
...
...
PcapDotNet/src/PcapDotNet.Core.Test/MoreIpV4Option.cs
View file @
8f749c59
...
...
@@ -39,7 +39,10 @@ namespace PcapDotNet.Core.Test
return
"Unknown (0x52) (12 bytes)"
;
case
IpV4OptionType
.
RouterAlert
:
return
"Router Alert: Unknown ("
+
((
IpV4OptionRouterAlert
)
option
).
Value
+
")"
;
ushort
routerAlertValue
=
((
IpV4OptionRouterAlert
)
option
).
Value
;
return
"Router Alert: "
+
((
routerAlertValue
!=
0
)
?
"Unknown ("
+
routerAlertValue
+
")"
:
"Every router examines packet"
);
case
IpV4OptionType
.
QuickStart
:
IpV4OptionQuickStart
quickStart
=
(
IpV4OptionQuickStart
)
option
;
...
...
PcapDotNet/src/PcapDotNet.Core.Test/WiresharkCompareTests.cs
View file @
8f749c59
This diff is collapsed.
Click to expand it.
PcapDotNet/src/PcapDotNet.Packets.Test/IpV4Tests.cs
View file @
8f749c59
...
...
@@ -129,7 +129,7 @@ namespace PcapDotNet.Packets.Test
ipV4Payload
);
Assert
.
IsTrue
(
ipV4Protocol
==
IpV4Protocol
.
Udp
||
packet
.
IsValid
,
"IsValid"
);
Assert
.
IsTrue
(
ipV4Protocol
==
IpV4Protocol
.
Udp
||
ipV4Protocol
==
IpV4Protocol
.
Tcp
||
packet
.
IsValid
,
"IsValid"
);
// Ethernet
Assert
.
AreEqual
(
packet
.
Length
-
EthernetDatagram
.
HeaderLength
,
packet
.
Ethernet
.
PayloadLength
,
"PayloadLength"
);
...
...
PcapDotNet/src/PcapDotNet.Packets.Test/PcapDotNet.Packets.Test.csproj
View file @
8f749c59
...
...
@@ -51,6 +51,7 @@
<Compile Include="PacketTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IpV4AddressTests.cs" />
<Compile Include="TcpTests.cs" />
<Compile Include="UdpTests.cs" />
</ItemGroup>
<ItemGroup>
...
...
PcapDotNet/src/PcapDotNet.Packets.Test/TcpTests.cs
0 → 100644
View file @
8f749c59
using
System
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
PcapDotNet.Packets.Ethernet
;
using
PcapDotNet.Packets.IpV4
;
using
PcapDotNet.Packets.TestUtils
;
using
PcapDotNet.Packets.Transport
;
using
PcapDotNet.TestUtils
;
namespace
PcapDotNet.Packets.Test
{
/// <summary>
/// Summary description for TcpTests
/// </summary>
[
TestClass
]
public
class
TcpTests
{
public
TcpTests
()
{
//
// TODO: Add constructor logic here
//
}
private
TestContext
testContextInstance
;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public
TestContext
TestContext
{
get
{
return
testContextInstance
;
}
set
{
testContextInstance
=
value
;
}
}
#
region
Additional
test
attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#
endregion
[
TestMethod
]
public
void
RandomTcpTest
()
{
MacAddress
ethernetSource
=
new
MacAddress
(
"00:01:02:03:04:05"
);
MacAddress
ethernetDestination
=
new
MacAddress
(
"A0:A1:A2:A3:A4:A5"
);
const
EthernetType
ethernetType
=
EthernetType
.
IpV4
;
Random
random
=
new
Random
();
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
();
for
(
int
i
=
0
;
i
!=
1000
;
++
i
)
{
ushort
tcpSourcePort
=
random
.
NextUShort
();
ushort
tcpDestinationPort
=
random
.
NextUShort
();
uint
tcpSequenceNumber
=
random
.
NextUInt
();
uint
tcpAcknowledgmentNumber
=
random
.
NextUInt
();
TcpFlags
tcpFlags
=
random
.
NextFlags
<
TcpFlags
>();
ushort
tcpWindow
=
random
.
NextUShort
();
ushort
tcpUrgentPointer
=
random
.
NextUShort
();
TcpOptions
tcpOptions
=
new
TcpOptions
();
Datagram
tcpPayload
=
random
.
NextDatagram
(
random
.
Next
(
60000
));
Packet
packet
=
PacketBuilder
.
EthernetIpV4Tcp
(
DateTime
.
Now
,
ethernetSource
,
ethernetDestination
,
ipV4TypeOfService
,
ipV4Identification
,
ipV4Fragmentation
,
ipV4Ttl
,
ipV4Source
,
ipV4Destination
,
ipV4Options
,
tcpSourcePort
,
tcpDestinationPort
,
tcpSequenceNumber
,
tcpAcknowledgmentNumber
,
tcpFlags
,
tcpWindow
,
tcpUrgentPointer
,
tcpOptions
,
tcpPayload
);
Assert
.
IsTrue
(
packet
.
IsValid
);
// UDP
Assert
.
AreEqual
(
tcpSourcePort
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
SourcePort
,
"Source Port"
);
Assert
.
AreEqual
(
tcpDestinationPort
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
DestinationPort
,
"Destination Port"
);
Assert
.
AreEqual
(
tcpSequenceNumber
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
SequenceNumber
,
"Sequence Number"
);
Assert
.
AreEqual
(
tcpAcknowledgmentNumber
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
AcknowledgmentNumber
,
"Acknowledgment Number"
);
Assert
.
AreEqual
(
tcpFlags
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
Flags
,
"Flags"
);
Assert
.
AreEqual
(
tcpWindow
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
Window
,
"Window"
);
Assert
.
AreEqual
(
tcpUrgentPointer
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
UrgentPointer
,
"Urgent Pointer"
);
Assert
.
AreEqual
(
TcpDatagram
.
HeaderMinimumLength
+
tcpOptions
.
BytesLength
+
tcpPayload
.
Length
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
Length
,
"Total Length"
);
Assert
.
IsTrue
(
packet
.
Ethernet
.
IpV4
.
IsTransportChecksumCorrect
,
"IsTransportChecksumCorrect"
);
Assert
.
AreEqual
(
tcpPayload
,
packet
.
Ethernet
.
IpV4
.
Tcp
.
Payload
,
"Payload"
);
}
}
[
TestMethod
]
public
void
UdpChecksumTest
()
{
Packet
packet
=
Packet
.
FromHexadecimalString
(
"3352c58e71ffc4f39ec3bae508004cfe0043361200008611eec22ea2c8d11e9eb7b9520c2a33f2bbbed998980bba4404f941019404eb51880496ce00000005a87a270013a683f572c10e1504a0df15448a"
,
DateTime
.
Now
,
DataLinkKind
.
Ethernet
);
Assert
.
IsTrue
(
packet
.
Ethernet
.
IpV4
.
IsTransportChecksumCorrect
);
}
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Packets/IpV4/IpV4Datagram.cs
View file @
8f749c59
...
...
@@ -189,8 +189,9 @@ namespace PcapDotNet.Packets.IpV4
{
if
(
_isTransportChecksumCorrect
==
null
)
{
ushort
transportChecksum
=
Udp
.
Checksum
;
_isTransportChecksumCorrect
=
(
transportChecksum
!=
0
)
&&
(
CalculateTransportChecksum
()
==
transportChecksum
);
ushort
transportChecksum
=
Transport
.
Checksum
;
_isTransportChecksumCorrect
=
(
Transport
.
IsChecksumOptional
&&
transportChecksum
==
0
)
||
(
CalculateTransportChecksum
()
==
transportChecksum
);
}
return
_isTransportChecksumCorrect
.
Value
;
}
...
...
@@ -207,12 +208,12 @@ namespace PcapDotNet.Packets.IpV4
/// <summary>
/// The payload of the datagram as a TCP datagram.
/// </summary>
public
Datagram
Tcp
public
Tcp
Datagram
Tcp
{
get
{
if
(
_tcp
==
null
&&
Length
>=
HeaderLength
)
_tcp
=
new
Datagram
(
Buffer
,
StartOffset
+
HeaderLength
,
Length
-
HeaderLength
);
_tcp
=
new
Tcp
Datagram
(
Buffer
,
StartOffset
+
HeaderLength
,
Length
-
HeaderLength
);
return
_tcp
;
}
...
...
@@ -232,6 +233,24 @@ namespace PcapDotNet.Packets.IpV4
}
}
public
TransportDatagram
Transport
{
get
{
switch
(
Protocol
)
{
case
IpV4Protocol
.
Tcp
:
return
Tcp
;
case
IpV4Protocol
.
Udp
:
return
Udp
;
default
:
return
null
;
}
}
}
internal
IpV4Datagram
(
byte
[]
buffer
,
int
offset
,
int
length
)
:
base
(
buffer
,
offset
,
length
)
{
...
...
@@ -245,6 +264,7 @@ namespace PcapDotNet.Packets.IpV4
IpV4Options
options
,
int
payloadLength
)
{
int
headerLength
=
HeaderMinimumLength
+
options
.
BytesLength
;
buffer
[
offset
+
Offset
.
VersionAndHeaderLength
]
=
(
byte
)((
DefaultVersion
<<
4
)
+
headerLength
/
4
);
buffer
[
offset
+
Offset
.
TypeOfService
]
=
typeOfService
;
buffer
.
Write
(
offset
+
Offset
.
TotalLength
,
(
ushort
)(
headerLength
+
payloadLength
),
Endianity
.
Big
);
...
...
@@ -260,18 +280,18 @@ namespace PcapDotNet.Packets.IpV4
buffer
.
Write
(
offset
+
Offset
.
HeaderChecksum
,
Sum16BitsToChecksum
(
Sum16Bits
(
buffer
,
offset
,
headerLength
)),
Endianity
.
Big
);
}
internal
static
void
WriteTransportChecksum
(
byte
[]
buffer
,
int
offset
,
int
headerLength
,
ushort
transportLength
,
int
transportChecksumOffset
)
internal
static
void
WriteTransportChecksum
(
byte
[]
buffer
,
int
offset
,
int
headerLength
,
ushort
transportLength
,
int
transportChecksumOffset
,
bool
isChecksumOptional
)
{
ushort
checksum
=
CalculateTransportChecksum
(
buffer
,
offset
,
headerLength
,
transportLength
,
transportChecksumOffset
);
ushort
checksum
=
CalculateTransportChecksum
(
buffer
,
offset
,
headerLength
,
transportLength
,
transportChecksumOffset
,
isChecksumOptional
);
buffer
.
Write
(
offset
+
headerLength
+
transportChecksumOffset
,
checksum
,
Endianity
.
Big
);
}
private
ushort
CalculateTransportChecksum
()
{
return
CalculateTransportChecksum
(
Buffer
,
StartOffset
,
HeaderLength
,
(
ushort
)(
TotalLength
-
HeaderLength
),
UdpDatagram
.
ChecksumOffset
);
return
CalculateTransportChecksum
(
Buffer
,
StartOffset
,
HeaderLength
,
(
ushort
)(
TotalLength
-
HeaderLength
),
Transport
.
ChecksumOffset
,
Transport
.
IsChecksumOptional
);
}
private
static
ushort
CalculateTransportChecksum
(
byte
[]
buffer
,
int
offset
,
int
headerLength
,
ushort
transportLength
,
int
transportChecksumOffset
)
private
static
ushort
CalculateTransportChecksum
(
byte
[]
buffer
,
int
offset
,
int
headerLength
,
ushort
transportLength
,
int
transportChecksumOffset
,
bool
isChecksumOptional
)
{
uint
sum
=
Sum16Bits
(
buffer
,
offset
+
Offset
.
Source
,
2
*
IpV4Address
.
SizeOf
)
+
buffer
[
offset
+
Offset
.
Protocol
]
+
transportLength
+
...
...
@@ -279,7 +299,7 @@ namespace PcapDotNet.Packets.IpV4
Sum16Bits
(
buffer
,
offset
+
headerLength
+
transportChecksumOffset
+
2
,
transportLength
-
transportChecksumOffset
-
2
);
ushort
checksumResult
=
Sum16BitsToChecksum
(
sum
);
if
(
checksumResult
==
0
)
if
(
checksumResult
==
0
&&
isChecksumOptional
)
return
0xFFFF
;
return
checksumResult
;
}
...
...
@@ -300,8 +320,10 @@ namespace PcapDotNet.Packets.IpV4
{
// case IpV4Protocol.Tcp:
// return Tcp.IsValid; //&& IsTransportChecksumCorrect;
case
IpV4Protocol
.
Tcp
:
case
IpV4Protocol
.
Udp
:
return
Udp
.
IsValid
&&
(
Udp
.
Checksum
==
0
||
IsTransportChecksumCorrect
);
return
Transport
.
IsValid
&&
(
Transport
.
IsChecksumOptional
&&
Transport
.
Checksum
==
0
||
IsTransportChecksumCorrect
);
default
:
// Todo check more protocols
return
true
;
...
...
@@ -343,7 +365,7 @@ namespace PcapDotNet.Packets.IpV4
private
bool
?
_isHeaderChecksumCorrect
;
private
bool
?
_isTransportChecksumCorrect
;
private
IpV4Options
_options
;
private
Datagram
_tcp
;
private
Tcp
Datagram
_tcp
;
private
UdpDatagram
_udp
;
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Packets/PacketBuilder.cs
View file @
8f749c59
...
...
@@ -110,7 +110,64 @@ namespace PcapDotNet.Packets
udpPayload
.
Write
(
buffer
,
ethernetIpV4HeadersLength
+
UdpDatagram
.
HeaderLength
);
if
(
udpCalculateChecksum
)
IpV4Datagram
.
WriteTransportChecksum
(
buffer
,
EthernetDatagram
.
HeaderLength
,
ipV4HeaderLength
,
(
ushort
)
transportLength
,
UdpDatagram
.
ChecksumOffset
);
IpV4Datagram
.
WriteTransportChecksum
(
buffer
,
EthernetDatagram
.
HeaderLength
,
ipV4HeaderLength
,
(
ushort
)
transportLength
,
UdpDatagram
.
Offset
.
Checksum
,
true
);
return
new
Packet
(
buffer
,
timestamp
,
new
DataLink
(
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 udp port.</param>
/// <param name="tcpDestinationPort">The destination udp port.</param>
/// <param name="tcpPayload">The payload of UDP datagram.</param>
/// <returns>A packet with a UDP over IPv4 over Ethernet datagram.</returns>
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
,
TcpFlags
tcpFlags
,
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
,
ipV4SourceAddress
,
ipV4DestinationAddress
,
ipV4Options
,
transportLength
);
TcpDatagram
.
WriteHeader
(
buffer
,
ethernetIpV4HeadersLength
,
tcpSourcePort
,
tcpDestinationPort
,
tcpSequenceNumber
,
tcpAcknowledgmentNumber
,
tcpFlags
,
tcpWindow
,
tcpUrgentPointer
,
tcpOptions
);
tcpPayload
.
Write
(
buffer
,
ethernetIpV4HeadersLength
+
tcpHeaderLength
);
IpV4Datagram
.
WriteTransportChecksum
(
buffer
,
EthernetDatagram
.
HeaderLength
,
ipV4HeaderLength
,
(
ushort
)
transportLength
,
TcpDatagram
.
Offset
.
Checksum
,
false
);
return
new
Packet
(
buffer
,
timestamp
,
new
DataLink
(
DataLinkKind
.
Ethernet
));
}
...
...
PcapDotNet/src/PcapDotNet.Packets/PcapDotNet.Packets.csproj
View file @
8f749c59
...
...
@@ -103,6 +103,8 @@
<Compile
Include=
"Properties\AssemblyInfo.cs"
/>
<Compile
Include=
"Transport\TcpDatagram.cs"
/>
<Compile
Include=
"Transport\TcpFlags.cs"
/>
<Compile
Include=
"Transport\TcpOption.cs"
/>
<Compile
Include=
"Transport\TcpOptions.cs"
/>
<Compile
Include=
"Transport\TransportDatagram.cs"
/>
<Compile
Include=
"Transport\UdpDatagram.cs"
/>
</ItemGroup>
...
...
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpDatagram.cs
View file @
8f749c59
using
System
;
namespace
PcapDotNet.Packets.Transport
{
/// <summary>
...
...
@@ -20,12 +22,13 @@ namespace PcapDotNet.Packets.Transport
/// </summary>
public
class
TcpDatagram
:
TransportDatagram
{
private
static
class
Offset
public
const
int
HeaderMinimumLength
=
20
;
internal
static
class
Offset
{
public
const
int
SequenceNumber
=
4
;
public
const
int
AcknowledgmentNumber
=
8
;
public
const
int
HeaderLength
=
12
;
public
const
int
Flags
=
12
;
public
const
int
HeaderLengthAndFlags
=
12
;
public
const
int
Window
=
14
;
public
const
int
Checksum
=
16
;
public
const
int
UrgentPointer
=
18
;
...
...
@@ -63,12 +66,17 @@ namespace PcapDotNet.Packets.Transport
/// </summary>
public
int
HeaderLength
{
get
{
return
4
*
(
this
[
Offset
.
HeaderLength
]
>>
4
);
}
get
{
return
4
*
(
this
[
Offset
.
HeaderLengthAndFlags
]
>>
4
);
}
}
public
int
RealHeaderLength
{
get
{
return
Math
.
Min
(
HeaderLength
,
Length
);
}
}
public
TcpFlags
Flags
{
get
{
return
(
TcpFlags
)(
ReadUShort
(
Offset
.
Flags
,
Endianity
.
Big
)
&
0x01FF
);
}
get
{
return
(
TcpFlags
)(
ReadUShort
(
Offset
.
HeaderLengthAnd
Flags
,
Endianity
.
Big
)
&
0x01FF
);
}
}
public
ushort
Window
...
...
@@ -76,14 +84,101 @@ namespace PcapDotNet.Packets.Transport
get
{
return
ReadUShort
(
Offset
.
Window
,
Endianity
.
Big
);
}
}
public
ushort
Checksum
public
override
ushort
Checksum
{
get
{
return
ReadUShort
(
Offset
.
Checksum
,
Endianity
.
Big
);
}
}
public
override
int
ChecksumOffset
{
get
{
return
Offset
.
Checksum
;
}
}
public
override
bool
IsChecksumOptional
{
get
{
return
false
;
}
}
public
ushort
UrgentPointer
{
get
{
return
ReadUShort
(
Offset
.
UrgentPointer
,
Endianity
.
Big
);
}
}
public
TcpOptions
Options
{
get
{
if
(
_options
==
null
)
_options
=
new
TcpOptions
(
Buffer
,
StartOffset
+
Offset
.
Options
,
RealHeaderLength
-
HeaderMinimumLength
);
return
_options
;
}
}
/// <summary>
/// The payload of the TCP datagram.
/// </summary>
public
Datagram
Payload
{
get
{
return
new
Datagram
(
Buffer
,
StartOffset
+
HeaderLength
,
Length
-
HeaderLength
);
}
}
public
bool
IsCwr
{
get
{
return
(
Flags
&
TcpFlags
.
Cwr
)
==
TcpFlags
.
Cwr
;
}
}
public
bool
IsEce
{
get
{
return
(
Flags
&
TcpFlags
.
Ece
)
==
TcpFlags
.
Ece
;
}
}
public
bool
IsUrg
{
get
{
return
(
Flags
&
TcpFlags
.
Urg
)
==
TcpFlags
.
Urg
;
}
}
public
bool
IsAck
{
get
{
return
(
Flags
&
TcpFlags
.
Ack
)
==
TcpFlags
.
Ack
;
}
}
public
bool
IsPush
{
get
{
return
(
Flags
&
TcpFlags
.
Psh
)
==
TcpFlags
.
Psh
;
}
}
public
bool
IsReset
{
get
{
return
(
Flags
&
TcpFlags
.
Rst
)
==
TcpFlags
.
Rst
;
}
}
public
bool
IsSyn
{
get
{
return
(
Flags
&
TcpFlags
.
Syn
)
==
TcpFlags
.
Syn
;
}
}
public
bool
IsFin
{
get
{
return
(
Flags
&
TcpFlags
.
Fin
)
==
TcpFlags
.
Fin
;
}
}
internal
static
void
WriteHeader
(
byte
[]
buffer
,
int
offset
,
ushort
sourcePort
,
ushort
destinationPort
,
uint
sequenceNumber
,
uint
acknowledgmentNumber
,
TcpFlags
flags
,
ushort
window
,
ushort
urgentPointer
,
TcpOptions
options
)
{
int
headerLength
=
HeaderMinimumLength
+
options
.
BytesLength
;
WriteHeader
(
buffer
,
offset
,
sourcePort
,
destinationPort
);
buffer
.
Write
(
offset
+
Offset
.
SequenceNumber
,
sequenceNumber
,
Endianity
.
Big
);
buffer
.
Write
(
offset
+
Offset
.
AcknowledgmentNumber
,
acknowledgmentNumber
,
Endianity
.
Big
);
buffer
.
Write
(
offset
+
Offset
.
HeaderLengthAndFlags
,
(
ushort
)(((
ushort
)((
headerLength
/
4
)
<<
12
))
|
(
ushort
)
flags
),
Endianity
.
Big
);
buffer
.
Write
(
offset
+
Offset
.
Window
,
window
,
Endianity
.
Big
);
buffer
.
Write
(
offset
+
Offset
.
UrgentPointer
,
urgentPointer
,
Endianity
.
Big
);
options
.
Write
(
buffer
,
offset
+
Offset
.
Options
);
}
private
TcpOptions
_options
;
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpFlags.cs
0 → 100644
View file @
8f749c59
using
System
;
namespace
PcapDotNet.Packets.Transport
{
/// <summary>
/// TCP Header Format
/// +-----+-----+----+-----+-----+-----+-----+-----+-----+-----+-----+
/// | Bit | 0-6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
/// +-----+-----+----+-----+-----+-----+-----+-----+-----+-----+-----+
/// | 0 | | NS | CWR | ECE | URG | ACK | PSH | RST | SYN | FIN |
/// +-----+-----+----+-----+-----+-----+-----+-----+-----+-----+-----+
/// </summary>
[
Flags
]
public
enum
TcpFlags
:
ushort
{
None
=
0x0000
,
Fin
=
0x0001
,
Syn
=
0x0002
,
Rst
=
0x0004
,
Psh
=
0x0008
,
Ack
=
0x0010
,
Urg
=
0x0020
,
Ece
=
0x0040
,
Cwr
=
0x0080
,
Ns
=
0x0100
,
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpOption.cs
0 → 100644
View file @
8f749c59
namespace
PcapDotNet.Packets.Transport
{
public
class
TcpOption
{
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Packets/Transport/TcpOptions.cs
0 → 100644
View file @
8f749c59
using
System
;
namespace
PcapDotNet.Packets.Transport
{
public
class
TcpOptions
{
public
static
TcpOptions
None
{
get
{
return
_none
;
}
}
/// <summary>
/// Creates options from a list of options.
/// </summary>
/// <param name="options">The list of options.</param>
public
TcpOptions
(
params
TcpOption
[]
options
)
{
_bytesLength
=
0
;
}
internal
TcpOptions
(
byte
[]
buffer
,
int
offset
,
int
length
)
// : this(Read(buffer, offset, length))
{
_bytesLength
=
length
;
}
public
int
BytesLength
{
get
{
return
_bytesLength
;
}
}
internal
void
Write
(
byte
[]
buffer
,
int
offset
)
{
// throw new NotImplementedException();
}
private
readonly
int
_bytesLength
;
private
readonly
bool
_isValid
;
private
static
readonly
TcpOptions
_none
=
new
TcpOptions
();
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Packets/Transport/TransportDatagram.cs
View file @
8f749c59
using
System
;
namespace
PcapDotNet.Packets.Transport
{
/// <summary>
...
...
@@ -39,6 +41,12 @@ namespace PcapDotNet.Packets.Transport
get
{
return
ReadUShort
(
Offset
.
DestinationPort
,
Endianity
.
Big
);
}
}
public
abstract
ushort
Checksum
{
get
;
}
public
abstract
int
ChecksumOffset
{
get
;
}
public
abstract
bool
IsChecksumOptional
{
get
;
}
protected
static
void
WriteHeader
(
byte
[]
buffer
,
int
offset
,
ushort
sourcePort
,
ushort
destinationPort
)
{
buffer
.
Write
(
offset
+
Offset
.
SourcePort
,
sourcePort
,
Endianity
.
Big
);
...
...
PcapDotNet/src/PcapDotNet.Packets/Transport/UdpDatagram.cs
View file @
8f749c59
...
...
@@ -33,7 +33,7 @@ namespace PcapDotNet.Packets.Transport
/// </summary>
public
const
int
HeaderLength
=
8
;
private
static
class
Offset
internal
static
class
Offset
{
// public const int SourcePort = 0;
// public const int DestinationPort = 2;
...
...
@@ -41,8 +41,6 @@ namespace PcapDotNet.Packets.Transport
public
const
int
Checksum
=
6
;
}
internal
const
int
ChecksumOffset
=
Offset
.
Checksum
;
/// <summary>
/// The length in octets of this user datagram including this header and the data.
/// (This means the minimum value of the length is eight.)
...
...
@@ -56,11 +54,21 @@ namespace PcapDotNet.Packets.Transport
/// Checksum is the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header,
/// the UDP header, and the data, padded with zero octets at the end (if necessary) to make a multiple of two octets.
/// </summary>
public
ushort
Checksum
public
override
ushort
Checksum
{
get
{
return
ReadUShort
(
Offset
.
Checksum
,
Endianity
.
Big
);
}
}
public
override
int
ChecksumOffset
{
get
{
return
Offset
.
Checksum
;
}
}
public
override
bool
IsChecksumOptional
{
get
{
return
true
;
}
}
/// <summary>
/// The payload of the UDP datagram.
/// </summary>
...
...
PcapDotNet/src/PcapDotNet.TestUtils/MoreRandom.cs
View file @
8f749c59
...
...
@@ -85,7 +85,8 @@ namespace PcapDotNet.TestUtils
public
static
DateTime
NextDateTime
(
this
Random
random
,
DateTime
minimumValue
,
DateTime
maximumValue
)
{
return
new
DateTime
(
random
.
NextLong
(
minimumValue
.
Ticks
,
maximumValue
.
Ticks
+
1
));
return
new
DateTime
(
random
.
NextLong
(
minimumValue
.
ToUniversalTime
().
Ticks
,
maximumValue
.
ToUniversalTime
().
Ticks
+
1
),
DateTimeKind
.
Utc
).
ToLocalTime
();
}
public
static
DateTime
NextDateTime
(
this
Random
random
)
...
...
@@ -115,5 +116,29 @@ namespace PcapDotNet.TestUtils
{
return
values
[
random
.
Next
(
values
.
Count
)];
}
public
static
T
NextFlags
<
T
>(
this
Random
random
)
{
Type
type
=
typeof
(
T
);
if
(!
type
.
IsEnum
)
throw
new
ArgumentException
(
"T must be an Enum"
);
IEnumerable
<
T
>
enumValues
=
(
IEnumerable
<
T
>)
Enum
.
GetValues
(
type
);
Type
underlyingType
=
Enum
.
GetUnderlyingType
(
type
);
List
<
object
>
enumValuesAsUnderlyingType
=
new
List
<
object
>(
enumValues
.
Select
(
value
=>
Convert
.
ChangeType
(
value
,
underlyingType
)));
List
<
ulong
>
enumValuesAsULong
;
if
(
underlyingType
==
typeof
(
ushort
))
enumValuesAsULong
=
new
List
<
ulong
>(
enumValuesAsUnderlyingType
.
Cast
<
ushort
>().
Select
(
value
=>
(
ulong
)
value
));
else
throw
new
ArgumentException
(
"Type "
+
underlyingType
+
" is not supported"
);
ulong
resultValue
=
enumValuesAsULong
.
Aggregate
(
default
(
ulong
),
(
result
,
value
)
=>
random
.
NextBool
()
?
result
|
value
:
result
);
T
resultEnum
=
(
T
)
Convert
.
ChangeType
(
resultValue
,
underlyingType
);
return
resultEnum
;
}
}
}
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