wip messages creation & handling
This commit is contained in:
@@ -7,8 +7,11 @@ use crate::{
|
||||
messages_structure::construct_message,
|
||||
registration,
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{collections::HashMap, net::SocketAddr};
|
||||
use std::{
|
||||
net::IpAddr,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
pub enum EventType {
|
||||
ServerHelloReply,
|
||||
@@ -42,6 +45,8 @@ pub fn handle_recevied_message(
|
||||
socket_addr: &SocketAddr,
|
||||
senders: &MultipleSenders,
|
||||
server_name: &String,
|
||||
cmd_tx: crossbeam_channel::Sender<NetworkEvent>,
|
||||
ip: SocketAddr,
|
||||
) {
|
||||
if recevied_message.len() < 4 {
|
||||
return;
|
||||
@@ -50,8 +55,31 @@ 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
|
||||
let mut is_resp_to_server_handshake = false;
|
||||
|
||||
if recevied_message[4] == HELLO {
|
||||
let length_bytes: [u8; 2] = recevied_message[TYPE..LENGTH]
|
||||
.try_into()
|
||||
.expect("Taille incorrecte");
|
||||
let msg_length = u16::from_be_bytes(length_bytes) as usize;
|
||||
let ilength = u16::from_be_bytes(length_bytes);
|
||||
let received_name =
|
||||
&recevied_message[LENGTH + EXTENSIONS..LENGTH + EXTENSIONS + ilength as usize];
|
||||
let name = String::from_utf8(received_name.to_vec()).expect("wrong name");
|
||||
if name.clone() == server_name.clone() {
|
||||
is_resp_to_server_handshake = true;
|
||||
}
|
||||
}
|
||||
|
||||
let resp = parse_message(recevied_message.to_vec(), id, crypto_pair, cmd_tx, ip);
|
||||
|
||||
match resp {
|
||||
None => {}
|
||||
Some(resp_msg) => {
|
||||
println!("msg_sent:{:?}", resp_msg);
|
||||
senders.send_via(0, resp_msg, ip.to_string(), is_resp_to_server_handshake);
|
||||
}
|
||||
}
|
||||
|
||||
// Lock the mutex to access the HashMap
|
||||
/*let list = messages_list.lock().unwrap();
|
||||
@@ -119,32 +147,12 @@ 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,
|
||||
crypto_pair: &CryptographicSignature,
|
||||
cmd_tx: crossbeam_channel::Sender<NetworkEvent>,
|
||||
ip: SocketAddr,
|
||||
) -> Option<Vec<u8>> {
|
||||
let cmd_tx_clone = cmd_tx.clone();
|
||||
|
||||
@@ -163,20 +171,32 @@ pub fn parse_message(
|
||||
// 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;
|
||||
let ilength = u16::from_be_bytes(length_bytes);
|
||||
let received_name =
|
||||
&received_message[LENGTH + EXTENSIONS..LENGTH + EXTENSIONS + ilength as usize];
|
||||
let received_username = String::from_utf8(received_name.to_vec());
|
||||
match received_username {
|
||||
Ok(username) => {
|
||||
let peer_pubkey = tokio::runtime::Runtime::new()
|
||||
.unwrap()
|
||||
.block_on(get_peer_key(&username))
|
||||
.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 {}",
|
||||
&username, received_message[ID], id
|
||||
);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("incorrect name: {}", e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -204,17 +224,10 @@ pub fn parse_message(
|
||||
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 err_msg = format!("Error received from peer {} : {}", ip, 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 err_msg = format!("Error received from peer {} : N/A", ip,);
|
||||
let _ = cmd_tx_clone.send(NetworkEvent::Error(err_msg));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user