| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
/***************************************************************************** |
| 12 |
Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois. |
| 13 |
All rights reserved. |
| 14 |
|
| 15 |
Redistribution and use in source and binary forms, with or without |
| 16 |
modification, are permitted provided that the following conditions are |
| 17 |
met: |
| 18 |
|
| 19 |
* Redistributions of source code must retain the above |
| 20 |
copyright notice, this list of conditions and the |
| 21 |
following disclaimer. |
| 22 |
|
| 23 |
* Redistributions in binary form must reproduce the |
| 24 |
above copyright notice, this list of conditions |
| 25 |
and the following disclaimer in the documentation |
| 26 |
and/or other materials provided with the distribution. |
| 27 |
|
| 28 |
* Neither the name of the University of Illinois |
| 29 |
nor the names of its contributors may be used to |
| 30 |
endorse or promote products derived from this |
| 31 |
software without specific prior written permission. |
| 32 |
|
| 33 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 34 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 35 |
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 36 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 37 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 38 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 39 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 40 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 41 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 42 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 43 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 |
*****************************************************************************/ |
| 45 |
|
| 46 |
/***************************************************************************** |
| 47 |
written by |
| 48 |
Yunhong Gu, last updated 02/12/2011 |
| 49 |
modified by |
| 50 |
Haivision Systems Inc. |
| 51 |
*****************************************************************************/ |
| 52 |
|
| 53 |
////////////////////////////////////////////////////////////////////////////// |
| 54 |
// 0 1 2 3 |
| 55 |
// 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 |
| 56 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 57 |
// | Packet Header | |
| 58 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 59 |
// | | |
| 60 |
// ~ Data / Control Information Field ~ |
| 61 |
// | | |
| 62 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 63 |
// |
| 64 |
// 0 1 2 3 |
| 65 |
// 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 |
| 66 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 67 |
// |0| Sequence Number | |
| 68 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 69 |
// |ff |o|kf |r| Message Number | |
| 70 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 71 |
// | Time Stamp | |
| 72 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 73 |
// | Destination Socket ID | |
| 74 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 75 |
// |
| 76 |
// bit 0: |
| 77 |
// 0: Data Packet |
| 78 |
// 1: Control Packet |
| 79 |
// bit ff: |
| 80 |
// 11: solo message packet |
| 81 |
// 10: first packet of a message |
| 82 |
// 01: last packet of a message |
| 83 |
// bit o: |
| 84 |
// 0: in order delivery not required |
| 85 |
// 1: in order delivery required |
| 86 |
// bit kf: HaiCrypt Key Flags |
| 87 |
// 00: not encrypted |
| 88 |
// 01: encrypted with even key |
| 89 |
// 10: encrypted with odd key |
| 90 |
// bit r: retransmission flag (set to 1 if this packet was sent again) |
| 91 |
// |
| 92 |
// 0 1 2 3 |
| 93 |
// 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 |
| 94 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 95 |
// |1| Type | Reserved | |
| 96 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 97 |
// | Additional Info | |
| 98 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 99 |
// | Time Stamp | |
| 100 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 101 |
// | Destination Socket ID | |
| 102 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 103 |
// |
| 104 |
// bit 1-15: Message type -- see @a UDTMessageType |
| 105 |
// 0: Protocol Connection Handshake (UMSG_HANDSHAKE} |
| 106 |
// Add. Info: Undefined |
| 107 |
// Control Info: Handshake information (see @a CHandShake) |
| 108 |
// 1: Keep-alive (UMSG_KEEPALIVE) |
| 109 |
// Add. Info: Undefined |
| 110 |
// Control Info: None |
| 111 |
// 2: Acknowledgement (UMSG_ACK) |
| 112 |
// Add. Info: The ACK sequence number |
| 113 |
// Control Info: The sequence number to which (but not include) all the previous packets have been received |
| 114 |
// Optional: RTT |
| 115 |
// RTT Variance |
| 116 |
// available receiver buffer size (in bytes) |
| 117 |
// advertised flow window size (number of packets) |
| 118 |
// estimated bandwidth (number of packets per second) |
| 119 |
// 3: Negative Acknowledgement (UMSG_LOSSREPORT) |
| 120 |
// Add. Info: Undefined |
| 121 |
// Control Info: Loss list (see loss list coding below) |
| 122 |
// 4: Congestion/Delay Warning (UMSG_CGWARNING) |
| 123 |
// Add. Info: Undefined |
| 124 |
// Control Info: None |
| 125 |
// 5: Shutdown (UMSG_SHUTDOWN) |
| 126 |
// Add. Info: Undefined |
| 127 |
// Control Info: None |
| 128 |
// 6: Acknowledgement of Acknowledement (UMSG_ACKACK) |
| 129 |
// Add. Info: The ACK sequence number |
| 130 |
// Control Info: None |
| 131 |
// 7: Message Drop Request (UMSG_DROPREQ) |
| 132 |
// Add. Info: Message ID |
| 133 |
// Control Info: first sequence number of the message |
| 134 |
// last sequence number of the message |
| 135 |
// 8: Error Signal from the Peer Side (UMSG_PEERERROR) |
| 136 |
// Add. Info: Error code |
| 137 |
// Control Info: None |
| 138 |
// 0x7FFF: Explained by bits 16 - 31 (UMSG_EXT) |
| 139 |
// |
| 140 |
// bit 16 - 31: |
| 141 |
// This space is used for future expansion or user defined control packets. |
| 142 |
// |
| 143 |
// 0 1 2 3 |
| 144 |
// 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 |
| 145 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 146 |
// |1| Sequence Number a (first) | |
| 147 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 148 |
// |0| Sequence Number b (last) | |
| 149 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 150 |
// |0| Sequence Number (single) | |
| 151 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 152 |
// |
| 153 |
// Loss List Field Coding: |
| 154 |
// For any consecutive lost sequence numbers that the difference between |
| 155 |
// the last and first is more than 1, only record the first (a) and the |
| 156 |
// the last (b) sequence numbers in the loss list field, and modify the |
| 157 |
// the first bit of a to 1. |
| 158 |
// For any single loss or consecutive loss less than 2 packets, use |
| 159 |
// the original sequence numbers in the field. |
| 160 |
|
| 161 |
#include "platform_sys.h" |
| 162 |
#include <cstddef> |
| 163 |
#include <cstring> |
| 164 |
#include "packet.h" |
| 165 |
#include "handshake.h" |
| 166 |
#include "logging.h" |
| 167 |
#include "handshake.h" |
| 168 |
|
| 169 |
namespace srt_logging |
| 170 |
{ |
| 171 |
extern Logger inlog; |
| 172 |
} |
| 173 |
using namespace srt_logging; |
| 174 |
|
| 175 |
namespace srt { |
| 176 |
|
| 177 |
// Set up the aliases in the constructor |
| 178 |
CPacket::CPacket() |
| 179 |
: m_nHeader() // Silences GCC 12 warning "used uninitialized". |
| 180 |
, m_extra_pad() |
| 181 |
, m_data_owned(false) |
| 182 |
, m_pcData((char*&)(m_PacketVector[PV_DATA].dataRef())) |
| 183 |
{ |
| 184 |
m_nHeader.clear(); |
| 185 |
|
| 186 |
// The part at PV_HEADER will be always set to a builtin buffer |
| 187 |
// containing SRT header. |
| 188 |
m_PacketVector[PV_HEADER].set(m_nHeader.raw(), HDR_SIZE); |
| 189 |
|
| 190 |
// The part at PV_DATA is zero-initialized. It should be |
| 191 |
// set (through m_pcData and setLength()) to some externally |
| 192 |
// provided buffer before calling CChannel::sendto(). |
| 193 |
m_PacketVector[PV_DATA].set(NULL, 0); |
| 194 |
} |
| 195 |
|
| 196 |
char* CPacket::getData() |
| 197 |
{ |
| 198 |
return (char*)m_PacketVector[PV_DATA].dataRef(); |
| 199 |
} |
| 200 |
|
| 201 |
void CPacket::allocate(size_t alloc_buffer_size) |
| 202 |
{ |
| 203 |
if (m_data_owned) |
| 204 |
{ |
| 205 |
if (getLength() == alloc_buffer_size) |
| 206 |
return; // already allocated |
| 207 |
|
| 208 |
// Would be nice to reallocate; for now just allocate again. |
| 209 |
delete[] m_pcData; |
| 210 |
} |
| 211 |
m_PacketVector[PV_DATA].set(new char[alloc_buffer_size], alloc_buffer_size); |
| 212 |
m_data_owned = true; |
| 213 |
} |
| 214 |
|
| 215 |
void CPacket::deallocate() |
| 216 |
{ |
| 217 |
if (m_data_owned) |
| 218 |
delete[](char*) m_PacketVector[PV_DATA].data(); |
| 219 |
m_PacketVector[PV_DATA].set(NULL, 0); |
| 220 |
m_data_owned = false; |
| 221 |
} |
| 222 |
|
| 223 |
char* CPacket::release() |
| 224 |
{ |
| 225 |
// When not owned, release returns NULL. |
| 226 |
char* buffer = NULL; |
| 227 |
if (m_data_owned) |
| 228 |
{ |
| 229 |
buffer = getData(); |
| 230 |
m_data_owned = false; |
| 231 |
} |
| 232 |
|
| 233 |
deallocate(); // won't delete because m_data_owned == false |
| 234 |
return buffer; |
| 235 |
} |
| 236 |
|
| 237 |
CPacket::~CPacket() |
| 238 |
{ |
| 239 |
// PV_HEADER is always owned, PV_DATA may use a "borrowed" buffer. |
| 240 |
// Delete the internal buffer only if it was declared as owned. |
| 241 |
deallocate(); |
| 242 |
} |
| 243 |
|
| 244 |
size_t CPacket::getLength() const |
| 245 |
{ |
| 246 |
return m_PacketVector[PV_DATA].size(); |
| 247 |
} |
| 248 |
|
| 249 |
void CPacket::setLength(size_t len) |
| 250 |
{ |
| 251 |
m_PacketVector[PV_DATA].setLength(len); |
| 252 |
} |
| 253 |
|
| 254 |
void CPacket::setLength(size_t len, size_t cap) |
| 255 |
{ |
| 256 |
SRT_ASSERT(len <= cap); |
| 257 |
setLength(len); |
| 258 |
m_zCapacity = cap; |
| 259 |
} |
| 260 |
|
| 261 |
#if ENABLE_HEAVY_LOGGING |
| 262 |
// Debug only |
| 263 |
static std::string FormatNumbers(UDTMessageType pkttype, const int32_t* lparam, void* rparam, const size_t size) |
| 264 |
{ |
| 265 |
// This may be changed over time, so use special interpretation |
| 266 |
// only for certain types, and still display all data, no matter |
| 267 |
// if it is expected to provide anything or not. |
| 268 |
std::ostringstream out; |
| 269 |
|
| 270 |
out << "ARG="; |
| 271 |
if (lparam) |
| 272 |
out << *lparam; |
| 273 |
else |
| 274 |
out << "none"; |
| 275 |
|
| 276 |
if (size == 0) |
| 277 |
{ |
| 278 |
out << " [no data]"; |
| 279 |
return out.str(); |
| 280 |
} |
| 281 |
else if (!rparam) |
| 282 |
{ |
| 283 |
out << " [ {" << size << "} ]"; |
| 284 |
return out.str(); |
| 285 |
} |
| 286 |
|
| 287 |
bool interp_as_seq = (pkttype == UMSG_LOSSREPORT || pkttype == UMSG_DROPREQ); |
| 288 |
bool display_dec = (pkttype == UMSG_ACK || pkttype == UMSG_ACKACK || pkttype == UMSG_DROPREQ); |
| 289 |
|
| 290 |
out << " [ "; |
| 291 |
|
| 292 |
// Will be effective only for hex/oct. |
| 293 |
out << std::showbase; |
| 294 |
|
| 295 |
const size_t size32 = size/4; |
| 296 |
for (size_t i = 0; i < size32; ++i) |
| 297 |
{ |
| 298 |
int32_t val = ((int32_t*)rparam)[i]; |
| 299 |
if (interp_as_seq) |
| 300 |
{ |
| 301 |
if (val & LOSSDATA_SEQNO_RANGE_FIRST) |
| 302 |
out << "<" << (val & (~LOSSDATA_SEQNO_RANGE_FIRST)) << ">"; |
| 303 |
else |
| 304 |
out << val; |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
if (!display_dec) |
| 309 |
{ |
| 310 |
out << std::hex; |
| 311 |
out << val << "/"; |
| 312 |
out << std::dec; |
| 313 |
} |
| 314 |
out << val; |
| 315 |
|
| 316 |
} |
| 317 |
out << " "; |
| 318 |
} |
| 319 |
|
| 320 |
out << "]"; |
| 321 |
return out.str(); |
| 322 |
} |
| 323 |
#endif |
| 324 |
|
| 325 |
void CPacket::pack(UDTMessageType pkttype, const int32_t* lparam, void* rparam, size_t size) |
| 326 |
{ |
| 327 |
// Set (bit-0 = 1) and (bit-1~15 = type) |
| 328 |
setControl(pkttype); |
| 329 |
HLOGC(inlog.Debug, log << "pack: type=" << MessageTypeStr(pkttype) << " " << FormatNumbers(pkttype, lparam, rparam, size)); |
| 330 |
|
| 331 |
// Set additional information and control information field |
| 332 |
switch (pkttype) |
| 333 |
{ |
| 334 |
case UMSG_ACK: // 0010 - Acknowledgement (ACK) |
| 335 |
// ACK packet seq. no. |
| 336 |
if (NULL != lparam) |
| 337 |
m_nHeader[SRT_PH_MSGNO] = *lparam; |
| 338 |
|
| 339 |
// data ACK seq. no. |
| 340 |
// optional: RTT (microsends), RTT variance (microseconds) advertised flow window size (packets), and estimated |
| 341 |
// link capacity (packets per second) |
| 342 |
m_PacketVector[PV_DATA].set(rparam, size); |
| 343 |
|
| 344 |
break; |
| 345 |
|
| 346 |
case UMSG_ACKACK: // 0110 - Acknowledgement of Acknowledgement (ACK-2) |
| 347 |
// ACK packet seq. no. |
| 348 |
m_nHeader[SRT_PH_MSGNO] = *lparam; |
| 349 |
|
| 350 |
// control info field should be none |
| 351 |
// but "writev" does not allow this |
| 352 |
m_PacketVector[PV_DATA].set((void*)&m_extra_pad, 4); |
| 353 |
|
| 354 |
break; |
| 355 |
|
| 356 |
case UMSG_LOSSREPORT: // 0011 - Loss Report (NAK) |
| 357 |
// loss list |
| 358 |
m_PacketVector[PV_DATA].set(rparam, size); |
| 359 |
|
| 360 |
break; |
| 361 |
|
| 362 |
case UMSG_CGWARNING: // 0100 - Congestion Warning |
| 363 |
// control info field should be none |
| 364 |
// but "writev" does not allow this |
| 365 |
m_PacketVector[PV_DATA].set((void*)&m_extra_pad, 4); |
| 366 |
|
| 367 |
break; |
| 368 |
|
| 369 |
case UMSG_KEEPALIVE: // 0001 - Keep-alive |
| 370 |
if (lparam) |
| 371 |
{ |
| 372 |
// XXX EXPERIMENTAL. Pass the 32-bit integer here. |
| 373 |
m_nHeader[SRT_PH_MSGNO] = *lparam; |
| 374 |
} |
| 375 |
// control info field should be none |
| 376 |
// but "writev" does not allow this |
| 377 |
m_PacketVector[PV_DATA].set((void*)&m_extra_pad, 4); |
| 378 |
|
| 379 |
break; |
| 380 |
|
| 381 |
case UMSG_HANDSHAKE: // 0000 - Handshake |
| 382 |
// control info filed is handshake info |
| 383 |
m_PacketVector[PV_DATA].set(rparam, size); |
| 384 |
|
| 385 |
break; |
| 386 |
|
| 387 |
case UMSG_SHUTDOWN: // 0101 - Shutdown |
| 388 |
// control info field should be none |
| 389 |
// but "writev" does not allow this |
| 390 |
m_PacketVector[PV_DATA].set((void*)&m_extra_pad, 4); |
| 391 |
|
| 392 |
break; |
| 393 |
|
| 394 |
case UMSG_DROPREQ: // 0111 - Message Drop Request |
| 395 |
// msg id |
| 396 |
m_nHeader[SRT_PH_MSGNO] = *lparam; |
| 397 |
|
| 398 |
// first seq no, last seq no |
| 399 |
m_PacketVector[PV_DATA].set(rparam, size); |
| 400 |
|
| 401 |
break; |
| 402 |
|
| 403 |
case UMSG_PEERERROR: // 1000 - Error Signal from the Peer Side |
| 404 |
// Error type |
| 405 |
m_nHeader[SRT_PH_MSGNO] = *lparam; |
| 406 |
|
| 407 |
// control info field should be none |
| 408 |
// but "writev" does not allow this |
| 409 |
m_PacketVector[PV_DATA].set((void*)&m_extra_pad, 4); |
| 410 |
|
| 411 |
break; |
| 412 |
|
| 413 |
case UMSG_EXT: // 0x7FFF - Reserved for user defined control packets |
| 414 |
// for extended control packet |
| 415 |
// "lparam" contains the extended type information for bit 16 - 31 |
| 416 |
// "rparam" is the control information |
| 417 |
m_nHeader[SRT_PH_SEQNO] |= *lparam; |
| 418 |
|
| 419 |
if (NULL != rparam) |
| 420 |
{ |
| 421 |
m_PacketVector[PV_DATA].set(rparam, size); |
| 422 |
} |
| 423 |
else |
| 424 |
{ |
| 425 |
m_PacketVector[PV_DATA].set((void*)&m_extra_pad, 4); |
| 426 |
} |
| 427 |
|
| 428 |
break; |
| 429 |
|
| 430 |
default: |
| 431 |
break; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
void CPacket::toNetworkByteOrder() |
| 436 |
{ |
| 437 |
// The payload of data packet should remain in network byte order. |
| 438 |
if (isControl()) |
| 439 |
{ |
| 440 |
HtoNLA((uint32_t*) m_pcData, (const uint32_t*) m_pcData, getLength() / 4); |
| 441 |
} |
| 442 |
|
| 443 |
// Convert packet header independent of packet type. |
| 444 |
uint32_t* p = m_nHeader; |
| 445 |
HtoNLA(p, p, 4); |
| 446 |
} |
| 447 |
|
| 448 |
void CPacket::toHostByteOrder() |
| 449 |
{ |
| 450 |
// Convert packet header independent of packet type. |
| 451 |
uint32_t* p = m_nHeader; |
| 452 |
NtoHLA(p, p, 4); |
| 453 |
|
| 454 |
// The payload of data packet should remain in network byte order. |
| 455 |
if (isControl()) |
| 456 |
{ |
| 457 |
NtoHLA((uint32_t*)m_pcData, (const uint32_t*)m_pcData, getLength() / 4); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
IOVector* CPacket::getPacketVector() |
| 462 |
{ |
| 463 |
return m_PacketVector; |
| 464 |
} |
| 465 |
|
| 466 |
UDTMessageType CPacket::getType() const |
| 467 |
{ |
| 468 |
return UDTMessageType(SEQNO_MSGTYPE::unwrap(m_nHeader[SRT_PH_SEQNO])); |
| 469 |
} |
| 470 |
|
| 471 |
int CPacket::getExtendedType() const |
| 472 |
{ |
| 473 |
return SEQNO_EXTTYPE::unwrap(m_nHeader[SRT_PH_SEQNO]); |
| 474 |
} |
| 475 |
|
| 476 |
int32_t CPacket::getAckSeqNo() const |
| 477 |
{ |
| 478 |
// read additional information field |
| 479 |
// This field is used only in UMSG_ACK and UMSG_ACKACK, |
| 480 |
// so 'getAckSeqNo' symbolically defines the only use of it |
| 481 |
// in case of CONTROL PACKET. |
| 482 |
return m_nHeader[SRT_PH_MSGNO]; |
| 483 |
} |
| 484 |
|
| 485 |
uint16_t CPacket::getControlFlags() const |
| 486 |
{ |
| 487 |
// This returns exactly the "extended type" value, |
| 488 |
// which is not used at all in case when the standard |
| 489 |
// type message is interpreted. This can be used to pass |
| 490 |
// additional special flags. |
| 491 |
return SEQNO_EXTTYPE::unwrap(m_nHeader[SRT_PH_SEQNO]); |
| 492 |
} |
| 493 |
|
| 494 |
PacketBoundary CPacket::getMsgBoundary() const |
| 495 |
{ |
| 496 |
return PacketBoundary(MSGNO_PACKET_BOUNDARY::unwrap(m_nHeader[SRT_PH_MSGNO])); |
| 497 |
} |
| 498 |
|
| 499 |
bool CPacket::getMsgOrderFlag() const |
| 500 |
{ |
| 501 |
return 0 != MSGNO_PACKET_INORDER::unwrap(m_nHeader[SRT_PH_MSGNO]); |
| 502 |
} |
| 503 |
|
| 504 |
int32_t CPacket::getMsgSeq(bool has_rexmit) const |
| 505 |
{ |
| 506 |
if (has_rexmit) |
| 507 |
{ |
| 508 |
return MSGNO_SEQ::unwrap(m_nHeader[SRT_PH_MSGNO]); |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
return MSGNO_SEQ_OLD::unwrap(m_nHeader[SRT_PH_MSGNO]); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
bool CPacket::getRexmitFlag() const |
| 517 |
{ |
| 518 |
return 0 != MSGNO_REXMIT::unwrap(m_nHeader[SRT_PH_MSGNO]); |
| 519 |
} |
| 520 |
|
| 521 |
void CPacket::setRexmitFlag(bool bRexmit) |
| 522 |
{ |
| 523 |
const int32_t clr_msgno = m_nHeader[SRT_PH_MSGNO] & ~MSGNO_REXMIT::mask; |
| 524 |
m_nHeader[SRT_PH_MSGNO] = clr_msgno | MSGNO_REXMIT::wrap(bRexmit? 1 : 0); |
| 525 |
} |
| 526 |
|
| 527 |
EncryptionKeySpec CPacket::getMsgCryptoFlags() const |
| 528 |
{ |
| 529 |
return EncryptionKeySpec(MSGNO_ENCKEYSPEC::unwrap(m_nHeader[SRT_PH_MSGNO])); |
| 530 |
} |
| 531 |
|
| 532 |
// This is required as the encryption/decryption happens in place. |
| 533 |
// This is required to clear off the flags after decryption or set |
| 534 |
// crypto flags after encrypting a packet. |
| 535 |
void CPacket::setMsgCryptoFlags(EncryptionKeySpec spec) |
| 536 |
{ |
| 537 |
int32_t clr_msgno = m_nHeader[SRT_PH_MSGNO] & ~MSGNO_ENCKEYSPEC::mask; |
| 538 |
m_nHeader[SRT_PH_MSGNO] = clr_msgno | EncryptionKeyBits(spec); |
| 539 |
} |
| 540 |
|
| 541 |
uint32_t CPacket::getMsgTimeStamp() const |
| 542 |
{ |
| 543 |
// SRT_DEBUG_TSBPD_WRAP used to enable smaller timestamps for faster testing of how wraparounds are handled |
| 544 |
return (uint32_t)m_nHeader[SRT_PH_TIMESTAMP] & TIMESTAMP_MASK; |
| 545 |
} |
| 546 |
|
| 547 |
CPacket* CPacket::clone() const |
| 548 |
{ |
| 549 |
CPacket* pkt = new CPacket; |
| 550 |
memcpy((pkt->m_nHeader), m_nHeader, HDR_SIZE); |
| 551 |
pkt->allocate(this->getLength()); |
| 552 |
SRT_ASSERT(this->getLength() == pkt->getLength()); |
| 553 |
memcpy((pkt->m_pcData), m_pcData, this->getLength()); |
| 554 |
pkt->m_DestAddr = m_DestAddr; |
| 555 |
|
| 556 |
return pkt; |
| 557 |
} |
| 558 |
|
| 559 |
// Useful for debugging |
| 560 |
std::string PacketMessageFlagStr(uint32_t msgno_field) |
| 561 |
{ |
| 562 |
using namespace std; |
| 563 |
|
| 564 |
stringstream out; |
| 565 |
|
| 566 |
static const char* const boundary[] = {"PB_SUBSEQUENT", "PB_LAST", "PB_FIRST", "PB_SOLO"}; |
| 567 |
static const char* const order[] = {"ORD_RELAXED", "ORD_REQUIRED"}; |
| 568 |
static const char* const crypto[] = {"EK_NOENC", "EK_EVEN", "EK_ODD", "EK*ERROR"}; |
| 569 |
static const char* const rexmit[] = {"SN_ORIGINAL", "SN_REXMIT"}; |
| 570 |
|
| 571 |
out << boundary[MSGNO_PACKET_BOUNDARY::unwrap(msgno_field)] << " "; |
| 572 |
out << order[MSGNO_PACKET_INORDER::unwrap(msgno_field)] << " "; |
| 573 |
out << crypto[MSGNO_ENCKEYSPEC::unwrap(msgno_field)] << " "; |
| 574 |
out << rexmit[MSGNO_REXMIT::unwrap(msgno_field)]; |
| 575 |
|
| 576 |
return out.str(); |
| 577 |
} |
| 578 |
|
| 579 |
inline void SprintSpecialWord(std::ostream& os, int32_t val) |
| 580 |
{ |
| 581 |
if (val & LOSSDATA_SEQNO_RANGE_FIRST) |
| 582 |
os << "<" << (val & (~LOSSDATA_SEQNO_RANGE_FIRST)) << ">"; |
| 583 |
else |
| 584 |
os << val; |
| 585 |
} |
| 586 |
|
| 587 |
#if ENABLE_LOGGING |
| 588 |
std::string CPacket::Info() |
| 589 |
{ |
| 590 |
std::ostringstream os; |
| 591 |
os << "TARGET=@" << id() << " "; |
| 592 |
|
| 593 |
if (isControl()) |
| 594 |
{ |
| 595 |
os << "CONTROL: size=" << getLength() << " type=" << MessageTypeStr(getType(), getExtendedType()); |
| 596 |
|
| 597 |
if (getType() == UMSG_HANDSHAKE) |
| 598 |
{ |
| 599 |
os << " HS: "; |
| 600 |
// For handshake we already have a parsing method |
| 601 |
CHandShake hs; |
| 602 |
hs.load_from(m_pcData, getLength()); |
| 603 |
os << hs.show(); |
| 604 |
} |
| 605 |
else |
| 606 |
{ |
| 607 |
// This is a value that some messages use for some purposes. |
| 608 |
// The "ack seq no" is one of the purposes, used by UMSG_ACK and UMSG_ACKACK. |
| 609 |
// This is simply the SRT_PH_MSGNO field used as a message number in data packets. |
| 610 |
os << " ARG: 0x"; |
| 611 |
os << std::hex << getAckSeqNo() << " "; |
| 612 |
os << std::dec << getAckSeqNo(); |
| 613 |
|
| 614 |
// It would be nice to see the extended packet data, but this |
| 615 |
// requires strictly a message-dependent interpreter. So let's simply |
| 616 |
// display all numbers in the array with the following restrictions: |
| 617 |
// - all data contained in the buffer are considered 32-bit integer |
| 618 |
// - sign flag will be cleared before displaying, with additional mark |
| 619 |
size_t wordlen = getLength() / 4; // drop any remainder if present |
| 620 |
int32_t* array = (int32_t*)m_pcData; |
| 621 |
os << " [ "; |
| 622 |
for (size_t i = 0; i < wordlen; ++i) |
| 623 |
{ |
| 624 |
SprintSpecialWord(os, array[i]); |
| 625 |
os << " "; |
| 626 |
} |
| 627 |
os << "]"; |
| 628 |
} |
| 629 |
} |
| 630 |
else |
| 631 |
{ |
| 632 |
// It's hard to extract the information about peer's supported rexmit flag. |
| 633 |
// This is only a log, nothing crucial, so we can risk displaying incorrect message number. |
| 634 |
// Declaring that the peer supports rexmit flag cuts off the highest bit from |
| 635 |
// the displayed number. |
| 636 |
os << "DATA: size=" << getLength() << " " << BufferStamp(m_pcData, getLength()) << " #" << getMsgSeq(true) |
| 637 |
<< " %" << getSeqNo() << " " << MessageFlagStr(); |
| 638 |
} |
| 639 |
|
| 640 |
return os.str(); |
| 641 |
} |
| 642 |
#endif |
| 643 |
|
| 644 |
} // end namespace srt |