-
Notifications
You must be signed in to change notification settings - Fork 685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement poll() for startCaptureBlockingMode
#1245
Conversation
@seladb please take a look. |
@tigercosmos I updated the base branch to Please also note CI fails on many platforms. Can you please check? |
@seladb Sure, I will fix the CI. I would like to ask your opinion about this PR. Does the current implementation make sense to you? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see a few initial comments. I will do a more thorough review once CI passes
@seladb I think it's ready for review |
it's weird, it seems that |
@seladb I try to use
Moreover, currently CI containers complain there is no iptables, so we also need to install it. I also check the changes on |
Maybe it's not possible to run |
True... after survey, seems that Github action has some limitations. Or, probably we should think of some other ways to test the function. |
@seladb Maybe we should not make it very complicated... as long as the poll version can work the same as the original version, maybe we don't need to prove that |
@tigercosmos I tend to agree... I can't think of a good way to test the poll timeout so let's just keep the existing tests. It's not ideal but we currently don't have a CI infra that is robust enough to test this scenario. Can you clean up the PR and I'll review it again? |
Pcap++/src/PcapLiveDevice.cpp
Outdated
m_StopThread = true; | ||
#if !defined(_WIN32) | ||
// Be careful when you modify the code related to `poll` in this block | ||
// Some tests are not supported to run on CIs: `TestPcapLiveDeviceBlockingModePollTimeout`, `TestPcapLiveDeviceBlockingModeNotTimeoutWithoutPoll` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seladb I added some comments here to mention that manual tests are required here
|
||
#if !defined(_WIN32) | ||
configs.emplace_back(); // the config used poll | ||
configs[1].usePoll = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this ensures the behaviour is as same as before.
|
||
PTF_TEST_CASE(TestPcapLiveDeviceBlockingModePollTimeout) | ||
{ | ||
#if !defined(_WIN32) and defined(MANUAL_TEST) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although this test cannot run on CI, we can manually run it locally.
I think it is worth keeping it, so I added a MANUAL_TEST
flag for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found that it's really difficult to use iptables to block all packets. No matter how I set up rules, I still can see packets on Wireshark. Maybe the easiest way is to use a clean interface to test... since we can ensure no packets on it ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seladb please let me know your opinion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I also encountered problems with iptables
. I think we should just remove these tests. There is no point in MANUAL_TEST
which we don't run anywhere...
PTF_ASSERT_TRUE(liveDev->startCapture(packetArrives, &packetCount)); | ||
PTF_TEST_CASE(TestPcapLiveDeviceBlockingModeNotTimeoutWithoutPoll) | ||
{ | ||
#if !defined(_WIN32) and defined(MANUAL_TEST) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for this test. use MANUAL_TEST
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this test also...
Pcap++/src/PcapLiveDevice.cpp
Outdated
else if(ready < 0) | ||
{ | ||
PCPP_LOG_ERROR("poll() got error '" << strerror(errno) << "'"); | ||
return -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can return 0
directly because in lines 597-600 we reset a few private members.
Instead we should set m_StopThread = true
and maybe add a new local variable pollError
and I think we can return 0
in this case (poll error)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
Pcap++/src/PcapLiveDevice.cpp
Outdated
} | ||
#else | ||
PCPP_LOG_ERROR("Windows doesn't support poll()"); | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto: we cannot return 0
here. We should probably add a new local variable pollError
and set it to true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
Pcap++/src/PcapLiveDevice.cpp
Outdated
{ | ||
while (!m_StopThread) | ||
{ | ||
if (pcap_dispatch(m_PcapDescriptor, -1, onPacketArrivesBlockingMode, reinterpret_cast<uint8_t*>(this)) == -1) | ||
if(pcap_dispatch(m_PcapDescriptor, -1, onPacketArrivesBlockingMode, (uint8_t*)this) < 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error in pcap_dispatch
is -1
and not other values:
https://linux.die.net/man/3/pcap_dispatch
It returns -1 if an error occurs or -2 if the loop terminated due to a call to pcap_breakloop() before any packets were processed. If your application uses pcap_breakloop(), make sure that you explicitly check for -1 and -2, rather than just checking for a return value < 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, fixed.
|
||
PTF_TEST_CASE(TestPcapLiveDeviceBlockingModePollTimeout) | ||
{ | ||
#if !defined(_WIN32) and defined(MANUAL_TEST) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I also encountered problems with iptables
. I think we should just remove these tests. There is no point in MANUAL_TEST
which we don't run anywhere...
PTF_ASSERT_TRUE(liveDev->startCapture(packetArrives, &packetCount)); | ||
PTF_TEST_CASE(TestPcapLiveDeviceBlockingModeNotTimeoutWithoutPoll) | ||
{ | ||
#if !defined(_WIN32) and defined(MANUAL_TEST) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this test also...
@seladb all addressed |
finally 🎉 |
Thank you @tigercosmos for your hard work and patience in solving all the issues, very much appreciated! 🙏 |
please refer to #1220
new in PR:
timeout
ofstartCaptureBlockingMode
keeps the unit as second but changes to usedouble
type to allow millisecond precision.usePoll
optionpcap_dispatch
. Usepoll()
to frequently check if it is already timeout.