1
//! Encoding and decoding for relay messages
2
//!
3
//! Relay messages are sent along circuits, inside RELAY or RELAY_EARLY
4
//! cells.
5

            
6
use super::msg;
7
use crate::chancell::CELL_DATA_LEN;
8
use derive_deftly::Deftly;
9
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
10
use std::str::FromStr;
11
use tor_bytes::{EncodeResult, Error, Result};
12
use tor_bytes::{Readable, Reader, Writeable, Writer};
13
use tor_memquota::derive_deftly_template_HasMemoryCost;
14

            
15
/// Indicates the payload is a hostname.
16
const T_HOSTNAME: u8 = 0x01;
17
/// Indicates the payload is an IPv4.
18
const T_IPV4: u8 = 0x04;
19
/// Indicates the payload is an IPv6.
20
const T_IPV6: u8 = 0x06;
21

            
22
/// Maximum length of an Address::Hostname set at 255.
23
const MAX_HOSTNAME_LEN: usize = u8::MAX as usize;
24

            
25
/// Address contained in a ConnectUdp and ConnectedUdp cell which can
26
/// represent a hostname, IPv4 or IPv6 along a port number.
27
#[derive(Clone, Debug, Eq, PartialEq, Deftly)]
28
#[derive_deftly(HasMemoryCost)]
29
pub struct AddressPort {
30
    /// Address.
31
    addr: Address,
32
    /// Port.
33
    port: u16,
34
}
35

            
36
impl Readable for AddressPort {
37
1029
    fn take_from(r: &mut Reader<'_>) -> Result<Self> {
38
1029
        Ok(Self {
39
1029
            addr: r.extract()?,
40
637
            port: r.take_u16()?,
41
        })
42
1029
    }
43
}
44

            
45
impl Writeable for AddressPort {
46
36
    fn write_onto<B: Writer + ?Sized>(&self, w: &mut B) -> EncodeResult<()> {
47
36
        w.write(&self.addr)?;
48
36
        w.write_u16(self.port);
49
36
        Ok(())
50
36
    }
51
}
52

            
53
impl TryFrom<(&str, u16)> for AddressPort {
54
    type Error = Error;
55

            
56
441
    fn try_from(value: (&str, u16)) -> Result<Self> {
57
441
        let addr = Address::from_str(value.0)?;
58
441
        Ok(Self {
59
441
            addr,
60
441
            port: value.1,
61
441
        })
62
441
    }
63
}
64

            
65
/// Address representing either a hostname, IPv4 or IPv6.
66
#[derive(Clone, Debug, Eq, PartialEq, Deftly)]
67
#[derive_deftly(HasMemoryCost)]
68
#[non_exhaustive]
69
pub enum Address {
70
    /// Hostname
71
    Hostname(Vec<u8>),
72
    /// IP version 4 address
73
    Ipv4(Ipv4Addr),
74
    /// IP version 6 address
75
    Ipv6(Ipv6Addr),
76
}
77

            
78
impl Address {
79
    /// Return true iff this is a Hostname.
80
784
    pub fn is_hostname(&self) -> bool {
81
784
        matches!(self, Address::Hostname(_))
82
784
    }
83

            
84
    /// Return the cell wire format address type value.
85
882
    fn wire_addr_type(&self) -> u8 {
86
882
        match self {
87
294
            Address::Hostname(_) => T_HOSTNAME,
88
294
            Address::Ipv4(_) => T_IPV4,
89
294
            Address::Ipv6(_) => T_IPV6,
90
        }
91
882
    }
92
}
93

            
94
impl Readable for Address {
95
1029
    fn take_from(r: &mut Reader<'_>) -> Result<Self> {
96
1029
        let addr_type = r.take_u8()?;
97
1049
        r.read_nested_u8len(|r| {
98
980
            Ok(match addr_type {
99
                T_HOSTNAME => {
100
294
                    let h = r.take_rest();
101
294
                    Self::Hostname(h.into())
102
                }
103
343
                T_IPV4 => Self::Ipv4(r.extract()?),
104
294
                T_IPV6 => Self::Ipv6(r.extract()?),
105
49
                _ => return Err(Error::InvalidMessage("Invalid address type".into())),
106
            })
107
1049
        })
108
1029
    }
109
}
110

            
111
impl Writeable for Address {
112
36
    fn write_onto<B: Writer + ?Sized>(&self, w: &mut B) -> EncodeResult<()> {
113
36
        // Address type.
114
36
        w.write_u8(self.wire_addr_type());
115
36
        // Address length and data.
116
36
        let mut w = w.write_nested_u8len();
117
36

            
118
36
        match self {
119
12
            Address::Hostname(h) => {
120
12
                w.write_all(&h[..]);
121
12
            }
122
12
            Address::Ipv4(ip) => w.write(ip)?,
123
12
            Address::Ipv6(ip) => w.write(ip)?,
124
        }
125

            
126
36
        w.finish()
127
36
    }
128
}
129

            
130
impl FromStr for Address {
131
    type Err = Error;
132

            
133
1078
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
134
1078
        if let Ok(ipv4) = Ipv4Addr::from_str(s) {
135
196
            Ok(Self::Ipv4(ipv4))
136
882
        } else if let Ok(ipv6) = Ipv6Addr::from_str(s) {
137
196
            Ok(Self::Ipv6(ipv6))
138
        } else {
139
686
            if s.len() > MAX_HOSTNAME_LEN {
140
49
                return Err(Error::InvalidMessage("Hostname too long".into()));
141
637
            }
142
637
            if s.contains('\0') {
143
49
                return Err(Error::InvalidMessage("Nul byte not permitted".into()));
144
588
            }
145
588

            
146
588
            let mut addr = s.to_string();
147
588
            addr.make_ascii_lowercase();
148
588
            Ok(Self::Hostname(addr.into_bytes()))
149
        }
150
1078
    }
