1 //! Unix-specific extensions to the `std::net` types.
2 
3 use std::io;
4 use sys::c::{self, c_int};
5 
6 use {TcpBuilder, UdpBuilder};
7 use ext::{self, AsSock};
8 
9 /// Unix-specific extensions for the `TcpBuilder` type in this library.
10 pub trait UnixTcpBuilderExt {
11     /// Set value for the `SO_REUSEPORT` option on this socket.
12     ///
13     /// This indicates that further calls to `bind` may allow reuse of local
14     /// addresses. For IPv4 sockets this means that a socket may bind even when
15     /// there's a socket already listening on this port.
reuse_port(&self, reuse: bool) -> io::Result<&Self>16     fn reuse_port(&self, reuse: bool) -> io::Result<&Self>;
17 
18     /// Check the value of the `SO_REUSEPORT` option on this socket.
get_reuse_port(&self) -> io::Result<bool>19     fn get_reuse_port(&self) -> io::Result<bool>;
20 }
21 
22 impl UnixTcpBuilderExt for TcpBuilder {
reuse_port(&self, reuse: bool) -> io::Result<&Self>23     fn reuse_port(&self, reuse: bool) -> io::Result<&Self> {
24         ext::set_opt(self.as_sock(), c::SOL_SOCKET, c::SO_REUSEPORT,
25                     reuse as c_int).map(|()| self)
26     }
27 
get_reuse_port(&self) -> io::Result<bool>28     fn get_reuse_port(&self) -> io::Result<bool> {
29         ext::get_opt(self.as_sock(), c::SOL_SOCKET, c::SO_REUSEPORT)
30             .map(ext::int2bool)
31     }
32 }
33 
34 /// Unix-specific extensions for the `UdpBuilder` type in this library.
35 pub trait UnixUdpBuilderExt {
36     /// Set value for the `SO_REUSEPORT` option on this socket.
37     ///
38     /// This indicates that further calls to `bind` may allow reuse of local
39     /// addresses. For IPv4 sockets this means that a socket may bind even when
40     /// there's a socket already listening on this port.
reuse_port(&self, reuse: bool) -> io::Result<&Self>41     fn reuse_port(&self, reuse: bool) -> io::Result<&Self>;
42 
43     /// Check the value of the `SO_REUSEPORT` option on this socket.
get_reuse_port(&self) -> io::Result<bool>44     fn get_reuse_port(&self) -> io::Result<bool>;
45 }
46 
47 impl UnixUdpBuilderExt for UdpBuilder {
reuse_port(&self, reuse: bool) -> io::Result<&Self>48     fn reuse_port(&self, reuse: bool) -> io::Result<&Self> {
49         ext::set_opt(self.as_sock(), c::SOL_SOCKET, c::SO_REUSEPORT,
50                     reuse as c_int).map(|()| self)
51     }
52 
get_reuse_port(&self) -> io::Result<bool>53     fn get_reuse_port(&self) -> io::Result<bool> {
54         ext::get_opt(self.as_sock(), c::SOL_SOCKET, c::SO_REUSEPORT)
55             .map(ext::int2bool)
56     }
57 }
58