Debian Codegraph @

Xpjproject/pjsip/src/pjsip-simple/evsub.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 * Global constant
42 */
43
44 /* Let's define this enum, so that it'll trigger compilation error
45 * when somebody define the same enum in sip_msg.h
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 * SUBSCRIBE method constant.
67 */
68 PJ_DEF(const pjsip_method*) pjsip_get_subscribe_method()
69 {
70 return &pjsip_subscribe_method;
71 }
72
73 /**
74 * NOTIFY method constant.
75 */
76 PJ_DEF(const pjsip_method*) pjsip_get_notify_method()
77 {
78 return &pjsip_notify_method;
79 }
80
81
82 /*
83 * Static prototypes.
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 * State names.
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 * Timer constants.
115 */
116
117 /* Number of seconds to send SUBSCRIBE before the actual expiration */
118 #define TIME_UAC_REFRESH PJSIP_EVSUB_TIME_UAC_REFRESH
119
120 /* Time to wait for the final NOTIFY after sending unsubscription */
121 #define TIME_UAC_TERMINATE PJSIP_EVSUB_TIME_UAC_TERMINATE
122
123 /* If client responds NOTIFY with non-2xx final response (such as 401),
124 * wait for this seconds for further NOTIFY, otherwise client will
125 * unsubscribe
126 */
127 #define TIME_UAC_WAIT_NOTIFY PJSIP_EVSUB_TIME_UAC_WAIT_NOTIFY
128
129
130 /*
131 * Timer id
132 */
133 enum timer_id
134 {
135 /* No timer. */
136 TIMER_TYPE_NONE,
137
138 /* Time to refresh client subscription.
139 * The action is to call on_client_refresh() callback.
140 */
141 TIMER_TYPE_UAC_REFRESH,
142
143 /* UAS timeout after to subscription refresh.
144 * The action is to call on_server_timeout() callback.
145 */
146 TIMER_TYPE_UAS_TIMEOUT,
147
148 /* UAC waiting for final NOTIFY after unsubscribing
149 * The action is to terminate.
150 */
151 TIMER_TYPE_UAC_TERMINATE,
152
153 /* UAC waiting for further NOTIFY after sending non-2xx response to
154 * NOTIFY. The action is to unsubscribe.
155 */
156 TIMER_TYPE_UAC_WAIT_NOTIFY,
157
158 /* Max nb of timer types. */
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 * Definition of event package.
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 * Event subscription module (mod-evsub).
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, /* prev, next. */
201 { "mod-evsub", 9 }, /* Name. */
202 -1, /* Id */
203 PJSIP_MOD_PRIORITY_DIALOG_USAGE, /* Priority */
204 NULL, /* load() */
205 NULL, /* start() */
206 NULL, /* stop() */
207 &mod_evsub_unload, /* unload() */
208 NULL, /* on_rx_request() */
209 NULL, /* on_rx_response() */
210 NULL, /* on_tx_request. */
211 NULL, /* on_tx_response() */
212 &mod_evsub_on_tsx_state, /* on_tsx_state() */
213 }
214 };
215
216
217 /*
218 * Event subscription session.
219 */
220 struct pjsip_evsub
221 {
222 char obj_name[PJ_MAX_OBJ_NAME]; /**< Name. */
223 pj_pool_t *pool; /**< Pool. */
224 pjsip_endpoint *endpt; /**< Endpoint instance. */
225 pjsip_dialog *dlg; /**< Underlying dialog. */
226 struct evpkg *pkg; /**< The event package. */
227 unsigned option; /**< Options. */
228 pjsip_evsub_user user; /**< Callback. */
229 pj_bool_t call_cb; /**< Notify callback? */
230 pjsip_role_e role; /**< UAC=subscriber, UAS=notifier */
231 pjsip_evsub_state state; /**< Subscription state. */
232 pj_str_t state_str; /**< String describing the state. */
233 pjsip_evsub_state dst_state; /**< Pending state to be set. */
234 pj_str_t dst_state_str;/**< Pending state to be set. */
235 pj_str_t term_reason; /**< Termination reason. */
236 pjsip_method method; /**< Method that established subscr.*/
237 pjsip_event_hdr *event; /**< Event description. */
238 pjsip_expires_hdr *expires; /**< Expires header */
239 pjsip_accept_hdr *accept; /**< Local Accept header. */
240 pjsip_hdr sub_hdr_list; /**< User-defined header. */
241
242 pj_time_val refresh_time; /**< Time to refresh. */
243 pj_timer_entry timer; /**< Internal timer. */
244 int pending_tsx; /**< Number of pending transactions.*/
245 pjsip_transaction *pending_sub; /**< Pending UAC SUBSCRIBE tsx. */
246 pj_timer_entry *pending_sub_timer; /**< Stop pending sub timer. */
247 pjsip_tx_data *pending_notify;/**< Pending NOTIFY to be sent. */
248 pj_bool_t calling_on_rx_refresh;/**< Inside on_rx_refresh()?*/
249 pj_bool_t deferred_state_notify;/**< Deferred TERMINATED notify */
250 pj_grp_lock_t *grp_lock; /* Session group lock */
251
252 void *mod_data[PJSIP_MAX_MODULE]; /**< Module data. */
253 };
254
255
256 /*
257 * This is the structure that will be "attached" to dialog.
258 * The purpose is to allow multiple subscriptions inside a dialog.
259 */
260 struct dlgsub
261 {
262 PJ_DECL_LIST_MEMBER(struct dlgsub);
263 pjsip_evsub *sub;
264 };
265
266
267 /* Static vars. */
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 * On unload module.
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 /* Proto for pjsipsimple_strerror().
290 * Defined in errno.c
291 */
292 PJ_DECL(pj_str_t) pjsipsimple_strerror( pj_status_t statcode,
293 char *buf, pj_size_t bufsize );
294
295 /*
296 * Init and register module.
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 /* Keep endpoint for future reference: */
315 mod_evsub.endpt = endpt;
316
317 /* Init event package list: */
318 pj_list_init(&mod_evsub.pkg_list);
319
320 /* Create pool: */
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 /* Register module: */
328 status = pjsip_endpt_register_module(endpt, &mod_evsub.mod);
329 if (status != PJ_SUCCESS)
330 goto on_error;
331
332 /* Create Allow-Events header: */
333 mod_evsub.allow_events_hdr = pjsip_allow_events_hdr_create(mod_evsub.pool);
334
335 /* Register SIP-event specific headers parser: */
336 status = pjsip_evsub_init_parser();
337 if (status != PJ_SUCCESS)
338 goto on_error;
339
340 /* Register new methods SUBSCRIBE and NOTIFY in Allow-ed header */
341 pjsip_endpt_add_capability(endpt, &mod_evsub.mod, PJSIP_H_ALLOW, NULL,
342 2, method_tags);
343
344 /* Done. */
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 * Get the instance of the module.
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 * Get the event subscription instance in the transaction.
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 * Set event subscription's module data.
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 * Get event subscription's module data.
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 * Find registered event package with matching name.
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 * Register an event package
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 /* Make sure accept_cnt < PJ_ARRAY_SIZE(pkg->pkg_accept->values) */
434 PJ_ASSERT_RETURN(accept_cnt <= PJSIP_GENERIC_ARRAY_MAX_COUNT,
435 PJ_ETOOMANY);
436
437 /* Make sure evsub module has been initialized */
438 PJ_ASSERT_RETURN(mod_evsub.mod.id != -1, PJ_EINVALIDOP);
439
440 /* Make sure no module with the specified name already registered: */
441 if (find_pkg(event_name) != NULL)
442 return PJSIP_SIMPLE_EPKGEXISTS;
443
444 /* Create new event package: */
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 /* Add to package list: */
458
459 pj_list_push_back(&mod_evsub.pkg_list, pkg);
460
461 /* Add to Allow-Events header: */
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 /* Add to endpoint's Accept header */
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 /* Done */
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 * Retrieve Allow-Events header
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 * Update expiration time.
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 * Schedule timer.
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 * Set event subscription UAS timout.
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 * Destructor.
560 */
561 static void evsub_on_destroy(void *obj)
562 {
563 pjsip_evsub *sub = (pjsip_evsub*)obj;
564
565 /* Decrement dialog's session */
566 pjsip_dlg_dec_session(sub->dlg, &mod_evsub.mod);
567 }
568
569
570 /*
571 * Destroy session.
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 /* Kill timer */
580 set_timer(sub, TIMER_TYPE_NONE, 0);
581
582 /* Kill timer for stopping pending sub (see ticket #1807) */
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 /* Remove this session from dialog's list of subscription */
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 /* Async auth send callback: resend the authenticated request via dialog.
606 * user_data points to pjsip_evsub*. No lock is held by the caller.
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 /* Async auth abandon callback: terminate the subscription.
619 * user_data points to pjsip_evsub*. No lock is held by the caller.
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 * Set subscription session state.
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 /* don't call the callback with NULL event, it may crash the app! */
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 /* Kill any timer. */
678 set_timer(sub, TIMER_TYPE_NONE, 0);
679
680 /* We must not destroy evsub if we're still calling the callback. */
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 * Timer callback.
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 /* If this timer entry has just been rescheduled or cancelled
706 * while waiting for dialog mutex, just return (see #1885 scenario 1).
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 /* Time for UAC to refresh subscription */
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 /* Refresh from UAC has not been received */
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 * Create subscription session, used for both client and notifier.
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 /* Make sure there's package register for the event name: */
813
814 pkg = find_pkg(event);
815 if (pkg == NULL)
816 return PJSIP_SIMPLE_ENOPKG;
817
818
819 /* Must lock dialog before using pool etc. */
820 pjsip_dlg_inc_lock(dlg);
821
822 /* Init attributes: */
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 /* Set name. */
843 pj_ansi_snprintf(sub->obj_name, PJ_ARRAY_SIZE(sub->obj_name),
844 "evsub%p", sub);
845
846 /* Copy callback, if any: */
847 if (user_cb)
848 pj_memcpy(&sub->user, user_cb, sizeof(pjsip_evsub_user));
849
850
851 /* Create Event header: */
852 sub->event = pjsip_event_hdr_create(sub->pool);
853 pj_strdup(sub->pool, &sub->event->event_type, event);
854
855
856 /* Check if another subscription has been registered to the dialog. In
857 * that case, just add ourselves to the subscription list, otherwise
858 * create and register a new subscription list.
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 /* Register as dialog usage: */
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 * Increment the event subscription's group lock.
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 * Decrement the event subscription's group lock.
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 * Create client subscription session.
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 /* Add unique Id to Event header, only when PJSIP_EVSUB_NO_EVENT_ID
929 * is not specified.
930 */
931 if ((option & PJSIP_EVSUB_NO_EVENT_ID) == 0) {
932 pj_create_unique_string(sub->pool, &sub->event->id_param);
933 }
934
935 /* Increment dlg session. */
936 pjsip_dlg_inc_session(sub->dlg, &mod_evsub.mod);
937
938 /* Init group lock */
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 /* Done */
949 *p_evsub = sub;
950
951 on_return:
952 pjsip_dlg_dec_lock(dlg);
953 return status;
954 }
955
956
957 /*
958 * Create server subscription session from incoming request.
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 /* Check arguments: */
974 PJ_ASSERT_RETURN(dlg && rdata && p_evsub, PJ_EINVAL);
975
976 /* MUST be request message: */
977 PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG,
978 PJSIP_ENOTREQUESTMSG);
979
980 /* Transaction MUST have been created (in the dialog) */
981 tsx = pjsip_rdata_get_tsx(rdata);
982 PJ_ASSERT_RETURN(tsx != NULL, PJSIP_ENOTSX);
983
984 /* No subscription must have been attached to transaction */
985 PJ_ASSERT_RETURN(tsx->mod_data[mod_evsub.mod.id] == NULL,
986 PJSIP_ETYPEEXISTS);
987
988 /* Package MUST implement on_rx_refresh */
989 PJ_ASSERT_RETURN(user_cb->on_rx_refresh, PJ_EINVALIDOP);
990
991 /* Request MUST have "Event" header. We need the Event header to get
992 * the package name (don't want to add more arguments in the function).
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 /* Start locking the mutex: */
1002
1003 pjsip_dlg_inc_lock(dlg);
1004
1005 /* Create the session: */
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 /* Just duplicate Event header from the request */
1013 sub->event = (pjsip_event_hdr*) pjsip_hdr_clone(sub->pool, event_hdr);
1014
1015 /* Set the method: */
1016 pjsip_method_copy(sub->pool, &sub->method,
1017 &rdata->msg_info.msg->line.req.method);
1018
1019 /* Set expiration time based on client request (in Expires header),
1020 * or package default expiration time.
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 /* Update time. */
1037 update_expires(sub, sub->expires->ivalue);
1038
1039 /* Update Accept header: */
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 /* Increment dlg session. */
1047 pjsip_dlg_inc_session(dlg, &mod_evsub.mod);
1048
1049 /* Init group lock */
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 /* We can start the session: */
1060
1061 sub->pending_tsx++;
1062 tsx->mod_data[mod_evsub.mod.id] = sub;
1063
1064 /* Done. */
1065 *p_evsub = sub;
1066
1067
1068 on_return:
1069 pjsip_dlg_dec_lock(dlg);
1070 return status;
1071 }
1072
1073
1074 /*
1075 * Forcefully destroy subscription.
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 /* I think it's pretty safe to disable this check.
1085
1086 if (sub->pending_tsx) {
1087 pj_assert(!"Unable to terminate when there's pending tsx");
1088 pjsip_dlg_dec_lock(sub->dlg);
1089 return PJ_EINVALIDOP;
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 * Get subscription state.
1102 */
1103 PJ_DEF(pjsip_evsub_state) pjsip_evsub_get_state(const pjsip_evsub *sub)
1104 {
1105 return sub->state;
1106 }
1107
1108 /*
1109 * Get state name.
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 * Get termination reason.
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 * Get subscription expiration time.
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 * Initiate client subscription
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 /* Use SUBSCRIBE if method is not specified */
1147 if (method == NULL)
1148 method = &pjsip_subscribe_method;
1149
1150 pjsip_dlg_inc_lock(sub->dlg);
1151
1152 /* Update method: */
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 /* Add Event header: */
1162 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
1163 pjsip_hdr_shallow_clone(tdata->pool, sub->event));
1164
1165 /* Update and add expires header: */
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 /* Add Supported header (it's optional in RFC 3265, but some event package
1172 * RFC may bring this requirement to SHOULD strength - e.g. RFC 5373)
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 /* Add Accept header: */
1185 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
1186 pjsip_hdr_shallow_clone(tdata->pool, sub->accept));
1187
1188
1189 /* Add Allow-Events header: */
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 /* Add custom headers */
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 * Add custom headers.
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 * Accept incoming subscription request.
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 /* Check arguments */
1250 PJ_ASSERT_RETURN(sub && rdata, PJ_EINVAL);
1251
1252 /* Can only be for server subscription: */
1253 PJ_ASSERT_RETURN(sub->role == PJSIP_ROLE_UAS, PJ_EINVALIDOP);
1254
1255 /* Only expect 2xx status code (for now) */
1256 PJ_ASSERT_RETURN(st_code/100 == 2, PJ_EINVALIDOP);
1257
1258 /* Subscription MUST have been attached to the transaction.
1259 * Initial subscription request will be attached on evsub_create_uas(),
1260 * while subsequent requests will be attached in tsx_state()
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 /* Lock dialog */
1267 pjsip_dlg_inc_lock(sub->dlg);
1268
1269 /* Create response: */
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 /* Add expires header: */
1277 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
1278 pjsip_hdr_shallow_clone(tdata->pool, sub->expires));
1279
1280 /* Add additional header, if any. */
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 /* Send the response: */
1291 status = pjsip_dlg_send_response( sub->dlg, tsx, tdata );
1292 if (status != PJ_SUCCESS)
1293 goto on_return;
1294
1295 /* Set UAS timeout timer, when status code is 2xx and state is not
1296 * terminated.
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 * Create Subscription-State header based on current server subscription
1313 * state.
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 /* Get the remaining time before refresh is required */
1325 pj_gettimeofday(&now);
1326 delay = sub->refresh_time;
1327 PJ_TIME_VAL_SUB(delay, now);
1328
1329 /* Create the Subscription-State header */
1330 sub_state = pjsip_sub_state_hdr_create(pool);
1331
1332 /* Fill up the header */
1333 switch (state) {
1334 case PJSIP_EVSUB_STATE_NULL:
1335 case PJSIP_EVSUB_STATE_SENT:
1336 pj_assert(!"Invalid state!");
1337 /* Treat as pending */
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 * Create and send NOTIFY request.
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 /* Check arguments. */
1379 PJ_ASSERT_RETURN(sub!=NULL && p_tdata!=NULL, PJ_EINVAL);
1380
1381 /* Lock dialog. */
1382 pjsip_dlg_inc_lock(sub->dlg);
1383
1384 /* Create NOTIFY request */
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 /* Add Event header */
1391 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
1392 pjsip_hdr_shallow_clone(tdata->pool, sub->event));
1393
1394 /* Add Subscription-State header */
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 /* Add Allow-Events header */
1400 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
1401 pjsip_hdr_shallow_clone(tdata->pool, mod_evsub.allow_events_hdr));
1402
1403 /* Add Authentication headers. */
1404 pjsip_auth_clt_init_req( &sub->dlg->auth_sess, tdata );
1405
1406 /* Update reason */
1407 if (reason)
1408 pj_strdup(sub->dlg->pool, &sub->term_reason, reason);
1409
1410 /* Save destination state. */
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 /* Unlock dialog */
1422 pjsip_dlg_dec_lock(sub->dlg);
1423 return status;
1424 }
1425
1426
1427 /*
1428 * Create NOTIFY to reflect current status.
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 * Send request.
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 /* Must be request message. */
1447 PJ_ASSERT_RETURN(tdata->msg->type == PJSIP_REQUEST_MSG,
1448 PJSIP_ENOTREQUESTMSG);
1449
1450 /* Lock */
1451 pjsip_dlg_inc_lock(sub->dlg);
1452
1453
1454 /* Delay sending NOTIFY if we're inside on_rx_refresh() callback
1455 * until we have sent the response to the incoming SUBSCRIBE.
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 /* Send the request. */
1466 status = pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL);
1467 if (status != PJ_SUCCESS)
1468 goto on_return;
1469
1470
1471 /* Special case for NOTIFY:
1472 * The new state was set in pjsip_evsub_notify(), but we apply the
1473 * new state now, when the request was actually sent.
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 /* Callback to be called to terminate transaction. */
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 /* Clear timer ID */
1507 entry->id = 0;
1508
1509 key = (pj_str_t*)entry->user_data;
1510 tsx = pjsip_tsx_layer_find_tsx(key, PJ_FALSE);
1511 /* Chance of race condition here */
1512 if (tsx) {
1513 pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_UPDATED);
1514 }
1515 }
1516
1517
1518 /*
1519 * Attach subscription session to newly created transaction, if appropriate.
1520 */
1521 static pjsip_evsub *on_new_transaction( pjsip_transaction *tsx,
1522 pjsip_event *event)
1523 {
1524 /*
1525 * Newly created transaction will not have subscription session
1526 * attached to it. Find the subscription session from the dialog,
1527 * by matching the Event header.
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 //Note:
1559 // this transaction can be other transaction in the dialog.
1560 // The assertion below probably only valid for dialog that
1561 // only has one event subscription usage.
1562 //pj_assert(!"First transaction event is not TX or RX!");
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 /* Not subscription related message */
1571 return NULL;
1572 }
1573
1574 /* Find the subscription in the dialog, based on the content
1575 * of Event header:
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 /* Event type matched.
1592 * Check if event ID matched too.
1593 */
1594 if (pj_strcmp(&dlgsub->sub->event->id_param,
1595 &event_hdr->id_param)==0)
1596 {
1597 /* Skip this subscription if it has no event ID and has been
1598 * terminated (see ticket #1647).
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 * Otherwise if it is an UAC subscription, AND
1613 * PJSIP_EVSUB_NO_EVENT_ID flag is set, AND
1614 * the session's event id is NULL, AND
1615 * the incoming request is NOTIFY with event ID, then
1616 * we consider it as a match, and update the
1617 * session's event id.
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 /* Update session's event id. */
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 /* Note:
1639 * the second condition is for https://github.com/pjsip/pjproject/issues/911
1640 * Take note that it could be us that is trying to send a final message,
1641 * such as final NOTIFY upon unsubscription.
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 /* This could be incoming request to create new subscription */
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 /* If this is an incoming NOTIFY, reject with 481 */
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 /* Found! */
1684 sub = dlgsub->sub;
1685
1686 /* Attach session to the transaction */
1687 tsx->mod_data[mod_evsub.mod.id] = sub;
1688 sub->pending_tsx++;
1689
1690 /* Special case for outgoing/UAC SUBSCRIBE/REFER transaction.
1691 * We can only have one pending UAC SUBSCRIBE/REFER, so if another
1692 * transaction is started while previous one still alive, terminate
1693 * the older one.
1694 *
1695 * Sample scenario:
1696 * - subscribe sent to destination that doesn't exist, transaction
1697 * is still retransmitting request, then unsubscribe is sent.
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 /* By convention, we use 490 (Request Updated) status code.
1716 * When transaction handler (below) see this status code, it
1717 * will ignore the transaction.
1718 */
1719 /* This unfortunately may cause deadlock, because at the moment
1720 * we are holding dialog's mutex. If a response to this
1721 * transaction is in progress in another thread, that thread
1722 * will deadlock when trying to acquire dialog mutex, because
1723 * it is holding the transaction mutex.
1724 *
1725 * So the solution is to register timer to kill this transaction.
1726 */
1727 //pjsip_tsx_terminate(sub->pending_sub, PJSIP_SC_REQUEST_UPDATED);
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 * Create response, adding custome headers and msg body.
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 /* Add response headers. */
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 /* Add msg body, if any */
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 /* Ignore */
1785 return PJ_SUCCESS;
1786 }
1787 }
1788
1789 return PJ_SUCCESS;
1790 }
1791
1792 /*
1793 * Get subscription state from the value of Subscription-State header.
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 * Transaction event processing by UAC, after subscription is sent.
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 /* Received response to outgoing request that establishes/refresh
1834 * subscription.
1835 */
1836
1837 /* First time initial request is sent. */
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 /* Only interested in final response */
1846 if (tsx->state != PJSIP_TSX_STATE_COMPLETED &&
1847 tsx->state != PJSIP_TSX_STATE_TERMINATED)
1848 {
1849 return;
1850 }
1851
1852 /* Clear pending subscription */
1853 if (tsx == sub->pending_sub) {
1854 sub->pending_sub = NULL;
1855 } else if (sub->pending_sub != NULL) {
1856 /* This SUBSCRIBE transaction has been "renewed" with another
1857 * SUBSCRIBE, so we can just ignore this. For example, user
1858 * sent SUBSCRIBE followed immediately with UN-SUBSCRIBE.
1859 */
1860 return;
1861 }
1862
1863 /* Handle authentication. */
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 /* Previously failed transaction has terminated */
1871 return;
1872 }
1873
1874 /* Dialog grp_lock is held by the caller
1875 * (pjsip_dlg_on_tsx_state). We do NOT release it here
1876 * because grp_lock is recursive (sync send from callback
1877 * works) and releasing a lock held by a parent caller
1878 * risks state changes from another thread.
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 /* Authentication failed! */
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 /* Successfull SUBSCRIBE request!
1920 * This could be:
1921 * - response to initial SUBSCRIBE request
1922 * - response to subsequent refresh
1923 * - response to unsubscription
1924 */
1925
1926 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
1927 /* Ignore; this transaction has been processed before */
1928 return;
1929 }
1930
1931 /* Update UAC refresh time, if response contains Expires header,
1932 * only when we're not unsubscribing.
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 /* Update time */
1947 update_expires(sub, sub->expires->ivalue);
1948
1949 /* Start UAC refresh timer, only when we're not unsubscribing */
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 /* Reduce timeout by about 1 - 10 secs (randomized) */
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 /* Otherwise set timer to terminate client subscription when
1964 * NOTIFY to end subscription is not received.
1965 */
1966 set_timer(sub, TIMER_TYPE_UAC_TERMINATE, TIME_UAC_TERMINATE);
1967 }
1968
1969 /* Set state, if necessary */
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 /* Failed SUBSCRIBE request!
1978 *
1979 * The RFC 3265 says that if outgoing SUBSCRIBE fails with status
1980 * other than 481, the subscription is still considered valid for
1981 * the duration of the last Expires.
1982 *
1983 * Since we send refresh about 5 seconds (TIME_UAC_REFRESH) before
1984 * expiration, theoritically the expiration is still valid for the
1985 * next 5 seconds even when we receive non-481 failed response.
1986 *
1987 * Ah, what the heck!
1988 *
1989 * Just terminate now!
1990 *
1991 */
1992
1993 if (sub->state == PJSIP_EVSUB_STATE_TERMINATED) {
1994 /* Ignore, has been handled before */
1995 return;
1996 }
1997
1998 /* Ignore 490 (Request Updated) status.
1999 * This happens when application sends SUBSCRIBE/REFER while
2000 * another one is still in progress.
2001 */
2002 if (tsx->status_code == PJSIP_SC_REQUEST_UPDATED) {
2003 return;
2004 }
2005
2006 /* Set state to TERMINATED */
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 /* Incoming NOTIFY.
2015 * This can be the result of:
2016 * - Initial subscription response
2017 * - UAS updating the resource info.
2018 * - Unsubscription response.
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 /* Only want to handle initial NOTIFY receive event. */
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 /* Get subscription state header. */
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 /* Bad request! Add warning header. */
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 /* Call application registered callback to handle incoming NOTIFY,
2062 * if any.
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 /* Application MUST specify final response! */
2069 PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; });
2070
2071 /* Must be a valid status code */
2072 PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; });
2073 }
2074
2075
2076 /* If non-2xx should be returned, then send the response.
2077 * No need to update server subscription state.
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 /* Start timer to terminate subscription, just in case server
2087 * is not able to generate NOTIFY to our response.
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 /* Update expiration from the value of expires param in
2105 * Subscription-State header, but ONLY when subscription state
2106 * is "active" or "pending", AND the header contains expires param.
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 /* Start UAC refresh timer, only when we're not unsubscribing */
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 /* Find out the state */
2127 get_hdr_state(sub_state, &new_state, &new_state_str);
2128
2129 /* Send response. */
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 /* Set the state */
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 * Unexpected method!
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 * Transaction event processing by UAS, after subscription is accepted.
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 * Incoming request (e.g. SUBSCRIBE or REFER) to refresh subsciption.
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 /* Only wants to handle the first event when the request is
2191 * received.
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 /* Set expiration time based on client request (in Expires header),
2200 * or package default expiration time.
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 /* Update time (before calling on_rx_refresh, since application
2220 * will send NOTIFY.
2221 */
2222 update_expires(sub, sub->expires->ivalue);
2223
2224
2225 /* Save old state.
2226 * If application respond with non-2xx, revert to old state.
2227 * But if subscriber wants to unsubscribe, there is no
2228 * turning back.
2229 */
2230 old_state = sub->state;
2231 old_state_str = sub->state_str;
2232
2233 /* Must set this before calling set_state(), to prevent evsub
2234 * from being destroyed.
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 /* Call application's on_rx_refresh, just in case it wants to send
2251 * response other than 200 (OK)
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 /* We shouldn't fail an unsubscription request, should we? */
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 /* Application MUST specify final response! */
2277 PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; });
2278
2279 /* Must be a valid status code */
2280 PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; });
2281
2282
2283 /* Create and send response */
2284 status = create_response(sub, rdata, st_code, st_text, &res_hdr,
2285 body, &tdata);
2286 if (status == PJ_SUCCESS) {
2287 /* Add expires header: */
2288 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
2289 pjsip_hdr_shallow_clone(tdata->pool,
2290 sub->expires));
2291
2292 /* Send */
2293 pjsip_dlg_send_response(sub->dlg, tsx, tdata);
2294 }
2295
2296 /* Update state or revert state */
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 /* Set UAS timeout timer, when state is not terminated. */
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 /* Send the pending NOTIFY sent by app from inside
2317 * on_rx_refresh() callback.
2318 */
2319 //pj_assert(sub->pending_notify);
2320 /* Make sure that pending_notify is set. */
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 /* Handle authentication */
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 /* Handled by other module already (e.g: invite module) */
2338 if (tsx->last_tx->auth_retry)
2339 return;
2340
2341 /* Dialog grp_lock held by caller; see SUBSCRIBE handler
2342 * comment above for locking rationale.
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 /* Can't authenticate. Terminate session (?) */
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 /* NOTIFY failure check */
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 * Terminate event usage if we receive non 2xx without retry_after
2410 * parameter, 481, 408 responses.
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 * Unexpected method!
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 * Notification when transaction state has changed!
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 /* Call on_tsx_state callback, if any. */
2445 if (sub->user.on_tsx_state && sub->call_cb)
2446 (*sub->user.on_tsx_state)(sub, tsx, event);
2447
2448
2449 /* Process the event: */
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 /* Check transaction TERMINATE event */
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