diff options
author | Martijnvdc <martijnvdc@gmail.com> | 2014-02-01 06:52:48 -0500 |
---|---|---|
committer | Martijnvdc <martijnvdc@gmail.com> | 2014-02-01 06:52:48 -0500 |
commit | 172f18b5e82cb33fcd6d3f56e9052232019fd8c1 (patch) | |
tree | 172faca60c5f1239125e20bcb9986423fbfd580e /toxav/toxrtp.c | |
parent | 984c564cba528328b134578a93a3795a0e0436f3 (diff) |
Added fixes to rtp and updated phone
Diffstat (limited to 'toxav/toxrtp.c')
-rwxr-xr-x | toxav/toxrtp.c | 275 |
1 files changed, 152 insertions, 123 deletions
diff --git a/toxav/toxrtp.c b/toxav/toxrtp.c index 03d20363..6b5ded45 100755 --- a/toxav/toxrtp.c +++ b/toxav/toxrtp.c | |||
@@ -28,8 +28,7 @@ | |||
28 | 28 | ||
29 | #include "toxrtp.h" | 29 | #include "toxrtp.h" |
30 | #include <assert.h> | 30 | #include <assert.h> |
31 | #include <limits.h> | 31 | #include <stdlib.h> |
32 | #include <pthread.h> | ||
33 | 32 | ||
34 | #include "../toxcore/util.h" | 33 | #include "../toxcore/util.h" |
35 | #include "../toxcore/network.h" | 34 | #include "../toxcore/network.h" |
@@ -59,6 +58,88 @@ | |||
59 | #define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f) | 58 | #define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f) |
60 | 59 | ||
61 | 60 | ||
61 | /** | ||
62 | * @brief Converts 4 bytes to uint32_t | ||
63 | * | ||
64 | * @param dest Where to convert | ||
65 | * @param bytes What bytes | ||
66 | * @return void | ||
67 | */ | ||
68 | inline__ void bytes_to_U32(uint32_t* dest, const uint8_t* bytes) | ||
69 | { | ||
70 | *dest = | ||
71 | #ifdef WORDS_BIGENDIAN | ||
72 | ( ( uint32_t ) * bytes ) | | ||
73 | ( ( uint32_t ) * ( bytes + 1 ) << 8 ) | | ||
74 | ( ( uint32_t ) * ( bytes + 2 ) << 16 ) | | ||
75 | ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ; | ||
76 | #else | ||
77 | ( ( uint32_t ) * bytes << 24 ) | | ||
78 | ( ( uint32_t ) * ( bytes + 1 ) << 16 ) | | ||
79 | ( ( uint32_t ) * ( bytes + 2 ) << 8 ) | | ||
80 | ( ( uint32_t ) * ( bytes + 3 ) ) ; | ||
81 | #endif | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * @brief Converts 2 bytes to uint16_t | ||
86 | * | ||
87 | * @param dest Where to convert | ||
88 | * @param bytes What bytes | ||
89 | * @return void | ||
90 | */ | ||
91 | inline__ void bytes_to_U16(uint16_t* dest, const uint8_t* bytes) | ||
92 | { | ||
93 | *dest = | ||
94 | #ifdef WORDS_BIGENDIAN | ||
95 | ( ( uint16_t ) * bytes ) | | ||
96 | ( ( uint16_t ) * ( bytes + 1 ) << 8 ); | ||
97 | #else | ||
98 | ( ( uint16_t ) * bytes << 8 ) | | ||
99 | ( ( uint16_t ) * ( bytes + 1 ) ); | ||
100 | #endif | ||
101 | } | ||
102 | |||
103 | /** | ||
104 | * @brief Convert uint32_t to byte string of size 4 | ||
105 | * | ||
106 | * @param dest Where to convert | ||
107 | * @param value The value | ||
108 | * @return void | ||
109 | */ | ||
110 | inline__ void U32_to_bytes(uint8_t* dest, uint32_t value) | ||
111 | { | ||
112 | #ifdef WORDS_BIGENDIAN | ||
113 | *(dest) = ( value ); | ||
114 | *(dest + 1) = ( value >> 8 ); | ||
115 | *(dest + 2) = ( value >> 16 ); | ||
116 | *(dest + 3) = ( value >> 24 ); | ||
117 | #else | ||
118 | *(dest) = ( value >> 24 ); | ||
119 | *(dest + 1) = ( value >> 16 ); | ||
120 | *(dest + 2) = ( value >> 8 ); | ||
121 | *(dest + 3) = ( value ); | ||
122 | #endif | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * @brief Convert uint16_t to byte string of size 2 | ||
127 | * | ||
128 | * @param dest Where to convert | ||
129 | * @param value The value | ||
130 | * @return void | ||
131 | */ | ||
132 | inline__ void U16_to_bytes(uint8_t* dest, uint16_t value) | ||
133 | { | ||
134 | #ifdef WORDS_BIGENDIAN | ||
135 | *(dest) = ( value ); | ||
136 | *(dest + 1) = ( value >> 8 ); | ||
137 | #else | ||
138 | *(dest) = ( value >> 8 ); | ||
139 | *(dest + 1) = ( value ); | ||
140 | #endif | ||
141 | } | ||
142 | |||
62 | 143 | ||
63 | /** | 144 | /** |
64 | * @brief Checks if message came in late. | 145 | * @brief Checks if message came in late. |
@@ -89,25 +170,31 @@ inline__ int check_late_message (RTPSession* session, RTPMessage* msg) | |||
89 | */ | 170 | */ |
90 | inline__ void increase_nonce(uint8_t* nonce, uint16_t target) | 171 | inline__ void increase_nonce(uint8_t* nonce, uint16_t target) |
91 | { | 172 | { |
92 | uint16_t _nonce_counter = ((uint16_t)( | 173 | uint16_t _nonce_counter; |
93 | (((uint16_t) nonce [crypto_box_NONCEBYTES - 1]) << 8 ) | | 174 | |
94 | (((uint16_t) nonce [crypto_box_NONCEBYTES - 2]) ))); | 175 | uint8_t _reverse_bytes[2]; |
95 | 176 | _reverse_bytes[0] = nonce[crypto_box_NONCEBYTES - 1]; | |
177 | _reverse_bytes[1] = nonce[crypto_box_NONCEBYTES - 2]; | ||
178 | |||
179 | bytes_to_U16(&_nonce_counter, _reverse_bytes ); | ||
180 | |||
96 | /* Check overflow */ | 181 | /* Check overflow */ |
97 | if (_nonce_counter > USHRT_MAX - target ) { /* 2 bytes are not long enough */ | 182 | if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */ |
98 | int _it = 3; | 183 | uint8_t _it = 3; |
99 | while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ? crypto_box_NONCEBYTES : 1; | 184 | while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ? crypto_box_NONCEBYTES : 1; |
100 | 185 | ||
101 | _nonce_counter = _nonce_counter - (USHRT_MAX - target ); /* Assign the rest of it */ | 186 | _nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */ |
102 | } else { /* Increase nonce */ | 187 | } else { /* Increase nonce */ |
103 | 188 | ||
104 | _nonce_counter+= target; | 189 | _nonce_counter+= target; |
105 | } | 190 | } |
106 | 191 | ||
107 | /* Assign the 8 last bytes */ | 192 | /* Assign the last bytes */ |
193 | |||
194 | U16_to_bytes( _reverse_bytes, _nonce_counter); | ||
195 | nonce [crypto_box_NONCEBYTES - 1] = _reverse_bytes[0]; | ||
196 | nonce [crypto_box_NONCEBYTES - 2] = _reverse_bytes[1]; | ||
108 | 197 | ||
109 | nonce [crypto_box_NONCEBYTES - 1] = (uint8_t) (_nonce_counter >> 8); | ||
110 | nonce [crypto_box_NONCEBYTES - 2] = (uint8_t) (_nonce_counter); | ||
111 | } | 198 | } |
112 | 199 | ||
113 | 200 | ||
@@ -141,15 +228,15 @@ static const uint32_t payload_table[] = | |||
141 | * @return RTPHeader* Extracted header. | 228 | * @return RTPHeader* Extracted header. |
142 | * @retval NULL Error occurred while extracting header. | 229 | * @retval NULL Error occurred while extracting header. |
143 | */ | 230 | */ |
144 | RTPHeader* extract_header ( const uint8_t* payload, size_t length ) | 231 | RTPHeader* extract_header ( const uint8_t* payload, int length ) |
145 | { | 232 | { |
146 | if ( !payload ) { | 233 | if ( !payload || !length ) { |
147 | return NULL; | 234 | return NULL; |
148 | } | 235 | } |
149 | 236 | ||
150 | const uint8_t* _it = payload; | 237 | const uint8_t* _it = payload; |
151 | 238 | ||
152 | RTPHeader* _retu = calloc(sizeof(RTPHeader), 1); | 239 | RTPHeader* _retu = calloc(1, sizeof (RTPHeader)); |
153 | assert(_retu); | 240 | assert(_retu); |
154 | 241 | ||
155 | _retu->flags = *_it; ++_it; | 242 | _retu->flags = *_it; ++_it; |
@@ -168,7 +255,7 @@ RTPHeader* extract_header ( const uint8_t* payload, size_t length ) | |||
168 | * Added a check for the size of the header little sooner so | 255 | * Added a check for the size of the header little sooner so |
169 | * I don't need to parse the other stuff if it's bad | 256 | * I don't need to parse the other stuff if it's bad |
170 | */ | 257 | */ |
171 | uint8_t _cc = GET_FLAG_CSRCC ( _retu ); | 258 | uint8_t _cc = GET_FLAG_CSRCC ( _retu ); |
172 | uint32_t _length = 12 /* Minimum header len */ + ( _cc * 4 ); | 259 | uint32_t _length = 12 /* Minimum header len */ + ( _cc * 4 ); |
173 | 260 | ||
174 | if ( length < _length ) { | 261 | if ( length < _length ) { |
@@ -178,7 +265,7 @@ RTPHeader* extract_header ( const uint8_t* payload, size_t length ) | |||
178 | } | 265 | } |
179 | 266 | ||
180 | if ( _cc > 0 ) { | 267 | if ( _cc > 0 ) { |
181 | _retu->csrc = calloc ( sizeof ( uint32_t ), _cc ); | 268 | _retu->csrc = calloc (_cc, sizeof (uint32_t)); |
182 | assert(_retu->csrc); | 269 | assert(_retu->csrc); |
183 | 270 | ||
184 | } else { /* But this should not happen ever */ | 271 | } else { /* But this should not happen ever */ |
@@ -191,26 +278,13 @@ RTPHeader* extract_header ( const uint8_t* payload, size_t length ) | |||
191 | _retu->marker_payloadt = *_it; ++_it; | 278 | _retu->marker_payloadt = *_it; ++_it; |
192 | _retu->length = _length; | 279 | _retu->length = _length; |
193 | 280 | ||
194 | _retu->timestamp = ( ( uint32_t ) * _it << 24 ) | | ||
195 | ( ( uint32_t ) * ( _it + 1 ) << 16 ) | | ||
196 | ( ( uint32_t ) * ( _it + 2 ) << 8 ) | | ||
197 | ( * ( _it + 3 ) ) ; | ||
198 | 281 | ||
199 | _it += 4; | 282 | bytes_to_U32(&_retu->timestamp, _it); _it += 4; |
283 | bytes_to_U32(&_retu->ssrc, _it); | ||
200 | 284 | ||
201 | _retu->ssrc = ( ( uint32_t ) * _it << 24 ) | | 285 | uint8_t _x; |
202 | ( ( uint32_t ) * ( _it + 1 ) << 16 ) | | ||
203 | ( ( uint32_t ) * ( _it + 2 ) << 8 ) | | ||
204 | ( ( uint32_t ) * ( _it + 3 ) ) ; | ||
205 | |||
206 | |||
207 | size_t _x; | ||
208 | for ( _x = 0; _x < _cc; _x++ ) { | 286 | for ( _x = 0; _x < _cc; _x++ ) { |
209 | _it += 4; | 287 | _it += 4; bytes_to_U32(&(_retu->csrc[_x]), _it); |
210 | _retu->csrc[_x] = ( ( uint32_t ) * _it << 24 ) | | ||
211 | ( ( uint32_t ) * ( _it + 1 ) << 16 ) | | ||
212 | ( ( uint32_t ) * ( _it + 2 ) << 8 ) | | ||
213 | ( ( uint32_t ) * ( _it + 3 ) ) ; | ||
214 | } | 288 | } |
215 | 289 | ||
216 | return _retu; | 290 | return _retu; |
@@ -228,29 +302,26 @@ RTPExtHeader* extract_ext_header ( const uint8_t* payload, size_t length ) | |||
228 | { | 302 | { |
229 | const uint8_t* _it = payload; | 303 | const uint8_t* _it = payload; |
230 | 304 | ||
231 | RTPExtHeader* _retu = calloc(sizeof(RTPExtHeader), 1); | 305 | RTPExtHeader* _retu = calloc(1, sizeof (RTPExtHeader)); |
232 | assert(_retu); | 306 | assert(_retu); |
233 | 307 | ||
234 | uint16_t _ext_length = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 ); _it += 2; | 308 | uint16_t _ext_length; |
309 | bytes_to_U16(&_ext_length, _it); _it += 2; | ||
310 | |||
235 | 311 | ||
236 | if ( length < ( _ext_length * sizeof(uint32_t) ) ) { | 312 | if ( length < ( _ext_length * sizeof(uint32_t) ) ) { |
237 | return NULL; | 313 | return NULL; |
238 | } | 314 | } |
239 | 315 | ||
240 | _retu->length = _ext_length; | 316 | _retu->length = _ext_length; |
241 | _retu->type = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 ); _it -= 2; | 317 | bytes_to_U16(&_retu->type, _it); _it += 2; |
242 | 318 | ||
243 | _retu->table = calloc(sizeof(uint32_t), _ext_length ); | 319 | _retu->table = calloc(_ext_length, sizeof (uint32_t)); |
244 | assert(_retu->table); | 320 | assert(_retu->table); |
245 | 321 | ||
246 | uint32_t* _table = _retu->table; | 322 | uint16_t _x; |
247 | size_t _i; | 323 | for ( _x = 0; _x < _ext_length; _x++ ) { |
248 | for ( _i = 0; _i < _ext_length; _i++ ) { | 324 | _it += 4; bytes_to_U32(&(_retu->table[_x]), _it); |
249 | _it += 4; | ||
250 | _table[_i] = ( ( uint32_t ) * _it << 24 ) | | ||
251 | ( ( uint32_t ) * ( _it + 1 ) << 16 ) | | ||
252 | ( ( uint32_t ) * ( _it + 2 ) << 8 ) | | ||
253 | ( ( uint32_t ) * ( _it + 3 ) ) ; | ||
254 | } | 325 | } |
255 | 326 | ||
256 | return _retu; | 327 | return _retu; |
@@ -271,33 +342,18 @@ uint8_t* add_header ( RTPHeader* header, uint8_t* payload ) | |||
271 | 342 | ||
272 | 343 | ||
273 | /* Add sequence number first */ | 344 | /* Add sequence number first */ |
274 | *_it = ( header->sequnum >> 8 ); ++_it; | 345 | U16_to_bytes(_it, header->sequnum); _it += 2; |
275 | *_it = ( header->sequnum ); ++_it; | ||
276 | 346 | ||
277 | *_it = header->flags; ++_it; | 347 | *_it = header->flags; ++_it; |
278 | *_it = header->marker_payloadt; ++_it; | 348 | *_it = header->marker_payloadt; ++_it; |
279 | 349 | ||
280 | 350 | ||
281 | uint32_t _timestamp = header->timestamp; | 351 | U32_to_bytes( _it, header->timestamp); _it+=4; |
282 | *_it = ( _timestamp >> 24 ); ++_it; | 352 | U32_to_bytes( _it, header->ssrc); |
283 | *_it = ( _timestamp >> 16 ); ++_it; | 353 | |
284 | *_it = ( _timestamp >> 8 ); ++_it; | 354 | uint8_t _x; |
285 | *_it = ( _timestamp ); ++_it; | ||
286 | |||
287 | uint32_t _ssrc = header->ssrc; | ||
288 | *_it = ( _ssrc >> 24 ); ++_it; | ||
289 | *_it = ( _ssrc >> 16 ); ++_it; | ||
290 | *_it = ( _ssrc >> 8 ); ++_it; | ||
291 | *_it = ( _ssrc ); | ||
292 | |||
293 | uint32_t *_csrc = header->csrc; | ||
294 | size_t _x; | ||
295 | for ( _x = 0; _x < _cc; _x++ ) { | 355 | for ( _x = 0; _x < _cc; _x++ ) { |
296 | ++_it; | 356 | _it+=4; U32_to_bytes( _it, header->csrc[_x]); |
297 | *_it = ( _csrc[_x] >> 24 ); ++_it; | ||
298 | *_it = ( _csrc[_x] >> 16 ); ++_it; | ||
299 | *_it = ( _csrc[_x] >> 8 ); ++_it; | ||
300 | *_it = ( _csrc[_x] ); | ||
301 | } | 357 | } |
302 | 358 | ||
303 | return _it; | 359 | return _it; |
@@ -314,21 +370,12 @@ uint8_t* add_ext_header ( RTPExtHeader* header, uint8_t* payload ) | |||
314 | { | 370 | { |
315 | uint8_t* _it = payload; | 371 | uint8_t* _it = payload; |
316 | 372 | ||
317 | *_it = ( header->length >> 8 ); _it++; | 373 | U16_to_bytes(_it, header->length); _it+=2; |
318 | *_it = ( header->length ); _it++; | 374 | U16_to_bytes(_it, header->type); _it-=2; /* Return to 0 position */ |
319 | 375 | ||
320 | *_it = ( header->type >> 8 ); ++_it; | 376 | uint16_t _x; |
321 | *_it = ( header->type ); | 377 | for ( _x = 0; _x < header->length; _x++ ) { |
322 | 378 | _it+=4; U32_to_bytes(_it, header->table[_x]); | |
323 | size_t x; | ||
324 | |||
325 | uint32_t* _hd_ext = header->table; | ||
326 | for ( x = 0; x < header->length; x++ ) { | ||
327 | ++_it; | ||
328 | *_it = ( _hd_ext[x] >> 24 ); ++_it; | ||
329 | *_it = ( _hd_ext[x] >> 16 ); ++_it; | ||
330 | *_it = ( _hd_ext[x] >> 8 ); ++_it; | ||
331 | *_it = ( _hd_ext[x] ); | ||
332 | } | 379 | } |
333 | 380 | ||
334 | return _it; | 381 | return _it; |
@@ -342,8 +389,7 @@ uint8_t* add_ext_header ( RTPExtHeader* header, uint8_t* payload ) | |||
342 | */ | 389 | */ |
343 | RTPHeader* build_header ( RTPSession* session ) | 390 | RTPHeader* build_header ( RTPSession* session ) |
344 | { | 391 | { |
345 | RTPHeader* _retu; | 392 | RTPHeader* _retu = calloc ( 1, sizeof (RTPHeader) ); |
346 | _retu = calloc ( sizeof * _retu, 1 ); | ||
347 | assert(_retu); | 393 | assert(_retu); |
348 | 394 | ||
349 | ADD_FLAG_VERSION ( _retu, session->version ); | 395 | ADD_FLAG_VERSION ( _retu, session->version ); |
@@ -358,7 +404,7 @@ RTPHeader* build_header ( RTPSession* session ) | |||
358 | _retu->ssrc = session->ssrc; | 404 | _retu->ssrc = session->ssrc; |
359 | 405 | ||
360 | if ( session->cc > 0 ) { | 406 | if ( session->cc > 0 ) { |
361 | _retu->csrc = calloc(sizeof(uint32_t), session->cc); | 407 | _retu->csrc = calloc(session->cc, sizeof (uint32_t)); |
362 | assert(_retu->csrc); | 408 | assert(_retu->csrc); |
363 | 409 | ||
364 | int i; | 410 | int i; |
@@ -388,12 +434,9 @@ RTPHeader* build_header ( RTPSession* session ) | |||
388 | * @return RTPMessage* | 434 | * @return RTPMessage* |
389 | * @retval NULL Error occurred. | 435 | * @retval NULL Error occurred. |
390 | */ | 436 | */ |
391 | RTPMessage* msg_parse ( RTPSession* session, uint16_t sequnum, const uint8_t* data, uint32_t length ) | 437 | RTPMessage* msg_parse ( uint16_t sequnum, const uint8_t* data, int length ) |
392 | { | 438 | { |
393 | assert( length != -1); | 439 | RTPMessage* _retu = calloc(1, sizeof (RTPMessage)); |
394 | |||
395 | RTPMessage* _retu = calloc(sizeof(RTPMessage), 1); | ||
396 | assert(_retu); | ||
397 | 440 | ||
398 | _retu->header = extract_header ( data, length ); /* It allocates memory and all */ | 441 | _retu->header = extract_header ( data, length ); /* It allocates memory and all */ |
399 | 442 | ||
@@ -413,7 +456,7 @@ RTPMessage* msg_parse ( RTPSession* session, uint16_t sequnum, const uint8_t* da | |||
413 | if ( _retu->ext_header ){ | 456 | if ( _retu->ext_header ){ |
414 | _retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); | 457 | _retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); |
415 | _from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); | 458 | _from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); |
416 | } else { | 459 | } else { /* Error */ |
417 | free (_retu->ext_header); | 460 | free (_retu->ext_header); |
418 | free (_retu->header); | 461 | free (_retu->header); |
419 | free (_retu); | 462 | free (_retu); |
@@ -423,20 +466,9 @@ RTPMessage* msg_parse ( RTPSession* session, uint16_t sequnum, const uint8_t* da | |||
423 | _retu->ext_header = NULL; | 466 | _retu->ext_header = NULL; |
424 | } | 467 | } |
425 | 468 | ||
426 | /* Get the payload */ | ||
427 | _retu->data = calloc ( sizeof ( uint8_t ), _retu->length ); | ||
428 | assert(_retu->data); | ||
429 | |||
430 | memcpy ( _retu->data, data + _from_pos, length - _from_pos ); | 469 | memcpy ( _retu->data, data + _from_pos, length - _from_pos ); |
431 | |||
432 | _retu->next = NULL; | 470 | _retu->next = NULL; |
433 | 471 | ||
434 | |||
435 | if ( session && check_late_message ( session, _retu) < 0 ){ | ||
436 | session->rsequnum = _retu->header->sequnum; | ||
437 | session->timestamp = _retu->header->timestamp; | ||
438 | } | ||
439 | |||
440 | return _retu; | 472 | return _retu; |
441 | } | 473 | } |
442 | 474 | ||
@@ -460,8 +492,9 @@ int rtp_handle_packet ( void* object, IP_Port ip_port, uint8_t* data, uint32_t l | |||
460 | return -1; | 492 | return -1; |
461 | 493 | ||
462 | uint8_t _plain[MAX_UDP_PACKET_SIZE]; | 494 | uint8_t _plain[MAX_UDP_PACKET_SIZE]; |
463 | 495 | ||
464 | uint16_t _sequnum = ( ( uint16_t ) data[1] << 8 ) | data[2]; | 496 | uint16_t _sequnum; |
497 | bytes_to_U16(&_sequnum, data + 1); | ||
465 | 498 | ||
466 | /* Clculate the right nonce */ | 499 | /* Clculate the right nonce */ |
467 | uint8_t _calculated[crypto_box_NONCEBYTES]; | 500 | uint8_t _calculated[crypto_box_NONCEBYTES]; |
@@ -500,10 +533,9 @@ int rtp_handle_packet ( void* object, IP_Port ip_port, uint8_t* data, uint32_t l | |||
500 | } | 533 | } |
501 | } | 534 | } |
502 | 535 | ||
503 | _msg = msg_parse ( NULL, _sequnum, _plain, _decrypted_length ); | 536 | _msg = msg_parse ( _sequnum, _plain, _decrypted_length ); |
504 | 537 | ||
505 | if ( !_msg ) | 538 | if ( !_msg ) return -1; |
506 | return -1; | ||
507 | 539 | ||
508 | /* Hopefully this goes well | 540 | /* Hopefully this goes well |
509 | * NOTE: Is this even used? | 541 | * NOTE: Is this even used? |
@@ -548,7 +580,7 @@ RTPMessage* rtp_new_message ( RTPSession* session, const uint8_t* data, uint32_t | |||
548 | return NULL; | 580 | return NULL; |
549 | 581 | ||
550 | uint8_t* _from_pos; | 582 | uint8_t* _from_pos; |
551 | RTPMessage* _retu = calloc(sizeof(RTPMessage), 1); | 583 | RTPMessage* _retu = calloc(1, sizeof (RTPMessage)); |
552 | assert(_retu); | 584 | assert(_retu); |
553 | 585 | ||
554 | /* Sets header values and copies the extension header in _retu */ | 586 | /* Sets header values and copies the extension header in _retu */ |
@@ -560,17 +592,10 @@ RTPMessage* rtp_new_message ( RTPSession* session, const uint8_t* data, uint32_t | |||
560 | 592 | ||
561 | if ( _retu->ext_header ) { | 593 | if ( _retu->ext_header ) { |
562 | _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); | 594 | _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); |
563 | /* Allocate Memory for _retu->_data */ | ||
564 | _retu->data = calloc ( sizeof _retu->data, _total_length ); | ||
565 | assert(_retu->data); | ||
566 | 595 | ||
567 | _from_pos = add_header ( _retu->header, _retu->data ); | 596 | _from_pos = add_header ( _retu->header, _retu->data ); |
568 | _from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 ); | 597 | _from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 ); |
569 | } else { | 598 | } else { |
570 | /* Allocate Memory for _retu->_data */ | ||
571 | _retu->data = calloc ( sizeof _retu->data, _total_length ); | ||
572 | assert(_retu->data); | ||
573 | |||
574 | _from_pos = add_header ( _retu->header, _retu->data ); | 599 | _from_pos = add_header ( _retu->header, _retu->data ); |
575 | } | 600 | } |
576 | 601 | ||
@@ -590,6 +615,11 @@ RTPMessage* rtp_new_message ( RTPSession* session, const uint8_t* data, uint32_t | |||
590 | } | 615 | } |
591 | 616 | ||
592 | 617 | ||
618 | |||
619 | |||
620 | |||
621 | |||
622 | |||
593 | /******************************************************************************************************************** | 623 | /******************************************************************************************************************** |
594 | ******************************************************************************************************************** | 624 | ******************************************************************************************************************** |
595 | ******************************************************************************************************************** | 625 | ******************************************************************************************************************** |
@@ -733,14 +763,13 @@ int rtp_send_msg ( RTPSession* session, Tox* messenger, const uint8_t* data, uin | |||
733 | /** | 763 | /** |
734 | * @brief Speaks for it self. | 764 | * @brief Speaks for it self. |
735 | * | 765 | * |
736 | * @param session The control session msg belongs to. It can be NULL. | 766 | * @param session The control session msg belongs to. You set it as NULL when freeing recved messages. |
767 | * Otherwise set it to session the message was created from. | ||
737 | * @param msg The message. | 768 | * @param msg The message. |
738 | * @return void | 769 | * @return void |
739 | */ | 770 | */ |
740 | void rtp_free_msg ( RTPSession* session, RTPMessage* msg ) | 771 | void rtp_free_msg ( RTPSession* session, RTPMessage* msg ) |
741 | { | 772 | { |
742 | free ( msg->data ); | ||
743 | |||
744 | if ( !session ){ | 773 | if ( !session ){ |
745 | free ( msg->header->csrc ); | 774 | free ( msg->header->csrc ); |
746 | if ( msg->ext_header ){ | 775 | if ( msg->ext_header ){ |
@@ -793,7 +822,7 @@ RTPSession* rtp_init_session ( int payload_type, | |||
793 | return NULL; | 822 | return NULL; |
794 | } | 823 | } |
795 | 824 | ||
796 | RTPSession* _retu = calloc(sizeof(RTPSession), 1); | 825 | RTPSession* _retu = calloc(1, sizeof(RTPSession)); |
797 | assert(_retu); | 826 | assert(_retu); |
798 | 827 | ||
799 | networking_registerhandler(_messenger_casted->net, payload_type, rtp_handle_packet, _retu); | 828 | networking_registerhandler(_messenger_casted->net, payload_type, rtp_handle_packet, _retu); |
@@ -819,15 +848,15 @@ RTPSession* rtp_init_session ( int payload_type, | |||
819 | _retu->decrypt_key = decrypt_key; | 848 | _retu->decrypt_key = decrypt_key; |
820 | 849 | ||
821 | /* Need to allocate new memory */ | 850 | /* Need to allocate new memory */ |
822 | _retu->encrypt_nonce = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); assert(_retu->encrypt_nonce); | 851 | _retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->encrypt_nonce); |
823 | _retu->decrypt_nonce = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); assert(_retu->decrypt_nonce); | 852 | _retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->decrypt_nonce); |
824 | _retu->nonce_cycle = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); assert(_retu->nonce_cycle); | 853 | _retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->nonce_cycle); |
825 | 854 | ||
826 | memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES); | 855 | memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES); |
827 | memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES); | 856 | memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES); |
828 | memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES); | 857 | memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES); |
829 | 858 | ||
830 | _retu->csrc = calloc(sizeof(uint32_t), 1); | 859 | _retu->csrc = calloc(1, sizeof (uint32_t)); |
831 | assert(_retu->csrc); | 860 | assert(_retu->csrc); |
832 | 861 | ||
833 | _retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */ | 862 | _retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */ |