summaryrefslogtreecommitdiffstats
path: root/bindings/_common/SipMessage.cxx
blob: 2f0000bdcba6b3d4b9e68d060174cebb65fe34d1 (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
352
353
354
355
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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.
*
* DOUBANGO 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 DOUBANGO.
*
*/
#include "SipMessage.h"


SdpMessage::SdpMessage()
    :m_pSdpMessage(tsk_null)
{
}

SdpMessage::SdpMessage(tsdp_message_t *_sdpmessage)
{
    m_pSdpMessage = (tsdp_message_t *)tsk_object_ref(_sdpmessage);
}

SdpMessage::~SdpMessage()
{
    TSK_OBJECT_SAFE_FREE(m_pSdpMessage);
}

char* SdpMessage::getSdpHeaderValue(const char* media, char name, unsigned index /*= 0*/)
{
    const tsdp_header_M_t* M;

    if((M = (const tsdp_header_M_t*)tsdp_message_get_header(m_pSdpMessage, tsdp_htype_M))) {
        tsdp_header_type_t type = tsdp_htype_Dummy;
        const tsdp_header_t* header;
        switch(name) {
        case 'a':
            type = tsdp_htype_A;
            break;
        case 'b':
            type = tsdp_htype_B;
            break;
        case 'c':
            type = tsdp_htype_C;
            break;
        case 'e':
            type = tsdp_htype_E;
            break;
        case 'i':
            type = tsdp_htype_I;
            break;
        case 'k':
            type = tsdp_htype_K;
            break;
        case 'm':
            type = tsdp_htype_M;
            break;
        case 'o':
            type = tsdp_htype_O;
            break;


        case 'p':
            type = tsdp_htype_P;
            break;
        case 'r':
            type = tsdp_htype_R;
            break;
        case 's':
            type = tsdp_htype_S;
            break;
        case 't':
            type = tsdp_htype_T;
            break;
        case 'u':
            type = tsdp_htype_U;
            break;
        case 'v':
            type = tsdp_htype_V;
            break;
        case 'z':
            type = tsdp_htype_Z;
            break;
        }

        if((header = tsdp_message_get_headerAt(m_pSdpMessage, type, index))) {
            return tsdp_header_tostring(header);
        }
    }

    return tsk_null;
}

char* SdpMessage::getSdpHeaderAValue(const char* media, const char* attributeName)
{
    const tsdp_header_M_t* M;
    tsk_size_t i;

    for(i = 0; (M = (const tsdp_header_M_t*)tsdp_message_get_headerAt(m_pSdpMessage, tsdp_htype_M, i)); i++) {
        if(tsk_striequals(M->media, media)) {
            const tsdp_header_A_t* A;
            if((A = tsdp_header_M_findA(M, attributeName))) {
                return tsk_strdup(A->value);
            }
        }
    }

    return tsk_null;
}


SipMessage::SipMessage()
    :m_pSipMessage(tsk_null), m_pSdpMessage(tsk_null)
{
}

SipMessage::SipMessage(tsip_message_t *_sipmessage)
    : m_pSdpMessage(tsk_null)
{
    m_pSipMessage = (tsip_message_t *)tsk_object_ref(_sipmessage);
}

SipMessage::~SipMessage()
{
    TSK_OBJECT_SAFE_FREE(m_pSipMessage);
    if(m_pSdpMessage) {
        delete m_pSdpMessage;
    }
}

bool SipMessage::isResponse()
{
    return TSIP_MESSAGE_IS_RESPONSE(m_pSipMessage);
}

tsip_request_type_t SipMessage::getRequestType()
{
    if(TSIP_MESSAGE_IS_REQUEST(m_pSipMessage)) {
        return (m_pSipMessage)->line.request.request_type;
    }
    return tsip_NONE;
}

short SipMessage::getResponseCode()
{
    return TSIP_RESPONSE_CODE(m_pSipMessage);
}

const char* SipMessage::getResponsePhrase()
{
    return TSIP_RESPONSE_PHRASE(m_pSipMessage);
}

const tsip_header_t* SipMessage::getSipHeader(const char* name, unsigned index /* =0 */)
{
    /* Do not worry about calling tsk_striequals() several times because the function
    * is fully optimized.
    */
    /* Code below comes from tsip_message_get_headerAt() */
    tsk_size_t pos = 0;
    const tsk_list_item_t *item;
    const tsip_header_t* hdr = tsk_null;
    if(!m_pSipMessage || !name) {
        return tsk_null;
    }

    if(tsk_striequals(name, "v") || tsk_striequals(name, "via")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->firstVia;
            goto bail;
        }
        else {
            pos++;
        }
    }
    if(tsk_striequals(name, "f") || tsk_striequals(name, "from")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->From;
            goto bail;
        }
        else {
            pos++;
        }
    }
    if(tsk_striequals(name, "t") || tsk_striequals(name, "to")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->To;
            goto bail;
        }
        else {
            pos++;
        }
    }
    if(tsk_striequals(name, "m") || tsk_striequals(name, "contact")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->Contact;
            goto bail;
        }
        else {
            pos++;
        }
    }
    if(tsk_striequals(name, "i") || tsk_striequals(name, "call-id")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->Call_ID;
            goto bail;
        }
        else {
            pos++;
        }
    }
    if(tsk_striequals(name, "cseq")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->CSeq;
            goto bail;
        }
        else {
            pos++;
        }
    }
    if(tsk_striequals(name, "expires")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->Expires;
            goto bail;
        }
        else {
            pos++;
        }
    }
    if(tsk_striequals(name, "c") || tsk_striequals(name, "content-type")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->Content_Type;
            goto bail;
        }
        else {
            pos++;
        }
    }
    if(tsk_striequals(name, "l") || tsk_striequals(name, "content-length")) {
        if(index == 0) {
            hdr = (const tsip_header_t*)m_pSipMessage->Content_Length;
            goto bail;
        }
        else {
            pos++;
        }
    }


    tsk_list_foreach(item, m_pSipMessage->headers) {
        if(tsk_striequals(tsip_header_get_name_2(TSIP_HEADER(item->data)), name)) {
            if(pos++ >= index) {
                hdr = (const tsip_header_t*)item->data;
                break;
            }
        }
    }


