Skip to content

Commit f4406a2

Browse files
committed
working on SCTP classes
1 parent 03387bd commit f4406a2

File tree

4 files changed

+169
-97
lines changed

4 files changed

+169
-97
lines changed

worker/include/RTC/SCTP/Chunk.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ namespace RTC
128128
return GetHeaderPointer()->type;
129129
}
130130

131+
virtual bool HasUnknownType() const final
132+
{
133+
auto type = GetType();
134+
135+
return type < ChunkType::DATA || type > ChunkType::SHUTDOWN_COMPLETE;
136+
}
137+
131138
virtual uint8_t GetFlags() const final
132139
{
133140
return GetHeaderPointer()->flags;

worker/include/RTC/SCTP/Packet.hpp

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define MS_RTC_SCTP_PACKET_HPP
33

44
#include "common.hpp"
5-
// #include "RTC/SCTP/Chunk.hpp"
5+
#include "RTC/SCTP/Chunk.hpp"
66
#include "RTC/Serializable.hpp"
77
#include <vector>
88

@@ -49,6 +49,9 @@ namespace RTC
4949

5050
class Packet : public Serializable
5151
{
52+
public:
53+
using ChunksIterator = typename std::vector<Chunk*>::const_iterator;
54+
5255
public:
5356
/**
5457
* Struct of a SCTP Packet Common Header.
@@ -67,7 +70,7 @@ namespace RTC
6770
/**
6871
* Whether given buffer could be a valid SCTP packet.
6972
*/
70-
static bool IsSctp(const uint8_t* buffer, size_t bufferLength);
73+
static bool IsPacket(const uint8_t* buffer, size_t bufferLength);
7174

7275
/**
7376
* Parse a SCTP packet.
@@ -98,65 +101,99 @@ namespace RTC
98101

99102
uint16_t GetSourcePort() const
100103
{
101-
return uint16_t{ ntohs(GetCommonHeaderPointer()->sourcePort) };
104+
return uint16_t{ ntohs(GetHeaderPointer()->sourcePort) };
102105
}
103106

104107
void SetSourcePort(uint16_t sourcePort)
105108
{
106109
AssertNotFrozen();
107110

108-
GetCommonHeaderPointer()->sourcePort = uint16_t{ htons(sourcePort) };
111+
GetHeaderPointer()->sourcePort = uint16_t{ htons(sourcePort) };
109112
}
110113

111114
uint16_t GetDestinationPort() const
112115
{
113-
return uint16_t{ ntohs(GetCommonHeaderPointer()->destinationPort) };
116+
return uint16_t{ ntohs(GetHeaderPointer()->destinationPort) };
114117
}
115118

116119
void SetDestinationPort(uint16_t destinationPort)
117120
{
118121
AssertNotFrozen();
119122

120-
GetCommonHeaderPointer()->destinationPort = uint16_t{ htons(destinationPort) };
123+
GetHeaderPointer()->destinationPort = uint16_t{ htons(destinationPort) };
121124
}
122125

123126
uint32_t GetVerificationTag() const
124127
{
125-
return uint32_t{ ntohl(GetCommonHeaderPointer()->verificationTag) };
128+
return uint32_t{ ntohl(GetHeaderPointer()->verificationTag) };
126129
}
127130

128131
void SetVerificationTag(uint32_t verificationTag)
129132
{
130133
AssertNotFrozen();
131134

132-
GetCommonHeaderPointer()->verificationTag = uint32_t{ htonl(verificationTag) };
135+
GetHeaderPointer()->verificationTag = uint32_t{ htonl(verificationTag) };
133136
}
134137

135138
uint32_t GetChecksum() const
136139
{
137-
return uint32_t{ ntohl(GetCommonHeaderPointer()->checksum) };
140+
return uint32_t{ ntohl(GetHeaderPointer()->checksum) };
138141
}
139142

140143
void SetChecksum(uint32_t checksum)
141144
{
142145
AssertNotFrozen();
143146

144-
GetCommonHeaderPointer()->checksum = uint32_t{ htonl(checksum) };
147+
GetHeaderPointer()->checksum = uint32_t{ htonl(checksum) };
148+
}
149+
150+
bool HasChunks() const
151+
{
152+
return GetLength() > Packet::CommonHeaderLength;
153+
}
154+
155+
size_t GetChunksCount() const
156+
{
157+
return this->chunks.size();
145158
}
146159

147-
// void AddChunk(Chunk* chunk)
148-
// {
149-
// AssertNotFrozen();
150-
//
151-
// this->chunks.push_back(chunk);
152-
// }
160+
ChunksIterator ChunksBegin() const
161+
{
162+
return this->chunks.begin();
163+
}
164+
165+
ChunksIterator ChunksEnd() const
166+
{
167+
return this->chunks.end();
168+
}
169+
170+
const Chunk* GetChunkAt(size_t idx) const
171+
{
172+
if (idx >= this->chunks.size())
173+
{
174+
return nullptr;
175+
}
176+
177+
return this->chunks[idx];
178+
}
179+
180+
/**
181+
* Clone given Chunk into Packet's buffer.
182+
*
183+
* @remarks
184+
* Once this method is called, the caller may want to free the original
185+
* given Chunk.
186+
*/
187+
void AddChunk(const Chunk* chunk);
153188

154189
private:
190+
virtual void InitializeHeader() final;
191+
155192
/**
156193
* NOTE: Return CommonHeader* instead of const CommonHeader* since we may
157194
* want to modify its fields.
158195
*/
159-
CommonHeader* GetCommonHeaderPointer() const
196+
CommonHeader* GetHeaderPointer() const
160197
{
161198
return reinterpret_cast<CommonHeader*>(const_cast<uint8_t*>(GetBuffer()));
162199
}
@@ -177,11 +214,11 @@ namespace RTC
177214
* since it's already serialized (obviously since we are parsing a
178215
* buffer).
179216
*/
180-
// void AddParsedChunk(Chunk* chunk);
217+
void AddParsedChunk(Chunk* chunk);
181218

182219
private:
183220
// Chunks.
184-
// std::vector<Chunk*> chunks;
221+
std::vector<Chunk*> chunks;
185222
};
186223
} // namespace SCTP
187224
} // namespace RTC

worker/src/RTC/SCTP/Packet.cpp

Lines changed: 105 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)