Skip to content

Commit 39050f6

Browse files
BerrysoftThomasdezeeuw
authored andcommitted
Add protocol() for Windows.
1 parent 16e85d5 commit 39050f6

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/sys/windows.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ use windows_sys::Win32::Foundation::{SetHandleInformation, HANDLE, HANDLE_FLAG_I
2323
use windows_sys::Win32::Networking::WinSock::{
2424
self, tcp_keepalive, FIONBIO, IN6_ADDR, IN6_ADDR_0, INVALID_SOCKET, IN_ADDR, IN_ADDR_0,
2525
POLLERR, POLLHUP, POLLRDNORM, POLLWRNORM, SD_BOTH, SD_RECEIVE, SD_SEND, SIO_KEEPALIVE_VALS,
26-
SOCKET_ERROR, WSABUF, WSAEMSGSIZE, WSAESHUTDOWN, WSAPOLLFD, WSAPROTOCOL_INFOW,
27-
WSA_FLAG_NO_HANDLE_INHERIT, WSA_FLAG_OVERLAPPED,
26+
SOCKET_ERROR, SO_PROTOCOL_INFOW, WSABUF, WSAEMSGSIZE, WSAESHUTDOWN, WSAPOLLFD,
27+
WSAPROTOCOL_INFOW, WSA_FLAG_NO_HANDLE_INHERIT, WSA_FLAG_OVERLAPPED,
2828
};
2929
use windows_sys::Win32::System::Threading::INFINITE;
3030

31-
use crate::{MsgHdr, RecvFlags, SockAddr, TcpKeepalive, Type};
31+
use crate::{MsgHdr, Protocol, RecvFlags, SockAddr, TcpKeepalive, Type};
3232

3333
#[allow(non_camel_case_types)]
3434
pub(crate) type c_int = std::os::raw::c_int;
@@ -920,6 +920,19 @@ impl crate::Socket {
920920
Ok(())
921921
}
922922
}
923+
924+
/// Returns the [`Protocol`] of this socket by checking the `SO_PROTOCOL_INFOW`
925+
/// option on this socket.
926+
#[cfg(feature = "all")]
927+
pub fn protocol(&self) -> io::Result<Option<Protocol>> {
928+
let info = unsafe {
929+
getsockopt::<WSAPROTOCOL_INFOW>(self.as_raw(), SOL_SOCKET, SO_PROTOCOL_INFOW)?
930+
};
931+
match info.iProtocol {
932+
0 => Ok(None),
933+
p => Ok(Some(Protocol::from(p))),
934+
}
935+
}
923936
}
924937

925938
#[cfg_attr(docsrs, doc(cfg(windows)))]

tests/socket.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ fn domain() {
11431143
target_os = "freebsd",
11441144
target_os = "fuchsia",
11451145
target_os = "linux",
1146+
target_os = "windows",
11461147
)
11471148
))]
11481149
#[test]

0 commit comments

Comments
 (0)