manage handshake wiip

This commit is contained in:
2026-01-09 01:03:40 +01:00
parent cd2f87cb81
commit 9fc33804d0
6 changed files with 145 additions and 212 deletions

View File

@@ -4,7 +4,7 @@ use crate::{
CryptographicSignature, get_peer_key, sign_message, verify_signature,
},
messages_channels::MultipleSenders,
messages_structure::{HandshakeMessage, constructMessage},
messages_structure::construct_message,
registration,
};
use std::sync::{Arc, Mutex};
@@ -19,7 +19,7 @@ pub enum EventType {
const ID: usize = 4;
const TYPE: usize = 5;
const LENGTH: usize = 7;
const EXTENSIONS: usize = 32;
const EXTENSIONS: usize = 4;
const SIGNATURE: usize = 64;
const PING: u8 = 0;
@@ -50,8 +50,11 @@ pub fn handle_recevied_message(
let message_id: [u8; 4] = recevied_message[0..4].try_into().expect("size error");
let id = i32::from_be_bytes(message_id);
parse_message(recevied_message, received_name, id, crypto_pair, cmd_tx)
//TODO
// Lock the mutex to access the HashMap
let list = messages_list.lock().unwrap();
/*let list = messages_list.lock().unwrap();
let eventtype = list.get(&id); // Clone the enum so we can release the lock if needed
match eventtype {
@@ -113,7 +116,7 @@ pub fn handle_recevied_message(
}
print!("Message not found for ID: {}", id)
}
}
}*/
}
pub fn ping(received_message: Vec<u8>, socket_addr: String) {}
@@ -140,9 +143,11 @@ pub fn parse_message(
received_message: Vec<u8>,
received_name: String,
id: i32,
username: String,
crypto_pair: &CryptographicSignature
crypto_pair: &CryptographicSignature,
cmd_tx: crossbeam_channel::Sender<NetworkEvent>,
) -> Option<Vec<u8>> {
let cmd_tx_clone = cmd_tx.clone();
let id_bytes: [u8; 4] = received_message[0..ID]
.try_into()
.expect("Taille incorrecte");
@@ -176,35 +181,62 @@ pub fn parse_message(
}
_ => {}
}
// Message handling
let mut constructed_message: Option<Vec<u8>> = None;
match msgtype {
// PING
//
// envoie un OK
PING => {
constructed_message = construct_message(OK, Vec::new(), id, crypto_pair);
}
//
// OK
//
// rien ?
// si NATTRAVERSALREQUEST alors
//
// ERROR
//
// affiche un msg d'erreur
//
ERROR => {
if let Ok(err_received) =
String::from_utf8(received_message[LENGTH..(msg_length + LENGTH)].to_vec())
{
let err_msg = format!(
"Error received from peer {} : {}",
String::from(received_name),
err_received
);
let _ = cmd_tx_clone.send(NetworkEvent::Error(err_msg));
} else {
let err_msg = format!(
"Error received from peer {} : N/A",
String::from(received_name),
);
let _ = cmd_tx_clone.send(NetworkEvent::Error(err_msg));
}
}
// HELLO
//
// envoie une hello reply
//
HELLO => {
let username_size = username.len();
let helloreply = constructMessage(msgtype, payload, &id, crypto_pair)
let hello_handshake =
HandshakeMessage::helloReply(id as u32, username_size as u16 + 4, username.clone());
//HandshakeMessage::display(&hello_handshake);
let hello_handshake_serialized = hello_handshake.serialize();
return Some(hello_handshake_serialized);
let mut payload = Vec::new();
payload.extend_from_slice(&0u32.to_be_bytes());
payload.extend_from_slice(&crypto_pair.username.clone().as_bytes());
let helloreply = construct_message(HELLOREPLY, payload, id, crypto_pair);
return helloreply;
}
// HELLOREPLY
//
// envoie un root request
//
// ajoute a la liste des peers handshake
HELLOREPLY => {}
//
// ROOTREQUEST
//
@@ -285,4 +317,5 @@ pub fn parse_message(
// envoie OK à S puis envoie un ping à S
_ => return None,
}
constructed_message
}