Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace socket FFI with libc versions #746

Merged
merged 2 commits into from
Sep 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ 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) }));
let res = try!(Errno::result(unsafe { libc::socket(domain as c_int, ty, protocol) }));

#[cfg(any(target_os = "android",
target_os = "dragonfly",
Expand Down Expand Up @@ -509,7 +509,7 @@ pub fn socketpair<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: Sock
}
let mut fds = [-1, -1];
let res = unsafe {
ffi::socketpair(domain as c_int, ty, protocol, fds.as_mut_ptr())
libc::socketpair(domain as c_int, ty, protocol, fds.as_mut_ptr())
};
try!(Errno::result(res));

Expand Down Expand Up @@ -542,7 +542,7 @@ pub fn socketpair<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: Sock
///
/// [Further reading](http://man7.org/linux/man-pages/man2/listen.2.html)
pub fn listen(sockfd: RawFd, backlog: usize) -> Result<()> {
let res = unsafe { ffi::listen(sockfd, backlog as c_int) };
let res = unsafe { libc::listen(sockfd, backlog as c_int) };

Errno::result(res).map(drop)
}
Expand All @@ -554,7 +554,7 @@ pub fn listen(sockfd: RawFd, backlog: usize) -> Result<()> {
pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
let res = unsafe {
let (ptr, len) = addr.as_ffi_pair();
ffi::bind(fd, ptr, len)
libc::bind(fd, ptr, len)
};

Errno::result(res).map(drop)
Expand All @@ -569,7 +569,7 @@ pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
let res = unsafe {
let (ptr, len) = addr.as_ffi_pair();
ffi::bind(fd, ptr, len as c_int)
libc::bind(fd, ptr, len as c_int)
};

Errno::result(res).map(drop)
Expand All @@ -579,7 +579,7 @@ pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
///
/// [Further reading](http://man7.org/linux/man-pages/man2/accept.2.html)
pub fn accept(sockfd: RawFd) -> Result<RawFd> {
let res = unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) };
let res = unsafe { libc::accept(sockfd, ptr::null_mut(), ptr::null_mut()) };

Errno::result(res)
}
Expand All @@ -593,7 +593,7 @@ pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {

#[inline]
fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
let res = try!(Errno::result(unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) }));
let res = try!(Errno::result(unsafe { libc::accept(sockfd, ptr::null_mut(), ptr::null_mut()) }));

#[cfg(any(target_os = "android",
target_os = "dragonfly",
Expand Down Expand Up @@ -635,7 +635,7 @@ fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
pub fn connect(fd: RawFd, addr: &SockAddr) -> Result<()> {
let res = unsafe {
let (ptr, len) = addr.as_ffi_pair();
ffi::connect(fd, ptr, len)
libc::connect(fd, ptr, len)
};

Errno::result(res).map(drop)
Expand Down Expand Up @@ -682,7 +682,7 @@ pub fn recvfrom(sockfd: RawFd, buf: &mut [u8]) -> Result<(usize, SockAddr)> {
pub fn sendto(fd: RawFd, buf: &[u8], addr: &SockAddr, flags: MsgFlags) -> Result<usize> {
let ret = unsafe {
let (ptr, len) = addr.as_ffi_pair();
ffi::sendto(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, flags.bits(), ptr, len)
libc::sendto(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, flags.bits(), ptr, len)
};

Errno::result(ret).map(|r| r as usize)
Expand All @@ -693,7 +693,7 @@ pub fn sendto(fd: RawFd, buf: &[u8], addr: &SockAddr, flags: MsgFlags) -> Result
/// [Further reading](http://man7.org/linux/man-pages/man2/send.2.html)
pub fn send(fd: RawFd, buf: &[u8], flags: MsgFlags) -> Result<usize> {
let ret = unsafe {
ffi::send(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, flags.bits())
libc::send(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, flags.bits())
};

Errno::result(ret).map(|r| r as usize)
Expand Down Expand Up @@ -775,7 +775,7 @@ pub fn getpeername(fd: RawFd) -> Result<SockAddr> {
let addr: sockaddr_storage = mem::uninitialized();
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;

let ret = ffi::getpeername(fd, mem::transmute(&addr), &mut len);
let ret = libc::getpeername(fd, mem::transmute(&addr), &mut len);

try!(Errno::result(ret));

Expand All @@ -791,7 +791,7 @@ pub fn getsockname(fd: RawFd) -> Result<SockAddr> {
let addr: sockaddr_storage = mem::uninitialized();
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;

let ret = ffi::getsockname(fd, mem::transmute(&addr), &mut len);
let ret = libc::getsockname(fd, mem::transmute(&addr), &mut len);

try!(Errno::result(ret));

Expand Down
14 changes: 7 additions & 7 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{ffi, GetSockOpt, SetSockOpt};
use super::{GetSockOpt, SetSockOpt};
use {Errno, Result};
use sys::time::TimeVal;
use libc::{self, c_int, uint8_t, c_void, socklen_t};
Expand All @@ -14,9 +14,9 @@ macro_rules! setsockopt_impl {
unsafe {
let setter: $setter = Set::new(val);

let res = ffi::setsockopt(fd, $level, $flag,
setter.ffi_ptr(),
setter.ffi_len());
let res = libc::setsockopt(fd, $level, $flag,
setter.ffi_ptr(),
setter.ffi_len());
Errno::result(res).map(drop)
}
}
Expand All @@ -33,9 +33,9 @@ macro_rules! getsockopt_impl {
unsafe {
let mut getter: $getter = Get::blank();

let res = ffi::getsockopt(fd, $level, $flag,
getter.ffi_ptr(),
getter.ffi_len());
let res = libc::getsockopt(fd, $level, $flag,
getter.ffi_ptr(),
getter.ffi_len());
try!(Errno::result(res));

Ok(getter.unwrap())
Expand Down
2 changes: 1 addition & 1 deletion test/sys/test_socket.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use nix::sys::socket::{InetAddr, UnixAddr, getsockname};
use std::{mem, slice};
use std::slice;
use std::net::{self, Ipv6Addr, SocketAddr, SocketAddrV6};
use std::path::Path;
use std::str::FromStr;
Expand Down