2025-01-28 15:47:24 +08:00

75 lines
1.9 KiB
Rust

use std::sync::RwLock;
use crate::utils::mac_to_string;
use sdlan_sn_rs::{peer::IpSubnet, utils::Mac};
use tracing::debug;
use crate::utils::generate_mac_address;
pub struct DeviceConfig {
pub mtu: u32,
pub mac: RwLock<Mac>,
pub ip: IpSubnet,
}
impl DeviceConfig {
pub fn new(mtu: u32) -> Self {
let mac = generate_mac_address();
println!("self mac: {}", mac_to_string(&mac));
debug!("self mac: {}", mac_to_string(&mac));
DeviceConfig {
mtu,
mac: RwLock::new(mac),
ip: IpSubnet::new(0, 0),
}
}
/*
pub fn set_ip(&self, net_addr: u32, net_bit_len: u8) {
if net_bit_len <= 8 || net_bit_len > 32 {
error!("configured net bit length error: {}", net_bit_len);
return;
}
self.ip.net_addr.store(net_addr, Ordering::Relaxed);
self.ip.net_bit_len.store(net_bit_len, Ordering::Relaxed);
}
*/
pub fn get_ip(&self) -> u32 {
self.ip.net_addr()
}
pub fn get_net_bit(&self) -> u8 {
self.ip.net_bit_len()
}
pub fn get_mac(&self) -> Mac {
let mac = *self.mac.read().unwrap();
mac
}
}
/// The mode in which open the virtual network adapter.
#[allow(unused)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum Mode {
/// TUN mode
///
/// The packets returned are on the IP layer (layer 3), prefixed with 4-byte header (2 bytes
/// are flags, 2 bytes are the protocol inside, eg one of
/// <https://en.wikipedia.org/wiki/EtherType#Examples>.
Tun = 1,
/// TAP mode
///
/// The packets are on the transport layer (layer 2), and start with ethernet frame header.
Tap = 2,
}
/*
pub trait TunDevice: ReadWriter {
fn name(&self) -> &str;
fn mode(&self) -> &Mode;
fn get_ip(&self) -> u32;
fn get_net_bit(&self) -> u8;
}
*/