Skip to content

Commit fcc952a

Browse files
committed
in_addr conversions + IP_DROP_MEMBERSHIP support
1 parent 979faf0 commit fcc952a

File tree

4 files changed

+93
-12
lines changed

4 files changed

+93
-12
lines changed

src/nix.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use libc;
22
use std;
33

4-
use errno::Errno;
4+
use errno::{Errno, EINVAL};
55

66
pub type NixResult<T> = Result<T, NixError>;
77

@@ -11,6 +11,12 @@ pub enum NixError {
1111
InvalidPath
1212
}
1313

14+
impl NixError {
15+
pub fn invalid_argument() -> NixError {
16+
NixError::Sys(EINVAL)
17+
}
18+
}
19+
1420
pub trait NixPath {
1521
fn with_nix_path<T, F>(&self, f: F) -> Result<T, NixError>
1622
where F: FnOnce(*const libc::c_char) -> T;

src/sys/socket/addr.rs

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
use {NixResult, NixError};
22
use super::{sa_family_t, in_addr, sockaddr_in, sockaddr_in6, sockaddr_un, AF_UNIX, AF_INET};
33
use errno::Errno;
4+
use libc;
45
use std::{mem, net, path, ptr};
56
use std::ffi::{AsOsStr, CStr, OsStr};
67
use std::os::unix::OsStrExt;
78

9+
/*
10+
*
11+
* ===== Sock addr =====
12+
*
13+
*/
14+
815
/// Represents a socket address
916
#[derive(Copy)]
1017
pub enum SockAddr {
@@ -66,21 +73,16 @@ impl ToSockAddr for path::Path {
6673
impl ToSockAddr for net::SocketAddr {
6774
fn to_sock_addr(&self) -> NixResult<SockAddr> {
6875
use std::net::IpAddr;
69-
use std::num::Int;
7076

7177
match self.ip() {
7278
IpAddr::V4(ip) => {
73-
let addr = ip.octets();
79+
let addr = ip.to_in_addr()
80+
.expect("in_addr conversion expected to be successful");
81+
7482
Ok(SockAddr::IpV4(sockaddr_in {
7583
sin_family: AF_INET as sa_family_t,
7684
sin_port: self.port(),
77-
sin_addr: in_addr {
78-
s_addr: Int::from_be(
79-
((addr[0] as u32) << 24) |
80-
((addr[1] as u32) << 16) |
81-
((addr[2] as u32) << 8) |
82-
((addr[3] as u32) << 0))
83-
},
85+
sin_addr: addr,
8486
.. unsafe { mem::zeroed() }
8587
}))
8688
}
@@ -129,3 +131,70 @@ impl FromSockAddr for path::PathBuf {
129131
None
130132
}
131133
}
134+
135+
/*
136+
*
137+
* ===== InAddr =====
138+
*
139+
*/
140+
141+
pub trait ToInAddr {
142+
fn to_in_addr(self) -> Option<libc::in_addr>;
143+
}
144+
145+
impl ToInAddr for SockAddr {
146+
fn to_in_addr(self) -> Option<libc::in_addr> {
147+
match self {
148+
SockAddr::IpV4(sock) => Some(sock.sin_addr),
149+
_ => None,
150+
}
151+
}
152+
}
153+
154+
impl<'a> ToInAddr for &'a SockAddr {
155+
fn to_in_addr(self) -> Option<libc::in_addr> {
156+
match *self {
157+
SockAddr::IpV4(ref sock) => Some(sock.sin_addr),
158+
_ => None,
159+
}
160+
}
161+
}
162+
163+
impl ToInAddr for net::IpAddr {
164+
fn to_in_addr(self) -> Option<libc::in_addr> {
165+
match self {
166+
net::IpAddr::V4(addr) => addr.to_in_addr(),
167+
_ => None,
168+
}
169+
}
170+
}
171+
172+
impl<'a> ToInAddr for &'a net::IpAddr {
173+
fn to_in_addr(self) -> Option<libc::in_addr> {
174+
match *self {
175+
net::IpAddr::V4(addr) => addr.to_in_addr(),
176+
_ => None,
177+
}
178+
}
179+
}
180+
181+
impl ToInAddr for net::Ipv4Addr {
182+
fn to_in_addr(self) -> Option<libc::in_addr> {
183+
use std::num::Int;
184+
185+
let addr = self.octets();
186+
Some(in_addr {
187+
s_addr: Int::from_be(
188+
((addr[0] as u32) << 24) |
189+
((addr[1] as u32) << 16) |
190+
((addr[2] as u32) << 8) |
191+
((addr[3] as u32) << 0))
192+
})
193+
}
194+
}
195+
196+
impl<'a> ToInAddr for &'a net::Ipv4Addr {
197+
fn to_in_addr(self) -> Option<libc::in_addr> {
198+
(*self).to_in_addr()
199+
}
200+
}

src/sys/socket/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::os::unix::prelude::*;
1010
mod addr;
1111
mod consts;
1212
mod ffi;
13+
mod multicast;
1314
pub mod sockopt;
1415

1516
/*
@@ -21,7 +22,8 @@ pub mod sockopt;
2122
pub use self::addr::{
2223
SockAddr,
2324
ToSockAddr,
24-
FromSockAddr
25+
FromSockAddr,
26+
ToInAddr,
2527
};
2628
pub use libc::{
2729
in_addr,
@@ -31,7 +33,10 @@ pub use libc::{
3133
sockaddr_in6,
3234
sockaddr_un,
3335
sa_family_t,
34-
ip_mreq
36+
};
37+
38+
pub use self::multicast::{
39+
ip_mreq,
3540
};
3641
pub use self::consts::*;
3742

src/sys/socket/sockopt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ sockopt_impl!(ReusePort, consts::SO_REUSEPORT, bool);
7272
sockopt_impl!(TcpNoDelay, consts::TCP_NODELAY, bool);
7373
sockopt_impl!(Linger, consts::SO_LINGER, super::linger);
7474
sockopt_impl!(IpAddMembership, consts::IP_ADD_MEMBERSHIP, super::ip_mreq);
75+
sockopt_impl!(IpDropMembership, consts::IP_DROP_MEMBERSHIP, super::ip_mreq);
7576
sockopt_impl!(IpMulticastTtl, consts::IP_MULTICAST_TTL, u8);
7677

7778
/*

0 commit comments

Comments
 (0)