Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
trivalik authored and robo committed Jan 2, 2025
1 parent 414ff0a commit 05d6c69
Show file tree
Hide file tree
Showing 36 changed files with 74 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public LivePacketDeviceTests()
#endif

[Fact]
public void SendAndReceievePacketTest()
public void SendAndReceivePacketTest()
{
const string SourceMac = "11:22:33:44:55:66";
const string DestinationMac = "77:88:99:AA:BB:CC";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void SetSamplingMethodOneEveryNTest()
public void SetSamplingMethodFirstAfterIntervalTest()
{
const int NumPackets = 10;

Packet expectedPacket = _random.NextEthernetPacket(100);
using (PacketCommunicator communicator = OpenOfflineDevice(NumPackets, expectedPacket, TimeSpan.FromSeconds(1)))
{
Expand Down Expand Up @@ -338,7 +338,7 @@ public void DumpToBadFileTest()
{
Assert.Throws<InvalidOperationException>(() => OpenOfflineDevice(10, _random.NextEthernetPacket(100), TimeSpan.Zero, "??"));
}

[Fact]
public void EmptyNameTest()
{
Expand Down Expand Up @@ -404,7 +404,6 @@ public void ReadUnicodeFilenameTest()
Assert.True(File.Exists(ReadUnicodeFilename), string.Format("File {0} doesn't exist", ReadUnicodeFilename));
}


[Fact]
public void ReadNonExistingUnicodeFilenameTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private static void ComparePacketsToWireshark(IEnumerable<Packet> packets)
{
// Wireshark's preferences file is %APPDATA%\Wireshark\preferences
FileName = Path.Combine(wiresharkDirectory, WiresharkTshark),
Arguments = "-o ip.check_checksum:TRUE " +
Arguments = "-o ip.check_checksum:TRUE " +
"-o ipv6.use_geoip:FALSE " +
"-o udp.check_checksum:TRUE " +
"-o tcp.relative_sequence_numbers:FALSE " +
Expand Down
2 changes: 1 addition & 1 deletion PcapDotNet/src/PcapDotNet.Core/BerkeleyPacketFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Dispose()

/// <summary>
/// Returns if a given filter applies to an offline packet.
/// This method is used to apply a filter to a packet that is currently in memory.
/// This method is used to apply a filter to a packet that is currently in memory.
/// This process does not need to open an adapter; we need just to create the proper filter (by settings parameters like the snapshot length, or the link-layer type) by means of the Pcap.
/// The current API of libpcap does not allow to receive a packet and to filter the packet after it has been received. However, this can be useful in case you want to filter packets in the application, instead of into the receiving process. This function allows you to do the job.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion PcapDotNet/src/PcapDotNet.Core/Native/PcapUnixPal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ internal extern static int pcap_getnonblock(
internal extern static int pcap_minor_version(PcapHandle /* pcap_t* */ p);

/// <summary>
/// pcap_activate() is used to activate a packet capture handle to look at packets on the network, with the options that were set on the handle being in effect.
/// pcap_activate() is used to activate a packet capture handle to look at packets on the network, with the options that were set on the handle being in effect.
/// </summary>
/// <returns>Returns 0 on success without warnings, a non-zero positive value on success with warnings, and a negative value on error. A non-zero return value indicates what warning or error condition occurred.</returns>
[DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace PcapDotNet.Core
public delegate void HandleStatistics(PacketSampleStatistics statistics);

/// <summary>
/// Used to receive and send packets accross the network or to read and write packets to a pcap file.
/// Used to receive and send packets across the network or to read and write packets to a pcap file.
/// </summary>
public abstract class PacketCommunicator : IDisposable
{
Expand All @@ -34,13 +34,13 @@ protected PacketCommunicator(SocketAddress netmask)
}

/// <summary>
/// Close the files associated with the capture and deallocates resources.
/// Close the files associated with the capture and deallocates resources.
/// </summary>
protected virtual void Dispose(bool disposing)
{
PcapDescriptor.Dispose();
}

/// <inheritdoc/>
public void Dispose()
{
Expand Down Expand Up @@ -110,7 +110,7 @@ public ReadOnlyCollection<PcapDataLink> SupportedDataLinks
}

/// <summary>
/// The dimension of the packet portion (in bytes) that is delivered to the application.
/// The dimension of the packet portion (in bytes) that is delivered to the application.
/// </summary>
public int SnapshotLength { get => Interop.Pcap.pcap_snapshot(PcapDescriptor); }

Expand Down Expand Up @@ -230,12 +230,12 @@ public void SetKernelMinimumBytesToCopy(int size)

/// <summary>
/// Define a sampling method for packet capture.
/// This function allows applying a sampling method to the packet capture process.
/// This function allows applying a sampling method to the packet capture process.
/// The method will be applied as soon as the capture starts.
/// </summary>
/// <remarks>
/// Warning: Sampling parameters cannot be changed when a capture is active. These parameters must be applied before starting the capture. If they are applied when the capture is in progress, the new settings are ignored.
/// Warning: Sampling works only when capturing data on Win32 or reading from a file. It has not been implemented on other platforms. Sampling works on remote machines provided that the probe (i.e. the capturing device) is a Win32 workstation.
/// Warning: Sampling works only when capturing data on Win32 or reading from a file. It has not been implemented on other platforms. Sampling works on remote machines provided that the probe (i.e. the capturing device) is a Win32 workstation.
/// </remarks>
/// <param name="method">The sampling method to be applied</param>
public void SetSamplingMethod(SamplingMethod method)
Expand Down Expand Up @@ -290,7 +290,7 @@ public PacketCommunicatorReceiveResult ReceivePacket(out Packet packet)

/// <summary>
/// Collect a group of packets.
/// Used to collect and process packets.
/// Used to collect and process packets.
/// <seealso cref="ReceivePacket"/>
/// <seealso cref="ReceivePackets"/>
/// <seealso cref="Break"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace PcapDotNet.Core
{
Expand Down Expand Up @@ -31,7 +31,7 @@ public abstract class SamplingMethod
public const int PCAP_SAMP_FIRST_AFTER_N_MS = 2;

internal abstract int Method { get; }

internal abstract int Value { get; }
}

Expand All @@ -40,7 +40,7 @@ public abstract class SamplingMethod
/// In other words, if the interval is set to 10 milliseconds, the first packet is returned to the caller; the next returned one will be the first packet that arrives when 10ms have elapsed.
/// </summary>
public sealed class SamplingMethodFirstAfterInterval : SamplingMethod
{
{
private readonly int _intervalInMilliseconds;

/// <summary>
Expand Down Expand Up @@ -82,7 +82,7 @@ public SamplingMethodFirstAfterInterval(TimeSpan interval)

/// <summary>
/// No sampling has to be done on the current capture.
/// In this case, no sampling algorithms are applied to the current capture.
/// In this case, no sampling algorithms are applied to the current capture.
/// </summary>
public sealed class SamplingMethodNone : SamplingMethod
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class LivePacketDevice : PacketDevice
/// </returns>
/// <exception cref="InvalidOperationException">
/// Thrown if some errors occurred.
/// An error could be due to several reasons:
/// An error could be due to several reasons:
/// <list type="bullet">
/// <item>libpcap/WinPcap was not installed on the local/remote host.</item>
/// <item>The user does not have enough privileges to list the devices.</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace PcapDotNet.Core
public sealed class OfflinePacketDevice : PacketDevice
{
private readonly string _fileName;

/// <summary>
/// Creates a device object from a pcap file.
/// The device can opened to read packets from.
Expand Down Expand Up @@ -39,7 +39,7 @@ public override string Description
}

/// <summary>
/// Interface flags. Currently the only possible flag is Loopback, that is set if the interface is a loopback interface.
/// Interface flags. Currently the only possible flag is Loopback, that is set if the interface is a loopback interface.
/// </summary>
public override DeviceAttributes Attributes
{
Expand All @@ -55,7 +55,7 @@ public override ReadOnlyCollection<DeviceAddress> Addresses
}

/// <summary>
/// Open a generic source in order to capture / send (WinPcap only) traffic.
/// Open a generic source in order to capture / send (WinPcap only) traffic.
/// </summary>
/// <param name="snapshotLength">length of the packet that has to be retained. For each packet received by the filter, only the first 'snapshotLength' bytes are stored in the buffer and passed to the user application. For instance, snaplen equal to 100 means that only the first 100 bytes of each packet are stored.</param>
/// <param name="attributes">Keeps several flags that can be needed for capturing packets.</param>
Expand All @@ -65,5 +65,5 @@ public override PacketCommunicator Open(int snapshotLength, PacketDeviceOpenAttr
{
return new OfflinePacketCommunicator(_fileName);
}
};
}
}
8 changes: 4 additions & 4 deletions PcapDotNet/src/PcapDotNet.Core/PacketDevice/PacketDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected PacketDevice() { }
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

/// <summary>
/// This snapshort length value should be sufficient, on most if not all networks, to capture all the data available from the packet.
/// This snapshot length value should be sufficient, on most if not all networks, to capture all the data available from the packet.
/// </summary>
public const int DefaultSnapshotLength = 65536;

Expand All @@ -28,7 +28,7 @@ protected PacketDevice() { }
public abstract string Description { get; }

/// <summary>
/// Interface flags. Currently the only possible flag is Loopback, that is set if the interface is a loopback interface.
/// Interface flags. Currently the only possible flag is Loopback, that is set if the interface is a loopback interface.
/// </summary>
public abstract DeviceAttributes Attributes { get; }

Expand All @@ -47,8 +47,8 @@ protected PacketDevice() { }
public abstract PacketCommunicator Open(int snapshotLength, PacketDeviceOpenAttributes attributes, int readTimeout);

/// <summary>
/// Open a generic source in order to capture / send (WinPcap only) traffic.
/// Uses maxmimum snapshotLength (65536), promiscuous mode and 1 second read timeout.
/// Open a generic source in order to capture / send (WinPcap only) traffic.
/// Uses maximum snapshotLength (65536), promiscuous mode and 1 second read timeout.
/// </summary>
public virtual PacketCommunicator Open()
{
Expand Down
2 changes: 1 addition & 1 deletion PcapDotNet/src/PcapDotNet.Core/PacketSendBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Enqueue(Packet packet)
{
throw new ArgumentNullException(nameof(packet));
}

var hdrSize = Interop.Pcap.PcapHeaderSize;
if (hdrSize + packet.Length > _buffer.Length - _currentBufferPosition)
{
Expand Down
2 changes: 1 addition & 1 deletion PcapDotNet/src/PcapDotNet.Core/PacketTotalStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace PcapDotNet.Core
{
/// <summary>
/// Statistics on capture from the start of the run.
/// Statistics on capture from the start of the run.
/// </summary>
public sealed class PacketTotalStatistics : IEquatable<PacketTotalStatistics>
{
Expand Down
2 changes: 1 addition & 1 deletion PcapDotNet/src/PcapDotNet.Core/PcapDataLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public override string ToString()
{
return Name + " (" + Description + ")";
}

/// <inheritdoc/>
public bool Equals(PcapDataLink other)
{
Expand Down
6 changes: 3 additions & 3 deletions PcapDotNet/src/PcapDotNet.Packets.Test/DnsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void DnsDomainNameCompressionTest()
{
DnsLayer dnsLayer = new DnsLayer();
TestDomainNameCompression(0, dnsLayer);

dnsLayer.Queries = new List<DnsQueryResourceRecord>();
dnsLayer.Answers = new List<DnsDataResourceRecord>();
dnsLayer.Authorities = new List<DnsDataResourceRecord>();
Expand Down Expand Up @@ -181,7 +181,7 @@ public void DnsOptResourceRecordTest()
DnsResourceDataOptions data = (DnsResourceDataOptions)random.NextDnsResourceData(DnsType.Opt);

DnsOptResourceRecord record = new DnsOptResourceRecord(domainName, sendersUdpPayloadSize, extendedRcode, version, flags, data);

Assert.Equal(domainName, record.DomainName);
Assert.Equal(sendersUdpPayloadSize, record.SendersUdpPayloadSize);
Assert.Equal(extendedRcode, record.ExtendedReturnCode);
Expand Down Expand Up @@ -723,7 +723,7 @@ public void DnsResourceDataDelegationSignerConstructorNullDigestTest()
{
Assert.Throws<ArgumentNullException>(() => new DnsResourceDataDelegationSigner(1, DnsAlgorithm.PrivateDns, DnsDigestType.Sha1, null));
}

[Fact]
public void DnsResourceDataDomainNameParseWrongLengthTest()
{
Expand Down
21 changes: 10 additions & 11 deletions PcapDotNet/src/PcapDotNet.Packets/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Numerics;
using System.Text;
Expand Down Expand Up @@ -141,7 +140,7 @@ public static void BlockCopy(this byte[] source, int sourceOffset, byte[] destin
/// <returns>The value read from the buffer.</returns>
public static byte ReadByte(this byte[] buffer, int offset)
{
if (buffer == null)
if (buffer == null)
throw new ArgumentNullException("buffer");
return buffer[offset];
}
Expand Down Expand Up @@ -387,7 +386,7 @@ public static UInt128 ReadUInt128(this byte[] buffer, int offset, Endianity endi
/// <returns>The value converted from the read bytes according to the endianity.</returns>
public static BigInteger ReadUnsignedBigInteger(this byte[] buffer, int offset, int length, Endianity endianity)
{
if (buffer == null)
if (buffer == null)
throw new ArgumentNullException("buffer");

BigInteger value = BigInteger.Zero;
Expand Down Expand Up @@ -494,7 +493,7 @@ public static IpV6Address ReadIpV6Address(this byte[] buffer, int offset, Endian
/// <param name="value">The value to write.</param>
public static void Write(this byte[] buffer, int offset, byte value)
{
if (buffer == null)
if (buffer == null)
throw new ArgumentNullException("buffer");

buffer[offset] = value;
Expand All @@ -520,9 +519,9 @@ public static void Write(this byte[] buffer, ref int offset, byte value)
/// <param name="value">The value to write.</param>
public static void Write(this byte[] buffer, ref int offset, IEnumerable<byte> value)
{
if (buffer == null)
if (buffer == null)
throw new ArgumentNullException("buffer");
if (value == null)
if (value == null)
throw new ArgumentNullException("value");

foreach (byte b in value)
Expand Down Expand Up @@ -751,9 +750,9 @@ public static void Write(this byte[] buffer, ref int offset, UInt128 value, Endi
/// <param name="endianity">The endianity to use when converting the value to bytes.</param>
public static void WriteUnsigned(this byte[] buffer, int offset, BigInteger value, int length, Endianity endianity)
{
if (buffer == null)
if (buffer == null)
throw new ArgumentNullException("buffer");

if (value.Sign < 0)
throw new ArgumentOutOfRangeException("value", value, "Must be non-negative.");
for (int i = 0; i != length && value != BigInteger.Zero; ++i, value >>= 8)
Expand Down Expand Up @@ -801,7 +800,7 @@ public static void Write(this byte[] buffer, int offset, DataSegment value)
/// <param name="value">The value to write.</param>
public static void Write(this byte[] buffer, ref int offset, DataSegment value)
{
if (value == null)
if (value == null)
throw new ArgumentNullException("value");

value.Write(buffer, offset);
Expand Down Expand Up @@ -967,7 +966,7 @@ private static UInt48 HostToNetworkOrder(UInt48 value)
{
UInt48* resultPtr = &result;
byte* resultBytePtr = (byte*)resultPtr;

UInt48* valuePtr = &value;
byte* valueBytePtr = (byte*)valuePtr;

Expand Down Expand Up @@ -1088,7 +1087,7 @@ private static void Write(byte[] buffer, int offset, UInt24 value)
}
}
}

private static void Write(byte[] buffer, int offset, int value)
{
unsafe
Expand Down
1 change: 0 additions & 1 deletion PcapDotNet/src/PcapDotNet.Packets/DataSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using PcapDotNet.Base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using PcapDotNet.Base;

namespace PcapDotNet.Packets.Dns
{
Expand All @@ -14,7 +12,7 @@ namespace PcapDotNet.Packets.Dns
/// </summary>
public abstract class DnsResourceData
{
internal const int StringMinimumLength = sizeof(byte);
internal const int StringMinimumLength = sizeof(byte);

/// <summary>
/// Returns the DnsResourceData concrete type that should be created for the given DnsType.
Expand Down
Loading

0 comments on commit 05d6c69

Please sign in to comment.