Skip to content

Commit

Permalink
Rollup merge of rust-lang#68313 - batrla:master, r=alexcrichton
Browse files Browse the repository at this point in the history
Options IP_MULTICAST_TTL and IP_MULTICAST_LOOP are 1 byte on BSD

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
  • Loading branch information
Centril authored Jan 21, 2020
2 parents 3484e2f + 5392442 commit dabd816
Showing 1 changed file with 51 additions and 73 deletions.
124 changes: 51 additions & 73 deletions src/libstd/sys_common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,75 +13,43 @@ use crate::time::Duration;

use libc::{c_int, c_void};

#[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"))] {
use libc::c_uchar;
type IpV4MultiCastType = c_uchar;
} else {
type IpV4MultiCastType = c_int;
}
}

////////////////////////////////////////////////////////////////////////////////
// sockaddr and misc bindings
Expand Down Expand Up @@ -566,20 +534,30 @@ 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)
setsockopt(
&self.inner,
c::IPPROTO_IP,
c::IP_MULTICAST_LOOP,
multicast_loop_v4 as IpV4MultiCastType,
)
}

pub fn multicast_loop_v4(&self) -> io::Result<bool> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
let raw: IpV4MultiCastType = 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)
setsockopt(
&self.inner,
c::IPPROTO_IP,
c::IP_MULTICAST_TTL,
multicast_ttl_v4 as IpV4MultiCastType,
)
}

pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
let raw: IpV4MultiCastType = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
Ok(raw as u32)
}

Expand Down

0 comments on commit dabd816

Please sign in to comment.