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
mod control;
mod data;
mod error;
mod modular_num;
mod msg_number;
mod seq_number;
mod socket_id;
mod time;
pub use control::*;
pub use data::*;
pub use error::*;
pub use msg_number::*;
pub use seq_number::*;
pub use socket_id::*;
pub use time::*;
use std::{
fmt::{self, Debug, Formatter},
io,
net::SocketAddr,
};
use bytes::{Buf, BufMut};
use super::options::PacketSize;
#[derive(Clone, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum Packet {
Data(DataPacket),
Control(ControlPacket),
}
impl Packet {
const IPV4_HEADER_SIZE: u64 = 20;
const UDP_HEADER_SIZE: u64 = 8;
const SRT_HEADER_SIZE: u64 = 16;
pub const HEADER_SIZE: PacketSize =
PacketSize(Self::IPV4_HEADER_SIZE + Self::UDP_HEADER_SIZE + Self::SRT_HEADER_SIZE);
pub fn timestamp(&self) -> TimeStamp {
match *self {
Packet::Data(DataPacket { timestamp, .. })
| Packet::Control(ControlPacket { timestamp, .. }) => timestamp,
}
}
pub fn dest_sockid(&self) -> SocketId {
match *self {
Packet::Data(DataPacket { dest_sockid, .. })
| Packet::Control(ControlPacket { dest_sockid, .. }) => dest_sockid,
}
}
pub fn data(&self) -> Option<&DataPacket> {
if let Packet::Data(d) = self {
Some(d)
} else {
None
}
}
pub fn control(&self) -> Option<&ControlPacket> {
if let Packet::Control(c) = self {
Some(c)
} else {
None
}
}
pub fn is_handshake(&self) -> bool {
matches!(
self,
Packet::Control(ControlPacket {
control_type: ControlTypes::Handshake(_),
..
})
)
}
pub fn wire_size(&self) -> usize {
match self {
Packet::Data(d) => d.wire_size(),
Packet::Control(c) => c.wire_size(),
}
}
pub fn parse<T: Buf>(buf: &mut T, is_ipv6: bool) -> Result<Packet, PacketParseError> {
if buf.remaining() < 16 {
return Err(PacketParseError::NotEnoughData);
}
let first = buf.chunk()[0];
Ok(if (first & 0x80) == 0 {
Packet::Data(DataPacket::parse(buf)?)
} else {
Packet::Control(ControlPacket::parse(buf, is_ipv6)?)
})
}
pub fn serialize<T: BufMut>(&self, into: &mut T) {
match *self {
Packet::Control(ref control) => {
control.serialize(into);
}
Packet::Data(ref data) => {
data.serialize(into);
}
}
}
}
impl From<DataPacket> for Packet {
fn from(dp: DataPacket) -> Self {
Packet::Data(dp)
}
}
impl From<ControlPacket> for Packet {
fn from(cp: ControlPacket) -> Self {
Packet::Control(cp)
}
}
impl Debug for Packet {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match self {
Packet::Data(dp) => write!(f, "{dp:?}"),
Packet::Control(cp) => write!(f, "{cp:?}"),
}
}
}
#[derive(Debug)]
pub enum ReceivePacketError {
Parse(PacketParseError),
Io(io::Error),
}
impl From<io::Error> for ReceivePacketError {
fn from(error: io::Error) -> Self {
ReceivePacketError::Io(error)
}
}
impl From<PacketParseError> for ReceivePacketError {
fn from(error: PacketParseError) -> Self {
ReceivePacketError::Parse(error)
}
}
impl fmt::Display for ReceivePacketError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ReceivePacketError::*;
match self {
Parse(e) => <PacketParseError as fmt::Display>::fmt(e, f),
Io(e) => <io::Error as fmt::Display>::fmt(e, f),
}
}
}
impl std::error::Error for ReceivePacketError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use ReceivePacketError::*;
match self {
Parse(e) => Some(e),
Io(e) => Some(e),
}
}
}
impl Eq for ReceivePacketError {}
impl PartialEq for ReceivePacketError {
fn eq(&self, other: &Self) -> bool {
use ReceivePacketError::*;
match (self, other) {
(Parse(s), Parse(o)) => s.eq(o),
(Io(s), Io(o)) => s.kind().eq(&o.kind()) && s.raw_os_error().eq(&o.raw_os_error()),
_ => false,
}
}
}
pub type ReceivePacketResult = Result<(Packet, SocketAddr), ReceivePacketError>;