Skip to content

Commit a9022b5

Browse files
author
Bryant Mairs
committed
Replace ip(v6)_mreq structs with libc equivalent
1 parent 310d205 commit a9022b5

File tree

3 files changed

+89
-64
lines changed

3 files changed

+89
-64
lines changed

src/sys/socket/mod.rs

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ use {Error, Result};
55
use errno::Errno;
66
use features;
77
use libc::{self, c_void, c_int, socklen_t, size_t};
8-
use std::{mem, ptr, slice};
8+
use std::{fmt, mem, ptr, slice};
99
use std::os::unix::io::RawFd;
1010
use sys::time::TimeVal;
1111
use sys::uio::IoVec;
1212

1313
mod addr;
1414
mod ffi;
15-
mod multicast;
1615
pub mod sockopt;
1716

1817
/*
@@ -34,22 +33,14 @@ pub use self::addr::{
3433
pub use ::sys::socket::addr::netlink::NetlinkAddr;
3534

3635
pub use libc::{
37-
in_addr,
38-
in6_addr,
36+
sa_family_t,
3937
sockaddr,
4038
sockaddr_in,
4139
sockaddr_in6,
40+
sockaddr_storage,
4241
sockaddr_un,
43-
sa_family_t,
44-
};
45-
46-
pub use self::multicast::{
47-
ip_mreq,
48-
ipv6_mreq,
4942
};
5043

51-
pub use libc::sockaddr_storage;
52-
5344
/// These constants are used to specify the communication semantics
5445
/// when creating a socket with [`socket()`](fn.socket.html)
5546
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -177,8 +168,6 @@ libc_bitflags!{
177168

178169
cfg_if! {
179170
if #[cfg(all(target_os = "linux", not(target_arch = "arm")))] {
180-
use std::fmt;
181-
182171
/// Unix credentials of the sending process.
183172
///
184173
/// This struct is used with the `SO_PEERCRED` ancillary message for UNIX sockets.
@@ -222,6 +211,76 @@ cfg_if! {
222211
}
223212
}
224213

214+
/// Request for multicast socket operations
215+
///
216+
/// This is a wrapper type around `ip_mreq`.
217+
#[repr(C)]
218+
#[derive(Clone, Copy)]
219+
pub struct IpMembershipRequest(libc::ip_mreq);
220+
221+
impl IpMembershipRequest {
222+
/// Instantiate a new `IpMembershipRequest`
223+
///
224+
/// If `interface` is `None`, then `Ipv4Addr::any()` will be used for the interface.
225+
pub fn new(group: Ipv4Addr, interface: Option<Ipv4Addr>) -> Self {
226+
IpMembershipRequest(libc::ip_mreq {
227+
imr_multiaddr: group.0,
228+
imr_interface: interface.unwrap_or(Ipv4Addr::any()).0,
229+
})
230+
}
231+
}
232+
233+
impl PartialEq for IpMembershipRequest {
234+
fn eq(&self, other: &Self) -> bool {
235+
self.0.imr_multiaddr.s_addr == other.0.imr_multiaddr.s_addr
236+
&& self.0.imr_interface.s_addr == other.0.imr_interface.s_addr
237+
}
238+
}
239+
impl Eq for IpMembershipRequest {}
240+
241+
impl fmt::Debug for IpMembershipRequest {
242+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
243+
f.debug_struct("IpMembershipRequest")
244+
.field("imr_multiaddr", &self.0.imr_multiaddr.s_addr)
245+
.field("imr_interface", &self.0.imr_interface.s_addr)
246+
.finish()
247+
}
248+
}
249+
250+
/// Request for ipv6 multicast socket operations
251+
///
252+
/// This is a wrapper type around `ipv6_mreq`.
253+
#[repr(C)]
254+
#[derive(Clone, Copy)]
255+
pub struct Ipv6MembershipRequest(libc::ipv6_mreq);
256+
257+
impl Ipv6MembershipRequest {
258+
/// Instantiate a new `Ipv6MembershipRequest`
259+
pub fn new(group: Ipv6Addr) -> Self {
260+
Ipv6MembershipRequest(libc::ipv6_mreq {
261+
ipv6mr_multiaddr: group.0,
262+
ipv6mr_interface: 0,
263+
})
264+
}
265+
}
266+
267+
impl PartialEq for Ipv6MembershipRequest {
268+
fn eq(&self, other: &Self) -> bool {
269+
self.0.ipv6mr_multiaddr.s6_addr == other.0.ipv6mr_multiaddr.s6_addr &&
270+
self.0.ipv6mr_interface == other.0.ipv6mr_interface
271+
}
272+
}
273+
impl Eq for Ipv6MembershipRequest {}
274+
275+
impl fmt::Debug for Ipv6MembershipRequest {
276+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
277+
f.debug_struct("Ipv6MembershipRequest")
278+
.field("ipv6mr_multiaddr", &self.0.ipv6mr_multiaddr.s6_addr)
279+
.field("ipv6mr_interface", &self.0.ipv6mr_interface)
280+
.finish()
281+
}
282+
}
283+
225284
/// Copy the in-memory representation of src into the byte slice dst,
226285
/// updating the slice to point to the remainder of dst only. Unsafe
227286
/// because it exposes all bytes in src, which may be UB if some of them

src/sys/socket/multicast.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/sys/socket/sockopt.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,22 @@ sockopt_impl!(Both, ReuseAddr, libc::SOL_SOCKET, libc::SO_REUSEADDR, bool);
133133
sockopt_impl!(Both, ReusePort, libc::SOL_SOCKET, libc::SO_REUSEPORT, bool);
134134
sockopt_impl!(Both, TcpNoDelay, libc::IPPROTO_TCP, libc::TCP_NODELAY, bool);
135135
sockopt_impl!(Both, Linger, libc::SOL_SOCKET, libc::SO_LINGER, libc::linger);
136-
sockopt_impl!(SetOnly, IpAddMembership, libc::IPPROTO_IP, libc::IP_ADD_MEMBERSHIP, super::ip_mreq);
137-
sockopt_impl!(SetOnly, IpDropMembership, libc::IPPROTO_IP, libc::IP_DROP_MEMBERSHIP, super::ip_mreq);
138-
#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd")))]
139-
sockopt_impl!(SetOnly, Ipv6AddMembership, libc::IPPROTO_IPV6, libc::IPV6_ADD_MEMBERSHIP, super::ipv6_mreq);
140-
#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd")))]
141-
sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_DROP_MEMBERSHIP, super::ipv6_mreq);
142-
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))]
143-
sockopt_impl!(SetOnly, Ipv6AddMembership, libc::IPPROTO_IPV6, libc::IPV6_JOIN_GROUP, super::ipv6_mreq);
144-
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))]
145-
sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_LEAVE_GROUP, super::ipv6_mreq);
136+
sockopt_impl!(SetOnly, IpAddMembership, libc::IPPROTO_IP, libc::IP_ADD_MEMBERSHIP, super::IpMembershipRequest);
137+
sockopt_impl!(SetOnly, IpDropMembership, libc::IPPROTO_IP, libc::IP_DROP_MEMBERSHIP, super::IpMembershipRequest);
138+
cfg_if! {
139+
if #[cfg(any(target_os = "android", target_os = "linux"))] {
140+
sockopt_impl!(SetOnly, Ipv6AddMembership, libc::IPPROTO_IPV6, libc::IPV6_ADD_MEMBERSHIP, super::Ipv6MembershipRequest);
141+
sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_DROP_MEMBERSHIP, super::Ipv6MembershipRequest);
142+
} else if #[cfg(any(target_os = "dragonfly",
143+
target_os = "freebsd",
144+
target_os = "ios",
145+
target_os = "macos",
146+
target_os = "netbsd",
147+
target_os = "openbsd"))] {
148+
sockopt_impl!(SetOnly, Ipv6AddMembership, libc::IPPROTO_IPV6, libc::IPV6_JOIN_GROUP, super::Ipv6MembershipRequest);
149+
sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_LEAVE_GROUP, super::Ipv6MembershipRequest);
150+
}
151+
}
146152
sockopt_impl!(Both, IpMulticastTtl, libc::IPPROTO_IP, libc::IP_MULTICAST_TTL, u8);
147153
sockopt_impl!(Both, IpMulticastLoop, libc::IPPROTO_IP, libc::IP_MULTICAST_LOOP, bool);
148154
sockopt_impl!(Both, ReceiveTimeout, libc::SOL_SOCKET, libc::SO_RCVTIMEO, TimeVal);

0 commit comments

Comments
 (0)