1
#![cfg_attr(docsrs, feature(doc_cfg))]
2
#![doc = include_str!("../README.md")]
3
// @@ begin lint list maintained by maint/add_warning @@
4
#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5
#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6
#![warn(missing_docs)]
7
#![warn(noop_method_call)]
8
#![warn(unreachable_pub)]
9
#![warn(clippy::all)]
10
#![deny(clippy::await_holding_lock)]
11
#![deny(clippy::cargo_common_metadata)]
12
#![deny(clippy::cast_lossless)]
13
#![deny(clippy::checked_conversions)]
14
#![warn(clippy::cognitive_complexity)]
15
#![deny(clippy::debug_assert_with_mut_call)]
16
#![deny(clippy::exhaustive_enums)]
17
#![deny(clippy::exhaustive_structs)]
18
#![deny(clippy::expl_impl_clone_on_copy)]
19
#![deny(clippy::fallible_impl_from)]
20
#![deny(clippy::implicit_clone)]
21
#![deny(clippy::large_stack_arrays)]
22
#![warn(clippy::manual_ok_or)]
23
#![deny(clippy::missing_docs_in_private_items)]
24
#![warn(clippy::needless_borrow)]
25
#![warn(clippy::needless_pass_by_value)]
26
#![warn(clippy::option_option)]
27
#![deny(clippy::print_stderr)]
28
#![deny(clippy::print_stdout)]
29
#![warn(clippy::rc_buffer)]
30
#![deny(clippy::ref_option_ref)]
31
#![warn(clippy::semicolon_if_nothing_returned)]
32
#![warn(clippy::trait_duplication_in_bounds)]
33
#![deny(clippy::unchecked_time_subtraction)]
34
#![deny(clippy::unnecessary_wraps)]
35
#![warn(clippy::unseparated_literal_suffix)]
36
#![deny(clippy::unwrap_used)]
37
#![deny(clippy::mod_module_files)]
38
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39
#![allow(clippy::uninlined_format_args)]
40
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43
#![allow(clippy::needless_lifetimes)] // See arti#1765
44
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45
#![allow(clippy::collapsible_if)] // See arti#2342
46
#![deny(clippy::unused_async)]
47
#![deny(clippy::string_slice)] // See arti#2571
48
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
49

            
50
#![allow(non_upper_case_globals)]
51
#![allow(clippy::upper_case_acronyms)]
52

            
53
use std::sync::Arc;
54

            
55
use caret::caret_int;
56

            
57
use thiserror::Error;
58
use tor_basic_utils::intern::InternCache;
59

            
60
pub mod named;
61

            
62
caret_int! {
63
    /// A recognized subprotocol.
64
    ///
65
    /// These names are kept in sync with the names used in consensus
66
    /// documents; the values are kept in sync with the values in the
67
    /// cbor document format in the walking onions proposal.
68
    ///
69
    /// For the full semantics of each subprotocol, see tor-spec.txt.
70
    #[derive(Hash,Ord,PartialOrd)]
71
    pub struct ProtoKind(u8) {
72
        /// Initiating and receiving channels, and getting cells on them.
73
        Link = 0,
74
        /// Different kinds of authenticate cells
75
        LinkAuth = 1,
76
        /// CREATE cells, CREATED cells, and the encryption that they
77
        /// create.
78
        Relay = 2,
79
        /// Serving and fetching network directory documents.
80
        DirCache = 3,
81
        /// Serving onion service descriptors
82
        HSDir = 4,
83
        /// Providing an onion service introduction point
84
        HSIntro = 5,
85
        /// Providing an onion service rendezvous point
86
        HSRend = 6,
87
        /// Describing a relay's functionality using router descriptors.
88
        Desc = 7,
89
        /// Describing a relay's functionality using microdescriptors.
90
        Microdesc = 8,
91
        /// Describing the network as a consensus directory document.
92
        Cons = 9,
93
        /// Sending and accepting circuit-level padding
94
        Padding = 10,
95
        /// Improved means of flow control on circuits.
96
        FlowCtrl = 11,
97
        /// Multi-path circuit support.
98
        Conflux = 12,
99
    }
100
}
101

            
102
/// How many recognized protocols are there?
103
const N_RECOGNIZED: usize = 13;
104

            
105
/// Maximum allowable value for a protocol's version field.
106
const MAX_VER: usize = 63;
107

            
108
/// A specific, named subversion of a protocol.
109
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
110
pub struct NamedSubver {
111
    /// The protocol in question
112
    ///
113
    /// Must be in-range for ProtoKind (0..N_RECOGNIZED).
114
    kind: ProtoKind,
115
    /// The version of the protocol
116
    ///
117
    /// Must be in 0..=MAX_VER
118
    version: u8,
119
}
120

            
121
impl NamedSubver {
122
    /// Create a new NamedSubver.
123
    ///
124
    /// # Panics
125
    ///
126
    /// Panics if `kind` is unrecognized or `version` is invalid.
127
    const fn new(kind: ProtoKind, version: u8) -> Self {
128
        assert!((kind.0 as usize) < N_RECOGNIZED);
129
        assert!((version as usize) <= MAX_VER);
130
        Self { kind, version }
131
    }
132
}
133

            
134
/// A subprotocol capability as represented by a (kind, version) tuple.
135
///
136
/// Does not necessarily represent a real subprotocol capability;
137
/// this type is meant for use in other pieces of the protocol.
138
///
139
/// # Ordering
140
///
141
/// Instances of `NumberedSubver` are sorted in lexicographic order by
142
/// their (kind, version) tuples.
143
//
144
// TODO: As with most other types in the crate, we should decide how to rename them as as part
145
// of #1934.
146
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq)]
147
pub struct NumberedSubver {
148
    /// The protocol in question
149
    kind: ProtoKind,
150
    /// The version of the protocol
151
    version: u8,
152
}
153

            
154
impl NumberedSubver {
155
    /// Construct a new [`NumberedSubver`]
156
460
    pub fn new(kind: impl Into<ProtoKind>, version: u8) -> Self {
157
460
        Self {
158
460
            kind: kind.into(),
159
460
            version,
160
460
        }
161
460
    }
162
    /// Return the ProtoKind and version for this [`NumberedSubver`].
163
    pub fn into_parts(self) -> (ProtoKind, u8) {
164
        (self.kind, self.version)
165
    }
166
}
167
impl From<NamedSubver> for NumberedSubver {
168
512
    fn from(value: NamedSubver) -> Self {
169
512
        Self {
170
512
            kind: value.kind,
171
512
            version: value.version,
172
512
        }
173
512
    }
174
}
175

            
176
#[cfg(feature = "tor-bytes")]
177
impl tor_bytes::Readable for NumberedSubver {
178
512
    fn take_from(b: &mut tor_bytes::Reader<'_>) -> tor_bytes::Result<Self> {
179
512
        let kind = b.take_u8()?;
180
512
        let version = b.take_u8()?;
181
448
        Ok(Self::new(kind, version))
182
512
    }
183
}
184

            
185
#[cfg(feature = "tor-bytes")]
186
impl tor_bytes::Writeable for NumberedSubver {
187
4
    fn write_onto<B: tor_bytes::Writer + ?Sized>(&self, b: &mut B) -> tor_bytes::EncodeResult<()> {
188
4
        b.write_u8(self.kind.into());
189
4
        b.write_u8(self.version);
190
4
        Ok(())
191
4
    }
192
}
193

            
194
/// Representation for a known or unknown protocol.
195
#[derive(Eq, PartialEq, Clone, Debug, Hash, Ord, PartialOrd)]
196
enum Protocol {
197
    /// A known protocol; represented by one of ProtoKind.
198
    ///
199
    /// ProtoKind must always be in the range 0..N_RECOGNIZED.
200
    Proto(ProtoKind),
201
    /// An unknown protocol; represented by its name.
202
    Unrecognized(String),
203
}
204

            
205
impl Protocol {
206
    /// Return true iff `s` is the name of a protocol we do not recognize.
207
678
    fn is_unrecognized(&self, s: &str) -> bool {
208
678
        match self {
209
678
            Protocol::Unrecognized(s2) => s2 == s,
210
            _ => false,
211
        }
212
678
    }
213
    /// Return a string representation of this protocol.
214
1220
    fn to_str(&self) -> &str {
215
1220
        match self {
216
            Protocol::Proto(k) => k.to_str().unwrap_or("<bug>"),
217
1220
            Protocol::Unrecognized(s) => s,
218
        }
219
1220
    }
220
}
221

            
222
/// Representation of a set of versions supported by a protocol.
223
///
224
/// For now, we only use this type for unrecognized protocols.
225
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
226
struct SubprotocolEntry {
227
    /// Which protocol's versions does this describe?
228
    proto: Protocol,
229
    /// A bit-vector defining which versions are supported.  If bit
230
    /// `(1<<i)` is set, then protocol version `i` is supported.
231
    supported: u64,
232
}
233

            
234
/// A set of supported or required subprotocol versions.
235
///
236
/// This type supports both recognized subprotocols (listed in ProtoKind),
237
/// and unrecognized subprotocols (stored by name).
238
///
239
/// To construct an instance, use the FromStr trait:
240
/// ```
241
/// use tor_protover::Protocols;
242
/// let p: Result<Protocols,_> = "Link=1-3 LinkAuth=2-3 Relay=1-2".parse();
243
/// ```
244
///
245
/// # Implementation notes
246
///
247
/// Because the number of distinct `Protocols` sets at any given time
248
/// is much smaller than the number of relays, this type is interned in order to
249
/// save memory and copying time.
250
///
251
/// This type is an Arc internally; it is cheap to clone.
252
#[derive(Debug, Clone, Default, Eq, PartialEq, Hash)]
253
#[cfg_attr(
254
    feature = "serde",
