Commit 4b703916 authored by Brickner_cp's avatar Brickner_cp

TCP

parent 647277c1
......@@ -270,21 +270,21 @@ namespace PcapDotNet.Core.Test
// Normal
TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, NumStatisticsToGather, int.MaxValue, 5, PacketSize,
PacketCommunicatorReceiveResult.Ok, NumStatisticsToGather, NumPacketsToSend, NumStatisticsToGather, NumStatisticsToGather + 0.02);
PacketCommunicatorReceiveResult.Ok, NumStatisticsToGather, NumPacketsToSend, NumStatisticsToGather, NumStatisticsToGather + 0.04);
// Wait for less statistics
TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, NumStatisticsToGather / 2, int.MaxValue, 5, PacketSize,
PacketCommunicatorReceiveResult.Ok, NumStatisticsToGather / 2, NumPacketsToSend, NumStatisticsToGather / 2, NumStatisticsToGather / 2 + 0.02);
PacketCommunicatorReceiveResult.Ok, NumStatisticsToGather / 2, NumPacketsToSend, NumStatisticsToGather / 2, NumStatisticsToGather / 2 + 0.04);
// Wait for more statistics
TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, 0, int.MaxValue, 5.5, PacketSize,
PacketCommunicatorReceiveResult.None, 5, NumPacketsToSend, 5.5, 5.52);
PacketCommunicatorReceiveResult.None, 5, NumPacketsToSend, 5.5, 5.54);
// Break loop
TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, NumStatisticsToGather, 0, 5, PacketSize,
PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0, 0.02);
PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0, 0.04);
TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, NumStatisticsToGather, NumStatisticsToGather / 2, 5, PacketSize,
PacketCommunicatorReceiveResult.BreakLoop, NumStatisticsToGather / 2, NumPacketsToSend, NumStatisticsToGather / 2, NumStatisticsToGather / 2 + 0.02);
PacketCommunicatorReceiveResult.BreakLoop, NumStatisticsToGather / 2, NumPacketsToSend, NumStatisticsToGather / 2, NumStatisticsToGather / 2 + 0.04);
}
[TestMethod]
......
......@@ -12,7 +12,7 @@ using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.TestUtils;
using PcapDotNet.Packets.Udp;
using PcapDotNet.Packets.Transport;
using PcapDotNet.TestUtils;
namespace PcapDotNet.Core.Test
......
......@@ -3,7 +3,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.TestUtils;
using PcapDotNet.Packets.Udp;
using PcapDotNet.Packets.Transport;
using PcapDotNet.TestUtils;
namespace PcapDotNet.Packets.Test
......@@ -103,6 +103,7 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(UdpDatagram.HeaderLength + udpPayload.Length, packet.Ethernet.IpV4.Udp.TotalLength, "Total Length");
Assert.IsTrue(!udpCalculateChecksum && packet.Ethernet.IpV4.Udp.Checksum == 0 ||
packet.Ethernet.IpV4.IsTransportChecksumCorrect, "IsTransportChecksumCorrect");
Assert.AreEqual(udpPayload, packet.Ethernet.IpV4.Udp.Payload, "Payload");
}
}
......
......@@ -2,7 +2,7 @@
using System.Collections;
using System.Linq;
using System.Text;
using PcapDotNet.Packets.Udp;
using PcapDotNet.Packets.Transport;
namespace PcapDotNet.Packets.IpV4
{
......
using System;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Udp;
using PcapDotNet.Packets.Transport;
namespace PcapDotNet.Packets
{
......
......@@ -101,7 +101,10 @@
<Compile Include="Packet.cs" />
<Compile Include="PacketBuilder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Udp\UdpDatagram.cs" />
<Compile Include="Transport\TcpDatagram.cs" />
<Compile Include="Transport\TcpFlags.cs" />
<Compile Include="Transport\TransportDatagram.cs" />
<Compile Include="Transport\UdpDatagram.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\PcapDotNet.snk" />
......
namespace PcapDotNet.Packets.Transport
{
/// <summary>
/// TCP Header Format
/// +-----+-------------+----------+----+-----+-----+-----+-----+-----+-----+-----+-----+------------------+
/// | Bit | 0-3 | 4-6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16-31 |
/// +-----+-------------+----------+----+-----+-----+-----+-----+-----+-----+-----+-----+------------------+
/// | 0 | Source Port | Destination Port |
/// +-----+-----------------------------------------------------------------------------+------------------+
/// | 32 | Sequence Number |
/// +-----+------------------------------------------------------------------------------------------------+
/// | 64 | Acknowledgment Number |
/// +-----+-------------+----------+----+-----+-----+-----+-----+-----+-----+-----+-----+------------------+
/// | 96 | Data Offset | Reserved | NS | CWR | ECE | URG | ACK | PSH | RST | SYN | FIN | Window |
/// +-----+-------------+----------+----+-----+-----+-----+-----+-----+-----+-----+-----+------------------+
/// | 128 | Checksum | Urgent Pointer |
/// +-----+-----------------------------------------------------------------------------+------------------+
/// | 160 | Options + Padding |
/// +-----+------------------------------------------------------------------------------------------------+
/// </summary>
public class TcpDatagram : TransportDatagram
{
private 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 Window = 14;
public const int Checksum = 16;
public const int UrgentPointer = 18;
public const int Options = 20;
}
public TcpDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length)
{
}
/// <summary>
/// The sequence number of the first data octet in this segment (except when SYN is present).
/// If SYN is present the sequence number is the initial sequence number (ISN) and the first data octet is ISN+1.
/// </summary>
public uint SequenceNumber
{
get { return ReadUInt(Offset.SequenceNumber, Endianity.Big); }
}
/// <summary>
/// If the ACK control bit is set this field contains the value of the next sequence number
/// the sender of the segment is expecting to receive.
/// Once a connection is established this is always sent.
/// </summary>
public uint AcknowledgmentNumber
{
get { return ReadUInt(Offset.AcknowledgmentNumber, Endianity.Big); }
}
/// <summary>
/// The number of bytes in the TCP Header.
/// This indicates where the data begins.
/// The TCP header (even one including options) is an integral number of 32 bits (4 bytes) long.
/// </summary>
public int HeaderLength
{
get { return 4 * (this[Offset.HeaderLength] >> 4); }
}
public TcpFlags Flags
{
get { return (TcpFlags)(ReadUShort(Offset.Flags, Endianity.Big) & 0x01FF); }
}
public ushort Window
{
get { return ReadUShort(Offset.Window, Endianity.Big); }
}
public ushort Checksum
{
get { return ReadUShort(Offset.Checksum, Endianity.Big); }
}
public ushort UrgentPointer
{
get { return ReadUShort(Offset.UrgentPointer, Endianity.Big); }
}
}
}
\ No newline at end of file
namespace PcapDotNet.Packets.Transport
{
/// <summary>
/// Contains the common part of UDP and TCP.
///
/// Format:
/// +-----+-------------+----------+-----+-----+-----+-----+-----+-----+------------------+
/// | Bit | 0-4 | 4-9 | 10 | 11 | 12 | 13 | 14 | 15 | 16-31 |
/// +-----+-------------+----------+-----+-----+-----+-----+-----+-----+------------------+
/// | 0 | Source Port | Destination Port |
/// +-----+------------------------------------------------------------+------------------+
/// | 32 | Sequence Number |
/// +-----+-------------------------------------------------------------------------------+
/// </summary>
public abstract class TransportDatagram : Datagram
{
private static class Offset
{
public const int SourcePort = 0;
public const int DestinationPort = 2;
}
/// <summary>
/// Indicates the port of the sending process.
/// In UDP, this field is optional and may only be assumed to be the port
/// to which a reply should be addressed in the absence of any other information.
/// If not used in UDP, a value of zero is inserted.
/// </summary>
public ushort SourcePort
{
get { return ReadUShort(Offset.SourcePort, Endianity.Big); }
}
/// <summary>
/// Destination Port has a meaning within the context of a particular internet destination address.
/// </summary>
public ushort DestinationPort
{
get { return ReadUShort(Offset.DestinationPort, Endianity.Big); }
}
protected static void WriteHeader(byte[] buffer, int offset, ushort sourcePort, ushort destinationPort)
{
buffer.Write(offset + Offset.SourcePort, sourcePort, Endianity.Big);
buffer.Write(offset + Offset.DestinationPort, destinationPort, Endianity.Big);
}
protected TransportDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length)
{
}
}
}
\ No newline at end of file
using System;
namespace PcapDotNet.Packets.Udp
namespace PcapDotNet.Packets.Transport
{
/// <summary>
/// This User Datagram Protocol (UDP) is defined to make available a datagram mode of packet-switched computer communication
......@@ -26,7 +26,7 @@ namespace PcapDotNet.Packets.Udp
/// +---------------- ...
/// </summary>
public class UdpDatagram : Datagram
public class UdpDatagram : TransportDatagram
{
/// <summary>
/// The number of bytes the datagram header takes.
......@@ -35,32 +35,14 @@ namespace PcapDotNet.Packets.Udp
private static class Offset
{
public const int SourcePort = 0;
public const int DestinationPort = 2;
// public const int SourcePort = 0;
// public const int DestinationPort = 2;
public const int TotalLength = 4;
public const int Checksum = 6;
}
internal const int ChecksumOffset = Offset.Checksum;
/// <summary>
/// Source Port is an optional field, when meaningful, it indicates the port of the sending process,
/// and may be assumed to be the port to which a reply should be addressed in the absence of any other information.
/// If not used, a value of zero is inserted.
/// </summary>
public ushort SourcePort
{
get { return ReadUShort(Offset.SourcePort, Endianity.Big); }
}
/// <summary>
/// Destination Port has a meaning within the context of a particular internet destination address.
/// </summary>
public ushort DestinationPort
{
get { return ReadUShort(Offset.DestinationPort, Endianity.Big); }
}
/// <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.)
......@@ -94,8 +76,7 @@ namespace PcapDotNet.Packets.Udp
internal static void WriteHeader(byte[] buffer, int offset, ushort sourcePort, ushort destinationPort, int payloadLength)
{
buffer.Write(offset + Offset.SourcePort, sourcePort, Endianity.Big);
buffer.Write(offset + Offset.DestinationPort, destinationPort, Endianity.Big);
WriteHeader(buffer, offset, sourcePort, destinationPort);
buffer.Write(offset + Offset.TotalLength, (ushort)(HeaderLength + payloadLength), Endianity.Big);
}
......
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