sciparse 0.6.1

Zero-copy SCION packet parsing, serialization and control plane components
Documentation
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright 2025 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Segment RPC types and conversions.

use prost::Message;

use crate::{
    dataplane_path::standard::types::HopFieldMac,
    rpc::FromRpcError,
    segment::{
        AsEntry, HopEntry, PeerEntry, SegmentHopField, SegmentInfo, Segments, SegmentsPage,
        SignedAsEntry, SignedPathSegment,
    },
};

impl SegmentHopField {
    /// Converts to a protobuf hop field message.
    #[inline]
    pub fn into_rpc(self) -> scion_protobuf::control_plane::v1::HopField {
        scion_protobuf::control_plane::v1::HopField {
            exp_time: self.expiration_units as u32,
            ingress: self.cons_ingress as u64,
            egress: self.cons_egress as u64,
            mac: self.mac.to_vec(),
        }
    }

    /// Tries to convert from a protobuf hop field message.
    #[inline]
    pub fn try_from_rpc(
        hop_field: scion_protobuf::control_plane::v1::HopField,
    ) -> Result<Self, FromRpcError> {
        if hop_field.mac.len() != 6 {
            return Err("Invalid MAC length in HopField".into());
        }

        Ok(SegmentHopField {
            expiration_units: hop_field
                .exp_time
                .try_into()
                .map_err(|_| "Exp Time in HopField is not a valid u8")?,
            cons_ingress: hop_field
                .ingress
                .try_into()
                .map_err(|_| "Ingress in HopField is not a valid u16")?,
            cons_egress: hop_field
                .egress
                .try_into()
                .map_err(|_| "Egress in HopField is not a valid u16")?,
            mac: HopFieldMac(
                hop_field.mac[..6]
                    .try_into()
                    .expect("MAC length checked above, should be 6"),
            ),
        })
    }
}
impl From<SegmentHopField> for scion_protobuf::control_plane::v1::HopField {
    #[inline]
    fn from(hop_field: SegmentHopField) -> Self {
        hop_field.into_rpc()
    }
}
impl TryFrom<scion_protobuf::control_plane::v1::HopField> for SegmentHopField {
    type Error = FromRpcError;
    #[inline]
    fn try_from(
        hop_field: scion_protobuf::control_plane::v1::HopField,
    ) -> Result<Self, Self::Error> {
        SegmentHopField::try_from_rpc(hop_field)
    }
}

impl HopEntry {
    /// Converts to a protobuf hop entry message.
    #[inline]
    pub fn into_rpc(self) -> scion_protobuf::control_plane::v1::HopEntry {
        scion_protobuf::control_plane::v1::HopEntry {
            ingress_mtu: self.ingress_mtu as u32,
            hop_field: Some(self.hop_field.into_rpc()),
        }
    }

    /// Tries to convert from a protobuf hop entry message.
    #[inline]
    pub fn try_from_rpc(
        entry: scion_protobuf::control_plane::v1::HopEntry,
    ) -> Result<Self, FromRpcError> {
        Ok(HopEntry {
            ingress_mtu: entry
                .ingress_mtu
                .try_into()
                .map_err(|_| "Ingress MTU in HopEntry is not a valid u16")?,
            hop_field: entry
                .hop_field
                .ok_or("Missing hop field in HopEntry")?
                .try_into()?,
        })
    }
}
impl From<HopEntry> for scion_protobuf::control_plane::v1::HopEntry {
    #[inline]
    fn from(entry: HopEntry) -> Self {
        entry.into_rpc()
    }
}
impl TryFrom<scion_protobuf::control_plane::v1::HopEntry> for HopEntry {
    type Error = FromRpcError;
    #[inline]
    fn try_from(entry: scion_protobuf::control_plane::v1::HopEntry) -> Result<Self, Self::Error> {
        HopEntry::try_from_rpc(entry)
    }
}

impl PeerEntry {
    /// Converts to a protobuf peer entry message.
    #[inline]
    pub fn into_rpc(self) -> scion_protobuf::control_plane::v1::PeerEntry {
        scion_protobuf::control_plane::v1::PeerEntry {
            peer_isd_as: self.peer.into(),
            peer_interface: self.peer_interface as u64,
            peer_mtu: self.peer_mtu as u32,
            hop_field: Some(self.hop_field.into_rpc()),
        }
    }

    /// Tries to convert from a protobuf peer entry message.
    #[inline]
    pub fn try_from_rpc(
        entry: scion_protobuf::control_plane::v1::PeerEntry,
    ) -> Result<Self, FromRpcError> {
        Ok(PeerEntry {
            peer: entry.peer_isd_as.into(),
            peer_interface: entry
                .peer_interface
                .try_into()
                .map_err(|_| "Peer interface in PeerEntry exceeds u16 maximum")?,
            peer_mtu: entry
                .peer_mtu
                .try_into()
                .map_err(|_| "Peer MTU in PeerEntry exceeds u16 maximum")?,
            hop_field: entry
                .hop_field
                .ok_or("Missing Hop Field in Peer Entry")?
                .try_into()?,
        })
    }
}
impl From<PeerEntry> for scion_protobuf::control_plane::v1::PeerEntry {
    #[inline]
    fn from(entry: PeerEntry) -> Self {
        entry.into_rpc()
    }
}
impl TryFrom<scion_protobuf::control_plane::v1::PeerEntry> for PeerEntry {
    type Error = FromRpcError;
    #[inline]
    fn try_from(entry: scion_protobuf::control_plane::v1::PeerEntry) -> Result<Self, Self::Error> {
        PeerEntry::try_from_rpc(entry)
    }
}