255
    derive(serde_with::DeserializeFromStr, serde_with::SerializeDisplay)
256
)]
257
pub struct Protocols(Arc<ProtocolsInner>);
258

            
259
/// Inner representation of Protocols.
260
///
261
/// We make this a separate type so that we can intern it inside an Arc.
262
#[derive(Default, Clone, Debug, Eq, PartialEq, Hash)]
263
struct ProtocolsInner {
264
    /// A mapping from protocols' integer encodings to bit-vectors.
265
    recognized: [u64; N_RECOGNIZED],
266
    /// A vector of unrecognized protocol versions,
267
    /// in sorted order.
268
    ///
269
    /// Every entry in this list has supported != 0.
270
    unrecognized: Vec<SubprotocolEntry>,
271
}
272

            
273
/// An InternCache of ProtocolsInner.
274
///
275
/// We intern ProtocolsInner objects because:
276
///  - There are very few _distinct_ values in any given set of relays.
277
///  - Every relay has one.
278
///  - We often want to copy them when we're remembering information about circuits.
279
static PROTOCOLS: InternCache<ProtocolsInner> = InternCache::new();
280

            
281
impl From<ProtocolsInner> for Protocols {
282
629904
    fn from(value: ProtocolsInner) -> Self {
283
629904
        Protocols(PROTOCOLS.intern(value))
284
629904
    }
285
}
286

            
287
impl Protocols {
288
    /// Return a new empty set of protocol versions.
289
    ///
290
    /// # Warning
291
    ///
292
    /// To the extend possible, avoid using empty lists to represent the capabilities
293
    /// of an unknown target.  Instead, if there is a consensus present, use the
294
    /// `required-relay-protocols` field of the consensus.
295
384
    pub fn new() -> Self {
296
384
        Protocols::default()
297
384
    }
298

            
299
    /// Helper: return true iff this protocol set contains the
300
    /// version `ver` of the protocol represented by the integer `proto`.
301
1571536
    fn supports_recognized_ver(&self, proto: usize, ver: u8) -> bool {
302
1571536
        if usize::from(ver) > MAX_VER {
303
2
            return false;
304
1571534
        }
305
1571534
        if proto >= self.0.recognized.len() {
306
            return false;
307
1571534
        }
308
1571534
        (self.0.recognized[proto] & (1 << ver)) != 0
309
1571536
    }
310
    /// Helper: return true iff this protocol set contains version
311
    /// `ver` of the unrecognized protocol represented by the string
312
    /// `proto`.
313
    ///
314
    /// Requires that `proto` is not the name of a recognized protocol.
315
10
    fn supports_unrecognized_ver(&self, proto: &str, ver: u8) -> bool {
316
10
        if usize::from(ver) > MAX_VER {
317
2
            return false;
318
8
        }
319
8
        let ent = self
320
8
            .0
321
8
            .unrecognized
322
8
            .iter()
323
12
            .find(|ent| ent.proto.is_unrecognized(proto));
324
8
        match ent {
325
4
            Some(e) => (e.supported & (1 << ver)) != 0,
326
4
            None => false,
327
        }
328
10
    }
329

            
330
    /// Return true if this list of protocols is empty.
331
960
    pub fn is_empty(&self) -> bool {
332
10127
        self.0.recognized.iter().all(|v| *v == 0)
333
640
            && self.0.unrecognized.iter().all(|p| p.supported == 0)
334
960
    }
335

            
336
    // TODO: Combine these next two functions into one by using a trait.
337
    /// Check whether a known protocol version is supported.
338
    ///
339
    /// ```
340
    /// use tor_protover::*;
341
    /// let protos: Protocols = "Link=1-3 HSDir=2,4-5".parse().unwrap();
342
    ///
343
    /// assert!(protos.supports_known_subver(ProtoKind::Link, 2));
344
    /// assert!(protos.supports_known_subver(ProtoKind::HSDir, 4));
345
    /// assert!(! protos.supports_known_subver(ProtoKind::HSDir, 3));
346
    /// assert!(! protos.supports_known_subver(ProtoKind::LinkAuth, 3));
347
    /// ```
348
1571404
    pub fn supports_known_subver(&self, proto: ProtoKind, ver: u8) -> bool {
349
1571404
        self.supports_recognized_ver(proto.get() as usize, ver)
350
1571404
    }
351
    /// Check whether a protocol version identified by a string is supported.
352
    ///
353
    /// ```
354
    /// use tor_protover::*;
355
    /// let protos: Protocols = "Link=1-3 Foobar=7".parse().unwrap();
356
    ///
357
    /// assert!(protos.supports_subver("Link", 2));
358
    /// assert!(protos.supports_subver("Foobar", 7));
359
    /// assert!(! protos.supports_subver("Link", 5));
360
    /// assert!(! protos.supports_subver("Foobar", 6));
361
    /// assert!(! protos.supports_subver("Wombat", 3));
362
    /// ```
363
142
    pub fn supports_subver(&self, proto: &str, ver: u8) -> bool {
364
142
        match ProtoKind::from_name(proto) {
365
132
            Some(p) => self.supports_recognized_ver(p.get() as usize, ver),
366
10
            None => self.supports_unrecognized_ver(proto, ver),
367
        }
368
142
    }
369

            
370
    /// Check whether a protocol version is supported.
371
    ///
372
    /// ```
373
    /// use tor_protover::*;
374
    /// let protos: Protocols = "Link=1-5 Desc=2-4".parse().unwrap();
375
    /// assert!(protos.supports_named_subver(named::DESC_FAMILY_IDS)); // Desc=4
376
    /// assert!(! protos.supports_named_subver(named::CONFLUX_BASE)); // Conflux=1
377
    /// ```
378
1570816
    pub fn supports_named_subver(&self, protover: NamedSubver) -> bool {
379
1570816
        self.supports_known_subver(protover.kind, protover.version)
380
1570816
    }
381

            
382
    /// Check whether a numbered subprotocol capability is supported.
383
    ///
384
    /// ```
385
    /// use tor_protover::*;
386
    /// let protos: Protocols = "Link=1-5 Desc=2-4".parse().unwrap();
387
    /// assert!(protos.supports_numbered_subver(NumberedSubver::new(ProtoKind::Desc, 4)));
388
    /// assert!(! protos.supports_numbered_subver(NumberedSubver::new(ProtoKind::Conflux, 1)));
389
    /// ```
390
576
    pub fn supports_numbered_subver(&self, protover: NumberedSubver) -> bool {
391
576
        self.supports_known_subver(protover.kind, protover.version)
392
576
    }
393

            
394
    /// Return a Protocols holding every protocol flag that is present in `self`
395
    /// but not `other`.
396
    ///
397
    /// ```
398
    /// use tor_protover::*;
399
    /// let protos: Protocols = "Desc=2-4 Microdesc=1-5".parse().unwrap();
400
    /// let protos2: Protocols = "Desc=3 Microdesc=3".parse().unwrap();
401
    /// assert_eq!(protos.difference(&protos2),
402
    ///            "Desc=2,4 Microdesc=1-2,4-5".parse().unwrap());
403
    /// ```
404
1036
    pub fn difference(&self, other: &Protocols) -> Protocols {
405
1036
        let mut r = ProtocolsInner::default();
406

            
407
13468
        for i in 0..N_RECOGNIZED {
408
13468
            r.recognized[i] = self.0.recognized[i] & !other.0.recognized[i];
409
13468
        }
410
        // This is not super efficient, but we don't have to do it often.
411
1040
        for ent in self.0.unrecognized.iter() {
412
80
            let mut ent = ent.clone();
413
87
            if let Some(other_ent) = other.0.unrecognized.iter().find(|e| e.proto == ent.proto) {
414
4
                ent.supported &= !other_ent.supported;
415
76
            }
416
80
            if ent.supported != 0 {
417
78
                r.unrecognized.push(ent);
418
78
            }
419
        }
420
1036
        Protocols::from(r)
421
1036
    }
422

            
423
    /// Return a Protocols holding every protocol flag that is present in `self`
424
    /// or `other` or both.
425
    ///
426
    /// ```
427
    /// use tor_protover::*;
428
    /// let protos: Protocols = "Desc=2-4 Microdesc=1-5".parse().unwrap();
429
    /// let protos2: Protocols = "Desc=3 Microdesc=10".parse().unwrap();
430
    /// assert_eq!(protos.union(&protos2),
431
    ///            "Desc=2-4 Microdesc=1-5,10".parse().unwrap());
432
    /// ```
433
332
    pub fn union(&self, other: &Protocols) -> Protocols {
434
332
        let mut r = (*self.0).clone();
435
4316
        for i in 0..N_RECOGNIZED {
436
4316
            r.recognized[i] |= other.0.recognized[i];
437
4316
        }
438
336
        for ent in other.0.unrecognized.iter() {
439
26
            if let Some(my_ent) = r.unrecognized.iter_mut().find(|e| e.proto == ent.proto) {
440
4
                my_ent.supported |= ent.supported;
441
12
            } else {
442
12
                r.unrecognized.push(ent.clone());
443
12
            }
444
        }
445
332
        r.unrecognized.sort();
446
332
        Protocols::from(r)
447
332
    }
448

            
449
    /// Return a Protocols holding every protocol flag that is present in both `self`
450
    /// and `other`.
451
    ///
452
    /// ```
453
    /// use tor_protover::*;
454
    /// let protos: Protocols = "Desc=2-4 Microdesc=1-5".parse().unwrap();
455
    /// let protos2: Protocols = "Desc=3 Microdesc=10".parse().unwrap();
456
    /// assert_eq!(protos.intersection(&protos2),
457
    ///            "Desc=3".parse().unwrap());
458
    /// ```
459
12
    pub fn intersection(&self, other: &Protocols) -> Protocols {
460
12
        let mut r = ProtocolsInner::default();
461
156
        for i in 0..N_RECOGNIZED {
462
156
            r.recognized[i] = self.0.recognized[i] & other.0.recognized[i];
463
156
        }
464
16
        for ent in self.0.unrecognized.iter() {
465
23
            if let Some(other_ent) = other.0.unrecognized.iter().find(|e| e.proto == ent.proto) {
466
4
                let supported = ent.supported & other_ent.supported;
467
4
                if supported != 0 {
468
4
                    r.unrecognized.push(SubprotocolEntry {
469
4
                        proto: ent.proto.clone(),
470
4
                        supported,
471
4
                    });
472
4
                }
473
12
            }
474
        }
475
12
        r.unrecognized.sort();
476
12
        Protocols::from(r)
477
12
    }
478
}
479

            
480
impl ProtocolsInner {
481
    /// Parsing helper: Try to add a new entry `ent` to this set of protocols.
482
    ///
483
    /// Uses `foundmask`, a bit mask saying which recognized protocols
484
    /// we've already found entries for.  Returns an error if `ent` is
485
    /// for a protocol we've already added.
486
    ///
487
    /// Does not preserve sorting order; the caller must call `self.unrecognized.sort()` before returning.
488
439900
    fn add(&mut self, foundmask: &mut u64, ent: SubprotocolEntry) -> Result<(), ParseError> {
489
439900
        match ent.proto {
490
438308
            Protocol::Proto(k) => {
491
438308
                let idx = k.get() as usize;
492
438308
                assert!(idx < N_RECOGNIZED); // guaranteed by invariant on Protocol::Proto
493
438308
                let bit = 1 << u64::from(k.get());
494
438308
                if (*foundmask & bit) != 0 {
495
4
                    return Err(ParseError::Duplicate);
496
438304
                }
497
438304
                *foundmask |= bit;
498
438304
                self.recognized[idx] = ent.supported;
499
            }
500
1592
            Protocol::Unrecognized(ref s) => {
501
1592
                if self
502
1592
                    .unrecognized
503
1592
                    .iter()
504
1617
                    .any(|ent| ent.proto.is_unrecognized(s))
505
                {
506
2
                    return Err(ParseError::Duplicate);
507
1590
                }
508
1590
                if ent.supported != 0 {
509
1590
                    self.unrecognized.push(ent);
510
1590
                }
511
            }
512
        }
513
439894
        Ok(())
514
439900
    }
515
}
516

            
517
/// An error representing a failure to parse a set of protocol versions.
518
#[derive(Error, Debug, PartialEq, Eq, Clone)]
519
#[non_exhaustive]
520
pub enum ParseError {
521
    /// A protocol version was not in the range 0..=63.
522
    #[error("Protocol version out of range")]
523
    OutOfRange,
524
    /// Some subprotocol or protocol version appeared more than once.
525
    #[error("Duplicate protocol entry")]
526
    Duplicate,
527
    /// The list of protocol versions was malformed in some other way.
528
    #[error("Malformed protocol entry")]
529
    Malformed,
530
}
531

            
532
/// Helper: return a new u64 in which bits `lo` through `hi` inclusive
533
/// are set to 1, and all the other bits are set to 0.
534
///
535
/// In other words, `bitrange(a,b)` is how we represent the range of
536
/// versions `a-b` in a protocol version bitmask.
537
///
538
/// ```ignore
539
/// # use tor_protover::bitrange;
540
/// assert_eq!(bitrange(0, 5), 0b111111);
541
/// assert_eq!(bitrange(2, 5), 0b111100);
542
/// assert_eq!(bitrange(2, 7), 0b11111100);
543
/// ```
544
447600
fn bitrange(lo: u64, hi: u64) -> u64 {
545
447600
    assert!(lo <= hi && lo <= 63 && hi <= 63);
546
447600
    let mut mask = !0;
547
447600
    mask <<= 63 - hi;
548
447600
    mask >>= 63 - hi + lo;
549
447600
    mask <<= lo;
550
447600
    mask
551
447600
}
552

            
553
/// Helper: return true if the provided string is a valid "integer"
554
/// in the form accepted by the protover spec.  This is stricter than
555
/// rust's integer parsing format.
556
895222
fn is_good_number(n: &str) -> bool {
557
912869
    n.chars().all(|ch| ch.is_ascii_digit()) && !n.starts_with('0')
558
895222
}
559

            
560
/// A single SubprotocolEntry is parsed from a string of the format
561
/// Name=Versions, where Versions is a comma-separated list of
562
/// integers or ranges of integers.
563
impl std::str::FromStr for SubprotocolEntry {
564
    type Err = ParseError;
565

            
566
    #[allow(clippy::string_slice)] // TODO
567
439930
    fn from_str(s: &str) -> Result<Self, ParseError> {
568
        // split the string on the =.
569
439928
        let (name, versions) = {
570
439930
            let eq_idx = s.find('=').ok_or(ParseError::Malformed)?;
571
439928
            (&s[..eq_idx], &s[eq_idx + 1..])
572
        };
573
        // Look up the protocol by name.
574
439928
        let proto = match ProtoKind::from_name(name) {
575
438334
            Some(p) => Protocol::Proto(p),
576
1594
            None => Protocol::Unrecognized(name.to_string()),
577
        };
578
439928
        if versions.is_empty() {
579
            // We need to handle this case specially, since otherwise
580
            // it would be treated below as a single empty value, which
581
            // would be rejected.
582
2
            return Ok(SubprotocolEntry {
583
2
                proto,
584
2
                supported: 0,
585
2
            });
586
439926
        }
587
        // Construct a bitmask based on the comma-separated versions.
588
439926
        let mut supported = 0_u64;
589
447616
        for ent in versions.split(',') {
590
            // Find and parse lo and hi for a single range of versions.
591
            // (If this is not a range, but rather a single version v,
592
            // treat it as if it were a range v-v.)
593
447616
            let (lo_s, hi_s) = {
594
447616
                match ent.find('-') {
595
100652
                    Some(pos) => (&ent[..pos], &ent[pos + 1..]),
596
346964
                    None => (ent, ent),
597
                }
598
            };
599
447616
            if !is_good_number(lo_s) {
600
10
                return Err(ParseError::Malformed);
601
447606
            }
602
447606
            if !is_good_number(hi_s) {
603
2
                return Err(ParseError::Malformed);
604
447604
            }
605
447604
            let lo: u64 = lo_s.parse().map_err(|_| ParseError::Malformed)?;
606
447602
            let hi: u64 = hi_s.parse().map_err(|_| ParseError::Malformed)?;
607
            // Make sure that lo and hi are in-bounds and consistent.
608
447598
            if lo > (MAX_VER as u64) || hi > (MAX_VER as u64) {
609
6
                return Err(ParseError::OutOfRange);
610
447592
            }
611
447592
            if lo > hi {
612
2
                return Err(ParseError::Malformed);
613
447590
            }
614
447590
            let mask = bitrange(lo, hi);
615
            // Make sure that no version is included twice.
616
447590
            if (supported & mask) != 0 {
617
2
                return Err(ParseError::Duplicate);
618
447588
            }
619
            // Add the appropriate bits to the mask.
620
447588
            supported |= mask;
621
        }
622
439898
        Ok(SubprotocolEntry { proto, supported })
623
439930
    }
624
}
625

            
626
/// A Protocols set can be parsed from a string according to the
627
/// format used in Tor consensus documents.
628
///
629
/// A protocols set is represented by a space-separated list of
630
/// entries.  Each entry is of the form `Name=Versions`, where `Name`
631
/// is the name of a protocol, and `Versions` is a comma-separated
632
/// list of version numbers and version ranges.  Each version range is
633
/// a pair of integers separated by `-`.
634
///
635
/// No protocol name may be listed twice.  No version may be listed
636
/// twice for a single protocol.  All versions must be in range 0
637
/// through 63 inclusive.
638
impl std::str::FromStr for Protocols {
639
    type Err = ParseError;
640

            
641
595914
    fn from_str(s: &str) -> Result<Self, ParseError> {
642
595914
        let mut result = ProtocolsInner::default();
643
595914
        let mut foundmask = 0_u64;
644
727744
        for ent in s.split(' ') {
645
727744
            if ent.is_empty() {
646
287814
                continue;
647
439930
            }
648

            
649
439930
            let s: SubprotocolEntry = ent.parse()?;
650
439900
            result.add(&mut foundmask, s)?;
651
        }
652
595878
        result.unrecognized.sort();
653
595878
        Ok(result.into())
654
595914
    }
655
}
656

            
657
/// Given a bitmask, return a list of the bits set in the mask, as a
658
/// String in the format expected by Tor consensus documents.
659
///
660
/// This implementation constructs ranges greedily.  For example, the
661
/// bitmask `0b0111011` will be represented as `0-1,3-5`, and not
662
/// `0,1,3,4,5` or `0,1,3-5`.
663
///
664
/// ```ignore
665
/// # use tor_protover::dumpmask;
666
/// assert_eq!(dumpmask(0b111111), "0-5");
667
/// assert_eq!(dumpmask(0b111100), "2-5");
668
/// assert_eq!(dumpmask(0b11111100), "2-7");
669
/// ```
670
3800
fn dumpmask(mut mask: u64) -> String {
671
    /// Helper: push a range (which may be a singleton) onto `v`.
672
3870
    fn append(v: &mut Vec<String>, lo: u32, hi: u32) {
673
3870
        if lo == hi {
674
1614
            v.push(lo.to_string());
675
2256
        } else {
676
2256
            v.push(format!("{}-{}", lo, hi));
677
2256
        }
678
3870
    }
679
    // We'll be building up our result here, then joining it with
680
    // commas.
681
3800
    let mut result = Vec::new();
682
    // This implementation is a little tricky, but it should be more
683
    // efficient than a raw search.  Basically, we're using the
684
    // function u64::trailing_zeros to count how large each range of
685
    // 1s or 0s is, and then shifting by that amount.
686

            
687
    // How many bits have we already shifted `mask`?
688
3800
    let mut shift = 0;
689
7668
    while mask != 0 {
690
3870
        let zeros = mask.trailing_zeros();
691
3870
        mask >>= zeros;
692
3870
        shift += zeros;
693
3870
        let ones = mask.trailing_ones();
694
3870
        append(&mut result, shift, shift + ones - 1);
695
3870
        shift += ones;
696
3870
        if ones == 64 {
697
            // We have to do this check to avoid overflow when formatting
698
            // the range `0-63`.
699
2
            break;
700
3868
        }
701
3868
        mask >>= ones;
702
    }
703
3800
    result.join(",")
704
3800
}
705

            
706
/// The Display trait formats a protocol set in the format expected by Tor
707
/// consensus documents.
708
///
709
/// ```
710
/// use tor_protover::*;
711
/// let protos: Protocols = "Link=1,2,3 Foobar=7 Relay=2".parse().unwrap();
712
/// assert_eq!(format!("{}", protos),
713
///            "Foobar=7 Link=1-3 Relay=2");
714
/// ```
715
impl std::fmt::Display for Protocols {
716
2058
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
717
2058
        let mut entries = Vec::new();
718
26754
        for (idx, mask) in self.0.recognized.iter().enumerate() {
719
26754
            if *mask != 0 {
720
2570
                let pk: ProtoKind = (idx as u8).into();
721
2570
                entries.push(format!("{}={}", pk, dumpmask(*mask)));
722
24184
            }
723
        }
724
2058
        for ent in &self.0.unrecognized {
725
1220
            if ent.supported != 0 {
726
1220
                entries.push(format!(
727
1220
                    "{}={}",
728
1220
                    ent.proto.to_str(),
729
1220
                    dumpmask(ent.supported)
730
1220
                ));
731
1220
            }
732
        }
733
        // This sort is required.
734
2058
        entries.sort();
735
2058
        write!(f, "{}", entries.join(" "))
736
2058
    }
