added crc after the ether packet
This commit is contained in:
parent
1a5e1d8be3
commit
6dd3d8694c
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1433,6 +1433,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
|||||||
name = "sdlan-rs"
|
name = "sdlan-rs"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"crc",
|
||||||
"dashmap 6.1.0",
|
"dashmap 6.1.0",
|
||||||
"dns-lookup",
|
"dns-lookup",
|
||||||
"etherparse",
|
"etherparse",
|
||||||
|
|||||||
@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
crc = "3.2.1"
|
||||||
dashmap = "6.0.1"
|
dashmap = "6.0.1"
|
||||||
dns-lookup = "2.0.4"
|
dns-lookup = "2.0.4"
|
||||||
etherparse = "0.15.0"
|
etherparse = "0.15.0"
|
||||||
|
|||||||
@ -13,6 +13,8 @@ use tokio::sync::{
|
|||||||
oneshot,
|
oneshot,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::utils::CRC_HASH;
|
||||||
|
|
||||||
use super::{get_edge, get_route_table};
|
use super::{get_edge, get_route_table};
|
||||||
|
|
||||||
static GLOBAL_ARP: OnceCell<ArpActor> = OnceCell::new();
|
static GLOBAL_ARP: OnceCell<ArpActor> = OnceCell::new();
|
||||||
@ -87,7 +89,7 @@ impl ArpHdr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn marshal_to_bytes(&self) -> Vec<u8> {
|
pub fn marshal_to_bytes(&self) -> Vec<u8> {
|
||||||
let mut result = Vec::with_capacity(42);
|
let mut result = Vec::with_capacity(64);
|
||||||
result.extend_from_slice(&self.ethhdr.dest);
|
result.extend_from_slice(&self.ethhdr.dest);
|
||||||
result.extend_from_slice(&self.ethhdr.src);
|
result.extend_from_slice(&self.ethhdr.src);
|
||||||
result.extend_from_slice(&self.ethhdr.eth_type.to_be_bytes());
|
result.extend_from_slice(&self.ethhdr.eth_type.to_be_bytes());
|
||||||
@ -103,6 +105,10 @@ impl ArpHdr {
|
|||||||
result.extend_from_slice(&self.dhwaddr);
|
result.extend_from_slice(&self.dhwaddr);
|
||||||
result.extend_from_slice(&self.dipaddr[0].to_be_bytes());
|
result.extend_from_slice(&self.dipaddr[0].to_be_bytes());
|
||||||
result.extend_from_slice(&self.dipaddr[1].to_be_bytes());
|
result.extend_from_slice(&self.dipaddr[1].to_be_bytes());
|
||||||
|
result.extend_from_slice(&[0; 18]);
|
||||||
|
|
||||||
|
let crc = CRC_HASH.checksum(&result).to_be_bytes();
|
||||||
|
result.extend_from_slice(&crc);
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,7 +14,7 @@ use crate::pb::{
|
|||||||
SdlRegisterSuperAck, SdlRegisterSuperNak, SdlSendRegisterEvent, SdlStunRequest, Sdlv6Info,
|
SdlRegisterSuperAck, SdlRegisterSuperNak, SdlSendRegisterEvent, SdlStunRequest, Sdlv6Info,
|
||||||
};
|
};
|
||||||
use crate::tcp::{init_tcp_conn, EventType, NakMsgCode, PacketType, SdlanTcp};
|
use crate::tcp::{init_tcp_conn, EventType, NakMsgCode, PacketType, SdlanTcp};
|
||||||
use crate::utils::{send_to_sock, CommandLine};
|
use crate::utils::{send_to_sock, CommandLine, CRC_HASH};
|
||||||
use crate::ConnectionState;
|
use crate::ConnectionState;
|
||||||
use etherparse::ether_type::ARP;
|
use etherparse::ether_type::ARP;
|
||||||
use etherparse::{Ethernet2Header, IpHeaders};
|
use etherparse::{Ethernet2Header, IpHeaders};
|
||||||
@ -661,12 +661,14 @@ async fn edge_send_packet_to_net(eee: &Node, data: &[u8]) {
|
|||||||
etherheader.destination = mac;
|
etherheader.destination = mac;
|
||||||
etherheader.ether_type = etherparse::EtherType::IPV4;
|
etherheader.ether_type = etherparse::EtherType::IPV4;
|
||||||
etherheader.source = src_mac;
|
etherheader.source = src_mac;
|
||||||
let mut packet = Vec::with_capacity(14 + data.len());
|
let mut packet = Vec::with_capacity(14 + data.len() + 4);
|
||||||
packet.extend_from_slice(ðerheader.to_bytes()[..]);
|
packet.extend_from_slice(ðerheader.to_bytes()[..]);
|
||||||
packet.extend_from_slice(&data);
|
packet.extend_from_slice(&data);
|
||||||
|
let crc = CRC_HASH.checksum(&packet);
|
||||||
|
packet.extend_from_slice(&crc.to_be_bytes());
|
||||||
|
|
||||||
let pkt_size = packet.len();
|
let pkt_size = packet.len();
|
||||||
println!("sending data with mac");
|
// println!("sending data with mac");
|
||||||
|
|
||||||
let Ok(encrypted) = aes_encrypt(&encrypt_key, &packet) else {
|
let Ok(encrypted) = aes_encrypt(&encrypt_key, &packet) else {
|
||||||
error!("failed to encrypt packet request");
|
error!("failed to encrypt packet request");
|
||||||
|
|||||||
@ -4,7 +4,10 @@ use std::{
|
|||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{network::send_packet_to_net, utils::mac_to_string};
|
use crate::{
|
||||||
|
network::send_packet_to_net,
|
||||||
|
utils::{mac_to_string, CRC_HASH},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{NULL_MAC, REGISTER_INTERVAL},
|
config::{NULL_MAC, REGISTER_INTERVAL},
|
||||||
@ -767,6 +770,17 @@ async fn handle_tun_packet(
|
|||||||
debug!("got packet from sock, will send to tun");
|
debug!("got packet from sock, will send to tun");
|
||||||
match Ethernet2Header::from_slice(&data) {
|
match Ethernet2Header::from_slice(&data) {
|
||||||
Ok((hdr, rest)) => {
|
Ok((hdr, rest)) => {
|
||||||
|
if rest.len() < 4 {
|
||||||
|
error!("payload length error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let crc_code = &rest[(rest.len() - 4)..rest.len()];
|
||||||
|
let rest = &rest[..(rest.len() - 4)];
|
||||||
|
let crc_hash: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);
|
||||||
|
let ck = CRC_HASH.checksum(&data[..(data.len()) - 4]);
|
||||||
|
let sent_ck = u32::from_be_bytes(crc_code.try_into().unwrap());
|
||||||
|
debug!("ck = {}, sent_ck = {}", ck, sent_ck);
|
||||||
|
|
||||||
debug!("ip size is {}", rest.len());
|
debug!("ip size is {}", rest.len());
|
||||||
let edge = get_edge();
|
let edge = get_edge();
|
||||||
let self_mac = edge.device_config.get_mac();
|
let self_mac = edge.device_config.get_mac();
|
||||||
@ -868,7 +882,7 @@ async fn handle_tun_packet(
|
|||||||
debug!("send to tun {} bytes", size);
|
debug!("send to tun {} bytes", size);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("failed to send to device");
|
error!("failed to send to device: {}", e.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// edge.tun.send_data_to_tun(Vec::from(hdr.1)).await;
|
// edge.tun.send_data_to_tun(Vec::from(hdr.1)).await;
|
||||||
|
|||||||
@ -9,6 +9,8 @@ pub use socks::*;
|
|||||||
mod pid_recorder;
|
mod pid_recorder;
|
||||||
pub use pid_recorder::PidRecorder;
|
pub use pid_recorder::PidRecorder;
|
||||||
|
|
||||||
|
pub const CRC_HASH: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);
|
||||||
|
|
||||||
pub fn mac_to_string(mac: &Mac) -> String {
|
pub fn mac_to_string(mac: &Mac) -> String {
|
||||||
format!(
|
format!(
|
||||||
"[{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}]",
|
"[{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}]",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user