ccRTP
rtppkt.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2015 Federico Montesino Pouzols <fedemp@altern.org>.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with GNU ccRTP. If not, see <http://www.gnu.org/licenses/>.
15 //
16 // As a special exception, you may use this file as part of a free software
17 // library without restriction. Specifically, if other files instantiate
18 // templates or use macros or inline functions from this file, or you compile
19 // this file and link it with other files to produce an executable, this
20 // file does not by itself cause the resulting executable to be covered by
21 // the GNU General Public License. This exception does not however
22 // invalidate any other reasons why the executable file might be covered by
23 // the GNU General Public License.
24 //
25 // This exception applies only to the code released under the name GNU
26 // ccRTP. If you copy code from other releases into a copy of GNU
27 // ccRTP, as the General Public License permits, the exception does
28 // not apply to the code that you add in this way. To avoid misleading
29 // anyone as to the status of such modified files, you must delete
30 // this exception notice from them.
31 //
32 // If you write modifications of your own for GNU ccRTP, it is your choice
33 // whether to permit this exception to apply to your modifications.
34 // If you do not wish that, delete this exception notice.
35 //
36 
37 #ifndef CCXX_RTP_RTPPKT_H_
38 #define CCXX_RTP_RTPPKT_H_
39 
40 #include <ccrtp/base.h>
41 #include <ccrtp/formats.h>
42 #include <ccrtp/CryptoContext.h>
43 
44 NAMESPACE_COMMONCPP
45 
70 class CryptoContext;
71 
72 class __EXPORT RTPPacket
73 {
74 private:
75  struct RTPFixedHeader;
76  struct RTPHeaderExt;
77 
78 public:
91  RTPPacket(const unsigned char* const block, size_t len,
92  bool duplicate = false);
93 
105  RTPPacket(size_t hdrlen, size_t plen, uint8 paddinglen, CryptoContext* pcc= NULL);
106 
113  inline uint32
114  getHeaderSize() const
115  { return hdrSize; }
116 
120  inline const uint8* const
121  getPayload() const
122  { return (uint8*)(buffer + getHeaderSize()); }
123 
127  inline uint32
128  getPayloadSize() const
129  { return payloadSize; }
130 
134  inline PayloadType
135  getPayloadType() const
136  { return static_cast<PayloadType>(getHeader()->payload); }
137 
141  inline uint16
142  getSeqNum() const
143  { return cachedSeqNum; }
144 
148  inline uint32
149  getTimestamp() const
150  { return cachedTimestamp; }
151 
155  inline uint8
156  getProtocolVersion() const
157  { return getHeader()->version; }
158 
163  inline bool
164  isPadded() const
165  { return getHeader()->padding; }
166 
173  inline uint8
174  getPaddingSize() const
175  { return buffer[total - 1]; }
176 
183  inline bool
184  isMarked() const
185  { return getHeader()->marker; }
186 
192  inline bool
193  isExtended() const
194  { return getHeader()->extension; }
195 
200  inline uint16
201  getCSRCsCount() const
202  { return getHeader()->cc; }
203 
211  inline const uint32*
212  getCSRCs() const
213  { return static_cast<const uint32*>(&(getHeader()->sources[1])); }
214 
227  inline uint16
228  getHdrExtUndefined() const
229  { return (isExtended()? getHeaderExt()->undefined : 0); }
230 
242  inline uint32
243  getHdrExtSize() const
244  { return (isExtended()?
245  (static_cast<uint32>(ntohs(getHeaderExt()->length)) << 2) :
246  0); }
247 
254  inline const unsigned char*
255  getHdrExtContent() const
256  { return (isExtended() ?
257  (reinterpret_cast<const unsigned char*>(getHeaderExt()) +
258  sizeof(RTPHeaderExt)) :
259  NULL); }
260 
267  inline const unsigned char* const
268  getRawPacket() const
269  { return buffer; }
270 
277  inline uint32
278  getRawPacketSize() const
279  { return total; }
280 
281  inline uint32
282  getRawPacketSizeSrtp() const
283  { return total + srtpLength; }
284 
285  inline size_t
286  getSizeOfFixedHeader() const
287  { return sizeof(RTPFixedHeader); }
288 
300  void reComputePayLength(bool padding);
301 
302 protected:
306  inline virtual ~RTPPacket()
307  { endPacket(); }
308 
312  void
313  endPacket();
314 
320  inline RTPFixedHeader*
321  getHeader() const
322  { return reinterpret_cast<RTPFixedHeader*>(buffer); }
323 
324  inline void
325  setExtension(bool e)
326  { getHeader()->extension = e; }
327 
335  inline const RTPHeaderExt*
336  getHeaderExt() const
337  {
338  uint32 fixsize = sizeof(RTPFixedHeader) + (getHeader()->cc << 2);
339  return (reinterpret_cast<RTPHeaderExt*>(buffer + fixsize));
340  }
341 
347  inline uint32
348  getRawTimestamp() const
349  { return ntohl(getHeader()->timestamp); }
350 
351  inline void
352  setbuffer(const void* src, size_t len, size_t pos)
353  { memcpy(buffer + pos,src,len); }
354 
356  uint16 cachedSeqNum;
359 
367 
373  int32 srtpLength;
374 
376  uint32 total;
377 
379  uint32 payloadSize;
380 
381 private:
383  unsigned char* buffer;
385  uint32 hdrSize;
387  bool duplicated;
388 
389 #ifdef CCXX_PACKED
390 #pragma pack(1)
391 #endif
392 
402  struct RTPFixedHeader
403  {
404 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
405 
406  unsigned char version:2;
407  unsigned char padding:1;
408  unsigned char extension:1;
409  unsigned char cc:4;
410  unsigned char marker:1;
411  unsigned char payload:7;
412 #else
413 
414  unsigned char cc:4;
415  unsigned char extension:1;
416  unsigned char padding:1;
417  unsigned char version:2;
418  unsigned char payload:7;
419  unsigned char marker:1;
420 #endif
421  uint16 sequence;
422  uint32 timestamp;
423  uint32 sources[1];
424  };
425 
434 public:
436  {
437 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
438  uint8 event : 8;
439  bool ebit : 1;
440  bool rbit : 1;
441  uint8 vol : 6;
442  uint16 duration : 16;
443 #else
444  uint8 event : 8;
445  uint8 vol : 6;
446  bool rbit : 1;
447  bool ebit : 1;
448  uint16 duration : 16;
449 #endif
450  };
451 
452 private:
460  struct RTPHeaderExt
461  {
462  uint16 undefined;
463  uint16 length;
464  };
465 #ifdef CCXX_PACKED
466 #pragma pack()
467 #endif
468 
469  /* definitions for access to most common 2833 fields... */
470 
471 public:
477  inline struct RFC2833Payload *getRaw2833Payload(void)
478  {return (struct RFC2833Payload *)getPayload();}
479 
485  inline uint16 get2833Duration(void)
486  {return ntohs(getRaw2833Payload()->duration);}
487 
493  inline void set2833Duration(uint16 timestamp)
494  {getRaw2833Payload()->duration = htons(timestamp);}
495 };
496 
507 class __EXPORT OutgoingRTPPkt : public RTPPacket
508 {
509 public:
536  OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
537  const unsigned char* const hdrext, uint32 hdrextlen,
538  const unsigned char* const data, size_t datalen,
539  uint8 paddinglen= 0, CryptoContext* pcc= NULL);
540 
561  OutgoingRTPPkt(const uint32* const csrcs, uint16 numcsrc,
562  const unsigned char* const data, size_t datalen,
563  uint8 paddinglen= 0, CryptoContext* pcc= NULL);
564 
581  OutgoingRTPPkt(const unsigned char* const data, size_t datalen,
582  uint8 paddinglen= 0, CryptoContext* pcc= NULL);
583 
585  { }
586 
590  inline void
591  setPayloadType(PayloadType pt)
592  { getHeader()->payload = pt; }
593 
599  inline void
600  setSeqNum(uint16 seq)
601  {
602  cachedSeqNum = seq;
603  getHeader()->sequence = htons(seq);
604  }
605 
609  inline void
610  setTimestamp(uint32 pts)
611  {
612  cachedTimestamp = pts;
613  getHeader()->timestamp = htonl(pts);
614  }
615 
622  inline void
623  setSSRC(uint32 ssrc) const
624  { getHeader()->sources[0] = htonl(ssrc); }
625 
633  inline void
634  setSSRCNetwork(uint32 ssrc) const
635  { getHeader()->sources[0] = ssrc; }
636 
644  inline void
645  setMarker(bool mark)
646  { getHeader()->marker = mark; }
647 
654  void protect(uint32 ssrc, CryptoContext* pcc);
655 
659  inline bool
660  operator==(const OutgoingRTPPkt &p) const
661  { return ( this->getSeqNum() == p.getSeqNum() ); }
662 
666  inline bool
667  operator!=(const OutgoingRTPPkt &p) const
668  { return ( this->getSeqNum() != p.getSeqNum() ); }
669 
670 private:
675  OutgoingRTPPkt(const OutgoingRTPPkt &o);
676 
682  operator=(const OutgoingRTPPkt &o);
683 
688  void setCSRCArray(const uint32* const csrcs, uint16 numcsrc);
689 
690 };
691 
704 class __EXPORT IncomingRTPPkt : public RTPPacket
705 {
706 public:
719  IncomingRTPPkt(const unsigned char* block, size_t len);
720 
722  { }
723 
729  inline bool
730  isHeaderValid()
731  { return headerValid; }
732 
739  inline uint32
740  getSSRC() const
741  { return cachedSSRC; }
742 
753  int32
754  unprotect(CryptoContext* pcc);
755 
760  inline bool
761  operator==(const IncomingRTPPkt &p) const
762  { return ( (this->getSeqNum() == p.getSeqNum()) &&
763  (this->getSSRC() == p.getSSRC()) ); }
764 
769  inline bool
770  operator!=(const IncomingRTPPkt &p) const
771  { return !( *this == p ); }
772 
773 private:
778  IncomingRTPPkt(const IncomingRTPPkt &ip);
779 
785  operator=(const IncomingRTPPkt &ip);
786 
788  bool headerValid;
790  uint32 cachedSSRC;
791  // Masks for RTP header validation: types matching RTCP SR or
792  // RR must be rejected to avoid accepting misaddressed RTCP
793  // packets.
794  static const uint16 RTP_INVALID_PT_MASK;
795  static const uint16 RTP_INVALID_PT_VALUE;
796 };
797  // rtppacket
799 
800 END_NAMESPACE
801 
802 #endif // ndef CCXX_RTP_RTPPKT_H_
803