Skip to main content

rust_p2p_core/nat/
mod.rs

1use crate::extend::addr::is_ipv6_global;
2use serde::{Deserialize, Serialize};
3use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
4
5#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Default)]
6pub enum NatType {
7    #[default]
8    Cone,
9    Symmetric,
10}
11
12impl NatType {
13    #[inline]
14    pub fn is_cone(&self) -> bool {
15        self == &NatType::Cone
16    }
17    #[inline]
18    pub fn is_symmetric(&self) -> bool {
19        self == &NatType::Symmetric
20    }
21}
22
23#[derive(Clone, Debug, Serialize, Deserialize)]
24pub struct NatInfo {
25    /// nat type of the network
26    pub nat_type: NatType,
27    /// the set of public Ipv4
28    pub public_ips: Vec<Ipv4Addr>,
29    /// the set of public ports mapped from the nat
30    pub public_udp_ports: Vec<u16>,
31    /// the set of mapped addresses where `TCP` serves on
32    pub mapping_tcp_addr: Vec<SocketAddr>,
33    /// the set of mapped addresses where `UDP` serves on
34    pub mapping_udp_addr: Vec<SocketAddr>,
35    /// The predicted range of public ports, it is used when the nat_type is symmetric
36    pub public_port_range: u16,
37    /// local IP address
38    pub local_ipv4: Ipv4Addr,
39    #[serde(default)]
40    pub local_ipv4s: Vec<Ipv4Addr>,
41    /// The public IPv6 address
42    pub ipv6: Option<Ipv6Addr>,
43    /// The local ports where the `UDP` services bind
44    pub local_udp_ports: Vec<u16>,
45    /// The local ports where the `TCP` services bind
46    pub local_tcp_port: u16,
47    /// The public port of `TCP` service, which works when there is either `nat1` or no `nat` exists
48    pub public_tcp_port: u16,
49}
50impl NatInfo {
51    pub(crate) fn flag(&self) -> Option<SocketAddr> {
52        let vec = self.public_ipv4_addr();
53        if let Some(v) = vec.first() {
54            return Some(*v);
55        }
56        let vec = self.public_ipv4_tcp();
57        if let Some(v) = vec.first() {
58            return Some(*v);
59        }
60        let vec = self.local_ipv4_addrs();
61        if let Some(v) = vec.first() {
62            return Some(*v);
63        }
64        let option = self.local_ipv4_tcp();
65        if option.is_some() {
66            return option;
67        }
68        None
69    }
70    pub fn ipv6_udp_addr(&self) -> Vec<SocketAddr> {
71        if let Some(ipv6) = self.ipv6 {
72            if is_ipv6_global(&ipv6) {
73                return self
74                    .local_udp_ports
75                    .iter()
76                    .map(|&port| SocketAddrV6::new(ipv6, port, 0, 0).into())
77                    .collect();
78            }
79        }
80        vec![]
81    }
82    pub fn ipv6_tcp_addr(&self) -> Option<SocketAddr> {
83        self.ipv6
84            .map(|ipv6| SocketAddrV6::new(ipv6, self.local_tcp_port, 0, 0).into())
85    }
86    pub fn public_ipv4_addr(&self) -> Vec<SocketAddr> {
87        if self.public_ips.is_empty() || self.public_udp_ports.is_empty() {
88            return vec![];
89        }
90        if self.public_ips.len() == 1 {
91            let ip = self.public_ips[0];
92            return self
93                .public_udp_ports
94                .iter()
95                .map(|&port| SocketAddrV4::new(ip, port).into())
96                .collect();
97        }
98        let port = self.public_udp_ports[0];
99        self.public_ips
100            .iter()
101            .map(|&ip| SocketAddrV4::new(ip, port).into())
102            .collect()
103    }
104    pub fn local_ipv4_addrs(&self) -> Vec<SocketAddr> {
105        if self.local_udp_ports.is_empty() {
106            return vec![];
107        }
108        if !self.local_ipv4s.is_empty() {
109            let mut rs = Vec::with_capacity(self.local_ipv4s.len() * self.local_udp_ports.len());
110            for ip in self.local_ipv4s.iter() {
111                if ip.is_unspecified() || ip.is_multicast() || ip.is_broadcast() {
112                    continue;
113                }
114                for port in self.local_udp_ports.iter() {
115                    rs.push(SocketAddrV4::new(*ip, *port).into());
116                }
117            }
118            return rs;
119        }
120        if self.local_ipv4.is_unspecified()
121            || self.local_ipv4.is_multicast()
122            || self.local_ipv4.is_broadcast()
123        {
124            return vec![];
125        }
126        self.local_udp_ports
127            .iter()
128            .map(|&port| SocketAddrV4::new(self.local_ipv4, port).into())
129            .collect()
130    }
131    pub fn local_ipv4_tcp(&self) -> Option<SocketAddr> {
132        if self.local_ipv4.is_unspecified()
133            || self.local_ipv4.is_multicast()
134            || self.local_ipv4.is_broadcast()
135            || self.local_tcp_port == 0
136        {
137            return None;
138        }
139        Some(SocketAddrV4::new(self.local_ipv4, self.local_tcp_port).into())
140    }
141    pub fn public_ipv4_tcp(&self) -> Vec<SocketAddr> {
142        if self.public_tcp_port == 0 {
143            return vec![];
144        }
145        self.public_ips
146            .iter()
147            .map(|&ip| SocketAddrV4::new(ip, self.public_tcp_port).into())
148            .collect()
149    }
150}