| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsip-simple/evsub.h> |
| 20 |
#include <pjsip-simple/evsub_msg.h> |
| 21 |
#include <pjsip-simple/errno.h> |
| 22 |
#include <pjsip/sip_errno.h> |
| 23 |
#include <pjsip/sip_module.h> |
| 24 |
#include <pjsip/sip_endpoint.h> |
| 25 |
#include <pjsip/sip_dialog.h> |
| 26 |
#include <pjsip/sip_auth.h> |
| 27 |
#include <pjsip/sip_transaction.h> |
| 28 |
#include <pjsip/sip_event.h> |
| 29 |
#include <pj/assert.h> |
| 30 |
#include <pj/guid.h> |
| 31 |
#include <pj/log.h> |
| 32 |
#include <pj/os.h> |
| 33 |
#include <pj/pool.h> |
| 34 |
#include <pj/rand.h> |
| 35 |
#include <pj/string.h> |
| 36 |
|
| 37 |
|
| 38 |
#define THIS_FILE "evsub.c" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
enum |
| 48 |
{ |
| 49 |
PJSIP_SUBSCRIBE_METHOD = PJSIP_OTHER_METHOD, |
| 50 |
PJSIP_NOTIFY_METHOD = PJSIP_OTHER_METHOD |
| 51 |
}; |
| 52 |
|
| 53 |
PJ_DEF_DATA(const pjsip_method) pjsip_subscribe_method = |
| 54 |
{ |
| 55 |
(pjsip_method_e) PJSIP_SUBSCRIBE_METHOD, |
| 56 |
{ "SUBSCRIBE", 9 } |
| 57 |
}; |
| 58 |
|
| 59 |
PJ_DEF_DATA(const pjsip_method) pjsip_notify_method = |
| 60 |
{ |
| 61 |
(pjsip_method_e) PJSIP_NOTIFY_METHOD, |
| 62 |
{ "NOTIFY", 6 } |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
PJ_DEF(const pjsip_method*) pjsip_get_subscribe_method() |
| 69 |
{ |
| 70 |
return &pjsip_subscribe_method; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
PJ_DEF(const pjsip_method*) pjsip_get_notify_method() |
| 77 |
{ |
| 78 |
return &pjsip_notify_method; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static void mod_evsub_on_tsx_state(pjsip_transaction*, pjsip_event*); |
| 86 |
static pj_status_t mod_evsub_unload(void); |
| 87 |
static pj_status_t evsub_async_auth_send_impl( |
| 88 |
pjsip_auth_clt_sess *auth_sess, |
| 89 |
void *user_data, |
| 90 |
pjsip_tx_data *tdata); |
| 91 |
static void evsub_async_auth_abandon_impl( |
| 92 |
pjsip_auth_clt_sess *auth_sess, |
| 93 |
void *user_data); |
| 94 |
static void set_state(pjsip_evsub *sub, pjsip_evsub_state state, |
| 95 |
const pj_str_t *state_str, pjsip_event *event, |
| 96 |
const pj_str_t *reason); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static pj_str_t evsub_state_names[] = |
| 103 |
{ |
| 104 |
{ "NULL", 4}, |
| 105 |
{ "SENT", 4}, |
| 106 |
{ "ACCEPTED", 8}, |
| 107 |
{ "PENDING", 7}, |
| 108 |
{ "ACTIVE", 6}, |
| 109 |
{ "TERMINATED", 10}, |
| 110 |
{ "UNKNOWN", 7} |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
#define TIME_UAC_REFRESH PJSIP_EVSUB_TIME_UAC_REFRESH |
| 119 |
|
| 120 |
|
| 121 |
#define TIME_UAC_TERMINATE PJSIP_EVSUB_TIME_UAC_TERMINATE |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#define TIME_UAC_WAIT_NOTIFY PJSIP_EVSUB_TIME_UAC_WAIT_NOTIFY |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
enum timer_id |
| 134 |
{ |
| 135 |
|
| 136 |
TIMER_TYPE_NONE, |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
TIMER_TYPE_UAC_REFRESH, |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
TIMER_TYPE_UAS_TIMEOUT, |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
TIMER_TYPE_UAC_TERMINATE, |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
TIMER_TYPE_UAC_WAIT_NOTIFY, |
| 157 |
|
| 158 |
|
| 159 |
TIMER_TYPE_MAX |
| 160 |
}; |
| 161 |
|
| 162 |
static const char *timer_names[] = |
| 163 |
{ |
| 164 |
"None", |
| 165 |
"UAC_REFRESH", |
| 166 |
"UAS_TIMEOUT", |
| 167 |
"UAC_TERMINATE", |
| 168 |
"UAC_WAIT_NOTIFY", |
| 169 |
"INVALID_TIMER" |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
struct evpkg |
| 176 |
{ |
| 177 |
PJ_DECL_LIST_MEMBER(struct evpkg); |
| 178 |
|
| 179 |
pj_str_t pkg_name; |
| 180 |
pjsip_module *pkg_mod; |
| 181 |
unsigned pkg_expires; |
| 182 |
pjsip_accept_hdr *pkg_accept; |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static struct mod_evsub |
| 190 |
{ |
| 191 |
pjsip_module mod; |
| 192 |
pj_pool_t *pool; |
| 193 |
pjsip_endpoint *endpt; |
| 194 |
struct evpkg pkg_list; |
| 195 |
pjsip_allow_events_hdr *allow_events_hdr; |
| 196 |
|
| 197 |
} mod_evsub = |
| 198 |
{ |
| 199 |
{ |
| 200 |
NULL, NULL, |
| 201 |
{ "mod-evsub", 9 }, |
| 202 |
-1, |
| 203 |
PJSIP_MOD_PRIORITY_DIALOG_USAGE, |
| 204 |
NULL, |
| 205 |
NULL, |
| 206 |
NULL, |
| 207 |
&mod_evsub_unload, |
| 208 |
NULL, |
| 209 |
NULL, |
| 210 |
NULL, |
| 211 |
NULL, |
| 212 |
&mod_evsub_on_tsx_state, |
| 213 |
} |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
struct pjsip_evsub |
| 221 |
{ |
| 222 |
char obj_name[PJ_MAX_OBJ_NAME]; |
| 223 |
pj_pool_t *pool; |
| 224 |
pjsip_endpoint *endpt; |
| 225 |
pjsip_dialog *dlg; |
| 226 |
struct evpkg *pkg; |
| 227 |
unsigned option; |
| 228 |
pjsip_evsub_user user; |
| 229 |
pj_bool_t call_cb; |
| 230 |
pjsip_role_e role; |
| 231 |
pjsip_evsub_state state; |
| 232 |
pj_str_t state_str; |
| 233 |
pjsip_evsub_state dst_state; |
| 234 |
pj_str_t dst_state_str; |
| 235 |
pj_str_t term_reason; |
| 236 |
pjsip_method method; |
| 237 |
pjsip_event_hdr *event; |
| 238 |
pjsip_expires_hdr *expires; |
| 239 |
pjsip_accept_hdr *accept; |
| 240 |
pjsip_hdr sub_hdr_list; |
| 241 |
|
| 242 |
pj_time_val refresh_time; |
| 243 |
pj_timer_entry timer; |
| 244 |
int pending_tsx; |
| 245 |
pjsip_transaction *pending_sub; |
| 246 |
pj_timer_entry *pending_sub_timer; |
| 247 |
pjsip_tx_data *pending_notify; |
| 248 |
pj_bool_t calling_on_rx_refresh; |
| 249 |
pj_bool_t deferred_state_notify; |
| 250 |
pj_grp_lock_t *grp_lock; |
| 251 |
|
| 252 |
void *mod_data[PJSIP_MAX_MODULE]; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
struct dlgsub |
| 261 |
{ |
| 262 |
PJ_DECL_LIST_MEMBER(struct dlgsub); |
| 263 |
pjsip_evsub *sub; |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
static const pj_str_t STR_EVENT = { "Event", 5 }; |
| 269 |
static const pj_str_t STR_EVENT_S = { "o", 1 }; |
| 270 |
static const pj_str_t STR_SUB_STATE = { "Subscription-State", 18 }; |
| 271 |
static const pj_str_t STR_TERMINATED = { "terminated", 10 }; |
| 272 |
static const pj_str_t STR_ACTIVE = { "active", 6 }; |
| 273 |
static const pj_str_t STR_PENDING = { "pending", 7 }; |
| 274 |
static const pj_str_t STR_TIMEOUT = { "timeout", 7}; |
| 275 |
static const pj_str_t STR_RETRY_AFTER = { "Retry-After", 11 }; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
static pj_status_t mod_evsub_unload(void) |
| 282 |
{ |
| 283 |
pjsip_endpt_release_pool(mod_evsub.endpt, mod_evsub.pool); |
| 284 |
mod_evsub.pool = NULL; |
| 285 |
|
| 286 |
return PJ_SUCCESS; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
PJ_DECL(pj_str_t) pjsipsimple_strerror( pj_status_t statcode, |
| 293 |
char *buf, pj_size_t bufsize ); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
PJ_DEF(pj_status_t) pjsip_evsub_init_module(pjsip_endpoint *endpt) |
| 299 |
{ |
| 300 |
pj_status_t status; |
| 301 |
pj_str_t method_tags[] = { |
| 302 |
{ "SUBSCRIBE", 9}, |
| 303 |
{ "NOTIFY", 6} |
| 304 |
}; |
| 305 |
|
| 306 |
status = pj_register_strerror(PJSIP_SIMPLE_ERRNO_START, |
| 307 |
PJ_ERRNO_SPACE_SIZE, |
| 308 |
&pjsipsimple_strerror); |
| 309 |
pj_assert(status == PJ_SUCCESS); |
| 310 |
|
| 311 |
PJ_ASSERT_RETURN(endpt != NULL, PJ_EINVAL); |
| 312 |
PJ_ASSERT_RETURN(mod_evsub.mod.id == -1, PJ_EINVALIDOP); |
| 313 |
|
| 314 |
|
| 315 |
mod_evsub.endpt = endpt; |
| 316 |
|
| 317 |
|
| 318 |
pj_list_init(&mod_evsub.pkg_list); |
| 319 |
|
| 320 |
|
| 321 |
mod_evsub.pool = pjsip_endpt_create_pool(endpt, "evsub", |
| 322 |
PJSIP_POOL_EVSUB_LEN, |
| 323 |
PJSIP_POOL_EVSUB_INC); |
| 324 |
if (!mod_evsub.pool) |
| 325 |
return PJ_ENOMEM; |
| 326 |
|
| 327 |
|
| 328 |
status = pjsip_endpt_register_module(endpt, &mod_evsub.mod); |
| 329 |
if (status != PJ_SUCCESS) |
| 330 |
goto on_error; |
| 331 |
|
| 332 |
|
| 333 |
mod_evsub.allow_events_hdr = pjsip_allow_events_hdr_create(mod_evsub.pool); |
| 334 |
|
| 335 |
|
| 336 |
status = pjsip_evsub_init_parser(); |
| 337 |
if (status != PJ_SUCCESS) |
| 338 |
goto on_error; |
| 339 |
|
| 340 |
|
| 341 |
pjsip_endpt_add_capability(endpt, &mod_evsub.mod, PJSIP_H_ALLOW, NULL, |
| 342 |
2, method_tags); |
| 343 |
|
| 344 |
|
| 345 |
return PJ_SUCCESS; |
| 346 |
|
| 347 |
on_error: |
| 348 |
if (mod_evsub.pool) { |
| 349 |
pjsip_endpt_release_pool(endpt, mod_evsub.pool); |
| 350 |
mod_evsub.pool = NULL; |
| 351 |
} |
| 352 |
mod_evsub.endpt = NULL; |
| 353 |
return status; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
PJ_DEF(pjsip_module*) pjsip_evsub_instance(void) |
| 361 |
{ |
| 362 |
PJ_ASSERT_RETURN(mod_evsub.mod.id != -1, NULL); |
| 363 |
|
| 364 |
return &mod_evsub.mod; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
PJ_DEF(pjsip_evsub*) pjsip_tsx_get_evsub(const pjsip_transaction *tsx) |
| 372 |
{ |
| 373 |
return (pjsip_evsub*) tsx->mod_data[mod_evsub.mod.id]; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
PJ_DEF(void) pjsip_evsub_set_mod_data( pjsip_evsub *sub, unsigned mod_id, |
| 381 |
void *data ) |
| 382 |
{ |
| 383 |
PJ_ASSERT_ON_FAIL(mod_id < PJSIP_MAX_MODULE, return); |
| 384 |
sub->mod_data[mod_id] = data; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
PJ_DEF(void*) pjsip_evsub_get_mod_data( const pjsip_evsub *sub, |
| 392 |
unsigned mod_id ) |
| 393 |
{ |
| 394 |
PJ_ASSERT_RETURN(mod_id < PJSIP_MAX_MODULE, NULL); |
| 395 |
return sub->mod_data[mod_id]; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
static struct evpkg* find_pkg(const pj_str_t *event_name) |
| 403 |
{ |
| 404 |
struct evpkg *pkg; |
| 405 |
|
| 406 |
pkg = mod_evsub.pkg_list.next; |
| 407 |
while (pkg != &mod_evsub.pkg_list) { |
| 408 |
|
| 409 |
if (pj_stricmp(&pkg->pkg_name, event_name) == 0) { |
| 410 |
return pkg; |
| 411 |
} |
| 412 |
|
| 413 |
pkg = pkg->next; |
| 414 |
} |
| 415 |
|
| 416 |
return NULL; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
PJ_DEF(pj_status_t) pjsip_evsub_register_pkg( pjsip_module *pkg_mod, |
| 423 |
const pj_str_t *event_name, |
| 424 |
unsigned expires, |
| 425 |
unsigned accept_cnt, |
| 426 |
const pj_str_t accept[]) |
| 427 |
{ |
| 428 |
struct evpkg *pkg; |
| 429 |
unsigned i; |
| 430 |
|
| 431 |
PJ_ASSERT_RETURN(pkg_mod && event_name, PJ_EINVAL); |
| 432 |
|
| 433 |
|
| 434 |
PJ_ASSERT_RETURN(accept_cnt <= PJSIP_GENERIC_ARRAY_MAX_COUNT, |
| 435 |
PJ_ETOOMANY); |
| 436 |
|
| 437 |
|
| 438 |
PJ_ASSERT_RETURN(mod_evsub.mod.id != -1, PJ_EINVALIDOP); |
| 439 |
|
| 440 |
|
| 441 |
if (find_pkg(event_name) != NULL) |
| 442 |
return PJSIP_SIMPLE_EPKGEXISTS; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
pkg = PJ_POOL_ALLOC_T(mod_evsub.pool, struct evpkg); |
| 447 |
pkg->pkg_mod = pkg_mod; |
| 448 |
pkg->pkg_expires = expires; |
| 449 |
pj_strdup(mod_evsub.pool, &pkg->pkg_name, event_name); |
| 450 |
|
| 451 |
pkg->pkg_accept = pjsip_accept_hdr_create(mod_evsub.pool); |
| 452 |
pkg->pkg_accept->count = accept_cnt; |
| 453 |
for (i=0; i<accept_cnt; ++i) { |
| 454 |
pj_strdup(mod_evsub.pool, &pkg->pkg_accept->values[i], &accept[i]); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
pj_list_push_back(&mod_evsub.pkg_list, pkg); |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
if (mod_evsub.allow_events_hdr->count != |
| 464 |
PJ_ARRAY_SIZE(mod_evsub.allow_events_hdr->values)) |
| 465 |
{ |
| 466 |
mod_evsub.allow_events_hdr->values[mod_evsub.allow_events_hdr->count] = |
| 467 |
pkg->pkg_name; |
| 468 |
++mod_evsub.allow_events_hdr->count; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
pjsip_endpt_add_capability(mod_evsub.endpt, &mod_evsub.mod, |
| 473 |
PJSIP_H_ACCEPT, NULL, |
| 474 |
pkg->pkg_accept->count, |
| 475 |
pkg->pkg_accept->values); |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
PJ_LOG(5,(THIS_FILE, "Event pkg \"%.*s\" registered by %.*s", |
| 481 |
(int)event_name->slen, event_name->ptr, |
| 482 |
(int)pkg_mod->name.slen, pkg_mod->name.ptr)); |
| 483 |
|
| 484 |
return PJ_SUCCESS; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
PJ_DEF(const pjsip_hdr*) pjsip_evsub_get_allow_events_hdr( |
| 492 |
const pjsip_module *m) |
| 493 |
{ |
| 494 |
struct mod_evsub *mod; |
| 495 |
|
| 496 |
if (m == NULL) |
| 497 |
m = pjsip_evsub_instance(); |
| 498 |
|
| 499 |
mod = (struct mod_evsub*)m; |
| 500 |
|
| 501 |
return (pjsip_hdr*) mod->allow_events_hdr; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
static void update_expires( pjsip_evsub *sub, pj_uint32_t interval ) |
| 509 |
{ |
| 510 |
pj_gettimeofday(&sub->refresh_time); |
| 511 |
sub->refresh_time.sec += interval; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
static void set_timer( pjsip_evsub *sub, int timer_id, |
| 519 |
pj_uint32_t seconds) |
| 520 |
{ |
| 521 |
if (sub->timer.id != TIMER_TYPE_NONE) { |
| 522 |
PJ_LOG(5,(sub->obj_name, "%s %s timer", |
| 523 |
(timer_id==sub->timer.id ? "Updating" : "Cancelling"), |
| 524 |
timer_names[sub->timer.id])); |
| 525 |
pjsip_endpt_cancel_timer(sub->endpt, &sub->timer); |
| 526 |
sub->timer.id = TIMER_TYPE_NONE; |
| 527 |
} |
| 528 |
|
| 529 |
if (timer_id != TIMER_TYPE_NONE && seconds != PJSIP_EXPIRES_NOT_SPECIFIED) |
| 530 |
{ |
| 531 |
pj_time_val timeout; |
| 532 |
|
| 533 |
PJ_ASSERT_ON_FAIL(timer_id>TIMER_TYPE_NONE && timer_id<TIMER_TYPE_MAX, |
| 534 |
return); |
| 535 |
|
| 536 |
timeout.sec = seconds; |
| 537 |
timeout.msec = 0; |
| 538 |
|
| 539 |
pj_timer_heap_schedule_w_grp_lock( |
| 540 |
pjsip_endpt_get_timer_heap(sub->endpt), |
| 541 |
&sub->timer, &timeout, timer_id, sub->grp_lock); |
| 542 |
|
| 543 |
PJ_LOG(5,(sub->obj_name, "Timer %s scheduled in %ld seconds", |
| 544 |
timer_names[sub->timer.id], timeout.sec)); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
PJ_DEF(void) pjsip_evsub_uas_set_timeout(pjsip_evsub *sub, pj_uint32_t seconds) |
| 553 |
{ |
| 554 |
set_timer(sub, TIMER_TYPE_UAS_TIMEOUT, (pj_int32_t)seconds); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
static void evsub_on_destroy(void *obj) |
| 562 |
{ |
| 563 |
pjsip_evsub *sub = (pjsip_evsub*)obj; |
| 564 |
|
| 565 |
|
| 566 |
pjsip_dlg_dec_session(sub->dlg, &mod_evsub.mod); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
static void evsub_destroy( pjsip_evsub *sub ) |
| 574 |
{ |
| 575 |
struct dlgsub *dlgsub_head, *dlgsub; |
| 576 |
|
| 577 |
PJ_LOG(4,(sub->obj_name, "Subscription destroyed")); |
| 578 |
|
| 579 |
|
| 580 |
set_timer(sub, TIMER_TYPE_NONE, 0); |
| 581 |
|
| 582 |
|
| 583 |
if (sub->pending_sub_timer && sub->pending_sub_timer->id == 1) { |
| 584 |
pjsip_endpt_cancel_timer(sub->endpt, sub->pending_sub_timer); |
| 585 |
sub->pending_sub_timer->id = 0; |
| 586 |
sub->pending_sub_timer = NULL; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
dlgsub_head = (struct dlgsub *) sub->dlg->mod_data[mod_evsub.mod.id]; |
| 591 |
dlgsub = dlgsub_head->next; |
| 592 |
while (dlgsub != dlgsub_head) { |
| 593 |
|
| 594 |
if (dlgsub->sub == sub) { |
| 595 |
pj_list_erase(dlgsub); |
| 596 |
break; |
| 597 |
} |
| 598 |
|
| 599 |
dlgsub = dlgsub->next; |
| 600 |
} |
| 601 |
|
| 602 |
pj_grp_lock_dec_ref(sub->grp_lock); |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
static pj_status_t evsub_async_auth_send_impl( |
| 609 |
pjsip_auth_clt_sess *auth_sess, |
| 610 |
void *user_data, |
| 611 |
pjsip_tx_data *tdata) |
| 612 |
{ |
| 613 |
pjsip_evsub *sub = (pjsip_evsub *)user_data; |
| 614 |
PJ_UNUSED_ARG(auth_sess); |
| 615 |
return pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
static void evsub_async_auth_abandon_impl(pjsip_auth_clt_sess *auth_sess, |
| 622 |
void *user_data) |
| 623 |
{ |
| 624 |
pjsip_evsub *sub = (pjsip_evsub *)user_data; |
| 625 |
PJ_UNUSED_ARG(auth_sess); |
| 626 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL, NULL); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
static void set_state( pjsip_evsub *sub, pjsip_evsub_state state, |
| 633 |
const pj_str_t *state_str, pjsip_event *event, |
| 634 |
const pj_str_t *reason) |
| 635 |
{ |
| 636 |
pjsip_evsub_state prev_state = sub->state; |
| 637 |
pj_str_t old_state_str = sub->state_str; |
| 638 |
pjsip_event dummy_event; |
| 639 |
|
| 640 |
sub->state = state; |
| 641 |
|
| 642 |
if (state_str && state_str->slen) |
| 643 |
pj_strdup_with_null(sub->pool, &sub->state_str, state_str); |
| 644 |
else |
| 645 |
sub->state_str = evsub_state_names[state]; |
| 646 |
|
| 647 |
if (reason && sub->term_reason.slen==0) |
| 648 |
pj_strdup(sub->pool, &sub->term_reason, reason); |
| 649 |
|
| 650 |
PJ_LOG(4,(sub->obj_name, |
| 651 |
"Subscription state changed %.*s --> %.*s", |
| 652 |
(int)old_state_str.slen, |
| 653 |
old_state_str.ptr, |
| 654 |
(int)sub->state_str.slen, |
| 655 |
sub->state_str.ptr)); |
| 656 |
pj_log_push_indent(); |
| 657 |
|
| 658 |
|
| 659 |
if (!event) { |
| 660 |
PJSIP_EVENT_INIT_USER(dummy_event, 0, 0, 0, 0); |
| 661 |
event = &dummy_event; |
| 662 |
} |
| 663 |
|
| 664 |
if (sub->user.on_evsub_state && sub->call_cb) { |
| 665 |
if (state == PJSIP_EVSUB_STATE_TERMINATED && |
| 666 |
sub->calling_on_rx_refresh) |
| 667 |
{ |
| 668 |
sub->deferred_state_notify = PJ_TRUE; |
| 669 |
} else { |
| 670 |
(*sub->user.on_evsub_state)(sub, event); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
if (state == PJSIP_EVSUB_STATE_TERMINATED && |
| 675 |
prev_state != PJSIP_EVSUB_STATE_TERMINATED) |
| 676 |
{ |
| 677 |
|
| 678 |
set_timer(sub, TIMER_TYPE_NONE, 0); |
| 679 |
|
| 680 |
|
| 681 |
if (sub->pending_tsx == 0 && !sub->calling_on_rx_refresh) { |
| 682 |
evsub_destroy(sub); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
pj_log_pop_indent(); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
static void on_timer( pj_timer_heap_t *timer_heap, |
| 694 |
struct pj_timer_entry *entry) |
| 695 |
{ |
| 696 |
pjsip_evsub *sub; |
| 697 |
int timer_id; |
| 698 |
|
| 699 |
PJ_UNUSED_ARG(timer_heap); |
| 700 |
|
| 701 |
sub = (pjsip_evsub*) entry->user_data; |
| 702 |
|
| 703 |
pjsip_dlg_inc_lock(sub->dlg); |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
if (pj_timer_entry_running(entry) || entry->id == TIMER_TYPE_NONE) { |
| 709 |
pjsip_dlg_dec_lock(sub->dlg); |
| 710 |
return; |
| 711 |
} |
| 712 |
|
| 713 |
timer_id = entry->id; |
| 714 |
entry->id = TIMER_TYPE_NONE; |
| 715 |
|
| 716 |
switch (timer_id) { |
| 717 |
|
| 718 |
case TIMER_TYPE_UAC_REFRESH: |
| 719 |
|
| 720 |
if (sub->user.on_client_refresh && sub->call_cb) { |
| 721 |
(*sub->user.on_client_refresh)(sub); |
| 722 |
} else { |
| 723 |
pjsip_tx_data *tdata; |
| 724 |
pj_status_t status; |
| 725 |
|
| 726 |
PJ_LOG(5,(sub->obj_name, "Refreshing subscription.")); |
| 727 |
pj_log_push_indent(); |
| 728 |
status = pjsip_evsub_initiate(sub, NULL, |
| 729 |
sub->expires->ivalue, |
| 730 |
&tdata); |
| 731 |
if (status == PJ_SUCCESS) |
| 732 |
pjsip_evsub_send_request(sub, tdata); |
| 733 |
|
| 734 |
pj_log_pop_indent(); |
| 735 |
} |
| 736 |
break; |
| 737 |
|
| 738 |
case TIMER_TYPE_UAS_TIMEOUT: |
| 739 |
|
| 740 |
if (sub->user.on_server_timeout && sub->call_cb) { |
| 741 |
(*sub->user.on_server_timeout)(sub); |
| 742 |
} else { |
| 743 |
pjsip_tx_data *tdata; |
| 744 |
pj_status_t status; |
| 745 |
|
| 746 |
PJ_LOG(5,(sub->obj_name, "Timeout waiting for refresh. " |
| 747 |
"Sending NOTIFY to terminate.")); |
| 748 |
pj_log_push_indent(); |
| 749 |
status = pjsip_evsub_notify( sub, PJSIP_EVSUB_STATE_TERMINATED, |
| 750 |
NULL, &STR_TIMEOUT, &tdata); |
| 751 |
if (status == PJ_SUCCESS) |
| 752 |
pjsip_evsub_send_request(sub, tdata); |
| 753 |
|
| 754 |
pj_log_pop_indent(); |
| 755 |
} |
| 756 |
break; |
| 757 |
|
| 758 |
case TIMER_TYPE_UAC_TERMINATE: |
| 759 |
{ |
| 760 |
pj_str_t timeout = {"timeout", 7}; |
| 761 |
|
| 762 |
PJ_LOG(5,(sub->obj_name, "Timeout waiting for final NOTIFY. " |
| 763 |
"Terminating..")); |
| 764 |
pj_log_push_indent(); |
| 765 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL, |
| 766 |
&timeout); |
| 767 |
pj_log_pop_indent(); |
| 768 |
} |
| 769 |
break; |
| 770 |
|
| 771 |
case TIMER_TYPE_UAC_WAIT_NOTIFY: |
| 772 |
{ |
| 773 |
pjsip_tx_data *tdata; |
| 774 |
pj_status_t status; |
| 775 |
|
| 776 |
PJ_LOG(5,(sub->obj_name, |
| 777 |
"Timeout waiting for subsequent NOTIFY (we did " |
| 778 |
"send non-2xx response for previous NOTIFY). " |
| 779 |
"Unsubscribing..")); |
| 780 |
pj_log_push_indent(); |
| 781 |
status = pjsip_evsub_initiate( sub, NULL, 0, &tdata); |
| 782 |
if (status == PJ_SUCCESS) |
| 783 |
pjsip_evsub_send_request(sub, tdata); |
| 784 |
|
| 785 |
pj_log_pop_indent(); |
| 786 |
} |
| 787 |
break; |
| 788 |
|
| 789 |
default: |
| 790 |
pj_assert(!"Invalid timer id"); |
| 791 |
} |
| 792 |
|
| 793 |
pjsip_dlg_dec_lock(sub->dlg); |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
static pj_status_t evsub_create( pjsip_dialog *dlg, |
| 801 |
pjsip_role_e role, |
| 802 |
const pjsip_evsub_user *user_cb, |
| 803 |
const pj_str_t *event, |
| 804 |
unsigned option, |
| 805 |
pjsip_evsub **p_evsub ) |
| 806 |
{ |
| 807 |
pjsip_evsub *sub; |
| 808 |
struct evpkg *pkg; |
| 809 |
struct dlgsub *dlgsub_head, *dlgsub; |
| 810 |
pj_status_t status; |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
pkg = find_pkg(event); |
| 815 |
if (pkg == NULL) |
| 816 |
return PJSIP_SIMPLE_ENOPKG; |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
pjsip_dlg_inc_lock(dlg); |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
sub = PJ_POOL_ZALLOC_T(dlg->pool, struct pjsip_evsub); |
| 825 |
sub->pool = dlg->pool; |
| 826 |
sub->endpt = dlg->endpt; |
| 827 |
sub->dlg = dlg; |
| 828 |
sub->pkg = pkg; |
| 829 |
sub->role = role; |
| 830 |
sub->call_cb = PJ_TRUE; |
| 831 |
sub->option = option; |
| 832 |
sub->state = PJSIP_EVSUB_STATE_NULL; |
| 833 |
sub->state_str = evsub_state_names[sub->state]; |
| 834 |
sub->expires = pjsip_expires_hdr_create(sub->pool, pkg->pkg_expires); |
| 835 |
sub->accept = (pjsip_accept_hdr*) |
| 836 |
pjsip_hdr_clone(sub->pool, pkg->pkg_accept); |
| 837 |
pj_list_init(&sub->sub_hdr_list); |
| 838 |
|
| 839 |
sub->timer.user_data = sub; |
| 840 |
sub->timer.cb = &on_timer; |
| 841 |
|
| 842 |
|
| 843 |
pj_ansi_snprintf(sub->obj_name, PJ_ARRAY_SIZE(sub->obj_name), |
| 844 |
"evsub%p", sub); |
| 845 |
|
| 846 |
|
| 847 |
if (user_cb) |
| 848 |
pj_memcpy(&sub->user, user_cb, sizeof(pjsip_evsub_user)); |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
sub->event = pjsip_event_hdr_create(sub->pool); |
| 853 |
pj_strdup(sub->pool, &sub->event->event_type, event); |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
if (pjsip_dlg_has_usage(dlg, &mod_evsub.mod)) { |
| 861 |
dlgsub_head = (struct dlgsub*) dlg->mod_data[mod_evsub.mod.id]; |
| 862 |
dlgsub = PJ_POOL_ALLOC_T(sub->pool, struct dlgsub); |
| 863 |
dlgsub->sub = sub; |
| 864 |
pj_list_push_back(dlgsub_head, dlgsub); |
| 865 |
} else { |
| 866 |
dlgsub_head = PJ_POOL_ALLOC_T(sub->pool, struct dlgsub); |
| 867 |
dlgsub = PJ_POOL_ALLOC_T(sub->pool, struct dlgsub); |
| 868 |
dlgsub->sub = sub; |
| 869 |
|
| 870 |
pj_list_init(dlgsub_head); |
| 871 |
pj_list_push_back(dlgsub_head, dlgsub); |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
status = pjsip_dlg_add_usage(dlg, &mod_evsub.mod, dlgsub_head); |
| 877 |
if (status != PJ_SUCCESS) { |
| 878 |
pjsip_dlg_dec_lock(dlg); |
| 879 |
return status; |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
PJ_LOG(5,(sub->obj_name, "%s subscription created, using dialog %s", |
| 884 |
(role==PJSIP_ROLE_UAC ? "UAC" : "UAS"), |
| 885 |
dlg->obj_name)); |
| 886 |
|
| 887 |
*p_evsub = sub; |
| 888 |
pjsip_dlg_dec_lock(dlg); |
| 889 |
|
| 890 |
return PJ_SUCCESS; |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
PJ_DEF(pj_status_t) pjsip_evsub_add_ref(pjsip_evsub *sub) |
| 897 |
{ |
| 898 |
return pj_grp_lock_add_ref(sub->grp_lock); |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
PJ_DEF(pj_status_t) pjsip_evsub_dec_ref(pjsip_evsub *sub) |
| 905 |
{ |
| 906 |
return pj_grp_lock_dec_ref(sub->grp_lock); |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
PJ_DEF(pj_status_t) pjsip_evsub_create_uac( pjsip_dialog *dlg, |
| 913 |
const pjsip_evsub_user *user_cb, |
| 914 |
const pj_str_t *event, |
| 915 |
unsigned option, |
| 916 |
pjsip_evsub **p_evsub) |
| 917 |
{ |
| 918 |
pjsip_evsub *sub; |
| 919 |
pj_status_t status; |
| 920 |
|
| 921 |
PJ_ASSERT_RETURN(dlg && event && p_evsub, PJ_EINVAL); |
| 922 |
|
| 923 |
pjsip_dlg_inc_lock(dlg); |
| 924 |
status = evsub_create(dlg, PJSIP_UAC_ROLE, user_cb, event, option, &sub); |
| 925 |
if (status != PJ_SUCCESS) |
| 926 |
goto on_return; |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
if ((option & PJSIP_EVSUB_NO_EVENT_ID) == 0) { |
| 932 |
pj_create_unique_string(sub->pool, &sub->event->id_param); |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
pjsip_dlg_inc_session(sub->dlg, &mod_evsub.mod); |
| 937 |
|
| 938 |
|
| 939 |
status = pj_grp_lock_create(dlg->pool, NULL, &sub->grp_lock); |
| 940 |
if (status != PJ_SUCCESS) { |
| 941 |
pjsip_dlg_dec_session(sub->dlg, &mod_evsub.mod); |
| 942 |
goto on_return; |
| 943 |
} |
| 944 |
|
| 945 |
pj_grp_lock_add_ref(sub->grp_lock); |
| 946 |
pj_grp_lock_add_handler(sub->grp_lock, dlg->pool, sub, &evsub_on_destroy); |
| 947 |
|
| 948 |
|
| 949 |
*p_evsub = sub; |
| 950 |
|
| 951 |
on_return: |
| 952 |
pjsip_dlg_dec_lock(dlg); |
| 953 |
return status; |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
PJ_DEF(pj_status_t) pjsip_evsub_create_uas( pjsip_dialog *dlg, |
| 961 |
const pjsip_evsub_user *user_cb, |
| 962 |
pjsip_rx_data *rdata, |
| 963 |
unsigned option, |
| 964 |
pjsip_evsub **p_evsub) |
| 965 |
{ |
| 966 |
pjsip_evsub *sub; |
| 967 |
pjsip_transaction *tsx; |
| 968 |
pjsip_accept_hdr *accept_hdr; |
| 969 |
pjsip_event_hdr *event_hdr; |
| 970 |
pjsip_expires_hdr *expires_hdr; |
| 971 |
pj_status_t status; |
| 972 |
|
| 973 |
|
| 974 |
PJ_ASSERT_RETURN(dlg && rdata && p_evsub, PJ_EINVAL); |
| 975 |
|
| 976 |
|
| 977 |
PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG, |
| 978 |
PJSIP_ENOTREQUESTMSG); |
| 979 |
|
| 980 |
|
| 981 |
tsx = pjsip_rdata_get_tsx(rdata); |
| 982 |
PJ_ASSERT_RETURN(tsx != NULL, PJSIP_ENOTSX); |
| 983 |
|
| 984 |
|
| 985 |
PJ_ASSERT_RETURN(tsx->mod_data[mod_evsub.mod.id] == NULL, |
| 986 |
PJSIP_ETYPEEXISTS); |
| 987 |
|
| 988 |
|
| 989 |
PJ_ASSERT_RETURN(user_cb->on_rx_refresh, PJ_EINVALIDOP); |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
event_hdr = (pjsip_event_hdr*) |
| 995 |
pjsip_msg_find_hdr_by_names(rdata->msg_info.msg, &STR_EVENT, |
| 996 |
&STR_EVENT_S, NULL); |
| 997 |
if (event_hdr == NULL) { |
| 998 |
return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST); |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
pjsip_dlg_inc_lock(dlg); |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
status = evsub_create(dlg, PJSIP_UAS_ROLE, user_cb, |
| 1008 |
&event_hdr->event_type, option, &sub); |
| 1009 |
if (status != PJ_SUCCESS) |
| 1010 |
goto on_return; |
| 1011 |
|
| 1012 |
|
| 1013 |
sub->event = (pjsip_event_hdr*) pjsip_hdr_clone(sub->pool, event_hdr); |
| 1014 |
|
| 1015 |
|
| 1016 |
pjsip_method_copy(sub->pool, &sub->method, |
| 1017 |
&rdata->msg_info.msg->line.req.method); |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
expires_hdr = (pjsip_expires_hdr*) |
| 1023 |
pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL); |
| 1024 |
if (expires_hdr) { |
| 1025 |
struct evpkg *evpkg; |
| 1026 |
|
| 1027 |
evpkg = find_pkg(&event_hdr->event_type); |
| 1028 |
if (evpkg) { |
| 1029 |
if (expires_hdr->ivalue < evpkg->pkg_expires) |
| 1030 |
sub->expires->ivalue = expires_hdr->ivalue; |
| 1031 |
else |
| 1032 |
sub->expires->ivalue = evpkg->pkg_expires; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
update_expires(sub, sub->expires->ivalue); |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
accept_hdr = (pjsip_accept_hdr*) |
| 1042 |
pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, NULL); |
| 1043 |
if (accept_hdr) |
| 1044 |
sub->accept = (pjsip_accept_hdr*)pjsip_hdr_clone(sub->pool,accept_hdr); |
| 1045 |
|
| 1046 |
|
| 1047 |
pjsip_dlg_inc_session(dlg, &mod_evsub.mod); |
| 1048 |
|
| 1049 |
|
| 1050 |
status = pj_grp_lock_create(dlg->pool, NULL, &sub->grp_lock); |
| 1051 |
if (status != PJ_SUCCESS) { |
| 1052 |
pjsip_dlg_dec_session(sub->dlg, &mod_evsub.mod); |
| 1053 |
goto on_return; |
| 1054 |
} |
| 1055 |
|
| 1056 |
pj_grp_lock_add_ref(sub->grp_lock); |
| 1057 |
pj_grp_lock_add_handler(sub->grp_lock, dlg->pool, sub, &evsub_on_destroy); |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
sub->pending_tsx++; |
| 1062 |
tsx->mod_data[mod_evsub.mod.id] = sub; |
| 1063 |
|
| 1064 |
|
| 1065 |
*p_evsub = sub; |
| 1066 |
|
| 1067 |
|
| 1068 |
on_return: |
| 1069 |
pjsip_dlg_dec_lock(dlg); |
| 1070 |
return status; |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
PJ_DEF(pj_status_t) pjsip_evsub_terminate( pjsip_evsub *sub, |
| 1078 |
pj_bool_t notify ) |
| 1079 |
{ |
| 1080 |
PJ_ASSERT_RETURN(sub, PJ_EINVAL); |
| 1081 |
|
| 1082 |
pjsip_dlg_inc_lock(sub->dlg); |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
sub->call_cb = notify; |
| 1094 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL, NULL); |
| 1095 |
|
| 1096 |
pjsip_dlg_dec_lock(sub->dlg); |
| 1097 |
return PJ_SUCCESS; |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
PJ_DEF(pjsip_evsub_state) pjsip_evsub_get_state(const pjsip_evsub *sub) |
| 1104 |
{ |
| 1105 |
return sub->state; |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
PJ_DEF(const char*) pjsip_evsub_get_state_name(const pjsip_evsub *sub) |
| 1112 |
{ |
| 1113 |
return sub->state_str.ptr; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
PJ_DEF(const pj_str_t*) pjsip_evsub_get_termination_reason( |
| 1120 |
const pjsip_evsub *sub) |
| 1121 |
{ |
| 1122 |
return &sub->term_reason; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
PJ_DEF(pj_uint32_t) pjsip_evsub_get_expires(const pjsip_evsub *sub) |
| 1129 |
{ |
| 1130 |
return sub->expires->ivalue; |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
PJ_DEF(pj_status_t) pjsip_evsub_initiate( pjsip_evsub *sub, |
| 1137 |
const pjsip_method *method, |
| 1138 |
pj_uint32_t expires, |
| 1139 |
pjsip_tx_data **p_tdata) |
| 1140 |
{ |
| 1141 |
pjsip_tx_data *tdata; |
| 1142 |
pj_status_t status; |
| 1143 |
|
| 1144 |
PJ_ASSERT_RETURN(sub!=NULL && p_tdata!=NULL, PJ_EINVAL); |
| 1145 |
|
| 1146 |
|
| 1147 |
if (method == NULL) |
| 1148 |
method = &pjsip_subscribe_method; |
| 1149 |
|
| 1150 |
pjsip_dlg_inc_lock(sub->dlg); |
| 1151 |
|
| 1152 |
|
| 1153 |
if (sub->state == PJSIP_EVSUB_STATE_NULL) |
| 1154 |
pjsip_method_copy(sub->pool, &sub->method, method); |
| 1155 |
|
| 1156 |
status = pjsip_dlg_create_request( sub->dlg, method, -1, &tdata); |
| 1157 |
if (status != PJ_SUCCESS) |
| 1158 |
goto on_return; |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 1163 |
pjsip_hdr_shallow_clone(tdata->pool, sub->event)); |
| 1164 |
|
| 1165 |
|
| 1166 |
if (expires != PJSIP_EXPIRES_NOT_SPECIFIED) |
| 1167 |
sub->expires->ivalue = expires; |
| 1168 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 1169 |
pjsip_hdr_shallow_clone(tdata->pool, sub->expires)); |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
{ |
| 1175 |
const pjsip_hdr *hdr = pjsip_endpt_get_capability(sub->endpt, |
| 1176 |
PJSIP_H_SUPPORTED, |
| 1177 |
NULL); |
| 1178 |
if (hdr) { |
| 1179 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 1180 |
pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
|
| 1185 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 1186 |
pjsip_hdr_shallow_clone(tdata->pool, sub->accept)); |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 1191 |
pjsip_hdr_shallow_clone(tdata->pool, |
| 1192 |
mod_evsub.allow_events_hdr)); |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
{ |
| 1197 |
const pjsip_hdr *hdr = sub->sub_hdr_list.next; |
| 1198 |
while (hdr != &sub->sub_hdr_list) { |
| 1199 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 1200 |
pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 1201 |
hdr = hdr->next; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
*p_tdata = tdata; |
| 1207 |
|
| 1208 |
|
| 1209 |
on_return: |
| 1210 |
|
| 1211 |
pjsip_dlg_dec_lock(sub->dlg); |
| 1212 |
return status; |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
PJ_DEF(pj_status_t) pjsip_evsub_add_header( pjsip_evsub *sub, |
| 1220 |
const pjsip_hdr *hdr_list ) |
| 1221 |
{ |
| 1222 |
const pjsip_hdr *hdr; |
| 1223 |
|
| 1224 |
PJ_ASSERT_RETURN(sub && hdr_list, PJ_EINVAL); |
| 1225 |
|
| 1226 |
hdr = hdr_list->next; |
| 1227 |
while (hdr != hdr_list) { |
| 1228 |
pj_list_push_back(&sub->sub_hdr_list, (pjsip_hdr*) |
| 1229 |
pjsip_hdr_clone(sub->pool, hdr)); |
| 1230 |
hdr = hdr->next; |
| 1231 |
} |
| 1232 |
|
| 1233 |
return PJ_SUCCESS; |
| 1234 |
} |
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
PJ_DEF(pj_status_t) pjsip_evsub_accept( pjsip_evsub *sub, |
| 1241 |
pjsip_rx_data *rdata, |
| 1242 |
int st_code, |
| 1243 |
const pjsip_hdr *hdr_list ) |
| 1244 |
{ |
| 1245 |
pjsip_tx_data *tdata; |
| 1246 |
pjsip_transaction *tsx; |
| 1247 |
pj_status_t status; |
| 1248 |
|
| 1249 |
|
| 1250 |
PJ_ASSERT_RETURN(sub && rdata, PJ_EINVAL); |
| 1251 |
|
| 1252 |
|
| 1253 |
PJ_ASSERT_RETURN(sub->role == PJSIP_ROLE_UAS, PJ_EINVALIDOP); |
| 1254 |
|
| 1255 |
|
| 1256 |
PJ_ASSERT_RETURN(st_code/100 == 2, PJ_EINVALIDOP); |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
tsx = pjsip_rdata_get_tsx(rdata); |
| 1263 |
PJ_ASSERT_RETURN(tsx->mod_data[mod_evsub.mod.id] != NULL, |
| 1264 |
PJ_EINVALIDOP); |
| 1265 |
|
| 1266 |
|
| 1267 |
pjsip_dlg_inc_lock(sub->dlg); |
| 1268 |
|
| 1269 |
|
| 1270 |
status = pjsip_dlg_create_response( sub->dlg, rdata, st_code, NULL, |
| 1271 |
&tdata); |
| 1272 |
if (status != PJ_SUCCESS) |
| 1273 |
goto on_return; |
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 1278 |
pjsip_hdr_shallow_clone(tdata->pool, sub->expires)); |
| 1279 |
|
| 1280 |
|
| 1281 |
if (hdr_list) { |
| 1282 |
const pjsip_hdr *hdr = hdr_list->next; |
| 1283 |
while (hdr != hdr_list) { |
| 1284 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 1285 |
pjsip_hdr_clone(tdata->pool, hdr)); |
| 1286 |
hdr = hdr->next; |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
status = pjsip_dlg_send_response( sub->dlg, tsx, tdata ); |
| 1292 |
if (status != PJ_SUCCESS) |
| 1293 |
goto on_return; |
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
if (st_code/100 == 2 && sub->state != PJSIP_EVSUB_STATE_TERMINATED) { |
| 1299 |
PJ_LOG(5,(sub->obj_name, "UAS timeout in %d seconds", |
| 1300 |
sub->expires->ivalue)); |
| 1301 |
set_timer(sub, TIMER_TYPE_UAS_TIMEOUT, sub->expires->ivalue); |
| 1302 |
} |
| 1303 |
|
| 1304 |
on_return: |
| 1305 |
|
| 1306 |
pjsip_dlg_dec_lock(sub->dlg); |
| 1307 |
return status; |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
static pjsip_sub_state_hdr* sub_state_create( pj_pool_t *pool, |
| 1316 |
pjsip_evsub *sub, |
| 1317 |
pjsip_evsub_state state, |
| 1318 |
const pj_str_t *state_str, |
| 1319 |
const pj_str_t *reason ) |
| 1320 |
{ |
| 1321 |
pjsip_sub_state_hdr *sub_state; |
| 1322 |
pj_time_val now, delay; |
| 1323 |
|
| 1324 |
|
| 1325 |
pj_gettimeofday(&now); |
| 1326 |
delay = sub->refresh_time; |
| 1327 |
PJ_TIME_VAL_SUB(delay, now); |
| 1328 |
|
| 1329 |
|
| 1330 |
sub_state = pjsip_sub_state_hdr_create(pool); |
| 1331 |
|
| 1332 |
|
| 1333 |
switch (state) { |
| 1334 |
case PJSIP_EVSUB_STATE_NULL: |
| 1335 |
case PJSIP_EVSUB_STATE_SENT: |
| 1336 |
pj_assert(!"Invalid state!"); |
| 1337 |
|
| 1338 |
|
| 1339 |
case PJSIP_EVSUB_STATE_ACCEPTED: |
| 1340 |
case PJSIP_EVSUB_STATE_PENDING: |
| 1341 |
sub_state->sub_state = STR_PENDING; |
| 1342 |
sub_state->expires_param = delay.sec; |
| 1343 |
break; |
| 1344 |
|
| 1345 |
case PJSIP_EVSUB_STATE_ACTIVE: |
| 1346 |
sub_state->sub_state = STR_ACTIVE; |
| 1347 |
sub_state->expires_param = delay.sec; |
| 1348 |
break; |
| 1349 |
|
| 1350 |
case PJSIP_EVSUB_STATE_TERMINATED: |
| 1351 |
sub_state->sub_state = STR_TERMINATED; |
| 1352 |
if (reason != NULL) |
| 1353 |
pj_strdup(pool, &sub_state->reason_param, reason); |
| 1354 |
break; |
| 1355 |
|
| 1356 |
case PJSIP_EVSUB_STATE_UNKNOWN: |
| 1357 |
pj_assert(state_str != NULL); |
| 1358 |
pj_strdup(pool, &sub_state->sub_state, state_str); |
| 1359 |
break; |
| 1360 |
} |
| 1361 |
|
| 1362 |
return sub_state; |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
PJ_DEF(pj_status_t) pjsip_evsub_notify( pjsip_evsub *sub, |
| 1369 |
pjsip_evsub_state state, |
| 1370 |
const pj_str_t *state_str, |
| 1371 |
const pj_str_t *reason, |
| 1372 |
pjsip_tx_data **p_tdata) |
| 1373 |
{ |
| 1374 |
pjsip_tx_data *tdata; |
| 1375 |
pjsip_sub_state_hdr *sub_state; |
| 1376 |
pj_status_t status; |
| 1377 |
|
| 1378 |
|
| 1379 |
PJ_ASSERT_RETURN(sub!=NULL && p_tdata!=NULL, PJ_EINVAL); |
| 1380 |
|
| 1381 |
|
| 1382 |
pjsip_dlg_inc_lock(sub->dlg); |
| 1383 |
|
| 1384 |
|
| 1385 |
status = pjsip_dlg_create_request( sub->dlg, pjsip_get_notify_method(), |
| 1386 |
-1, &tdata); |
| 1387 |
if (status != PJ_SUCCESS) |
| 1388 |
goto on_return; |
| 1389 |
|
| 1390 |
|
| 1391 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 1392 |
pjsip_hdr_shallow_clone(tdata->pool, sub->event)); |
| 1393 |
|
| 1394 |
|
| 1395 |
sub_state = sub_state_create(tdata->pool, sub, state, state_str, |
| 1396 |
reason); |
| 1397 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)sub_state); |
| 1398 |
|
| 1399 |
|
| 1400 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) |
| 1401 |
pjsip_hdr_shallow_clone(tdata->pool, mod_evsub.allow_events_hdr)); |
| 1402 |
|
| 1403 |
|
| 1404 |
pjsip_auth_clt_init_req( &sub->dlg->auth_sess, tdata ); |
| 1405 |
|
| 1406 |
|
| 1407 |
if (reason) |
| 1408 |
pj_strdup(sub->dlg->pool, &sub->term_reason, reason); |
| 1409 |
|
| 1410 |
|
| 1411 |
sub->dst_state = state; |
| 1412 |
if (state_str) |
| 1413 |
pj_strdup(sub->pool, &sub->dst_state_str, state_str); |
| 1414 |
else |
| 1415 |
sub->dst_state_str.slen = 0; |
| 1416 |
|
| 1417 |
|
| 1418 |
*p_tdata = tdata; |
| 1419 |
|
| 1420 |
on_return: |
| 1421 |
|
| 1422 |
pjsip_dlg_dec_lock(sub->dlg); |
| 1423 |
return status; |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
PJ_DEF(pj_status_t) pjsip_evsub_current_notify( pjsip_evsub *sub, |
| 1431 |
pjsip_tx_data **p_tdata ) |
| 1432 |
{ |
| 1433 |
return pjsip_evsub_notify( sub, sub->state, &sub->state_str, |
| 1434 |
NULL, p_tdata ); |
| 1435 |
} |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
PJ_DEF(pj_status_t) pjsip_evsub_send_request( pjsip_evsub *sub, |
| 1442 |
pjsip_tx_data *tdata) |
| 1443 |
{ |
| 1444 |
pj_status_t status = PJ_SUCCESS; |
| 1445 |
|
| 1446 |
|
| 1447 |
PJ_ASSERT_RETURN(tdata->msg->type == PJSIP_REQUEST_MSG, |
| 1448 |
PJSIP_ENOTREQUESTMSG); |
| 1449 |
|
| 1450 |
|
| 1451 |
pjsip_dlg_inc_lock(sub->dlg); |
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
if (sub->calling_on_rx_refresh && |
| 1458 |
pjsip_method_cmp(&tdata->msg->line.req.method, |
| 1459 |
&pjsip_notify_method)==0) |
| 1460 |
{ |
| 1461 |
sub->pending_notify = tdata; |
| 1462 |
goto on_return; |
| 1463 |
} |
| 1464 |
|
| 1465 |
|
| 1466 |
status = pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL); |
| 1467 |
if (status != PJ_SUCCESS) |
| 1468 |
goto on_return; |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
if (pjsip_method_cmp(&tdata->msg->line.req.method, |
| 1476 |
&pjsip_notify_method)==0) |
| 1477 |
{ |
| 1478 |
PJ_ASSERT_ON_FAIL( sub->dst_state!=PJSIP_EVSUB_STATE_NULL, |
| 1479 |
{goto on_return;}); |
| 1480 |
|
| 1481 |
set_state(sub, sub->dst_state, |
| 1482 |
(sub->dst_state_str.slen ? &sub->dst_state_str : NULL), |
| 1483 |
NULL, NULL); |
| 1484 |
|
| 1485 |
sub->dst_state = PJSIP_EVSUB_STATE_NULL; |
| 1486 |
sub->dst_state_str.slen = 0; |
| 1487 |
|
| 1488 |
} |
| 1489 |
|
| 1490 |
|
| 1491 |
on_return: |
| 1492 |
pjsip_dlg_dec_lock(sub->dlg); |
| 1493 |
return status; |
| 1494 |
} |
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
static void terminate_timer_cb(pj_timer_heap_t *timer_heap, |
| 1499 |
struct pj_timer_entry *entry) |
| 1500 |
{ |
| 1501 |
pj_str_t *key; |
| 1502 |
pjsip_transaction *tsx; |
| 1503 |
|
| 1504 |
PJ_UNUSED_ARG(timer_heap); |
| 1505 |
|
| 1506 |
|
| 1507 |
entry->id = 0; |
| 1508 |
|
| 1509 |
key = (pj_str_t*)entry->user_data; |
| 1510 |
tsx = pjsip_tsx_layer_find_tsx(key, PJ_FALSE); |
| 1511 |
|
| 1512 |
if (tsx) { |
| 1513 |
pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_UPDATED); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
static pjsip_evsub *on_new_transaction( pjsip_transaction *tsx, |
| 1522 |
pjsip_event *event) |
| 1523 |
{ |
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
pjsip_dialog *dlg; |
| 1530 |
pjsip_event_hdr *event_hdr; |
| 1531 |
pjsip_msg *msg; |
| 1532 |
struct dlgsub *dlgsub_head, *dlgsub; |
| 1533 |
pjsip_evsub *sub; |
| 1534 |
|
| 1535 |
dlg = pjsip_tsx_get_dlg(tsx); |
| 1536 |
if (!dlg) { |
| 1537 |
pj_assert(!"Transaction should have a dialog instance!"); |
| 1538 |
return NULL; |
| 1539 |
} |
| 1540 |
|
| 1541 |
|
| 1542 |
switch (event->body.tsx_state.type) { |
| 1543 |
case PJSIP_EVENT_RX_MSG: |
| 1544 |
msg = event->body.tsx_state.src.rdata->msg_info.msg; |
| 1545 |
break; |
| 1546 |
case PJSIP_EVENT_TX_MSG: |
| 1547 |
msg = event->body.tsx_state.src.tdata->msg; |
| 1548 |
break; |
| 1549 |
default: |
| 1550 |
if (tsx->role == PJSIP_ROLE_UAC) |
| 1551 |
msg = tsx->last_tx->msg; |
| 1552 |
else |
| 1553 |
msg = NULL; |
| 1554 |
break; |
| 1555 |
} |
| 1556 |
|
| 1557 |
if (!msg) { |
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
return NULL; |
| 1564 |
} |
| 1565 |
|
| 1566 |
event_hdr = (pjsip_event_hdr*) |
| 1567 |
pjsip_msg_find_hdr_by_names(msg, &STR_EVENT, |
| 1568 |
&STR_EVENT_S, NULL); |
| 1569 |
if (!event_hdr) { |
| 1570 |
|
| 1571 |
return NULL; |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
dlgsub_head = (struct dlgsub*) dlg->mod_data[mod_evsub.mod.id]; |
| 1579 |
if (dlgsub_head == NULL) { |
| 1580 |
dlgsub_head = PJ_POOL_ALLOC_T(dlg->pool, struct dlgsub); |
| 1581 |
pj_list_init(dlgsub_head); |
| 1582 |
dlg->mod_data[mod_evsub.mod.id] = dlgsub_head; |
| 1583 |
} |
| 1584 |
dlgsub = dlgsub_head->next; |
| 1585 |
|
| 1586 |
while (dlgsub != dlgsub_head) { |
| 1587 |
|
| 1588 |
if (pj_stricmp(&dlgsub->sub->event->event_type, |
| 1589 |
&event_hdr->event_type)==0) |
| 1590 |
{ |
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
if (pj_strcmp(&dlgsub->sub->event->id_param, |
| 1595 |
&event_hdr->id_param)==0) |
| 1596 |
{ |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
if ((dlgsub->sub->option & PJSIP_EVSUB_NO_EVENT_ID) && |
| 1601 |
(pjsip_evsub_get_state(dlgsub->sub)== |
| 1602 |
PJSIP_EVSUB_STATE_TERMINATED)) |
| 1603 |
{ |
| 1604 |
dlgsub = dlgsub->next; |
| 1605 |
continue; |
| 1606 |
} else { |
| 1607 |
break; |
| 1608 |
} |
| 1609 |
|
| 1610 |
} |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
else if (dlgsub->sub->role == PJSIP_ROLE_UAC && |
| 1620 |
(dlgsub->sub->option & PJSIP_EVSUB_NO_EVENT_ID)!=0 && |
| 1621 |
dlgsub->sub->event->id_param.slen==0 && |
| 1622 |
!pjsip_method_cmp(&tsx->method, &pjsip_notify_method)) |
| 1623 |
{ |
| 1624 |
|
| 1625 |
pj_strdup(dlgsub->sub->pool, |
| 1626 |
&dlgsub->sub->event->id_param, |
| 1627 |
&event_hdr->id_param); |
| 1628 |
|
| 1629 |
break; |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
dlgsub = dlgsub->next; |
| 1636 |
} |
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
if (dlgsub == dlgsub_head || |
| 1644 |
(dlgsub->sub && |
| 1645 |
tsx->role == PJSIP_ROLE_UAS && |
| 1646 |
pjsip_evsub_get_state(dlgsub->sub)==PJSIP_EVSUB_STATE_TERMINATED)) |
| 1647 |
{ |
| 1648 |
const char *reason_msg = |
| 1649 |
(dlgsub == dlgsub_head ? "Subscription Does Not Exist" : |
| 1650 |
"Subscription already terminated"); |
| 1651 |
|
| 1652 |
|
| 1653 |
PJ_LOG(4,(THIS_FILE, |
| 1654 |
"%s for %.*s, event=%.*s;id=%.*s", |
| 1655 |
reason_msg, |
| 1656 |
(int)tsx->method.name.slen, |
| 1657 |
tsx->method.name.ptr, |
| 1658 |
(int)event_hdr->event_type.slen, |
| 1659 |
event_hdr->event_type.ptr, |
| 1660 |
(int)event_hdr->id_param.slen, |
| 1661 |
event_hdr->id_param.ptr)); |
| 1662 |
|
| 1663 |
|
| 1664 |
if (tsx->state == PJSIP_TSX_STATE_TRYING && |
| 1665 |
pjsip_method_cmp(&tsx->method, &pjsip_notify_method)==0) |
| 1666 |
{ |
| 1667 |
pj_str_t reason; |
| 1668 |
pjsip_tx_data *tdata; |
| 1669 |
pj_status_t status; |
| 1670 |
|
| 1671 |
pj_cstr(&reason, reason_msg); |
| 1672 |
status = pjsip_dlg_create_response(dlg, |
| 1673 |
event->body.tsx_state.src.rdata, |
| 1674 |
481, &reason, |
| 1675 |
&tdata); |
| 1676 |
if (status == PJ_SUCCESS) { |
| 1677 |
status = pjsip_dlg_send_response(dlg, tsx, tdata); |
| 1678 |
} |
| 1679 |
} |
| 1680 |
return NULL; |
| 1681 |
} |
| 1682 |
|
| 1683 |
|
| 1684 |
sub = dlgsub->sub; |
| 1685 |
|
| 1686 |
|
| 1687 |
tsx->mod_data[mod_evsub.mod.id] = sub; |
| 1688 |
sub->pending_tsx++; |
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
if (tsx->role == PJSIP_ROLE_UAC && |
| 1700 |
tsx->state == PJSIP_TSX_STATE_CALLING && |
| 1701 |
(pjsip_method_cmp(&tsx->method, &sub->method) == 0 || |
| 1702 |
pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method) == 0)) |
| 1703 |
{ |
| 1704 |
|
| 1705 |
if (sub->pending_sub && |
| 1706 |
sub->pending_sub->state < PJSIP_TSX_STATE_COMPLETED) |
| 1707 |
{ |
| 1708 |
pj_timer_entry *timer; |
| 1709 |
pj_str_t *key; |
| 1710 |
pj_time_val timeout = {0, 0}; |
| 1711 |
|
| 1712 |
PJ_LOG(4,(sub->obj_name, |
| 1713 |
"Cancelling pending subscription request")); |
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
timer = PJ_POOL_ZALLOC_T(dlg->pool, pj_timer_entry); |
| 1729 |
key = PJ_POOL_ALLOC_T(dlg->pool, pj_str_t); |
| 1730 |
pj_strdup(dlg->pool, key, &sub->pending_sub->transaction_key); |
| 1731 |
timer->cb = &terminate_timer_cb; |
| 1732 |
timer->user_data = key; |
| 1733 |
timer->id = 1; |
| 1734 |
sub->pending_sub_timer = timer; |
| 1735 |
|
| 1736 |
pjsip_endpt_schedule_timer(dlg->endpt, timer, &timeout); |
| 1737 |
} |
| 1738 |
|
| 1739 |
sub->pending_sub = tsx; |
| 1740 |
|
| 1741 |
} |
| 1742 |
|
| 1743 |
return sub; |
| 1744 |
} |
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
static pj_status_t create_response( pjsip_evsub *sub, |
| 1751 |
pjsip_rx_data *rdata, |
| 1752 |
int st_code, |
| 1753 |
const pj_str_t *st_text, |
| 1754 |
const pjsip_hdr *res_hdr, |
| 1755 |
const pjsip_msg_body *body, |
| 1756 |
pjsip_tx_data **p_tdata) |
| 1757 |
{ |
| 1758 |
pjsip_tx_data *tdata; |
| 1759 |
pjsip_hdr *hdr; |
| 1760 |
pj_status_t status; |
| 1761 |
|
| 1762 |
status = pjsip_dlg_create_response(sub->dlg, rdata, |
| 1763 |
st_code, st_text, &tdata); |
| 1764 |
if (status != PJ_SUCCESS) |
| 1765 |
return status; |
| 1766 |
|
| 1767 |
*p_tdata = tdata; |
| 1768 |
|
| 1769 |
|
| 1770 |
hdr = res_hdr->next; |
| 1771 |
while (hdr != res_hdr) { |
| 1772 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 1773 |
pjsip_hdr_clone(tdata->pool, hdr)); |
| 1774 |
hdr = hdr->next; |
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
if (body) { |
| 1779 |
tdata->msg->body = pjsip_msg_body_clone(tdata->pool, body); |
| 1780 |
if (tdata->msg->body == NULL) { |
| 1781 |
|
| 1782 |
PJ_LOG(4,(THIS_FILE, "Error: unable to clone msg body")); |
| 1783 |
|
| 1784 |
|
| 1785 |
return PJ_SUCCESS; |
| 1786 |
} |
| 1787 |
} |
| 1788 |
|
| 1789 |
return PJ_SUCCESS; |
| 1790 |
} |
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
static void get_hdr_state( pjsip_sub_state_hdr *sub_state, |
| 1796 |
pjsip_evsub_state *state, |
| 1797 |
pj_str_t **state_str ) |
| 1798 |
{ |
| 1799 |
if (pj_stricmp(&sub_state->sub_state, &STR_TERMINATED)==0) { |
| 1800 |
|
| 1801 |
*state = PJSIP_EVSUB_STATE_TERMINATED; |
| 1802 |
*state_str = NULL; |
| 1803 |
|
| 1804 |
} else if (pj_stricmp(&sub_state->sub_state, &STR_ACTIVE)==0) { |
| 1805 |
|
| 1806 |
*state = PJSIP_EVSUB_STATE_ACTIVE; |
| 1807 |
*state_str = NULL; |
| 1808 |
|
| 1809 |
} else if (pj_stricmp(&sub_state->sub_state, &STR_PENDING)==0) { |
| 1810 |
|
| 1811 |
*state = PJSIP_EVSUB_STATE_PENDING; |
| 1812 |
*state_str = NULL; |
| 1813 |
|
| 1814 |
} else { |
| 1815 |
|
| 1816 |
*state = PJSIP_EVSUB_STATE_UNKNOWN; |
| 1817 |
*state_str = &sub_state->sub_state; |
| 1818 |
|
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
static void on_tsx_state_uac( pjsip_evsub *sub, pjsip_transaction *tsx, |
| 1826 |
pjsip_event *event ) |
| 1827 |
{ |
| 1828 |
|
| 1829 |
if (pjsip_method_cmp(&tsx->method, &sub->method)==0 || |
| 1830 |
pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method)==0) |
| 1831 |
{ |
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
if (sub->state == PJSIP_EVSUB_STATE_NULL && |
| 1839 |
tsx->state == PJSIP_TSX_STATE_CALLING) |
| 1840 |
{ |
| 1841 |
set_state(sub, PJSIP_EVSUB_STATE_SENT, NULL, event, NULL); |
| 1842 |
return; |
| 1843 |
} |
| 1844 |
|
| 1845 |
|
| 1846 |
if (tsx->state != PJSIP_TSX_STATE_COMPLETED && |
| 1847 |
tsx->state != PJSIP_TSX_STATE_TERMINATED) |
| 1848 |
{ |
| 1849 |
return; |
| 1850 |
} |
| 1851 |
|
| 1852 |
|
| 1853 |
if (tsx == sub->pending_sub) { |
| 1854 |
sub->pending_sub = NULL; |
| 1855 |
} else if (sub->pending_sub != NULL) { |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
return; |
| 1861 |
} |
| 1862 |
|
| 1863 |
|
| 1864 |
if (tsx->status_code==401 || tsx->status_code==407) { |
| 1865 |
pjsip_tx_data *tdata; |
| 1866 |
pj_status_t status; |
| 1867 |
pjsip_auth_clt_async_on_chal_param chal_param = { 0 }; |
| 1868 |
|
| 1869 |
if (tsx->state == PJSIP_TSX_STATE_TERMINATED) { |
| 1870 |
|
| 1871 |
return; |
| 1872 |
} |
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
{ |
| 1881 |
pjsip_auth_clt_async_impl_token *token; |
| 1882 |
token = PJ_POOL_ZALLOC_T(tsx->pool, |
| 1883 |
pjsip_auth_clt_async_impl_token); |
| 1884 |
token->user_data = sub; |
| 1885 |
token->send_impl = &evsub_async_auth_send_impl; |
| 1886 |
token->abandon_impl = &evsub_async_auth_abandon_impl; |
| 1887 |
token->grp_lock = tsx->grp_lock; |
| 1888 |
pj_grp_lock_add_ref(tsx->grp_lock); |
| 1889 |
|
| 1890 |
pj_bzero(&chal_param, sizeof(chal_param)); |
| 1891 |
chal_param.rdata = event->body.tsx_state.src.rdata; |
| 1892 |
chal_param.tdata = tsx->last_tx; |
| 1893 |
status = pjsip_auth_clt_async_impl_on_challenge( |
| 1894 |
&sub->dlg->auth_sess, |
| 1895 |
token, &chal_param); |
| 1896 |
if (status != PJ_SUCCESS) |
| 1897 |
pj_grp_lock_dec_ref(tsx->grp_lock); |
| 1898 |
} |
| 1899 |
if (status != PJ_SUCCESS) { |
| 1900 |
status = pjsip_auth_clt_reinit_req( |
| 1901 |
&sub->dlg->auth_sess, |
| 1902 |
event->body.tsx_state.src.rdata, |
| 1903 |
tsx->last_tx, &tdata); |
| 1904 |
if (status == PJ_SUCCESS) |
| 1905 |
status = pjsip_dlg_send_request(sub->dlg, tdata, -1, |
| 1906 |
NULL); |
| 1907 |
if (status != PJ_SUCCESS) { |
| 1908 |
|
| 1909 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, |
| 1910 |
NULL, event, &tsx->status_text); |
| 1911 |
} |
| 1912 |
} |
| 1913 |
|
| 1914 |
return; |
| 1915 |
} |
| 1916 |
|
| 1917 |
if (tsx->status_code/100 == 2) { |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
if (tsx->state == PJSIP_TSX_STATE_TERMINATED) { |
| 1927 |
|
| 1928 |
return; |
| 1929 |
} |
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
if (sub->expires->ivalue != 0) { |
| 1935 |
pjsip_msg *msg; |
| 1936 |
pjsip_expires_hdr *expires; |
| 1937 |
|
| 1938 |
msg = event->body.tsx_state.src.rdata->msg_info.msg; |
| 1939 |
expires = (pjsip_expires_hdr*) |
| 1940 |
pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
| 1941 |
if (expires) { |
| 1942 |
sub->expires->ivalue = expires->ivalue; |
| 1943 |
} |
| 1944 |
} |
| 1945 |
|
| 1946 |
|
| 1947 |
update_expires(sub, sub->expires->ivalue); |
| 1948 |
|
| 1949 |
|
| 1950 |
if (sub->expires->ivalue != 0) { |
| 1951 |
unsigned timeout = (sub->expires->ivalue > TIME_UAC_REFRESH) ? |
| 1952 |
sub->expires->ivalue - TIME_UAC_REFRESH : sub->expires->ivalue; |
| 1953 |
|
| 1954 |
|
| 1955 |
if (timeout > 10) |
| 1956 |
timeout += -10 + (pj_rand() % 10); |
| 1957 |
|
| 1958 |
PJ_LOG(5,(sub->obj_name, "Will refresh in %d seconds", |
| 1959 |
timeout)); |
| 1960 |
set_timer(sub, TIMER_TYPE_UAC_REFRESH, timeout); |
| 1961 |
|
| 1962 |
} else { |
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
set_timer(sub, TIMER_TYPE_UAC_TERMINATE, TIME_UAC_TERMINATE); |
| 1967 |
} |
| 1968 |
|
| 1969 |
|
| 1970 |
pj_assert(sub->state != PJSIP_EVSUB_STATE_NULL); |
| 1971 |
if (sub->state == PJSIP_EVSUB_STATE_SENT) { |
| 1972 |
set_state(sub, PJSIP_EVSUB_STATE_ACCEPTED, NULL, event, NULL); |
| 1973 |
} |
| 1974 |
|
| 1975 |
} else { |
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
if (sub->state == PJSIP_EVSUB_STATE_TERMINATED) { |
| 1994 |
|
| 1995 |
return; |
| 1996 |
} |
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
if (tsx->status_code == PJSIP_SC_REQUEST_UPDATED) { |
| 2003 |
return; |
| 2004 |
} |
| 2005 |
|
| 2006 |
|
| 2007 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, |
| 2008 |
NULL, event, &tsx->status_text); |
| 2009 |
|
| 2010 |
} |
| 2011 |
|
| 2012 |
} else if (pjsip_method_cmp(&tsx->method, &pjsip_notify_method) == 0) { |
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
int st_code = 200; |
| 2021 |
pj_str_t *st_text = NULL; |
| 2022 |
pjsip_hdr res_hdr; |
| 2023 |
pjsip_msg_body *body = NULL; |
| 2024 |
|
| 2025 |
pjsip_rx_data *rdata; |
| 2026 |
pjsip_msg *msg; |
| 2027 |
pjsip_sub_state_hdr *sub_state; |
| 2028 |
|
| 2029 |
pjsip_evsub_state new_state; |
| 2030 |
pj_str_t *new_state_str; |
| 2031 |
|
| 2032 |
pjsip_tx_data *tdata; |
| 2033 |
pj_status_t status; |
| 2034 |
|
| 2035 |
|
| 2036 |
if (tsx->state != PJSIP_TSX_STATE_TRYING) |
| 2037 |
return; |
| 2038 |
|
| 2039 |
|
| 2040 |
rdata = event->body.tsx_state.src.rdata; |
| 2041 |
msg = rdata->msg_info.msg; |
| 2042 |
|
| 2043 |
pj_list_init(&res_hdr); |
| 2044 |
|
| 2045 |
|
| 2046 |
sub_state = (pjsip_sub_state_hdr*) |
| 2047 |
pjsip_msg_find_hdr_by_name(msg, &STR_SUB_STATE, NULL); |
| 2048 |
if (sub_state == NULL) { |
| 2049 |
|
| 2050 |
pjsip_warning_hdr *warn_hdr; |
| 2051 |
pj_str_t warn_text = { "Missing Subscription-State header", 33}; |
| 2052 |
|
| 2053 |
|
| 2054 |
st_code = PJSIP_SC_BAD_REQUEST; |
| 2055 |
warn_hdr = pjsip_warning_hdr_create(rdata->tp_info.pool, 399, |
| 2056 |
pjsip_endpt_name(sub->endpt), |
| 2057 |
&warn_text); |
| 2058 |
pj_list_push_back(&res_hdr, warn_hdr); |
| 2059 |
} |
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
|
| 2064 |
if (st_code==200 && sub->user.on_rx_notify && sub->call_cb) { |
| 2065 |
(*sub->user.on_rx_notify)(sub, rdata, &st_code, &st_text, |
| 2066 |
&res_hdr, &body); |
| 2067 |
|
| 2068 |
|
| 2069 |
PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; }); |
| 2070 |
|
| 2071 |
|
| 2072 |
PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; }); |
| 2073 |
} |
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
if (st_code >= 300) { |
| 2080 |
status = create_response(sub, rdata, st_code, st_text, &res_hdr, |
| 2081 |
body, &tdata); |
| 2082 |
if (status == PJ_SUCCESS) { |
| 2083 |
status = pjsip_dlg_send_response(sub->dlg, tsx, tdata); |
| 2084 |
} |
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
if (status == PJ_SUCCESS) { |
| 2090 |
unsigned timeout = TIME_UAC_WAIT_NOTIFY; |
| 2091 |
set_timer(sub, TIMER_TYPE_UAC_WAIT_NOTIFY, timeout); |
| 2092 |
} else { |
| 2093 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 2094 |
pj_str_t reason; |
| 2095 |
|
| 2096 |
reason = pj_strerror(status, errmsg, sizeof(errmsg)); |
| 2097 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL, |
| 2098 |
&reason); |
| 2099 |
} |
| 2100 |
|
| 2101 |
return; |
| 2102 |
} |
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
if (sub->expires->ivalue != 0 && |
| 2109 |
sub_state->expires_param != PJSIP_EXPIRES_NOT_SPECIFIED && |
| 2110 |
(pj_stricmp(&sub_state->sub_state, &STR_ACTIVE)==0 || |
| 2111 |
pj_stricmp(&sub_state->sub_state, &STR_PENDING)==0)) |
| 2112 |
{ |
| 2113 |
unsigned next_refresh = sub_state->expires_param; |
| 2114 |
unsigned timeout; |
| 2115 |
|
| 2116 |
update_expires(sub, next_refresh); |
| 2117 |
|
| 2118 |
|
| 2119 |
timeout = (next_refresh > TIME_UAC_REFRESH) ? |
| 2120 |
next_refresh - TIME_UAC_REFRESH : next_refresh; |
| 2121 |
|
| 2122 |
PJ_LOG(5,(sub->obj_name, "Will refresh in %d seconds", timeout)); |
| 2123 |
set_timer(sub, TIMER_TYPE_UAC_REFRESH, timeout); |
| 2124 |
} |
| 2125 |
|
| 2126 |
|
| 2127 |
get_hdr_state(sub_state, &new_state, &new_state_str); |
| 2128 |
|
| 2129 |
|
| 2130 |
status = create_response(sub, rdata, st_code, st_text, &res_hdr, |
| 2131 |
body, &tdata); |
| 2132 |
if (status == PJ_SUCCESS) |
| 2133 |
status = pjsip_dlg_send_response(sub->dlg, tsx, tdata); |
| 2134 |
|
| 2135 |
|
| 2136 |
if (status == PJ_SUCCESS) { |
| 2137 |
set_state(sub, new_state, new_state_str, event, |
| 2138 |
&sub_state->reason_param); |
| 2139 |
} else { |
| 2140 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 2141 |
pj_str_t reason; |
| 2142 |
|
| 2143 |
reason = pj_strerror(status, errmsg, sizeof(errmsg)); |
| 2144 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event, |
| 2145 |
&reason); |
| 2146 |
} |
| 2147 |
|
| 2148 |
|
| 2149 |
} else { |
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
PJ_LOG(4,(sub->obj_name, "Unexpected transaction method %.*s", |
| 2155 |
(int)tsx->method.name.slen, tsx->method.name.ptr)); |
| 2156 |
} |
| 2157 |
} |
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
static void on_tsx_state_uas( pjsip_evsub *sub, pjsip_transaction *tsx, |
| 2164 |
pjsip_event *event) |
| 2165 |
{ |
| 2166 |
|
| 2167 |
if (pjsip_method_cmp(&tsx->method, &sub->method) == 0 || |
| 2168 |
pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method) == 0) |
| 2169 |
{ |
| 2170 |
|
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
pjsip_rx_data *rdata; |
| 2176 |
pjsip_event_hdr *event_hdr; |
| 2177 |
pjsip_expires_hdr *expires; |
| 2178 |
pjsip_msg *msg; |
| 2179 |
pjsip_tx_data *tdata; |
| 2180 |
int st_code = 200; |
| 2181 |
pj_str_t *st_text = NULL; |
| 2182 |
pjsip_hdr res_hdr; |
| 2183 |
pjsip_msg_body *body = NULL; |
| 2184 |
pjsip_evsub_state old_state; |
| 2185 |
pj_str_t old_state_str; |
| 2186 |
pj_str_t reason = { NULL, 0 }; |
| 2187 |
pj_status_t status; |
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
if (tsx->state != PJSIP_TSX_STATE_TRYING) |
| 2194 |
return; |
| 2195 |
|
| 2196 |
rdata = event->body.tsx_state.src.rdata; |
| 2197 |
msg = rdata->msg_info.msg; |
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
|
| 2202 |
event_hdr = (pjsip_event_hdr*) |
| 2203 |
pjsip_msg_find_hdr_by_names(msg, &STR_EVENT, |
| 2204 |
&STR_EVENT, NULL); |
| 2205 |
expires = (pjsip_expires_hdr*) |
| 2206 |
pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
| 2207 |
if (event_hdr && expires) { |
| 2208 |
struct evpkg *evpkg; |
| 2209 |
|
| 2210 |
evpkg = find_pkg(&event_hdr->event_type); |
| 2211 |
if (evpkg) { |
| 2212 |
if (expires->ivalue < evpkg->pkg_expires) |
| 2213 |
sub->expires->ivalue = expires->ivalue; |
| 2214 |
else |
| 2215 |
sub->expires->ivalue = evpkg->pkg_expires; |
| 2216 |
} |
| 2217 |
} |
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
update_expires(sub, sub->expires->ivalue); |
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
old_state = sub->state; |
| 2231 |
old_state_str = sub->state_str; |
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
sub->calling_on_rx_refresh = PJ_TRUE; |
| 2237 |
|
| 2238 |
if (sub->expires->ivalue == 0) { |
| 2239 |
pj_str_t timeout = { "timeout", 7}; |
| 2240 |
|
| 2241 |
PJ_LOG(4,(sub->obj_name, "Receiving unsubscription request " |
| 2242 |
"(Expires=0).")); |
| 2243 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event, |
| 2244 |
&timeout); |
| 2245 |
} else if (sub->state == PJSIP_EVSUB_STATE_NULL) { |
| 2246 |
sub->state = PJSIP_EVSUB_STATE_ACCEPTED; |
| 2247 |
sub->state_str = evsub_state_names[sub->state]; |
| 2248 |
} |
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
pj_list_init(&res_hdr); |
| 2254 |
|
| 2255 |
if (sub->user.on_rx_refresh && sub->call_cb) { |
| 2256 |
(*sub->user.on_rx_refresh)(sub, rdata, &st_code, &st_text, |
| 2257 |
&res_hdr, &body); |
| 2258 |
|
| 2259 |
if (sub->expires->ivalue == 0) st_code = 200; |
| 2260 |
} |
| 2261 |
sub->calling_on_rx_refresh = PJ_FALSE; |
| 2262 |
|
| 2263 |
if (sub->deferred_state_notify) { |
| 2264 |
sub->deferred_state_notify = PJ_FALSE; |
| 2265 |
|
| 2266 |
if (sub->user.on_evsub_state && sub->call_cb) |
| 2267 |
(*sub->user.on_evsub_state)(sub, event); |
| 2268 |
|
| 2269 |
if (sub->state == PJSIP_EVSUB_STATE_TERMINATED && |
| 2270 |
sub->pending_tsx == 0) |
| 2271 |
{ |
| 2272 |
evsub_destroy(sub); |
| 2273 |
} |
| 2274 |
} |
| 2275 |
|
| 2276 |
|
| 2277 |
PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; }); |
| 2278 |
|
| 2279 |
|
| 2280 |
PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; }); |
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
status = create_response(sub, rdata, st_code, st_text, &res_hdr, |
| 2285 |
body, &tdata); |
| 2286 |
if (status == PJ_SUCCESS) { |
| 2287 |
|
| 2288 |
pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*) |
| 2289 |
pjsip_hdr_shallow_clone(tdata->pool, |
| 2290 |
sub->expires)); |
| 2291 |
|
| 2292 |
|
| 2293 |
pjsip_dlg_send_response(sub->dlg, tsx, tdata); |
| 2294 |
} |
| 2295 |
|
| 2296 |
|
| 2297 |
if (st_code/100==2) { |
| 2298 |
|
| 2299 |
if (sub->state == PJSIP_EVSUB_STATE_NULL) { |
| 2300 |
set_state(sub, sub->state, NULL, event, &reason); |
| 2301 |
} |
| 2302 |
|
| 2303 |
|
| 2304 |
if (sub->state != PJSIP_EVSUB_STATE_TERMINATED) { |
| 2305 |
PJ_LOG(5,(sub->obj_name, "UAS timeout in %d seconds", |
| 2306 |
sub->expires->ivalue)); |
| 2307 |
set_timer(sub, TIMER_TYPE_UAS_TIMEOUT, |
| 2308 |
sub->expires->ivalue); |
| 2309 |
} |
| 2310 |
|
| 2311 |
} else { |
| 2312 |
sub->state = old_state; |
| 2313 |
sub->state_str = old_state_str; |
| 2314 |
} |
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
if (sub->pending_notify) { |
| 2322 |
status = pjsip_evsub_send_request(sub, sub->pending_notify); |
| 2323 |
sub->pending_notify = NULL; |
| 2324 |
} |
| 2325 |
|
| 2326 |
} else if (pjsip_method_cmp(&tsx->method, &pjsip_notify_method)==0) { |
| 2327 |
|
| 2328 |
|
| 2329 |
if (tsx->state == PJSIP_TSX_STATE_COMPLETED && |
| 2330 |
(tsx->status_code==401 || tsx->status_code==407)) |
| 2331 |
{ |
| 2332 |
pjsip_tx_data *tdata; |
| 2333 |
pj_status_t status; |
| 2334 |
pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; |
| 2335 |
pjsip_auth_clt_async_on_chal_param chal_param = { 0 }; |
| 2336 |
|
| 2337 |
|
| 2338 |
if (tsx->last_tx->auth_retry) |
| 2339 |
return; |
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
{ |
| 2345 |
pjsip_auth_clt_async_impl_token *token; |
| 2346 |
token = PJ_POOL_ZALLOC_T(tsx->pool, |
| 2347 |
pjsip_auth_clt_async_impl_token); |
| 2348 |
token->user_data = sub; |
| 2349 |
token->send_impl = &evsub_async_auth_send_impl; |
| 2350 |
token->abandon_impl = &evsub_async_auth_abandon_impl; |
| 2351 |
token->grp_lock = tsx->grp_lock; |
| 2352 |
pj_grp_lock_add_ref(tsx->grp_lock); |
| 2353 |
|
| 2354 |
pj_bzero(&chal_param, sizeof(chal_param)); |
| 2355 |
chal_param.rdata = rdata; |
| 2356 |
chal_param.tdata = tsx->last_tx; |
| 2357 |
status = pjsip_auth_clt_async_impl_on_challenge( |
| 2358 |
&sub->dlg->auth_sess, |
| 2359 |
token, &chal_param); |
| 2360 |
if (status != PJ_SUCCESS) |
| 2361 |
pj_grp_lock_dec_ref(tsx->grp_lock); |
| 2362 |
} |
| 2363 |
if (status != PJ_SUCCESS) { |
| 2364 |
status = pjsip_auth_clt_reinit_req(&sub->dlg->auth_sess, |
| 2365 |
rdata, tsx->last_tx, |
| 2366 |
&tdata); |
| 2367 |
if (status == PJ_SUCCESS) |
| 2368 |
status = pjsip_dlg_send_request(sub->dlg, tdata, -1, |
| 2369 |
NULL); |
| 2370 |
if (status != PJ_SUCCESS) { |
| 2371 |
|
| 2372 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL, |
| 2373 |
&tsx->status_text); |
| 2374 |
} |
| 2375 |
} |
| 2376 |
return; |
| 2377 |
|
| 2378 |
} |
| 2379 |
|
| 2380 |
if (sub->state == PJSIP_EVSUB_STATE_TERMINATED) |
| 2381 |
return; |
| 2382 |
|
| 2383 |
|
| 2384 |
if (tsx->status_code/100 != 2) { |
| 2385 |
pj_bool_t should_terminate_sub = PJ_FALSE; |
| 2386 |
|
| 2387 |
if (event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 2388 |
if (tsx->status_code == 481) { |
| 2389 |
should_terminate_sub = PJ_TRUE; |
| 2390 |
} else { |
| 2391 |
pjsip_retry_after_hdr *retry_after; |
| 2392 |
pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; |
| 2393 |
pjsip_msg *msg = rdata->msg_info.msg; |
| 2394 |
|
| 2395 |
retry_after = (pjsip_retry_after_hdr*) |
| 2396 |
pjsip_msg_find_hdr_by_name(msg, &STR_RETRY_AFTER, NULL); |
| 2397 |
|
| 2398 |
if (!retry_after) { |
| 2399 |
should_terminate_sub = PJ_TRUE; |
| 2400 |
} |
| 2401 |
} |
| 2402 |
} else if (event->body.tsx_state.type == PJSIP_EVENT_TIMER) { |
| 2403 |
if (tsx->status_code == 408) { |
| 2404 |
should_terminate_sub = PJ_TRUE; |
| 2405 |
} |
| 2406 |
} |
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 |
if (should_terminate_sub) { |
| 2413 |
set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event, |
| 2414 |
&tsx->status_text); |
| 2415 |
return; |
| 2416 |
} |
| 2417 |
} |
| 2418 |
} else { |
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
PJ_LOG(4,(sub->obj_name, "Unexpected transaction method %.*s", |
| 2424 |
(int)tsx->method.name.slen, tsx->method.name.ptr)); |
| 2425 |
|
| 2426 |
} |
| 2427 |
} |
| 2428 |
|
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
static void mod_evsub_on_tsx_state(pjsip_transaction *tsx, pjsip_event *event) |
| 2434 |
{ |
| 2435 |
pjsip_evsub *sub = pjsip_tsx_get_evsub(tsx); |
| 2436 |
|
| 2437 |
if (sub == NULL) { |
| 2438 |
sub = on_new_transaction(tsx, event); |
| 2439 |
if (sub == NULL) |
| 2440 |
return; |
| 2441 |
} |
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
if (sub->user.on_tsx_state && sub->call_cb) |
| 2446 |
(*sub->user.on_tsx_state)(sub, tsx, event); |
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
if (sub->role == PJSIP_ROLE_UAC) { |
| 2452 |
on_tsx_state_uac(sub, tsx, event); |
| 2453 |
} else { |
| 2454 |
on_tsx_state_uas(sub, tsx, event); |
| 2455 |
} |
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
if (tsx->state == PJSIP_TSX_STATE_TERMINATED) { |
| 2460 |
|
| 2461 |
--sub->pending_tsx; |
| 2462 |
|
| 2463 |
if (sub->state == PJSIP_EVSUB_STATE_TERMINATED && |
| 2464 |
sub->pending_tsx == 0) |
| 2465 |
{ |
| 2466 |
evsub_destroy(sub); |
| 2467 |
} |
| 2468 |
|
| 2469 |
} |
| 2470 |
} |
| 2471 |
|
| 2472 |
|