fix out of boudns

This commit is contained in:
TIBERGHIEN corentin
2026-01-24 23:59:15 +01:00
parent 95c2dfe83c
commit aec686b502
5 changed files with 60 additions and 16 deletions

View File

@@ -154,8 +154,8 @@ impl MerkleNode {
match self {
MerkleNode::Chunk(_) => 0,
MerkleNode::Directory(_) => 1,
MerkleNode::Big(_) => 3,
MerkleNode::BigDirectory(_) => 4,
MerkleNode::Big(_) => 2,
MerkleNode::BigDirectory(_) => 3,
}
}

View File

@@ -11,7 +11,7 @@ pub fn parse_received_datum(
datum_length: usize,
) -> Option<([u8; 32], MerkleNode)> {
let hash_name: [u8; 32] = recevied_datum[..32].try_into().expect("error");
let value = &recevied_datum[32..datum_length];
let value = &recevied_datum[32..recevied_datum.len()];
let value_slice = value.to_vec();
println!("valueslice: {:?}, {}", value_slice, value_slice.len());
let datum_type = value_slice[0];
@@ -49,12 +49,22 @@ pub fn parse_received_datum(
}
}
BIG => {
let mut bigdir_entries: Vec<NodeHash> = Vec::new();
let mut offset = 1 as usize;
for i in 0..((value_slice.len() - 1) / 32) as u8 {
offset = (1 + 32 * i as usize) as usize;
println!("offset:{}, i:{}", offset, i);
let hash = &value_slice[offset..offset + 32];
// envoyer un datum request
bigdir_entries.push(hash.try_into().expect("incorrect size"));
}
println!("its a BIG bro");
let chlidren: Vec<NodeHash> = Vec::new();
Some((
hash_name,
MerkleNode::Big(crate::BigNode {
children_hashes: chlidren,
children_hashes: bigdir_entries,
}),
))
}

View File

@@ -185,7 +185,7 @@ pub enum NetworkCommand {
SendDatum(MerkleNode, [u8; 32], String),
SendNoDatum(Vec<u8>, String),
SendRootReply(Vec<u8>, String),
InitDownload([u8; 32], String),
InitDownload([u8; 32], String, String),
// ...
}
@@ -205,7 +205,7 @@ pub enum NetworkEvent {
ServerHandshakeFailed(String),
DatumRequest([u8; 32], String),
RootRequest(String),
InitDownload([u8; 32], String),
InitDownload([u8; 32], String, String),
// ...
}
@@ -246,11 +246,14 @@ pub fn start_p2p_executor(
// Check for commands from the GUI
if let Ok(cmd) = cmd_rx.try_recv() {
match cmd {
NetworkCommand::InitDownload(hash, ip) => {
NetworkCommand::InitDownload(hash, ip, name) => {
if let Some(sd) = shared_data.as_ref() {
if let Some(res) = sd.handshake_peers.get_peer_info_username(ip) {
let _ = event_tx
.send(NetworkEvent::InitDownload(hash, res.ip.to_string()));
let _ = event_tx.send(NetworkEvent::InitDownload(
hash,
res.ip.to_string(),
name.to_string(),
));
}
}
}

View File

@@ -475,6 +475,7 @@ pub fn parse_message(
.try_into()
.expect("incorrect size"),
);
println!("received length:{}", received_length);
let received_datum = &received_message[LENGTH..];
let parsed_node =
parse_received_datum(received_datum.to_vec(), received_length as usize);