]> git.lizzy.rs Git - rust.git/commitdiff
Options IP_MULTICAST_TTL and IP_MULTICAST_LOOP are 1 byte on BSD and Solaris
authorVita Batrla <vita.batrla@gmail.com>
Fri, 17 Jan 2020 13:43:36 +0000 (14:43 +0100)
committerVita Batrla <vita.batrla@gmail.com>
Fri, 17 Jan 2020 15:01:42 +0000 (16:01 +0100)
See ip(4P) man page:
 IP_MULTICAST_TTL     Time  to live for multicast datagrams. This option
                      takes an unsigned character as  an  argument.  Its
                      value  is  the TTL that IP uses on outgoing multi-
                      cast datagrams. The default is 1.

 IP_MULTICAST_LOOP    Loopback for multicast datagrams. Normally  multi-
                      cast  datagrams  are  delivered  to members on the
                      sending  host  (or  sending  zone).  Setting   the
                      unsigned  character argument to 0 causes the oppo-
                      site behavior, meaning that  when  multiple  zones
                      are  present,  the  datagrams are delivered to all
                      zones except the sending zone.

https://docs.oracle.com/cd/E88353_01/html/E37851/ip-4p.html
https://man.openbsd.org/ip.4

src/libstd/sys_common/net.rs

index c7d4828892c046066de3c27e09d7c76fad321dc3..32e392f1efc86024ed56e9df9ce8b4939dae6dfb 100644 (file)
 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",
@@ -565,24 +569,6 @@ pub fn broadcast(&self) -> io::Result<bool> {
         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 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)
-    }
-
     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)
     }
@@ -663,6 +649,52 @@ pub fn connect(&self, addr: io::Result<&SocketAddr>) -> io::Result<()> {
     }
 }
 
+#[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 }