virtualization_rs/virtualization/
network_device.rs

1//! network device module
2
3use crate::base::{Id, NSString};
4
5use objc::rc::StrongPtr;
6use objc::{class, msg_send, sel, sel_impl};
7
8/// common behaviors for network device attachment
9pub trait VZNetworkDeviceAttachment {
10    fn id(&self) -> Id;
11}
12
13/// configure of NAT network device attachment
14pub struct VZNATNetworkDeviceAttachment(StrongPtr);
15
16impl VZNATNetworkDeviceAttachment {
17    pub fn new() -> VZNATNetworkDeviceAttachment {
18        unsafe {
19            let p = StrongPtr::new(msg_send![class!(VZNATNetworkDeviceAttachment), new]);
20            VZNATNetworkDeviceAttachment(p)
21        }
22    }
23}
24
25impl VZNetworkDeviceAttachment for VZNATNetworkDeviceAttachment {
26    fn id(&self) -> Id {
27        *self.0
28    }
29}
30
31/// common behaviors for bridge network interface
32pub trait VZBridgedNetworkInterface {
33    fn id(&self) -> Id;
34    fn localized_display_name(&self) -> NSString {
35        let _obj = self.id();
36        let p = unsafe { StrongPtr::retain(msg_send![class!(_obj), localizedDisplayName]) };
37        NSString(p)
38    }
39    fn identifier(&self) -> NSString {
40        let _obj = self.id();
41        let p = unsafe { StrongPtr::retain(msg_send![class!(_obj), identifier]) };
42        NSString(p)
43    }
44}
45
46/// configure of bridge network device attachment
47pub struct VZBridgedNetworkDeviceAttachment(StrongPtr);
48
49impl VZBridgedNetworkDeviceAttachment {
50    pub fn new<T: VZBridgedNetworkInterface>(interface: T) -> VZBridgedNetworkDeviceAttachment {
51        unsafe {
52            let obj: Id = msg_send![class!(VZBridgedNetworkDeviceAttachment), alloc];
53            let p = StrongPtr::new(msg_send![obj, initWithInterface:interface.id()]);
54            VZBridgedNetworkDeviceAttachment(p)
55        }
56    }
57}
58
59impl VZNetworkDeviceAttachment for VZBridgedNetworkDeviceAttachment {
60    fn id(&self) -> Id {
61        *self.0
62    }
63}
64
65/// MAC address
66pub struct VZMACAddress(pub StrongPtr);
67
68impl VZMACAddress {
69    pub fn new() -> VZMACAddress {
70        let p = unsafe { StrongPtr::new(msg_send![class!(VZMACAddress), new]) };
71        VZMACAddress(p)
72    }
73    pub fn random_locally_administered_address() -> VZMACAddress {
74        let p = unsafe {
75            StrongPtr::new(msg_send![
76                class!(VZMACAddress),
77                randomLocallyAdministeredAddress
78            ])
79        };
80        VZMACAddress(p)
81    }
82
83    pub fn init_with_string(s: &str) -> VZMACAddress {
84        let string = NSString::new(s);
85        let p =
86            unsafe { StrongPtr::new(msg_send![class!(VZMACAddress), initWithString:*string.0]) };
87        VZMACAddress(p)
88    }
89}
90
91/// common configure of network device
92pub trait VZNetworkDeviceConfiguration {
93    fn id(&self) -> Id;
94}
95
96/// configure of network device through the Virtio interface
97pub struct VZVirtioNetworkDeviceConfiguration(StrongPtr);
98
99impl VZVirtioNetworkDeviceConfiguration {
100    pub fn new<T: VZNetworkDeviceAttachment>(attachment: T) -> VZVirtioNetworkDeviceConfiguration {
101        unsafe {
102            let p = StrongPtr::new(msg_send![class!(VZVirtioNetworkDeviceConfiguration), new]);
103            let _: Id = msg_send![*p, setAttachment:attachment.id()];
104            VZVirtioNetworkDeviceConfiguration(p)
105        }
106    }
107
108    pub fn set_attachment<T: VZNetworkDeviceAttachment>(&mut self, attachment: T) {
109        unsafe {
110            let _: Id = msg_send![*self.0, setAttachment:attachment.id()];
111        }
112    }
113
114    pub fn set_mac_address(&mut self, mac: VZMACAddress) {
115        unsafe {
116            let _: Id = msg_send![*self.0, setMACAddress:*mac.0];
117        }
118    }
119}
120
121impl VZNetworkDeviceConfiguration for VZVirtioNetworkDeviceConfiguration {
122    fn id(&self) -> Id {
123        *self.0
124    }
125}