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

feat: added ttl and set_ttl methods to TcpListener #1079

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl TcpListener {
Incoming {
incoming: Box::pin(self.watcher.incoming()),
}
}
}

/// Turn this into a stream over the connections being received on this
/// listener.
Expand Down Expand Up @@ -211,6 +211,48 @@ impl TcpListener {
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.watcher.get_ref().local_addr()
}

/// Gets the value of the `IP_TTL` option for this socket.
///
/// For more information about this option, see [`TcpListener::set_ttl`].
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::net::TcpListener;
///
/// let listener = TcpListener::bind("127.0.0.1:80").await?;
/// listener.set_ttl(100).expect("could not set TTL");
/// assert_eq!(listener.ttl().unwrap_or(0), 100);
/// #
/// # Ok(()) }) }
/// ```
pub fn ttl(&self) -> io::Result<u32> {
self.watcher.get_ref().ttl()
}

/// Sets the value for the `IP_TTL` option on this socket.
///
/// This value sets the time-to-live field that is used in every packet sent
/// from this socket.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use std::net::TcpListener;
///
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
/// listener.set_ttl(100).expect("could not set TTL");
/// #
/// # Ok(()) }) }
/// ```
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
self.watcher.get_ref().set_ttl(ttl)
}
}

/// A stream of incoming TCP connections.
Expand All @@ -233,7 +275,11 @@ impl Stream for Incoming<'_> {

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let res = ready!(Pin::new(&mut self.incoming).poll_next(cx));
Poll::Ready(res.map(|res| res.map(|stream| TcpStream { watcher: Arc::new(stream) })))
Poll::Ready(res.map(|res| {
res.map(|stream| TcpStream {
watcher: Arc::new(stream),
})
}))
}
}

Expand Down