1
//! router status entries - items for all varieties, that vary
2
//!
3
//! **This file is reincluded multiple times**,
4
//! by the macros in [`crate::doc::ns_variety_definition_macros`],
5
//! once for votes, and once for each consensus flavour.
6
//! It is *not* a module `crate::doc::netstatus::rs::each_variety`.
7
//!
8
//! Each time this file is included by one of the macros mentioned above,
9
//! the `ns_***` macros (such as `ns_const_name!`) may expand to different values.
10
//!
11
//! See [`crate::doc::ns_variety_definition_macros`].
12

            
13
use super::*;
14

            
15
// Explicit parsing arrangements for document digest field in `m` items.
16
//
17
// https://spec.torproject.org/dir-spec/consensus-formats.html#item:r
18
// https://spec.torproject.org/dir-spec/consensus-formats.html#item:m
19
// https://spec.torproject.org/dir-spec/computing-consensus.html#flavor:microdesc
20
//
21
// The document digest moves about, and vote `m` items are even more exciting.
22
// This is for the benefit of the `with` annotations for RouterStatus.m.
23
//
24
// We need to make this an import that can be used with `deftly(netdoc(with = ))`.
25
// `with` expects a path, not a type, so we can't use ns_type!.
26
//
27
// (Normally when trying to parse an item whose single field implements ItemArgumentParseable
28
// but not ItemValueParseable, we would use #[deftly(netdoc(single_arg))]
29
// but here we can't do that because we can't have variety-dependent attributes.)
30
ns_choose! { (
31
    use NotPresentEachValue as doc_digest_item_m;
32
) (
33
    // doc_digest_item_m implemented in rs/md.rs
34
) (
35
    use RouterStatusMdDigestsVote as doc_digest_item_m;
36
) }
37

            
38
/// Type of the referenced document digest in form suitable for parsing and encoding
39
type DocDigestB64 = FixedB64<DOC_DIGEST_LEN>;
40

            
41
/// Intro item for a router status entry
42
///
43
/// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:r>
44
///
45
/// <https://spec.torproject.org/dir-spec/computing-consensus.html#flavor:microdesc>
46
/// `r` item.
47
#[derive(Debug, Clone, Deftly)]
48
#[derive_deftly(ItemValueEncodable, ItemValueParseable)]
49
#[non_exhaustive]
50
pub struct RouterStatusIntroItem {
51
    /// The nickname for this relay.
52
    ///
53
    /// Nicknames can be used for convenience purpose, but no more:
54
    /// there is no mechanism to enforce their uniqueness.
55
    pub nickname: Nickname,
56

            
57
    /// Fingerprint of the old-style RSA identity for this relay.
58
    pub identity: Base64Fingerprint,
59

            
60
    /// Digest of the document for this relay (except md consensuses)
61
    // TODO SPEC rename in the spec from `digest` to "doc_digest"
62
    // TODO SPEC in md consensuses the referenced document digest is in a separate `m` item
63
    pub doc_digest: ns_type!(DocDigestB64, NotPresent, DocDigestB64),
64

            
65
    /// Publication time.
66
    pub publication: ns_type!(
67
        IgnoredPublicationTimeSp,
68
        IgnoredPublicationTimeSp,
69
        Iso8601TimeSp
70
    ),
71

            
72
    /// IPv4 address
73
    pub ip: std::net::Ipv4Addr,
74

            
75
    /// Relay port
76
    pub or_port: u16,
77

            
78
    /// Directory port
79
    ///
80
    /// Always 0 when read by the old parser.
81
    pub dir_port: u16,
82
}
83

            
84
/// A single relay's status, in a network status document.
85
///
86
/// <https://spec.torproject.org/dir-spec/consensus-formats.html#section:router-status>
87
///
88
/// <https://spec.torproject.org/dir-spec/computing-consensus.html#flavor:microdesc>
89
/// under "Changes to router status entries".
90
//
91
// In most netdocs we would use the item keywords as the field names.  But routerstatus
92
// entry keywords are chosen to be very short to minimise the consensus size, so we
93
// use longer names in the struct and specify the keyword separately.
94
#[derive(Debug, Clone, Deftly)]
95
#[derive_deftly(NetdocEncodable, NetdocParseable)]
96
#[non_exhaustive]
97
pub struct RouterStatus {
98
    /// `r` --- Introduce a routerstatus entry
99
    ///
100
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:r>
101
    /// (and, the md version, which is different).
102
    pub r: RouterStatusIntroItem,
103

            
104
    /// `m` --- Microdescriptor or document digest
105
    ///
106
    /// In an md consensus, the hash of the document for this relay.
107
    /// In a vote, microdescriptor hashes for the various consensus methods.
108
    ///
109
    /// <https://spec.torproject.org/dir-spec/computing-consensus.html#flavor:microdesc>
110
    /// `r` item.
111
    // We call this field `m` rather than `doc_digest` because it's not always the doc digest.
112
    // TODO SPEC in all but md consensuses the referenced document digest is in the `r` intro item
113
    //
114
    // TODO SPEC Adjust microdesc consensus `m` item position in the spec.
115
    // This item is here because this is where C Tor puts it.  
116
    #[deftly(netdoc(with = doc_digest_item_m))]
117
    pub m: ns_type!(NotPresent, DocDigestB64, Vec<RouterStatusMdDigestsVote>),
118

            
119
    /// `a` --- Further router address(es) (IPv6)
120
    ///
121
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:a>
122
    /// (and, the md version, which is different).
123
    #[deftly(netdoc(single_arg))]
124
    pub a: Vec<net::SocketAddr>,
125

            
126
    /// `s` --- Router status flags
127
    ///
128
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:s>
129
    #[deftly(netdoc(
130
        keyword = "s",
131
        with = {
132
            relay_flags::ParserEncoder::<ns_type!(
133
                relay_flags::ConsensusRepr,
134
                relay_flags::ConsensusRepr,
135
                relay_flags::NoImplicitRepr,
136
            )>
137
        },
138
    ))]
