1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
extern crate libc;

use std::ffi::*;
use std::{string, ptr, mem, slice};
use virt;
use node::NodeInfo;
use domain::VirDomain;
use error::VirError;
use self::libc::funcs::c95::stdlib;

#[derive(Clone)]
pub struct Connection {
    conn: virt::virConnectPtr
}

pub enum ConnectionType {
    OPEN,
    READONLY
    //TODO: Add Auth Option
}

impl Connection {
    pub fn new(uri: String, conntype: ConnectionType) -> Result<Connection, VirError> {
        unsafe {
            let cUri = CString::new(uri).unwrap();
            let ptr: virt::virConnectPtr = match conntype {
                ConnectionType::OPEN => { virt::virConnectOpen(cUri.as_ptr()) },
                ConnectionType::READONLY => { virt::virConnectOpenReadOnly(cUri.as_ptr()) }
            };

            match ptr.is_null() {
                true => Err(VirError::new()),
                false => Ok(Connection{conn:ptr})
            }
        }
    }

    pub fn capabilities(&self) -> Result<String, VirError> {
        unsafe {
            let cap = virt::virConnectGetCapabilities(self.conn);
            match cap.is_null() {
                false => Ok(String::from_utf8_lossy(CStr::from_ptr(cap).to_bytes()).into_owned()),
                true => Err(VirError::new())
            }
        }
    }

    pub fn connection_type(&self) -> Result<String, VirError> {
        unsafe {
            let results = virt::virConnectGetType(self.conn);
            match results.is_null() {
                true => Err(VirError::new()),
                false => Ok(String::from_utf8_lossy(CStr::from_ptr(results).to_bytes()).into_owned())
            }
        }
    }

    pub fn node_info(&self) -> Result<NodeInfo, VirError> {
        unsafe {
            let nptr: virt::virNodeInfoPtr = ptr::null_mut();
            let results = virt::virNodeGetInfo(self.conn, nptr);
            match results == -1 {
                true => Err(VirError::new()),
                false => Ok(NodeInfo{ptr:nptr})
            }
        }
    }

    pub fn hostname(&self) -> Result<String, VirError> {
        unsafe {
            let name = virt::virConnectGetHostname(self.conn);
            match name.is_null() {
                true => Err(VirError::new()),
                false => Ok(String::from_utf8_lossy(CStr::from_ptr(name).to_bytes()).into_owned())
            }
        }
    }

    pub fn version(&self) -> Result<u64, VirError> {
        unsafe {
            let size = mem::size_of::<*mut u64>() as libc::size_t;
            let v: *mut u64 = stdlib::malloc(size) as *mut u64;
            match virt::virConnectGetVersion(self.conn, v) != -1 {
                true => {
                    let ver = v;
                    stdlib::free(v as *mut libc::types::common::c95::c_void);
                    Ok(ver as u64)
                },
                false => {
                    stdlib::free(v as *mut libc::types::common::c95::c_void);
                    Err(VirError::new())
                }
            }
        }
    }

    pub fn libvirt_version(&self) -> Result<u64, VirError> {
        unsafe {
            let size = mem::size_of::<*mut u64>() as libc::size_t;
            let v: *mut u64 = stdlib::malloc(size) as *mut u64;
            match virt::virConnectGetLibVersion(self.conn, v) != -1 {
                true => {
                    let ver = v;
                    stdlib::free(v as *mut libc::types::common::c95::c_void);
                    Ok(ver as u64)
                },
                false => {
                    stdlib::free(v as *mut libc::types::common::c95::c_void);
                    Err(VirError::new())
                },
            }
        }
    }

    pub fn get_type(&self) -> Result<String, VirError> {
        unsafe {
            let name = virt::virConnectGetType(self.conn);
            match name == ptr::null() {
                true => Err(VirError::new()),
                false => Ok(String::from_utf8_lossy(CStr::from_ptr(name).to_bytes()).into_owned())
            }
        }
    }

    pub fn alive(&self) -> Result<(), VirError> {
        unsafe {
            match virt::virConnectIsAlive(self.conn) != -1 {
                true => Ok(()),
                false => Err(VirError::new())
            }
        }
    }

    pub fn encrypted(&self) -> Result<(), VirError> {
        unsafe {
            match virt::virConnectIsEncrypted(self.conn) != -1 {
                true => Ok(()),
                false => Err(VirError::new())
            }
        }
    }

    pub fn secured(&self) -> Result<(), VirError> {
        unsafe {
            match virt::virConnectIsSecure(self.conn) != -1 {
                true => Ok(()),
                false => Err(VirError::new())
            }
        }
    }

    pub fn count_defined_domain(&self) -> Result<i32, VirError> {
        unsafe {
            let count = virt::virConnectNumOfDefinedDomains(self.conn);
            match count == -1 {
                true => Err(VirError::new()),
                false => Ok(count as i32)
            }
        }
    }


    pub fn count_domain(&self) -> Result<i32, VirError> {
        unsafe {
            let count = virt::virConnectNumOfDomains(self.conn);
            match count == -1 {
                true => Err(VirError::new()),
                false => Ok(count as i32)
            }
        }
    }

