From 66bef5cd21e2fb7f0d6f33ee0eb2299829a2eeb4 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Sun, 11 Apr 2021 21:50:02 +0200 Subject: [PATCH] Fixes --- src/crypto/common.rs | 3 +-- src/crypto/mod.rs | 1 - src/engine/socket_thread.rs | 45 ++++++++++++++++++++++++++++--------- src/net.rs | 10 ++++----- src/tests/mod.rs | 2 +- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/crypto/common.rs b/src/crypto/common.rs index 503593e..403eede 100644 --- a/src/crypto/common.rs +++ b/src/crypto/common.rs @@ -8,7 +8,6 @@ use crate::{ types::NodeId, util::{from_base62, to_base62, MsgBuffer}, }; -use libc::BPF_FS_MAGIC; use ring::{ aead::{self, Algorithm, LessSafeKey, UnboundKey}, agreement::{EphemeralPrivateKey, UnparsedPublicKey}, @@ -253,8 +252,8 @@ impl PeerCrypto { if msg.stage() == STAGE_PONG { buffer.set_length(last_init_message.len()); buffer.message_mut().copy_from_slice(last_init_message); + return Ok(MessageResult::Reply) } - return Ok(MessageResult::Reply) } } Ok(MessageResult::None) diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 4910294..6ea20e4 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -9,4 +9,3 @@ mod rotate; pub use self::core::{EXTRA_LEN, TAG_LEN}; pub use common::*; -pub use self::core::{EXTRA_LEN, TAG_LEN}; diff --git a/src/engine/socket_thread.rs b/src/engine/socket_thread.rs index 961483b..9de798e 100644 --- a/src/engine/socket_thread.rs +++ b/src/engine/socket_thread.rs @@ -1,20 +1,20 @@ use super::{ - shared::{SharedPeerCrypto, SharedTable, SharedTraffic}, common::SPACE_BEFORE, + shared::{SharedPeerCrypto, SharedTable, SharedTraffic}, }; use crate::{ beacon::BeaconSerializer, config::{DEFAULT_PEER_TIMEOUT, DEFAULT_PORT}, - crypto::{is_init_message, InitResult, InitState, MessageResult, Crypto}, - device::{Type, Device}, + crypto::{is_init_message, Crypto, InitResult, InitState, MessageResult}, + device::{Device, Type}, engine::common::{Hash, PeerData}, error::Error, messages::{ AddrList, NodeInfo, PeerInfo, MESSAGE_TYPE_CLOSE, MESSAGE_TYPE_DATA, MESSAGE_TYPE_KEEPALIVE, MESSAGE_TYPE_NODE_INFO, }, - net::{mapped_addr, Socket}, + net::{mapped_addr, parse_listen, Socket}, port_forwarding::PortForwarding, types::{Address, NodeId, Range, RangeList}, util::{addr_nice, resolve, MsgBuffer, StatsdMsg, Time, TimeSource}, @@ -236,7 +236,17 @@ impl SocketThread) -> Result<(), Error> { if let Some(peer) = self.peers.get_mut(&addr) { peer.last_seen = TS::now(); - peer.timeout = TS::now() + self.config.peer_timeout as Time + peer.timeout = TS::now() + self.config.peer_timeout as Time; + if let Some(info) = &info { + // Update peer addresses, always add seen address + peer.addrs.clear(); + peer.addrs.push(addr); + for addr in &info.addrs { + if !peer.addrs.contains(addr) { + peer.addrs.push(*addr); + } + } + } } else { error!("Received peer update from non peer {}", addr_nice(addr)); return Ok(()); @@ -281,6 +291,12 @@ impl SocketThread SocketThread SocketThread SocketThread io::Result<()> { self.own_addresses.clear(); - self.own_addresses.push(self.socket.address().await.map(mapped_addr)?); + let socket_addr = self.socket.address().await.map(mapped_addr)?; + // 1) Specified advertise addresses + for addr in &self.config.advertise_addresses { + self.own_addresses.push(parse_listen(addr, socket_addr.port())); + } + // 2) Address of UDP socket + self.own_addresses.push(socket_addr); + // 3) Addresses from port forwarding if let Some(ref pfw) = self.port_forwarding { self.own_addresses.push(pfw.get_internal_ip().into()); self.own_addresses.push(pfw.get_external_ip().into()); diff --git a/src/net.rs b/src/net.rs index c8922dd..899cec5 100644 --- a/src/net.rs +++ b/src/net.rs @@ -2,15 +2,15 @@ // Copyright (C) 2015-2021 Dennis Schwerdel // This software is licensed under GPL-3 or newer (see LICENSE.md) -use super::util::{MockTimeSource, MsgBuffer, Time, TimeSource}; +use crate::config::DEFAULT_PORT; use crate::port_forwarding::PortForwarding; +use crate::util::{MockTimeSource, MsgBuffer, Time, TimeSource}; use async_trait::async_trait; use parking_lot::Mutex; use std::{ collections::{HashMap, VecDeque}, io::{self, ErrorKind}, net::{IpAddr, Ipv6Addr, SocketAddr, UdpSocket}, - os::unix::io::AsRawFd, sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -62,11 +62,10 @@ impl Clone for NetSocket { } } - #[async_trait] impl Socket for NetSocket { async fn listen(addr: &str) -> Result { - let addr = parse_listen(addr); + let addr = parse_listen(addr, DEFAULT_PORT); Ok(NetSocket(UdpSocket::bind(addr)?)) } @@ -144,11 +143,10 @@ impl MockSocket { } } - #[async_trait] impl Socket for MockSocket { async fn listen(addr: &str) -> Result { - Ok(Self::new(parse_listen(addr))) + Ok(Self::new(parse_listen(addr, DEFAULT_PORT))) } async fn receive(&mut self, buffer: &mut MsgBuffer) -> Result { diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 67dd149..dc3af06 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -8,6 +8,6 @@ mod payload; mod peers; #[test] -fn test_time_format() { +async fn test_time_format() { assert!(time::OffsetDateTime::try_now_local().is_ok()); }