impl SegmentInfo {
    /// Converts to a protobuf segment information message.
    #[inline]
    pub fn into_rpc(self) -> scion_protobuf::control_plane::v1::SegmentInformation {
        scion_protobuf::control_plane::v1::SegmentInformation {
            timestamp: self.timestamp as i64,
            segment_id: self.segment_id as u32,
        }
    }

    /// Tries to convert from a protobuf segment information message.
    #[inline]
    pub fn try_from_rpc(
        info: scion_protobuf::control_plane::v1::SegmentInformation,
    ) -> Result<Self, FromRpcError> {
        Ok(SegmentInfo::new(
            info.timestamp
                .try_into()
                .map_err(|_| "Timestamp is not a valid u32")?,
            info.segment_id
                .try_into()
                .map_err(|_| "Segment ID is not a valid u16")?,
        ))
    }
}
impl From<SegmentInfo> for scion_protobuf::control_plane::v1::SegmentInformation {
    #[inline]
    fn from(info: SegmentInfo) -> Self {
        info.into_rpc()
    }
}
impl TryFrom<scion_protobuf::control_plane::v1::SegmentInformation> for SegmentInfo {
    type Error = FromRpcError;
    #[inline]
    fn try_from(
        info: scion_protobuf::control_plane::v1::SegmentInformation,
    ) -> Result<Self, Self::Error> {
        SegmentInfo::try_from_rpc(info)
    }
}

impl SignedAsEntry {
    /// Converts to a protobuf AS entry message.
    #[inline]
    pub fn into_rpc(self) -> scion_protobuf::control_plane::v1::AsEntry {
        scion_protobuf::control_plane::v1::AsEntry {
            signed: Some(self.signed.into_rpc()),
            // Todo: We should be able to fill this in
            unsigned: None,
        }
    }

    /// Tries to convert from a protobuf AS entry message.
    #[inline]
    pub fn try_from_rpc(
        entry: scion_protobuf::control_plane::v1::AsEntry,
    ) -> Result<Self, FromRpcError> {
        let signed = entry.signed.ok_or("Missing Signed Message")?;
        let hdr_and_body = scion_protobuf::crypto::v1::HeaderAndBodyInternal::decode(
            signed.header_and_body.as_ref(),
        )
        .map_err(|_| "Failed to decode Signed Header and Body")?;
        let unverified_body = hdr_and_body.body;
        let entry =
            scion_protobuf::control_plane::v1::AsEntrySignedBody::decode(unverified_body.as_ref())
                .map_err(|_| "Failed to decode AsEntrySignedBody")?;

        Ok(SignedAsEntry {
            entry: AsEntry {
                local: entry.isd_as.into(),
                mtu: entry.mtu,
                next: entry.next_isd_as.into(),
                hop_entry: entry.hop_entry.ok_or("missing Hop Entry")?.try_into()?,
                peer_entries: entry
                    .peer_entries
                    .into_iter()
                    .map(TryFrom::try_from)
                    .collect::<Result<_, _>>()?,
                extensions: Vec::new(),
                unsigned_extensions: Vec::new(),
            },
            signed: signed.into(),
        })
    }
}
impl From<SignedAsEntry> for scion_protobuf::control_plane::v1::AsEntry {
    #[inline]
    fn from(as_entry: SignedAsEntry) -> Self {
        as_entry.into_rpc()
    }
}
impl TryFrom<scion_protobuf::control_plane::v1::AsEntry> for SignedAsEntry {
    type Error = FromRpcError;
    #[inline]
    fn try_from(entry: scion_protobuf::control_plane::v1::AsEntry) -> Result<Self, Self::Error> {
        SignedAsEntry::try_from_rpc(entry)
    }
}

impl SignedPathSegment {
    /// Converts to a protobuf path segment message.
    #[inline]
    pub fn into_rpc(self) -> scion_protobuf::control_plane::v1::PathSegment {
        scion_protobuf::control_plane::v1::PathSegment {
            segment_info: self.info.into_rpc().encode_to_vec(),
            as_entries: self.as_entries.into_iter().map(Into::into).collect(),
        }
    }

