dotpcap / sharppcap

Official repository - Fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets
1.33k stars 268 forks source link

Add support for reading capture data from a file handle #400

Closed heck-gd closed 2 years ago

heck-gd commented 2 years ago

Add a CaptureHandleReaderDevice that can be instantiated with an existing file handle. This is advantageous in situations where one would like fine-grained control over how the handle is opened.

kayoub5 commented 2 years ago

Missing Unit Tests

codecov-commenter commented 2 years ago

Codecov Report

Merging #400 (bebe7fa) into master (790246e) will decrease coverage by 21.08%. The diff coverage is 74.28%.

@@             Coverage Diff             @@
##           master     #400       +/-   ##
===========================================
- Coverage   83.18%   62.09%   -21.09%     
===========================================
  Files          49       51        +2     
  Lines        2748     2781       +33     
  Branches      301      304        +3     
===========================================
- Hits         2286     1727      -559     
- Misses        343      960      +617     
+ Partials      119       94       -25     
Impacted Files Coverage Δ
SharpPcap/LibPcap/CaptureFileReaderDevice.cs 70.00% <ø> (-0.74%) :arrow_down:
SharpPcap/LibPcap/CaptureHandleReaderDevice.cs 71.42% <71.42%> (ø)
SharpPcap/LibPcap/LibPcapSafeNativeMethods.cs 54.28% <80.00%> (-2.39%) :arrow_down:
SharpPcap/LibPcap/CaptureReaderDevice.cs 100.00% <100.00%> (ø)
SharpPcap/LibPcap/PcapDeviceCaptureLoop.cs 87.80% <100.00%> (ø)
SharpPcap/WinpkFilter/DriverHandle.cs 0.00% <0.00%> (-100.00%) :arrow_down:
SharpPcap/WinDivert/WinDivertHeader.cs 0.00% <0.00%> (-100.00%) :arrow_down:
SharpPcap/WinpkFilter/NativeMethods.cs 0.00% <0.00%> (-100.00%) :arrow_down:
SharpPcap/WinpkFilter/WinpkFilterHeader.cs 0.00% <0.00%> (-100.00%) :arrow_down:
SharpPcap/WinpkFilter/WinpkFilterDriver.cs 0.00% <0.00%> (-96.78%) :arrow_down:
... and 18 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 790246e...bebe7fa. Read the comment docs.

heck-gd commented 2 years ago

Added tests now, they're for the most part based on the ones for files. One ugly part is the whole Handle/FILE* shenanigans, which required me to add a reference to Mono.Posix.NETStandard to the test project. But it can't be helped, because libpcap doesn't accept a native fd on non-Windows systems.

kayoub5 commented 2 years ago

@heck-gd take a look at proposed changes in https://github.com/dotpcap/sharppcap/pull/403/commits/3de9ac7417b5dbb0bb397b71ab5156beba33f5cd

kayoub5 commented 2 years ago

@heck-gd The new API requires specific Libpcap versions, could you handle the TypeLoadException caused by missing functions and throw NotSupportedException instead?

Also add the [LibpcapVersion("....")] attribute to the tests to mark minimum Libpcap version needed for the test to run

kayoub5 commented 2 years ago

@heck-gd could you rebase your PR into one commit?

heck-gd commented 2 years ago

@heck-gd could you rebase your PR into one commit?

Done. I've also cherry-picked your commit from #403.