~ubuntu-branches/ubuntu/utopic/sflphone/utopic-proposed

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjsip/src/pjsip/sip_util_statefull.c

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2013-06-30 11:40:56 UTC
  • mfrom: (4.1.18 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130630114056-0np50jkyqo6vnmii
Tags: 1.2.3-2
* changeset_r92d62cfc54732bbbcfff2b1d36c096b120b981a5.diff 
  - fixes automatic endian detection 
* Update Vcs: fixes vcs-field-not-canonical

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: sip_util_statefull.c 3553 2011-05-05 06:14:19Z nanang $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
 
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19
 
 */
20
 
#include <pjsip/sip_util.h>
21
 
#include <pjsip/sip_module.h>
22
 
#include <pjsip/sip_endpoint.h>
23
 
#include <pjsip/sip_transaction.h>
24
 
#include <pjsip/sip_event.h>
25
 
#include <pjsip/sip_errno.h>
26
 
#include <pj/assert.h>
27
 
#include <pj/log.h>
28
 
#include <pj/pool.h>
29
 
#include <pj/string.h>
30
 
 
31
 
struct tsx_data
32
 
{
33
 
    void *token;
34
 
    void (*cb)(void*, pjsip_event*);
35
 
};
36
 
 
37
 
static void mod_util_on_tsx_state(pjsip_transaction*, pjsip_event*);
38
 
 
39
 
/* This module will be registered in pjsip_endpt.c */
40
 
 
41
 
pjsip_module mod_stateful_util = 
42
 
{
43
 
    NULL, NULL,                     /* prev, next.                      */
44
 
    { "mod-stateful-util", 17 },    /* Name.                            */
45
 
    -1,                             /* Id                               */
46
 
    PJSIP_MOD_PRIORITY_APPLICATION, /* Priority                         */
47
 
    NULL,                           /* load()                           */
48
 
    NULL,                           /* start()                          */
49
 
    NULL,                           /* stop()                           */
50
 
    NULL,                           /* unload()                         */
51
 
    NULL,                           /* on_rx_request()                  */
52
 
    NULL,                           /* on_rx_response()                 */
53
 
    NULL,                           /* on_tx_request.                   */
54
 
    NULL,                           /* on_tx_response()                 */
55
 
    &mod_util_on_tsx_state,         /* on_tsx_state()                   */
56
 
};
57
 
 
58
 
static void mod_util_on_tsx_state(pjsip_transaction *tsx, pjsip_event *event)
59
 
{
60
 
    struct tsx_data *tsx_data;
61
 
 
62
 
    if (event->type != PJSIP_EVENT_TSX_STATE)
63
 
        return;
64
 
 
65
 
    tsx_data = (struct tsx_data*) tsx->mod_data[mod_stateful_util.id];
66
 
    if (tsx_data == NULL)
67
 
        return;
68
 
 
69
 
    if (tsx->status_code < 200)
70
 
        return;
71
 
 
72
 
    /* Call the callback, if any, and prevent the callback to be called again
73
 
     * by clearing the transaction's module_data.
74
 
     */
75
 
    tsx->mod_data[mod_stateful_util.id] = NULL;
76
 
 
77
 
    if (tsx_data->cb) {
78
 
        (*tsx_data->cb)(tsx_data->token, event);
79
 
    }
80
 
}
81
 
 
82
 
 
83
 
PJ_DEF(pj_status_t) pjsip_endpt_send_request(  pjsip_endpoint *endpt,
84
 
                                               pjsip_tx_data *tdata,
85
 
                                               pj_int32_t timeout,
86
 
                                               void *token,
87
 
                                               pjsip_endpt_send_callback cb)
88
 
{
89
 
    pjsip_transaction *tsx;
90
 
    struct tsx_data *tsx_data;
91
 
    pj_status_t status;
92
 
 
93
 
    PJ_ASSERT_RETURN(endpt && tdata && (timeout==-1 || timeout>0), PJ_EINVAL);
94
 
 
95
 
    /* Check that transaction layer module is registered to endpoint */
96
 
    PJ_ASSERT_RETURN(mod_stateful_util.id != -1, PJ_EINVALIDOP);
97
 
 
98
 
    PJ_UNUSED_ARG(timeout);
99
 
 
100
 
    status = pjsip_tsx_create_uac(&mod_stateful_util, tdata, &tsx);
101
 
    if (status != PJ_SUCCESS) {
102
 
        pjsip_tx_data_dec_ref(tdata);
103
 
        return status;
104
 
    }
105
 
 
106
 
    pjsip_tsx_set_transport(tsx, &tdata->tp_sel);
107
 
 
108
 
    tsx_data = PJ_POOL_ALLOC_T(tsx->pool, struct tsx_data);
109
 
    tsx_data->token = token;
110
 
    tsx_data->cb = cb;
111
 
 
112
 
    tsx->mod_data[mod_stateful_util.id] = tsx_data;
113
 
 
114
 
    status = pjsip_tsx_send_msg(tsx, NULL);
115
 
    if (status != PJ_SUCCESS)
116
 
        pjsip_tx_data_dec_ref(tdata);
117
 
 
118
 
    return status;
119
 
}
120
 
 
121
 
 
122
 
/*
123
 
 * Send response statefully.
124
 
 */
125
 
PJ_DEF(pj_status_t) pjsip_endpt_respond(  pjsip_endpoint *endpt,
126
 
                                          pjsip_module *tsx_user,
127
 
                                          pjsip_rx_data *rdata,
128
 
                                          int st_code,
129
 
                                          const pj_str_t *st_text,
130
 
                                          const pjsip_hdr *hdr_list,
131
 
                                          const pjsip_msg_body *body,
132
 
                                          pjsip_transaction **p_tsx )
133
 
{
134
 
    pj_status_t status;
135
 
    pjsip_tx_data *tdata;
136
 
    pjsip_transaction *tsx;
137
 
 
138
 
    /* Validate arguments. */
139
 
    PJ_ASSERT_RETURN(endpt && rdata, PJ_EINVAL);
140
 
 
141
 
    if (p_tsx) *p_tsx = NULL;
142
 
 
143
 
    /* Create response message */
144
 
    status = pjsip_endpt_create_response( endpt, rdata, st_code, st_text, 
145
 
                                          &tdata);
146
 
    if (status != PJ_SUCCESS)
147
 
        return status;
148
 
 
149
 
    /* Add the message headers, if any */
150
 
    if (hdr_list) {
151
 
        const pjsip_hdr *hdr = hdr_list->next;
152
 
        while (hdr != hdr_list) {
153
 
            pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
154
 
                              pjsip_hdr_clone(tdata->pool, hdr) );
155
 
            hdr = hdr->next;
156
 
        }
157
 
    }
158
 
 
159
 
    /* Add the message body, if any. */
160
 
    if (body) {
161
 
        tdata->msg->body = pjsip_msg_body_clone( tdata->pool, body );
162
 
        if (tdata->msg->body == NULL) {
163
 
            pjsip_tx_data_dec_ref(tdata);
164
 
            return status;
165
 
        }
166
 
    }
167
 
 
168
 
    /* Create UAS transaction. */
169
 
    status = pjsip_tsx_create_uas(tsx_user, rdata, &tsx);
170
 
    if (status != PJ_SUCCESS) {
171
 
        pjsip_tx_data_dec_ref(tdata);
172
 
        return status;
173
 
    }
174
 
 
175
 
    /* Feed the request to the transaction. */
176
 
    pjsip_tsx_recv_msg(tsx, rdata);
177
 
 
178
 
    /* Send the message. */
179
 
    status = pjsip_tsx_send_msg(tsx, tdata);
180
 
    if (status != PJ_SUCCESS) {
181
 
        pjsip_tx_data_dec_ref(tdata);
182
 
    } else if (p_tsx) {
183
 
        *p_tsx = tsx;
184
 
    }
185
 
 
186
 
    return status;
187
 
}
188
 
 
189