changed the is_multi_broadcast

This commit is contained in:
asxalex 2024-10-06 15:35:51 +08:00
parent dd5610addc
commit ea5d463216
3 changed files with 22 additions and 12 deletions

View File

@ -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<SdlanSock> {
}
}
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 {

View File

@ -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)]

5
src/utils/mytype.rs Normal file
View File

@ -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 */