Commit c0c69224 authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent 252d0cf2
using System; using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Packets;
namespace PcapDotNet.Core.Test namespace PcapDotNet.Core.Test
{ {
...@@ -56,7 +57,6 @@ namespace PcapDotNet.Core.Test ...@@ -56,7 +57,6 @@ namespace PcapDotNet.Core.Test
// //
#endregion #endregion
[TestMethod] [TestMethod]
[ExpectedException(typeof(InvalidOperationException))] [ExpectedException(typeof(InvalidOperationException))]
public void BadFilterErrorTest() public void BadFilterErrorTest()
...@@ -66,5 +66,41 @@ namespace PcapDotNet.Core.Test ...@@ -66,5 +66,41 @@ namespace PcapDotNet.Core.Test
communicator.SetFilter("illegal filter string"); communicator.SetFilter("illegal filter string");
} }
} }
[TestMethod]
public void NoCommunicatorConstructorTest()
{
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);
Packet unexpectedPacket = MoreRandom.BuildRandomPacket(DestinationMac, SourceMac, 100);
using (BerkeleyPacketFilter filter = new BerkeleyPacketFilter("ether src " + SourceMac + " and ether dst " + DestinationMac, 100, DataLinkKind.Ethernet))
{
using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
{
communicator.SetFilter(filter);
for (int i = 0; i != 5; ++i)
{
communicator.SendPacket(expectedPacket);
communicator.SendPacket(unexpectedPacket);
}
Packet packet;
PacketCommunicatorReceiveResult result;
for (int i = 0; i != 5; ++i)
{
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(expectedPacket, packet);
}
result = communicator.GetPacket(out packet);
Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result);
Assert.IsNull(packet);
}
}
}
} }
} }
\ No newline at end of file
...@@ -121,7 +121,10 @@ namespace PcapDotNet.Core.Test ...@@ -121,7 +121,10 @@ namespace PcapDotNet.Core.Test
expectedDiff = TimeSpan.Zero; expectedDiff = TimeSpan.Zero;
} }
TimeSpan actualDiff = packet.Timestamp - lastTimestamp; TimeSpan actualDiff = packet.Timestamp - lastTimestamp;
Assert.AreEqual(expectedDiff, actualDiff); MoreAssert.IsInRange(
expectedDiff.Subtract(TimeSpan.FromSeconds(0.01)),
expectedDiff.Add(TimeSpan.FromSeconds(0.01)),
actualDiff);
} }
lastTimestamp = packet.Timestamp; lastTimestamp = packet.Timestamp;
++numPacketsHandled; ++numPacketsHandled;
......
...@@ -2,11 +2,54 @@ ...@@ -2,11 +2,54 @@
#include "Pcap.h" #include "Pcap.h"
#include "MarshalingServices.h" #include "MarshalingServices.h"
#include "PcapError.h" #include "PcapError.h"
#include "PcapDataLink.h"
using namespace System; using namespace System;
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
using namespace Packets;
BerkeleyPacketFilter::BerkeleyPacketFilter(String^ filterString, int snapshotLength, DataLinkKind kind, IpV4SocketAddress^ netmask)
{
Initialize(filterString, snapshotLength, kind, netmask);
}
BerkeleyPacketFilter::BerkeleyPacketFilter(String^ filterString, int snapshotLength, DataLinkKind kind)
{
Initialize(filterString, snapshotLength, kind, nullptr);
}
BerkeleyPacketFilter::BerkeleyPacketFilter(pcap_t* pcapDescriptor, String^ filterString, IpV4SocketAddress^ netmask) BerkeleyPacketFilter::BerkeleyPacketFilter(pcap_t* pcapDescriptor, String^ filterString, IpV4SocketAddress^ netmask)
{
Initialize(pcapDescriptor, filterString, netmask);
}
void BerkeleyPacketFilter::SetFilter(pcap_t* pcapDescriptor)
{
if (pcap_setfilter(pcapDescriptor, _bpf) != 0)
throw PcapError::BuildInvalidOperation("Failed setting bpf filter", pcapDescriptor);
}
BerkeleyPacketFilter::~BerkeleyPacketFilter()
{
pcap_freecode(_bpf);
delete _bpf;
}
void BerkeleyPacketFilter::Initialize(String^ filterString, int snapshotLength, DataLinkKind kind, IpV4SocketAddress^ netmask)
{
PcapDataLink dataLink = PcapDataLink(kind);
pcap_t* pcapDescriptor = pcap_open_dead(dataLink.Value, snapshotLength);
try
{
Initialize(pcapDescriptor, filterString, netmask);
}
finally
{
pcap_close(pcapDescriptor);
}
}
void BerkeleyPacketFilter::Initialize(pcap_t* pcapDescriptor, String^ filterString, IpV4SocketAddress^ netmask)
{ {
std::string unmanagedFilterString = MarshalingServices::ManagedToUnmanagedString(filterString); std::string unmanagedFilterString = MarshalingServices::ManagedToUnmanagedString(filterString);
...@@ -29,16 +72,3 @@ BerkeleyPacketFilter::BerkeleyPacketFilter(pcap_t* pcapDescriptor, String^ filte ...@@ -29,16 +72,3 @@ BerkeleyPacketFilter::BerkeleyPacketFilter(pcap_t* pcapDescriptor, String^ filte
throw; throw;
} }
} }
void BerkeleyPacketFilter::SetFilter(pcap_t* pcapDescriptor)
{
if (pcap_setfilter(pcapDescriptor, _bpf) != 0)
throw PcapError::BuildInvalidOperation("Failed setting bpf filter", pcapDescriptor);
}
BerkeleyPacketFilter::~BerkeleyPacketFilter()
{
pcap_freecode(_bpf);
delete _bpf;
}
...@@ -8,6 +8,9 @@ namespace PcapDotNet { namespace Core ...@@ -8,6 +8,9 @@ namespace PcapDotNet { namespace Core
public ref class BerkeleyPacketFilter : System::IDisposable public ref class BerkeleyPacketFilter : System::IDisposable
{ {
public: public:
BerkeleyPacketFilter(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind, IpV4SocketAddress^ netmask);
BerkeleyPacketFilter(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind);
void SetFilter(pcap_t* pcapDescriptor); void SetFilter(pcap_t* pcapDescriptor);
~BerkeleyPacketFilter(); // IDisposable ~BerkeleyPacketFilter(); // IDisposable
...@@ -15,6 +18,10 @@ namespace PcapDotNet { namespace Core ...@@ -15,6 +18,10 @@ namespace PcapDotNet { namespace Core
internal: internal:
BerkeleyPacketFilter(pcap_t* pcapDescriptor, System::String^ filterString, IpV4SocketAddress^ netmask); BerkeleyPacketFilter(pcap_t* pcapDescriptor, System::String^ filterString, IpV4SocketAddress^ netmask);
private:
void Initialize(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind, IpV4SocketAddress^ netmask);
void Initialize(pcap_t* pcapDescriptor, System::String^ filterString, IpV4SocketAddress^ netmask);
private: private:
bpf_program* _bpf; bpf_program* _bpf;
}; };
......
...@@ -9,6 +9,11 @@ using namespace System; ...@@ -9,6 +9,11 @@ using namespace System;
using namespace Packets; using namespace Packets;
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
PcapDataLink::PcapDataLink(DataLinkKind kind)
{
_value = KindToValue(kind);
}
PcapDataLink::PcapDataLink(int value) PcapDataLink::PcapDataLink(int value)
{ {
_value = value; _value = value;
...@@ -64,3 +69,15 @@ String^ PcapDataLink::ToString() ...@@ -64,3 +69,15 @@ String^ PcapDataLink::ToString()
{ {
return Name + " (" + Description + ")"; return Name + " (" + Description + ")";
} }
// static
int PcapDataLink::KindToValue(DataLinkKind kind)
{
switch (kind)
{
case DataLinkKind::Ethernet:
return 1;
default:
throw gcnew NotSupportedException("PcapDataLink kind " + kind.ToString() + " is unsupported");
}
}
...@@ -5,6 +5,7 @@ namespace PcapDotNet { namespace Core ...@@ -5,6 +5,7 @@ namespace PcapDotNet { namespace Core
public value class PcapDataLink : Packets::IDataLink public value class PcapDataLink : Packets::IDataLink
{ {
public: public:
PcapDataLink(Packets::DataLinkKind kind);
PcapDataLink(int value); PcapDataLink(int value);
PcapDataLink(System::String^ name); PcapDataLink(System::String^ name);
...@@ -30,6 +31,9 @@ namespace PcapDotNet { namespace Core ...@@ -30,6 +31,9 @@ namespace PcapDotNet { namespace Core
virtual System::String^ ToString() override; virtual System::String^ ToString() override;
private:
static int KindToValue(Packets::DataLinkKind kind);
private: private:
int _value; int _value;
}; };
......
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