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