rtci-sctp 0.3.0

Sans-I/O SCTP (RFC 9260) with partial reliability and stream reconfiguration, for WebRTC data channels
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use crate::chunk::Chunk;
use crate::chunk::chunk_abort::ChunkAbort;
use crate::chunk::chunk_cookie_ack::ChunkCookieAck;
use crate::chunk::chunk_cookie_echo::ChunkCookieEcho;
use crate::chunk::chunk_error::ChunkError;
use crate::chunk::chunk_forward_tsn::ChunkForwardTsn;
use crate::chunk::chunk_header::*;
use crate::chunk::chunk_heartbeat::ChunkHeartbeat;
use crate::chunk::chunk_heartbeat_ack::ChunkHeartbeatAck;
use crate::chunk::chunk_i_forward_tsn::ChunkIForwardTsn;
use crate::chunk::chunk_init::ChunkInit;
use crate::chunk::chunk_padding::ChunkPadding;
use crate::chunk::chunk_payload_data::ChunkPayloadData;
use crate::chunk::chunk_reconfig::ChunkReconfig;
use crate::chunk::chunk_selective_ack::ChunkSelectiveAck;
use crate::chunk::chunk_shutdown::ChunkShutdown;
use crate::chunk::chunk_shutdown_ack::ChunkShutdownAck;
use crate::chunk::chunk_shutdown_complete::ChunkShutdownComplete;
use crate::chunk::chunk_type::*;
use crate::chunk::chunk_unknown::ChunkUnknown;
use crate::util::*;
use shared::error::{Error, Result};

use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::fmt;

///Packet represents an SCTP packet, defined in https://tools.ietf.org/html/rfc4960#section-3
///An SCTP packet is composed of a common header and chunks.  A chunk
///contains either control information or user data.
///
///
///SCTP Packet Format
/// 0                   1                   2                   3
/// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///|                        Common Header                          |
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///|                          Chunk #1                             |
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///|                           ...                                 |
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///|                          Chunk #n                             |
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///
///
///SCTP Common Header Format
///
/// 0                   1                   2                   3
/// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///|     Source Value Number        |     Destination Value Number |
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///|                      Verification Tag                         |
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///|                           Checksum                            |
///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
pub(crate) const PACKET_HEADER_SIZE: usize = 12;

#[derive(Default, Debug)]
pub(crate) struct CommonHeader {
    pub(crate) source_port: u16,
    pub(crate) destination_port: u16,
    pub(crate) verification_tag: u32,
}

#[derive(Default, Debug)]
pub struct PartialDecode {
    pub(crate) common_header: CommonHeader,
    pub(crate) remaining: Bytes,
    pub(crate) first_chunk_type: ChunkType,
    pub(crate) initiate_tag: Option<u32>,
    pub(crate) cookie: Option<Bytes>,
    /// The packet's checksum field is zero and its CRC32c has not been computed: the whole
    /// packet, for whoever will not take a zero checksum on trust (RFC 9653 5.3).
    pub(crate) unverified: Option<Bytes>,
}

impl PartialDecode {
    /// Whether the packet may be processed. A packet whose checksum field is zero is taken
    /// as it is by an endpoint that announced "Zero Checksum Acceptable" (RFC 9653 5.3);
    /// for any other, zero has to be the packet's CRC32c.
    pub(crate) fn checksum_ok(&self, zero_acceptable: bool) -> bool {
        match &self.unverified {
            None => true,
            Some(_) if zero_acceptable => true,
            Some(raw) => generate_packet_checksum(raw) == 0,
        }
    }

