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;
using namespace BPacket;
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);
}
......@@ -21,7 +21,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([System::Runtime::InteropSe
pcap_pkthdr* packetHeader;
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)
return result;
......@@ -39,7 +39,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([System::Runtime::InteropSe
void PcapDeviceHandler::SendPacket(Packet^ packet)
{
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");
}
......@@ -47,14 +47,14 @@ void PcapDeviceHandler::SendPacket(Packet^ packet)
BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString)
{
return gcnew BpfFilter(_handler, filterString, _ipV4Netmask);
return gcnew BpfFilter(_pcapDescriptor, filterString, _ipV4Netmask);
}
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)
PcapDumpFile^ PcapDeviceHandler::OpenDump(System::String^ 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)
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);
}
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
Eof = -2 // if EOF was reached reading from an offline capture
};
public ref class PcapDeviceHandler
public ref class PcapDeviceHandler : System::IDisposable
{
public:
PcapDeviceHandler(pcap_t* handler, SocketAddress^ netmask);
PcapDeviceHandler(pcap_t* pcapDescriptor, SocketAddress^ netmask);
DeviceHandlerResult GetNextPacket([System::Runtime::InteropServices::Out] BPacket::Packet^% packet);
......@@ -29,14 +29,16 @@ namespace PcapDotNet
PcapDumpFile^ OpenDump(System::String^ filename);
~PcapDeviceHandler();
internal:
property pcap_t* Handler
property pcap_t* Descriptor
{
pcap_t* get();
}
private:
pcap_t* _handler;
pcap_t* _pcapDescriptor;
IpV4SocketAddress^ _ipV4Netmask;
};
}
\ No newline at end of file
......@@ -67,13 +67,11 @@ List<PcapAddress^>^ PcapLiveDevice::Addresses::get()
PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout)
{
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
std::string deviceName = MarshalingServices::ManagedToUnmanagedString(Name);
// 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
// 65536 guarantees that the whole packet will be captured on all the link layers
safe_cast<int>(flags),
......@@ -81,7 +79,7 @@ PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags,
NULL, // authentication on the remote machine
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));
}
......@@ -89,7 +87,7 @@ PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags,
SocketAddress^ netmask;
if (Addresses->Count != 0)
netmask = Addresses[0]->Netmask;
return gcnew PcapDeviceHandler(adhandle, netmask);
return gcnew PcapDeviceHandler(pcapDescriptor, netmask);
}
// Private Methods
......
......@@ -22,10 +22,10 @@ void PcapSendQueue::Enqueue(Packet^ packet)
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)
{
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