Commit 611dfbbe authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent 4a505ab8
......@@ -24,6 +24,19 @@ namespace Packets
return (ushort)ReadShort(buffer, offset, endianity);
}
public static uint ReadUInt(this byte[] buffer, int offset, Endianity endianity)
{
return (uint)ReadInt(buffer, offset, endianity);
}
public static int ReadInt(this byte[] buffer, int offset, Endianity endianity)
{
int value = ReadInt(buffer, offset);
if (IsWrongEndianity(endianity))
value = IPAddress.HostToNetworkOrder(value);
return value;
}
public static void Write(this byte[] buffer, int offset, short value, Endianity endianity)
{
if (IsWrongEndianity(endianity))
......@@ -52,6 +65,17 @@ namespace Packets
}
}
private static int ReadInt(byte[] buffer, int offset)
{
unsafe
{
fixed (byte* ptr = &buffer[offset])
{
return *((int*)ptr);
}
}
}
private static void Write(byte[] buffer, int offset, short value)
{
unsafe
......
......@@ -57,6 +57,19 @@ namespace Packets
return Equals(obj as Packet);
}
public override int GetHashCode()
{
int hashCode = 0;
int offset = 0;
for (; offset < _data.Length - 3; offset += 4)
hashCode ^= _data.ReadInt(offset, Endianity.Small);
if (offset < _data.Length - 1)
hashCode ^= _data.ReadShort(offset, Endianity.Small);
if (offset < _data.Length)
hashCode ^= _data[offset] >> 2;
return hashCode;
}
private readonly byte[] _data;
private readonly DateTime _timestamp;
private readonly IDataLink _dataLink;
......
......@@ -76,7 +76,7 @@ namespace PcapDotNet.Core.Test
const string DestinationMac = "77:88:99:AA:BB:CC";
List<Packet> packetsToSend;
using (PacketSendQueue queue = BuildQueue(out packetsToSend, 100, 100, SourceMac, DestinationMac, 0.5))
using (PacketSendBuffer queue = BuildQueue(out packetsToSend, 100, 100, SourceMac, DestinationMac, 0.5))
{
using (PacketCommunicator communicator = OfflinePacketDeviceTests.OpenOfflineDevice())
{
......@@ -92,7 +92,7 @@ namespace PcapDotNet.Core.Test
const string DestinationMac = "77:88:99:AA:BB:CC";
List<Packet> packetsToSend;
using (PacketSendQueue queue = BuildQueue(out packetsToSend, numPacketsToSend, packetSize, SourceMac, DestinationMac, secondsBetweenTimestamps))
using (PacketSendBuffer queue = BuildQueue(out packetsToSend, numPacketsToSend, packetSize, SourceMac, DestinationMac, secondsBetweenTimestamps))
{
using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
{
......@@ -137,11 +137,11 @@ namespace PcapDotNet.Core.Test
}
}
private static PacketSendQueue BuildQueue(out List<Packet> packetsToSend, int numPackets, int packetSize, string sourceMac, string destinationMac, double secondsBetweenTimestamps)
private static PacketSendBuffer BuildQueue(out List<Packet> packetsToSend, int numPackets, int packetSize, string sourceMac, string destinationMac, double secondsBetweenTimestamps)
{
int rawPacketSize = packetSize + 16; // I don't know why 16
PacketSendQueue queue = new PacketSendQueue((uint)(numPackets * rawPacketSize));
PacketSendBuffer queue = new PacketSendBuffer((uint)(numPackets * rawPacketSize));
try
{
DateTime timestamp = DateTime.Now.AddSeconds(-100);
......
<?xml version="1.0" encoding="utf-8" ?>
<Dictionary>
<Words>
<Unrecognized>
<!--Word>cb</Word-->
</Unrecognized>
<Recognized>
<Word>netmask</Word>
<Word>pcap</Word>
<Word>eof</Word>
<Word>netmask</Word>
</Recognized>
<Deprecated>
<!--Term PreferredAlternate="EnterpriseServices">complus</Term-->
</Deprecated>
<Compound>
<!--Term CompoundAlternate="DataStore">datastore</Term-->
</Compound>
<DiscreteExceptions>
<!--Term>netmask</Term-->
</DiscreteExceptions>
</Words>
<Acronyms>
<CasingExceptions>
<!-->Acronym>Eof</Acronym-->
<Acronym>Ip</Acronym>
<Acronym>Ms</Acronym>
</CasingExceptions>
</Acronyms>
</Dictionary>
\ No newline at end of file
......@@ -25,7 +25,7 @@ PacketTotalStatistics^ LivePacketCommunicator::TotalStatistics::get()
return gcnew PacketTotalStatistics(packetsReceived, packetsDroppedByDriver, packetsDroppedByInterface, packetsCaptured);
}
void LivePacketCommunicator::Transmit(PacketSendQueue^ sendQueue, bool isSync)
void LivePacketCommunicator::Transmit(PacketSendBuffer^ sendBuffer, bool isSync)
{
sendQueue->Transmit(PcapDescriptor, isSync);
sendBuffer->Transmit(PcapDescriptor, isSync);
}
......@@ -15,7 +15,7 @@ namespace PcapDotNet { namespace Core
PacketTotalStatistics^ get() override;
}
virtual void Transmit(PacketSendQueue^ sendQueue, bool isSync) override;
virtual void Transmit(PacketSendBuffer^ sendBuffer, bool isSync) override;
internal:
LivePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth,
......
......@@ -13,7 +13,7 @@ OfflinePacketCommunicator::OfflinePacketCommunicator(const char* source, int sna
{
}
void OfflinePacketCommunicator::Transmit(PacketSendQueue^ sendQueue, bool isSync)
void OfflinePacketCommunicator::Transmit(PacketSendBuffer^ sendBuffer, bool isSync)
{
throw gcnew InvalidOperationException("Can't transmit queue to an offline device");
}
......@@ -13,7 +13,7 @@ namespace PcapDotNet { namespace Core
PacketTotalStatistics^ get() override;
}
virtual void Transmit(PacketSendQueue^ sendQueue, bool isSync) override;
virtual void Transmit(PacketSendBuffer^ sendBuffer, bool isSync) override;
internal:
OfflinePacketCommunicator(const char* source, int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout, pcap_rmtauth* auth);
......
......@@ -122,13 +122,13 @@ void PacketCommunicator::NonBlocking::set(bool value)
void PacketCommunicator::SetKernelBufferSize(int size)
{
if (pcap_setbuff(_pcapDescriptor, size) != 0)
throw BuildInvalidOperation("Error setting kernel buffer size to " + size.ToString());
throw BuildInvalidOperation("Error setting kernel buffer size to " + size.ToString(CultureInfo::InvariantCulture));
}
void PacketCommunicator::SetKernelMinimumBytesToCopy(int size)
{
if (pcap_setmintocopy(_pcapDescriptor, size) != 0)
throw BuildInvalidOperation("Error setting kernel minimum bytes to copy to " + size.ToString());
throw BuildInvalidOperation("Error setting kernel minimum bytes to copy to " + size.ToString(CultureInfo::InvariantCulture));
}
void PacketCommunicator::SetSamplingMethod(SamplingMethod^ method)
......@@ -156,7 +156,7 @@ PacketCommunicatorReceiveResult PacketCommunicator::ReceivePacket([Out] Packet^%
return result;
}
PacketCommunicatorReceiveResult PacketCommunicator::ReceiveSomePackets([Out] int% numPacketsGot, int maxPackets, HandlePacket^ callBack)
PacketCommunicatorReceiveResult PacketCommunicator::ReceiveSomePackets([Out] int% countGot, int maxPackets, HandlePacket^ callBack)
{
AssertMode(PacketCommunicatorMode::Capture);
......@@ -164,22 +164,22 @@ PacketCommunicatorReceiveResult PacketCommunicator::ReceiveSomePackets([Out] int
HandlerDelegate^ packetHandlerDelegate = gcnew HandlerDelegate(packetHandler, &PacketHandler::Handle);
pcap_handler functionPointer = (pcap_handler)Marshal::GetFunctionPointerForDelegate(packetHandlerDelegate).ToPointer();
numPacketsGot = pcap_dispatch(_pcapDescriptor,
countGot = pcap_dispatch(_pcapDescriptor,
maxPackets,
functionPointer,
NULL);
switch (numPacketsGot)
switch (countGot)
{
case -2:
numPacketsGot = 0;
countGot = 0;
return PacketCommunicatorReceiveResult::BreakLoop;
case -1:
throw BuildInvalidOperation("Failed reading from device");
case 0:
if (packetHandler->PacketCounter != 0)
{
numPacketsGot = packetHandler->PacketCounter;
countGot = packetHandler->PacketCounter;
return PacketCommunicatorReceiveResult::Eof;
}
}
......
......@@ -76,7 +76,7 @@ namespace PcapDotNet { namespace Core
delegate void HandlePacket(Packets::Packet^ packet);
PacketCommunicatorReceiveResult ReceivePacket([System::Runtime::InteropServices::Out] Packets::Packet^% packet);
PacketCommunicatorReceiveResult ReceiveSomePackets([System::Runtime::InteropServices::Out] int% numPacketsGot, int maxPackets, HandlePacket^ callback);
PacketCommunicatorReceiveResult ReceiveSomePackets([System::Runtime::InteropServices::Out] int% countGot, int maxPackets, HandlePacket^ callback);
PacketCommunicatorReceiveResult ReceivePackets(int count, HandlePacket^ callback);
delegate void HandleStatistics(PacketSampleStatistics^ statistics);
......@@ -86,7 +86,7 @@ namespace PcapDotNet { namespace Core
void Break();
void SendPacket(Packets::Packet^ packet);
virtual void Transmit(PacketSendQueue^ sendQueue, bool isSync) = 0;
virtual void Transmit(PacketSendBuffer^ sendBuffer, bool isSync) = 0;
BerkeleyPacketFilter^ CreateFilter(System::String^ filterValue);
void SetFilter(BerkeleyPacketFilter^ filter);
......
......@@ -32,5 +32,8 @@ namespace PcapDotNet { namespace Core
virtual PacketCommunicator^ Open(int snapshotLength, PacketDeviceOpenFlags flags, int readTimeout) = 0;
virtual PacketCommunicator^ Open();
protected:
PacketDevice(){}
};
}}
\ No newline at end of file
......@@ -8,5 +8,8 @@ namespace PcapDotNet { namespace Core
{
public:
static void GetPcapHeader(pcap_pkthdr &header, Packets::Packet^ packet);
private:
PacketHeader(){}
};
}}
\ No newline at end of file
......@@ -7,12 +7,12 @@ using namespace System;
using namespace PcapDotNet::Core;
using namespace Packets;
PacketSendQueue::PacketSendQueue(unsigned int capacity)
PacketSendBuffer::PacketSendBuffer(unsigned int capacity)
{
_pcapSendQueue = pcap_sendqueue_alloc(capacity);
}
void PacketSendQueue::Enqueue(Packet^ packet)
void PacketSendBuffer::Enqueue(Packet^ packet)
{
pcap_pkthdr pcapHeader;
PacketHeader::GetPcapHeader(pcapHeader, packet);
......@@ -21,12 +21,12 @@ void PacketSendQueue::Enqueue(Packet^ packet)
throw gcnew InvalidOperationException("Failed enqueueing to SendQueue");
}
PacketSendQueue::~PacketSendQueue()
PacketSendBuffer::~PacketSendBuffer()
{
pcap_sendqueue_destroy(_pcapSendQueue);
}
void PacketSendQueue::Transmit(pcap_t* pcapDescriptor, bool isSync)
void PacketSendBuffer::Transmit(pcap_t* pcapDescriptor, bool isSync)
{
unsigned int numBytesTransmitted = pcap_sendqueue_transmit(pcapDescriptor, _pcapSendQueue, isSync);
if (numBytesTransmitted < _pcapSendQueue->len)
......
......@@ -4,14 +4,14 @@
namespace PcapDotNet { namespace Core
{
public ref class PacketSendQueue : System::IDisposable
public ref class PacketSendBuffer : System::IDisposable
{
public:
PacketSendQueue(unsigned int capacity);
PacketSendBuffer(unsigned int capacity);
void Enqueue(Packets::Packet^ packet);
~PacketSendQueue();
~PacketSendBuffer();
internal:
void Transmit(pcap_t* pcapDescriptor, bool isSync);
......
......@@ -43,9 +43,18 @@ bool PacketTotalStatistics::Equals(PacketTotalStatistics^ other)
PacketsCaptured == other->PacketsCaptured);
}
bool PacketTotalStatistics::Equals(Object^ other)
bool PacketTotalStatistics::Equals(Object^ obj)
{
return Equals(dynamic_cast<PacketTotalStatistics^>(other));
return Equals(dynamic_cast<PacketTotalStatistics^>(obj));
}
int PacketTotalStatistics::GetHashCode()
{
return
_packetsReceived ^
_packetsDroppedByDriver ^
_packetsDroppedByInterface ^
_packetsCaptured;
}
String^ PacketTotalStatistics::ToString()
......
......@@ -28,7 +28,9 @@ namespace PcapDotNet { namespace Core
}
virtual bool Equals(PacketTotalStatistics^ other);
virtual bool Equals(System::Object^ other) override;
virtual bool Equals(System::Object^ obj) override;
virtual int GetHashCode() override;
virtual System::String^ ToString() override;
......
......@@ -6,6 +6,7 @@
#include "Pcap.h"
using namespace System;
using namespace System::Globalization;
using namespace Packets;
using namespace PcapDotNet::Core;
......@@ -36,7 +37,7 @@ DataLinkKind PcapDataLink::Kind::get()
case 1:
return DataLinkKind::Ethernet;
default:
throw gcnew NotSupportedException("PcapDataLink " + Value.ToString() + " - " + ToString() + " is unsupported");
throw gcnew NotSupportedException("PcapDataLink " + Value.ToString(CultureInfo::InvariantCulture) + " - " + ToString() + " is unsupported");
}
}
......@@ -50,7 +51,7 @@ String^ PcapDataLink::Name::get()
{
const char* name = pcap_datalink_val_to_name(Value);
if (name == NULL)
throw gcnew ArgumentException("datalink " + Value.ToString() + " has no name", "Value");
throw gcnew InvalidOperationException("datalink " + Value.ToString(CultureInfo::InvariantCulture) + " has no name");
return gcnew String(name);
}
......@@ -59,18 +60,49 @@ String^ PcapDataLink::Description::get()
{
const char* description = pcap_datalink_val_to_description(Value);
if (description == NULL)
throw gcnew ArgumentException("datalink " + Value.ToString() + " has no description", "Value");
throw gcnew InvalidOperationException("datalink " + Value.ToString(CultureInfo::InvariantCulture) + " has no description");
return gcnew String(description);
}
String^ PcapDataLink::ToString()
{
return Name + " (" + Description + ")";
}
bool PcapDataLink::Equals(PcapDataLink other)
{
return _value == other._value;
}
bool PcapDataLink::Equals(System::Object^ obj)
{
PcapDataLink^ other = dynamic_cast<PcapDataLink^>(obj);
if (other == nullptr)
return false;
return Equals((PcapDataLink)other);
}
int PcapDataLink::GetHashCode()
{
return Value.GetHashCode();
}
// static
bool PcapDataLink::operator ==(PcapDataLink dataLink1, PcapDataLink dataLink2)
{
return dataLink1.Equals(dataLink2);
}
// static
bool PcapDataLink::operator !=(PcapDataLink dataLink1, PcapDataLink dataLink2)
{
return !dataLink1.Equals(dataLink2);
}
// private
int PcapDataLink::KindToValue(DataLinkKind kind)
{
switch (kind)
......
......@@ -2,7 +2,7 @@
namespace PcapDotNet { namespace Core
{
public value class PcapDataLink : Packets::IDataLink
public value class PcapDataLink : Packets::IDataLink, System::IEquatable<PcapDataLink>
{
public:
PcapDataLink(Packets::DataLinkKind kind);
......@@ -31,6 +31,15 @@ namespace PcapDotNet { namespace Core
virtual System::String^ ToString() override;
virtual bool Equals(PcapDataLink other);
virtual bool Equals(System::Object^ obj) override;
virtual int GetHashCode() override;
static bool operator ==(PcapDataLink dataLink1, PcapDataLink dataLink2);
static bool operator !=(PcapDataLink dataLink1, PcapDataLink dataLink2);
private:
static int KindToValue(Packets::DataLinkKind kind);
......
......@@ -9,5 +9,8 @@ namespace PcapDotNet { namespace Core
public:
static System::String^ GetErrorMessage(pcap_t* pcapDescriptor);
static System::InvalidOperationException^ BuildInvalidOperation(System::String^ errorMessage, pcap_t* pcapDescriptor);
private:
PcapError(){}
};
}}
\ No newline at end of file
......@@ -2,12 +2,15 @@
namespace PcapDotNet { namespace Core
{
public ref class PcapLibrary
public ref class PcapLibrary sealed
{
public:
static property System::String^ Version
{
System::String^ get();
}
private:
PcapLibrary(){}
};
}}
\ No newline at end of file
......@@ -14,5 +14,8 @@ namespace PcapDotNet { namespace Core
{
int get() = 0;
}
protected:
SamplingMethod(){}
};
}}
\ No newline at end of file
......@@ -16,7 +16,8 @@ namespace PcapDotNet { namespace Core
OSI = ISO, // OSI is ISO
ECMA = 8, // european computer manufacturers
DATAKIT = 9, // datakit protocols
CCITT = 10, // CCITT protocols, X.25 etc
CCITT = 10,
// CCITT protocols, X.25 etc
SNA = 11, // IBM SNA
DECnet = 12, // DECnet
DLI = 13, // Direct data link interface
......
......@@ -9,5 +9,8 @@ namespace PcapDotNet { namespace Core
public:
static void PcapTimestampToDateTime(const timeval& pcapTimestamp, [System::Runtime::InteropServices::Out] System::DateTime% dateTime);
static void DateTimeToPcapTimestamp(System::DateTime dateTime, timeval& pcapTimestamp);
private:
Timestamp(){}
};
}}
\ No newline at end of file
......@@ -92,6 +92,7 @@
/>
<Tool
Name="VCFxCopTool"
Rules="-Microsoft.Design#CA1021;-Microsoft.Design#CA1028;-Microsoft.Design#CA1027"
EnableFxCop="true"
Dictionaries="CodeAnalysisDictionary.xml"
/>
......
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