diff options
| author | Vita Batrla <vita.batrla@gmail.com> | 2020-01-17 22:46:32 +0100 |
|---|---|---|
| committer | Vita Batrla <vita.batrla@gmail.com> | 2020-01-17 22:46:32 +0100 |
| commit | dda32e4e535fb3fb9e728b8c96386db7d231b247 (patch) | |
| tree | 1396d585240db7759f1e05a1d158e1a6a0eb4751 /src/libstd/sys_common | |
| parent | 34878d7b05813e090b370f48b8d437e4bd875094 (diff) | |
| download | rust-dda32e4e535fb3fb9e728b8c96386db7d231b247.tar.gz rust-dda32e4e535fb3fb9e728b8c96386db7d231b247.zip | |
refactor fix using cfg_if!
Diffstat (limited to 'src/libstd/sys_common')
| -rw-r--r-- | src/libstd/sys_common/net.rs | 175 |
1 files changed, 55 insertions, 120 deletions
diff --git a/src/libstd/sys_common/net.rs b/src/libstd/sys_common/net.rs index 32e392f1efc..152da978bfd 100644 --- a/src/libstd/sys_common/net.rs +++ b/src/libstd/sys_common/net.rs @@ -12,80 +12,43 @@ use crate::sys_common::{AsInner, FromInner, IntoInner}; use crate::time::Duration; use libc::{c_int, c_void}; -#[cfg(any(target_os = "dragonfly", target_os = "freebsd", - target_os = "openbsd", target_os = "netbsd", - target_os = "solaris"))] -use libc::{c_uchar}; - -#[cfg(not(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "openbsd", - target_os = "netbsd", - target_os = "solaris", - target_os = "haiku", - target_os = "l4re" -)))] -use crate::sys::net::netc::IPV6_ADD_MEMBERSHIP; -#[cfg(not(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "openbsd", - target_os = "netbsd", - target_os = "solaris", - target_os = "haiku", - target_os = "l4re" -)))] -use crate::sys::net::netc::IPV6_DROP_MEMBERSHIP; -#[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "openbsd", - target_os = "netbsd", - target_os = "solaris", - target_os = "haiku", - target_os = "l4re" -))] -use crate::sys::net::netc::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP; -#[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "ios", - target_os = "macos", - target_os = "openbsd", - target_os = "netbsd", - target_os = "solaris", - target_os = "haiku", - target_os = "l4re" -))] -use crate::sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP; - -#[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "openbsd", - target_os = "netbsd", - target_os = "haiku" -))] -use libc::MSG_NOSIGNAL; -#[cfg(not(any( - target_os = "linux", - target_os = "android", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "openbsd", - target_os = "netbsd", - target_os = "haiku" -)))] -const MSG_NOSIGNAL: c_int = 0x0; + +cfg_if::cfg_if! { + if #[cfg(any( + target_os = "dragonfly", target_os = "freebsd", + target_os = "ios", target_os = "macos", + target_os = "openbsd", target_os = "netbsd", + target_os = "solaris", target_os = "haiku", target_os = "l4re"))] { + use crate::sys::net::netc::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP; + use crate::sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP; + } else { + use crate::sys::net::netc::IPV6_ADD_MEMBERSHIP; + use crate::sys::net::netc::IPV6_DROP_MEMBERSHIP; + } +} + +cfg_if::cfg_if! { + if #[cfg(any( + target_os = "linux", target_os = "android", + target_os = "dragonfly", target_os = "freebsd", + target_os = "openbsd", target_os = "netbsd", + target_os = "haiku"))] { + use libc::MSG_NOSIGNAL; + } else { + const MSG_NOSIGNAL: c_int = 0x0; + } +} + +cfg_if::cfg_if! { + if #[cfg(any( + target_os = "dragonfly", target_os = "freebsd", + target_os = "openbsd", target_os = "netbsd", + target_os = "solaris"))] { + type ip_mcast_type_v4 = c_uchar; + } else { + type ip_mcast_type_v4 = c_int; + } +} //////////////////////////////////////////////////////////////////////////////// // sockaddr and misc bindings @@ -569,6 +532,24 @@ impl UdpSocket { Ok(raw != 0) } + pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> { + setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as ip_mcast_type_v4) + } + + pub fn multicast_loop_v4(&self) -> io::Result<bool> { + let raw: ip_mcast_type_v4 = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?; + Ok(raw != 0) + } + + pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> { + setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as ip_mcast_type_v4) + } + + pub fn multicast_ttl_v4(&self) -> io::Result<u32> { + let raw: ip_mcast_type_v4 = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?; + Ok(raw as u32) + } + pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> { setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int) } @@ -649,52 +630,6 @@ impl UdpSocket { } } -#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd", - target_os = "openbsd", target_os = "netbsd", - target_os = "solaris")))] -impl UdpSocket { - pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> { - setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_int) - } - - pub fn multicast_loop_v4(&self) -> io::Result<bool> { - let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?; - Ok(raw != 0) - } - - pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> { - setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_int) - } - - pub fn multicast_ttl_v4(&self) -> io::Result<u32> { - let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?; - Ok(raw as u32) - } -} - -#[cfg(any(target_os = "dragonfly", target_os = "freebsd", - target_os = "openbsd", target_os = "netbsd", - target_os = "solaris"))] -impl UdpSocket { - pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> { - setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_uchar) - } - - pub fn multicast_loop_v4(&self) -> io::Result<bool> { - let raw: c_uchar = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?; - Ok(raw != 0) - } - - pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> { - setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_uchar) - } - - pub fn multicast_ttl_v4(&self) -> io::Result<u32> { - let raw: c_uchar = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?; - Ok(raw as u32) - } -} - impl FromInner<Socket> for UdpSocket { fn from_inner(socket: Socket) -> UdpSocket { UdpSocket { inner: socket } |
