Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

rtcppkt.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2001,2002,2007 Federico Montesino Pouzols <fedemp@altern.org>.
00002 //  
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 // 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 // 
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software 
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 // 
00017 // As a special exception, you may use this file as part of a free software
00018 // library without restriction.  Specifically, if other files instantiate
00019 // templates or use macros or inline functions from this file, or you compile
00020 // this file and link it with other files to produce an executable, this
00021 // file does not by itself cause the resulting executable to be covered by
00022 // the GNU General Public License.  This exception does not however    
00023 // invalidate any other reasons why the executable file might be covered by
00024 // the GNU General Public License.    
00025 //
00026 // This exception applies only to the code released under the name GNU
00027 // ccRTP.  If you copy code from other releases into a copy of GNU
00028 // ccRTP, as the General Public License permits, the exception does
00029 // not apply to the code that you add in this way.  To avoid misleading
00030 // anyone as to the status of such modified files, you must delete
00031 // this exception notice from them.
00032 //
00033 // If you write modifications of your own for GNU ccRTP, it is your choice
00034 // whether to permit this exception to apply to your modifications.
00035 // If you do not wish that, delete this exception notice.
00036 //
00037 
00044 #include "private.h"
00045 #include <ccrtp/rtcppkt.h>
00046 
00047 #ifdef  CCXX_NAMESPACES
00048 namespace ost {
00049 #endif
00050 
00051 #if __BYTE_ORDER == __BIG_ENDIAN
00052 const uint16 RTCPCompoundHandler::RTCP_VALID_MASK = (0xc000 | 0x2000  | 0xfe);
00053 const uint16 RTCPCompoundHandler::RTCP_VALID_VALUE = ((CCRTP_VERSION << 14) | RTCPPacket::tSR);
00054 #else
00055 const uint16 RTCPCompoundHandler::RTCP_VALID_MASK = (0x00c0 | 0x0020 | 0xfe00);
00056 const uint16 RTCPCompoundHandler::RTCP_VALID_VALUE = ((CCRTP_VERSION << 6) | (RTCPPacket::tSR << 8));
00057 #endif
00058 
00059 timeval
00060 NTP2Timeval(uint32 msw, uint32 lsw)
00061 {
00062         struct timeval t; 
00063         t.tv_sec = msw - NTP_EPOCH_OFFSET;
00064         t.tv_usec = (uint32)((((double)lsw) * 1000000.0) / ((uint32)(~0)));
00065         return t;
00066 }
00067 
00068 uint32
00069 timevalIntervalTo65536(timeval& t)
00070 {
00071         const uint32 f = 65536;
00072         uint32 result = t.tv_sec * f;
00073         result += (t.tv_usec << 12) / 125000 * 2; // * (4096 / 125000) * 2
00074         return result;
00075 }
00076 
00077 timeval
00078 microtimeout2Timeval(microtimeout_t to)
00079 {
00080         timeval result;
00081         result.tv_sec = to % 1000000;
00082         result.tv_usec = to / 1000000;
00083         return result;
00084 }
00085 
00086 RTCPCompoundHandler::RTCPCompoundHandler(uint16 mtu) :
00087         rtcpSendBuffer(new unsigned char[mtu]),
00088         rtcpRecvBuffer(new unsigned char[mtu]),
00089         pathMTU(mtu)
00090 { 
00091 }
00092 
00093 RTCPCompoundHandler::~RTCPCompoundHandler() 
00094 { 
00095 #ifdef  CCXX_EXCEPTIONS
00096         try {
00097 #endif
00098                 delete [] rtcpRecvBuffer;
00099 #ifdef  CCXX_EXCEPTIONS
00100         } catch (...) {}
00101         try {
00102 #endif
00103                 delete [] rtcpSendBuffer;
00104 #ifdef  CCXX_EXCEPTIONS
00105         } catch (...) {}
00106 #endif
00107 }
00108 
00109 bool
00110 RTCPCompoundHandler::checkCompoundRTCPHeader(size_t len) 
00111 {
00112         // Note that the first packet in the compount --in order to
00113         // detect possibly misaddressed RTP packets-- is more
00114         // thoroughly checked than the following. This mask checks the
00115         // first packet's version, padding (must be zero) and type
00116         // (must be SR or RR).
00117         if ( (*(reinterpret_cast<uint16*>(rtcpRecvBuffer)) 
00118               & RTCP_VALID_MASK)
00119              != RTCP_VALID_VALUE ) {
00120                 return false;
00121         }
00122 
00123         // this checks that every packet in the compound is tagged
00124         // with version == CCRTP_VERSION, and the length of the compound
00125         // packet matches the addition of the packets lenghts
00126         uint32 pointer = 0;
00127         RTCPPacket* pkt;
00128         do {
00129                 pkt = reinterpret_cast<RTCPPacket*>
00130                         (rtcpRecvBuffer + pointer);
00131                 pointer += (ntohs(pkt->fh.length)+1) << 2;
00132         } while ( (pointer < len) && (CCRTP_VERSION == pkt->fh.version) );
00133 
00134         if ( pointer != len )
00135                 return false;
00136 
00137         return true;
00138 }
00139 
00140 #ifdef  CCXX_NAMESPACES
00141 }
00142 #endif
00143 

© sourcejam.com 2005-2008