Commit f66abd8c authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent 9443a5b3
......@@ -101,50 +101,46 @@ 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;
// Test normal mode
TestGetSomePackets(SourceMac, DestinationMac, 0, int.MaxValue, PacketSize, false, PacketCommunicatorReceiveResult.Ok, 0, 1, 1.02);
TestGetSomePackets(SourceMac, DestinationMac, NumPacketsToSend, int.MaxValue, PacketSize, false, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
TestGetSomePackets(0, 0, int.MaxValue, PacketSize, false, PacketCommunicatorReceiveResult.Ok, 0, 1, 1.02);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend, int.MaxValue, PacketSize, false, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
TestGetSomePackets(NumPacketsToSend, 0, int.MaxValue, PacketSize, false, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
TestGetSomePackets(NumPacketsToSend, -1, int.MaxValue, PacketSize, false, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, PacketSize, false, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
// Test non blocking
TestGetSomePackets(SourceMac, DestinationMac, 0, int.MaxValue, PacketSize, true, PacketCommunicatorReceiveResult.Ok, 0, 0, 0.02);
TestGetSomePackets(SourceMac, DestinationMac, NumPacketsToSend, int.MaxValue, PacketSize, true, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
TestGetSomePackets(0, 0, int.MaxValue, PacketSize, true, PacketCommunicatorReceiveResult.Ok, 0, 0, 0.02);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend, int.MaxValue, PacketSize, true, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
// Test break loop
TestGetSomePackets(SourceMac, DestinationMac, NumPacketsToSend, NumPacketsToSend / 2, PacketSize, false, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02);
TestGetSomePackets(SourceMac, DestinationMac, NumPacketsToSend, NumPacketsToSend / 2, PacketSize, true, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02);
TestGetSomePackets(SourceMac, DestinationMac, NumPacketsToSend, 0, PacketSize, false, PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0.02);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend, NumPacketsToSend / 2, PacketSize, false, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend, NumPacketsToSend / 2, PacketSize, true, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend, 0, PacketSize, false, PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0.02);
}
[TestMethod]
public void GetPacketsTest()
{
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;
// Normal
TestGetPackets(SourceMac, DestinationMac, NumPacketsToSend, NumPacketsToSend, int.MaxValue, 2, PacketSize,
PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
TestGetPackets(NumPacketsToSend, NumPacketsToSend, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0, 0.02);
// Wait for less packets
TestGetPackets(SourceMac, DestinationMac, NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, 2, PacketSize,
PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02);
TestGetPackets(NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0, 0.02);
// Wait for more packets
TestGetPackets(SourceMac, DestinationMac, NumPacketsToSend, 0, int.MaxValue, 2, PacketSize,
PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.02);
TestGetPackets(NumPacketsToSend, 0, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.02);
TestGetPackets(NumPacketsToSend, -1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.02);
TestGetPackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, 2, PacketSize, PacketCommunicatorReceiveResult.None, NumPacketsToSend, 2, 2.02);
// Break loop
TestGetPackets(SourceMac, DestinationMac, NumPacketsToSend, NumPacketsToSend, 0, 2, PacketSize,
PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0.02);
TestGetPackets(SourceMac, DestinationMac, NumPacketsToSend, NumPacketsToSend, NumPacketsToSend / 2, 2, PacketSize,
PacketCommunicatorReceiveResult.BreakLoop, NumPacketsToSend / 2, 0, 0.02);
TestGetPackets(NumPacketsToSend, NumPacketsToSend, 0, 2, PacketSize, PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0.02);
TestGetPackets(NumPacketsToSend, NumPacketsToSend, NumPacketsToSend / 2, 2, PacketSize, PacketCommunicatorReceiveResult.BreakLoop, NumPacketsToSend / 2, 0, 0.02);
}
[TestMethod]
......@@ -294,16 +290,19 @@ namespace PcapDotNet.Core.Test
}
}
private static void TestGetSomePackets(string sourceMac, string destinationMac, int numPacketsToSend, int numPacketsToBreakLoop, int packetSize, bool nonBlocking,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds)
private static void TestGetSomePackets(int numPacketsToSend, int numPacketsToGet, int numPacketsToBreakLoop, int packetSize, bool nonBlocking,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds)
{
Packet packetToSend = MoreRandom.BuildRandomPacket(sourceMac, destinationMac, packetSize);
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
Packet packetToSend = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, packetSize);
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.NonBlocking = nonBlocking;
Assert.AreEqual(nonBlocking, communicator.NonBlocking);
communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
int numPacketsGot;
for (int i = 0; i != numPacketsToSend; ++i)
......@@ -313,7 +312,7 @@ namespace PcapDotNet.Core.Test
if (numPacketsToBreakLoop == 0)
communicator.Break();
DateTime startWaiting = DateTime.Now;
PacketCommunicatorReceiveResult result = communicator.GetSomePackets(out numPacketsGot, numPacketsToSend,
PacketCommunicatorReceiveResult result = communicator.GetSomePackets(out numPacketsGot, numPacketsToGet,
delegate(Packet packet)
{
Assert.AreEqual(packetToSend, packet);
......@@ -330,16 +329,23 @@ namespace PcapDotNet.Core.Test
}
}
private void TestGetPackets(string sourceMac, string destinationMac, int numPacketsToSend,
int numPacketsToWait, int numPacketsToBreakLoop, double secondsToWait, int packetSize,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets,
double expectedMinSeconds, double expectedMaxSeconds)
private void TestGetPackets(int numPacketsToSend, int numPacketsToWait, int numPacketsToBreakLoop, double secondsToWait, int packetSize,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets,
double expectedMinSeconds, double expectedMaxSeconds)
{
string TestDescription = "NumPacketsToSend=" + numPacketsToSend + ". NumPacketsToWait=" + numPacketsToWait +
". NumPacketsToBreakLoop=" + numPacketsToBreakLoop + ". SecondsToWait=" +
secondsToWait + ". PacketSize=" + packetSize;
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
using (PacketCommunicator communicator = OpenLiveDevice())
{
communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
Packet sentPacket = MoreRandom.BuildRandomPacket(sourceMac, destinationMac, packetSize);
Packet sentPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, packetSize);
PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None;
int numPacketsHandled = 0;
......@@ -368,7 +374,7 @@ namespace PcapDotNet.Core.Test
thread.Abort();
DateTime finishedWaiting = DateTime.Now;
Assert.AreEqual(expectedResult, result);
Assert.AreEqual(expectedResult, result, TestDescription);
Assert.AreEqual(expectedNumPackets, numPacketsHandled);
MoreAssert.IsInRange(expectedMinSeconds, expectedMaxSeconds, (finishedWaiting - startWaiting).TotalSeconds);
}
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Packets;
......@@ -60,13 +61,13 @@ namespace PcapDotNet.Core.Test
#endregion
[TestMethod]
public void DumpPacketsTest()
public void GetPacketTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
const int NumPackets = 10;
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 24);
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 100);
using (PacketCommunicator communicator = OpenOfflineDevice(NumPackets, expectedPacket))
{
......@@ -90,13 +91,47 @@ namespace PcapDotNet.Core.Test
}
[TestMethod]
public void SetNonBlockTest()
public void GetSomePacketsTest()
{
const int NumPackets = 10;
const int NumPacketsToSend = 100;
// Normal
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend, int.MaxValue, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0.05, 0.05);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0.05, 0.05);
// Eof
TestGetSomePackets(NumPacketsToSend, 0, int.MaxValue, PacketCommunicatorReceiveResult.Eof, NumPacketsToSend, 0.05, 0.05);
TestGetSomePackets(NumPacketsToSend, -1, int.MaxValue, PacketCommunicatorReceiveResult.Eof, NumPacketsToSend, 0.05, 0.05);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, PacketCommunicatorReceiveResult.Eof, NumPacketsToSend, 0.05, 0.05);
Packet packet = MoreRandom.BuildRandomPacket(24);
// Break loop
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend, NumPacketsToSend / 2, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0.05, 0.05);
TestGetSomePackets(NumPacketsToSend, NumPacketsToSend, 0, PacketCommunicatorReceiveResult.BreakLoop, 0, 0.05, 0.05);
}
[TestMethod]
public void GetPacketsTest()
{
const int NumPacketsToSend = 100;
using (PacketCommunicator communicator = OpenOfflineDevice(NumPackets, packet))
// Normal
TestGetPackets(NumPacketsToSend, NumPacketsToSend, int.MaxValue, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend, 0.05, 0.05);
TestGetPackets(NumPacketsToSend, NumPacketsToSend / 2, int.MaxValue, PacketCommunicatorReceiveResult.Ok, NumPacketsToSend / 2, 0.05, 0.05);
// Eof
TestGetPackets(NumPacketsToSend, 0, int.MaxValue, PacketCommunicatorReceiveResult.Eof, NumPacketsToSend, 0.05, 0.05);
TestGetPackets(NumPacketsToSend, -1, int.MaxValue, PacketCommunicatorReceiveResult.Eof, NumPacketsToSend, 0.05, 0.05);
TestGetPackets(NumPacketsToSend, NumPacketsToSend + 1, int.MaxValue, PacketCommunicatorReceiveResult.Eof, NumPacketsToSend, 0.05, 0.05);
// Break loop
TestGetPackets(NumPacketsToSend, NumPacketsToSend, NumPacketsToSend / 2, PacketCommunicatorReceiveResult.BreakLoop, NumPacketsToSend / 2, 0.05, 0.05);
TestGetPackets(NumPacketsToSend, NumPacketsToSend, 0, PacketCommunicatorReceiveResult.BreakLoop, 0, 0.05, 0.05);
}
[TestMethod]
public void SetNonBlockTest()
{
using (PacketCommunicator communicator = OpenOfflineDevice())
{
Assert.AreEqual(false, communicator.NonBlocking);
communicator.NonBlocking = false;
......@@ -108,13 +143,9 @@ namespace PcapDotNet.Core.Test
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void GetTotalStatisticsTest()
public void GetTotalStatisticsErrorTest()
{
const int NumPackets = 10;
Packet packet = MoreRandom.BuildRandomPacket(24);
using (PacketCommunicator communicator = OpenOfflineDevice(NumPackets, packet))
using (PacketCommunicator communicator = OpenOfflineDevice())
{
Assert.IsNotNull(communicator.TotalStatistics);
}
......@@ -129,7 +160,87 @@ namespace PcapDotNet.Core.Test
}
}
public static PacketCommunicator OpenOfflineDevice(int numPackets, Packet packet)
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void SendPacketTest()
{
using (PacketCommunicator communicator = OpenOfflineDevice())
{
communicator.SendPacket(MoreRandom.BuildRandomPacket(100));
}
}
private static void TestGetSomePackets(int numPacketsToSend, int numPacketsToGet, int numPacketsToBreakLoop,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets,
double expectedMinSeconds, double expectedMaxSeconds)
{
string TestDescription = "NumPacketsToSend=" + numPacketsToSend + ". NumPacketsToGet=" + numPacketsToGet +
". NumPacketsToBreakLoop=" + numPacketsToBreakLoop;
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 100);
using (PacketCommunicator communicator = OpenOfflineDevice(numPacketsToSend, expectedPacket))
{
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
if (numPacketsToBreakLoop == 0)
communicator.Break();
PacketHandler handler = new PacketHandler(expectedPacket, expectedMinSeconds, expectedMaxSeconds, communicator, numPacketsToBreakLoop);
int numPacketsGot;
PacketCommunicatorReceiveResult result = communicator.GetSomePackets(out numPacketsGot, numPacketsToGet, handler.Handle);
Assert.AreEqual(expectedResult, result);
Assert.AreEqual(expectedNumPackets, numPacketsGot, "NumPacketsGot. Test: " + TestDescription);
Assert.AreEqual(expectedNumPackets, handler.NumPacketsHandled, "NumPacketsHandled. Test: " + TestDescription);
}
}
private static void TestGetPackets(int numPacketsToSend, int numPacketsToGet, int numPacketsToBreakLoop,
PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets,
double expectedMinSeconds, double expectedMaxSeconds)
{
string TestDescription = "NumPacketsToSend=" + numPacketsToSend + ". NumPacketsToGet=" + numPacketsToGet +
". NumPacketsToBreakLoop=" + numPacketsToBreakLoop;
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 24);
using (PacketCommunicator communicator = OpenOfflineDevice(numPacketsToSend, expectedPacket))
{
communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);
if (numPacketsToBreakLoop == 0)
communicator.Break();
PacketHandler handler = new PacketHandler(expectedPacket, expectedMinSeconds, expectedMaxSeconds, communicator, numPacketsToBreakLoop);
PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None;
Thread thread = new Thread(delegate()
{
result = communicator.GetPackets(numPacketsToGet, handler.Handle);
});
thread.Start();
if (!thread.Join(TimeSpan.FromSeconds(5)))
{
thread.Abort();
}
Assert.AreEqual(expectedResult, result, TestDescription);
Assert.AreEqual(expectedNumPackets, handler.NumPacketsHandled);
}
}
private static PacketCommunicator OpenOfflineDevice()
{
return OpenOfflineDevice(10, MoreRandom.BuildRandomPacket(100));
}
private static PacketCommunicator OpenOfflineDevice(int numPackets, Packet packet)
{
string dumpFilename = Path.GetTempPath() + @"dump.pcap";
......
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Packets;
namespace PcapDotNet.Core.Test
{
internal class PacketHandler
{
public PacketHandler(Packet expectedPacket, DateTime expectedMinTimestamp, DateTime expectedMaxTimestamp,
PacketCommunicator communicator, int numPacketsToBreakLoop)
{
_expectedPacket = expectedPacket;
_expectedMinTimestamp = expectedMinTimestamp;
_expectedMaxTimestamp = expectedMaxTimestamp;
_communicator = communicator;
_numPacketsToBreakLoop = numPacketsToBreakLoop;
}
public PacketHandler(Packet expectedPacket, double expectedSecondsBefore, double expectedSecondsAfter, PacketCommunicator communicator, int numPacketsToBreakLoop)
: this(expectedPacket, expectedPacket.Timestamp.AddSeconds(-expectedSecondsBefore), expectedPacket.Timestamp.AddSeconds(expectedSecondsAfter), communicator, numPacketsToBreakLoop)
{
}
public void Handle(Packet packet)
{
Assert.AreEqual(_expectedPacket, packet);
MoreAssert.IsInRange(_expectedMinTimestamp, _expectedMaxTimestamp, packet.Timestamp);
++_numPacketsHandled;
if (NumPacketsHandled >= _numPacketsToBreakLoop)
_communicator.Break();
}
public int NumPacketsHandled
{
get { return _numPacketsHandled; }
}
private readonly Packet _expectedPacket;
private readonly DateTime _expectedMinTimestamp;
private readonly DateTime _expectedMaxTimestamp;
private readonly PacketCommunicator _communicator;
private readonly int _numPacketsToBreakLoop = int.MaxValue;
private int _numPacketsHandled; // = 0;
}
}
\ No newline at end of file
......@@ -46,6 +46,7 @@
<Compile Include="MoreAssert.cs" />
<Compile Include="LivePacketDeviceTests.cs" />
<Compile Include="MoreRandom.cs" />
<Compile Include="PacketHandler.cs" />
<Compile Include="PacketSendQueueTests.cs" />
<Compile Include="OfflinePacketDeviceTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
......
#include "OnlinePacketCommunicator.h"
#include "LivePacketCommunicator.h"
#include "Pcap.h"
using namespace PcapDotNet::Core;
OnlinePacketCommunicator::OnlinePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth, SocketAddress^ netmask)
LivePacketCommunicator::LivePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth, SocketAddress^ netmask)
: PacketCommunicator(source, snapshotLength, flags, readTimeout, auth, netmask)
{
}
PacketTotalStatistics^ OnlinePacketCommunicator::TotalStatistics::get()
PacketTotalStatistics^ LivePacketCommunicator::TotalStatistics::get()
{
int statisticsSize;
pcap_stat* statistics = pcap_stats_ex(_pcapDescriptor, &statisticsSize);
......
......@@ -4,7 +4,7 @@
namespace PcapDotNet { namespace Core
{
public ref class OnlinePacketCommunicator : PacketCommunicator
public ref class LivePacketCommunicator : PacketCommunicator
{
public:
virtual property PacketTotalStatistics^ TotalStatistics
......@@ -13,8 +13,8 @@ namespace PcapDotNet { namespace Core
}
internal:
OnlinePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth,
SocketAddress^ netmask);
LivePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth,
SocketAddress^ netmask);
};
}}
\ No newline at end of file
......@@ -4,7 +4,7 @@
#include "MarshalingServices.h"
#include "Pcap.h"
#include "OnlinePacketCommunicator.h"
#include "LivePacketCommunicator.h"
using namespace System;
using namespace System::Collections::Generic;
......@@ -82,7 +82,7 @@ PacketCommunicator^ LivePacketDevice::Open(int snapshotLength, PacketDeviceOpenF
netmask = Addresses[0]->Netmask;
// Open the device
return gcnew OnlinePacketCommunicator(deviceName.c_str(), snapshotLength, flags, readTimeout, NULL, netmask);
return gcnew LivePacketCommunicator(deviceName.c_str(), snapshotLength, flags, readTimeout, NULL, netmask);
}
// Private Methods
......
......@@ -148,13 +148,21 @@ PacketCommunicatorReceiveResult PacketCommunicator::GetSomePackets([Out] int% nu
functionPointer,
NULL);
if (numPacketsGot == -1)
throw BuildInvalidOperation("Failed reading from device");
if (numPacketsGot == -2)
switch (numPacketsGot)
{
case -2:
numPacketsGot = 0;
return PacketCommunicatorReceiveResult::BreakLoop;
case -1:
throw BuildInvalidOperation("Failed reading from device");
case 0:
if (packetHandler->PacketCounter != 0)
{
numPacketsGot = packetHandler->PacketCounter;
return PacketCommunicatorReceiveResult::Eof;
}
}
return PacketCommunicatorReceiveResult::Ok;
}
......@@ -167,10 +175,17 @@ PacketCommunicatorReceiveResult PacketCommunicator::GetPackets(int numPackets, H
pcap_handler functionPointer = (pcap_handler)Marshal::GetFunctionPointerForDelegate(packetHandlerDelegate).ToPointer();
int result = pcap_loop(_pcapDescriptor, numPackets, functionPointer, NULL);
if (result == -1)
throw BuildInvalidOperation("Failed reading from device");
if (result == -2)
switch (result)
{
case -2:
return PacketCommunicatorReceiveResult::BreakLoop;
case -1:
throw BuildInvalidOperation("Failed reading from device");
case 0:
if (packetHandler->PacketCounter != numPackets)
return PacketCommunicatorReceiveResult::Eof;
}
return PacketCommunicatorReceiveResult::Ok;
}
......@@ -315,9 +330,15 @@ InvalidOperationException^ PacketCommunicator::BuildInvalidOperation(System::Str
void PacketCommunicator::PacketHandler::Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData)
{
++_packetCounter;
_callBack->Invoke(CreatePacket(*packetHeader, packetData, _dataLink));
}
int PacketCommunicator::PacketHandler::PacketCounter::get()
{
return _packetCounter;
}
void PacketCommunicator::StatisticsHandler::Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData)
{
_callBack->Invoke(CreateStatistics(*packetHeader, packetData));
......
......@@ -115,8 +115,15 @@ namespace PcapDotNet { namespace Core
void Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData);
property int PacketCounter
{
int get();
}
private:
HandlePacket^ _callBack;
PcapDataLink _dataLink;
int _packetCounter;
};
ref class StatisticsHandler
......@@ -129,6 +136,7 @@ namespace PcapDotNet { namespace Core
void Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData);
private:
HandleStatistics^ _callBack;
};
......
......@@ -53,11 +53,15 @@ String^ PacketTotalStatistics::ToString()
StringBuilder^ stringBuilder = gcnew StringBuilder();
stringBuilder->Append("Packets Received: ");
stringBuilder->Append(PacketsReceived);
stringBuilder->Append(". ");
stringBuilder->Append("Packets Dropped By Driver: ");
stringBuilder->Append(PacketsDroppedByDriver);
stringBuilder->Append(". ");
stringBuilder->Append("Packets Dropped By Interface: ");
stringBuilder->Append(PacketsDroppedByInterface);
stringBuilder->Append(". ");
stringBuilder->Append("Packets Captured: ");
stringBuilder->Append(PacketsCaptured);
stringBuilder->Append(".");
return stringBuilder->ToString();
}
\ No newline at end of file
......@@ -267,19 +267,19 @@
Name="PacketCommunicator"
>
<File
RelativePath=".\OfflinePacketCommunicator.cpp"
RelativePath=".\LivePacketCommunicator.cpp"
>
</File>
<File
RelativePath=".\OfflinePacketCommunicator.h"
RelativePath=".\LivePacketCommunicator.h"
>
</File>
<File
RelativePath=".\OnlinePacketCommunicator.cpp"
RelativePath=".\OfflinePacketCommunicator.cpp"
>
</File>
<File
RelativePath=".\OnlinePacketCommunicator.h"
RelativePath=".\OfflinePacketCommunicator.h"
>
</File>
<File
......
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