rust_p2p_core/punch/
config.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
use crate::nat::NatInfo;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::ops;
use std::str::FromStr;

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Serialize, Deserialize)]
pub enum PunchModel {
    IPv4Tcp,
    IPv4Udp,
    IPv6Tcp,
    IPv6Udp,
}

impl ops::BitOr<PunchModel> for PunchModel {
    type Output = PunchModelBox;

    fn bitor(self, rhs: PunchModel) -> Self::Output {
        let mut model = PunchModelBox::empty();
        model.or(self);
        model.or(rhs);
        model
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PunchModelBox {
    models: HashSet<PunchModel>,
}

impl Default for PunchModelBox {
    fn default() -> Self {
        PunchModelBox::all()
    }
}

impl ops::BitOr<PunchModel> for PunchModelBox {
    type Output = PunchModelBox;

    fn bitor(mut self, rhs: PunchModel) -> Self::Output {
        self.or(rhs);
        self
    }
}

impl PunchModelBox {
    pub fn all() -> Self {
        PunchModel::IPv4Tcp | PunchModel::IPv4Udp | PunchModel::IPv6Tcp | PunchModel::IPv6Udp
    }
    pub fn ipv4() -> Self {
        PunchModel::IPv4Tcp | PunchModel::IPv4Udp
    }
    pub fn ipv6() -> Self {
        PunchModel::IPv6Tcp | PunchModel::IPv6Udp
    }
    pub fn empty() -> Self {
        Self {
            models: Default::default(),
        }
    }
    pub fn or(&mut self, punch_model: PunchModel) {
        self.models.insert(punch_model);
    }
    pub fn is_match(&self, punch_model: PunchModel) -> bool {
        self.models.contains(&punch_model)
    }
}

#[derive(Clone, Debug)]
pub struct PunchModelBoxes {
    boxes: Vec<PunchModelBox>,
}

impl ops::BitAnd<PunchModelBox> for PunchModelBox {
    type Output = PunchModelBoxes;

    fn bitand(self, rhs: PunchModelBox) -> Self::Output {
        let mut boxes = PunchModelBoxes::empty();
        boxes.and(rhs);
        boxes
    }
}

impl PunchModelBoxes {
    pub fn all() -> Self {
        Self {
            boxes: vec![PunchModelBox::all()],
        }
    }
    pub fn empty() -> Self {
        Self { boxes: Vec::new() }
    }
    pub fn and(&mut self, punch_model_box: PunchModelBox) {
        self.boxes.push(punch_model_box)
    }
    pub fn is_match(&self, punch_model: PunchModel) -> bool {
        if self.boxes.is_empty() {
            return false;
        }
        for x in &self.boxes {
            if !x.is_match(punch_model) {
                return false;
            }
        }
        true
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PunchConsultInfo {
    pub peer_punch_model: PunchModelBox,
    pub peer_nat_info: NatInfo,
}

impl PunchConsultInfo {
    pub fn new(peer_punch_model: PunchModelBox, peer_nat_info: NatInfo) -> Self {
        Self {
            peer_punch_model,
            peer_nat_info,
        }
    }
}

#[derive(Clone, Debug)]
pub struct PunchInfo {
    pub(crate) initiate_by_oneself: bool,
    pub(crate) punch_model: PunchModelBoxes,
    pub(crate) peer_nat_info: NatInfo,
}

impl PunchInfo {
    pub fn new(
        initiate_by_oneself: bool,
        punch_model: PunchModelBoxes,
        peer_nat_info: NatInfo,
    ) -> Self {
        Self {
            initiate_by_oneself,
            punch_model,
            peer_nat_info,
        }
    }
    pub fn new_by_oneself(punch_model: PunchModelBoxes, peer_nat_info: NatInfo) -> Self {
        Self {
            initiate_by_oneself: true,
            punch_model,
            peer_nat_info,
        }
    }
    pub fn new_by_other(punch_model: PunchModelBoxes, peer_nat_info: NatInfo) -> Self {
        Self {
            initiate_by_oneself: false,
            punch_model,
            peer_nat_info,
        }
    }
    pub(crate) fn use_ttl(&self) -> bool {
        self.initiate_by_oneself ^ (self.peer_nat_info.seq % 2 == 0)
    }
}

impl FromStr for PunchModel {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().trim() {
            "ipv4-tcp" => Ok(PunchModel::IPv4Tcp),
            "ipv4-udp" => Ok(PunchModel::IPv4Udp),
            "ipv6-tcp" => Ok(PunchModel::IPv6Tcp),
            "ipv6-udp" => Ok(PunchModel::IPv6Udp),
            _ => Err(format!(
                "not match '{}', enum: ipv4-tcp/ipv4-udp/ipv6-tcp/ipv6-udp",
                s
            )),
        }
    }
}