summaryrefslogtreecommitdiff
path: root/toxrtp/toxrtp_message.c
blob: e7f1f2c0ea4227c2bfb047a5db8f6fb2704b107f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*   rtp_message.c
 *
 *   Rtp Message handler. It handles message/header parsing.
 *   Refer to RTP: A Transport Protocol for Real-Time Applications ( RFC 3550 ) for more info. !Red!
 *
 *
 *   Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *   This file is part of Tox.
 *
 *   Tox is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Tox is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include "toxrtp_message.h"
#include "toxrtp.h"
#include <stdio.h>

#ifdef _USE_ERRORS
#include "toxrtp_error_id.h"
#endif /* _USE_ERRORS */

#include <assert.h>

/* Some defines */

/* End of defines */

void rtp_header_print (const rtp_header_t* _header)
{
    printf("Header:      \n"
           "Version:    %d\n"
           "Padding:    %d\n"
           "Ext:        %d\n"
           "CC:         %d\n"
           "marker:     %d\n"
           "payload typ:%d\n\n"
           "sequ num:   %d\n"
           "Timestamp:  %d\n"
           "SSrc:       %d\n"
           "CSrc:       %d\n"
           "Lenght:     %d\n"
           ,rtp_header_get_flag_version(_header)
           ,rtp_header_get_flag_padding(_header)
           ,rtp_header_get_flag_extension(_header)
           ,rtp_header_get_flag_CSRC_count(_header)
           ,rtp_header_get_setting_marker(_header)
           ,rtp_header_get_setting_payload_type(_header)
           ,_header->_sequence_number
           ,_header->_timestamp
           ,_header->_ssrc
           ,_header->_csrc[0]
           ,_header->_length
           );
}

rtp_header_t* rtp_extract_header ( const uint8_t* _payload, size_t _bytes )
{
    if ( !_payload ) {
        t_perror ( RTP_ERROR_PAYLOAD_NULL );
        return NULL;
    }
    const uint8_t* _it = _payload;

    rtp_header_t* _retu = calloc(sizeof(rtp_header_t), 1);
    assert(_retu);

    _retu->_flags = *_it; ++_it;
    
    /* This indicates if the first 2 bytes are valid.
     * Now it my happen that this is out of order but
     * it cuts down chances of parsing some invalid value
     */
    if ( rtp_header_get_flag_version(_retu) != RTP_VERSION ){
        printf("Invalid version: %d\n", rtp_header_get_flag_version(_retu));
        //assert(rtp_header_get_flag_version(_retu) == RTP_VERSION);
        /* Deallocate */
        //DEALLOCATOR(_retu);
        //return NULL;
    }

    /*
     * Added a check for the size of the header little sooner so
     * I don't need to parse the other stuff if it's bad
     */
    uint8_t cc = rtp_header_get_flag_CSRC_count ( _retu );
    uint32_t _lenght = _MIN_HEADER_LENGTH + ( cc * 4 );

    if ( _bytes < _lenght ) {
        t_perror ( RTP_ERROR_PAYLOAD_INVALID );
        return NULL;
    }

    if ( cc > 0 ) {
        _retu->_csrc = calloc ( sizeof ( uint32_t ), cc );
        assert(_retu->_csrc);

    } else { /* But this should not happen ever */
        t_perror ( RTP_ERROR_HEADER_PARSING );
        return NULL;
    }


    _retu->_marker_payload_t = *_it; ++_it;
    _retu->_length = _lenght;
    _retu->_sequence_number = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 );

    _it += 2;

    _retu->_timestamp = ( ( uint32_t ) * _it         << 24 ) |
                        ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
                        ( ( uint32_t ) * ( _it + 2 ) << 8 )  |
                        (              * ( _it + 3 ) ) ;

    _it += 4;

    _retu->_ssrc = ( ( uint32_t ) * _it         << 24 ) |
                   ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
                   ( ( uint32_t ) * ( _it + 2 ) << 8 )  |
                   ( ( uint32_t ) * ( _it + 3 ) ) ;


    size_t x;
    for ( x = 0; x < cc; x++ ) {
        _it += 4;
        _retu->_csrc[x] = ( ( uint32_t ) * _it          << 24 ) |
                          ( ( uint32_t ) * ( _it + 1 )  << 16 ) |
                          ( ( uint32_t ) * ( _it + 2 )  << 8 )  |
                          ( ( uint32_t ) * ( _it + 3 ) ) ;
    }

    return _retu;
}

rtp_ext_header_t* rtp_extract_ext_header ( const uint8_t* _payload, size_t _bytes )
{
    if ( !_payload ) {
        t_perror ( RTP_ERROR_PAYLOAD_NULL );
        return NULL;
    }



    const uint8_t* _it = _payload;

    rtp_ext_header_t* _retu = calloc(sizeof(rtp_ext_header_t), 1);
    assert(_retu);

    uint16_t _ext_len = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 ); _it += 2;

    if ( _bytes < ( _ext_len * sizeof(uint32_t) ) ) {
        t_perror ( RTP_ERROR_PAYLOAD_INVALID );
        return NULL;
    }

    _retu->_ext_len  = _ext_len;
    _retu->_ext_type = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 ); _it -= 2;

    _retu->_hd_ext = calloc(sizeof(uint32_t), _ext_len);
    assert(_retu->_hd_ext);

    uint32_t* _hd_ext = _retu->_hd_ext;
    size_t i;
    for ( i = 0; i < _ext_len; i++ ) {
        _it += 4;
        _hd_ext[i] = ( ( uint32_t ) * _it         << 24 ) |
                     ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
                     ( ( uint32_t ) * ( _it + 2 ) << 8 )  |
                     ( ( uint32_t ) * ( _it + 3 ) ) ;
    }

    return _retu;
}

