dotpcap / sharppcap

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

.NET Core 2.0 on Linux: Unable to load DLL 'wpcap' #42

Closed AlexanderHermann closed 5 years ago

AlexanderHermann commented 6 years ago

Hi, I have created a basic .NET Core 2.0 application using the VS template for a ".NET Core ASP.NET Core Web Application".

Code

using System;
using SharpPcap.LibPcap;

namespace SharpTest
{
    class Program
    {
        static void Main()
        {
            // Retrieve the device list
            var devices = LibPcapLiveDeviceList.Instance;

            // Print out the devices
            int i = 0;
            foreach (var dev in devices)
            {
                /* Description */
                Console.WriteLine("Device {0}: {1} | {2}", i, dev.Name, dev.Description);
                i++;
            }

            Console.ReadLine();
        }
    }
}

Dependencies

(from current sharppcap revision 4.5.0)

oldsqlwnb commented 6 years ago

The message says that you need to install the WinPcap driver https://www.winpcap.org/install/default.htm. But since you are working on Linux I have no Idea how one would go about it. Maybe try extracting the wcap.dll from the WinPcap driver and reference it directly into your project.

chmorgan commented 6 years ago

@oldsqlwnb the issue is a bit deeper than that. I'll probably end up implementing dynamic library loading to resolve it. If you run under mono then the SharpPcap.dll.config file's dllmap will take effect and let you map wpcap to libpcap. So the fix is to run under mono for now and ensure the dllmap is correct for the name of libpcap on your system. I'm hoping to have a better fix that is dotnet compatible but it does require a bit of code to dynamically load dlls based on platform etc.

AlexanderHermann commented 6 years ago

@chmorgan Thanks for the clarification. Since my goal is to abstain from Mono and rely solely on .NET Core, I guess I will have to wait for the prospective solution of dynamically loading DLLs based on the platform.

chmorgan commented 6 years ago

Hi Alexander.

This is a known limitation of the library. You could run your application under mono with a .config file that does a dllmap to libpcap if you’d like a short term work around.

On Mon, Apr 16, 2018 at 7:35 AM AlexanderHermann notifications@github.com wrote:

Hi, I have created a basic .NET Core 2.0 application using the VS template for a ".NET Core ASP.NET Core Web Application". Code

using System; using SharpPcap.LibPcap;

namespace SharpTest { class Program { static void Main() { // Retrieve the device list var devices = LibPcapLiveDeviceList.Instance;

        // Print out the devices
        int i = 0;
        foreach (var dev in devices)
        {
            /* Description */
            Console.WriteLine("Device {0}: {1} | {2}", i, dev.Name, dev.Description);
            i++;
        }

        Console.ReadLine();
    }
}

}

Dependencies

(from current sharppcap revision 4.5.0)

  • PacketDotNet.dll
  • SharpPcap.dll

Target Framework

.NET Core 2.0 (added "netcoreapp2.0" for the "TargetFramework" property in the .csproj-file) Steps (Linux Ubuntu 16.04)

Copied to Linux, started with: $ dotnet SharpTest.dll Error

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'wpcap': The specified module or one of its dependencies could not be found. (Exception from HRESULT: 0x8007007E) at SharpPcap.LibPcap.LibPcapSafeNativeMethods.pcap_findalldevs(IntPtr& alldevs, StringBuilder errbuf) at SharpPcap.LibPcap.LibPcapLiveDeviceList.GetDevices() at SharpPcap.LibPcap.LibPcapLiveDeviceList.Refresh() at SharpPcap.LibPcap.LibPcapLiveDeviceList..ctor() at SharpPcap.LibPcap.LibPcapLiveDeviceList.get_Instance() at SharpTest.Program.Main() in C:\Users\Username\source\repos\SharpTest\SharpTest\Program.cs:line 11

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/chmorgan/sharppcap/issues/42, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJ-ANmhYPdldrrONuR2Pn1ifoulPZRGks5tpIHjgaJpZM4TWWlo .

los93sol commented 6 years ago

An alternative that I used was to just drop the lip cap dll so it is published with the project and hard coded it to look for my dll.

@chmorgan, instead of trying to get fancy with some dynamic loader why not just have the caller pass in what they want to use?

chmorgan commented 6 years ago

How would that look/work in terms of api? Right now the dll is specified at compile time. Is it possible to pass that in and use the same binding mechanism?

On Tue, Jun 26, 2018 at 1:28 AM Adam Shortland notifications@github.com wrote:

An alternative that I used was to just drop the lip cap dll so it is published with the project and hard coded it to look for my dll.

@chmorgan https://github.com/chmorgan, instead of trying to get fancy with some dynamic loader why not just have the caller pass in what they want to use?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chmorgan/sharppcap/issues/42#issuecomment-400183326, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJ-AFQ49_i6WROfmdUwepwk90j9Hr1Iks5uAcaAgaJpZM4TWWlo .

los93sol commented 6 years ago

I can’t say for certain about WinPcap, but for libpcap on Linux I didn’t have to install it, I just dropped the dll in the project bin folder and that works as expected. I’m not sure about licensing but it seems like you could split the NuGet packages so you could publish SharpPCap, WinPCap (that just contains the dll), and LibPCap (again just containing the dll). That would give you the ability to control the naming and guarantee the file exists in the bin which also helps ease deployment of apps using SharpPCap since you don’t need to make sure one or the other is present on the target machine. Just another idea to skin it.

chmorgan commented 6 years ago

@los93sol I'd really prefer not to ship libpcap along with the library. Then I'd have to worry about keeping that up-to-date, which architectures people were using 32bit vs. 64bit etc.

I'm glad dropping the file into the folder worked. One downside of the static specification approach that is being used today is that the library name is fixed. On linux especially the library names aren't super consistent. Mono took care of some of that but still required a .config file. If I had control over the library loading I could search wisely, looking for libpcap.so, then maybe libpcap1.so, libpcap.so.1 etc depending on what linux distributions are naming the library. That would let us drop the .config file entirely on linux (which only mono supports anyway).

chmorgan commented 6 years ago

PRs welcome if someone wants to look at addressing the dynamic loading issue. I don't think its super tough but I'd prefer to use a standard approach if only msft can get their act together and adopt at least one of the proposals.

winkmichael commented 6 years ago

Include SharpPcap.dll.config in the runtime directory next to SharpPCap.dll

<!-- Configuration file used by Mono to aid in dll mapping between platforms so the same
     assembly can be used on Windows, where it will find wpcap.dll, and on linux/mac where
     Mono will map the dll to libpcap.so.
     This file should be placed along side of the assembly dll or in a system searchable location.
     See http://www.mono-project.com/Interop_with_Native_Libraries#Library_Names -->
<configuration>
  <dllmap dll="wpcap" target="libpcap.so.0.8" os="linux" />
  <dllmap dll="wpcap" target="libpcap.dylib"  os="osx"   />
</configuration>
lukasvosyka commented 5 years ago

Maybe this is being solved soon from the coreclr team..

https://github.com/dotnet/corefx/issues/32015

chmorgan commented 5 years ago

@lukasvosyka yep, that is the new ticket to track that issue. When there is a dotnet solution I'll be applying that to sharppcap to resolve the issue.

AlexanderHermann commented 5 years ago

That's great news, thanks for the update!

epasinetti commented 5 years ago

@chmorgan any news about this issue ? I can't get work with workaround of .config file. I'm compiling the dotnet core app in VisualStudio 2017 on windows, is there any workaround to get work the library on Linux Debian 9 ? Thank you

chmorgan commented 5 years ago

@epasinetti it sounds like the issue is still winding through the dotnet process. I'm getting some updates from the above ticket below but its been quiet for a week. If you are watching the above link to the api ticket I'd be interested when they get that into a usable form. At that point we'll want to get a PR in to support it and finally get proper dotnet support.

fukaminakrize commented 5 years ago

Hi @epasinetti, as a workaround to get sharppcap working on Linux using .net core, I've setup libwpcap.so link file pointing to installed libpcap.so (_ln -s /usr/lib/x86_64-linux-gnu/libpcap.so.0.8 /usr/lib/x8664-linux-gnu/libwpcap.so).

chmorgan commented 5 years ago

Hi @fukaminakrize, @AlexanderHermann, please check out the 4.6.0 release, it should address the cross platform issues with dotnet.

AlexanderHermann commented 5 years ago

Hi @chmorgan, glad to hear the issue was addressed, thanks for the heads up!

bbhxwl commented 4 years ago

I haven't solved it yet. I don't know why. I use the latest version.

humbertomoli99 commented 2 years ago

is there a solution to this? i have the same problem, i'm on windows 10

kayoub5 commented 2 years ago

@Humberto-Molina please open a separate ticket with details on your specific context.