Commit 746fc817 authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent 22797897
...@@ -9,9 +9,9 @@ using namespace System; ...@@ -9,9 +9,9 @@ using namespace System;
using namespace BPacket; using namespace BPacket;
using namespace PcapDotNet; using namespace PcapDotNet;
PcapDeviceHandler::PcapDeviceHandler(pcap_t* handler, SocketAddress^ netmask) PcapDeviceHandler::PcapDeviceHandler(pcap_t* pcapDescriptor, SocketAddress^ netmask)
{ {
_handler = handler; _pcapDescriptor = pcapDescriptor;
_ipV4Netmask = dynamic_cast<IpV4SocketAddress^>(netmask); _ipV4Netmask = dynamic_cast<IpV4SocketAddress^>(netmask);
} }
...@@ -21,7 +21,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([System::Runtime::InteropSe ...@@ -21,7 +21,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([System::Runtime::InteropSe
pcap_pkthdr* packetHeader; pcap_pkthdr* packetHeader;
const unsigned char* packetData; const unsigned char* packetData;
DeviceHandlerResult result = safe_cast<DeviceHandlerResult>(pcap_next_ex(_handler, &packetHeader, &packetData)); DeviceHandlerResult result = safe_cast<DeviceHandlerResult>(pcap_next_ex(_pcapDescriptor, &packetHeader, &packetData));
if (result != DeviceHandlerResult::Ok) if (result != DeviceHandlerResult::Ok)
return result; return result;
...@@ -39,7 +39,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([System::Runtime::InteropSe ...@@ -39,7 +39,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([System::Runtime::InteropSe
void PcapDeviceHandler::SendPacket(Packet^ packet) void PcapDeviceHandler::SendPacket(Packet^ packet)
{ {
pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0]; pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0];
if (pcap_sendpacket(_handler, unamangedPacketBytes, packet->Length) != 0) if (pcap_sendpacket(_pcapDescriptor, unamangedPacketBytes, packet->Length) != 0)
{ {
throw gcnew InvalidOperationException("Failed sending packet"); throw gcnew InvalidOperationException("Failed sending packet");
} }
...@@ -47,14 +47,14 @@ void PcapDeviceHandler::SendPacket(Packet^ packet) ...@@ -47,14 +47,14 @@ void PcapDeviceHandler::SendPacket(Packet^ packet)
BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString) BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString)
{ {
return gcnew BpfFilter(_handler, filterString, _ipV4Netmask); return gcnew BpfFilter(_pcapDescriptor, filterString, _ipV4Netmask);
} }
void PcapDeviceHandler::SetFilter(BpfFilter^ filter) void PcapDeviceHandler::SetFilter(BpfFilter^ filter)
{ {
if (pcap_setfilter(_handler, &filter->Bpf) < 0) if (pcap_setfilter(_pcapDescriptor, &filter->Bpf) < 0)
{ {
throw gcnew InvalidOperationException("Error setting the filter: " + gcnew String(pcap_geterr(_handler))); throw gcnew InvalidOperationException("Error setting the filter: " + gcnew String(pcap_geterr(_pcapDescriptor)));
} }
} }
...@@ -74,13 +74,18 @@ void PcapDeviceHandler::SetFilter(String^ filterString) ...@@ -74,13 +74,18 @@ void PcapDeviceHandler::SetFilter(String^ filterString)
PcapDumpFile^ PcapDeviceHandler::OpenDump(System::String^ filename) PcapDumpFile^ PcapDeviceHandler::OpenDump(System::String^ filename)
{ {
std::string unmanagedString = MarshalingServices::ManagedToUnmanagedString(filename); std::string unmanagedString = MarshalingServices::ManagedToUnmanagedString(filename);
pcap_dumper_t* dumpFile = pcap_dump_open(_handler, unmanagedString.c_str()); pcap_dumper_t* dumpFile = pcap_dump_open(_pcapDescriptor, unmanagedString.c_str());
if (dumpFile == NULL) if (dumpFile == NULL)
throw gcnew InvalidOperationException("Error opening output file " + filename + " Error: " + gcnew System::String(pcap_geterr(_handler))); throw gcnew InvalidOperationException("Error opening output file " + filename + " Error: " + gcnew System::String(pcap_geterr(_pcapDescriptor)));
return gcnew PcapDumpFile(dumpFile, filename); return gcnew PcapDumpFile(dumpFile, filename);
} }
pcap_t* PcapDeviceHandler::Handler::get() PcapDeviceHandler::~PcapDeviceHandler()
{ {
return _handler; pcap_close(_pcapDescriptor);
}
pcap_t* PcapDeviceHandler::Descriptor::get()
{
return _pcapDescriptor;
} }
\ No newline at end of file
...@@ -14,10 +14,10 @@ namespace PcapDotNet ...@@ -14,10 +14,10 @@ namespace PcapDotNet
Eof = -2 // if EOF was reached reading from an offline capture Eof = -2 // if EOF was reached reading from an offline capture
}; };
public ref class PcapDeviceHandler public ref class PcapDeviceHandler : System::IDisposable
{ {
public: public:
PcapDeviceHandler(pcap_t* handler, SocketAddress^ netmask); PcapDeviceHandler(pcap_t* pcapDescriptor, SocketAddress^ netmask);
DeviceHandlerResult GetNextPacket([System::Runtime::InteropServices::Out] BPacket::Packet^% packet); DeviceHandlerResult GetNextPacket([System::Runtime::InteropServices::Out] BPacket::Packet^% packet);
...@@ -29,14 +29,16 @@ namespace PcapDotNet ...@@ -29,14 +29,16 @@ namespace PcapDotNet
PcapDumpFile^ OpenDump(System::String^ filename); PcapDumpFile^ OpenDump(System::String^ filename);
~PcapDeviceHandler();
internal: internal:
property pcap_t* Handler property pcap_t* Descriptor
{ {
pcap_t* get(); pcap_t* get();
} }
private: private:
pcap_t* _handler; pcap_t* _pcapDescriptor;
IpV4SocketAddress^ _ipV4Netmask; IpV4SocketAddress^ _ipV4Netmask;
}; };
} }
\ No newline at end of file
...@@ -67,13 +67,11 @@ List<PcapAddress^>^ PcapLiveDevice::Addresses::get() ...@@ -67,13 +67,11 @@ List<PcapAddress^>^ PcapLiveDevice::Addresses::get()
PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout) PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout)
{ {
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
std::string deviceName = MarshalingServices::ManagedToUnmanagedString(Name); std::string deviceName = MarshalingServices::ManagedToUnmanagedString(Name);
// Open the device // Open the device
adhandle = pcap_open(deviceName.c_str(), // name of the device char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *pcapDescriptor = pcap_open(deviceName.c_str(), // name of the device
snapLen, // portion of the packet to capture snapLen, // portion of the packet to capture
// 65536 guarantees that the whole packet will be captured on all the link layers // 65536 guarantees that the whole packet will be captured on all the link layers
safe_cast<int>(flags), safe_cast<int>(flags),
...@@ -81,7 +79,7 @@ PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags, ...@@ -81,7 +79,7 @@ PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags,
NULL, // authentication on the remote machine NULL, // authentication on the remote machine
errbuf); // error buffer errbuf); // error buffer
if (adhandle == NULL) if (pcapDescriptor == NULL)
{ {
gcnew InvalidOperationException(String::Format("Unable to open the adapter. %s is not supported by WinPcap", Name)); gcnew InvalidOperationException(String::Format("Unable to open the adapter. %s is not supported by WinPcap", Name));
} }
...@@ -89,7 +87,7 @@ PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags, ...@@ -89,7 +87,7 @@ PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags,
SocketAddress^ netmask; SocketAddress^ netmask;
if (Addresses->Count != 0) if (Addresses->Count != 0)
netmask = Addresses[0]->Netmask; netmask = Addresses[0]->Netmask;
return gcnew PcapDeviceHandler(adhandle, netmask); return gcnew PcapDeviceHandler(pcapDescriptor, netmask);
} }
// Private Methods // Private Methods
......
...@@ -22,10 +22,10 @@ void PcapSendQueue::Enqueue(Packet^ packet) ...@@ -22,10 +22,10 @@ void PcapSendQueue::Enqueue(Packet^ packet)
void PcapSendQueue::Transmit(PcapDeviceHandler^ deviceHandler, bool isSync) void PcapSendQueue::Transmit(PcapDeviceHandler^ deviceHandler, bool isSync)
{ {
unsigned int numBytesTransmitted = pcap_sendqueue_transmit(deviceHandler->Handler, _pcapSendQueue, isSync); unsigned int numBytesTransmitted = pcap_sendqueue_transmit(deviceHandler->Descriptor, _pcapSendQueue, isSync);
if (numBytesTransmitted < _pcapSendQueue->len) if (numBytesTransmitted < _pcapSendQueue->len)
{ {
throw gcnew InvalidOperationException(String::Format("An error occurred sending the packets: %s. Only %d bytes were sent", gcnew String(pcap_geterr(deviceHandler->Handler)), numBytesTransmitted)); throw gcnew InvalidOperationException(String::Format("An error occurred sending the packets: %s. Only %d bytes were sent", gcnew String(pcap_geterr(deviceHandler->Descriptor)), numBytesTransmitted));
} }
} }
......
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