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 @@
<Compile Include="Sequence.cs" />
<Compile Include="SerialNumber32.cs" />
<Compile Include="ShortExtensions.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="TimeSpanExtensions.cs" />
<Compile Include="TypeExtensions.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.Linq;
using System.Threading;
......@@ -44,17 +44,14 @@ namespace PcapDotNet.Core.Test
//
#endregion
[TestMethod]
public void OpenOfflineMultipleTimes()
private static void TestOpenMultipleTimes(int numTimes, string filename)
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
const int NumPackets = 10;
Packet expectedPacket = _random.NextEthernetPacket(100, SourceMac, DestinationMac);
PacketDevice device = GetOfflineDevice(NumPackets, expectedPacket);
// TODO: Fix so we can go beyond 509.
// See http://www.winpcap.org/pipermail/winpcap-bugs/2012-December/001547.html
for (int j = 0; j != 100; ++j)
PacketDevice device = GetOfflineDevice(NumPackets, expectedPacket, TimeSpan.Zero, Path.GetTempPath() + @"dump.pcap", Path.GetTempPath() + filename);
for (int j = 0; j != numTimes; ++j)
{
using (PacketCommunicator communicator = device.Open())
{
......@@ -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]
public void GetPacketTest()
{
......@@ -189,7 +200,7 @@ namespace PcapDotNet.Core.Test
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)]
[ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
public void OpenNullFilenameTest()
{
using (new OfflinePacketDevice(null).Open())
......@@ -450,7 +461,11 @@ namespace PcapDotNet.Core.Test
}
if (readFilename != dumpFilename)
{
if (File.Exists(readFilename))
File.Delete(readFilename);
File.Move(dumpFilename, readFilename);
}
OfflinePacketDevice device = new OfflinePacketDevice(readFilename);
Assert.AreEqual(0, device.Addresses.Count);
......
......@@ -5,6 +5,7 @@
using namespace System;
using namespace System::Globalization;
using namespace PcapDotNet::Base;
using namespace PcapDotNet::Core;
PacketTotalStatistics^ OfflinePacketCommunicator::TotalStatistics::get()
......@@ -27,16 +28,24 @@ OfflinePacketCommunicator::OfflinePacketCommunicator(String^ filename)
// static
pcap_t* OfflinePacketCommunicator::OpenFile(String^ fileName)
{
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));
if (fileName == nullptr)
throw gcnew ArgumentNullException("fileName");
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)
{
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));
if (fcloseResult != 0)
errorMessage += " Also failed closing the file.";
......@@ -44,4 +53,4 @@ pcap_t* OfflinePacketCommunicator::OpenFile(String^ fileName)
}
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