webrtc_util/vnet/
interface.rs1use std::net::SocketAddr;
2
3use ipnet::*;
4
5use crate::error::*;
6
7#[derive(Debug, Clone, Default)]
8pub struct Interface {
9 pub(crate) name: String,
10 pub(crate) addrs: Vec<IpNet>,
11}
12
13impl Interface {
14 pub fn new(name: String, addrs: Vec<IpNet>) -> Self {
15 Interface { name, addrs }
16 }
17
18 pub fn add_addr(&mut self, addr: IpNet) {
19 self.addrs.push(addr);
20 }
21
22 pub fn name(&self) -> &str {
23 &self.name
24 }
25 pub fn addrs(&self) -> &[IpNet] {
26 &self.addrs
27 }
28
29 pub fn convert(addr: SocketAddr, mask: Option<SocketAddr>) -> Result<IpNet> {
30 if let Some(mask) = mask {
31 Ok(IpNet::with_netmask(addr.ip(), mask.ip()).map_err(|_| Error::ErrInvalidMask)?)
32 } else {
33 Ok(IpNet::new(addr.ip(), if addr.is_ipv4() { 32 } else { 128 })
34 .expect("ipv4 should always work with prefix 32 and ipv6 with prefix 128"))
35 }
36 }
37}