From 9f8d41ad1c654b16aa3322a0d0af857d444e677b Mon Sep 17 00:00:00 2001 From: Zeki Sherif Date: Wed, 21 Oct 2020 12:34:56 -0700 Subject: [PATCH 1/4] net: Add get/set reuseport, get_localaddr and get_reuseaddr for TcpSocket --- tokio/src/net/tcp/socket.rs | 121 ++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/tokio/src/net/tcp/socket.rs b/tokio/src/net/tcp/socket.rs index 5b0f802afab..4f9c3bf34c2 100644 --- a/tokio/src/net/tcp/socket.rs +++ b/tokio/src/net/tcp/socket.rs @@ -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); + /// + /// Ok(()) + /// } + /// ``` + pub fn get_reuseaddr(&self) -> io::Result { + 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"))))] + 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"))))] + pub fn get_reuseport(&self) -> io::Result { + self.inner.get_reuseport() + } + + /// Get the local address of this socket. + /// + /// Will `Err` 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.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 { + self.inner.get_localaddr() + } + /// Bind the socket to the given address. /// /// This calls the `bind(2)` operating-system function. Behavior is From f334012c2c6282cd5924eff1a84e58efb907d173 Mon Sep 17 00:00:00 2001 From: Zeki Sherif Date: Mon, 2 Nov 2020 07:46:46 -0800 Subject: [PATCH 2/4] Address comments --- tokio/src/net/tcp/socket.rs | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/tokio/src/net/tcp/socket.rs b/tokio/src/net/tcp/socket.rs index 4f9c3bf34c2..9cf8b96e286 100644 --- a/tokio/src/net/tcp/socket.rs +++ b/tokio/src/net/tcp/socket.rs @@ -198,21 +198,19 @@ impl TcpSocket { /// /// let socket = TcpSocket::new_v4()?; /// socket.set_reuseaddr(true)?; - /// assert!(socket.get_reuseaddr().unwrap()); + /// assert!(socket.reuseaddr().unwrap()); /// socket.bind(addr)?; /// /// let listener = socket.listen(1024)?; - /// # drop(listener); - /// /// Ok(()) /// } /// ``` - pub fn get_reuseaddr(&self) -> io::Result { + pub fn reuseaddr(&self) -> io::Result { self.inner.get_reuseaddr() } /// Allow the socket to bind to an in-use port. Only available for unix systems - /// (excluding Solaris). + /// (excluding Solaris & Illumos). /// /// Behavior is platform specific. Refer to the target platform's /// documentation for more details. @@ -233,18 +231,16 @@ impl TcpSocket { /// socket.bind(addr)?; /// /// let listener = socket.listen(1024)?; - /// # drop(listener); - /// /// Ok(()) /// } /// ``` - #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] + #[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). + /// (excluding Solaris & Illumos). /// /// Behavior is platform specific. Refer to the target platform's /// documentation for more details. @@ -262,23 +258,21 @@ impl TcpSocket { /// /// let socket = TcpSocket::new_v4()?; /// socket.set_reuseport(true)?; - /// assert!(socket.get_reuseport().unwrap()); + /// assert!(socket.reuseport().unwrap()); /// socket.bind(addr)?; /// /// let listener = socket.listen(1024)?; - /// # drop(listener); - /// /// Ok(()) /// } /// ``` - #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] - pub fn get_reuseport(&self) -> io::Result { + #[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))] + pub fn reuseport(&self) -> io::Result { self.inner.get_reuseport() } /// Get the local address of this socket. /// - /// Will `Err` on windows if called before `bind` + /// Will fail on windows if called before `bind`. /// /// # Examples /// @@ -293,14 +287,12 @@ impl TcpSocket { /// /// let socket = TcpSocket::new_v4()?; /// socket.bind(addr)?; - /// assert_eq!(socket.get_localaddr().unwrap().to_string(), "127.0.0.1:8080"); + /// assert_eq!(socket.local_addr().unwrap().to_string(), "127.0.0.1:8080"); /// let listener = socket.listen(1024)?; - /// # drop(listener); - /// /// Ok(()) /// } /// ``` - pub fn get_localaddr(&self) -> io::Result { + pub fn local_addr(&self) -> io::Result { self.inner.get_localaddr() } From c6608348ff17febd2fce1ac53969698b29fb39c0 Mon Sep 17 00:00:00 2001 From: Zeki Sherif Date: Mon, 2 Nov 2020 10:08:06 -0800 Subject: [PATCH 3/4] set cfg attr --- tokio/src/net/tcp/socket.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tokio/src/net/tcp/socket.rs b/tokio/src/net/tcp/socket.rs index 9cf8b96e286..53d56d938ab 100644 --- a/tokio/src/net/tcp/socket.rs +++ b/tokio/src/net/tcp/socket.rs @@ -235,6 +235,7 @@ impl TcpSocket { /// } /// ``` #[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))] + #[cfg_attr(docsrs, doc(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) } @@ -266,6 +267,7 @@ impl TcpSocket { /// } /// ``` #[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))] + #[cfg_attr(docsrs, doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))))] pub fn reuseport(&self) -> io::Result { self.inner.get_reuseport() } From a10488a1ec488137a51f805c8d3f92422e402cf6 Mon Sep 17 00:00:00 2001 From: Zeki Sherif Date: Mon, 2 Nov 2020 10:11:59 -0800 Subject: [PATCH 4/4] fmt --- tokio/src/net/tcp/socket.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tokio/src/net/tcp/socket.rs b/tokio/src/net/tcp/socket.rs index 53d56d938ab..9e3ca9908bf 100644 --- a/tokio/src/net/tcp/socket.rs +++ b/tokio/src/net/tcp/socket.rs @@ -235,7 +235,10 @@ impl TcpSocket { /// } /// ``` #[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))] - #[cfg_attr(docsrs, doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))))] + #[cfg_attr( + docsrs, + doc(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) } @@ -267,7 +270,10 @@ impl TcpSocket { /// } /// ``` #[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))] - #[cfg_attr(docsrs, doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))))] + #[cfg_attr( + docsrs, + doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))) + )] pub fn reuseport(&self) -> io::Result { self.inner.get_reuseport() }