Skip to content

Commit 40ecd03

Browse files
committed
fix: ::1 and 127.0.0.0/8 are private range addresses
1 parent 67d5711 commit 40ecd03

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

stacks-common/src/types/net.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,14 @@ impl PeerAddress {
209209
/// Is this a private IP address?
210210
pub fn is_in_private_range(&self) -> bool {
211211
if self.is_ipv4() {
212-
// 10.0.0.0/8, 172.16.0.0/12, or 192.168.0.0/16
212+
// 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, or 127.0.0.0/8
213213
self.0[12] == 10
214214
|| (self.0[12] == 172 && self.0[13] >= 16 && self.0[13] <= 31)
215215
|| (self.0[12] == 192 && self.0[13] == 168)
216+
|| self.0[12] == 127
216217
} else {
217-
self.0[0] >= 0xfc
218+
// private address (fc00::/7) or localhost (::1)
219+
self.0[0] >= 0xfc || (self.0[0..15] == [0u8; 15] && self.0[15] == 1)
218220
}
219221
}
220222

stackslib/src/net/neighbors/walk.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,9 @@ impl<DB: NeighborWalkDB, NC: NeighborComms> NeighborWalk<DB, NC> {
673673
// just use the one we used to contact it. This can happen if the
674674
// node is behind a load-balancer, or is doing port-forwarding,
675675
// etc.
676-
if neighbor_from_handshake.addr.addrbytes.is_in_private_range() {
676+
if neighbor_from_handshake.addr.addrbytes.is_in_private_range()
677+
|| neighbor_from_handshake.addr.addrbytes.is_anynet()
678+
{
677679
debug!(
678680
"{}: outbound neighbor gave private IP address {:?}; assuming it meant {:?}",
679681
local_peer_str, &neighbor_from_handshake.addr, &self.cur_neighbor.addr

stackslib/src/net/stackerdb/sync.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
8585
let mut found = HashSet::new();
8686
let mut min_age =
8787
get_epoch_time_secs().saturating_sub(network.get_connection_opts().max_neighbor_age);
88-
while found.len() < self.max_neighbors && min_age != 0 {
88+
while found.len() < self.max_neighbors {
8989
let peers_iter = PeerDB::find_stacker_db_replicas(
9090
network.peerdb_conn(),
9191
network.get_local_peer().network_id,
@@ -123,8 +123,7 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
123123
// search for older neighbors
124124
if min_age > 1 {
125125
min_age = 1;
126-
} else if min_age == 1 {
127-
min_age = 0;
126+
} else if min_age <= 1 {
128127
break;
129128
}
130129
}

0 commit comments

Comments
 (0)