1use super::ack::AckPacket;
21use super::handshake::HandshakePacket;
22use super::misc::{
23 AckAckPacket, CongestionWarningPacket, DropReqPacket, KeepAlivePacket, PeerErrorPacket,
24 ShutdownPacket,
25};
26use super::nak::NakPacket;
27use super::number::{AckNumber, SocketId, SrtTimestamp};
28use super::{Error, Result, SRT_HEADER_LEN, be32, put_be32};
29use broadcast_common::error::ReservedBits;
30use broadcast_common::len::{need_in, need_out};
31
32pub(crate) const CONTROL_TYPE_HANDSHAKE: u16 = 0x0000;
36pub(crate) const CONTROL_TYPE_KEEPALIVE: u16 = 0x0001;
38pub(crate) const CONTROL_TYPE_ACK: u16 = 0x0002;
40pub(crate) const CONTROL_TYPE_NAK: u16 = 0x0003;
42pub(crate) const CONTROL_TYPE_CONGESTION_WARNING: u16 = 0x0004;
44pub(crate) const CONTROL_TYPE_SHUTDOWN: u16 = 0x0005;
46pub(crate) const CONTROL_TYPE_ACKACK: u16 = 0x0006;
48pub(crate) const CONTROL_TYPE_DROPREQ: u16 = 0x0007;
50pub(crate) const CONTROL_TYPE_PEERERROR: u16 = 0x0008;
52pub(crate) const CONTROL_TYPE_USER_DEFINED: u16 = 0x7FFF;
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
59#[cfg_attr(feature = "serde", derive(serde::Serialize))]
60#[non_exhaustive]
61pub enum ControlType {
62 Handshake,
64 KeepAlive,
66 Ack,
68 Nak,
70 CongestionWarning,
72 Shutdown,
74 AckAck,
76 DropReq,
78 PeerError,
80 UserDefined,
83 Reserved(u16),
85}
86
87impl ControlType {
88 pub fn from_bits(v: u16) -> Self {
90 match v {
91 CONTROL_TYPE_HANDSHAKE => ControlType::Handshake,
92 CONTROL_TYPE_KEEPALIVE => ControlType::KeepAlive,
93 CONTROL_TYPE_ACK => ControlType::Ack,
94 CONTROL_TYPE_NAK => ControlType::Nak,
95 CONTROL_TYPE_CONGESTION_WARNING => ControlType::CongestionWarning,
96 CONTROL_TYPE_SHUTDOWN => ControlType::Shutdown,
97 CONTROL_TYPE_ACKACK => ControlType::AckAck,
98 CONTROL_TYPE_DROPREQ => ControlType::DropReq,
99 CONTROL_TYPE_PEERERROR => ControlType::PeerError,
100 CONTROL_TYPE_USER_DEFINED => ControlType::UserDefined,
101 other => ControlType::Reserved(other),
102 }
103 }
104
105 pub fn to_bits(self) -> u16 {
107 match self {
108 ControlType::Handshake => CONTROL_TYPE_HANDSHAKE,
109 ControlType::KeepAlive => CONTROL_TYPE_KEEPALIVE,
110 ControlType::Ack => CONTROL_TYPE_ACK,
111 ControlType::Nak => CONTROL_TYPE_NAK,
112 ControlType::CongestionWarning => CONTROL_TYPE_CONGESTION_WARNING,
113 ControlType::Shutdown => CONTROL_TYPE_SHUTDOWN,
114 ControlType::AckAck => CONTROL_TYPE_ACKACK,
115 ControlType::DropReq => CONTROL_TYPE_DROPREQ,
116 ControlType::PeerError => CONTROL_TYPE_PEERERROR,
117 ControlType::UserDefined => CONTROL_TYPE_USER_DEFINED,
118 ControlType::Reserved(v) => v,
119 }
120 }
121
122 pub fn name(&self) -> &'static str {
124 match self {
125 ControlType::Handshake => "handshake",
126 ControlType::KeepAlive => "keep-alive",
127 ControlType::Ack => "ACK",
128 ControlType::Nak => "NAK",
129 ControlType::CongestionWarning => "congestion warning",
130 ControlType::Shutdown => "shutdown",
131 ControlType::AckAck => "ACKACK",
132 ControlType::DropReq => "message drop request",
133 ControlType::PeerError => "peer error",
134 ControlType::UserDefined => "user-defined",
135 ControlType::Reserved(_) => "reserved",
136 }
137 }
138}
139
140broadcast_common::impl_spec_display!(ControlType, Reserved);
141
142#[derive(Debug, Clone, PartialEq)]
147#[cfg_attr(feature = "serde", derive(serde::Serialize))]
148pub struct UserDefinedPacket<'a> {
149 pub control_type: u16,
151 pub subtype: u16,
154 pub type_specific_info: u32,
156 pub timestamp: SrtTimestamp,
158 pub dest_socket_id: SocketId,
160 pub cif: &'a [u8],
162}
163
164impl<'a> UserDefinedPacket<'a> {
165 pub fn as_key_material(&self) -> Result<super::KeyMaterial<'a>> {
169 super::KeyMaterial::parse(self.cif)
170 }
171}
172
173#[derive(Debug, Clone, PartialEq)]
175#[cfg_attr(feature = "serde", derive(serde::Serialize))]
176#[non_exhaustive]
177pub enum ControlPacket<'a> {
178 Handshake(HandshakePacket<'a>),
180 KeepAlive(KeepAlivePacket),
182 Ack(AckPacket),
184 Nak(NakPacket),
186 CongestionWarning(CongestionWarningPacket),
188 Shutdown(ShutdownPacket),
190 AckAck(AckAckPacket),
192 DropReq(DropReqPacket),
194 PeerError(PeerErrorPacket),
196 UserDefined(UserDefinedPacket<'a>),
198}
199
200fn check_reserved_u16(what: &'static str, v: u16) -> Result<()> {
201 if v != 0 {
202 return Err(Error::ReservedBits(ReservedBits {
203 field: what,
204 expected: 0,
205 found: u64::from(v),
206 }));
207 }
208 Ok(())
209}
210
211fn check_reserved_u32(what: &'static str, v: u32) -> Result<()> {
212 if v != 0 {
213 return Err(Error::ReservedBits(ReservedBits {
214 field: what,
215 expected: 0,
216 found: u64::from(v),
217 }));
218 }
219 Ok(())
220}
221
222const LIBSRT_CIF_PAD_LEN: usize = 4;
232
233fn check_no_cif_or_libsrt_pad(what: &'static str, cif: &[u8]) -> Result<bool> {
240 match cif.len() {
241 0 => Ok(false),
242 LIBSRT_CIF_PAD_LEN => {
243 let value = be32(cif, 0);
244 if value != 0 {
245 return Err(Error::ReservedBits(ReservedBits {
246 field: what,
247 expected: 0,
248 found: u64::from(value),
249 }));
250 }
251 Ok(true)
252 }
253 extra => Err(Error::UnexpectedTrailingBytes { what, extra }),
254 }
255}
256
257impl<'a> ControlPacket<'a> {
258 pub fn parse(bytes: &'a [u8]) -> Result<Self> {
265 need_in(bytes, SRT_HEADER_LEN, "SRT control packet header")?;
266 let word0 = be32(bytes, 0);
267 if word0 & super::F_BIT == 0 {
268 return Err(Error::WrongPacketKind {
269 expected: "control packet (F=1)",
270 });
271 }
272 let control_type_bits = ((word0 >> 16) & 0x7FFF) as u16;
273 let subtype = (word0 & 0xFFFF) as u16;
274 let type_specific_info = be32(bytes, 4);
275 let timestamp = SrtTimestamp::from_wire(be32(bytes, 8));
276 let dest_socket_id = SocketId::from_wire(be32(bytes, 12));
277 let cif = &bytes[SRT_HEADER_LEN..];
278 let control_type = ControlType::from_bits(control_type_bits);
279
280 Ok(match control_type {
281 ControlType::Handshake => {
282 check_reserved_u16("Subtype", subtype)?;
283 check_reserved_u32("Type-specific Information", type_specific_info)?;
284 ControlPacket::Handshake(HandshakePacket::parse_cif(
285 timestamp,
286 dest_socket_id,
287 cif,
288 )?)
289 }
290 ControlType::KeepAlive => {
291 check_reserved_u16("Subtype", subtype)?;
292 check_reserved_u32("Type-specific Information", type_specific_info)?;
293 let libsrt_pad = check_no_cif_or_libsrt_pad("keep-alive CIF", cif)?;
294 ControlPacket::KeepAlive(KeepAlivePacket {
295 timestamp,
296 dest_socket_id,
297 libsrt_pad,
298 })
299 }
300 ControlType::Ack => {
301 check_reserved_u16("Subtype", subtype)?;
302 ControlPacket::Ack(AckPacket::parse_cif(
303 type_specific_info,
304 timestamp,
305 dest_socket_id,
306 cif,
307 )?)
308 }
309 ControlType::Nak => {
310 check_reserved_u16("Subtype", subtype)?;
311 check_reserved_u32("Type-specific Information", type_specific_info)?;
312 ControlPacket::Nak(NakPacket::parse_cif(timestamp, dest_socket_id, cif)?)
313 }
314 ControlType::CongestionWarning => {
315 check_reserved_u16("Subtype", subtype)?;
316 check_reserved_u32("Type-specific Information", type_specific_info)?;
317 let libsrt_pad = check_no_cif_or_libsrt_pad("congestion warning CIF", cif)?;
318 ControlPacket::CongestionWarning(CongestionWarningPacket {
319 timestamp,
320 dest_socket_id,
321 libsrt_pad,
322 })
323 }
324 ControlType::Shutdown => {
325 check_reserved_u16("Subtype", subtype)?;
326 check_reserved_u32("Type-specific Information", type_specific_info)?;
327 let libsrt_pad = check_no_cif_or_libsrt_pad("shutdown CIF", cif)?;
328 ControlPacket::Shutdown(ShutdownPacket {
329 timestamp,
330 dest_socket_id,
331 libsrt_pad,
332 })
333 }
334 ControlType::AckAck => {
335 check_reserved_u16("Subtype", subtype)?;
336 let libsrt_pad = check_no_cif_or_libsrt_pad("ACKACK CIF", cif)?;
337 ControlPacket::AckAck(AckAckPacket {
338 ack_number: AckNumber::from_wire(type_specific_info),
339 timestamp,
340 dest_socket_id,
341 libsrt_pad,
342 })
343 }
344 ControlType::DropReq => {
345 check_reserved_u16("Subtype", subtype)?;
346 ControlPacket::DropReq(DropReqPacket::parse_cif(
347 type_specific_info,
348 timestamp,
349 dest_socket_id,
350 cif,
351 )?)
352 }
353 ControlType::PeerError => {
354 check_reserved_u16("Subtype", subtype)?;
355 let libsrt_pad = check_no_cif_or_libsrt_pad("peer error CIF", cif)?;
356 ControlPacket::PeerError(PeerErrorPacket {
357 error_code: type_specific_info,
358 timestamp,
359 dest_socket_id,
360 libsrt_pad,
361 })
362 }
363 ControlType::UserDefined | ControlType::Reserved(_) => {
364 ControlPacket::UserDefined(UserDefinedPacket {
365 control_type: control_type_bits,
366 subtype,
367 type_specific_info,
368 timestamp,
369 dest_socket_id,
370 cif,
371 })
372 }
373 })
374 }
375
376 pub fn control_type(&self) -> ControlType {
378 match self {
379 ControlPacket::Handshake(_) => ControlType::Handshake,
380 ControlPacket::KeepAlive(_) => ControlType::KeepAlive,
381 ControlPacket::Ack(_) => ControlType::Ack,
382 ControlPacket::Nak(_) => ControlType::Nak,
383 ControlPacket::CongestionWarning(_) => ControlType::CongestionWarning,
384 ControlPacket::Shutdown(_) => ControlType::Shutdown,
385 ControlPacket::AckAck(_) => ControlType::AckAck,
386 ControlPacket::DropReq(_) => ControlType::DropReq,
387 ControlPacket::PeerError(_) => ControlType::PeerError,
388 ControlPacket::UserDefined(u) => ControlType::from_bits(u.control_type),
389 }
390 }
391
392 fn subtype(&self) -> u16 {
393 match self {
394 ControlPacket::UserDefined(u) => u.subtype,
395 _ => 0,
396 }
397 }
398
399 fn word1(&self) -> Result<u32> {
400 Ok(match self {
401 ControlPacket::Handshake(_)
402 | ControlPacket::KeepAlive(_)
403 | ControlPacket::Nak(_)
404 | ControlPacket::CongestionWarning(_)
405 | ControlPacket::Shutdown(_) => 0,
406 ControlPacket::Ack(a) => a.ack_number.value(),
411 ControlPacket::AckAck(a) => a.ack_number.value(),
412 ControlPacket::DropReq(d) => d.message_number.value(),
413 ControlPacket::PeerError(p) => p.error_code,
414 ControlPacket::UserDefined(u) => u.type_specific_info,
415 })
416 }
417
418 fn timestamp(&self) -> SrtTimestamp {
419 match self {
420 ControlPacket::Handshake(h) => h.timestamp,
421 ControlPacket::KeepAlive(k) => k.timestamp,
422 ControlPacket::Ack(a) => a.timestamp,
423 ControlPacket::Nak(n) => n.timestamp,
424 ControlPacket::CongestionWarning(c) => c.timestamp,
425 ControlPacket::Shutdown(s) => s.timestamp,
426 ControlPacket::AckAck(a) => a.timestamp,
427 ControlPacket::DropReq(d) => d.timestamp,
428 ControlPacket::PeerError(p) => p.timestamp,
429 ControlPacket::UserDefined(u) => u.timestamp,
430 }
431 }
432
433 fn dest_socket_id(&self) -> SocketId {
434 match self {
435 ControlPacket::Handshake(h) => h.dest_socket_id,
436 ControlPacket::KeepAlive(k) => k.dest_socket_id,
437 ControlPacket::Ack(a) => a.dest_socket_id,
438 ControlPacket::Nak(n) => n.dest_socket_id,
439 ControlPacket::CongestionWarning(c) => c.dest_socket_id,
440 ControlPacket::Shutdown(s) => s.dest_socket_id,
441 ControlPacket::AckAck(a) => a.dest_socket_id,
442 ControlPacket::DropReq(d) => d.dest_socket_id,
443 ControlPacket::PeerError(p) => p.dest_socket_id,
444 ControlPacket::UserDefined(u) => u.dest_socket_id,
445 }
446 }
447
448 fn cif_len(&self) -> usize {
449 match self {
450 ControlPacket::Handshake(h) => h.cif_len(),
451 ControlPacket::KeepAlive(k) => {
455 if k.libsrt_pad {
456 LIBSRT_CIF_PAD_LEN
457 } else {
458 0
459 }
460 }
461 ControlPacket::CongestionWarning(c) => {
462 if c.libsrt_pad {
463 LIBSRT_CIF_PAD_LEN
464 } else {
465 0
466 }
467 }
468 ControlPacket::Shutdown(s) => {
469 if s.libsrt_pad {
470 LIBSRT_CIF_PAD_LEN
471 } else {
472 0
473 }
474 }
475 ControlPacket::AckAck(a) => {
476 if a.libsrt_pad {
477 LIBSRT_CIF_PAD_LEN
478 } else {
479 0
480 }
481 }
482 ControlPacket::PeerError(p) => {
483 if p.libsrt_pad {
484 LIBSRT_CIF_PAD_LEN
485 } else {
486 0
487 }
488 }
489 ControlPacket::Ack(a) => a.cif_len(),
490 ControlPacket::Nak(n) => n.cif_len(),
491 ControlPacket::DropReq(d) => d.cif_len(),
492 ControlPacket::UserDefined(u) => u.cif.len(),
493 }
494 }
495
496 pub fn serialized_len(&self) -> usize {
498 SRT_HEADER_LEN + self.cif_len()
499 }
500
501 pub fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
503 let len = self.serialized_len();
504 need_out(buf, len)?;
505 let control_type_bits = self.control_type().to_bits();
506 broadcast_common::len::fit_bits(u64::from(control_type_bits), 15, "Control Type")?;
507 let word0 = super::F_BIT | (u32::from(control_type_bits) << 16) | u32::from(self.subtype());
508 put_be32(buf, 0, word0);
509 put_be32(buf, 4, self.word1()?);
510 put_be32(buf, 8, self.timestamp().value());
511 put_be32(buf, 12, self.dest_socket_id().value());
512 let cif = &mut buf[SRT_HEADER_LEN..len];
513 match self {
514 ControlPacket::Handshake(h) => {
515 h.write_cif(cif)?;
516 }
517 ControlPacket::KeepAlive(_)
518 | ControlPacket::CongestionWarning(_)
519 | ControlPacket::Shutdown(_)
520 | ControlPacket::AckAck(_)
521 | ControlPacket::PeerError(_) => {
522 cif.fill(0);
523 }
524 ControlPacket::Ack(a) => {
525 a.write_cif(cif)?;
526 }
527 ControlPacket::Nak(n) => {
528 n.write_cif(cif);
529 }
530 ControlPacket::DropReq(d) => {
531 d.write_cif(cif)?;
532 }
533 ControlPacket::UserDefined(u) => {
534 cif.copy_from_slice(u.cif);
535 }
536 }
537 Ok(len)
538 }
539}
540
541impl<'a> broadcast_common::Parse<'a> for ControlPacket<'a> {
542 type Error = crate::Error;
543
544 fn parse(bytes: &'a [u8]) -> core::result::Result<Self, Self::Error> {
545 ControlPacket::parse(bytes)
546 }
547}
548
549impl broadcast_common::Serialize for ControlPacket<'_> {
550 type Error = crate::Error;
551
552 fn serialized_len(&self) -> usize {
553 ControlPacket::serialized_len(self)
554 }
555
556 fn serialize_into(&self, buf: &mut [u8]) -> core::result::Result<usize, Self::Error> {
557 ControlPacket::serialize_into(self, buf)
558 }
559}
560
561#[cfg(test)]
562mod tests {
563 use super::*;
564
565 #[test]
566 fn every_control_type_round_trips_its_table_1_wire_value() {
567 for (ty, value) in [
568 (ControlType::Handshake, 0x0000u16),
569 (ControlType::KeepAlive, 0x0001),
570 (ControlType::Ack, 0x0002),
571 (ControlType::Nak, 0x0003),
572 (ControlType::CongestionWarning, 0x0004),
573 (ControlType::Shutdown, 0x0005),
574 (ControlType::AckAck, 0x0006),
575 (ControlType::DropReq, 0x0007),
576 (ControlType::PeerError, 0x0008),
577 (ControlType::UserDefined, 0x7FFF),
578 ] {
579 assert_eq!(ty.to_bits(), value, "{}", ty.name());
580 assert_eq!(ControlType::from_bits(value), ty);
581 }
582 }
583
584 #[test]
585 fn a_control_type_off_table_1_is_reserved_and_keeps_its_value() {
586 for value in [0x0009u16, 0x0010, 0x7FFE] {
589 assert_eq!(ControlType::from_bits(value), ControlType::Reserved(value));
590 assert_eq!(ControlType::from_bits(value).to_bits(), value);
591 }
592 assert_eq!(ControlType::Reserved(0x0010).name(), "reserved");
593 }
594}