Search code examples
pcap-ng

C++ - read and write pcapng files without libpcap


I'm interested in reading and writing pcapng files without using libpcap or WinPcap. Anyone knows how to do it?


Solution

  • I can recommend a C-library which does that, it's called LightPcapNg and apparently PcapPlusPlus is using it to have a cleaner C++ wrapper.

    Since you're interested in C++, here is a code snippet of how to read a pcap-ng file using PcapPlusPlus:

    #include <PcapFileDevice.h>
    
    void readAndWritePcapNg(char* inputFileName, char* outputFileName)
    {
        // reader instance
        PcapNgFileReaderDevice readerDev(inputFileName);
    
        // writer instance
        PcapNgFileWriterDevice writerDev(outputFileName);
    
        // open reader and writer
        readerDev.open();
        writerDev.open();
    
        RawPacket rawPacket;
    
        // read packets from file
        while (readerDev.getNextPacket(rawPacket))
        {
            Packet packet(&rawPacket);
    
            // do whatever you want with the packet
            ....
            ....
    
            // write the packet to the output file
            writerDev.writePacket(rawPacket);
        }
    
        // close reader and writer
        readerDev.close();
        writerDev.close();
    }