szymonwieloch / rust-rawsock

Rust library for obtaining and sending raw network packets from interfaces.
MIT License
68 stars 14 forks source link

Not loading `npcap` when `WinPcap` and `npcap` coexist. #6

Open spacemeowx2 opened 5 years ago

spacemeowx2 commented 5 years ago

It seems that we prefer npcap. But when WinPcap and npcap coexist, rawsock actually loads WinPcap.

https://github.com/szymonwieloch/rust-rawsock/blob/53408bd5c2d3511002482cf60a39dd773ccde153/src/wpcap/paths.rs#L3-L8

Since Npcap\wpcap.dll searchs Packet.dll instead of Npcap\Packet.dll(could be seen using procmon), and Npcap\wpcap.dll wouldn't load correctly with wrong Packet.dll.

And because what we need is wpcap.dll, I think all Packet.dll could be removed from default paths.

Solution:

I think it should be an issue about dlopen.

According to the document (npcap-sdk-1.03/docs/npcap-devguide.html#npcap-feature-native in npcap-sdk-1.03.zip), we need to call SetDllDirectory to make wpcap.dll loading right dll.

You need to do the following one step:

Before calling LoadLibrary to load wpcap.dll, call SetDllDirectory to add C:\Windows\System32\Npcap\ to DLL search path.

szymonwieloch commented 5 years ago

@spacemeowx2 Thank you for your report. I have done some debugging and research to better understand the problem.

You are basically right - npcap when installed together with wpcap is installed in such a way that you cannot just open it using LoadLibrary(). This is unexpected. However my understanding is that it works OK when installed as the main library.

We have two choices here. The first one is to assume that rawsock supports only "full" installation of npcap. The second one is to modify the code so that it loads npcap even when installed together with wpcap. My prefference is the second option but at the same time I can see it as a kind of costly change with relatively small value. Not many users will be using npcap together with wpcap and still strictly prefer the first one. Additionally I would like to first implement in dlopen a feature that would allow you to set dll search path from within the process in a portable way. I am not even sure if this is possible (maybe the setenv() function on POSIX will allow you to modify LD_LIBRARY_PATH variable, maybe not).

So I will keep this issue as an low-priority enhancement and try to extend dlopen first.

szymonwieloch commented 5 years ago

Blocked by: dlopen issue

tan-wei commented 4 years ago

Is any workaround now? After I have a glance at the discussion, I think I can release with 2 dll files in the current directory, which is not that elegant. :-P