| 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 01/02/2011 |
| 49 |
modified by |
| 50 |
Haivision Systems Inc. |
| 51 |
*****************************************************************************/ |
| 52 |
|
| 53 |
#ifndef INC_SRT_PACKET_H |
| 54 |
#define INC_SRT_PACKET_H |
| 55 |
|
| 56 |
#include "udt.h" |
| 57 |
#include "common.h" |
| 58 |
#include "utilities.h" |
| 59 |
#include "netinet_any.h" |
| 60 |
#include "packetfilter_api.h" |
| 61 |
|
| 62 |
namespace srt |
| 63 |
{ |
| 64 |
|
| 65 |
////////////////////////////////////////////////////////////////////////////// |
| 66 |
// The purpose of the IOVector class is to proide a platform-independet interface |
| 67 |
// to the WSABUF on Windows and iovec on Linux, that can be easily converted |
| 68 |
// to the native structure for use in WSARecvFrom() and recvmsg(...) functions |
| 69 |
class IOVector |
| 70 |
#ifdef _WIN32 |
| 71 |
: public WSABUF |
| 72 |
#else |
| 73 |
: public iovec |
| 74 |
#endif |
| 75 |
{ |
| 76 |
public: |
| 77 |
IOVector() { set(NULL, 0); } |
| 78 |
|
| 79 |
inline void set(void* buffer, size_t length) |
| 80 |
{ |
| 81 |
#ifdef _WIN32 |
| 82 |
len = (ULONG)length; |
| 83 |
buf = (CHAR*)buffer; |
| 84 |
#else |
| 85 |
iov_base = (void*)buffer; |
| 86 |
iov_len = length; |
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
inline char*& dataRef() |
| 91 |
{ |
| 92 |
#ifdef _WIN32 |
| 93 |
return buf; |
| 94 |
#else |
| 95 |
return (char*&)iov_base; |
| 96 |
#endif |
| 97 |
} |
| 98 |
|
| 99 |
inline char* data() |
| 100 |
{ |
| 101 |
#ifdef _WIN32 |
| 102 |
return buf; |
| 103 |
#else |
| 104 |
return (char*)iov_base; |
| 105 |
#endif |
| 106 |
} |
| 107 |
|
| 108 |
inline size_t size() const |
| 109 |
{ |
| 110 |
#ifdef _WIN32 |
| 111 |
return (size_t)len; |
| 112 |
#else |
| 113 |
return iov_len; |
| 114 |
#endif |
| 115 |
} |
| 116 |
|
| 117 |
inline void setLength(size_t length) |
| 118 |
{ |
| 119 |
#ifdef _WIN32 |
| 120 |
len = (ULONG)length; |
| 121 |
#else |
| 122 |
iov_len = length; |
| 123 |
#endif |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
/// To define packets in order in the buffer. This is public due to being used in buffer. |
| 128 |
enum PacketBoundary |
| 129 |
{ |
| 130 |
PB_SUBSEQUENT = 0, // 00: a packet in the middle of a message, neither the first, not the last. |
| 131 |
PB_LAST = 1, // 01: last packet of a message |
| 132 |
PB_FIRST = 2, // 10: first packet of a message |
| 133 |
PB_SOLO = 3, // 11: solo message packet |
| 134 |
}; |
| 135 |
|
| 136 |
// Breakdown of the PM_SEQNO field in the header: |
| 137 |
// C| X X ... X, where: |
| 138 |
typedef Bits<31> SEQNO_CONTROL; |
| 139 |
// 1|T T T T T T T T T T T T T T T|E E...E |
| 140 |
typedef Bits<30, 16> SEQNO_MSGTYPE; |
| 141 |
typedef Bits<15, 0> SEQNO_EXTTYPE; |
| 142 |
// 0|S S ... S |
| 143 |
typedef Bits<30, 0> SEQNO_VALUE; |
| 144 |
|
| 145 |
// This bit cannot be used by SEQNO anyway, so it's additionally used |
| 146 |
// in LOSSREPORT data specification to define that this value is the |
| 147 |
// BEGIN value for a SEQNO range (to distinguish it from a SOLO loss SEQNO value). |
| 148 |
const int32_t LOSSDATA_SEQNO_RANGE_FIRST = SEQNO_CONTROL::mask; |
| 149 |
|
| 150 |
// Just cosmetics for readability. |
| 151 |
const int32_t LOSSDATA_SEQNO_RANGE_LAST = 0, LOSSDATA_SEQNO_SOLO = 0; |
| 152 |
|
| 153 |
inline int32_t CreateControlSeqNo(UDTMessageType type) |
| 154 |
{ |
| 155 |
return SEQNO_CONTROL::mask | SEQNO_MSGTYPE::wrap(uint32_t(type)); |
| 156 |
} |
| 157 |
|
| 158 |
inline int32_t CreateControlExtSeqNo(int exttype) |
| 159 |
{ |
| 160 |
return SEQNO_CONTROL::mask | SEQNO_MSGTYPE::wrap(size_t(UMSG_EXT)) | SEQNO_EXTTYPE::wrap(exttype); |
| 161 |
} |
| 162 |
|
| 163 |
// MSGNO breakdown: B B|O|K K|R|M M M M M M M M M M...M |
| 164 |
typedef Bits<31, 30> MSGNO_PACKET_BOUNDARY; |
| 165 |
typedef Bits<29> MSGNO_PACKET_INORDER; |
| 166 |
typedef Bits<28, 27> MSGNO_ENCKEYSPEC; |
| 167 |
#if 1 // can block rexmit flag |
| 168 |
// New bit breakdown - rexmit flag supported. |
| 169 |
typedef Bits<26> MSGNO_REXMIT; |
| 170 |
typedef Bits<25, 0> MSGNO_SEQ; |
| 171 |
// Old bit breakdown - no rexmit flag |
| 172 |
typedef Bits<26, 0> MSGNO_SEQ_OLD; |
| 173 |
// This symbol is for older SRT version, where the peer does not support the MSGNO_REXMIT flag. |
| 174 |
// The message should be extracted as PMASK_MSGNO_SEQ, if REXMIT is supported, and PMASK_MSGNO_SEQ_OLD otherwise. |
| 175 |
|
| 176 |
const uint32_t PACKET_SND_NORMAL = 0, PACKET_SND_REXMIT = MSGNO_REXMIT::mask; |
| 177 |
const int MSGNO_SEQ_MAX = MSGNO_SEQ::mask; |
| 178 |
|
| 179 |
#else |
| 180 |
// Old bit breakdown - no rexmit flag |
| 181 |
typedef Bits<26, 0> MSGNO_SEQ; |
| 182 |
#endif |
| 183 |
|
| 184 |
typedef RollNumber<MSGNO_SEQ::size - 1, 1> MsgNo; |
| 185 |
|
| 186 |
// constexpr in C++11 ! |
| 187 |
inline int32_t PacketBoundaryBits(PacketBoundary o) |
| 188 |
{ |
| 189 |
return MSGNO_PACKET_BOUNDARY::wrap(int32_t(o)); |
| 190 |
} |
| 191 |
|
| 192 |
enum EncryptionKeySpec |
| 193 |
{ |
| 194 |
EK_NOENC = 0, |
| 195 |
EK_EVEN = 1, |
| 196 |
EK_ODD = 2 |
| 197 |
}; |
| 198 |
|
| 199 |
enum EncryptionStatus |
| 200 |
{ |
| 201 |
ENCS_CLEAR = 0, |
| 202 |
ENCS_FAILED = -1, |
| 203 |
ENCS_NOTSUP = -2 |
| 204 |
}; |
| 205 |
|
| 206 |
const int32_t PMASK_MSGNO_ENCKEYSPEC = MSGNO_ENCKEYSPEC::mask; |
| 207 |
inline int32_t EncryptionKeyBits(EncryptionKeySpec f) |
| 208 |
{ |
| 209 |
return MSGNO_ENCKEYSPEC::wrap(int32_t(f)); |
| 210 |
} |
| 211 |
inline EncryptionKeySpec GetEncryptionKeySpec(int32_t msgno) |
| 212 |
{ |
| 213 |
return EncryptionKeySpec(MSGNO_ENCKEYSPEC::unwrap(msgno)); |
| 214 |
} |
| 215 |
|
| 216 |
const int32_t PUMASK_SEQNO_PROBE = 0xF; |
| 217 |
|
| 218 |
std::string PacketMessageFlagStr(uint32_t msgno_field); |
| 219 |
|
| 220 |
class CPacket |
| 221 |
{ |
| 222 |
friend class CChannel; |
| 223 |
friend class CSndQueue; |
| 224 |
friend class CRcvQueue; |
| 225 |
|
| 226 |
public: |
| 227 |
CPacket(); |
| 228 |
~CPacket(); |
| 229 |
|
| 230 |
void allocate(size_t size); |
| 231 |
void deallocate(); |
| 232 |
|
| 233 |
/// Get the payload or the control information field length. |
| 234 |
/// @return the payload or the control information field length. |
| 235 |
size_t getLength() const; |
| 236 |
|
| 237 |
/// Set the payload or the control information field length. |
| 238 |
/// @param len [in] the payload or the control information field length. |
| 239 |
void setLength(size_t len); |
| 240 |
|
| 241 |
/// Set the payload or the control information field length. |
| 242 |
/// @param len [in] the payload or the control information field length. |
| 243 |
/// @param cap [in] capacity (if known). |
| 244 |
void setLength(size_t len, size_t cap); |
| 245 |
|
| 246 |
/// Pack a Control packet. |
| 247 |
/// @param pkttype [in] packet type filed. |
| 248 |
/// @param lparam [in] pointer to the first data structure, explained by the packet type. |
| 249 |
/// @param rparam [in] pointer to the second data structure, explained by the packet type. |
| 250 |
/// @param size [in] size of rparam, in number of bytes; |
| 251 |
void pack(UDTMessageType pkttype, const int32_t* lparam = NULL, void* rparam = NULL, size_t size = 0); |
| 252 |
|
| 253 |
/// Read the packet vector. |
| 254 |
/// @return Pointer to the packet vector. |
| 255 |
IOVector* getPacketVector(); |
| 256 |
|
| 257 |
uint32_t* getHeader() { return m_nHeader; } |
| 258 |
|
| 259 |
/// Read the packet type. |
| 260 |
/// @return packet type filed (000 ~ 111). |
| 261 |
UDTMessageType getType() const; |
| 262 |
|
| 263 |
bool isControl(UDTMessageType type) const { return isControl() && type == getType(); } |
| 264 |
|
| 265 |
bool isControl() const { return 0 != SEQNO_CONTROL::unwrap(m_nHeader[SRT_PH_SEQNO]); } |
| 266 |
|
| 267 |
void setControl(UDTMessageType type) { m_nHeader[SRT_PH_SEQNO] = SEQNO_CONTROL::mask | SEQNO_MSGTYPE::wrap(type); } |
| 268 |
|
| 269 |
/// Read the extended packet type. |
| 270 |
/// @return extended packet type filed (0x000 ~ 0xFFF). |
| 271 |
int getExtendedType() const; |
| 272 |
|
| 273 |
/// Read the ACK-2 seq. no. |
| 274 |
/// @return packet header field (bit 16~31). |
| 275 |
int32_t getAckSeqNo() const; |
| 276 |
|
| 277 |
uint16_t getControlFlags() const; |
| 278 |
|
| 279 |
// Note: this will return a "singular" value, if the packet |
| 280 |
// contains the control message |
| 281 |
int32_t getSeqNo() const { return m_nHeader[SRT_PH_SEQNO]; } |
| 282 |
|
| 283 |
/// Read the message boundary flag bit. |
| 284 |
/// @return packet header field [1] (bit 0~1). |
| 285 |
PacketBoundary getMsgBoundary() const; |
| 286 |
|
| 287 |
/// Read the message inorder delivery flag bit. |
| 288 |
/// @return packet header field [1] (bit 2). |
| 289 |
bool getMsgOrderFlag() const; |
| 290 |
|
| 291 |
/// Read the rexmit flag (true if the packet was sent due to retransmission). |
| 292 |
/// If the peer does not support retransmission flag, the current agent cannot use it as well |
| 293 |
/// (because the peer will understand this bit as a part of MSGNO field). |
| 294 |
bool getRexmitFlag() const; |
| 295 |
|
| 296 |
void setRexmitFlag(bool bRexmit); |
| 297 |
|
| 298 |
/// Read the message sequence number. |
| 299 |
/// @return packet header field [1] |
| 300 |
int32_t getMsgSeq(bool has_rexmit = true) const; |
| 301 |
|
| 302 |
/// Read the message crypto key bits. |
| 303 |
/// @return packet header field [1] (bit 3~4). |
| 304 |
EncryptionKeySpec getMsgCryptoFlags() const; |
| 305 |
|
| 306 |
void setMsgCryptoFlags(EncryptionKeySpec spec); |
| 307 |
|
| 308 |
/// Read the message time stamp. |
| 309 |
/// @return packet header field [2] (bit 0~31, bit 0-26 if SRT_DEBUG_TSBPD_WRAP). |
| 310 |
uint32_t getMsgTimeStamp() const; |
| 311 |
|
| 312 |
sockaddr_any udpDestAddr() const { return m_DestAddr; } |
| 313 |
|
| 314 |
#ifdef SRT_DEBUG_TSBPD_WRAP // Receiver |
| 315 |
static const uint32_t MAX_TIMESTAMP = 0x07FFFFFF; // 27 bit fast wraparound for tests (~2m15s) |
| 316 |
#else |
| 317 |
static const uint32_t MAX_TIMESTAMP = 0xFFFFFFFF; // Full 32 bit (01h11m35s) |
| 318 |
#endif |
| 319 |
|
| 320 |
protected: |
| 321 |
static const uint32_t TIMESTAMP_MASK = MAX_TIMESTAMP; // this value to be also used as a mask |
| 322 |
public: |
| 323 |
/// Clone this packet. |
| 324 |
/// @return Pointer to the new packet. |
| 325 |
CPacket* clone() const; |
| 326 |
|
| 327 |
enum PacketVectorFields |
| 328 |
{ |
| 329 |
PV_HEADER = 0, |
| 330 |
PV_DATA = 1, |
| 331 |
|
| 332 |
PV_SIZE = 2 |
| 333 |
}; |
| 334 |
|
| 335 |
public: |
| 336 |
/// @brief Convert the packet inline to a network byte order (Little-endian). |
| 337 |
void toNetworkByteOrder(); |
| 338 |
/// @brief Convert the packet inline to a host byte order. |
| 339 |
void toHostByteOrder(); |
| 340 |
|
| 341 |
protected: |
| 342 |
// DynamicStruct is the same as array of given type and size, just it |
| 343 |
// enforces that you index it using a symbol from symbolic enum type, not by a bare integer. |
| 344 |
typedef DynamicStruct<uint32_t, SRT_PH_E_SIZE, SrtPktHeaderFields> HEADER_TYPE; |
| 345 |
HEADER_TYPE m_nHeader; //< The 128-bit header field |
| 346 |
|
| 347 |
IOVector m_PacketVector[PV_SIZE]; //< The two-dimensional vector of an SRT packet [header, data] |
| 348 |
|
| 349 |
int32_t m_extra_pad; |
| 350 |
bool m_data_owned; |
| 351 |
sockaddr_any m_DestAddr; |
| 352 |
size_t m_zCapacity; |
| 353 |
|
| 354 |
protected: |
| 355 |
CPacket& operator=(const CPacket&); |
| 356 |
CPacket(const CPacket&); |
| 357 |
|
| 358 |
public: |
| 359 |
char*& m_pcData; // alias: payload (data packet) / control information fields (control packet) |
| 360 |
|
| 361 |
SRTU_PROPERTY_RO(SRTSOCKET, id, SRTSOCKET(m_nHeader[SRT_PH_ID])); |
| 362 |
SRTU_PROPERTY_WO_ARG(SRTSOCKET, id, m_nHeader[SRT_PH_ID] = int32_t(arg)); |
| 363 |
|
| 364 |
SRTU_PROPERTY_RW(int32_t, seqno, m_nHeader[SRT_PH_SEQNO]); |
| 365 |
SRTU_PROPERTY_RW(int32_t, msgflags, m_nHeader[SRT_PH_MSGNO]); |
| 366 |
SRTU_PROPERTY_RW(int32_t, timestamp, m_nHeader[SRT_PH_TIMESTAMP]); |
| 367 |
|
| 368 |
// Experimental: sometimes these references don't work! |
| 369 |
char* getData(); |
| 370 |
char* release(); |
| 371 |
|
| 372 |
static const size_t HDR_SIZE = sizeof(HEADER_TYPE); // packet header size = SRT_PH_E_SIZE * sizeof(uint32_t) |
| 373 |
|
| 374 |
// Can also be calculated as: sizeof(struct ether_header) + sizeof(struct ip) + sizeof(struct udphdr). |
| 375 |
static const size_t UDP_HDR_SIZE = 28; // 20 bytes IPv4 + 8 bytes of UDP { u16 sport, dport, len, csum }. |
| 376 |
|
| 377 |
static const size_t SRT_DATA_HDR_SIZE = UDP_HDR_SIZE + HDR_SIZE; |
| 378 |
|
| 379 |
// Maximum transmission unit size. 1500 in case of Ethernet II (RFC 1191). |
| 380 |
static const size_t ETH_MAX_MTU_SIZE = 1500; |
| 381 |
|
| 382 |
// Maximum payload size of an SRT packet. |
| 383 |
static const size_t SRT_MAX_PAYLOAD_SIZE = ETH_MAX_MTU_SIZE - SRT_DATA_HDR_SIZE; |
| 384 |
|
| 385 |
// Packet interface |
| 386 |
char* data() { return m_pcData; } |
| 387 |
const char* data() const { return m_pcData; } |
| 388 |
size_t size() const { return getLength(); } |
| 389 |
size_t capacity() const { return m_zCapacity; } |
| 390 |
void setCapacity(size_t cap) { m_zCapacity = cap; } |
| 391 |
uint32_t header(SrtPktHeaderFields field) const { return m_nHeader[field]; } |
| 392 |
|
| 393 |
#if ENABLE_LOGGING |
| 394 |
std::string MessageFlagStr() { return PacketMessageFlagStr(m_nHeader[SRT_PH_MSGNO]); } |
| 395 |
std::string Info(); |
| 396 |
#else |
| 397 |
std::string MessageFlagStr() { return std::string(); } |
| 398 |
std::string Info() { return std::string(); } |
| 399 |
#endif |
| 400 |
}; |
| 401 |
|
| 402 |
} // namespace srt |
| 403 |
|
| 404 |
#endif |