]> git.proxmox.com Git - rustc.git/blob - vendor/socket2/SO_ACCEPTCONN.patch
New upstream version 1.52.1+dfsg1
[rustc.git] / vendor / socket2 / SO_ACCEPTCONN.patch
1 diff --git a/src/socket.rs b/src/socket.rs
2 index 725ad2a..3f7b499 100644
3 --- a/src/socket.rs
4 +++ b/src/socket.rs
5 @@ -963,6 +963,26 @@ fn inner(&self) -> &sys::Socket {
6 }
7 }
8
9 +/// Socket options get/set using `SOL_SOCKET`.
10 +///
11 +/// Additional documentation can be found in documentation of the OS.
12 +/// * Linux: <https://man7.org/linux/man-pages/man7/socket.7.html>
13 +/// * Windows: <https://docs.microsoft.com/en-us/windows/win32/winsock/sol-socket-socket-options>
14 +impl Socket {
15 + /// Gets the value of the `SO_ACCEPTCONN` option on this socket.
16 + ///
17 + /// Returns `true` if this socket has been marked to accept connections with
18 + /// [`listen`].
19 + ///
20 + /// [`listen`]: Socket::listen
21 + pub fn is_listener(&self) -> io::Result<bool> {
22 + unsafe {
23 + getsockopt::<c_int>(self.inner, sys::SOL_SOCKET, sys::SO_ACCEPTCONN)
24 + .map(|is_listener| is_listener != 0)
25 + }
26 + }
27 +}
28 +
29 /// Socket options for TCP socket, get/set using `IPPROTO_TCP`.
30 ///
31 /// Additional documentation can be found in documentation of the OS.
32 diff --git a/src/sys/unix.rs b/src/sys/unix.rs
33 index 60593a1..51b916a 100644
34 --- a/src/sys/unix.rs
35 +++ b/src/sys/unix.rs
36 @@ -54,8 +54,8 @@
37 pub(crate) use libc::MSG_OOB;
38 pub(crate) use libc::{
39 IPPROTO_IP, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS,
40 - IPV6_V6ONLY, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_TTL, MSG_PEEK, SOL_SOCKET, SO_BROADCAST,
41 - SO_ERROR, TCP_NODELAY,
42 + IPV6_V6ONLY, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_TTL, MSG_PEEK, SOL_SOCKET, SO_ACCEPTCONN,
43 + SO_BROADCAST, SO_ERROR, TCP_NODELAY,
44 };
45
46 // See this type in the Windows file.
47 diff --git a/src/sys/windows.rs b/src/sys/windows.rs
48 index 663b63f..a212525 100644
49 --- a/src/sys/windows.rs
50 +++ b/src/sys/windows.rs
51 @@ -60,7 +60,7 @@
52 pub(crate) use winapi::um::ws2tcpip::socklen_t;
53 // Used in `Socket`.
54 pub(crate) use winapi::shared::ws2def::{
55 - IPPROTO_IP, SOL_SOCKET, SO_BROADCAST, SO_ERROR, TCP_NODELAY,
56 + IPPROTO_IP, SOL_SOCKET, SO_ACCEPTCONN, SO_BROADCAST, SO_ERROR, TCP_NODELAY,
57 };
58 pub(crate) use winapi::shared::ws2ipdef::{
59 IPV6_MULTICAST_HOPS, IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS, IPV6_V6ONLY, IP_MULTICAST_LOOP,
60 diff --git a/tests/options.rs b/tests/options.rs
61 index 9c3d1c2..2d256a7 100644
62 --- a/tests/options.rs
63 +++ b/tests/options.rs
64 @@ -1,6 +1,8 @@
65 //! Tests for getting and setting socket options.
66
67 -use socket2::{Domain, Socket, Type};
68 +use std::net::SocketAddr;
69 +
70 +use socket2::{Domain, Protocol, Socket, Type};
71
72 /// Macro to create a simple test to set and get a socket option.
73 macro_rules! test {
74 @@ -86,6 +88,22 @@ fn $get_fn() {
75 set_mark(123)
76 );
77
78 +#[test]
79 +fn is_listener() {
80 + // TODO: IPv6.
81 +
82 + let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))
83 + .expect("failed to create `Socket`");
84 + //assert_eq!(socket.is_listener().unwrap(), false);
85 +
86 + let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
87 + let addr = addr.into();
88 + socket.bind(&addr).unwrap();
89 + socket.listen(1).unwrap();
90 + dbg!(socket.is_listener());
91 + assert_eq!(socket.is_listener().unwrap(), true);
92 +}
93 +
94 test!(IPv4 ttl, set_ttl(40));
95 #[cfg(not(windows))] // TODO: returns `WSAENOPROTOOPT` (10042) on Windows.
96 test!(IPv4 broadcast, set_broadcast(true));