turn_rs/router/ports.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
use ahash::AHashMap;
use rand::{thread_rng, Rng};
use std::{
net::SocketAddr,
ops::Range,
sync::{Mutex, RwLock},
};
/// Bit Flag
#[derive(PartialEq)]
pub enum Bit {
Low,
High,
}
/// 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.
pub struct PortPools {
pub buckets: Vec<u64>,
allocated: usize,
bit_len: u32,
peak: usize,
}
impl Default for PortPools {
fn default() -> Self {
Self::new()
}
}
impl PortPools {
pub fn new() -> Self {
Self {
buckets: vec![0; bucket_size()],
peak: bucket_size() - 1,
bit_len: bit_len(),
allocated: 0,
}
}
/// 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);
/// ```
pub fn capacity(&self) -> usize {
capacity()
}
/// 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);
/// ```
pub fn len(&self) -> usize {
self.allocated
}
/// 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);
/// ```
pub fn is_empty(&self) -> bool {
self.allocated == 0
}
/// 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());
/// ```
#[rustfmt::skip]
pub fn alloc(&mut self, si: Option<usize>) -> Option<u16> {
let mut start = si.unwrap_or_else(|| self.random() as usize);
let previous = if start == 0 { self.peak } else { start - 1 };
let mut index = None;
// warn: loop
loop {
if let Some(i) = self.find_high(start) {
index = Some(i as usize);
break;
}
if start == self.peak {
start = 0;
} else {
start += 1;
}
if start == previous {
break;
}
}
let bi = match index {
None => return None,
Some(i) => i,
};
self.write(start, bi, Bit::High);
self.allocated += 1;
let num = (start * 64 + bi) as u16;
let port = port_range().start + num;
Some(port)
}
/// 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));
/// ```
pub fn find_high(&self, i: usize) -> Option<u32> {
let bucket = self.buckets[i];
let offset = if bucket < u64::MAX {
bucket.leading_ones()
} else {
return None;
};
if i == self.peak && offset > self.bit_len {
return None;
}
Some(offset)
}
/// 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));
/// ```
pub fn write(&mut self, offset: usize, i: usize, bit: Bit) {
let bucket = self.buckets[offset];
let high_mask = 1 << (63 - i);
let mask = match bit {
Bit::Low => u64::MAX ^ high_mask,
Bit::High => high_mask,
};
self.buckets[offset] = match bit {
Bit::High => bucket | mask,
Bit::Low => bucket & mask,
};
}
/// 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));
/// ```
pub fn read(&self, o: usize, i: usize) -> Bit {
match (self.buckets[o] & (1 << (63 - i))) >> (63 - i) {
0 => Bit::Low,
1 => Bit::High,
_ => panic!(),
}
}
/// 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));
/// ```
pub fn restore(&mut self, port: u16) {
assert!(port_range().contains(&port));
let offset = (port - port_range().start) as usize;
let bucket = offset / 64;
let bit = offset - (bucket * 64);
if self.read(bucket, bit) == Bit::Low {
return;
}
self.write(bucket, bit, Bit::Low);
self.allocated -= 1;
}
/// 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));
/// ```
pub fn random(&self) -> u16 {
let mut rng = thread_rng();
rng.gen_range(0..self.peak as u16)
}
}
/// port table.
pub struct Ports {
pools: Mutex<PortPools>,
map: RwLock<AHashMap<u16, SocketAddr>>,
bounds: RwLock<AHashMap<SocketAddr, AHashMap<SocketAddr, u16>>>,
}
impl Default for Ports {
fn default() -> Self {
Self::new()
}
}
impl Ports {
pub fn new() -> Self {
Self {
bounds: RwLock::new(AHashMap::with_capacity(capacity())),
map: RwLock::new(AHashMap::with_capacity(capacity())),
pools: Mutex::new(PortPools::new()),
}
}
/// get ports capacity.
///
/// # Examples
///
/// ```
/// use turn_rs::router::ports::*;
///
/// let ports = Ports::new();
/// assert_eq!(ports.capacity(), 65535 - 49152);
/// ```
pub fn capacity(&self) -> usize {
self.pools.lock().unwrap().capacity()
}
/// get ports allocated size.
///
/// # Examples
///
/// ```
/// use turn_rs::router::ports::*;
///
/// let ports = Ports::new();
/// assert_eq!(ports.len(), 0);
/// ```
pub fn len(&self) -> usize {
self.pools.lock().unwrap().len()
}
/// get ports allocated size is empty.
///
/// # Examples
///
/// ```
/// use turn_rs::router::ports::*;
///
/// let ports = Ports::new();
/// assert_eq!(ports.is_empty(), true);
/// ```
pub fn is_empty(&self) -> bool {
self.pools.lock().unwrap().len() == 0
}
/// get address from port.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use turn_rs::router::ports::*;
///
/// let ports = Ports::new();
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let port = ports.alloc(&addr).unwrap();
///
/// assert!(ports.get(port).is_some());
/// ```
pub fn get(&self, p: u16) -> Option<SocketAddr> {
self.map.read().unwrap().get(&p).cloned()
}
/// get address bound port.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use turn_rs::router::ports::*;
///
/// let local = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let peer = "127.0.0.1:8081".parse::<SocketAddr>().unwrap();
///
/// let pools = Ports::new();
///
/// let port = pools.alloc(&local).unwrap();
/// assert!(pools.bound(&local, port).is_some());
/// assert!(pools.bound(&peer, port).is_some());
///
/// assert_eq!(pools.get_bound(&local, &peer), Some(port));
/// ```
pub fn get_bound(&self, a: &SocketAddr, p: &SocketAddr) -> Option<u16> {
self.bounds.read().unwrap().get(p)?.get(a).cloned()
}
/// allocate port in ports.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use turn_rs::router::ports::*;
///
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
///
/// let pools = Ports::new();
/// assert_eq!(pools.alloc(&addr).is_some(), true);
/// ```
pub fn alloc(&self, a: &SocketAddr) -> Option<u16> {
let port = self.pools.lock().unwrap().alloc(None)?;
self.map.write().unwrap().insert(port, *a);
Some(port)
}
/// bound address and peer port.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use turn_rs::router::ports::*;
///
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
///
/// let pools = Ports::new();
/// let port = pools.alloc(&addr).unwrap();
///
/// assert!(pools.bound(&addr, port).is_some());
/// ```
pub fn bound(&self, addr: &SocketAddr, port: u16) -> Option<()> {
let peer = *self.map.read().unwrap().get(&port)?;
self.bounds
.write()
.unwrap()
.entry(*addr)
.or_insert_with(|| AHashMap::with_capacity(10))
.entry(peer)
.or_insert(port);
Some(())
}
/// bound address and peer port.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use turn_rs::router::ports::*;
///
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
///
/// let pools = Ports::new();
/// let port = pools.alloc(&addr).unwrap();
///
/// assert!(pools.bound(&addr, port).is_some());
/// assert!(pools.remove(&addr, &vec![port]).is_some());
/// ```
pub fn remove(&self, a: &SocketAddr, ports: &[u16]) -> Option<()> {
let mut pools = self.pools.lock().unwrap();
let mut map = self.map.write().unwrap();
for p in ports {
pools.restore(*p);
map.remove(p);
}
self.bounds.write().unwrap().remove(a);
Some(())
}
}
/// compute bucket size.
///
/// # Examples
///
/// ```
/// use turn_rs::router::ports::*;
///
/// assert_eq!(bucket_size(), 256);
/// ```
pub fn bucket_size() -> usize {
(capacity() as f32 / 64.0).ceil() as usize
}
/// compute bucket last bit max offset.
///
/// # Examples
///
/// ```
/// use turn_rs::router::ports::*;
///
/// assert_eq!(bit_len(), 63);
/// ```
pub fn bit_len() -> u32 {
(capacity() as f32 % 64.0).ceil() as u32
}
/// compute capacity.
///
/// # Examples
///
/// ```
/// use turn_rs::router::ports::*;
///
/// assert_eq!(capacity(), 65535 - 49152);
/// ```
pub const fn capacity() -> usize {
65535 - 49152
}
/// get port range.
///
/// # Examples
///
/// ```
/// use turn_rs::router::ports::*;
///
/// assert_eq!(port_range(), 49152..65535);
/// ```
pub const fn port_range() -> Range<u16> {
49152..65535
}