packet.cpp 21 KB

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