Skip to content

Commit 34878d7

Browse files
author
Vita Batrla
committed
Options IP_MULTICAST_TTL and IP_MULTICAST_LOOP are 1 byte on BSD and Solaris
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
1 parent 2480c9e commit 34878d7

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

src/libstd/sys_common/net.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
1212
use crate::time::Duration;
1313

1414
use libc::{c_int, c_void};
15+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",
16+
target_os = "openbsd", target_os = "netbsd",
17+
target_os = "solaris"))]
18+
use libc::{c_uchar};
1519

1620
#[cfg(not(any(
1721
target_os = "dragonfly",
@@ -565,24 +569,6 @@ impl UdpSocket {
565569
Ok(raw != 0)
566570
}
567571

568-
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
569-
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_int)
570-
}
571-
572-
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
573-
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
574-
Ok(raw != 0)
575-
}
576-
577-
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
578-
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_int)
579-
}
580-
581-
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
582-
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
583-
Ok(raw as u32)
584-
}
585-
586572
pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
587573
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int)
588574
}
@@ -663,6 +649,52 @@ impl UdpSocket {
663649
}
664650
}
665651

652+
#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd",
653+
target_os = "openbsd", target_os = "netbsd",
654+
target_os = "solaris")))]
655+
impl UdpSocket {
656+
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
657+
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_int)
658+
}
659+
660+
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
661+
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
662+
Ok(raw != 0)
663+
}
664+
665+
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
666+
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_int)
667+
}
668+
669+
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
670+
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
671+
Ok(raw as u32)
672+
}
673+
}
674+
675+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",
676+
target_os = "openbsd", target_os = "netbsd",
677+
target_os = "solaris"))]
678+
impl UdpSocket {
679+
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
680+
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_uchar)
681+
}
682+
683+
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
684+
let raw: c_uchar = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
685+
Ok(raw != 0)
686+
}
687+
688+
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
689+
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_uchar)
690+
}
691+
692+
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
693+
let raw: c_uchar = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
694+
Ok(raw as u32)
695+
}
696+
}
697+
666698
impl FromInner<Socket> for UdpSocket {
667699
fn from_inner(socket: Socket) -> UdpSocket {
668700
UdpSocket { inner: socket }

0 commit comments

Comments
 (0)