Commit f4eff1b4 authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent c0c69224
...@@ -73,10 +73,10 @@ namespace PcapDotNet.Core.Test ...@@ -73,10 +73,10 @@ namespace PcapDotNet.Core.Test
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";
Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 100); Packet expectedPacket = MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, 1000);
Packet unexpectedPacket = MoreRandom.BuildRandomPacket(DestinationMac, SourceMac, 100); Packet unexpectedPacket = MoreRandom.BuildRandomPacket(DestinationMac, SourceMac, 1000);
using (BerkeleyPacketFilter filter = new BerkeleyPacketFilter("ether src " + SourceMac + " and ether dst " + DestinationMac, 100, DataLinkKind.Ethernet)) using (BerkeleyPacketFilter filter = new BerkeleyPacketFilter("ether src " + SourceMac + " and ether dst " + DestinationMac, 1000, DataLinkKind.Ethernet))
{ {
using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice()) using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
{ {
...@@ -102,5 +102,29 @@ namespace PcapDotNet.Core.Test ...@@ -102,5 +102,29 @@ namespace PcapDotNet.Core.Test
} }
} }
} }
[TestMethod]
public void TestTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
const int snapshotLength = 500;
using (BerkeleyPacketFilter filter = new BerkeleyPacketFilter("ether src " + SourceMac + " and ether dst " + DestinationMac, snapshotLength, DataLinkKind.Ethernet))
{
Assert.IsTrue(filter.Test(MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, snapshotLength / 2)));
Assert.IsTrue(filter.Test(MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, snapshotLength - 1)));
Assert.IsTrue(filter.Test(MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, snapshotLength)));
Assert.IsTrue(filter.Test(MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, snapshotLength + 1)));
Assert.IsTrue(filter.Test(MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, snapshotLength / 2)));
Assert.IsFalse(filter.Test(MoreRandom.BuildRandomPacket(DestinationMac, SourceMac, snapshotLength / 2)));
int actualSnapshotLength;
Assert.IsTrue(filter.Test(out actualSnapshotLength, MoreRandom.BuildRandomPacket(SourceMac, DestinationMac, snapshotLength / 2)));
Assert.AreEqual(snapshotLength, actualSnapshotLength);
}
}
} }
} }
\ No newline at end of file
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
#include "MarshalingServices.h" #include "MarshalingServices.h"
#include "PcapError.h" #include "PcapError.h"
#include "PcapDataLink.h" #include "PcapDataLink.h"
#include "PacketHeader.h"
using namespace System; using namespace System;
using namespace System::Runtime::InteropServices;
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
using namespace Packets; using namespace Packets;
...@@ -18,15 +20,20 @@ BerkeleyPacketFilter::BerkeleyPacketFilter(String^ filterString, int snapshotLen ...@@ -18,15 +20,20 @@ BerkeleyPacketFilter::BerkeleyPacketFilter(String^ filterString, int snapshotLen
Initialize(filterString, snapshotLength, kind, nullptr); Initialize(filterString, snapshotLength, kind, nullptr);
} }
BerkeleyPacketFilter::BerkeleyPacketFilter(pcap_t* pcapDescriptor, String^ filterString, IpV4SocketAddress^ netmask) bool BerkeleyPacketFilter::Test([Out] int% snapshotLength, Packets::Packet^ packet)
{ {
Initialize(pcapDescriptor, filterString, netmask); pcap_pkthdr pcapHeader;
PacketHeader::GetPcapHeader(pcapHeader, packet);
pin_ptr<Byte> unmanagedPacketBytes = &packet->Buffer[0];
snapshotLength = pcap_offline_filter(_bpf, &pcapHeader, unmanagedPacketBytes);
return (snapshotLength != 0);
} }
void BerkeleyPacketFilter::SetFilter(pcap_t* pcapDescriptor) bool BerkeleyPacketFilter::Test(Packets::Packet^ packet)
{ {
if (pcap_setfilter(pcapDescriptor, _bpf) != 0) int snapshotLength;
throw PcapError::BuildInvalidOperation("Failed setting bpf filter", pcapDescriptor); return Test(snapshotLength, packet);
} }
BerkeleyPacketFilter::~BerkeleyPacketFilter() BerkeleyPacketFilter::~BerkeleyPacketFilter()
...@@ -35,6 +42,21 @@ BerkeleyPacketFilter::~BerkeleyPacketFilter() ...@@ -35,6 +42,21 @@ BerkeleyPacketFilter::~BerkeleyPacketFilter()
delete _bpf; delete _bpf;
} }
// Internal
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);
}
// Private
void BerkeleyPacketFilter::Initialize(String^ filterString, int snapshotLength, DataLinkKind kind, IpV4SocketAddress^ netmask) void BerkeleyPacketFilter::Initialize(String^ filterString, int snapshotLength, DataLinkKind kind, IpV4SocketAddress^ netmask)
{ {
PcapDataLink dataLink = PcapDataLink(kind); PcapDataLink dataLink = PcapDataLink(kind);
......
...@@ -11,12 +11,14 @@ namespace PcapDotNet { namespace Core ...@@ -11,12 +11,14 @@ namespace PcapDotNet { namespace Core
BerkeleyPacketFilter(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind, IpV4SocketAddress^ netmask); BerkeleyPacketFilter(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind, IpV4SocketAddress^ netmask);
BerkeleyPacketFilter(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind); BerkeleyPacketFilter(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind);
void SetFilter(pcap_t* pcapDescriptor); bool Test([System::Runtime::InteropServices::Out] int% snapshotLength, Packets::Packet^ packet);
bool Test(Packets::Packet^ packet);
~BerkeleyPacketFilter(); // IDisposable ~BerkeleyPacketFilter(); // IDisposable
internal: internal:
BerkeleyPacketFilter(pcap_t* pcapDescriptor, System::String^ filterString, IpV4SocketAddress^ netmask); BerkeleyPacketFilter(pcap_t* pcapDescriptor, System::String^ filterString, IpV4SocketAddress^ netmask);
void SetFilter(pcap_t* pcapDescriptor);
private: private:
void Initialize(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind, IpV4SocketAddress^ netmask); void Initialize(System::String^ filterString, int snapshotLength, Packets::DataLinkKind kind, IpV4SocketAddress^ netmask);
......
...@@ -16,8 +16,8 @@ void PacketSendQueue::Enqueue(Packet^ packet) ...@@ -16,8 +16,8 @@ void PacketSendQueue::Enqueue(Packet^ packet)
{ {
pcap_pkthdr pcapHeader; pcap_pkthdr pcapHeader;
PacketHeader::GetPcapHeader(pcapHeader, packet); PacketHeader::GetPcapHeader(pcapHeader, packet);
pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0]; pin_ptr<Byte> unmanagedPacketBytes = &packet->Buffer[0];
if (pcap_sendqueue_queue(_pcapSendQueue, &pcapHeader, unamangedPacketBytes) != 0) if (pcap_sendqueue_queue(_pcapSendQueue, &pcapHeader, unmanagedPacketBytes) != 0)
throw gcnew InvalidOperationException("Failed enqueueing to SendQueue"); throw gcnew InvalidOperationException("Failed enqueueing to SendQueue");
} }
......
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