diff options
author | Haibo Huang <hhb@google.com> | 2021-02-09 23:59:55 -0800 |
---|---|---|
committer | Haibo Huang <hhb@google.com> | 2021-02-09 23:59:55 -0800 |
commit | efd766e4f6509a71dc88162251d17381dba22a88 (patch) | |
tree | f9f41bab9dbe13a435a5b620c904470f1eb9053c /src/lib.rs | |
parent | e11f231f09baa820f4f3f40ee130ac663321c83e (diff) | |
download | vsock-efd766e4f6509a71dc88162251d17381dba22a88.tar.gz |
Upgrade rust/crates/vsock to 0.2.3
Test: make
Change-Id: I3dcb8b2d7eced30b99d792c42bc7ca3a375ed1d7
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -18,15 +18,16 @@ //! Virtio socket support for Rust. use std::io::{Error, ErrorKind, Read, Result, Write}; -use std::mem::size_of; +use std::mem::{self, size_of}; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use libc::*; -use nix::sys::socket::{SockAddr, VsockAddr}; use std::ffi::c_void; use std::net::Shutdown; use std::time::Duration; +pub use nix::sys::socket::{SockAddr, VsockAddr}; + fn new_socket() -> libc::c_int { unsafe { socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0) } } @@ -88,6 +89,11 @@ impl VsockListener { Ok(Self { socket }) } + /// Create a new VsockListener with specified cid and port. + pub fn bind_with_cid_port(cid: u32, port: u32) -> Result<VsockListener> { + Self::bind(&SockAddr::Vsock(VsockAddr::new(cid, port))) + } + /// The local socket address of the listener. pub fn local_addr(&self) -> Result<SockAddr> { let mut vsock_addr = sockaddr_vm { @@ -198,7 +204,9 @@ impl FromRawFd for VsockListener { impl IntoRawFd for VsockListener { fn into_raw_fd(self) -> RawFd { - self.socket + let fd = self.socket; + mem::forget(self); + fd } } @@ -244,6 +252,11 @@ impl VsockStream { } } + /// Open a connection to a remote host with specified cid and port. + pub fn connect_with_cid_port(cid: u32, port: u32) -> Result<Self> { + Self::connect(&SockAddr::Vsock(VsockAddr::new(cid, port))) + } + /// Virtio socket address of the remote peer associated with this connection. pub fn peer_addr(&self) -> Result<SockAddr> { let mut vsock_addr = sockaddr_vm { @@ -393,6 +406,8 @@ impl VsockStream { )); } + // https://github.com/rust-lang/libc/issues/1848 + #[cfg_attr(target_env = "musl", allow(deprecated))] let secs = if dur.as_secs() > time_t::max_value() as u64 { time_t::max_value() } else { @@ -478,7 +493,9 @@ impl FromRawFd for VsockStream { impl IntoRawFd for VsockStream { fn into_raw_fd(self) -> RawFd { - self.socket + let fd = self.socket; + mem::forget(self); + fd } } |