1
/* $Id: sip_util_statefull.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
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>
29
#include <pj/string.h>
34
void (*cb)(void*, pjsip_event*);
37
static void mod_util_on_tsx_state(pjsip_transaction*, pjsip_event*);
39
/* This module will be registered in pjsip_endpt.c */
41
pjsip_module mod_stateful_util =
43
NULL, NULL, /* prev, next. */
44
{ "mod-stateful-util", 17 }, /* Name. */
46
PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
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() */
58
static void mod_util_on_tsx_state(pjsip_transaction *tsx, pjsip_event *event)
60
struct tsx_data *tsx_data;
62
if (event->type != PJSIP_EVENT_TSX_STATE)
65
tsx_data = (struct tsx_data*) tsx->mod_data[mod_stateful_util.id];
69
if (tsx->status_code < 200)
72
/* Call the callback, if any, and prevent the callback to be called again
73
* by clearing the transaction's module_data.
75
tsx->mod_data[mod_stateful_util.id] = NULL;
78
(*tsx_data->cb)(tsx_data->token, event);
83
PJ_DEF(pj_status_t) pjsip_endpt_send_request( pjsip_endpoint *endpt,
87
pjsip_endpt_send_callback cb)
89
pjsip_transaction *tsx;
90
struct tsx_data *tsx_data;
93
PJ_ASSERT_RETURN(endpt && tdata && (timeout==-1 || timeout>0), PJ_EINVAL);
95
/* Check that transaction layer module is registered to endpoint */
96
PJ_ASSERT_RETURN(mod_stateful_util.id != -1, PJ_EINVALIDOP);
98
PJ_UNUSED_ARG(timeout);
100
status = pjsip_tsx_create_uac(&mod_stateful_util, tdata, &tsx);
101
if (status != PJ_SUCCESS) {
102
pjsip_tx_data_dec_ref(tdata);
106
pjsip_tsx_set_transport(tsx, &tdata->tp_sel);
108
tsx_data = PJ_POOL_ALLOC_T(tsx->pool, struct tsx_data);
109
tsx_data->token = token;
112
tsx->mod_data[mod_stateful_util.id] = tsx_data;
114
status = pjsip_tsx_send_msg(tsx, NULL);
115
if (status != PJ_SUCCESS)
116
pjsip_tx_data_dec_ref(tdata);
123
* Send response statefully.
125
PJ_DEF(pj_status_t) pjsip_endpt_respond( pjsip_endpoint *endpt,
126
pjsip_module *tsx_user,
127
pjsip_rx_data *rdata,
129
const pj_str_t *st_text,
130
const pjsip_hdr *hdr_list,
131
const pjsip_msg_body *body,
132
pjsip_transaction **p_tsx )
135
pjsip_tx_data *tdata;
136
pjsip_transaction *tsx;
138
/* Validate arguments. */
139
PJ_ASSERT_RETURN(endpt && rdata, PJ_EINVAL);
141
if (p_tsx) *p_tsx = NULL;
143
/* Create response message */
144
status = pjsip_endpt_create_response( endpt, rdata, st_code, st_text,
146
if (status != PJ_SUCCESS)
149
/* Add the message headers, if any */
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) );
159
/* Add the message body, if any. */
161
tdata->msg->body = pjsip_msg_body_clone( tdata->pool, body );
162
if (tdata->msg->body == NULL) {
163
pjsip_tx_data_dec_ref(tdata);
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);
175
/* Feed the request to the transaction. */
176
pjsip_tsx_recv_msg(tsx, rdata);
178
/* Send the message. */
179
status = pjsip_tsx_send_msg(tsx, tdata);
180
if (status != PJ_SUCCESS) {
181
pjsip_tx_data_dec_ref(tdata);