-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
encoding.rs
241 lines (209 loc) · 7.17 KB
/
encoding.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
// Internet2 addresses with support for Tor v3
//
// Written in 2019-2022 by
// Dr. Maxim Orlovsky <orlovsky@lnp-bp.org>
// Martin Habovstiak <martin.habovstiak@gmail.com>
//
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the MIT License along with this software.
// If not, see <https://opensource.org/licenses/MIT>.
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use strict_encoding::net::{
AddrFormat, DecodeError, RawAddr, Transport, Uniform, UniformAddr,
};
#[cfg(feature = "tor")]
use torut::onion::{TorPublicKeyV3, TORV3_PUBLIC_KEY_LENGTH};
use crate::inet::PartialSocketAddr;
use crate::{InetAddr, InetSocketAddr, InetSocketAddrExt};
impl strict_encoding::Strategy for InetAddr {
type Strategy = strict_encoding::strategies::UsingUniformAddr;
}
impl strict_encoding::Strategy for PartialSocketAddr {
type Strategy = strict_encoding::strategies::UsingUniformAddr;
}
impl strict_encoding::Strategy for InetSocketAddr {
type Strategy = strict_encoding::strategies::UsingUniformAddr;
}
impl strict_encoding::Strategy for InetSocketAddrExt {
type Strategy = strict_encoding::strategies::UsingUniformAddr;
}
impl Uniform for InetAddr {
#[inline]
fn addr_format(&self) -> AddrFormat {
match self {
InetAddr::IPv4(_) => AddrFormat::IpV4,
InetAddr::IPv6(_) => AddrFormat::IpV6,
#[cfg(feature = "tor")]
InetAddr::Tor(_) => AddrFormat::OnionV3,
}
}
#[inline]
fn addr(&self) -> RawAddr {
match self {
InetAddr::IPv4(ip) => ip.addr(),
InetAddr::IPv6(ip) => ip.addr(),
#[cfg(feature = "tor")]
InetAddr::Tor(tor) => {
use strict_encoding::net::ADDR_LEN;
let mut buf = [0u8; ADDR_LEN];
buf[1..].copy_from_slice(&tor.to_bytes());
buf
}
}
}
#[inline]
fn port(&self) -> Option<u16> { None }
#[inline]
fn transport(&self) -> Option<Transport> { None }
#[inline]
fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
where
Self: Sized,
{
if addr.port.is_some() || addr.transport.is_some() {
return Err(DecodeError::ExcessiveData);
}
Self::from_uniform_addr_lossy(addr)
}
#[inline]
fn from_uniform_addr_lossy(addr: UniformAddr) -> Result<Self, DecodeError>
where
Self: Sized,
{
Ok(match addr.addr_format {
AddrFormat::IpV4 => {
InetAddr::IPv4(Ipv4Addr::from_uniform_addr_lossy(addr)?)
}
AddrFormat::IpV6 => {
InetAddr::IPv6(Ipv6Addr::from_uniform_addr_lossy(addr)?)
}
#[cfg(feature = "tor")]
AddrFormat::OnionV3 => InetAddr::Tor(tor_from_raw_addr(addr.addr)?),
_ => return Err(DecodeError::UnsupportedAddrFormat),
})
}
}
impl Uniform for PartialSocketAddr {
fn addr_format(&self) -> AddrFormat { self.address().addr_format() }
fn addr(&self) -> RawAddr { self.address().addr() }
fn port(&self) -> Option<u16> { PartialSocketAddr::port(*self) }
fn transport(&self) -> Option<Transport> { None }
fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
where
Self: Sized,
{
if addr.transport.is_some() {
return Err(DecodeError::ExcessiveData);
}
Self::from_uniform_addr_lossy(addr)
}
fn from_uniform_addr_lossy(addr: UniformAddr) -> Result<Self, DecodeError>
where
Self: Sized,
{
if addr.port.is_some() {
InetSocketAddr::from_uniform_addr_lossy(addr)
.map(PartialSocketAddr::from)
} else {
InetAddr::from_uniform_addr_lossy(addr).map(PartialSocketAddr::from)
}
}
}
impl Uniform for InetSocketAddr {
#[inline]
fn addr_format(&self) -> AddrFormat { self.address().addr_format() }
#[inline]
fn addr(&self) -> RawAddr { self.address().addr() }
#[inline]
fn port(&self) -> Option<u16> {
match self {
InetSocketAddr::IPv4(socket) => Some(socket.port()),
InetSocketAddr::IPv6(socket) => Some(socket.port()),
#[cfg(feature = "tor")]
InetSocketAddr::Tor(_) => None,
}
}
#[inline]
fn transport(&self) -> Option<Transport> { None }
#[inline]
fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
where
Self: Sized,
{
if addr.transport.is_some() {
return Err(DecodeError::ExcessiveData);
}
Self::from_uniform_addr_lossy(addr)
}
#[inline]
fn from_uniform_addr_lossy(addr: UniformAddr) -> Result<Self, DecodeError>
where
Self: Sized,
{
Ok(match addr.addr_format {
AddrFormat::IpV4 => InetSocketAddr::IPv4(
SocketAddrV4::from_uniform_addr_lossy(addr)?,
),
AddrFormat::IpV6 => InetSocketAddr::IPv6(
SocketAddrV6::from_uniform_addr_lossy(addr)?,
),
#[cfg(feature = "tor")]
AddrFormat::OnionV3 => {
InetSocketAddr::Tor(tor_from_raw_addr(addr.addr)?)
}
_ => return Err(DecodeError::UnsupportedAddrFormat),
})
}
}
impl Uniform for InetSocketAddrExt {
#[inline]
fn addr_format(&self) -> AddrFormat { self.1.addr_format() }
#[inline]
fn addr(&self) -> RawAddr { self.1.addr() }
#[inline]
fn port(&self) -> Option<u16> { self.1.port() }
#[inline]
fn transport(&self) -> Option<Transport> {
Some(match self.0 {
crate::Transport::Tcp => Transport::Tcp,
crate::Transport::Udp => Transport::Udp,
crate::Transport::Mtcp => Transport::Mtcp,
crate::Transport::Quic => Transport::Quic,
})
}
#[inline]
fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
where
Self: Sized,
{
Self::from_uniform_addr_lossy(addr)
}
#[inline]
fn from_uniform_addr_lossy(addr: UniformAddr) -> Result<Self, DecodeError>
where
Self: Sized,
{
if let Some(transport) = addr.transport {
let address = InetSocketAddr::from_uniform_addr_lossy(addr)?;
let transport = match transport {
Transport::Tcp => crate::Transport::Tcp,
Transport::Udp => crate::Transport::Udp,
Transport::Mtcp => crate::Transport::Mtcp,
Transport::Quic => crate::Transport::Quic,
_ => return Err(DecodeError::UnknownTransport),
};
Ok(InetSocketAddrExt(transport, address))
} else {
Err(DecodeError::InsufficientData)
}
}
}
#[cfg(feature = "tor")]
fn tor_from_raw_addr(raw: RawAddr) -> Result<TorPublicKeyV3, DecodeError> {
let mut a = [0u8; TORV3_PUBLIC_KEY_LENGTH];
a.copy_from_slice(&raw[1..]);
TorPublicKeyV3::from_bytes(&a).map_err(|_| DecodeError::InvalidPubkey)
}