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 1 commit
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
121 changes: 121 additions & 0 deletions tokio/src/net/tcp/socket.rs
Expand Up @@ -183,6 +183,127 @@ 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.get_reuseaddr().unwrap());
/// socket.bind(addr)?;
///
/// let listener = socket.listen(1024)?;
/// # drop(listener);
zekisherif marked this conversation as resolved.
Show resolved Hide resolved
///
/// Ok(())
/// }
/// ```
pub fn get_reuseaddr(&self) -> io::Result<bool> {
zekisherif marked this conversation as resolved.
Show resolved Hide resolved
self.inner.get_reuseaddr()
}

/// Allow the socket to bind to an in-use port. Only available for unix systems
/// (excluding Solaris).
///
/// 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)?;
/// # drop(listener);
///
/// Ok(())
/// }
/// ```
#[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))]
zekisherif marked this conversation as resolved.
Show resolved Hide resolved
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).
///
/// 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.get_reuseport().unwrap());
/// socket.bind(addr)?;
///
/// let listener = socket.listen(1024)?;
/// # drop(listener);
///
/// Ok(())
/// }
/// ```
#[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))]
zekisherif marked this conversation as resolved.
Show resolved Hide resolved
pub fn get_reuseport(&self) -> io::Result<bool> {
zekisherif marked this conversation as resolved.
Show resolved Hide resolved
self.inner.get_reuseport()
}

/// Get the local address of this socket.
///
/// Will `Err` on windows if called before `bind`
zekisherif marked this conversation as resolved.
Show resolved Hide resolved
///
/// # 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.get_localaddr().unwrap().to_string(), "127.0.0.1:8080");
/// let listener = socket.listen(1024)?;
/// # drop(listener);
///
/// Ok(())
/// }
/// ```
pub fn get_localaddr(&self) -> io::Result<SocketAddr> {
zekisherif marked this conversation as resolved.
Show resolved Hide resolved
self.inner.get_localaddr()
}

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