fixes
This commit is contained in:
parent
a87899c402
commit
4d6ea8e626
@ -191,13 +191,16 @@ fn main() -> std::io::Result<()> {
|
|||||||
"{} reaching to other peers to obtain ip address",
|
"{} reaching to other peers to obtain ip address",
|
||||||
"[LOG]".blue()
|
"[LOG]".blue()
|
||||||
);
|
);
|
||||||
virtual_network
|
let mut network_write_lock = virtual_network.write().unwrap(); // avoid deadlock
|
||||||
.write()
|
|
||||||
.unwrap()
|
|
||||||
|
let encrypted = network_write_lock.encrypted;
|
||||||
|
let key = network_write_lock.key;
|
||||||
|
network_write_lock
|
||||||
.peers
|
.peers
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.for_each(|peer| {
|
.for_each(|peer| {
|
||||||
match net::P2P_query(&mut buf, &peer.sock_addr, &socket, virtual_network.clone()) {
|
match net::P2P_query(&mut buf, &peer.sock_addr, &socket, encrypted,key) {
|
||||||
Ok(ip) => {
|
Ok(ip) => {
|
||||||
ips_used[ip.octets()[3] as usize] = true;
|
ips_used[ip.octets()[3] as usize] = true;
|
||||||
peer.private_ip = ip;
|
peer.private_ip = ip;
|
||||||
@ -210,23 +213,20 @@ fn main() -> std::io::Result<()> {
|
|||||||
),
|
),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
virtual_network.write().unwrap().private_ip = std::net::Ipv4Addr::new(
|
network_write_lock.private_ip = std::net::Ipv4Addr::new(
|
||||||
DEFAULT_NETWORK_PREFIX[0],
|
DEFAULT_NETWORK_PREFIX[0],
|
||||||
DEFAULT_NETWORK_PREFIX[1],
|
DEFAULT_NETWORK_PREFIX[1],
|
||||||
DEFAULT_NETWORK_PREFIX[2],
|
DEFAULT_NETWORK_PREFIX[2],
|
||||||
ips_used.par_iter().position_first(|&b| !b).unwrap() as u8,
|
ips_used.par_iter().position_first(|&b| !b).unwrap() as u8,
|
||||||
); // find first element that is false
|
); // find first element that is false
|
||||||
|
|
||||||
virtual_network
|
network_write_lock
|
||||||
.write()
|
|
||||||
.unwrap()
|
|
||||||
.peers
|
.peers
|
||||||
.retain(|peer| peer.private_ip != std::net::Ipv4Addr::UNSPECIFIED); // remove all peers without ip
|
.retain(|peer| peer.private_ip != std::net::Ipv4Addr::UNSPECIFIED); // remove all peers without ip
|
||||||
|
|
||||||
virtual_network
|
network_write_lock
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.peers
|
.peers
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|peer| {
|
.for_each(|peer| {
|
||||||
@ -234,8 +234,8 @@ fn main() -> std::io::Result<()> {
|
|||||||
&mut buf,
|
&mut buf,
|
||||||
&peer.sock_addr,
|
&peer.sock_addr,
|
||||||
&socket,
|
&socket,
|
||||||
virtual_network.read().unwrap().private_ip,
|
network_write_lock.private_ip,
|
||||||
virtual_network.clone(),
|
encrypted,key
|
||||||
) {
|
) {
|
||||||
Ok(_) => eprintln!(
|
Ok(_) => eprintln!(
|
||||||
"{} registered with peer: {}",
|
"{} registered with peer: {}",
|
||||||
|
@ -412,7 +412,8 @@ pub fn P2P_query(
|
|||||||
buf: &mut [u8; UDP_BUFFER_SIZE],
|
buf: &mut [u8; UDP_BUFFER_SIZE],
|
||||||
dst: &SocketAddr,
|
dst: &SocketAddr,
|
||||||
socket: &UdpSocket,
|
socket: &UdpSocket,
|
||||||
network: Arc<std::sync::RwLock<types::Network>>,
|
encrypted: bool, // avoid deadlock
|
||||||
|
key: [u8; 32]
|
||||||
) -> Result<std::net::Ipv4Addr, Box<dyn std::error::Error>> {
|
) -> Result<std::net::Ipv4Addr, Box<dyn std::error::Error>> {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!("P2P QUERY method");
|
println!("P2P QUERY method");
|
||||||
@ -433,9 +434,9 @@ pub fn P2P_query(
|
|||||||
let tmp_decrypted: Vec<u8>;
|
let tmp_decrypted: Vec<u8>;
|
||||||
|
|
||||||
return Ok(std::net::Ipv4Addr::from_str(
|
return Ok(std::net::Ipv4Addr::from_str(
|
||||||
if network.read().unwrap().encrypted {
|
if encrypted {
|
||||||
match shared::crypto::decrypt(
|
match shared::crypto::decrypt(
|
||||||
&network.read().unwrap().key,
|
&key,
|
||||||
&iv,
|
&iv,
|
||||||
&buf[P2PStandardDataPositions::DATA as usize..data_lenght - 1],
|
&buf[P2PStandardDataPositions::DATA as usize..data_lenght - 1],
|
||||||
) {
|
) {
|
||||||
@ -470,16 +471,17 @@ pub fn P2P_hello(
|
|||||||
dst: &SocketAddr,
|
dst: &SocketAddr,
|
||||||
socket: &UdpSocket,
|
socket: &UdpSocket,
|
||||||
private_ip: Ipv4Addr,
|
private_ip: Ipv4Addr,
|
||||||
network: Arc<RwLock<types::Network>>,
|
encrypted: bool, // avoid deadlock
|
||||||
|
key: [u8; 32],
|
||||||
) -> Result<usize, ServerErrorResponses> {
|
) -> Result<usize, ServerErrorResponses> {
|
||||||
let private_ip_str = private_ip.to_string();
|
let private_ip_str = private_ip.to_string();
|
||||||
let (private_ip_final, iv) = if network.read().unwrap().encrypted {
|
let (private_ip_final, iv) = if encrypted {
|
||||||
let mut rng = rng();
|
let mut rng = rng();
|
||||||
let mut iv: [u8; BLOCK_SIZE] = [0u8; BLOCK_SIZE];
|
let mut iv: [u8; BLOCK_SIZE] = [0u8; BLOCK_SIZE];
|
||||||
rng.fill_bytes(&mut iv);
|
rng.fill_bytes(&mut iv);
|
||||||
(
|
(
|
||||||
shared::crypto::encrypt(
|
shared::crypto::encrypt(
|
||||||
&network.read().unwrap().key,
|
&key,
|
||||||
&iv,
|
&iv,
|
||||||
&private_ip_str.as_bytes(),
|
&private_ip_str.as_bytes(),
|
||||||
)
|
)
|
||||||
@ -495,7 +497,17 @@ pub fn P2P_hello(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut send_buf: Box<[u8]> =
|
let mut send_buf: Box<[u8]> =
|
||||||
vec![0u8; 1 + P2PStandardDataPositions::DATA as usize + private_ip_final.len()].into();
|
vec![0u8; P2PStandardDataPositions::DATA as usize + private_ip_final.len()].into();
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
eprintln!(
|
||||||
|
"registering network:\niv: {}\nIP: {}",
|
||||||
|
iv.iter().map(|x| format!("{:02X} ", x)).collect::<String>(),
|
||||||
|
private_ip_final
|
||||||
|
.iter()
|
||||||
|
.map(|x| format!("{:02X} ", x))
|
||||||
|
.collect::<String>(),
|
||||||
|
);
|
||||||
|
|
||||||
send_buf[0] = P2PMethods::PEER_HELLO as u8;
|
send_buf[0] = P2PMethods::PEER_HELLO as u8;
|
||||||
send_buf[P2PStandardDataPositions::IV as usize
|
send_buf[P2PStandardDataPositions::IV as usize
|
||||||
@ -569,7 +581,7 @@ pub async fn handle_incoming_connection(
|
|||||||
if encrypted {
|
if encrypted {
|
||||||
let mut rng = rng();
|
let mut rng = rng();
|
||||||
rng.fill_bytes(&mut iv);
|
rng.fill_bytes(&mut iv);
|
||||||
|
send_buf[P2PStandardDataPositions::IV as usize..P2PStandardDataPositions::IV as usize+BLOCK_SIZE].copy_from_slice(&iv);
|
||||||
send_buf[P2PStandardDataPositions::DATA as usize..P2PStandardDataPositions::DATA as usize + (private_ip_str.len() + (BLOCK_SIZE - (private_ip_str.len() % BLOCK_SIZE)))].copy_from_slice(shared::crypto::encrypt(&network.read().unwrap().key, &iv, private_ip_str.as_bytes()).unwrap().as_slice());
|
send_buf[P2PStandardDataPositions::DATA as usize..P2PStandardDataPositions::DATA as usize + (private_ip_str.len() + (BLOCK_SIZE - (private_ip_str.len() % BLOCK_SIZE)))].copy_from_slice(shared::crypto::encrypt(&network.read().unwrap().key, &iv, private_ip_str.as_bytes()).unwrap().as_slice());
|
||||||
} else {
|
} else {
|
||||||
send_buf[P2PStandardDataPositions::DATA as usize..P2PStandardDataPositions::DATA as usize + private_ip_str.len()].copy_from_slice(private_ip_str.as_bytes());
|
send_buf[P2PStandardDataPositions::DATA as usize..P2PStandardDataPositions::DATA as usize + private_ip_str.len()].copy_from_slice(private_ip_str.as_bytes());
|
||||||
@ -592,13 +604,23 @@ pub async fn handle_incoming_connection(
|
|||||||
let mut network_write_lock = network.write().unwrap();
|
let mut network_write_lock = network.write().unwrap();
|
||||||
let key: [u8; 32] = network_write_lock.key;
|
let key: [u8; 32] = network_write_lock.key;
|
||||||
let encrypted: bool = network_write_lock.encrypted;
|
let encrypted: bool = network_write_lock.encrypted;
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
eprintln!(
|
||||||
|
"registering network:\niv: {}\nIP: {}",
|
||||||
|
&buf[P2PStandardDataPositions::IV as usize
|
||||||
|
..P2PStandardDataPositions::IV as usize + BLOCK_SIZE].iter().map(|x| format!("{:02X} ", x)).collect::<String>(),
|
||||||
|
&buf[P2PStandardDataPositions::DATA as usize..data_lenght as usize-1 /*compensate for size and index diference*/]
|
||||||
|
.iter()
|
||||||
|
.map(|x| format!("{:02X} ", x))
|
||||||
|
.collect::<String>(),
|
||||||
|
);
|
||||||
network_write_lock.peers.push(Peer::new(
|
network_write_lock.peers.push(Peer::new(
|
||||||
src,
|
src,
|
||||||
Some(
|
Some(
|
||||||
match std::net::Ipv4Addr::from_str(
|
match std::net::Ipv4Addr::from_str(
|
||||||
match std::str::from_utf8(if encrypted {
|
match std::str::from_utf8(if encrypted {
|
||||||
match shared::crypto::decrypt(&key, &buf[P2PStandardDataPositions::IV as usize
|
match shared::crypto::decrypt(&key, &buf[P2PStandardDataPositions::IV as usize
|
||||||
..P2PStandardDataPositions::IV as usize + BLOCK_SIZE], &buf[P2PStandardDataPositions::DATA as usize..data_lenght as usize-1 /*compensate for size and index diference*/]) {
|
..P2PStandardDataPositions::IV as usize + BLOCK_SIZE], &buf[P2PStandardDataPositions::DATA as usize..data_lenght as usize]) {
|
||||||
Ok(data) => {tmp_data = data; &tmp_data},
|
Ok(data) => {tmp_data = data; &tmp_data},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
@ -610,7 +632,7 @@ pub async fn handle_incoming_connection(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
&buf[P2PStandardDataPositions::DATA as usize..data_lenght as usize-1 /*compensate for size and index diference*/]
|
&buf[P2PStandardDataPositions::DATA as usize..data_lenght as usize]
|
||||||
}) {
|
}) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user