    pub(crate) fn unmarshal(raw: &Bytes) -> Result<Self> {
        if raw.len() < PACKET_HEADER_SIZE {
            return Err(Error::ErrPacketRawTooSmall);
        }

        let reader = &mut raw.clone();

        let source_port = reader.get_u16();
        let destination_port = reader.get_u16();
        let verification_tag = reader.get_u32();
        let their_checksum = reader.get_u32_le();
        // RFC 9653: a checksum of zero is left for the association the packet is for to
        // judge, which may have said that it takes zero; the CRC32c is then not computed.
        let unverified = (their_checksum == 0).then(|| raw.clone());
        if unverified.is_none() && their_checksum != generate_packet_checksum(raw) {
            return Err(Error::ErrChecksumMismatch);
        }

        if reader.remaining() < CHUNK_HEADER_SIZE {
            return Err(Error::ErrParseSctpChunkNotEnoughData);
        }

        let header = ChunkHeader::unmarshal(reader)?;
        reader.advance(CHUNK_HEADER_SIZE);

        let mut initiate_tag = None;
        let mut cookie = None;
        match header.typ {
            CT_INIT | CT_INIT_ACK => {
                if header.value_length() < 4 {
                    return Err(Error::ErrChunkValueNotLongEnough);
                }
                initiate_tag = Some(reader.get_u32());
            }
            CT_COOKIE_ECHO => {
                cookie = Some(raw.slice(
                    PACKET_HEADER_SIZE + CHUNK_HEADER_SIZE
                        ..PACKET_HEADER_SIZE + CHUNK_HEADER_SIZE + header.value_length(),
                ));
            }
            _ => {}
        }

        Ok(PartialDecode {
            common_header: CommonHeader {
                source_port,
                destination_port,
                verification_tag,
            },
            remaining: raw.slice(PACKET_HEADER_SIZE..),
            first_chunk_type: header.typ,
            initiate_tag,
            cookie,
            unverified,
        })
    }

    /// The type and value of each chunk in the packet, as far as their headers are sound.
    /// For a packet no association will parse (RFC 9260 8.4).
    pub(crate) fn chunk_headers(&self) -> Vec<(ChunkType, Bytes)> {
        let raw = &self.remaining;
        let mut out = vec![];
        let mut offset = 0;
        while offset + CHUNK_HEADER_SIZE <= raw.len() {
            let length = u16::from_be_bytes([raw[offset + 2], raw[offset + 3]]) as usize;
            if length < CHUNK_HEADER_SIZE || offset + length > raw.len() {
                break;
            }
            out.push((
                ChunkType(raw[offset]),
                raw.slice(offset + CHUNK_HEADER_SIZE..offset + length),
            ));
            offset += length + get_padding_size(length);
        }
        out
    }

    pub(crate) fn finish(self) -> Result<Packet> {
        Ok(Packet {
            chunks: parse_chunks(&self.remaining, 0)?,
            common_header: self.common_header,
        })
    }
}

