From a6319cfb0e6617cb7cc57c26eb9e815bb6cc3455 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Thu, 30 Nov 2023 15:04:36 -0300 Subject: [PATCH] [runtime] Enhancement: Shuffle In Release Mode --- src/rust/runtime/network/ephemeral.rs | 29 ++++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/rust/runtime/network/ephemeral.rs b/src/rust/runtime/network/ephemeral.rs index e87775bdc..c8a88fb91 100644 --- a/src/rust/runtime/network/ephemeral.rs +++ b/src/rust/runtime/network/ephemeral.rs @@ -1,7 +1,12 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +//====================================================================================================================== +// Imports +//====================================================================================================================== + use crate::runtime::fail::Fail; +#[cfg(not(debug_assertions))] use ::rand::prelude::{ SeedableRng, SliceRandom, @@ -12,10 +17,12 @@ use ::rand::prelude::{ // Constants //====================================================================================================================== +/// First private port. See https://datatracker.ietf.org/doc/html/rfc6335 for details. const FIRST_PRIVATE_PORT: u16 = 49152; +/// Last private port. See https://datatracker.ietf.org/doc/html/rfc6335 for details. const LAST_PRIVATE_PORT: u16 = 65535; -/// Seed number of ephemeral port allocator. -#[cfg(debug_assertions)] +/// Seed number for ephemeral port allocator. +#[cfg(not(debug_assertions))] const EPHEMERAL_PORT_SEED: u64 = 12345; //====================================================================================================================== @@ -86,21 +93,15 @@ impl EphemeralPorts { impl Default for EphemeralPorts { /// Creates a new ephemeral port allocator. fn default() -> Self { - let mut rng: SmallRng = { - #[cfg(debug_assertions)] - { - SmallRng::seed_from_u64(EPHEMERAL_PORT_SEED) - } - #[cfg(not(debug_assertions))] - { - SmallRng::from_entropy() - } - }; let mut ports: Vec = Vec::::new(); - for port in FIRST_PRIVATE_PORT..=LAST_PRIVATE_PORT { + for port in (FIRST_PRIVATE_PORT..=LAST_PRIVATE_PORT).rev() { ports.push(port); } - ports.shuffle(&mut rng); + #[cfg(not(debug_assertions))] + { + let mut rng: SmallRng = SmallRng::seed_from_u64(EPHEMERAL_PORT_SEED); + ports.shuffle(&mut rng); + } Self { ports } } }