Benny Prijono | 053f522 | 2006-11-11 16:16:04 +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 | 053f522 | 2006-11-11 16:16:04 +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_replaces.h> |
| 21 | #include <pjsip-ua/sip_inv.h> |
| 22 | #include <pjsip/print_util.h> |
| 23 | #include <pjsip/sip_endpoint.h> |
| 24 | #include <pjsip/sip_errno.h> |
| 25 | #include <pjsip/sip_parser.h> |
| 26 | #include <pjsip/sip_transport.h> |
| 27 | #include <pjsip/sip_ua_layer.h> |
| 28 | #include <pjsip/sip_util.h> |
| 29 | #include <pj/assert.h> |
Nanang Izzuddin | c549094 | 2009-08-17 15:59:21 +0000 | [diff] [blame] | 30 | #include <pj/log.h> |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 31 | #include <pj/pool.h> |
| 32 | #include <pj/string.h> |
| 33 | |
Nanang Izzuddin | c549094 | 2009-08-17 15:59:21 +0000 | [diff] [blame] | 34 | #define THIS_FILE "sip_replaces.c" |
| 35 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Replaces header vptr. |
| 39 | */ |
| 40 | static int replaces_hdr_print( pjsip_replaces_hdr *hdr, |
| 41 | char *buf, pj_size_t size); |
| 42 | static pjsip_replaces_hdr* replaces_hdr_clone( pj_pool_t *pool, |
| 43 | const pjsip_replaces_hdr *hdr); |
| 44 | static pjsip_replaces_hdr* replaces_hdr_shallow_clone( pj_pool_t *pool, |
| 45 | const pjsip_replaces_hdr*); |
| 46 | |
| 47 | static pjsip_hdr_vptr replaces_hdr_vptr = |
| 48 | { |
| 49 | (pjsip_hdr_clone_fptr) &replaces_hdr_clone, |
| 50 | (pjsip_hdr_clone_fptr) &replaces_hdr_shallow_clone, |
| 51 | (pjsip_hdr_print_fptr) &replaces_hdr_print, |
| 52 | }; |
| 53 | |
| 54 | /* Globals */ |
| 55 | static pjsip_endpoint *the_endpt; |
Nanang Izzuddin | c549094 | 2009-08-17 15:59:21 +0000 | [diff] [blame] | 56 | static pj_bool_t is_initialized; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 57 | |
| 58 | PJ_DEF(pjsip_replaces_hdr*) pjsip_replaces_hdr_create(pj_pool_t *pool) |
| 59 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 60 | pjsip_replaces_hdr *hdr = PJ_POOL_ZALLOC_T(pool, pjsip_replaces_hdr); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 61 | hdr->type = PJSIP_H_OTHER; |
| 62 | hdr->name.ptr = "Replaces"; |
| 63 | hdr->name.slen = 8; |
| 64 | hdr->vptr = &replaces_hdr_vptr; |
| 65 | pj_list_init(hdr); |
| 66 | pj_list_init(&hdr->other_param); |
| 67 | return hdr; |
| 68 | } |
| 69 | |
| 70 | static int replaces_hdr_print( pjsip_replaces_hdr *hdr, |
| 71 | char *buf, pj_size_t size) |
| 72 | { |
| 73 | char *p = buf; |
| 74 | char *endbuf = buf+size; |
| 75 | int printed; |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 76 | const pjsip_parser_const_t *pc = pjsip_parser_const(); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 77 | |
| 78 | copy_advance(p, hdr->name); |
| 79 | *p++ = ':'; |
| 80 | *p++ = ' '; |
| 81 | |
| 82 | copy_advance(p, hdr->call_id); |
| 83 | copy_advance_pair(p, ";to-tag=", 8, hdr->to_tag); |
| 84 | copy_advance_pair(p, ";from-tag=", 10, hdr->from_tag); |
| 85 | |
| 86 | if (hdr->early_only) { |
| 87 | const pj_str_t str_early_only = { ";early-only", 11 }; |
| 88 | copy_advance(p, str_early_only); |
| 89 | } |
| 90 | |
| 91 | printed = pjsip_param_print_on(&hdr->other_param, p, endbuf-p, |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 92 | &pc->pjsip_TOKEN_SPEC, |
| 93 | &pc->pjsip_TOKEN_SPEC, ';'); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 94 | if (printed < 0) |
| 95 | return printed; |
| 96 | |
| 97 | p += printed; |
| 98 | return p - buf; |
| 99 | } |
| 100 | |
| 101 | static pjsip_replaces_hdr* replaces_hdr_clone( pj_pool_t *pool, |
| 102 | const pjsip_replaces_hdr *rhs) |
| 103 | { |
| 104 | pjsip_replaces_hdr *hdr = pjsip_replaces_hdr_create(pool); |
| 105 | pj_strdup(pool, &hdr->call_id, &rhs->call_id); |
| 106 | pj_strdup(pool, &hdr->to_tag, &rhs->to_tag); |
| 107 | pj_strdup(pool, &hdr->from_tag, &rhs->from_tag); |
| 108 | hdr->early_only = rhs->early_only; |
| 109 | pjsip_param_clone(pool, &hdr->other_param, &rhs->other_param); |
| 110 | return hdr; |
| 111 | } |
| 112 | |
| 113 | static pjsip_replaces_hdr* |
| 114 | replaces_hdr_shallow_clone( pj_pool_t *pool, |
| 115 | const pjsip_replaces_hdr *rhs ) |
| 116 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 117 | pjsip_replaces_hdr *hdr = PJ_POOL_ALLOC_T(pool, pjsip_replaces_hdr); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 118 | pj_memcpy(hdr, rhs, sizeof(*hdr)); |
| 119 | pjsip_param_shallow_clone(pool, &hdr->other_param, &rhs->other_param); |
| 120 | return hdr; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /* |
| 125 | * Parse Replaces header. |
| 126 | */ |
| 127 | static pjsip_hdr *parse_hdr_replaces(pjsip_parse_ctx *ctx) |
| 128 | { |
| 129 | pjsip_replaces_hdr *hdr = pjsip_replaces_hdr_create(ctx->pool); |
| 130 | const pj_str_t to_tag = { "to-tag", 6 }; |
| 131 | const pj_str_t from_tag = { "from-tag", 8 }; |
| 132 | const pj_str_t early_only_tag = { "early-only", 10 }; |
| 133 | |
Benny Prijono | 75862ec | 2006-11-27 11:10:52 +0000 | [diff] [blame] | 134 | /*pj_scan_get(ctx->scanner, &pjsip_TOKEN_SPEC, &hdr->call_id);*/ |
| 135 | /* Get Call-ID (until ';' is found). using pjsip_TOKEN_SPEC doesn't work |
| 136 | * because it stops parsing when '@' character is found. |
| 137 | */ |
| 138 | pj_scan_get_until_ch(ctx->scanner, ';', &hdr->call_id); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 139 | |
| 140 | while (*ctx->scanner->curptr == ';') { |
| 141 | pj_str_t pname, pvalue; |
| 142 | |
| 143 | pj_scan_get_char(ctx->scanner); |
| 144 | pjsip_parse_param_imp(ctx->scanner, ctx->pool, &pname, &pvalue, 0); |
| 145 | |
| 146 | if (pj_stricmp(&pname, &to_tag)==0) { |
| 147 | hdr->to_tag = pvalue; |
| 148 | } else if (pj_stricmp(&pname, &from_tag)==0) { |
| 149 | hdr->from_tag = pvalue; |
| 150 | } else if (pj_stricmp(&pname, &early_only_tag)==0) { |
| 151 | hdr->early_only = PJ_TRUE; |
| 152 | } else { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 153 | pjsip_param *param = PJ_POOL_ALLOC_T(ctx->pool, pjsip_param); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 154 | param->name = pname; |
| 155 | param->value = pvalue; |
| 156 | pj_list_push_back(&hdr->other_param, param); |
| 157 | } |
| 158 | } |
| 159 | pjsip_parse_end_hdr_imp( ctx->scanner ); |
| 160 | return (pjsip_hdr*)hdr; |
| 161 | } |
| 162 | |
Nanang Izzuddin | c549094 | 2009-08-17 15:59:21 +0000 | [diff] [blame] | 163 | |
| 164 | /* Deinitialize Replaces */ |
| 165 | static void pjsip_replaces_deinit_module(void) |
| 166 | { |
| 167 | is_initialized = PJ_FALSE; |
| 168 | } |
| 169 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 170 | /* |
| 171 | * Initialize Replaces support in PJSIP. |
| 172 | */ |
| 173 | PJ_DEF(pj_status_t) pjsip_replaces_init_module(pjsip_endpoint *endpt) |
| 174 | { |
| 175 | pj_status_t status; |
| 176 | const pj_str_t STR_REPLACES = { "replaces", 8 }; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 177 | |
| 178 | the_endpt = endpt; |
| 179 | |
| 180 | if (is_initialized) |
| 181 | return PJ_SUCCESS; |
| 182 | |
| 183 | /* Register Replaces header parser */ |
| 184 | status = pjsip_register_hdr_parser( "Replaces", NULL, |
| 185 | &parse_hdr_replaces); |
| 186 | if (status != PJ_SUCCESS) |
| 187 | return status; |
| 188 | |
| 189 | /* Register "replaces" capability */ |
| 190 | status = pjsip_endpt_add_capability(endpt, NULL, PJSIP_H_SUPPORTED, NULL, |
| 191 | 1, &STR_REPLACES); |
| 192 | |
Nanang Izzuddin | c549094 | 2009-08-17 15:59:21 +0000 | [diff] [blame] | 193 | /* Register deinit module to be executed when PJLIB shutdown */ |
| 194 | if (pj_atexit(&pjsip_replaces_deinit_module) != PJ_SUCCESS) { |
| 195 | /* Failure to register this function may cause this module won't |
| 196 | * work properly when the stack is restarted (without quitting |
| 197 | * application). |
| 198 | */ |
| 199 | pj_assert(!"Failed to register Replaces deinit."); |
| 200 | PJ_LOG(1, (THIS_FILE, "Failed to register Replaces deinit.")); |
| 201 | } |
| 202 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 203 | is_initialized = PJ_TRUE; |
| 204 | return PJ_SUCCESS; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /* |
| 209 | * Verify that incoming request with Replaces header can be processed. |
| 210 | */ |
| 211 | PJ_DEF(pj_status_t) pjsip_replaces_verify_request( pjsip_rx_data *rdata, |
| 212 | pjsip_dialog **p_dlg, |
| 213 | pj_bool_t lock_dlg, |
| 214 | pjsip_tx_data **p_tdata) |
| 215 | { |
| 216 | const pj_str_t STR_REPLACES = { "Replaces", 8 }; |
| 217 | pjsip_replaces_hdr *rep_hdr; |
| 218 | int code = 200; |
| 219 | const char *warn_text = NULL; |
| 220 | pjsip_hdr res_hdr_list; |
| 221 | pjsip_dialog *dlg = NULL; |
| 222 | pjsip_inv_session *inv; |
| 223 | pj_status_t status = PJ_SUCCESS; |
| 224 | |
| 225 | PJ_ASSERT_RETURN(rdata && p_dlg, PJ_EINVAL); |
| 226 | |
| 227 | /* Check that pjsip_replaces_init_module() has been called. */ |
| 228 | PJ_ASSERT_RETURN(the_endpt != NULL, PJ_EINVALIDOP); |
| 229 | |
| 230 | |
| 231 | /* Init output arguments */ |
| 232 | *p_dlg = NULL; |
| 233 | if (p_tdata) *p_tdata = NULL; |
| 234 | |
| 235 | pj_list_init(&res_hdr_list); |
| 236 | |
| 237 | /* Find Replaces header */ |
| 238 | rep_hdr = (pjsip_replaces_hdr*) |
| 239 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &STR_REPLACES, |
| 240 | NULL); |
| 241 | if (!rep_hdr) { |
| 242 | /* No Replaces header. No further processing is necessary. */ |
| 243 | return PJ_SUCCESS; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /* Check that there's no other Replaces header and return 400 Bad Request |
| 248 | * if not. |
| 249 | */ |
| 250 | if (pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &STR_REPLACES, |
| 251 | rep_hdr->next)) { |
| 252 | code = PJSIP_SC_BAD_REQUEST; |
| 253 | warn_text = "Found multiple Replaces headers"; |
| 254 | goto on_return; |
| 255 | } |
| 256 | |
| 257 | /* Find the dialog identified by Replaces header (and always lock the |
| 258 | * dialog no matter what application wants). |
| 259 | */ |
| 260 | dlg = pjsip_ua_find_dialog(&rep_hdr->call_id, &rep_hdr->to_tag, |
| 261 | &rep_hdr->from_tag, PJ_TRUE); |
| 262 | |
| 263 | /* Respond with 481 "Call/Transaction Does Not Exist" response if |
| 264 | * no dialog is found. |
| 265 | */ |
| 266 | if (dlg == NULL) { |
| 267 | code = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST; |
| 268 | warn_text = "No dialog found for Replaces request"; |
| 269 | goto on_return; |
| 270 | } |
| 271 | |
| 272 | /* Get the invite session within the dialog */ |
| 273 | inv = pjsip_dlg_get_inv_session(dlg); |
| 274 | |
| 275 | /* Return 481 if no invite session is present. */ |
| 276 | if (inv == NULL) { |
| 277 | code = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST; |
| 278 | warn_text = "No INVITE session found for Replaces request"; |
| 279 | goto on_return; |
| 280 | } |
| 281 | |
| 282 | /* Return 603 Declined response if invite session has already |
| 283 | * terminated |
| 284 | */ |
| 285 | if (inv->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 286 | code = PJSIP_SC_DECLINE; |
| 287 | warn_text = "INVITE session already terminated"; |
| 288 | goto on_return; |
| 289 | } |
| 290 | |
| 291 | /* If "early-only" flag is present, check that the invite session |
| 292 | * has not been confirmed yet. If the session has been confirmed, |
| 293 | * return 486 "Busy Here" response. |
| 294 | */ |
| 295 | if (rep_hdr->early_only && inv->state >= PJSIP_INV_STATE_CONNECTING) { |
| 296 | code = PJSIP_SC_BUSY_HERE; |
| 297 | warn_text = "INVITE session already established"; |
| 298 | goto on_return; |
| 299 | } |
| 300 | |
| 301 | /* If the Replaces header field matches an early dialog that was not |
| 302 | * initiated by this UA, it returns a 481 (Call/Transaction Does Not |
| 303 | * Exist) response to the new INVITE. |
| 304 | */ |
| 305 | if (inv->state <= PJSIP_INV_STATE_EARLY && inv->role != PJSIP_ROLE_UAC) { |
| 306 | code = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST; |
| 307 | warn_text = "Found early INVITE session but not initiated by this UA"; |
| 308 | goto on_return; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /* |
| 313 | * Looks like everything is okay!! |
| 314 | */ |
| 315 | *p_dlg = dlg; |
| 316 | status = PJ_SUCCESS; |
| 317 | code = 200; |
| 318 | |
| 319 | on_return: |
| 320 | |
| 321 | /* Create response if necessary */ |
| 322 | if (code != 200) { |
| 323 | /* If we have dialog we must unlock it */ |
| 324 | if (dlg) |
| 325 | pjsip_dlg_dec_lock(dlg); |
| 326 | |
| 327 | /* Create response */ |
| 328 | if (p_tdata) { |
| 329 | pjsip_tx_data *tdata; |
| 330 | const pjsip_hdr *h; |
| 331 | |
| 332 | status = pjsip_endpt_create_response(the_endpt, rdata, code, |
| 333 | NULL, &tdata); |
| 334 | |
| 335 | if (status != PJ_SUCCESS) |
| 336 | return status; |
| 337 | |
| 338 | /* Add response headers. */ |
| 339 | h = res_hdr_list.next; |
| 340 | while (h != &res_hdr_list) { |
| 341 | pjsip_hdr *cloned; |
| 342 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 343 | cloned = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 344 | PJ_ASSERT_RETURN(cloned, PJ_ENOMEM); |
| 345 | |
| 346 | pjsip_msg_add_hdr(tdata->msg, cloned); |
| 347 | |
| 348 | h = h->next; |
| 349 | } |
| 350 | |
| 351 | /* Add warn text, if any */ |
| 352 | if (warn_text) { |
| 353 | pjsip_warning_hdr *warn_hdr; |
| 354 | pj_str_t warn_value = pj_str((char*)warn_text); |
| 355 | |
| 356 | warn_hdr=pjsip_warning_hdr_create(tdata->pool, 399, |
| 357 | pjsip_endpt_name(the_endpt), |
| 358 | &warn_value); |
| 359 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)warn_hdr); |
| 360 | } |
| 361 | |
| 362 | *p_tdata = tdata; |
| 363 | } |
| 364 | |
| 365 | /* Can not return PJ_SUCCESS when response message is produced. |
| 366 | * Ref: PROTOS test ~#2490 |
| 367 | */ |
| 368 | if (status == PJ_SUCCESS) |
| 369 | status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 370 | |
| 371 | } else { |
| 372 | /* If application doesn't want to lock the dialog, unlock it */ |
| 373 | if (!lock_dlg) |
| 374 | pjsip_dlg_dec_lock(dlg); |
| 375 | } |
| 376 | |
| 377 | return status; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | |