ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rtcppkt.cpp
Go to the documentation of this file.
1 // Copyright (C) 2001-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 General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // ccRTP. If you copy code from other releases into a copy of GNU
28 // ccRTP, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU ccRTP, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
44 #include "private.h"
45 #include <ccrtp/rtcppkt.h>
46 
47 NAMESPACE_COMMONCPP
48 
49 #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
50 const uint16 RTCPCompoundHandler::RTCP_VALID_MASK = (0xc000 | 0x2000 | 0xfe);
51 const uint16 RTCPCompoundHandler::RTCP_VALID_VALUE = ((CCRTP_VERSION << 14) | RTCPPacket::tSR);
52 #else
53 const uint16 RTCPCompoundHandler::RTCP_VALID_MASK = (0x00c0 | 0x0020 | 0xfe00);
54 const uint16 RTCPCompoundHandler::RTCP_VALID_VALUE = ((CCRTP_VERSION << 6) | (RTCPPacket::tSR << 8));
55 #endif
56 
57 timeval
58 NTP2Timeval(uint32 msw, uint32 lsw)
59 {
60  struct timeval t;
61  t.tv_sec = msw - NTP_EPOCH_OFFSET;
62  t.tv_usec = (uint32)((((double)lsw) * 1000000.0) / ((uint32)(~0)));
63  return t;
64 }
65 
66 uint32
68 {
69  const uint32 f = 65536;
70  uint32 result = t.tv_sec * f;
71  result += (t.tv_usec << 12) / 125000 * 2; // * (4096 / 125000) * 2
72  return result;
73 }
74 
75 timeval
77 {
78  timeval result;
79  result.tv_sec = to % 1000000;
80  result.tv_usec = to / 1000000;
81  return result;
82 }
83 
85 rtcpSendBuffer(new unsigned char[mtu]()), rtcpRecvBuffer(new unsigned char[mtu]()),
86 pathMTU(mtu)
87 {
88 }
89 
91 {
92 #ifdef CCXX_EXCEPTIONS
93  try {
94 #endif
95  delete [] rtcpRecvBuffer;
96 #ifdef CCXX_EXCEPTIONS
97  } catch (...) {}
98  try {
99 #endif
100  delete [] rtcpSendBuffer;
101 #ifdef CCXX_EXCEPTIONS
102  } catch (...) {}
103 #endif
104 }
105 
106 bool
108 {
109  // Note that the first packet in the compount --in order to
110  // detect possibly misaddressed RTP packets-- is more
111  // thoroughly checked than the following. This mask checks the
112  // first packet's version, padding (must be zero) and type
113  // (must be SR or RR).
114  if ( (*(reinterpret_cast<uint16*>(rtcpRecvBuffer))
115  & RTCP_VALID_MASK)
116  != RTCP_VALID_VALUE ) {
117  return false;
118  }
119 
120  // this checks that every packet in the compound is tagged
121  // with version == CCRTP_VERSION, and the length of the compound
122  // packet matches the addition of the packets lenghts
123  uint32 pointer = 0;
124  RTCPPacket* pkt;
125  do {
126  pkt = reinterpret_cast<RTCPPacket*>
127  (rtcpRecvBuffer + pointer);
128  pointer += (ntohs(pkt->fh.length)+1) << 2;
129  } while ( (pointer < len) && (CCRTP_VERSION == pkt->fh.version) );
130 
131  if ( pointer != len )
132  return false;
133 
134  return true;
135 }
136 
137 END_NAMESPACE
138 
unsigned char * rtcpSendBuffer
Definition: rtcppkt.h:352
NAMESPACE_COMMONCPP const uint32 NTP_EPOCH_OFFSET
Definition: private.h:142
const uint8 CCRTP_VERSION
RTP protocol version supported.
Definition: base.h:64
bool checkCompoundRTCPHeader(size_t len)
Perform RTCP compound packet header validity check as specified in draft-ietv-avt-rtp-new.
Definition: rtcppkt.cpp:107
uint32 microtimeout_t
Time interval expressed in microseconds.
Definition: base.h:67
static const uint16 RTCP_VALID_VALUE
Definition: rtcppkt.h:364
RTCP packets handling.
Declaration of ccRTP internal stuff.
Struct representing general RTCP packet headers as they are sent through the network.
Definition: rtcppkt.h:277
uint32 timevalIntervalTo65536(timeval &t)
Convert a time interval, expressed as a timeval, into a 32-bit time interval expressed in units of 1/...
Definition: rtcppkt.cpp:67
unsigned char * rtcpRecvBuffer
Definition: rtcppkt.h:355
uint16 length
number of 32-bit words in the packet (minus one).
Definition: rtcppkt.h:264
RTCPCompoundHandler(uint16 mtu=defaultPathMTU)
Definition: rtcppkt.cpp:84
timeval microtimeout2Timeval(microtimeout_t to)
Convert a time interval, expressed as a microtimeout_t (number of microseconds), into a timeval value...
Definition: rtcppkt.cpp:76
timeval NTP2Timeval(uint32 msw, uint32 lsw)
Convert a NTP timestamp, expressed as two 32-bit long words, into a timeval value.
Definition: rtcppkt.cpp:58
static const uint16 RTCP_VALID_MASK
Definition: rtcppkt.h:363
unsigned char version
Version, currently 2.
Definition: rtcppkt.h:261
RTCPFixedHeader fh
Fixed RTCP header.
Definition: rtcppkt.h:310