Skip to content

Commit d8e3195

Browse files
authored
FIxed reentrant mode allocated buffer going out of scope. (#1737)
* FIxed reentrant mode allocated buffer going out of scope. * Lint
1 parent 7bbe77f commit d8e3195

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

Pcap++/src/PfRingDevice.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <pthread.h>
1212
#include <chrono>
1313
#include <algorithm>
14+
#include <vector>
1415

1516
#define DEFAULT_PF_RING_SNAPLEN 1600
1617

@@ -624,23 +625,27 @@ namespace pcpp
624625
return;
625626
}
626627

627-
while (!this->m_StopThread)
628-
{
629-
// if buffer is nullptr PF_RING avoids copy of the data
630-
uint8_t* buffer = nullptr;
631-
uint32_t bufferLen = 0;
628+
// Zero copy is not supported in reentrant mode
629+
const bool zeroCopySupported = !m_ReentrantMode;
632630

633-
// in multi-threaded mode flag PF_RING_REENTRANT is set, and this flag doesn't work with zero copy
634-
// so I need to allocate a buffer and set buffer to point to it
635-
if (this->m_ReentrantMode)
636-
{
637-
uint8_t tempBuffer[PCPP_MAX_PACKET_SIZE];
638-
buffer = tempBuffer;
639-
bufferLen = PCPP_MAX_PACKET_SIZE;
640-
}
631+
// If the `bufferPtr` is set to nullptr, PF_RING will use zero copy mode and sets `bufferPtr` to point to the
632+
// packet data.
633+
uint8_t* bufferPtr = nullptr;
634+
uint32_t bufferLen = 0;
635+
std::vector<uint8_t> recvBuffer;
641636

637+
// If zero copy is not supported, allocate a buffer for the packet data.
638+
if (!zeroCopySupported)
639+
{
640+
recvBuffer.resize(PCPP_MAX_PACKET_SIZE);
641+
bufferPtr = recvBuffer.data();
642+
bufferLen = recvBuffer.size();
643+
}
644+
645+
while (!this->m_StopThread)
646+
{
642647
struct pfring_pkthdr pktHdr;
643-
int recvRes = pfring_recv(ring, &buffer, bufferLen, &pktHdr, 0);
648+
int recvRes = pfring_recv(ring, &bufferPtr, bufferLen, &pktHdr, 0);
644649
if (recvRes > 0)
645650
{
646651
// if caplen < len it means we don't have the whole packet. Treat this case as packet drop
@@ -651,7 +656,7 @@ namespace pcpp
651656
// continue;
652657
// }
653658

654-
RawPacket rawPacket(buffer, pktHdr.caplen, pktHdr.ts, false);
659+
RawPacket rawPacket(bufferPtr, pktHdr.caplen, pktHdr.ts, false);
655660
this->m_OnPacketsArriveCallback(&rawPacket, 1, coreId, this, this->m_OnPacketsArriveUserCookie);
656661
}
657662
else if (recvRes < 0)

0 commit comments

Comments
 (0)