blob: d4dc8b4b13b0b5f57b8ea203b94451dfdb15a218 [file] [log] [blame]
Benny Prijono834aee32006-02-19 01:38:06 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono834aee32006-02-19 01:38:06 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjsip-simple/evsub.h>
21#include <pjsip-simple/evsub_msg.h>
22#include <pjsip-simple/errno.h>
23#include <pjsip/sip_errno.h>
24#include <pjsip/sip_module.h>
25#include <pjsip/sip_endpoint.h>
26#include <pjsip/sip_dialog.h>
27#include <pjsip/sip_auth.h>
28#include <pjsip/sip_transaction.h>
29#include <pjsip/sip_event.h>
30#include <pj/assert.h>
31#include <pj/guid.h>
32#include <pj/log.h>
33#include <pj/os.h>
34#include <pj/pool.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 */
47enum
48{
49 PJSIP_SUBSCRIBE_METHOD = PJSIP_OTHER_METHOD,
50 PJSIP_NOTIFY_METHOD = PJSIP_OTHER_METHOD
51};
52
Benny Prijono1f61a8f2007-08-16 10:11:44 +000053PJ_DEF_DATA(const pjsip_method) pjsip_subscribe_method =
Benny Prijono834aee32006-02-19 01:38:06 +000054{
Benny Prijono9d4469d2007-05-02 05:14:29 +000055 (pjsip_method_e) PJSIP_SUBSCRIBE_METHOD,
Benny Prijono834aee32006-02-19 01:38:06 +000056 { "SUBSCRIBE", 9 }
57};
58
Benny Prijono1f61a8f2007-08-16 10:11:44 +000059PJ_DEF_DATA(const pjsip_method) pjsip_notify_method =
Benny Prijono834aee32006-02-19 01:38:06 +000060{
Benny Prijono9d4469d2007-05-02 05:14:29 +000061 (pjsip_method_e) PJSIP_NOTIFY_METHOD,
Benny Prijono834aee32006-02-19 01:38:06 +000062 { "NOTIFY", 6 }
63};
64
Benny Prijono1f61a8f2007-08-16 10:11:44 +000065/**
66 * SUBSCRIBE method constant.
67 */
68PJ_DEF(const pjsip_method*) pjsip_get_subscribe_method()
69{
70 return &pjsip_subscribe_method;
71}
72
73/**
74 * NOTIFY method constant.
75 */
76PJ_DEF(const pjsip_method*) pjsip_get_notify_method()
77{
78 return &pjsip_notify_method;
79}
80
81
Benny Prijono834aee32006-02-19 01:38:06 +000082/*
83 * Static prototypes.
84 */
85static void mod_evsub_on_tsx_state(pjsip_transaction*, pjsip_event*);
86static pj_status_t mod_evsub_unload(void);
87
88
89/*
90 * State names.
91 */
92static pj_str_t evsub_state_names[] =
93{
94 { "NULL", 4},
95 { "SENT", 4},
96 { "ACCEPTED", 8},
97 { "PENDING", 7},
98 { "ACTIVE", 6},
99 { "TERMINATED", 10},
100 { "UNKNOWN", 7}
101};
102
103/*
104 * Timer constants.
105 */
106
107/* Number of seconds to send SUBSCRIBE before the actual expiration */
Benny Prijonocf2e6732009-06-01 15:39:52 +0000108#define TIME_UAC_REFRESH PJSIP_EVSUB_TIME_UAC_REFRESH
Benny Prijono834aee32006-02-19 01:38:06 +0000109
110/* Time to wait for the final NOTIFY after sending unsubscription */
Benny Prijonocf2e6732009-06-01 15:39:52 +0000111#define TIME_UAC_TERMINATE PJSIP_EVSUB_TIME_UAC_TERMINATE
Benny Prijono834aee32006-02-19 01:38:06 +0000112
113/* If client responds NOTIFY with non-2xx final response (such as 401),
114 * wait for this seconds for further NOTIFY, otherwise client will
115 * unsubscribe
116 */
Benny Prijonocf2e6732009-06-01 15:39:52 +0000117#define TIME_UAC_WAIT_NOTIFY PJSIP_EVSUB_TIME_UAC_WAIT_NOTIFY
Benny Prijono834aee32006-02-19 01:38:06 +0000118
119
120/*
121 * Timer id
122 */
123enum timer_id
124{
125 /* No timer. */
126 TIMER_TYPE_NONE,
127
128 /* Time to refresh client subscription.
129 * The action is to call on_client_refresh() callback.
130 */
131 TIMER_TYPE_UAC_REFRESH,
132
133 /* UAS timeout after to subscription refresh.
134 * The action is to call on_server_timeout() callback.
135 */
136 TIMER_TYPE_UAS_TIMEOUT,
137
138 /* UAC waiting for final NOTIFY after unsubscribing
139 * The action is to terminate.
140 */
141 TIMER_TYPE_UAC_TERMINATE,
142
143 /* UAC waiting for further NOTIFY after sending non-2xx response to
144 * NOTIFY. The action is to unsubscribe.
145 */
146 TIMER_TYPE_UAC_WAIT_NOTIFY,
147
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000148 /* Max nb of timer types. */
149 TIMER_TYPE_MAX
Benny Prijono834aee32006-02-19 01:38:06 +0000150};
151
152static const char *timer_names[] =
153{
154 "None",
155 "UAC_REFRESH",
156 "UAS_TIMEOUT"
157 "UAC_TERMINATE",
158 "UAC_WAIT_NOTIFY",
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000159 "INVALID_TIMER"
Benny Prijono834aee32006-02-19 01:38:06 +0000160};
161
162/*
163 * Definition of event package.
164 */
165struct evpkg
166{
167 PJ_DECL_LIST_MEMBER(struct evpkg);
168
169 pj_str_t pkg_name;
170 pjsip_module *pkg_mod;
171 unsigned pkg_expires;
172 pjsip_accept_hdr *pkg_accept;
173};
174
175
176/*
177 * Event subscription module (mod-evsub).
178 */
179static struct mod_evsub
180{
181 pjsip_module mod;
182 pj_pool_t *pool;
183 pjsip_endpoint *endpt;
184 struct evpkg pkg_list;
185 pjsip_allow_events_hdr *allow_events_hdr;
186
187} mod_evsub =
188{
189 {
Benny Prijono2f8992b2006-02-25 21:16:36 +0000190 NULL, NULL, /* prev, next. */
191 { "mod-evsub", 9 }, /* Name. */
192 -1, /* Id */
193 PJSIP_MOD_PRIORITY_DIALOG_USAGE, /* Priority */
194 NULL, /* load() */
195 NULL, /* start() */
196 NULL, /* stop() */
197 &mod_evsub_unload, /* unload() */
198 NULL, /* on_rx_request() */
199 NULL, /* on_rx_response() */
200 NULL, /* on_tx_request. */
201 NULL, /* on_tx_response() */
202 &mod_evsub_on_tsx_state, /* on_tsx_state() */
Benny Prijono834aee32006-02-19 01:38:06 +0000203 }
204};
205
206
207/*
208 * Event subscription session.
209 */
210struct pjsip_evsub
211{
212 char obj_name[PJ_MAX_OBJ_NAME]; /**< Name. */
213 pj_pool_t *pool; /**< Pool. */
214 pjsip_endpoint *endpt; /**< Endpoint instance. */
215 pjsip_dialog *dlg; /**< Underlying dialog. */
216 struct evpkg *pkg; /**< The event package. */
Benny Prijono26ff9062006-02-21 23:47:00 +0000217 unsigned option; /**< Options. */
Benny Prijono834aee32006-02-19 01:38:06 +0000218 pjsip_evsub_user user; /**< Callback. */
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000219 pj_bool_t call_cb; /**< Notify callback? */
Benny Prijono834aee32006-02-19 01:38:06 +0000220 pjsip_role_e role; /**< UAC=subscriber, UAS=notifier */
221 pjsip_evsub_state state; /**< Subscription state. */
222 pj_str_t state_str; /**< String describing the state. */
223 pjsip_evsub_state dst_state; /**< Pending state to be set. */
224 pj_str_t dst_state_str;/**< Pending state to be set. */
Benny Prijono75130572008-07-17 13:53:41 +0000225 pj_str_t term_reason; /**< Termination reason. */
Benny Prijono834aee32006-02-19 01:38:06 +0000226 pjsip_method method; /**< Method that established subscr.*/
227 pjsip_event_hdr *event; /**< Event description. */
228 pjsip_expires_hdr *expires; /**< Expires header */
229 pjsip_accept_hdr *accept; /**< Local Accept header. */
230
231 pj_time_val refresh_time; /**< Time to refresh. */
232 pj_timer_entry timer; /**< Internal timer. */
233 int pending_tsx; /**< Number of pending transactions.*/
Benny Prijono69b98ab2006-03-03 10:23:35 +0000234 pjsip_transaction *pending_sub; /**< Pending UAC SUBSCRIBE tsx. */
Benny Prijono834aee32006-02-19 01:38:06 +0000235
236 void *mod_data[PJSIP_MAX_MODULE]; /**< Module data. */
237};
238
239
240/*
241 * This is the structure that will be "attached" to dialog.
242 * The purpose is to allow multiple subscriptions inside a dialog.
243 */
244struct dlgsub
245{
246 PJ_DECL_LIST_MEMBER(struct dlgsub);
247 pjsip_evsub *sub;
248};
249
250
251/* Static vars. */
252static const pj_str_t STR_EVENT = { "Event", 5 };
Benny Prijono0c13f3d2008-07-16 22:39:45 +0000253static const pj_str_t STR_EVENT_S = { "Event", 5 };
Benny Prijono834aee32006-02-19 01:38:06 +0000254static const pj_str_t STR_SUB_STATE = { "Subscription-State", 18 };
255static const pj_str_t STR_TERMINATED = { "terminated", 10 };
256static const pj_str_t STR_ACTIVE = { "active", 6 };
257static const pj_str_t STR_PENDING = { "pending", 7 };
258static const pj_str_t STR_TIMEOUT = { "timeout", 7};
259
Benny Prijono26ff9062006-02-21 23:47:00 +0000260
Benny Prijono834aee32006-02-19 01:38:06 +0000261/*
262 * On unload module.
263 */
264static pj_status_t mod_evsub_unload(void)
265{
266 pjsip_endpt_release_pool(mod_evsub.endpt, mod_evsub.pool);
267 mod_evsub.pool = NULL;
268
269 return PJ_SUCCESS;
270}
271
Benny Prijono26ff9062006-02-21 23:47:00 +0000272/* Proto for pjsipsimple_strerror().
273 * Defined in errno.c
274 */
275PJ_DECL(pj_str_t) pjsipsimple_strerror( pj_status_t statcode,
276 char *buf, pj_size_t bufsize );
277
Benny Prijono834aee32006-02-19 01:38:06 +0000278/*
279 * Init and register module.
280 */
281PJ_DEF(pj_status_t) pjsip_evsub_init_module(pjsip_endpoint *endpt)
282{
283 pj_status_t status;
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000284 pj_str_t method_tags[] = {
285 { "SUBSCRIBE", 9},
286 { "NOTIFY", 6}
287 };
Benny Prijono834aee32006-02-19 01:38:06 +0000288
Benny Prijono26ff9062006-02-21 23:47:00 +0000289 pj_register_strerror(PJSIP_SIMPLE_ERRNO_START, PJ_ERRNO_SPACE_SIZE,
290 &pjsipsimple_strerror);
291
Benny Prijono834aee32006-02-19 01:38:06 +0000292 PJ_ASSERT_RETURN(endpt != NULL, PJ_EINVAL);
293 PJ_ASSERT_RETURN(mod_evsub.mod.id == -1, PJ_EINVALIDOP);
294
295 /* Keep endpoint for future reference: */
296 mod_evsub.endpt = endpt;
297
298 /* Init event package list: */
299 pj_list_init(&mod_evsub.pkg_list);
300
301 /* Create pool: */
Benny Prijono10d8dbd2008-07-13 13:12:36 +0000302 mod_evsub.pool = pjsip_endpt_create_pool(endpt, "evsub", 512, 512);
Benny Prijono834aee32006-02-19 01:38:06 +0000303 if (!mod_evsub.pool)
304 return PJ_ENOMEM;
305
306 /* Register module: */
307 status = pjsip_endpt_register_module(endpt, &mod_evsub.mod);
308 if (status != PJ_SUCCESS)
309 goto on_error;
310
311 /* Create Allow-Events header: */
312 mod_evsub.allow_events_hdr = pjsip_allow_events_hdr_create(mod_evsub.pool);
313
314 /* Register SIP-event specific headers parser: */
315 pjsip_evsub_init_parser();
316
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000317 /* Register new methods SUBSCRIBE and NOTIFY in Allow-ed header */
318 pjsip_endpt_add_capability(endpt, &mod_evsub.mod, PJSIP_H_ALLOW, NULL,
319 2, method_tags);
320
321 /* Done. */
Benny Prijono834aee32006-02-19 01:38:06 +0000322 return PJ_SUCCESS;
323
324on_error:
325 if (mod_evsub.pool) {
326 pjsip_endpt_release_pool(endpt, mod_evsub.pool);
327 mod_evsub.pool = NULL;
328 }
329 mod_evsub.endpt = NULL;
330 return status;
331}
332
333
334/*
335 * Get the instance of the module.
336 */
337PJ_DEF(pjsip_module*) pjsip_evsub_instance(void)
338{
339 PJ_ASSERT_RETURN(mod_evsub.mod.id != -1, NULL);
340
341 return &mod_evsub.mod;
342}
343
344
345/*
346 * Get the event subscription instance in the transaction.
347 */
348PJ_DEF(pjsip_evsub*) pjsip_tsx_get_evsub(pjsip_transaction *tsx)
349{
Benny Prijono9d4469d2007-05-02 05:14:29 +0000350 return (pjsip_evsub*) tsx->mod_data[mod_evsub.mod.id];
Benny Prijono834aee32006-02-19 01:38:06 +0000351}
352
353
354/*
355 * Set event subscription's module data.
356 */
357PJ_DEF(void) pjsip_evsub_set_mod_data( pjsip_evsub *sub, unsigned mod_id,
358 void *data )
359{
360 PJ_ASSERT_ON_FAIL(mod_id < PJSIP_MAX_MODULE, return);
361 sub->mod_data[mod_id] = data;
362}
363
364
365/*
366 * Get event subscription's module data.
367 */
368PJ_DEF(void*) pjsip_evsub_get_mod_data( pjsip_evsub *sub, unsigned mod_id )
369{
370 PJ_ASSERT_RETURN(mod_id < PJSIP_MAX_MODULE, NULL);
371 return sub->mod_data[mod_id];
372}
373
374
375/*
376 * Find registered event package with matching name.
377 */
378static struct evpkg* find_pkg(const pj_str_t *event_name)
379{
380 struct evpkg *pkg;
381
382 pkg = mod_evsub.pkg_list.next;
383 while (pkg != &mod_evsub.pkg_list) {
384
385 if (pj_stricmp(&pkg->pkg_name, event_name) == 0) {
386 return pkg;
387 }
388
389 pkg = pkg->next;
390 }
391
392 return NULL;
393}
394
395/*
396 * Register an event package
397 */
398PJ_DEF(pj_status_t) pjsip_evsub_register_pkg( pjsip_module *pkg_mod,
399 const pj_str_t *event_name,
400 unsigned expires,
401 unsigned accept_cnt,
402 const pj_str_t accept[])
403{
404 struct evpkg *pkg;
405 unsigned i;
406
407 PJ_ASSERT_RETURN(pkg_mod && event_name, PJ_EINVAL);
408 PJ_ASSERT_RETURN(accept_cnt < PJ_ARRAY_SIZE(pkg->pkg_accept->values),
409 PJ_ETOOMANY);
410
Benny Prijonoc2456cc2007-10-25 03:19:58 +0000411 /* Make sure evsub module has been initialized */
412 PJ_ASSERT_RETURN(mod_evsub.mod.id != -1, PJ_EINVALIDOP);
413
Benny Prijono834aee32006-02-19 01:38:06 +0000414 /* Make sure no module with the specified name already registered: */
415
416 PJ_ASSERT_RETURN(find_pkg(event_name) == NULL, PJSIP_SIMPLE_EPKGEXISTS);
417
418
419 /* Create new event package: */
420
Benny Prijono9d4469d2007-05-02 05:14:29 +0000421 pkg = PJ_POOL_ALLOC_T(mod_evsub.pool, struct evpkg);
Benny Prijono834aee32006-02-19 01:38:06 +0000422 pkg->pkg_mod = pkg_mod;
423 pkg->pkg_expires = expires;
424 pj_strdup(mod_evsub.pool, &pkg->pkg_name, event_name);
425
426 pkg->pkg_accept = pjsip_accept_hdr_create(mod_evsub.pool);
427 pkg->pkg_accept->count = accept_cnt;
428 for (i=0; i<accept_cnt; ++i) {
429 pj_strdup(mod_evsub.pool, &pkg->pkg_accept->values[i], &accept[i]);
430 }
431
432 /* Add to package list: */
433
434 pj_list_push_back(&mod_evsub.pkg_list, pkg);
435
436 /* Add to Allow-Events header: */
437
438 if (mod_evsub.allow_events_hdr->count !=
439 PJ_ARRAY_SIZE(mod_evsub.allow_events_hdr->values))
440 {
441 mod_evsub.allow_events_hdr->values[mod_evsub.allow_events_hdr->count] =
442 pkg->pkg_name;
443 ++mod_evsub.allow_events_hdr->count;
444 }
445
Benny Prijono56315612006-07-18 14:39:40 +0000446 /* Add to endpoint's Accept header */
447 pjsip_endpt_add_capability(mod_evsub.endpt, &mod_evsub.mod,
448 PJSIP_H_ACCEPT, NULL,
449 pkg->pkg_accept->count,
450 pkg->pkg_accept->values);
451
Benny Prijono834aee32006-02-19 01:38:06 +0000452
453 /* Done */
454
455 PJ_LOG(5,(THIS_FILE, "Event pkg \"%.*s\" registered by %.*s",
456 (int)event_name->slen, event_name->ptr,
457 (int)pkg_mod->name.slen, pkg_mod->name.ptr));
458
459 return PJ_SUCCESS;
460}
461
462
Benny Prijono56315612006-07-18 14:39:40 +0000463/*
464 * Retrieve Allow-Events header
465 */
466PJ_DEF(const pjsip_hdr*) pjsip_evsub_get_allow_events_hdr(pjsip_module *m)
467{
468 struct mod_evsub *mod;
469
470 if (m == NULL)
471 m = pjsip_evsub_instance();
472
473 mod = (struct mod_evsub*)m;
474
475 return (pjsip_hdr*) mod->allow_events_hdr;
476}
477
Benny Prijono834aee32006-02-19 01:38:06 +0000478
479/*
480 * Update expiration time.
481 */
482static void update_expires( pjsip_evsub *sub, pj_uint32_t interval )
483{
484 pj_gettimeofday(&sub->refresh_time);
485 sub->refresh_time.sec += interval;
486}
487
488
489/*
490 * Schedule timer.
491 */
492static void set_timer( pjsip_evsub *sub, int timer_id,
493 pj_int32_t seconds)
494{
495 if (sub->timer.id != TIMER_TYPE_NONE) {
496 PJ_LOG(5,(sub->obj_name, "%s %s timer",
497 (timer_id==sub->timer.id ? "Updating" : "Cancelling"),
498 timer_names[sub->timer.id]));
499 pjsip_endpt_cancel_timer(sub->endpt, &sub->timer);
500 sub->timer.id = TIMER_TYPE_NONE;
501 }
502
503 if (timer_id != TIMER_TYPE_NONE) {
504 pj_time_val timeout;
505
506 PJ_ASSERT_ON_FAIL(seconds > 0, return);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000507 PJ_ASSERT_ON_FAIL(timer_id>TIMER_TYPE_NONE && timer_id<TIMER_TYPE_MAX,
508 return);
Benny Prijono834aee32006-02-19 01:38:06 +0000509
510 timeout.sec = seconds;
511 timeout.msec = 0;
512 sub->timer.id = timer_id;
513
514 pjsip_endpt_schedule_timer(sub->endpt, &sub->timer, &timeout);
515
516 PJ_LOG(5,(sub->obj_name, "Timer %s scheduled in %d seconds",
517 timer_names[sub->timer.id], timeout.sec));
518 }
519}
520
521
522/*
523 * Destroy session.
524 */
525static void evsub_destroy( pjsip_evsub *sub )
526{
527 struct dlgsub *dlgsub_head, *dlgsub;
528
529 PJ_LOG(4,(sub->obj_name, "Subscription destroyed"));
530
531 /* Kill timer */
532 set_timer(sub, TIMER_TYPE_NONE, 0);
533
Benny Prijono9d4469d2007-05-02 05:14:29 +0000534 /* Remove this session from dialog's list of subscription */
535 dlgsub_head = (struct dlgsub *) sub->dlg->mod_data[mod_evsub.mod.id];
Benny Prijono834aee32006-02-19 01:38:06 +0000536 dlgsub = dlgsub_head->next;
537 while (dlgsub != dlgsub_head) {
538
539 if (dlgsub->sub == sub) {
540 pj_list_erase(dlgsub);
541 break;
542 }
543
544 dlgsub = dlgsub->next;
545 }
546
547 /* Decrement dialog's session */
548 pjsip_dlg_dec_session(sub->dlg, &mod_evsub.mod);
549}
550
551/*
552 * Set subscription session state.
553 */
554static void set_state( pjsip_evsub *sub, pjsip_evsub_state state,
Benny Prijono75130572008-07-17 13:53:41 +0000555 const pj_str_t *state_str, pjsip_event *event,
556 const pj_str_t *reason)
Benny Prijono834aee32006-02-19 01:38:06 +0000557{
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000558 pjsip_evsub_state prev_state = sub->state;
Benny Prijono834aee32006-02-19 01:38:06 +0000559 pj_str_t old_state_str = sub->state_str;
560
561 sub->state = state;
562
563 if (state_str && state_str->slen)
564 pj_strdup_with_null(sub->pool, &sub->state_str, state_str);
565 else
566 sub->state_str = evsub_state_names[state];
567
Benny Prijono75130572008-07-17 13:53:41 +0000568 if (reason && sub->term_reason.slen==0)
569 pj_strdup(sub->pool, &sub->term_reason, reason);
570
Benny Prijono834aee32006-02-19 01:38:06 +0000571 PJ_LOG(4,(sub->obj_name,
572 "Subscription state changed %.*s --> %.*s",
573 (int)old_state_str.slen,
574 old_state_str.ptr,
575 (int)sub->state_str.slen,
576 sub->state_str.ptr));
577
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000578 if (sub->user.on_evsub_state && sub->call_cb)
Benny Prijono834aee32006-02-19 01:38:06 +0000579 (*sub->user.on_evsub_state)(sub, event);
580
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000581 if (state == PJSIP_EVSUB_STATE_TERMINATED &&
582 prev_state != PJSIP_EVSUB_STATE_TERMINATED)
583 {
Benny Prijono834aee32006-02-19 01:38:06 +0000584 if (sub->pending_tsx == 0) {
585 evsub_destroy(sub);
586 }
587 }
588}
589
590
591/*
592 * Timer callback.
593 */
594static void on_timer( pj_timer_heap_t *timer_heap,
595 struct pj_timer_entry *entry)
596{
597 pjsip_evsub *sub;
598 int timer_id;
599
600 PJ_UNUSED_ARG(timer_heap);
601
Benny Prijono9d4469d2007-05-02 05:14:29 +0000602 sub = (pjsip_evsub*) entry->user_data;
Benny Prijono834aee32006-02-19 01:38:06 +0000603
604 pjsip_dlg_inc_lock(sub->dlg);
605
606 timer_id = entry->id;
607 entry->id = TIMER_TYPE_NONE;
608
609 switch (timer_id) {
610
611 case TIMER_TYPE_UAC_REFRESH:
612 /* Time for UAC to refresh subscription */
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000613 if (sub->user.on_client_refresh && sub->call_cb) {
Benny Prijono834aee32006-02-19 01:38:06 +0000614 (*sub->user.on_client_refresh)(sub);
615 } else {
616 pjsip_tx_data *tdata;
617 pj_status_t status;
618
619 PJ_LOG(5,(sub->obj_name, "Refreshing subscription."));
Benny Prijono736d0f72006-09-13 22:45:38 +0000620 status = pjsip_evsub_initiate(sub, NULL,
Benny Prijono834aee32006-02-19 01:38:06 +0000621 sub->expires->ivalue,
622 &tdata);
623 if (status == PJ_SUCCESS)
624 pjsip_evsub_send_request(sub, tdata);
625 }
626 break;
627
628 case TIMER_TYPE_UAS_TIMEOUT:
629 /* Refresh from UAC has not been received */
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000630 if (sub->user.on_server_timeout && sub->call_cb) {
Benny Prijono834aee32006-02-19 01:38:06 +0000631 (*sub->user.on_server_timeout)(sub);
632 } else {
633 pjsip_tx_data *tdata;
634 pj_status_t status;
635
636 PJ_LOG(5,(sub->obj_name, "Timeout waiting for refresh. "
637 "Sending NOTIFY to terminate."));
638 status = pjsip_evsub_notify( sub, PJSIP_EVSUB_STATE_TERMINATED,
639 NULL, &STR_TIMEOUT, &tdata);
640 if (status == PJ_SUCCESS)
641 pjsip_evsub_send_request(sub, tdata);
642 }
643 break;
644
645 case TIMER_TYPE_UAC_TERMINATE:
646 {
Benny Prijono75130572008-07-17 13:53:41 +0000647 pj_str_t timeout = {"timeout", 7};
648
Benny Prijono834aee32006-02-19 01:38:06 +0000649 PJ_LOG(5,(sub->obj_name, "Timeout waiting for final NOTIFY. "
650 "Terminating.."));
Benny Prijono75130572008-07-17 13:53:41 +0000651 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL,
652 &timeout);
Benny Prijono834aee32006-02-19 01:38:06 +0000653 }
654 break;
655
656 case TIMER_TYPE_UAC_WAIT_NOTIFY:
657 {
658 pjsip_tx_data *tdata;
659 pj_status_t status;
660
661 PJ_LOG(5,(sub->obj_name,
662 "Timeout waiting for subsequent NOTIFY (we did "
663 "send non-2xx response for previous NOTIFY). "
664 "Unsubscribing.."));
Benny Prijono736d0f72006-09-13 22:45:38 +0000665 status = pjsip_evsub_initiate( sub, NULL, 0, &tdata);
Benny Prijono834aee32006-02-19 01:38:06 +0000666 if (status == PJ_SUCCESS)
667 pjsip_evsub_send_request(sub, tdata);
668 }
669 break;
670
671 default:
672 pj_assert(!"Invalid timer id");
673 }
674
675 pjsip_dlg_dec_lock(sub->dlg);
676}
677
678
679/*
680 * Create subscription session, used for both client and notifier.
681 */
682static pj_status_t evsub_create( pjsip_dialog *dlg,
683 pjsip_role_e role,
684 const pjsip_evsub_user *user_cb,
685 const pj_str_t *event,
Benny Prijono26ff9062006-02-21 23:47:00 +0000686 unsigned option,
Benny Prijono834aee32006-02-19 01:38:06 +0000687 pjsip_evsub **p_evsub )
688{
689 pjsip_evsub *sub;
690 struct evpkg *pkg;
691 struct dlgsub *dlgsub_head, *dlgsub;
692 pj_status_t status;
693
694 /* Make sure there's package register for the event name: */
695
696 pkg = find_pkg(event);
697 if (pkg == NULL)
698 return PJSIP_SIMPLE_ENOPKG;
699
700
Benny Prijono8eae8382006-08-10 21:44:26 +0000701 /* Must lock dialog before using pool etc. */
702 pjsip_dlg_inc_lock(dlg);
703
Benny Prijono834aee32006-02-19 01:38:06 +0000704 /* Init attributes: */
705
Benny Prijono9d4469d2007-05-02 05:14:29 +0000706 sub = PJ_POOL_ZALLOC_T(dlg->pool, struct pjsip_evsub);
Benny Prijono834aee32006-02-19 01:38:06 +0000707 sub->pool = dlg->pool;
708 sub->endpt = dlg->endpt;
709 sub->dlg = dlg;
710 sub->pkg = pkg;
711 sub->role = role;
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000712 sub->call_cb = PJ_TRUE;
Benny Prijono26ff9062006-02-21 23:47:00 +0000713 sub->option = option;
Benny Prijono834aee32006-02-19 01:38:06 +0000714 sub->state = PJSIP_EVSUB_STATE_NULL;
715 sub->state_str = evsub_state_names[sub->state];
716 sub->expires = pjsip_expires_hdr_create(sub->pool, pkg->pkg_expires);
Benny Prijono9d4469d2007-05-02 05:14:29 +0000717 sub->accept = (pjsip_accept_hdr*)
718 pjsip_hdr_clone(sub->pool, pkg->pkg_accept);
Benny Prijono834aee32006-02-19 01:38:06 +0000719
720 sub->timer.user_data = sub;
721 sub->timer.cb = &on_timer;
722
723 /* Set name. */
Benny Prijonoed811d72006-03-10 12:57:12 +0000724 pj_ansi_snprintf(sub->obj_name, PJ_ARRAY_SIZE(sub->obj_name),
725 "evsub%p", sub);
Benny Prijono834aee32006-02-19 01:38:06 +0000726
727
728 /* Copy callback, if any: */
729 if (user_cb)
730 pj_memcpy(&sub->user, user_cb, sizeof(pjsip_evsub_user));
731
732
733 /* Create Event header: */
734 sub->event = pjsip_event_hdr_create(sub->pool);
735 pj_strdup(sub->pool, &sub->event->event_type, event);
736
737
738 /* Create subcription list: */
739
Benny Prijono9d4469d2007-05-02 05:14:29 +0000740 dlgsub_head = PJ_POOL_ALLOC_T(sub->pool, struct dlgsub);
741 dlgsub = PJ_POOL_ALLOC_T(sub->pool, struct dlgsub);
Benny Prijono834aee32006-02-19 01:38:06 +0000742 dlgsub->sub = sub;
743
744 pj_list_init(dlgsub_head);
745 pj_list_push_back(dlgsub_head, dlgsub);
746
747
748 /* Register as dialog usage: */
749
750 status = pjsip_dlg_add_usage(dlg, &mod_evsub.mod, dlgsub_head);
Benny Prijono8eae8382006-08-10 21:44:26 +0000751 if (status != PJ_SUCCESS) {
752 pjsip_dlg_dec_lock(dlg);
Benny Prijono834aee32006-02-19 01:38:06 +0000753 return status;
Benny Prijono8eae8382006-08-10 21:44:26 +0000754 }
Benny Prijono834aee32006-02-19 01:38:06 +0000755
756
757 PJ_LOG(5,(sub->obj_name, "%s subscription created, using dialog %s",
758 (role==PJSIP_ROLE_UAC ? "UAC" : "UAS"),
759 dlg->obj_name));
760
761 *p_evsub = sub;
Benny Prijono8eae8382006-08-10 21:44:26 +0000762 pjsip_dlg_dec_lock(dlg);
Benny Prijono834aee32006-02-19 01:38:06 +0000763
764 return PJ_SUCCESS;
765}
766
767
768
769/*
770 * Create client subscription session.
771 */
772PJ_DEF(pj_status_t) pjsip_evsub_create_uac( pjsip_dialog *dlg,
773 const pjsip_evsub_user *user_cb,
774 const pj_str_t *event,
Benny Prijono26ff9062006-02-21 23:47:00 +0000775 unsigned option,
Benny Prijono834aee32006-02-19 01:38:06 +0000776 pjsip_evsub **p_evsub)
777{
778 pjsip_evsub *sub;
779 pj_status_t status;
780
781 PJ_ASSERT_RETURN(dlg && event && p_evsub, PJ_EINVAL);
782
783 pjsip_dlg_inc_lock(dlg);
Benny Prijono26ff9062006-02-21 23:47:00 +0000784 status = evsub_create(dlg, PJSIP_UAC_ROLE, user_cb, event, option, &sub);
Benny Prijono834aee32006-02-19 01:38:06 +0000785 if (status != PJ_SUCCESS)
786 goto on_return;
787
Benny Prijono26ff9062006-02-21 23:47:00 +0000788 /* Add unique Id to Event header, only when PJSIP_EVSUB_NO_EVENT_ID
789 * is not specified.
790 */
791 if ((option & PJSIP_EVSUB_NO_EVENT_ID) == 0) {
792 pj_create_unique_string(sub->pool, &sub->event->id_param);
793 }
Benny Prijono834aee32006-02-19 01:38:06 +0000794
795 /* Increment dlg session. */
796 pjsip_dlg_inc_session(sub->dlg, &mod_evsub.mod);
797
798 /* Done */
799 *p_evsub = sub;
800
801on_return:
802 pjsip_dlg_dec_lock(dlg);
803 return status;
804}
805
806
807/*
808 * Create server subscription session from incoming request.
809 */
810PJ_DEF(pj_status_t) pjsip_evsub_create_uas( pjsip_dialog *dlg,
811 const pjsip_evsub_user *user_cb,
812 pjsip_rx_data *rdata,
Benny Prijono26ff9062006-02-21 23:47:00 +0000813 unsigned option,
Benny Prijono834aee32006-02-19 01:38:06 +0000814 pjsip_evsub **p_evsub)
815{
816 pjsip_evsub *sub;
817 pjsip_transaction *tsx;
818 pjsip_accept_hdr *accept_hdr;
819 pjsip_event_hdr *event_hdr;
820 pjsip_expires_hdr *expires_hdr;
821 pj_status_t status;
822
823 /* Check arguments: */
824 PJ_ASSERT_RETURN(dlg && rdata && p_evsub, PJ_EINVAL);
825
826 /* MUST be request message: */
827 PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG,
828 PJSIP_ENOTREQUESTMSG);
829
830 /* Transaction MUST have been created (in the dialog) */
831 tsx = pjsip_rdata_get_tsx(rdata);
832 PJ_ASSERT_RETURN(tsx != NULL, PJSIP_ENOTSX);
833
834 /* No subscription must have been attached to transaction */
835 PJ_ASSERT_RETURN(tsx->mod_data[mod_evsub.mod.id] == NULL,
836 PJSIP_ETYPEEXISTS);
837
838 /* Package MUST implement on_rx_refresh */
839 PJ_ASSERT_RETURN(user_cb->on_rx_refresh, PJ_EINVALIDOP);
840
Benny Prijono26ff9062006-02-21 23:47:00 +0000841 /* Request MUST have "Event" header. We need the Event header to get
842 * the package name (don't want to add more arguments in the function).
843 */
Benny Prijono834aee32006-02-19 01:38:06 +0000844 event_hdr = (pjsip_event_hdr*)
Benny Prijono0c13f3d2008-07-16 22:39:45 +0000845 pjsip_msg_find_hdr_by_names(rdata->msg_info.msg, &STR_EVENT,
846 &STR_EVENT_S, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +0000847 if (event_hdr == NULL) {
848 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST);
849 }
850
851 /* Start locking the mutex: */
852
853 pjsip_dlg_inc_lock(dlg);
854
855 /* Create the session: */
856
857 status = evsub_create(dlg, PJSIP_UAS_ROLE, user_cb,
Benny Prijono26ff9062006-02-21 23:47:00 +0000858 &event_hdr->event_type, option, &sub);
Benny Prijono834aee32006-02-19 01:38:06 +0000859 if (status != PJ_SUCCESS)
860 goto on_return;
861
862 /* Just duplicate Event header from the request */
Benny Prijono9d4469d2007-05-02 05:14:29 +0000863 sub->event = (pjsip_event_hdr*) pjsip_hdr_clone(sub->pool, event_hdr);
Benny Prijono834aee32006-02-19 01:38:06 +0000864
865 /* Set the method: */
866 pjsip_method_copy(sub->pool, &sub->method,
867 &rdata->msg_info.msg->line.req.method);
868
869 /* Update expiration time according to client request: */
870
871 expires_hdr = (pjsip_expires_hdr*)
872 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
873 if (expires_hdr) {
874 sub->expires->ivalue = expires_hdr->ivalue;
875 }
876
877 /* Update time. */
878 update_expires(sub, sub->expires->ivalue);
879
880 /* Update Accept header: */
881
882 accept_hdr = (pjsip_accept_hdr*)
883 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, NULL);
884 if (accept_hdr)
Benny Prijono9d4469d2007-05-02 05:14:29 +0000885 sub->accept = (pjsip_accept_hdr*)pjsip_hdr_clone(sub->pool,accept_hdr);
Benny Prijono834aee32006-02-19 01:38:06 +0000886
887 /* We can start the session: */
888
889 pjsip_dlg_inc_session(dlg, &mod_evsub.mod);
890 sub->pending_tsx++;
891 tsx->mod_data[mod_evsub.mod.id] = sub;
892
893
894 /* Done. */
895 *p_evsub = sub;
896
897
898on_return:
899 pjsip_dlg_dec_lock(dlg);
900 return status;
901}
902
903
904/*
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000905 * Forcefully destroy subscription.
906 */
907PJ_DEF(pj_status_t) pjsip_evsub_terminate( pjsip_evsub *sub,
908 pj_bool_t notify )
909{
910 PJ_ASSERT_RETURN(sub, PJ_EINVAL);
911
912 pjsip_dlg_inc_lock(sub->dlg);
913
Benny Prijonod524e822006-09-22 12:48:18 +0000914 /* I think it's pretty safe to disable this check.
915
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000916 if (sub->pending_tsx) {
917 pj_assert(!"Unable to terminate when there's pending tsx");
918 pjsip_dlg_dec_lock(sub->dlg);
919 return PJ_EINVALIDOP;
920 }
Benny Prijonod524e822006-09-22 12:48:18 +0000921 */
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000922
923 sub->call_cb = notify;
Benny Prijono75130572008-07-17 13:53:41 +0000924 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL, NULL);
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000925
926 pjsip_dlg_dec_lock(sub->dlg);
927 return PJ_SUCCESS;
928}
929
930/*
Benny Prijono834aee32006-02-19 01:38:06 +0000931 * Get subscription state.
932 */
933PJ_DEF(pjsip_evsub_state) pjsip_evsub_get_state(pjsip_evsub *sub)
934{
935 return sub->state;
936}
937
938/*
939 * Get state name.
940 */
941PJ_DEF(const char*) pjsip_evsub_get_state_name(pjsip_evsub *sub)
942{
943 return sub->state_str.ptr;
944}
945
Benny Prijono75130572008-07-17 13:53:41 +0000946/*
947 * Get termination reason.
948 */
949PJ_DEF(const pj_str_t*) pjsip_evsub_get_termination_reason(pjsip_evsub *sub)
950{
951 return &sub->term_reason;
952}
Benny Prijono834aee32006-02-19 01:38:06 +0000953
954/*
955 * Initiate client subscription
956 */
957PJ_DEF(pj_status_t) pjsip_evsub_initiate( pjsip_evsub *sub,
958 const pjsip_method *method,
959 pj_int32_t expires,
960 pjsip_tx_data **p_tdata)
961{
962 pjsip_tx_data *tdata;
963 pj_status_t status;
964
965 PJ_ASSERT_RETURN(sub!=NULL && p_tdata!=NULL, PJ_EINVAL);
966
967 /* Use SUBSCRIBE if method is not specified */
968 if (method == NULL)
969 method = &pjsip_subscribe_method;
970
971 pjsip_dlg_inc_lock(sub->dlg);
972
973 /* Update method: */
974 if (sub->state == PJSIP_EVSUB_STATE_NULL)
975 pjsip_method_copy(sub->pool, &sub->method, method);
976
977 status = pjsip_dlg_create_request( sub->dlg, method, -1, &tdata);
978 if (status != PJ_SUCCESS)
979 goto on_return;
980
981
982 /* Add Event header: */
Benny Prijono9d4469d2007-05-02 05:14:29 +0000983 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +0000984 pjsip_hdr_shallow_clone(tdata->pool, sub->event));
985
986 /* Update and add expires header: */
987 if (expires >= 0)
988 sub->expires->ivalue = expires;
Benny Prijono9d4469d2007-05-02 05:14:29 +0000989 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +0000990 pjsip_hdr_shallow_clone(tdata->pool, sub->expires));
991
992 /* Add Accept header: */
Benny Prijono9d4469d2007-05-02 05:14:29 +0000993 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +0000994 pjsip_hdr_shallow_clone(tdata->pool, sub->accept));
995
996
997 /* Add Allow-Events header: */
Benny Prijono9d4469d2007-05-02 05:14:29 +0000998 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +0000999 pjsip_hdr_shallow_clone(tdata->pool,
1000 mod_evsub.allow_events_hdr));
1001
1002
1003 *p_tdata = tdata;
1004
1005
1006on_return:
1007
1008 pjsip_dlg_dec_lock(sub->dlg);
1009 return status;
1010}
1011
1012
1013/*
1014 * Accept incoming subscription request.
1015 */
1016PJ_DEF(pj_status_t) pjsip_evsub_accept( pjsip_evsub *sub,
1017 pjsip_rx_data *rdata,
1018 int st_code,
1019 const pjsip_hdr *hdr_list )
1020{
1021 pjsip_tx_data *tdata;
1022 pjsip_transaction *tsx;
1023 pj_status_t status;
1024
1025 /* Check arguments */
1026 PJ_ASSERT_RETURN(sub && rdata, PJ_EINVAL);
1027
1028 /* Can only be for server subscription: */
1029 PJ_ASSERT_RETURN(sub->role == PJSIP_ROLE_UAS, PJ_EINVALIDOP);
1030
1031 /* Only expect 2xx status code (for now) */
1032 PJ_ASSERT_RETURN(st_code/100 == 2, PJ_EINVALIDOP);
1033
1034 /* Subscription MUST have been attached to the transaction.
1035 * Initial subscription request will be attached on evsub_create_uas(),
1036 * while subsequent requests will be attached in tsx_state()
1037 */
1038 tsx = pjsip_rdata_get_tsx(rdata);
1039 PJ_ASSERT_RETURN(tsx->mod_data[mod_evsub.mod.id] != NULL,
1040 PJ_EINVALIDOP);
1041
1042 /* Lock dialog */
1043 pjsip_dlg_inc_lock(sub->dlg);
1044
1045 /* Create response: */
1046 status = pjsip_dlg_create_response( sub->dlg, rdata, st_code, NULL,
1047 &tdata);
1048 if (status != PJ_SUCCESS)
1049 goto on_return;
1050
1051
1052 /* Add expires header: */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001053 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +00001054 pjsip_hdr_shallow_clone(tdata->pool, sub->expires));
1055
Benny Prijonob0808372006-03-02 21:18:58 +00001056 /* Add additional header, if any. */
1057 if (hdr_list) {
1058 const pjsip_hdr *hdr = hdr_list->next;
1059 while (hdr != hdr_list) {
Benny Prijono9d4469d2007-05-02 05:14:29 +00001060 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijonob0808372006-03-02 21:18:58 +00001061 pjsip_hdr_clone(tdata->pool, hdr));
1062 hdr = hdr->next;
1063 }
1064 }
Benny Prijono834aee32006-02-19 01:38:06 +00001065
1066 /* Send the response: */
1067 status = pjsip_dlg_send_response( sub->dlg, tsx, tdata );
1068 if (status != PJ_SUCCESS)
1069 goto on_return;
1070
1071
1072on_return:
1073
1074 pjsip_dlg_dec_lock(sub->dlg);
1075 return status;
1076}
1077
1078
1079/*
1080 * Create Subscription-State header based on current server subscription
1081 * state.
1082 */
1083static pjsip_sub_state_hdr* sub_state_create( pj_pool_t *pool,
1084 pjsip_evsub *sub,
1085 pjsip_evsub_state state,
1086 const pj_str_t *state_str,
1087 const pj_str_t *reason )
1088{
1089 pjsip_sub_state_hdr *sub_state;
1090 pj_time_val now, delay;
1091
1092 /* Get the remaining time before refresh is required */
1093 pj_gettimeofday(&now);
1094 delay = sub->refresh_time;
1095 PJ_TIME_VAL_SUB(delay, now);
1096
1097 /* Create the Subscription-State header */
1098 sub_state = pjsip_sub_state_hdr_create(pool);
1099
1100 /* Fill up the header */
1101 switch (state) {
Benny Prijonof80b1bf2006-02-19 02:24:27 +00001102 case PJSIP_EVSUB_STATE_NULL:
Benny Prijono834aee32006-02-19 01:38:06 +00001103 case PJSIP_EVSUB_STATE_SENT:
Benny Prijono834aee32006-02-19 01:38:06 +00001104 pj_assert(!"Invalid state!");
1105 /* Treat as pending */
1106
Benny Prijono75130572008-07-17 13:53:41 +00001107 case PJSIP_EVSUB_STATE_ACCEPTED:
Benny Prijono834aee32006-02-19 01:38:06 +00001108 case PJSIP_EVSUB_STATE_PENDING:
1109 sub_state->sub_state = STR_PENDING;
1110 sub_state->expires_param = delay.sec;
1111 break;
1112
1113 case PJSIP_EVSUB_STATE_ACTIVE:
1114 sub_state->sub_state = STR_ACTIVE;
1115 sub_state->expires_param = delay.sec;
1116 break;
1117
1118 case PJSIP_EVSUB_STATE_TERMINATED:
1119 sub_state->sub_state = STR_TERMINATED;
1120 if (reason != NULL)
1121 pj_strdup(pool, &sub_state->reason_param, reason);
1122 break;
1123
1124 case PJSIP_EVSUB_STATE_UNKNOWN:
1125 pj_assert(state_str != NULL);
1126 pj_strdup(pool, &sub_state->sub_state, state_str);
1127 break;
1128 }
1129
1130 return sub_state;
1131}
1132
1133/*
1134 * Create and send NOTIFY request.
1135 */
1136PJ_DEF(pj_status_t) pjsip_evsub_notify( pjsip_evsub *sub,
1137 pjsip_evsub_state state,
1138 const pj_str_t *state_str,
1139 const pj_str_t *reason,
1140 pjsip_tx_data **p_tdata)
1141{
1142 pjsip_tx_data *tdata;
1143 pjsip_sub_state_hdr *sub_state;
1144 pj_status_t status;
1145
1146 /* Check arguments. */
1147 PJ_ASSERT_RETURN(sub!=NULL && p_tdata!=NULL, PJ_EINVAL);
1148
1149 /* Lock dialog. */
1150 pjsip_dlg_inc_lock(sub->dlg);
1151
1152 /* Create NOTIFY request */
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001153 status = pjsip_dlg_create_request( sub->dlg, pjsip_get_notify_method(),
1154 -1, &tdata);
Benny Prijono834aee32006-02-19 01:38:06 +00001155 if (status != PJ_SUCCESS)
1156 goto on_return;
1157
1158 /* Add Event header */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001159 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +00001160 pjsip_hdr_shallow_clone(tdata->pool, sub->event));
1161
1162 /* Add Subscription-State header */
1163 sub_state = sub_state_create(tdata->pool, sub, state, state_str,
1164 reason);
1165 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)sub_state);
1166
1167 /* Add Allow-Events header */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001168 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +00001169 pjsip_hdr_shallow_clone(tdata->pool, mod_evsub.allow_events_hdr));
1170
1171 /* Add Authentication headers. */
1172 pjsip_auth_clt_init_req( &sub->dlg->auth_sess, tdata );
1173
Benny Prijono75130572008-07-17 13:53:41 +00001174 /* Update reason */
1175 if (reason)
1176 pj_strdup(sub->dlg->pool, &sub->term_reason, reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001177
1178 /* Save destination state. */
1179 sub->dst_state = state;
1180 if (state_str)
1181 pj_strdup(sub->pool, &sub->dst_state_str, state_str);
1182 else
1183 sub->dst_state_str.slen = 0;
1184
1185
1186 *p_tdata = tdata;
1187
1188on_return:
1189 /* Unlock dialog */
1190 pjsip_dlg_dec_lock(sub->dlg);
1191 return status;
1192}
1193
1194
1195/*
1196 * Create NOTIFY to reflect current status.
1197 */
1198PJ_DEF(pj_status_t) pjsip_evsub_current_notify( pjsip_evsub *sub,
1199 pjsip_tx_data **p_tdata )
1200{
1201 return pjsip_evsub_notify( sub, sub->state, &sub->state_str,
1202 NULL, p_tdata );
1203}
1204
1205
1206/*
1207 * Send request.
1208 */
1209PJ_DEF(pj_status_t) pjsip_evsub_send_request( pjsip_evsub *sub,
1210 pjsip_tx_data *tdata)
1211{
1212 pj_status_t status;
1213
1214 /* Must be request message. */
1215 PJ_ASSERT_RETURN(tdata->msg->type == PJSIP_REQUEST_MSG,
1216 PJSIP_ENOTREQUESTMSG);
1217
1218 /* Lock */
1219 pjsip_dlg_inc_lock(sub->dlg);
1220
1221 /* Send the request. */
Benny Prijono64158af2006-04-04 11:06:34 +00001222 status = pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001223 if (status != PJ_SUCCESS)
1224 goto on_return;
1225
1226
1227 /* Special case for NOTIFY:
1228 * The new state was set in pjsip_evsub_notify(), but we apply the
1229 * new state now, when the request was actually sent.
1230 */
1231 if (pjsip_method_cmp(&tdata->msg->line.req.method,
1232 &pjsip_notify_method)==0)
1233 {
1234 PJ_ASSERT_ON_FAIL( sub->dst_state!=PJSIP_EVSUB_STATE_NULL,
1235 {goto on_return;});
1236
1237 set_state(sub, sub->dst_state,
1238 (sub->dst_state_str.slen ? &sub->dst_state_str : NULL),
Benny Prijono75130572008-07-17 13:53:41 +00001239 NULL, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001240
1241 sub->dst_state = PJSIP_EVSUB_STATE_NULL;
1242 sub->dst_state_str.slen = 0;
1243
1244 }
1245
1246
1247on_return:
1248 pjsip_dlg_dec_lock(sub->dlg);
1249 return status;
1250}
1251
1252
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001253/* Callback to be called to terminate transaction. */
1254static void terminate_timer_cb(pj_timer_heap_t *timer_heap,
1255 struct pj_timer_entry *entry)
1256{
1257 pj_str_t *key;
1258 pjsip_transaction *tsx;
1259
1260 PJ_UNUSED_ARG(timer_heap);
1261
1262 key = (pj_str_t*)entry->user_data;
1263 tsx = pjsip_tsx_layer_find_tsx(key, PJ_FALSE);
1264 /* Chance of race condition here */
1265 if (tsx) {
1266 pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_UPDATED);
1267 }
1268}
1269
Benny Prijono834aee32006-02-19 01:38:06 +00001270
1271/*
1272 * Attach subscription session to newly created transaction, if appropriate.
1273 */
1274static pjsip_evsub *on_new_transaction( pjsip_transaction *tsx,
1275 pjsip_event *event)
1276{
1277 /*
1278 * Newly created transaction will not have subscription session
1279 * attached to it. Find the subscription session from the dialog,
1280 * by matching the Event header.
1281 */
1282 pjsip_dialog *dlg;
1283 pjsip_event_hdr *event_hdr;
1284 pjsip_msg *msg;
1285 struct dlgsub *dlgsub_head, *dlgsub;
1286 pjsip_evsub *sub;
1287
1288 dlg = pjsip_tsx_get_dlg(tsx);
1289 if (!dlg) {
1290 pj_assert(!"Transaction should have a dialog instance!");
1291 return NULL;
1292 }
1293
Benny Prijono26ff9062006-02-21 23:47:00 +00001294
Benny Prijono834aee32006-02-19 01:38:06 +00001295 switch (event->body.tsx_state.type) {
1296 case PJSIP_EVENT_RX_MSG:
1297 msg = event->body.tsx_state.src.rdata->msg_info.msg;
1298 break;
1299 case PJSIP_EVENT_TX_MSG:
1300 msg = event->body.tsx_state.src.tdata->msg;
1301 break;
1302 default:
1303 if (tsx->role == PJSIP_ROLE_UAC)
1304 msg = tsx->last_tx->msg;
1305 else
1306 msg = NULL;
1307 break;
1308 }
1309
1310 if (!msg) {
Benny Prijono26ff9062006-02-21 23:47:00 +00001311 //Note:
1312 // this transaction can be other transaction in the dialog.
1313 // The assertion below probably only valid for dialog that
1314 // only has one event subscription usage.
1315 //pj_assert(!"First transaction event is not TX or RX!");
Benny Prijono834aee32006-02-19 01:38:06 +00001316 return NULL;
1317 }
1318
Benny Prijono9d4469d2007-05-02 05:14:29 +00001319 event_hdr = (pjsip_event_hdr*)
Benny Prijono0c13f3d2008-07-16 22:39:45 +00001320 pjsip_msg_find_hdr_by_names(msg, &STR_EVENT,
1321 &STR_EVENT_S, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001322 if (!event_hdr) {
1323 /* Not subscription related message */
1324 return NULL;
1325 }
1326
1327 /* Find the subscription in the dialog, based on the content
1328 * of Event header:
1329 */
1330
Benny Prijono9d4469d2007-05-02 05:14:29 +00001331 dlgsub_head = (struct dlgsub*) dlg->mod_data[mod_evsub.mod.id];
Benny Prijono834aee32006-02-19 01:38:06 +00001332 if (dlgsub_head == NULL) {
Benny Prijono9d4469d2007-05-02 05:14:29 +00001333 dlgsub_head = PJ_POOL_ALLOC_T(dlg->pool, struct dlgsub);
Benny Prijono834aee32006-02-19 01:38:06 +00001334 pj_list_init(dlgsub_head);
1335 dlg->mod_data[mod_evsub.mod.id] = dlgsub_head;
1336 }
1337 dlgsub = dlgsub_head->next;
1338
1339 while (dlgsub != dlgsub_head) {
1340
Benny Prijono26ff9062006-02-21 23:47:00 +00001341 if (pj_stricmp(&dlgsub->sub->event->event_type,
1342 &event_hdr->event_type)==0)
Benny Prijono834aee32006-02-19 01:38:06 +00001343 {
Benny Prijono26ff9062006-02-21 23:47:00 +00001344 /* Event type matched.
1345 * Check if event ID matched too.
1346 */
1347 if (pj_strcmp(&dlgsub->sub->event->id_param,
1348 &event_hdr->id_param)==0)
1349 {
1350
1351 break;
1352
1353 }
1354 /*
1355 * Otherwise if it is an UAC subscription, AND
1356 * PJSIP_EVSUB_NO_EVENT_ID flag is set, AND
1357 * the session's event id is NULL, AND
1358 * the incoming request is NOTIFY with event ID, then
1359 * we consider it as a match, and update the
1360 * session's event id.
1361 */
1362 else if (dlgsub->sub->role == PJSIP_ROLE_UAC &&
1363 (dlgsub->sub->option & PJSIP_EVSUB_NO_EVENT_ID)!=0 &&
1364 dlgsub->sub->event->id_param.slen==0 &&
1365 !pjsip_method_cmp(&tsx->method, &pjsip_notify_method))
1366 {
1367 /* Update session's event id. */
1368 pj_strdup(dlgsub->sub->pool,
1369 &dlgsub->sub->event->id_param,
1370 &event_hdr->id_param);
1371
1372 break;
1373 }
Benny Prijono834aee32006-02-19 01:38:06 +00001374 }
Benny Prijono26ff9062006-02-21 23:47:00 +00001375
1376
1377
Benny Prijono834aee32006-02-19 01:38:06 +00001378 dlgsub = dlgsub->next;
1379 }
1380
1381 if (dlgsub == dlgsub_head) {
1382 /* This could be incoming request to create new subscription */
1383 PJ_LOG(4,(THIS_FILE,
1384 "Subscription not found for %.*s, event=%.*s;id=%.*s",
Benny Prijono26ff9062006-02-21 23:47:00 +00001385 (int)tsx->method.name.slen,
1386 tsx->method.name.ptr,
Benny Prijono834aee32006-02-19 01:38:06 +00001387 (int)event_hdr->event_type.slen,
1388 event_hdr->event_type.ptr,
1389 (int)event_hdr->id_param.slen,
1390 event_hdr->id_param.ptr));
1391
1392 /* If this is an incoming NOTIFY, reject with 481 */
1393 if (tsx->state == PJSIP_TSX_STATE_TRYING &&
1394 pjsip_method_cmp(&tsx->method, &pjsip_notify_method)==0)
1395 {
1396 pj_str_t reason = pj_str("Subscription Does Not Exist");
1397 pjsip_tx_data *tdata;
1398 pj_status_t status;
1399
1400 status = pjsip_dlg_create_response(dlg,
1401 event->body.tsx_state.src.rdata,
1402 481, &reason,
1403 &tdata);
1404 if (status == PJ_SUCCESS) {
1405 status = pjsip_dlg_send_response(dlg, tsx, tdata);
1406 }
1407 }
1408 return NULL;
1409 }
1410
1411 /* Found! */
1412 sub = dlgsub->sub;
1413
1414 /* Attach session to the transaction */
1415 tsx->mod_data[mod_evsub.mod.id] = sub;
1416 sub->pending_tsx++;
1417
Benny Prijono69b98ab2006-03-03 10:23:35 +00001418 /* Special case for outgoing/UAC SUBSCRIBE/REFER transaction.
1419 * We can only have one pending UAC SUBSCRIBE/REFER, so if another
1420 * transaction is started while previous one still alive, terminate
1421 * the older one.
1422 *
1423 * Sample scenario:
1424 * - subscribe sent to destination that doesn't exist, transaction
1425 * is still retransmitting request, then unsubscribe is sent.
1426 */
1427 if (tsx->role == PJSIP_ROLE_UAC &&
1428 tsx->state == PJSIP_TSX_STATE_CALLING &&
Benny Prijono736d0f72006-09-13 22:45:38 +00001429 (pjsip_method_cmp(&tsx->method, &sub->method) == 0 ||
1430 pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method) == 0))
Benny Prijono69b98ab2006-03-03 10:23:35 +00001431 {
1432
1433 if (sub->pending_sub &&
1434 sub->pending_sub->state < PJSIP_TSX_STATE_COMPLETED)
1435 {
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001436 pj_timer_entry *timer;
1437 pj_str_t *key;
1438 pj_time_val timeout = {0, 0};
1439
Benny Prijono69b98ab2006-03-03 10:23:35 +00001440 PJ_LOG(4,(sub->obj_name,
Benny Prijono736d0f72006-09-13 22:45:38 +00001441 "Cancelling pending subscription request"));
Benny Prijono69b98ab2006-03-03 10:23:35 +00001442
1443 /* By convention, we use 490 (Request Updated) status code.
1444 * When transaction handler (below) see this status code, it
1445 * will ignore the transaction.
1446 */
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001447 /* This unfortunately may cause deadlock, because at the moment
1448 * we are holding dialog's mutex. If a response to this
1449 * transaction is in progress in another thread, that thread
1450 * will deadlock when trying to acquire dialog mutex, because
1451 * it is holding the transaction mutex.
1452 *
1453 * So the solution is to register timer to kill this transaction.
1454 */
1455 //pjsip_tsx_terminate(sub->pending_sub, PJSIP_SC_REQUEST_UPDATED);
1456 timer = PJ_POOL_ZALLOC_T(dlg->pool, pj_timer_entry);
1457 key = PJ_POOL_ALLOC_T(dlg->pool, pj_str_t);
1458 pj_strdup(dlg->pool, key, &sub->pending_sub->transaction_key);
1459 timer->cb = &terminate_timer_cb;
1460 timer->user_data = key;
1461
1462 pjsip_endpt_schedule_timer(dlg->endpt, timer, &timeout);
Benny Prijono69b98ab2006-03-03 10:23:35 +00001463 }
1464
1465 sub->pending_sub = tsx;
1466
Benny Prijono69b98ab2006-03-03 10:23:35 +00001467 }
1468
Benny Prijono834aee32006-02-19 01:38:06 +00001469 return sub;
1470}
1471
1472
1473/*
1474 * Create response, adding custome headers and msg body.
1475 */
1476static pj_status_t create_response( pjsip_evsub *sub,
1477 pjsip_rx_data *rdata,
1478 int st_code,
1479 const pj_str_t *st_text,
1480 const pjsip_hdr *res_hdr,
1481 const pjsip_msg_body *body,
1482 pjsip_tx_data **p_tdata)
1483{
1484 pjsip_tx_data *tdata;
1485 pjsip_hdr *hdr;
1486 pj_status_t status;
1487
1488 status = pjsip_dlg_create_response(sub->dlg, rdata,
1489 st_code, st_text, &tdata);
1490 if (status != PJ_SUCCESS)
1491 return status;
1492
1493 *p_tdata = tdata;
1494
1495 /* Add response headers. */
1496 hdr = res_hdr->next;
1497 while (hdr != res_hdr) {
Benny Prijono9d4469d2007-05-02 05:14:29 +00001498 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +00001499 pjsip_hdr_clone(tdata->pool, hdr));
1500 hdr = hdr->next;
1501 }
1502
1503 /* Add msg body, if any */
1504 if (body) {
Benny Prijonob0808372006-03-02 21:18:58 +00001505 tdata->msg->body = pjsip_msg_body_clone(tdata->pool, body);
1506 if (tdata->msg->body == NULL) {
1507
1508 PJ_LOG(4,(THIS_FILE, "Error: unable to clone msg body"));
1509
Benny Prijono834aee32006-02-19 01:38:06 +00001510 /* Ignore */
1511 return PJ_SUCCESS;
1512 }
1513 }
1514
1515 return PJ_SUCCESS;
1516}
1517
1518/*
1519 * Get subscription state from the value of Subscription-State header.
1520 */
1521static void get_hdr_state( pjsip_sub_state_hdr *sub_state,
1522 pjsip_evsub_state *state,
1523 pj_str_t **state_str )
1524{
1525 if (pj_stricmp(&sub_state->sub_state, &STR_TERMINATED)==0) {
1526
1527 *state = PJSIP_EVSUB_STATE_TERMINATED;
1528 *state_str = NULL;
1529
1530 } else if (pj_stricmp(&sub_state->sub_state, &STR_ACTIVE)==0) {
1531
1532 *state = PJSIP_EVSUB_STATE_ACTIVE;
1533 *state_str = NULL;
1534
1535 } else if (pj_stricmp(&sub_state->sub_state, &STR_PENDING)==0) {
1536
1537 *state = PJSIP_EVSUB_STATE_PENDING;
1538 *state_str = NULL;
1539
1540 } else {
1541
1542 *state = PJSIP_EVSUB_STATE_UNKNOWN;
1543 *state_str = &sub_state->sub_state;
1544
1545 }
1546}
1547
1548/*
1549 * Transaction event processing by UAC, after subscription is sent.
1550 */
1551static void on_tsx_state_uac( pjsip_evsub *sub, pjsip_transaction *tsx,
1552 pjsip_event *event )
1553{
1554
Benny Prijono736d0f72006-09-13 22:45:38 +00001555 if (pjsip_method_cmp(&tsx->method, &sub->method)==0 ||
1556 pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method)==0)
1557 {
Benny Prijono834aee32006-02-19 01:38:06 +00001558
1559 /* Received response to outgoing request that establishes/refresh
1560 * subscription.
1561 */
1562
1563 /* First time initial request is sent. */
1564 if (sub->state == PJSIP_EVSUB_STATE_NULL &&
1565 tsx->state == PJSIP_TSX_STATE_CALLING)
1566 {
Benny Prijono75130572008-07-17 13:53:41 +00001567 set_state(sub, PJSIP_EVSUB_STATE_SENT, NULL, event, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001568 return;
1569 }
1570
1571 /* Only interested in final response */
1572 if (tsx->state != PJSIP_TSX_STATE_COMPLETED &&
1573 tsx->state != PJSIP_TSX_STATE_TERMINATED)
1574 {
1575 return;
1576 }
1577
Benny Prijono1d8d6082006-04-29 12:38:25 +00001578 /* Clear pending subscription */
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001579 if (tsx == sub->pending_sub) {
Benny Prijono1d8d6082006-04-29 12:38:25 +00001580 sub->pending_sub = NULL;
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001581 } else if (sub->pending_sub != NULL) {
1582 /* This SUBSCRIBE transaction has been "renewed" with another
1583 * SUBSCRIBE, so we can just ignore this. For example, user
1584 * sent SUBSCRIBE followed immediately with UN-SUBSCRIBE.
1585 */
1586 return;
1587 }
Benny Prijono1d8d6082006-04-29 12:38:25 +00001588
Benny Prijono834aee32006-02-19 01:38:06 +00001589 /* Handle authentication. */
1590 if (tsx->status_code==401 || tsx->status_code==407) {
1591 pjsip_tx_data *tdata;
1592 pj_status_t status;
1593
1594 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
1595 /* Previously failed transaction has terminated */
1596 return;
1597 }
1598
1599 status = pjsip_auth_clt_reinit_req(&sub->dlg->auth_sess,
1600 event->body.tsx_state.src.rdata,
1601 tsx->last_tx, &tdata);
1602 if (status == PJ_SUCCESS)
Benny Prijono64158af2006-04-04 11:06:34 +00001603 status = pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001604
1605 if (status != PJ_SUCCESS) {
1606 /* Authentication failed! */
1607 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED,
Benny Prijono75130572008-07-17 13:53:41 +00001608 NULL, event, &tsx->status_text);
Benny Prijono834aee32006-02-19 01:38:06 +00001609 return;
1610 }
1611
1612 return;
1613 }
1614
1615 if (tsx->status_code/100 == 2) {
1616
1617 /* Successfull SUBSCRIBE request!
1618 * This could be:
1619 * - response to initial SUBSCRIBE request
1620 * - response to subsequent refresh
1621 * - response to unsubscription
1622 */
1623
1624 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
1625 /* Ignore; this transaction has been processed before */
1626 return;
1627 }
1628
1629 /* Update UAC refresh time, if response contains Expires header,
1630 * only when we're not unsubscribing.
1631 */
1632 if (sub->expires->ivalue != 0) {
1633 pjsip_msg *msg;
1634 pjsip_expires_hdr *expires;
1635
1636 msg = event->body.tsx_state.src.rdata->msg_info.msg;
Benny Prijono9d4469d2007-05-02 05:14:29 +00001637 expires = (pjsip_expires_hdr*)
1638 pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001639 if (expires) {
1640 sub->expires->ivalue = expires->ivalue;
1641 }
1642 }
1643
1644 /* Update time */
1645 update_expires(sub, sub->expires->ivalue);
1646
1647 /* Start UAC refresh timer, only when we're not unsubscribing */
1648 if (sub->expires->ivalue != 0) {
1649 unsigned timeout = (sub->expires->ivalue > TIME_UAC_REFRESH) ?
1650 sub->expires->ivalue - TIME_UAC_REFRESH : sub->expires->ivalue;
1651
1652 PJ_LOG(5,(sub->obj_name, "Will refresh in %d seconds",
1653 timeout));
1654 set_timer(sub, TIMER_TYPE_UAC_REFRESH, timeout);
1655
1656 } else {
1657 /* Otherwise set timer to terminate client subscription when
1658 * NOTIFY to end subscription is not received.
1659 */
1660 set_timer(sub, TIMER_TYPE_UAC_TERMINATE, TIME_UAC_TERMINATE);
1661 }
1662
1663 /* Set state, if necessary */
1664 pj_assert(sub->state != PJSIP_EVSUB_STATE_NULL);
1665 if (sub->state == PJSIP_EVSUB_STATE_SENT) {
Benny Prijono75130572008-07-17 13:53:41 +00001666 set_state(sub, PJSIP_EVSUB_STATE_ACCEPTED, NULL, event, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001667 }
1668
1669 } else {
1670
1671 /* Failed SUBSCRIBE request!
1672 *
1673 * The RFC 3265 says that if outgoing SUBSCRIBE fails with status
1674 * other than 481, the subscription is still considered valid for
1675 * the duration of the last Expires.
1676 *
1677 * Since we send refresh about 5 seconds (TIME_UAC_REFRESH) before
1678 * expiration, theoritically the expiration is still valid for the
1679 * next 5 seconds even when we receive non-481 failed response.
1680 *
1681 * Ah, what the heck!
1682 *
1683 * Just terminate now!
1684 *
1685 */
1686
1687 if (sub->state == PJSIP_EVSUB_STATE_TERMINATED) {
1688 /* Ignore, has been handled before */
1689 return;
1690 }
1691
Benny Prijono69b98ab2006-03-03 10:23:35 +00001692 /* Ignore 490 (Request Updated) status.
1693 * This happens when application sends SUBSCRIBE/REFER while
1694 * another one is still in progress.
1695 */
1696 if (tsx->status_code == PJSIP_SC_REQUEST_UPDATED) {
1697 return;
1698 }
1699
Benny Prijono834aee32006-02-19 01:38:06 +00001700 /* Kill any timer. */
1701 set_timer(sub, TIMER_TYPE_NONE, 0);
1702
1703 /* Set state to TERMINATED */
1704 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED,
Benny Prijono75130572008-07-17 13:53:41 +00001705 NULL, event, &tsx->status_text);
Benny Prijono834aee32006-02-19 01:38:06 +00001706
1707 }
1708
1709 } else if (pjsip_method_cmp(&tsx->method, &pjsip_notify_method) == 0) {
1710
1711 /* Incoming NOTIFY.
1712 * This can be the result of:
1713 * - Initial subscription response
1714 * - UAS updating the resource info.
1715 * - Unsubscription response.
1716 */
1717 int st_code = 200;
1718 pj_str_t *st_text = NULL;
1719 pjsip_hdr res_hdr;
1720 pjsip_msg_body *body = NULL;
1721
1722 pjsip_rx_data *rdata;
1723 pjsip_msg *msg;
1724 pjsip_sub_state_hdr *sub_state;
1725
1726 pjsip_evsub_state new_state;
1727 pj_str_t *new_state_str;
1728
1729 pjsip_tx_data *tdata;
1730 pj_status_t status;
1731 int next_refresh;
1732
1733 /* Only want to handle initial NOTIFY receive event. */
1734 if (tsx->state != PJSIP_TSX_STATE_TRYING)
1735 return;
1736
1737
1738 rdata = event->body.tsx_state.src.rdata;
1739 msg = rdata->msg_info.msg;
1740
1741 pj_list_init(&res_hdr);
1742
1743 /* Get subscription state header. */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001744 sub_state = (pjsip_sub_state_hdr*)
1745 pjsip_msg_find_hdr_by_name(msg, &STR_SUB_STATE, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001746 if (sub_state == NULL) {
1747
1748 pjsip_warning_hdr *warn_hdr;
1749 pj_str_t warn_text = { "Missing Subscription-State header", 33};
1750
1751 /* Bad request! Add warning header. */
1752 st_code = PJSIP_SC_BAD_REQUEST;
1753 warn_hdr = pjsip_warning_hdr_create(rdata->tp_info.pool, 399,
1754 pjsip_endpt_name(sub->endpt),
1755 &warn_text);
1756 pj_list_push_back(&res_hdr, warn_hdr);
1757 }
1758
1759 /* Call application registered callback to handle incoming NOTIFY,
1760 * if any.
1761 */
Benny Prijonod4e0abd2006-03-05 11:53:36 +00001762 if (st_code==200 && sub->user.on_rx_notify && sub->call_cb) {
Benny Prijono834aee32006-02-19 01:38:06 +00001763 (*sub->user.on_rx_notify)(sub, rdata, &st_code, &st_text,
1764 &res_hdr, &body);
1765
1766 /* Application MUST specify final response! */
1767 PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; });
1768
1769 /* Must be a valid status code */
1770 PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; });
1771 }
1772
1773
1774 /* If non-2xx should be returned, then send the response.
1775 * No need to update server subscription state.
1776 */
1777 if (st_code >= 300) {
1778 status = create_response(sub, rdata, st_code, st_text, &res_hdr,
1779 body, &tdata);
1780 if (status == PJ_SUCCESS) {
1781 status = pjsip_dlg_send_response(sub->dlg, tsx, tdata);
1782 }
1783
1784 /* Start timer to terminate subscription, just in case server
1785 * is not able to generate NOTIFY to our response.
1786 */
1787 if (status == PJ_SUCCESS) {
1788 unsigned timeout = TIME_UAC_WAIT_NOTIFY;
1789 set_timer(sub, TIMER_TYPE_UAC_WAIT_NOTIFY, timeout);
1790 } else {
Benny Prijono75130572008-07-17 13:53:41 +00001791 char errmsg[PJ_ERR_MSG_SIZE];
1792 pj_str_t reason;
1793
1794 reason = pj_strerror(status, errmsg, sizeof(errmsg));
1795 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL,
1796 &reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001797 }
1798
1799 return;
1800 }
1801
1802 /* Update expiration from the value of expires param in
1803 * Subscription-State header, but ONLY when subscription state
1804 * is "active" or "pending", AND the header contains expires param.
1805 */
1806 if (sub->expires->ivalue != 0 &&
1807 sub_state->expires_param >= 0 &&
1808 (pj_stricmp(&sub_state->sub_state, &STR_ACTIVE)==0 ||
1809 pj_stricmp(&sub_state->sub_state, &STR_PENDING)==0))
1810 {
1811 next_refresh = sub_state->expires_param;
1812
1813 } else {
1814 next_refresh = sub->expires->ivalue;
1815 }
1816
1817 /* Update time */
1818 update_expires(sub, next_refresh);
1819
1820 /* Start UAC refresh timer, only when we're not unsubscribing */
1821 if (sub->expires->ivalue != 0) {
1822 unsigned timeout = (next_refresh > TIME_UAC_REFRESH) ?
1823 next_refresh - TIME_UAC_REFRESH : next_refresh;
1824
1825 PJ_LOG(5,(sub->obj_name, "Will refresh in %d seconds", timeout));
1826 set_timer(sub, TIMER_TYPE_UAC_REFRESH, timeout);
1827 }
1828
1829 /* Find out the state */
1830 get_hdr_state(sub_state, &new_state, &new_state_str);
1831
1832 /* Send response. */
1833 status = create_response(sub, rdata, st_code, st_text, &res_hdr,
1834 body, &tdata);
1835 if (status == PJ_SUCCESS)
1836 status = pjsip_dlg_send_response(sub->dlg, tsx, tdata);
1837
1838 /* Set the state */
1839 if (status == PJ_SUCCESS) {
Benny Prijono75130572008-07-17 13:53:41 +00001840 set_state(sub, new_state, new_state_str, event,
1841 &sub_state->reason_param);
Benny Prijono834aee32006-02-19 01:38:06 +00001842 } else {
Benny Prijono75130572008-07-17 13:53:41 +00001843 char errmsg[PJ_ERR_MSG_SIZE];
1844 pj_str_t reason;
1845
1846 reason = pj_strerror(status, errmsg, sizeof(errmsg));
1847 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event,
1848 &reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001849 }
1850
1851
1852 } else {
1853
1854 /*
1855 * Unexpected method!
1856 */
1857 PJ_LOG(4,(sub->obj_name, "Unexpected transaction method %.*s",
1858 (int)tsx->method.name.slen, tsx->method.name.ptr));
1859 }
1860}
1861
1862
1863/*
1864 * Transaction event processing by UAS, after subscription is accepted.
1865 */
1866static void on_tsx_state_uas( pjsip_evsub *sub, pjsip_transaction *tsx,
1867 pjsip_event *event)
1868{
1869
Benny Prijono736d0f72006-09-13 22:45:38 +00001870 if (pjsip_method_cmp(&tsx->method, &sub->method) == 0 ||
1871 pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method) == 0)
1872 {
Benny Prijono834aee32006-02-19 01:38:06 +00001873
1874 /*
1875 * Incoming request (e.g. SUBSCRIBE or REFER) to refresh subsciption.
1876 *
1877 */
1878 pjsip_rx_data *rdata;
1879 pjsip_event_hdr *event_hdr;
1880 pjsip_expires_hdr *expires;
1881 pjsip_msg *msg;
1882 pjsip_tx_data *tdata;
1883 int st_code = 200;
1884 pj_str_t *st_text = NULL;
1885 pjsip_hdr res_hdr;
1886 pjsip_msg_body *body = NULL;
1887 pjsip_evsub_state old_state;
1888 pj_str_t old_state_str;
Benny Prijono75130572008-07-17 13:53:41 +00001889 pj_str_t reason = { NULL, 0 };
Benny Prijono834aee32006-02-19 01:38:06 +00001890 pj_status_t status;
1891
1892
1893 /* Only wants to handle the first event when the request is
1894 * received.
1895 */
1896 if (tsx->state != PJSIP_TSX_STATE_TRYING)
1897 return;
1898
1899 rdata = event->body.tsx_state.src.rdata;
1900 msg = rdata->msg_info.msg;
1901
1902 /* Set expiration time based on client request (in Expires header),
1903 * or package default expiration time.
1904 */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001905 event_hdr = (pjsip_event_hdr*)
Benny Prijono0c13f3d2008-07-16 22:39:45 +00001906 pjsip_msg_find_hdr_by_names(msg, &STR_EVENT,
1907 &STR_EVENT, NULL);
Benny Prijono9d4469d2007-05-02 05:14:29 +00001908 expires = (pjsip_expires_hdr*)
1909 pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001910 if (event_hdr && expires) {
1911 struct evpkg *evpkg;
1912
1913 evpkg = find_pkg(&event_hdr->event_type);
1914 if (evpkg) {
1915 if (expires->ivalue < (pj_int32_t)evpkg->pkg_expires)
1916 sub->expires->ivalue = expires->ivalue;
1917 else
1918 sub->expires->ivalue = evpkg->pkg_expires;
1919 }
1920 }
1921
1922 /* Update time (before calling on_rx_refresh, since application
1923 * will send NOTIFY.
1924 */
1925 update_expires(sub, sub->expires->ivalue);
1926
1927
1928 /* Save old state.
1929 * If application respond with non-2xx, revert to old state.
1930 */
1931 old_state = sub->state;
1932 old_state_str = sub->state_str;
1933
1934 if (sub->expires->ivalue == 0) {
1935 sub->state = PJSIP_EVSUB_STATE_TERMINATED;
1936 sub->state_str = evsub_state_names[sub->state];
1937 } else if (sub->state == PJSIP_EVSUB_STATE_NULL) {
1938 sub->state = PJSIP_EVSUB_STATE_ACCEPTED;
1939 sub->state_str = evsub_state_names[sub->state];
1940 }
1941
1942 /* Call application's on_rx_refresh, just in case it wants to send
1943 * response other than 200 (OK)
1944 */
1945 pj_list_init(&res_hdr);
1946
Benny Prijonod4e0abd2006-03-05 11:53:36 +00001947 if (sub->user.on_rx_refresh && sub->call_cb) {
1948 (*sub->user.on_rx_refresh)(sub, rdata, &st_code, &st_text,
1949 &res_hdr, &body);
1950 }
Benny Prijono834aee32006-02-19 01:38:06 +00001951
1952 /* Application MUST specify final response! */
1953 PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; });
1954
1955 /* Must be a valid status code */
1956 PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; });
1957
1958
1959 /* Create and send response */
1960 status = create_response(sub, rdata, st_code, st_text, &res_hdr,
1961 body, &tdata);
1962 if (status == PJ_SUCCESS) {
1963 /* Add expires header: */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001964 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +00001965 pjsip_hdr_shallow_clone(tdata->pool,
1966 sub->expires));
1967
1968 /* Send */
1969 status = pjsip_dlg_send_response(sub->dlg, tsx, tdata);
1970 }
1971
1972 /* Update state or revert state */
1973 if (st_code/100==2) {
1974
1975 if (sub->expires->ivalue == 0) {
Benny Prijono75130572008-07-17 13:53:41 +00001976 set_state(sub, sub->state, NULL, event, &reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001977 } else if (sub->state == PJSIP_EVSUB_STATE_NULL) {
Benny Prijono75130572008-07-17 13:53:41 +00001978 set_state(sub, sub->state, NULL, event, &reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001979 }
1980
1981 /* Set UAS timeout timer, when state is not terminated. */
1982 if (sub->state != PJSIP_EVSUB_STATE_TERMINATED) {
1983 PJ_LOG(5,(sub->obj_name, "UAS timeout in %d seconds",
1984 sub->expires->ivalue));
1985 set_timer(sub, TIMER_TYPE_UAS_TIMEOUT,
1986 sub->expires->ivalue);
1987 }
1988
1989 } else {
1990 sub->state = old_state;
1991 sub->state_str = old_state_str;
1992 }
1993
1994
1995 } else if (pjsip_method_cmp(&tsx->method, &pjsip_notify_method)==0) {
1996
1997 /* Handle authentication */
1998 if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
1999 (tsx->status_code==401 || tsx->status_code==407))
2000 {
2001 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
2002 pjsip_tx_data *tdata;
2003 pj_status_t status;
2004
2005 status = pjsip_auth_clt_reinit_req( &sub->dlg->auth_sess, rdata,
2006 tsx->last_tx, &tdata);
2007 if (status == PJ_SUCCESS)
Benny Prijono64158af2006-04-04 11:06:34 +00002008 status = pjsip_dlg_send_request( sub->dlg, tdata, -1, NULL );
Benny Prijono834aee32006-02-19 01:38:06 +00002009
2010 if (status != PJ_SUCCESS) {
2011 /* Can't authenticate. Terminate session (?) */
Benny Prijono75130572008-07-17 13:53:41 +00002012 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL,
2013 &tsx->status_text);
Benny Prijono441ce002006-03-07 15:15:01 +00002014 return;
Benny Prijono834aee32006-02-19 01:38:06 +00002015 }
Benny Prijono26ff9062006-02-21 23:47:00 +00002016
2017 }
2018 /*
2019 * Terminate event usage if we receive 481, 408, and 7 class
2020 * responses.
2021 */
2022 if (sub->state != PJSIP_EVSUB_STATE_TERMINATED &&
2023 (tsx->status_code==481 || tsx->status_code==408 ||
2024 tsx->status_code/100 == 7))
2025 {
Benny Prijono75130572008-07-17 13:53:41 +00002026 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event,
2027 &tsx->status_text);
Benny Prijono441ce002006-03-07 15:15:01 +00002028 return;
Benny Prijono26ff9062006-02-21 23:47:00 +00002029 }
Benny Prijono834aee32006-02-19 01:38:06 +00002030
2031 } else {
2032
2033 /*
2034 * Unexpected method!
2035 */
2036 PJ_LOG(4,(sub->obj_name, "Unexpected transaction method %.*s",
2037 (int)tsx->method.name.slen, tsx->method.name.ptr));
2038
2039 }
2040}
2041
2042
2043/*
2044 * Notification when transaction state has changed!
2045 */
2046static void mod_evsub_on_tsx_state(pjsip_transaction *tsx, pjsip_event *event)
2047{
2048 pjsip_evsub *sub = pjsip_tsx_get_evsub(tsx);
2049
2050 if (sub == NULL) {
2051 sub = on_new_transaction(tsx, event);
2052 if (sub == NULL)
2053 return;
2054 }
2055
2056
2057 /* Call on_tsx_state callback, if any. */
Benny Prijonod4e0abd2006-03-05 11:53:36 +00002058 if (sub->user.on_tsx_state && sub->call_cb)
Benny Prijono834aee32006-02-19 01:38:06 +00002059 (*sub->user.on_tsx_state)(sub, tsx, event);
2060
2061
2062 /* Process the event: */
2063
2064 if (sub->role == PJSIP_ROLE_UAC) {
2065 on_tsx_state_uac(sub, tsx, event);
2066 } else {
2067 on_tsx_state_uas(sub, tsx, event);
2068 }
2069
2070
2071 /* Check transaction TERMINATE event */
2072 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
2073
2074 --sub->pending_tsx;
2075
2076 if (sub->state == PJSIP_EVSUB_STATE_TERMINATED &&
2077 sub->pending_tsx == 0)
2078 {
2079 evsub_destroy(sub);
2080 }
2081
2082 }
2083}
2084
2085