| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <pjsip-ua/sip_timer.h> |
| 19 |
#include <pjsip/print_util.h> |
| 20 |
#include <pjsip/sip_endpoint.h> |
| 21 |
#include <pjsip/sip_event.h> |
| 22 |
#include <pjsip/sip_transaction.h> |
| 23 |
#include <pj/log.h> |
| 24 |
#include <pj/math.h> |
| 25 |
#include <pj/os.h> |
| 26 |
#include <pj/pool.h> |
| 27 |
|
| 28 |
#define THIS_FILE "sip_timer.c" |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#define ABS_MIN_SE 90 |
| 33 |
#define REFRESHER_EXPIRE_TIMER_ID 2 |
| 34 |
|
| 35 |
|
| 36 |
static const pj_str_t STR_SE = {"Session-Expires", 15}; |
| 37 |
static const pj_str_t STR_SHORT_SE = {"x", 1}; |
| 38 |
static const pj_str_t STR_MIN_SE = {"Min-SE", 6}; |
| 39 |
static const pj_str_t STR_REFRESHER = {"refresher", 9}; |
| 40 |
static const pj_str_t STR_UAC = {"uac", 3}; |
| 41 |
static const pj_str_t STR_UAS = {"uas", 3}; |
| 42 |
static const pj_str_t STR_TIMER = {"timer", 5}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
enum timer_refresher { |
| 47 |
TR_UNKNOWN, |
| 48 |
TR_UAC, |
| 49 |
TR_UAS |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
struct pjsip_timer |
| 54 |
{ |
| 55 |
pj_bool_t active; |
| 56 |
pjsip_timer_setting setting; |
| 57 |
enum timer_refresher refresher; |
| 58 |
pj_time_val last_refresh; |
| 59 |
|
| 60 |
pj_timer_entry timer; |
| 61 |
pj_bool_t use_update; |
| 62 |
|
| 63 |
pj_bool_t with_sdp; |
| 64 |
pjsip_role_e role; |
| 65 |
|
| 66 |
void *refresh_tdata; |
| 67 |
|
| 68 |
pj_timer_entry expire_timer; |
| 69 |
|
| 70 |
pj_int32_t last_422_cseq; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
static void stop_timer(pjsip_inv_session *inv); |
| 75 |
static void start_timer(pjsip_inv_session *inv); |
| 76 |
static pj_bool_t is_initialized; |
| 77 |
const pjsip_method pjsip_update_method = { PJSIP_OTHER_METHOD, {"UPDATE", 6}}; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static int se_hdr_print(pjsip_sess_expires_hdr *hdr, |
| 82 |
char *buf, pj_size_t size); |
| 83 |
static pjsip_sess_expires_hdr* se_hdr_clone(pj_pool_t *pool, |
| 84 |
const pjsip_sess_expires_hdr *hdr); |
| 85 |
static pjsip_sess_expires_hdr* se_hdr_shallow_clone( |
| 86 |
pj_pool_t *pool, |
| 87 |
const pjsip_sess_expires_hdr* hdr); |
| 88 |
|
| 89 |
static pjsip_hdr_vptr se_hdr_vptr = |
| 90 |
{ |
| 91 |
(pjsip_hdr_clone_fptr) &se_hdr_clone, |
| 92 |
(pjsip_hdr_clone_fptr) &se_hdr_shallow_clone, |
| 93 |
(pjsip_hdr_print_fptr) &se_hdr_print, |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static int min_se_hdr_print(pjsip_min_se_hdr *hdr, |
| 100 |
char *buf, pj_size_t size); |
| 101 |
static pjsip_min_se_hdr* min_se_hdr_clone(pj_pool_t *pool, |
| 102 |
const pjsip_min_se_hdr *hdr); |
| 103 |
static pjsip_min_se_hdr* min_se_hdr_shallow_clone( |
| 104 |
pj_pool_t *pool, |
| 105 |
const pjsip_min_se_hdr* hdr); |
| 106 |
|
| 107 |
static pjsip_hdr_vptr min_se_hdr_vptr = |
| 108 |
{ |
| 109 |
(pjsip_hdr_clone_fptr) &min_se_hdr_clone, |
| 110 |
(pjsip_hdr_clone_fptr) &min_se_hdr_shallow_clone, |
| 111 |
(pjsip_hdr_print_fptr) &min_se_hdr_print, |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static int se_hdr_print(pjsip_sess_expires_hdr *hdr, |
| 118 |
char *buf, pj_size_t size) |
| 119 |
{ |
| 120 |
char *p = buf; |
| 121 |
char *endbuf = buf+size; |
| 122 |
pj_ssize_t printed; |
| 123 |
const pjsip_parser_const_t *pc = pjsip_parser_const(); |
| 124 |
const pj_str_t *hname = pjsip_cfg()->endpt.use_compact_form? |
| 125 |
&hdr->sname : &hdr->name; |
| 126 |
|
| 127 |
|
| 128 |
if ((endbuf - p) < (hname->slen + 16)) |
| 129 |
return -1; |
| 130 |
|
| 131 |
copy_advance(p, (*hname)); |
| 132 |
*p++ = ':'; |
| 133 |
*p++ = ' '; |
| 134 |
|
| 135 |
printed = pj_utoa(hdr->sess_expires, p); |
| 136 |
p += printed; |
| 137 |
|
| 138 |
|
| 139 |
if (hdr->refresher.slen) |
| 140 |
{ |
| 141 |
if ((endbuf - p) < (STR_REFRESHER.slen + 2 + hdr->refresher.slen)) |
| 142 |
return -1; |
| 143 |
|
| 144 |
*p++ = ';'; |
| 145 |
copy_advance(p, STR_REFRESHER); |
| 146 |
*p++ = '='; |
| 147 |
copy_advance(p, hdr->refresher); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
printed = pjsip_param_print_on(&hdr->other_param, p, endbuf-p, |
| 152 |
&pc->pjsip_TOKEN_SPEC, |
| 153 |
&pc->pjsip_TOKEN_SPEC, ';'); |
| 154 |
if (printed < 0) |
| 155 |
return (int)printed; |
| 156 |
|
| 157 |
p += printed; |
| 158 |
return (int)(p - buf); |
| 159 |
} |
| 160 |
|
| 161 |
static pjsip_sess_expires_hdr* se_hdr_clone(pj_pool_t *pool, |
| 162 |
const pjsip_sess_expires_hdr *hsrc) |
| 163 |
{ |
| 164 |
pjsip_sess_expires_hdr *hdr = pjsip_sess_expires_hdr_create(pool); |
| 165 |
hdr->sess_expires = hsrc->sess_expires; |
| 166 |
pj_strdup(pool, &hdr->refresher, &hsrc->refresher); |
| 167 |
pjsip_param_clone(pool, &hdr->other_param, &hsrc->other_param); |
| 168 |
return hdr; |
| 169 |
} |
| 170 |
|
| 171 |
static pjsip_sess_expires_hdr* se_hdr_shallow_clone( |
| 172 |
pj_pool_t *pool, |
| 173 |
const pjsip_sess_expires_hdr* hsrc) |
| 174 |
{ |
| 175 |
pjsip_sess_expires_hdr *hdr = PJ_POOL_ALLOC_T(pool,pjsip_sess_expires_hdr); |
| 176 |
pj_memcpy(hdr, hsrc, sizeof(*hdr)); |
| 177 |
pjsip_param_shallow_clone(pool, &hdr->other_param, &hsrc->other_param); |
| 178 |
return hdr; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static int min_se_hdr_print(pjsip_min_se_hdr *hdr, |
| 185 |
char *buf, pj_size_t size) |
| 186 |
{ |
| 187 |
char *p = buf; |
| 188 |
char *endbuf = buf+size; |
| 189 |
pj_ssize_t printed; |
| 190 |
const pjsip_parser_const_t *pc = pjsip_parser_const(); |
| 191 |
|
| 192 |
|
| 193 |
if ((endbuf - p) < (hdr->name.slen + 16)) |
| 194 |
return -1; |
| 195 |
|
| 196 |
copy_advance(p, hdr->name); |
| 197 |
*p++ = ':'; |
| 198 |
*p++ = ' '; |
| 199 |
|
| 200 |
printed = pj_utoa(hdr->min_se, p); |
| 201 |
p += printed; |
| 202 |
|
| 203 |
|
| 204 |
printed = pjsip_param_print_on(&hdr->other_param, p, endbuf-p, |
| 205 |
&pc->pjsip_TOKEN_SPEC, |
| 206 |
&pc->pjsip_TOKEN_SPEC, ';'); |
| 207 |
if (printed < 0) |
| 208 |
return (int)printed; |
| 209 |
|
| 210 |
p += printed; |
| 211 |
return (int)(p - buf); |
| 212 |
} |
| 213 |
|
| 214 |
static pjsip_min_se_hdr* min_se_hdr_clone(pj_pool_t *pool, |
| 215 |
const pjsip_min_se_hdr *hsrc) |
| 216 |
{ |
| 217 |
pjsip_min_se_hdr *hdr = pjsip_min_se_hdr_create(pool); |
| 218 |
hdr->min_se = hsrc->min_se; |
| 219 |
pjsip_param_clone(pool, &hdr->other_param, &hsrc->other_param); |
| 220 |
return hdr; |
| 221 |
} |
| 222 |
|
| 223 |
static pjsip_min_se_hdr* min_se_hdr_shallow_clone( |
| 224 |
pj_pool_t *pool, |
| 225 |
const pjsip_min_se_hdr* hsrc) |
| 226 |
{ |
| 227 |
pjsip_min_se_hdr *hdr = PJ_POOL_ALLOC_T(pool, pjsip_min_se_hdr); |
| 228 |
pj_memcpy(hdr, hsrc, sizeof(*hdr)); |
| 229 |
pjsip_param_shallow_clone(pool, &hdr->other_param, &hsrc->other_param); |
| 230 |
return hdr; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
static pjsip_hdr *parse_hdr_se(pjsip_parse_ctx *ctx) |
| 238 |
{ |
| 239 |
pjsip_sess_expires_hdr *hdr = pjsip_sess_expires_hdr_create(ctx->pool); |
| 240 |
const pjsip_parser_const_t *pc = pjsip_parser_const(); |
| 241 |
pj_str_t token; |
| 242 |
|
| 243 |
pj_scan_get(ctx->scanner, &pc->pjsip_DIGIT_SPEC, &token); |
| 244 |
hdr->sess_expires = pj_strtoul(&token); |
| 245 |
|
| 246 |
while (*ctx->scanner->curptr == ';') { |
| 247 |
pj_str_t pname, pvalue; |
| 248 |
|
| 249 |
pj_scan_get_char(ctx->scanner); |
| 250 |
pjsip_parse_param_imp(ctx->scanner, ctx->pool, &pname, &pvalue, 0); |
| 251 |
|
| 252 |
if (pj_stricmp(&pname, &STR_REFRESHER)==0) { |
| 253 |
hdr->refresher = pvalue; |
| 254 |
} else { |
| 255 |
pjsip_param *param = PJ_POOL_ALLOC_T(ctx->pool, pjsip_param); |
| 256 |
param->name = pname; |
| 257 |
param->value = pvalue; |
| 258 |
pj_list_push_back(&hdr->other_param, param); |
| 259 |
} |
| 260 |
} |
| 261 |
pjsip_parse_end_hdr_imp( ctx->scanner ); |
| 262 |
return (pjsip_hdr*)hdr; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
static pjsip_hdr *parse_hdr_min_se(pjsip_parse_ctx *ctx) |
| 269 |
{ |
| 270 |
pjsip_min_se_hdr *hdr = pjsip_min_se_hdr_create(ctx->pool); |
| 271 |
const pjsip_parser_const_t *pc = pjsip_parser_const(); |
| 272 |
pj_str_t token; |
| 273 |
|
| 274 |
pj_scan_get(ctx->scanner, &pc->pjsip_DIGIT_SPEC, &token); |
| 275 |
hdr->min_se = pj_strtoul(&token); |
| 276 |
|
| 277 |
while (*ctx->scanner->curptr == ';') { |
| 278 |
pj_str_t pname, pvalue; |
| 279 |
pjsip_param *param = PJ_POOL_ALLOC_T(ctx->pool, pjsip_param); |
| 280 |
|
| 281 |
pj_scan_get_char(ctx->scanner); |
| 282 |
pjsip_parse_param_imp(ctx->scanner, ctx->pool, &pname, &pvalue, 0); |
| 283 |
|
| 284 |
param->name = pname; |
| 285 |
param->value = pvalue; |
| 286 |
pj_list_push_back(&hdr->other_param, param); |
| 287 |
} |
| 288 |
pjsip_parse_end_hdr_imp( ctx->scanner ); |
| 289 |
return (pjsip_hdr*)hdr; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
static void add_timer_headers(pjsip_inv_session *inv, pjsip_tx_data *tdata, |
| 297 |
pj_bool_t add_se, pj_bool_t add_min_se) |
| 298 |
{ |
| 299 |
pjsip_timer *timer = inv->timer; |
| 300 |
|
| 301 |
|
| 302 |
if (add_se) { |
| 303 |
pjsip_sess_expires_hdr *hdr; |
| 304 |
|
| 305 |
hdr = pjsip_sess_expires_hdr_create(tdata->pool); |
| 306 |
hdr->sess_expires = timer->setting.sess_expires; |
| 307 |
if (timer->refresher != TR_UNKNOWN) |
| 308 |
hdr->refresher = (timer->refresher == TR_UAC? STR_UAC : STR_UAS); |
| 309 |
|
| 310 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) hdr); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
if (add_min_se) { |
| 315 |
pjsip_min_se_hdr *hdr; |
| 316 |
|
| 317 |
hdr = pjsip_min_se_hdr_create(tdata->pool); |
| 318 |
hdr->min_se = timer->setting.min_se; |
| 319 |
|
| 320 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) hdr); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
static void timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry) |
| 329 |
{ |
| 330 |
pjsip_inv_session *inv = (pjsip_inv_session*) entry->user_data; |
| 331 |
pjsip_tx_data *tdata = NULL; |
| 332 |
pj_status_t status; |
| 333 |
pj_bool_t as_refresher; |
| 334 |
int entry_id; |
| 335 |
char obj_name[PJ_MAX_OBJ_NAME]; |
| 336 |
|
| 337 |
pj_assert(inv); |
| 338 |
|
| 339 |
PJ_UNUSED_ARG(timer_heap); |
| 340 |
|
| 341 |
|
| 342 |
pjsip_dlg_inc_lock(inv->dlg); |
| 343 |
|
| 344 |
|
| 345 |
as_refresher = |
| 346 |
(inv->timer->refresher == TR_UAC && inv->timer->role == PJSIP_ROLE_UAC) || |
| 347 |
(inv->timer->refresher == TR_UAS && inv->timer->role == PJSIP_ROLE_UAS); |
| 348 |
|
| 349 |
entry_id = entry->id; |
| 350 |
pj_ansi_strxcpy(obj_name, inv->pool->obj_name, PJ_MAX_OBJ_NAME); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if (as_refresher && (entry_id != REFRESHER_EXPIRE_TIMER_ID)) { |
| 360 |
pj_time_val now; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
pjmedia_sdp_neg_state neg_state = pjmedia_sdp_neg_get_state(inv->neg); |
| 368 |
|
| 369 |
inv->timer->timer.id = 0; |
| 370 |
|
| 371 |
if ( (!inv->timer->use_update && ( |
| 372 |
inv->invite_tsx != NULL || |
| 373 |
neg_state != PJMEDIA_SDP_NEG_STATE_DONE) |
| 374 |
) |
| 375 |
|| |
| 376 |
(inv->timer->use_update && inv->timer->with_sdp && |
| 377 |
neg_state != PJMEDIA_SDP_NEG_STATE_DONE |
| 378 |
) |
| 379 |
) |
| 380 |
{ |
| 381 |
pj_time_val delay = {1, 0}; |
| 382 |
|
| 383 |
inv->timer->timer.id = 1; |
| 384 |
pjsip_endpt_schedule_timer(inv->dlg->endpt, &inv->timer->timer, |
| 385 |
&delay); |
| 386 |
pjsip_dlg_dec_lock(inv->dlg); |
| 387 |
|
| 388 |
PJ_LOG(3, (obj_name, |
| 389 |
"Reschedule refresh request after %ld seconds as " |
| 390 |
"there is another SDP negotiation in progress", |
| 391 |
delay.sec)); |
| 392 |
return; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
if (inv->timer->use_update) { |
| 397 |
const pjmedia_sdp_session *offer = NULL; |
| 398 |
|
| 399 |
if (inv->timer->with_sdp) { |
| 400 |
pjmedia_sdp_neg_get_active_local(inv->neg, &offer); |
| 401 |
} |
| 402 |
status = pjsip_inv_update(inv, NULL, offer, &tdata); |
| 403 |
} else { |
| 404 |
|
| 405 |
pjsip_msg_body *body; |
| 406 |
const pjmedia_sdp_session *offer = NULL; |
| 407 |
|
| 408 |
pj_assert(pjmedia_sdp_neg_get_state(inv->neg) == |
| 409 |
PJMEDIA_SDP_NEG_STATE_DONE); |
| 410 |
|
| 411 |
status = pjsip_inv_invite(inv, &tdata); |
| 412 |
if (status == PJ_SUCCESS) |
| 413 |
status = pjmedia_sdp_neg_send_local_offer(inv->pool_prov, |
| 414 |
inv->neg, &offer); |
| 415 |
if (status == PJ_SUCCESS) |
| 416 |
status = pjmedia_sdp_neg_get_neg_local(inv->neg, &offer); |
| 417 |
if (status == PJ_SUCCESS) { |
| 418 |
status = pjsip_create_sdp_body(tdata->pool, |
| 419 |
(pjmedia_sdp_session*)offer, &body); |
| 420 |
tdata->msg->body = body; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
pj_gettimeofday(&now); |
| 425 |
PJ_LOG(4, (obj_name, |
| 426 |
"Refreshing session after %lds (expiration period=%ds)", |
| 427 |
(now.sec-inv->timer->last_refresh.sec), |
| 428 |
inv->timer->setting.sess_expires)); |
| 429 |
} else { |
| 430 |
|
| 431 |
pj_time_val now; |
| 432 |
pj_str_t reason = pj_str("No session refresh received."); |
| 433 |
|
| 434 |
if (as_refresher) |
| 435 |
inv->timer->expire_timer.id = 0; |
| 436 |
else |
| 437 |
inv->timer->timer.id = 0; |
| 438 |
|
| 439 |
|
| 440 |
status = pjsip_inv_end_session(inv, PJSIP_SC_REQUEST_TIMEOUT, |
| 441 |
&reason, &tdata); |
| 442 |
|
| 443 |
pj_gettimeofday(&now); |
| 444 |
PJ_LOG(3, (obj_name, |
| 445 |
"No session %s received after %lds " |
| 446 |
"(expiration period=%ds), stopping session now!", |
| 447 |
(as_refresher?"refresh response":"refresh"), |
| 448 |
(now.sec-inv->timer->last_refresh.sec), |
| 449 |
inv->timer->setting.sess_expires)); |
| 450 |
|
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
pjsip_dlg_dec_lock(inv->dlg); |
| 455 |
|
| 456 |
|
| 457 |
if (tdata && status == PJ_SUCCESS) { |
| 458 |
inv->timer->refresh_tdata = tdata; |
| 459 |
|
| 460 |
status = pjsip_inv_send_msg(inv, tdata); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
if (status != PJ_SUCCESS) { |
| 470 |
PJ_PERROR(2, (obj_name, status, |
| 471 |
"Error in %s session timer", |
| 472 |
((as_refresher && entry_id != REFRESHER_EXPIRE_TIMER_ID)? |
| 473 |
"refreshing" : "terminating"))); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
static void start_timer(pjsip_inv_session *inv) |
| 479 |
{ |
| 480 |
const pj_str_t UPDATE = { "UPDATE", 6 }; |
| 481 |
pjsip_timer *timer = inv->timer; |
| 482 |
pj_time_val delay = {0}; |
| 483 |
|
| 484 |
pj_assert(inv->timer->active == PJ_TRUE); |
| 485 |
|
| 486 |
stop_timer(inv); |
| 487 |
|
| 488 |
inv->timer->use_update = |
| 489 |
(pjsip_dlg_remote_has_cap(inv->dlg, PJSIP_H_ALLOW, NULL, |
| 490 |
&UPDATE) == PJSIP_DIALOG_CAP_SUPPORTED); |
| 491 |
if (!inv->timer->use_update) { |
| 492 |
|
| 493 |
inv->timer->with_sdp = PJ_TRUE; |
| 494 |
} |
| 495 |
|
| 496 |
pj_timer_entry_init(&timer->timer, |
| 497 |
1, |
| 498 |
inv, |
| 499 |
timer_cb); |
| 500 |
|
| 501 |
|
| 502 |
if ((timer->refresher == TR_UAC && inv->timer->role == PJSIP_ROLE_UAC) || |
| 503 |
(timer->refresher == TR_UAS && inv->timer->role == PJSIP_ROLE_UAS)) |
| 504 |
{ |
| 505 |
|
| 506 |
pj_timer_entry_init(&timer->expire_timer, |
| 507 |
REFRESHER_EXPIRE_TIMER_ID, |
| 508 |
inv, |
| 509 |
timer_cb); |
| 510 |
|
| 511 |
delay.sec = timer->setting.sess_expires; |
| 512 |
|
| 513 |
pjsip_endpt_schedule_timer(inv->dlg->endpt, &timer->expire_timer, |
| 514 |
&delay); |
| 515 |
|
| 516 |
|
| 517 |
delay.sec = timer->setting.sess_expires / 2; |
| 518 |
} else { |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
delay.sec = timer->setting.sess_expires - |
| 524 |
timer->setting.sess_expires/3; |
| 525 |
delay.sec = PJ_MAX((long)timer->setting.sess_expires-32, delay.sec); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
pjsip_endpt_schedule_timer(inv->dlg->endpt, &timer->timer, &delay); |
| 530 |
|
| 531 |
|
| 532 |
pj_gettimeofday(&timer->last_refresh); |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
static void stop_timer(pjsip_inv_session *inv) |
| 537 |
{ |
| 538 |
if (inv->timer->timer.id != 0) { |
| 539 |
pjsip_endpt_cancel_timer(inv->dlg->endpt, &inv->timer->timer); |
| 540 |
inv->timer->timer.id = 0; |
| 541 |
} |
| 542 |
|
| 543 |
if (inv->timer->expire_timer.id != 0) { |
| 544 |
pjsip_endpt_cancel_timer(inv->dlg->endpt, &inv->timer->expire_timer); |
| 545 |
inv->timer->expire_timer.id = 0; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
static void pjsip_timer_deinit_module(pjsip_endpoint *endpt) |
| 551 |
{ |
| 552 |
PJ_TODO(provide_initialized_flag_for_each_endpoint); |
| 553 |
PJ_UNUSED_ARG(endpt); |
| 554 |
is_initialized = PJ_FALSE; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
PJ_DEF(pj_status_t) pjsip_timer_init_module(pjsip_endpoint *endpt) |
| 561 |
{ |
| 562 |
pj_status_t status; |
| 563 |
|
| 564 |
PJ_ASSERT_RETURN(endpt, PJ_EINVAL); |
| 565 |
|
| 566 |
if (is_initialized) |
| 567 |
return PJ_SUCCESS; |
| 568 |
|
| 569 |
|
| 570 |
status = pjsip_register_hdr_parser( STR_SE.ptr, STR_SHORT_SE.ptr, |
| 571 |
&parse_hdr_se); |
| 572 |
if (status != PJ_SUCCESS) |
| 573 |
return status; |
| 574 |
|
| 575 |
|
| 576 |
status = pjsip_register_hdr_parser( STR_MIN_SE.ptr, NULL, |
| 577 |
&parse_hdr_min_se); |
| 578 |
if (status != PJ_SUCCESS) |
| 579 |
return status; |
| 580 |
|
| 581 |
|
| 582 |
status = pjsip_endpt_add_capability(endpt, NULL, PJSIP_H_SUPPORTED, |
| 583 |
NULL, 1, &STR_TIMER); |
| 584 |
if (status != PJ_SUCCESS) |
| 585 |
return status; |
| 586 |
|
| 587 |
|
| 588 |
if (pjsip_endpt_atexit(endpt, &pjsip_timer_deinit_module) != PJ_SUCCESS) { |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
pj_assert(!"Failed to register Session Timer deinit."); |
| 594 |
PJ_LOG(1, (THIS_FILE, "Failed to register Session Timer deinit.")); |
| 595 |
} |
| 596 |
|
| 597 |
is_initialized = PJ_TRUE; |
| 598 |
|
| 599 |
return PJ_SUCCESS; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
PJ_DEF(pj_status_t) pjsip_timer_setting_default(pjsip_timer_setting *setting) |
| 607 |
{ |
| 608 |
pj_bzero(setting, sizeof(pjsip_timer_setting)); |
| 609 |
|
| 610 |
setting->sess_expires = PJSIP_SESS_TIMER_DEF_SE; |
| 611 |
setting->min_se = ABS_MIN_SE; |
| 612 |
|
| 613 |
return PJ_SUCCESS; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
PJ_DEF(pj_status_t) pjsip_timer_init_session( |
| 620 |
pjsip_inv_session *inv, |
| 621 |
const pjsip_timer_setting *setting) |
| 622 |
{ |
| 623 |
pjsip_timer_setting *s; |
| 624 |
|
| 625 |
pj_assert(is_initialized); |
| 626 |
PJ_ASSERT_RETURN(inv, PJ_EINVAL); |
| 627 |
|
| 628 |
|
| 629 |
if (!inv->timer) |
| 630 |
inv->timer = PJ_POOL_ZALLOC_T(inv->pool, pjsip_timer); |
| 631 |
else |
| 632 |
pj_bzero(inv->timer, sizeof(pjsip_timer)); |
| 633 |
|
| 634 |
s = &inv->timer->setting; |
| 635 |
|
| 636 |
|
| 637 |
if (setting) { |
| 638 |
PJ_ASSERT_RETURN(setting->min_se >= ABS_MIN_SE, |
| 639 |
PJ_ETOOSMALL); |
| 640 |
PJ_ASSERT_RETURN(setting->sess_expires >= setting->min_se, |
| 641 |
PJ_EINVAL); |
| 642 |
|
| 643 |
pj_memcpy(s, setting, sizeof(*s)); |
| 644 |
} else { |
| 645 |
pjsip_timer_setting_default(s); |
| 646 |
} |
| 647 |
|
| 648 |
return PJ_SUCCESS; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
PJ_DEF(pjsip_sess_expires_hdr*) pjsip_sess_expires_hdr_create( |
| 656 |
pj_pool_t *pool) |
| 657 |
{ |
| 658 |
pjsip_sess_expires_hdr *hdr = PJ_POOL_ZALLOC_T(pool, |
| 659 |
pjsip_sess_expires_hdr); |
| 660 |
|
| 661 |
pj_assert(is_initialized); |
| 662 |
|
| 663 |
hdr->type = PJSIP_H_OTHER; |
| 664 |
hdr->name = STR_SE; |
| 665 |
hdr->sname = STR_SHORT_SE; |
| 666 |
hdr->vptr = &se_hdr_vptr; |
| 667 |
pj_list_init(hdr); |
| 668 |
pj_list_init(&hdr->other_param); |
| 669 |
return hdr; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
PJ_DEF(pjsip_min_se_hdr*) pjsip_min_se_hdr_create(pj_pool_t *pool) |
| 677 |
{ |
| 678 |
pjsip_min_se_hdr *hdr = PJ_POOL_ZALLOC_T(pool, pjsip_min_se_hdr); |
| 679 |
|
| 680 |
pj_assert(is_initialized); |
| 681 |
|
| 682 |
hdr->type = PJSIP_H_OTHER; |
| 683 |
hdr->name = STR_MIN_SE; |
| 684 |
hdr->vptr = &min_se_hdr_vptr; |
| 685 |
pj_list_init(hdr); |
| 686 |
pj_list_init(&hdr->other_param); |
| 687 |
return hdr; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
PJ_DEF(pj_status_t) pjsip_timer_update_req(pjsip_inv_session *inv, |
| 696 |
pjsip_tx_data *tdata) |
| 697 |
{ |
| 698 |
PJ_ASSERT_RETURN(inv && tdata, PJ_EINVAL); |
| 699 |
|
| 700 |
|
| 701 |
if ((inv->options & PJSIP_INV_SUPPORT_TIMER) == 0) |
| 702 |
return PJ_SUCCESS; |
| 703 |
|
| 704 |
pj_assert(is_initialized); |
| 705 |
|
| 706 |
|
| 707 |
if (inv->timer == NULL) |
| 708 |
pjsip_timer_init_session(inv, NULL); |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
if (inv->timer->refresher != TR_UNKNOWN) { |
| 714 |
pj_bool_t as_refresher; |
| 715 |
|
| 716 |
|
| 717 |
as_refresher = |
| 718 |
(inv->timer->refresher==TR_UAC && inv->timer->role==PJSIP_ROLE_UAC) || |
| 719 |
(inv->timer->refresher==TR_UAS && inv->timer->role==PJSIP_ROLE_UAS); |
| 720 |
|
| 721 |
|
| 722 |
inv->timer->role = PJSIP_ROLE_UAC; |
| 723 |
|
| 724 |
|
| 725 |
inv->timer->refresher = as_refresher? TR_UAC : TR_UAS; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
add_timer_headers(inv, tdata, PJ_TRUE, PJ_TRUE); |
| 730 |
|
| 731 |
return PJ_SUCCESS; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
PJ_DEF(pj_status_t) pjsip_timer_process_resp(pjsip_inv_session *inv, |
| 741 |
const pjsip_rx_data *rdata, |
| 742 |
pjsip_status_code *st_code) |
| 743 |
{ |
| 744 |
const pjsip_msg *msg; |
| 745 |
|
| 746 |
PJ_ASSERT_ON_FAIL(inv && rdata, |
| 747 |
{if(st_code)*st_code=PJSIP_SC_INTERNAL_SERVER_ERROR;return PJ_EINVAL;}); |
| 748 |
|
| 749 |
|
| 750 |
if ((inv->options & PJSIP_INV_SUPPORT_TIMER) == 0) { |
| 751 |
if (rdata->msg_info.msg->line.status.code == |
| 752 |
PJSIP_SC_SESSION_TIMER_TOO_SMALL) |
| 753 |
{ |
| 754 |
return PJSIP_EINVALIDSTATUS; |
| 755 |
} else { |
| 756 |
return PJ_SUCCESS; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
pj_assert(is_initialized); |
| 761 |
|
| 762 |
msg = rdata->msg_info.msg; |
| 763 |
pj_assert(msg->type == PJSIP_RESPONSE_MSG); |
| 764 |
|
| 765 |
|
| 766 |
if (rdata->msg_info.cseq->method.id != PJSIP_INVITE_METHOD && |
| 767 |
pjsip_method_cmp(&rdata->msg_info.cseq->method, &pjsip_update_method)) |
| 768 |
{ |
| 769 |
return PJ_SUCCESS; |
| 770 |
} |
| 771 |
|
| 772 |
if (msg->line.status.code == PJSIP_SC_SESSION_TIMER_TOO_SMALL) { |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
pjsip_tx_data *tdata; |
| 777 |
pjsip_min_se_hdr *min_se_hdr; |
| 778 |
pjsip_hdr *hdr; |
| 779 |
pjsip_via_hdr *via; |
| 780 |
|
| 781 |
|
| 782 |
min_se_hdr = (pjsip_min_se_hdr*) |
| 783 |
pjsip_msg_find_hdr_by_name(msg, &STR_MIN_SE, NULL); |
| 784 |
if (min_se_hdr == NULL) { |
| 785 |
|
| 786 |
PJ_LOG(3, (inv->pool->obj_name, |
| 787 |
"Received 422 (Session Interval Too Small) response " |
| 788 |
"without Min-SE header!")); |
| 789 |
|
| 790 |
pjsip_timer_end_session(inv); |
| 791 |
return PJSIP_EMISSINGHDR; |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
if (rdata->msg_info.cseq->cseq == inv->timer->last_422_cseq + 1) { |
| 798 |
if (inv->state == PJSIP_INV_STATE_CONFIRMED) { |
| 799 |
inv->invite_tsx = NULL; |
| 800 |
return PJ_SUCCESS; |
| 801 |
} else { |
| 802 |
return PJSIP_ERRNO_FROM_SIP_STATUS( |
| 803 |
PJSIP_SC_SESSION_TIMER_TOO_SMALL); |
| 804 |
} |
| 805 |
} |
| 806 |
inv->timer->last_422_cseq = rdata->msg_info.cseq->cseq; |
| 807 |
|
| 808 |
|
| 809 |
pj_assert(inv->timer); |
| 810 |
|
| 811 |
|
| 812 |
inv->timer->setting.min_se = PJ_MAX(min_se_hdr->min_se, |
| 813 |
inv->timer->setting.min_se); |
| 814 |
|
| 815 |
|
| 816 |
if (inv->timer->setting.sess_expires < inv->timer->setting.min_se) |
| 817 |
inv->timer->setting.sess_expires = inv->timer->setting.min_se; |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
tdata = pjsip_rdata_get_tsx((pjsip_rx_data*)rdata)->last_tx; |
| 823 |
|
| 824 |
|
| 825 |
via = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
| 826 |
pj_assert(via); |
| 827 |
via->branch_param.slen = 0; |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
pjsip_restore_strict_route_set(tdata); |
| 833 |
|
| 834 |
|
| 835 |
pjsip_tx_data_invalidate_msg(tdata); |
| 836 |
|
| 837 |
pjsip_tx_data_add_ref(tdata); |
| 838 |
|
| 839 |
|
| 840 |
hdr = (pjsip_hdr*) pjsip_msg_find_hdr_by_name(tdata->msg, |
| 841 |
&STR_MIN_SE, NULL); |
| 842 |
if (hdr != NULL) pj_list_erase(hdr); |
| 843 |
|
| 844 |
hdr = (pjsip_hdr*) pjsip_msg_find_hdr_by_names(tdata->msg, &STR_SE, |
| 845 |
&STR_SHORT_SE, NULL); |
| 846 |
if (hdr != NULL) pj_list_erase(hdr); |
| 847 |
|
| 848 |
add_timer_headers(inv, tdata, PJ_TRUE, PJ_TRUE); |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
if (inv->state < PJSIP_INV_STATE_CONFIRMED) |
| 854 |
pjsip_inv_uac_restart(inv, PJ_FALSE); |
| 855 |
else if (tdata->msg->line.req.method.id == PJSIP_INVITE_METHOD) |
| 856 |
inv->invite_tsx = NULL; |
| 857 |
|
| 858 |
|
| 859 |
pjsip_inv_send_msg(inv, tdata); |
| 860 |
|
| 861 |
return PJ_SUCCESS; |
| 862 |
|
| 863 |
} else if (msg->line.status.code/100 == 2) { |
| 864 |
|
| 865 |
pjsip_sess_expires_hdr *se_hdr; |
| 866 |
|
| 867 |
|
| 868 |
se_hdr = (pjsip_sess_expires_hdr*) pjsip_msg_find_hdr_by_names( |
| 869 |
msg, &STR_SE, |
| 870 |
&STR_SHORT_SE, NULL); |
| 871 |
if (se_hdr == NULL) { |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
if (inv->options & PJSIP_INV_REQUIRE_TIMER) { |
| 876 |
if (st_code) |
| 877 |
*st_code = PJSIP_SC_EXTENSION_REQUIRED; |
| 878 |
pjsip_timer_end_session(inv); |
| 879 |
return PJSIP_ERRNO_FROM_SIP_STATUS( |
| 880 |
PJSIP_SC_EXTENSION_REQUIRED); |
| 881 |
} |
| 882 |
|
| 883 |
if ((inv->options & PJSIP_INV_ALWAYS_USE_TIMER) == 0) { |
| 884 |
|
| 885 |
pjsip_timer_end_session(inv); |
| 886 |
return PJ_SUCCESS; |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
if (inv->timer == NULL) |
| 892 |
pjsip_timer_init_session(inv, NULL); |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
if (se_hdr && |
| 898 |
se_hdr->sess_expires < inv->timer->setting.min_se) |
| 899 |
{ |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
if (se_hdr->sess_expires >= ABS_MIN_SE) { |
| 905 |
PJ_LOG(3, (inv->pool->obj_name, |
| 906 |
"Peer responds with bad Session-Expires, %ds, " |
| 907 |
"which is less than Min-SE specified in request, " |
| 908 |
"%ds. Well, let's just accept and use it.", |
| 909 |
se_hdr->sess_expires, inv->timer->setting.min_se)); |
| 910 |
|
| 911 |
inv->timer->setting.sess_expires = se_hdr->sess_expires; |
| 912 |
inv->timer->setting.min_se = se_hdr->sess_expires; |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
if (se_hdr && |
| 927 |
se_hdr->sess_expires <= inv->timer->setting.sess_expires && |
| 928 |
se_hdr->sess_expires >= inv->timer->setting.min_se) |
| 929 |
{ |
| 930 |
|
| 931 |
inv->timer->setting.sess_expires = se_hdr->sess_expires; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
if (se_hdr && pj_stricmp(&se_hdr->refresher, &STR_UAC) == 0) |
| 936 |
inv->timer->refresher = TR_UAC; |
| 937 |
else if (se_hdr && pj_stricmp(&se_hdr->refresher, &STR_UAS) == 0) |
| 938 |
inv->timer->refresher = TR_UAS; |
| 939 |
else |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
inv->timer->refresher = TR_UAC; |
| 945 |
|
| 946 |
|
| 947 |
inv->timer->role = PJSIP_ROLE_UAC; |
| 948 |
|
| 949 |
|
| 950 |
inv->timer->active = PJ_TRUE; |
| 951 |
start_timer(inv); |
| 952 |
|
| 953 |
} else if (pjsip_method_cmp(&rdata->msg_info.cseq->method, |
| 954 |
&pjsip_update_method) == 0 && |
| 955 |
msg->line.status.code >= 400 && msg->line.status.code < 600) |
| 956 |
{ |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
if (inv->timer->timer.id == 0 && inv->timer->use_update && |
| 965 |
inv->timer->with_sdp == PJ_FALSE) |
| 966 |
{ |
| 967 |
inv->timer->with_sdp = PJ_TRUE; |
| 968 |
timer_cb(NULL, &inv->timer->timer); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
return PJ_SUCCESS; |
| 973 |
} |
| 974 |
|
| 975 |
PJ_DEF(pj_status_t) pjsip_timer_handle_refresh_error( |
| 976 |
pjsip_inv_session *inv, |
| 977 |
pjsip_event *event) |
| 978 |
{ |
| 979 |
PJ_ASSERT_RETURN(inv && event, PJ_EINVAL); |
| 980 |
|
| 981 |
|
| 982 |
if ((inv->options & PJSIP_INV_SUPPORT_TIMER) == 0) |
| 983 |
return PJ_SUCCESS; |
| 984 |
|
| 985 |
pj_assert(is_initialized); |
| 986 |
|
| 987 |
if (inv->timer && inv->timer->active) { |
| 988 |
pj_bool_t as_refresher; |
| 989 |
|
| 990 |
|
| 991 |
as_refresher = ((inv->timer->refresher == TR_UAC) && |
| 992 |
(inv->timer->role == PJSIP_ROLE_UAC)) || |
| 993 |
((inv->timer->refresher == TR_UAS) && |
| 994 |
(inv->timer->role == PJSIP_ROLE_UAS)); |
| 995 |
|
| 996 |
|
| 997 |
if ((as_refresher) && (event->type == PJSIP_EVENT_TSX_STATE) && |
| 998 |
(inv->timer->refresh_tdata == event->body.tsx_state.tsx->last_tx)) |
| 999 |
{ |
| 1000 |
pjsip_status_code st_code; |
| 1001 |
pjsip_tx_data *bye; |
| 1002 |
pj_status_t status; |
| 1003 |
|
| 1004 |
st_code = |
| 1005 |
(pjsip_status_code)event->body.tsx_state.tsx->status_code; |
| 1006 |
|
| 1007 |
PJ_LOG(3, (inv->pool->obj_name, |
| 1008 |
"Receive error %d for refresh request %.*s/cseq=%d", |
| 1009 |
st_code, (int)event->body.tsx_state.tsx->method.name.slen, |
| 1010 |
event->body.tsx_state.tsx->method.name.ptr, |
| 1011 |
event->body.tsx_state.tsx->cseq)); |
| 1012 |
|
| 1013 |
if (st_code == 503 && PJSIP_SESS_TIMER_RETRY_DELAY >= 0) { |
| 1014 |
|
| 1015 |
pj_time_val delay = {PJSIP_SESS_TIMER_RETRY_DELAY, 0}; |
| 1016 |
|
| 1017 |
PJ_LOG(3, (inv->pool->obj_name, "Scheduling to retry refresh " |
| 1018 |
"request after %ld second(s)", delay.sec)); |
| 1019 |
|
| 1020 |
inv->timer->timer.id = 1; |
| 1021 |
pjsip_endpt_schedule_timer(inv->dlg->endpt, |
| 1022 |
&inv->timer->timer, &delay); |
| 1023 |
} else { |
| 1024 |
PJ_LOG(3, (inv->pool->obj_name, "Ending session now")); |
| 1025 |
|
| 1026 |
status = pjsip_inv_end_session(inv, |
| 1027 |
event->body.tsx_state.tsx->status_code, |
| 1028 |
pjsip_get_status_text(st_code), |
| 1029 |
&bye); |
| 1030 |
|
| 1031 |
if (status == PJ_SUCCESS && bye) |
| 1032 |
status = pjsip_inv_send_msg(inv, bye); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
return PJ_SUCCESS; |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
PJ_DEF(pj_status_t) pjsip_timer_process_req(pjsip_inv_session *inv, |
| 1044 |
const pjsip_rx_data *rdata, |
| 1045 |
pjsip_status_code *st_code) |
| 1046 |
{ |
| 1047 |
pjsip_min_se_hdr *min_se_hdr; |
| 1048 |
pjsip_sess_expires_hdr *se_hdr; |
| 1049 |
const pjsip_msg *msg; |
| 1050 |
unsigned min_se; |
| 1051 |
|
| 1052 |
PJ_ASSERT_ON_FAIL(inv && rdata, |
| 1053 |
{if(st_code)*st_code=PJSIP_SC_INTERNAL_SERVER_ERROR;return PJ_EINVAL;}); |
| 1054 |
|
| 1055 |
|
| 1056 |
if ((inv->options & PJSIP_INV_SUPPORT_TIMER) == 0) |
| 1057 |
return PJ_SUCCESS; |
| 1058 |
|
| 1059 |
pj_assert(is_initialized); |
| 1060 |
|
| 1061 |
msg = rdata->msg_info.msg; |
| 1062 |
pj_assert(msg->type == PJSIP_REQUEST_MSG); |
| 1063 |
|
| 1064 |
|
| 1065 |
if (msg->line.req.method.id != PJSIP_INVITE_METHOD && |
| 1066 |
pjsip_method_cmp(&rdata->msg_info.cseq->method, &pjsip_update_method)) |
| 1067 |
{ |
| 1068 |
return PJ_SUCCESS; |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
if (inv->timer == NULL) |
| 1073 |
pjsip_timer_init_session(inv, NULL); |
| 1074 |
|
| 1075 |
|
| 1076 |
se_hdr = (pjsip_sess_expires_hdr*) pjsip_msg_find_hdr_by_names( |
| 1077 |
msg, &STR_SE, &STR_SHORT_SE, NULL); |
| 1078 |
|
| 1079 |
min_se_hdr = (pjsip_min_se_hdr*) pjsip_msg_find_hdr_by_name(msg, |
| 1080 |
&STR_MIN_SE, NULL); |
| 1081 |
|
| 1082 |
min_se = inv->timer->setting.min_se; |
| 1083 |
if (min_se_hdr) |
| 1084 |
min_se = PJ_MAX(min_se_hdr->min_se, min_se); |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
if (se_hdr && se_hdr->sess_expires < min_se) { |
| 1090 |
if (st_code) |
| 1091 |
*st_code = PJSIP_SC_SESSION_TIMER_TOO_SMALL; |
| 1092 |
return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_SESSION_TIMER_TOO_SMALL); |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
if (se_hdr) { |
| 1100 |
|
| 1101 |
inv->timer->setting.sess_expires = se_hdr->sess_expires; |
| 1102 |
} else if (inv->timer->setting.sess_expires < min_se) { |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
inv->timer->setting.sess_expires = min_se; |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
if (se_hdr == NULL) { |
| 1116 |
if ((inv->options & |
| 1117 |
(PJSIP_INV_REQUIRE_TIMER | PJSIP_INV_ALWAYS_USE_TIMER)) == 0) |
| 1118 |
{ |
| 1119 |
|
| 1120 |
pjsip_timer_end_session(inv); |
| 1121 |
return PJ_SUCCESS; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
if (se_hdr && pj_stricmp(&se_hdr->refresher, &STR_UAC) == 0) |
| 1127 |
inv->timer->refresher = TR_UAC; |
| 1128 |
else if (se_hdr && pj_stricmp(&se_hdr->refresher, &STR_UAS) == 0) |
| 1129 |
inv->timer->refresher = TR_UAS; |
| 1130 |
else { |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
if (inv->timer->refresher != TR_UNKNOWN) { |
| 1135 |
pj_bool_t as_refresher; |
| 1136 |
|
| 1137 |
|
| 1138 |
as_refresher = |
| 1139 |
(inv->timer->refresher==TR_UAC && inv->timer->role==PJSIP_ROLE_UAC) || |
| 1140 |
(inv->timer->refresher==TR_UAS && inv->timer->role==PJSIP_ROLE_UAS); |
| 1141 |
|
| 1142 |
|
| 1143 |
inv->timer->refresher = as_refresher? TR_UAS : TR_UAC; |
| 1144 |
} else { |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
pj_bool_t uac_supports_timer = PJ_FALSE; |
| 1152 |
pjsip_supported_hdr *sup_hdr; |
| 1153 |
|
| 1154 |
sup_hdr = (pjsip_supported_hdr*) |
| 1155 |
pjsip_msg_find_hdr(msg, PJSIP_H_SUPPORTED, NULL); |
| 1156 |
if (sup_hdr) { |
| 1157 |
unsigned i; |
| 1158 |
|
| 1159 |
for (i = 0; i < sup_hdr->count; i++) { |
| 1160 |
if (pj_stricmp(&sup_hdr->values[i], &STR_TIMER) == 0) { |
| 1161 |
uac_supports_timer = PJ_TRUE; |
| 1162 |
break; |
| 1163 |
} |
| 1164 |
} |
| 1165 |
} |
| 1166 |
inv->timer->refresher = (uac_supports_timer && se_hdr)? TR_UAC: |
| 1167 |
TR_UAS; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
inv->timer->role = PJSIP_ROLE_UAS; |
| 1173 |
|
| 1174 |
|
| 1175 |
inv->timer->active = PJ_TRUE; |
| 1176 |
|
| 1177 |
return PJ_SUCCESS; |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
PJ_DEF(pj_status_t) pjsip_timer_update_resp(pjsip_inv_session *inv, |
| 1184 |
pjsip_tx_data *tdata) |
| 1185 |
{ |
| 1186 |
pjsip_msg *msg; |
| 1187 |
|
| 1188 |
|
| 1189 |
if ((inv->options & PJSIP_INV_SUPPORT_TIMER) == 0) |
| 1190 |
return PJ_SUCCESS; |
| 1191 |
|
| 1192 |
pj_assert(is_initialized); |
| 1193 |
PJ_ASSERT_RETURN(inv && tdata, PJ_EINVAL); |
| 1194 |
|
| 1195 |
msg = tdata->msg; |
| 1196 |
|
| 1197 |
if (msg->line.status.code/100 == 2) |
| 1198 |
{ |
| 1199 |
if (inv->timer && inv->timer->active) { |
| 1200 |
|
| 1201 |
add_timer_headers(inv, tdata, PJ_TRUE, PJ_FALSE); |
| 1202 |
|
| 1203 |
|
| 1204 |
if (inv->timer->refresher == TR_UAC) { |
| 1205 |
pjsip_require_hdr *req_hdr; |
| 1206 |
pj_bool_t req_hdr_has_timer = PJ_FALSE; |
| 1207 |
|
| 1208 |
req_hdr = (pjsip_require_hdr*) |
| 1209 |
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_REQUIRE, |
| 1210 |
NULL); |
| 1211 |
if (req_hdr == NULL) { |
| 1212 |
req_hdr = pjsip_require_hdr_create(tdata->pool); |
| 1213 |
PJ_ASSERT_RETURN(req_hdr, PJ_ENOMEM); |
| 1214 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)req_hdr); |
| 1215 |
} else { |
| 1216 |
unsigned i; |
| 1217 |
for (i = 0; i < req_hdr->count; ++i) { |
| 1218 |
if (pj_stricmp(&req_hdr->values[i], &STR_TIMER)) { |
| 1219 |
req_hdr_has_timer = PJ_TRUE; |
| 1220 |
break; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
} |
| 1224 |
if (!req_hdr_has_timer) |
| 1225 |
req_hdr->values[req_hdr->count++] = STR_TIMER; |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
start_timer(inv); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
else if (msg->line.status.code == PJSIP_SC_SESSION_TIMER_TOO_SMALL) |
| 1233 |
{ |
| 1234 |
|
| 1235 |
add_timer_headers(inv, tdata, PJ_FALSE, PJ_TRUE); |
| 1236 |
} |
| 1237 |
|
| 1238 |
return PJ_SUCCESS; |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
PJ_DEF(pj_status_t) pjsip_timer_end_session(pjsip_inv_session *inv) |
| 1246 |
{ |
| 1247 |
PJ_ASSERT_RETURN(inv, PJ_EINVAL); |
| 1248 |
|
| 1249 |
if (inv->timer) { |
| 1250 |
|
| 1251 |
inv->timer->active = PJ_FALSE; |
| 1252 |
|
| 1253 |
|
| 1254 |
stop_timer(inv); |
| 1255 |
} |
| 1256 |
|
| 1257 |
return PJ_SUCCESS; |
| 1258 |
} |