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

net: Add get/set reuseport and get_localaddr/reuseaddr for TcpSocket #3083

Merged
merged 4 commits into from Nov 2, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
113 changes: 113 additions & 0 deletions tokio/src/net/tcp/socket.rs
Expand Up @@ -183,6 +183,119 @@ impl TcpSocket {
self.inner.set_reuseaddr(reuseaddr)
}

/// Retrieves the value set for `SO_REUSEADDR` on this socket
///
/// # Examples
///
/// ```no_run
/// use tokio::net::TcpSocket;
///
/// use std::io;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let addr = "127.0.0.1:8080".parse().unwrap();
///
/// let socket = TcpSocket::new_v4()?;
/// socket.set_reuseaddr(true)?;
/// assert!(socket.reuseaddr().unwrap());
/// socket.bind(addr)?;
///
/// let listener = socket.listen(1024)?;
/// Ok(())
/// }
/// ```
pub fn reuseaddr(&self) -> io::Result<bool> {
self.inner.get_reuseaddr()
}

/// Allow the socket to bind to an in-use port. Only available for unix systems
/// (excluding Solaris & Illumos).
///
/// Behavior is platform specific. Refer to the target platform's
/// documentation for more details.
///
/// # Examples
///
/// ```no_run
/// use tokio::net::TcpSocket;
///
/// use std::io;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let addr = "127.0.0.1:8080".parse().unwrap();
///
/// let socket = TcpSocket::new_v4()?;
/// socket.set_reuseport(true)?;
/// socket.bind(addr)?;
///
/// let listener = socket.listen(1024)?;
/// Ok(())
/// }
/// ```
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
pub fn set_reuseport(&self, reuseport: bool) -> io::Result<()> {
self.inner.set_reuseport(reuseport)
}

/// Allow the socket to bind to an in-use port. Only available for unix systems
/// (excluding Solaris & Illumos).
///
/// Behavior is platform specific. Refer to the target platform's
/// documentation for more details.
///
/// # Examples
///
/// ```no_run
/// use tokio::net::TcpSocket;
///
/// use std::io;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let addr = "127.0.0.1:8080".parse().unwrap();
///
/// let socket = TcpSocket::new_v4()?;
/// socket.set_reuseport(true)?;
/// assert!(socket.reuseport().unwrap());
/// socket.bind(addr)?;
///
/// let listener = socket.listen(1024)?;
/// Ok(())
/// }
/// ```
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
pub fn reuseport(&self) -> io::Result<bool> {
self.inner.get_reuseport()
}

/// Get the local address of this socket.
///
/// Will fail on windows if called before `bind`.
///
/// # Examples
///
/// ```no_run
/// use tokio::net::TcpSocket;
///
/// use std::io;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let addr = "127.0.0.1:8080".parse().unwrap();
///
/// let socket = TcpSocket::new_v4()?;
/// socket.bind(addr)?;
/// assert_eq!(socket.local_addr().unwrap().to_string(), "127.0.0.1:8080");
/// let listener = socket.listen(1024)?;
/// Ok(())
/// }
/// ```
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.inner.get_localaddr()
}

/// Bind the socket to the given address.
///
/// This calls the `bind(2)` operating-system function. Behavior is
Expand Down