messages rewrite

This commit is contained in:
2026-01-07 23:34:44 +01:00
parent f51b8e999c
commit dc1767abe4
5 changed files with 276 additions and 38 deletions

View File

@@ -16,29 +16,24 @@ pub enum EventType {
PeerHello,
}
/*pub fn handle_recevied_message(
messages_list: &mut HashMap<i32, EventType>,
recevied_message: &Vec<u8>,
crypto_pair: &CryptographicSignature,
socket_addr: &SocketAddr,
senders: &MultipleSenders,
) {
let message_id: [u8; 4] = recevied_message[0..4].try_into().expect("size error");
let id = i32::from_be_bytes(message_id);
let eventtype = messages_list.get(&id);
match eventtype {
Some(EventType::ServerHelloReply) => {
registration::register_ip_addresses(
&crypto_pair,
socket_addr.ip().to_string(),
&senders,
messages_list,
);
}
Some(_) => print!("Not implemented"),
None => print!("Message not found"),
}
}*/
const ID: usize = 4;
const TYPE: usize = 5;
const LENGTH: usize = 7;
const EXTENSIONS: usize = 32;
const SIGNATURE: usize = 64;
const PING: u8 = 0;
const OK: u8 = 128;
const ERROR: u8 = 129;
const HELLO: u8 = 1;
const HELLOREPLY: u8 = 130;
const ROOTREQUEST: u8 = 2;
const ROOTREPLY: u8 = 131;
const DATUMREQUEST: u8 = 3;
const NODATUM: u8 = 133;
const DATUM: u8 = 132;
const NATTRAVERSALREQUEST: u8 = 4;
const NATTRAVERSALREQUEST2: u8 = 5;
pub fn handle_recevied_message(
messages_list: &Arc<Mutex<HashMap<i32, EventType>>>,
@@ -120,3 +115,173 @@ pub fn handle_recevied_message(
}
}
}
pub fn ping(received_message: Vec<u8>, socket_addr: String) {}
pub fn ok(received_message: Vec<u8>, socket_addr: String) {}
pub fn error(received_message: Vec<u8>, socket_addr: String) {}
pub fn hello(received_message: Vec<u8>, socket_addr: String) {}
pub fn hello_reply(received_message: Vec<u8>, socket_addr: String) {}
pub fn root_request(received_message: Vec<u8>, socket_addr: String) {}
pub fn root_reply(received_message: Vec<u8>, socket_addr: String) {}
pub fn datum_request(received_message: Vec<u8>, socket_addr: String) {}
pub fn no_datum(received_message: Vec<u8>, socket_addr: String) {}
pub fn datum(received_message: Vec<u8>, socket_addr: String) {}
pub fn parse_message(
received_message: Vec<u8>,
received_name: String,
id: i32,
username: String,
) -> Option<Vec<u8>> {
let id_bytes: [u8; 4] = received_message[0..ID]
.try_into()
.expect("Taille incorrecte");
let msgtype = received_message[ID];
let length_bytes: [u8; 2] = received_message[TYPE..LENGTH]
.try_into()
.expect("Taille incorrecte");
let msg_length = u16::from_be_bytes(length_bytes) as usize;
// verify signature
match msgtype {
HELLO | HELLOREPLY | ROOTREPLY | NODATUM | NATTRAVERSALREQUEST | NATTRAVERSALREQUEST2 => {
let peer_pubkey = tokio::runtime::Runtime::new()
.unwrap()
.block_on(get_peer_key(&received_name))
.expect("failed to retrieve public key");
let signature: [u8; SIGNATURE] = received_message
[LENGTH + msg_length..LENGTH + msg_length + SIGNATURE]
.try_into()
.expect("Taille incorrecte");
if !verify_signature(peer_pubkey, &received_message) {
println!(
"incorrect signature from given peer: {}, ignoring message of type {} with id {}",
&received_name, received_message[ID], id
);
return None;
}
}
_ => {}
}
match msgtype {
// PING
//
// envoie un OK
//
// OK
//
// si NATTRAVERSALREQUEST alors
//
// ERROR
//
// affiche un msg d'erreur
//
// HELLO
//
// envoie une hello reply
//
HELLO => {
let username_size = username.len();
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);
}
// HELLOREPLY
//
// envoie un root request
//
// ROOTREQUEST
//
// envoie un root reply
//
// ROOTREPLY
//
// envoie un datum request
//
// DATUMREQUEST
//
// envoie le datum
//
// NODATUM
//
// affiche un msg d'erreur
//
// DATUM
//
// parcourt le directory recu ou le big directory et renvoie une DATUMREQUEST pour chaque
// directory ou big directory lu
//
// NATTRAVERSALREQUEST
//
// repond OK et envoie un NATTRAVERSALREQUEST2 au pair B
//
// NATTRAVERSALREQUEST2
//
// envoie OK à S puis envoie un ping à S
// PING
//
// envoie un OK
//
// OK
//
// si NATTRAVERSALREQUEST alors
//
// ERROR
//
// affiche un msg d'erreur
//
// HELLO
//
// envoie une hello reply
//
// HELLOREPLY
//
// envoie un root request
//
// ROOTREQUEST
//
// envoie un root reply
//
// ROOTREPLY
//
// envoie un datum request
//
// DATUMREQUEST
//
// envoie le datum
//
// NODATUM
//
// affiche un msg d'erreur
//
// DATUM
//
// parcourt le directory recu ou le big directory et renvoie une DATUMREQUEST pour chaque
// directory ou big directory lu
//
// NATTRAVERSALREQUEST
//
// repond OK et envoie un NATTRAVERSALREQUEST2 au pair B
//
// NATTRAVERSALREQUEST2
//
// envoie OK à S puis envoie un ping à S
_ => return None,
}
}