Skip to content
This repository was archived by the owner on May 1, 2021. It is now read-only.
Closed
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ readme = "README.md"

[target."cfg(unix)".dependencies]
libc = "0.2.77"
vsock = { version = "0.2.1", optional = true }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["handleapi", "ws2tcpip"] }
Expand Down
47 changes: 47 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ use {
winapi::um::ws2tcpip::socklen_t,
};

#[cfg(feature = "vsock")]
use vsock::VsockStream;

/// A raw socket address.
struct Addr {
storage: sockaddr_storage,
Expand Down Expand Up @@ -232,6 +235,50 @@ pub fn unix<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
unsafe { Ok(UnixStream::from_raw_fd(fd)) }
}

/// Creates a pending VSOCK connection to the specified path.
///
/// The returned stream will be in non-blocking mode and in the process of connecting to the
/// specified CID/port.
///
/// The stream becomes writable when connected.
///
/// # Examples
///
/// ```no_run
/// use polling::{Event, Poller};
/// use std::time::Duration;
///
/// // Create a pending VSOCK connection.
/// let stream = nb_connect::vsock(1, 9999)?;
///
/// // Create a poller that waits for the stream to become writable.
/// let poller = Poller::new()?;
/// poller.add(&stream, Event::writable(0))?;
///
/// // Wait for at most 1 second.
/// if poller.wait(&mut Vec::new(), Some(Duration::from_secs(1)))? == 0 {
/// println!("timeout");
/// } else {
/// println!("connected");
/// }
/// # std::io::Result::Ok(())
/// ```
#[cfg(all(unix, feature = "vsock"))]
pub fn vsock(cid: u32, port: u32) -> io::Result<VsockStream> {
let addr = unsafe {
let mut addr = mem::zeroed::<libc::sockaddr_vm>();
addr.svm_cid = cid;
addr.svm_port = port;
Addr::from_raw_parts(
&addr as *const _ as *const _,
mem::size_of::<libc::sockaddr_vm>() as u32,
)
};

let fd = connect(addr, libc::AF_VSOCK, 0)?;
unsafe { Ok(VsockStream::from_raw_fd(fd)) }
}

/// Creates a pending TCP connection to the specified address.
///
/// The returned TCP stream will be in non-blocking mode and in the process of connecting to the
Expand Down