151
}
152

            
153
impl From<IpAddr> for Address {
154
    fn from(ip: IpAddr) -> Self {
155
        match ip {
156
            IpAddr::V4(ip) => Address::Ipv4(ip),
157
            IpAddr::V6(ip) => Address::Ipv6(ip),
158
        }
159
    }
160
}
161

            
162
/// A ConnectUdp message creates a new UDP data stream.
163
///
164
/// Upon receiving a ConnectUdp message, a relay tries to connect to the given address with the UDP
165
/// protocol if the exit policy permits.
166
///
167
/// If the exit decides to reject the message, or if the UDP connection fails, the exit should send
168
/// an End message.
169
///
170
/// Clients should reject these messages.
171
#[derive(Debug, Clone, Deftly)]
172
#[derive_deftly(HasMemoryCost)]
173
pub struct ConnectUdp {
174
    /// Same as Begin flags.
175
    flags: msg::BeginFlags,
176
    /// Address to connect to. Can be Hostname, IPv4 or IPv6.
177
    addr: AddressPort,
178
}
179

            
180
impl ConnectUdp {
181
    /// Construct a new ConnectUdp cell
182
10
    pub fn new<F>(addr: &str, port: u16, flags: F) -> crate::Result<Self>
183
10
    where
184
10
        F: Into<msg::BeginFlags>,
185
10
    {
186
10
        Ok(Self {
187
10
            addr: (addr, port)
188
10
                .try_into()
189
10
                .map_err(|_| crate::Error::BadStreamAddress)?,
190
10
            flags: flags.into(),
191
        })
192
10
    }
193
}
194

            
195
impl msg::Body for ConnectUdp {
196
686
    fn decode_from_reader(r: &mut Reader<'_>) -> Result<Self> {
197
686
        let flags = r.take_u32()?;
198
686
        let addr = r.extract()?;
199

            
200
294
        Ok(Self {
201
294
            flags: flags.into(),
202
294
            addr,
203
294
        })
204
686
    }
205

            
206
20
    fn encode_onto<W: Writer + ?Sized>(self, w: &mut W) -> EncodeResult<()> {
207
20
        w.write_u32(self.flags.bits());
208
20
        w.write(&self.addr)?;
209
20
        Ok(())
210
20
    }
211
}
212

            
213
/// A ConnectedUdp cell sent in response to a ConnectUdp.
214
#[derive(Debug, Clone, Deftly)]
215
#[derive_deftly(HasMemoryCost)]
216
pub struct ConnectedUdp {
217
    /// The address that the relay has bound locally of a ConnectUdp. Note
218
    /// that this might not be the relay address from the descriptor.
219
    our_address: AddressPort,
220
    /// The address that the stream is connected to.
221
    their_address: AddressPort,
222
}
223

            
224
impl ConnectedUdp {
225
    /// Construct a new ConnectedUdp cell.
226
98
    pub fn new(our_address: AddressPort, their_address: AddressPort) -> Result<Self> {
227
98
        Ok(Self {
228
98
            our_address,
229
98
            their_address,
230
98
        })
231
98
    }
232
}
233

            
234
impl msg::Body for ConnectedUdp {
235
196
    fn decode_from_reader(r: &mut Reader<'_>) -> Result<Self> {
236
196
        let our_address: AddressPort = r.extract()?;
237
196
        if our_address.addr.is_hostname() {
238
49
            return Err(Error::InvalidMessage("Our address is a Hostname".into()));
239
147
        }
240
147
        let their_address: AddressPort = r.extract()?;
241
147
        if their_address.addr.is_hostname() {
242
49
            return Err(Error::InvalidMessage("Their address is a Hostname".into()));
243
98
        }
244
98

            
245
98
        Ok(Self {
246
98
            our_address,
247
98
            their_address,
248
98
        })
249
196
    }
250

            
251
8
    fn encode_onto<W: Writer + ?Sized>(self, w: &mut W) -> EncodeResult<()> {
252
8
        w.write(&self.our_address)?;
253
8
        w.write(&self.their_address)?;
254
8
        Ok(())
255
8
    }
256
}
257

            
258
/// A Datagram message represents data sent along a UDP stream.
259
///
260
/// Upon receiving a Datagram message for a live stream, the client or
261
/// exit sends that data onto the associated UDP connection.
262
///
263
/// These messages hold between 1 and [Datagram::MAXLEN] bytes of data each.
264
#[derive(Debug, Clone, Deftly)]
265
#[derive_deftly(HasMemoryCost)]
266
pub struct Datagram {
267
    /// Contents of the cell, to be sent on a specific stream
268
    body: Vec<u8>,
269
}
270

            
271
impl Datagram {
272
    /// NOTE: Proposal 340, fragmented relay message, might change this value reality.
273
    /// The longest allowable body length for a single data cell.
274
    pub const MAXLEN: usize = CELL_DATA_LEN - 11;
275

            
276
    /// Construct a new data cell.
277
    ///
278
    /// Returns an error if `inp` is longer than [`Datagram::MAXLEN`] bytes.
279
    pub fn new(inp: &[u8]) -> crate::Result<Self> {
280
        if inp.len() > msg::Data::MAXLEN {
281
            return Err(crate::Error::CantEncode("Datagram too long"));
282
        }
283
        Ok(Self::new_unchecked(inp.into()))
284
    }
285

            
286
    /// Construct a new cell from a provided vector of bytes.
287
    ///
288
    /// The vector _must_ have fewer than [`Datagram::MAXLEN`] bytes.
289
    fn new_unchecked(body: Vec<u8>) -> Self {
290
        Self { body }
291
    }
292
}
293

            
294
impl From<Datagram> for Vec<u8> {
295
    fn from(data: Datagram) -> Vec<u8> {
296
        data.body
297
    }
298
}
299

            
300
impl AsRef<[u8]> for Datagram {
301
    fn as_ref(&self) -> &[u8] {
302
        &self.body[..]
303
    }
304
}
305

            
306
impl msg::Body for Datagram {
307
    fn decode_from_reader(r: &mut Reader<'_>) -> Result<Self> {
308
        Ok(Datagram {
309
            body: r.take(r.remaining())?.into(),
310
        })
311
    }
312

            
313
    fn encode_onto<W: Writer + ?Sized>(self, w: &mut W) -> EncodeResult<()> {
314
        w.write_all(&self.body);
315
        Ok(())
316
    }
317
}