Skip to content

Commit ae5024d

Browse files
authored
Don't use lower case class names (#632)
1 parent af3ea21 commit ae5024d

13 files changed

+65
-100
lines changed

SharpPcap/LibPcap/BpfProgram.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static BpfProgram Create(LinkLayers linktype, string filter, int optimize
8585
private BpfProgram()
8686
: base(true)
8787
{
88-
var bpfProgram = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.bpf_program)));
88+
var bpfProgram = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.BpfProgram)));
8989
SetHandle(bpfProgram);
9090
}
9191

SharpPcap/LibPcap/LibPcapSafeNativeMethods.Encoding.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public unsafe object MarshalNativeToManaged(IntPtr nativeData)
117117
}
118118
}
119119

120-
[StructLayout(LayoutKind.Sequential)]
121120
internal struct ErrorBuffer
122121
{
123122
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]

SharpPcap/LibPcap/LibPcapSafeNativeMethods.Interop.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ out ErrorBuffer /* char* */ errbuf
3939
[DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)]
4040
internal extern static int pcap_findalldevs_ex(
4141
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PcapStringMarshaler))] string /*char **/source,
42-
ref pcap_rmtauth /*pcap_rmtauth **/auth,
42+
ref PcapRmtAuth /*pcap_rmtauth **/auth,
4343
ref IntPtr /*pcap_if_t ** */alldevs,
4444
out ErrorBuffer /* char* */ errbuf
4545
);
@@ -53,7 +53,7 @@ out ErrorBuffer /* char* */ errbuf
5353
int packetLen,
5454
int flags,
5555
int read_timeout,
56-
ref pcap_rmtauth rmtauth,
56+
ref PcapRmtAuth rmtauth,
5757
out ErrorBuffer /* char* */ errbuf
5858
);
5959

@@ -235,7 +235,9 @@ out ErrorBuffer /* char* */ errbuf
235235
/// A <see cref="int"/>
236236
/// </returns>
237237
[DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)]
238-
internal extern static int pcap_stats(PcapHandle /* pcap_t* */ adapter, IntPtr /* struct pcap_stat* */ stat);
238+
internal extern static int pcap_stats(PcapHandle /* pcap_t* */ adapter, ref PcapStatUnix stat);
239+
[DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)]
240+
internal extern static int pcap_stats(PcapHandle /* pcap_t* */ adapter, ref PcapStatWindows stat);
239241

240242
/// <summary>
241243
/// Returns the snapshot length
@@ -336,7 +338,7 @@ out ErrorBuffer /* char* */ errbuf
336338
/// during the send. The error can be caused by a driver/adapter
337339
/// problem or by an inconsistent/bogus send queue.</returns>
338340
[DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)]
339-
internal extern static int pcap_sendqueue_transmit(PcapHandle /*pcap_t * */p, ref pcap_send_queue queue, int sync);
341+
internal extern static int pcap_sendqueue_transmit(PcapHandle /*pcap_t * */p, ref PcapSendQueue queue, int sync);
340342
#endregion
341343

342344
#region Timestamp related functions

SharpPcap/LibPcap/PcapAddress.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class PcapAddress
3636
internal PcapAddress()
3737
{ }
3838

