Skip to content

Commit

Permalink
Enable Nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
kayoub5 committed Mar 2, 2025
1 parent f0e535a commit 27e4c7c
Show file tree
Hide file tree
Showing 38 changed files with 188 additions and 190 deletions.
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
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);
}
}

6 changes: 3 additions & 3 deletions SharpPcap/IPcapDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ public interface IPcapDevice : IDisposable
/// <summary>
/// The last pcap error associated with this pcap device
/// </summary>
string LastError { get; }
string? LastError { get; }

/// <summary>
/// Kernel level filtering expression associated with this device.
/// For more info on filter expression syntax, see:
/// https://www.winpcap.org/docs/docs_412/html/group__language.html
/// </summary>
string Filter { get; set; }
string? Filter { get; set; }

/// <summary>
/// Mac address of the physical device
/// </summary>
System.Net.NetworkInformation.PhysicalAddress MacAddress { get; }
System.Net.NetworkInformation.PhysicalAddress? MacAddress { get; }

/// <summary>
/// Open the device. To start capturing call the 'StartCapture' function
Expand Down
10 changes: 4 additions & 6 deletions SharpPcap/LibPcap/BpfProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BpfProgram : SafeHandleZeroOrMinusOneIsInvalid
|| Pcap.LibpcapVersion >= new Version(1, 8, 0);
private static readonly object SyncCompile = new object();

public static BpfProgram TryCreate(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
public static BpfProgram? TryCreate(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
{
var bpfProgram = new BpfProgram();
int result;
Expand Down Expand Up @@ -67,12 +67,10 @@ public static BpfProgram Create(PcapHandle pcapHandle, string filter, int optimi
return bpfProgram;
}

public static BpfProgram TryCreate(LinkLayers linktype, string filter, int optimize = 1, uint netmask = 0)
public static BpfProgram? TryCreate(LinkLayers linktype, string filter, int optimize = 1, uint netmask = 0)
{
using (var handle = LibPcapSafeNativeMethods.pcap_open_dead((int)linktype, Pcap.MAX_PACKET_SIZE))
{
return TryCreate(handle, filter, optimize, netmask);
}
using var handle = LibPcapSafeNativeMethods.pcap_open_dead((int)linktype, Pcap.MAX_PACKET_SIZE);
return TryCreate(handle, filter, optimize, netmask);
}


Expand Down
5 changes: 3 additions & 2 deletions SharpPcap/LibPcap/CaptureFileWriterDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public override string Description
/// Constructor
/// </summary>
public CaptureFileWriterDevice(string captureFilename, System.IO.FileMode mode = FileMode.OpenOrCreate)
: base(null)
{
m_pcapFile = captureFilename;
fileMode = mode;
Expand Down Expand Up @@ -142,7 +143,7 @@ public override void Open(DeviceConfiguration configuration)
/// <returns>
/// A <see cref="PcapStatistics"/>
/// </returns>
public override ICaptureStatistics Statistics => null;
public override ICaptureStatistics? Statistics => null;

/// <summary>
/// Writes a packet to the pcap dump file associated with this device.
Expand Down Expand Up @@ -192,7 +193,7 @@ public void Write(RawCapture p)
Write(data, ref header);
}

void IInjectionDevice.SendPacket(ReadOnlySpan<byte> p, ICaptureHeader header)
void IInjectionDevice.SendPacket(ReadOnlySpan<byte> p, ICaptureHeader? header)
{
Write(p);
}
Expand Down
7 changes: 6 additions & 1 deletion SharpPcap/LibPcap/CaptureReaderDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace SharpPcap.LibPcap
/// </summary>
public abstract class CaptureReaderDevice : PcapDevice
{
protected CaptureReaderDevice()
: base(null)
{
}

/// <summary>
/// Retrieves pcap statistics.
///
Expand All @@ -16,6 +21,6 @@ public abstract class CaptureReaderDevice : PcapDevice
/// <returns>
/// A <see cref="PcapStatistics"/>
/// </returns>
public override ICaptureStatistics Statistics => null;
public override ICaptureStatistics? Statistics => null;
}
}
31 changes: 8 additions & 23 deletions SharpPcap/LibPcap/LibPcapLiveDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,8 @@ namespace SharpPcap.LibPcap
/// <summary>
/// Capture live packets from a network device
/// </summary>
public class LibPcapLiveDevice : PcapDevice, ILiveDevice
public class LibPcapLiveDevice(PcapInterface pcapInterface) : PcapDevice(pcapInterface), ILiveDevice
{
/// <summary>
/// Constructs a new PcapDevice based on a 'pcapIf' struct
/// </summary>
/// <param name="pcapIf">A 'pcapIf' struct representing
/// the pcap device</param>
public LibPcapLiveDevice(PcapInterface pcapIf)
{
m_pcapIf = pcapIf;
}

/// <summary>
/// Default contructor for subclasses
/// </summary>
protected LibPcapLiveDevice()
{
}

/// <summary>
/// PcapDevice finalizer. Ensure PcapDevices are stopped and closed before exit.
Expand All @@ -46,31 +30,31 @@ protected LibPcapLiveDevice()
/// </summary>
public override string Name
{
get { return m_pcapIf.Name; }
get { return pcapInterface.Name; }
}

/// <summary>
/// Addresses that represent this device
/// </summary>
public virtual ReadOnlyCollection<PcapAddress> Addresses
{
get { return new ReadOnlyCollection<PcapAddress>(m_pcapIf.Addresses); }
get { return new ReadOnlyCollection<PcapAddress>(pcapInterface.Addresses); }
}

/// <summary>
/// Gets the pcap description of this device
/// </summary>
public override string Description
{
get { return m_pcapIf.Description; }
get { return pcapInterface.Description; }
}

/// <summary>
/// Interface flags, see pcap_findalldevs() man page for more info
/// </summary>
public virtual uint Flags
{
get { return m_pcapIf.Flags; }
get { return pcapInterface.Flags; }
}

/// <summary>
Expand All @@ -93,7 +77,7 @@ public override void Open(DeviceConfiguration configuration)
{
return;
}
var credentials = configuration.Credentials ?? Interface.Credentials;
var credentials = configuration.Credentials ?? pcapInterface.Credentials;
var mode = configuration.Mode;

// Check if immediate is supported
Expand Down Expand Up @@ -267,6 +251,7 @@ public override void Open(DeviceConfiguration configuration)
private const int disableBlocking = 0;
private const int enableBlocking = 1;


/// <summary>
/// Set/Get Non-Blocking Mode. returns allways false for savefiles.
/// </summary>
Expand Down Expand Up @@ -311,7 +296,7 @@ public bool NonBlockingMode
/// Sends a raw packet through this device
/// </summary>
/// <param name="p">The packet bytes to send</param>
public void SendPacket(ReadOnlySpan<byte> p, ICaptureHeader header = null)
public void SendPacket(ReadOnlySpan<byte> p, ICaptureHeader? header = null)
{
ThrowIfNotOpen("Can't send packet, the device is closed");
int res;
Expand Down
Loading

0 comments on commit 27e4c7c

Please sign in to comment.