rust_p2p_core/route/
mod.rs1use std::fmt;
2use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
3
4pub mod route_table;
5
6pub const DEFAULT_RTT: u32 = 9999;
7
8use crate::tunnel::udp::UDPIndex;
9#[non_exhaustive]
10#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
11pub enum Index {
12 Udp(UDPIndex),
13 Tcp(usize),
14}
15impl Index {
16 pub fn index(&self) -> usize {
17 match self {
18 Index::Udp(index) => index.index(),
19 Index::Tcp(index) => *index,
20 }
21 }
22 pub fn protocol(&self) -> ConnectProtocol {
23 match self {
24 Index::Tcp(_) => ConnectProtocol::TCP,
25 Index::Udp(_) => ConnectProtocol::UDP,
26 }
27 }
28}
29#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
30pub struct RouteKey {
31 index: Index,
32 addr: SocketAddr,
33}
34impl Default for RouteKey {
35 fn default() -> Self {
36 Self {
37 index: Index::Tcp(0),
38 addr: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)),
39 }
40 }
41}
42impl RouteKey {
43 pub(crate) const fn new(index: Index, addr: SocketAddr) -> Self {
44 Self { index, addr }
45 }
46 #[inline]
47 pub fn protocol(&self) -> ConnectProtocol {
48 self.index.protocol()
49 }
50 #[inline]
51 pub fn index(&self) -> Index {
52 self.index
53 }
54 #[inline]
55 pub fn index_usize(&self) -> usize {
56 self.index.index()
57 }
58 #[inline]
59 pub fn addr(&self) -> SocketAddr {
60 self.addr
61 }
62}
63#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
64pub struct RouteSortKey {
65 metric: u8,
66 rtt: u32,
67}
68
69#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
70pub enum ConnectProtocol {
71 UDP,
72 TCP,
73}
74impl ConnectProtocol {
75 #[inline]
76 pub fn is_tcp(&self) -> bool {
77 self == &ConnectProtocol::TCP
78 }
79 #[inline]
80 pub fn is_udp(&self) -> bool {
81 self == &ConnectProtocol::UDP
82 }
83}
84
85impl fmt::Display for RouteKey {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 let protocol = self.protocol();
88 write!(f, "{}://{}", protocol, self.addr())
89 }
90}
91
92impl fmt::Display for ConnectProtocol {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 match self {
95 ConnectProtocol::UDP => write!(f, "udp"),
96 ConnectProtocol::TCP => write!(f, "tcp"),
97 }
98 }
99}