PcapPlusPlus
PcapPlusPlus copied to clipboard
Need help with linking Pcap++ in my custom code.
Question
I have followed the steps to build from source on linux from the master branch. I proceeded to include the static libraries (.a) into my project which builds up into a shared library. I have made sure I have built it with position independent code as mentioned in #1219 .
1> g++ -o "/home/username/projects/my_project/bin/x64/Debug/libOutput.so" -Wl,--no-undefined -Wl,-L/home/username/pcap++PICLibs/lib -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -shared /home/username/projects/my_project/obj/x64/Debug/libOutput.o -lpcap -lCommon++ -lPacket++ -lPcap++
1> /usr/bin/ld : error : /home/username/pcap++PICLibs/lib/libPcap++.a(PcapLiveDeviceList.cpp.o): in function `pcpp::PcapLiveDeviceList::getPcapLiveDeviceByIp(pcpp::IPv4Address const&) const':
1> PcapLiveDeviceList.cpp : error : (.text+0xa1): undefined reference to `pcpp::internal::try_sockaddr2in_addr(sockaddr*)'
1> /usr/bin/ld : error : PcapLiveDeviceList.cpp:(.text+0xb1): undefined reference to `pcpp::operator==(pcpp::IPv4Address const&, in_addr const&)'
1> /usr/bin/ld : error : PcapLiveDeviceList.cpp:(.text+0xf8): undefined reference to `pcpp::internal::sockaddr2string(sockaddr const*, char*, unsigned long)'
1> /usr/bin/ld : error : PcapLiveDeviceList.cpp:(.text+0x118): undefined reference to `pcpp::internal::try_sockaddr2in_addr(sockaddr*)'
Operating systems
Linux
@clementperon any idea why the build would fail?
@zyl-one can you please post the output of how you build PcapPlusPlus?
@zyl-one which build system do you use ?
Could you try to have -lCommon++ -lPacket++ -lPcap++ two times ? It could be the order of the link.
Fixed the above issue, the order of the link was the problem.
I have another issue regarding building from source on Windows. I need some assistance with this.
Build Configuration: Windows x64 Debug Usage: PcapPlusPlus static libraries (.lib) with my custom code which builds into a shared library (.dll)
I've used the release static library pcapplusplus-23.09-windows-vs2022-x64-release.zip for the task below, and it works fine.
std::vector<pcpp::PcapLiveDevice *> devicesList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
int devicesSize = devicesList.size();
for (int iterator = 0; iterator < devicesSize; iterator++) {
std::cout << devicesList[iterator]->getName() << std::endl;
}
The error message in the errbuf in DeviceUtils.cpp while I'm debugging says -> error calling packetRequest : Couldn't get error message for error (3758096571)
I've tried using CMake, MSBuild, and Visual Studio to generate static libraries (.lib) to mitigate any potential build process issues. I've also tried switching between the Npcap SDK and the WinPcap Developer's Pack to check for other potential issues.
Thanks for the assistance, @seladb @clementperon.
I just found out that it isn’t a build issue. The static libraries (.lib) work fine when I build from the source of v23.09. I believe the code changes made since then are causing this issue.
@seladb @clementperon
There are a lot of BREAKING changes since the latest version. If you target the latest master, please check how it works on CI. We have tests on many Linux distributions already.
@zyl-one just to make sure I understand: the code above works fine with v23.09 but doesn't work with the latest code in master?
The error that you mentioned: error calling packetRequest : Couldn't get error message for error (3758096571) - where do you get it, is it after calling pcap_findalldevs?
@seladb
-
YES, the code works with
v23.09but doesn't work with the latest code inmaster. -
YES, it happens right after calling
pcap_findalldevs. Also, at the end, when I try to get getPcapLiveDevicesList() with the instance, it returns corrupt data.
Build configuration: x64 Windows (Visual Studio Debug) Usage: With Npcap 1.79
Code snippet:
pcpp::PcapLiveDeviceList& liveDeviceListInstance = pcpp::PcapLiveDeviceList::getInstance();
std::vector<pcpp::PcapLiveDevice *> devicesList = liveDeviceListInstance.getPcapLiveDevicesList();
Attaching screenshots for reference:
Hmm, this is super weird. The way of fetching the live devices between v23.09 and the current master hasn't changed drastically. At its core both use pcap_findalldevs. The only real change in the current master is that the devices are now internally managed by unique_ptrs.
From what I found in this NPcap issue the returned error code is probably a private NTStatus error code from some driver, probably the NPcap one.
Interestingly enough, after pcap_findalldevs, I also get the same error code and message stored in the error buffer, but the return interface arrays are valid, so that is probably not the main issue.
Tested both on current master and v23.09. (Win 10 22H2, Npcap 1.79 Debug x64)
On the master branch, could you please place a breakpoint on the line DeviceUtils.cpp#L19, and then provide a screenshot of the linked list pcap_findalldevs returns?
Normal output should be something like this example:
As for the corrupted PcapLiveDevices. Could you break on PcapLiveDeviceList.cpp#L70 and check if the devices are already corrupted at that stage, as that is the part that initializes them?
Wait?! The beginning of the issue mentioned that the platform is "Linux", and now it's "Windows"? So do you have a problem with Linux or not?
Wait?! The beginning of the issue mentioned that the platform is "Linux", and now it's "Windows"? So do you have a problem with Linux or not?
@tigercosmos yeah, in this https://github.com/seladb/PcapPlusPlus/issues/1533#issuecomment-2288438580
Should have probably been two issues tbh...
Sorry for the confusion, @tigercosmos. I should have opened a separate issue for the Windows issue to avoid this. I'll summarize everything in this comment and will open new issues for each if necessary.
Initially, I had an issue with generating a build for Linux with my custom code, as I mentioned here. This was resolved—the order of the link was the problem.
Currently, I have two open issues:
- As mentioned here, I couldn't get the devicesList. This occurs with the current master branch while I'm building from source for Windows x64 (Visual Studio Debug) with Npcap 1.79. It works well with
release v23.09-> pcapplusplus-23.09-windows-vs2022-x64-release.zip. - The new issue is with Linux when I build from source with
release v23.09with libpcap. Sometimes, thedevicesListisn't populated and returns a size of 0. Other times, thedevicesListis populated, but thegetName()method returns an empty string when I try to print it.
Code snippet for the 2nd issue:
std::vector<pcpp::PcapLiveDevice *> devicesList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
int devicesSize = devicesList.size();
for (int iterator = 0; iterator < devicesSize; iterator++) {
std::cout << devicesList[iterator]->getName() << std::endl;
}
For me, in release v23.09, Windows works fine, but Linux doesn't. In the latest master, Linux works fine, but Windows doesn't. Hence, I'm juggling between the two. I'm trying to find a state where both can be built from source successfully and work at runtime.
Sorry again for the lengthy message. I appreciate your understanding.
@zyl-one can you please help us debug the issue as @Dimi1010 suggested in https://github.com/seladb/PcapPlusPlus/issues/1533#issuecomment-2293504797 ?
I debugged the same, there are no issues with the code. It happened because Visual Studio was caching my headers (It was mismatching the header files and the .libs [i.e. 23.09 .h files for the latest master's .libs]). @seladb @clementperon @tigercosmos @Dimi1010