From ea5d463216674a27f454e257df646e4d4413a970 Mon Sep 17 00:00:00 2001 From: asxalex Date: Sun, 6 Oct 2024 15:35:51 +0800 Subject: [PATCH] changed the is_multi_broadcast --- src/utils/helper.rs | 27 +++++++++++++++------------ src/utils/mod.rs | 2 ++ src/utils/mytype.rs | 5 +++++ 3 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 src/utils/mytype.rs diff --git a/src/utils/helper.rs b/src/utils/helper.rs index 28e64c6..c337f45 100644 --- a/src/utils/helper.rs +++ b/src/utils/helper.rs @@ -49,7 +49,7 @@ pub fn get_current_timestamp() -> u64 { use crate::peer::SdlanSock; -use super::{gen_uuid, Result}; +use super::{gen_uuid, Mac, Result, BROADCAST_MAC, IPV6_MULTICAST_MAC, MULTICAST_MAC}; use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; @@ -103,21 +103,24 @@ pub fn get_sdlan_sock_from_socketaddr(addr: SocketAddr) -> Result { } } -pub fn is_broadcast(ip: u32) -> bool { - ip == 0xffffffff +#[inline] +pub fn is_broadcast(mac: &Mac) -> bool { + *mac == BROADCAST_MAC } -pub fn is_multicast(ip: u32) -> bool { - let first = ((ip >> 24) & 0xff) as u8; - first >= 224 && first <= 239 +#[inline] +pub fn is_multicast(mac: &Mac) -> bool { + (mac[..3] == MULTICAST_MAC[..3]) && ((mac[3] >> 7) != 0x01) } -pub fn is_multi_broadcast(ip: u32) -> bool { - if ip == 0xffffffff { - return true; - } - let first = ((ip >> 24) & 0xff) as u8; - first >= 224 && first <= 239 +#[inline] +pub fn is_ipv6_multicast(mac: &Mac) -> bool { + mac[..2] == IPV6_MULTICAST_MAC[..2] +} + +#[inline] +pub fn is_multi_broadcast(mac: &Mac) -> bool { + is_broadcast(mac) || is_multicast(mac) || is_ipv6_multicast(mac) } pub fn ip_to_string(ip: &u32) -> String { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e301714..3c5a576 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,6 +3,7 @@ mod error; mod helper; mod myaes; mod myrsa; +mod mytype; mod myuuid; pub use encode_decode::*; @@ -13,6 +14,7 @@ pub use myrsa::{ gen_rsa_keys, load_private_key_file, load_public_key, load_public_key_file, rsa_decrypt, rsa_encrypt, }; +pub use mytype::*; pub use myuuid::*; #[cfg(test)] diff --git a/src/utils/mytype.rs b/src/utils/mytype.rs new file mode 100644 index 0000000..409544a --- /dev/null +++ b/src/utils/mytype.rs @@ -0,0 +1,5 @@ +pub type Mac = [u8; 6]; + +pub const BROADCAST_MAC: Mac = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; +pub const MULTICAST_MAC: Mac = [0x01, 0x00, 0x5E, 0x00, 0x00, 0x00]; /* First 3 bytes are meaningful */ +pub const IPV6_MULTICAST_MAC: Mac = [0x33, 0x33, 0x00, 0x00, 0x00, 0x00]; /* First 2 bytes are meaningful */