uint8_t* rtp_add_header ( rtp_header_t* _header, uint8_t* _payload )
{
    uint8_t cc = rtp_header_get_flag_CSRC_count ( _header );

    uint8_t* _it = _payload;

    *_it = _header->_flags; ++_it;
    *_it = _header->_marker_payload_t; ++_it;

    *_it = ( _header->_sequence_number >> 8 ); ++_it;
    *_it = ( _header->_sequence_number ); ++_it;

    uint32_t _timestamp = _header->_timestamp;
    *_it = ( _timestamp >> 24 ); ++_it;
    *_it = ( _timestamp >> 16 ); ++_it;
    *_it = ( _timestamp >> 8 ); ++_it;
    *_it = ( _timestamp ); ++_it;

    uint32_t _ssrc = _header->_ssrc;
    *_it = ( _ssrc >> 24 ); ++_it;
    *_it = ( _ssrc >> 16 ); ++_it;
    *_it = ( _ssrc >> 8 ); ++_it;
    *_it = ( _ssrc );

    uint32_t *_csrc = _header->_csrc;
    size_t x;
    for ( x = 0; x < cc; x++ ) {
        ++_it;
        *_it = ( _csrc[x] >> 24 );  ++_it;
        *_it = ( _csrc[x] >> 16 );  ++_it;
        *_it = ( _csrc[x] >> 8 );   ++_it;
        *_it = ( _csrc[x] );
    }

    return _it;
}

uint8_t* rtp_add_extention_header ( rtp_ext_header_t* _header, uint8_t* _payload )
{
    uint8_t* _it = _payload;

    *_it = ( _header->_ext_len >> 8 ); _it++;
    *_it = ( _header->_ext_len ); _it++;

    *_it = ( _header->_ext_type >> 8 ); ++_it;
    *_it = ( _header->_ext_type );

    size_t x;

    uint32_t* _hd_ext = _header->_hd_ext;
    for ( x = 0; x < _header->_ext_len; x++ ) {
        ++_it;
        *_it = ( _hd_ext[x] >> 24 );  ++_it;
        *_it = ( _hd_ext[x] >> 16 );  ++_it;
        *_it = ( _hd_ext[x] >> 8 );  ++_it;
        *_it = ( _hd_ext[x] );
    }

    return _it;
}

size_t rtp_header_get_size ( const rtp_header_t* _header )
{
    return ( 8 + ( rtp_header_get_flag_CSRC_count ( _header ) * 4 ) );
}
/* Setting flags */

void rtp_header_add_flag_version ( rtp_header_t* _header, uint32_t value )
{
    ( _header->_flags ) &= 0x3F;
    ( _header->_flags ) |= ( ( ( value ) << 6 ) & 0xC0 );
}

void rtp_header_add_flag_padding ( rtp_header_t* _header, uint32_t value )
{
    if ( value > 0 ) {
        value = 1; /* It can only be 1 */
    }

    ( _header->_flags ) &= 0xDF;
    ( _header->_flags ) |= ( ( ( value ) << 5 ) & 0x20 );
}

void rtp_header_add_flag_extension ( rtp_header_t* _header, uint32_t value )
{
    if ( value > 0 ) {
        value = 1; /* It can only be 1 */
    }

    ( _header->_flags ) &= 0xEF;
    ( _header->_flags ) |= ( ( ( value ) << 4 ) & 0x10 );
}

void rtp_header_add_flag_CSRC_count ( rtp_header_t* _header, uint32_t value )
{
    ( _header->_flags ) &= 0xF0;
    ( _header->_flags ) |= ( ( value ) & 0x0F );
}

void rtp_header_add_setting_marker ( rtp_header_t* _header, uint32_t value )
{
    if ( value > 1 )
        value = 1;

    ( _header->_marker_payload_t ) &= 0x7F;
    ( _header->_marker_payload_t ) |= ( ( ( value ) << 7 ) /*& 0x80 */ );
}

void rtp_header_add_setting_payload ( rtp_header_t* _header, uint32_t value )
{
    if ( value > 127 )
        value = 127; /* Well set to maximum */

    ( _header->_marker_payload_t ) &= 0x80;
    ( _header->_marker_payload_t ) |= ( ( value ) /* & 0x7F */ );
}

/* Getting values from flags */
uint8_t rtp_header_get_flag_version ( const rtp_header_t* _header )
{
    return ( _header->_flags & 0xd0 ) >> 6;
}

uint8_t rtp_header_get_flag_padding ( const rtp_header_t* _header )
{
    return ( _header->_flags & 0x20 ) >> 5;
}

uint8_t rtp_header_get_flag_extension ( const rtp_header_t* _header )
{
    return ( _header->_flags & 0x10 ) >> 4;
}

uint8_t rtp_header_get_flag_CSRC_count ( const rtp_header_t* _header )
{
    return ( _header->_flags & 0x0f );
}
uint8_t rtp_header_get_setting_marker ( const rtp_header_t* _header )
{
    return ( _header->_marker_payload_t ) >> 7;
}
uint8_t rtp_header_get_setting_payload_type ( const rtp_header_t* _header )
{
    /*
       uint8_t _retu;

       if ( _header->_marker_payload_t >> 7 == 1 ) {
           _header->_marker_payload_t ^= 0x80;
           _retu = _header->_marker_payload_t;
           _header->_marker_payload_t ^= 0x80;
       } else {
           _retu = _header->_marker_payload_t;
       }
    */
    /* return to start value
    return _retu; */
    return _header->_marker_payload_t & 0x7f;
}

/*  */