Skip to content

Commit ae4e8d7

Browse files
authored
net: add get/set reuseport, reuseaddr, localaddr for TcpSocket (#3083)
1 parent 7a18ca2 commit ae4e8d7

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

tokio/src/net/tcp/socket.rs

+121
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,127 @@ impl TcpSocket {
183183
self.inner.set_reuseaddr(reuseaddr)
184184
}
185185

186+
/// Retrieves the value set for `SO_REUSEADDR` on this socket
187+
///
188+
/// # Examples
189+
///
190+
/// ```no_run
191+
/// use tokio::net::TcpSocket;
192+
///
193+
/// use std::io;
194+
///
195+
/// #[tokio::main]
196+
/// async fn main() -> io::Result<()> {
197+
/// let addr = "127.0.0.1:8080".parse().unwrap();
198+
///
199+
/// let socket = TcpSocket::new_v4()?;
200+
/// socket.set_reuseaddr(true)?;
201+
/// assert!(socket.reuseaddr().unwrap());
202+
/// socket.bind(addr)?;
203+
///
204+
/// let listener = socket.listen(1024)?;
205+
/// Ok(())
206+
/// }
207+
/// ```
208+
pub fn reuseaddr(&self) -> io::Result<bool> {
209+
self.inner.get_reuseaddr()
210+
}
211+
212+
/// Allow the socket to bind to an in-use port. Only available for unix systems
213+
/// (excluding Solaris & Illumos).
214+
///
215+
/// Behavior is platform specific. Refer to the target platform's
216+
/// documentation for more details.
217+
///
218+
/// # Examples
219+
///
220+
/// ```no_run
221+
/// use tokio::net::TcpSocket;
222+
///
223+
/// use std::io;
224+
///
225+
/// #[tokio::main]
226+
/// async fn main() -> io::Result<()> {
227+
/// let addr = "127.0.0.1:8080".parse().unwrap();
228+
///
229+
/// let socket = TcpSocket::new_v4()?;
230+
/// socket.set_reuseport(true)?;
231+
/// socket.bind(addr)?;
232+
///
233+
/// let listener = socket.listen(1024)?;
234+
/// Ok(())
235+
/// }
236+
/// ```
237+
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
238+
#[cfg_attr(
239+
docsrs,
240+
doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos"))))
241+
)]
242+
pub fn set_reuseport(&self, reuseport: bool) -> io::Result<()> {
243+
self.inner.set_reuseport(reuseport)
244+
}
245+
246+
/// Allow the socket to bind to an in-use port. Only available for unix systems
247+
/// (excluding Solaris & Illumos).
248+
///
249+
/// Behavior is platform specific. Refer to the target platform's
250+
/// documentation for more details.
251+
///
252+
/// # Examples
253+
///
254+
/// ```no_run
255+
/// use tokio::net::TcpSocket;
256+
///
257+
/// use std::io;
258+
///
259+
/// #[tokio::main]
260+
/// async fn main() -> io::Result<()> {
261+
/// let addr = "127.0.0.1:8080".parse().unwrap();
262+
///
263+
/// let socket = TcpSocket::new_v4()?;
264+
/// socket.set_reuseport(true)?;
265+
/// assert!(socket.reuseport().unwrap());
266+
/// socket.bind(addr)?;
267+
///
268+
/// let listener = socket.listen(1024)?;
269+
/// Ok(())
270+
/// }
271+
/// ```
272+
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
273+
#[cfg_attr(
274+
docsrs,
275+
doc(cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos"))))
276+
)]
277+
pub fn reuseport(&self) -> io::Result<bool> {
278+
self.inner.get_reuseport()
279+
}
280+
281+
/// Get the local address of this socket.
282+
///
283+
/// Will fail on windows if called before `bind`.
284+
///
285+
/// # Examples
286+
///
287+
/// ```no_run
288+
/// use tokio::net::TcpSocket;
289+
///
290+
/// use std::io;
291+
///
292+
/// #[tokio::main]
293+
/// async fn main() -> io::Result<()> {
294+
/// let addr = "127.0.0.1:8080".parse().unwrap();
295+
///
296+
/// let socket = TcpSocket::new_v4()?;
297+
/// socket.bind(addr)?;
298+
/// assert_eq!(socket.local_addr().unwrap().to_string(), "127.0.0.1:8080");
299+
/// let listener = socket.listen(1024)?;
300+
/// Ok(())
301+
/// }
302+
/// ```
303+
pub fn local_addr(&self) -> io::Result<SocketAddr> {
304+
self.inner.get_localaddr()
305+
}
306+
186307
/// Bind the socket to the given address.
187308
///
188309
/// This calls the `bind(2)` operating-system function. Behavior is

0 commit comments

Comments
 (0)