Commit 843c5d44 authored by Brickner_cp's avatar Brickner_cp

Better errors in Core

parent 9ab19972
...@@ -5,6 +5,7 @@ using System.Linq; ...@@ -5,6 +5,7 @@ using System.Linq;
using PcapDotNet.Core; using PcapDotNet.Core;
using PcapDotNet.Core.Extensions; using PcapDotNet.Core.Extensions;
using PcapDotNet.Packets; using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4; using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport; using PcapDotNet.Packets.Transport;
...@@ -70,15 +71,22 @@ namespace UsingLinq ...@@ -70,15 +71,22 @@ namespace UsingLinq
// start the capture // start the capture
var query = from packet in communicator.ReceivePackets() var query = from packet in communicator.ReceivePackets()
where !packet.IsValid // where !packet.IsValid
select packet; select packet;
using (PacketDumpFile dumpFile = communicator.OpenDump("dump.pcap")) using (PacketDumpFile dumpFile = communicator.OpenDump("dump.pcap"))
{ {
foreach (Packet packet in query) foreach (Packet packet in query)
{ {
Console.WriteLine("Captured Packet " + packet.Timestamp); if (packet.Length <= 60 &&
dumpFile.Dump(packet); packet.Ethernet.EtherType == EthernetType.IpV4 &&
packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp &&
packet.Ethernet.IpV4.Tcp.ControlBits == (TcpControlBits.Synchronize | TcpControlBits.Acknowledgment))
{
Console.WriteLine("Captured Packet " + packet.Timestamp);
}
// dumpFile.Dump(packet);
} }
} }
} }
......
...@@ -297,7 +297,7 @@ namespace PcapDotNet.Core.Test ...@@ -297,7 +297,7 @@ namespace PcapDotNet.Core.Test
TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, NumStatisticsToGather, 0, 5, PacketSize, TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, NumStatisticsToGather, 0, 5, PacketSize,
PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0, 0.04); PacketCommunicatorReceiveResult.BreakLoop, 0, 0, 0, 0.04);
TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, NumStatisticsToGather, NumStatisticsToGather / 2, 5, PacketSize, TestGetStatistics(SourceMac, DestinationMac, NumPacketsToSend, NumStatisticsToGather, NumStatisticsToGather / 2, 5, PacketSize,
PacketCommunicatorReceiveResult.BreakLoop, NumStatisticsToGather / 2, NumPacketsToSend, NumStatisticsToGather / 2, NumStatisticsToGather / 2 + 0.04); PacketCommunicatorReceiveResult.BreakLoop, NumStatisticsToGather / 2, NumPacketsToSend, NumStatisticsToGather / 2, NumStatisticsToGather / 2 + 0.1);
} }
[TestMethod] [TestMethod]
......
...@@ -16,13 +16,13 @@ using namespace PcapDotNet::Core; ...@@ -16,13 +16,13 @@ using namespace PcapDotNet::Core;
ReadOnlyCollection<LivePacketDevice^>^ LivePacketDevice::AllLocalMachine::get() ReadOnlyCollection<LivePacketDevice^>^ LivePacketDevice::AllLocalMachine::get()
{ {
pcap_if_t *alldevs; pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE]; char errorBuffer[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, errorBuffer) == -1)
{ {
String^ errorString = gcnew String(errbuf); String^ errorString = gcnew String(errorBuffer);
throw gcnew InvalidOperationException(String::Format(CultureInfo::InvariantCulture, "Failed getting devices. Error: {0}", errorString)); throw gcnew InvalidOperationException(String::Format(CultureInfo::InvariantCulture, "Failed getting devices. Error: {0}", errorString));
} }
......
...@@ -26,12 +26,3 @@ array<Byte>^ MarshalingServices::UnamangedToManagedByteArray(const unsigned char ...@@ -26,12 +26,3 @@ array<Byte>^ MarshalingServices::UnamangedToManagedByteArray(const unsigned char
Marshal::Copy((IntPtr)const_cast<unsigned char*>(unmanagedByteArray), managedBytes, offset, count); Marshal::Copy((IntPtr)const_cast<unsigned char*>(unmanagedByteArray), managedBytes, offset, count);
return managedBytes; return managedBytes;
} }
/*
// static
System::String^ MarshalingServices::UnmangedToManagedString(const std::string& unmanagedString)
{
array<Byte>^ managedBytes = UnamangedToManagedByteArray(reinterpret_cast<const unsigned char*>(unmanagedString.c_str()), 0, (int)unmanagedString.length());
return System::Text::Encoding::ASCII->GetString(managedBytes);
}
*/
\ No newline at end of file
...@@ -10,7 +10,6 @@ namespace PcapDotNet { namespace Core ...@@ -10,7 +10,6 @@ namespace PcapDotNet { namespace Core
static std::string ManagedToUnmanagedString(System::String^ managedString); static std::string ManagedToUnmanagedString(System::String^ managedString);
static array<System::Byte>^ UnamangedToManagedByteArray(const unsigned char* unmanagedByteArray, int offset, int count); static array<System::Byte>^ UnamangedToManagedByteArray(const unsigned char* unmanagedByteArray, int offset, int count);
// static System::String^ UnmangedToManagedString(const std::string& unmanagedString);
private: private:
[System::Diagnostics::DebuggerNonUserCode] [System::Diagnostics::DebuggerNonUserCode]
......
...@@ -42,16 +42,16 @@ PacketCommunicator^ OfflinePacketDevice::Open(int snapshotLength, PacketDeviceOp ...@@ -42,16 +42,16 @@ PacketCommunicator^ OfflinePacketDevice::Open(int snapshotLength, PacketDeviceOp
// Create the source string according to the new WinPcap syntax // Create the source string according to the new WinPcap syntax
char source[PCAP_BUF_SIZE]; char source[PCAP_BUF_SIZE];
char errbuf[PCAP_ERRBUF_SIZE]; char errorBuffer[PCAP_ERRBUF_SIZE];
if (pcap_createsrcstr(source, // variable that will keep the source string if (pcap_createsrcstr(source, // variable that will keep the source string
PCAP_SRC_FILE, // we want to open a file PCAP_SRC_FILE, // we want to open a file
NULL, // remote host NULL, // remote host
NULL, // port on the remote host NULL, // port on the remote host
unamangedFilename.c_str(), // name of the file we want to open unamangedFilename.c_str(), // name of the file we want to open
errbuf // error buffer errorBuffer // error buffer
) != 0) ) != 0)
{ {
throw gcnew InvalidOperationException("Error creating a source string from filename " + _fileName + " Error: " + gcnew String(errbuf)); throw gcnew InvalidOperationException("Error creating a source string from filename " + _fileName + " Error: " + gcnew String(errorBuffer));
} }
return gcnew OfflinePacketCommunicator(source, snapshotLength, attributes, readTimeout, NULL); return gcnew OfflinePacketCommunicator(source, snapshotLength, attributes, readTimeout, NULL);
......
...@@ -90,8 +90,8 @@ void PacketCommunicator::Mode::set(PacketCommunicatorMode value) ...@@ -90,8 +90,8 @@ void PacketCommunicator::Mode::set(PacketCommunicatorMode value)
bool PacketCommunicator::NonBlocking::get() bool PacketCommunicator::NonBlocking::get()
{ {
char errbuf[PCAP_ERRBUF_SIZE]; char errorBuffer[PCAP_ERRBUF_SIZE];
int nonBlockValue = pcap_getnonblock(_pcapDescriptor, errbuf); int nonBlockValue = pcap_getnonblock(_pcapDescriptor, errorBuffer);
if (nonBlockValue == -1) if (nonBlockValue == -1)
throw BuildInvalidOperation("Error getting NonBlocking value"); throw BuildInvalidOperation("Error getting NonBlocking value");
return nonBlockValue != 0; return nonBlockValue != 0;
...@@ -99,8 +99,8 @@ bool PacketCommunicator::NonBlocking::get() ...@@ -99,8 +99,8 @@ bool PacketCommunicator::NonBlocking::get()
void PacketCommunicator::NonBlocking::set(bool value) void PacketCommunicator::NonBlocking::set(bool value)
{ {
char errbuf[PCAP_ERRBUF_SIZE]; char errorBuffer[PCAP_ERRBUF_SIZE];
if (pcap_setnonblock(_pcapDescriptor, value, errbuf) != 0) if (pcap_setnonblock(_pcapDescriptor, value, errorBuffer) != 0)
throw BuildInvalidOperation("Error setting NonBlocking to " + value.ToString()); throw BuildInvalidOperation("Error setting NonBlocking to " + value.ToString());
} }
...@@ -284,20 +284,17 @@ PacketCommunicator::~PacketCommunicator() ...@@ -284,20 +284,17 @@ PacketCommunicator::~PacketCommunicator()
PacketCommunicator::PacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenAttributes attributes, int readTimeout, pcap_rmtauth *auth, SocketAddress^ netmask) PacketCommunicator::PacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenAttributes attributes, int readTimeout, pcap_rmtauth *auth, SocketAddress^ netmask)
{ {
// Open the device // Open the device
char errbuf[PCAP_ERRBUF_SIZE]; char errorBuffer[PCAP_ERRBUF_SIZE];
pcap_t *pcapDescriptor = pcap_open(source, // name of the device pcap_t *pcapDescriptor = pcap_open(source, // name of the device
snapshotLength, // portion of the packet to capture snapshotLength, // 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>(attributes), safe_cast<int>(attributes),
readTimeout, // read timeout readTimeout, // read timeout
auth, // authentication on the remote machine auth, // authentication on the remote machine
errbuf); // error buffer errorBuffer); // error buffer
if (pcapDescriptor == NULL) if (pcapDescriptor == NULL)
{ throw gcnew InvalidOperationException("Unable to open the adapter. Adapter name: " + gcnew String(source) + ". WinPcap Error: " + gcnew String(errorBuffer));
throw gcnew InvalidOperationException("Unable to open the adapter. " + gcnew String(source) + " is not supported by WinPcap");
// throw gcnew InvalidOperationException("Unable to open the adapter. " + gcnew String(source) + " is not supported by WinPcap. Error: " + MarshalingServices::UnmangedToManagedString(std::string(errbuf)));
}
_pcapDescriptor = pcapDescriptor; _pcapDescriptor = pcapDescriptor;
_ipV4Netmask = dynamic_cast<IpV4SocketAddress^>(netmask); _ipV4Netmask = dynamic_cast<IpV4SocketAddress^>(netmask);
......
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