bail:
    return hdr;
}

// e.g. getHeaderParamValue("content-type");
char* SipMessage::getSipHeaderValue(const char* name, unsigned index /* = 0*/)
{
    const tsip_header_t* header;
    if((header = this->getSipHeader(name, index))) {

        switch(header->type) {
        case tsip_htype_From:
            return tsip_uri_tostring(((const tsip_header_From_t*)header)->uri, tsk_false, tsk_false);
        case tsip_htype_To:
            return tsip_uri_tostring(((const tsip_header_To_t*)header)->uri, tsk_false, tsk_false);
            break;
        case tsip_htype_P_Asserted_Identity:
            return tsip_uri_tostring(((const tsip_header_P_Asserted_Identity_t*)header)->uri, tsk_false, tsk_false);
            break;

        default:
            return tsip_header_value_tostring(header);
        }
    }
    // SWIG: %newobject getHeaderValueAt;
    return tsk_null;
}

// e.g. getHeaderParamValue("content-type", "charset");
char* SipMessage::getSipHeaderParamValue(const char* name, const char* param, unsigned index /*=0*/)
{
    const tsip_header_t* header;

    if((header = this->getSipHeader(name, index))) {
        return tsip_header_get_param_value(header, param);
    }

    // SWIG: %newobject getSipHeaderParamValue;
    return tsk_null;
}

/** Returns the content length.
*/
unsigned SipMessage::getSipContentLength()
{
    return TSIP_MESSAGE_CONTENT_DATA_LENGTH(m_pSipMessage);
}

/** Gets the message content
* @param output A pointer to the output buffer where to copy the data. MUST
* be allocated by the caller.
* @param maxsize The maximum number of octets to copy. Should be less than the size of the
* @a output buffer. You can use @a getSipContentLength() to get the right value to use.
* @retval The number of octet copied in the @a output buffer.
*/
unsigned SipMessage::getSipContent(void* output, unsigned maxsize)
{
    unsigned retsize = 0;
    if(output && maxsize && TSIP_MESSAGE_HAS_CONTENT(m_pSipMessage)) {
        retsize = (m_pSipMessage->Content->size > maxsize) ? maxsize : m_pSipMessage->Content->size;
        memcpy(output, m_pSipMessage->Content->data, retsize);
    }
    return retsize;
}

const void* SipMessage::getSipContentPtr()
{
    if(m_pSipMessage && m_pSipMessage->Content) {
        return m_pSipMessage->Content->data;
    }
    return tsk_null;
}

const SdpMessage* SipMessage::getSdpMessage()
{
    if(!m_pSdpMessage && TSIP_MESSAGE_HAS_CONTENT(m_pSipMessage)) {
        tsdp_message_t* sdp = tsdp_message_parse(m_pSipMessage->Content->data, m_pSipMessage->Content->size);
        if(sdp) {
            m_pSdpMessage = new SdpMessage(sdp);
            TSK_OBJECT_SAFE_FREE(sdp);
        }
    }

    return m_pSdpMessage;
}
OpenPOWER on IntegriCloud