Struct PortPools

Source
pub struct PortPools {
    pub buckets: Vec<u64>,
    /* private fields */
}
Expand description

Random Port

Recently, awareness has been raised about a number of “blind” attacks (i.e., attacks that can be performed without the need to sniff the packets that correspond to the transport protocol instance to be attacked) that can be performed against the Transmission Control Protocol (TCP) [RFC0793] and similar protocols. The consequences of these attacks range from throughput reduction to broken connections or data corruption [RFC5927] [RFC4953] [Watson].

All these attacks rely on the attacker’s ability to guess or know the five-tuple (Protocol, Source Address, Source port, Destination Address, Destination Port) that identifies the transport protocol instance to be attacked.

Services are usually located at fixed, “well-known” ports [IANA] at the host supplying the service (the server). Client applications connecting to any such service will contact the server by specifying the server IP address and service port number. The IP address and port number of the client are normally left unspecified by the client application and thus are chosen automatically by the client networking stack. Ports chosen automatically by the networking stack are known as ephemeral ports [Stevens].

While the server IP address, the well-known port, and the client IP address may be known by an attacker, the ephemeral port of the client is usually unknown and must be guessed.

Fields§

§buckets: Vec<u64>

Implementations§

Source§

impl PortPools

Source

pub fn new() -> Self

Source

pub fn capacity(&self) -> usize

get pools capacity.

§Examples
use turn_rs::router::ports::Bit;
use turn_rs::router::ports::PortPools;

let pools = PortPools::new();
assert_eq!(pools.capacity(), 65535 - 49152);
Source

pub fn len(&self) -> usize

get pools allocated size.

use turn_rs::router::ports::PortPools;

let mut pools = PortPools::new();
assert_eq!(pools.len(), 0);

pools.alloc(None).unwrap();
assert_eq!(pools.len(), 1);
Source

pub fn is_empty(&self) -> bool

get pools allocated size is empty.

use turn_rs::router::ports::PortPools;

let mut pools = PortPools::new();
assert_eq!(pools.len(), 0);
assert_eq!(pools.is_empty(), true);
Source

pub fn alloc(&mut self, si: Option<usize>) -> Option<u16>

random assign a port.

§Examples
use turn_rs::router::ports::PortPools;

let mut pool = PortPools::new();

assert_eq!(pool.alloc(Some(0)), Some(49152));
assert_eq!(pool.alloc(Some(0)), Some(49153));

assert!(pool.alloc(None).is_some());
Source

pub fn find_high(&self, i: usize) -> Option<u32>

find the high bit in the bucket.

§Examples
use turn_rs::router::ports::PortPools;

let mut pool = PortPools::new();

assert_eq!(pool.alloc(Some(0)), Some(49152));
assert_eq!(pool.alloc(Some(0)), Some(49153));

assert_eq!(pool.find_high(0), Some(2));
assert_eq!(pool.find_high(0), Some(2));
assert_eq!(pool.find_high(1), Some(0));
Source

pub fn write(&mut self, offset: usize, i: usize, bit: Bit)

write bit flag in the bucket.

§Examples
use turn_rs::router::ports::Bit;
use turn_rs::router::ports::PortPools;

let mut pool = PortPools::new();

assert_eq!(pool.alloc(Some(0)), Some(49152));
assert_eq!(pool.alloc(Some(0)), Some(49153));

pool.write(0, 0, Bit::High);
pool.write(0, 1, Bit::High);

assert_eq!(pool.alloc(Some(0)), Some(49154));
assert_eq!(pool.alloc(Some(0)), Some(49155));
Source

pub fn read(&self, o: usize, i: usize) -> Bit

read bucket bit value.

§Examples
use turn_rs::router::ports::Bit;
use turn_rs::router::ports::PortPools;

let mut pool = PortPools::new();

assert_eq!(pool.alloc(Some(0)), Some(49152));
assert_eq!(pool.alloc(Some(0)), Some(49153));

assert_eq!(pool.find_high(0), Some(2));
assert_eq!(pool.find_high(1), Some(0));

pool.write(0, 0, Bit::High);
pool.write(0, 1, Bit::High);

assert_eq!(pool.alloc(Some(0)), Some(49154));
assert_eq!(pool.alloc(Some(0)), Some(49155));

pool.restore(49152);
pool.restore(49153);

assert_eq!(pool.alloc(Some(0)), Some(49152));
assert_eq!(pool.alloc(Some(0)), Some(49153));
Source

pub fn restore(&mut self, port: u16)

restore port in the buckets.

§Examples
use turn_rs::router::ports::PortPools;

let mut pool = PortPools::new();

assert_eq!(pool.alloc(Some(0)), Some(49152));
assert_eq!(pool.alloc(Some(0)), Some(49153));

pool.restore(49152);
pool.restore(49153);

assert_eq!(pool.alloc(Some(0)), Some(49152));
assert_eq!(pool.alloc(Some(0)), Some(49153));
Source

pub fn random(&self) -> u16

get random buckets index.

§Examples
use turn_rs::router::ports::*;

let pool = PortPools::new();

let max = bucket_size() as u16;
let index = pool.random();
assert!((0..max - 1).contains(&index));

Trait Implementations§

Source§

impl Default for PortPools

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V