命名空间冲突 libcrafter/Pcap++

Namespace Conflict libcrafter/Pcap++

我正在尝试制作一个用于交换数据包的自制系统,尤其是在 CentOS 上没有任何上述内容的第 2 层...

我用

问题是,当我的程序接收和发送数据包(因此使用 pcap++ 和 libcrafter)时,与 class Packet 存在冲突,两者都在 libcrafter 中定义在 Pcap++...

这是我的 header:

#include <stdio.h>
#include <iostream>
#include <getopt.h>
#include <stdlib.h>
#include <fstream>
#include <memory>
#include <pthread.h>
#include <errno.h>
#include <vector>
#include <string>
#include <sstream>
/**** LIBCRAFTER ******/
#include <MacAddress.h>
#include <IpAddress.h>
#include <PlatformSpecificUtils.h>
#include <PcapLiveDeviceList.h>
#include <PcapLiveDevice.h>
#include <EthLayer.h>
#include <PayloadLayer.h>
#include <Logger.h>
#include <in.h>
#include <PointerVector.h>
#include <crafter.h>
using namespace Crafter;
using namespace std;

这是我遇到的第一个错误:

server_resp.cpp: In function ‘int main(int, char**)’:
server_resp.cpp:74:3: error: reference to ‘Packet’ is ambiguous
   Packet pack_decod(capturedPackets.getAndRemoveFromVector(position));
   ^
In file included from /home/myself/Downloads/PcapPlusPlus-master/Dist/header/PcapLiveDevice.h:9:0,
                 from /home/myself/Downloads/PcapPlusPlus-master/Dist/header/PcapLiveDeviceList.h:5,
                 from server_resp.cpp:16:
/home/myself/Downloads/PcapPlusPlus-master/Dist/header/Packet.h:19:7: note: candidates are: class Packet
 class Packet {
       ^
In file included from /usr/local/include/crafter/Crafter.h:104:0,
                 from /usr/local/include/crafter.h:33,
                 from server_resp.cpp:23:
/usr/local/include/crafter/Packet.h:44:8: note:                 class Crafter::Packet
  class Packet {
        ^

我可以提供一个MME,但是它很长(150行),我想把问题简单化...

编辑:我也遇到了 class Ethernet

的问题

对于像这样的名称冲突,您实际上只能做两件事:

  1. 更改源代码并将库包装在自己的命名空间中。这样你就可以做类似 Crafter::Packet.

    的事情

    - 或 -

  2. 您可以将两个数据包之一重命名为唯一的名称。这也意味着将 all 引用更改为库本身中的新数据包名称。

我倾向于将库包装在它自己的命名空间中。因为这是最省力的事情。

最后,我建议制作一个补丁并将其邮寄给库的作者。这可能不是第一次,也不会是最后一次。

请注意现在 PcapPlusPlus 有自己的命名空间 (pcpp) 所以这些冲突应该不会再发生了