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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright 2025 Mysten Labs
// Copyright 2026 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.

//! SCION address combining [IsdAsn], [ScionHostAddr] and a Port

use std::{
    fmt::Display,
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
    str::FromStr,
};

use serde_with::{DeserializeFromStr, SerializeDisplay};

use crate::{
    address::ip_socket_addr::ScionSocketIpAddr,
    core::macros::impl_from,
    scion::{
        address::{
            AddressParseError,
            addr::{ScionAddr, ScionAddrSvc, ScionAddrV4, ScionAddrV6},
            host_addr::{ScionHostAddr, ServiceAddr},
        },
        identifier::isd_asn::IsdAsn,
    },
};

/// SCION address combining [IsdAsn], [ScionHostAddr] and a Port
/// See [ScionAddr](crate::address::addr::ScionAddr) for ([IsdAsn] and [ScionHostAddr]
/// without Port).
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, SerializeDisplay, DeserializeFromStr,
)]
#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
pub enum ScionSocketAddr {
    /// IPv4 SCION Socket Address
    V4(ScionSocketAddrV4),
    /// IPv6 SCION Socket Address
    V6(ScionSocketAddrV6),
    /// Service SCION Socket Address
    Svc(ScionSocketAddrSvc),
}
impl ScionSocketAddr {
    /// Create a new SCION Socket Address
    #[inline]
    pub const fn new(isd_asn: IsdAsn, host: ScionHostAddr, port: u16) -> Self {
        match host {
            ScionHostAddr::V4(host) => {
                Self::V4(ScionSocketAddrV4 {
                    isd_asn,
                    host,
                    port,
                })
            }
            ScionHostAddr::V6(host) => {
                Self::V6(ScionSocketAddrV6 {
                    isd_asn,
                    host,
                    port,
                })
            }
            ScionHostAddr::Svc(host) => {
                Self::Svc(ScionSocketAddrSvc {
                    isd_asn,
                    host,
                    port,
                })
            }
        }
    }

    /// Create a SCION Socket Address from a standard Socket Address
    #[inline]
    pub const fn from_std(isd_asn: IsdAsn, addr: std::net::SocketAddr) -> Self {
        match addr {
            SocketAddr::V4(v4) => {
                Self::V4(ScionSocketAddrV4 {
                    isd_asn,
                    host: *v4.ip(),
                    port: v4.port(),
                })
            }
            SocketAddr::V6(v6) => {
                Self::V6(ScionSocketAddrV6 {
                    isd_asn,
                    host: *v6.ip(),
                    port: v6.port(),
                })
            }
        }
    }

    /// Create a SCION Socket Address from a SCION Address and a port number
    #[inline]
    pub const fn from_scion_addr(scion_addr: ScionAddr, port: u16) -> Self {
        match scion_addr {
            ScionAddr::V4(addr) => {
                Self::V4(ScionSocketAddrV4 {
                    isd_asn: addr.isd_asn,
                    host: addr.host,
                    port,
                })
            }
            ScionAddr::V6(addr) => {
                Self::V6(ScionSocketAddrV6 {
                    isd_asn: addr.isd_asn,
                    host: addr.host,
                    port,
                })
            }
            ScionAddr::Svc(addr) => {
                Self::Svc(ScionSocketAddrSvc {
                    isd_asn: addr.isd_asn,
                    host: addr.host,
                    port,
                })
            }
        }
    }

    /// Returns a [SocketAddr] from the SCION Socket Address if it is an IPv4 or IPv6 address.
    /// Returns None if it is a Service Address.
    #[inline]
    pub const fn socket_addr(&self) -> Option<SocketAddr> {
        match self {
            ScionSocketAddr::V4(addr) => {
                Some(SocketAddr::V4(std::net::SocketAddrV4::new(
                    addr.host, addr.port,
                )))
            }
            ScionSocketAddr::V6(addr) => {
                Some(SocketAddr::V6(std::net::SocketAddrV6::new(
                    addr.host, addr.port, 0, 0,
                )))
            }
            ScionSocketAddr::Svc(_) => None,
        }
    }

