Commit 6e912488 authored by Brickner_cp's avatar Brickner_cp

Opening files fixes:

1. When opening files with ASCII or ISO-8859-1 filenames, you can now open unlimited number of files (and not just ~500). Note that when opening files with other characters there's a problem releasing the resources due to http://www.winpcap.org/pipermail/winpcap-bugs/2012-December/001547.html
2. When giving a null filename when opening a file, NullArgumentException is thrown instead of InvalidOperationException.
parent 2b6be2f2
...@@ -100,6 +100,7 @@ ...@@ -100,6 +100,7 @@
<Compile Include="Sequence.cs" /> <Compile Include="Sequence.cs" />
<Compile Include="SerialNumber32.cs" /> <Compile Include="SerialNumber32.cs" />
<Compile Include="ShortExtensions.cs" /> <Compile Include="ShortExtensions.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="TimeSpanExtensions.cs" /> <Compile Include="TimeSpanExtensions.cs" />
<Compile Include="TypeExtensions.cs" /> <Compile Include="TypeExtensions.cs" />
<Compile Include="UInt128.cs" /> <Compile Include="UInt128.cs" />
......
using System;
using System.Globalization;
using System.Linq;
namespace PcapDotNet.Base
{
/// <summary>
/// Extension methods for String.
/// </summary>
public static class StringExtensions
{
public static bool AreAllCharactersInRange(this string value, char minValue, char maxValue)
{
return value.All(c => c >= minValue && c <= maxValue);
}
}
}
\ No newline at end of file
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
...@@ -44,17 +44,14 @@ namespace PcapDotNet.Core.Test ...@@ -44,17 +44,14 @@ namespace PcapDotNet.Core.Test
// //
#endregion #endregion
[TestMethod] private static void TestOpenMultipleTimes(int numTimes, string filename)
public void OpenOfflineMultipleTimes()
{ {
const string SourceMac = "11:22:33:44:55:66"; const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC"; const string DestinationMac = "77:88:99:AA:BB:CC";
const int NumPackets = 10; const int NumPackets = 10;
Packet expectedPacket = _random.NextEthernetPacket(100, SourceMac, DestinationMac); Packet expectedPacket = _random.NextEthernetPacket(100, SourceMac, DestinationMac);
PacketDevice device = GetOfflineDevice(NumPackets, expectedPacket); PacketDevice device = GetOfflineDevice(NumPackets, expectedPacket, TimeSpan.Zero, Path.GetTempPath() + @"dump.pcap", Path.GetTempPath() + filename);
// TODO: Fix so we can go beyond 509. for (int j = 0; j != numTimes; ++j)
// See http://www.winpcap.org/pipermail/winpcap-bugs/2012-December/001547.html
for (int j = 0; j != 100; ++j)
{ {
using (PacketCommunicator communicator = device.Open()) using (PacketCommunicator communicator = device.Open())
{ {
...@@ -76,6 +73,20 @@ namespace PcapDotNet.Core.Test ...@@ -76,6 +73,20 @@ namespace PcapDotNet.Core.Test
} }
} }
[TestMethod]
public void OpenOfflineMultipleTimes()
{
TestOpenMultipleTimes(1000, @"dump.pcap");
}
[TestMethod]
public void OpenOfflineMultipleTimesUnicode()
{
// TODO: Fix so we can go beyond 509 when using unicode filenames.
// See http://www.winpcap.org/pipermail/winpcap-bugs/2012-December/001547.html
TestOpenMultipleTimes(100, @"דמפ.pcap");
}
[TestMethod] [TestMethod]
public void GetPacketTest() public void GetPacketTest()
{ {
...@@ -189,7 +200,7 @@ namespace PcapDotNet.Core.Test ...@@ -189,7 +200,7 @@ namespace PcapDotNet.Core.Test
} }
[TestMethod] [TestMethod]
[ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
public void OpenNullFilenameTest() public void OpenNullFilenameTest()
{ {
using (new OfflinePacketDevice(null).Open()) using (new OfflinePacketDevice(null).Open())
...@@ -450,7 +461,11 @@ namespace PcapDotNet.Core.Test ...@@ -450,7 +461,11 @@ namespace PcapDotNet.Core.Test
} }
if (readFilename != dumpFilename) if (readFilename != dumpFilename)
{
if (File.Exists(readFilename))
File.Delete(readFilename);
File.Move(dumpFilename, readFilename); File.Move(dumpFilename, readFilename);
}
OfflinePacketDevice device = new OfflinePacketDevice(readFilename); OfflinePacketDevice device = new OfflinePacketDevice(readFilename);
Assert.AreEqual(0, device.Addresses.Count); Assert.AreEqual(0, device.Addresses.Count);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
using namespace System; using namespace System;
using namespace System::Globalization; using namespace System::Globalization;
using namespace PcapDotNet::Base;
using namespace PcapDotNet::Core; using namespace PcapDotNet::Core;
PacketTotalStatistics^ OfflinePacketCommunicator::TotalStatistics::get() PacketTotalStatistics^ OfflinePacketCommunicator::TotalStatistics::get()
...@@ -27,16 +28,24 @@ OfflinePacketCommunicator::OfflinePacketCommunicator(String^ filename) ...@@ -27,16 +28,24 @@ OfflinePacketCommunicator::OfflinePacketCommunicator(String^ filename)
// static // static
pcap_t* OfflinePacketCommunicator::OpenFile(String^ fileName) pcap_t* OfflinePacketCommunicator::OpenFile(String^ fileName)
{ {
std::wstring unamangedFilename = MarshalingServices::ManagedToUnmanagedWideString(fileName); if (fileName == nullptr)
FILE* file = _wfopen(unamangedFilename.c_str(), L"rb"); throw gcnew ArgumentNullException("fileName");
if (file == NULL)
throw gcnew InvalidOperationException(String::Format(CultureInfo::InvariantCulture, "Failed opening file {0}.", fileName));
char errorBuffer[PCAP_ERRBUF_SIZE]; char errorBuffer[PCAP_ERRBUF_SIZE];
pcap_t *pcapDescriptor = pcap_fopen_offline(file, errorBuffer); FILE* file = NULL;
pcap_t *pcapDescriptor;
if (!StringExtensions::AreAllCharactersInRange(fileName, 0, 255)) {
std::wstring unamangedFilename = MarshalingServices::ManagedToUnmanagedWideString(fileName);
FILE* file = _wfopen(unamangedFilename.c_str(), L"rb");
if (file == NULL)
throw gcnew InvalidOperationException(String::Format(CultureInfo::InvariantCulture, "Failed opening file {0}.", fileName));
pcapDescriptor = pcap_fopen_offline(file, errorBuffer);
} else {
std::string unamangedFilename = MarshalingServices::ManagedToUnmanagedString(fileName);
pcapDescriptor = pcap_open_offline(unamangedFilename.c_str(), errorBuffer);
}
if (pcapDescriptor == NULL) if (pcapDescriptor == NULL)
{ {
int fcloseResult = fclose(file); int fcloseResult = file == NULL ? 0 : fclose(file);
String^ errorMessage = String::Format(CultureInfo::InvariantCulture, "Failed opening file {0}. Error: {1}.", fileName, gcnew String(errorBuffer)); String^ errorMessage = String::Format(CultureInfo::InvariantCulture, "Failed opening file {0}. Error: {1}.", fileName, gcnew String(errorBuffer));
if (fcloseResult != 0) if (fcloseResult != 0)
errorMessage += " Also failed closing the file."; errorMessage += " Also failed closing the file.";
...@@ -44,4 +53,4 @@ pcap_t* OfflinePacketCommunicator::OpenFile(String^ fileName) ...@@ -44,4 +53,4 @@ pcap_t* OfflinePacketCommunicator::OpenFile(String^ fileName)
} }
return pcapDescriptor; return pcapDescriptor;
} }
\ No newline at end of file
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