1
//! router status entries - items that vary by consensus flavor
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_flavor`.
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
impl RouterStatus {
16
    /// Return an iterator of ORPort addresses for this routerstatus
17
141338103
    pub fn addrs(&self) -> impl Iterator<Item = net::SocketAddr> {
18
141338103
        chain!(
19
141338103
            [std::net::SocketAddrV4::new(self.r.ip, self.r.or_port).into()],
20
141338103
            self.a.iter().copied(),
21
        )
22
141338103
    }
23
    /// Return the declared weight of this routerstatus in the directory.
24
31311200
    pub fn weight(&self) -> &RelayWeight {
25
31311200
        &self.weight
26
31311200
    }
27
    /// Return the protovers that this routerstatus says it implements.
28
3011885
    pub fn protovers(&self) -> &Protocols {
29
3011885
        &self.protos
30
3011885
    }
31
    /// Return the nickname of this routerstatus.
32
    pub fn nickname(&self) -> &str {
33
        self.r.nickname.as_str()
34
    }
35
    /// Return the relay flags of this routerstatus.
36
    pub fn flags(&self) -> &RelayFlags {
37
        &self.flags.known
38
    }
39
    /// Return the version of this routerstatus.
40
    pub fn version(&self) -> Option<&crate::doc::netstatus::rs::SoftwareVersion> {
41
        self.version.as_ref()
42
    }
43
    /// Return true if the ed25519 identity on this relay reflects a
44
    /// true consensus among the authorities.
45
63097153
    pub fn ed25519_id_is_usable(&self) -> bool {
46
63097153
        !self.flags.contains(RelayFlag::NoEdConsensus)
47
63097153
    }
48
    /// Return true if this routerstatus is listed with the BadExit flag.
49
42566202
    pub fn is_flagged_bad_exit(&self) -> bool {
50
42566202
        self.flags.contains(RelayFlag::BadExit)
51
42566202
    }
52
    /// Return true if this routerstatus is listed with the v2dir flag.
53
32028997
    pub fn is_flagged_v2dir(&self) -> bool {
54
32028997
        self.flags.contains(RelayFlag::V2Dir)
55
32028997
    }
56
    /// Return true if this routerstatus is listed with the Exit flag.
57
31390968
    pub fn is_flagged_exit(&self) -> bool {
58
31390968
        self.flags.contains(RelayFlag::Exit)
59
31390968
    }
60
    /// Return true if this routerstatus is listed with the Guard flag.
61
33371597
    pub fn is_flagged_guard(&self) -> bool {
62
33371597
        self.flags.contains(RelayFlag::Guard)
63
33371597
    }
64
    /// Return true if this routerstatus is listed with the HSDir flag.
65
428015
    pub fn is_flagged_hsdir(&self) -> bool {
66
428015
        self.flags.contains(RelayFlag::HSDir)
67
428015
    }
68
    /// Return true if this routerstatus is listed with the Stable flag.
69
3566612
    pub fn is_flagged_stable(&self) -> bool {
70
3566612
        self.flags.contains(RelayFlag::Stable)
71
3566612
    }
72
    /// Return true if this routerstatus is listed with the Fast flag.
73
51271199
    pub fn is_flagged_fast(&self) -> bool {
74
51271199
        self.flags.contains(RelayFlag::Fast)
75
51271199
    }
76
    /// Return true if this routerstatus is listed with the MiddleOnly flag.
77
28812
    pub fn is_flagged_middle_only(&self) -> bool {
78
28812
        self.flags.contains(RelayFlag::MiddleOnly)
79
28812
    }
80
}
81

            
82
impl RouterStatus {
83
    /// Return RSA identity for the relay described by this RouterStatus
84
231723785
    pub fn rsa_identity(&self) -> &RsaIdentity {
85
231723785
        &self.r.identity
86
231723785
    }
87

            
88
    /// Return the networkstatus consensus flavor in which this
89
    /// routerstatus appears.
90
16107
    pub(crate) fn flavor() -> ConsensusFlavor {
91
16107
        FLAVOR
92
16107
    }
93

            
94
    /// Parse a generic routerstatus from a section.
95
    ///
96
    /// Requires that the section obeys the right SectionRules,
97
    /// matching `consensus_flavor`.
98
1924
    pub(crate) fn from_section(sec: &Section<'_, NetstatusKwd>) -> Result<RouterStatus> {
99
        use NetstatusKwd::*;
100
        // R line
101
1924
        let r_item = sec.required(RS_R)?;
102
1924
        let nickname = r_item.required_arg(0)?.parse()?;
103
1924
        let ident = r_item.required_arg(1)?;
104
1924
        let identity = ident.parse::<Base64Fingerprint>()?;
105
        // Fields to skip in the "r" line.
106
1924
        let n_skip = match FLAVOR {
107
1912
            ConsensusFlavor::Microdesc => 0,
108
12
            ConsensusFlavor::Plain => 1,
109
        };
110
        // We check that the published time is well-formed, but we never use it
111
        // for anything in a consensus document.
112
1924
        let _ignore_published: time::SystemTime = {
113
            // TODO: It's annoying to have to do this allocation, since we
114
            // already have a slice that contains both of these arguments.
115
            // Instead, we could get a slice of arguments: we'd have to add
116
            // a feature for that.
117
1924
            let mut p = r_item.required_arg(2 + n_skip)?.to_string();
118
1924
            p.push(' ');
119
1924
            p.push_str(r_item.required_arg(3 + n_skip)?);
120
1924
            p.parse::<Iso8601TimeSp>()?.into()
121
        };
122
1924
        let ip = r_item.required_arg(4 + n_skip)?.parse::<net::Ipv4Addr>()?;
123
1924
        let or_port = r_item.required_arg(5 + n_skip)?.parse::<u16>()?;
124
1924
        let _ = r_item.required_arg(6 + n_skip)?.parse::<u16>()?;
125

            
126
        // main address and A lines.
127
1924
        let a_items = sec.slice(RS_A);
128
1924
        let a = a_items
129
1924
            .iter()
130
1979
            .map(|a_item| Ok(a_item.required_arg(0)?.parse::<net::SocketAddr>()?))
131
1924
            .collect::<Result<Vec<_>>>()?;
132

            
133
        // S line
134
        //
135
        // Wrong for votes, but this code doesn't run for votes.
136
1924
        let flags = DocRelayFlags::from_item_consensus(sec.required(RS_S)?)?;
137

            
138
        // V line
139
1922
        let version = sec.maybe(RS_V).args_as_str().map(str::parse).transpose()?;
140

            
141
        // PR line
142
1922
        let protos = {
143
1922
            let tok = sec.required(RS_PR)?;
144
1922
            tok.args_as_str()
145
1922
                .parse::<Protocols>()
146
1922
                .map_err(|e| EK::BadArgument.at_pos(tok.pos()).with_source(e))?
147
        };
148

            
149
        // W line
150
1922
        let weight = sec
151
1922
            .get(RS_W)
152
1922
            .map(RelayWeight::from_item)
153
1922
            .transpose()?
154
1920
            .unwrap_or_default();
155

            
156
        // No p line
157
        // no ID line
158

            
159
        // Try to find the document digest.  This is in different
160
        // places depending on the kind of consensus we're in.
161
1920
        let doc_digest: DocDigest = match FLAVOR {
162
            ConsensusFlavor::Microdesc => {
163
                // M line
164
1908
                let m_item = sec.required(RS_M)?;
165
1908
                DocDigest::decode(m_item.required_arg(0)?)?
166
            }
167
12
            ConsensusFlavor::Plain => DocDigest::decode(r_item.required_arg(2)?)?,
168
        };
169

            
170
        ns_choose! { (
171
12
            let r_doc_digest = doc_digest;
172
12
            let m_doc_digest = NotPresent;
173
        ) (
174
1906
            let r_doc_digest = NotPresent;
175
1906
            let m_doc_digest = doc_digest;
176
        ) (
177
            let r_doc_digest = doc_digest;
178
            let m_doc_digest = NotPresent;
179
        ) };
180

            
181
1918
        Ok(RouterStatus {
182
1918
            r: RouterStatusIntroItem {
183
1918
                nickname,
184
1918
                identity,
185
1918
                or_port,
186
1918
                doc_digest: r_doc_digest,
187
1918
                publication: IgnoredPublicationTimeSp,
188
1918
                ip,
189
1918
            },
190
1918
            m: m_doc_digest,
191
1918
            a,
192
1918
            flags,
193
1918
            version,
194
1918
            protos,
195
1918
            weight,
196
1918
        })
197
1924
    }
198
}
199

            
200
impl FromRsString for DocDigest {
201
1920
    fn decode(s: &str) -> Result<DocDigest> {
202
1920
        s.parse::<B64>()?
203
1918
            .check_len(DOC_DIGEST_LEN..=DOC_DIGEST_LEN)?
204
1918
            .as_bytes()
205
1918
            .try_into()
206
1918
            .map_err(|_| Error::from(internal!("correct length on digest, but unable to convert")))
207
1920
    }
208
}