/// Parses the chunks of a packet, from `offset` in `raw` to its end.
///
/// A chunk of a type the engine has no parser for becomes a [`ChunkUnknown`]: RFC 9260 3.2
/// has the receiver act on the upper two bits of its type, which is the association's work.
/// A partial chunk at the end (one the packet is too short to hold) is dropped and the chunks
/// before it stand (RFC 9260 6.10). A chunk of a known type that does not parse fails the
/// packet.
fn parse_chunks(raw: &Bytes, mut offset: usize) -> Result<Vec<Box<dyn Chunk>>> {
    let mut chunks: Vec<Box<dyn Chunk>> = vec![];
    while offset + CHUNK_HEADER_SIZE <= raw.len() {
        let chunk_length = u16::from_be_bytes([raw[offset + 2], raw[offset + 3]]) as usize;
        if chunk_length < CHUNK_HEADER_SIZE {
            return Err(Error::ErrChunkHeaderInvalidLength);
        }
        if offset + chunk_length > raw.len() {
            // A partial chunk: with nothing before it the packet holds no chunk at all.
            if chunks.is_empty() {
                return Err(Error::ErrChunkHeaderNotEnoughSpace);
            }
            break;
        }
        let chunk_buf = raw.slice(offset..offset + chunk_length);
        let c: Box<dyn Chunk> = match ChunkType(raw[offset]) {
            CT_INIT => Box::new(ChunkInit::unmarshal(&chunk_buf)?),
            CT_INIT_ACK => Box::new(ChunkInit::unmarshal(&chunk_buf)?),
            CT_ABORT => Box::new(ChunkAbort::unmarshal(&chunk_buf)?),
            CT_COOKIE_ECHO => Box::new(ChunkCookieEcho::unmarshal(&chunk_buf)?),
            CT_COOKIE_ACK => Box::new(ChunkCookieAck::unmarshal(&chunk_buf)?),
            CT_HEARTBEAT => Box::new(ChunkHeartbeat::unmarshal(&chunk_buf)?),
            CT_HEARTBEAT_ACK => Box::new(ChunkHeartbeatAck::unmarshal(&chunk_buf)?),
            CT_PAYLOAD_DATA | CT_I_DATA => Box::new(ChunkPayloadData::unmarshal(&chunk_buf)?),
            CT_SACK => Box::new(ChunkSelectiveAck::unmarshal(&chunk_buf)?),
            CT_RECONFIG => Box::new(ChunkReconfig::unmarshal(&chunk_buf)?),
            CT_PAD => Box::new(ChunkPadding::unmarshal(&chunk_buf)?),
            CT_FORWARD_TSN => Box::new(ChunkForwardTsn::unmarshal(&chunk_buf)?),
            CT_I_FORWARD_TSN => Box::new(ChunkIForwardTsn::unmarshal(&chunk_buf)?),
            CT_ERROR => Box::new(ChunkError::unmarshal(&chunk_buf)?),
            CT_SHUTDOWN => Box::new(ChunkShutdown::unmarshal(&chunk_buf)?),
            CT_SHUTDOWN_ACK => Box::new(ChunkShutdownAck::unmarshal(&chunk_buf)?),
            CT_SHUTDOWN_COMPLETE => Box::new(ChunkShutdownComplete::unmarshal(&chunk_buf)?),
            _ => Box::new(ChunkUnknown::unmarshal(&chunk_buf)?),
        };
        // By the length on the wire, not the one the parsed chunk would be written with.
        offset += chunk_length + get_padding_size(chunk_length);
        chunks.push(c);
    }
    Ok(chunks)
}

#[derive(Default, Debug)]
pub(crate) struct Packet {
    pub(crate) common_header: CommonHeader,
    pub(crate) chunks: Vec<Box<dyn Chunk>>,
}

/// makes packet printable
impl fmt::Display for Packet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut res = format!(
            "Packet:
        source_port: {}
        destination_port: {}
        verification_tag: {}
        ",
            self.common_header.source_port,
            self.common_header.destination_port,
            self.common_header.verification_tag,
        );
        for chunk in &self.chunks {
            res += format!("Chunk: {}", chunk).as_str();
        }
        write!(f, "{}", res)
    }
}

impl Packet {
    pub(crate) fn unmarshal(raw: &Bytes) -> Result<Self> {
        if raw.len() < PACKET_HEADER_SIZE {
            return Err(Error::ErrPacketRawTooSmall);
        }

        let reader = &mut raw.clone();

        let source_port = reader.get_u16();
        let destination_port = reader.get_u16();
        let verification_tag = reader.get_u32();
        let their_checksum = reader.get_u32_le();
        let our_checksum = generate_packet_checksum(raw);

        if their_checksum != our_checksum {
            return Err(Error::ErrChecksumMismatch);
        }

        let chunks = parse_chunks(raw, PACKET_HEADER_SIZE)?;

        Ok(Packet {
            common_header: CommonHeader {
                source_port,
                destination_port,
                verification_tag,
            },
            chunks,
        })
    }

    /// Exact number of bytes `marshal_to` will append: common header plus every
    /// chunk with its trailing padding. Lets callers size the output buffer in a
    /// single allocation instead of allocating small and reallocating.
    pub(crate) fn marshaled_len(&self) -> usize {
        let body_len: usize = self
            .chunks
            .iter()
            .map(|c| {
                let n = CHUNK_HEADER_SIZE + c.value_length();
                n + get_padding_size(n)
            })
            .sum();
        PACKET_HEADER_SIZE + body_len
    }

    pub(crate) fn marshal_to(&self, writer: &mut BytesMut) -> Result<usize> {
        // Grow the buffer once so appending chunks never reallocates. A direct
        // caller that passed an undersized buffer gets its single growth here;
        // `marshal` pre-sizes, so it skips this path entirely.
        writer.reserve(self.marshaled_len());
        Self::write_framed(
            &self.common_header,
            self.chunks.iter().map(|c| &**c as &dyn Chunk),
            writer,
        )
    }

