diff options
author | Carl Lerche <me@carllerche.com> | 2015-04-06 17:40:26 -0700 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2015-04-06 17:40:26 -0700 |
commit | f43ecde470b1c4a71e452f9c5bcf7b76e92c5a06 (patch) | |
tree | 98b4d5c6a66f919f657af16b900e633a6a099649 /src/sys/socket/mod.rs | |
parent | 9e935330dd252d4ec640197a6040df2aaeb583c9 (diff) | |
download | nix-f43ecde470b1c4a71e452f9c5bcf7b76e92c5a06.zip |
Use fcntl:Fd vs. std::os::unix::io::RawFd
Diffstat (limited to 'src/sys/socket/mod.rs')
-rw-r--r-- | src/sys/socket/mod.rs | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index dce80cee..72bc4ccf 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -4,11 +4,10 @@ use {Error, Result, from_ffi}; use errno::Errno; use features; -use fcntl::{fcntl, FD_CLOEXEC, O_NONBLOCK}; +use fcntl::{fcntl, Fd, FD_CLOEXEC, O_NONBLOCK}; use fcntl::FcntlArg::{F_SETFD, F_SETFL}; use libc::{c_void, c_int, socklen_t, size_t}; use std::{fmt, mem, ptr}; -use std::os::unix::prelude::*; mod addr; mod consts; @@ -78,7 +77,7 @@ bitflags!( /// Create an endpoint for communication /// /// [Further reading](http://man7.org/linux/man-pages/man2/socket.2.html) -pub fn socket(domain: AddressFamily, ty: SockType, flags: SockFlag) -> Result<RawFd> { +pub fn socket(domain: AddressFamily, ty: SockType, flags: SockFlag) -> Result<Fd> { let mut ty = ty as c_int; let feat_atomic = features::socket_atomic_cloexec(); @@ -109,7 +108,7 @@ pub fn socket(domain: AddressFamily, ty: SockType, flags: SockFlag) -> Result<Ra /// Listen for connections on a socket /// /// [Further reading](http://man7.org/linux/man-pages/man2/listen.2.html) -pub fn listen(sockfd: RawFd, backlog: usize) -> Result<()> { +pub fn listen(sockfd: Fd, backlog: usize) -> Result<()> { let res = unsafe { ffi::listen(sockfd, backlog as c_int) }; from_ffi(res) } @@ -117,7 +116,7 @@ pub fn listen(sockfd: RawFd, backlog: usize) -> Result<()> { /// Bind a name to a socket /// /// [Further reading](http://man7.org/linux/man-pages/man2/bind.2.html) -pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> { +pub fn bind(fd: Fd, addr: &SockAddr) -> Result<()> { let res = unsafe { let (ptr, len) = addr.as_ffi_pair(); ffi::bind(fd, ptr, len) @@ -129,7 +128,7 @@ pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> { /// Accept a connection on a socket /// /// [Further reading](http://man7.org/linux/man-pages/man2/accept.2.html) -pub fn accept(sockfd: RawFd) -> Result<RawFd> { +pub fn accept(sockfd: Fd) -> Result<Fd> { let res = unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) }; if res < 0 { @@ -142,12 +141,12 @@ pub fn accept(sockfd: RawFd) -> Result<RawFd> { /// Accept a connection on a socket /// /// [Further reading](http://man7.org/linux/man-pages/man2/accept.2.html) -pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> { +pub fn accept4(sockfd: Fd, flags: SockFlag) -> Result<Fd> { accept4_polyfill(sockfd, flags) } #[inline] -fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> { +fn accept4_polyfill(sockfd: Fd, flags: SockFlag) -> Result<Fd> { let res = unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) }; if res < 0 { @@ -168,7 +167,7 @@ fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> { /// Initiate a connection on a socket /// /// [Further reading](http://man7.org/linux/man-pages/man2/connect.2.html) -pub fn connect(fd: RawFd, addr: &SockAddr) -> Result<()> { +pub fn connect(fd: Fd, addr: &SockAddr) -> Result<()> { let res = unsafe { let (ptr, len) = addr.as_ffi_pair(); ffi::connect(fd, ptr, len) @@ -181,7 +180,7 @@ pub fn connect(fd: RawFd, addr: &SockAddr) -> Result<()> { /// the number of bytes read and the socket address of the sender. /// /// [Further reading](http://man7.org/linux/man-pages/man2/recvmsg.2.html) -pub fn recvfrom(sockfd: RawFd, buf: &mut [u8]) -> Result<(usize, SockAddr)> { +pub fn recvfrom(sockfd: Fd, buf: &mut [u8]) -> Result<(usize, SockAddr)> { unsafe { let addr: sockaddr_storage = mem::zeroed(); let mut len = mem::size_of::<sockaddr_storage>() as socklen_t; @@ -203,7 +202,7 @@ pub fn recvfrom(sockfd: RawFd, buf: &mut [u8]) -> Result<(usize, SockAddr)> { } } -pub fn sendto(fd: RawFd, buf: &[u8], addr: &SockAddr, flags: SockMessageFlags) -> Result<usize> { +pub fn sendto(fd: Fd, buf: &[u8], addr: &SockAddr, flags: SockMessageFlags) -> 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, ptr, len) @@ -253,30 +252,30 @@ pub trait SockOpt : Copy + fmt::Debug { type Set; #[doc(hidden)] - fn get(&self, fd: RawFd, level: c_int) -> Result<Self::Get>; + fn get(&self, fd: Fd, level: c_int) -> Result<Self::Get>; #[doc(hidden)] - fn set(&self, fd: RawFd, level: c_int, val: Self::Set) -> Result<()>; + fn set(&self, fd: Fd, level: c_int, val: Self::Set) -> Result<()>; } /// Get the current value for the requested socket option /// /// [Further reading](http://man7.org/linux/man-pages/man2/setsockopt.2.html) -pub fn getsockopt<O: SockOpt>(fd: RawFd, level: SockLevel, opt: O) -> Result<O::Get> { +pub fn getsockopt<O: SockOpt>(fd: Fd, level: SockLevel, opt: O) -> Result<O::Get> { opt.get(fd, level as c_int) } /// Sets the value for the requested socket option /// /// [Further reading](http://man7.org/linux/man-pages/man2/setsockopt.2.html) -pub fn setsockopt<O: SockOpt>(fd: RawFd, level: SockLevel, opt: O, val: O::Set) -> Result<()> { +pub fn setsockopt<O: SockOpt>(fd: Fd, level: SockLevel, opt: O, val: O::Set) -> Result<()> { opt.set(fd, level as c_int, val) } /// Get the address of the peer connected to the socket `fd`. /// /// [Further reading](http://man7.org/linux/man-pages/man2/getpeername.2.html) -pub fn getpeername(fd: RawFd) -> Result<SockAddr> { +pub fn getpeername(fd: Fd) -> Result<SockAddr> { unsafe { let addr: sockaddr_storage = mem::uninitialized(); let mut len = mem::size_of::<sockaddr_storage>() as socklen_t; @@ -294,7 +293,7 @@ pub fn getpeername(fd: RawFd) -> Result<SockAddr> { /// Get the current address to which the socket `fd` is bound. /// /// [Further reading](http://man7.org/linux/man-pages/man2/getsockname.2.html) -pub fn getsockname(fd: RawFd) -> Result<SockAddr> { +pub fn getsockname(fd: Fd) -> Result<SockAddr> { unsafe { let addr: sockaddr_storage = mem::uninitialized(); let mut len = mem::size_of::<sockaddr_storage>() as socklen_t; |