    /// Returns an [IpAddr] if the SCION Socket Address is an IPv4 or IPv6 address.
    /// Returns None if it is a Service Address.
    #[inline]
    pub const fn ip(&self) -> Option<IpAddr> {
        match self {
            ScionSocketAddr::V4(addr) => Some(IpAddr::V4(addr.host)),
            ScionSocketAddr::V6(addr) => Some(IpAddr::V6(addr.host)),
            ScionSocketAddr::Svc(_) => None,
        }
    }

    /// Returns a [ScionAddr] from the SCION Socket Address
    #[inline]
    pub const fn scion_addr(&self) -> ScionAddr {
        match self {
            ScionSocketAddr::V4(addr) => {
                ScionAddr::V4(crate::scion::address::addr::ScionAddrV4 {
                    isd_asn: addr.isd_asn,
                    host: addr.host,
                })
            }
            ScionSocketAddr::V6(addr) => {
                ScionAddr::V6(crate::scion::address::addr::ScionAddrV6 {
                    isd_asn: addr.isd_asn,
                    host: addr.host,
                })
            }
            ScionSocketAddr::Svc(addr) => {
                ScionAddr::Svc(crate::scion::address::addr::ScionAddrSvc {
                    isd_asn: addr.isd_asn,
                    host: addr.host,
                })
            }
        }
    }

    /// Returns the port number
    #[inline]
    pub const fn port(&self) -> u16 {
        match self {
            ScionSocketAddr::V4(addr) => addr.port,
            ScionSocketAddr::V6(addr) => addr.port,
            ScionSocketAddr::Svc(addr) => addr.port,
        }
    }

    /// Sets the port number
    #[inline]
    pub fn set_port(&mut self, port: u16) {
        match self {
            ScionSocketAddr::V4(addr) => addr.port = port,
            ScionSocketAddr::V6(addr) => addr.port = port,
            ScionSocketAddr::Svc(addr) => addr.port = port,
        }
    }

    /// Returns the [ScionHostAddr]
    #[inline]
    pub const fn host(&self) -> ScionHostAddr {
        match self {
            ScionSocketAddr::V4(addr) => ScionHostAddr::V4(addr.host),
            ScionSocketAddr::V6(addr) => ScionHostAddr::V6(addr.host),
            ScionSocketAddr::Svc(addr) => ScionHostAddr::Svc(addr.host),
        }
    }

    /// Sets the [ScionHostAddr]
    #[inline]
    pub fn set_host(&mut self, host: ScionHostAddr) {
        *self = Self::new(self.isd_asn(), host, self.port());
    }

    /// Returns the ISD-AS number
    #[inline]
    pub const fn isd_asn(&self) -> IsdAsn {
        match self {
            ScionSocketAddr::V4(addr) => addr.isd_asn,
            ScionSocketAddr::V6(addr) => addr.isd_asn,
            ScionSocketAddr::Svc(addr) => addr.isd_asn,
        }
    }

    /// Sets the ISD-AS number
    #[inline]
    pub fn set_isd_asn(&mut self, isd_asn: IsdAsn) {
        match self {
            ScionSocketAddr::V4(addr) => addr.isd_asn = isd_asn,
            ScionSocketAddr::V6(addr) => addr.isd_asn = isd_asn,
            ScionSocketAddr::Svc(addr) => addr.isd_asn = isd_asn,
        }
    }

    /// Returns a [ScionSocketIpAddr] if the SCION Socket Address is an IPv4 or IPv6 address.
    /// Returns None if it is a Service Address.
    #[inline]
    pub const fn try_to_scion_sock_ip_addr(&self) -> Option<ScionSocketIpAddr> {
        match self {
            ScionSocketAddr::V4(addr) => Some(ScionSocketIpAddr::V4(*addr)),
            ScionSocketAddr::V6(addr) => Some(ScionSocketIpAddr::V6(*addr)),
            ScionSocketAddr::Svc(_) => None,
        }
    }

