excludes private ip addresses

This commit is contained in:
behzad nouri
2021-07-16 13:41:01 -04:00
committed by Trent Nelson
parent 919c3ae6ec
commit e316586516
6 changed files with 62 additions and 8 deletions

View File

@@ -2,6 +2,7 @@
pub mod packet;
pub mod recvmmsg;
pub mod sendmmsg;
pub mod socket;
pub mod streamer;
#[macro_use]

View File

@@ -1,5 +1,8 @@
//! The `packet` module defines data structures and methods to pull data from the network.
use crate::recvmmsg::{recv_mmsg, NUM_RCVMMSGS};
use crate::{
recvmmsg::{recv_mmsg, NUM_RCVMMSGS},
socket::is_global,
};
pub use solana_perf::packet::{
limited_deserialize, to_packets_chunked, Packets, PacketsRecycler, NUM_PACKETS,
PACKETS_PER_BATCH,
@@ -56,8 +59,10 @@ pub fn recv_from(obj: &mut Packets, socket: &UdpSocket, max_wait_ms: u64) -> Res
pub fn send_to(obj: &Packets, socket: &UdpSocket) -> Result<()> {
for p in &obj.packets {
let a = p.meta.addr();
socket.send_to(&p.data[..p.meta.size], &a)?;
let addr = p.meta.addr();
if is_global(&addr) {
socket.send_to(&p.data[..p.meta.size], &addr)?;
}
}
Ok(())
}

28
streamer/src/socket.rs Normal file
View File

@@ -0,0 +1,28 @@
use std::net::SocketAddr;
// TODO: remove these once IpAddr::is_global is stable.
#[cfg(test)]
pub fn is_global(_: &SocketAddr) -> bool {
true
}
#[cfg(not(test))]
pub fn is_global(addr: &SocketAddr) -> bool {
use std::net::IpAddr;
match addr.ip() {
IpAddr::V4(addr) => {
// TODO: Consider excluding:
// addr.is_loopback() || addr.is_link_local()
// || addr.is_broadcast() || addr.is_documentation()
// || addr.is_unspecified()
!addr.is_private()
}
IpAddr::V6(_) => {
// TODO: Consider excluding:
// addr.is_loopback() || addr.is_unspecified(),
true
}
}
}