Skip to content

Commit 27e4c7c

Browse files
committed
Enable Nullable
1 parent f0e535a commit 27e4c7c

38 files changed

+188
-190
lines changed

Diff for: Examples/CreatingCaptureFile/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static void device_OnPacketArrival(object sender, PacketCapture e)
9898

9999
if (rawPacket.LinkLayerType == PacketDotNet.LinkLayers.Ethernet)
100100
{
101-
var packet = PacketDotNet.Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);
101+
var packet = Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);
102102
var ethernetPacket = (EthernetPacket)packet;
103103

104104
Console.WriteLine("{0} At: {1}:{2}: MAC:{3} -> MAC:{4}",

Diff for: SharpPcap/ARP.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ARP
2222
/// <param name="device">The network device on which this resolver sends its ARP packets</param>
2323
public ARP(LibPcapLiveDevice device)
2424
{
25-
pcapInterface = device.Interface;
25+
pcapInterface = device.Interface ?? throw new ArgumentException();
2626
}
2727

2828
/// <summary>
@@ -37,7 +37,7 @@ public ARP(LibPcapLiveDevice device)
3737
/// <param name="destIP">The IP address to resolve</param>
3838
/// <returns>The MAC address that matches to the given IP address or
3939
/// null if there was a timeout</returns>
40-
public PhysicalAddress Resolve(System.Net.IPAddress destIP)
40+
public PhysicalAddress? Resolve(System.Net.IPAddress destIP)
4141
{
4242
return Resolve(destIP, null, null);
4343
}
@@ -50,9 +50,9 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP)
5050
/// <param name="localMAC">The localMAC address to use, if null the local mac will be discovered</param>
5151
/// <returns>The MAC address that matches to the given IP address or
5252
/// null if there was a timeout</returns>
53-
public PhysicalAddress Resolve(System.Net.IPAddress destIP,
54-
System.Net.IPAddress localIP,
55-
PhysicalAddress localMAC)
53+
public PhysicalAddress? Resolve(System.Net.IPAddress destIP,
54+
System.Net.IPAddress? localIP,
55+
PhysicalAddress? localMAC)
5656
{
5757
// if no local ip address is specified attempt to find one from the adapter
5858
if (localIP == null)
@@ -61,10 +61,10 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP,
6161
// ARP is ipv4, NDP is used for ipv6
6262
foreach (var address in pcapInterface.Addresses)
6363
{
64-
if (address.Addr.type == Sockaddr.AddressTypes.AF_INET_AF_INET6)
64+
if (address.Addr?.type == Sockaddr.AddressTypes.AF_INET_AF_INET6)
6565
{
6666
// make sure the address is ipv4
67-
if (address.Addr.ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
67+
if (address.Addr?.ipAddress?.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
6868
{
6969
localIP = address.Addr.ipAddress;
7070
break; // break out of the foreach
@@ -84,7 +84,7 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP,
8484
{
8585
foreach (var address in pcapInterface.Addresses)
8686
{
87-
if (address.Addr.type == Sockaddr.AddressTypes.HARDWARE)
87+
if (address.Addr?.type == Sockaddr.AddressTypes.HARDWARE)
8888
{
8989
localMAC = address.Addr.hardwareAddress;
9090
}
@@ -103,7 +103,7 @@ public PhysicalAddress Resolve(System.Net.IPAddress destIP,
103103
}
104104
}
105105

106-
internal static PhysicalAddress Resolve(
106+
internal static PhysicalAddress? Resolve(
107107
ILiveDevice device,
108108
System.Net.IPAddress destIP,
109109
System.Net.IPAddress localIP,
@@ -125,7 +125,7 @@ internal static PhysicalAddress Resolve(
125125

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

128-
PacketDotNet.ArpPacket arpPacket = null;
128+
PacketDotNet.ArpPacket? arpPacket = null;
129129

130130
// attempt to resolve the address with the current timeout
131131
var timeoutDateTime = DateTime.Now + timeout;
@@ -171,7 +171,7 @@ internal static PhysicalAddress Resolve(
171171
else
172172
{
173173
//return the resolved MAC address
174-
return arpPacket.SenderHardwareAddress;
174+
return arpPacket?.SenderHardwareAddress;
175175
}
176176
}
177177

Diff for: SharpPcap/BaseLiveDevice.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,39 @@ namespace SharpPcap
1818
public abstract class BaseLiveDevice : IDisposable
1919
{
2020

21-
private CancellationTokenSource TokenSource;
22-
private Task CaptureTask;
21+
private CancellationTokenSource? TokenSource;
22+
private Task? CaptureTask;
2323
public bool Started => CaptureTask?.IsCompleted == false;
2424

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

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

29-
public ICaptureStatistics Statistics => null;
29+
public ICaptureStatistics? Statistics => null;
3030

3131
public TimestampResolution TimestampResolution => TimestampResolution.Microsecond;
3232

3333
public virtual LinkLayers LinkType => LinkLayers.Ethernet;
3434

35-
public event PacketArrivalEventHandler OnPacketArrival;
36-
public event CaptureStoppedEventHandler OnCaptureStopped;
35+
public event PacketArrivalEventHandler? OnPacketArrival;
36+
public event CaptureStoppedEventHandler? OnCaptureStopped;
3737

38-
protected BpfProgram FilterProgram;
39-
private string FilterValue;
40-
public string Filter
38+
protected BpfProgram? FilterProgram;
39+
private string? FilterValue;
40+
public string? Filter
4141
{
4242
get => FilterValue;
4343
set
4444
{
45-
using (var pcapHandle = LibPcapSafeNativeMethods.pcap_open_dead((int)LinkType, Pcap.MAX_PACKET_SIZE))
45+
if (value == null)
4646
{
47-
FilterProgram = BpfProgram.Create(pcapHandle, value);
48-
FilterValue = value;
47+
FilterValue = null;
48+
FilterProgram = null;
49+
return;
4950
}
51+
using var pcapHandle = LibPcapSafeNativeMethods.pcap_open_dead((int)LinkType, Pcap.MAX_PACKET_SIZE);
52+
FilterProgram = BpfProgram.Create(pcapHandle, value);
53+
FilterValue = value;
5054
}
5155
}
5256

Diff for: SharpPcap/CaptureDeviceExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static void SendPacket(this IInjectionDevice device, Packet p, int size)
7979
/// <param name="device"></param>
8080
/// <param name="p"></param>
8181
/// <param name="header"></param>
82-
public static void SendPacket(this IInjectionDevice device, RawCapture p, ICaptureHeader header = null)
82+
public static void SendPacket(this IInjectionDevice device, RawCapture p, ICaptureHeader? header = null)
8383
{
8484
device.SendPacket(new ReadOnlySpan<byte>(p.Data), header);
8585
}

Diff for: SharpPcap/CaptureDeviceList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SharpPcap
1313
/// </summary>
1414
public class CaptureDeviceList : ReadOnlyCollection<ILiveDevice>
1515
{
16-
private static CaptureDeviceList instance;
16+
private static CaptureDeviceList? instance;
1717

1818
private LibPcap.LibPcapLiveDeviceList libPcapDeviceList;
1919

Diff for: SharpPcap/DeviceConfiguration.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class DeviceConfiguration
2424

2525
public int? KernelBufferSize { get; set; }
2626

27-
public RemoteAuthentication Credentials { get; set; }
27+
public RemoteAuthentication? Credentials { get; set; }
2828

2929
public bool? Immediate { get; set; }
3030

@@ -41,7 +41,7 @@ public class DeviceConfiguration
4141

4242
public TimestampType? TimestampType { get; set; }
4343

44-
public event EventHandler<ConfigurationFailedEventArgs> ConfigurationFailed;
44+
public event EventHandler<ConfigurationFailedEventArgs>? ConfigurationFailed;
4545

4646
internal void RaiseConfigurationFailed(string property, PcapError error, string message)
4747
{
@@ -69,8 +69,8 @@ internal void RaiseConfigurationFailed(string property, PcapError error, string
6969

7070
public class ConfigurationFailedEventArgs : EventArgs
7171
{
72-
public PcapError Error { get; internal set; }
73-
public string Property { get; internal set; }
74-
public string Message { get; internal set; }
72+
public PcapError Error { get; internal init; }
73+
public required string Property { get; init; }
74+
public required string Message { get; init; }
7575
}
7676
}

Diff for: SharpPcap/ICaptureDevice.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public interface ICaptureDevice : IPcapDevice
6464
///
6565
/// Devices that lack statistics support return null
6666
/// </summary>
67-
ICaptureStatistics Statistics { get; }
67+
ICaptureStatistics? Statistics { get; }
6868

6969
#region Timestamp
7070
/// <summary>

Diff for: SharpPcap/IInjectionDevice.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IInjectionDevice : IPcapDevice
1212
/// </summary>
1313
/// <param name="p">The packet bytes to send</param>
1414
/// <param name="size">The number of bytes to send</param>
15-
void SendPacket(ReadOnlySpan<byte> p, ICaptureHeader header = null);
15+
void SendPacket(ReadOnlySpan<byte> p, ICaptureHeader? header = null);
1616
}
1717
}
1818

Diff for: SharpPcap/IPcapDevice.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ public interface IPcapDevice : IDisposable
2323
/// <summary>
2424
/// The last pcap error associated with this pcap device
2525
/// </summary>
26-
string LastError { get; }
26+
string? LastError { get; }
2727

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

3535
/// <summary>
3636
/// Mac address of the physical device
3737
/// </summary>
38-
System.Net.NetworkInformation.PhysicalAddress MacAddress { get; }
38+
System.Net.NetworkInformation.PhysicalAddress? MacAddress { get; }
3939

4040
/// <summary>
4141
/// Open the device. To start capturing call the 'StartCapture' function

Diff for: SharpPcap/LibPcap/BpfProgram.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class BpfProgram : SafeHandleZeroOrMinusOneIsInvalid
2222
|| Pcap.LibpcapVersion >= new Version(1, 8, 0);
2323
private static readonly object SyncCompile = new object();
2424

25-
public static BpfProgram TryCreate(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
25+
public static BpfProgram? TryCreate(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
2626
{
2727
var bpfProgram = new BpfProgram();
2828
int result;
@@ -67,12 +67,10 @@ public static BpfProgram Create(PcapHandle pcapHandle, string filter, int optimi
6767
return bpfProgram;
6868
}
6969

70-
public static BpfProgram TryCreate(LinkLayers linktype, string filter, int optimize = 1, uint netmask = 0)
70+
public static BpfProgram? TryCreate(LinkLayers linktype, string filter, int optimize = 1, uint netmask = 0)
7171
{
72-
using (var handle = LibPcapSafeNativeMethods.pcap_open_dead((int)linktype, Pcap.MAX_PACKET_SIZE))
73-
{
74-
return TryCreate(handle, filter, optimize, netmask);
75-
}
72+
using var handle = LibPcapSafeNativeMethods.pcap_open_dead((int)linktype, Pcap.MAX_PACKET_SIZE);
73+
return TryCreate(handle, filter, optimize, netmask);
7674
}
7775

7876

Diff for: SharpPcap/LibPcap/CaptureFileWriterDevice.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public override string Description
6363
/// Constructor
6464
/// </summary>
6565
public CaptureFileWriterDevice(string captureFilename, System.IO.FileMode mode = FileMode.OpenOrCreate)
66+
: base(null)
6667
{
6768
m_pcapFile = captureFilename;
6869
fileMode = mode;
@@ -142,7 +143,7 @@ public override void Open(DeviceConfiguration configuration)
142143
/// <returns>
143144
/// A <see cref="PcapStatistics"/>
144145
/// </returns>
145-
public override ICaptureStatistics Statistics => null;
146+
public override ICaptureStatistics? Statistics => null;
146147

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

195-
void IInjectionDevice.SendPacket(ReadOnlySpan<byte> p, ICaptureHeader header)
196+
void IInjectionDevice.SendPacket(ReadOnlySpan<byte> p, ICaptureHeader? header)
196197
{
197198
Write(p);
198199
}

Diff for: SharpPcap/LibPcap/CaptureReaderDevice.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace SharpPcap.LibPcap
88
/// </summary>
99
public abstract class CaptureReaderDevice : PcapDevice
1010
{
11+
protected CaptureReaderDevice()
12+
: base(null)
13+
{
14+
}
15+
1116
/// <summary>
1217
/// Retrieves pcap statistics.
1318
///
@@ -16,6 +21,6 @@ public abstract class CaptureReaderDevice : PcapDevice
1621
/// <returns>
1722
/// A <see cref="PcapStatistics"/>
1823
/// </returns>
19-
public override ICaptureStatistics Statistics => null;
24+
public override ICaptureStatistics? Statistics => null;
2025
}
2126
}

Diff for: SharpPcap/LibPcap/LibPcapLiveDevice.cs

+8-23
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,8 @@ namespace SharpPcap.LibPcap
1414
/// <summary>
1515
/// Capture live packets from a network device
1616
/// </summary>
17-
public class LibPcapLiveDevice : PcapDevice, ILiveDevice
17+
public class LibPcapLiveDevice(PcapInterface pcapInterface) : PcapDevice(pcapInterface), ILiveDevice
1818
{
19-
/// <summary>
20-
/// Constructs a new PcapDevice based on a 'pcapIf' struct
21-
/// </summary>
22-
/// <param name="pcapIf">A 'pcapIf' struct representing
23-
/// the pcap device</param>
24-
public LibPcapLiveDevice(PcapInterface pcapIf)
25-
{
26-
m_pcapIf = pcapIf;
27-
}
28-
29-
/// <summary>
30-
/// Default contructor for subclasses
31-
/// </summary>
32-
protected LibPcapLiveDevice()
33-
{
34-
}
3519

3620
/// <summary>
3721
/// PcapDevice finalizer. Ensure PcapDevices are stopped and closed before exit.
@@ -46,31 +30,31 @@ protected LibPcapLiveDevice()
4630
/// </summary>
4731
public override string Name
4832
{
49-
get { return m_pcapIf.Name; }
33+
get { return pcapInterface.Name; }
5034
}
5135

5236
/// <summary>
5337
/// Addresses that represent this device
5438
/// </summary>
5539
public virtual ReadOnlyCollection<PcapAddress> Addresses
5640
{
57-
get { return new ReadOnlyCollection<PcapAddress>(m_pcapIf.Addresses); }
41+
get { return new ReadOnlyCollection<PcapAddress>(pcapInterface.Addresses); }
5842
}
5943

6044
/// <summary>
6145
/// Gets the pcap description of this device
6246
/// </summary>
6347
public override string Description
6448
{
65-
get { return m_pcapIf.Description; }
49+
get { return pcapInterface.Description; }
6650
}
6751

6852
/// <summary>
6953
/// Interface flags, see pcap_findalldevs() man page for more info
7054
/// </summary>
7155
public virtual uint Flags
7256
{
73-
get { return m_pcapIf.Flags; }
57+
get { return pcapInterface.Flags; }
7458
}
7559

7660
/// <summary>
@@ -93,7 +77,7 @@ public override void Open(DeviceConfiguration configuration)
9377
{
9478
return;
9579
}
96-
var credentials = configuration.Credentials ?? Interface.Credentials;
80+
var credentials = configuration.Credentials ?? pcapInterface.Credentials;
9781
var mode = configuration.Mode;
9882

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

254+
270255
/// <summary>
271256
/// Set/Get Non-Blocking Mode. returns allways false for savefiles.
272257
/// </summary>
@@ -311,7 +296,7 @@ public bool NonBlockingMode
311296
/// Sends a raw packet through this device
312297
/// </summary>
313298
/// <param name="p">The packet bytes to send</param>
314-
public void SendPacket(ReadOnlySpan<byte> p, ICaptureHeader header = null)
299+
public void SendPacket(ReadOnlySpan<byte> p, ICaptureHeader? header = null)
315300
{
316301
ThrowIfNotOpen("Can't send packet, the device is closed");
317302
int res;

0 commit comments

Comments
 (0)