Commit f50a1591 authored by Brickner_cp's avatar Brickner_cp

--no commit message

--no commit message
parent 387b308a
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using PcapDotNet;
......@@ -16,7 +17,7 @@ namespace WinPcapDotNet.Console
static void Main(string[] args)
{
System.Console.WriteLine("Start");
List<PcapLiveDevice> devices = PcapLiveDevice.AllLocalMachine;
IList<PcapLiveDevice> devices = PcapLiveDevice.AllLocalMachine;
if (devices.Count == 0)
return;
......@@ -65,8 +66,11 @@ namespace WinPcapDotNet.Console
private static void GetNextPackets(IPcapDevice device, string filter)
{
using (PcapDeviceHandler deviceHandler = device.Open(PcapDevice.DefaultSnapLen, PcapDeviceOpenFlags.Promiscuous, 10 * 1000))
using (PcapDeviceHandler deviceHandler = device.Open(PcapDevice.DefaultSnapshotLength, PcapDeviceOpenFlags.Promiscuous, 10 * 1000))
{
System.Console.WriteLine("datalink = " + deviceHandler.DataLink.Name + " description = " + deviceHandler.DataLink.Description);
//foreach (PcapDataLink datalink in deviceHandler.SupportedDataLinks)
// System.Console.WriteLine("supported datalink = " + datalink);
deviceHandler.SetFilter(filter);
int numPacketsGot;
deviceHandler.GetSomePackets(1000,
......
......@@ -17,19 +17,22 @@ namespace PcapDotNet
property System::String^ Name { System::String^ get(); };
property System::String^ Description { System::String^ get(); };
property DeviceFlags^ Flags { DeviceFlags^ get(); };
property System::Collections::Generic::List<PcapAddress^>^ Addresses { System::Collections::Generic::List<PcapAddress^>^ get(); };
property System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ Addresses
{
System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ get();
}
/// <summary>
/// Open a generic source in order to capture / send (WinPcap only) traffic.
/// </summary>
/// <param name="snapLen">length of the packet that has to be retained. For each packet received by the filter, only the first 'snaplen' bytes are stored in the buffer and passed to the user application. For instance, snaplen equal to 100 means that only the first 100 bytes of each packet are stored.</param>
/// <param name="snapshotLength">length of the packet that has to be retained. For each packet received by the filter, only the first 'snapshotLength' bytes are stored in the buffer and passed to the user application. For instance, snaplen equal to 100 means that only the first 100 bytes of each packet are stored.</param>
/// <param name="flags">keeps several flags that can be needed for capturing packets.</param>
/// <param name="flags">read timeout in milliseconds. The read timeout is used to arrange that the read not necessarily return immediately when a packet is seen, but that it waits for some amount of time to allow more packets to arrive and to read multiple packets from the OS kernel in one operation. Not all platforms support a read timeout; on platforms that don't, the read timeout is ignored.</param>
PcapDeviceHandler^ Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout);
PcapDeviceHandler^ Open(int snapshotLength, PcapDeviceOpenFlags flags, int readTimeout);
/// <summary>
/// Open a generic source in order to capture / send (WinPcap only) traffic.
/// Uses maxmimum snaplen (65536), promiscuous mode and 1 second read timeout.
/// Uses maxmimum snapshotLength (65536), promiscuous mode and 1 second read timeout.
/// </summary>
PcapDeviceHandler^ Open();
};
......
#include "PcapDataLink.h"
#include <string>
#include "MarshalingServices.h"
#include "Pcap.h"
using namespace System;
using namespace PcapDotNet;
PcapDataLink::PcapDataLink(int value)
{
_value = value;
}
PcapDataLink::PcapDataLink(String^ name)
{
std::string unmanagedName = MarshalingServices::ManagedToUnmanagedString(name);
int value = pcap_datalink_name_to_val(unmanagedName.c_str());
if (value == -1)
throw gcnew ArgumentException("Invalid datalink name " + name, "name");
_value = value;
}
int PcapDataLink::Value::get()
{
return _value;
}
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");
return gcnew String(name);
}
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");
return gcnew String(description);
}
String^ PcapDataLink::ToString()
{
return Name;
}
#pragma once
namespace PcapDotNet
{
public value class PcapDataLink
{
public:
PcapDataLink(int value);
PcapDataLink(System::String^ name);
property int Value
{
int get();
}
property System::String^ Name
{
System::String^ get();
}
property System::String^ Description
{
System::String^ get();
}
virtual System::String^ ToString() override;
private:
int _value;
};
}
......@@ -4,5 +4,5 @@ using namespace PcapDotNet;
PcapDeviceHandler^ PcapDevice::Open()
{
return Open(DefaultSnapLen, PcapDeviceOpenFlags::Promiscuous, 1000);
return Open(DefaultSnapshotLength, PcapDeviceOpenFlags::Promiscuous, 1000);
}
......@@ -7,7 +7,7 @@ namespace PcapDotNet
public ref class PcapDevice abstract : IPcapDevice
{
public:
static const int DefaultSnapLen = 65536;
static const int DefaultSnapshotLength = 65536;
virtual property System::String^ Name
{
......@@ -24,12 +24,12 @@ namespace PcapDotNet
DeviceFlags^ get() = 0;
}
virtual property System::Collections::Generic::List<PcapAddress^>^ Addresses
virtual property System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ Addresses
{
System::Collections::Generic::List<PcapAddress^>^ get() = 0;
System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ get() = 0;
}
virtual PcapDeviceHandler^ Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout) = 0;
virtual PcapDeviceHandler^ Open(int snapshotLength, PcapDeviceOpenFlags flags, int readTimeout) = 0;
virtual PcapDeviceHandler^ Open();
};
......
......@@ -7,18 +7,20 @@
#include "Pcap.h"
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Collections::ObjectModel;
using namespace System::Collections::Generic;
using namespace BPacket;
using namespace PcapDotNet;
using namespace System::Runtime::InteropServices;
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
PcapDeviceHandler::PcapDeviceHandler(const char* source, int snapLen, PcapDeviceOpenFlags flags, int readTimeout, pcap_rmtauth *auth, SocketAddress^ netmask)
PcapDeviceHandler::PcapDeviceHandler(const char* source, int snapshotLength, PcapDeviceOpenFlags flags, int readTimeout, pcap_rmtauth *auth, SocketAddress^ netmask)
{
// Open the device
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *pcapDescriptor = pcap_open(source, // name of the device
snapLen, // 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
safe_cast<int>(flags),
readTimeout, // read timeout
......@@ -27,13 +29,48 @@ PcapDeviceHandler::PcapDeviceHandler(const char* source, int snapLen, PcapDevice
if (pcapDescriptor == NULL)
{
gcnew InvalidOperationException(String::Format("Unable to open the adapter. %s is not supported by WinPcap", gcnew String(source)));
throw gcnew InvalidOperationException(String::Format("Unable to open the adapter. %s is not supported by WinPcap", gcnew String(source)));
}
_pcapDescriptor = pcapDescriptor;
_ipV4Netmask = dynamic_cast<IpV4SocketAddress^>(netmask);
}
PcapDataLink PcapDeviceHandler::DataLink::get()
{
return PcapDataLink(pcap_datalink(_pcapDescriptor));
}
void PcapDeviceHandler::DataLink::set(PcapDataLink value)
{
if (pcap_set_datalink(_pcapDescriptor, value.Value) == -1)
throw BuildInvalidOperation("Failed setting datalink " + value.ToString());
}
ReadOnlyCollection<PcapDataLink>^ PcapDeviceHandler::SupportedDataLinks::get()
{
throw gcnew NotSupportedException("Supported DataLinks is unsupported to avoid winpcap memory leak");
int* dataLinks;
int numDatalinks = pcap_list_datalinks(_pcapDescriptor, &dataLinks);
if (numDatalinks == -1)
throw BuildInvalidOperation("Failed getting supported datalinks");
try
{
List<PcapDataLink>^ results = gcnew List<PcapDataLink>(numDatalinks);
for (int i = 0; i != numDatalinks; ++i)
results->Add(PcapDataLink(dataLinks[i]));
return gcnew ReadOnlyCollection<PcapDataLink>(results);
}
finally
{
// This doesn't work because of a bug. See http://www.winpcap.org/pipermail/winpcap-users/2008-May/002500.html
// todo look for pcap_free_datalinks()
// free(dataLinks);
}
}
DeviceHandlerMode PcapDeviceHandler::Mode::get()
{
return _mode;
......
......@@ -5,6 +5,7 @@
#include "PcapDumpFile.h"
#include "PcapDeviceOpenFlags.h"
#include "PcapStatistics.h"
#include "PcapDataLink.h"
namespace PcapDotNet
{
......@@ -27,9 +28,20 @@ namespace PcapDotNet
public ref class PcapDeviceHandler : System::IDisposable
{
public:
PcapDeviceHandler(const char* source, int snapLen, PcapDeviceOpenFlags flags, int readTimeout, pcap_rmtauth *auth,
PcapDeviceHandler(const char* source, int snapshotLength, PcapDeviceOpenFlags flags, int readTimeout, pcap_rmtauth *auth,
SocketAddress^ netmask);
property PcapDataLink DataLink
{
PcapDataLink get();
void set(PcapDataLink value);
}
property System::Collections::ObjectModel::ReadOnlyCollection<PcapDataLink>^ SupportedDataLinks
{
System::Collections::ObjectModel::ReadOnlyCollection<PcapDataLink>^ get();
}
property DeviceHandlerMode Mode
{
DeviceHandlerMode get();
......
......@@ -7,10 +7,11 @@
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
using namespace BPacket;
using namespace PcapDotNet;
List<PcapLiveDevice^>^ PcapLiveDevice::AllLocalMachine::get()
ReadOnlyCollection<PcapLiveDevice^>^ PcapLiveDevice::AllLocalMachine::get()
{
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE];
......@@ -39,9 +40,9 @@ List<PcapLiveDevice^>^ PcapLiveDevice::AllLocalMachine::get()
result->Add(gcnew PcapLiveDevice(gcnew String(d->name),
gcnew String(d->description),
safe_cast<DeviceFlags>(d->flags),
addresses));
gcnew ReadOnlyCollection<PcapAddress^>(addresses)));
}
return result;
return gcnew ReadOnlyCollection<PcapLiveDevice^>(result);
}
finally
{
......@@ -65,12 +66,12 @@ DeviceFlags^ PcapLiveDevice::Flags::get()
return _flags;
}
List<PcapAddress^>^ PcapLiveDevice::Addresses::get()
ReadOnlyCollection<PcapAddress^>^ PcapLiveDevice::Addresses::get()
{
return _addresses;
return gcnew ReadOnlyCollection<PcapAddress^>(_addresses);
}
PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout)
PcapDeviceHandler^ PcapLiveDevice::Open(int snapshotLength, PcapDeviceOpenFlags flags, int readTimeout)
{
std::string deviceName = MarshalingServices::ManagedToUnmanagedString(Name);
......@@ -80,12 +81,12 @@ PcapDeviceHandler^ PcapLiveDevice::Open(int snapLen, PcapDeviceOpenFlags flags,
netmask = Addresses[0]->Netmask;
// Open the device
return gcnew PcapDeviceHandler(deviceName.c_str(), snapLen, flags, readTimeout, NULL, netmask);
return gcnew PcapDeviceHandler(deviceName.c_str(), snapshotLength, flags, readTimeout, NULL, netmask);
}
// Private Methods
PcapLiveDevice::PcapLiveDevice(String^ name, String^ description, DeviceFlags^ flags, List<PcapAddress^>^ addresses)
PcapLiveDevice::PcapLiveDevice(String^ name, String^ description, DeviceFlags^ flags, ReadOnlyCollection<PcapAddress^>^ addresses)
{
_name = name;
_description = description;
......
......@@ -7,9 +7,9 @@ namespace PcapDotNet
public ref class PcapLiveDevice : PcapDevice
{
public:
static property System::Collections::Generic::List<PcapLiveDevice^>^ AllLocalMachine
static property System::Collections::ObjectModel::ReadOnlyCollection<PcapLiveDevice^>^ AllLocalMachine
{
System::Collections::Generic::List<PcapLiveDevice^>^ get();
System::Collections::ObjectModel::ReadOnlyCollection<PcapLiveDevice^>^ get();
}
virtual property System::String^ Name
......@@ -27,26 +27,26 @@ namespace PcapDotNet
DeviceFlags^ get() override;
}
virtual property System::Collections::Generic::List<PcapAddress^>^ Addresses
virtual property System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ Addresses
{
System::Collections::Generic::List<PcapAddress^>^ get() override;
System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ get() override;
}
/// <summary>
/// Open a generic source in order to capture / send (WinPcap only) traffic.
/// </summary>
/// <param name="snapLen">length of the packet that has to be retained. For each packet received by the filter, only the first 'snaplen' bytes are stored in the buffer and passed to the user application. For instance, snaplen equal to 100 means that only the first 100 bytes of each packet are stored.</param>
/// <param name="snapshotLength">length of the packet that has to be retained. For each packet received by the filter, only the first 'snapshotLength' bytes are stored in the buffer and passed to the user application. For instance, snaplen equal to 100 means that only the first 100 bytes of each packet are stored.</param>
/// <param name="flags">keeps several flags that can be needed for capturing packets.</param>
/// <param name="flags">read timeout in milliseconds. The read timeout is used to arrange that the read not necessarily return immediately when a packet is seen, but that it waits for some amount of time to allow more packets to arrive and to read multiple packets from the OS kernel in one operation. Not all platforms support a read timeout; on platforms that don't, the read timeout is ignored.</param>
virtual PcapDeviceHandler^ Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout) override;
virtual PcapDeviceHandler^ Open(int snapshotLength, PcapDeviceOpenFlags flags, int readTimeout) override;
private:
PcapLiveDevice(System::String^ name, System::String^ description, DeviceFlags^ flags, System::Collections::Generic::List<PcapAddress^>^ addresses);
PcapLiveDevice(System::String^ name, System::String^ description, DeviceFlags^ flags, System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ addresses);
private:
System::String^ _name;
System::String^ _description;
DeviceFlags^ _flags;
System::Collections::Generic::List<PcapAddress^>^ _addresses;
System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ _addresses;
};
}
\ No newline at end of file
......@@ -7,6 +7,7 @@
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
using namespace PcapDotNet;
PcapOfflineDevice::PcapOfflineDevice(System::String^ filename)
......@@ -29,12 +30,12 @@ DeviceFlags^ PcapOfflineDevice::Flags::get()
return DeviceFlags::None;
}
List<PcapAddress^>^ PcapOfflineDevice::Addresses::get()
ReadOnlyCollection<PcapAddress^>^ PcapOfflineDevice::Addresses::get()
{
return gcnew List<PcapAddress^>();
return gcnew ReadOnlyCollection<PcapAddress^>(gcnew List<PcapAddress^>());
}
PcapDeviceHandler^ PcapOfflineDevice::Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout)
PcapDeviceHandler^ PcapOfflineDevice::Open(int snapshotLength, PcapDeviceOpenFlags flags, int readTimeout)
{
std::string unamangedFilename = MarshalingServices::ManagedToUnmanagedString(_filename);
......@@ -52,5 +53,5 @@ PcapDeviceHandler^ PcapOfflineDevice::Open(int snapLen, PcapDeviceOpenFlags flag
throw gcnew InvalidOperationException("Error creating a source string from filename " + _filename + " Error: " + gcnew String(errbuf));
}
return gcnew PcapDeviceHandler(source, snapLen, flags, readTimeout, NULL, nullptr);
return gcnew PcapDeviceHandler(source, snapshotLength, flags, readTimeout, NULL, nullptr);
}
......@@ -24,12 +24,12 @@ namespace PcapDotNet
DeviceFlags^ get() override;
}
virtual property System::Collections::Generic::List<PcapAddress^>^ Addresses
virtual property System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ Addresses
{
System::Collections::Generic::List<PcapAddress^>^ get() override;
System::Collections::ObjectModel::ReadOnlyCollection<PcapAddress^>^ get() override;
}
virtual PcapDeviceHandler^ Open(int snapLen, PcapDeviceOpenFlags flags, int readTimeout) override;
virtual PcapDeviceHandler^ Open(int snapshotLength, PcapDeviceOpenFlags flags, int readTimeout) override;
private:
System::String^ _filename;
......
......@@ -246,6 +246,14 @@
RelativePath=".\PcapAddress.h"
>
</File>
<File
RelativePath=".\PcapDataLink.cpp"
>
</File>
<File
RelativePath=".\PcapDataLink.h"
>
</File>
<File
RelativePath=".\PcapDeclarations.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