dotpcap / sharppcap

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

wpcap.so: cannot open on linux build #478

Closed TechnoChimp closed 11 months ago

TechnoChimp commented 12 months ago

Tested on both Ubuntu 18.04 and Ubuntu 22.04. Both with dotnet7.

.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="SharpPcap" Version="6.2.5" />
    ...
  </ItemGroup>

</Project>

Build command: dotnet publish -r linux-amd64 -c Release

Full output when running the generated executable:

Unhandled Exception: System.DllNotFoundException: Unable to load shared library 'wpcap' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable:
wpcap.so: cannot open shared object file: No such file or directory
libwpcap.so: cannot open shared object file: No such file or directory
wpcap: cannot open shared object file: No such file or directory
libwpcap: cannot open shared object file: No such file or directory

   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x4e
   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x10f
   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
   at SharpPcap.LibPcap.LibPcapSafeNativeMethods.pcap_findalldevs(IntPtr&, StringBuilder) + 0x6f
   at SharpPcap.LibPcap.PcapInterface.GetAllPcapInterfaces() + 0x40
   at SharpPcap.LibPcap.LibPcapLiveDeviceList.GetDevices() + 0x32
   at SharpPcap.LibPcap.LibPcapLiveDeviceList.Refresh() + 0x2b
   at SharpPcap.LibPcap.LibPcapLiveDeviceList.get_Instance() + 0x2a
   at <redacted>.Program.<Main>d__0.MoveNext() + 0x2e
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xc2
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x44
   at <redacted>.Program.<Main>(String[]) + 0x20
   at <redacted>!<BaseAddress>+0xd9e0ab
Aborted (core dumped)
kayoub5 commented 12 months ago

what libpcap version did you install?

TechnoChimp commented 12 months ago

I installed the latest libpcap-dev from apt. This version seems to be fine. If I link the libpcap.so file as libwpcap.so then it does work, but based on old closed issues that shouldn't be needed since that issue was resolved.

kayoub5 commented 12 months ago

the solution implemented for dynamic detection of pcap library, is not compatible with PublishAot

TechnoChimp commented 11 months ago

Ah ok, that did seem to be my problem. Removing Aot I can now run the executable on a machine as long as libpcap-dev is installed on that machine. (If libpcap-dev is not installed I still get the error regarding libwpcap missing which threw me off for a second there.)

Regarding the libpcap-dev dependency, I don't suppose there's a way to bundle this into the build package so it doesn't need to be installed alongside my generated executable?

kayoub5 commented 11 months ago

sharppcap lools for a libpcap.so, whether you can bundle that or not (and it keeps working across systems), is a question for the libpcap maintainers, there is no definitive answer here.