2024-01-30 09:58:10 +00:00
|
|
|
use crate::error::{Error, Result};
|
2024-01-08 22:13:51 +00:00
|
|
|
use crate::sys::{XsdMessageHeader, XSD_ERROR};
|
2024-01-30 09:58:10 +00:00
|
|
|
use std::ffi::CString;
|
2024-02-23 03:52:48 +00:00
|
|
|
use std::fs::{self, metadata, File};
|
2024-01-08 20:43:16 +00:00
|
|
|
use std::io::{Read, Write};
|
|
|
|
use std::mem::size_of;
|
2024-02-23 03:52:48 +00:00
|
|
|
use std::os::unix::fs::FileTypeExt;
|
2024-02-23 04:37:53 +00:00
|
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
|
use tokio::net::UnixStream;
|
2024-01-08 20:43:16 +00:00
|
|
|
|
2024-02-23 03:52:48 +00:00
|
|
|
const XEN_BUS_PATHS: &[&str] = &["/dev/xen/xenbus", "/var/run/xenstored/socket"];
|
2024-01-08 20:43:16 +00:00
|
|
|
|
|
|
|
fn find_bus_path() -> Option<String> {
|
|
|
|
for path in XEN_BUS_PATHS {
|
|
|
|
match metadata(path) {
|
|
|
|
Ok(_) => return Some(String::from(*path)),
|
2024-01-08 22:13:51 +00:00
|
|
|
Err(_) => continue,
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-02-23 04:37:53 +00:00
|
|
|
#[async_trait::async_trait]
|
2024-02-23 03:52:48 +00:00
|
|
|
trait XsdTransport {
|
2024-02-23 04:37:53 +00:00
|
|
|
async fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()>;
|
|
|
|
async fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()>;
|
2024-02-23 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:37:53 +00:00
|
|
|
#[async_trait::async_trait]
|
2024-02-23 03:52:48 +00:00
|
|
|
impl XsdTransport for UnixStream {
|
2024-02-23 04:37:53 +00:00
|
|
|
async fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()> {
|
|
|
|
Ok(self.write_all(buf).await?)
|
2024-02-23 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:37:53 +00:00
|
|
|
async fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
|
|
|
|
self.read_exact(buf).await?;
|
|
|
|
Ok(())
|
2024-02-23 03:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct XsdFileTransport {
|
|
|
|
handle: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl XsdFileTransport {
|
|
|
|
pub fn new(path: &str) -> Result<XsdFileTransport> {
|
|
|
|
let handle = File::options().read(true).write(true).open(path)?;
|
|
|
|
Ok(XsdFileTransport { handle })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 04:37:53 +00:00
|
|
|
#[async_trait::async_trait]
|
2024-02-23 03:52:48 +00:00
|
|
|
impl XsdTransport for XsdFileTransport {
|
2024-02-23 04:37:53 +00:00
|
|
|
async fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
|
2024-02-23 03:52:48 +00:00
|
|
|
Ok(self.handle.read_exact(buf)?)
|
|
|
|
}
|
|
|
|
|
2024-02-23 04:37:53 +00:00
|
|
|
async fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()> {
|
2024-02-23 03:52:48 +00:00
|
|
|
self.handle.write_all(buf)?;
|
|
|
|
self.handle.flush()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 20:43:16 +00:00
|
|
|
pub struct XsdSocket {
|
2024-02-23 03:52:48 +00:00
|
|
|
handle: Box<dyn XsdTransport>,
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct XsdResponse {
|
|
|
|
pub header: XsdMessageHeader,
|
2024-01-08 22:13:51 +00:00
|
|
|
pub payload: Vec<u8>,
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl XsdResponse {
|
2024-01-30 09:58:10 +00:00
|
|
|
pub fn parse_string(&self) -> Result<String> {
|
2024-01-17 13:22:47 +00:00
|
|
|
Ok(CString::from_vec_with_nul(self.payload.clone())?.into_string()?)
|
2024-01-08 23:04:06 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 09:58:10 +00:00
|
|
|
pub fn parse_string_vec(&self) -> Result<Vec<String>> {
|
2024-01-08 20:43:16 +00:00
|
|
|
let mut strings: Vec<String> = Vec::new();
|
|
|
|
let mut buffer: Vec<u8> = Vec::new();
|
|
|
|
for b in &self.payload {
|
|
|
|
if *b == 0 {
|
|
|
|
let string = String::from_utf8(buffer.clone())?;
|
|
|
|
strings.push(string);
|
|
|
|
buffer.clear();
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-08 22:13:51 +00:00
|
|
|
buffer.push(*b);
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|
|
|
|
Ok(strings)
|
|
|
|
}
|
2024-01-08 22:13:51 +00:00
|
|
|
|
2024-01-30 09:58:10 +00:00
|
|
|
pub fn parse_bool(&self) -> Result<bool> {
|
2024-01-18 14:15:42 +00:00
|
|
|
Ok(true)
|
2024-01-08 22:13:51 +00:00
|
|
|
}
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl XsdSocket {
|
2024-02-23 04:37:53 +00:00
|
|
|
pub async fn open() -> Result<XsdSocket> {
|
2024-01-08 20:43:16 +00:00
|
|
|
let path = match find_bus_path() {
|
|
|
|
Some(path) => path,
|
2024-01-30 09:58:10 +00:00
|
|
|
None => return Err(Error::BusNotFound),
|
2024-01-08 20:43:16 +00:00
|
|
|
};
|
2024-02-23 03:52:48 +00:00
|
|
|
|
|
|
|
let metadata = fs::metadata(&path)?;
|
|
|
|
let file_type = metadata.file_type();
|
|
|
|
if file_type.is_socket() {
|
2024-02-23 04:37:53 +00:00
|
|
|
let stream = UnixStream::connect(&path).await?;
|
2024-02-23 03:52:48 +00:00
|
|
|
return Ok(XsdSocket {
|
|
|
|
handle: Box::new(stream),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let transport = XsdFileTransport::new(&path)?;
|
|
|
|
Ok(XsdSocket {
|
|
|
|
handle: Box::new(transport),
|
|
|
|
})
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 04:37:53 +00:00
|
|
|
pub async fn send(&mut self, tx: u32, typ: u32, buf: &[u8]) -> Result<XsdResponse> {
|
2024-01-08 20:43:16 +00:00
|
|
|
let header = XsdMessageHeader {
|
|
|
|
typ,
|
|
|
|
req: 0,
|
|
|
|
tx,
|
2024-01-08 22:13:51 +00:00
|
|
|
len: buf.len() as u32,
|
2024-01-08 20:43:16 +00:00
|
|
|
};
|
2024-02-23 03:52:48 +00:00
|
|
|
let header_bytes = bytemuck::bytes_of(&header);
|
|
|
|
let mut composed: Vec<u8> = Vec::new();
|
|
|
|
composed.extend_from_slice(header_bytes);
|
|
|
|
composed.extend_from_slice(buf);
|
2024-02-23 04:37:53 +00:00
|
|
|
self.handle.xsd_write_all(&composed).await?;
|
2024-01-08 20:43:16 +00:00
|
|
|
let mut result_buf = vec![0u8; size_of::<XsdMessageHeader>()];
|
2024-02-23 04:37:53 +00:00
|
|
|
self.handle
|
|
|
|
.xsd_read_exact(result_buf.as_mut_slice())
|
|
|
|
.await?;
|
2024-01-08 20:43:16 +00:00
|
|
|
let result_header = bytemuck::from_bytes::<XsdMessageHeader>(&result_buf);
|
|
|
|
let mut payload = vec![0u8; result_header.len as usize];
|
2024-02-23 04:37:53 +00:00
|
|
|
self.handle.xsd_read_exact(payload.as_mut_slice()).await?;
|
2024-01-08 20:43:16 +00:00
|
|
|
if result_header.typ == XSD_ERROR {
|
|
|
|
let error = CString::from_vec_with_nul(payload)?;
|
2024-01-30 09:58:10 +00:00
|
|
|
return Err(Error::ResponseError(error.into_string()?));
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|
2024-01-08 22:16:33 +00:00
|
|
|
let response = XsdResponse { header, payload };
|
2024-01-08 20:43:16 +00:00
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
|
2024-02-23 04:37:53 +00:00
|
|
|
pub async fn send_single(&mut self, tx: u32, typ: u32, string: &str) -> Result<XsdResponse> {
|
2024-01-09 23:40:17 +00:00
|
|
|
let text = CString::new(string)?;
|
|
|
|
let buf = text.as_bytes_with_nul();
|
2024-02-23 04:37:53 +00:00
|
|
|
self.send(tx, typ, buf).await
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|
2024-01-09 23:40:17 +00:00
|
|
|
|
2024-02-23 04:37:53 +00:00
|
|
|
pub async fn send_multiple(
|
|
|
|
&mut self,
|
|
|
|
tx: u32,
|
|
|
|
typ: u32,
|
|
|
|
array: &[&str],
|
|
|
|
) -> Result<XsdResponse> {
|
2024-01-09 23:40:17 +00:00
|
|
|
let mut buf: Vec<u8> = Vec::new();
|
|
|
|
for item in array {
|
|
|
|
buf.extend_from_slice(item.as_bytes());
|
|
|
|
buf.push(0);
|
|
|
|
}
|
2024-02-23 04:37:53 +00:00
|
|
|
self.send(tx, typ, buf.as_slice()).await
|
2024-01-09 23:40:17 +00:00
|
|
|
}
|
2024-01-08 20:43:16 +00:00
|
|
|
}
|