0

I'm trying to make an homemade system for exchanging packets, especially on layer 2 without anything above, on CentOS...

I use

  • libcrafter to build packets
  • pcap++ to sniff packets
  • (crypto++ to ensure a minimal security - off topic for this question)

The thing is, as my program receives and sends packets (and thus uses pcap++ and libcrafter), there is a conflict with the class Packet, defined both in libcrafter and in Pcap++...

Here's my 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;

And here is the first error I get :

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 {
        ^

I can provide a MME, but it's quite long (150 lines), and I wanted to keep the question simple...

Edit : I also have the problem with the class Ethernet

seladb
  • 852
  • 1
  • 13
  • 29
3isenHeim
  • 243
  • 5
  • 22

2 Answers2

2

Please notice that now PcapPlusPlus has its own namespace (pcpp) so these conflicts shouldn't happen anymore

seladb
  • 852
  • 1
  • 13
  • 29
James
  • 21
  • 2
1

With name conflicts such as this one there are only two things you can practically do:

  1. Change the source code and wrap the library in its own namespace. That way you can do something like Crafter::Packet.

    - Or -

  2. you could rename one of the two Packets to something unique. It also means changing all references to the new Packet name in the library itself.

I'm partial to wrapping the library in its own namespace. As it's the least effort thing to do.

Lastly, I suggest making a patch of this and mailing it to the authors of the libraries. It probably won't be the first time this happens, nor will it be the last.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Nathilion
  • 309
  • 1
  • 7
  • Just to be clear, you suggest that I create a namespace for `pcap++`, because it doesn't have its own namespace ? – 3isenHeim Sep 01 '15 at 11:14
  • 1
    I had a closer look at the libraries' source and noticed that Crafter is already in a namespace whereas pcap++ isn't. Then I noticed a specific line in your code: 'using namespace Crafter;'. Basically you don't have to do anything other than remove that line and simply state which kind of Packet you want to use as per my answer. Basically you need some means to distinguish the two Packets. Crafter already has an extra namespace wrapper so I'd use it. Also, don't do 'using namespace std;'. It will cause the same problems as you have now with generally named functions (such as min and max). – Nathilion Sep 01 '15 at 11:17
  • OK, for crafter it's thus Crafter::Packet, and the rest, it will automatically search in pcap++ ? I'm not used to namespace :/ – 3isenHeim Sep 01 '15 at 11:23
  • 1
    Namespaces in simpel terms: the compiler's parser organises all names it finds in a hierarchy. Then based on this big tree-like structure it starts to compile your typed words, your code, into machine code. If it just so happens there are two identical names _at the same level in the hierarchy_ the compiler gets confused about which of the two names to use. Remember that each name represents a different piece of code! When this happens the compiler just gives up and says: I got two identical names and don't know what to do. Bye! Using the full name (Crafter::Packet) this confusion is prevented – Nathilion Sep 01 '15 at 11:37
  • Okay ! Solved it by deleting `using namespace Crafter`, and specifying `Crafter::Packet` each time I wanted to use a libcrafter Packet. For the Packets from the other library, just `Packet` is enough. Thanks ! – 3isenHeim Sep 01 '15 at 11:41