39-
internal PcapAddress(PcapUnmanagedStructures.pcap_addr pcap_addr)
39+
internal PcapAddress(PcapUnmanagedStructures.PcapAddr pcap_addr)
4040
{
4141
if (pcap_addr.Addr != IntPtr.Zero)
4242
Addr = new Sockaddr(pcap_addr.Addr);

SharpPcap/LibPcap/PcapHeader.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ private static int GetTimevalSize()
2626
{
2727
if (is32BitTs)
2828
{
29-
return Marshal.SizeOf<timeval_windows>();
29+
return Marshal.SizeOf<TimevalWindows>();
3030
}
3131
if (isMacOSX)
3232
{
33-
return Marshal.SizeOf<timeval_macosx>();
33+
return Marshal.SizeOf<TimevalMacosx>();
3434
}
35-
return Marshal.SizeOf<timeval_unix>();
35+
return Marshal.SizeOf<TimevalUnix>();
3636
}
3737

3838
/// <summary>
@@ -44,19 +44,19 @@ private unsafe PcapHeader(IntPtr pcap_pkthdr, TimestampResolution resolution)
4444
ulong tv_usec;
4545
if (is32BitTs)
4646
{
47-
var ts = *(timeval_windows*)pcap_pkthdr;
47+
var ts = *(TimevalWindows*)pcap_pkthdr;
4848
tv_sec = (ulong)ts.tv_sec;
4949
tv_usec = (ulong)ts.tv_usec;
5050
}
5151
else if (isMacOSX)
5252
{
53-
var ts = *(timeval_macosx*)pcap_pkthdr;
53+
var ts = *(TimevalMacosx*)pcap_pkthdr;
5454
tv_sec = (ulong)ts.tv_sec;
5555
tv_usec = (ulong)ts.tv_usec;
5656
}
5757
else
5858
{
59-
var ts = *(timeval_unix*)pcap_pkthdr;
59+
var ts = *(TimevalUnix*)pcap_pkthdr;
6060
tv_sec = (ulong)ts.tv_sec;
6161
tv_usec = (ulong)ts.tv_usec;
6262
}
@@ -119,7 +119,7 @@ public IntPtr MarshalToIntPtr(TimestampResolution resolution)
119119
if (is32BitTs)
120120
{
121121
// setup the structure to marshal
122-
var timeval = new timeval_windows
122+
var timeval = new TimevalWindows
123123
{
124124
tv_sec = (int)tv_sec,
125125
tv_usec = (int)tv_usec
@@ -128,7 +128,7 @@ public IntPtr MarshalToIntPtr(TimestampResolution resolution)
128128
}
129129
else if (isMacOSX)
130130
{
131-
var timeval = new timeval_macosx
131+
var timeval = new TimevalMacosx
132132
{
133133
tv_sec = (IntPtr)tv_sec,
134134
tv_usec = (int)tv_usec
@@ -137,7 +137,7 @@ public IntPtr MarshalToIntPtr(TimestampResolution resolution)
137137
}
138138
else
139139
{
140-
var timeval = new timeval_unix
140+
var timeval = new TimevalUnix
141141
{
142142
tv_sec = (IntPtr)tv_sec,
143143
tv_usec = (IntPtr)tv_usec

SharpPcap/LibPcap/PcapInterface.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class PcapInterface
6363
/// </summary>
6464
public PhysicalAddress? MacAddress { get; }
6565

66-
internal PcapInterface(pcap_if pcapIf, NetworkInterface? networkInterface, RemoteAuthentication? credentials)
66+
internal unsafe PcapInterface(PcapIf pcapIf, NetworkInterface? networkInterface, RemoteAuthentication? credentials)
6767
{
6868
Name = pcapIf.Name;
6969
Description = pcapIf.Description;
@@ -77,9 +77,9 @@ internal PcapInterface(pcap_if pcapIf, NetworkInterface? networkInterface, Remot
7777
while (address != IntPtr.Zero)
7878
{
7979
// Marshal memory pointer into a sockaddr struct
80-
var addr = Marshal.PtrToStructure<pcap_addr>(address);
80+
var addr = (PcapAddr*)address;
8181

82-
PcapAddress newAddress = new PcapAddress(addr);
82+
PcapAddress newAddress = new PcapAddress(*addr);
8383
Addresses.Add(newAddress);
8484

8585
// is this a hardware address?
@@ -98,7 +98,7 @@ internal PcapInterface(pcap_if pcapIf, NetworkInterface? networkInterface, Remot
9898
}
9999
}
100100

101-
address = addr.Next; // move to the next address
101+
address = addr->Next; // move to the next address
102102
}
103103

104104
// attempt to populate the mac address,
@@ -227,7 +227,7 @@ static private IReadOnlyList<PcapInterface> GetAllPcapInterfaces(IntPtr devicePt
227227
while (nextDevPtr != IntPtr.Zero)
228228
{
229229
// Marshal pointer into a struct
230-
var pcap_if_unmanaged = Marshal.PtrToStructure<pcap_if>(nextDevPtr);
230+
var pcap_if_unmanaged = Marshal.PtrToStructure<PcapIf>(nextDevPtr);
231231
NetworkInterface? networkInterface = null;
232232
foreach (var nic in nics)
233233
{

SharpPcap/LibPcap/PcapStatistics.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Runtime.InteropServices;
7+
using static SharpPcap.LibPcap.PcapUnmanagedStructures;
78

89
namespace SharpPcap.LibPcap
910
{
@@ -36,66 +37,52 @@ internal PcapStatistics() { }
3637
/// pcap_t* for the adapter
3738
/// A <see cref="IntPtr"/>
3839
/// </param>
39-
internal PcapStatistics(PcapHandle pcap_t)
40+
internal unsafe PcapStatistics(PcapHandle pcap_t)
4041
{
41-
IntPtr stat;
42+
PcapStatUnix statUnix = new();
43+
PcapStatWindows statWindows = new();
44+
int result;
4245

4346
if (Environment.OSVersion.Platform == PlatformID.Unix)
4447
{
45-
// allocate memory for the struct
46-
stat = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_stat_unix)));
48+
result = LibPcapSafeNativeMethods.pcap_stats(pcap_t, ref statUnix);
4749
}
4850
else
4951
{
50-
// allocate memory for the struct
51-
stat = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_stat_windows)));
52+
result = LibPcapSafeNativeMethods.pcap_stats(pcap_t, ref statWindows);
5253
}
5354

5455
// retrieve the stats
55-
var result = (PcapUnmanagedStructures.PcapStatReturnValue)LibPcapSafeNativeMethods.pcap_stats(pcap_t, stat);
5656

5757
// process the return value
58-
switch (result)
58+
switch ((PcapStatReturnValue)result)
5959
{
60-
case PcapUnmanagedStructures.PcapStatReturnValue.Error:
60+
case PcapStatReturnValue.Error:
6161
// retrieve the error information
6262
var error = LibPcapLiveDevice.GetLastError(pcap_t);
63-
64-
// free the stats memory so we don't leak before we throw
65-
Marshal.FreeHGlobal(stat);
66-
6763
throw new StatisticsException(error);
68-
case PcapUnmanagedStructures.PcapStatReturnValue.Success:
64+
case PcapStatReturnValue.Success:
6965
// nothing to do upon success
7066
break;
7167
}
7268

7369
// marshal the unmanaged memory into an object of the proper type
7470
if (Environment.OSVersion.Platform == PlatformID.Unix)
7571
{
76-
var managedStat = Marshal.PtrToStructure<PcapUnmanagedStructures.pcap_stat_unix>(stat);
77-
7872
// copy the values
79-
this.ReceivedPackets = (uint)managedStat.ps_recv.ToInt64();
80-
this.DroppedPackets = (uint)managedStat.ps_drop.ToInt64();
81-
// this.InterfaceDroppedPackets = (uint)managedStat.ps_ifdrop;
73+
this.ReceivedPackets = (uint)statUnix.ps_recv;
74+
this.DroppedPackets = (uint)statUnix.ps_drop;
8275
}
8376
else
8477
{
85-
var managedStat = Marshal.PtrToStructure<PcapUnmanagedStructures.pcap_stat_windows>(stat);
86-
8778
// copy the values
88-
this.ReceivedPackets = (uint)managedStat.ps_recv;
89-
this.DroppedPackets = (uint)managedStat.ps_drop;
90-
// this.InterfaceDroppedPackets = (uint)managedStat.ps_ifdrop;
79+
this.ReceivedPackets = statWindows.ps_recv;
80+
this.DroppedPackets = statWindows.ps_drop;
9181
}
9282

9383
// NOTE: Not supported on unix or npcap, no need to
9484
// put a bogus value in this field
9585
this.InterfaceDroppedPackets = 0;
96-
97-
// free the stats
98-
Marshal.FreeHGlobal(stat);
9986
}
10087

10188
/// <summary>

0 commit comments

Comments
 (0)