Debian Codegraph @

third_party/rust/neqo-transport/src/packet/metadata.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 // Enable just this file for logging to just see packets.
8 // e.g. "RUST_LOG=neqo_transport::dump neqo-client ..."
9
10 use std::fmt::{self, Display, Formatter};
11
12 use neqo_common::IpTos;
13 use qlog::events::quic::PacketHeader;
14 use strum::Display;
15
16 use super::DecryptedPacket;
17 use crate::{
18 packet::{PacketNumber, PacketType},
19 path::PathRef,
20 };
21
22 #[derive(Clone, Copy, Display)]
23 pub enum Direction {
24 #[strum(to_string = "TX ->")]
25 Tx,
26 #[strum(to_string = "-> RX")]
27 Rx,
28 }
29
30 pub struct MetaData<'a> {
31 path: &'a PathRef,
32 direction: Direction,
33 packet_type: PacketType,
34 packet_number: PacketNumber,
35 tos: IpTos,
36 len: usize,
37 payload: &'a [u8],
38 }
39
40 impl MetaData<'_> {
41 pub fn new_in<'a>(
42 path: &'a PathRef,
43 tos: IpTos,
44 len: usize,
45 decrypted: &'a DecryptedPacket,
46 ) -> MetaData<'a> {
47 MetaData {
48 path,
49 direction: Direction::Rx,
50 packet_type: decrypted.packet_type(),
51 packet_number: decrypted.pn(),
52 tos,
53 len,
54 payload: decrypted,
55 }
56 }
57
58 pub const fn new_out<'a>(
59 path: &'a PathRef,
60 packet_type: PacketType,
61 packet_number: PacketNumber,
62 length: usize,
63 payload: &'a [u8],
64 tos: IpTos,
65 ) -> MetaData<'a> {
66 MetaData {
67 path,
68 direction: Direction::Tx,
69 packet_type,
70 packet_number,
71 tos,
72 len: length,
73 payload,
74 }
75 }
76
77 #[must_use]
78 pub const fn direction(&self) -> Direction {
79 self.direction
80 }
81
82 #[must_use]
83 pub const fn length(&self) -> usize {
84 self.len
85 }
86
87 #[must_use]
88 pub const fn payload(&self) -> &[u8] {
89 self.payload
90 }
91 }
92
93 impl From<MetaData<'_>> for PacketHeader {
94 fn from(val: MetaData<'_>) -> Self {
95 Self::with_type(
96 val.packet_type.into(),
97 Some(val.packet_number),
98 None,
99 None,
100 None,
101 )
102 }
103 }
104
105 impl Display for MetaData<'_> {
106 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
107 write!(
108 f,
109 "pn={} type={:?} {} {:?} len {}",
110 self.packet_number,
111 self.packet_type,
112 self.path.borrow(),
113 self.tos,
114 self.len,
115 )
116 }
117 }