解决不同features编译时告警的问题

This commit is contained in:
lubeilin
2023-10-08 17:46:31 +08:00
parent dacce892ff
commit 9df34207f7
11 changed files with 178 additions and 56 deletions
+15
View File
@@ -483,6 +483,21 @@ fn print_usage(program: &str, _opts: Options) {
println!(" -i <in-ip> 配置点对网(IP代理)时使用,-i 192.168.0.0/24,10.26.0.3表示允许接收网段192.168.0.0/24的数据"); println!(" -i <in-ip> 配置点对网(IP代理)时使用,-i 192.168.0.0/24,10.26.0.3表示允许接收网段192.168.0.0/24的数据");
println!(" 并转发到10.26.0.3,可指定多个网段"); println!(" 并转发到10.26.0.3,可指定多个网段");
println!(" -o <out-ip> 配置点对网时使用,-o 192.168.0.0/24表示允许将数据转发到192.168.0.0/24,可指定多个网段"); println!(" -o <out-ip> 配置点对网时使用,-o 192.168.0.0/24表示允许将数据转发到192.168.0.0/24,可指定多个网段");
#[cfg(not(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
)))]
let enums = String::new();
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
let mut enums = String::new(); let mut enums = String::new();
#[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))] #[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))]
enums.push_str("/aes_gcm"); enums.push_str("/aes_gcm");
+1 -1
View File
@@ -53,5 +53,5 @@ aes_cbc=["cbc"]
aes_ecb=["ecb"] aes_ecb=["ecb"]
sm4_cbc=["libsm"] sm4_cbc=["libsm"]
aes_gcm=["aes-gcm"] aes_gcm=["aes-gcm"]
server_encrypt =["rsa","spki"] server_encrypt =["aes-gcm","rsa","spki"]
ip_proxy=["dashmap"] ip_proxy=["dashmap"]
+2 -7
View File
@@ -298,12 +298,7 @@ impl Context {
} }
fn get_udp_by_route(&self, route_key: &RouteKey) -> Option<Arc<UdpSocket>> { fn get_udp_by_route(&self, route_key: &RouteKey) -> Option<Arc<UdpSocket>> {
let guard = &crossbeam_epoch::pin(); let guard = &crossbeam_epoch::pin();
let udp_map = unsafe { let udp_map = unsafe { self.inner.udp_map.load(Ordering::Relaxed, guard).deref() };
self.inner
.udp_map
.load(Ordering::Relaxed, guard)
.deref()
};
udp_map.get(&route_key.index).cloned() udp_map.get(&route_key.index).cloned()
} }
@@ -500,7 +495,7 @@ impl Context {
table_share = e.current; table_share = e.current;
} }
} }
}else{ } else {
return; return;
} }
} }
+108 -2
View File
@@ -15,8 +15,22 @@ use crate::cipher::openssl_aes_ecb::AesEcbCipher;
use crate::cipher::ring_aes_gcm_cipher::AesGcmCipher; use crate::cipher::ring_aes_gcm_cipher::AesGcmCipher;
#[cfg(feature = "sm4_cbc")] #[cfg(feature = "sm4_cbc")]
use crate::cipher::sm4_cbc::Sm4CbcCipher; use crate::cipher::sm4_cbc::Sm4CbcCipher;
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
use crate::cipher::Finger; use crate::cipher::Finger;
use crate::protocol::NetPacket; use crate::protocol::NetPacket;
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
use sha2::Digest; use sha2::Digest;
use std::io; use std::io;
use std::str::FromStr; use std::str::FromStr;
@@ -38,6 +52,21 @@ impl FromStr for CipherModel {
type Err = String; type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
#[cfg(not(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
)))]
return Err(format!("not match '{}', no encrypt", s));
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
match s.to_lowercase().trim() { match s.to_lowercase().trim() {
#[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))] #[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))]
"aes_gcm" => Ok(CipherModel::AesGcm), "aes_gcm" => Ok(CipherModel::AesGcm),
@@ -49,7 +78,7 @@ impl FromStr for CipherModel {
"sm4_cbc" => Ok(CipherModel::Sm4Cbc), "sm4_cbc" => Ok(CipherModel::Sm4Cbc),
_ => { _ => {
let mut enums = String::new(); let mut enums = String::new();
#[cfg(feature = "aes_gcm")] #[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))]
enums.push_str("/aes_gcm"); enums.push_str("/aes_gcm");
#[cfg(feature = "aes_cbc")] #[cfg(feature = "aes_cbc")]
enums.push_str("/aes_cbc"); enums.push_str("/aes_cbc");
@@ -80,8 +109,28 @@ pub enum Cipher {
Sm4Cbc(Sm4CbcCipher), Sm4Cbc(Sm4CbcCipher),
None, None,
} }
impl Cipher { impl Cipher {
#[cfg(not(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
)))]
pub fn new_password(
_model: CipherModel,
_password: Option<String>,
_token: Option<String>,
) -> Self {
Cipher::None
}
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
pub fn new_password( pub fn new_password(
model: CipherModel, model: CipherModel,
password: Option<String>, password: Option<String>,
@@ -134,6 +183,23 @@ impl Cipher {
Cipher::None Cipher::None
} }
} }
#[cfg(not(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
)))]
pub fn new_key(_key: [u8; 32], _token: String) -> io::Result<Self> {
Err(io::Error::new(io::ErrorKind::Other, "key error"))
}
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
pub fn new_key(key: [u8; 32], token: String) -> io::Result<Self> { pub fn new_key(key: [u8; 32], token: String) -> io::Result<Self> {
let finger = Some(Finger::new(&token)); let finger = Some(Finger::new(&token));
match key.len() { match key.len() {
@@ -171,6 +237,26 @@ impl Cipher {
} }
} }
} }
#[cfg(not(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
)))]
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
&self,
_net_packet: &mut NetPacket<B>,
) -> io::Result<()> {
Ok(())
}
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>( pub fn encrypt_ipv4<B: AsRef<[u8]> + AsMut<[u8]>>(
&self, &self,
net_packet: &mut NetPacket<B>, net_packet: &mut NetPacket<B>,
@@ -187,6 +273,26 @@ impl Cipher {
Cipher::None => Ok(()), Cipher::None => Ok(()),
} }
} }
#[cfg(not(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
)))]
pub fn check_finger<B: AsRef<[u8]> + AsMut<[u8]>>(
&self,
_net_packet: &NetPacket<B>,
) -> io::Result<()> {
Ok(())
}
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
pub fn check_finger<B: AsRef<[u8]>>(&self, net_packet: &NetPacket<B>) -> io::Result<()> { pub fn check_finger<B: AsRef<[u8]>>(&self, net_packet: &NetPacket<B>) -> io::Result<()> {
match self { match self {
#[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))] #[cfg(any(feature = "aes_gcm", feature = "server_encrypt"))]
+14
View File
@@ -7,6 +7,13 @@ mod aes_ecb;
#[cfg(not(feature = "ring-cipher"))] #[cfg(not(feature = "ring-cipher"))]
mod aes_gcm_cipher; mod aes_gcm_cipher;
mod cipher; mod cipher;
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
mod finger; mod finger;
#[cfg(feature = "aes_ecb")] #[cfg(feature = "aes_ecb")]
#[cfg(any(feature = "openssl-vendored", feature = "openssl"))] #[cfg(any(feature = "openssl-vendored", feature = "openssl"))]
@@ -19,5 +26,12 @@ mod rsa_cipher;
mod sm4_cbc; mod sm4_cbc;
pub use cipher::Cipher; pub use cipher::Cipher;
pub use cipher::CipherModel; pub use cipher::CipherModel;
#[cfg(any(
feature = "aes_gcm",
feature = "server_encrypt",
feature = "aes_cbc",
feature = "aes_ecb",
feature = "sm4_cbc"
))]
pub use finger::Finger; pub use finger::Finger;
pub use rsa_cipher::RsaCipher; pub use rsa_cipher::RsaCipher;
+12 -11
View File
@@ -1,10 +1,10 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::net::TcpStream; use std::net::TcpStream;
use std::net::UdpSocket; use std::net::UdpSocket;
use std::sync::Arc; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use crossbeam_epoch::Atomic; use crossbeam_epoch::Atomic;
@@ -13,25 +13,25 @@ use parking_lot::Mutex;
use rand::Rng; use rand::Rng;
use tokio::sync::mpsc::channel; use tokio::sync::mpsc::channel;
use crate::channel::{Route, RouteKey};
use crate::channel::channel::{Channel, Context}; use crate::channel::channel::{Channel, Context};
use crate::channel::idle::Idle; use crate::channel::idle::Idle;
use crate::channel::punch::{NatInfo, Punch, PunchModel}; use crate::channel::punch::{NatInfo, Punch, PunchModel};
use crate::channel::sender::ChannelSender; use crate::channel::sender::ChannelSender;
use crate::channel::{Route, RouteKey};
use crate::cipher::{Cipher, CipherModel, RsaCipher}; use crate::cipher::{Cipher, CipherModel, RsaCipher};
use crate::core::status::VntStatusManger; use crate::core::status::VntStatusManger;
use crate::error::Error; use crate::error::Error;
use crate::external_route::{AllowExternalRoute, ExternalRoute}; use crate::external_route::{AllowExternalRoute, ExternalRoute};
use crate::handle::{
ConnectStatus, CurrentDeviceInfo, handshake_handler, heartbeat_handler, PeerDeviceInfo,
punch_handler, registration_handler,
};
use crate::handle::handshake_handler::HandshakeEnum; use crate::handle::handshake_handler::HandshakeEnum;
use crate::handle::recv_handler::ChannelDataHandler; use crate::handle::recv_handler::ChannelDataHandler;
use crate::handle::registration_handler::{RegResponse, ReqEnum}; use crate::handle::registration_handler::{RegResponse, ReqEnum};
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
use crate::handle::tun_tap::tap_handler; use crate::handle::tun_tap::tap_handler;
use crate::handle::tun_tap::tun_handler; use crate::handle::tun_tap::tun_handler;
use crate::handle::{
handshake_handler, heartbeat_handler, punch_handler, registration_handler, ConnectStatus,
CurrentDeviceInfo, PeerDeviceInfo,
};
use crate::igmp_server::IgmpServer; use crate::igmp_server::IgmpServer;
use crate::nat::NatTest; use crate::nat::NatTest;
use crate::tun_tap_device; use crate::tun_tap_device;
@@ -88,7 +88,7 @@ impl VntUtil {
None None
}; };
let server_cipher = if config.server_encrypt { let server_cipher = if config.server_encrypt {
let mut key = [0 as u8; 32]; let mut key = [0u8; 32];
rand::thread_rng().fill(&mut key); rand::thread_rng().fill(&mut key);
Cipher::new_key(key, config.token.clone())? Cipher::new_key(key, config.token.clone())?
} else { } else {
@@ -278,7 +278,8 @@ impl VntUtil {
)); ));
let device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>> = let device_list: Arc<Mutex<(u16, Vec<PeerDeviceInfo>)>> =
Arc::new(Mutex::new((response.epoch, response.device_info_list))); Arc::new(Mutex::new((response.epoch, response.device_info_list)));
let peer_nat_info_map: Arc<Atomic<HashMap<Ipv4Addr, NatInfo>>> = Arc::new(Atomic::new(HashMap::new())); let peer_nat_info_map: Arc<Atomic<HashMap<Ipv4Addr, NatInfo>>> =
Arc::new(Atomic::new(HashMap::new()));
let connect_status = Arc::new(AtomicCell::new(ConnectStatus::Connected)); let connect_status = Arc::new(AtomicCell::new(ConnectStatus::Connected));
let public_ip = response.public_ip; let public_ip = response.public_ip;
let public_port = response.public_port; let public_port = response.public_port;
@@ -504,8 +505,8 @@ impl Vnt {
} }
pub fn peer_nat_info(&self, ip: &Ipv4Addr) -> Option<NatInfo> { pub fn peer_nat_info(&self, ip: &Ipv4Addr) -> Option<NatInfo> {
let guard = &crossbeam_epoch::pin(); let guard = &crossbeam_epoch::pin();
let shared = self.peer_nat_info_map.load(Ordering::Acquire,guard); let shared = self.peer_nat_info_map.load(Ordering::Acquire, guard);
let map = unsafe{shared.deref()}; let map = unsafe { shared.deref() };
map.get(ip).map(|e| e.clone()) map.get(ip).map(|e| e.clone())
} }
pub fn connection_status(&self) -> ConnectStatus { pub fn connection_status(&self) -> ConnectStatus {
+9 -10
View File
@@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use std::sync::Arc;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use crossbeam_epoch::{Atomic, Owned}; use crossbeam_epoch::{Atomic, Owned};
@@ -14,28 +14,28 @@ use packet::icmp::{icmp, Kind};
use packet::ip::ipv4; use packet::ip::ipv4;
use packet::ip::ipv4::packet::IpV4Packet; use packet::ip::ipv4::packet::IpV4Packet;
use crate::channel::{Route, RouteKey};
use crate::channel::channel::Context; use crate::channel::channel::Context;
use crate::channel::punch::{NatInfo, NatType}; use crate::channel::punch::{NatInfo, NatType};
use crate::channel::{Route, RouteKey};
use crate::cipher::{Cipher, RsaCipher}; use crate::cipher::{Cipher, RsaCipher};
use crate::error::Error; use crate::error::Error;
use crate::external_route::AllowExternalRoute; use crate::external_route::AllowExternalRoute;
use crate::handle::{ConnectStatus, CurrentDeviceInfo, PeerDeviceInfo, PeerDeviceStatus};
use crate::handle::handshake_handler::secret_handshake_req; use crate::handle::handshake_handler::secret_handshake_req;
use crate::handle::registration_handler::Register; use crate::handle::registration_handler::Register;
use crate::handle::{ConnectStatus, CurrentDeviceInfo, PeerDeviceInfo, PeerDeviceStatus};
use crate::igmp_server::IgmpServer; use crate::igmp_server::IgmpServer;
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")]
use crate::ip_proxy::{IpProxyMap, ProxyHandler}; use crate::ip_proxy::{IpProxyMap, ProxyHandler};
use crate::nat; use crate::nat;
use crate::nat::NatTest; use crate::nat::NatTest;
use crate::proto::message::{DeviceList, PunchInfo, PunchNatType, RegistrationResponse}; use crate::proto::message::{DeviceList, PunchInfo, PunchNatType, RegistrationResponse};
use crate::protocol::{
control_packet, ip_turn_packet, MAX_TTL, NetPacket, other_turn_packet, Protocol,
service_packet, Version,
};
use crate::protocol::body::ENCRYPTION_RESERVED; use crate::protocol::body::ENCRYPTION_RESERVED;
use crate::protocol::control_packet::ControlPacket; use crate::protocol::control_packet::ControlPacket;
use crate::protocol::error_packet::InErrorPacket; use crate::protocol::error_packet::InErrorPacket;
use crate::protocol::{
control_packet, ip_turn_packet, other_turn_packet, service_packet, NetPacket, Protocol,
Version, MAX_TTL,
};
use crate::tun_tap_device::DeviceWriter; use crate::tun_tap_device::DeviceWriter;
#[derive(Clone)] #[derive(Clone)]
@@ -71,8 +71,7 @@ impl ChannelDataHandler {
device_writer: DeviceWriter, device_writer: DeviceWriter,
connect_status: Arc<AtomicCell<ConnectStatus>>, connect_status: Arc<AtomicCell<ConnectStatus>>,
peer_nat_info_map: Arc<Atomic<HashMap<Ipv4Addr, NatInfo>>>, peer_nat_info_map: Arc<Atomic<HashMap<Ipv4Addr, NatInfo>>>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
ip_proxy_map: Option<IpProxyMap>,
out_external_route: AllowExternalRoute, out_external_route: AllowExternalRoute,
cone_sender: Sender<(Ipv4Addr, NatInfo)>, cone_sender: Sender<(Ipv4Addr, NatInfo)>,
symmetric_sender: Sender<(Ipv4Addr, NatInfo)>, symmetric_sender: Sender<(Ipv4Addr, NatInfo)>,
@@ -476,7 +475,7 @@ impl ChannelDataHandler {
let nat_map_shared = nat_map.load(Ordering::Acquire, guard); let nat_map_shared = nat_map.load(Ordering::Acquire, guard);
let mut map = unsafe { nat_map_shared.deref().clone() }; let mut map = unsafe { nat_map_shared.deref().clone() };
map.insert(source, peer_nat_info.clone()); map.insert(source, peer_nat_info.clone());
nat_map.store(Owned::new(map),Ordering::Release); nat_map.store(Owned::new(map), Ordering::Release);
unsafe { unsafe {
guard.defer_destroy(nat_map_shared); guard.defer_destroy(nat_map_shared);
} }
+1 -2
View File
@@ -98,8 +98,7 @@ pub fn base_handle(
igmp_server: &Option<IgmpServer>, igmp_server: &Option<IgmpServer>,
current_device: CurrentDeviceInfo, current_device: CurrentDeviceInfo,
ip_route: &Option<ExternalRoute>, ip_route: &Option<ExternalRoute>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")] proxy_map: &Option<IpProxyMap>,
proxy_map: &Option<IpProxyMap>,
client_cipher: &Cipher, client_cipher: &Cipher,
server_cipher: &Cipher, server_cipher: &Cipher,
) -> Result<()> { ) -> Result<()> {
+3 -6
View File
@@ -30,8 +30,7 @@ pub fn start(
igmp_server: Option<IgmpServer>, igmp_server: Option<IgmpServer>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: Option<ExternalRoute>, ip_route: Option<ExternalRoute>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
ip_proxy_map: Option<IpProxyMap>,
client_cipher: Cipher, client_cipher: Cipher,
server_cipher: Cipher, server_cipher: Cipher,
parallel: usize, parallel: usize,
@@ -138,8 +137,7 @@ fn start_simple(
igmp_server: Option<IgmpServer>, igmp_server: Option<IgmpServer>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: Option<ExternalRoute>, ip_route: Option<ExternalRoute>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
ip_proxy_map: Option<IpProxyMap>,
client_cipher: Cipher, client_cipher: Cipher,
server_cipher: Cipher, server_cipher: Cipher,
) -> io::Result<()> { ) -> io::Result<()> {
@@ -175,8 +173,7 @@ fn handle(
device_writer: &DeviceWriter, device_writer: &DeviceWriter,
sender: &ChannelSender, sender: &ChannelSender,
ip_route: &Option<ExternalRoute>, ip_route: &Option<ExternalRoute>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")] proxy_map: &Option<IpProxyMap>,
proxy_map: &Option<IpProxyMap>,
client_cipher: &Cipher, client_cipher: &Cipher,
server_cipher: &Cipher, server_cipher: &Cipher,
) -> crate::Result<()> { ) -> crate::Result<()> {
+3 -6
View File
@@ -45,8 +45,7 @@ fn handle(
igmp_server: &Option<IgmpServer>, igmp_server: &Option<IgmpServer>,
current_device: CurrentDeviceInfo, current_device: CurrentDeviceInfo,
ip_route: &Option<ExternalRoute>, ip_route: &Option<ExternalRoute>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")] proxy_map: &Option<IpProxyMap>,
proxy_map: &Option<IpProxyMap>,
client_cipher: &Cipher, client_cipher: &Cipher,
server_cipher: &Cipher, server_cipher: &Cipher,
) -> Result<()> { ) -> Result<()> {
@@ -78,8 +77,7 @@ pub fn start(
igmp_server: Option<IgmpServer>, igmp_server: Option<IgmpServer>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: Option<ExternalRoute>, ip_route: Option<ExternalRoute>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
ip_proxy_map: Option<IpProxyMap>,
client_cipher: Cipher, client_cipher: Cipher,
server_cipher: Cipher, server_cipher: Cipher,
parallel: usize, parallel: usize,
@@ -190,8 +188,7 @@ fn start_simple(
igmp_server: Option<IgmpServer>, igmp_server: Option<IgmpServer>,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>, current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
ip_route: Option<ExternalRoute>, ip_route: Option<ExternalRoute>,
#[cfg(feature = "ip_proxy")] #[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
ip_proxy_map: Option<IpProxyMap>,
client_cipher: Cipher, client_cipher: Cipher,
server_cipher: Cipher, server_cipher: Cipher,
) -> io::Result<()> { ) -> io::Result<()> {
+10 -11
View File
@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::sync::Arc;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use crossbeam_epoch::{Atomic, Owned}; use crossbeam_epoch::{Atomic, Owned};
@@ -56,7 +56,8 @@ pub struct IgmpServer {
impl IgmpServer { impl IgmpServer {
pub fn new(device_writer: DeviceWriter) -> Self { pub fn new(device_writer: DeviceWriter) -> Self {
let multicast: Arc<Atomic<HashMap<Ipv4Addr, Arc<RwLock<Multicast>>>>> = Arc::new(Atomic::new(HashMap::with_capacity(16))); let multicast: Arc<Atomic<HashMap<Ipv4Addr, Arc<RwLock<Multicast>>>>> =
Arc::new(Atomic::new(HashMap::with_capacity(16)));
std::thread::spawn(move || { std::thread::spawn(move || {
//预留以太网帧头和ip头 //预留以太网帧头和ip头
let mut buf = [0; 14 + 24 + 12]; let mut buf = [0; 14 + 24 + 12];
@@ -98,7 +99,7 @@ impl IgmpServer {
} }
pub fn load(&self, multicast_addr: &Ipv4Addr) -> Option<Arc<RwLock<Multicast>>> { pub fn load(&self, multicast_addr: &Ipv4Addr) -> Option<Arc<RwLock<Multicast>>> {
let guard = &crossbeam_epoch::pin(); let guard = &crossbeam_epoch::pin();
let multicast = unsafe{self.multicast.load(Ordering::Relaxed, guard).deref()}; let multicast = unsafe { self.multicast.load(Ordering::Relaxed, guard).deref() };
if let Some(entry) = multicast.get(multicast_addr) { if let Some(entry) = multicast.get(multicast_addr) {
Some(entry.clone()) Some(entry.clone())
} else { } else {
@@ -107,8 +108,8 @@ impl IgmpServer {
} }
pub fn handle(&self, buf: &[u8], source: Ipv4Addr) -> crate::Result<()> { pub fn handle(&self, buf: &[u8], source: Ipv4Addr) -> crate::Result<()> {
let guard = &crossbeam_epoch::pin(); let guard = &crossbeam_epoch::pin();
let multicast = unsafe{self.multicast.load(Ordering::Relaxed, guard).deref()}; let multicast = unsafe { self.multicast.load(Ordering::Relaxed, guard).deref() };
for (_,v) in multicast.iter() { for (_, v) in multicast.iter() {
let mut list = Vec::new(); let mut list = Vec::new();
let mut write_guard = v.write(); let mut write_guard = v.write();
for (ip, time) in &write_guard.members { for (ip, time) in &write_guard.members {
@@ -233,22 +234,20 @@ impl IgmpServer {
} }
Ok(()) Ok(())
} }
fn get_multicast(&self,multicast_addr:&Ipv4Addr)->Option<Arc<RwLock<Multicast>>>{ fn get_multicast(&self, multicast_addr: &Ipv4Addr) -> Option<Arc<RwLock<Multicast>>> {
let guard = &crossbeam_epoch::pin(); let guard = &crossbeam_epoch::pin();
let multicast = &self.multicast; let multicast = &self.multicast;
let table_share = multicast.load(Ordering::Acquire, guard); let table_share = multicast.load(Ordering::Acquire, guard);
unsafe { unsafe { table_share.deref().get(multicast_addr).map(|v| v.clone()) }
table_share.deref().get(multicast_addr).map(|v|v.clone())
}
} }
fn add_multicast(&self,multicast_addr:Ipv4Addr)->Arc<RwLock<Multicast>>{ fn add_multicast(&self, multicast_addr: Ipv4Addr) -> Arc<RwLock<Multicast>> {
let guard = &crossbeam_epoch::pin(); let guard = &crossbeam_epoch::pin();
let multicast = &self.multicast; let multicast = &self.multicast;
let mut table_share = multicast.load(Ordering::Acquire, guard); let mut table_share = multicast.load(Ordering::Acquire, guard);
let value = Arc::new(RwLock::new(Multicast::new())); let value = Arc::new(RwLock::new(Multicast::new()));
loop { loop {
let mut table = unsafe { table_share.deref().clone() }; let mut table = unsafe { table_share.deref().clone() };
table.insert(multicast_addr,value.clone()); table.insert(multicast_addr, value.clone());
match self.multicast.compare_exchange( match self.multicast.compare_exchange(
table_share, table_share,
Owned::new(table.clone()), Owned::new(table.clone()),