mirror of
https://gitee.com/openharmony/third_party_rust_nix
synced 2025-03-02 13:35:54 +00:00
Merge #1684
1684: Replace the Sockaddr enum with a union r=rtzoeller a=asomers The SockAddr enum is quite large, and the user must allocate space for the whole thing even though he usually knows what type he needs. Furthermore, thanks to the sa_family field, the sockaddr types are basically an enum even in C. So replace the ungainly enum with a SockaddrLike trait implemented by all sockaddr types and a SockaddrStorage union that has safe accessors. Also, deprecate InetAddr, which only existed to support SockAddr. Supplants #1504 Fixes #1544 Co-authored-by: Alan Somers <asomers@gmail.com>
This commit is contained in:
commit
0fe668265f
@ -74,6 +74,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
|
||||
- Changed `getrlimit` and `setrlimit` to use `rlim_t` directly
|
||||
instead of `Option<rlim_t>`.
|
||||
(#[1668](https://github.com/nix-rust/nix/pull/1668))
|
||||
- Deprecated `InetAddr` and `SockAddr` in favor of `SockaddrIn`, `SockaddrIn6`,
|
||||
and `SockaddrStorage`.
|
||||
(#[1684](https://github.com/nix-rust/nix/pull/1684))
|
||||
|
||||
### Fixed
|
||||
|
||||
|
@ -10,7 +10,7 @@ use std::mem;
|
||||
use std::option::Option;
|
||||
|
||||
use crate::{Result, Errno};
|
||||
use crate::sys::socket::SockAddr;
|
||||
use crate::sys::socket::{SockaddrLike, SockaddrStorage};
|
||||
use crate::net::if_::*;
|
||||
|
||||
/// Describes a single address for an interface as returned by `getifaddrs`.
|
||||
@ -21,13 +21,13 @@ pub struct InterfaceAddress {
|
||||
/// Flags as from `SIOCGIFFLAGS` ioctl
|
||||
pub flags: InterfaceFlags,
|
||||
/// Network address of this interface
|
||||
pub address: Option<SockAddr>,
|
||||
pub address: Option<SockaddrStorage>,
|
||||
/// Netmask of this interface
|
||||
pub netmask: Option<SockAddr>,
|
||||
pub netmask: Option<SockaddrStorage>,
|
||||
/// Broadcast address of this interface, if applicable
|
||||
pub broadcast: Option<SockAddr>,
|
||||
pub broadcast: Option<SockaddrStorage>,
|
||||
/// Point-to-point destination address
|
||||
pub destination: Option<SockAddr>,
|
||||
pub destination: Option<SockaddrStorage>,
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
@ -46,8 +46,8 @@ impl InterfaceAddress {
|
||||
/// Create an `InterfaceAddress` from the libc struct.
|
||||
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
|
||||
let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
|
||||
let address = unsafe { SockAddr::from_libc_sockaddr(info.ifa_addr) };
|
||||
let netmask = unsafe { SockAddr::from_libc_sockaddr(info.ifa_netmask) };
|
||||
let address = unsafe { SockaddrStorage::from_raw(info.ifa_addr, None) };
|
||||
let netmask = unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
|
||||
let mut addr = InterfaceAddress {
|
||||
interface_name: ifname.to_string_lossy().to_string(),
|
||||
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
|
||||
@ -59,9 +59,9 @@ impl InterfaceAddress {
|
||||
|
||||
let ifu = get_ifu_from_sockaddr(info);
|
||||
if addr.flags.contains(InterfaceFlags::IFF_POINTOPOINT) {
|
||||
addr.destination = unsafe { SockAddr::from_libc_sockaddr(ifu) };
|
||||
addr.destination = unsafe { SockaddrStorage::from_raw(ifu, None) };
|
||||
} else if addr.flags.contains(InterfaceFlags::IFF_BROADCAST) {
|
||||
addr.broadcast = unsafe { SockAddr::from_libc_sockaddr(ifu) };
|
||||
addr.broadcast = unsafe { SockaddrStorage::from_raw(ifu, None) };
|
||||
}
|
||||
|
||||
addr
|
||||
@ -103,9 +103,9 @@ impl Iterator for InterfaceAddressIterator {
|
||||
/// Note that the underlying implementation differs between OSes. Only the
|
||||
/// most common address families are supported by the nix crate (due to
|
||||
/// lack of time and complexity of testing). The address family is encoded
|
||||
/// in the specific variant of `SockAddr` returned for the fields `address`,
|
||||
/// `netmask`, `broadcast`, and `destination`. For any entry not supported,
|
||||
/// the returned list will contain a `None` entry.
|
||||
/// in the specific variant of `SockaddrStorage` returned for the fields
|
||||
/// `address`, `netmask`, `broadcast`, and `destination`. For any entry not
|
||||
/// supported, the returned list will contain a `None` entry.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
|
@ -31,6 +31,7 @@
|
||||
//! * `reboot` - Reboot the system
|
||||
//! * `resource` - Process resource limits
|
||||
//! * `sched` - Manipulate process's scheduling
|
||||
//! * `socket` - Sockets, whether for networking or local use
|
||||
//! * `signal` - Send and receive signals to processes
|
||||
//! * `term` - Terminal control APIs
|
||||
//! * `time` - Query the operating system's clocks
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -17,6 +17,7 @@ use crate::sys::{
|
||||
uio::IoVec
|
||||
};
|
||||
|
||||
#[deny(missing_docs)]
|
||||
mod addr;
|
||||
#[deny(missing_docs)]
|
||||
pub mod sockopt;
|
||||
@ -27,12 +28,16 @@ pub mod sockopt;
|
||||
*
|
||||
*/
|
||||
|
||||
pub use self::addr::{SockaddrLike, SockaddrStorage};
|
||||
|
||||
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
|
||||
#[allow(deprecated)]
|
||||
pub use self::addr::{
|
||||
AddressFamily,
|
||||
SockAddr,
|
||||
UnixAddr,
|
||||
};
|
||||
#[allow(deprecated)]
|
||||
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
|
||||
#[cfg(feature = "net")]
|
||||
pub use self::addr::{
|
||||
@ -40,14 +45,18 @@ pub use self::addr::{
|
||||
IpAddr,
|
||||
Ipv4Addr,
|
||||
Ipv6Addr,
|
||||
LinkAddr
|
||||
LinkAddr,
|
||||
SockaddrIn,
|
||||
SockaddrIn6
|
||||
};
|
||||
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
|
||||
#[allow(deprecated)]
|
||||
pub use self::addr::{
|
||||
AddressFamily,
|
||||
SockAddr,
|
||||
UnixAddr,
|
||||
};
|
||||
#[allow(deprecated)]
|
||||
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
|
||||
#[cfg(feature = "net")]
|
||||
pub use self::addr::{
|
||||
@ -55,8 +64,13 @@ pub use self::addr::{
|
||||
IpAddr,
|
||||
Ipv4Addr,
|
||||
Ipv6Addr,
|
||||
SockaddrIn,
|
||||
SockaddrIn6
|
||||
};
|
||||
|
||||
#[cfg(any(target_os = "ios", target_os = "macos"))]
|
||||
#[cfg(feature = "ioctl")]
|
||||
pub use crate::sys::socket::addr::sys_control::SysControlAddr;
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
pub use crate::sys::socket::addr::netlink::NetlinkAddr;
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
@ -550,15 +564,15 @@ macro_rules! cmsg_space {
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct RecvMsg<'a> {
|
||||
pub struct RecvMsg<'a, S> {
|
||||
pub bytes: usize,
|
||||
cmsghdr: Option<&'a cmsghdr>,
|
||||
pub address: Option<SockAddr>,
|
||||
pub address: Option<S>,
|
||||
pub flags: MsgFlags,
|
||||
mhdr: msghdr,
|
||||
}
|
||||
|
||||
impl<'a> RecvMsg<'a> {
|
||||
impl<'a, S> RecvMsg<'a, S> {
|
||||
/// Iterate over the valid control messages pointed to by this
|
||||
/// msghdr.
|
||||
pub fn cmsgs(&self) -> CmsgIterator {
|
||||
@ -635,6 +649,7 @@ pub enum ControlMessageOwned {
|
||||
/// # use nix::sys::uio::IoVec;
|
||||
/// # use nix::sys::time::*;
|
||||
/// # use std::time::*;
|
||||
/// # use std::str::FromStr;
|
||||
/// # fn main() {
|
||||
/// // Set up
|
||||
/// let message = "Ohayō!".as_bytes();
|
||||
@ -644,9 +659,9 @@ pub enum ControlMessageOwned {
|
||||
/// SockFlag::empty(),
|
||||
/// None).unwrap();
|
||||
/// setsockopt(in_socket, sockopt::ReceiveTimestamp, &true).unwrap();
|
||||
/// let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
|
||||
/// bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
|
||||
/// let address = getsockname(in_socket).unwrap();
|
||||
/// let localhost = SockaddrIn::from_str("127.0.0.1:0").unwrap();
|
||||
/// bind(in_socket, &localhost);
|
||||
/// let address: SockaddrIn = getsockname(in_socket).unwrap();
|
||||
/// // Get initial time
|
||||
/// let time0 = SystemTime::now();
|
||||
/// // Send the message
|
||||
@ -658,7 +673,8 @@ pub enum ControlMessageOwned {
|
||||
/// let mut buffer = vec![0u8; message.len()];
|
||||
/// let mut cmsgspace = cmsg_space!(TimeVal);
|
||||
/// let iov = [IoVec::from_mut_slice(&mut buffer)];
|
||||
/// let r = recvmsg(in_socket, &iov, Some(&mut cmsgspace), flags).unwrap();
|
||||
/// let r = recvmsg::<SockaddrIn>(in_socket, &iov, Some(&mut cmsgspace), flags)
|
||||
/// .unwrap();
|
||||
/// let rtime = match r.cmsgs().next() {
|
||||
/// Some(ControlMessageOwned::ScmTimestamp(rtime)) => rtime,
|
||||
/// Some(_) => panic!("Unexpected control message"),
|
||||
@ -1334,8 +1350,42 @@ impl<'a> ControlMessage<'a> {
|
||||
/// as with sendto.
|
||||
///
|
||||
/// Allocates if cmsgs is nonempty.
|
||||
pub fn sendmsg(fd: RawFd, iov: &[IoVec<&[u8]>], cmsgs: &[ControlMessage],
|
||||
flags: MsgFlags, addr: Option<&SockAddr>) -> Result<usize>
|
||||
///
|
||||
/// # Examples
|
||||
/// When not directing to any specific address, use `()` for the generic type
|
||||
/// ```
|
||||
/// # use nix::sys::socket::*;
|
||||
/// # use nix::unistd::pipe;
|
||||
/// # use nix::sys::uio::IoVec;
|
||||
/// let (fd1, fd2) = socketpair(AddressFamily::Unix, SockType::Stream, None,
|
||||
/// SockFlag::empty())
|
||||
/// .unwrap();
|
||||
/// let (r, w) = pipe().unwrap();
|
||||
///
|
||||
/// let iov = [IoVec::from_slice(b"hello")];
|
||||
/// let fds = [r];
|
||||
/// let cmsg = ControlMessage::ScmRights(&fds);
|
||||
/// sendmsg::<()>(fd1, &iov, &[cmsg], MsgFlags::empty(), None).unwrap();
|
||||
/// ```
|
||||
/// When directing to a specific address, the generic type will be inferred.
|
||||
/// ```
|
||||
/// # use nix::sys::socket::*;
|
||||
/// # use nix::unistd::pipe;
|
||||
/// # use nix::sys::uio::IoVec;
|
||||
/// # use std::str::FromStr;
|
||||
/// let localhost = SockaddrIn::from_str("1.2.3.4:8080").unwrap();
|
||||
/// let fd = socket(AddressFamily::Inet, SockType::Datagram, SockFlag::empty(),
|
||||
/// None).unwrap();
|
||||
/// let (r, w) = pipe().unwrap();
|
||||
///
|
||||
/// let iov = [IoVec::from_slice(b"hello")];
|
||||
/// let fds = [r];
|
||||
/// let cmsg = ControlMessage::ScmRights(&fds);
|
||||
/// sendmsg(fd, &iov, &[cmsg], MsgFlags::empty(), Some(&localhost)).unwrap();
|
||||
/// ```
|
||||
pub fn sendmsg<S>(fd: RawFd, iov: &[IoVec<&[u8]>], cmsgs: &[ControlMessage],
|
||||
flags: MsgFlags, addr: Option<&S>) -> Result<usize>
|
||||
where S: SockaddrLike
|
||||
{
|
||||
let capacity = cmsgs.iter().map(|c| c.space()).sum();
|
||||
|
||||
@ -1357,14 +1407,15 @@ pub fn sendmsg(fd: RawFd, iov: &[IoVec<&[u8]>], cmsgs: &[ControlMessage],
|
||||
target_os = "netbsd",
|
||||
))]
|
||||
#[derive(Debug)]
|
||||
pub struct SendMmsgData<'a, I, C>
|
||||
pub struct SendMmsgData<'a, I, C, S>
|
||||
where
|
||||
I: AsRef<[IoVec<&'a [u8]>]>,
|
||||
C: AsRef<[ControlMessage<'a>]>
|
||||
C: AsRef<[ControlMessage<'a>]>,
|
||||
S: SockaddrLike + 'a
|
||||
{
|
||||
pub iov: I,
|
||||
pub cmsgs: C,
|
||||
pub addr: Option<SockAddr>,
|
||||
pub addr: Option<S>,
|
||||
pub _lt: std::marker::PhantomData<&'a I>,
|
||||
}
|
||||
|
||||
@ -1391,14 +1442,15 @@ pub struct SendMmsgData<'a, I, C>
|
||||
target_os = "freebsd",
|
||||
target_os = "netbsd",
|
||||
))]
|
||||
pub fn sendmmsg<'a, I, C>(
|
||||
pub fn sendmmsg<'a, I, C, S>(
|
||||
fd: RawFd,
|
||||
data: impl std::iter::IntoIterator<Item=&'a SendMmsgData<'a, I, C>>,
|
||||
data: impl std::iter::IntoIterator<Item=&'a SendMmsgData<'a, I, C, S>>,
|
||||
flags: MsgFlags
|
||||
) -> Result<Vec<usize>>
|
||||
where
|
||||
I: AsRef<[IoVec<&'a [u8]>]> + 'a,
|
||||
C: AsRef<[ControlMessage<'a>]> + 'a,
|
||||
S: SockaddrLike + 'a
|
||||
{
|
||||
let iter = data.into_iter();
|
||||
|
||||
@ -1486,15 +1538,16 @@ pub struct RecvMmsgData<'a, I>
|
||||
target_os = "netbsd",
|
||||
))]
|
||||
#[allow(clippy::needless_collect)] // Complicated false positive
|
||||
pub fn recvmmsg<'a, I>(
|
||||
pub fn recvmmsg<'a, I, S>(
|
||||
fd: RawFd,
|
||||
data: impl std::iter::IntoIterator<Item=&'a mut RecvMmsgData<'a, I>,
|
||||
IntoIter=impl ExactSizeIterator + Iterator<Item=&'a mut RecvMmsgData<'a, I>>>,
|
||||
flags: MsgFlags,
|
||||
timeout: Option<crate::sys::time::TimeSpec>
|
||||
) -> Result<Vec<RecvMsg<'a>>>
|
||||
) -> Result<Vec<RecvMsg<'a, S>>>
|
||||
where
|
||||
I: AsRef<[IoVec<&'a mut [u8]>]> + 'a,
|
||||
S: Copy + SockaddrLike + 'a
|
||||
{
|
||||
let iter = data.into_iter();
|
||||
|
||||
@ -1556,13 +1609,15 @@ pub fn recvmmsg<'a, I>(
|
||||
.collect())
|
||||
}
|
||||
|
||||
unsafe fn read_mhdr<'a, 'b>(
|
||||
unsafe fn read_mhdr<'a, 'b, S>(
|
||||
mhdr: msghdr,
|
||||
r: isize,
|
||||
msg_controllen: usize,
|
||||
address: sockaddr_storage,
|
||||
address: S,
|
||||
cmsg_buffer: &'a mut Option<&'b mut Vec<u8>>
|
||||
) -> RecvMsg<'b> {
|
||||
) -> RecvMsg<'b, S>
|
||||
where S: SockaddrLike
|
||||
{
|
||||
let cmsghdr = {
|
||||
if mhdr.msg_controllen > 0 {
|
||||
// got control message(s)
|
||||
@ -1578,27 +1633,23 @@ unsafe fn read_mhdr<'a, 'b>(
|
||||
}.as_ref()
|
||||
};
|
||||
|
||||
let address = sockaddr_storage_to_addr(
|
||||
&address ,
|
||||
mhdr.msg_namelen as usize
|
||||
).ok();
|
||||
|
||||
RecvMsg {
|
||||
bytes: r as usize,
|
||||
cmsghdr,
|
||||
address,
|
||||
address: Some(address),
|
||||
flags: MsgFlags::from_bits_truncate(mhdr.msg_flags),
|
||||
mhdr,
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn pack_mhdr_to_receive<'a, I>(
|
||||
unsafe fn pack_mhdr_to_receive<'a, I, S>(
|
||||
iov: I,
|
||||
cmsg_buffer: &mut Option<&mut Vec<u8>>,
|
||||
address: *mut sockaddr_storage,
|
||||
address: *mut S,
|
||||
) -> (usize, msghdr)
|
||||
where
|
||||
I: AsRef<[IoVec<&'a mut [u8]>]> + 'a,
|
||||
S: SockaddrLike + 'a
|
||||
{
|
||||
let (msg_control, msg_controllen) = cmsg_buffer.as_mut()
|
||||
.map(|v| (v.as_mut_ptr(), v.capacity()))
|
||||
@ -1609,8 +1660,8 @@ unsafe fn pack_mhdr_to_receive<'a, I>(
|
||||
// initialize it.
|
||||
let mut mhdr = mem::MaybeUninit::<msghdr>::zeroed();
|
||||
let p = mhdr.as_mut_ptr();
|
||||
(*p).msg_name = address as *mut c_void;
|
||||
(*p).msg_namelen = mem::size_of::<sockaddr_storage>() as socklen_t;
|
||||
(*p).msg_name = (*address).as_mut_ptr() as *mut c_void;
|
||||
(*p).msg_namelen = S::size();
|
||||
(*p).msg_iov = iov.as_ref().as_ptr() as *mut iovec;
|
||||
(*p).msg_iovlen = iov.as_ref().len() as _;
|
||||
(*p).msg_control = msg_control as *mut c_void;
|
||||
@ -1622,27 +1673,19 @@ unsafe fn pack_mhdr_to_receive<'a, I>(
|
||||
(msg_controllen, mhdr)
|
||||
}
|
||||
|
||||
fn pack_mhdr_to_send<'a, I, C>(
|
||||
fn pack_mhdr_to_send<'a, I, C, S>(
|
||||
cmsg_buffer: &mut [u8],
|
||||
iov: I,
|
||||
cmsgs: C,
|
||||
addr: Option<&SockAddr>
|
||||
addr: Option<&S>
|
||||
) -> msghdr
|
||||
where
|
||||
I: AsRef<[IoVec<&'a [u8]>]>,
|
||||
C: AsRef<[ControlMessage<'a>]>
|
||||
C: AsRef<[ControlMessage<'a>]>,
|
||||
S: SockaddrLike + 'a
|
||||
{
|
||||
let capacity = cmsg_buffer.len();
|
||||
|
||||
// Next encode the sending address, if provided
|
||||
let (name, namelen) = match addr {
|
||||
Some(addr) => {
|
||||
let (x, y) = addr.as_ffi_pair();
|
||||
(x as *const _, y)
|
||||
},
|
||||
None => (ptr::null(), 0),
|
||||
};
|
||||
|
||||
// The message header must be initialized before the individual cmsgs.
|
||||
let cmsg_ptr = if capacity > 0 {
|
||||
cmsg_buffer.as_ptr() as *mut c_void
|
||||
@ -1655,8 +1698,8 @@ fn pack_mhdr_to_send<'a, I, C>(
|
||||
// initialize it.
|
||||
let mut mhdr = mem::MaybeUninit::<msghdr>::zeroed();
|
||||
let p = mhdr.as_mut_ptr();
|
||||
(*p).msg_name = name as *mut _;
|
||||
(*p).msg_namelen = namelen;
|
||||
(*p).msg_name = addr.map(S::as_ptr).unwrap_or(ptr::null()) as *mut _;
|
||||
(*p).msg_namelen = addr.map(S::len).unwrap_or(0);
|
||||
// transmute iov into a mutable pointer. sendmsg doesn't really mutate
|
||||
// the buffer, but the standard says that it takes a mutable pointer
|
||||
(*p).msg_iov = iov.as_ref().as_ptr() as *mut _;
|
||||
@ -1697,9 +1740,10 @@ fn pack_mhdr_to_send<'a, I, C>(
|
||||
///
|
||||
/// # References
|
||||
/// [recvmsg(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvmsg.html)
|
||||
pub fn recvmsg<'a>(fd: RawFd, iov: &[IoVec<&mut [u8]>],
|
||||
pub fn recvmsg<'a, S>(fd: RawFd, iov: &[IoVec<&mut [u8]>],
|
||||
mut cmsg_buffer: Option<&'a mut Vec<u8>>,
|
||||
flags: MsgFlags) -> Result<RecvMsg<'a>>
|
||||
flags: MsgFlags) -> Result<RecvMsg<'a, S>>
|
||||
where S: SockaddrLike + 'a
|
||||
{
|
||||
let mut address = mem::MaybeUninit::uninit();
|
||||
|
||||
@ -1779,10 +1823,9 @@ pub fn listen(sockfd: RawFd, backlog: usize) -> Result<()> {
|
||||
/// Bind a name to a socket
|
||||
///
|
||||
/// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html)
|
||||
pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
|
||||
pub fn bind(fd: RawFd, addr: &dyn SockaddrLike) -> Result<()> {
|
||||
let res = unsafe {
|
||||
let (ptr, len) = addr.as_ffi_pair();
|
||||
libc::bind(fd, ptr, len)
|
||||
libc::bind(fd, addr.as_ptr(), addr.len())
|
||||
};
|
||||
|
||||
Errno::result(res).map(drop)
|
||||
@ -1825,10 +1868,9 @@ pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
|
||||
/// Initiate a connection on a socket
|
||||
///
|
||||
/// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html)
|
||||
pub fn connect(fd: RawFd, addr: &SockAddr) -> Result<()> {
|
||||
pub fn connect(fd: RawFd, addr: &dyn SockaddrLike) -> Result<()> {
|
||||
let res = unsafe {
|
||||
let (ptr, len) = addr.as_ffi_pair();
|
||||
libc::connect(fd, ptr, len)
|
||||
libc::connect(fd, addr.as_ptr(), addr.len())
|
||||
};
|
||||
|
||||
Errno::result(res).map(drop)
|
||||
@ -1855,36 +1897,38 @@ pub fn recv(sockfd: RawFd, buf: &mut [u8], flags: MsgFlags) -> Result<usize> {
|
||||
/// address of the sender.
|
||||
///
|
||||
/// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html)
|
||||
pub fn recvfrom(sockfd: RawFd, buf: &mut [u8])
|
||||
-> Result<(usize, Option<SockAddr>)>
|
||||
pub fn recvfrom<T:SockaddrLike>(sockfd: RawFd, buf: &mut [u8])
|
||||
-> Result<(usize, Option<T>)>
|
||||
{
|
||||
unsafe {
|
||||
let mut addr: sockaddr_storage = mem::zeroed();
|
||||
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;
|
||||
let mut addr = mem::MaybeUninit::uninit();
|
||||
let mut len = mem::size_of::<T>() as socklen_t;
|
||||
|
||||
let ret = Errno::result(libc::recvfrom(
|
||||
sockfd,
|
||||
buf.as_ptr() as *mut c_void,
|
||||
buf.len() as size_t,
|
||||
0,
|
||||
&mut addr as *mut libc::sockaddr_storage as *mut libc::sockaddr,
|
||||
addr.as_mut_ptr() as *mut libc::sockaddr,
|
||||
&mut len as *mut socklen_t))? as usize;
|
||||
|
||||
match sockaddr_storage_to_addr(&addr, len as usize) {
|
||||
Err(Errno::ENOTCONN) => Ok((ret, None)),
|
||||
Ok(addr) => Ok((ret, Some(addr))),
|
||||
Err(e) => Err(e)
|
||||
}
|
||||
Ok((ret, T::from_raw(&addr.assume_init(), Some(len))))
|
||||
}
|
||||
}
|
||||
|
||||
/// Send a message to a socket
|
||||
///
|
||||
/// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html)
|
||||
pub fn sendto(fd: RawFd, buf: &[u8], addr: &SockAddr, flags: MsgFlags) -> Result<usize> {
|
||||
pub fn sendto(fd: RawFd, buf: &[u8], addr: &dyn SockaddrLike, flags: MsgFlags) -> Result<usize> {
|
||||
let ret = unsafe {
|
||||
let (ptr, len) = addr.as_ffi_pair();
|
||||
libc::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(),
|
||||
addr.as_ptr(),
|
||||
addr.len()
|
||||
)
|
||||
};
|
||||
|
||||
Errno::result(ret).map(|r| r as usize)
|
||||
@ -1954,10 +1998,10 @@ pub fn setsockopt<O: SetSockOpt>(fd: RawFd, opt: O, val: &O::Val) -> Result<()>
|
||||
/// Get the address of the peer connected to the socket `fd`.
|
||||
///
|
||||
/// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html)
|
||||
pub fn getpeername(fd: RawFd) -> Result<SockAddr> {
|
||||
pub fn getpeername<T: SockaddrLike>(fd: RawFd) -> Result<T> {
|
||||
unsafe {
|
||||
let mut addr = mem::MaybeUninit::uninit();
|
||||
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;
|
||||
let mut addr = mem::MaybeUninit::<T>::uninit();
|
||||
let mut len = T::size();
|
||||
|
||||
let ret = libc::getpeername(
|
||||
fd,
|
||||
@ -1967,17 +2011,18 @@ pub fn getpeername(fd: RawFd) -> Result<SockAddr> {
|
||||
|
||||
Errno::result(ret)?;
|
||||
|
||||
sockaddr_storage_to_addr(&addr.assume_init(), len as usize)
|
||||
T::from_raw(addr.assume_init().as_ptr(), Some(len))
|
||||
.ok_or(Errno::EINVAL)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the current address to which the socket `fd` is bound.
|
||||
///
|
||||
/// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html)
|
||||
pub fn getsockname(fd: RawFd) -> Result<SockAddr> {
|
||||
pub fn getsockname<T: SockaddrLike>(fd: RawFd) -> Result<T> {
|
||||
unsafe {
|
||||
let mut addr = mem::MaybeUninit::uninit();
|
||||
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;
|
||||
let mut addr = mem::MaybeUninit::<T>::uninit();
|
||||
let mut len = T::size();
|
||||
|
||||
let ret = libc::getsockname(
|
||||
fd,
|
||||
@ -1987,7 +2032,8 @@ pub fn getsockname(fd: RawFd) -> Result<SockAddr> {
|
||||
|
||||
Errno::result(ret)?;
|
||||
|
||||
sockaddr_storage_to_addr(&addr.assume_init(), len as usize)
|
||||
T::from_raw(addr.assume_init().as_ptr(), Some(len))
|
||||
.ok_or(Errno::EINVAL)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1999,6 +2045,11 @@ pub fn getsockname(fd: RawFd) -> Result<SockAddr> {
|
||||
/// allocated and valid. It must be at least as large as all the useful parts
|
||||
/// of the structure. Note that in the case of a `sockaddr_un`, `len` need not
|
||||
/// include the terminating null.
|
||||
#[deprecated(
|
||||
since = "0.24.0",
|
||||
note = "use SockaddrLike or SockaddrStorage instead"
|
||||
)]
|
||||
#[allow(deprecated)]
|
||||
pub fn sockaddr_storage_to_addr(
|
||||
addr: &sockaddr_storage,
|
||||
len: usize) -> Result<SockAddr> {
|
||||
|
@ -1,8 +1,11 @@
|
||||
use nix::sys::socket::{AddressFamily, InetAddr, SockAddr, UnixAddr, getsockname, sockaddr, sockaddr_in6, sockaddr_storage_to_addr};
|
||||
#[allow(deprecated)]
|
||||
use nix::sys::socket::InetAddr;
|
||||
use nix::sys::socket::{AddressFamily,
|
||||
UnixAddr, getsockname, sockaddr, sockaddr_in6};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::mem::{self, MaybeUninit};
|
||||
use std::net::{self, Ipv6Addr, SocketAddr, SocketAddrV6};
|
||||
use std::net::{self, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
|
||||
use std::os::unix::io::RawFd;
|
||||
use std::path::Path;
|
||||
use std::slice;
|
||||
@ -11,6 +14,7 @@ use libc::{c_char, sockaddr_storage};
|
||||
#[cfg(any(target_os = "linux", target_os= "android"))]
|
||||
use crate::*;
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[test]
|
||||
pub fn test_inetv4_addr_to_sock_addr() {
|
||||
let actual: net::SocketAddr = FromStr::from_str("127.0.0.1:3000").unwrap();
|
||||
@ -34,8 +38,11 @@ pub fn test_inetv4_addr_to_sock_addr() {
|
||||
assert_eq!(actual, inet);
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[test]
|
||||
pub fn test_inetv4_addr_roundtrip_sockaddr_storage_to_addr() {
|
||||
use nix::sys::socket::{SockAddr, sockaddr_storage_to_addr};
|
||||
|
||||
let actual: net::SocketAddr = FromStr::from_str("127.0.0.1:3000").unwrap();
|
||||
let addr = InetAddr::from_std(&actual);
|
||||
let sockaddr = SockAddr::new_inet(addr);
|
||||
@ -63,13 +70,11 @@ pub fn test_inetv4_addr_roundtrip_sockaddr_storage_to_addr() {
|
||||
pub fn test_timestamping() {
|
||||
use nix::sys::socket::{
|
||||
recvmsg, sendmsg, setsockopt, socket, sockopt::Timestamping, ControlMessageOwned, MsgFlags,
|
||||
SockFlag, SockType, TimestampingFlag,
|
||||
SockaddrIn, SockFlag, SockType, TimestampingFlag,
|
||||
};
|
||||
use nix::sys::uio::IoVec;
|
||||
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:6790").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let sock_addr = SockaddrIn::from_str("127.0.0.1:6790").unwrap();
|
||||
|
||||
let ssock = socket(
|
||||
AddressFamily::Inet,
|
||||
@ -97,7 +102,7 @@ pub fn test_timestamping() {
|
||||
let iov2 = [IoVec::from_mut_slice(&mut rbuf)];
|
||||
let mut cmsg = cmsg_space!(nix::sys::socket::Timestamps);
|
||||
sendmsg(ssock, &iov1, &[], flags, Some(&sock_addr)).unwrap();
|
||||
let recv = recvmsg(rsock, &iov2, Some(&mut cmsg), flags).unwrap();
|
||||
let recv = recvmsg::<()>(rsock, &iov2, Some(&mut cmsg), flags).unwrap();
|
||||
|
||||
let mut ts = None;
|
||||
for c in recv.cmsgs() {
|
||||
@ -115,29 +120,11 @@ pub fn test_timestamping() {
|
||||
assert!(std::time::Duration::from(diff).as_secs() < 60);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_inetv6_addr_to_sock_addr() {
|
||||
let port: u16 = 3000;
|
||||
let flowinfo: u32 = 1;
|
||||
let scope_id: u32 = 2;
|
||||
let ip: Ipv6Addr = "fe80::1".parse().unwrap();
|
||||
|
||||
let actual = SocketAddr::V6(SocketAddrV6::new(ip, port, flowinfo, scope_id));
|
||||
let addr = InetAddr::from_std(&actual);
|
||||
|
||||
match addr {
|
||||
InetAddr::V6(addr) => {
|
||||
assert_eq!(addr.sin6_port, port.to_be());
|
||||
assert_eq!(addr.sin6_flowinfo, flowinfo);
|
||||
assert_eq!(addr.sin6_scope_id, scope_id);
|
||||
}
|
||||
_ => panic!("nope"),
|
||||
}
|
||||
|
||||
assert_eq!(actual, addr.to_std());
|
||||
}
|
||||
#[allow(deprecated)]
|
||||
#[test]
|
||||
pub fn test_inetv6_addr_roundtrip_sockaddr_storage_to_addr() {
|
||||
use nix::sys::socket::{SockAddr, sockaddr_storage_to_addr};
|
||||
|
||||
let port: u16 = 3000;
|
||||
let flowinfo: u32 = 1;
|
||||
let scope_id: u32 = 2;
|
||||
@ -244,13 +231,13 @@ pub fn test_abstract_uds_addr() {
|
||||
#[test]
|
||||
pub fn test_getsockname() {
|
||||
use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag};
|
||||
use nix::sys::socket::{bind, SockAddr};
|
||||
use nix::sys::socket::bind;
|
||||
|
||||
let tempdir = tempfile::tempdir().unwrap();
|
||||
let sockname = tempdir.path().join("sock");
|
||||
let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::empty(), None)
|
||||
.expect("socket failed");
|
||||
let sockaddr = SockAddr::new_unix(&sockname).unwrap();
|
||||
let sockaddr = UnixAddr::new(&sockname).unwrap();
|
||||
bind(sock, &sockaddr).expect("bind failed");
|
||||
assert_eq!(sockaddr, getsockname(sock).expect("getsockname failed"));
|
||||
}
|
||||
@ -277,10 +264,10 @@ mod recvfrom {
|
||||
|
||||
const MSG: &[u8] = b"Hello, World!";
|
||||
|
||||
fn sendrecv<Fs, Fr>(rsock: RawFd, ssock: RawFd, f_send: Fs, mut f_recv: Fr) -> Option<SockAddr>
|
||||
fn sendrecv<Fs, Fr>(rsock: RawFd, ssock: RawFd, f_send: Fs, mut f_recv: Fr) -> Option<SockaddrStorage>
|
||||
where
|
||||
Fs: Fn(RawFd, &[u8], MsgFlags) -> Result<usize> + Send + 'static,
|
||||
Fr: FnMut(usize, Option<SockAddr>),
|
||||
Fr: FnMut(usize, Option<SockaddrStorage>),
|
||||
{
|
||||
let mut buf: [u8; 13] = [0u8; 13];
|
||||
let mut l = 0;
|
||||
@ -316,9 +303,8 @@ mod recvfrom {
|
||||
|
||||
#[test]
|
||||
pub fn udp() {
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:6789").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let std_sa = SocketAddrV4::from_str("127.0.0.1:6789").unwrap();
|
||||
let sock_addr = SockaddrIn::from(std_sa);
|
||||
let rsock = socket(AddressFamily::Inet,
|
||||
SockType::Datagram,
|
||||
SockFlag::empty(),
|
||||
@ -335,7 +321,7 @@ mod recvfrom {
|
||||
sendto(s, m, &sock_addr, flags)
|
||||
},|_, _| {});
|
||||
// UDP sockets should set the from address
|
||||
assert_eq!(AddressFamily::Inet, from.unwrap().family());
|
||||
assert_eq!(AddressFamily::Inet, from.unwrap().family().unwrap());
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
@ -356,9 +342,7 @@ mod recvfrom {
|
||||
// with size 2 and two UDP packet with size 1 will be sent.
|
||||
let segment_size: u16 = 2;
|
||||
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:6791").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let sock_addr = SockaddrIn::new(127, 0, 0, 1, 6791);
|
||||
let rsock = socket(AddressFamily::Inet,
|
||||
SockType::Datagram,
|
||||
SockFlag::empty(),
|
||||
@ -429,12 +413,10 @@ mod recvfrom {
|
||||
pub fn udp_sendmmsg() {
|
||||
use nix::sys::uio::IoVec;
|
||||
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:6793").unwrap();
|
||||
let std_sa2 = SocketAddr::from_str("127.0.0.1:6794").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let inet_addr2 = InetAddr::from_std(&std_sa2);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let sock_addr2 = SockAddr::new_inet(inet_addr2);
|
||||
let std_sa = SocketAddrV4::from_str("127.0.0.1:6793").unwrap();
|
||||
let std_sa2 = SocketAddrV4::from_str("127.0.0.1:6794").unwrap();
|
||||
let sock_addr = SockaddrIn::from(std_sa);
|
||||
let sock_addr2 = SockaddrIn::from(std_sa2);
|
||||
|
||||
let rsock = socket(AddressFamily::Inet,
|
||||
SockType::Datagram,
|
||||
@ -482,7 +464,7 @@ mod recvfrom {
|
||||
})
|
||||
}, |_, _ | {});
|
||||
// UDP sockets should set the from address
|
||||
assert_eq!(AddressFamily::Inet, from.unwrap().family());
|
||||
assert_eq!(AddressFamily::Inet, from.unwrap().family().unwrap());
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
@ -499,9 +481,8 @@ mod recvfrom {
|
||||
const NUM_MESSAGES_SENT: usize = 2;
|
||||
const DATA: [u8; 2] = [1,2];
|
||||
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:6798").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let inet_addr = SocketAddrV4::from_str("127.0.0.1:6798").unwrap();
|
||||
let sock_addr = SockaddrIn::from(inet_addr);
|
||||
|
||||
let rsock = socket(AddressFamily::Inet,
|
||||
SockType::Datagram,
|
||||
@ -537,11 +518,11 @@ mod recvfrom {
|
||||
})
|
||||
};
|
||||
|
||||
let res = recvmmsg(rsock, &mut msgs, MsgFlags::empty(), None).expect("recvmmsg");
|
||||
let res: Vec<RecvMsg<SockaddrIn>> = recvmmsg(rsock, &mut msgs, MsgFlags::empty(), None).expect("recvmmsg");
|
||||
assert_eq!(res.len(), DATA.len());
|
||||
|
||||
for RecvMsg { address, bytes, .. } in res.into_iter() {
|
||||
assert_eq!(AddressFamily::Inet, address.unwrap().family());
|
||||
assert_eq!(AddressFamily::Inet, address.unwrap().family().unwrap());
|
||||
assert_eq!(DATA.len(), bytes);
|
||||
}
|
||||
|
||||
@ -566,9 +547,8 @@ mod recvfrom {
|
||||
const NUM_MESSAGES_SENT: usize = 2;
|
||||
const DATA: [u8; 4] = [1,2,3,4];
|
||||
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:6799").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let inet_addr = SocketAddrV4::from_str("127.0.0.1:6799").unwrap();
|
||||
let sock_addr = SockaddrIn::from(inet_addr);
|
||||
|
||||
let rsock = socket(AddressFamily::Inet,
|
||||
SockType::Datagram,
|
||||
@ -609,11 +589,11 @@ mod recvfrom {
|
||||
})
|
||||
};
|
||||
|
||||
let res = recvmmsg(rsock, &mut msgs, MsgFlags::MSG_DONTWAIT, None).expect("recvmmsg");
|
||||
let res: Vec<RecvMsg<SockaddrIn>> = recvmmsg(rsock, &mut msgs, MsgFlags::MSG_DONTWAIT, None).expect("recvmmsg");
|
||||
assert_eq!(res.len(), NUM_MESSAGES_SENT);
|
||||
|
||||
for RecvMsg { address, bytes, .. } in res.into_iter() {
|
||||
assert_eq!(AddressFamily::Inet, address.unwrap().family());
|
||||
assert_eq!(AddressFamily::Inet, address.unwrap().family().unwrap());
|
||||
assert_eq!(DATA.len(), bytes);
|
||||
}
|
||||
|
||||
@ -633,7 +613,7 @@ pub fn test_recvmsg_ebadf() {
|
||||
let mut buf = [0u8; 5];
|
||||
let iov = [IoVec::from_mut_slice(&mut buf[..])];
|
||||
let fd = -1; // Bad file descriptor
|
||||
let r = recvmsg(fd, &iov, None, MsgFlags::empty());
|
||||
let r = recvmsg::<()>(fd, &iov, None, MsgFlags::empty());
|
||||
assert_eq!(r.err().unwrap(), Errno::EBADF);
|
||||
}
|
||||
|
||||
@ -657,7 +637,7 @@ pub fn test_scm_rights() {
|
||||
let iov = [IoVec::from_slice(b"hello")];
|
||||
let fds = [r];
|
||||
let cmsg = ControlMessage::ScmRights(&fds);
|
||||
assert_eq!(sendmsg(fd1, &iov, &[cmsg], MsgFlags::empty(), None).unwrap(), 5);
|
||||
assert_eq!(sendmsg::<()>(fd1, &iov, &[cmsg], MsgFlags::empty(), None).unwrap(), 5);
|
||||
close(r).unwrap();
|
||||
close(fd1).unwrap();
|
||||
}
|
||||
@ -666,7 +646,7 @@ pub fn test_scm_rights() {
|
||||
let mut buf = [0u8; 5];
|
||||
let iov = [IoVec::from_mut_slice(&mut buf[..])];
|
||||
let mut cmsgspace = cmsg_space!([RawFd; 1]);
|
||||
let msg = recvmsg(fd2, &iov, Some(&mut cmsgspace), MsgFlags::empty()).unwrap();
|
||||
let msg = recvmsg::<()>(fd2, &iov, Some(&mut cmsgspace), MsgFlags::empty()).unwrap();
|
||||
|
||||
for cmsg in msg.cmsgs() {
|
||||
if let ControlMessageOwned::ScmRights(fd) = cmsg {
|
||||
@ -700,7 +680,7 @@ pub fn test_af_alg_cipher() {
|
||||
use nix::sys::uio::IoVec;
|
||||
use nix::unistd::read;
|
||||
use nix::sys::socket::{socket, sendmsg, bind, accept, setsockopt,
|
||||
AddressFamily, SockType, SockFlag, SockAddr,
|
||||
AddressFamily, SockType, SockFlag, AlgAddr,
|
||||
ControlMessage, MsgFlags};
|
||||
use nix::sys::socket::sockopt::AlgSetKey;
|
||||
|
||||
@ -723,22 +703,18 @@ pub fn test_af_alg_cipher() {
|
||||
let sock = socket(AddressFamily::Alg, SockType::SeqPacket, SockFlag::empty(), None)
|
||||
.expect("socket failed");
|
||||
|
||||
let sockaddr = SockAddr::new_alg(alg_type, alg_name);
|
||||
let sockaddr = AlgAddr::new(alg_type, alg_name);
|
||||
bind(sock, &sockaddr).expect("bind failed");
|
||||
|
||||
if let SockAddr::Alg(alg) = sockaddr {
|
||||
assert_eq!(alg.alg_name().to_string_lossy(), alg_name);
|
||||
assert_eq!(alg.alg_type().to_string_lossy(), alg_type);
|
||||
} else {
|
||||
panic!("unexpected SockAddr");
|
||||
}
|
||||
assert_eq!(sockaddr.alg_name().to_string_lossy(), alg_name);
|
||||
assert_eq!(sockaddr.alg_type().to_string_lossy(), alg_type);
|
||||
|
||||
setsockopt(sock, AlgSetKey::default(), &key).expect("setsockopt");
|
||||
let session_socket = accept(sock).expect("accept failed");
|
||||
|
||||
let msgs = [ControlMessage::AlgSetOp(&libc::ALG_OP_ENCRYPT), ControlMessage::AlgSetIv(iv.as_slice())];
|
||||
let iov = IoVec::from_slice(&payload);
|
||||
sendmsg(session_socket, &[iov], &msgs, MsgFlags::empty(), None).expect("sendmsg encrypt");
|
||||
sendmsg::<()>(session_socket, &[iov], &msgs, MsgFlags::empty(), None).expect("sendmsg encrypt");
|
||||
|
||||
// allocate buffer for encrypted data
|
||||
let mut encrypted = vec![0u8; payload_len];
|
||||
@ -750,7 +726,7 @@ pub fn test_af_alg_cipher() {
|
||||
let iv = vec![1u8; iv_len];
|
||||
|
||||
let msgs = [ControlMessage::AlgSetOp(&libc::ALG_OP_DECRYPT), ControlMessage::AlgSetIv(iv.as_slice())];
|
||||
sendmsg(session_socket, &[iov], &msgs, MsgFlags::empty(), None).expect("sendmsg decrypt");
|
||||
sendmsg::<()>(session_socket, &[iov], &msgs, MsgFlags::empty(), None).expect("sendmsg decrypt");
|
||||
|
||||
// allocate buffer for decrypted data
|
||||
let mut decrypted = vec![0u8; payload_len];
|
||||
@ -771,7 +747,7 @@ pub fn test_af_alg_aead() {
|
||||
use nix::sys::uio::IoVec;
|
||||
use nix::unistd::{read, close};
|
||||
use nix::sys::socket::{socket, sendmsg, bind, accept, setsockopt,
|
||||
AddressFamily, SockType, SockFlag, SockAddr,
|
||||
AddressFamily, SockType, SockFlag, AlgAddr,
|
||||
ControlMessage, MsgFlags};
|
||||
use nix::sys::socket::sockopt::{AlgSetKey, AlgSetAeadAuthSize};
|
||||
|
||||
@ -807,7 +783,7 @@ pub fn test_af_alg_aead() {
|
||||
let sock = socket(AddressFamily::Alg, SockType::SeqPacket, SockFlag::empty(), None)
|
||||
.expect("socket failed");
|
||||
|
||||
let sockaddr = SockAddr::new_alg(alg_type, alg_name);
|
||||
let sockaddr = AlgAddr::new(alg_type, alg_name);
|
||||
bind(sock, &sockaddr).expect("bind failed");
|
||||
|
||||
setsockopt(sock, AlgSetAeadAuthSize, &auth_size).expect("setsockopt AlgSetAeadAuthSize");
|
||||
@ -819,7 +795,7 @@ pub fn test_af_alg_aead() {
|
||||
ControlMessage::AlgSetIv(iv.as_slice()),
|
||||
ControlMessage::AlgSetAeadAssoclen(&assoc_size)];
|
||||
let iov = IoVec::from_slice(&payload);
|
||||
sendmsg(session_socket, &[iov], &msgs, MsgFlags::empty(), None).expect("sendmsg encrypt");
|
||||
sendmsg::<()>(session_socket, &[iov], &msgs, MsgFlags::empty(), None).expect("sendmsg encrypt");
|
||||
|
||||
// allocate buffer for encrypted data
|
||||
let mut encrypted = vec![0u8; (assoc_size as usize) + payload_len + auth_size];
|
||||
@ -842,7 +818,7 @@ pub fn test_af_alg_aead() {
|
||||
ControlMessage::AlgSetIv(iv.as_slice()),
|
||||
ControlMessage::AlgSetAeadAssoclen(&assoc_size),
|
||||
];
|
||||
sendmsg(session_socket, &[iov], &msgs, MsgFlags::empty(), None).expect("sendmsg decrypt");
|
||||
sendmsg::<()>(session_socket, &[iov], &msgs, MsgFlags::empty(), None).expect("sendmsg decrypt");
|
||||
|
||||
// allocate buffer for decrypted data
|
||||
let mut decrypted = vec![0u8; payload_len + (assoc_size as usize) + auth_size];
|
||||
@ -872,7 +848,7 @@ pub fn test_sendmsg_ipv4packetinfo() {
|
||||
use cfg_if::cfg_if;
|
||||
use nix::sys::uio::IoVec;
|
||||
use nix::sys::socket::{socket, sendmsg, bind,
|
||||
AddressFamily, SockType, SockFlag, SockAddr,
|
||||
AddressFamily, SockType, SockFlag, SockaddrIn,
|
||||
ControlMessage, MsgFlags};
|
||||
|
||||
let sock = socket(AddressFamily::Inet,
|
||||
@ -881,39 +857,32 @@ pub fn test_sendmsg_ipv4packetinfo() {
|
||||
None)
|
||||
.expect("socket failed");
|
||||
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:4000").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let sock_addr = SockaddrIn::new(127,0,0,1, 4000);
|
||||
|
||||
bind(sock, &sock_addr).expect("bind failed");
|
||||
|
||||
let slice = [1u8, 2, 3, 4, 5, 6, 7, 8];
|
||||
let iov = [IoVec::from_slice(&slice)];
|
||||
|
||||
if let InetAddr::V4(sin) = inet_addr {
|
||||
cfg_if! {
|
||||
if #[cfg(target_os = "netbsd")] {
|
||||
let _dontcare = sin;
|
||||
let pi = libc::in_pktinfo {
|
||||
ipi_ifindex: 0, /* Unspecified interface */
|
||||
ipi_addr: libc::in_addr { s_addr: 0 },
|
||||
};
|
||||
} else {
|
||||
let pi = libc::in_pktinfo {
|
||||
ipi_ifindex: 0, /* Unspecified interface */
|
||||
ipi_addr: libc::in_addr { s_addr: 0 },
|
||||
ipi_spec_dst: sin.sin_addr,
|
||||
};
|
||||
}
|
||||
cfg_if! {
|
||||
if #[cfg(target_os = "netbsd")] {
|
||||
let pi = libc::in_pktinfo {
|
||||
ipi_ifindex: 0, /* Unspecified interface */
|
||||
ipi_addr: libc::in_addr { s_addr: 0 },
|
||||
};
|
||||
} else {
|
||||
let pi = libc::in_pktinfo {
|
||||
ipi_ifindex: 0, /* Unspecified interface */
|
||||
ipi_addr: libc::in_addr { s_addr: 0 },
|
||||
ipi_spec_dst: sock_addr.as_ref().sin_addr,
|
||||
};
|
||||
}
|
||||
|
||||
let cmsg = [ControlMessage::Ipv4PacketInfo(&pi)];
|
||||
|
||||
sendmsg(sock, &iov, &cmsg, MsgFlags::empty(), Some(&sock_addr))
|
||||
.expect("sendmsg");
|
||||
} else {
|
||||
panic!("No IPv4 addresses available for testing?");
|
||||
}
|
||||
|
||||
let cmsg = [ControlMessage::Ipv4PacketInfo(&pi)];
|
||||
|
||||
sendmsg(sock, &iov, &cmsg, MsgFlags::empty(), Some(&sock_addr))
|
||||
.expect("sendmsg");
|
||||
}
|
||||
|
||||
// Verify `ControlMessage::Ipv6PacketInfo` for `sendmsg`.
|
||||
@ -933,7 +902,7 @@ pub fn test_sendmsg_ipv6packetinfo() {
|
||||
use nix::errno::Errno;
|
||||
use nix::sys::uio::IoVec;
|
||||
use nix::sys::socket::{socket, sendmsg, bind,
|
||||
AddressFamily, SockType, SockFlag, SockAddr,
|
||||
AddressFamily, SockType, SockFlag, SockaddrIn6,
|
||||
ControlMessage, MsgFlags};
|
||||
|
||||
let sock = socket(AddressFamily::Inet6,
|
||||
@ -942,9 +911,8 @@ pub fn test_sendmsg_ipv6packetinfo() {
|
||||
None)
|
||||
.expect("socket failed");
|
||||
|
||||
let std_sa = SocketAddr::from_str("[::1]:6000").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let std_sa = SocketAddrV6::from_str("[::1]:6000").unwrap();
|
||||
let sock_addr: SockaddrIn6 = SockaddrIn6::from(std_sa);
|
||||
|
||||
if let Err(Errno::EADDRNOTAVAIL) = bind(sock, &sock_addr) {
|
||||
println!("IPv6 not available, skipping test.");
|
||||
@ -954,19 +922,15 @@ pub fn test_sendmsg_ipv6packetinfo() {
|
||||
let slice = [1u8, 2, 3, 4, 5, 6, 7, 8];
|
||||
let iov = [IoVec::from_slice(&slice)];
|
||||
|
||||
if let InetAddr::V6(sin) = inet_addr {
|
||||
let pi = libc::in6_pktinfo {
|
||||
ipi6_ifindex: 0, /* Unspecified interface */
|
||||
ipi6_addr: sin.sin6_addr,
|
||||
};
|
||||
let pi = libc::in6_pktinfo {
|
||||
ipi6_ifindex: 0, /* Unspecified interface */
|
||||
ipi6_addr: sock_addr.as_ref().sin6_addr,
|
||||
};
|
||||
|
||||
let cmsg = [ControlMessage::Ipv6PacketInfo(&pi)];
|
||||
let cmsg = [ControlMessage::Ipv6PacketInfo(&pi)];
|
||||
|
||||
sendmsg(sock, &iov, &cmsg, MsgFlags::empty(), Some(&sock_addr))
|
||||
.expect("sendmsg");
|
||||
} else {
|
||||
println!("No IPv6 addresses available for testing: skipping testing Ipv6PacketInfo");
|
||||
}
|
||||
sendmsg::<SockaddrIn6>(sock, &iov, &cmsg, MsgFlags::empty(), Some(&sock_addr))
|
||||
.expect("sendmsg");
|
||||
}
|
||||
|
||||
/// Tests that passing multiple fds using a single `ControlMessage` works.
|
||||
@ -987,7 +951,7 @@ fn test_scm_rights_single_cmsg_multiple_fds() {
|
||||
let mut buf = [0u8; 8];
|
||||
let iovec = [IoVec::from_mut_slice(&mut buf)];
|
||||
let mut space = cmsg_space!([RawFd; 2]);
|
||||
let msg = recvmsg(
|
||||
let msg = recvmsg::<()>(
|
||||
receive.as_raw_fd(),
|
||||
&iovec,
|
||||
Some(&mut space),
|
||||
@ -1014,7 +978,7 @@ fn test_scm_rights_single_cmsg_multiple_fds() {
|
||||
let iov = [IoVec::from_slice(&slice)];
|
||||
let fds = [libc::STDIN_FILENO, libc::STDOUT_FILENO]; // pass stdin and stdout
|
||||
let cmsg = [ControlMessage::ScmRights(&fds)];
|
||||
sendmsg(send.as_raw_fd(), &iov, &cmsg, MsgFlags::empty(), None).unwrap();
|
||||
sendmsg::<()>(send.as_raw_fd(), &iov, &cmsg, MsgFlags::empty(), None).unwrap();
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
@ -1034,7 +998,7 @@ pub fn test_sendmsg_empty_cmsgs() {
|
||||
|
||||
{
|
||||
let iov = [IoVec::from_slice(b"hello")];
|
||||
assert_eq!(sendmsg(fd1, &iov, &[], MsgFlags::empty(), None).unwrap(), 5);
|
||||
assert_eq!(sendmsg::<()>(fd1, &iov, &[], MsgFlags::empty(), None).unwrap(), 5);
|
||||
close(fd1).unwrap();
|
||||
}
|
||||
|
||||
@ -1042,7 +1006,7 @@ pub fn test_sendmsg_empty_cmsgs() {
|
||||
let mut buf = [0u8; 5];
|
||||
let iov = [IoVec::from_mut_slice(&mut buf[..])];
|
||||
let mut cmsgspace = cmsg_space!([RawFd; 1]);
|
||||
let msg = recvmsg(fd2, &iov, Some(&mut cmsgspace), MsgFlags::empty()).unwrap();
|
||||
let msg = recvmsg::<()>(fd2, &iov, Some(&mut cmsgspace), MsgFlags::empty()).unwrap();
|
||||
|
||||
for _ in msg.cmsgs() {
|
||||
panic!("unexpected cmsg");
|
||||
@ -1083,7 +1047,7 @@ fn test_scm_credentials() {
|
||||
let cmsg = ControlMessage::ScmCredentials(&cred);
|
||||
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
|
||||
let cmsg = ControlMessage::ScmCreds;
|
||||
assert_eq!(sendmsg(send, &iov, &[cmsg], MsgFlags::empty(), None).unwrap(), 5);
|
||||
assert_eq!(sendmsg::<()>(send, &iov, &[cmsg], MsgFlags::empty(), None).unwrap(), 5);
|
||||
close(send).unwrap();
|
||||
}
|
||||
|
||||
@ -1091,7 +1055,7 @@ fn test_scm_credentials() {
|
||||
let mut buf = [0u8; 5];
|
||||
let iov = [IoVec::from_mut_slice(&mut buf[..])];
|
||||
let mut cmsgspace = cmsg_space!(UnixCredentials);
|
||||
let msg = recvmsg(recv, &iov, Some(&mut cmsgspace), MsgFlags::empty()).unwrap();
|
||||
let msg = recvmsg::<()>(recv, &iov, Some(&mut cmsgspace), MsgFlags::empty()).unwrap();
|
||||
let mut received_cred = None;
|
||||
|
||||
for cmsg in msg.cmsgs() {
|
||||
@ -1168,7 +1132,7 @@ fn test_impl_scm_credentials_and_rights(mut space: Vec<u8>) {
|
||||
ControlMessage::ScmCredentials(&cred),
|
||||
ControlMessage::ScmRights(&fds),
|
||||
];
|
||||
assert_eq!(sendmsg(send, &iov, &cmsgs, MsgFlags::empty(), None).unwrap(), 5);
|
||||
assert_eq!(sendmsg::<()>(send, &iov, &cmsgs, MsgFlags::empty(), None).unwrap(), 5);
|
||||
close(r).unwrap();
|
||||
close(send).unwrap();
|
||||
}
|
||||
@ -1176,7 +1140,7 @@ fn test_impl_scm_credentials_and_rights(mut space: Vec<u8>) {
|
||||
{
|
||||
let mut buf = [0u8; 5];
|
||||
let iov = [IoVec::from_mut_slice(&mut buf[..])];
|
||||
let msg = recvmsg(recv, &iov, Some(&mut space), MsgFlags::empty()).unwrap();
|
||||
let msg = recvmsg::<()>(recv, &iov, Some(&mut space), MsgFlags::empty()).unwrap();
|
||||
let mut received_cred = None;
|
||||
|
||||
assert_eq!(msg.cmsgs().count(), 2, "expected 2 cmsgs");
|
||||
@ -1218,7 +1182,7 @@ fn test_impl_scm_credentials_and_rights(mut space: Vec<u8>) {
|
||||
#[test]
|
||||
pub fn test_unixdomain() {
|
||||
use nix::sys::socket::{SockType, SockFlag};
|
||||
use nix::sys::socket::{bind, socket, connect, listen, accept, SockAddr};
|
||||
use nix::sys::socket::{bind, socket, connect, listen, accept, UnixAddr};
|
||||
use nix::unistd::{read, write, close};
|
||||
use std::thread;
|
||||
|
||||
@ -1226,7 +1190,7 @@ pub fn test_unixdomain() {
|
||||
let sockname = tempdir.path().join("sock");
|
||||
let s1 = socket(AddressFamily::Unix, SockType::Stream,
|
||||
SockFlag::empty(), None).expect("socket failed");
|
||||
let sockaddr = SockAddr::new_unix(&sockname).unwrap();
|
||||
let sockaddr = UnixAddr::new(&sockname).unwrap();
|
||||
bind(s1, &sockaddr).expect("bind failed");
|
||||
listen(s1, 10).expect("listen failed");
|
||||
|
||||
@ -1254,13 +1218,14 @@ pub fn test_unixdomain() {
|
||||
#[test]
|
||||
pub fn test_syscontrol() {
|
||||
use nix::errno::Errno;
|
||||
use nix::sys::socket::{socket, SockAddr, SockType, SockFlag, SockProtocol};
|
||||
use nix::sys::socket::{socket, SysControlAddr, SockType, SockFlag, SockProtocol};
|
||||
|
||||
let fd = socket(AddressFamily::System, SockType::Datagram,
|
||||
SockFlag::empty(), SockProtocol::KextControl)
|
||||
.expect("socket failed");
|
||||
let _sockaddr = SockAddr::new_sys_control(fd, "com.apple.net.utun_control", 0).expect("resolving sys_control name failed");
|
||||
assert_eq!(SockAddr::new_sys_control(fd, "foo.bar.lol", 0).err(), Some(Errno::ENOENT));
|
||||
SysControlAddr::from_name(fd, "com.apple.net.utun_control", 0)
|
||||
.expect("resolving sys_control name failed");
|
||||
assert_eq!(SysControlAddr::from_name(fd, "foo.bar.lol", 0).err(), Some(Errno::ENOENT));
|
||||
|
||||
// requires root privileges
|
||||
// connect(fd, &sockaddr).expect("connect failed");
|
||||
@ -1280,6 +1245,7 @@ fn loopback_address(family: AddressFamily) -> Option<nix::ifaddrs::InterfaceAddr
|
||||
use std::io::Write;
|
||||
use nix::ifaddrs::getifaddrs;
|
||||
use nix::net::if_::*;
|
||||
use nix::sys::socket::SockaddrLike;
|
||||
|
||||
let addrs = match getifaddrs() {
|
||||
Ok(iter) => iter,
|
||||
@ -1292,22 +1258,10 @@ fn loopback_address(family: AddressFamily) -> Option<nix::ifaddrs::InterfaceAddr
|
||||
};
|
||||
// return first address matching family
|
||||
for ifaddr in addrs {
|
||||
if ifaddr.flags.contains(InterfaceFlags::IFF_LOOPBACK) {
|
||||
match ifaddr.address {
|
||||
Some(SockAddr::Inet(InetAddr::V4(..))) => {
|
||||
match family {
|
||||
AddressFamily::Inet => return Some(ifaddr),
|
||||
_ => continue
|
||||
}
|
||||
},
|
||||
Some(SockAddr::Inet(InetAddr::V6(..))) => {
|
||||
match family {
|
||||
AddressFamily::Inet6 => return Some(ifaddr),
|
||||
_ => continue
|
||||
}
|
||||
},
|
||||
_ => continue,
|
||||
}
|
||||
if ifaddr.flags.contains(InterfaceFlags::IFF_LOOPBACK) &&
|
||||
ifaddr.address.as_ref().and_then(SockaddrLike::family) == Some(family)
|
||||
{
|
||||
return Some(ifaddr)
|
||||
}
|
||||
}
|
||||
None
|
||||
@ -1332,7 +1286,7 @@ fn loopback_address(family: AddressFamily) -> Option<nix::ifaddrs::InterfaceAddr
|
||||
#[test]
|
||||
pub fn test_recv_ipv4pktinfo() {
|
||||
use nix::sys::socket::sockopt::Ipv4PacketInfo;
|
||||
use nix::sys::socket::{bind, SockFlag, SockType};
|
||||
use nix::sys::socket::{bind, SockaddrIn, SockFlag, SockType};
|
||||
use nix::sys::socket::{getsockname, setsockopt, socket};
|
||||
use nix::sys::socket::{recvmsg, sendmsg, ControlMessageOwned, MsgFlags};
|
||||
use nix::sys::uio::IoVec;
|
||||
@ -1351,7 +1305,7 @@ pub fn test_recv_ipv4pktinfo() {
|
||||
None,
|
||||
).expect("receive socket failed");
|
||||
bind(receive, &lo).expect("bind failed");
|
||||
let sa = getsockname(receive).expect("getsockname failed");
|
||||
let sa: SockaddrIn = getsockname(receive).expect("getsockname failed");
|
||||
setsockopt(receive, Ipv4PacketInfo, &true).expect("setsockopt failed");
|
||||
|
||||
{
|
||||
@ -1371,7 +1325,7 @@ pub fn test_recv_ipv4pktinfo() {
|
||||
let mut buf = [0u8; 8];
|
||||
let iovec = [IoVec::from_mut_slice(&mut buf)];
|
||||
let mut space = cmsg_space!(libc::in_pktinfo);
|
||||
let msg = recvmsg(
|
||||
let msg = recvmsg::<()>(
|
||||
receive,
|
||||
&iovec,
|
||||
Some(&mut space),
|
||||
@ -1422,8 +1376,8 @@ pub fn test_recv_ipv4pktinfo() {
|
||||
pub fn test_recvif() {
|
||||
use nix::net::if_::*;
|
||||
use nix::sys::socket::sockopt::{Ipv4RecvIf, Ipv4RecvDstAddr};
|
||||
use nix::sys::socket::{bind, SockFlag, SockType};
|
||||
use nix::sys::socket::{getsockname, setsockopt, socket, SockAddr};
|
||||
use nix::sys::socket::{bind, SockaddrIn, SockFlag, SockType};
|
||||
use nix::sys::socket::{getsockname, setsockopt, socket};
|
||||
use nix::sys::socket::{recvmsg, sendmsg, ControlMessageOwned, MsgFlags};
|
||||
use nix::sys::uio::IoVec;
|
||||
|
||||
@ -1440,7 +1394,7 @@ pub fn test_recvif() {
|
||||
None,
|
||||
).expect("receive socket failed");
|
||||
bind(receive, &lo).expect("bind failed");
|
||||
let sa = getsockname(receive).expect("getsockname failed");
|
||||
let sa: SockaddrIn = getsockname(receive).expect("getsockname failed");
|
||||
setsockopt(receive, Ipv4RecvIf, &true).expect("setsockopt IP_RECVIF failed");
|
||||
setsockopt(receive, Ipv4RecvDstAddr, &true).expect("setsockopt IP_RECVDSTADDR failed");
|
||||
|
||||
@ -1461,7 +1415,7 @@ pub fn test_recvif() {
|
||||
let mut buf = [0u8; 8];
|
||||
let iovec = [IoVec::from_mut_slice(&mut buf)];
|
||||
let mut space = cmsg_space!(libc::sockaddr_dl, libc::in_addr);
|
||||
let msg = recvmsg(
|
||||
let msg = recvmsg::<()>(
|
||||
receive,
|
||||
&iovec,
|
||||
Some(&mut space),
|
||||
@ -1490,11 +1444,11 @@ pub fn test_recvif() {
|
||||
},
|
||||
ControlMessageOwned::Ipv4RecvDstAddr(addr) => {
|
||||
rx_recvdstaddr = true;
|
||||
if let SockAddr::Inet(InetAddr::V4(a)) = lo {
|
||||
assert_eq!(a.sin_addr.s_addr,
|
||||
if let Some(sin) = lo.as_sockaddr_in() {
|
||||
assert_eq!(sin.as_ref().sin_addr.s_addr,
|
||||
addr.s_addr,
|
||||
"unexpected destination address (expected {}, got {})",
|
||||
a.sin_addr.s_addr,
|
||||
sin.as_ref().sin_addr.s_addr,
|
||||
addr.s_addr);
|
||||
} else {
|
||||
panic!("unexpected Sockaddr");
|
||||
@ -1535,7 +1489,7 @@ pub fn test_recvif() {
|
||||
pub fn test_recv_ipv6pktinfo() {
|
||||
use nix::net::if_::*;
|
||||
use nix::sys::socket::sockopt::Ipv6RecvPacketInfo;
|
||||
use nix::sys::socket::{bind, SockFlag, SockType};
|
||||
use nix::sys::socket::{bind, SockaddrIn6, SockFlag, SockType};
|
||||
use nix::sys::socket::{getsockname, setsockopt, socket};
|
||||
use nix::sys::socket::{recvmsg, sendmsg, ControlMessageOwned, MsgFlags};
|
||||
use nix::sys::uio::IoVec;
|
||||
@ -1553,7 +1507,7 @@ pub fn test_recv_ipv6pktinfo() {
|
||||
None,
|
||||
).expect("receive socket failed");
|
||||
bind(receive, &lo).expect("bind failed");
|
||||
let sa = getsockname(receive).expect("getsockname failed");
|
||||
let sa: SockaddrIn6 = getsockname(receive).expect("getsockname failed");
|
||||
setsockopt(receive, Ipv6RecvPacketInfo, &true).expect("setsockopt failed");
|
||||
|
||||
{
|
||||
@ -1573,7 +1527,7 @@ pub fn test_recv_ipv6pktinfo() {
|
||||
let mut buf = [0u8; 8];
|
||||
let iovec = [IoVec::from_mut_slice(&mut buf)];
|
||||
let mut space = cmsg_space!(libc::in6_pktinfo);
|
||||
let msg = recvmsg(
|
||||
let msg = recvmsg::<()>(
|
||||
receive,
|
||||
&iovec,
|
||||
Some(&mut space),
|
||||
@ -1611,7 +1565,7 @@ pub fn test_recv_ipv6pktinfo() {
|
||||
pub fn test_vsock() {
|
||||
use nix::errno::Errno;
|
||||
use nix::sys::socket::{AddressFamily, socket, bind, connect, listen,
|
||||
SockAddr, SockType, SockFlag};
|
||||
SockType, SockFlag, VsockAddr};
|
||||
use nix::unistd::{close};
|
||||
use std::thread;
|
||||
|
||||
@ -1622,12 +1576,12 @@ pub fn test_vsock() {
|
||||
.expect("socket failed");
|
||||
|
||||
// VMADDR_CID_HYPERVISOR is reserved, so we expect an EADDRNOTAVAIL error.
|
||||
let sockaddr = SockAddr::new_vsock(libc::VMADDR_CID_HYPERVISOR, port);
|
||||
assert_eq!(bind(s1, &sockaddr).err(),
|
||||
let sockaddr_hv = VsockAddr::new(libc::VMADDR_CID_HYPERVISOR, port);
|
||||
assert_eq!(bind(s1, &sockaddr_hv).err(),
|
||||
Some(Errno::EADDRNOTAVAIL));
|
||||
|
||||
let sockaddr = SockAddr::new_vsock(libc::VMADDR_CID_ANY, port);
|
||||
assert_eq!(bind(s1, &sockaddr), Ok(()));
|
||||
let sockaddr_any = VsockAddr::new(libc::VMADDR_CID_ANY, port);
|
||||
assert_eq!(bind(s1, &sockaddr_any), Ok(()));
|
||||
listen(s1, 10).expect("listen failed");
|
||||
|
||||
let thr = thread::spawn(move || {
|
||||
@ -1637,11 +1591,11 @@ pub fn test_vsock() {
|
||||
SockFlag::empty(), None)
|
||||
.expect("socket failed");
|
||||
|
||||
let sockaddr = SockAddr::new_vsock(cid, port);
|
||||
let sockaddr_host = VsockAddr::new(cid, port);
|
||||
|
||||
// The current implementation does not support loopback devices, so,
|
||||
// for now, we expect a failure on the connect.
|
||||
assert_ne!(connect(s2, &sockaddr), Ok(()));
|
||||
assert_ne!(connect(s2, &sockaddr_host), Ok(()));
|
||||
|
||||
close(s2).unwrap();
|
||||
});
|
||||
@ -1669,9 +1623,9 @@ fn test_recvmsg_timestampns() {
|
||||
SockFlag::empty(),
|
||||
None).unwrap();
|
||||
setsockopt(in_socket, sockopt::ReceiveTimestampns, &true).unwrap();
|
||||
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
|
||||
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
|
||||
let address = getsockname(in_socket).unwrap();
|
||||
let localhost = SockaddrIn::new(127, 0, 0, 1, 0);
|
||||
bind(in_socket, &localhost).unwrap();
|
||||
let address: SockaddrIn = getsockname(in_socket).unwrap();
|
||||
// Get initial time
|
||||
let time0 = SystemTime::now();
|
||||
// Send the message
|
||||
@ -1683,7 +1637,7 @@ fn test_recvmsg_timestampns() {
|
||||
let mut buffer = vec![0u8; message.len()];
|
||||
let mut cmsgspace = nix::cmsg_space!(TimeSpec);
|
||||
let iov = [IoVec::from_mut_slice(&mut buffer)];
|
||||
let r = recvmsg(in_socket, &iov, Some(&mut cmsgspace), flags).unwrap();
|
||||
let r = recvmsg::<()>(in_socket, &iov, Some(&mut cmsgspace), flags).unwrap();
|
||||
let rtime = match r.cmsgs().next() {
|
||||
Some(ControlMessageOwned::ScmTimestampns(rtime)) => rtime,
|
||||
Some(_) => panic!("Unexpected control message"),
|
||||
@ -1720,9 +1674,9 @@ fn test_recvmmsg_timestampns() {
|
||||
SockFlag::empty(),
|
||||
None).unwrap();
|
||||
setsockopt(in_socket, sockopt::ReceiveTimestampns, &true).unwrap();
|
||||
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
|
||||
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
|
||||
let address = getsockname(in_socket).unwrap();
|
||||
let localhost = SockaddrIn::from_str("127.0.0.1:0").unwrap();
|
||||
bind(in_socket, &localhost).unwrap();
|
||||
let address: SockaddrIn = getsockname(in_socket).unwrap();
|
||||
// Get initial time
|
||||
let time0 = SystemTime::now();
|
||||
// Send the message
|
||||
@ -1740,7 +1694,7 @@ fn test_recvmmsg_timestampns() {
|
||||
cmsg_buffer: Some(&mut cmsgspace),
|
||||
},
|
||||
];
|
||||
let r = recvmmsg(in_socket, &mut data, flags, None).unwrap();
|
||||
let r: Vec<RecvMsg<()>> = recvmmsg(in_socket, &mut data, flags, None).unwrap();
|
||||
let rtime = match r[0].cmsgs().next() {
|
||||
Some(ControlMessageOwned::ScmTimestampns(rtime)) => rtime,
|
||||
Some(_) => panic!("Unexpected control message"),
|
||||
@ -1783,10 +1737,10 @@ fn test_recvmsg_rxq_ovfl() {
|
||||
SockFlag::empty(),
|
||||
None).unwrap();
|
||||
|
||||
let localhost = InetAddr::new(IpAddr::new_v4(127, 0, 0, 1), 0);
|
||||
bind(in_socket, &SockAddr::new_inet(localhost)).unwrap();
|
||||
let localhost = SockaddrIn::from_str("127.0.0.1:0").unwrap();
|
||||
bind(in_socket, &localhost).unwrap();
|
||||
|
||||
let address = getsockname(in_socket).unwrap();
|
||||
let address: SockaddrIn = getsockname(in_socket).unwrap();
|
||||
connect(out_socket, &address).unwrap();
|
||||
|
||||
// Set SO_RXQ_OVFL flag.
|
||||
@ -1815,7 +1769,7 @@ fn test_recvmsg_rxq_ovfl() {
|
||||
|
||||
let iov = [IoVec::from_mut_slice(&mut buffer)];
|
||||
|
||||
match recvmsg(
|
||||
match recvmsg::<()>(
|
||||
in_socket,
|
||||
&iov,
|
||||
Some(&mut cmsgspace),
|
||||
@ -1847,7 +1801,7 @@ fn test_recvmsg_rxq_ovfl() {
|
||||
))]
|
||||
mod linux_errqueue {
|
||||
use nix::sys::socket::*;
|
||||
use super::{FromStr, SocketAddr};
|
||||
use super::FromStr;
|
||||
|
||||
// Send a UDP datagram to a bogus destination address and observe an ICMP error (v4).
|
||||
//
|
||||
@ -1953,12 +1907,8 @@ mod linux_errqueue {
|
||||
use nix::sys::uio::IoVec;
|
||||
|
||||
const MESSAGE_CONTENTS: &str = "ABCDEF";
|
||||
|
||||
let sock_addr = {
|
||||
let std_sa = SocketAddr::from_str(sa).unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
SockAddr::new_inet(inet_addr)
|
||||
};
|
||||
let std_sa = std::net::SocketAddr::from_str(sa).unwrap();
|
||||
let sock_addr = SockaddrStorage::from(std_sa);
|
||||
let sock = socket(af, SockType::Datagram, SockFlag::SOCK_CLOEXEC, None).unwrap();
|
||||
setsockopt(sock, opt, &true).unwrap();
|
||||
if let Err(e) = sendto(sock, MESSAGE_CONTENTS.as_bytes(), &sock_addr, MsgFlags::empty()) {
|
||||
@ -2006,16 +1956,14 @@ mod linux_errqueue {
|
||||
pub fn test_txtime() {
|
||||
use nix::sys::socket::{
|
||||
bind, recvmsg, sendmsg, setsockopt, socket, sockopt, ControlMessage,
|
||||
MsgFlags, SockFlag, SockType,
|
||||
MsgFlags, SockaddrIn, SockFlag, SockType,
|
||||
};
|
||||
use nix::sys::time::TimeValLike;
|
||||
use nix::time::{ClockId, clock_gettime};
|
||||
|
||||
require_kernel_version!(test_txtime, ">= 5.8");
|
||||
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:6802").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let sock_addr = SockaddrIn::from_str("127.0.0.1:6802").unwrap();
|
||||
|
||||
let ssock = socket(
|
||||
AddressFamily::Inet,
|
||||
@ -2052,5 +2000,5 @@ pub fn test_txtime() {
|
||||
|
||||
let mut rbuf = [0u8; 2048];
|
||||
let iov2 = [nix::sys::uio::IoVec::from_mut_slice(&mut rbuf)];
|
||||
recvmsg(rsock, &iov2, None, MsgFlags::empty()).unwrap();
|
||||
recvmsg::<()>(rsock, &iov2, None, MsgFlags::empty()).unwrap();
|
||||
}
|
||||
|
@ -72,14 +72,13 @@ fn test_so_buf() {
|
||||
|
||||
#[test]
|
||||
fn test_so_tcp_maxseg() {
|
||||
use std::net::SocketAddr;
|
||||
use std::net::SocketAddrV4;
|
||||
use std::str::FromStr;
|
||||
use nix::sys::socket::{accept, bind, connect, listen, InetAddr, SockAddr};
|
||||
use nix::sys::socket::{accept, bind, connect, listen, SockaddrIn};
|
||||
use nix::unistd::{close, write};
|
||||
|
||||
let std_sa = SocketAddr::from_str("127.0.0.1:4001").unwrap();
|
||||
let inet_addr = InetAddr::from_std(&std_sa);
|
||||
let sock_addr = SockAddr::new_inet(inet_addr);
|
||||
let std_sa = SocketAddrV4::from_str("127.0.0.1:4001").unwrap();
|
||||
let sock_addr = SockaddrIn::from(std_sa);
|
||||
|
||||
let rsock = socket(AddressFamily::Inet, SockType::Stream, SockFlag::empty(), SockProtocol::Tcp)
|
||||
.unwrap();
|
||||
|
Loading…
x
Reference in New Issue
Block a user