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
#[cfg(test)]
mod xoraddr_test;
use crate::addr::*;
use crate::attributes::*;
use crate::checks::*;
use crate::errors::*;
use crate::message::*;
use util::Error;
use std::fmt;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::mem;
const WORD_SIZE: usize = mem::size_of::<usize>();
fn safe_xorbytes(dst: &mut [u8], a: &[u8], b: &[u8]) -> usize {
let mut n = a.len();
if b.len() < n {
n = b.len();
}
if dst.len() < n {
n = dst.len();
}
for i in 0..n {
dst[i] = a[i] ^ b[i];
}
n
}
fn xor_bytes(dst: &mut [u8], a: &[u8], b: &[u8]) -> usize {
safe_xorbytes(dst, a, b)
}
pub struct XorMappedAddress {
pub ip: IpAddr,
pub port: u16,
}
impl Default for XorMappedAddress {
fn default() -> Self {
XorMappedAddress {
ip: IpAddr::V4(Ipv4Addr::from(0)),
port: 0,
}
}
}
impl fmt::Display for XorMappedAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let family = match self.ip {
IpAddr::V4(_) => FAMILY_IPV4,
IpAddr::V6(_) => FAMILY_IPV6,
};
if family == FAMILY_IPV4 {
write!(f, "{}:{}", self.ip, self.port)
} else {
write!(f, "[{}]:{}", self.ip, self.port)
}
}
}
impl Setter for XorMappedAddress {
fn add_to(&self, m: &mut Message) -> Result<(), Error> {
self.add_to_as(m, ATTR_XORMAPPED_ADDRESS)
}
}
impl Getter for XorMappedAddress {
fn get_from(&mut self, m: &Message) -> Result<(), Error> {
self.get_from_as(m, ATTR_XORMAPPED_ADDRESS)
}
}
impl XorMappedAddress {
pub fn add_to_as(&self, m: &mut Message, t: AttrType) -> Result<(), Error> {
let (family, ip_len, ip) = match self.ip {
IpAddr::V4(ipv4) => (FAMILY_IPV4, IPV4LEN, ipv4.octets().to_vec()),
IpAddr::V6(ipv6) => (FAMILY_IPV6, IPV6LEN, ipv6.octets().to_vec()),
};
let mut value = vec![0; 32 + 128];
let mut xor_value = vec![0; IPV6LEN];
xor_value[4..].copy_from_slice(&m.transaction_id.0);
xor_value[0..4].copy_from_slice(&MAGIC_COOKIE.to_be_bytes());
value[0..2].copy_from_slice(&family.to_be_bytes());
value[2..4].copy_from_slice(&(self.port ^ (MAGIC_COOKIE >> 16) as u16).to_be_bytes());
xor_bytes(&mut value[4..4 + ip_len], &ip, &xor_value);
m.add(t, &value[..4 + ip_len]);
Ok(())
}
pub fn get_from_as(&mut self, m: &Message, t: AttrType) -> Result<(), Error> {
let v = m.get(t)?;
if v.len() <= 4 {
return Err(ERR_UNEXPECTED_EOF.clone());
}
let family = u16::from_be_bytes([v[0], v[1]]);
if family != FAMILY_IPV6 && family != FAMILY_IPV4 {
return Err(Error::new(format!("bad value {}", family)));
}
check_overflow(
t,
v[4..].len(),
if family == FAMILY_IPV4 {
IPV4LEN
} else {
IPV6LEN
},
)?;
self.port = u16::from_be_bytes([v[2], v[3]]) ^ (MAGIC_COOKIE >> 16) as u16;
let mut xor_value = vec![0; 4 + TRANSACTION_ID_SIZE];
xor_value[0..4].copy_from_slice(&MAGIC_COOKIE.to_be_bytes());
xor_value[4..].copy_from_slice(&m.transaction_id.0);
if family == FAMILY_IPV6 {
let mut ip = [0; IPV6LEN];
xor_bytes(&mut ip, &v[4..], &xor_value);
self.ip = IpAddr::V6(Ipv6Addr::from(ip));
} else {
let mut ip = [0; IPV4LEN];
xor_bytes(&mut ip, &v[4..], &xor_value);
self.ip = IpAddr::V4(Ipv4Addr::from(ip));
};
Ok(())
}
}