aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 82d5092..aaeb393 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -17,15 +17,17 @@
//! Virtio socket support for Rust.
-use std::io::{Error, ErrorKind, Read, Result, Write};
-use std::mem::{self, size_of};
-use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
-
use libc::*;
+use nix::ioctl_read_bad;
use std::ffi::c_void;
+use std::fs::File;
+use std::io::{Error, ErrorKind, Read, Result, Write};
+use std::mem::{self, size_of};
use std::net::Shutdown;
+use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::time::Duration;
+pub use libc::{VMADDR_CID_ANY, VMADDR_CID_HOST, VMADDR_CID_HYPERVISOR, VMADDR_CID_LOCAL};
pub use nix::sys::socket::{SockAddr, VsockAddr};
fn new_socket() -> libc::c_int {
@@ -134,10 +136,11 @@ impl VsockListener {
};
let mut vsock_addr_len = size_of::<sockaddr_vm>() as socklen_t;
let socket = unsafe {
- accept(
+ accept4(
self.socket,
&mut vsock_addr as *mut _ as *mut sockaddr,
&mut vsock_addr_len,
+ SOCK_CLOEXEC,
)
};
if socket < 0 {
@@ -504,3 +507,22 @@ impl Drop for VsockStream {
unsafe { close(self.socket) };
}
}
+
+const IOCTL_VM_SOCKETS_GET_LOCAL_CID: usize = 0x7b9;
+ioctl_read_bad!(
+ vm_sockets_get_local_cid,
+ IOCTL_VM_SOCKETS_GET_LOCAL_CID,
+ u32
+);
+
+/// Gets the CID of the local machine.
+///
+/// Note that when calling [`VsockListener::bind`], you should generally use [`VMADDR_CID_ANY`]
+/// instead, and for making a loopback connection you should use [`VMADDR_CID_LOCAL`].
+pub fn get_local_cid() -> Result<u32> {
+ let f = File::open("/dev/vsock")?;
+ let mut cid = 0;
+ // SAFETY: the kernel only modifies the given u32 integer.
+ unsafe { vm_sockets_get_local_cid(f.as_raw_fd(), &mut cid) }?;
+ Ok(cid)
+}