Commit ce9ee300 authored by Brickner_cp's avatar Brickner_cp

Some refactoring.

parent d40e339a
...@@ -28,6 +28,7 @@ namespace PcapDotNet.Base ...@@ -28,6 +28,7 @@ namespace PcapDotNet.Base
/// A bitwise combination of NumberStyles values that indicates the permitted format of s. /// A bitwise combination of NumberStyles values that indicates the permitted format of s.
/// A typical value to specify is NumberStyles.Integer. /// A typical value to specify is NumberStyles.Integer.
/// </param> /// </param>
/// <param name="provider">An System.IFormatProvider that supplies culture-specific formatting information about value.</param>
/// <returns>A 48-bit unsigned integer equivalent to the number specified in s.</returns> /// <returns>A 48-bit unsigned integer equivalent to the number specified in s.</returns>
public static UInt48 Parse(string value, NumberStyles style, IFormatProvider provider) public static UInt48 Parse(string value, NumberStyles style, IFormatProvider provider)
{ {
......
using System; using System;
using System.IO; using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Base;
using PcapDotNet.Packets; using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet; using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.TestUtils;
using PcapDotNet.TestUtils;
namespace PcapDotNet.Core.Test namespace PcapDotNet.Core.Test
{ {
/// <summary>
/// Summary description for DumpPacketsTests
/// </summary>
[TestClass]
public class DumpPacketsTests
{
public DumpPacketsTests()
{
//
// TODO: Add constructor logic here
//
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void DumpIpV4PacketTest()
{
MacAddress ethernetSource = new MacAddress("00:01:02:03:04:05");
MacAddress ethernetDestination = new MacAddress("A0:A1:A2:A3:A4:A5");
Random random = new Random();
using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
{
using (PacketDumpFile dumpFile = communicator.OpenDump(@"c:\wow.pcap"))
{
for (int i = 0; i != 1000; ++i)
{
byte ipV4TypeOfService = random.NextByte();
ushort ipV4Identification = random.NextUShort();
byte ipV4Ttl = random.NextByte();
IpV4FragmentationOptions ipV4FragmentationFlags = random.NextEnum<IpV4FragmentationOptions>();
ushort ipV4FragmentationOffset = (ushort)(random.NextUShort() / 8 * 8);
IpV4Fragmentation ipV4Fragmentation = new IpV4Fragmentation(ipV4FragmentationFlags, ipV4FragmentationOffset);
IpV4Protocol ipV4Protocol = random.NextEnum<IpV4Protocol>();
IpV4Address ipV4Source = new IpV4Address(random.NextUInt());
IpV4Address ipV4Destination = new IpV4Address(random.NextUInt());
IpV4Options ipV4Options = random.NextIpV4Options();
byte[] ipV4PayloadBuffer = new byte[random.Next(0, 50 * 1024)];
random.NextBytes(ipV4PayloadBuffer);
Datagram ipV4Payload = new Datagram(ipV4PayloadBuffer);
Packet packet = PacketBuilder.EthernetIpV4(DateTime.Now,
ethernetSource, ethernetDestination,
ipV4TypeOfService, ipV4Identification, ipV4Fragmentation, ipV4Ttl, ipV4Protocol,
ipV4Source, ipV4Destination, ipV4Options,
ipV4Payload);
dumpFile.Dump(packet);
dumpFile.Flush();
}
}
}
}
}
/// <summary> /// <summary>
/// Summary description for PacketDumpFileTests /// Summary description for PacketDumpFileTests
/// </summary> /// </summary>
...@@ -165,7 +65,7 @@ namespace PcapDotNet.Core.Test ...@@ -165,7 +65,7 @@ namespace PcapDotNet.Core.Test
string filename = Path.GetTempPath() + @"dump.pcap"; string filename = Path.GetTempPath() + @"dump.pcap";
Packet expectedPacket = PacketBuilder.Ethernet(DateTime.Now, new MacAddress(1), new MacAddress(2), EthernetType.QInQ, Packet expectedPacket = PacketBuilder.Ethernet(DateTime.Now, new MacAddress(1), new MacAddress(2), EthernetType.QInQ,
new Datagram(new byte[] {1, 2, 3})); new Datagram(new byte[] {1, 2, 3}));
PacketDumpFile.Dump(filename, new PcapDataLink(DataLinkKind.Ethernet), PacketDevice.DefaultSnapshotLength, PacketDumpFile.Dump(filename, new PcapDataLink(DataLinkKind.Ethernet), PacketDevice.DefaultSnapshotLength,
new[] {expectedPacket}); new[] {expectedPacket});
......
...@@ -47,11 +47,11 @@ ...@@ -47,11 +47,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="BerkeleyPacketFilterTests.cs" /> <Compile Include="BerkeleyPacketFilterTests.cs" />
<Compile Include="DumpPacketsTests.cs" />
<Compile Include="MoreAssert.cs" /> <Compile Include="MoreAssert.cs" />
<Compile Include="LivePacketDeviceTests.cs" /> <Compile Include="LivePacketDeviceTests.cs" />
<Compile Include="MoreIpV4Option.cs" /> <Compile Include="MoreIpV4Option.cs" />
<Compile Include="MoreXElement.cs" /> <Compile Include="MoreXElement.cs" />
<Compile Include="PacketDumpFileTests.cs" />
<Compile Include="PacketHandler.cs" /> <Compile Include="PacketHandler.cs" />
<Compile Include="OfflinePacketDeviceTests.cs" /> <Compile Include="OfflinePacketDeviceTests.cs" />
<Compile Include="PacketSendBufferTests.cs" /> <Compile Include="PacketSendBufferTests.cs" />
......
...@@ -87,7 +87,7 @@ namespace PcapDotNet.Core.Test ...@@ -87,7 +87,7 @@ namespace PcapDotNet.Core.Test
Random random = new Random(); Random random = new Random();
for (int i = 0; i != numPackets; ++i) for (int i = 0; i != numPackets; ++i)
{ {
DateTime packetTimestamp = random.NextDateTime(PacketCommunicator.MinimumPacketTimestamp, PacketCommunicator.MaximumPacketTimestamp); DateTime packetTimestamp = random.NextDateTime(PacketTimestamp.MinimumPacketTimestamp, PacketTimestamp.MaximumPacketTimestamp);
if (random.NextBool()) if (random.NextBool())
{ {
yield return yield return
...@@ -315,14 +315,6 @@ namespace PcapDotNet.Core.Test ...@@ -315,14 +315,6 @@ namespace PcapDotNet.Core.Test
return optionFieldShow; return optionFieldShow;
}); });
// List<string> strings1 = new List<string>(optionShows);
// List<string> strings2 = new List<string>(option.GetWiresharkSubfieldStrings());
// for (int i = 0; i != strings1.Count; ++i )
// {
// Assert.AreEqual(strings1[i], strings2[i]);
// }
Assert.IsTrue(optionShows.SequenceEqual(option.GetWiresharkSubfieldStrings())); Assert.IsTrue(optionShows.SequenceEqual(option.GetWiresharkSubfieldStrings()));
} }
} }
...@@ -355,7 +347,7 @@ namespace PcapDotNet.Core.Test ...@@ -355,7 +347,7 @@ namespace PcapDotNet.Core.Test
DateTime fieldTimestamp = fieldShow[4] == ' ' DateTime fieldTimestamp = fieldShow[4] == ' '
? DateTime.ParseExact(fieldShow, "MMM d, yyyy HH:mm:ss.fffffff", CultureInfo.InvariantCulture) ? DateTime.ParseExact(fieldShow, "MMM d, yyyy HH:mm:ss.fffffff", CultureInfo.InvariantCulture)
: DateTime.ParseExact(fieldShow, "MMM dd, yyyy HH:mm:ss.fffffff", CultureInfo.InvariantCulture); : DateTime.ParseExact(fieldShow, "MMM dd, yyyy HH:mm:ss.fffffff", CultureInfo.InvariantCulture);
MoreAssert.IsInRange(fieldTimestamp.AddSeconds(-0.5), fieldTimestamp.AddSeconds(0.5), packet.Timestamp); MoreAssert.IsInRange(fieldTimestamp.AddSeconds(-1), fieldTimestamp.AddSeconds(1), packet.Timestamp);
break; break;
case "frame.len": case "frame.len":
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "MarshalingServices.h" #include "MarshalingServices.h"
#include "PacketDumpFile.h" #include "PacketDumpFile.h"
#include "Timestamp.h" #include "PacketTimestamp.h"
#include "PcapError.h" #include "PcapError.h"
#include "Pcap.h" #include "Pcap.h"
...@@ -14,28 +14,6 @@ using namespace System::Collections::Generic; ...@@ -14,28 +14,6 @@ using namespace System::Collections::Generic;
using namespace PcapDotNet::Packets; using namespace PcapDotNet::Packets;
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
// static
DateTime PacketCommunicator::MinimumPacketTimestamp::get()
{
timeval zeroTimeValue;
zeroTimeValue.tv_sec = Int32::MinValue;
zeroTimeValue.tv_usec = Int32::MinValue;
DateTime result;
Timestamp::PcapTimestampToDateTime(zeroTimeValue, result);
return result;
}
// static
DateTime PacketCommunicator::MaximumPacketTimestamp::get()
{
timeval maxTimeValue;
maxTimeValue.tv_sec = Int32::MaxValue;
maxTimeValue.tv_usec = Int32::MaxValue;
DateTime result;
Timestamp::PcapTimestampToDateTime(maxTimeValue, result);
return result;
}
PcapDataLink PacketCommunicator::DataLink::get() PcapDataLink PacketCommunicator::DataLink::get()
{ {
return PcapDataLink(pcap_datalink(_pcapDescriptor)); return PcapDataLink(pcap_datalink(_pcapDescriptor));
...@@ -341,7 +319,7 @@ InvalidOperationException^ PacketCommunicator::BuildInvalidOperation(System::Str ...@@ -341,7 +319,7 @@ InvalidOperationException^ PacketCommunicator::BuildInvalidOperation(System::Str
Packet^ PacketCommunicator::CreatePacket(const pcap_pkthdr& packetHeader, const unsigned char* packetData, IDataLink^ dataLink) Packet^ PacketCommunicator::CreatePacket(const pcap_pkthdr& packetHeader, const unsigned char* packetData, IDataLink^ dataLink)
{ {
DateTime timestamp; DateTime timestamp;
Timestamp::PcapTimestampToDateTime(packetHeader.ts, timestamp); PacketTimestamp::PcapTimestampToDateTime(packetHeader.ts, timestamp);
array<Byte>^ managedPacketData = MarshalingServices::UnamangedToManagedByteArray(packetData, 0, packetHeader.caplen); array<Byte>^ managedPacketData = MarshalingServices::UnamangedToManagedByteArray(packetData, 0, packetHeader.caplen);
return gcnew Packet(managedPacketData, timestamp, dataLink); return gcnew Packet(managedPacketData, timestamp, dataLink);
......
...@@ -23,16 +23,6 @@ namespace PcapDotNet { namespace Core ...@@ -23,16 +23,6 @@ namespace PcapDotNet { namespace Core
public ref class PacketCommunicator abstract : System::IDisposable public ref class PacketCommunicator abstract : System::IDisposable
{ {
public: public:
static property System::DateTime MinimumPacketTimestamp
{
System::DateTime get();
}
static property System::DateTime MaximumPacketTimestamp
{
System::DateTime get();
}
/// <summary> /// <summary>
/// The link layer of an adapter. /// The link layer of an adapter.
/// </summary> /// </summary>
......
#include "PacketDumpFile.h" #include "PacketDumpFile.h"
#include "Timestamp.h" #include "PacketTimestamp.h"
#include "PacketHeader.h" #include "PacketHeader.h"
#include "MarshalingServices.h" #include "MarshalingServices.h"
#include "PcapError.h" #include "PcapError.h"
......
#include "PacketHeader.h" #include "PacketHeader.h"
#include "Pcap.h" #include "Pcap.h"
#include "Timestamp.h" #include "PacketTimestamp.h"
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
using namespace PcapDotNet::Packets; using namespace PcapDotNet::Packets;
...@@ -8,7 +8,7 @@ using namespace PcapDotNet::Packets; ...@@ -8,7 +8,7 @@ using namespace PcapDotNet::Packets;
// static // static
void PacketHeader::GetPcapHeader(pcap_pkthdr &header, Packet^ packet) void PacketHeader::GetPcapHeader(pcap_pkthdr &header, Packet^ packet)
{ {
Timestamp::DateTimeToPcapTimestamp(packet->Timestamp, header.ts); PacketTimestamp::DateTimeToPcapTimestamp(packet->Timestamp, header.ts);
header.len = packet->Length; header.len = packet->Length;
header.caplen = packet->Length; header.caplen = packet->Length;
} }
\ No newline at end of file
#include "PacketSampleStatistics.h" #include "PacketSampleStatistics.h"
#include "Timestamp.h" #include "PacketTimestamp.h"
#include "Pcap.h" #include "Pcap.h"
using namespace System; using namespace System;
...@@ -28,7 +28,7 @@ System::String^ PacketSampleStatistics::ToString() ...@@ -28,7 +28,7 @@ System::String^ PacketSampleStatistics::ToString()
PacketSampleStatistics::PacketSampleStatistics(const pcap_pkthdr& packetHeader, const unsigned char* packetData) PacketSampleStatistics::PacketSampleStatistics(const pcap_pkthdr& packetHeader, const unsigned char* packetData)
{ {
PcapDotNet::Core::Timestamp::PcapTimestampToDateTime(packetHeader.ts, _timestamp); PacketTimestamp::PcapTimestampToDateTime(packetHeader.ts, _timestamp);
_acceptedPackets = *reinterpret_cast<const unsigned long*>(packetData); _acceptedPackets = *reinterpret_cast<const unsigned long*>(packetData);
_acceptedBytes = *reinterpret_cast<const unsigned long*>(packetData + 8); _acceptedBytes = *reinterpret_cast<const unsigned long*>(packetData + 8);
......
#include "Timestamp.h" #include "PacketTimestamp.h"
#include "Pcap.h" #include "Pcap.h"
using namespace System; using namespace System;
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
// static
DateTime PacketTimestamp::MinimumPacketTimestamp::get()
{
return _minimumPacketTimestamp;
}
// static
DateTime PacketTimestamp::MaximumPacketTimestamp::get()
{
return _maximumPacketTimestamp;
}
// static // static
void Timestamp::PcapTimestampToDateTime(const timeval& pcapTimestamp, [System::Runtime::InteropServices::Out] System::DateTime% dateTime) void PacketTimestamp::PcapTimestampToDateTime(const timeval& pcapTimestamp, [Runtime::InteropServices::Out] DateTime% dateTime)
{ {
dateTime = DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc); dateTime = DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc);
dateTime = dateTime.Add(TimeSpan::FromSeconds(pcapTimestamp.tv_sec) + TimeSpan::FromMilliseconds(((double)pcapTimestamp.tv_usec) / 1000)); dateTime = dateTime.Add(TimeSpan::FromSeconds(pcapTimestamp.tv_sec) + TimeSpan::FromMilliseconds(((double)pcapTimestamp.tv_usec) / 1000));
...@@ -13,10 +25,24 @@ void Timestamp::PcapTimestampToDateTime(const timeval& pcapTimestamp, [System::R ...@@ -13,10 +25,24 @@ void Timestamp::PcapTimestampToDateTime(const timeval& pcapTimestamp, [System::R
} }
// static // static
void Timestamp::DateTimeToPcapTimestamp(System::DateTime dateTime, timeval& pcapTimestamp) void PacketTimestamp::DateTimeToPcapTimestamp(DateTime dateTime, timeval& pcapTimestamp)
{ {
dateTime = dateTime.ToUniversalTime(); dateTime = dateTime.ToUniversalTime();
TimeSpan timespan = dateTime - DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc); TimeSpan timespan = dateTime - DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc);
pcapTimestamp.tv_sec = (long)timespan.TotalSeconds; pcapTimestamp.tv_sec = (long)timespan.TotalSeconds;
pcapTimestamp.tv_usec = (long)(timespan.Milliseconds * 1000); pcapTimestamp.tv_usec = (long)(timespan.Milliseconds * 1000);
}
// static
void PacketTimestamp::Initialize()
{
// Min
timeval pcapTimestamp;
pcapTimestamp.tv_sec = Int32::MinValue;
pcapTimestamp.tv_usec = Int32::MinValue;
PacketTimestamp::PcapTimestampToDateTime(pcapTimestamp, _minimumPacketTimestamp);
pcapTimestamp.tv_sec = Int32::MaxValue;
pcapTimestamp.tv_usec = Int32::MaxValue;
PacketTimestamp::PcapTimestampToDateTime(pcapTimestamp, _maximumPacketTimestamp);
} }
\ No newline at end of file
...@@ -4,13 +4,35 @@ ...@@ -4,13 +4,35 @@
namespace PcapDotNet { namespace Core namespace PcapDotNet { namespace Core
{ {
private ref class Timestamp public ref class PacketTimestamp sealed
{ {
public: public:
/// <summary>
/// The minimum legal timestamp to put in a packet.
/// </summary>
static property System::DateTime MinimumPacketTimestamp
{
System::DateTime get();
}
/// <summary>
/// The maximum legal timestamp to put in a packet.
/// </summary>
static property System::DateTime MaximumPacketTimestamp
{
System::DateTime get();
}
internal:
static void PcapTimestampToDateTime(const timeval& pcapTimestamp, [System::Runtime::InteropServices::Out] System::DateTime% dateTime); static void PcapTimestampToDateTime(const timeval& pcapTimestamp, [System::Runtime::InteropServices::Out] System::DateTime% dateTime);
static void DateTimeToPcapTimestamp(System::DateTime dateTime, timeval& pcapTimestamp); static void DateTimeToPcapTimestamp(System::DateTime dateTime, timeval& pcapTimestamp);
private: private:
Timestamp(){} static PacketTimestamp() { Initialize(); }
PacketTimestamp(){}
static void Initialize();
static System::DateTime _minimumPacketTimestamp;
static System::DateTime _maximumPacketTimestamp;
}; };
}} }}
\ No newline at end of file
...@@ -372,11 +372,11 @@ ...@@ -372,11 +372,11 @@
> >
</File> </File>
<File <File
RelativePath=".\Timestamp.cpp" RelativePath=".\PacketTimestamp.cpp"
> >
</File> </File>
<File <File
RelativePath=".\Timestamp.h" RelativePath=".\PacketTimestamp.h"
> >
</File> </File>
</Filter> </Filter>
......
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