Skip to content

Commit 34a0786

Browse files
coolreader18Thomasdezeeuw
authored andcommittedApr 23, 2021
Enable Socket::r#type on all platforms
1 parent 8716af6 commit 34a0786

File tree

4 files changed

+14
-28
lines changed

4 files changed

+14
-28
lines changed
 

‎src/socket.rs

+6
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ impl Socket {
280280
sys::getpeername(self.inner)
281281
}
282282

283+
/// Returns the [`Type`] of this socket by checking the `SO_TYPE` option on
284+
/// this socket.
285+
pub fn r#type(&self) -> io::Result<Type> {
286+
unsafe { getsockopt::<c_int>(self.inner, sys::SOL_SOCKET, sys::SO_TYPE).map(Type) }
287+
}
288+
283289
/// Creates a new independently owned handle to the underlying socket.
284290
///
285291
/// # Notes

‎src/sys/unix.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(crate) use libc::{
8282
IPV6_MULTICAST_HOPS, IPV6_MULTICAST_IF, IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS, IPV6_V6ONLY,
8383
IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_MULTICAST_IF, IP_MULTICAST_LOOP, IP_MULTICAST_TTL,
8484
IP_TTL, MSG_OOB, MSG_PEEK, SOL_SOCKET, SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_RCVBUF,
85-
SO_RCVTIMEO, SO_REUSEADDR, SO_SNDBUF, SO_SNDTIMEO, TCP_NODELAY,
85+
SO_RCVTIMEO, SO_REUSEADDR, SO_SNDBUF, SO_SNDTIMEO, SO_TYPE, TCP_NODELAY,
8686
};
8787
#[cfg(not(any(
8888
target_os = "dragonfly",
@@ -1081,21 +1081,6 @@ impl crate::Socket {
10811081
}
10821082
}
10831083

1084-
/// Returns the [`Type`] of this socket by checking the `SO_TYPE` option on
1085-
/// this socket.
1086-
#[cfg(all(
1087-
feature = "all",
1088-
any(
1089-
target_os = "android",
1090-
target_os = "freebsd",
1091-
target_os = "fuchsia",
1092-
target_os = "linux",
1093-
)
1094-
))]
1095-
pub fn r#type(&self) -> io::Result<Type> {
1096-
unsafe { getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_TYPE).map(Type) }
1097-
}
1098-
10991084
/// Gets the value for the `SO_MARK` option on this socket.
11001085
///
11011086
/// This value gets the socket mark field for each packet sent through

‎src/sys/windows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(crate) use winapi::um::ws2tcpip::socklen_t;
6464
// Used in `Socket`.
6565
pub(crate) use winapi::shared::ws2def::{
6666
IPPROTO_IP, SOL_SOCKET, SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE,
67-
SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_SNDBUF, SO_SNDTIMEO, TCP_NODELAY,
67+
SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_SNDBUF, SO_SNDTIMEO, SO_TYPE, TCP_NODELAY,
6868
};
6969
pub(crate) use winapi::shared::ws2ipdef::{
7070
IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_MREQ as Ipv6Mreq, IPV6_MULTICAST_HOPS,

‎tests/socket.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -924,15 +924,6 @@ fn protocol() {
924924
assert_eq!(socket.protocol().unwrap(), Some(Protocol::UDP));
925925
}
926926

927-
#[cfg(all(
928-
feature = "all",
929-
any(
930-
target_os = "android",
931-
target_os = "freebsd",
932-
target_os = "fuchsia",
933-
target_os = "linux",
934-
)
935-
))]
936927
#[test]
937928
fn r#type() {
938929
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
@@ -941,8 +932,12 @@ fn r#type() {
941932
let socket = Socket::new(Domain::IPV6, Type::DGRAM, None).unwrap();
942933
assert_eq!(socket.r#type().unwrap(), Type::DGRAM);
943934

944-
let socket = Socket::new(Domain::UNIX, Type::SEQPACKET, None).unwrap();
945-
assert_eq!(socket.r#type().unwrap(), Type::SEQPACKET);
935+
// macos doesn't support seqpacket
936+
#[cfg(all(unix, not(target_vendor = "apple"), feature = "all"))]
937+
{
938+
let socket = Socket::new(Domain::UNIX, Type::SEQPACKET, None).unwrap();
939+
assert_eq!(socket.r#type().unwrap(), Type::SEQPACKET);
940+
}
946941
}
947942

948943
#[cfg(all(feature = "all", target_os = "linux"))]

0 commit comments

Comments
 (0)
Please sign in to comment.