use std::ffi::CString;
use std::str;
use crate::connect::Connect;
use crate::error::Error;
#[derive(Debug)]
pub struct Interface {
ptr: Option<sys::virInterfacePtr>,
}
unsafe impl Send for Interface {}
unsafe impl Sync for Interface {}
impl Drop for Interface {
fn drop(&mut self) {
if self.ptr.is_some() {
if let Err(e) = self.free() {
panic!("Unable to drop memory for Interface: {e}")
}
}
}
}
impl Clone for Interface {
fn clone(&self) -> Self {
self.add_ref().unwrap()
}
}
impl Interface {
pub unsafe fn from_ptr(ptr: sys::virInterfacePtr) -> Interface {
Interface { ptr: Some(ptr) }
}
fn add_ref(&self) -> Result<Interface, Error> {
unsafe {
if sys::virInterfaceRef(self.as_ptr()) == -1 {
return Err(Error::last_error());
}
}
Ok(unsafe { Interface::from_ptr(self.as_ptr()) })
}
pub fn as_ptr(&self) -> sys::virInterfacePtr {
self.ptr.unwrap()
}
pub fn get_connect(&self) -> Result<Connect, Error> {
let ptr = unsafe { sys::virInterfaceGetConnect(self.as_ptr()) };
if ptr.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { Connect::from_ptr(ptr) })
}
pub fn lookup_by_name(conn: &Connect, id: &str) -> Result<Interface, Error> {
let id_buf = CString::new(id).unwrap();
let ptr = unsafe { sys::virInterfaceLookupByName(conn.as_ptr(), id_buf.as_ptr()) };
if ptr.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { Interface::from_ptr(ptr) })
}
pub fn define_xml(conn: &Connect, xml: &str, flags: u32) -> Result<Interface, Error> {
let xml_buf = CString::new(xml).unwrap();
let ptr = unsafe {
sys::virInterfaceDefineXML(conn.as_ptr(), xml_buf.as_ptr(), flags as libc::c_uint)
};
if ptr.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { Interface::from_ptr(ptr) })
}
pub fn lookup_by_mac_string(conn: &Connect, id: &str) -> Result<Interface, Error> {
let id_buf = CString::new(id).unwrap();
let ptr = unsafe { sys::virInterfaceLookupByMACString(conn.as_ptr(), id_buf.as_ptr()) };
if ptr.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { Interface::from_ptr(ptr) })
}
pub fn get_name(&self) -> Result<String, Error> {
let n = unsafe { sys::virInterfaceGetName(self.as_ptr()) };
if n.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(n, nofree) })
}
pub fn get_mac_string(&self) -> Result<String, Error> {
let mac = unsafe { sys::virInterfaceGetMACString(self.as_ptr()) };
if mac.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(mac, nofree) })
}
pub fn get_xml_desc(&self, flags: sys::virInterfaceXMLFlags) -> Result<String, Error> {
let xml = unsafe { sys::virInterfaceGetXMLDesc(self.as_ptr(), flags) };
if xml.is_null() {
return Err(Error::last_error());
}
Ok(unsafe { c_chars_to_string!(xml) })
}
pub fn create(&self, flags: sys::virInterfaceXMLFlags) -> Result<u32, Error> {
let ret = unsafe { sys::virInterfaceCreate(self.as_ptr(), flags) };
if ret == -1 {
return Err(Error::last_error());
}
Ok(ret as u32)
}
pub fn destroy(&self, flags: u32) -> Result<(), Error> {
let ret = unsafe { sys::virInterfaceDestroy(self.as_ptr(), flags) };
if ret == -1 {
return Err(Error::last_error());
}
Ok(())
}
pub fn undefine(&self) -> Result<(), Error> {
let ret = unsafe { sys::virInterfaceUndefine(self.as_ptr()) };
if ret == -1 {
return Err(Error::last_error());
}
Ok(())
}
pub fn free(&mut self) -> Result<(), Error> {
let ret = unsafe { sys::virInterfaceFree(self.as_ptr()) };
if ret == -1 {
return Err(Error::last_error());
}
self.ptr = None;
Ok(())
}
pub fn is_active(&self) -> Result<bool, Error> {
let ret = unsafe { sys::virInterfaceIsActive(self.as_ptr()) };
if ret == -1 {
return Err(Error::last_error());
}
Ok(ret == 1)
}
}