Skip to content

Commit

Permalink
Check pcap_dispatch return value (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
seladb authored Dec 5, 2023
1 parent 4e8702a commit d08fdb8
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,24 @@ void PcapLiveDevice::captureThreadMain()
if (m_CaptureCallbackMode)
{
while (!m_StopThread)
pcap_dispatch(m_PcapDescriptor, -1, onPacketArrives, (uint8_t*)this);
{
if (pcap_dispatch(m_PcapDescriptor, -1, onPacketArrives, reinterpret_cast<uint8_t*>(this)) == -1)
{
PCPP_LOG_ERROR("pcap_dispatch returned an error: " << pcap_geterr(m_PcapDescriptor));
m_StopThread = true;
}
}
}
else
{
while (!m_StopThread)
pcap_dispatch(m_PcapDescriptor, 100, onPacketArrivesNoCallback, (uint8_t*)this);
{
if (pcap_dispatch(m_PcapDescriptor, 100, onPacketArrivesNoCallback, reinterpret_cast<uint8_t*>(this)) == -1)
{
PCPP_LOG_ERROR("pcap_dispatch returned an error: " << pcap_geterr(m_PcapDescriptor));
m_StopThread = true;
}
}
}
PCPP_LOG_DEBUG("Ended capture thread for device '" << m_Name << "'");
}
Expand Down Expand Up @@ -496,11 +508,18 @@ int PcapLiveDevice::startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacke
m_CaptureThreadStarted = true;
m_StopThread = false;

bool pcapDispatchError = false;

if (timeout <= 0)
{
while (!m_StopThread)
{
pcap_dispatch(m_PcapDescriptor, -1, onPacketArrivesBlockingMode, (uint8_t*)this);
if (pcap_dispatch(m_PcapDescriptor, -1, onPacketArrivesBlockingMode, reinterpret_cast<uint8_t*>(this)) == -1)
{
PCPP_LOG_ERROR("pcap_dispatch returned an error: " << pcap_geterr(m_PcapDescriptor));
pcapDispatchError = true;
m_StopThread = true;
}
}
curTimeSec = startTimeSec + timeout;
}
Expand All @@ -509,7 +528,12 @@ int PcapLiveDevice::startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacke
while (!m_StopThread && curTimeSec <= (startTimeSec + timeout))
{
long curTimeNSec = 0;
pcap_dispatch(m_PcapDescriptor, -1, onPacketArrivesBlockingMode, (uint8_t*)this);
if (pcap_dispatch(m_PcapDescriptor, -1, onPacketArrivesBlockingMode, reinterpret_cast<uint8_t*>(this)) == -1)
{
PCPP_LOG_ERROR("pcap_dispatch returned an error: " << pcap_geterr(m_PcapDescriptor));
pcapDispatchError = true;
m_StopThread = true;
}
clockGetTime(curTimeSec, curTimeNSec);
}
}
Expand All @@ -521,6 +545,11 @@ int PcapLiveDevice::startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacke
m_cbOnPacketArrivesBlockingMode = nullptr;
m_cbOnPacketArrivesBlockingModeUserCookie = nullptr;

if (pcapDispatchError)
{
return 0;
}

if (curTimeSec > (startTimeSec + timeout))
return -1;
return 1;
Expand Down

0 comments on commit d08fdb8

Please sign in to comment.