libtins嗅探器undefined [英] libtins Sniffer undefined

查看:223
本文介绍了libtins嗅探器undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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未定义.我已经将所有库都包含在链接器中.

I have the default code libtins gives you, but for some reason Sniffer is undefined. I have included all libs in my linker.

构建时也会给我以下错误:

Also it gives me the following error when I built:

-- 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时,它给了我另一个错误:

I have checked and there is no file called config.h however there is one called config.h.in. When I removed .in it gave me another error:

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 ==========

他们的网站上,我唯一不了解的指示是

The only instruction I didn't understand on their website is when they say

您还需要将此宏定义添加到您的项目中:

You also need to add this macro definition to your project:

TINS​​_STATIC

TINS_STATIC

我不太清楚这意味着什么,所以这也可能是问题所在.

I don't exactly know what this means so this might also be the issue.

提前谢谢!

推荐答案

以下是在Windows上使用libtins的说明.您可以使用预编译的库,也可以自己编译libtins.如果您不确定,我建议您使用预编译的libtins版本,因为它的工作量较小.

Here are instructions for using libtins on Windows. You can either use a pre-compiled library or compile libtins yourself. If you are unsure, I would recommend using a pre-compiled version of libtins, because it is less work.

所有这些说明均基于链接.

All of these instructions are based on link.

  • 下载预编译的libtin.在此处(调试/发布,版本32/64)中选择所需的目标平台少量).在右侧,单击工件",然后下载zip存档.解压缩拉链.在内部,您将找到一个include和一个lib文件夹.

  • Download the pre-compiled libtins. Select your required target platform here (debug/release, 32/64 bit). Towards the right, click on "Artifacts", then download the zip archive. Unpack the zip. Inside you will find a include and a lib folder.

此处下载npcap SDK.解压缩zip存档.在内部,您将找到一个Include和一个Lib文件夹.请注意,lib文件夹具有64位变体作为子文件夹(Lib/x64).

Download the npcap SDK from here. Unpack the zip archive. Inside you will find a Include and a Lib folder. Note the lib folder has the 64 bit variant as a sub-folder (Lib/x64).

在Visual Studio中,打开您的项目设置.在顶部,确保配置/平台与您所需的目标平台(调试/发行版,32/64位)相匹配.

In Visual Studio, open your project settings. At the top, ensure that the configuration/platform match your required target platform (debug/release, 32/64 bit).

  • C/C++> General> Additional include directories下,添加libtins和npcap的包含目录.例如.它应该看起来像<some-path>\libtins-vs2015-x64-release\libtins\include;<some-path>\npcap-sdk-1.05\Include;%(AdditionalIncludeDirectories).

  • 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 Studio将无法找到libtins和npcap的标头.您将收到诸如以下错误:

Explanation: Without this, Visual Studio will not be able to find the headers of libtins and npcap. You would get errors such as:

  • 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
  • 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

C/C++> Preprocessor> Preprocessor definitions下,添加TINS_STATIC.

Under C/C++ > Preprocessor > Preprocessor definitions, add TINS_STATIC.

说明:这等同于在包含任何libtins标头之前在代码中添加#define TINS_STATIC.这将导致libtins在其标头中忽略任何dllexport/dllimport语句,如果您将libtins用作共享/动态库而不是静态库,则需要使用此语句(有关更多信息,请参见链接

Explanation: This is equivalent to adding #define TINS_STATIC in your code before including any libtins headers. It will cause libtins to omit any dllexport/dllimport statements in its headers, which would be needed if you would use libtins as a shared/dynamic library, instead of as a static library (for more info see link, link and in the libtins source code, include/tins/macros.h for how TINS_STATIC is used). Without this you would get errors such as:

  • 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 &)"
  • 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 &)"

