Skip to content

Commit

Permalink
Merge branch 'feature/fix_ci' into chore/rpcap_linux_auth
Browse files Browse the repository at this point in the history
  • Loading branch information
kayoub5 committed Mar 2, 2025
2 parents 205b1ab + 978c10f commit 6d9d0d6
Show file tree
Hide file tree
Showing 62 changed files with 353 additions and 424 deletions.
2 changes: 0 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
image: ubuntu-2004:202201-02
steps:
- checkout
- run: sudo -E bash scripts/install-dotnet.sh --install-dir /usr/local/bin
- run: sudo -E bash scripts/install-tap.sh
# Download and compile latest libpcap
- when:
Expand All @@ -58,7 +57,6 @@ jobs:
resource_class: arm.medium
steps:
- checkout
- run: sudo -E bash scripts/install-dotnet.sh --install-dir /usr/local/bin
- run: sudo -E bash scripts/install-tap.sh
- run: sudo apt-get install libpcap0.8
- run: sudo -E bash scripts/test.sh
Expand Down
10 changes: 4 additions & 6 deletions .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Setup .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x
- name: Install .net dependencies
run: dotnet restore
fetch-depth: 0
- name: Install libpcap
run: sudo -E bash scripts/install-libpcap.sh
- name: Install tap
Expand All @@ -30,11 +26,13 @@ jobs:
run: dotnet build SharpPcap/SharpPcap.csproj
- name: Test
run: sudo -E bash scripts/test.sh
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- run: sudo chmod -R +r Test/TestResults
if: always()
- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
uses: mikepenz/action-junit-report@v5
if: always()
with:
report_paths: Test/TestResults/TestResults.xml
Expand Down
31 changes: 0 additions & 31 deletions .reuse/dep5

This file was deleted.

47 changes: 0 additions & 47 deletions .semaphore/semaphore.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Examples/CreatingCaptureFile/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static void device_OnPacketArrival(object sender, PacketCapture e)

if (rawPacket.LinkLayerType == PacketDotNet.LinkLayers.Ethernet)
{
var packet = PacketDotNet.Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);
var packet = Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);
var ethernetPacket = (EthernetPacket)packet;