737
}
738

            
739
impl FromIterator<NamedSubver> for Protocols {
740
1296
    fn from_iter<T: IntoIterator<Item = NamedSubver>>(iter: T) -> Self {
741
1296
        let mut r = ProtocolsInner::default();
742
2403
        for named_subver in iter {
743
2402
            let proto_idx = usize::from(named_subver.kind.get());
744
2402
            let proto_ver = named_subver.version;
745

            
746
            // These are guaranteed by invariants on NamedSubver.
747
2402
            assert!(proto_idx < N_RECOGNIZED);
748
2402
            assert!(usize::from(proto_ver) <= MAX_VER);
749
2402
            r.recognized[proto_idx] |= 1_u64 << proto_ver;
750
        }
751
1296
        Protocols::from(r)
752
1296
    }
753
}
754

            
755
/// Documentation: when is a protocol "supported"?
756
///
757
/// Arti should consider itself to "support" a protocol if, _as built_,
758
/// it implements the protocol completely.
759
///
760
/// Just having the protocol listed among the [`named`]
761
/// protocols is not enough, and neither is an incomplete
762
/// or uncompliant implementation.
763
///
764
/// Similarly, if the protocol is not compiled in,
765
/// it is not technically _supported_.
766
///
767
/// When in doubt, ask yourself:
768
/// - If another Tor implementation believed that we implemented this protocol,
769
///   and began to speak it to us, would we be able to do so?
770
/// - If the protocol were required,
771
///   would this software as built actually meet that requirement?
772
///
773
/// If either answer is no, the protocol is not supported.
774
pub mod doc_supported {}
775

            
776
/// Documentation about changing lists of supported versions.
777
///
778
/// # Warning
779
///
780
/// You need to be extremely careful when removing
781
/// _any_ entry from a list of supported protocols.
782
///
783
/// If you remove an entry while it still appears as "recommended" in the consensus,
784
/// you'll cause all the instances without it to warn.
785
///
786
/// If you remove an entry while it still appears as "required" in the
787
///  consensus, you'll cause all the instances without it to refuse to connect
788
/// to the network, and shut down.
789
///
790
/// If you need to remove a version from a list of supported protocols,
791
/// you need to make sure that it is not listed in the _current consensuses_:
792
/// just removing it from the list that the authorities vote for is NOT ENOUGH.
793
/// You need to remove it from the required list,
794
/// and THEN let the authorities upgrade and vote on new
795
/// consensuses without it. Only once those consensuses are out is it safe to
796
/// remove from the list of required protocols.
797
///
798
/// ## Example
799
///
800
/// One concrete example of a very dangerous race that could occur:
801
///
802
/// Suppose that the client supports protocols "HsDir=1-2" and the consensus
803
/// requires protocols "HsDir=1-2".  If the client supported protocol list is
804
/// then changed to "HSDir=2", while the consensus stills lists "HSDir=1-2",
805
/// then these clients, even very recent ones, will shut down because they
806
/// don't support "HSDir=1".
807
///
808
/// And so, changes need to be done in strict sequence as described above.
809
pub mod doc_changing {}
810

            
811
#[cfg(test)]
812
mod test {
813
    // @@ begin test lint list maintained by maint/add_warning @@
814
    #![allow(clippy::bool_assert_comparison)]
815
    #![allow(clippy::clone_on_copy)]
816
    #![allow(clippy::dbg_macro)]
817
    #![allow(clippy::mixed_attributes_style)]
818
    #![allow(clippy::print_stderr)]
819
    #![allow(clippy::print_stdout)]
820
    #![allow(clippy::single_char_pattern)]
821
    #![allow(clippy::unwrap_used)]
822
    #![allow(clippy::unchecked_time_subtraction)]
823
    #![allow(clippy::useless_vec)]
824
    #![allow(clippy::needless_pass_by_value)]
825
    #![allow(clippy::string_slice)] // See arti#2571
826
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
827
    use std::str::FromStr;
828

            
829
    use super::*;
830

            
831
    #[test]
832
    fn test_bitrange() {
833
        assert_eq!(0b1, bitrange(0, 0));
834
        assert_eq!(0b10, bitrange(1, 1));
835
        assert_eq!(0b11, bitrange(0, 1));
836
        assert_eq!(0b1111110000000, bitrange(7, 12));
837
        assert_eq!(!0, bitrange(0, 63));
838
    }
839

            
840
    #[test]
841
    fn test_dumpmask() {
842
        assert_eq!("", dumpmask(0));
843
        assert_eq!("0-5", dumpmask(0b111111));
844
        assert_eq!("4-5", dumpmask(0b110000));
845
        assert_eq!("1,4-5", dumpmask(0b110010));
846
        assert_eq!("0-63", dumpmask(!0));
847
    }
848

            
849
    #[test]
850
    fn test_canonical() -> Result<(), ParseError> {
851
        fn t(orig: &str, canonical: &str) -> Result<(), ParseError> {
852
            let protos: Protocols = orig.parse()?;
853
            let enc = format!("{}", protos);
854
            assert_eq!(enc, canonical);
855
            Ok(())
856
        }
857

            
858
        t("", "")?;
859
        t(" ", "")?;
860
        t("Link=5,6,7,9 Relay=4-7,2", "Link=5-7,9 Relay=2,4-7")?;
861
        t("FlowCtrl= Padding=8,7 Desc=1-5,6-8", "Desc=1-8 Padding=7-8")?;
862
        t("Zelda=7 Gannon=3,6 Link=4", "Gannon=3,6 Link=4 Zelda=7")?;
863

            
864
        Ok(())
865
    }
866

            
867
    #[test]
868
    fn test_invalid() {
869
        fn t(s: &str) -> ParseError {
870
            let protos: Result<Protocols, ParseError> = s.parse();
871
            assert!(protos.is_err());
872
            protos.err().unwrap()
873
        }
874

            
875
        assert_eq!(t("Link=1-100"), ParseError::OutOfRange);
876
        assert_eq!(t("Zelda=100"), ParseError::OutOfRange);
877
        assert_eq!(t("Link=100-200"), ParseError::OutOfRange);
878

            
879
        assert_eq!(t("Link=1,1"), ParseError::Duplicate);
880
        assert_eq!(t("Link=1 Link=1"), ParseError::Duplicate);
881
        assert_eq!(t("Link=1 Link=3"), ParseError::Duplicate);
882
        assert_eq!(t("Zelda=1 Zelda=3"), ParseError::Duplicate);
883

            
884
        assert_eq!(t("Link=Zelda"), ParseError::Malformed);
885
        assert_eq!(t("Link=6-2"), ParseError::Malformed);
886
        assert_eq!(t("Link=6-"), ParseError::Malformed);
887
        assert_eq!(t("Link=6-,2"), ParseError::Malformed);
888
        assert_eq!(t("Link=1,,2"), ParseError::Malformed);
889
        assert_eq!(t("Link=6-frog"), ParseError::Malformed);
890
        assert_eq!(t("Link=gannon-9"), ParseError::Malformed);
891
        assert_eq!(t("Link Zelda"), ParseError::Malformed);
892

            
893
        assert_eq!(t("Link=01"), ParseError::Malformed);
894
        assert_eq!(t("Link=waffle"), ParseError::Malformed);
895
        assert_eq!(t("Link=1_1"), ParseError::Malformed);
896
    }
897

            
898
    #[test]
899
    fn test_supports() -> Result<(), ParseError> {
900
        let p: Protocols = "Link=4,5-7 Padding=2 Lonk=1-3,5".parse()?;
901

            
902
        assert!(p.supports_known_subver(ProtoKind::Padding, 2));
903
        assert!(!p.supports_known_subver(ProtoKind::Padding, 1));
904
        assert!(p.supports_known_subver(ProtoKind::Link, 6));
905
        assert!(!p.supports_known_subver(ProtoKind::Link, 255));
906
        assert!(!p.supports_known_subver(ProtoKind::Cons, 1));
907
        assert!(!p.supports_known_subver(ProtoKind::Cons, 0));
908
        assert!(p.supports_subver("Link", 6));
909
        assert!(!p.supports_subver("link", 6));
910
        assert!(!p.supports_subver("Cons", 0));
911
        assert!(p.supports_subver("Lonk", 3));
912
        assert!(!p.supports_subver("Lonk", 4));
913
        assert!(!p.supports_subver("lonk", 3));
914
        assert!(!p.supports_subver("Lonk", 64));
915

            
916
        Ok(())
917
    }
918

            
919
    #[test]
920
    fn test_difference() -> Result<(), ParseError> {
921
        let p1: Protocols = "Link=1-10 Desc=5-10 Relay=1,3,5,7,9 Other=7-60 Mine=1-20".parse()?;
922
        let p2: Protocols = "Link=3-4 Desc=1-6 Relay=2-6 Other=8 Theirs=20".parse()?;
923

            
924
        assert_eq!(
925
            p1.difference(&p2),
926
            Protocols::from_str("Link=1-2,5-10 Desc=7-10 Relay=1,7,9 Other=7,9-60 Mine=1-20")?
927
        );
928
        assert_eq!(
929
            p2.difference(&p1),
930
            Protocols::from_str("Desc=1-4 Relay=2,4,6 Theirs=20")?,
931
        );
932

            
933
        let nil = Protocols::default();
934
        assert_eq!(p1.difference(&nil), p1);
935
        assert_eq!(p2.difference(&nil), p2);
936
        assert_eq!(nil.difference(&p1), nil);
937
        assert_eq!(nil.difference(&p2), nil);
938

            
939
        Ok(())
940
    }
941

            
942
    #[test]
943
    fn test_union() -> Result<(), ParseError> {
944
        let p1: Protocols = "Link=1-10 Desc=5-10 Relay=1,3,5,7,9 Other=7-60 Mine=1-20".parse()?;
945
        let p2: Protocols = "Link=3-4 Desc=1-6 Relay=2-6 Other=2,8 Theirs=20".parse()?;
946

            
947
        assert_eq!(
948
            p1.union(&p2),
949
            Protocols::from_str(
950
                "Link=1-10 Desc=1-10 Relay=1-7,9 Other=2,7-60 Theirs=20 Mine=1-20"
951
            )?
952
        );
953
        assert_eq!(
954
            p2.union(&p1),
955
            Protocols::from_str(
956
                "Link=1-10 Desc=1-10 Relay=1-7,9 Other=2,7-60 Theirs=20 Mine=1-20"
957
            )?
958
        );
959

            
960
        let nil = Protocols::default();
961
        assert_eq!(p1.union(&nil), p1);
962
        assert_eq!(p2.union(&nil), p2);
963
        assert_eq!(nil.union(&p1), p1);
964
        assert_eq!(nil.union(&p2), p2);
965

            
966
        Ok(())
967
    }
968

            
969
    #[test]
970
    fn test_intersection() -> Result<(), ParseError> {
971
        let p1: Protocols = "Link=1-10 Desc=5-10 Relay=1,3,5,7,9 Other=7-60 Mine=1-20".parse()?;
972
        let p2: Protocols = "Link=3-4 Desc=1-6 Relay=2-6 Other=2,8 Theirs=20".parse()?;
973

            
974
        assert_eq!(
975
            p1.intersection(&p2),
976
            Protocols::from_str("Link=3-4 Desc=5-6 Relay=3,5 Other=8")?
977
        );
978
        assert_eq!(
979
            p2.intersection(&p1),
980
            Protocols::from_str("Link=3-4 Desc=5-6 Relay=3,5 Other=8")?
981
        );
982

            
983
        let nil = Protocols::default();
984
        assert_eq!(p1.intersection(&nil), nil);
985
        assert_eq!(p2.intersection(&nil), nil);
986
        assert_eq!(nil.intersection(&p1), nil);
987
        assert_eq!(nil.intersection(&p2), nil);
988

            
989
        Ok(())
990
    }
991

            
992
    #[test]
993
    fn from_iter() {
994
        use named as n;
995
        let empty: [NamedSubver; 0] = [];
996
        let prs: Protocols = empty.iter().copied().collect();
997
        assert_eq!(prs, Protocols::default());
998
        let prs: Protocols = empty.into_iter().collect();
999
        assert_eq!(prs, Protocols::default());
        let prs = [
            n::LINK_V3,
            n::HSDIR_V3,
            n::LINK_V4,
            n::LINK_V5,
            n::CONFLUX_BASE,
        ]
        .into_iter()
        .collect::<Protocols>();
        assert_eq!(prs, "Link=3-5 HSDir=2 Conflux=1".parse().unwrap());
    }
    #[test]
    fn order_numbered_subvers() {
        // We rely on this sort order elsewhere in our protocol.
        assert!(NumberedSubver::new(5, 7) < NumberedSubver::new(7, 5));
        assert!(NumberedSubver::new(7, 5) < NumberedSubver::new(7, 6));
        assert!(NumberedSubver::new(7, 6) < NumberedSubver::new(8, 6));
    }
}