avoid packets sending to non-exist peer causing route loop (#1378)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use std::collections::hash_map::DefaultHasher;
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
use std::net::IpAddr;
|
||||||
use std::{
|
use std::{
|
||||||
hash::Hasher,
|
hash::Hasher,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
@@ -239,6 +240,13 @@ impl GlobalCtx {
|
|||||||
self.config.get_id()
|
self.config.get_id()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_ip_in_same_network(&self, ip: &IpAddr) -> bool {
|
||||||
|
match ip {
|
||||||
|
IpAddr::V4(v4) => self.get_ipv4().map(|x| x.contains(v4)).unwrap_or(false),
|
||||||
|
IpAddr::V6(v6) => self.get_ipv6().map(|x| x.contains(v6)).unwrap_or(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_network_identity(&self) -> NetworkIdentity {
|
pub fn get_network_identity(&self) -> NetworkIdentity {
|
||||||
self.config.get_network_identity()
|
self.config.get_network_identity()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1055,10 +1055,19 @@ impl PeerManager {
|
|||||||
|| ipv4_addr.is_multicast()
|
|| ipv4_addr.is_multicast()
|
||||||
|| *ipv4_addr == ipv4_inet.last_address()
|
|| *ipv4_addr == ipv4_inet.last_address()
|
||||||
{
|
{
|
||||||
dst_peers.extend(self.peers.list_routes().await.iter().map(|x| *x.key()));
|
dst_peers.extend(self.peers.list_routes().await.iter().filter_map(|x| {
|
||||||
|
if *x.key() != self.my_peer_id {
|
||||||
|
Some(*x.key())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}));
|
||||||
} else if let Some(peer_id) = self.peers.get_peer_id_by_ipv4(ipv4_addr).await {
|
} else if let Some(peer_id) = self.peers.get_peer_id_by_ipv4(ipv4_addr).await {
|
||||||
dst_peers.push(peer_id);
|
dst_peers.push(peer_id);
|
||||||
} else {
|
} else if !self
|
||||||
|
.global_ctx
|
||||||
|
.is_ip_in_same_network(&std::net::IpAddr::V4(*ipv4_addr))
|
||||||
|
{
|
||||||
for exit_node in &self.exit_nodes {
|
for exit_node in &self.exit_nodes {
|
||||||
let IpAddr::V4(exit_node) = exit_node else {
|
let IpAddr::V4(exit_node) = exit_node else {
|
||||||
continue;
|
continue;
|
||||||
@@ -1072,8 +1081,12 @@ impl PeerManager {
|
|||||||
}
|
}
|
||||||
#[cfg(target_env = "ohos")]
|
#[cfg(target_env = "ohos")]
|
||||||
{
|
{
|
||||||
if dst_peers.is_empty() {
|
if dst_peers.is_empty()
|
||||||
tracing::info!("no peer id for ipv4: {}, set exit_node for ohos", ipv4_addr);
|
&& !self
|
||||||
|
.global_ctx
|
||||||
|
.is_ip_in_same_network(&std::net::IpAddr::V4(*ipv4_addr))
|
||||||
|
{
|
||||||
|
tracing::trace!("no peer id for ipv4: {}, set exit_node for ohos", ipv4_addr);
|
||||||
dst_peers.push(self.my_peer_id.clone());
|
dst_peers.push(self.my_peer_id.clone());
|
||||||
is_exit_node = true;
|
is_exit_node = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2396,6 +2396,15 @@ impl Route for PeerRoute {
|
|||||||
return Some(p.peer_id);
|
return Some(p.peer_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only get peer id for proxy when the dst ipv4 is not in same network with us
|
||||||
|
if self
|
||||||
|
.global_ctx
|
||||||
|
.is_ip_in_same_network(&std::net::IpAddr::V4(*ipv4_addr))
|
||||||
|
{
|
||||||
|
tracing::trace!(?ipv4_addr, "ipv4 addr is in same network with us");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(peer_id) = route_table.get_peer_id_for_proxy(ipv4_addr) {
|
if let Some(peer_id) = route_table.get_peer_id_for_proxy(ipv4_addr) {
|
||||||
return Some(peer_id);
|
return Some(peer_id);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user