diff --git a/.gitignore b/.gitignore
index 8ed99823..b733cc2a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,6 +7,8 @@ PcapDotNet/src/TestResults
PcapDotNet.DevelopersPack/3rdParty/
artifacts/
.vs/
+.idea/
+.vscode/
TemporaryGeneratedFile_*
*.cache
*.csproj.FileListAbsolute.txt
@@ -15,6 +17,6 @@ TemporaryGeneratedFile_*
*.pdb
*.sdf
*.suo
-*.vcxproj.user
+*.user
*.db
*.opendb
diff --git a/PcapDotNet/src/Directory.Build.props b/PcapDotNet/src/Directory.Build.props
index 285115ee..1aae6d64 100644
--- a/PcapDotNet/src/Directory.Build.props
+++ b/PcapDotNet/src/Directory.Build.props
@@ -16,36 +16,22 @@
True
True
False
+ net40;net80
- netstandard2.0
true
$(MSBuildThisFileDirectory)PcapDotNet.snk
-
-
-
-
-
+
- netstandard2.0
False
-
-
-
-
-
-
- net48;net8.0
- False
-
-
+
diff --git a/PcapDotNet/src/PcapDotNet.Base.Test/PcapDotNet.Base.Test.csproj b/PcapDotNet/src/PcapDotNet.Base.Test/PcapDotNet.Base.Test.csproj
index e6e31e4e..a99a8d99 100644
--- a/PcapDotNet/src/PcapDotNet.Base.Test/PcapDotNet.Base.Test.csproj
+++ b/PcapDotNet/src/PcapDotNet.Base.Test/PcapDotNet.Base.Test.csproj
@@ -2,8 +2,13 @@
-
-
+
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
diff --git a/PcapDotNet/src/PcapDotNet.Base.Test/UInt128Tests.cs b/PcapDotNet/src/PcapDotNet.Base.Test/UInt128Tests.cs
index 3239a891..12d00a41 100644
--- a/PcapDotNet/src/PcapDotNet.Base.Test/UInt128Tests.cs
+++ b/PcapDotNet/src/PcapDotNet.Base.Test/UInt128Tests.cs
@@ -70,7 +70,7 @@ public void CastToULongOverflow()
}
catch (Exception)
{
- Assert.Fail();
+ Assert.False(true);
return;
}
Assert.Equal(overflow, (ulong)value);
@@ -251,4 +251,4 @@ public void ToStringTestFirstBitIsOne()
Assert.Equal(ValueString, value.ToString("x32"));
}
}
-}
\ No newline at end of file
+}
diff --git a/PcapDotNet/src/PcapDotNet.Base/IListExtensions.cs b/PcapDotNet/src/PcapDotNet.Base/IListExtensions.cs
index 697a6a55..1bbb968b 100644
--- a/PcapDotNet/src/PcapDotNet.Base/IListExtensions.cs
+++ b/PcapDotNet/src/PcapDotNet.Base/IListExtensions.cs
@@ -11,6 +11,7 @@ namespace PcapDotNet.Base
public static class IListExtensions
// ReSharper restore InconsistentNaming
{
+#if !NET7_0_OR_GREATER
///
/// Wraps a list with a ReadOnlyCollection.
///
@@ -21,7 +22,7 @@ public static ReadOnlyCollection AsReadOnly(this IList list)
{
return new ReadOnlyCollection(list);
}
-
+#endif
///
/// Returns an enumerable of all the elements in the given list starting in a specific offset and taking no more than a specific count.
///
@@ -37,4 +38,4 @@ public static IEnumerable Range(this IList list, int offset, int count)
yield return list[i];
}
}
-}
\ No newline at end of file
+}
diff --git a/PcapDotNet/src/PcapDotNet.Core.Extensions/LivePacketDeviceExtensions.cs b/PcapDotNet/src/PcapDotNet.Core.Extensions/LivePacketDeviceExtensions.cs
index 8e350017..92a4503f 100644
--- a/PcapDotNet/src/PcapDotNet.Core.Extensions/LivePacketDeviceExtensions.cs
+++ b/PcapDotNet/src/PcapDotNet.Core.Extensions/LivePacketDeviceExtensions.cs
@@ -4,6 +4,7 @@
using System.Management;
using System.Net.NetworkInformation;
using Microsoft.Win32;
+using PcapDotNet.Core.Native;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet;
@@ -31,6 +32,8 @@ public static string GetGuid(this LivePacketDevice livePacketDevice)
throw new ArgumentNullException("livePacketDevice");
string livePacketDeviceName = livePacketDevice.Name;
+ if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
+ return livePacketDeviceName;
if (!livePacketDeviceName.StartsWith(NamePrefix, StringComparison.Ordinal))
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
@@ -50,11 +53,14 @@ public static string GetGuid(this LivePacketDevice livePacketDevice)
/// When the PNPDeviceID cannot be retrieved from the registry.
public static string GetPnpDeviceId(this LivePacketDevice livePacketDevice)
{
+ if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
+ throw new InvalidOperationException("Platform not supported");
+
string guid = livePacketDevice.GetGuid();
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(NetworkConnectionConfigKey + @"\" + guid + @"\Connection"))
{
- string pnpDeviceId = key.GetValue("PnpInstanceID") as string;
+ string pnpDeviceId = key?.GetValue("PnpInstanceID") as string;
if (pnpDeviceId == null)
throw new InvalidOperationException("Could not find PNP Device ID in the registry");
return pnpDeviceId;
@@ -75,7 +81,7 @@ public static NetworkInterface GetNetworkInterface(this LivePacketDevice livePac
throw new ArgumentNullException("livePacketDevice");
string guid = GetGuid(livePacketDevice);
- return NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(networkInterface => networkInterface.Id == guid);
+ return Interop.Pcap.GetAllNetworkInterfacesByDotNet().FirstOrDefault(networkInterface => networkInterface.Id == guid);
}
///
@@ -95,6 +101,12 @@ public static MacAddress GetMacAddress(this LivePacketDevice livePacketDevice)
return new MacAddress(addressBytes.ReadUInt48(0, Endianity.Big));
}
+ if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX
+ && livePacketDevice.Name == "rpcap://\\Device\\NPF_Loopback")
+ {
+ return new MacAddress();
+ }
+
return livePacketDevice.GetMacAddressWmi();
}
@@ -107,6 +119,9 @@ public static MacAddress GetMacAddress(this LivePacketDevice livePacketDevice)
/// When the cannot be retrieved using WMI.
private static MacAddress GetMacAddressWmi(this LivePacketDevice livePacketDevice)
{
+ if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
+ throw new InvalidOperationException("No MAC Address on device: " + livePacketDevice.Name);
+
string pnpDeviceId = livePacketDevice.GetPnpDeviceId();
string escapedPnpDeviceId = pnpDeviceId.Replace(@"\", @"\\");
@@ -128,4 +143,4 @@ private static MacAddress GetMacAddressWmi(this LivePacketDevice livePacketDevic
throw new InvalidOperationException("No MAC Address for WMI instance with PNP Device ID: " + pnpDeviceId);
}
}
-}
\ No newline at end of file
+}
diff --git a/PcapDotNet/src/PcapDotNet.Core.Extensions/NetworkInterfaceExtensions.cs b/PcapDotNet/src/PcapDotNet.Core.Extensions/NetworkInterfaceExtensions.cs
index 62a09f94..18206ab4 100644
--- a/PcapDotNet/src/PcapDotNet.Core.Extensions/NetworkInterfaceExtensions.cs
+++ b/PcapDotNet/src/PcapDotNet.Core.Extensions/NetworkInterfaceExtensions.cs
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Net.NetworkInformation;
+using System.Runtime.InteropServices;
namespace PcapDotNet.Core.Extensions
{
@@ -21,7 +22,9 @@ public static LivePacketDevice GetLivePacketDevice(this NetworkInterface network
if (networkInterface == null)
throw new ArgumentNullException("networkInterface");
- return LivePacketDevice.AllLocalMachine.FirstOrDefault(device => device.Name == LivePacketDeviceExtensions.NamePrefix + networkInterface.Id);
+ return LivePacketDevice.AllLocalMachine.FirstOrDefault(device => Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX
+ ? device.Name == networkInterface.Id
+ : device.Name == LivePacketDeviceExtensions.NamePrefix + networkInterface.Id);
}
}
-}
\ No newline at end of file
+}
diff --git a/PcapDotNet/src/PcapDotNet.Core.Extensions/PcapDotNet.Core.Extensions.csproj b/PcapDotNet/src/PcapDotNet.Core.Extensions/PcapDotNet.Core.Extensions.csproj
index 7c82e536..13156947 100644
--- a/PcapDotNet/src/PcapDotNet.Core.Extensions/PcapDotNet.Core.Extensions.csproj
+++ b/PcapDotNet/src/PcapDotNet.Core.Extensions/PcapDotNet.Core.Extensions.csproj
@@ -7,8 +7,11 @@
-
-
+
-
\ No newline at end of file
+
+
+
+
+
diff --git a/PcapDotNet/src/PcapDotNet.Core.Extensions/TaskExtensions.cs b/PcapDotNet/src/PcapDotNet.Core.Extensions/TaskExtensions.cs
new file mode 100644
index 00000000..de19ff13
--- /dev/null
+++ b/PcapDotNet/src/PcapDotNet.Core.Extensions/TaskExtensions.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace PcapDotNet.Core.Extensions
+{
+ ///
+ /// Extension methods for Task class.
+ ///
+ public static class TaskExtensions
+ {
+ ///
+ /// Creates a Task that will complete after a time delay.
+ ///
+ /// The time span to wait before completing the returned Task
+ /// A Task that represents the time delay
+ ///
+ /// The is less than -1 or greater than the maximum allowed timer duration.
+ ///
+ ///
+ /// After the specified time delay, the Task is completed in RanToCompletion state.
+ ///
+ public static Task Delay(TimeSpan delay)
+ {
+ // timer inaccuracy https://github.com/dotnet/runtime/issues/100455
+#if NETCOREAPP1_0_OR_GREATER
+ return Task.Delay(delay.Add(TimeSpan.FromMilliseconds(1))); // +1 is to workaround, random return less than 1 ms too early
+#else
+ var tcs = new TaskCompletionSource