Linker> General> Additional library directories下,添加libtins和npcap的lib目录.根据目标平台,为npcacp(32/64位)选择正确的lib目录.结果应类似于<some-path>\libtins-vs2015-x64-release\libtins\lib;<some-path>\npcap-sdk-1.05\Lib\x64;%(AdditionalLibraryDirectories).

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 Studio将无法找到libtins和npcap库.您将收到诸如以下错误:

Explanation: Without this, Visual Studio will be unable to find the libtins and npcap libraries. You would get errors such as:

  • LNK1181 cannot open input file 'tins.lib'
  • LNK1181 cannot open input file 'Packet.lib'
  • LNK1181 cannot open input file 'tins.lib'
  • LNK1181 cannot open input file 'Packet.lib'

Linker> Input> Additional dependencies下,添加tins.lib,npcap库Packet.libwpcap.lib和Windows库Iphlpapi.libWs2_32.lib.结果应类似于tins.lib;Packet.lib;wpcap.lib;Iphlpapi.lib;Ws2_32.lib;%(AdditionalDependencies).

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 Studio链接到libtins和npcap二进制文件以及必需的Windows网络库.没有这个,您将得到诸如以下的错误:

Explanation: This will direct Visual Studio to link against the libtins and npcap binaries and also the necessary Windows networking libraries. Without this, you would get errors such as:

  • 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
  • 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

在源代码中,请确保在包含任何libtins标头之前添加#include <algorithm>.

In your source code, ensure you add #include <algorithm> before including any libtins headers.

说明:没有这个,我得到了C2039 'min': is not a member of 'std'.

Explanation: Without this I got C2039 'min': is not a member of 'std'.

您的代码现在应该可以编译了.但是,请注意eth0是Linux特定的接口名称.在任何情况下,它都不会在Windows上提供任何输出.请参阅此链接,以了解如何在Windows上标识网络接口名称.因此,您的姓名应以{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX}的形式更长,而不是eth0.

Your code should now compile. However, note that eth0 is a Linux-specific interface name. In all likelihood, it will not give you any output on Windows. Refer to this link on how to identify the network interface name on Windows. So instead of eth0, your name should be something longer of the form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX}.

作为先决条件,您应该知道如何使用命令提示符.

As a prerequisite, you should know how to use a command prompt.

  • 安装cmake(链接)
  • 下载libtins源代码(链接),解压缩档案.
  • 像以前一样下载npcap SDK.我没有尝试也从源代码编译npcap.
  • 在libtins源代码文件夹中,打开命令提示符.运行以下命令,使PCAP_ROOT_DIR适应于您下载npcap SDK的位置.还要适应您所需的配置(调试/发布).
  • Install cmake (link)
  • Download the libtins source code (link), unpack the archive.
  • Download the npcap SDK as before. I did not attempt to also compile npcap from source.
  • In the libtins source code folder, open a command prompt. Run the following commands, adapt the PCAP_ROOT_DIR to where you have downloaded the npcap SDK. Also adapt to your desired configuration (debug/release).
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.
  • 遵循上面给出的有关Visual Studio的说明,但是将libtins的路径调整为您自己编译的版本:
    • 对于其他库目录",请使用例如<some-path>\libtins-master\build\lib\Release.
    • 对于其他包含目录",请使用例如<some-path>\libtins-master\include.
      • In the libtins source folder under .\build\lib\Release, you should now have a tins.lib.
      • Follow the instructions for Visual Studio given above, but adapt the paths for libtins to your self-compiled version:
        • For "Additional library directories", use e.g. <some-path>\libtins-master\build\lib\Release.
        • For "Additional include directories", use e.g. <some-path>\libtins-master\include.
        • 请注意,这不会启用libtins的所有功能.为此,您需要安装其他依赖项(openssl,boost).第一个cmake命令应告诉您已启用了哪些功能.

          Note that this will not enable all features of libtins. For that you would need to install additional dependencies (openssl, boost). The first cmake command should tell you which features have been enabled.

          这篇关于libtins嗅探器undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