Commit 4239204c authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent 6a6df7e5
...@@ -2,6 +2,11 @@ namespace Packets ...@@ -2,6 +2,11 @@ namespace Packets
{ {
public class Datagram public class Datagram
{ {
public Datagram(byte[] buffer)
: this(buffer, 0, buffer.Length)
{
}
public Datagram(byte[] buffer, int offset, int length) public Datagram(byte[] buffer, int offset, int length)
{ {
_buffer = buffer; _buffer = buffer;
......
...@@ -18,7 +18,7 @@ namespace Packets ...@@ -18,7 +18,7 @@ namespace Packets
public const int EtherTypeLength = 12; public const int EtherTypeLength = 12;
} }
internal const int HeaderLength = 14; public const int HeaderLength = 14;
public EthernetDatagram(byte[] buffer, int offset, int length) public EthernetDatagram(byte[] buffer, int offset, int length)
: base(buffer, offset, length) : base(buffer, offset, length)
......
...@@ -71,11 +71,20 @@ namespace PcapDotNet.Core.Test ...@@ -71,11 +71,20 @@ namespace PcapDotNet.Core.Test
{ {
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
Packet packet;
DateTime startWaiting = DateTime.Now;
PacketCommunicatorReceiveResult result = communicator.GetPacket(out packet);
DateTime finishedWaiting = DateTime.Now;
Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result);
Assert.AreEqual<uint>(0, communicator.TotalStatistics.PacketsCaptured);
MoreAssert.IsInRange(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1.02), finishedWaiting - startWaiting);
Packet sentPacket = PacketBuilder.Ethernet(DateTime.Now, Packet sentPacket = PacketBuilder.Ethernet(DateTime.Now,
new MacAddress(SourceMac), new MacAddress(SourceMac),
new MacAddress(DestinationMac), new MacAddress(DestinationMac),
EthernetType.IpV4, EthernetType.IpV4,
new Datagram(new byte[10], 0, 10)); GetRandomDatagram(10));
DateTime startSendingTime = DateTime.Now; DateTime startSendingTime = DateTime.Now;
...@@ -84,8 +93,7 @@ namespace PcapDotNet.Core.Test ...@@ -84,8 +93,7 @@ namespace PcapDotNet.Core.Test
DateTime endSendingTime = DateTime.Now; DateTime endSendingTime = DateTime.Now;
Packet packet; result = communicator.GetPacket(out packet);
PacketCommunicatorReceiveResult result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result); Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual<uint>(NumPacketsToSend, communicator.TotalStatistics.PacketsCaptured); Assert.AreEqual<uint>(NumPacketsToSend, communicator.TotalStatistics.PacketsCaptured);
...@@ -94,6 +102,56 @@ namespace PcapDotNet.Core.Test ...@@ -94,6 +102,56 @@ namespace PcapDotNet.Core.Test
} }
} }
[TestMethod]
public void GetSomePacketsTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
const int NumPacketsToSend = 100;
const int PacketSize = 100;
TestGetSomePackets(SourceMac, DestinationMac, 0, PacketSize, false, PacketCommunicatorReceiveResult.Ok, 0, 1, 1.02);
TestGetSomePackets(SourceMac, DestinationMac, NumPacketsToSend, PacketSize, false, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
TestGetSomePackets(SourceMac, DestinationMac, 0, PacketSize, true, PacketCommunicatorReceiveResult.Ok, 0, 0, 0.02);
TestGetSomePackets(SourceMac, DestinationMac, NumPacketsToSend, PacketSize, true, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
}
private void TestGetSomePackets(string sourceMac, string destinationMac, int numPacketsToSend, int packetSize, bool nonBlocking,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds)
{
Packet packetToSend = PacketBuilder.Ethernet(DateTime.Now,
new MacAddress(sourceMac),
new MacAddress(destinationMac),
EthernetType.IpV4,
GetRandomDatagram(packetSize - EthernetDatagram.HeaderLength));
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.NonBlocking = nonBlocking;
communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
int numPacketsGot;
for (int i = 0; i != numPacketsToSend; ++i)
communicator.SendPacket(packetToSend);
int numPacketsHandled = 0;
DateTime startWaiting = DateTime.Now;
PacketCommunicatorReceiveResult result = communicator.GetSomePackets(out numPacketsGot, numPacketsToSend,
delegate(Packet packet)
{
Assert.AreEqual(packetToSend, packet);
++numPacketsHandled;
});
DateTime finishedWaiting = DateTime.Now;
Assert.AreEqual(expectedResult, result);
Assert.AreEqual(expectedNumPackets, numPacketsGot);
Assert.AreEqual(expectedNumPackets, numPacketsHandled);
MoreAssert.IsInRange(expectedMinSeconds, expectedMaxSeconds, (finishedWaiting - startWaiting).TotalSeconds);
}
}
[TestMethod] [TestMethod]
public void DumpPacketsTest() public void DumpPacketsTest()
{ {
...@@ -105,7 +163,7 @@ namespace PcapDotNet.Core.Test ...@@ -105,7 +163,7 @@ namespace PcapDotNet.Core.Test
new MacAddress(SourceMac), new MacAddress(SourceMac),
new MacAddress(DestinationMac), new MacAddress(DestinationMac),
EthernetType.IpV4, EthernetType.IpV4,
new Datagram(new byte[10], 0, 10)); GetRandomDatagram(10));
using (PacketCommunicator communicator = OpenLiveDevice()) using (PacketCommunicator communicator = OpenLiveDevice())
{ {
...@@ -128,7 +186,7 @@ namespace PcapDotNet.Core.Test ...@@ -128,7 +186,7 @@ namespace PcapDotNet.Core.Test
} }
[TestMethod] [TestMethod]
public void ReceiveManyPacketsTest() public void GetPacketsTest()
{ {
const string SourceMac = "11:22:33:44:55:66"; const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC"; const string DestinationMac = "77:88:99:AA:BB:CC";
...@@ -142,7 +200,7 @@ namespace PcapDotNet.Core.Test ...@@ -142,7 +200,7 @@ namespace PcapDotNet.Core.Test
new MacAddress(SourceMac), new MacAddress(SourceMac),
new MacAddress(DestinationMac), new MacAddress(DestinationMac),
EthernetType.IpV4, EthernetType.IpV4,
new Datagram(new byte[10], 0, 10)); GetRandomDatagram(10));
PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None; PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None;
int numPacketsGot = 0; int numPacketsGot = 0;
...@@ -168,6 +226,99 @@ namespace PcapDotNet.Core.Test ...@@ -168,6 +226,99 @@ namespace PcapDotNet.Core.Test
} }
} }
[TestMethod]
public void GetNextStatisticsTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
const int NumPacketsToSend = 100;
const int PacketSize = 100;
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.Mode = PacketCommunicatorMode.Statistics;
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
Packet sentPacket = PacketBuilder.Ethernet(DateTime.Now,
new MacAddress(SourceMac),
new MacAddress(DestinationMac),
EthernetType.IpV4,
GetRandomDatagram(PacketSize - EthernetDatagram.HeaderLength));
PacketSampleStatistics statistics;
PacketCommunicatorReceiveResult result = communicator.GetNextStatistics(out statistics);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
MoreAssert.IsInRange(DateTime.Now.AddSeconds(-1), DateTime.Now.AddSeconds(1), statistics.Timestamp);
Assert.AreEqual<uint>(0, statistics.AcceptedPackets);
Assert.AreEqual<uint>(0, statistics.AcceptedBytes);
for (int i = 0; i != NumPacketsToSend; ++i)
communicator.SendPacket(sentPacket);
result = communicator.GetNextStatistics(out statistics);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
MoreAssert.IsInRange(DateTime.Now.AddSeconds(-1), DateTime.Now.AddSeconds(1), statistics.Timestamp);
Assert.AreEqual<uint>(NumPacketsToSend, statistics.AcceptedPackets, "AcceptedPackets");
// Todo check byte statistics
// Assert.AreEqual<uint>((uint)(sentPacket.Length * NumPacketsToSend), statistics.AcceptedBytes,
// "AcceptedBytes. Diff Per Packet: " +
// (statistics.AcceptedBytes - sentPacket.Length * NumPacketsToSend) /
// ((double)NumPacketsToSend));
}
}
[TestMethod]
public void GetStatisticsTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
const int NumPacketsToSend = 100;
const int NumStatisticsToGather = 3;
const int PacketSize = 100;
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.Mode = PacketCommunicatorMode.Statistics;
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
Packet sentPacket = PacketBuilder.Ethernet(DateTime.Now,
new MacAddress(SourceMac),
new MacAddress(DestinationMac),
EthernetType.IpV4,
GetRandomDatagram(PacketSize - EthernetDatagram.HeaderLength));
PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None;
int numStatisticsGot = 0;
ulong totalPackets = 0;
ulong totalBytes = 0;
Thread thread = new Thread(delegate()
{
result = communicator.GetStatistics(NumStatisticsToGather,
delegate(PacketSampleStatistics statistics)
{
totalPackets += statistics.AcceptedPackets;
totalBytes += statistics.AcceptedBytes;
++numStatisticsGot;
});
});
thread.Start();
for (int i = 0; i != NumPacketsToSend; ++i)
communicator.SendPacket(sentPacket);
if (!thread.Join(TimeSpan.FromSeconds(5)))
thread.Abort();
Assert.AreEqual(NumStatisticsToGather, numStatisticsGot, "NumStatistics");
Assert.AreEqual<ulong>(NumPacketsToSend, totalPackets, "NumPackets");
// Todo check bytes statistics
// Assert.AreEqual<ulong>((ulong)(NumPacketsToSend * sentPacket.Length), totalBytes, "NumBytes");
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result, "Result");
}
}
private static PacketCommunicator OpenLiveDevice() private static PacketCommunicator OpenLiveDevice()
{ {
IList<LivePacketDevice> devices = LivePacketDevice.AllLocalMachine; IList<LivePacketDevice> devices = LivePacketDevice.AllLocalMachine;
...@@ -197,5 +348,14 @@ namespace PcapDotNet.Core.Test ...@@ -197,5 +348,14 @@ namespace PcapDotNet.Core.Test
throw; throw;
} }
} }
private Datagram GetRandomDatagram(int length)
{
byte[] buffer = new byte[length];
_random.NextBytes(buffer);
return new Datagram(buffer);
}
private Random _random = new Random();
} }
} }
\ No newline at end of file
...@@ -154,7 +154,7 @@ PacketCommunicatorReceiveResult PacketCommunicator::GetPacket([Out] Packet^% pac ...@@ -154,7 +154,7 @@ PacketCommunicatorReceiveResult PacketCommunicator::GetPacket([Out] Packet^% pac
return result; return result;
} }
PacketCommunicatorReceiveResult PacketCommunicator::GetSomePackets(int maxPackets, HandlePacket^ callBack, [Out] int% numPacketsGot) PacketCommunicatorReceiveResult PacketCommunicator::GetSomePackets([Out] int% numPacketsGot, int maxPackets, HandlePacket^ callBack)
{ {
AssertMode(PacketCommunicatorMode::Capture); AssertMode(PacketCommunicatorMode::Capture);
...@@ -209,6 +209,22 @@ PacketCommunicatorReceiveResult PacketCommunicator::GetNextStatistics([Out] Pack ...@@ -209,6 +209,22 @@ PacketCommunicatorReceiveResult PacketCommunicator::GetNextStatistics([Out] Pack
return result; return result;
} }
PacketCommunicatorReceiveResult PacketCommunicator::GetStatistics(int numStatistics, HandleStatistics^ callBack)
{
AssertMode(PacketCommunicatorMode::Statistics);
StatisticsHandler^ statisticsHandler = gcnew StatisticsHandler(callBack);
HandlerDelegate^ statisticsHandlerDelegate = gcnew HandlerDelegate(statisticsHandler, &StatisticsHandler::Handle);
pcap_handler functionPointer = (pcap_handler)Marshal::GetFunctionPointerForDelegate(statisticsHandlerDelegate).ToPointer();
int result = pcap_loop(_pcapDescriptor, numStatistics, functionPointer, NULL);
if (result == -1)
throw BuildInvalidOperation("Failed reading from device");
if (result == -2)
return PacketCommunicatorReceiveResult::BreakLoop;
return PacketCommunicatorReceiveResult::Ok;
}
void PacketCommunicator::SendPacket(Packet^ packet) void PacketCommunicator::SendPacket(Packet^ packet)
{ {
pin_ptr<Byte> unamangedPacketBytes; pin_ptr<Byte> unamangedPacketBytes;
......
...@@ -84,11 +84,12 @@ namespace PcapDotNet { namespace Core ...@@ -84,11 +84,12 @@ namespace PcapDotNet { namespace Core
delegate void HandlePacket(Packets::Packet^ packet); delegate void HandlePacket(Packets::Packet^ packet);
PacketCommunicatorReceiveResult GetPacket([System::Runtime::InteropServices::Out] Packets::Packet^% packet); PacketCommunicatorReceiveResult GetPacket([System::Runtime::InteropServices::Out] Packets::Packet^% packet);
PacketCommunicatorReceiveResult GetSomePackets(int maxPackets, HandlePacket^ callBack, [System::Runtime::InteropServices::Out] int% numPacketsGot); PacketCommunicatorReceiveResult GetSomePackets([System::Runtime::InteropServices::Out] int% numPacketsGot, int maxPackets, HandlePacket^ callBack);
PacketCommunicatorReceiveResult GetPackets(int numPackets, HandlePacket^ callBack); PacketCommunicatorReceiveResult GetPackets(int numPackets, HandlePacket^ callBack);
delegate void HandleStatistics(PacketSampleStatistics^ statistics); delegate void HandleStatistics(PacketSampleStatistics^ statistics);
PacketCommunicatorReceiveResult GetNextStatistics([System::Runtime::InteropServices::Out] PacketSampleStatistics^% statistics); PacketCommunicatorReceiveResult GetNextStatistics([System::Runtime::InteropServices::Out] PacketSampleStatistics^% statistics);
PacketCommunicatorReceiveResult GetStatistics(int numStatistics, HandleStatistics^ callBack);
void SendPacket(Packets::Packet^ packet); void SendPacket(Packets::Packet^ packet);
void Transmit(PacketSendQueue^ sendQueue, bool isSync); void Transmit(PacketSendQueue^ sendQueue, bool isSync);
......
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