1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3 // Copyright (C) 2015 Cherokees of Idaho.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // As a special exception, you may use this file as part of a free software
20 // library without restriction. Specifically, if other files instantiate
21 // templates or use macros or inline functions from this file, or you compile
22 // this file and link it with other files to produce an executable, this
23 // file does not by itself cause the resulting executable to be covered by
24 // the GNU General Public License. This exception does not however
25 // invalidate any other reasons why the executable file might be covered by
26 // the GNU General Public License.
27 //
28 // This exception applies only to the code released under the name GNU
29 // ccRTP. If you copy code from other releases into a copy of GNU
30 // ccRTP, as the General Public License permits, the exception does
31 // not apply to the code that you add in this way. To avoid misleading
32 // anyone as to the status of such modified files, you must delete
33 // this exception notice from them.
34 //
35 // If you write modifications of your own for GNU ccRTP, it is your choice
36 // whether to permit this exception to apply to your modifications.
37 // If you do not wish that, delete this exception notice.
38 //
39
49
50 NAMESPACE_COMMONCPP
51
52 // Default to 8Khz when no value is specified.
54
55 //uint32 PayloadFormat::staticRates[lastStaticPayloadType]
57 // audio types:
58 8000, // 0 - sptPCMU
59 0, // 1 - reserved
60 8000, // 2 - sptG726_32
61 8000, // 3 - sptGSM
62 8000, // 4 - sptG723
63 8000, // 5 - sptDVI4_8000
64 16000, // 6 - sptDVI4_16000
65 8000, // 7 - sptLPC
66 8000, // 8 - sptPCMA
67 8000, // 9 - sptG722
68 44100, // 10 - sptL16_DUAL
69 44100, // 11 - sptL16_MONO
70 8000, // 12 - sptQCELP
71 0, // 13 - reserved
72 90000, // 14 - sptMPA
73 8000, // 15 - sptG728
74 11015, // 16 - sptDVI4_11025
75 22050, // 17 - sptDVI4_22050
76 8000 // 18 - sptG729
77 /* 0, // reserved
78 0, // unassigned
79 0, // unassigned
80 0, // unassigned
81 0 // unassigned
82 */
83 // All video types have 90000 hz RTP clock rate.
84 // If sometime in the future a static video payload type is
85 // defined with a different RTP clock rate (quite
86 // unprobable). This table and/or the StaticPayloadType
87 // constructor must be changed.
88 };
89
91 {
94 // audio static type
96 } else {
97 // video static type
99 }
100 }
101
103 {
106 }
107
108 // constructor commonly used for incoming packets
110 total((uint32)len), duplicated(duplicate)
111 {
118 }
120 len -= block[len - 1];
122
123 if ( duplicate ) {
124 buffer =
new unsigned char[len];
126 } else {
127 buffer =
const_cast<unsigned char*
>(block);
128 }
129 }
130
131 // constructor commonly used for outgoing packets
133 payloadSize((uint32)plen), buffer(NULL), hdrSize((uint32)hdrlen),
134 duplicated(false)
135 {
137 // compute if there must be padding
138 uint8 padding = 0;
139 if ( 0 != paddinglen ) {
140 padding = paddinglen - (
total % paddinglen);
142 }
145 if (pcc != NULL) {
146 // compute additional memory for SRTP data
149 }
150
151 // now we know the actual total length of the packet, get some memory
152 // but take SRTP data into account. Don't change total because some RTP
153 // functions rely on the fact that total is the overall size (without
154 // the SRTP data)
156 *(
reinterpret_cast<uint32*
>(
getHeader())) = 0;
158 if ( 0 != padding ) {
162 } else {
164 }
165 }
166
168 {
169 #ifdef CCXX_EXCEPTIONS
170 try {
171 #endif
173 #ifdef CCXX_EXCEPTIONS
174 } catch (...) { };
175 #endif
176 }
177
179 {
180 // If payloadsize was computed without padding set then re-compute
181 // payloadSize after the padding bit was set and set padding flag
182 // in RTP header - option for SRTP
183 if (padding) {
184 size_t len = 0;
188 }
189 }
190
192 const unsigned char* const hdrext, uint32 hdrextlen,
193 const unsigned char* const data, size_t datalen,
195 RTPPacket((getSizeOfFixedHeader() + sizeof(uint32) * numcsrc + hdrextlen),datalen,paddinglen, pcc)
196 {
198 // add CSCR identifiers (putting them in network order).
200 pointer += numcsrc * sizeof(uint32);
201
202 // add header extension.
205 pointer += hdrextlen;
206
207 // add data.
209 }
210
212 const unsigned char* data,
size_t datalen, uint8 paddinglen,
CryptoContext* pcc) :
213 RTPPacket((getSizeOfFixedHeader() + sizeof(uint32) *numcsrc),datalen, paddinglen, pcc)
214 {
216 // add CSCR identifiers (putting them in network order).
218 pointer += numcsrc * sizeof(uint32);
219
220 // not needed, as the RTPPacket constructor sets by default
221 // the whole fixed header to 0.
222 // getHeader()->extension = 0;
223
224 // add data.
226 }
227
230 RTPPacket(getSizeOfFixedHeader(),datalen,paddinglen, pcc)
231 {
232 // not needed, as the RTPPacket constructor sets by default
233 // the whole fixed header to 0.
234 //getHeader()->cc = 0;
235 //getHeader()->extension = 0;
236
238 }
239
241 {
243 uint32* csrc =
const_cast<uint32*
>(
getCSRCs());
244 for ( int i = 0; i < numcsrc; i++ )
245 csrc[i] = htonl(csrc[i]);
247 }
248
250 {
251 /* Encrypt the packet */
253
255
256 // NO MKI support yet - here we assume MKI is zero. To build in MKI
257 // take MKI length into account when storing the authentication tag.
258
259 /* Compute MAC */
262 /* Update the ROC if necessary */
265 }
266 }
267
268 // These masks are valid regardless of endianness.
271
274 {
275 // first, perform validity check:
276 // 1) check protocol version
277 // 2) it is not an SR nor an RR
278 // 3) consistent length field value (taking CC value and P and
279 // X bits into account)
281 /*
282 ||
283 getPayloadSize() <= 0 ) {
284 */
286 return;
287 }
292 }
293
295 {
296 if (pcc == NULL) {
297 return true;
298 }
299
300 /*
301 * This is the setting of the packet data when we come to this
302 * point:
303 *
304 * total: complete length of received data
305 * buffer: points to data as received from network
306 * hdrSize: length of header including header extension
307 * payloadSize: length of data excluding hdrSize and padding
308 *
309 * Because this is an SRTP packet we need to adjust some values here.
310 * The SRTP MKI and authentication data is always at the end of a
311 * packet. Thus compute the position of this data.
312 */
313
315
316 // now adjust total because some RTP functions rely on the fact that
317 // total is the full length of data without SRTP data.
319
320 // recompute payloadSize by subtracting SRTP data
322
323 // unused??
324 // const uint8* mki = getRawPacket() + srtpDataIndex;
326
327 /* Replay control */
329 return -2;
330 }
331 /* Guess the index */
333
334 uint32 guessedRoc = (uint32)(guessedIndex >> 16);
336
339 delete[] mac;
340 return -1;
341 }
342 delete[] mac;
343
344 /* Decrypt the content */
346
347 /* Update the Crypto-context */
349
350 return 1;
351 }
352
353 END_NAMESPACE
354
uint32 total
total length, including header, payload and padding
size_t getSizeOfFixedHeader() const
A base class for both IncomingRTPPkt and OutgoingRTPPkt.
void srtpAuthenticate(RTPPacket *rtp, uint32 roc, uint8 *tag)
Compute the authentication tag.
int32 getMkiLength() const
Get the length of the MKI in bytes.
The implementation for a SRTP cryptographic context.
void setRoc(uint32 r)
Set the Roll-Over-Counter.
const uint8 CCRTP_VERSION
RTP protocol version supported.
void reComputePayLength(bool padding)
Re-compute payload length.
PayloadType getPayloadType() const
uint32 getRawTimestamp() const
Obtain the absolute timestamp carried in the packet header.
const uint32 * getCSRCs() const
Get the 32-bit identifiers of the contributing sources for the packet as an array, of length getCSRCsCount().
uint32 getRoc() const
Get the Roll-Over-Counter.
Declaration of ccRTP internal stuff.
int32 unprotect(CryptoContext *pcc)
Unprotect a received packet.
unsigned char * buffer
packet in memory
OutgoingRTPPkt(const uint32 *const csrcs, uint16 numcsrc, const unsigned char *const hdrext, uint32 hdrextlen, const unsigned char *const data, size_t datalen, uint8 paddinglen=0, CryptoContext *pcc=NULL)
Construct a new packet to be sent, containing several contributing source identifiers, header extensions and payload.
static const uint16 RTP_INVALID_PT_VALUE
bool checkReplay(uint16 newSeqNumber)
Check for packet replay.
void srtpEncrypt(RTPPacket *rtp, uint64 index, uint32 ssrc)
Perform SRTP encryption.
uint32 srtpDataOffset
Offset into packet memory pointing to area for SRTP data.
IncomingRTPPkt(const unsigned char *block, size_t len)
Build an RTP packet object from a data buffer.
RTPFixedHeader * getHeader() const
Return low level structure for the header of the packet.
uint8 getProtocolVersion() const
uint32 cachedTimestamp
Packet timestamp in host order (includes initial shift).
static const uint16 RTP_INVALID_PT_MASK
const unsigned char *const getRawPacket() const
Get the raw packet as it will be sent through the network.
uint32 cachedSSRC
SSRC 32-bit identifier in host order.
void endPacket()
Free memory allocated for the packet.
void update(uint16 newSeqNumber)
Update the SRTP packet index.
int32 getTagLength() const
Get the length of the SRTP authentication tag in bytes.
bool headerValid
Header validity, checked at construction time.
ITU-T G.729. CS-ACELP audio.
void setbuffer(const void *src, size_t len, size_t pos)
uint64 guessIndex(uint16 newSeqNumber)
Compute (guess) the new SRTP index based on the sequence number of a received RTP packet...
void protect(uint32 ssrc, CryptoContext *pcc)
Called packet is setup.
void setCSRCArray(const uint32 *const csrcs, uint16 numcsrc)
Set the list of CSRC identifiers in an RTP packet, switching host to network order.
void setExtension(bool e)
RTPPacket(const unsigned char *const block, size_t len, bool duplicate=false)
Constructor, construct a packet object given the memory zone its content (header and payload) is stor...
uint32 hdrSize
size of the header, including contributing sources and extensions
int32 srtpLength
Lebgth of additional SRTP data.
StaticPayloadType
RTP static payload types (default bindings) defined in the AVP profile.
uint16 cachedSeqNum
Packet sequence number in host order.
uint32 payloadSize
note: payload (not full packet) size.
uint8 PayloadType
RTP payload type numeric identifier.