    /// Returns true if the SCION Socket Address is an IP address (IPv4 or IPv6)
    #[inline]
    pub const fn is_ip(&self) -> bool {
        matches!(self, ScionSocketAddr::V4(_) | ScionSocketAddr::V6(_))
    }

    /// Returns true if the SCION Socket Address is an IPv4 address
    #[inline]
    pub const fn is_ipv4(&self) -> bool {
        matches!(self, ScionSocketAddr::V4(_))
    }

    /// Returns true if the SCION Socket Address is an IPv6 address
    #[inline]
    pub const fn is_ipv6(&self) -> bool {
        matches!(self, ScionSocketAddr::V6(_))
    }

    /// Returns true if the SCION Socket Address is a service address
    #[inline]
    pub const fn is_service(&self) -> bool {
        matches!(self, ScionSocketAddr::Svc(_))
    }
}
impl Display for ScionSocketAddr {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ScionSocketAddr::V4(addr) => addr.fmt(f),
            ScionSocketAddr::V6(addr) => addr.fmt(f),
            ScionSocketAddr::Svc(addr) => addr.fmt(f),
        }
    }
}
impl FromStr for ScionSocketAddr {
    type Err = AddressParseError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        ScionSocketAddrSvc::from_str(s)
            .map(Self::Svc)
            .or_else(|_| ScionSocketAddrV4::from_str(s).map(Self::V4))
            .or_else(|_| ScionSocketAddrV6::from_str(s).map(Self::V6))
            .map_err(|_| AddressParseError::Socket)
    }
}
impl_from!(ScionSocketAddrV4, ScionSocketAddr, |v| Self::V4(v));
impl_from!(ScionSocketAddrV6, ScionSocketAddr, |v| Self::V6(v));
impl_from!(ScionSocketAddrSvc, ScionSocketAddr, |v| Self::Svc(v));
impl_from!(ScionSocketIpAddr, ScionSocketAddr, |v| {
    v.into_scion_sock_addr()
});
impl_from!(ScionSocketAddrV4, ScionAddr, |v| v.to_scion_addr());
impl_from!(ScionSocketAddrV6, ScionAddr, |v| v.to_scion_addr());
impl_from!(ScionSocketAddrSvc, ScionAddr, |v| v.to_scion_addr());

/// SCION IPv4 Socket Address combining [IsdAsn], [Ipv4Addr] and a Port
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, SerializeDisplay, DeserializeFromStr,
)]
#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
pub struct ScionSocketAddrV4 {
    /// ISD-AS number
    pub isd_asn: IsdAsn,
    /// IPv4 Address
    pub host: Ipv4Addr,
    /// Port number
    pub port: u16,
}
impl ScionSocketAddrV4 {
    /// Create a new SCION IPv4 Socket Address
    #[inline]
    pub const fn new(isd_asn: IsdAsn, addr: Ipv4Addr, port: u16) -> Self {
        Self {
            isd_asn,
            host: addr,
            port,
        }
    }

    /// Returns a [ScionAddr] from the SCION Socket Address
    ///
    /// Discards the port number
    #[inline]
    pub const fn to_scion_addr(&self) -> ScionAddr {
        ScionAddr::V4(ScionAddrV4 {
            isd_asn: self.isd_asn,
            host: self.host,
        })
    }
}
impl FromStr for ScionSocketAddrV4 {
    type Err = AddressParseError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (addr, addr_port) =
            parse_socket_addr::<ScionAddrV4>(s).ok_or(AddressParseError::SocketV4)?;

        Ok(ScionSocketAddrV4 {
            isd_asn: addr.isd_asn,
            host: addr.host,
            port: addr_port,
        })
    }
}
impl Display for ScionSocketAddrV4 {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        format_socket_addr(self.isd_asn, ScionHostAddr::V4(self.host), self.port, f)
    }
}

/// SCION IPv6 Socket Address combining [IsdAsn], [Ipv6Addr] and a Port
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, SerializeDisplay, DeserializeFromStr,
)]
#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
pub struct ScionSocketAddrV6 {
    /// ISD-AS number
    pub isd_asn: IsdAsn,
    /// IPv6 Address
    pub host: Ipv6Addr,
    /// Port number
    pub port: u16,
}
impl ScionSocketAddrV6 {
    /// Create a new SCION IPv6 Socket Address
    #[inline]
    pub const fn new(isd_asn: IsdAsn, addr: Ipv6Addr, port: u16) -> Self {
        Self {
            isd_asn,
            host: addr,
            port,
        }
    }

    /// Returns a [ScionAddr] from the SCION Socket Address
    ///
    /// Discards the port number
    #[inline]
    pub const fn to_scion_addr(&self) -> ScionAddr {
        ScionAddr::V6(ScionAddrV6 {
            isd_asn: self.isd_asn,
            host: self.host,
        })
    }
}
impl FromStr for ScionSocketAddrV6 {
    type Err = AddressParseError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (addr, addr_port) =
            parse_socket_addr::<ScionAddrV6>(s).ok_or(AddressParseError::SocketV6)?;
        Ok(ScionSocketAddrV6 {
            isd_asn: addr.isd_asn,
            host: addr.host,
            port: addr_port,
        })
    }
}
impl Display for ScionSocketAddrV6 {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        format_socket_addr(self.isd_asn, ScionHostAddr::V6(self.host), self.port, f)
    }
}

/// SCION Service Socket Address combining [IsdAsn], [ServiceAddr] and a Port
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, SerializeDisplay, DeserializeFromStr,
)]
#[cfg_attr(feature = "proptest", derive(proptest_derive::Arbitrary))]
pub struct ScionSocketAddrSvc {
    /// ISD-AS number
    pub isd_asn: IsdAsn,
    /// Service Address
    pub host: ServiceAddr,
    /// Port number
    pub port: u16,
}
impl ScionSocketAddrSvc {
    /// Create a new SCION Service Socket Address
    #[inline]
    pub const fn new(isd_asn: IsdAsn, addr: ServiceAddr, port: u16) -> Self {
        Self {
            isd_asn,
            host: addr,
            port,
        }
    }

    /// Returns a [ScionAddr] from the SCION Socket Address
    ///
    /// Discards the port number
    #[inline]
    pub const fn to_scion_addr(&self) -> ScionAddr {
        ScionAddr::Svc(ScionAddrSvc {
            isd_asn: self.isd_asn,
            host: self.host,
        })
    }
}
impl FromStr for ScionSocketAddrSvc {
    type Err = AddressParseError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (addr, addr_port) =
            parse_socket_addr::<ScionAddrSvc>(s).ok_or(AddressParseError::SocketSvc)?;
        Ok(ScionSocketAddrSvc {
            isd_asn: addr.isd_asn,
            host: addr.host,
            port: addr_port,
        })
    }
}
impl Display for ScionSocketAddrSvc {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        format_socket_addr(self.isd_asn, ScionHostAddr::Svc(self.host), self.port, f)
    }
}

#[inline]
fn format_socket_addr(
    isd_asn: IsdAsn,
    host: ScionHostAddr,
    port: u16,
    f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
    write!(f, "[{},{}]:{}", isd_asn, host, port)
}

#[inline]
fn parse_socket_addr<T: FromStr>(s: &str) -> Option<(T, u16)> {
    let (bracketed_addr, port) = s.rsplit_once(':')?;

    if !bracketed_addr.starts_with('[') && bracketed_addr.ends_with(']') {
        return None;
    }

    let scion_addr: T = bracketed_addr[1..bracketed_addr.len() - 1].parse().ok()?;
    let port: u16 = port.parse().ok()?;

    Some((scion_addr, port))
}