139
    pub flags: DocRelayFlags,
140

            
141
    /// `v` --- Relay's Tor software version
142
    ///
143
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:v>
144
    #[deftly(netdoc(keyword = "v"))]
145
    pub version: Option<SoftwareVersion>,
146

            
147
    /// `pr` --- Subprotocol capabilities supported
148
    ///
149
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:v>
150
    #[deftly(netdoc(keyword = "pr"))]
151
    pub protos: Protocols,
152

            
153
    /// `w` --- Bandwidth estimates
154
    ///
155
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:w>
156
    #[deftly(netdoc(flatten))]
157
    pub weight: RelayWeightsItem,
158

            
159
    /// `p` --- Exit ports summary
160
    ///
161
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:p>
162
    ///
163
    /// This field is not properly parsed in plain consensuses by the old parser.
164
    #[deftly(netdoc(keyword = "p"))]
165
    pub port_policy: ns_type!(Option<Intern<PortPolicy>>, NotPresent, Option<Intern<PortPolicy>>),
166

            
167
    /// `id` --- Relay’s (ed25519) identity
168
    ///
169
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:id>
170
    #[deftly(netdoc(keyword = "id"))] 
171
    pub ed25519_id: ns_type!(NotPresent, NotPresent, Ed25519IdentityLine),
172

            
173
    /// `stats` -- Statistics for this relay
174
    ///
175
    /// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:stats>
176
    pub stats: ns_type!(NotPresent, NotPresent, NetParams<F64Finite>),
177
}
178

            
179
impl RouterStatus {
180
    /// Return the digest of the document identified by this
181
    /// routerstatus.
182
    ///
183
    /// The `doc_digest` method is provided on all varieties of routerstatus entry
184
    /// to help paper over the protocol anomaly, that the digest is in a different place
185
    /// in md routerstatus entries.
186
71123882
    pub fn doc_digest(&self) -> &DocDigest {
187
71123882
        ns_expr!(&self.r.doc_digest, &self.m, &self.r.doc_digest,)
188
71123882
    }
189
}
190

            
191
impl EncodeOrd for RouterStatus {
192
60
    fn encode_cmp(&self, other: &Self) -> Ordering {
193
        // Type inference seems to need a *lot* of help here!
194
60
        let k: for <'i> fn(&'i RouterStatus) -> &'i _  = |rs| &rs .r.identity;
195
60
        EncodeOrd::encode_cmp(k(self), k(other))
196
60
    }
197
}