    /// Tries to convert from a protobuf path segment message.
    #[inline]
    pub fn try_from_rpc(
        segment: scion_protobuf::control_plane::v1::PathSegment,
    ) -> Result<Self, FromRpcError> {
        let segment_info = scion_protobuf::control_plane::v1::SegmentInformation::decode(
            segment.segment_info.as_slice(),
        )
        .map_err(|_| "Failed to decode segment info")?;

        Ok(Self {
            info: segment_info.try_into()?,
            as_entries: segment
                .as_entries
                .into_iter()
                .map(SignedAsEntry::try_from)
                .collect::<Result<_, _>>()?,
        })
    }
}
impl From<SignedPathSegment> for scion_protobuf::control_plane::v1::PathSegment {
    #[inline]
    fn from(value: SignedPathSegment) -> Self {
        value.into_rpc()
    }
}
impl TryFrom<scion_protobuf::control_plane::v1::PathSegment> for SignedPathSegment {
    type Error = FromRpcError;
    #[inline]
    fn try_from(
        segment: scion_protobuf::control_plane::v1::PathSegment,
    ) -> Result<Self, Self::Error> {
        SignedPathSegment::try_from_rpc(segment)
    }
}

type RpcSegments =
    std::collections::HashMap<i32, scion_protobuf::control_plane::v1::segments_response::Segments>;

impl Segments {
    /// Converts to protobuf segments grouped by type.
    pub fn into_rpc(
        self,
    ) -> std::collections::HashMap<
        i32,
        scion_protobuf::control_plane::v1::segments_response::Segments,
    > {
        use scion_protobuf::control_plane::v1::{SegmentType, segments_response};
        let mut segments = std::collections::HashMap::new();
        if !self.up_segments.is_empty() {
            segments.insert(
                SegmentType::Up as i32,
                segments_response::Segments {
                    segments: self.up_segments.into_iter().map(Into::into).collect(),
                },
            );
        }
        if !self.down_segments.is_empty() {
            segments.insert(
                SegmentType::Down as i32,
                segments_response::Segments {
                    segments: self.down_segments.into_iter().map(Into::into).collect(),
                },
            );
        }
        if !self.core_segments.is_empty() {
            segments.insert(
                SegmentType::Core as i32,
                segments_response::Segments {
                    segments: self.core_segments.into_iter().map(Into::into).collect(),
                },
            );
        }
        segments
    }

    /// Tries to convert from a protobuf segments response message.
    pub fn try_from_rpc(value: RpcSegments) -> Result<Self, FromRpcError> {
        let mut up_segments = Vec::new();
        let mut down_segments = Vec::new();
        let mut core_segments = Vec::new();
        for (segment_type, segments) in value {
            let segment_type =
                match scion_protobuf::control_plane::v1::SegmentType::try_from(segment_type) {
                    Ok(t) => t,
                    Err(_err) => {
                        // Skip unrecognized segment types
                        continue;
                    }
                };
            for path_segment in segments.segments {
                let segment = path_segment.try_into()?;
                match segment_type {
                    scion_protobuf::control_plane::v1::SegmentType::Up => {
                        up_segments.push(segment);
                    }
                    scion_protobuf::control_plane::v1::SegmentType::Core => {
                        core_segments.push(segment);
                    }
                    scion_protobuf::control_plane::v1::SegmentType::Down => {
                        down_segments.push(segment);
                    }
                    scion_protobuf::control_plane::v1::SegmentType::Unspecified => {
                        // Skip unrecognized segment types
                        continue;
                    }
                }
            }
        }
        Ok(Self {
            up_segments,
            down_segments,
            core_segments,
        })
    }
}
impl From<Segments> for RpcSegments {
    #[inline]
    fn from(segments: Segments) -> Self {
        segments.into_rpc()
    }
}
impl TryFrom<RpcSegments> for Segments {
    type Error = FromRpcError;
    #[inline]
    fn try_from(
        value: std::collections::HashMap<
            i32,
            scion_protobuf::control_plane::v1::segments_response::Segments,
        >,
    ) -> Result<Self, Self::Error> {
        Segments::try_from_rpc(value)
    }
}

impl SegmentsPage {
    /// Converts to a protobuf segments response message.
    #[inline]
    pub fn into_rpc(self) -> scion_protobuf::control_plane::v1::SegmentsResponse {
        scion_protobuf::control_plane::v1::SegmentsResponse {
            segments: self.segments.into_rpc(),
            deprecated_signed_revocations: Vec::new(),
        }
    }

    /// Tries to convert from a protobuf segments response message.
    #[inline]
    pub fn try_from_rpc(
        value: scion_protobuf::control_plane::v1::SegmentsResponse,
    ) -> Result<Self, FromRpcError> {
        Ok(Self {
            segments: Segments::try_from_rpc(value.segments)?,
            // TODO(pagination): There is no pagination in the control service.
            next_page_token: "".to_string(),
        })
    }
}
impl From<SegmentsPage> for scion_protobuf::control_plane::v1::SegmentsResponse {
    #[inline]
    fn from(page: SegmentsPage) -> Self {
        page.into_rpc()
    }
}
impl TryFrom<scion_protobuf::control_plane::v1::SegmentsResponse> for SegmentsPage {
    type Error = FromRpcError;
    #[inline]
    fn try_from(
        value: scion_protobuf::control_plane::v1::SegmentsResponse,
    ) -> Result<Self, Self::Error> {
        SegmentsPage::try_from_rpc(value)
    }
}