@@ -13,7 +13,7 @@ namespace RTC
1313 {
1414 /* Class methods. */
1515
16- bool Packet::IsSctp (const uint8_t * buffer, size_t bufferLength)
16+ bool Packet::IsPacket (const uint8_t * buffer, size_t bufferLength)
1717 {
1818 auto * header = reinterpret_cast <const Packet::CommonHeader*>(buffer);
1919
@@ -30,7 +30,7 @@ namespace RTC
3030 {
3131 MS_TRACE ();
3232
33- if (!Packet::IsSctp (buffer, bufferLength))
33+ if (!Packet::IsPacket (buffer, bufferLength))
3434 {
3535 MS_WARN_TAG (sctp, " not an SCTP packet" );
3636
@@ -55,73 +55,73 @@ namespace RTC
5555 // Move to chunks.
5656 ptr = packet->GetChunksPointer ();
5757
58- // while (ptr < buffer + bufferLength)
59- // {
60- // // The remaining length in the buffer is the potential buffer length
61- // // of the chunk.
62- // size_t chunkBufferLength = packet->GetEndPointer() - ptr;
63-
64- // // Here we must anticipate the type of each chunk to use its appropriate
65- // // parser.
66- // Chunk::ChunkType chunkType;
67- // uint16_t chunkLength;
68-
69- // if (!Chunk::IsChunk(ptr, itemBufferLength , chunkType, chunkLength))
70- // {
71- // MS_WARN_DEV("not a Chunk");
72-
73- // delete packet;
74- // return nullptr;
75- // }
76-
77- // Chunk* chunk{ nullptr };
78-
79- // MS_DEBUG_DEV("parsing Chunk [ptr:%zu, type:%" PRIu8 "]", ptr - buffer, chunkType);
80-
81- // switch (chunkType)
82- // {
83- // case Chunk::ChunkType::XXXXX:
84- // {
85- // chunk = XxxxxChunk::Parse(ptr, itemBufferLength );
86-
87- // if (!chunk)
88- // {
89- // MS_WARN_DEV("XxxxxChunk parser failed");
90-
91- // delete packet;
92- // return nullptr;
93- // }
94-
95- // break;
96- // }
97-
98- // default:
99- // {
100- // chunk = UnknownChunk::Parse(ptr, itemBufferLength );
101-
102- // if (!chunk)
103- // {
104- // MS_WARN_DEV("UnknownChunk parser failed");
105-
106- // delete packet;
107- // return nullptr;
108- // }
109- // }
110- // }
111-
112- // // Let's fix chunk's buffer length. This is because we didn't know its
113- // // exact length when we called FooItem ::Parse() so we passed the rest
114- // // of the Packet buffer as buffer length. Once chunk is parsed, and
115- // // given that it is part of the Packet buffer, we can fix its buffer
116- // // length by making it be equal to its real length.
117- // chunk->SetBufferLength(chunk->GetLength());
118-
119- // // Here we are parsing so we don't use AddChunk() (that clones the
120- // // Chunk into the Packet buffer) but AddParsedChunk().
121- // packet->AddParsedChunk(chunk);
122-
123- // ptr += chunk->GetLength();
124- // }
58+ while (ptr < buffer + bufferLength)
59+ {
60+ // The remaining length in the buffer is the potential buffer length
61+ // of the chunk.
62+ size_t chunkBufferLength = packet->GetEndPointer () - ptr;
63+
64+ // Here we must anticipate the type of each chunk to use its appropriate
65+ // parser.
66+ Chunk::ChunkType chunkType;
67+ uint16_t chunkLength;
68+
69+ if (!Chunk::IsChunk (ptr, chunkBufferLength , chunkType, chunkLength))
70+ {
71+ MS_WARN_DEV (" not a Chunk" );
72+
73+ delete packet;
74+ return nullptr ;
75+ }
76+
77+ Chunk* chunk{ nullptr };
78+
79+ MS_DEBUG_DEV (" parsing Chunk [ptr:%zu, type:%" PRIu8 " ]" , ptr - buffer, chunkType);
80+
81+ switch (chunkType)
82+ {
83+ // case Chunk::ChunkType::XXXXX:
84+ // {
85+ // chunk = XxxxxChunk::Parse(ptr, chunkBufferLength );
86+
87+ // if (!chunk)
88+ // {
89+ // MS_WARN_DEV("XxxxxChunk parser failed");
90+
91+ // delete packet;
92+ // return nullptr;
93+ // }
94+
95+ // break;
96+ // }
97+
98+ // default:
99+ // {
100+ // chunk = UnknownChunk::Parse(ptr, chunkBufferLength );
101+
102+ // if (!chunk)
103+ // {
104+ // MS_WARN_DEV("UnknownChunk parser failed");
105+
106+ // delete packet;
107+ // return nullptr;
108+ // }
109+ // }
110+ }
111+
112+ // Let's fix chunk's buffer length. This is because we didn't know its
113+ // exact length when we called Chunk ::Parse() so we passed the rest
114+ // of the Packet buffer as buffer length. Once chunk is parsed, and
115+ // given that it is part of the Packet buffer, we can fix its buffer
116+ // length by making it be equal to its real length.
117+ chunk->SetBufferLength (chunk->GetLength ());
118+
119+ // Here we are parsing so we don't use AddChunk() (that clones the
120+ // Chunk into the Packet buffer) but AddParsedChunk().
121+ packet->AddParsedChunk (chunk);
122+
123+ ptr += chunk->GetLength ();
124+ }
125125
126126 const size_t computedLength = ptr - buffer;
127127
@@ -181,13 +181,12 @@ namespace RTC
181181 MS_DUMP (" destination port: %" PRIu16, GetDestinationPort ());
182182 MS_DUMP (" verification tag: %" PRIu32, GetVerificationTag ());
183183 MS_DUMP (" checksum: %" PRIu32, GetChecksum ());
184- // TODO
185- // MS_DUMP(" has chunks: %s", HasChunks() ? "yes" : "no");
186- // MS_DUMP(" chunks count: %zu", GetChunksCount());
187- // for (auto* chunk : this->chunks)
188- // {
189- // chunk->Dump();
190- // }
184+ MS_DUMP (" has chunks: %s" , HasChunks () ? " yes" : " no" );
185+ MS_DUMP (" chunks count: %zu" , GetChunksCount ());
186+ for (auto * chunk : this ->chunks )
187+ {
188+ chunk->Dump ();
189+ }
191190 MS_DUMP (" </Packet>" );
192191 }
193192
@@ -229,7 +228,7 @@ namespace RTC
229228 SetBuffer (buffer);
230229 SetBufferLength (bufferLength);
231230
232- // May unfreeze the packet (but not its items ).
231+ // May unfreeze the packet (but not its chunks ).
233232 Unfreeze ();
234233 }
235234
@@ -241,5 +240,34 @@ namespace RTC
241240
242241 return nullptr ;
243242 }
243+
244+ void Packet::AddChunk (const Chunk* chunk)
245+ {
246+ MS_TRACE ();
247+
248+ AssertNotFrozen ();
249+
250+ // TODO
251+ }
252+
253+ void Packet::InitializeHeader ()
254+ {
255+ MS_TRACE ();
256+
257+ SetSourcePort (0u );
258+ SetDestinationPort (0u );
259+ SetVerificationTag (0u );
260+ SetChecksum (0u );
261+ }
262+
263+ void Packet::AddParsedChunk (Chunk* chunk)
264+ {
265+ MS_TRACE ();
266+
267+ // Freeze the chunk.
268+ chunk->Freeze ();
269+
270+ this ->chunks .push_back (chunk);
271+ }
244272 } // namespace SCTP
245273} // namespace RTC
0 commit comments