#include <iostream>
#include <tins/tins.h>
using namespace Tins;
using namespace std;
bool callback(const PDU& pdu) {
// Find the IP layer
const IP& ip = pdu.rfind_pdu<IP>();
// Find the TCP layer
const TCP& tcp = pdu.rfind_pdu<TCP>();
cout << ip.src_addr() << ":" << tcp.sport() << " -> "
<< ip.dst_addr() << ":" << tcp.dport() << endl;
return true;
}
int main() {
Sniffer("eth0").sniff_loop(callback);
}我有libtins提供的默认代码,但出于某种原因,Sniffer没有定义。我已经在我的链接器中包括了所有的libs。
此外,当我构建时,它还会给出以下错误:
-- Build started: Project: Packet Sniffing, Configuration: Debug x64 ------
1>Packet Sniffing.cpp
1>C:\Users\usr\Documents\Code\C++ Projects\static\libtins\include\tins\macros.h(37,10): fatal error C1083: Cannot open include file: 'tins/config.h': No such file or directory
1>Done building project "Packet Sniffing.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========我已经检查过了,没有名为config.h的文件,但是有一个文件名为config.h.in。当我删除.in时,它给了我另一个错误:
1>------ Build started: Project: Packet Sniffing, Configuration: Debug x64 ------
1>Packet Sniffing.cpp
1>C:\Users\usr\Documents\Code\C++ Projects\static\libtins\include\tins\config.h(5,1): fatal error C1021: invalid preprocessor command 'cmakedefine'
1>Done building project "Packet Sniffing.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========我在他们的网站上唯一不明白的是他们说
还需要将此宏定义添加到项目中: TINS_STATIC
我不太清楚这意味着什么,所以这也可能是问题所在。
提前感谢!
发布于 2020-04-04 13:22:16
下面是在Windows上使用libtins的说明。您可以使用预编译库,也可以自己编译libtins。如果您不确定,我建议使用预编译版本的libtins,因为它的工作量较少。
所有这些指令都是基于链接的。
在Windows上使用预编译libtins
include和一个lib文件夹。Include和一个Lib文件夹。注意,lib文件夹将64位变量作为子文件夹(Lib/x64)。- Under `C/C++` > `General` > `Additional include directories`, add the include directories of libtins and npcap. E.g. it should look something like `<some-path>\libtins-vs2015-x64-release\libtins\include;<some-path>\npcap-sdk-1.05\Include;%(AdditionalIncludeDirectories)`.解释:没有它,Visual将无法找到libtins和npcap的头文件。您将得到以下错误:
- `E1696 cannot open source file "tins/tins.h"`
- `E0020 identifier "PDU" is undefined`
- `E1696 cannot open source file "pcap.h"`
- `E0020 identifier "pcap_t" is undefined`
- Under `C/C++` > `Preprocessor` > `Preprocessor definitions`, add `TINS_STATIC`. 解释:这相当于在包含任何libtins头之前在代码中添加#define TINS_STATIC。它将导致libtins在其头中省略任何dllexport/dllimport语句,如果要将libtins用作共享/动态库而不是静态库,则需要这些语句(有关更多信息,请参见链接、链接和libtins源代码中的include/tins/macros.h以了解TINS_STATIC的使用方式)。如果没有这一点,您将得到错误,例如:
- `C4251 'Tins::DNS::records_data_': class 'std::vector<uint8_t,std::allocator<uint8_t>>' needs to have dll-interface to be used by clients of class 'Tins::DNS'`
- `LNK2001 unresolved external symbol "__declspec(dllimport) public: __cdecl Tins::Sniffer::Sniffer(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)"`
- Under `Linker` > `General` > `Additional library directories`, add the lib directories of libtins and npcap. Choose the right lib directory for npcacp (32/64 bit) depending on your target platform. The result should look something like `<some-path>\libtins-vs2015-x64-release\libtins\lib;<some-path>\npcap-sdk-1.05\Lib\x64;%(AdditionalLibraryDirectories)`.解释:没有它,Visual将无法找到libtins和npcap库。您将得到以下错误:
- `LNK1181 cannot open input file 'tins.lib'`
- `LNK1181 cannot open input file 'Packet.lib'`
- Under `Linker` > `Input` > `Additional dependencies`, add `tins.lib`, the npcap libraries `Packet.lib`, `wpcap.lib`, and the Windows libraries `Iphlpapi.lib`, `Ws2_32.lib`. The result should look something like `tins.lib;Packet.lib;wpcap.lib;Iphlpapi.lib;Ws2_32.lib;%(AdditionalDependencies)`.解释:这将指导Visual链接到libtins和npcap二进制文件以及必要的Windows。如果不这样做,您将得到以下错误:
- `LNK2001 unresolved external symbol "public: __cdecl Tins::Sniffer::Sniffer(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)"`
- `LNK2001 unresolved external symbol pcap_lookupnet`
- `LNK2001 unresolved external symbol GetAdaptersAddresses`
- `LNK2001 unresolved external symbol __imp_inet_pton`
- In your source code, ensure you add `#include <algorithm>` before including any libtins headers.解释:没有这个,我得到了C2039 'min': is not a member of 'std'。
eth0是一个特定于Linux的接口名称。很可能,它不会在Windows上给出任何输出。有关如何标识Windows上的网络接口名称,请参阅此链接。因此,您的名称不应该是eth0,而应该是更长的表单{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX}。在Windows上从源代码编译libtins
作为先决条件,您应该知道如何使用命令提示符。
PCAP_ROOT_DIR适应下载npcap的位置。还可以适应所需的配置(调试/发布)。mkdir build
cd build
& 'C:\Program Files\CMake\bin\cmake.exe' ../ -DPCAP_ROOT_DIR=<some-path>\npcap-sdk-1.05\ -DLIBTINS_ENABLE_WPA2=0 -DLIBTINS_BUILD_SHARED=off -DCMAKE_BUILD_TYPE=Release
& 'C:\Program Files\CMake\bin\cmake.exe' --build . --config Release.\build\lib\Release下的libtins源文件夹中,现在应该有一个tins.lib。<some-path>\libtins-master\build\lib\Release。<some-path>\libtins-master\include。
请注意,这不会启用libtins的所有功能。为此,您需要安装额外的依赖项(openssl,boost)。第一个cmake命令应该告诉您已经启用了哪些功能。
https://stackoverflow.com/questions/60841031
复制相似问题