Console.WriteLine("{0} At: {1}:{2}: MAC:{3} -> MAC:{4}",
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ See the [Examples](https://github.com/chmorgan/sharppcap/tree/master/Examples) f
```cs
void Device_OnPacketArrival(object s, PacketCapture e)
{
Console.WriteLine(e.Packet);
Console.WriteLine(e.GetPacket());
}

using var device = LibPcapLiveDeviceList.Instance[0];
Expand All @@ -86,7 +86,7 @@ See the [Examples](https://github.com/chmorgan/sharppcap/tree/master/Examples) f
```cs
void Device_OnPacketArrival(object s, PacketCapture e)
{
Console.WriteLine(e.Packet);
Console.WriteLine(e.GetPacket());
}

using var device = new CaptureFileReaderDevice("filename.pcap");
Expand Down
40 changes: 40 additions & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2023-2024 Ayoub Kaanich <[email protected]>
# SPDX-License-Identifier: MIT
version = 1
SPDX-PackageName = "sharppcap"
SPDX-PackageSupplier = "Chris Morgan <[email protected]>"
SPDX-PackageDownloadLocation = "https://github.com/dotpcap/sharppcap"

[[annotations]]
path = [
"Examples/**",
"SharpPcap.sln",
"Test/capture_files/10k_packets.pcap",
"Test/capture_files/arp_request_response.pcap",
"Test/capture_files/ipv6_http.pcap",
"Test/capture_files/ipv6_icmpv6_packet.pcap",
"Test/capture_files/ip_packet_bogus_length.pcap",
"Test/capture_files/README",
"Test/capture_files/tcp.pcap",
"Test/capture_files/tcp_with_extra_bytes.pcap",
"Test/capture_files/test_stream.pcap",
"Test/capture_files/udp_dns_request_response.pcap",
]
precedence = "aggregate"
SPDX-FileCopyrightText = [
"Tamir Gal <[email protected]>",
"Chris Morgan <[email protected]>",
]
SPDX-License-Identifier = "MIT"

[[annotations]]
path = ["Tutorial/**", "History.md", "README.md", "renovate.json"]
precedence = "aggregate"
SPDX-FileCopyrightText = "Chris Morgan <[email protected]>"
SPDX-License-Identifier = "MIT"

[[annotations]]
path = "Test/capture_files/arp_with_vlan.pcap"
precedence = "aggregate"
SPDX-FileCopyrightText = "Houcem Benali <[email protected]>"
SPDX-License-Identifier = "MIT"
22 changes: 11 additions & 11 deletions SharpPcap/ARP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ARP
/// <param name="device">The network device on which this resolver sends its ARP packets</param>
public ARP(LibPcapLiveDevice device)
{
pcapInterface = device.Interface;
pcapInterface = device.Interface ?? throw new ArgumentException();
}

/// <summary>
Expand All @@ -37,7 +37,7 @@ public ARP(LibPcapLiveDevice device)
/// <param name="destIP">The IP address to resolve</param>
/// <returns>The MAC address that matches to the given IP address or
/// null if there was a timeout</returns>
public PhysicalAddress Resolve(System.Net.IPAddress destIP)
public PhysicalAddress? Resolve(System.Net.IPAddress destIP)
{
return Resolve(destIP, null, null);
}
Expand All @@ -50,9 +50,9 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP)
/// <param name="localMAC">The localMAC address to use, if null the local mac will be discovered</param>
/// <returns>The MAC address that matches to the given IP address or
/// null if there was a timeout</returns>
public PhysicalAddress Resolve(System.Net.IPAddress destIP,
System.Net.IPAddress localIP,
PhysicalAddress localMAC)
public PhysicalAddress? Resolve(System.Net.IPAddress destIP,
System.Net.IPAddress? localIP,
PhysicalAddress? localMAC)
{
// if no local ip address is specified attempt to find one from the adapter
if (localIP == null)
Expand All @@ -61,10 +61,10 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP,
// ARP is ipv4, NDP is used for ipv6
foreach (var address in pcapInterface.Addresses)
{
if (address.Addr.type == Sockaddr.AddressTypes.AF_INET_AF_INET6)
if (address.Addr?.type == Sockaddr.AddressTypes.AF_INET_AF_INET6)
{
// make sure the address is ipv4
if (address.Addr.ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
if (address.Addr?.ipAddress?.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = address.Addr.ipAddress;
break; // break out of the foreach
Expand All @@ -84,7 +84,7 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP,
{
foreach (var address in pcapInterface.Addresses)
{
if (address.Addr.type == Sockaddr.AddressTypes.HARDWARE)
if (address.Addr?.type == Sockaddr.AddressTypes.HARDWARE)
{
localMAC = address.Addr.hardwareAddress;
}
Expand All @@ -103,7 +103,7 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP,
}
}

internal static PhysicalAddress Resolve(
internal static PhysicalAddress? Resolve(
ILiveDevice device,
System.Net.IPAddress destIP,
System.Net.IPAddress localIP,
Expand All @@ -125,7 +125,7 @@ internal static PhysicalAddress Resolve(

var requestInterval = new TimeSpan(0, 0, 1);

PacketDotNet.ArpPacket arpPacket = null;
PacketDotNet.ArpPacket? arpPacket = null;

// attempt to resolve the address with the current timeout
var timeoutDateTime = DateTime.Now + timeout;
Expand Down Expand Up @@ -171,7 +171,7 @@ internal static PhysicalAddress Resolve(
else
{
//return the resolved MAC address
return arpPacket.SenderHardwareAddress;
return arpPacket?.SenderHardwareAddress;
}
}

Expand Down
26 changes: 15 additions & 11 deletions SharpPcap/BaseLiveDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,39 @@ namespace SharpPcap
public abstract class BaseLiveDevice : IDisposable
{

private CancellationTokenSource TokenSource;
private Task CaptureTask;
private CancellationTokenSource? TokenSource;
private Task? CaptureTask;
public bool Started => CaptureTask?.IsCompleted == false;

protected TimeSpan ReadTimeout { get; set; } = TimeSpan.FromSeconds(1);

public TimeSpan StopCaptureTimeout { get; set; } = TimeSpan.FromSeconds(1);

public ICaptureStatistics Statistics => null;
public ICaptureStatistics? Statistics => null;

public TimestampResolution TimestampResolution => TimestampResolution.Microsecond;

public virtual LinkLayers LinkType => LinkLayers.Ethernet;

public event PacketArrivalEventHandler OnPacketArrival;
public event CaptureStoppedEventHandler OnCaptureStopped;
public event PacketArrivalEventHandler? OnPacketArrival;
public event CaptureStoppedEventHandler? OnCaptureStopped;

protected BpfProgram FilterProgram;
private string FilterValue;
public string Filter
protected BpfProgram? FilterProgram;
private string? FilterValue;
public string? Filter
{
get => FilterValue;
set
{
using (var pcapHandle = LibPcapSafeNativeMethods.pcap_open_dead((int)LinkType, Pcap.MAX_PACKET_SIZE))
if (value == null)
{
FilterProgram = BpfProgram.Create(pcapHandle, value);
FilterValue = value;
FilterValue = null;
FilterProgram = null;
return;
}
using var pcapHandle = LibPcapSafeNativeMethods.pcap_open_dead((int)LinkType, Pcap.MAX_PACKET_SIZE);
FilterProgram = BpfProgram.Create(pcapHandle, value);
FilterValue = value;
}
}

Expand Down
2 changes: 1 addition & 1 deletion SharpPcap/CaptureDeviceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void SendPacket(this IInjectionDevice device, Packet p, int size)
/// <param name="device"></param>
/// <param name="p"></param>
/// <param name="header"></param>
public static void SendPacket(this IInjectionDevice device, RawCapture p, ICaptureHeader header = null)
public static void SendPacket(this IInjectionDevice device, RawCapture p, ICaptureHeader? header = null)
{
device.SendPacket(new ReadOnlySpan<byte>(p.Data), header);
}
Expand Down
2 changes: 1 addition & 1 deletion SharpPcap/CaptureDeviceList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace SharpPcap
/// </summary>
public class CaptureDeviceList : ReadOnlyCollection<ILiveDevice>
{
private static CaptureDeviceList instance;
private static CaptureDeviceList? instance;

private LibPcap.LibPcapLiveDeviceList libPcapDeviceList;

Expand Down
10 changes: 5 additions & 5 deletions SharpPcap/DeviceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DeviceConfiguration

public int? KernelBufferSize { get; set; }

public RemoteAuthentication Credentials { get; set; }
public RemoteAuthentication? Credentials { get; set; }

public bool? Immediate { get; set; }

Expand All @@ -41,7 +41,7 @@ public class DeviceConfiguration

public TimestampType? TimestampType { get; set; }

public event EventHandler<ConfigurationFailedEventArgs> ConfigurationFailed;
public event EventHandler<ConfigurationFailedEventArgs>? ConfigurationFailed;

internal void RaiseConfigurationFailed(string property, PcapError error, string message)
{
Expand Down Expand Up @@ -69,8 +69,8 @@ internal void RaiseConfigurationFailed(string property, PcapError error, string

public class ConfigurationFailedEventArgs : EventArgs
{
public PcapError Error { get; internal set; }
public string Property { get; internal set; }
public string Message { get; internal set; }
public PcapError Error { get; internal init; }
public required string Property { get; init; }
public required string Message { get; init; }
}
}
2 changes: 1 addition & 1 deletion SharpPcap/ICaptureDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public interface ICaptureDevice : IPcapDevice
///
/// Devices that lack statistics support return null
/// </summary>
ICaptureStatistics Statistics { get; }
ICaptureStatistics? Statistics { get; }

#region Timestamp
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SharpPcap/IInjectionDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IInjectionDevice : IPcapDevice
/// </summary>
/// <param name="p">The packet bytes to send</param>
/// <param name="size">The number of bytes to send</param>
void SendPacket(ReadOnlySpan<byte> p, ICaptureHeader header = null);
void SendPacket(ReadOnlySpan<byte> p, ICaptureHeader? header = null);
}
}

Loading

0 comments on commit 6d9d0d6

Please sign in to comment.