Skip to content

Commit

Permalink
Replace extra SOCK_ constants with libc versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Susurrus committed Aug 10, 2017
1 parent 087aece commit 67214cd
Showing 1 changed file with 81 additions and 24 deletions.
105 changes: 81 additions & 24 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,46 @@ pub enum SockProtocol {
KextControl = libc::SYSPROTO_CONTROL,
}

bitflags!(
/// Extra flags - Supported by Linux 2.6.27, normalized on other platforms
// FIXME: Replace these two separate declarations with one once https://github.com/rust-lang-nursery/bitflags/issues/113
// is resolved.
#[cfg(not(any(target_os = "mac", target_os = "ios")))]
libc_bitflags!{
/// Additional socket options
pub flags SockFlag: c_int {
/// Set non-blocking mode on the new socket
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
SOCK_NONBLOCK,
/// Set close-on-exec on the new descriptor
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
SOCK_CLOEXEC,
/// Return `EPIPE` instead of raising `SIGPIPE`
#[cfg(target_os = "netbsd")]
SOCK_NOSIGPIPE,
/// For domains `AF_INET(6)`, only allow `connect(2)`, `sendto(2)`, or `sendmsg(2)`
/// to the DNS port (typically 53)
#[cfg(target_os = "openbsd")]
SOCK_DNS,
}
}

#[cfg(any(target_os = "mac", target_os = "ios"))]
bitflags!{
/// Additional socket options
pub struct SockFlag: c_int {
const SOCK_NONBLOCK = 0o0004000;
const SOCK_CLOEXEC = 0o2000000;
/// Empty placeholder
const EMPTY = 0;
}
);
}

libc_bitflags!{
/// Flags for send/recv and their relatives
Expand Down Expand Up @@ -449,13 +482,21 @@ pub fn socket<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: SockType
// TODO: Check the kernel version
let res = try!(Errno::result(unsafe { ffi::socket(domain as c_int, ty, protocol) }));

if !feat_atomic {
if flags.contains(SOCK_CLOEXEC) {
try!(fcntl(res, F_SETFD(FD_CLOEXEC)));
}
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
{
if !feat_atomic {
if flags.contains(SOCK_CLOEXEC) {
try!(fcntl(res, F_SETFD(FD_CLOEXEC)));
}

if flags.contains(SOCK_NONBLOCK) {
try!(fcntl(res, F_SETFL(O_NONBLOCK)));
if flags.contains(SOCK_NONBLOCK) {
try!(fcntl(res, F_SETFL(O_NONBLOCK)));
}
}
}

Expand Down Expand Up @@ -483,15 +524,23 @@ pub fn socketpair<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: Sock
};
try!(Errno::result(res));

if !feat_atomic {
if flags.contains(SOCK_CLOEXEC) {
try!(fcntl(fds[0], F_SETFD(FD_CLOEXEC)));
try!(fcntl(fds[1], F_SETFD(FD_CLOEXEC)));
}
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
{
if !feat_atomic {
if flags.contains(SOCK_CLOEXEC) {
try!(fcntl(fds[0], F_SETFD(FD_CLOEXEC)));
try!(fcntl(fds[1], F_SETFD(FD_CLOEXEC)));
}

if flags.contains(SOCK_NONBLOCK) {
try!(fcntl(fds[0], F_SETFL(O_NONBLOCK)));
try!(fcntl(fds[1], F_SETFL(O_NONBLOCK)));
if flags.contains(SOCK_NONBLOCK) {
try!(fcntl(fds[0], F_SETFL(O_NONBLOCK)));
try!(fcntl(fds[1], F_SETFL(O_NONBLOCK)));
}
}
}
Ok((fds[0], fds[1]))
Expand Down Expand Up @@ -554,12 +603,20 @@ pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
let res = try!(Errno::result(unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) }));

if flags.contains(SOCK_CLOEXEC) {
try!(fcntl(res, F_SETFD(FD_CLOEXEC)));
}
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
{
if flags.contains(SOCK_CLOEXEC) {
try!(fcntl(res, F_SETFD(FD_CLOEXEC)));
}

if flags.contains(SOCK_NONBLOCK) {
try!(fcntl(res, F_SETFL(O_NONBLOCK)));
if flags.contains(SOCK_NONBLOCK) {
try!(fcntl(res, F_SETFL(O_NONBLOCK)));
}
}

Ok(res)
Expand Down

0 comments on commit 67214cd

Please sign in to comment.