    /// As [`marshal`](Self::marshal), with the checksum field left zero when `zero_checksum`
    /// is set (RFC 9653 5.2: for a peer that announced it takes that).
    pub(crate) fn marshal_with(&self, zero_checksum: bool) -> Result<Bytes> {
        let mut buf = BytesMut::with_capacity(self.marshaled_len());
        Self::write_framed_with(
            &self.common_header,
            self.chunks.iter().map(|c| &**c as &dyn Chunk),
            &mut buf,
            zero_checksum,
        )
        .map(|_| buf.freeze())
    }

    pub(crate) fn marshal(&self) -> Result<Bytes> {
        // Allocate the exact packet length once, up front. `with_capacity` is a
        // cheaper direct allocation than growing an empty `BytesMut` via
        // `reserve`, and `write_framed` needs no further sizing.
        let mut buf = BytesMut::with_capacity(self.marshaled_len());
        // `.map`, not `?; Ok(..)`: the result is transformed, not inspected, so
        // there is no separate error-return branch to leave untested (`write_framed`
        // only fails if a chunk's own `marshal_to` does). Identical codegen.
        Self::write_framed(
            &self.common_header,
            self.chunks.iter().map(|c| &**c as &dyn Chunk),
            &mut buf,
        )
        .map(|_| buf.freeze())
    }

    /// Serialize the common header, every chunk (with trailing padding) and the
    /// CRC-32C into `writer`, appended at its current end. This is the single
    /// home of the SCTP on-wire packet framing: `marshal`/`marshal_to` feed it
    /// boxed chunks, and the DATA send path feeds it borrowed `ChunkPayloadData`
    /// directly (no `Box`). The caller sizes the buffer.
    pub(crate) fn write_framed<'a>(
        common_header: &CommonHeader,
        chunks: impl Iterator<Item = &'a dyn Chunk>,
        writer: &mut BytesMut,
    ) -> Result<usize> {
        Self::write_framed_with(common_header, chunks, writer, false)
    }

    /// As [`write_framed`](Self::write_framed); with `zero_checksum` the CRC32c is not
    /// computed and the checksum field stays zero (RFC 9653).
    pub(crate) fn write_framed_with<'a>(
        common_header: &CommonHeader,
        chunks: impl Iterator<Item = &'a dyn Chunk>,
        writer: &mut BytesMut,
        zero_checksum: bool,
    ) -> Result<usize> {
        let start = writer.len();

        // Populate static headers
        writer.put_u16(common_header.source_port);
        writer.put_u16(common_header.destination_port);
        writer.put_u32(common_header.verification_tag);

        // Checksum placeholder (bytes 8..12). Kept zero while marshalling so the
        // CRC below is computed over exactly the bytes the receiver validates;
        // patched in place once the packet is complete.
        let checksum_offset = writer.len();
        writer.put_u32(0);

        // Marshal each chunk straight into the output buffer — no per-chunk
        // intermediate allocation, no byte-by-byte `extend`, no final re-copy.
        let chunks_start = writer.len();
        for c in chunks {
            c.marshal_to(writer)?;

            let padding_needed = get_padding_size(writer.len() - chunks_start);
            if padding_needed != 0 {
                writer.put_bytes(0, padding_needed);
            }
        }

        if zero_checksum {
            return Ok(writer.len());
        }

        // CRC-32C over the whole packet, checksum field still zero.
        let checksum = crc32c::crc32c(&writer[start..]);

        // Checksum is already in BigEndian; store little-endian so it isn't flipped.
        writer[checksum_offset..checksum_offset + 4].copy_from_slice(&checksum.to_le_bytes());

        Ok(writer.len())
    }
}

