Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 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-ua/sip_inv.h> |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 21 | #include <pjsip-ua/sip_100rel.h> |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 22 | #include <pjsip-ua/sip_timer.h> |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 23 | #include <pjsip/sip_module.h> |
| 24 | #include <pjsip/sip_endpoint.h> |
| 25 | #include <pjsip/sip_event.h> |
| 26 | #include <pjsip/sip_transaction.h> |
| 27 | #include <pjmedia/sdp.h> |
| 28 | #include <pjmedia/sdp_neg.h> |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 29 | #include <pjmedia/errno.h> |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 30 | #include <pj/string.h> |
| 31 | #include <pj/pool.h> |
| 32 | #include <pj/assert.h> |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 33 | #include <pj/os.h> |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 34 | #include <pj/log.h> |
| 35 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 36 | /* |
| 37 | * Note on offer/answer: |
| 38 | * |
| 39 | * The offer/answer framework in this implementation assumes the occurence |
| 40 | * of SDP in a particular request/response according to this table: |
| 41 | |
| 42 | offer answer Note: |
| 43 | ======================================================================== |
| 44 | INVITE X INVITE may contain offer |
| 45 | 18x/INVITE X X Response may contain offer or answer |
| 46 | 2xx/INVITE X X Response may contain offer or answer |
| 47 | ACK X ACK may contain answer |
| 48 | |
| 49 | PRACK X PRACK can only contain answer |
| 50 | 2xx/PRACK Response may not have offer nor answer |
| 51 | |
| 52 | UPDATE X UPDATE may only contain offer |
| 53 | 2xx/UPDATE X Response may only contain answer |
| 54 | ======================================================================== |
| 55 | |
| 56 | * |
| 57 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 58 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 59 | #define THIS_FILE "sip_inv.c" |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 60 | |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 61 | static const char *inv_state_names[] = |
| 62 | { |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 63 | "NULL", |
| 64 | "CALLING", |
| 65 | "INCOMING", |
| 66 | "EARLY", |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 67 | "CONNECTING", |
Benny Prijono | c505570 | 2007-01-13 23:20:18 +0000 | [diff] [blame] | 68 | "CONFIRMED", |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 69 | "DISCONNCTD", |
| 70 | "TERMINATED", |
| 71 | }; |
| 72 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 73 | /* UPDATE method */ |
Nanang Izzuddin | 9b93f86 | 2009-08-11 18:21:13 +0000 | [diff] [blame] | 74 | static const pjsip_method pjsip_update_method = |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 75 | { |
| 76 | PJSIP_OTHER_METHOD, |
| 77 | { "UPDATE", 6 } |
| 78 | }; |
| 79 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 80 | #define POOL_INIT_SIZE 256 |
| 81 | #define POOL_INC_SIZE 256 |
| 82 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 83 | /* |
| 84 | * Static prototypes. |
| 85 | */ |
| 86 | static pj_status_t mod_inv_load(pjsip_endpoint *endpt); |
| 87 | static pj_status_t mod_inv_unload(void); |
| 88 | static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata); |
| 89 | static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata); |
| 90 | static void mod_inv_on_tsx_state(pjsip_transaction*, pjsip_event*); |
| 91 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 92 | static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e); |
| 93 | static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e); |
| 94 | static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e); |
| 95 | static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e); |
| 96 | static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e); |
| 97 | static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e); |
| 98 | static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 99 | |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 100 | static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv, |
| 101 | pjsip_transaction *tsx, |
| 102 | pjsip_rx_data *rdata); |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 103 | static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv ); |
| 104 | static pjsip_msg_body *create_sdp_body(pj_pool_t *pool, |
| 105 | const pjmedia_sdp_session *c_sdp); |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 106 | static pj_status_t process_answer( pjsip_inv_session *inv, |
| 107 | int st_code, |
| 108 | pjsip_tx_data *tdata, |
| 109 | const pjmedia_sdp_session *local_sdp); |
| 110 | |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 111 | static pj_status_t handle_timer_response(pjsip_inv_session *inv, |
| 112 | const pjsip_rx_data *rdata, |
| 113 | pj_bool_t end_sess_on_failure); |
| 114 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 115 | static void (*inv_state_handler[])( pjsip_inv_session *inv, pjsip_event *e) = |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 116 | { |
| 117 | &inv_on_state_null, |
| 118 | &inv_on_state_calling, |
| 119 | &inv_on_state_incoming, |
| 120 | &inv_on_state_early, |
| 121 | &inv_on_state_connecting, |
| 122 | &inv_on_state_confirmed, |
| 123 | &inv_on_state_disconnected, |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static struct mod_inv |
| 127 | { |
| 128 | pjsip_module mod; |
| 129 | pjsip_endpoint *endpt; |
| 130 | pjsip_inv_callback cb; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 131 | } mod_inv = |
| 132 | { |
| 133 | { |
Benny Prijono | 2f8992b | 2006-02-25 21:16:36 +0000 | [diff] [blame] | 134 | NULL, NULL, /* prev, next. */ |
| 135 | { "mod-invite", 10 }, /* Name. */ |
| 136 | -1, /* Id */ |
| 137 | PJSIP_MOD_PRIORITY_DIALOG_USAGE, /* Priority */ |
| 138 | &mod_inv_load, /* load() */ |
| 139 | NULL, /* start() */ |
| 140 | NULL, /* stop() */ |
| 141 | &mod_inv_unload, /* unload() */ |
| 142 | &mod_inv_on_rx_request, /* on_rx_request() */ |
| 143 | &mod_inv_on_rx_response, /* on_rx_response() */ |
| 144 | NULL, /* on_tx_request. */ |
| 145 | NULL, /* on_tx_response() */ |
| 146 | &mod_inv_on_tsx_state, /* on_tsx_state() */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 147 | } |
| 148 | }; |
| 149 | |
| 150 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 151 | /* Invite session data to be attached to transaction. */ |
| 152 | struct tsx_inv_data |
| 153 | { |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 154 | pjsip_inv_session *inv; /* The invite session */ |
| 155 | pj_bool_t sdp_done; /* SDP negotiation done for this tsx? */ |
| 156 | pj_str_t done_tag; /* To tag in RX response with answer */ |
| 157 | pj_bool_t done_early;/* Negotiation was done for early med? */ |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 160 | /* |
| 161 | * Module load() |
| 162 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 163 | static pj_status_t mod_inv_load(pjsip_endpoint *endpt) |
| 164 | { |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 165 | pj_str_t allowed[] = {{"INVITE", 6}, {"ACK",3}, {"BYE",3}, {"CANCEL",6}, |
| 166 | { "UPDATE", 6}}; |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 167 | pj_str_t accepted = { "application/sdp", 15 }; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 168 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 169 | /* Register supported methods: INVITE, ACK, BYE, CANCEL, UPDATE */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 170 | pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ALLOW, NULL, |
| 171 | PJ_ARRAY_SIZE(allowed), allowed); |
| 172 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 173 | /* Register "application/sdp" in Accept header */ |
| 174 | pjsip_endpt_add_capability(endpt, &mod_inv.mod, PJSIP_H_ACCEPT, NULL, |
| 175 | 1, &accepted); |
| 176 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 177 | return PJ_SUCCESS; |
| 178 | } |
| 179 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Module unload() |
| 182 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 183 | static pj_status_t mod_inv_unload(void) |
| 184 | { |
| 185 | /* Should remove capability here */ |
| 186 | return PJ_SUCCESS; |
| 187 | } |
| 188 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 189 | /* |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 190 | * Set session state. |
| 191 | */ |
| 192 | void inv_set_state(pjsip_inv_session *inv, pjsip_inv_state state, |
| 193 | pjsip_event *e) |
| 194 | { |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 195 | pjsip_inv_state prev_state = inv->state; |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 196 | pj_status_t status; |
| 197 | |
| 198 | |
| 199 | /* If state is confirmed, check that SDP negotiation is done, |
| 200 | * otherwise disconnect the session. |
| 201 | */ |
| 202 | if (state == PJSIP_INV_STATE_CONFIRMED) { |
| 203 | if (pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) { |
| 204 | pjsip_tx_data *bye; |
| 205 | |
| 206 | PJ_LOG(4,(inv->obj_name, "SDP offer/answer incomplete, ending the " |
| 207 | "session")); |
| 208 | |
| 209 | status = pjsip_inv_end_session(inv, PJSIP_SC_NOT_ACCEPTABLE, |
| 210 | NULL, &bye); |
| 211 | if (status == PJ_SUCCESS && bye) |
| 212 | status = pjsip_inv_send_msg(inv, bye); |
| 213 | |
| 214 | return; |
| 215 | } |
| 216 | } |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 217 | |
| 218 | /* Set state. */ |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 219 | inv->state = state; |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 220 | |
| 221 | /* If state is DISCONNECTED, cause code MUST have been set. */ |
| 222 | pj_assert(inv->state != PJSIP_INV_STATE_DISCONNECTED || |
| 223 | inv->cause != 0); |
| 224 | |
| 225 | /* Call on_state_changed() callback. */ |
| 226 | if (mod_inv.cb.on_state_changed && inv->notify) |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 227 | (*mod_inv.cb.on_state_changed)(inv, e); |
| 228 | |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 229 | /* Only decrement when previous state is not already DISCONNECTED */ |
| 230 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED && |
| 231 | prev_state != PJSIP_INV_STATE_DISCONNECTED) |
| 232 | { |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 233 | if (inv->last_ack) { |
| 234 | pjsip_tx_data_dec_ref(inv->last_ack); |
| 235 | inv->last_ack = NULL; |
| 236 | } |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 237 | if (inv->invite_req) { |
| 238 | pjsip_tx_data_dec_ref(inv->invite_req); |
| 239 | inv->invite_req = NULL; |
| 240 | } |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 241 | pjsip_100rel_end_session(inv); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 242 | pjsip_timer_end_session(inv); |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 243 | pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod); |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 244 | |
| 245 | /* Release the flip-flop pools */ |
| 246 | pj_pool_release(inv->pool_prov); |
Benny Prijono | bcc8dd7 | 2010-02-09 12:28:03 +0000 | [diff] [blame] | 247 | inv->pool_prov = NULL; |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 248 | pj_pool_release(inv->pool_active); |
Benny Prijono | bcc8dd7 | 2010-02-09 12:28:03 +0000 | [diff] [blame] | 249 | inv->pool_active = NULL; |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 250 | } |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
| 254 | /* |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 255 | * Set cause code. |
| 256 | */ |
| 257 | void inv_set_cause(pjsip_inv_session *inv, int cause_code, |
| 258 | const pj_str_t *cause_text) |
| 259 | { |
| 260 | if (cause_code > inv->cause) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 261 | inv->cause = (pjsip_status_code) cause_code; |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 262 | if (cause_text) |
| 263 | pj_strdup(inv->pool, &inv->cause_text, cause_text); |
| 264 | else if (cause_code/100 == 2) |
| 265 | inv->cause_text = pj_str("Normal call clearing"); |
| 266 | else |
| 267 | inv->cause_text = *pjsip_get_status_text(cause_code); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 272 | /* |
| 273 | * Check if outgoing request needs to have SDP answer. |
| 274 | * This applies for both ACK and PRACK requests. |
| 275 | */ |
Benny Prijono | 9569a0b | 2007-10-04 15:35:26 +0000 | [diff] [blame] | 276 | static const pjmedia_sdp_session *inv_has_pending_answer(pjsip_inv_session *inv, |
| 277 | pjsip_transaction *tsx) |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 278 | { |
| 279 | pjmedia_sdp_neg_state neg_state; |
Benny Prijono | 9569a0b | 2007-10-04 15:35:26 +0000 | [diff] [blame] | 280 | const pjmedia_sdp_session *sdp = NULL; |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 281 | pj_status_t status; |
| 282 | |
| 283 | /* If SDP negotiator is ready, start negotiation. */ |
| 284 | |
| 285 | /* Start nego when appropriate. */ |
| 286 | neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) : |
| 287 | PJMEDIA_SDP_NEG_STATE_NULL; |
| 288 | |
| 289 | if (neg_state == PJMEDIA_SDP_NEG_STATE_DONE) { |
| 290 | |
| 291 | /* Nothing to do */ |
| 292 | |
| 293 | } else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO && |
| 294 | pjmedia_sdp_neg_has_local_answer(inv->neg) ) |
| 295 | { |
| 296 | struct tsx_inv_data *tsx_inv_data; |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 297 | struct tsx_inv_data dummy; |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 298 | |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 299 | /* Get invite session's transaction data. |
| 300 | * Note that tsx may be NULL, for example when application sends |
| 301 | * delayed ACK request (at this time, the original INVITE |
| 302 | * transaction may have been destroyed. |
| 303 | */ |
| 304 | if (tsx) { |
| 305 | tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id]; |
| 306 | } else { |
| 307 | tsx_inv_data = &dummy; |
| 308 | pj_bzero(&dummy, sizeof(dummy)); |
| 309 | dummy.inv = inv; |
| 310 | } |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 311 | |
| 312 | status = inv_negotiate_sdp(inv); |
| 313 | if (status != PJ_SUCCESS) |
| 314 | return NULL; |
| 315 | |
| 316 | /* Mark this transaction has having SDP offer/answer done. */ |
| 317 | tsx_inv_data->sdp_done = 1; |
| 318 | |
| 319 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp); |
| 320 | |
| 321 | } else { |
| 322 | /* This remark is only valid for ACK. |
| 323 | PJ_LOG(4,(inv->dlg->obj_name, |
| 324 | "FYI, the SDP negotiator state (%s) is in a mess " |
| 325 | "when sending this ACK/PRACK request", |
| 326 | pjmedia_sdp_neg_state_str(neg_state))); |
| 327 | */ |
| 328 | } |
| 329 | |
| 330 | return sdp; |
| 331 | } |
| 332 | |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 333 | |
| 334 | /* |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 335 | * Send ACK for 2xx response. |
| 336 | */ |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 337 | static pj_status_t inv_send_ack(pjsip_inv_session *inv, pjsip_event *e) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 338 | { |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 339 | pjsip_rx_data *rdata; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 340 | pj_status_t status; |
| 341 | |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 342 | if (e->type == PJSIP_EVENT_TSX_STATE) |
| 343 | rdata = e->body.tsx_state.src.rdata; |
| 344 | else if (e->type == PJSIP_EVENT_RX_MSG) |
| 345 | rdata = e->body.rx_msg.rdata; |
| 346 | else { |
| 347 | pj_assert(!"Unsupported event type"); |
| 348 | return PJ_EBUG; |
| 349 | } |
| 350 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 351 | PJ_LOG(5,(inv->obj_name, "Received %s, sending ACK", |
| 352 | pjsip_rx_data_get_info(rdata))); |
| 353 | |
Benny Prijono | 9a04864 | 2010-02-10 08:48:27 +0000 | [diff] [blame] | 354 | /* Check if we have cached ACK request. Must not use the cached ACK |
| 355 | * if it's still marked as pending by transport (#1011) |
| 356 | */ |
| 357 | if (inv->last_ack && rdata->msg_info.cseq->cseq == inv->last_ack_cseq && |
| 358 | !inv->last_ack->is_pending) |
| 359 | { |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 360 | pjsip_tx_data_add_ref(inv->last_ack); |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 361 | |
| 362 | } else if (mod_inv.cb.on_send_ack) { |
| 363 | /* If application handles ACK transmission manually, just notify the |
| 364 | * callback |
| 365 | */ |
| 366 | PJ_LOG(5,(inv->obj_name, "Received %s, notifying application callback", |
| 367 | pjsip_rx_data_get_info(rdata))); |
| 368 | |
| 369 | (*mod_inv.cb.on_send_ack)(inv, rdata); |
| 370 | return PJ_SUCCESS; |
| 371 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 372 | } else { |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 373 | status = pjsip_inv_create_ack(inv, rdata->msg_info.cseq->cseq, |
| 374 | &inv->last_ack); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 377 | /* Send ACK */ |
| 378 | status = pjsip_dlg_send_request(inv->dlg, inv->last_ack, -1, NULL); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 379 | if (status != PJ_SUCCESS) { |
| 380 | /* Better luck next time */ |
| 381 | pj_assert(!"Unable to send ACK!"); |
| 382 | return status; |
| 383 | } |
| 384 | |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 385 | |
Benny Prijono | e6da48a | 2008-09-22 14:36:00 +0000 | [diff] [blame] | 386 | /* Set state to CONFIRMED (if we're not in CONFIRMED yet). |
| 387 | * But don't set it to CONFIRMED if we're already DISCONNECTED |
| 388 | * (this may have been a late 200/OK response. |
| 389 | */ |
| 390 | if (inv->state < PJSIP_INV_STATE_CONFIRMED) { |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 391 | inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, e); |
| 392 | } |
| 393 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 394 | return PJ_SUCCESS; |
| 395 | } |
| 396 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 397 | /* |
| 398 | * Module on_rx_request() |
| 399 | * |
| 400 | * This callback is called for these events: |
| 401 | * - endpoint receives request which was unhandled by higher priority |
| 402 | * modules (e.g. transaction layer, dialog layer). |
| 403 | * - dialog distributes incoming request to its usages. |
| 404 | */ |
| 405 | static pj_bool_t mod_inv_on_rx_request(pjsip_rx_data *rdata) |
| 406 | { |
| 407 | pjsip_method *method; |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 408 | pjsip_dialog *dlg; |
| 409 | pjsip_inv_session *inv; |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 410 | |
| 411 | /* Only wants to receive request from a dialog. */ |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 412 | dlg = pjsip_rdata_get_dlg(rdata); |
| 413 | if (dlg == NULL) |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 414 | return PJ_FALSE; |
| 415 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 416 | inv = (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id]; |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 417 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 418 | /* Report to dialog that we handle INVITE, CANCEL, BYE, ACK. |
| 419 | * If we need to send response, it will be sent in the state |
| 420 | * handlers. |
| 421 | */ |
| 422 | method = &rdata->msg_info.msg->line.req.method; |
| 423 | |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 424 | if (method->id == PJSIP_INVITE_METHOD) { |
| 425 | return PJ_TRUE; |
| 426 | } |
| 427 | |
| 428 | /* BYE and CANCEL must have existing invite session */ |
| 429 | if (method->id == PJSIP_BYE_METHOD || |
| 430 | method->id == PJSIP_CANCEL_METHOD) |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 431 | { |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 432 | if (inv == NULL) |
| 433 | return PJ_FALSE; |
| 434 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 435 | return PJ_TRUE; |
| 436 | } |
| 437 | |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 438 | /* On receipt ACK request, when state is CONNECTING, |
| 439 | * move state to CONFIRMED. |
| 440 | */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 441 | if (method->id == PJSIP_ACK_METHOD && inv) { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 442 | |
Benny Prijono | f521eb0 | 2006-08-06 23:07:25 +0000 | [diff] [blame] | 443 | /* Ignore ACK if pending INVITE transaction has not finished. */ |
| 444 | if (inv->invite_tsx && |
| 445 | inv->invite_tsx->state < PJSIP_TSX_STATE_COMPLETED) |
| 446 | { |
| 447 | return PJ_TRUE; |
| 448 | } |
| 449 | |
Benny Prijono | 5eff043 | 2006-02-09 14:14:21 +0000 | [diff] [blame] | 450 | /* Terminate INVITE transaction, if it's still present. */ |
| 451 | if (inv->invite_tsx && |
| 452 | inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED) |
| 453 | { |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 454 | /* Before we terminate INVITE transaction, process the SDP |
Benny Prijono | 59e9e95 | 2008-09-21 22:55:43 +0000 | [diff] [blame] | 455 | * in the ACK request, if any. |
| 456 | * Only do this when invite state is not already disconnected |
| 457 | * (http://trac.pjsip.org/repos/ticket/640). |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 458 | */ |
Benny Prijono | 59e9e95 | 2008-09-21 22:55:43 +0000 | [diff] [blame] | 459 | if (inv->state < PJSIP_INV_STATE_DISCONNECTED) { |
| 460 | inv_check_sdp_in_incoming_msg(inv, inv->invite_tsx, rdata); |
| 461 | } |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 462 | |
| 463 | /* Now we can terminate the INVITE transaction */ |
Benny Prijono | f521eb0 | 2006-08-06 23:07:25 +0000 | [diff] [blame] | 464 | pj_assert(inv->invite_tsx->status_code >= 200); |
Benny Prijono | 5eff043 | 2006-02-09 14:14:21 +0000 | [diff] [blame] | 465 | pjsip_tsx_terminate(inv->invite_tsx, |
| 466 | inv->invite_tsx->status_code); |
| 467 | inv->invite_tsx = NULL; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 468 | if (inv->last_answer) { |
| 469 | pjsip_tx_data_dec_ref(inv->last_answer); |
| 470 | inv->last_answer = NULL; |
| 471 | } |
Benny Prijono | 5eff043 | 2006-02-09 14:14:21 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Benny Prijono | 32ac8fe | 2006-03-02 21:16:55 +0000 | [diff] [blame] | 474 | /* On receipt of ACK, only set state to confirmed when state |
| 475 | * is CONNECTING (e.g. we don't want to set the state to confirmed |
| 476 | * when we receive ACK retransmission after sending non-2xx!) |
| 477 | */ |
| 478 | if (inv->state == PJSIP_INV_STATE_CONNECTING) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 479 | pjsip_event event; |
| 480 | |
| 481 | PJSIP_EVENT_INIT_RX_MSG(event, rdata); |
| 482 | inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, &event); |
| 483 | } |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 486 | return PJ_FALSE; |
| 487 | } |
| 488 | |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 489 | /* This function will process Session Timer headers in received |
| 490 | * 2xx or 422 response of INVITE/UPDATE request. |
| 491 | */ |
| 492 | static pj_status_t handle_timer_response(pjsip_inv_session *inv, |
| 493 | const pjsip_rx_data *rdata, |
| 494 | pj_bool_t end_sess_on_failure) |
| 495 | { |
| 496 | pjsip_status_code st_code; |
| 497 | pj_status_t status; |
| 498 | |
| 499 | status = pjsip_timer_process_resp(inv, rdata, &st_code); |
| 500 | if (status != PJ_SUCCESS && end_sess_on_failure) { |
| 501 | pjsip_tx_data *tdata; |
| 502 | pj_status_t status2; |
| 503 | |
| 504 | status2 = pjsip_inv_end_session(inv, st_code, NULL, &tdata); |
| 505 | if (tdata && status2 == PJ_SUCCESS) |
| 506 | pjsip_inv_send_msg(inv, tdata); |
| 507 | } |
| 508 | |
| 509 | return status; |
| 510 | } |
| 511 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 512 | /* |
| 513 | * Module on_rx_response(). |
| 514 | * |
| 515 | * This callback is called for these events: |
| 516 | * - dialog distributes incoming 2xx response to INVITE (outside |
| 517 | * transaction) to its usages. |
| 518 | * - endpoint distributes strayed responses. |
| 519 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 520 | static pj_bool_t mod_inv_on_rx_response(pjsip_rx_data *rdata) |
| 521 | { |
| 522 | pjsip_dialog *dlg; |
| 523 | pjsip_inv_session *inv; |
| 524 | pjsip_msg *msg = rdata->msg_info.msg; |
| 525 | |
| 526 | dlg = pjsip_rdata_get_dlg(rdata); |
| 527 | |
| 528 | /* Ignore responses outside dialog */ |
| 529 | if (dlg == NULL) |
| 530 | return PJ_FALSE; |
| 531 | |
| 532 | /* Ignore responses not belonging to invite session */ |
| 533 | inv = pjsip_dlg_get_inv_session(dlg); |
| 534 | if (inv == NULL) |
| 535 | return PJ_FALSE; |
| 536 | |
| 537 | /* This MAY be retransmission of 2xx response to INVITE. |
| 538 | * If it is, we need to send ACK. |
| 539 | */ |
| 540 | if (msg->type == PJSIP_RESPONSE_MSG && msg->line.status.code/100==2 && |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 541 | rdata->msg_info.cseq->method.id == PJSIP_INVITE_METHOD && |
| 542 | inv->invite_tsx == NULL) |
| 543 | { |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 544 | pjsip_event e; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 545 | |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 546 | PJSIP_EVENT_INIT_RX_MSG(e, rdata); |
| 547 | inv_send_ack(inv, &e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 548 | return PJ_TRUE; |
| 549 | |
| 550 | } |
| 551 | |
| 552 | /* No other processing needs to be done here. */ |
| 553 | return PJ_FALSE; |
| 554 | } |
| 555 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 556 | /* |
| 557 | * Module on_tsx_state() |
| 558 | * |
| 559 | * This callback is called by dialog framework for all transactions |
| 560 | * inside the dialog for all its dialog usages. |
| 561 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 562 | static void mod_inv_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e) |
| 563 | { |
| 564 | pjsip_dialog *dlg; |
| 565 | pjsip_inv_session *inv; |
| 566 | |
| 567 | dlg = pjsip_tsx_get_dlg(tsx); |
| 568 | if (dlg == NULL) |
| 569 | return; |
| 570 | |
| 571 | inv = pjsip_dlg_get_inv_session(dlg); |
| 572 | if (inv == NULL) |
| 573 | return; |
| 574 | |
| 575 | /* Call state handler for the invite session. */ |
| 576 | (*inv_state_handler[inv->state])(inv, e); |
| 577 | |
| 578 | /* Call on_tsx_state */ |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 579 | if (mod_inv.cb.on_tsx_state_changed && inv->notify) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 580 | (*mod_inv.cb.on_tsx_state_changed)(inv, tsx, e); |
| 581 | |
Benny Prijono | 4624994 | 2007-02-19 22:23:14 +0000 | [diff] [blame] | 582 | /* Clear invite transaction when tsx is confirmed. |
| 583 | * Previously we set invite_tsx to NULL only when transaction has |
| 584 | * terminated, but this didn't work when ACK has the same Via branch |
| 585 | * value as the INVITE (see http://www.pjsip.org/trac/ticket/113) |
| 586 | */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 587 | if (tsx->state>=PJSIP_TSX_STATE_CONFIRMED && tsx == inv->invite_tsx) { |
Benny Prijono | 4624994 | 2007-02-19 22:23:14 +0000 | [diff] [blame] | 588 | inv->invite_tsx = NULL; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 589 | if (inv->last_answer) { |
| 590 | pjsip_tx_data_dec_ref(inv->last_answer); |
| 591 | inv->last_answer = NULL; |
| 592 | } |
| 593 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 596 | |
| 597 | /* |
| 598 | * Initialize the invite module. |
| 599 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 600 | PJ_DEF(pj_status_t) pjsip_inv_usage_init( pjsip_endpoint *endpt, |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 601 | const pjsip_inv_callback *cb) |
| 602 | { |
| 603 | pj_status_t status; |
| 604 | |
| 605 | /* Check arguments. */ |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 606 | PJ_ASSERT_RETURN(endpt && cb, PJ_EINVAL); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 607 | |
| 608 | /* Some callbacks are mandatory */ |
| 609 | PJ_ASSERT_RETURN(cb->on_state_changed && cb->on_new_session, PJ_EINVAL); |
| 610 | |
| 611 | /* Check if module already registered. */ |
| 612 | PJ_ASSERT_RETURN(mod_inv.mod.id == -1, PJ_EINVALIDOP); |
| 613 | |
| 614 | /* Copy param. */ |
| 615 | pj_memcpy(&mod_inv.cb, cb, sizeof(pjsip_inv_callback)); |
| 616 | |
| 617 | mod_inv.endpt = endpt; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 618 | |
| 619 | /* Register the module. */ |
| 620 | status = pjsip_endpt_register_module(endpt, &mod_inv.mod); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 621 | if (status != PJ_SUCCESS) |
| 622 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 623 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 624 | return PJ_SUCCESS; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 627 | /* |
| 628 | * Get the instance of invite module. |
| 629 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 630 | PJ_DEF(pjsip_module*) pjsip_inv_usage_instance(void) |
| 631 | { |
| 632 | return &mod_inv.mod; |
| 633 | } |
| 634 | |
| 635 | |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 636 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 637 | /* |
| 638 | * Return the invite session for the specified dialog. |
| 639 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 640 | PJ_DEF(pjsip_inv_session*) pjsip_dlg_get_inv_session(pjsip_dialog *dlg) |
| 641 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 642 | return (pjsip_inv_session*) dlg->mod_data[mod_inv.mod.id]; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 645 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 646 | /* |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 647 | * Get INVITE state name. |
| 648 | */ |
| 649 | PJ_DEF(const char *) pjsip_inv_state_name(pjsip_inv_state state) |
| 650 | { |
| 651 | PJ_ASSERT_RETURN(state >= PJSIP_INV_STATE_NULL && |
| 652 | state <= PJSIP_INV_STATE_DISCONNECTED, |
| 653 | "??"); |
| 654 | |
| 655 | return inv_state_names[state]; |
| 656 | } |
| 657 | |
| 658 | /* |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 659 | * Create UAC invite session. |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 660 | */ |
| 661 | PJ_DEF(pj_status_t) pjsip_inv_create_uac( pjsip_dialog *dlg, |
| 662 | const pjmedia_sdp_session *local_sdp, |
| 663 | unsigned options, |
| 664 | pjsip_inv_session **p_inv) |
| 665 | { |
| 666 | pjsip_inv_session *inv; |
| 667 | pj_status_t status; |
| 668 | |
| 669 | /* Verify arguments. */ |
| 670 | PJ_ASSERT_RETURN(dlg && p_inv, PJ_EINVAL); |
| 671 | |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 672 | /* Must lock dialog first */ |
| 673 | pjsip_dlg_inc_lock(dlg); |
| 674 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 675 | /* Normalize options */ |
| 676 | if (options & PJSIP_INV_REQUIRE_100REL) |
| 677 | options |= PJSIP_INV_SUPPORT_100REL; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 678 | if (options & PJSIP_INV_REQUIRE_TIMER) |
| 679 | options |= PJSIP_INV_SUPPORT_TIMER; |
| 680 | |
| 681 | /* Create the session */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 682 | inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session); |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 683 | pj_assert(inv != NULL); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 684 | |
| 685 | inv->pool = dlg->pool; |
| 686 | inv->role = PJSIP_ROLE_UAC; |
| 687 | inv->state = PJSIP_INV_STATE_NULL; |
| 688 | inv->dlg = dlg; |
| 689 | inv->options = options; |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 690 | inv->notify = PJ_TRUE; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 691 | inv->cause = (pjsip_status_code) 0; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 692 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 693 | /* Create flip-flop pool (see ticket #877) */ |
| 694 | /* (using inv->obj_name as temporary variable for pool names */ |
| 695 | pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool); |
| 696 | inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name, |
| 697 | POOL_INIT_SIZE, POOL_INC_SIZE); |
| 698 | inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name, |
| 699 | POOL_INIT_SIZE, POOL_INC_SIZE); |
| 700 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 701 | /* Object name will use the same dialog pointer. */ |
Benny Prijono | ed811d7 | 2006-03-10 12:57:12 +0000 | [diff] [blame] | 702 | pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg); |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 703 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 704 | /* Create negotiator if local_sdp is specified. */ |
| 705 | if (local_sdp) { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 706 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 707 | local_sdp, &inv->neg); |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 708 | if (status != PJ_SUCCESS) { |
| 709 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 710 | return status; |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 711 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /* Register invite as dialog usage. */ |
| 715 | status = pjsip_dlg_add_usage(dlg, &mod_inv.mod, inv); |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 716 | if (status != PJ_SUCCESS) { |
| 717 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 718 | return status; |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 719 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 720 | |
| 721 | /* Increment dialog session */ |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 722 | pjsip_dlg_inc_session(dlg, &mod_inv.mod); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 723 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 724 | /* Create 100rel handler */ |
| 725 | pjsip_100rel_attach(inv); |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 726 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 727 | /* Done */ |
| 728 | *p_inv = inv; |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 729 | |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 730 | pjsip_dlg_dec_lock(dlg); |
| 731 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 732 | PJ_LOG(5,(inv->obj_name, "UAC invite session created for dialog %s", |
| 733 | dlg->obj_name)); |
| 734 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 735 | return PJ_SUCCESS; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Verify incoming INVITE request. |
| 740 | */ |
Benny Prijono | 87a9021 | 2008-01-23 20:29:30 +0000 | [diff] [blame] | 741 | PJ_DEF(pj_status_t) pjsip_inv_verify_request2(pjsip_rx_data *rdata, |
| 742 | unsigned *options, |
| 743 | const pjmedia_sdp_session *r_sdp, |
| 744 | const pjmedia_sdp_session *l_sdp, |
| 745 | pjsip_dialog *dlg, |
| 746 | pjsip_endpoint *endpt, |
| 747 | pjsip_tx_data **p_tdata) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 748 | { |
| 749 | pjsip_msg *msg; |
| 750 | pjsip_allow_hdr *allow; |
| 751 | pjsip_supported_hdr *sup_hdr; |
| 752 | pjsip_require_hdr *req_hdr; |
| 753 | int code = 200; |
| 754 | unsigned rem_option = 0; |
| 755 | pj_status_t status = PJ_SUCCESS; |
| 756 | pjsip_hdr res_hdr_list; |
| 757 | |
| 758 | /* Init return arguments. */ |
| 759 | if (p_tdata) *p_tdata = NULL; |
| 760 | |
| 761 | /* Verify arguments. */ |
| 762 | PJ_ASSERT_RETURN(rdata != NULL && options != NULL, PJ_EINVAL); |
| 763 | |
| 764 | /* Normalize options */ |
| 765 | if (*options & PJSIP_INV_REQUIRE_100REL) |
| 766 | *options |= PJSIP_INV_SUPPORT_100REL; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 767 | if (*options & PJSIP_INV_REQUIRE_TIMER) |
| 768 | *options |= PJSIP_INV_SUPPORT_TIMER; |
| 769 | |
| 770 | /* Get the message in rdata */ |
| 771 | msg = rdata->msg_info.msg; |
| 772 | |
| 773 | /* Must be INVITE request. */ |
| 774 | PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG && |
| 775 | msg->line.req.method.id == PJSIP_INVITE_METHOD, |
| 776 | PJ_EINVAL); |
| 777 | |
| 778 | /* If tdata is specified, then either dlg or endpt must be specified */ |
| 779 | PJ_ASSERT_RETURN((!p_tdata) || (endpt || dlg), PJ_EINVAL); |
| 780 | |
| 781 | /* Get the endpoint */ |
| 782 | endpt = endpt ? endpt : dlg->endpt; |
| 783 | |
| 784 | /* Init response header list */ |
| 785 | pj_list_init(&res_hdr_list); |
| 786 | |
Benny Prijono | 87a9021 | 2008-01-23 20:29:30 +0000 | [diff] [blame] | 787 | /* Check the request body, see if it's something that we support, |
| 788 | * only when the body hasn't been parsed before. |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 789 | */ |
Benny Prijono | 87a9021 | 2008-01-23 20:29:30 +0000 | [diff] [blame] | 790 | if (r_sdp==NULL && msg->body) { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 791 | pjsip_msg_body *body = msg->body; |
| 792 | pj_str_t str_application = {"application", 11}; |
| 793 | pj_str_t str_sdp = { "sdp", 3 }; |
| 794 | pjmedia_sdp_session *sdp; |
| 795 | |
| 796 | /* Check content type. */ |
| 797 | if (pj_stricmp(&body->content_type.type, &str_application) != 0 || |
| 798 | pj_stricmp(&body->content_type.subtype, &str_sdp) != 0) |
| 799 | { |
| 800 | /* Not "application/sdp" */ |
| 801 | code = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE; |
| 802 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 803 | |
| 804 | if (p_tdata) { |
| 805 | /* Add Accept header to response */ |
| 806 | pjsip_accept_hdr *acc; |
| 807 | |
| 808 | acc = pjsip_accept_hdr_create(rdata->tp_info.pool); |
| 809 | PJ_ASSERT_RETURN(acc, PJ_ENOMEM); |
| 810 | acc->values[acc->count++] = pj_str("application/sdp"); |
| 811 | pj_list_push_back(&res_hdr_list, acc); |
| 812 | } |
| 813 | |
| 814 | goto on_return; |
| 815 | } |
| 816 | |
| 817 | /* Parse and validate SDP */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 818 | status = pjmedia_sdp_parse(rdata->tp_info.pool, |
| 819 | (char*)body->data, body->len, &sdp); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 820 | if (status == PJ_SUCCESS) |
| 821 | status = pjmedia_sdp_validate(sdp); |
| 822 | |
| 823 | if (status != PJ_SUCCESS) { |
| 824 | /* Unparseable or invalid SDP */ |
| 825 | code = PJSIP_SC_BAD_REQUEST; |
| 826 | |
| 827 | if (p_tdata) { |
| 828 | /* Add Warning header. */ |
| 829 | pjsip_warning_hdr *w; |
| 830 | |
| 831 | w = pjsip_warning_hdr_create_from_status(rdata->tp_info.pool, |
| 832 | pjsip_endpt_name(endpt), |
| 833 | status); |
| 834 | PJ_ASSERT_RETURN(w, PJ_ENOMEM); |
| 835 | |
| 836 | pj_list_push_back(&res_hdr_list, w); |
| 837 | } |
| 838 | |
| 839 | goto on_return; |
| 840 | } |
| 841 | |
Benny Prijono | 87a9021 | 2008-01-23 20:29:30 +0000 | [diff] [blame] | 842 | r_sdp = sdp; |
| 843 | } |
| 844 | |
| 845 | if (r_sdp) { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 846 | /* Negotiate with local SDP */ |
| 847 | if (l_sdp) { |
| 848 | pjmedia_sdp_neg *neg; |
| 849 | |
| 850 | /* Local SDP must be valid! */ |
| 851 | PJ_ASSERT_RETURN((status=pjmedia_sdp_validate(l_sdp))==PJ_SUCCESS, |
| 852 | status); |
| 853 | |
| 854 | /* Create SDP negotiator */ |
| 855 | status = pjmedia_sdp_neg_create_w_remote_offer( |
Benny Prijono | 87a9021 | 2008-01-23 20:29:30 +0000 | [diff] [blame] | 856 | rdata->tp_info.pool, l_sdp, r_sdp, &neg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 857 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 858 | |
| 859 | /* Negotiate SDP */ |
| 860 | status = pjmedia_sdp_neg_negotiate(rdata->tp_info.pool, neg, 0); |
| 861 | if (status != PJ_SUCCESS) { |
| 862 | |
| 863 | /* Incompatible media */ |
| 864 | code = PJSIP_SC_NOT_ACCEPTABLE_HERE; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 865 | |
| 866 | if (p_tdata) { |
| 867 | pjsip_accept_hdr *acc; |
| 868 | pjsip_warning_hdr *w; |
| 869 | |
| 870 | /* Add Warning header. */ |
| 871 | w = pjsip_warning_hdr_create_from_status( |
| 872 | rdata->tp_info.pool, |
| 873 | pjsip_endpt_name(endpt), status); |
| 874 | PJ_ASSERT_RETURN(w, PJ_ENOMEM); |
| 875 | |
| 876 | pj_list_push_back(&res_hdr_list, w); |
| 877 | |
| 878 | /* Add Accept header to response */ |
| 879 | acc = pjsip_accept_hdr_create(rdata->tp_info.pool); |
| 880 | PJ_ASSERT_RETURN(acc, PJ_ENOMEM); |
| 881 | acc->values[acc->count++] = pj_str("application/sdp"); |
| 882 | pj_list_push_back(&res_hdr_list, acc); |
| 883 | |
| 884 | } |
| 885 | |
| 886 | goto on_return; |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | /* Check supported methods, see if peer supports UPDATE. |
| 892 | * We just assume that peer supports standard INVITE, ACK, CANCEL, and BYE |
| 893 | * implicitly by sending this INVITE. |
| 894 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 895 | allow = (pjsip_allow_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_ALLOW, NULL); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 896 | if (allow) { |
| 897 | unsigned i; |
| 898 | const pj_str_t STR_UPDATE = { "UPDATE", 6 }; |
| 899 | |
| 900 | for (i=0; i<allow->count; ++i) { |
| 901 | if (pj_stricmp(&allow->values[i], &STR_UPDATE)==0) |
| 902 | break; |
| 903 | } |
| 904 | |
| 905 | if (i != allow->count) { |
| 906 | /* UPDATE is present in Allow */ |
| 907 | rem_option |= PJSIP_INV_SUPPORT_UPDATE; |
| 908 | } |
| 909 | |
| 910 | } |
| 911 | |
| 912 | /* Check Supported header */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 913 | sup_hdr = (pjsip_supported_hdr*) |
| 914 | pjsip_msg_find_hdr(msg, PJSIP_H_SUPPORTED, NULL); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 915 | if (sup_hdr) { |
| 916 | unsigned i; |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 917 | const pj_str_t STR_100REL = { "100rel", 6}; |
| 918 | const pj_str_t STR_TIMER = { "timer", 5}; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 919 | |
| 920 | for (i=0; i<sup_hdr->count; ++i) { |
| 921 | if (pj_stricmp(&sup_hdr->values[i], &STR_100REL)==0) |
| 922 | rem_option |= PJSIP_INV_SUPPORT_100REL; |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 923 | if (pj_stricmp(&sup_hdr->values[i], &STR_TIMER)==0) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 924 | rem_option |= PJSIP_INV_SUPPORT_TIMER; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /* Check Require header */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 929 | req_hdr = (pjsip_require_hdr*) |
| 930 | pjsip_msg_find_hdr(msg, PJSIP_H_REQUIRE, NULL); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 931 | if (req_hdr) { |
| 932 | unsigned i; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 933 | const pj_str_t STR_100REL = { "100rel", 6}; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 934 | const pj_str_t STR_REPLACES = { "replaces", 8 }; |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 935 | const pj_str_t STR_TIMER = { "timer", 5 }; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 936 | unsigned unsupp_cnt = 0; |
| 937 | pj_str_t unsupp_tags[PJSIP_GENERIC_ARRAY_MAX_COUNT]; |
| 938 | |
| 939 | for (i=0; i<req_hdr->count; ++i) { |
| 940 | if ((*options & PJSIP_INV_SUPPORT_100REL) && |
| 941 | pj_stricmp(&req_hdr->values[i], &STR_100REL)==0) |
| 942 | { |
| 943 | rem_option |= PJSIP_INV_REQUIRE_100REL; |
| 944 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 945 | } else if ((*options & PJSIP_INV_SUPPORT_TIMER) && |
| 946 | pj_stricmp(&req_hdr->values[i], &STR_TIMER)==0) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 947 | { |
| 948 | rem_option |= PJSIP_INV_REQUIRE_TIMER; |
| 949 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 950 | } else if (pj_stricmp(&req_hdr->values[i], &STR_REPLACES)==0) { |
| 951 | pj_bool_t supp; |
| 952 | |
| 953 | supp = pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED, |
| 954 | NULL, &STR_REPLACES); |
| 955 | if (!supp) |
| 956 | unsupp_tags[unsupp_cnt++] = req_hdr->values[i]; |
| 957 | |
Nanang Izzuddin | 5d5a20e | 2009-08-06 16:04:20 +0000 | [diff] [blame] | 958 | } else if (!pjsip_endpt_has_capability(endpt, PJSIP_H_SUPPORTED, |
| 959 | NULL, &req_hdr->values[i])) |
| 960 | { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 961 | /* Unknown/unsupported extension tag! */ |
| 962 | unsupp_tags[unsupp_cnt++] = req_hdr->values[i]; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | /* Check if there are required tags that we don't support */ |
| 967 | if (unsupp_cnt) { |
| 968 | |
| 969 | code = PJSIP_SC_BAD_EXTENSION; |
| 970 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 971 | |
| 972 | if (p_tdata) { |
| 973 | pjsip_unsupported_hdr *unsupp_hdr; |
| 974 | const pjsip_hdr *h; |
| 975 | |
| 976 | /* Add Unsupported header. */ |
| 977 | unsupp_hdr = pjsip_unsupported_hdr_create(rdata->tp_info.pool); |
| 978 | PJ_ASSERT_RETURN(unsupp_hdr != NULL, PJ_ENOMEM); |
| 979 | |
| 980 | unsupp_hdr->count = unsupp_cnt; |
| 981 | for (i=0; i<unsupp_cnt; ++i) |
| 982 | unsupp_hdr->values[i] = unsupp_tags[i]; |
| 983 | |
| 984 | pj_list_push_back(&res_hdr_list, unsupp_hdr); |
| 985 | |
| 986 | /* Add Supported header. */ |
| 987 | h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, |
| 988 | NULL); |
| 989 | pj_assert(h); |
| 990 | if (h) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 991 | sup_hdr = (pjsip_supported_hdr*) |
| 992 | pjsip_hdr_clone(rdata->tp_info.pool, h); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 993 | pj_list_push_back(&res_hdr_list, sup_hdr); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | goto on_return; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | /* Check if there are local requirements that are not supported |
| 1002 | * by peer. |
| 1003 | */ |
| 1004 | if ( ((*options & PJSIP_INV_REQUIRE_100REL)!=0 && |
| 1005 | (rem_option & PJSIP_INV_SUPPORT_100REL)==0) || |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 1006 | ((*options & PJSIP_INV_REQUIRE_TIMER)!=0 && |
| 1007 | (rem_option & PJSIP_INV_SUPPORT_TIMER)==0)) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1008 | { |
| 1009 | code = PJSIP_SC_EXTENSION_REQUIRED; |
| 1010 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 1011 | |
| 1012 | if (p_tdata) { |
| 1013 | const pjsip_hdr *h; |
| 1014 | |
| 1015 | /* Add Require header. */ |
| 1016 | req_hdr = pjsip_require_hdr_create(rdata->tp_info.pool); |
| 1017 | PJ_ASSERT_RETURN(req_hdr != NULL, PJ_ENOMEM); |
| 1018 | |
| 1019 | if (*options & PJSIP_INV_REQUIRE_100REL) |
| 1020 | req_hdr->values[req_hdr->count++] = pj_str("100rel"); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1021 | if (*options & PJSIP_INV_REQUIRE_TIMER) |
| 1022 | req_hdr->values[req_hdr->count++] = pj_str("timer"); |
| 1023 | |
| 1024 | pj_list_push_back(&res_hdr_list, req_hdr); |
| 1025 | |
| 1026 | /* Add Supported header. */ |
| 1027 | h = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, |
| 1028 | NULL); |
| 1029 | pj_assert(h); |
| 1030 | if (h) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1031 | sup_hdr = (pjsip_supported_hdr*) |
| 1032 | pjsip_hdr_clone(rdata->tp_info.pool, h); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1033 | pj_list_push_back(&res_hdr_list, sup_hdr); |
| 1034 | } |
| 1035 | |
| 1036 | } |
| 1037 | |
| 1038 | goto on_return; |
| 1039 | } |
| 1040 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1041 | /* If remote Require something that we support, make us Require |
| 1042 | * that feature too. |
| 1043 | */ |
| 1044 | if (rem_option & PJSIP_INV_REQUIRE_100REL) { |
| 1045 | pj_assert(*options & PJSIP_INV_SUPPORT_100REL); |
| 1046 | *options |= PJSIP_INV_REQUIRE_100REL; |
| 1047 | } |
| 1048 | if (rem_option & PJSIP_INV_REQUIRE_TIMER) { |
| 1049 | pj_assert(*options & PJSIP_INV_SUPPORT_TIMER); |
| 1050 | *options |= PJSIP_INV_REQUIRE_TIMER; |
| 1051 | } |
| 1052 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1053 | on_return: |
| 1054 | |
| 1055 | /* Create response if necessary */ |
| 1056 | if (code != 200 && p_tdata) { |
| 1057 | pjsip_tx_data *tdata; |
| 1058 | const pjsip_hdr *h; |
| 1059 | |
| 1060 | if (dlg) { |
| 1061 | status = pjsip_dlg_create_response(dlg, rdata, code, NULL, |
| 1062 | &tdata); |
| 1063 | } else { |
| 1064 | status = pjsip_endpt_create_response(endpt, rdata, code, NULL, |
| 1065 | &tdata); |
| 1066 | } |
| 1067 | |
| 1068 | if (status != PJ_SUCCESS) |
| 1069 | return status; |
| 1070 | |
| 1071 | /* Add response headers. */ |
| 1072 | h = res_hdr_list.next; |
| 1073 | while (h != &res_hdr_list) { |
| 1074 | pjsip_hdr *cloned; |
| 1075 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1076 | cloned = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1077 | PJ_ASSERT_RETURN(cloned, PJ_ENOMEM); |
| 1078 | |
| 1079 | pjsip_msg_add_hdr(tdata->msg, cloned); |
| 1080 | |
| 1081 | h = h->next; |
| 1082 | } |
| 1083 | |
| 1084 | *p_tdata = tdata; |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 1085 | |
| 1086 | /* Can not return PJ_SUCCESS when response message is produced. |
| 1087 | * Ref: PROTOS test ~#2490 |
| 1088 | */ |
| 1089 | if (status == PJ_SUCCESS) |
| 1090 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 1091 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | return status; |
| 1095 | } |
| 1096 | |
Benny Prijono | 87a9021 | 2008-01-23 20:29:30 +0000 | [diff] [blame] | 1097 | |
| 1098 | /* |
| 1099 | * Verify incoming INVITE request. |
| 1100 | */ |
| 1101 | PJ_DEF(pj_status_t) pjsip_inv_verify_request( pjsip_rx_data *rdata, |
| 1102 | unsigned *options, |
| 1103 | const pjmedia_sdp_session *l_sdp, |
| 1104 | pjsip_dialog *dlg, |
| 1105 | pjsip_endpoint *endpt, |
| 1106 | pjsip_tx_data **p_tdata) |
| 1107 | { |
| 1108 | return pjsip_inv_verify_request2(rdata, options, NULL, l_sdp, dlg, |
| 1109 | endpt, p_tdata); |
| 1110 | } |
| 1111 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1112 | /* |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 1113 | * Create UAS invite session. |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1114 | */ |
| 1115 | PJ_DEF(pj_status_t) pjsip_inv_create_uas( pjsip_dialog *dlg, |
| 1116 | pjsip_rx_data *rdata, |
| 1117 | const pjmedia_sdp_session *local_sdp, |
| 1118 | unsigned options, |
| 1119 | pjsip_inv_session **p_inv) |
| 1120 | { |
| 1121 | pjsip_inv_session *inv; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1122 | struct tsx_inv_data *tsx_inv_data; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1123 | pjsip_msg *msg; |
| 1124 | pjmedia_sdp_session *rem_sdp = NULL; |
| 1125 | pj_status_t status; |
| 1126 | |
| 1127 | /* Verify arguments. */ |
| 1128 | PJ_ASSERT_RETURN(dlg && rdata && p_inv, PJ_EINVAL); |
| 1129 | |
| 1130 | /* Dialog MUST have been initialised. */ |
| 1131 | PJ_ASSERT_RETURN(pjsip_rdata_get_tsx(rdata) != NULL, PJ_EINVALIDOP); |
| 1132 | |
| 1133 | msg = rdata->msg_info.msg; |
| 1134 | |
| 1135 | /* rdata MUST contain INVITE request */ |
| 1136 | PJ_ASSERT_RETURN(msg->type == PJSIP_REQUEST_MSG && |
| 1137 | msg->line.req.method.id == PJSIP_INVITE_METHOD, |
| 1138 | PJ_EINVALIDOP); |
| 1139 | |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1140 | /* Lock dialog */ |
| 1141 | pjsip_dlg_inc_lock(dlg); |
| 1142 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1143 | /* Normalize options */ |
| 1144 | if (options & PJSIP_INV_REQUIRE_100REL) |
| 1145 | options |= PJSIP_INV_SUPPORT_100REL; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1146 | if (options & PJSIP_INV_REQUIRE_TIMER) |
| 1147 | options |= PJSIP_INV_SUPPORT_TIMER; |
| 1148 | |
| 1149 | /* Create the session */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1150 | inv = PJ_POOL_ZALLOC_T(dlg->pool, pjsip_inv_session); |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1151 | pj_assert(inv != NULL); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1152 | |
| 1153 | inv->pool = dlg->pool; |
| 1154 | inv->role = PJSIP_ROLE_UAS; |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 1155 | inv->state = PJSIP_INV_STATE_NULL; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1156 | inv->dlg = dlg; |
| 1157 | inv->options = options; |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 1158 | inv->notify = PJ_TRUE; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 1159 | inv->cause = (pjsip_status_code) 0; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1160 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1161 | /* Create flip-flop pool (see ticket #877) */ |
| 1162 | /* (using inv->obj_name as temporary variable for pool names */ |
| 1163 | pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg->pool); |
| 1164 | inv->pool_prov = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name, |
| 1165 | POOL_INIT_SIZE, POOL_INC_SIZE); |
| 1166 | inv->pool_active = pjsip_endpt_create_pool(dlg->endpt, inv->obj_name, |
| 1167 | POOL_INIT_SIZE, POOL_INC_SIZE); |
| 1168 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1169 | /* Object name will use the same dialog pointer. */ |
Benny Prijono | ed811d7 | 2006-03-10 12:57:12 +0000 | [diff] [blame] | 1170 | pj_ansi_snprintf(inv->obj_name, PJ_MAX_OBJ_NAME, "inv%p", dlg); |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1171 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1172 | /* Parse SDP in message body, if present. */ |
| 1173 | if (msg->body) { |
| 1174 | pjsip_msg_body *body = msg->body; |
| 1175 | |
| 1176 | /* Parse and validate SDP */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1177 | status = pjmedia_sdp_parse(inv->pool, (char*)body->data, body->len, |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1178 | &rem_sdp); |
| 1179 | if (status == PJ_SUCCESS) |
| 1180 | status = pjmedia_sdp_validate(rem_sdp); |
| 1181 | |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1182 | if (status != PJ_SUCCESS) { |
| 1183 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1184 | return status; |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1185 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | /* Create negotiator. */ |
| 1189 | if (rem_sdp) { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1190 | status = pjmedia_sdp_neg_create_w_remote_offer(inv->pool, |
| 1191 | local_sdp, rem_sdp, |
| 1192 | &inv->neg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1193 | |
| 1194 | } else if (local_sdp) { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1195 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 1196 | local_sdp, &inv->neg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1197 | } else { |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 1198 | status = PJ_SUCCESS; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1201 | if (status != PJ_SUCCESS) { |
| 1202 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1203 | return status; |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1204 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* Register invite as dialog usage. */ |
| 1207 | status = pjsip_dlg_add_usage(dlg, &mod_inv.mod, inv); |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1208 | if (status != PJ_SUCCESS) { |
| 1209 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1210 | return status; |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1211 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1212 | |
| 1213 | /* Increment session in the dialog. */ |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1214 | pjsip_dlg_inc_session(dlg, &mod_inv.mod); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1215 | |
| 1216 | /* Save the invite transaction. */ |
| 1217 | inv->invite_tsx = pjsip_rdata_get_tsx(rdata); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1218 | |
| 1219 | /* Attach our data to the transaction. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1220 | tsx_inv_data = PJ_POOL_ZALLOC_T(inv->invite_tsx->pool, struct tsx_inv_data); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1221 | tsx_inv_data->inv = inv; |
| 1222 | inv->invite_tsx->mod_data[mod_inv.mod.id] = tsx_inv_data; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1223 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1224 | /* Create 100rel handler */ |
| 1225 | if (inv->options & PJSIP_INV_REQUIRE_100REL) { |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1226 | pjsip_100rel_attach(inv); |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1227 | } |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1228 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1229 | /* Done */ |
Benny Prijono | 8eae838 | 2006-08-10 21:44:26 +0000 | [diff] [blame] | 1230 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1231 | *p_inv = inv; |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1232 | |
| 1233 | PJ_LOG(5,(inv->obj_name, "UAS invite session created for dialog %s", |
| 1234 | dlg->obj_name)); |
| 1235 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1236 | return PJ_SUCCESS; |
| 1237 | } |
| 1238 | |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 1239 | /* |
| 1240 | * Forcefully terminate the session. |
| 1241 | */ |
| 1242 | PJ_DEF(pj_status_t) pjsip_inv_terminate( pjsip_inv_session *inv, |
| 1243 | int st_code, |
| 1244 | pj_bool_t notify) |
| 1245 | { |
| 1246 | PJ_ASSERT_RETURN(inv, PJ_EINVAL); |
| 1247 | |
| 1248 | /* Lock dialog. */ |
| 1249 | pjsip_dlg_inc_lock(inv->dlg); |
| 1250 | |
| 1251 | /* Set callback notify flag. */ |
| 1252 | inv->notify = notify; |
| 1253 | |
| 1254 | /* If there's pending transaction, terminate the transaction. |
| 1255 | * This may subsequently set the INVITE session state to |
| 1256 | * disconnected. |
| 1257 | */ |
| 1258 | if (inv->invite_tsx && |
| 1259 | inv->invite_tsx->state <= PJSIP_TSX_STATE_COMPLETED) |
| 1260 | { |
| 1261 | pjsip_tsx_terminate(inv->invite_tsx, st_code); |
| 1262 | |
| 1263 | } |
| 1264 | |
| 1265 | /* Set cause. */ |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 1266 | inv_set_cause(inv, st_code, NULL); |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 1267 | |
| 1268 | /* Forcefully terminate the session if state is not DISCONNECTED */ |
| 1269 | if (inv->state != PJSIP_INV_STATE_DISCONNECTED) { |
| 1270 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, NULL); |
| 1271 | } |
| 1272 | |
| 1273 | /* Done. |
| 1274 | * The dec_lock() below will actually destroys the dialog if it |
| 1275 | * has no other session. |
| 1276 | */ |
| 1277 | pjsip_dlg_dec_lock(inv->dlg); |
| 1278 | |
| 1279 | return PJ_SUCCESS; |
| 1280 | } |
| 1281 | |
| 1282 | |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 1283 | /* |
| 1284 | * Restart UAC session, possibly because app or us wants to re-send the |
| 1285 | * INVITE request due to 401/407 challenge or 3xx response. |
| 1286 | */ |
| 1287 | PJ_DEF(pj_status_t) pjsip_inv_uac_restart(pjsip_inv_session *inv, |
| 1288 | pj_bool_t new_offer) |
| 1289 | { |
| 1290 | PJ_ASSERT_RETURN(inv, PJ_EINVAL); |
| 1291 | |
| 1292 | inv->state = PJSIP_INV_STATE_NULL; |
| 1293 | inv->invite_tsx = NULL; |
| 1294 | if (inv->last_answer) { |
| 1295 | pjsip_tx_data_dec_ref(inv->last_answer); |
| 1296 | inv->last_answer = NULL; |
| 1297 | } |
| 1298 | |
| 1299 | if (new_offer && inv->neg) { |
| 1300 | pjmedia_sdp_neg_state neg_state; |
| 1301 | |
| 1302 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 1303 | if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) { |
| 1304 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | return PJ_SUCCESS; |
| 1309 | } |
| 1310 | |
| 1311 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1312 | static void *clone_sdp(pj_pool_t *pool, const void *data, unsigned len) |
| 1313 | { |
| 1314 | PJ_UNUSED_ARG(len); |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1315 | return pjmedia_sdp_session_clone(pool, (const pjmedia_sdp_session*)data); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static int print_sdp(pjsip_msg_body *body, char *buf, pj_size_t len) |
| 1319 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1320 | return pjmedia_sdp_print((const pjmedia_sdp_session*)body->data, buf, len); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1323 | |
| 1324 | PJ_DEF(pj_status_t) pjsip_create_sdp_body( pj_pool_t *pool, |
| 1325 | pjmedia_sdp_session *sdp, |
| 1326 | pjsip_msg_body **p_body) |
| 1327 | { |
| 1328 | const pj_str_t STR_APPLICATION = { "application", 11}; |
| 1329 | const pj_str_t STR_SDP = { "sdp", 3 }; |
| 1330 | pjsip_msg_body *body; |
| 1331 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1332 | body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1333 | PJ_ASSERT_RETURN(body != NULL, PJ_ENOMEM); |
| 1334 | |
| 1335 | body->content_type.type = STR_APPLICATION; |
| 1336 | body->content_type.subtype = STR_SDP; |
| 1337 | body->data = sdp; |
| 1338 | body->len = 0; |
| 1339 | body->clone_data = &clone_sdp; |
| 1340 | body->print_body = &print_sdp; |
| 1341 | |
| 1342 | *p_body = body; |
| 1343 | |
| 1344 | return PJ_SUCCESS; |
| 1345 | } |
| 1346 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1347 | static pjsip_msg_body *create_sdp_body(pj_pool_t *pool, |
| 1348 | const pjmedia_sdp_session *c_sdp) |
| 1349 | { |
| 1350 | pjsip_msg_body *body; |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1351 | pj_status_t status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1352 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1353 | status = pjsip_create_sdp_body(pool, |
| 1354 | pjmedia_sdp_session_clone(pool, c_sdp), |
| 1355 | &body); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1356 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1357 | if (status != PJ_SUCCESS) |
| 1358 | return NULL; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1359 | |
| 1360 | return body; |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * Create initial INVITE request. |
| 1365 | */ |
| 1366 | PJ_DEF(pj_status_t) pjsip_inv_invite( pjsip_inv_session *inv, |
| 1367 | pjsip_tx_data **p_tdata ) |
| 1368 | { |
| 1369 | pjsip_tx_data *tdata; |
| 1370 | const pjsip_hdr *hdr; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1371 | pj_bool_t has_sdp; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1372 | pj_status_t status; |
| 1373 | |
| 1374 | /* Verify arguments. */ |
| 1375 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 1376 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1377 | /* State MUST be NULL or CONFIRMED. */ |
| 1378 | PJ_ASSERT_RETURN(inv->state == PJSIP_INV_STATE_NULL || |
| 1379 | inv->state == PJSIP_INV_STATE_CONFIRMED, |
| 1380 | PJ_EINVALIDOP); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1381 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1382 | /* Lock dialog. */ |
| 1383 | pjsip_dlg_inc_lock(inv->dlg); |
| 1384 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1385 | /* Create the INVITE request. */ |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1386 | status = pjsip_dlg_create_request(inv->dlg, pjsip_get_invite_method(), -1, |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1387 | &tdata); |
| 1388 | if (status != PJ_SUCCESS) |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1389 | goto on_return; |
| 1390 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1391 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1392 | /* If this is the first INVITE, then copy the headers from inv_hdr. |
| 1393 | * These are the headers parsed from the request URI when the |
| 1394 | * dialog was created. |
| 1395 | */ |
| 1396 | if (inv->state == PJSIP_INV_STATE_NULL) { |
| 1397 | hdr = inv->dlg->inv_hdr.next; |
| 1398 | |
| 1399 | while (hdr != &inv->dlg->inv_hdr) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1400 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1401 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 1402 | hdr = hdr->next; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | /* See if we have SDP to send. */ |
| 1407 | if (inv->neg) { |
| 1408 | pjmedia_sdp_neg_state neg_state; |
| 1409 | |
| 1410 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 1411 | |
| 1412 | has_sdp = (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER || |
| 1413 | (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO && |
| 1414 | pjmedia_sdp_neg_has_local_answer(inv->neg))); |
| 1415 | |
| 1416 | |
| 1417 | } else { |
| 1418 | has_sdp = PJ_FALSE; |
| 1419 | } |
| 1420 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1421 | /* Add SDP, if any. */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1422 | if (has_sdp) { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1423 | const pjmedia_sdp_session *offer; |
| 1424 | |
| 1425 | status = pjmedia_sdp_neg_get_neg_local(inv->neg, &offer); |
Benny Prijono | 176a11f | 2009-04-14 11:14:51 +0000 | [diff] [blame] | 1426 | if (status != PJ_SUCCESS) { |
| 1427 | pjsip_tx_data_dec_ref(tdata); |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1428 | goto on_return; |
Benny Prijono | 176a11f | 2009-04-14 11:14:51 +0000 | [diff] [blame] | 1429 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1430 | |
| 1431 | tdata->msg->body = create_sdp_body(tdata->pool, offer); |
| 1432 | } |
| 1433 | |
| 1434 | /* Add Allow header. */ |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1435 | if (inv->dlg->add_allow) { |
Benny Prijono | 95673f3 | 2007-06-26 08:23:18 +0000 | [diff] [blame] | 1436 | hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_ALLOW, NULL); |
| 1437 | if (hdr) { |
| 1438 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 1439 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 1440 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | /* Add Supported header */ |
| 1444 | hdr = pjsip_endpt_get_capability(inv->dlg->endpt, PJSIP_H_SUPPORTED, NULL); |
| 1445 | if (hdr) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1446 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1447 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 1448 | } |
| 1449 | |
| 1450 | /* Add Require header. */ |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1451 | if ((inv->options & PJSIP_INV_REQUIRE_100REL) || |
| 1452 | (inv->options & PJSIP_INV_REQUIRE_TIMER)) |
| 1453 | { |
| 1454 | pjsip_require_hdr *hreq; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1455 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1456 | hreq = pjsip_require_hdr_create(tdata->pool); |
| 1457 | |
| 1458 | if (inv->options & PJSIP_INV_REQUIRE_100REL) |
| 1459 | hreq->values[hreq->count++] = pj_str("100rel"); |
| 1460 | if (inv->options & PJSIP_INV_REQUIRE_TIMER) |
| 1461 | hreq->values[hreq->count++] = pj_str("timer"); |
| 1462 | |
| 1463 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) hreq); |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1464 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1465 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1466 | status = pjsip_timer_update_req(inv, tdata); |
| 1467 | if (status != PJ_SUCCESS) |
| 1468 | goto on_return; |
| 1469 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1470 | /* Done. */ |
| 1471 | *p_tdata = tdata; |
| 1472 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1473 | |
| 1474 | on_return: |
| 1475 | pjsip_dlg_dec_lock(inv->dlg); |
| 1476 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1480 | /* Util: swap pool */ |
| 1481 | static void swap_pool(pj_pool_t **p1, pj_pool_t **p2) |
| 1482 | { |
| 1483 | pj_pool_t *tmp = *p1; |
| 1484 | *p1 = *p2; |
| 1485 | *p2 = tmp; |
| 1486 | } |
| 1487 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1488 | /* |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 1489 | * Initiate SDP negotiation in the SDP negotiator. |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 1490 | */ |
| 1491 | static pj_status_t inv_negotiate_sdp( pjsip_inv_session *inv ) |
| 1492 | { |
| 1493 | pj_status_t status; |
| 1494 | |
| 1495 | PJ_ASSERT_RETURN(pjmedia_sdp_neg_get_state(inv->neg) == |
| 1496 | PJMEDIA_SDP_NEG_STATE_WAIT_NEGO, |
| 1497 | PJMEDIA_SDPNEG_EINSTATE); |
| 1498 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1499 | status = pjmedia_sdp_neg_negotiate(inv->pool_prov, inv->neg, 0); |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 1500 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1501 | PJ_LOG(5,(inv->obj_name, "SDP negotiation done, status=%d", status)); |
| 1502 | |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 1503 | if (mod_inv.cb.on_media_update && inv->notify) |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 1504 | (*mod_inv.cb.on_media_update)(inv, status); |
| 1505 | |
Benny Prijono | bcc8dd7 | 2010-02-09 12:28:03 +0000 | [diff] [blame] | 1506 | /* Invite session may have been terminated by the application even |
| 1507 | * after a successful SDP negotiation, for example when no audio |
| 1508 | * codec is present in the offer (see ticket #1034). |
| 1509 | */ |
| 1510 | if (inv->state != PJSIP_INV_STATE_DISCONNECTED) { |
Nanang Izzuddin | 756da44 | 2009-08-14 13:23:22 +0000 | [diff] [blame] | 1511 | |
Benny Prijono | bcc8dd7 | 2010-02-09 12:28:03 +0000 | [diff] [blame] | 1512 | /* Swap the flip-flop pool when SDP negotiation success. */ |
| 1513 | if (status == PJ_SUCCESS) { |
| 1514 | swap_pool(&inv->pool_prov, &inv->pool_active); |
| 1515 | } |
| 1516 | |
| 1517 | /* Reset the provisional pool regardless SDP negotiation result. */ |
| 1518 | pj_pool_reset(inv->pool_prov); |
| 1519 | |
| 1520 | } else { |
| 1521 | |
| 1522 | status = PJSIP_ERRNO_FROM_SIP_STATUS(inv->cause); |
| 1523 | } |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1524 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 1525 | return status; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1529 | * Check in incoming message for SDP offer/answer. |
| 1530 | */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1531 | static pj_status_t inv_check_sdp_in_incoming_msg( pjsip_inv_session *inv, |
| 1532 | pjsip_transaction *tsx, |
| 1533 | pjsip_rx_data *rdata) |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1534 | { |
| 1535 | struct tsx_inv_data *tsx_inv_data; |
| 1536 | static const pj_str_t str_application = { "application", 11 }; |
| 1537 | static const pj_str_t str_sdp = { "sdp", 3 }; |
| 1538 | pj_status_t status; |
| 1539 | pjsip_msg *msg; |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1540 | pjmedia_sdp_session *rem_sdp; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1541 | |
| 1542 | /* Check if SDP is present in the message. */ |
| 1543 | |
| 1544 | msg = rdata->msg_info.msg; |
| 1545 | if (msg->body == NULL) { |
| 1546 | /* Message doesn't have body. */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1547 | return PJ_SUCCESS; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | if (pj_stricmp(&msg->body->content_type.type, &str_application) || |
| 1551 | pj_stricmp(&msg->body->content_type.subtype, &str_sdp)) |
| 1552 | { |
| 1553 | /* Message body is not "application/sdp" */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1554 | return PJMEDIA_SDP_EINSDP; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1557 | /* Get/attach invite session's transaction data */ |
| 1558 | tsx_inv_data = (struct tsx_inv_data*) tsx->mod_data[mod_inv.mod.id]; |
| 1559 | if (tsx_inv_data == NULL) { |
| 1560 | tsx_inv_data = PJ_POOL_ZALLOC_T(tsx->pool, struct tsx_inv_data); |
| 1561 | tsx_inv_data->inv = inv; |
| 1562 | tsx->mod_data[mod_inv.mod.id] = tsx_inv_data; |
| 1563 | } |
| 1564 | |
| 1565 | /* MUST NOT do multiple SDP offer/answer in a single transaction, |
| 1566 | * EXCEPT if: |
| 1567 | * - this is an initial UAC INVITE transaction (i.e. not re-INVITE), and |
| 1568 | * - the previous negotiation was done on an early media (18x) and |
| 1569 | * this response is a final/2xx response, and |
| 1570 | * - the 2xx response has different To tag than the 18x response |
| 1571 | * (i.e. the request has forked). |
| 1572 | * |
| 1573 | * The exception above is to add a rudimentary support for early media |
| 1574 | * forking (sample case: custom ringback). See this ticket for more |
| 1575 | * info: http://trac.pjsip.org/repos/ticket/657 |
| 1576 | */ |
| 1577 | if (tsx_inv_data->sdp_done) { |
| 1578 | pj_str_t res_tag; |
| 1579 | |
| 1580 | res_tag = rdata->msg_info.to->tag; |
| 1581 | |
| 1582 | /* Allow final response after SDP has been negotiated in early |
| 1583 | * media, IF this response is a final response with different |
| 1584 | * tag. |
| 1585 | */ |
| 1586 | if (tsx->role == PJSIP_ROLE_UAC && |
| 1587 | rdata->msg_info.msg->line.status.code/100 == 2 && |
| 1588 | tsx_inv_data->done_early && |
| 1589 | pj_strcmp(&tsx_inv_data->done_tag, &res_tag)) |
| 1590 | { |
| 1591 | const pjmedia_sdp_session *reoffer_sdp = NULL; |
| 1592 | |
| 1593 | PJ_LOG(4,(inv->obj_name, "Received forked final response " |
| 1594 | "after SDP negotiation has been done in early " |
| 1595 | "media. Renegotiating SDP..")); |
| 1596 | |
| 1597 | /* Retrieve original SDP offer from INVITE request */ |
| 1598 | reoffer_sdp = (const pjmedia_sdp_session*) |
| 1599 | tsx->last_tx->msg->body->data; |
| 1600 | |
| 1601 | /* Feed the original offer to negotiator */ |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1602 | status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, |
| 1603 | inv->neg, |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1604 | reoffer_sdp); |
| 1605 | if (status != PJ_SUCCESS) { |
| 1606 | PJ_LOG(1,(inv->obj_name, "Error updating local offer for " |
| 1607 | "forked 2xx response (err=%d)", status)); |
| 1608 | return status; |
| 1609 | } |
| 1610 | |
| 1611 | } else { |
| 1612 | |
| 1613 | if (rdata->msg_info.msg->body) { |
| 1614 | PJ_LOG(4,(inv->obj_name, "SDP negotiation done, message " |
| 1615 | "body is ignored")); |
| 1616 | } |
| 1617 | return PJ_SUCCESS; |
| 1618 | } |
| 1619 | } |
| 1620 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1621 | /* Parse the SDP body. */ |
| 1622 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1623 | status = pjmedia_sdp_parse(rdata->tp_info.pool, |
| 1624 | (char*)msg->body->data, |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1625 | msg->body->len, &rem_sdp); |
Benny Prijono | c1b1c0a | 2007-11-06 08:48:02 +0000 | [diff] [blame] | 1626 | if (status == PJ_SUCCESS) |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1627 | status = pjmedia_sdp_validate(rem_sdp); |
Benny Prijono | c1b1c0a | 2007-11-06 08:48:02 +0000 | [diff] [blame] | 1628 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1629 | if (status != PJ_SUCCESS) { |
| 1630 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 1631 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 1632 | PJ_LOG(4,(THIS_FILE, "Error parsing SDP in %s: %s", |
| 1633 | pjsip_rx_data_get_info(rdata), errmsg)); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1634 | return PJMEDIA_SDP_EINSDP; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /* The SDP can be an offer or answer, depending on negotiator's state */ |
| 1638 | |
| 1639 | if (inv->neg == NULL || |
| 1640 | pjmedia_sdp_neg_get_state(inv->neg) == PJMEDIA_SDP_NEG_STATE_DONE) |
| 1641 | { |
| 1642 | |
| 1643 | /* This is an offer. */ |
| 1644 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1645 | PJ_LOG(5,(inv->obj_name, "Got SDP offer in %s", |
| 1646 | pjsip_rx_data_get_info(rdata))); |
| 1647 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1648 | if (inv->neg == NULL) { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1649 | status=pjmedia_sdp_neg_create_w_remote_offer(inv->pool, NULL, |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1650 | rem_sdp, &inv->neg); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1651 | } else { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1652 | status=pjmedia_sdp_neg_set_remote_offer(inv->pool_prov, inv->neg, |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1653 | rem_sdp); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | if (status != PJ_SUCCESS) { |
| 1657 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 1658 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 1659 | PJ_LOG(4,(THIS_FILE, "Error processing SDP offer in %s: %s", |
| 1660 | pjsip_rx_data_get_info(rdata), errmsg)); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1661 | return PJMEDIA_SDP_EINSDP; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* Inform application about remote offer. */ |
| 1665 | |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 1666 | if (mod_inv.cb.on_rx_offer && inv->notify) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1667 | |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1668 | (*mod_inv.cb.on_rx_offer)(inv, rem_sdp); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1669 | |
| 1670 | } |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1671 | |
| 1672 | } else if (pjmedia_sdp_neg_get_state(inv->neg) == |
| 1673 | PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) |
| 1674 | { |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1675 | int status_code; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1676 | |
| 1677 | /* This is an answer. |
| 1678 | * Process and negotiate remote answer. |
| 1679 | */ |
| 1680 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1681 | PJ_LOG(5,(inv->obj_name, "Got SDP answer in %s", |
| 1682 | pjsip_rx_data_get_info(rdata))); |
| 1683 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1684 | status = pjmedia_sdp_neg_set_remote_answer(inv->pool_prov, inv->neg, |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1685 | rem_sdp); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1686 | |
| 1687 | if (status != PJ_SUCCESS) { |
| 1688 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 1689 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 1690 | PJ_LOG(4,(THIS_FILE, "Error processing SDP answer in %s: %s", |
| 1691 | pjsip_rx_data_get_info(rdata), errmsg)); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1692 | return PJMEDIA_SDP_EINSDP; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | /* Negotiate SDP */ |
| 1696 | |
| 1697 | inv_negotiate_sdp(inv); |
| 1698 | |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1699 | /* Mark this transaction has having SDP offer/answer done, and |
| 1700 | * save the reference to the To tag |
| 1701 | */ |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1702 | |
| 1703 | tsx_inv_data->sdp_done = 1; |
Benny Prijono | 8fcb433 | 2008-10-31 18:01:48 +0000 | [diff] [blame] | 1704 | status_code = rdata->msg_info.msg->line.status.code; |
| 1705 | tsx_inv_data->done_early = (status_code/100==1); |
| 1706 | pj_strdup(tsx->pool, &tsx_inv_data->done_tag, |
| 1707 | &rdata->msg_info.to->tag); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1708 | |
| 1709 | } else { |
| 1710 | |
| 1711 | PJ_LOG(5,(THIS_FILE, "Ignored SDP in %s: negotiator state is %s", |
| 1712 | pjsip_rx_data_get_info(rdata), |
| 1713 | pjmedia_sdp_neg_state_str(pjmedia_sdp_neg_get_state(inv->neg)))); |
| 1714 | } |
| 1715 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1716 | return PJ_SUCCESS; |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1720 | /* |
| 1721 | * Process INVITE answer, for both initial and subsequent re-INVITE |
| 1722 | */ |
| 1723 | static pj_status_t process_answer( pjsip_inv_session *inv, |
| 1724 | int st_code, |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1725 | pjsip_tx_data *tdata, |
| 1726 | const pjmedia_sdp_session *local_sdp) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1727 | { |
| 1728 | pj_status_t status; |
Benny Prijono | ab7399b | 2006-02-27 00:40:31 +0000 | [diff] [blame] | 1729 | const pjmedia_sdp_session *sdp = NULL; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1730 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1731 | /* If local_sdp is specified, then we MUST NOT have answered the |
| 1732 | * offer before. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1733 | */ |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1734 | if (local_sdp && (st_code/100==1 || st_code/100==2)) { |
| 1735 | |
| 1736 | if (inv->neg == NULL) { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1737 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 1738 | local_sdp, |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1739 | &inv->neg); |
| 1740 | } else if (pjmedia_sdp_neg_get_state(inv->neg)== |
| 1741 | PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER) |
| 1742 | { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1743 | status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, inv->neg, |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1744 | local_sdp); |
| 1745 | } else { |
| 1746 | |
| 1747 | /* Can not specify local SDP at this state. */ |
| 1748 | pj_assert(0); |
| 1749 | status = PJMEDIA_SDPNEG_EINSTATE; |
| 1750 | } |
| 1751 | |
| 1752 | if (status != PJ_SUCCESS) |
| 1753 | return status; |
| 1754 | |
| 1755 | } |
| 1756 | |
| 1757 | |
| 1758 | /* If SDP negotiator is ready, start negotiation. */ |
| 1759 | if (st_code/100==2 || (st_code/10==18 && st_code!=180)) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1760 | |
| 1761 | pjmedia_sdp_neg_state neg_state; |
| 1762 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1763 | /* Start nego when appropriate. */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1764 | neg_state = inv->neg ? pjmedia_sdp_neg_get_state(inv->neg) : |
| 1765 | PJMEDIA_SDP_NEG_STATE_NULL; |
| 1766 | |
| 1767 | if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) { |
| 1768 | |
| 1769 | status = pjmedia_sdp_neg_get_neg_local(inv->neg, &sdp); |
| 1770 | |
| 1771 | } else if (neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO && |
| 1772 | pjmedia_sdp_neg_has_local_answer(inv->neg) ) |
| 1773 | { |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 1774 | struct tsx_inv_data *tsx_inv_data; |
| 1775 | |
| 1776 | /* Get invite session's transaction data */ |
| 1777 | tsx_inv_data = (struct tsx_inv_data*) |
| 1778 | inv->invite_tsx->mod_data[mod_inv.mod.id]; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1779 | |
| 1780 | status = inv_negotiate_sdp(inv); |
| 1781 | if (status != PJ_SUCCESS) |
| 1782 | return status; |
| 1783 | |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 1784 | /* Mark this transaction has having SDP offer/answer done. */ |
| 1785 | tsx_inv_data->sdp_done = 1; |
| 1786 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1787 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp); |
| 1788 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1791 | /* Include SDP when it's available for 2xx and 18x (but not 180) response. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1792 | * Subsequent response will include this SDP. |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 1793 | * |
| 1794 | * Note note: |
| 1795 | * - When offer/answer has been completed in reliable 183, we MUST NOT |
| 1796 | * send SDP in 2xx response. So if we don't have SDP to send, clear |
| 1797 | * the SDP in the message body ONLY if 100rel is active in this |
| 1798 | * session. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1799 | */ |
| 1800 | if (sdp) { |
| 1801 | tdata->msg->body = create_sdp_body(tdata->pool, sdp); |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 1802 | } else { |
| 1803 | if (inv->options & PJSIP_INV_REQUIRE_100REL) { |
| 1804 | tdata->msg->body = NULL; |
| 1805 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1808 | |
| 1809 | return PJ_SUCCESS; |
| 1810 | } |
| 1811 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 1812 | |
| 1813 | /* |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1814 | * Create first response to INVITE |
| 1815 | */ |
| 1816 | PJ_DEF(pj_status_t) pjsip_inv_initial_answer( pjsip_inv_session *inv, |
| 1817 | pjsip_rx_data *rdata, |
| 1818 | int st_code, |
| 1819 | const pj_str_t *st_text, |
| 1820 | const pjmedia_sdp_session *sdp, |
| 1821 | pjsip_tx_data **p_tdata) |
| 1822 | { |
| 1823 | pjsip_tx_data *tdata; |
| 1824 | pj_status_t status; |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 1825 | pjsip_status_code st_code2; |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1826 | |
| 1827 | /* Verify arguments. */ |
| 1828 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 1829 | |
| 1830 | /* Must have INVITE transaction. */ |
| 1831 | PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG); |
| 1832 | |
| 1833 | pjsip_dlg_inc_lock(inv->dlg); |
| 1834 | |
| 1835 | /* Create response */ |
| 1836 | status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, st_text, |
| 1837 | &tdata); |
| 1838 | if (status != PJ_SUCCESS) |
| 1839 | goto on_return; |
| 1840 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1841 | /* Invoke Session Timers module */ |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 1842 | status = pjsip_timer_process_req(inv, rdata, &st_code2); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1843 | if (status != PJ_SUCCESS) { |
| 1844 | pj_status_t status2; |
| 1845 | |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 1846 | status2 = pjsip_dlg_modify_response(inv->dlg, tdata, st_code2, NULL); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1847 | if (status2 != PJ_SUCCESS) { |
| 1848 | pjsip_tx_data_dec_ref(tdata); |
| 1849 | goto on_return; |
| 1850 | } |
| 1851 | status2 = pjsip_timer_update_resp(inv, tdata); |
| 1852 | if (status2 == PJ_SUCCESS) |
| 1853 | *p_tdata = tdata; |
| 1854 | else |
| 1855 | pjsip_tx_data_dec_ref(tdata); |
| 1856 | |
| 1857 | goto on_return; |
| 1858 | } |
| 1859 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1860 | /* Process SDP in answer */ |
| 1861 | status = process_answer(inv, st_code, tdata, sdp); |
| 1862 | if (status != PJ_SUCCESS) { |
| 1863 | pjsip_tx_data_dec_ref(tdata); |
| 1864 | goto on_return; |
| 1865 | } |
| 1866 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1867 | /* Save this answer */ |
| 1868 | inv->last_answer = tdata; |
| 1869 | pjsip_tx_data_add_ref(inv->last_answer); |
| 1870 | PJ_LOG(5,(inv->dlg->obj_name, "Initial answer %s", |
| 1871 | pjsip_tx_data_get_info(inv->last_answer))); |
| 1872 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1873 | /* Invoke Session Timers */ |
| 1874 | pjsip_timer_update_resp(inv, tdata); |
| 1875 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1876 | *p_tdata = tdata; |
| 1877 | |
| 1878 | on_return: |
| 1879 | pjsip_dlg_dec_lock(inv->dlg); |
| 1880 | return status; |
| 1881 | } |
| 1882 | |
| 1883 | |
| 1884 | /* |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1885 | * Answer initial INVITE |
| 1886 | * Re-INVITE will be answered automatically, and will not use this function. |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1887 | */ |
| 1888 | PJ_DEF(pj_status_t) pjsip_inv_answer( pjsip_inv_session *inv, |
| 1889 | int st_code, |
| 1890 | const pj_str_t *st_text, |
| 1891 | const pjmedia_sdp_session *local_sdp, |
| 1892 | pjsip_tx_data **p_tdata ) |
| 1893 | { |
| 1894 | pjsip_tx_data *last_res; |
| 1895 | pj_status_t status; |
| 1896 | |
| 1897 | /* Verify arguments. */ |
| 1898 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 1899 | |
| 1900 | /* Must have INVITE transaction. */ |
| 1901 | PJ_ASSERT_RETURN(inv->invite_tsx, PJ_EBUG); |
| 1902 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1903 | /* Must have created an answer before */ |
| 1904 | PJ_ASSERT_RETURN(inv->last_answer, PJ_EINVALIDOP); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1905 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1906 | pjsip_dlg_inc_lock(inv->dlg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1907 | |
| 1908 | /* Modify last response. */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1909 | last_res = inv->last_answer; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1910 | status = pjsip_dlg_modify_response(inv->dlg, last_res, st_code, st_text); |
| 1911 | if (status != PJ_SUCCESS) |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1912 | goto on_return; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1913 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 1914 | /* For non-2xx final response, strip message body */ |
| 1915 | if (st_code >= 300) { |
| 1916 | last_res->msg->body = NULL; |
| 1917 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1918 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1919 | /* Process SDP in answer */ |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1920 | status = process_answer(inv, st_code, last_res, local_sdp); |
| 1921 | if (status != PJ_SUCCESS) { |
| 1922 | pjsip_tx_data_dec_ref(last_res); |
| 1923 | goto on_return; |
| 1924 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1925 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1926 | /* Invoke Session Timers */ |
| 1927 | pjsip_timer_update_resp(inv, last_res); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1928 | |
| 1929 | *p_tdata = last_res; |
| 1930 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1931 | on_return: |
| 1932 | pjsip_dlg_dec_lock(inv->dlg); |
| 1933 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | |
| 1937 | /* |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1938 | * Set SDP answer. |
| 1939 | */ |
| 1940 | PJ_DEF(pj_status_t) pjsip_inv_set_sdp_answer( pjsip_inv_session *inv, |
| 1941 | const pjmedia_sdp_session *sdp ) |
| 1942 | { |
| 1943 | pj_status_t status; |
| 1944 | |
| 1945 | PJ_ASSERT_RETURN(inv && sdp, PJ_EINVAL); |
| 1946 | |
| 1947 | pjsip_dlg_inc_lock(inv->dlg); |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 1948 | status = pjmedia_sdp_neg_set_local_answer( inv->pool_prov, inv->neg, sdp); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1949 | pjsip_dlg_dec_lock(inv->dlg); |
| 1950 | |
| 1951 | return status; |
| 1952 | } |
| 1953 | |
| 1954 | |
| 1955 | /* |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1956 | * End session. |
| 1957 | */ |
| 1958 | PJ_DEF(pj_status_t) pjsip_inv_end_session( pjsip_inv_session *inv, |
| 1959 | int st_code, |
| 1960 | const pj_str_t *st_text, |
| 1961 | pjsip_tx_data **p_tdata ) |
| 1962 | { |
| 1963 | pjsip_tx_data *tdata; |
| 1964 | pj_status_t status; |
| 1965 | |
| 1966 | /* Verify arguments. */ |
| 1967 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 1968 | |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 1969 | /* Set cause code. */ |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 1970 | inv_set_cause(inv, st_code, st_text); |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 1971 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1972 | /* Create appropriate message. */ |
| 1973 | switch (inv->state) { |
| 1974 | case PJSIP_INV_STATE_CALLING: |
| 1975 | case PJSIP_INV_STATE_EARLY: |
| 1976 | case PJSIP_INV_STATE_INCOMING: |
| 1977 | |
| 1978 | if (inv->role == PJSIP_ROLE_UAC) { |
| 1979 | |
| 1980 | /* For UAC when session has not been confirmed, create CANCEL. */ |
| 1981 | |
| 1982 | /* MUST have the original UAC INVITE transaction. */ |
| 1983 | PJ_ASSERT_RETURN(inv->invite_tsx != NULL, PJ_EBUG); |
| 1984 | |
| 1985 | /* But CANCEL should only be called when we have received a |
| 1986 | * provisional response. If we haven't received any responses, |
| 1987 | * just destroy the transaction. |
| 1988 | */ |
| 1989 | if (inv->invite_tsx->status_code < 100) { |
| 1990 | |
Benny Prijono | 006a4e8 | 2009-04-26 11:30:22 +0000 | [diff] [blame] | 1991 | /* Do not stop INVITE retransmission, see ticket #506 */ |
| 1992 | //pjsip_tsx_stop_retransmit(inv->invite_tsx); |
Benny Prijono | 1dc8be0 | 2007-05-30 04:26:40 +0000 | [diff] [blame] | 1993 | inv->cancelling = PJ_TRUE; |
| 1994 | inv->pending_cancel = PJ_TRUE; |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 1995 | *p_tdata = NULL; |
Benny Prijono | 006a4e8 | 2009-04-26 11:30:22 +0000 | [diff] [blame] | 1996 | PJ_LOG(4, (inv->obj_name, "Delaying CANCEL since no " |
| 1997 | "provisional response is received yet")); |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 1998 | return PJ_SUCCESS; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | /* The CSeq here assumes that the dialog is started with an |
| 2002 | * INVITE session. This may not be correct; dialog can be |
| 2003 | * started as SUBSCRIBE session. |
| 2004 | * So fix this! |
| 2005 | */ |
| 2006 | status = pjsip_endpt_create_cancel(inv->dlg->endpt, |
| 2007 | inv->invite_tsx->last_tx, |
| 2008 | &tdata); |
Benny Prijono | 99b0437 | 2009-04-26 11:02:04 +0000 | [diff] [blame] | 2009 | if (status != PJ_SUCCESS) |
| 2010 | return status; |
| 2011 | |
| 2012 | /* Set timeout for the INVITE transaction, in case UAS is not |
| 2013 | * able to respond the INVITE with 487 final response. The |
| 2014 | * timeout value is 64*T1. |
| 2015 | */ |
| 2016 | pjsip_tsx_set_timeout(inv->invite_tsx, 64 * pjsip_cfg()->tsx.t1); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2017 | |
| 2018 | } else { |
| 2019 | |
| 2020 | /* For UAS, send a final response. */ |
| 2021 | tdata = inv->invite_tsx->last_tx; |
| 2022 | PJ_ASSERT_RETURN(tdata != NULL, PJ_EINVALIDOP); |
| 2023 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2024 | //status = pjsip_dlg_modify_response(inv->dlg, tdata, st_code, |
| 2025 | // st_text); |
| 2026 | status = pjsip_inv_answer(inv, st_code, st_text, NULL, &tdata); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2027 | } |
| 2028 | break; |
| 2029 | |
| 2030 | case PJSIP_INV_STATE_CONNECTING: |
| 2031 | case PJSIP_INV_STATE_CONFIRMED: |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 2032 | /* End Session Timer */ |
| 2033 | pjsip_timer_end_session(inv); |
| 2034 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2035 | /* For established dialog, send BYE */ |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 2036 | status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(), |
| 2037 | -1, &tdata); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2038 | break; |
| 2039 | |
| 2040 | case PJSIP_INV_STATE_DISCONNECTED: |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2041 | /* No need to do anything. */ |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 2042 | return PJSIP_ESESSIONTERMINATED; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2043 | |
| 2044 | default: |
| 2045 | pj_assert("!Invalid operation!"); |
| 2046 | return PJ_EINVALIDOP; |
| 2047 | } |
| 2048 | |
| 2049 | if (status != PJ_SUCCESS) |
| 2050 | return status; |
| 2051 | |
| 2052 | |
| 2053 | /* Done */ |
| 2054 | |
Benny Prijono | 0606e70 | 2007-05-22 12:21:40 +0000 | [diff] [blame] | 2055 | inv->cancelling = PJ_TRUE; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2056 | *p_tdata = tdata; |
| 2057 | |
| 2058 | return PJ_SUCCESS; |
| 2059 | } |
| 2060 | |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 2061 | /* Following redirection recursion, get next target from the target set and |
| 2062 | * notify user. |
| 2063 | * |
| 2064 | * Returns PJ_FALSE if recursion fails (either because there's no more target |
| 2065 | * or user rejects the recursion). If we return PJ_FALSE, caller should |
| 2066 | * disconnect the session. |
| 2067 | * |
| 2068 | * Note: |
| 2069 | * the event 'e' argument may be NULL. |
| 2070 | */ |
| 2071 | static pj_bool_t inv_uac_recurse(pjsip_inv_session *inv, int code, |
| 2072 | const pj_str_t *reason, pjsip_event *e) |
| 2073 | { |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 2074 | pjsip_redirect_op op; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 2075 | pjsip_target *target; |
| 2076 | |
| 2077 | /* Won't redirect if the callback is not implemented. */ |
| 2078 | if (mod_inv.cb.on_redirected == NULL) |
| 2079 | return PJ_FALSE; |
| 2080 | |
| 2081 | if (reason == NULL) |
| 2082 | reason = pjsip_get_status_text(code); |
| 2083 | |
| 2084 | /* Set status of current target */ |
| 2085 | pjsip_target_assign_status(inv->dlg->target_set.current, inv->dlg->pool, |
| 2086 | code, reason); |
| 2087 | |
| 2088 | /* Fetch next target from the target set. We only want to |
| 2089 | * process SIP/SIPS URI for now. |
| 2090 | */ |
| 2091 | for (;;) { |
| 2092 | target = pjsip_target_set_get_next(&inv->dlg->target_set); |
| 2093 | if (target == NULL) { |
| 2094 | /* No more target. */ |
| 2095 | return PJ_FALSE; |
| 2096 | } |
| 2097 | |
| 2098 | if (!PJSIP_URI_SCHEME_IS_SIP(target->uri) && |
| 2099 | !PJSIP_URI_SCHEME_IS_SIPS(target->uri)) |
| 2100 | { |
| 2101 | code = PJSIP_SC_UNSUPPORTED_URI_SCHEME; |
| 2102 | reason = pjsip_get_status_text(code); |
| 2103 | |
| 2104 | /* Mark this target as unusable and fetch next target. */ |
| 2105 | pjsip_target_assign_status(target, inv->dlg->pool, code, reason); |
| 2106 | } else { |
| 2107 | /* Found a target */ |
| 2108 | break; |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | /* We have target in 'target'. Set this target as current target |
| 2113 | * and notify callback. |
| 2114 | */ |
| 2115 | pjsip_target_set_set_current(&inv->dlg->target_set, target); |
| 2116 | |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 2117 | op = (*mod_inv.cb.on_redirected)(inv, target->uri, e); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 2118 | |
| 2119 | |
| 2120 | /* Check what the application wants to do now */ |
| 2121 | switch (op) { |
| 2122 | case PJSIP_REDIRECT_ACCEPT: |
| 2123 | case PJSIP_REDIRECT_STOP: |
| 2124 | /* Must increment session counter, that's the convention of the |
| 2125 | * pjsip_inv_process_redirect(). |
| 2126 | */ |
| 2127 | pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod); |
| 2128 | |
| 2129 | /* Act on the recursion */ |
| 2130 | pjsip_inv_process_redirect(inv, op, e); |
| 2131 | return PJ_TRUE; |
| 2132 | |
| 2133 | case PJSIP_REDIRECT_PENDING: |
| 2134 | /* Increment session so that the dialog/session is not destroyed |
| 2135 | * while we're waiting for user confirmation. |
| 2136 | */ |
| 2137 | pjsip_dlg_inc_session(inv->dlg, &mod_inv.mod); |
| 2138 | |
| 2139 | /* Also clear the invite_tsx variable, otherwise when this tsx is |
| 2140 | * terminated, it will also terminate the session. |
| 2141 | */ |
| 2142 | inv->invite_tsx = NULL; |
| 2143 | |
| 2144 | /* Done. The processing will continue once the application calls |
| 2145 | * pjsip_inv_process_redirect(). |
| 2146 | */ |
| 2147 | return PJ_TRUE; |
| 2148 | |
| 2149 | case PJSIP_REDIRECT_REJECT: |
| 2150 | /* Recursively call this function again to fetch next target, if any. |
| 2151 | */ |
| 2152 | return inv_uac_recurse(inv, PJSIP_SC_REQUEST_TERMINATED, NULL, e); |
| 2153 | |
| 2154 | } |
| 2155 | |
| 2156 | pj_assert(!"Should not reach here"); |
| 2157 | return PJ_FALSE; |
| 2158 | } |
| 2159 | |
| 2160 | |
| 2161 | /* Process redirection/recursion */ |
| 2162 | PJ_DEF(pj_status_t) pjsip_inv_process_redirect( pjsip_inv_session *inv, |
| 2163 | pjsip_redirect_op op, |
| 2164 | pjsip_event *e) |
| 2165 | { |
| 2166 | const pjsip_status_code cancel_code = PJSIP_SC_REQUEST_TERMINATED; |
| 2167 | pjsip_event usr_event; |
| 2168 | pj_status_t status = PJ_SUCCESS; |
| 2169 | |
| 2170 | PJ_ASSERT_RETURN(inv && op != PJSIP_REDIRECT_PENDING, PJ_EINVAL); |
| 2171 | |
| 2172 | if (e == NULL) { |
| 2173 | PJSIP_EVENT_INIT_USER(usr_event, NULL, NULL, NULL, NULL); |
| 2174 | e = &usr_event; |
| 2175 | } |
| 2176 | |
| 2177 | pjsip_dlg_inc_lock(inv->dlg); |
| 2178 | |
| 2179 | /* Decrement session. That's the convention here to prevent the dialog |
| 2180 | * or session from being destroyed while we're waiting for user |
| 2181 | * confirmation. |
| 2182 | */ |
| 2183 | pjsip_dlg_dec_session(inv->dlg, &mod_inv.mod); |
| 2184 | |
| 2185 | /* See what the application wants to do now */ |
| 2186 | switch (op) { |
| 2187 | case PJSIP_REDIRECT_ACCEPT: |
| 2188 | /* User accept the redirection. Reset the session and resend the |
| 2189 | * INVITE request. |
| 2190 | */ |
| 2191 | { |
| 2192 | pjsip_tx_data *tdata; |
| 2193 | pjsip_via_hdr *via; |
| 2194 | |
| 2195 | /* Get the original INVITE request. */ |
| 2196 | tdata = inv->invite_req; |
| 2197 | pjsip_tx_data_add_ref(tdata); |
| 2198 | |
| 2199 | /* Restore strict route set. |
| 2200 | * See http://trac.pjsip.org/repos/ticket/492 |
| 2201 | */ |
| 2202 | pjsip_restore_strict_route_set(tdata); |
| 2203 | |
| 2204 | /* Set target */ |
Benny Prijono | 20da799 | 2008-12-18 16:48:43 +0000 | [diff] [blame] | 2205 | tdata->msg->line.req.uri = (pjsip_uri*) |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 2206 | pjsip_uri_clone(tdata->pool, inv->dlg->target_set.current->uri); |
| 2207 | |
| 2208 | /* Remove branch param in Via header. */ |
| 2209 | via = (pjsip_via_hdr*) |
| 2210 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
| 2211 | via->branch_param.slen = 0; |
| 2212 | |
| 2213 | /* Must invalidate the message! */ |
| 2214 | pjsip_tx_data_invalidate_msg(tdata); |
| 2215 | |
| 2216 | /* Reset the session */ |
| 2217 | pjsip_inv_uac_restart(inv, PJ_FALSE); |
| 2218 | |
| 2219 | /* (re)Send the INVITE request */ |
| 2220 | status = pjsip_inv_send_msg(inv, tdata); |
| 2221 | } |
| 2222 | break; |
| 2223 | |
| 2224 | case PJSIP_REDIRECT_STOP: |
| 2225 | /* User doesn't want the redirection. Disconnect the session now. */ |
| 2226 | inv_set_cause(inv, cancel_code, pjsip_get_status_text(cancel_code)); |
| 2227 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 2228 | |
| 2229 | /* Caller should expect that the invite session is gone now, so |
| 2230 | * we don't need to set status to PJSIP_ESESSIONTERMINATED here. |
| 2231 | */ |
| 2232 | break; |
| 2233 | |
| 2234 | case PJSIP_REDIRECT_REJECT: |
| 2235 | /* Current target is rejected. Fetch next target if any. */ |
| 2236 | if (inv_uac_recurse(inv, cancel_code, NULL, NULL) == PJ_FALSE) { |
| 2237 | inv_set_cause(inv, cancel_code, |
| 2238 | pjsip_get_status_text(cancel_code)); |
| 2239 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 2240 | |
| 2241 | /* Tell caller that the invite session is gone now */ |
| 2242 | status = PJSIP_ESESSIONTERMINATED; |
| 2243 | } |
| 2244 | break; |
| 2245 | |
| 2246 | |
| 2247 | case PJSIP_REDIRECT_PENDING: |
| 2248 | pj_assert(!"Should not happen"); |
| 2249 | break; |
| 2250 | } |
| 2251 | |
| 2252 | |
| 2253 | pjsip_dlg_dec_lock(inv->dlg); |
| 2254 | |
| 2255 | return status; |
| 2256 | } |
| 2257 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2258 | |
| 2259 | /* |
| 2260 | * Create re-INVITE. |
| 2261 | */ |
| 2262 | PJ_DEF(pj_status_t) pjsip_inv_reinvite( pjsip_inv_session *inv, |
| 2263 | const pj_str_t *new_contact, |
| 2264 | const pjmedia_sdp_session *new_offer, |
| 2265 | pjsip_tx_data **p_tdata ) |
| 2266 | { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2267 | pj_status_t status; |
| 2268 | pjsip_contact_hdr *contact_hdr = NULL; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2269 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2270 | /* Check arguments. */ |
| 2271 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2272 | |
| 2273 | /* Must NOT have a pending INVITE transaction */ |
Benny Prijono | 2b78782 | 2007-06-12 15:29:52 +0000 | [diff] [blame] | 2274 | if (inv->invite_tsx!=NULL) |
| 2275 | return PJ_EINVALIDOP; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2276 | |
| 2277 | |
| 2278 | pjsip_dlg_inc_lock(inv->dlg); |
| 2279 | |
| 2280 | if (new_contact) { |
| 2281 | pj_str_t tmp; |
| 2282 | const pj_str_t STR_CONTACT = { "Contact", 7 }; |
| 2283 | |
| 2284 | pj_strdup_with_null(inv->dlg->pool, &tmp, new_contact); |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2285 | contact_hdr = (pjsip_contact_hdr*) |
| 2286 | pjsip_parse_hdr(inv->dlg->pool, &STR_CONTACT, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2287 | tmp.ptr, tmp.slen, NULL); |
| 2288 | if (!contact_hdr) { |
| 2289 | status = PJSIP_EINVALIDURI; |
| 2290 | goto on_return; |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | |
| 2295 | if (new_offer) { |
| 2296 | if (!inv->neg) { |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 2297 | status = pjmedia_sdp_neg_create_w_local_offer(inv->pool, |
| 2298 | new_offer, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2299 | &inv->neg); |
| 2300 | if (status != PJ_SUCCESS) |
| 2301 | goto on_return; |
| 2302 | |
| 2303 | } else switch (pjmedia_sdp_neg_get_state(inv->neg)) { |
| 2304 | |
| 2305 | case PJMEDIA_SDP_NEG_STATE_NULL: |
| 2306 | pj_assert(!"Unexpected SDP neg state NULL"); |
| 2307 | status = PJ_EBUG; |
| 2308 | goto on_return; |
| 2309 | |
| 2310 | case PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER: |
| 2311 | PJ_LOG(4,(inv->obj_name, |
| 2312 | "pjsip_inv_reinvite: already have an offer, new " |
| 2313 | "offer is ignored")); |
| 2314 | break; |
| 2315 | |
| 2316 | case PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER: |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 2317 | status = pjmedia_sdp_neg_set_local_answer(inv->pool_prov, |
| 2318 | inv->neg, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2319 | new_offer); |
| 2320 | if (status != PJ_SUCCESS) |
| 2321 | goto on_return; |
| 2322 | break; |
| 2323 | |
| 2324 | case PJMEDIA_SDP_NEG_STATE_WAIT_NEGO: |
| 2325 | PJ_LOG(4,(inv->obj_name, |
| 2326 | "pjsip_inv_reinvite: SDP in WAIT_NEGO state, new " |
| 2327 | "offer is ignored")); |
| 2328 | break; |
| 2329 | |
| 2330 | case PJMEDIA_SDP_NEG_STATE_DONE: |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 2331 | status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, |
| 2332 | inv->neg, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2333 | new_offer); |
| 2334 | if (status != PJ_SUCCESS) |
| 2335 | goto on_return; |
| 2336 | break; |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | if (contact_hdr) |
| 2341 | inv->dlg->local.contact = contact_hdr; |
| 2342 | |
| 2343 | status = pjsip_inv_invite(inv, p_tdata); |
| 2344 | |
| 2345 | on_return: |
| 2346 | pjsip_dlg_dec_lock(inv->dlg); |
| 2347 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | /* |
| 2351 | * Create UPDATE. |
| 2352 | */ |
| 2353 | PJ_DEF(pj_status_t) pjsip_inv_update ( pjsip_inv_session *inv, |
| 2354 | const pj_str_t *new_contact, |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2355 | const pjmedia_sdp_session *offer, |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2356 | pjsip_tx_data **p_tdata ) |
| 2357 | { |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2358 | pjsip_contact_hdr *contact_hdr = NULL; |
| 2359 | pjsip_tx_data *tdata = NULL; |
| 2360 | pjmedia_sdp_session *sdp_copy; |
| 2361 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2362 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2363 | /* Verify arguments. */ |
| 2364 | PJ_ASSERT_RETURN(inv && p_tdata && offer, PJ_EINVAL); |
| 2365 | |
| 2366 | /* Dialog must have been established */ |
| 2367 | PJ_ASSERT_RETURN(inv->dlg->state == PJSIP_DIALOG_STATE_ESTABLISHED, |
| 2368 | PJ_EINVALIDOP); |
| 2369 | |
| 2370 | /* Invite session must not have been disconnected */ |
| 2371 | PJ_ASSERT_RETURN(inv->state < PJSIP_INV_STATE_DISCONNECTED, |
| 2372 | PJ_EINVALIDOP); |
| 2373 | |
| 2374 | /* Lock dialog. */ |
| 2375 | pjsip_dlg_inc_lock(inv->dlg); |
| 2376 | |
| 2377 | /* Process offer */ |
| 2378 | if (pjmedia_sdp_neg_get_state(inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) { |
| 2379 | PJ_LOG(4,(inv->dlg->obj_name, |
| 2380 | "Invalid SDP offer/answer state for UPDATE")); |
| 2381 | status = PJ_EINVALIDOP; |
| 2382 | goto on_error; |
| 2383 | } |
| 2384 | |
Benny Prijono | 60e31fc | 2009-04-23 11:50:25 +0000 | [diff] [blame] | 2385 | /* Notify negotiator about the new offer. This will fix the offer |
| 2386 | * with correct SDP origin. |
| 2387 | */ |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 2388 | status = pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, inv->neg, |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2389 | offer); |
| 2390 | if (status != PJ_SUCCESS) |
| 2391 | goto on_error; |
| 2392 | |
Benny Prijono | 60e31fc | 2009-04-23 11:50:25 +0000 | [diff] [blame] | 2393 | /* Retrieve the "fixed" offer from negotiator */ |
| 2394 | pjmedia_sdp_neg_get_neg_local(inv->neg, &offer); |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2395 | |
| 2396 | /* Update Contact if required */ |
| 2397 | if (new_contact) { |
| 2398 | pj_str_t tmp; |
| 2399 | const pj_str_t STR_CONTACT = { "Contact", 7 }; |
| 2400 | |
| 2401 | pj_strdup_with_null(inv->dlg->pool, &tmp, new_contact); |
| 2402 | contact_hdr = (pjsip_contact_hdr*) |
| 2403 | pjsip_parse_hdr(inv->dlg->pool, &STR_CONTACT, |
| 2404 | tmp.ptr, tmp.slen, NULL); |
| 2405 | if (!contact_hdr) { |
| 2406 | status = PJSIP_EINVALIDURI; |
| 2407 | goto on_error; |
| 2408 | } |
| 2409 | |
| 2410 | inv->dlg->local.contact = contact_hdr; |
| 2411 | } |
| 2412 | |
| 2413 | /* Create request */ |
| 2414 | status = pjsip_dlg_create_request(inv->dlg, &pjsip_update_method, |
| 2415 | -1, &tdata); |
| 2416 | if (status != PJ_SUCCESS) |
| 2417 | goto on_error; |
| 2418 | |
| 2419 | /* Attach SDP body */ |
| 2420 | sdp_copy = pjmedia_sdp_session_clone(tdata->pool, offer); |
| 2421 | pjsip_create_sdp_body(tdata->pool, sdp_copy, &tdata->msg->body); |
| 2422 | |
| 2423 | /* Unlock dialog. */ |
| 2424 | pjsip_dlg_dec_lock(inv->dlg); |
| 2425 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 2426 | status = pjsip_timer_update_req(inv, tdata); |
| 2427 | if (status != PJ_SUCCESS) |
| 2428 | goto on_error; |
| 2429 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2430 | *p_tdata = tdata; |
| 2431 | |
| 2432 | return PJ_SUCCESS; |
| 2433 | |
| 2434 | on_error: |
| 2435 | if (tdata) |
| 2436 | pjsip_tx_data_dec_ref(tdata); |
| 2437 | |
| 2438 | /* Unlock dialog. */ |
| 2439 | pjsip_dlg_dec_lock(inv->dlg); |
| 2440 | |
| 2441 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /* |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 2445 | * Create an ACK request. |
| 2446 | */ |
| 2447 | PJ_DEF(pj_status_t) pjsip_inv_create_ack(pjsip_inv_session *inv, |
| 2448 | int cseq, |
| 2449 | pjsip_tx_data **p_tdata) |
| 2450 | { |
| 2451 | const pjmedia_sdp_session *sdp = NULL; |
| 2452 | pj_status_t status; |
| 2453 | |
| 2454 | PJ_ASSERT_RETURN(inv && p_tdata, PJ_EINVAL); |
| 2455 | |
| 2456 | /* Lock dialog. */ |
| 2457 | pjsip_dlg_inc_lock(inv->dlg); |
| 2458 | |
| 2459 | /* Destroy last_ack */ |
| 2460 | if (inv->last_ack) { |
| 2461 | pjsip_tx_data_dec_ref(inv->last_ack); |
| 2462 | inv->last_ack = NULL; |
| 2463 | } |
| 2464 | |
| 2465 | /* Create new ACK request */ |
| 2466 | status = pjsip_dlg_create_request(inv->dlg, pjsip_get_ack_method(), |
| 2467 | cseq, &inv->last_ack); |
| 2468 | if (status != PJ_SUCCESS) { |
| 2469 | pjsip_dlg_dec_lock(inv->dlg); |
| 2470 | return status; |
| 2471 | } |
| 2472 | |
| 2473 | /* See if we have pending SDP answer to send */ |
| 2474 | sdp = inv_has_pending_answer(inv, inv->invite_tsx); |
| 2475 | if (sdp) { |
| 2476 | inv->last_ack->msg->body = create_sdp_body(inv->last_ack->pool, sdp); |
| 2477 | } |
| 2478 | |
| 2479 | /* Keep this for subsequent response retransmission */ |
| 2480 | inv->last_ack_cseq = cseq; |
| 2481 | pjsip_tx_data_add_ref(inv->last_ack); |
| 2482 | |
| 2483 | /* Done */ |
| 2484 | *p_tdata = inv->last_ack; |
| 2485 | |
| 2486 | /* Unlock dialog. */ |
| 2487 | pjsip_dlg_dec_lock(inv->dlg); |
| 2488 | |
| 2489 | return PJ_SUCCESS; |
| 2490 | } |
| 2491 | |
| 2492 | /* |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2493 | * Send a request or response message. |
| 2494 | */ |
| 2495 | PJ_DEF(pj_status_t) pjsip_inv_send_msg( pjsip_inv_session *inv, |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 2496 | pjsip_tx_data *tdata) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2497 | { |
| 2498 | pj_status_t status; |
| 2499 | |
| 2500 | /* Verify arguments. */ |
| 2501 | PJ_ASSERT_RETURN(inv && tdata, PJ_EINVAL); |
| 2502 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 2503 | PJ_LOG(5,(inv->obj_name, "Sending %s", |
| 2504 | pjsip_tx_data_get_info(tdata))); |
| 2505 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2506 | if (tdata->msg->type == PJSIP_REQUEST_MSG) { |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 2507 | struct tsx_inv_data *tsx_inv_data; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2508 | |
Benny Prijono | 64158af | 2006-04-04 11:06:34 +0000 | [diff] [blame] | 2509 | pjsip_dlg_inc_lock(inv->dlg); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2510 | |
Benny Prijono | 22e48c9 | 2008-03-20 14:40:50 +0000 | [diff] [blame] | 2511 | /* Check again that we didn't receive incoming re-INVITE */ |
Benny Prijono | 9ae5dfc | 2008-03-27 17:30:51 +0000 | [diff] [blame] | 2512 | if (tdata->msg->line.req.method.id==PJSIP_INVITE_METHOD && |
| 2513 | inv->invite_tsx) |
| 2514 | { |
Benny Prijono | 22e48c9 | 2008-03-20 14:40:50 +0000 | [diff] [blame] | 2515 | pjsip_tx_data_dec_ref(tdata); |
| 2516 | pjsip_dlg_dec_lock(inv->dlg); |
| 2517 | return PJ_EINVALIDOP; |
| 2518 | } |
| 2519 | |
| 2520 | /* Associate our data in outgoing invite transaction */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2521 | tsx_inv_data = PJ_POOL_ZALLOC_T(inv->pool, struct tsx_inv_data); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 2522 | tsx_inv_data->inv = inv; |
| 2523 | |
Benny Prijono | 64158af | 2006-04-04 11:06:34 +0000 | [diff] [blame] | 2524 | pjsip_dlg_dec_lock(inv->dlg); |
| 2525 | |
| 2526 | status = pjsip_dlg_send_request(inv->dlg, tdata, mod_inv.mod.id, |
| 2527 | tsx_inv_data); |
| 2528 | if (status != PJ_SUCCESS) |
| 2529 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2530 | |
| 2531 | } else { |
| 2532 | pjsip_cseq_hdr *cseq; |
| 2533 | |
| 2534 | /* Can only do this to send response to original INVITE |
| 2535 | * request. |
| 2536 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2537 | PJ_ASSERT_RETURN((cseq=(pjsip_cseq_hdr*)pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL))!=NULL |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 2538 | && (cseq->cseq == inv->invite_tsx->cseq), |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2539 | PJ_EINVALIDOP); |
| 2540 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 2541 | if (inv->options & PJSIP_INV_REQUIRE_100REL) { |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2542 | status = pjsip_100rel_tx_response(inv, tdata); |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 2543 | } else |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 2544 | { |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2545 | status = pjsip_dlg_send_response(inv->dlg, inv->invite_tsx, tdata); |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2548 | if (status != PJ_SUCCESS) |
| 2549 | return status; |
| 2550 | } |
| 2551 | |
| 2552 | /* Done (?) */ |
| 2553 | return PJ_SUCCESS; |
| 2554 | } |
| 2555 | |
| 2556 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2557 | /* |
| 2558 | * Respond to incoming CANCEL request. |
| 2559 | */ |
| 2560 | static void inv_respond_incoming_cancel(pjsip_inv_session *inv, |
| 2561 | pjsip_transaction *cancel_tsx, |
| 2562 | pjsip_rx_data *rdata) |
| 2563 | { |
| 2564 | pjsip_tx_data *tdata; |
| 2565 | pjsip_transaction *invite_tsx; |
| 2566 | pj_str_t key; |
| 2567 | pj_status_t status; |
| 2568 | |
| 2569 | /* See if we have matching INVITE server transaction: */ |
| 2570 | |
| 2571 | pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_ROLE_UAS, |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 2572 | pjsip_get_invite_method(), rdata); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2573 | invite_tsx = pjsip_tsx_layer_find_tsx(&key, PJ_TRUE); |
| 2574 | |
| 2575 | if (invite_tsx == NULL) { |
| 2576 | |
| 2577 | /* Invite transaction not found! |
Benny Prijono | c514576 | 2007-11-23 12:04:40 +0000 | [diff] [blame] | 2578 | * Respond CANCEL with 481 (RFC 3261 Section 9.2 page 55) |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2579 | */ |
Benny Prijono | c514576 | 2007-11-23 12:04:40 +0000 | [diff] [blame] | 2580 | status = pjsip_dlg_create_response( inv->dlg, rdata, 481, NULL, |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2581 | &tdata); |
| 2582 | |
| 2583 | } else { |
| 2584 | /* Always answer CANCEL will 200 (OK) regardless of |
| 2585 | * the state of the INVITE transaction. |
| 2586 | */ |
| 2587 | status = pjsip_dlg_create_response( inv->dlg, rdata, 200, NULL, |
| 2588 | &tdata); |
| 2589 | } |
| 2590 | |
| 2591 | /* See if we have created the response successfully. */ |
| 2592 | if (status != PJ_SUCCESS) return; |
| 2593 | |
| 2594 | /* Send the CANCEL response */ |
| 2595 | status = pjsip_dlg_send_response(inv->dlg, cancel_tsx, tdata); |
| 2596 | if (status != PJ_SUCCESS) return; |
| 2597 | |
| 2598 | |
| 2599 | /* See if we need to terminate the UAS INVITE transaction |
| 2600 | * with 487 (Request Terminated) response. |
| 2601 | */ |
| 2602 | if (invite_tsx && invite_tsx->status_code < 200) { |
| 2603 | |
| 2604 | pj_assert(invite_tsx->last_tx != NULL); |
| 2605 | |
| 2606 | tdata = invite_tsx->last_tx; |
| 2607 | |
| 2608 | status = pjsip_dlg_modify_response(inv->dlg, tdata, 487, NULL); |
Benny Prijono | fc8bb14 | 2007-11-08 09:56:50 +0000 | [diff] [blame] | 2609 | if (status == PJ_SUCCESS) { |
| 2610 | /* Remove the message body */ |
| 2611 | tdata->msg->body = NULL; |
Benny Prijono | 1e08e4f | 2009-05-13 08:57:38 +0000 | [diff] [blame] | 2612 | if (inv->options & PJSIP_INV_REQUIRE_100REL) { |
| 2613 | status = pjsip_100rel_tx_response(inv, tdata); |
| 2614 | } else { |
| 2615 | status = pjsip_dlg_send_response(inv->dlg, invite_tsx, |
| 2616 | tdata); |
| 2617 | } |
Benny Prijono | fc8bb14 | 2007-11-08 09:56:50 +0000 | [diff] [blame] | 2618 | } |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | if (invite_tsx) |
| 2622 | pj_mutex_unlock(invite_tsx->mutex); |
| 2623 | } |
| 2624 | |
| 2625 | |
| 2626 | /* |
| 2627 | * Respond to incoming BYE request. |
| 2628 | */ |
| 2629 | static void inv_respond_incoming_bye( pjsip_inv_session *inv, |
| 2630 | pjsip_transaction *bye_tsx, |
| 2631 | pjsip_rx_data *rdata, |
| 2632 | pjsip_event *e ) |
| 2633 | { |
| 2634 | pj_status_t status; |
| 2635 | pjsip_tx_data *tdata; |
| 2636 | |
| 2637 | /* Respond BYE with 200: */ |
| 2638 | |
| 2639 | status = pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata); |
| 2640 | if (status != PJ_SUCCESS) return; |
| 2641 | |
| 2642 | status = pjsip_dlg_send_response(inv->dlg, bye_tsx, tdata); |
| 2643 | if (status != PJ_SUCCESS) return; |
| 2644 | |
| 2645 | /* Terminate session: */ |
| 2646 | |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 2647 | if (inv->state != PJSIP_INV_STATE_DISCONNECTED) { |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 2648 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2649 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 2650 | } |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | /* |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 2654 | * Respond to BYE request. |
| 2655 | */ |
| 2656 | static void inv_handle_bye_response( pjsip_inv_session *inv, |
| 2657 | pjsip_transaction *tsx, |
| 2658 | pjsip_rx_data *rdata, |
| 2659 | pjsip_event *e ) |
| 2660 | { |
| 2661 | pj_status_t status; |
| 2662 | |
| 2663 | if (e->body.tsx_state.type != PJSIP_EVENT_RX_MSG) { |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 2664 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 2665 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 2666 | return; |
| 2667 | } |
| 2668 | |
| 2669 | /* Handle 401/407 challenge. */ |
| 2670 | if (tsx->status_code == 401 || tsx->status_code == 407) { |
| 2671 | |
| 2672 | pjsip_tx_data *tdata; |
| 2673 | |
| 2674 | status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, |
| 2675 | rdata, |
| 2676 | tsx->last_tx, |
| 2677 | &tdata); |
| 2678 | |
| 2679 | if (status != PJ_SUCCESS) { |
| 2680 | |
| 2681 | /* Does not have proper credentials. |
| 2682 | * End the session anyway. |
| 2683 | */ |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 2684 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 2685 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 2686 | |
| 2687 | } else { |
| 2688 | /* Re-send BYE. */ |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 2689 | status = pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | } else { |
| 2693 | |
| 2694 | /* End the session. */ |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 2695 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 2696 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 2697 | } |
| 2698 | |
| 2699 | } |
| 2700 | |
| 2701 | /* |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2702 | * Respond to incoming UPDATE request. |
| 2703 | */ |
| 2704 | static void inv_respond_incoming_update(pjsip_inv_session *inv, |
| 2705 | pjsip_rx_data *rdata) |
| 2706 | { |
| 2707 | pjmedia_sdp_neg_state neg_state; |
| 2708 | pj_status_t status; |
| 2709 | pjsip_tx_data *tdata = NULL; |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 2710 | pjsip_status_code st_code; |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2711 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 2712 | /* Invoke Session Timers module */ |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 2713 | status = pjsip_timer_process_req(inv, rdata, &st_code); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 2714 | if (status != PJ_SUCCESS) { |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 2715 | status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 2716 | NULL, &tdata); |
| 2717 | goto on_return; |
| 2718 | } |
| 2719 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2720 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 2721 | |
| 2722 | /* Send 491 if we receive UPDATE while we're waiting for an answer */ |
| 2723 | if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) { |
| 2724 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 2725 | PJSIP_SC_REQUEST_PENDING, NULL, |
| 2726 | &tdata); |
| 2727 | } |
| 2728 | /* Send 500 with Retry-After header set randomly between 0 and 10 if we |
| 2729 | * receive UPDATE while we haven't sent answer. |
| 2730 | */ |
| 2731 | else if (neg_state == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER || |
| 2732 | neg_state == PJMEDIA_SDP_NEG_STATE_WAIT_NEGO) { |
| 2733 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 2734 | PJSIP_SC_INTERNAL_SERVER_ERROR, |
| 2735 | NULL, &tdata); |
| 2736 | |
Benny Prijono | c5cbc05 | 2007-11-08 09:44:08 +0000 | [diff] [blame] | 2737 | /* If UPDATE doesn't contain SDP, just respond with 200/OK. |
| 2738 | * This is a valid scenario according to session-timer draft. |
| 2739 | */ |
| 2740 | } else if (rdata->msg_info.msg->body == NULL) { |
| 2741 | |
| 2742 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 2743 | 200, NULL, &tdata); |
| 2744 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2745 | } else { |
| 2746 | /* We receive new offer from remote */ |
| 2747 | inv_check_sdp_in_incoming_msg(inv, pjsip_rdata_get_tsx(rdata), rdata); |
| 2748 | |
| 2749 | /* Application MUST have supplied the answer by now. |
| 2750 | * If so, negotiate the SDP. |
| 2751 | */ |
| 2752 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 2753 | if (neg_state != PJMEDIA_SDP_NEG_STATE_WAIT_NEGO || |
| 2754 | (status=inv_negotiate_sdp(inv)) != PJ_SUCCESS) |
| 2755 | { |
| 2756 | /* Negotiation has failed */ |
| 2757 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 2758 | PJSIP_SC_NOT_ACCEPTABLE_HERE, |
| 2759 | NULL, &tdata); |
| 2760 | } else { |
| 2761 | /* New media has been negotiated successfully, send 200/OK */ |
| 2762 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 2763 | PJSIP_SC_OK, NULL, &tdata); |
| 2764 | if (status == PJ_SUCCESS) { |
Benny Prijono | 9569a0b | 2007-10-04 15:35:26 +0000 | [diff] [blame] | 2765 | const pjmedia_sdp_session *sdp; |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2766 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &sdp); |
| 2767 | if (status == PJ_SUCCESS) |
| 2768 | tdata->msg->body = create_sdp_body(tdata->pool, sdp); |
| 2769 | } |
| 2770 | } |
| 2771 | } |
| 2772 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 2773 | on_return: |
| 2774 | /* Invoke Session Timers */ |
| 2775 | if (status == PJ_SUCCESS) |
| 2776 | status = pjsip_timer_update_resp(inv, tdata); |
| 2777 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2778 | if (status != PJ_SUCCESS) { |
| 2779 | if (tdata != NULL) { |
| 2780 | pjsip_tx_data_dec_ref(tdata); |
| 2781 | tdata = NULL; |
| 2782 | } |
| 2783 | return; |
| 2784 | } |
| 2785 | |
| 2786 | pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), tdata); |
| 2787 | } |
| 2788 | |
| 2789 | |
| 2790 | /* |
| 2791 | * Handle incoming response to UAC UPDATE request. |
| 2792 | */ |
| 2793 | static void inv_handle_update_response( pjsip_inv_session *inv, |
| 2794 | pjsip_event *e) |
| 2795 | { |
| 2796 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 2797 | struct tsx_inv_data *tsx_inv_data = NULL; |
| 2798 | pj_status_t status = -1; |
| 2799 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 2800 | /* Handle 401/407 challenge. */ |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2801 | if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 2802 | (tsx->status_code == 401 || tsx->status_code == 407)) |
| 2803 | { |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 2804 | pjsip_tx_data *tdata; |
| 2805 | |
| 2806 | status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, |
| 2807 | e->body.tsx_state.src.rdata, |
| 2808 | tsx->last_tx, |
| 2809 | &tdata); |
| 2810 | |
| 2811 | if (status != PJ_SUCCESS) { |
| 2812 | |
Benny Prijono | b2ad04a | 2008-06-26 13:24:10 +0000 | [diff] [blame] | 2813 | /* Somehow failed. Probably it's not a good idea to terminate |
| 2814 | * the session since this is just a request within dialog. And |
| 2815 | * even if we terminate we should send BYE. |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 2816 | */ |
Benny Prijono | b2ad04a | 2008-06-26 13:24:10 +0000 | [diff] [blame] | 2817 | /* |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 2818 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
| 2819 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | b2ad04a | 2008-06-26 13:24:10 +0000 | [diff] [blame] | 2820 | */ |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 2821 | |
| 2822 | } else { |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 2823 | /* Re-send request. */ |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 2824 | status = pjsip_inv_send_msg(inv, tdata); |
| 2825 | } |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | /* Process 422 response */ |
| 2829 | else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 2830 | tsx->status_code == 422) |
| 2831 | { |
| 2832 | status = handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 2833 | PJ_FALSE); |
| 2834 | } |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 2835 | |
| 2836 | /* Process 2xx response */ |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 2837 | else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2838 | tsx->status_code/100 == 2 && |
| 2839 | e->body.tsx_state.src.rdata->msg_info.msg->body) |
| 2840 | { |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 2841 | status = handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 2842 | PJ_FALSE); |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2843 | status = inv_check_sdp_in_incoming_msg(inv, tsx, |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 2844 | e->body.tsx_state.src.rdata); |
| 2845 | } |
| 2846 | |
| 2847 | /* Get/attach invite session's transaction data */ |
| 2848 | else |
| 2849 | { |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2850 | tsx_inv_data = (struct tsx_inv_data*)tsx->mod_data[mod_inv.mod.id]; |
| 2851 | if (tsx_inv_data == NULL) { |
| 2852 | tsx_inv_data=PJ_POOL_ZALLOC_T(tsx->pool, struct tsx_inv_data); |
| 2853 | tsx_inv_data->inv = inv; |
| 2854 | tsx->mod_data[mod_inv.mod.id] = tsx_inv_data; |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | /* Otherwise if we don't get successful response, cancel |
| 2859 | * our negotiator. |
| 2860 | */ |
| 2861 | if (status != PJ_SUCCESS && |
| 2862 | pjmedia_sdp_neg_get_state(inv->neg)==PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER && |
| 2863 | tsx_inv_data && tsx_inv_data->sdp_done == PJ_FALSE) |
| 2864 | { |
| 2865 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 2866 | |
| 2867 | /* Prevent from us cancelling different offer! */ |
| 2868 | tsx_inv_data->sdp_done = PJ_TRUE; |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | |
| 2873 | /* |
| 2874 | * Handle incoming reliable response. |
| 2875 | */ |
| 2876 | static void inv_handle_incoming_reliable_response(pjsip_inv_session *inv, |
| 2877 | pjsip_rx_data *rdata) |
| 2878 | { |
| 2879 | pjsip_tx_data *tdata; |
Benny Prijono | 9569a0b | 2007-10-04 15:35:26 +0000 | [diff] [blame] | 2880 | const pjmedia_sdp_session *sdp; |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 2881 | pj_status_t status; |
| 2882 | |
| 2883 | /* Create PRACK */ |
| 2884 | status = pjsip_100rel_create_prack(inv, rdata, &tdata); |
| 2885 | if (status != PJ_SUCCESS) |
| 2886 | return; |
| 2887 | |
| 2888 | /* See if we need to attach SDP answer on the PRACK request */ |
| 2889 | sdp = inv_has_pending_answer(inv, pjsip_rdata_get_tsx(rdata)); |
| 2890 | if (sdp) { |
| 2891 | tdata->msg->body = create_sdp_body(tdata->pool, sdp); |
| 2892 | } |
| 2893 | |
| 2894 | /* Send PRACK (must be using 100rel module!) */ |
| 2895 | pjsip_100rel_send_prack(inv, tdata); |
| 2896 | } |
| 2897 | |
| 2898 | |
| 2899 | /* |
| 2900 | * Handle incoming PRACK. |
| 2901 | */ |
| 2902 | static void inv_respond_incoming_prack(pjsip_inv_session *inv, |
| 2903 | pjsip_rx_data *rdata) |
| 2904 | { |
| 2905 | pj_status_t status; |
| 2906 | |
| 2907 | /* Run through 100rel module to see if we can accept this |
| 2908 | * PRACK request. The 100rel will send 200/OK to PRACK request. |
| 2909 | */ |
| 2910 | status = pjsip_100rel_on_rx_prack(inv, rdata); |
| 2911 | if (status != PJ_SUCCESS) |
| 2912 | return; |
| 2913 | |
| 2914 | /* Now check for SDP answer in the PRACK request */ |
| 2915 | if (rdata->msg_info.msg->body) { |
| 2916 | status = inv_check_sdp_in_incoming_msg(inv, |
| 2917 | pjsip_rdata_get_tsx(rdata), rdata); |
| 2918 | } else { |
| 2919 | /* No SDP body */ |
| 2920 | status = -1; |
| 2921 | } |
| 2922 | |
| 2923 | /* If SDP negotiation has been successful, also mark the |
| 2924 | * SDP negotiation flag in the invite transaction to be |
| 2925 | * done too. |
| 2926 | */ |
| 2927 | if (status == PJ_SUCCESS && inv->invite_tsx) { |
| 2928 | struct tsx_inv_data *tsx_inv_data; |
| 2929 | |
| 2930 | /* Get/attach invite session's transaction data */ |
| 2931 | tsx_inv_data = (struct tsx_inv_data*) |
| 2932 | inv->invite_tsx->mod_data[mod_inv.mod.id]; |
| 2933 | if (tsx_inv_data == NULL) { |
| 2934 | tsx_inv_data = PJ_POOL_ZALLOC_T(inv->invite_tsx->pool, |
| 2935 | struct tsx_inv_data); |
| 2936 | tsx_inv_data->inv = inv; |
| 2937 | inv->invite_tsx->mod_data[mod_inv.mod.id] = tsx_inv_data; |
| 2938 | } |
| 2939 | |
| 2940 | tsx_inv_data->sdp_done = PJ_TRUE; |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | |
| 2945 | /* |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2946 | * State NULL is before anything is sent/received. |
| 2947 | */ |
| 2948 | static void inv_on_state_null( pjsip_inv_session *inv, pjsip_event *e) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2949 | { |
| 2950 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 2951 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 2952 | |
| 2953 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 2954 | |
| 2955 | if (tsx->method.id == PJSIP_INVITE_METHOD) { |
| 2956 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 2957 | /* Keep the initial INVITE transaction. */ |
| 2958 | if (inv->invite_tsx == NULL) |
| 2959 | inv->invite_tsx = tsx; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2960 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 2961 | if (dlg->role == PJSIP_ROLE_UAC) { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2962 | |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 2963 | /* Save the original INVITE request, if on_redirected() callback |
| 2964 | * is implemented. We may need to resend the INVITE if we receive |
| 2965 | * redirection response. |
| 2966 | */ |
| 2967 | if (mod_inv.cb.on_redirected) { |
| 2968 | if (inv->invite_req) { |
| 2969 | pjsip_tx_data_dec_ref(inv->invite_req); |
| 2970 | inv->invite_req = NULL; |
| 2971 | } |
| 2972 | inv->invite_req = tsx->last_tx; |
| 2973 | pjsip_tx_data_add_ref(inv->invite_req); |
| 2974 | } |
| 2975 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2976 | switch (tsx->state) { |
| 2977 | case PJSIP_TSX_STATE_CALLING: |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2978 | inv_set_state(inv, PJSIP_INV_STATE_CALLING, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2979 | break; |
| 2980 | default: |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 2981 | inv_on_state_calling(inv, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2982 | break; |
| 2983 | } |
| 2984 | |
| 2985 | } else { |
| 2986 | switch (tsx->state) { |
| 2987 | case PJSIP_TSX_STATE_TRYING: |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 2988 | inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 2989 | break; |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 2990 | case PJSIP_TSX_STATE_PROCEEDING: |
| 2991 | inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e); |
| 2992 | if (tsx->status_code > 100) |
| 2993 | inv_set_state(inv, PJSIP_INV_STATE_EARLY, e); |
| 2994 | break; |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 2995 | case PJSIP_TSX_STATE_TERMINATED: |
| 2996 | /* there is a failure in sending response. */ |
| 2997 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 2998 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 2999 | break; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3000 | default: |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 3001 | inv_on_state_incoming(inv, e); |
| 3002 | break; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | } else { |
| 3007 | pj_assert(!"Unexpected transaction type"); |
| 3008 | } |
| 3009 | } |
| 3010 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3011 | /* |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3012 | * Generic UAC transaction handler: |
| 3013 | * - resend request on 401 or 407 response. |
| 3014 | * - terminate dialog on 408 and 481 response. |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3015 | * - resend request on 422 response. |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3016 | */ |
| 3017 | static pj_bool_t handle_uac_tsx_response(pjsip_inv_session *inv, |
| 3018 | pjsip_event *e) |
| 3019 | { |
| 3020 | /* RFC 3261 Section 12.2.1.2: |
| 3021 | * If the response for a request within a dialog is a 481 |
| 3022 | * (Call/Transaction Does Not Exist) or a 408 (Request Timeout), the UAC |
| 3023 | * SHOULD terminate the dialog. A UAC SHOULD also terminate a dialog if |
| 3024 | * no response at all is received for the request (the client |
| 3025 | * transaction would inform the TU about the timeout.) |
| 3026 | * |
| 3027 | * For INVITE initiated dialogs, terminating the dialog consists of |
| 3028 | * sending a BYE. |
| 3029 | * |
| 3030 | * Note: |
| 3031 | * according to X, this should terminate dialog usage only, not the |
| 3032 | * dialog. |
| 3033 | */ |
| 3034 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3035 | |
| 3036 | pj_assert(tsx->role == PJSIP_UAC_ROLE); |
| 3037 | |
| 3038 | /* Note that 481 response to CANCEL does not terminate dialog usage, |
| 3039 | * but only the transaction. |
| 3040 | */ |
Benny Prijono | 61fc5e6 | 2008-06-25 18:35:31 +0000 | [diff] [blame] | 3041 | if (inv->state != PJSIP_INV_STATE_DISCONNECTED && |
| 3042 | ((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST && |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3043 | tsx->method.id != PJSIP_CANCEL_METHOD) || |
Benny Prijono | 61fc5e6 | 2008-06-25 18:35:31 +0000 | [diff] [blame] | 3044 | tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT || |
| 3045 | tsx->status_code == PJSIP_SC_TSX_TIMEOUT || |
| 3046 | tsx->status_code == PJSIP_SC_TSX_TRANSPORT_ERROR)) |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3047 | { |
| 3048 | pjsip_tx_data *bye; |
| 3049 | pj_status_t status; |
| 3050 | |
| 3051 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3052 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3053 | |
| 3054 | /* Send BYE */ |
| 3055 | status = pjsip_dlg_create_request(inv->dlg, pjsip_get_bye_method(), |
| 3056 | -1, &bye); |
| 3057 | if (status == PJ_SUCCESS) { |
| 3058 | pjsip_inv_send_msg(inv, bye); |
| 3059 | } |
| 3060 | |
| 3061 | return PJ_TRUE; /* Handled */ |
| 3062 | |
| 3063 | } |
| 3064 | /* Handle 401/407 challenge. */ |
| 3065 | else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3066 | (tsx->status_code == PJSIP_SC_UNAUTHORIZED || |
| 3067 | tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED)) |
| 3068 | { |
| 3069 | |
| 3070 | pjsip_tx_data *tdata; |
| 3071 | pj_status_t status; |
| 3072 | |
Benny Prijono | b2ad04a | 2008-06-26 13:24:10 +0000 | [diff] [blame] | 3073 | if (tsx->method.id == PJSIP_INVITE_METHOD) |
| 3074 | inv->invite_tsx = NULL; |
| 3075 | |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3076 | status = pjsip_auth_clt_reinit_req( &inv->dlg->auth_sess, |
| 3077 | e->body.tsx_state.src.rdata, |
| 3078 | tsx->last_tx, &tdata); |
| 3079 | |
| 3080 | if (status != PJ_SUCCESS) { |
Benny Prijono | b2ad04a | 2008-06-26 13:24:10 +0000 | [diff] [blame] | 3081 | /* Somehow failed. Probably it's not a good idea to terminate |
| 3082 | * the session since this is just a request within dialog. And |
| 3083 | * even if we terminate we should send BYE. |
| 3084 | */ |
| 3085 | /* |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3086 | inv_set_cause(inv, PJSIP_SC_OK, NULL); |
| 3087 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | b2ad04a | 2008-06-26 13:24:10 +0000 | [diff] [blame] | 3088 | */ |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3089 | |
| 3090 | } else { |
| 3091 | /* Re-send request. */ |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3092 | status = pjsip_inv_send_msg(inv, tdata); |
| 3093 | } |
| 3094 | |
| 3095 | return PJ_TRUE; /* Handled */ |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | /* Handle session timer 422 response. */ |
| 3099 | else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3100 | tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL) |
| 3101 | { |
| 3102 | handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 3103 | PJ_FALSE); |
| 3104 | |
| 3105 | return PJ_TRUE; /* Handled */ |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3106 | |
| 3107 | } else { |
| 3108 | return PJ_FALSE; /* Unhandled */ |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 3113 | /* Handle call rejection, especially with regard to processing call |
| 3114 | * redirection. We need to handle the following scenarios: |
| 3115 | * - 3xx response is received -- see if on_redirected() callback is |
| 3116 | * implemented. If so, add the Contact URIs in the response to the |
| 3117 | * target set and notify user. |
| 3118 | * - 4xx - 6xx resposne is received -- see if we're currently recursing, |
| 3119 | * if so fetch the next target if any and notify the on_redirected() |
| 3120 | * callback. |
| 3121 | * - for other cases -- disconnect the session. |
| 3122 | */ |
| 3123 | static void handle_uac_call_rejection(pjsip_inv_session *inv, pjsip_event *e) |
| 3124 | { |
| 3125 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3126 | pj_status_t status; |
| 3127 | |
| 3128 | if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 300)) { |
| 3129 | |
| 3130 | if (mod_inv.cb.on_redirected == NULL) { |
| 3131 | |
| 3132 | /* Redirection callback is not implemented, disconnect the |
| 3133 | * call. |
| 3134 | */ |
| 3135 | goto terminate_session; |
| 3136 | |
| 3137 | } else { |
| 3138 | const pjsip_msg *res_msg; |
| 3139 | |
| 3140 | res_msg = e->body.tsx_state.src.rdata->msg_info.msg; |
| 3141 | |
| 3142 | /* Gather all Contact URI's in the response and add them |
| 3143 | * to target set. The function will take care of removing |
| 3144 | * duplicate URI's. |
| 3145 | */ |
| 3146 | pjsip_target_set_add_from_msg(&inv->dlg->target_set, |
| 3147 | inv->dlg->pool, res_msg); |
| 3148 | |
| 3149 | /* Recurse to alternate targets if application allows us */ |
| 3150 | if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) |
| 3151 | { |
| 3152 | /* Recursion fails, terminate session now */ |
| 3153 | goto terminate_session; |
| 3154 | } |
| 3155 | |
| 3156 | /* Done */ |
| 3157 | } |
| 3158 | |
| 3159 | } else if ((tsx->status_code==401 || tsx->status_code==407) && |
| 3160 | !inv->cancelling) |
| 3161 | { |
| 3162 | |
| 3163 | /* Handle authentication failure: |
| 3164 | * Resend the request with Authorization header. |
| 3165 | */ |
| 3166 | pjsip_tx_data *tdata; |
| 3167 | |
| 3168 | status = pjsip_auth_clt_reinit_req(&inv->dlg->auth_sess, |
| 3169 | e->body.tsx_state.src.rdata, |
| 3170 | tsx->last_tx, |
| 3171 | &tdata); |
| 3172 | |
| 3173 | if (status != PJ_SUCCESS) { |
| 3174 | |
| 3175 | /* Does not have proper credentials. If we are currently |
| 3176 | * recursing, try the next target. Otherwise end the session. |
| 3177 | */ |
| 3178 | if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) |
| 3179 | { |
| 3180 | /* Recursion fails, terminate session now */ |
| 3181 | goto terminate_session; |
| 3182 | } |
| 3183 | |
| 3184 | } else { |
| 3185 | |
| 3186 | /* Restart session. */ |
| 3187 | pjsip_inv_uac_restart(inv, PJ_FALSE); |
| 3188 | |
| 3189 | /* Send the request. */ |
| 3190 | status = pjsip_inv_send_msg(inv, tdata); |
| 3191 | } |
| 3192 | |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3193 | } else if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 3194 | tsx->status_code == PJSIP_SC_SESSION_TIMER_TOO_SMALL) |
| 3195 | { |
| 3196 | /* Handle session timer 422 response: |
| 3197 | * Resend the request with requested session timer setting. |
| 3198 | */ |
| 3199 | status = handle_timer_response(inv, e->body.tsx_state.src.rdata, |
| 3200 | PJ_TRUE); |
| 3201 | |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 3202 | } else if (PJSIP_IS_STATUS_IN_CLASS(tsx->status_code, 600)) { |
| 3203 | /* Global error */ |
| 3204 | goto terminate_session; |
| 3205 | |
| 3206 | } else { |
| 3207 | /* See if we have alternate target to try */ |
| 3208 | if (!inv_uac_recurse(inv, tsx->status_code, &tsx->status_text, e)) { |
| 3209 | /* Recursion fails, terminate session now */ |
| 3210 | goto terminate_session; |
| 3211 | } |
| 3212 | } |
| 3213 | return; |
| 3214 | |
| 3215 | terminate_session: |
| 3216 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
| 3217 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3218 | } |
| 3219 | |
| 3220 | |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3221 | /* |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3222 | * State CALLING is after sending initial INVITE request but before |
| 3223 | * any response (with tag) is received. |
| 3224 | */ |
| 3225 | static void inv_on_state_calling( pjsip_inv_session *inv, pjsip_event *e) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3226 | { |
| 3227 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3228 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 3229 | pj_status_t status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3230 | |
| 3231 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3232 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3233 | if (tsx == inv->invite_tsx) { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3234 | |
| 3235 | switch (tsx->state) { |
| 3236 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 3237 | case PJSIP_TSX_STATE_CALLING: |
| 3238 | inv_set_state(inv, PJSIP_INV_STATE_CALLING, e); |
| 3239 | break; |
| 3240 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3241 | case PJSIP_TSX_STATE_PROCEEDING: |
Benny Prijono | 1dc8be0 | 2007-05-30 04:26:40 +0000 | [diff] [blame] | 3242 | if (inv->pending_cancel) { |
| 3243 | pjsip_tx_data *cancel; |
| 3244 | |
| 3245 | inv->pending_cancel = PJ_FALSE; |
| 3246 | |
| 3247 | status = pjsip_inv_end_session(inv, 487, NULL, &cancel); |
| 3248 | if (status == PJ_SUCCESS && cancel) |
| 3249 | status = pjsip_inv_send_msg(inv, cancel); |
| 3250 | } |
| 3251 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3252 | if (dlg->remote.info->tag.slen) { |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3253 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3254 | inv_set_state(inv, PJSIP_INV_STATE_EARLY, e); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3255 | |
| 3256 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3257 | e->body.tsx_state.src.rdata); |
| 3258 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3259 | if (pjsip_100rel_is_reliable(e->body.tsx_state.src.rdata)) { |
| 3260 | inv_handle_incoming_reliable_response( |
| 3261 | inv, e->body.tsx_state.src.rdata); |
| 3262 | } |
| 3263 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3264 | } else { |
| 3265 | /* Ignore 100 (Trying) response, as it doesn't change |
| 3266 | * session state. It only ceases retransmissions. |
| 3267 | */ |
| 3268 | } |
| 3269 | break; |
| 3270 | |
| 3271 | case PJSIP_TSX_STATE_COMPLETED: |
| 3272 | if (tsx->status_code/100 == 2) { |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3273 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3274 | /* This should not happen. |
| 3275 | * When transaction receives 2xx, it should be terminated |
| 3276 | */ |
| 3277 | pj_assert(0); |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3278 | |
| 3279 | /* Process session timer response. */ |
| 3280 | status = handle_timer_response(inv, |
| 3281 | e->body.tsx_state.src.rdata, |
| 3282 | PJ_TRUE); |
| 3283 | if (status != PJ_SUCCESS) |
| 3284 | break; |
| 3285 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3286 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3287 | |
| 3288 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3289 | e->body.tsx_state.src.rdata); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3290 | |
| 3291 | } else { |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 3292 | handle_uac_call_rejection(inv, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3293 | } |
| 3294 | break; |
| 3295 | |
| 3296 | case PJSIP_TSX_STATE_TERMINATED: |
| 3297 | /* INVITE transaction can be terminated either because UAC |
| 3298 | * transaction received 2xx response or because of transport |
| 3299 | * error. |
| 3300 | */ |
| 3301 | if (tsx->status_code/100 == 2) { |
| 3302 | /* This must be receipt of 2xx response */ |
| 3303 | |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3304 | /* Process session timer response. */ |
| 3305 | status = handle_timer_response(inv, |
| 3306 | e->body.tsx_state.src.rdata, |
| 3307 | PJ_TRUE); |
| 3308 | if (status != PJ_SUCCESS) |
| 3309 | break; |
| 3310 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3311 | /* Set state to CONNECTING */ |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3312 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3313 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3314 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3315 | e->body.tsx_state.src.rdata); |
| 3316 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3317 | /* Send ACK */ |
| 3318 | pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 3319 | |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 3320 | inv_send_ack(inv, e); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3321 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3322 | } else { |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 3323 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3324 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3325 | } |
| 3326 | break; |
| 3327 | |
Benny Prijono | 34a404e | 2006-02-09 14:38:30 +0000 | [diff] [blame] | 3328 | default: |
| 3329 | break; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3330 | } |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 3331 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3332 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 3333 | /* |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3334 | * Handle case when outgoing request is answered with 481 (Call/ |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 3335 | * Transaction Does Not Exist), 408, or when it's timed out. In these |
| 3336 | * cases, disconnect session (i.e. dialog usage only). |
Benny Prijono | c514576 | 2007-11-23 12:04:40 +0000 | [diff] [blame] | 3337 | * Note that 481 response to CANCEL does not terminate dialog usage, |
| 3338 | * but only the transaction. |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 3339 | */ |
Benny Prijono | c514576 | 2007-11-23 12:04:40 +0000 | [diff] [blame] | 3340 | if ((tsx->status_code == PJSIP_SC_CALL_TSX_DOES_NOT_EXIST && |
| 3341 | tsx->method.id != PJSIP_CANCEL_METHOD) || |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 3342 | tsx->status_code == PJSIP_SC_REQUEST_TIMEOUT || |
| 3343 | tsx->status_code == PJSIP_SC_TSX_TIMEOUT || |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 3344 | tsx->status_code == PJSIP_SC_TSX_TRANSPORT_ERROR) |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 3345 | { |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 3346 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 3347 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
| 3348 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3349 | } |
| 3350 | } |
| 3351 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3352 | /* |
| 3353 | * State INCOMING is after we received the request, but before |
| 3354 | * responses with tag are sent. |
| 3355 | */ |
| 3356 | static void inv_on_state_incoming( pjsip_inv_session *inv, pjsip_event *e) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3357 | { |
| 3358 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3359 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 3360 | |
| 3361 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3362 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3363 | if (tsx == inv->invite_tsx) { |
| 3364 | |
| 3365 | /* |
| 3366 | * Handle the INVITE state transition. |
| 3367 | */ |
| 3368 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3369 | switch (tsx->state) { |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3370 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 3371 | case PJSIP_TSX_STATE_TRYING: |
| 3372 | inv_set_state(inv, PJSIP_INV_STATE_INCOMING, e); |
| 3373 | break; |
| 3374 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3375 | case PJSIP_TSX_STATE_PROCEEDING: |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3376 | /* |
| 3377 | * Transaction sent provisional response. |
| 3378 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3379 | if (tsx->status_code > 100) |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3380 | inv_set_state(inv, PJSIP_INV_STATE_EARLY, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3381 | break; |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3382 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3383 | case PJSIP_TSX_STATE_COMPLETED: |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3384 | /* |
| 3385 | * Transaction sent final response. |
| 3386 | */ |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 3387 | if (tsx->status_code/100 == 2) { |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3388 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 3389 | } else { |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 3390 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3391 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 3392 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3393 | break; |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3394 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3395 | case PJSIP_TSX_STATE_TERMINATED: |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3396 | /* |
| 3397 | * This happens on transport error (e.g. failed to send |
| 3398 | * response) |
| 3399 | */ |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 3400 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3401 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3402 | break; |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3403 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3404 | default: |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3405 | pj_assert(!"Unexpected INVITE state"); |
| 3406 | break; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3407 | } |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3408 | |
| 3409 | } else if (tsx->method.id == PJSIP_CANCEL_METHOD && |
| 3410 | tsx->role == PJSIP_ROLE_UAS && |
| 3411 | tsx->state < PJSIP_TSX_STATE_COMPLETED && |
| 3412 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG ) |
| 3413 | { |
| 3414 | |
| 3415 | /* |
| 3416 | * Handle incoming CANCEL request. |
| 3417 | */ |
| 3418 | |
| 3419 | inv_respond_incoming_cancel(inv, tsx, e->body.tsx_state.src.rdata); |
| 3420 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3424 | /* |
| 3425 | * State EARLY is for both UAS and UAC, after response with To tag |
| 3426 | * is sent/received. |
| 3427 | */ |
| 3428 | static void inv_on_state_early( pjsip_inv_session *inv, pjsip_event *e) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3429 | { |
| 3430 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3431 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 3432 | |
| 3433 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3434 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3435 | if (tsx == inv->invite_tsx) { |
| 3436 | |
| 3437 | /* |
| 3438 | * Handle the INVITE state progress. |
| 3439 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3440 | |
| 3441 | switch (tsx->state) { |
| 3442 | |
| 3443 | case PJSIP_TSX_STATE_PROCEEDING: |
| 3444 | /* Send/received another provisional response. */ |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3445 | inv_set_state(inv, PJSIP_INV_STATE_EARLY, e); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3446 | |
| 3447 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 3448 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3449 | e->body.tsx_state.src.rdata); |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3450 | |
| 3451 | if (pjsip_100rel_is_reliable(e->body.tsx_state.src.rdata)) { |
| 3452 | inv_handle_incoming_reliable_response( |
| 3453 | inv, e->body.tsx_state.src.rdata); |
| 3454 | } |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3455 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3456 | break; |
| 3457 | |
| 3458 | case PJSIP_TSX_STATE_COMPLETED: |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3459 | if (tsx->status_code/100 == 2) { |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3460 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3461 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3462 | pj_status_t status; |
| 3463 | |
| 3464 | /* Process session timer response. */ |
| 3465 | status = handle_timer_response(inv, |
| 3466 | e->body.tsx_state.src.rdata, |
| 3467 | PJ_TRUE); |
| 3468 | if (status != PJ_SUCCESS) |
| 3469 | break; |
| 3470 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3471 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3472 | e->body.tsx_state.src.rdata); |
| 3473 | } |
| 3474 | |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 3475 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 3476 | |
| 3477 | handle_uac_call_rejection(inv, e); |
| 3478 | |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 3479 | } else { |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 3480 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3481 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | d4e0abd | 2006-03-05 11:53:36 +0000 | [diff] [blame] | 3482 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3483 | break; |
| 3484 | |
Benny Prijono | f319507 | 2006-02-14 21:15:30 +0000 | [diff] [blame] | 3485 | case PJSIP_TSX_STATE_CONFIRMED: |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 3486 | /* For some reason can go here (maybe when ACK for 2xx has |
| 3487 | * the same branch value as the INVITE transaction) */ |
Benny Prijono | f319507 | 2006-02-14 21:15:30 +0000 | [diff] [blame] | 3488 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3489 | case PJSIP_TSX_STATE_TERMINATED: |
| 3490 | /* INVITE transaction can be terminated either because UAC |
| 3491 | * transaction received 2xx response or because of transport |
| 3492 | * error. |
| 3493 | */ |
| 3494 | if (tsx->status_code/100 == 2) { |
| 3495 | |
| 3496 | /* This must be receipt of 2xx response */ |
| 3497 | |
| 3498 | /* Set state to CONNECTING */ |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3499 | inv_set_state(inv, PJSIP_INV_STATE_CONNECTING, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3500 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3501 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3502 | pj_status_t status; |
| 3503 | |
| 3504 | /* Process session timer response. */ |
| 3505 | status = handle_timer_response(inv, |
| 3506 | e->body.tsx_state.src.rdata, |
| 3507 | PJ_TRUE); |
| 3508 | if (status != PJ_SUCCESS) |
| 3509 | break; |
| 3510 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3511 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3512 | e->body.tsx_state.src.rdata); |
| 3513 | } |
| 3514 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3515 | /* if UAC, send ACK and move state to confirmed. */ |
| 3516 | if (tsx->role == PJSIP_ROLE_UAC) { |
| 3517 | pj_assert(e->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 3518 | |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 3519 | inv_send_ack(inv, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | } else { |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 3523 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3524 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3525 | } |
| 3526 | break; |
| 3527 | |
| 3528 | default: |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3529 | pj_assert(!"Unexpected INVITE tsx state"); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3532 | } else if (inv->role == PJSIP_ROLE_UAS && |
| 3533 | tsx->role == PJSIP_ROLE_UAS && |
| 3534 | tsx->method.id == PJSIP_CANCEL_METHOD && |
| 3535 | tsx->state < PJSIP_TSX_STATE_COMPLETED && |
| 3536 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG ) |
| 3537 | { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3538 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3539 | /* |
| 3540 | * Handle incoming CANCEL request. |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 3541 | */ |
| 3542 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3543 | inv_respond_incoming_cancel(inv, tsx, e->body.tsx_state.src.rdata); |
| 3544 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3545 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 3546 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 3547 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 3548 | { |
| 3549 | /* |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3550 | * Handle incoming UPDATE |
| 3551 | */ |
| 3552 | inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata); |
| 3553 | |
| 3554 | |
| 3555 | } else if (tsx->role == PJSIP_ROLE_UAC && |
| 3556 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 3557 | tsx->state == PJSIP_TSX_STATE_TERMINATED) && |
| 3558 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 3559 | { |
| 3560 | /* |
| 3561 | * Handle response to outgoing UPDATE request. |
| 3562 | */ |
| 3563 | inv_handle_update_response(inv, e); |
| 3564 | |
| 3565 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 3566 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 3567 | pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0) |
| 3568 | { |
| 3569 | /* |
| 3570 | * Handle incoming PRACK |
| 3571 | */ |
| 3572 | inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata); |
| 3573 | |
| 3574 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3575 | |
| 3576 | /* Generic handling for UAC tsx completion */ |
| 3577 | handle_uac_tsx_response(inv, e); |
Benny Prijono | 7efa2d6 | 2009-04-27 12:50:16 +0000 | [diff] [blame] | 3578 | |
| 3579 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 3580 | tsx->method.id == PJSIP_BYE_METHOD && |
| 3581 | tsx->status_code < 200 && |
| 3582 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 3583 | { |
| 3584 | /* Received BYE before the 2xx/OK response to INVITE. |
| 3585 | * Assume that the 2xx/OK response is lost and the BYE |
| 3586 | * arrives earlier. |
| 3587 | */ |
| 3588 | inv_respond_incoming_bye(inv, tsx, e->body.tsx_state.src.rdata, e); |
| 3589 | |
| 3590 | /* Set timer just in case we will never get the final response |
| 3591 | * for INVITE. |
| 3592 | */ |
| 3593 | pjsip_tsx_set_timeout(inv->invite_tsx, 64*pjsip_cfg()->tsx.t1); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3594 | } |
| 3595 | } |
| 3596 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3597 | /* |
| 3598 | * State CONNECTING is after 2xx response to INVITE is sent/received. |
| 3599 | */ |
| 3600 | static void inv_on_state_connecting( pjsip_inv_session *inv, pjsip_event *e) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3601 | { |
| 3602 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3603 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 3604 | |
| 3605 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3606 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3607 | if (tsx == inv->invite_tsx) { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3608 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3609 | /* |
| 3610 | * Handle INVITE state progression. |
| 3611 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3612 | switch (tsx->state) { |
| 3613 | |
| 3614 | case PJSIP_TSX_STATE_CONFIRMED: |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 3615 | /* It can only go here if incoming ACK request has the same Via |
| 3616 | * branch parameter as the INVITE transaction. |
| 3617 | */ |
| 3618 | if (tsx->status_code/100 == 2) { |
| 3619 | if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 3620 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3621 | e->body.tsx_state.src.rdata); |
| 3622 | } |
| 3623 | |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 3624 | inv_set_state(inv, PJSIP_INV_STATE_CONFIRMED, e); |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 3625 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3626 | break; |
| 3627 | |
| 3628 | case PJSIP_TSX_STATE_TERMINATED: |
| 3629 | /* INVITE transaction can be terminated either because UAC |
| 3630 | * transaction received 2xx response or because of transport |
| 3631 | * error. |
| 3632 | */ |
| 3633 | if (tsx->status_code/100 != 2) { |
Benny Prijono | 0b6340c | 2006-06-13 22:21:23 +0000 | [diff] [blame] | 3634 | inv_set_cause(inv, tsx->status_code, &tsx->status_text); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3635 | inv_set_state(inv, PJSIP_INV_STATE_DISCONNECTED, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3636 | } |
| 3637 | break; |
| 3638 | |
| 3639 | case PJSIP_TSX_STATE_DESTROYED: |
| 3640 | /* Do nothing. */ |
| 3641 | break; |
| 3642 | |
| 3643 | default: |
| 3644 | pj_assert(!"Unexpected state"); |
| 3645 | } |
| 3646 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3647 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 3648 | tsx->method.id == PJSIP_BYE_METHOD && |
| 3649 | tsx->status_code < 200 && |
| 3650 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 3651 | { |
| 3652 | |
| 3653 | /* |
| 3654 | * Handle incoming BYE. |
| 3655 | */ |
| 3656 | |
| 3657 | inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e ); |
| 3658 | |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 3659 | } else if (tsx->method.id == PJSIP_BYE_METHOD && |
| 3660 | tsx->role == PJSIP_ROLE_UAC && |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3661 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 3662 | tsx->state == PJSIP_TSX_STATE_TERMINATED)) |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 3663 | { |
| 3664 | |
| 3665 | /* |
| 3666 | * Outgoing BYE |
| 3667 | */ |
| 3668 | inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e); |
| 3669 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3670 | } |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 3671 | else if (tsx->method.id == PJSIP_CANCEL_METHOD && |
| 3672 | tsx->role == PJSIP_ROLE_UAS && |
| 3673 | tsx->status_code < 200 && |
| 3674 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 3675 | { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 3676 | |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 3677 | /* |
| 3678 | * Handle strandled incoming CANCEL. |
| 3679 | */ |
| 3680 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 3681 | pjsip_tx_data *tdata; |
| 3682 | pj_status_t status; |
| 3683 | |
| 3684 | status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata); |
| 3685 | if (status != PJ_SUCCESS) return; |
| 3686 | |
| 3687 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 3688 | if (status != PJ_SUCCESS) return; |
| 3689 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3690 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 3691 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 3692 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 3693 | { |
| 3694 | /* |
| 3695 | * Handle incoming UPDATE |
| 3696 | */ |
| 3697 | inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata); |
| 3698 | |
| 3699 | |
| 3700 | } else if (tsx->role == PJSIP_ROLE_UAC && |
| 3701 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 3702 | tsx->state == PJSIP_TSX_STATE_TERMINATED) && |
| 3703 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 3704 | { |
| 3705 | /* |
| 3706 | * Handle response to outgoing UPDATE request. |
| 3707 | */ |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3708 | if (handle_uac_tsx_response(inv, e) == PJ_FALSE) |
Benny Prijono | 8740238 | 2008-02-21 19:28:21 +0000 | [diff] [blame] | 3709 | inv_handle_update_response(inv, e); |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3710 | |
| 3711 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 3712 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 3713 | pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0) |
| 3714 | { |
| 3715 | /* |
| 3716 | * Handle incoming PRACK |
| 3717 | */ |
| 3718 | inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata); |
| 3719 | |
| 3720 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
Benny Prijono | 8740238 | 2008-02-21 19:28:21 +0000 | [diff] [blame] | 3721 | |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3722 | /* Generic handling for UAC tsx completion */ |
| 3723 | handle_uac_tsx_response(inv, e); |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 3724 | } |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 3725 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3728 | /* |
| 3729 | * State CONFIRMED is after ACK is sent/received. |
| 3730 | */ |
| 3731 | static void inv_on_state_confirmed( pjsip_inv_session *inv, pjsip_event *e) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3732 | { |
| 3733 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 3734 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
| 3735 | |
| 3736 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 3737 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3738 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3739 | if (tsx->method.id == PJSIP_BYE_METHOD && |
| 3740 | tsx->role == PJSIP_ROLE_UAC && |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3741 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 3742 | tsx->state == PJSIP_TSX_STATE_TERMINATED)) |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3743 | { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 3744 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3745 | /* |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 3746 | * Outgoing BYE |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3747 | */ |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3748 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame] | 3749 | inv_handle_bye_response( inv, tsx, e->body.tsx_state.src.rdata, e); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3750 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3751 | } |
| 3752 | else if (tsx->method.id == PJSIP_BYE_METHOD && |
| 3753 | tsx->role == PJSIP_ROLE_UAS && |
| 3754 | tsx->status_code < 200 && |
| 3755 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 3756 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 3757 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3758 | /* |
| 3759 | * Handle incoming BYE. |
| 3760 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3761 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 3762 | inv_respond_incoming_bye( inv, tsx, e->body.tsx_state.src.rdata, e ); |
| 3763 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 3764 | } |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 3765 | else if (tsx->method.id == PJSIP_CANCEL_METHOD && |
| 3766 | tsx->role == PJSIP_ROLE_UAS && |
| 3767 | tsx->status_code < 200 && |
| 3768 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 3769 | { |
| 3770 | |
| 3771 | /* |
| 3772 | * Handle strandled incoming CANCEL. |
| 3773 | */ |
| 3774 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 3775 | pjsip_tx_data *tdata; |
| 3776 | pj_status_t status; |
| 3777 | |
| 3778 | status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata); |
| 3779 | if (status != PJ_SUCCESS) return; |
| 3780 | |
| 3781 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 3782 | if (status != PJ_SUCCESS) return; |
| 3783 | |
| 3784 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3785 | else if (tsx->method.id == PJSIP_INVITE_METHOD && |
| 3786 | tsx->role == PJSIP_ROLE_UAS) |
| 3787 | { |
| 3788 | |
| 3789 | /* |
| 3790 | * Handle incoming re-INVITE |
| 3791 | */ |
| 3792 | if (tsx->state == PJSIP_TSX_STATE_TRYING) { |
| 3793 | |
| 3794 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
| 3795 | pjsip_tx_data *tdata; |
| 3796 | pj_status_t status; |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 3797 | pjsip_status_code st_code; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3798 | |
| 3799 | /* Check if we have INVITE pending. */ |
| 3800 | if (inv->invite_tsx && inv->invite_tsx!=tsx) { |
Benny Prijono | 4624994 | 2007-02-19 22:23:14 +0000 | [diff] [blame] | 3801 | pj_str_t reason; |
| 3802 | |
| 3803 | reason = pj_str("Another INVITE transaction in progress"); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3804 | |
| 3805 | /* Can not receive re-INVITE while another one is pending. */ |
Benny Prijono | 4624994 | 2007-02-19 22:23:14 +0000 | [diff] [blame] | 3806 | status = pjsip_dlg_create_response( inv->dlg, rdata, 500, |
| 3807 | &reason, &tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3808 | if (status != PJ_SUCCESS) |
| 3809 | return; |
| 3810 | |
| 3811 | status = pjsip_dlg_send_response( inv->dlg, tsx, tdata); |
| 3812 | |
| 3813 | |
| 3814 | return; |
| 3815 | } |
| 3816 | |
| 3817 | /* Save the invite transaction. */ |
| 3818 | inv->invite_tsx = tsx; |
| 3819 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 3820 | /* Process session timers headers in the re-INVITE */ |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 3821 | status = pjsip_timer_process_req(inv, rdata, &st_code); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 3822 | if (status != PJ_SUCCESS) { |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 3823 | status = pjsip_dlg_create_response(inv->dlg, rdata, st_code, |
| 3824 | NULL, &tdata); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 3825 | if (status != PJ_SUCCESS) |
| 3826 | return; |
| 3827 | |
| 3828 | pjsip_timer_update_resp(inv, tdata); |
| 3829 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 3830 | return; |
| 3831 | } |
| 3832 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3833 | /* Process SDP in incoming message. */ |
| 3834 | status = inv_check_sdp_in_incoming_msg(inv, tsx, rdata); |
| 3835 | |
| 3836 | if (status != PJ_SUCCESS) { |
| 3837 | |
| 3838 | /* Not Acceptable */ |
| 3839 | const pjsip_hdr *accept; |
| 3840 | |
| 3841 | status = pjsip_dlg_create_response(inv->dlg, rdata, |
| 3842 | 488, NULL, &tdata); |
| 3843 | if (status != PJ_SUCCESS) |
| 3844 | return; |
| 3845 | |
| 3846 | |
| 3847 | accept = pjsip_endpt_get_capability(dlg->endpt, PJSIP_H_ACCEPT, |
| 3848 | NULL); |
| 3849 | if (accept) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3850 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3851 | pjsip_hdr_clone(tdata->pool, accept)); |
| 3852 | } |
| 3853 | |
| 3854 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 3855 | |
| 3856 | return; |
| 3857 | } |
| 3858 | |
| 3859 | /* Create 2xx ANSWER */ |
| 3860 | status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata); |
| 3861 | if (status != PJ_SUCCESS) |
| 3862 | return; |
| 3863 | |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 3864 | /* If the INVITE request has SDP body, send answer. |
| 3865 | * Otherwise generate offer from local active SDP. |
| 3866 | */ |
| 3867 | if (rdata->msg_info.msg->body != NULL) { |
| 3868 | status = process_answer(inv, 200, tdata, NULL); |
| 3869 | } else { |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3870 | /* INVITE does not have SDP. |
| 3871 | * If on_create_offer() callback is implemented, ask app. |
| 3872 | * to generate an offer, otherwise just send active local |
| 3873 | * SDP to signal that nothing gets modified. |
| 3874 | */ |
| 3875 | pjmedia_sdp_session *sdp = NULL; |
| 3876 | |
| 3877 | if (mod_inv.cb.on_create_offer) { |
| 3878 | (*mod_inv.cb.on_create_offer)(inv, &sdp); |
| 3879 | if (sdp) { |
Benny Prijono | 60e31fc | 2009-04-23 11:50:25 +0000 | [diff] [blame] | 3880 | /* Notify negotiator about the new offer. This will |
| 3881 | * fix the offer with correct SDP origin. |
| 3882 | */ |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 3883 | status = |
| 3884 | pjmedia_sdp_neg_modify_local_offer(inv->pool_prov, |
| 3885 | inv->neg, |
| 3886 | sdp); |
Benny Prijono | 60e31fc | 2009-04-23 11:50:25 +0000 | [diff] [blame] | 3887 | |
| 3888 | /* Retrieve the "fixed" offer from negotiator */ |
Benny Prijono | c8fe3df | 2009-04-29 20:56:57 +0000 | [diff] [blame] | 3889 | if (status==PJ_SUCCESS) { |
| 3890 | const pjmedia_sdp_session *lsdp = NULL; |
| 3891 | pjmedia_sdp_neg_get_neg_local(inv->neg, &lsdp); |
| 3892 | sdp = (pjmedia_sdp_session*)lsdp; |
| 3893 | } |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | if (sdp == NULL) { |
| 3898 | const pjmedia_sdp_session *active_sdp = NULL; |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 3899 | status = pjmedia_sdp_neg_send_local_offer(inv->pool_prov, |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3900 | inv->neg, |
| 3901 | &active_sdp); |
| 3902 | if (status == PJ_SUCCESS) |
| 3903 | sdp = (pjmedia_sdp_session*) active_sdp; |
| 3904 | } |
| 3905 | |
| 3906 | if (sdp) { |
| 3907 | tdata->msg->body = create_sdp_body(tdata->pool, sdp); |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 3908 | } |
| 3909 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3910 | |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 3911 | if (status != PJ_SUCCESS) { |
| 3912 | /* |
| 3913 | * SDP negotiation has failed. |
| 3914 | */ |
| 3915 | pj_status_t rc; |
| 3916 | pj_str_t reason; |
| 3917 | |
| 3918 | /* Delete the 2xx answer */ |
| 3919 | pjsip_tx_data_dec_ref(tdata); |
| 3920 | |
| 3921 | /* Create 500 response */ |
| 3922 | reason = pj_str("SDP negotiation failed"); |
| 3923 | rc = pjsip_dlg_create_response(dlg, rdata, 500, &reason, |
| 3924 | &tdata); |
| 3925 | if (rc == PJ_SUCCESS) { |
| 3926 | pjsip_warning_hdr *w; |
| 3927 | const pj_str_t *endpt_name; |
| 3928 | |
| 3929 | endpt_name = pjsip_endpt_name(dlg->endpt); |
| 3930 | w = pjsip_warning_hdr_create_from_status(tdata->pool, |
| 3931 | endpt_name, |
| 3932 | status); |
| 3933 | if (w) |
| 3934 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)w); |
| 3935 | |
| 3936 | pjsip_inv_send_msg(inv, tdata); |
| 3937 | } |
| 3938 | return; |
| 3939 | } |
| 3940 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 3941 | /* Invoke Session Timers */ |
| 3942 | pjsip_timer_update_resp(inv, tdata); |
| 3943 | |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 3944 | /* Send 2xx regardless of the status of negotiation */ |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 3945 | status = pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3946 | |
Benny Prijono | 7d91009 | 2007-06-20 04:19:46 +0000 | [diff] [blame] | 3947 | } else if (tsx->state == PJSIP_TSX_STATE_CONFIRMED) { |
| 3948 | /* This is the case where ACK has the same branch as |
| 3949 | * the INVITE request. |
| 3950 | */ |
| 3951 | if (tsx->status_code/100 == 2 && |
| 3952 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 3953 | { |
| 3954 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3955 | e->body.tsx_state.src.rdata); |
| 3956 | } |
| 3957 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | } |
| 3961 | else if (tsx->method.id == PJSIP_INVITE_METHOD && |
| 3962 | tsx->role == PJSIP_ROLE_UAC) |
| 3963 | { |
Benny Prijono | 22e48c9 | 2008-03-20 14:40:50 +0000 | [diff] [blame] | 3964 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3965 | /* |
| 3966 | * Handle outgoing re-INVITE |
| 3967 | */ |
Benny Prijono | 22e48c9 | 2008-03-20 14:40:50 +0000 | [diff] [blame] | 3968 | if (tsx->state == PJSIP_TSX_STATE_CALLING) { |
| 3969 | |
Benny Prijono | 61fc5e6 | 2008-06-25 18:35:31 +0000 | [diff] [blame] | 3970 | /* Must not have other pending INVITE transaction */ |
| 3971 | pj_assert(inv->invite_tsx==NULL || tsx==inv->invite_tsx); |
| 3972 | |
Benny Prijono | 22e48c9 | 2008-03-20 14:40:50 +0000 | [diff] [blame] | 3973 | /* Save pending invite transaction */ |
| 3974 | inv->invite_tsx = tsx; |
| 3975 | |
| 3976 | } else if (tsx->state == PJSIP_TSX_STATE_TERMINATED && |
| 3977 | tsx->status_code/100 == 2) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3978 | { |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3979 | pj_status_t status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3980 | |
| 3981 | /* Re-INVITE was accepted. */ |
| 3982 | |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 3983 | /* Process session timer response. */ |
| 3984 | status = handle_timer_response(inv, |
| 3985 | e->body.tsx_state.src.rdata, |
| 3986 | PJ_TRUE); |
| 3987 | if (status != PJ_SUCCESS) |
| 3988 | return; |
| 3989 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3990 | /* Process SDP */ |
| 3991 | inv_check_sdp_in_incoming_msg(inv, tsx, |
| 3992 | e->body.tsx_state.src.rdata); |
| 3993 | |
| 3994 | /* Send ACK */ |
Benny Prijono | d5f9f42 | 2007-11-25 04:40:07 +0000 | [diff] [blame] | 3995 | inv_send_ack(inv, e); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3996 | |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 3997 | } else if (handle_uac_tsx_response(inv, e)) { |
Benny Prijono | 8740238 | 2008-02-21 19:28:21 +0000 | [diff] [blame] | 3998 | |
| 3999 | /* Handle response that terminates dialog */ |
| 4000 | /* Nothing to do (already handled) */ |
| 4001 | |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 4002 | } else if (tsx->status_code >= 300 && tsx->status_code < 700) { |
| 4003 | |
| 4004 | pjmedia_sdp_neg_state neg_state; |
| 4005 | |
| 4006 | /* Outgoing INVITE transaction has failed, cancel SDP nego */ |
| 4007 | neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 4008 | if (neg_state == PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) { |
| 4009 | pjmedia_sdp_neg_cancel_offer(inv->neg); |
| 4010 | } |
Benny Prijono | e641a74 | 2009-05-01 12:01:28 +0000 | [diff] [blame] | 4011 | |
| 4012 | if (tsx == inv->invite_tsx) |
| 4013 | inv->invite_tsx = NULL; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4014 | } |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 4015 | |
| 4016 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4017 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 4018 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 4019 | { |
| 4020 | /* |
| 4021 | * Handle incoming UPDATE |
| 4022 | */ |
| 4023 | inv_respond_incoming_update(inv, e->body.tsx_state.src.rdata); |
| 4024 | |
| 4025 | } else if (tsx->role == PJSIP_ROLE_UAC && |
| 4026 | (tsx->state == PJSIP_TSX_STATE_COMPLETED || |
| 4027 | tsx->state == PJSIP_TSX_STATE_TERMINATED) && |
| 4028 | pjsip_method_cmp(&tsx->method, &pjsip_update_method)==0) |
| 4029 | { |
| 4030 | /* |
| 4031 | * Handle response to outgoing UPDATE request. |
| 4032 | */ |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 4033 | if (handle_uac_tsx_response(inv, e) == PJ_FALSE) |
Benny Prijono | 8740238 | 2008-02-21 19:28:21 +0000 | [diff] [blame] | 4034 | inv_handle_update_response(inv, e); |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 4035 | |
| 4036 | } else if (tsx->role == PJSIP_ROLE_UAS && |
| 4037 | tsx->state == PJSIP_TSX_STATE_TRYING && |
| 4038 | pjsip_method_cmp(&tsx->method, &pjsip_prack_method)==0) |
| 4039 | { |
| 4040 | /* |
| 4041 | * Handle strandled incoming PRACK |
| 4042 | */ |
| 4043 | inv_respond_incoming_prack(inv, e->body.tsx_state.src.rdata); |
| 4044 | |
| 4045 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 4046 | /* |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 4047 | * Handle 401/407/408/481/422 response |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 4048 | */ |
Benny Prijono | 0b4c57b | 2008-06-25 10:15:01 +0000 | [diff] [blame] | 4049 | handle_uac_tsx_response(inv, e); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4050 | } |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 4051 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4054 | /* |
| 4055 | * After session has been terminated, but before dialog is destroyed |
| 4056 | * (because dialog has other usages, or because dialog is waiting for |
| 4057 | * the last transaction to terminate). |
| 4058 | */ |
| 4059 | static void inv_on_state_disconnected( pjsip_inv_session *inv, pjsip_event *e) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 4060 | { |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4061 | pjsip_transaction *tsx = e->body.tsx_state.tsx; |
| 4062 | pjsip_dialog *dlg = pjsip_tsx_get_dlg(tsx); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 4063 | |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4064 | PJ_ASSERT_ON_FAIL(tsx && dlg, return); |
| 4065 | |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 4066 | if (tsx->role == PJSIP_ROLE_UAS && |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4067 | tsx->status_code < 200 && |
| 4068 | e->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 4069 | { |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 4070 | pjsip_rx_data *rdata = e->body.tsx_state.src.rdata; |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4071 | |
| 4072 | /* |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 4073 | * Respond BYE with 200/OK |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4074 | */ |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 4075 | if (tsx->method.id == PJSIP_BYE_METHOD) { |
| 4076 | inv_respond_incoming_bye( inv, tsx, rdata, e ); |
| 4077 | } else if (tsx->method.id == PJSIP_CANCEL_METHOD) { |
| 4078 | /* |
| 4079 | * Respond CANCEL with 200/OK too. |
| 4080 | */ |
| 4081 | pjsip_tx_data *tdata; |
| 4082 | pj_status_t status; |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4083 | |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 4084 | status = pjsip_dlg_create_response(dlg, rdata, 200, NULL, &tdata); |
| 4085 | if (status != PJ_SUCCESS) return; |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4086 | |
Benny Prijono | 7012722 | 2006-07-02 14:53:05 +0000 | [diff] [blame] | 4087 | status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 4088 | if (status != PJ_SUCCESS) return; |
| 4089 | |
| 4090 | } |
Benny Prijono | 61fc5e6 | 2008-06-25 18:35:31 +0000 | [diff] [blame] | 4091 | |
| 4092 | } else if (tsx->role == PJSIP_ROLE_UAC) { |
| 4093 | /* |
Nanang Izzuddin | cf69c28 | 2009-10-16 06:28:56 +0000 | [diff] [blame] | 4094 | * Handle 401/407/408/481/422 response |
Benny Prijono | 61fc5e6 | 2008-06-25 18:35:31 +0000 | [diff] [blame] | 4095 | */ |
| 4096 | handle_uac_tsx_response(inv, e); |
Benny Prijono | 8ad5535 | 2006-02-08 11:16:05 +0000 | [diff] [blame] | 4097 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |