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
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);
int numPacketsGot;
......@@ -94,8 +94,6 @@ namespace WinPcapDotNet.Console
break;
case DeviceHandlerResult.Timeout:
continue;
case DeviceHandlerResult.Error:
throw new InvalidOperationException("Failed reading from device");
case DeviceHandlerResult.Eof:
continue;
}
......@@ -122,8 +120,6 @@ namespace WinPcapDotNet.Console
break;
case DeviceHandlerResult.Timeout:
continue;
case DeviceHandlerResult.Error:
throw new InvalidOperationException("Failed reading from device");
case DeviceHandlerResult.Eof:
continue;
}
......@@ -156,8 +152,6 @@ namespace WinPcapDotNet.Console
break;
case DeviceHandlerResult.Timeout:
continue;
case DeviceHandlerResult.Error:
throw new InvalidOperationException("Failed reading from device");
case DeviceHandlerResult.Eof:
continue;
}
......@@ -184,8 +178,6 @@ namespace WinPcapDotNet.Console
break;
case DeviceHandlerResult.Timeout:
continue;
case DeviceHandlerResult.Error:
throw new InvalidOperationException("Failed reading from device");
case DeviceHandlerResult.Eof:
continue;
}
......
#include "BpfFilter.h"
#include "Pcap.h"
#include "MarshalingServices.h"
#include "PcapError.h"
using namespace System;
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);
......@@ -17,9 +18,9 @@ BpfFilter::BpfFilter(pcap_t* handler, String^ filterString, IpV4SocketAddress^ n
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(...)
......@@ -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
public ref class BpfFilter : System::IDisposable
{
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
{
bpf_program& get();
}
~BpfFilter(); // IDisposable
private:
bpf_program* _bpf;
......
......@@ -3,6 +3,7 @@
#include "MarshalingServices.h"
#include "PcapDumpFile.h"
#include "Timestamp.h"
#include "PcapError.h"
#include "Pcap.h"
using namespace System;
......@@ -47,8 +48,7 @@ void PcapDeviceHandler::Mode::set(DeviceHandlerMode value)
DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet)
{
if (Mode != DeviceHandlerMode::Capture)
throw gcnew InvalidOperationException("Must be in capture mode to get packets");
AssertMode(DeviceHandlerMode::Capture);
pcap_pkthdr* packetHeader;
const unsigned char* packetData;
......@@ -66,36 +66,18 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet)
DeviceHandlerResult PcapDeviceHandler::GetNextPackets(int maxPackets, HandlePacket^ callBack, [Out] int% numPacketsGot)
{
if (Mode != DeviceHandlerMode::Capture)
throw gcnew InvalidOperationException("Must be in capture mode to get packets");
AssertMode(DeviceHandlerMode::Capture);
PacketHandler^ packetHandler = gcnew PacketHandler(callBack);
PacketHandler::Delegate^ packetHandlerDelegate = gcnew PacketHandler::Delegate(packetHandler,
HandlerDelegate^ packetHandlerDelegate = gcnew HandlerDelegate(packetHandler,
&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)
{
if (Mode != DeviceHandlerMode::Statistics)
throw gcnew InvalidOperationException("Must be in statistics mode to get statistics");
AssertMode(DeviceHandlerMode::Statistics);
pcap_pkthdr* packetHeader;
const unsigned char* packetData;
......@@ -107,14 +89,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextStatistics([Out] PcapStatistics^%
return result;
}
timeval pcapTimestamp = packetHeader->ts;
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);
statistics = CreateStatistics(*packetHeader, packetData);
return result;
}
......@@ -123,9 +98,7 @@ void PcapDeviceHandler::SendPacket(Packet^ packet)
{
pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0];
if (pcap_sendpacket(_pcapDescriptor, unamangedPacketBytes, packet->Length) != 0)
{
throw gcnew InvalidOperationException("Failed sending packet");
}
throw BuildInvalidOperation("Failed writing to device");
}
BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString)
......@@ -135,10 +108,7 @@ BpfFilter^ PcapDeviceHandler::CreateFilter(String^ filterString)
void PcapDeviceHandler::SetFilter(BpfFilter^ filter)
{
if (pcap_setfilter(_pcapDescriptor, &filter->Bpf) < 0)
{
throw gcnew InvalidOperationException("Error setting the filter: " + gcnew String(pcap_geterr(_pcapDescriptor)));
}
filter->SetFilter(_pcapDescriptor);
}
void PcapDeviceHandler::SetFilter(String^ filterString)
......@@ -156,11 +126,7 @@ void PcapDeviceHandler::SetFilter(String^ filterString)
PcapDumpFile^ PcapDeviceHandler::OpenDump(System::String^ filename)
{
std::string unmanagedString = MarshalingServices::ManagedToUnmanagedString(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);
return gcnew PcapDumpFile(_pcapDescriptor, filename);
}
PcapDeviceHandler::~PcapDeviceHandler()
......@@ -183,6 +149,18 @@ Packet^ PcapDeviceHandler::CreatePacket(const pcap_pkthdr& packetHeader, const u
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)
{
int result = pcap_next_ex(_pcapDescriptor, packetHeader, packetData);
......@@ -191,17 +169,60 @@ DeviceHandlerResult PcapDeviceHandler::RunPcapNextEx(pcap_pkthdr** packetHeader,
case -2:
return DeviceHandlerResult::Eof;
case -1:
return DeviceHandlerResult::Error;
throw PcapError::BuildInvalidOperation("Failed reading from device", _pcapDescriptor);
case 0:
return DeviceHandlerResult::Timeout;
case 1:
return DeviceHandlerResult::Ok;
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)
{
_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
{
Ok, // if the packet has been read without problems
Timeout, // if the timeout set with Open() has elapsed.
Error, // if an error occurred
Eof, // if EOF was reached reading from an offline capture
BreakLoop //
};
......@@ -38,9 +37,10 @@ namespace PcapDotNet
}
delegate void HandlePacket(BPacket::Packet^ packet);
DeviceHandlerResult GetNextPacket([System::Runtime::InteropServices::Out] BPacket::Packet^% packet);
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);
void SendPacket(BPacket::Packet^ packet);
......@@ -61,9 +61,24 @@ namespace PcapDotNet
private:
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);
[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
{
public:
......@@ -72,14 +87,24 @@ namespace PcapDotNet
_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);
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:
pcap_t* _pcapDescriptor;
IpV4SocketAddress^ _ipV4Netmask;
......
......@@ -2,16 +2,20 @@
#include "Timestamp.h"
#include "PacketHeader.h"
#include "MarshalingServices.h"
#include "PcapError.h"
#include "Pcap.h"
using namespace System;
using namespace PcapDotNet;
using namespace BPacket;
PcapDumpFile::PcapDumpFile(pcap_dumper_t* handler, System::String^ filename)
PcapDumpFile::PcapDumpFile(pcap_t* pcapDescriptor, System::String^ filename)
{
_handler = handler;
_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)
......@@ -21,5 +25,25 @@ void PcapDumpFile::Dump(Packet^ packet)
std::string unmanagedFilename = MarshalingServices::ManagedToUnmanagedString(_filename);
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 @@
namespace PcapDotNet
{
public ref class PcapDumpFile
public ref class PcapDumpFile : System::IDisposable
{
public:
PcapDumpFile(pcap_dumper_t* handler, System::String^ filename);
PcapDumpFile(pcap_t* pcapDescriptor, System::String^ filename);
void Dump(BPacket::Packet^ packet);
void Flush();
property long Position
{
long get();
}
~PcapDumpFile();
private:
pcap_dumper_t* _handler;
pcap_dumper_t* _pcapDumper;
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,13 +16,15 @@ List<PcapLiveDevice^>^ PcapLiveDevice::AllLocalMachine::get()
char errbuf[PCAP_ERRBUF_SIZE];
// Retrieve the device list from the local machine
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL // auth is not needed
, &alldevs, errbuf) == -1)
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, // auth is not needed
&alldevs, errbuf) == -1)
{
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));
}
try
{
List<PcapLiveDevice^>^ result = gcnew List<PcapLiveDevice^>();
for (pcap_if_t *d = alldevs; d != NULL; d = d->next)
{
......@@ -39,10 +41,13 @@ List<PcapLiveDevice^>^ PcapLiveDevice::AllLocalMachine::get()
safe_cast<DeviceFlags>(d->flags),
addresses));
}
return result;
}
finally
{
// We don't need any more the device list. Free it
pcap_freealldevs(alldevs);
return result;
}
}
String^ PcapLiveDevice::Name::get()
......
#include "PcapSendQueue.h"
#include "PacketHeader.h"
#include "PcapError.h"
#include "Pcap.h"
using namespace System;
......@@ -16,17 +17,15 @@ void PcapSendQueue::Enqueue(Packet^ packet)
pcap_pkthdr pcapHeader;
PacketHeader::GetPcapHeader(pcapHeader, packet);
pin_ptr<Byte> unamangedPacketBytes = &packet->Buffer[0];
if (pcap_sendqueue_queue(_pcapSendQueue, &pcapHeader, unamangedPacketBytes) == -1)
throw gcnew InvalidOperationException("Failed enqueue packet");
if (pcap_sendqueue_queue(_pcapSendQueue, &pcapHeader, unamangedPacketBytes) != 0)
throw gcnew InvalidOperationException("Failed enqueueing to SendQueue");
}
void PcapSendQueue::Transmit(PcapDeviceHandler^ deviceHandler, bool 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->Descriptor)), numBytesTransmitted));
}
throw PcapError::BuildInvalidOperation("Failed transmiting packets from queue", deviceHandler->Descriptor);
}
PcapSendQueue::~PcapSendQueue()
......
......@@ -282,6 +282,14 @@
RelativePath=".\PcapDumpFile.h"
>
</File>
<File
RelativePath=".\PcapError.cpp"
>
</File>
<File
RelativePath=".\PcapError.h"
>
</File>
<File
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