impl Packet {
    pub(crate) fn check_packet(&self) -> Result<()> {
        // All packets must adhere to these rules

        // This is the SCTP sender's port number.  It can be used by the
        // receiver in combination with the source IP address, the SCTP
        // destination port, and possibly the destination IP address to
        // identify the association to which this packet belongs.  The port
        // number 0 MUST NOT be used.
        if self.common_header.source_port == 0 {
            return Err(Error::ErrSctpPacketSourcePortZero);
        }

        // This is the SCTP port number to which this packet is destined.
        // The receiving host will use this port number to de-multiplex the
        // SCTP packet to the correct receiving endpoint/application.  The
        // port number 0 MUST NOT be used.
        if self.common_header.destination_port == 0 {
            return Err(Error::ErrSctpPacketDestinationPortZero);
        }

        // Check values on the packet that are specific to a particular chunk type
        for c in &self.chunks {
            if let Some(ci) = c.as_any().downcast_ref::<ChunkInit>() {
                // An INIT or INIT ACK chunk MUST NOT be bundled with any other chunk.
                // They MUST be the only chunks present in the SCTP packets that carry
                // them.
                if self.chunks.len() != 1 {
                    return Err(Error::ErrInitChunkBundled);
                }
                if ci.is_ack {
                    continue;
                }

                // A packet containing an INIT chunk MUST have a zero Verification
                // Tag.
                if self.common_header.verification_tag != 0 {
                    return Err(Error::ErrInitChunkVerifyTagNotZero);
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_packet_unmarshal() -> Result<()> {
        let result = Packet::unmarshal(&Bytes::new());
        assert!(
            result.is_err(),
            "Unmarshal should fail when a packet is too small to be SCTP"
        );

        let header_only = Bytes::from_static(&[
            0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x06, 0xa9, 0x00, 0xe1,
        ]);
        let pkt = Packet::unmarshal(&header_only)?;
        //assert!(result.o(), "Unmarshal failed for SCTP packet with no chunks: {}", result);
        assert_eq!(
            pkt.common_header.source_port, 5000,
            "Unmarshal passed for SCTP packet, but got incorrect source port exp: {} act: {}",
            5000, pkt.common_header.source_port
        );
        assert_eq!(
            pkt.common_header.destination_port, 5000,
            "Unmarshal passed for SCTP packet, but got incorrect destination port exp: {} act: {}",
            5000, pkt.common_header.destination_port
        );
        assert_eq!(
            pkt.common_header.verification_tag, 0,
            "Unmarshal passed for SCTP packet, but got incorrect verification tag exp: {} act: {}",
            0, pkt.common_header.verification_tag
        );

        let raw_chunk = Bytes::from_static(&[
            0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x81, 0x46, 0x9d, 0xfc, 0x01, 0x00,
            0x00, 0x56, 0x55, 0xb9, 0x64, 0xa5, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00,
            0xe8, 0x6d, 0x10, 0x30, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f,
            0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x9f, 0xeb, 0xbb, 0x5c,
            0x50, 0xc9, 0xbf, 0x75, 0x9c, 0xb1, 0x2c, 0x57, 0x4f, 0xa4, 0x5a, 0x51, 0xba, 0x60,
            0x17, 0x78, 0x27, 0x94, 0x5c, 0x31, 0xe6, 0x5d, 0x5b, 0x09, 0x47, 0xe2, 0x22, 0x06,
            0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1,
            0x00, 0x00,
        ]);

        Packet::unmarshal(&raw_chunk)?;

        Ok(())
    }

    #[test]
    fn test_packet_marshal() -> Result<()> {
        let header_only = Bytes::from_static(&[
            0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x06, 0xa9, 0x00, 0xe1,
        ]);
        let pkt = Packet::unmarshal(&header_only)?;
        let header_only_marshaled = pkt.marshal()?;
        assert_eq!(
            header_only, header_only_marshaled,
            "Unmarshal/Marshaled header only packet did not match \nheaderOnly: {:?} \nheader_only_marshaled {:?}",
            header_only, header_only_marshaled
        );

        Ok(())
    }

    // The boxing-free DATA send path marshals borrowed `ChunkPayloadData` straight
    // into a shared buffer via `write_framed`; it must produce exactly the same
    // bytes as building a `Packet` of boxed chunks and calling `marshal` -- both go
    // through `write_framed`. Also exercises `marshal_to`.
    #[test]
    fn test_marshal_data_chunks_matches_packet_marshal() -> Result<()> {
        use crate::chunk::chunk_payload_data::PayloadProtocolIdentifier;

        let mk_common = || CommonHeader {
            source_port: 5000,
            destination_port: 5000,
            verification_tag: 0xdeadbeef,
        };
        let mk_chunk = |tsn: u32, ssn: u16, data: &'static [u8]| ChunkPayloadData {
            stream_identifier: 1,
            payload_type: PayloadProtocolIdentifier::Binary,
            user_data: Bytes::from_static(data),
            beginning_fragment: true,
            ending_fragment: true,
            tsn,
            stream_sequence_number: ssn,
            ..Default::default()
        };

        // A single chunk, and a two-chunk bundle whose first payload length (5)
        // forces inter-chunk padding -- so the padding path is covered too.
        let cases = [
            vec![mk_chunk(1, 0, b"hello")],
            vec![mk_chunk(1, 0, b"hello"), mk_chunk(2, 1, b"world!!")],
        ];
        for chunks in cases {
            let direct = {
                let mut buf = BytesMut::new();
                Packet::write_framed(
                    &mk_common(),
                    chunks.iter().map(|c| c as &dyn Chunk),
                    &mut buf,
                )?;
                buf.freeze()
            };

            let pkt = Packet {
                common_header: mk_common(),
                chunks: chunks
                    .iter()
                    .cloned()
                    .map(|c| Box::new(c) as Box<dyn Chunk>)
                    .collect(),
            };
            assert_eq!(
                direct,
                pkt.marshal()?,
                "write_framed DATA path must equal Packet::marshal"
            );

            let mut buf = BytesMut::new();
            pkt.marshal_to(&mut buf)?;
            assert_eq!(
                direct,
                buf.freeze(),
                "marshal_to must equal the write_framed DATA path"
            );
        }

        Ok(())
    }

    /*fn BenchmarkPacketGenerateChecksum(b *testing.B) {
        var data [1024]byte

        for i := 0; i < b.N; i++ {
            _ = generatePacketChecksum(data[:])
        }
    }*/

    #[test]
    fn test_partial_decode_init_chunk() -> Result<()> {
        let raw_pkt = Bytes::from_static(&[
            0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x81, 0x46, 0x9d, 0xfc, 0x01, 0x00,
            0x00, 0x56, 0x55, 0xb9, 0x64, 0xa5, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00,
            0xe8, 0x6d, 0x10, 0x30, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f,
            0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x9f, 0xeb, 0xbb, 0x5c,
            0x50, 0xc9, 0xbf, 0x75, 0x9c, 0xb1, 0x2c, 0x57, 0x4f, 0xa4, 0x5a, 0x51, 0xba, 0x60,
            0x17, 0x78, 0x27, 0x94, 0x5c, 0x31, 0xe6, 0x5d, 0x5b, 0x09, 0x47, 0xe2, 0x22, 0x06,
            0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1,
            0x00, 0x00,
        ]);
        let pkt = PartialDecode::unmarshal(&raw_pkt)?;

        assert_eq!(pkt.first_chunk_type, CT_INIT);
        if let Some(initiate_tag) = pkt.initiate_tag {
            assert_eq!(
                initiate_tag, 1438213285,
                "Unmarshal passed for SCTP packet, but got incorrect initiate tag exp: {} act: {}",
                1438213285, initiate_tag
            );
        }

        Ok(())
    }

    #[test]
    fn test_partial_decode_init_ack() -> Result<()> {
        let raw_pkt = Bytes::from_static(&[
            0x13, 0x88, 0x13, 0x88, 0xce, 0x15, 0x79, 0xa2, 0x96, 0x19, 0xe8, 0xb2, 0x02, 0x00,
            0x00, 0x1c, 0xeb, 0x81, 0x4e, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00,
            0x50, 0xdf, 0x90, 0xd9, 0x00, 0x07, 0x00, 0x08, 0x94, 0x06, 0x2f, 0x93,
        ]);
        let pkt = PartialDecode::unmarshal(&raw_pkt)?;

        assert_eq!(pkt.first_chunk_type, CT_INIT_ACK);
        if let Some(initiate_tag) = pkt.initiate_tag {
            assert_eq!(
                initiate_tag, 3951119873u32,
                "Unmarshal passed for SCTP packet, but got incorrect initiate tag exp: {} act: {}",
                3951119873u32, initiate_tag
            );
        }

        Ok(())
    }

    #[test]
    fn test_partial_decode_truncated_init() {
        // INIT chunk header with length 4: no room for the initiate tag.
        let mut raw = vec![
            0x13, 0x88, 0x13, 0x88, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 4,
        ];
        let checksum = generate_packet_checksum(&Bytes::from(raw.clone()));
        raw[8..12].copy_from_slice(&checksum.to_le_bytes());
        assert!(PartialDecode::unmarshal(&Bytes::from(raw)).is_err());
    }

    #[test]
    fn test_unmarshal_variable_length_chunks_followed_by_other_chunks() -> Result<()> {
        use crate::chunk::chunk_payload_data::PayloadProtocolIdentifier;
        use crate::chunk::{ErrorCause, PROTOCOL_VIOLATION, UNRECOGNIZED_CHUNK_TYPE};

        let original = Packet {
            common_header: CommonHeader {
                source_port: 5000,
                destination_port: 5000,
                verification_tag: 0xdeadbeef,
            },
            chunks: vec![
                Box::new(ChunkForwardTsn {
                    new_cumulative_tsn: 3,
                    streams: vec![],
                }),
                Box::new(ChunkAbort {
                    error_causes: vec![ErrorCause {
                        code: PROTOCOL_VIOLATION,
                        ..Default::default()
                    }],
                    tag_reflected: false,
                }),
                Box::new(ChunkError {
                    error_causes: vec![ErrorCause {
                        code: UNRECOGNIZED_CHUNK_TYPE,
                        raw: Bytes::from_static(&[0xc0, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03]),
                    }],
                }),
                Box::new(ChunkShutdown {
                    cumulative_tsn_ack: 0x12345678,
                }),
                Box::new(ChunkPayloadData {
                    unordered: true,
                    beginning_fragment: true,
                    ending_fragment: true,
                    tsn: 4,
                    stream_identifier: 1,
                    payload_type: PayloadProtocolIdentifier::Binary,
                    user_data: Bytes::from_static(&[0xaa, 0xbb, 0xcc, 0xdd]),
                    ..Default::default()
                }),
            ],
        };
        let raw = original.marshal()?;
        let parsed = Packet::unmarshal(&raw)?;

        assert_eq!(parsed.chunks.len(), 5);

        let fwd = parsed.chunks[0]
            .as_any()
            .downcast_ref::<ChunkForwardTsn>()
            .expect("chunks[0] should be FORWARD-TSN");
        assert_eq!(fwd.new_cumulative_tsn, 3);
        assert!(fwd.streams.is_empty());

        let abort = parsed.chunks[1]
            .as_any()
            .downcast_ref::<ChunkAbort>()
            .expect("chunks[1] should be ABORT");
        assert_eq!(abort.error_causes.len(), 1);
        assert_eq!(abort.error_causes[0].error_cause_code(), PROTOCOL_VIOLATION);

        let err = parsed.chunks[2]
            .as_any()
            .downcast_ref::<ChunkError>()
            .expect("chunks[2] should be ERROR");
        assert_eq!(err.error_causes.len(), 1);
        assert_eq!(
            err.error_causes[0].error_cause_code(),
            UNRECOGNIZED_CHUNK_TYPE
        );

        let shutdown = parsed.chunks[3]
            .as_any()
            .downcast_ref::<ChunkShutdown>()
            .expect("chunks[3] should be SHUTDOWN");
        assert_eq!(shutdown.cumulative_tsn_ack, 0x12345678);

        let data = parsed.chunks[4]
            .as_any()
            .downcast_ref::<ChunkPayloadData>()
            .expect("chunks[4] should be DATA");
        assert_eq!(data.tsn, 4);
        assert_eq!(data.stream_identifier, 1);
        assert_eq!(data.payload_type, PayloadProtocolIdentifier::Binary);
        assert_eq!(&data.user_data[..], &[0xaa, 0xbb, 0xcc, 0xdd]);
        Ok(())
    }
}