    pub fn domains(&self) -> Result<Vec<i32>, VirError> {
        unsafe {
            let mut ids = ptr::null_mut();

            let num = virt::virConnectListDomains(self.conn, ids, 1024);

            match num != -1 {
                true => {
                    let mut list: Vec<i32> = Vec::new();
                    let n = slice::from_raw_parts(ids, num as usize);
                    for id in n.to_vec() {
                        list.push(id as i32);
                    }
                    stdlib::free(ids as *mut libc::types::common::c95::c_void);
                    Ok(list)
                },
                false => Err(VirError::new())
            }
        }
    }

    pub fn list_interfaces(&self) -> Result<Vec<String>, VirError> {
        unsafe {
            let count = virt::virConnectNumOfInterfaces(self.conn);
            let mut names = vec![ptr::null_mut(); count as usize];

            match virt::virConnectListInterfaces(self.conn, names.as_mut_ptr(), count) != -1 {
                true => {
                    let mut list: Vec<String> = Vec::new();
                    for name in names {
                        list.push(String::from_utf8_lossy(CStr::from_ptr(name).to_bytes()).into_owned());
                    }
                    Ok(list)
                },
                false => Err(VirError::new())
            }
        }
    }

    pub fn list_all_domains(&self, flags: u32) -> Result<Vec<VirDomain>, VirError> {
        unsafe {
            let mut doms: *mut virt::virDomainPtr = ptr::null_mut();
            let num = virt::virConnectListAllDomains(self.conn, &mut doms, flags);
            match num != -1 {
                true => {
                    let mut list: Vec<VirDomain> = Vec::new();
                    let d = slice::from_raw_parts::<virt::virDomainPtr>(&*doms, num as usize);
                    for dom in d.to_vec() {
                        list.push(VirDomain{ptr: dom});
                    }
                    stdlib::free(doms as *mut libc::types::common::c95::c_void);
                    Ok(list)
                },
                false => {
                    stdlib::free(doms as *mut libc::types::common::c95::c_void);
                    Err(VirError::new())
                }
            }
        }
    }

    pub fn defined_domains(&self) -> Result<Vec<String>, VirError> {
        unsafe {
            let mut names = ptr::null_mut();
            let num = virt::virConnectListDefinedDomains(self.conn, &mut names, 1024);
            match num != -1 {
                true => {
                    let mut list: Vec<String> = Vec::new();
                    let n = slice::from_raw_parts(names, num as usize);
                    for name in n.to_vec() {
                        list.push(String::from_utf8_lossy(CStr::from_ptr(&name).to_bytes()).into_owned());
                    }
                    Ok(list)
                },
                false => Err(VirError::new())
            }
        }
    }

    pub fn list_network(&self) -> Result<Vec<String>, VirError> {
        unsafe {
            let count = virt::virConnectNumOfNetworks(self.conn);
            let mut names = vec![ptr::null_mut(); count as usize];

            match virt::virConnectListNetworks(self.conn, names.as_mut_ptr(), count) != -1 {
                true => {
                    let mut list: Vec<String> = Vec::new();
                    for name in names {
                        list.push(String::from_utf8_lossy(CStr::from_ptr(name).to_bytes()).into_owned());
                    }
                    Ok(list)
                },
                false => Err(VirError::new())
            }
        }
    }

    pub fn list_storage_pool(&self) -> Result<Vec<String>, VirError> {
        unsafe {
            let count = virt::virConnectNumOfStoragePools(self.conn);
            let mut names = vec![ptr::null_mut(); count as usize];

            match virt::virConnectListStoragePools(self.conn, names.as_mut_ptr(), count) != -1 {
                true => {
                    let mut list: Vec<String> = Vec::new();
                    for name in names {
                        list.push(String::from_utf8_lossy(CStr::from_ptr(name).to_bytes()).into_owned());
                    }
                    Ok(list)
                },
                false => Err(VirError::new())
            }
        }
    }

    pub fn lookup_domain_byid(&self, id: i32) -> Result<VirDomain, VirError>  {
        unsafe {
            let dptr = virt::virDomainLookupByID(self.conn, id);

            match dptr.is_null() {
                false => Ok(VirDomain{ptr: dptr}),
                true => Err(VirError::new())
            }

        }
    }

    pub fn lookup_domain_byname(&self, name: &str) -> Result<VirDomain, VirError> {
        unsafe {
            let dptr = virt::virDomainLookupByName(self.conn, CString::new(name).unwrap().as_ptr());
            match dptr.is_null() {
                false => Ok(VirDomain{ptr: dptr}),
                true => Err(VirError::new())
            }
        }
    }

    pub fn create_domain(&self, xml: &str, flags: u32) -> Result<VirDomain, VirError> {
        unsafe {
            let cxml = CString::new(xml).unwrap();
            let pDomain = virt::virDomainCreateXML(self.conn, cxml.as_ptr(), flags);
            match pDomain.is_null() {
                false => Ok(VirDomain{ptr: pDomain}),
                true => Err(VirError::new())
            }
        }
    }

    pub fn define_domain(&self, xml: &str) -> Result<VirDomain, VirError> {
        unsafe {
            let cxml = CString::new(xml).unwrap();
            let pDomain = virt::virDomainDefineXML(self.conn, cxml.as_ptr());

            match pDomain.is_null() {
                true => Err(VirError::new()),
                false => Ok(VirDomain{ptr: pDomain})
            }
        }
    }

    pub fn close(&self) -> Result<(), VirError> {
        unsafe {
            match virt::virConnectClose(self.conn) != -1 {
                true => Ok(()),
                false => Err(VirError::new())
            }
        }
    }
}