blob: 0e0e878c1f9430a10c2c50c2b886645d62430c23 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 2001,2002,2007 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
38/**
39 * @file rtcppkt.cpp
40 *
41 * @short RTCPCompoundHandler class implementation.
42 **/
43
44#include "private.h"
45#include <ccrtp/rtcppkt.h>
46
47#ifdef CCXX_NAMESPACES
48namespace ost {
49#endif
50
51#if __BYTE_ORDER == __BIG_ENDIAN
52const uint16 RTCPCompoundHandler::RTCP_VALID_MASK = (0xc000 | 0x2000 | 0xfe);
53const uint16 RTCPCompoundHandler::RTCP_VALID_VALUE = ((CCRTP_VERSION << 14) | RTCPPacket::tSR);
54#else
55const uint16 RTCPCompoundHandler::RTCP_VALID_MASK = (0x00c0 | 0x0020 | 0xfe00);
56const uint16 RTCPCompoundHandler::RTCP_VALID_VALUE = ((CCRTP_VERSION << 6) | (RTCPPacket::tSR << 8));
57#endif
58
59timeval
60NTP2Timeval(uint32 msw, uint32 lsw)
61{
62 struct timeval t;
63 t.tv_sec = msw - NTP_EPOCH_OFFSET;
64 t.tv_usec = (uint32)((((double)lsw) * 1000000.0) / ((uint32)(~0)));
65 return t;
66}
67
68uint32
69timevalIntervalTo65536(timeval& t)
70{
71 const uint32 f = 65536;
72 uint32 result = t.tv_sec * f;
73 result += (t.tv_usec << 12) / 125000 * 2; // * (4096 / 125000) * 2
74 return result;
75}
76
77timeval
78microtimeout2Timeval(microtimeout_t to)
79{
80 timeval result;
81 result.tv_sec = to % 1000000;
82 result.tv_usec = to / 1000000;
83 return result;
84}
85
86RTCPCompoundHandler::RTCPCompoundHandler(uint16 mtu) :
87rtcpSendBuffer(new unsigned char[mtu]), rtcpRecvBuffer(new unsigned char[mtu]),
88pathMTU(mtu)
89{
90}
91
92RTCPCompoundHandler::~RTCPCompoundHandler()
93{
94#ifdef CCXX_EXCEPTIONS
95 try {
96#endif
97 delete [] rtcpRecvBuffer;
98#ifdef CCXX_EXCEPTIONS
99 } catch (...) {}
100 try {
101#endif
102 delete [] rtcpSendBuffer;
103#ifdef CCXX_EXCEPTIONS
104 } catch (...) {}
105#endif
106}
107
108bool
109RTCPCompoundHandler::checkCompoundRTCPHeader(size_t len)
110{
111 // Note that the first packet in the compount --in order to
112 // detect possibly misaddressed RTP packets-- is more
113 // thoroughly checked than the following. This mask checks the
114 // first packet's version, padding (must be zero) and type
115 // (must be SR or RR).
116 if ( (*(reinterpret_cast<uint16*>(rtcpRecvBuffer))
117 & RTCP_VALID_MASK)
118 != RTCP_VALID_VALUE ) {
119 return false;
120 }
121
122 // this checks that every packet in the compound is tagged
123 // with version == CCRTP_VERSION, and the length of the compound
124 // packet matches the addition of the packets lenghts
125 uint32 pointer = 0;
126 RTCPPacket* pkt;
127 do {
128 pkt = reinterpret_cast<RTCPPacket*>
129 (rtcpRecvBuffer + pointer);
130 pointer += (ntohs(pkt->fh.length)+1) << 2;
131 } while ( (pointer < len) && (CCRTP_VERSION == pkt->fh.version) );
132
133 if ( pointer != len )
134 return false;
135
136 return true;
137}
138
139#ifdef CCXX_NAMESPACES
140}
141#endif
142
143/** EMACS **
144 * Local variables:
145 * mode: c++
146 * c-basic-offset: 4
147 * End:
148 */