Commit 16159dcc authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent ca7a7b84
...@@ -65,7 +65,7 @@ namespace WinPcapDotNet.Console ...@@ -65,7 +65,7 @@ namespace WinPcapDotNet.Console
private static void GetNextPackets(IPcapDevice device, string filter) private static void GetNextPackets(IPcapDevice device, string filter)
{ {
using (PcapDeviceHandler deviceHandler = device.Open(PcapDevice.DefaultSnapLen, PcapDeviceOpenFlags.Promiscuous, 1 * 1000)) using (PcapDeviceHandler deviceHandler = device.Open(PcapDevice.DefaultSnapLen, PcapDeviceOpenFlags.Promiscuous, 10 * 1000))
{ {
deviceHandler.SetFilter(filter); deviceHandler.SetFilter(filter);
int numPacketsGot; int numPacketsGot;
...@@ -94,8 +94,6 @@ namespace WinPcapDotNet.Console ...@@ -94,8 +94,6 @@ namespace WinPcapDotNet.Console
break; break;
case DeviceHandlerResult.Timeout: case DeviceHandlerResult.Timeout:
continue; continue;
case DeviceHandlerResult.Error:
throw new InvalidOperationException("Failed reading from device");
case DeviceHandlerResult.Eof: case DeviceHandlerResult.Eof:
continue; continue;
} }
...@@ -122,8 +120,6 @@ namespace WinPcapDotNet.Console ...@@ -122,8 +120,6 @@ namespace WinPcapDotNet.Console
break; break;
case DeviceHandlerResult.Timeout: case DeviceHandlerResult.Timeout:
continue; continue;
case DeviceHandlerResult.Error:
throw new InvalidOperationException("Failed reading from device");
case DeviceHandlerResult.Eof: case DeviceHandlerResult.Eof:
continue; continue;
} }
...@@ -156,8 +152,6 @@ namespace WinPcapDotNet.Console ...@@ -156,8 +152,6 @@ namespace WinPcapDotNet.Console
break; break;
case DeviceHandlerResult.Timeout: case DeviceHandlerResult.Timeout:
continue; continue;
case DeviceHandlerResult.Error:
throw new InvalidOperationException("Failed reading from device");
case DeviceHandlerResult.Eof: case DeviceHandlerResult.Eof:
continue; continue;
} }
...@@ -184,8 +178,6 @@ namespace WinPcapDotNet.Console ...@@ -184,8 +178,6 @@ namespace WinPcapDotNet.Console
break; break;
case DeviceHandlerResult.Timeout: case DeviceHandlerResult.Timeout:
continue; continue;
case DeviceHandlerResult.Error:
throw new InvalidOperationException("Failed reading from device");
case DeviceHandlerResult.Eof: case DeviceHandlerResult.Eof:
continue; continue;
} }
......
#include "BpfFilter.h" #include "BpfFilter.h"
#include "Pcap.h" #include "Pcap.h"
#include "MarshalingServices.h" #include "MarshalingServices.h"
#include "PcapError.h"
using namespace System; using namespace System;
using namespace PcapDotNet; using namespace PcapDotNet;
BpfFilter::BpfFilter(pcap_t* handler, String^ filterString, IpV4SocketAddress^ netmask) BpfFilter::BpfFilter(pcap_t* pcapDescriptor, String^ filterString, IpV4SocketAddress^ netmask)
{ {
std::string unmanagedFilterString = MarshalingServices::ManagedToUnmanagedString(filterString); std::string unmanagedFilterString = MarshalingServices::ManagedToUnmanagedString(filterString);
...@@ -17,9 +18,9 @@ BpfFilter::BpfFilter(pcap_t* handler, String^ filterString, IpV4SocketAddress^ n ...@@ -17,9 +18,9 @@ BpfFilter::BpfFilter(pcap_t* handler, String^ filterString, IpV4SocketAddress^ n
try try
{ {
if (pcap_compile(handler, _bpf, const_cast<char*>(unmanagedFilterString.c_str()), 1, netmaskValue) < 0) if (pcap_compile(pcapDescriptor, _bpf, const_cast<char*>(unmanagedFilterString.c_str()), 1, netmaskValue) < 0)
{ {
gcnew ArgumentException("An error has occured when compiling the filter: " + gcnew String(pcap_geterr(handler))); gcnew ArgumentException("An error has occured when compiling the filter: " + PcapError::GetErrorMessage(pcapDescriptor));
} }
} }
catch(...) catch(...)
...@@ -29,12 +30,15 @@ BpfFilter::BpfFilter(pcap_t* handler, String^ filterString, IpV4SocketAddress^ n ...@@ -29,12 +30,15 @@ BpfFilter::BpfFilter(pcap_t* handler, String^ filterString, IpV4SocketAddress^ n
} }
} }
BpfFilter::~BpfFilter() void BpfFilter::SetFilter(pcap_t* pcapDescriptor)
{ {
delete _bpf; if (pcap_setfilter(pcapDescriptor, _bpf) != 0)
throw PcapError::BuildInvalidOperation("Failed setting bpf filter", pcapDescriptor);
} }
bpf_program& BpfFilter::Bpf::get()
BpfFilter::~BpfFilter()
{ {
return *_bpf; pcap_freecode(_bpf);
delete _bpf;
} }
...@@ -8,14 +8,11 @@ namespace PcapDotNet ...@@ -8,14 +8,11 @@ namespace PcapDotNet
public ref class BpfFilter : System::IDisposable public ref class BpfFilter : System::IDisposable
{ {
public: public:
BpfFilter(pcap_t* handler, System::String^ filterString, IpV4SocketAddress^ netmask); BpfFilter(pcap_t* pcapDescriptor, System::String^ filterString, IpV4SocketAddress^ netmask);
~BpfFilter(); // IDisposable void SetFilter(pcap_t* pcapDescriptor);
property bpf_program& Bpf ~BpfFilter(); // IDisposable
{
bpf_program& get();
}
private: private:
bpf_program* _bpf; bpf_program* _bpf;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "MarshalingServices.h" #include "MarshalingServices.h"
#include "PcapDumpFile.h" #include "PcapDumpFile.h"
#include "Timestamp.h" #include "Timestamp.h"
#include "PcapError.h"
#include "Pcap.h" #include "Pcap.h"
using namespace System; using namespace System;
...@@ -47,8 +48,7 @@ void PcapDeviceHandler::Mode::set(DeviceHandlerMode value) ...@@ -47,8 +48,7 @@ void PcapDeviceHandler::Mode::set(DeviceHandlerMode value)
DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet) DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet)
{ {
if (Mode != DeviceHandlerMode::Capture) AssertMode(DeviceHandlerMode::Capture);
throw gcnew InvalidOperationException("Must be in capture mode to get packets");
pcap_pkthdr* packetHeader; pcap_pkthdr* packetHeader;
const unsigned char* packetData; const unsigned char* packetData;
...@@ -66,36 +66,18 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet) ...@@ -66,36 +66,18 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet)
DeviceHandlerResult PcapDeviceHandler::GetNextPackets(int maxPackets, HandlePacket^ callBack, [Out] int% numPacketsGot) DeviceHandlerResult PcapDeviceHandler::GetNextPackets(int maxPackets, HandlePacket^ callBack, [Out] int% numPacketsGot)
{ {
if (Mode != DeviceHandlerMode::Capture) AssertMode(DeviceHandlerMode::Capture);
throw gcnew InvalidOperationException("Must be in capture mode to get packets");
PacketHandler^ packetHandler = gcnew PacketHandler(callBack); PacketHandler^ packetHandler = gcnew PacketHandler(callBack);
PacketHandler::Delegate^ packetHandlerDelegate = gcnew PacketHandler::Delegate(packetHandler, HandlerDelegate^ packetHandlerDelegate = gcnew HandlerDelegate(packetHandler,
&PacketHandler::Handle); &PacketHandler::Handle);
pcap_handler functionPointer =
(pcap_handler)Marshal::GetFunctionPointerForDelegate(packetHandlerDelegate).ToPointer();
numPacketsGot = pcap_dispatch(_pcapDescriptor,
maxPackets,
functionPointer,
NULL);
if (numPacketsGot == -1)
{
throw gcnew InvalidOperationException("Failed Getting packets. Error: " + gcnew String(pcap_geterr(_pcapDescriptor)));
}
if (numPacketsGot == -2)
{
return DeviceHandlerResult::BreakLoop;
}
return DeviceHandlerResult::Ok; return RunPcapDispatch(maxPackets, packetHandlerDelegate, numPacketsGot);
} }
DeviceHandlerResult PcapDeviceHandler::GetNextStatistics([Out] PcapStatistics^% statistics) DeviceHandlerResult PcapDeviceHandler::GetNextStatistics([Out] PcapStatistics^% statistics)
{ {
if (Mode != DeviceHandlerMode::Statistics) AssertMode(DeviceHandlerMode::Statistics);
throw gcnew InvalidOperationException("Must be in statistics mode to get statistics");
pcap_pkthdr* packetHeader; pcap_pkthdr* packetHeader;
const unsigned char* packetData; const unsigned char* packetData;
...@@ -107,14 +89,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextStatistics([Out] PcapStatistics^% ...@@ -107,14 +89,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextStatistics([Out] PcapStatistics^%
return result; return result;
} }
timeval pcapTimestamp = packetHeader->ts; statistics = CreateStatistics(*packetHeader, packetData);
DateTime timestamp;
Timestamp::PcapTimestampToDateTime(packetHeader->ts, timestamp);
unsigned long acceptedPackets = *reinterpret_cast<const unsigned long*>(packetData);
unsigned long acceptedBytes = *reinterpret_cast<const unsigned long*>(packetData + 8);
statistics = gcnew PcapStatistics(timestamp, acceptedPackets, acceptedBytes);
return result; return result;
} }
...@@ -123,9 +98,7 @@ void PcapDeviceHandler::SendPacket(Packet^ packet) ...@@ -123,9 +98,7 @@ void PcapDeviceHandler::SendPacket(Packet^ packet)
{ {
pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0]; pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0];
if (pcap_sendpacket(_pcapDescriptor, unamangedPacketBytes, packet->Length) != 0) if (pcap_sendpacket(_pcapDescriptor, unamangedPacketBytes, packet->Length) != 0)
{ throw BuildInvalidOperation("Failed writing to device");
throw gcnew InvalidOperationException("Failed sending packet");
}
} }
BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString) BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString)
...@@ -135,10 +108,7 @@ BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString) ...@@ -135,10 +108,7 @@ BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString)
void PcapDeviceHandler::SetFilter(BpfFilter^ filter) void PcapDeviceHandler::SetFilter(BpfFilter^ filter)
{ {
if (pcap_setfilter(_pcapDescriptor, &filter->Bpf) < 0) filter->SetFilter(_pcapDescriptor);
{
throw gcnew InvalidOperationException("Error setting the filter: " + gcnew String(pcap_geterr(_pcapDescriptor)));
}
} }
void PcapDeviceHandler::SetFilter(String^ filterString) void PcapDeviceHandler::SetFilter(String^ filterString)
...@@ -156,11 +126,7 @@ void PcapDeviceHandler::SetFilter(String^ filterString) ...@@ -156,11 +126,7 @@ void PcapDeviceHandler::SetFilter(String^ filterString)
PcapDumpFile^ PcapDeviceHandler::OpenDump(System::String^ filename) PcapDumpFile^ PcapDeviceHandler::OpenDump(System::String^ filename)
{ {
std::string unmanagedString = MarshalingServices::ManagedToUnmanagedString(filename); return gcnew PcapDumpFile(_pcapDescriptor, filename);
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(_pcapDescriptor)));
return gcnew PcapDumpFile(dumpFile, filename);
} }
PcapDeviceHandler::~PcapDeviceHandler() PcapDeviceHandler::~PcapDeviceHandler()
...@@ -183,6 +149,18 @@ Packet^ PcapDeviceHandler::CreatePacket(const pcap_pkthdr& packetHeader, const u ...@@ -183,6 +149,18 @@ Packet^ PcapDeviceHandler::CreatePacket(const pcap_pkthdr& packetHeader, const u
return gcnew Packet(managedPacketData, timestamp); return gcnew Packet(managedPacketData, timestamp);
} }
// static
PcapStatistics^ PcapDeviceHandler::CreateStatistics(const pcap_pkthdr& packetHeader, const unsigned char* packetData)
{
DateTime timestamp;
Timestamp::PcapTimestampToDateTime(packetHeader.ts, timestamp);
unsigned long acceptedPackets = *reinterpret_cast<const unsigned long*>(packetData);
unsigned long acceptedBytes = *reinterpret_cast<const unsigned long*>(packetData + 8);
return gcnew PcapStatistics(timestamp, acceptedPackets, acceptedBytes);
}
DeviceHandlerResult PcapDeviceHandler::RunPcapNextEx(pcap_pkthdr** packetHeader, const unsigned char** packetData) DeviceHandlerResult PcapDeviceHandler::RunPcapNextEx(pcap_pkthdr** packetHeader, const unsigned char** packetData)
{ {
int result = pcap_next_ex(_pcapDescriptor, packetHeader, packetData); int result = pcap_next_ex(_pcapDescriptor, packetHeader, packetData);
...@@ -191,17 +169,60 @@ DeviceHandlerResult PcapDeviceHandler::RunPcapNextEx(pcap_pkthdr** packetHeader, ...@@ -191,17 +169,60 @@ DeviceHandlerResult PcapDeviceHandler::RunPcapNextEx(pcap_pkthdr** packetHeader,
case -2: case -2:
return DeviceHandlerResult::Eof; return DeviceHandlerResult::Eof;
case -1: case -1:
return DeviceHandlerResult::Error; throw PcapError::BuildInvalidOperation("Failed reading from device", _pcapDescriptor);
case 0: case 0:
return DeviceHandlerResult::Timeout; return DeviceHandlerResult::Timeout;
case 1: case 1:
return DeviceHandlerResult::Ok; return DeviceHandlerResult::Ok;
default: default:
throw gcnew InvalidOperationException("Result value " + result + " is undefined"); throw gcnew InvalidOperationException("Result value " + result.ToString() + " is undefined");
} }
} }
DeviceHandlerResult PcapDeviceHandler::RunPcapDispatch(int maxInstances, HandlerDelegate^ callBack, [System::Runtime::InteropServices::Out] int% numInstancesGot)
{
pcap_handler functionPointer =
(pcap_handler)Marshal::GetFunctionPointerForDelegate(callBack).ToPointer();
numInstancesGot = pcap_dispatch(_pcapDescriptor,
maxInstances,
functionPointer,
NULL);
if (numInstancesGot == -1)
{
throw BuildInvalidOperation("Failed reading from device");
}
if (numInstancesGot == -2)
{
return DeviceHandlerResult::BreakLoop;
}
return DeviceHandlerResult::Ok;
}
void PcapDeviceHandler::AssertMode(DeviceHandlerMode mode)
{
if (Mode != mode)
throw gcnew InvalidOperationException("Wrong Mode. Must be in mode " + mode.ToString() + " and not in mode " + Mode.ToString());
}
String^ PcapDeviceHandler::ErrorMessage::get()
{
return PcapError::GetErrorMessage(_pcapDescriptor);
}
InvalidOperationException^ PcapDeviceHandler::BuildInvalidOperation(System::String^ errorMessage)
{
return PcapError::BuildInvalidOperation(errorMessage, _pcapDescriptor);
}
void PcapDeviceHandler::PacketHandler::Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData) void PcapDeviceHandler::PacketHandler::Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData)
{ {
_callBack->Invoke(CreatePacket(*packetHeader, packetData)); _callBack->Invoke(CreatePacket(*packetHeader, packetData));
}
void PcapDeviceHandler::StatisticsHandler::Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData)
{
_callBack->Invoke(CreateStatistics(*packetHeader, packetData));
} }
\ No newline at end of file
...@@ -12,7 +12,6 @@ namespace PcapDotNet ...@@ -12,7 +12,6 @@ namespace PcapDotNet
{ {
Ok, // if the packet has been read without problems Ok, // if the packet has been read without problems
Timeout, // if the timeout set with Open() has elapsed. Timeout, // if the timeout set with Open() has elapsed.
Error, // if an error occurred
Eof, // if EOF was reached reading from an offline capture Eof, // if EOF was reached reading from an offline capture
BreakLoop // BreakLoop //
}; };
...@@ -38,9 +37,10 @@ namespace PcapDotNet ...@@ -38,9 +37,10 @@ namespace PcapDotNet
} }
delegate void HandlePacket(BPacket::Packet^ packet); delegate void HandlePacket(BPacket::Packet^ packet);
DeviceHandlerResult GetNextPacket([System::Runtime::InteropServices::Out] BPacket::Packet^% packet); DeviceHandlerResult GetNextPacket([System::Runtime::InteropServices::Out] BPacket::Packet^% packet);
DeviceHandlerResult GetNextPackets(int maxPackets, HandlePacket^ callBack, [System::Runtime::InteropServices::Out] int% numPacketsGot); DeviceHandlerResult GetNextPackets(int maxPackets, HandlePacket^ callBack, [System::Runtime::InteropServices::Out] int% numPacketsGot);
delegate void HandleStatistics(PcapStatistics^ statistics);
DeviceHandlerResult GetNextStatistics([System::Runtime::InteropServices::Out] PcapStatistics^% statistics); DeviceHandlerResult GetNextStatistics([System::Runtime::InteropServices::Out] PcapStatistics^% statistics);
void SendPacket(BPacket::Packet^ packet); void SendPacket(BPacket::Packet^ packet);
...@@ -61,9 +61,24 @@ namespace PcapDotNet ...@@ -61,9 +61,24 @@ namespace PcapDotNet
private: private:
static BPacket::Packet^ CreatePacket(const pcap_pkthdr& packetHeader, const unsigned char* packetData); static BPacket::Packet^ CreatePacket(const pcap_pkthdr& packetHeader, const unsigned char* packetData);
static PcapStatistics^ PcapDeviceHandler::CreateStatistics(const pcap_pkthdr& packetHeader, const unsigned char* packetData);
DeviceHandlerResult RunPcapNextEx(pcap_pkthdr** packetHeader, const unsigned char** packetData); DeviceHandlerResult RunPcapNextEx(pcap_pkthdr** packetHeader, const unsigned char** packetData);
[System::Runtime::InteropServices::UnmanagedFunctionPointer(System::Runtime::InteropServices::CallingConvention::Cdecl)]
delegate void HandlerDelegate(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData);
DeviceHandlerResult RunPcapDispatch(int maxInstances, HandlerDelegate^ callBack, [System::Runtime::InteropServices::Out] int% numInstancesGot);
void AssertMode(DeviceHandlerMode mode);
property System::String^ ErrorMessage
{
System::String^ get();
}
System::InvalidOperationException^ BuildInvalidOperation(System::String^ errorMessage);
ref class PacketHandler ref class PacketHandler
{ {
public: public:
...@@ -72,14 +87,24 @@ namespace PcapDotNet ...@@ -72,14 +87,24 @@ namespace PcapDotNet
_callBack = callBack; _callBack = callBack;
} }
[System::Runtime::InteropServices::UnmanagedFunctionPointer(System::Runtime::InteropServices::CallingConvention::Cdecl)]
delegate void Delegate(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData);
void Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData); void Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData);
HandlePacket^ _callBack; HandlePacket^ _callBack;
}; };
ref class StatisticsHandler
{
public:
StatisticsHandler(HandleStatistics^ callBack)
{
_callBack = callBack;
}
void Handle(unsigned char *user, const struct pcap_pkthdr *packetHeader, const unsigned char *packetData);
HandleStatistics^ _callBack;
};
private: private:
pcap_t* _pcapDescriptor; pcap_t* _pcapDescriptor;
IpV4SocketAddress^ _ipV4Netmask; IpV4SocketAddress^ _ipV4Netmask;
......
...@@ -2,16 +2,20 @@ ...@@ -2,16 +2,20 @@
#include "Timestamp.h" #include "Timestamp.h"
#include "PacketHeader.h" #include "PacketHeader.h"
#include "MarshalingServices.h" #include "MarshalingServices.h"
#include "PcapError.h"
#include "Pcap.h" #include "Pcap.h"
using namespace System; using namespace System;
using namespace PcapDotNet; using namespace PcapDotNet;
using namespace BPacket; using namespace BPacket;
PcapDumpFile::PcapDumpFile(pcap_dumper_t* handler, System::String^ filename) PcapDumpFile::PcapDumpFile(pcap_t* pcapDescriptor, System::String^ filename)
{ {
_handler = handler;
_filename = filename; _filename = filename;
std::string unmanagedString = MarshalingServices::ManagedToUnmanagedString(_filename);
_pcapDumper = pcap_dump_open(pcapDescriptor, unmanagedString.c_str());
if (_pcapDumper == NULL)
throw gcnew InvalidOperationException("Error opening output file " + filename + " Error: " + PcapError::GetErrorMessage(pcapDescriptor));
} }
void PcapDumpFile::Dump(Packet^ packet) void PcapDumpFile::Dump(Packet^ packet)
...@@ -21,5 +25,25 @@ void PcapDumpFile::Dump(Packet^ packet) ...@@ -21,5 +25,25 @@ void PcapDumpFile::Dump(Packet^ packet)
std::string unmanagedFilename = MarshalingServices::ManagedToUnmanagedString(_filename); std::string unmanagedFilename = MarshalingServices::ManagedToUnmanagedString(_filename);
pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0]; pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0];
pcap_dump(reinterpret_cast<unsigned char*>(_handler), &header, unamangedPacketBytes); pcap_dump(reinterpret_cast<unsigned char*>(_pcapDumper), &header, unamangedPacketBytes);
}
void PcapDumpFile::Flush()
{
if (pcap_dump_flush(_pcapDumper) != 0)
throw gcnew InvalidOperationException("Failed flusing to file " + _filename);
}
long PcapDumpFile::Position::get()
{
long position = pcap_dump_ftell(_pcapDumper);
if (position == -1)
throw gcnew InvalidOperationException("Failed getting position");
return position;
}
PcapDumpFile::~PcapDumpFile()
{
pcap_dump_close(_pcapDumper);
} }
\ No newline at end of file
...@@ -4,15 +4,24 @@ ...@@ -4,15 +4,24 @@
namespace PcapDotNet namespace PcapDotNet
{ {
public ref class PcapDumpFile public ref class PcapDumpFile : System::IDisposable
{ {
public: public:
PcapDumpFile(pcap_dumper_t* handler, System::String^ filename); PcapDumpFile(pcap_t* pcapDescriptor, System::String^ filename);
void Dump(BPacket::Packet^ packet); void Dump(BPacket::Packet^ packet);
void Flush();
property long Position
{
long get();
}
~PcapDumpFile();
private: private:
pcap_dumper_t* _handler; pcap_dumper_t* _pcapDumper;
System::String^ _filename; System::String^ _filename;
}; };
} }
\ No newline at end of file
#include "PcapError.h"
#include "Pcap.h"
using namespace System;
using namespace System::Text;
using namespace PcapDotNet;
// static
String^ PcapError::GetErrorMessage(pcap_t* pcapDescriptor)
{
return gcnew String(pcap_geterr(pcapDescriptor));
}
// static
InvalidOperationException^ PcapError::BuildInvalidOperation(String^ errorMessage, pcap_t* pcapDescriptor)
{
StringBuilder^ fullError = gcnew StringBuilder(errorMessage);
if (pcapDescriptor != NULL)
{
String^ pcapError = gcnew String(pcap_geterr(pcapDescriptor));
if (!String::IsNullOrEmpty(pcapError))
{
fullError->Append(". ");
fullError->Append(pcapError);
}
}
return gcnew InvalidOperationException(fullError->ToString());
}
\ No newline at end of file
#pragma once
#include "PcapDeclarations.h"
namespace PcapDotNet
{
private ref class PcapError
{
public:
static System::String^ GetErrorMessage(pcap_t* pcapDescriptor);
static System::InvalidOperationException^ BuildInvalidOperation(System::String^ errorMessage, pcap_t* pcapDescriptor);
};
}
\ No newline at end of file
...@@ -16,33 +16,38 @@ List<PcapLiveDevice^>^ PcapLiveDevice::AllLocalMachine::get() ...@@ -16,33 +16,38 @@ List<PcapLiveDevice^>^ PcapLiveDevice::AllLocalMachine::get()
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
// Retrieve the device list from the local machine // Retrieve the device list from the local machine
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL // auth is not needed if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, // auth is not needed
, &alldevs, errbuf) == -1) &alldevs, errbuf) == -1)
{ {
String^ errorString = gcnew String(errbuf); String^ errorString = gcnew String(errbuf);
throw gcnew InvalidOperationException(String::Format("Error in pcap_findalldevs_ex: %s\n", errorString)); throw gcnew InvalidOperationException(String::Format("Failed getting devices. Error: %s", errorString));
} }
List<PcapLiveDevice^>^ result = gcnew List<PcapLiveDevice^>(); try
for (pcap_if_t *d = alldevs; d != NULL; d = d->next)
{ {
// IP addresses List<PcapLiveDevice^>^ result = gcnew List<PcapLiveDevice^>();
List<PcapAddress^>^ addresses = gcnew List<PcapAddress^>(); for (pcap_if_t *d = alldevs; d != NULL; d = d->next)
for (pcap_addr_t *a = d->addresses; a; a = a->next)
{ {
PcapAddress^ deviceAddress = gcnew PcapAddress(a); // IP addresses
addresses->Add(deviceAddress); List<PcapAddress^>^ addresses = gcnew List<PcapAddress^>();
for (pcap_addr_t *a = d->addresses; a; a = a->next)
{
PcapAddress^ deviceAddress = gcnew PcapAddress(a);
addresses->Add(deviceAddress);
}
result->Add(gcnew PcapLiveDevice(gcnew String(d->name),
gcnew String(d->description),
safe_cast<DeviceFlags>(d->flags),
addresses));
} }
return result;
result->Add(gcnew PcapLiveDevice(gcnew String(d->name), }
gcnew String(d->description), finally
safe_cast<DeviceFlags>(d->flags), {
addresses)); // We don't need any more the device list. Free it
pcap_freealldevs(alldevs);
} }
// We don't need any more the device list. Free it
pcap_freealldevs(alldevs);
return result;
} }
String^ PcapLiveDevice::Name::get() String^ PcapLiveDevice::Name::get()
......
#include "PcapSendQueue.h" #include "PcapSendQueue.h"
#include "PacketHeader.h" #include "PacketHeader.h"
#include "PcapError.h"
#include "Pcap.h" #include "Pcap.h"
using namespace System; using namespace System;
...@@ -16,17 +17,15 @@ void PcapSendQueue::Enqueue(Packet^ packet) ...@@ -16,17 +17,15 @@ void PcapSendQueue::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> unamangedPacketBytes = &packet->Buffer[0];
if (pcap_sendqueue_queue(_pcapSendQueue, &pcapHeader, unamangedPacketBytes) == -1) if (pcap_sendqueue_queue(_pcapSendQueue, &pcapHeader, unamangedPacketBytes) != 0)
throw gcnew InvalidOperationException("Failed enqueue packet"); throw gcnew InvalidOperationException("Failed enqueueing to SendQueue");
} }
void PcapSendQueue::Transmit(PcapDeviceHandler^ deviceHandler, bool isSync) void PcapSendQueue::Transmit(PcapDeviceHandler^ deviceHandler, bool isSync)
{ {
unsigned int numBytesTransmitted = pcap_sendqueue_transmit(deviceHandler->Descriptor, _pcapSendQueue, isSync); unsigned int numBytesTransmitted = pcap_sendqueue_transmit(deviceHandler->Descriptor, _pcapSendQueue, isSync);
if (numBytesTransmitted < _pcapSendQueue->len) if (numBytesTransmitted < _pcapSendQueue->len)
{ throw PcapError::BuildInvalidOperation("Failed transmiting packets from queue", deviceHandler->Descriptor);
throw gcnew InvalidOperationException(String::Format("An error occurred sending the packets: %s. Only %d bytes were sent", gcnew String(pcap_geterr(deviceHandler->Descriptor)), numBytesTransmitted));
}
} }
PcapSendQueue::~PcapSendQueue() PcapSendQueue::~PcapSendQueue()
......
...@@ -282,6 +282,14 @@ ...@@ -282,6 +282,14 @@
RelativePath=".\PcapDumpFile.h" RelativePath=".\PcapDumpFile.h"
> >
</File> </File>
<File
RelativePath=".\PcapError.cpp"
>
</File>
<File
RelativePath=".\PcapError.h"
>
</File>
<File <File
RelativePath=".\PcapLiveDevice.cpp" RelativePath=".\PcapLiveDevice.cpp"
> >
......
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