Skip to content
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

next_with_timeout() with zero timeout waits indefinitely #671

Open
wxifze opened this issue Dec 20, 2023 · 1 comment
Open

next_with_timeout() with zero timeout waits indefinitely #671

wxifze opened this issue Dec 20, 2023 · 1 comment

Comments

@wxifze
Copy link

wxifze commented Dec 20, 2023

The following code is expected to poll in a tight loop, instead it waits indefinitely for every packet (confirmed with strace(1)):

use pnet::{transport::*, packet::ip::*};
use std::time::Duration;

fn main() {
    let (_, mut tx) = transport_channel(
        4096, TransportChannelType::Layer4(
            TransportProtocol::Ipv4(
                IpNextHeaderProtocols::Icmp
    ))).unwrap();
    loop {
        let _ = icmp_packet_iter(&mut tx).next_with_timeout(Duration::new(0, 0));
    }
}

I suspect this is because socket option SO_RCVTIMEO interprets zero timeout as infinite. From socket(7) man:

If the timeout is set to zero (the default) then the operation will never timeout.

The problem can be solved by specifying timeout as the smallest non-zero value (1000ns since timeval.tv_usec is in microseconds):

use pnet::{transport::*, packet::ip::*};
use std::time::Duration;

fn main() {
    let (_, mut tx) = transport_channel(
        4096, TransportChannelType::Layer4(
            TransportProtocol::Ipv4(
                IpNextHeaderProtocols::Icmp
    ))).unwrap();
    loop {
        let _ = icmp_packet_iter(&mut tx).next_with_timeout(Duration::new(0, 1000));
    }
}

Either values from 0 to 999ns should be automatically converted to 1us or the documentation should be updated to mention this unexpected behavior. Since there is already a blocking method next() I prefer the former.

@zszep
Copy link

zszep commented Feb 7, 2024

@wxifze
Did you ever solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants