blob: 7acd0badab5b7f06274ff0c9bddfd619e60318e9 [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
Benny Prijonoce00fa02009-06-30 13:47:44 +00001381 /* Note:
1382 * the second condition is for http://trac.pjsip.org/repos/ticket/911
1383 */
1384 if (dlgsub == dlgsub_head ||
1385 (dlgsub->sub &&
1386 pjsip_evsub_get_state(dlgsub->sub)==PJSIP_EVSUB_STATE_TERMINATED))
1387 {
1388 const char *reason_msg =
1389 (dlgsub == dlgsub_head ? "Subscription Does Not Exist" :
1390 "Subscription already terminated");
1391
Benny Prijono834aee32006-02-19 01:38:06 +00001392 /* This could be incoming request to create new subscription */
1393 PJ_LOG(4,(THIS_FILE,
Benny Prijonoce00fa02009-06-30 13:47:44 +00001394 "%s for %.*s, event=%.*s;id=%.*s",
1395 reason_msg,
Benny Prijono26ff9062006-02-21 23:47:00 +00001396 (int)tsx->method.name.slen,
1397 tsx->method.name.ptr,
Benny Prijono834aee32006-02-19 01:38:06 +00001398 (int)event_hdr->event_type.slen,
1399 event_hdr->event_type.ptr,
1400 (int)event_hdr->id_param.slen,
1401 event_hdr->id_param.ptr));
1402
1403 /* If this is an incoming NOTIFY, reject with 481 */
1404 if (tsx->state == PJSIP_TSX_STATE_TRYING &&
1405 pjsip_method_cmp(&tsx->method, &pjsip_notify_method)==0)
1406 {
Benny Prijonoce00fa02009-06-30 13:47:44 +00001407 pj_str_t reason;
Benny Prijono834aee32006-02-19 01:38:06 +00001408 pjsip_tx_data *tdata;
1409 pj_status_t status;
1410
Benny Prijonoce00fa02009-06-30 13:47:44 +00001411 pj_cstr(&reason, reason_msg);
Benny Prijono834aee32006-02-19 01:38:06 +00001412 status = pjsip_dlg_create_response(dlg,
1413 event->body.tsx_state.src.rdata,
1414 481, &reason,
1415 &tdata);
1416 if (status == PJ_SUCCESS) {
1417 status = pjsip_dlg_send_response(dlg, tsx, tdata);
1418 }
1419 }
1420 return NULL;
1421 }
1422
1423 /* Found! */
1424 sub = dlgsub->sub;
1425
1426 /* Attach session to the transaction */
1427 tsx->mod_data[mod_evsub.mod.id] = sub;
1428 sub->pending_tsx++;
1429
Benny Prijono69b98ab2006-03-03 10:23:35 +00001430 /* Special case for outgoing/UAC SUBSCRIBE/REFER transaction.
1431 * We can only have one pending UAC SUBSCRIBE/REFER, so if another
1432 * transaction is started while previous one still alive, terminate
1433 * the older one.
1434 *
1435 * Sample scenario:
1436 * - subscribe sent to destination that doesn't exist, transaction
1437 * is still retransmitting request, then unsubscribe is sent.
1438 */
1439 if (tsx->role == PJSIP_ROLE_UAC &&
1440 tsx->state == PJSIP_TSX_STATE_CALLING &&
Benny Prijono736d0f72006-09-13 22:45:38 +00001441 (pjsip_method_cmp(&tsx->method, &sub->method) == 0 ||
1442 pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method) == 0))
Benny Prijono69b98ab2006-03-03 10:23:35 +00001443 {
1444
1445 if (sub->pending_sub &&
1446 sub->pending_sub->state < PJSIP_TSX_STATE_COMPLETED)
1447 {
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001448 pj_timer_entry *timer;
1449 pj_str_t *key;
1450 pj_time_val timeout = {0, 0};
1451
Benny Prijono69b98ab2006-03-03 10:23:35 +00001452 PJ_LOG(4,(sub->obj_name,
Benny Prijono736d0f72006-09-13 22:45:38 +00001453 "Cancelling pending subscription request"));
Benny Prijono69b98ab2006-03-03 10:23:35 +00001454
1455 /* By convention, we use 490 (Request Updated) status code.
1456 * When transaction handler (below) see this status code, it
1457 * will ignore the transaction.
1458 */
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001459 /* This unfortunately may cause deadlock, because at the moment
1460 * we are holding dialog's mutex. If a response to this
1461 * transaction is in progress in another thread, that thread
1462 * will deadlock when trying to acquire dialog mutex, because
1463 * it is holding the transaction mutex.
1464 *
1465 * So the solution is to register timer to kill this transaction.
1466 */
1467 //pjsip_tsx_terminate(sub->pending_sub, PJSIP_SC_REQUEST_UPDATED);
1468 timer = PJ_POOL_ZALLOC_T(dlg->pool, pj_timer_entry);
1469 key = PJ_POOL_ALLOC_T(dlg->pool, pj_str_t);
1470 pj_strdup(dlg->pool, key, &sub->pending_sub->transaction_key);
1471 timer->cb = &terminate_timer_cb;
1472 timer->user_data = key;
1473
1474 pjsip_endpt_schedule_timer(dlg->endpt, timer, &timeout);
Benny Prijono69b98ab2006-03-03 10:23:35 +00001475 }
1476
1477 sub->pending_sub = tsx;
1478
Benny Prijono69b98ab2006-03-03 10:23:35 +00001479 }
1480
Benny Prijono834aee32006-02-19 01:38:06 +00001481 return sub;
1482}
1483
1484
1485/*
1486 * Create response, adding custome headers and msg body.
1487 */
1488static pj_status_t create_response( pjsip_evsub *sub,
1489 pjsip_rx_data *rdata,
1490 int st_code,
1491 const pj_str_t *st_text,
1492 const pjsip_hdr *res_hdr,
1493 const pjsip_msg_body *body,
1494 pjsip_tx_data **p_tdata)
1495{
1496 pjsip_tx_data *tdata;
1497 pjsip_hdr *hdr;
1498 pj_status_t status;
1499
1500 status = pjsip_dlg_create_response(sub->dlg, rdata,
1501 st_code, st_text, &tdata);
1502 if (status != PJ_SUCCESS)
1503 return status;
1504
1505 *p_tdata = tdata;
1506
1507 /* Add response headers. */
1508 hdr = res_hdr->next;
1509 while (hdr != res_hdr) {
Benny Prijono9d4469d2007-05-02 05:14:29 +00001510 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +00001511 pjsip_hdr_clone(tdata->pool, hdr));
1512 hdr = hdr->next;
1513 }
1514
1515 /* Add msg body, if any */
1516 if (body) {
Benny Prijonob0808372006-03-02 21:18:58 +00001517 tdata->msg->body = pjsip_msg_body_clone(tdata->pool, body);
1518 if (tdata->msg->body == NULL) {
1519
1520 PJ_LOG(4,(THIS_FILE, "Error: unable to clone msg body"));
1521
Benny Prijono834aee32006-02-19 01:38:06 +00001522 /* Ignore */
1523 return PJ_SUCCESS;
1524 }
1525 }
1526
1527 return PJ_SUCCESS;
1528}
1529
1530/*
1531 * Get subscription state from the value of Subscription-State header.
1532 */
1533static void get_hdr_state( pjsip_sub_state_hdr *sub_state,
1534 pjsip_evsub_state *state,
1535 pj_str_t **state_str )
1536{
1537 if (pj_stricmp(&sub_state->sub_state, &STR_TERMINATED)==0) {
1538
1539 *state = PJSIP_EVSUB_STATE_TERMINATED;
1540 *state_str = NULL;
1541
1542 } else if (pj_stricmp(&sub_state->sub_state, &STR_ACTIVE)==0) {
1543
1544 *state = PJSIP_EVSUB_STATE_ACTIVE;
1545 *state_str = NULL;
1546
1547 } else if (pj_stricmp(&sub_state->sub_state, &STR_PENDING)==0) {
1548
1549 *state = PJSIP_EVSUB_STATE_PENDING;
1550 *state_str = NULL;
1551
1552 } else {
1553
1554 *state = PJSIP_EVSUB_STATE_UNKNOWN;
1555 *state_str = &sub_state->sub_state;
1556
1557 }
1558}
1559
1560/*
1561 * Transaction event processing by UAC, after subscription is sent.
1562 */
1563static void on_tsx_state_uac( pjsip_evsub *sub, pjsip_transaction *tsx,
1564 pjsip_event *event )
1565{
1566
Benny Prijono736d0f72006-09-13 22:45:38 +00001567 if (pjsip_method_cmp(&tsx->method, &sub->method)==0 ||
1568 pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method)==0)
1569 {
Benny Prijono834aee32006-02-19 01:38:06 +00001570
1571 /* Received response to outgoing request that establishes/refresh
1572 * subscription.
1573 */
1574
1575 /* First time initial request is sent. */
1576 if (sub->state == PJSIP_EVSUB_STATE_NULL &&
1577 tsx->state == PJSIP_TSX_STATE_CALLING)
1578 {
Benny Prijono75130572008-07-17 13:53:41 +00001579 set_state(sub, PJSIP_EVSUB_STATE_SENT, NULL, event, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001580 return;
1581 }
1582
1583 /* Only interested in final response */
1584 if (tsx->state != PJSIP_TSX_STATE_COMPLETED &&
1585 tsx->state != PJSIP_TSX_STATE_TERMINATED)
1586 {
1587 return;
1588 }
1589
Benny Prijono1d8d6082006-04-29 12:38:25 +00001590 /* Clear pending subscription */
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001591 if (tsx == sub->pending_sub) {
Benny Prijono1d8d6082006-04-29 12:38:25 +00001592 sub->pending_sub = NULL;
Benny Prijonoefb9b6b2007-06-27 12:52:35 +00001593 } else if (sub->pending_sub != NULL) {
1594 /* This SUBSCRIBE transaction has been "renewed" with another
1595 * SUBSCRIBE, so we can just ignore this. For example, user
1596 * sent SUBSCRIBE followed immediately with UN-SUBSCRIBE.
1597 */
1598 return;
1599 }
Benny Prijono1d8d6082006-04-29 12:38:25 +00001600
Benny Prijono834aee32006-02-19 01:38:06 +00001601 /* Handle authentication. */
1602 if (tsx->status_code==401 || tsx->status_code==407) {
1603 pjsip_tx_data *tdata;
1604 pj_status_t status;
1605
1606 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
1607 /* Previously failed transaction has terminated */
1608 return;
1609 }
1610
1611 status = pjsip_auth_clt_reinit_req(&sub->dlg->auth_sess,
1612 event->body.tsx_state.src.rdata,
1613 tsx->last_tx, &tdata);
1614 if (status == PJ_SUCCESS)
Benny Prijono64158af2006-04-04 11:06:34 +00001615 status = pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001616
1617 if (status != PJ_SUCCESS) {
1618 /* Authentication failed! */
1619 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED,
Benny Prijono75130572008-07-17 13:53:41 +00001620 NULL, event, &tsx->status_text);
Benny Prijono834aee32006-02-19 01:38:06 +00001621 return;
1622 }
1623
1624 return;
1625 }
1626
1627 if (tsx->status_code/100 == 2) {
1628
1629 /* Successfull SUBSCRIBE request!
1630 * This could be:
1631 * - response to initial SUBSCRIBE request
1632 * - response to subsequent refresh
1633 * - response to unsubscription
1634 */
1635
1636 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
1637 /* Ignore; this transaction has been processed before */
1638 return;
1639 }
1640
1641 /* Update UAC refresh time, if response contains Expires header,
1642 * only when we're not unsubscribing.
1643 */
1644 if (sub->expires->ivalue != 0) {
1645 pjsip_msg *msg;
1646 pjsip_expires_hdr *expires;
1647
1648 msg = event->body.tsx_state.src.rdata->msg_info.msg;
Benny Prijono9d4469d2007-05-02 05:14:29 +00001649 expires = (pjsip_expires_hdr*)
1650 pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001651 if (expires) {
1652 sub->expires->ivalue = expires->ivalue;
1653 }
1654 }
1655
1656 /* Update time */
1657 update_expires(sub, sub->expires->ivalue);
1658
1659 /* Start UAC refresh timer, only when we're not unsubscribing */
1660 if (sub->expires->ivalue != 0) {
1661 unsigned timeout = (sub->expires->ivalue > TIME_UAC_REFRESH) ?
1662 sub->expires->ivalue - TIME_UAC_REFRESH : sub->expires->ivalue;
1663
1664 PJ_LOG(5,(sub->obj_name, "Will refresh in %d seconds",
1665 timeout));
1666 set_timer(sub, TIMER_TYPE_UAC_REFRESH, timeout);
1667
1668 } else {
1669 /* Otherwise set timer to terminate client subscription when
1670 * NOTIFY to end subscription is not received.
1671 */
1672 set_timer(sub, TIMER_TYPE_UAC_TERMINATE, TIME_UAC_TERMINATE);
1673 }
1674
1675 /* Set state, if necessary */
1676 pj_assert(sub->state != PJSIP_EVSUB_STATE_NULL);
1677 if (sub->state == PJSIP_EVSUB_STATE_SENT) {
Benny Prijono75130572008-07-17 13:53:41 +00001678 set_state(sub, PJSIP_EVSUB_STATE_ACCEPTED, NULL, event, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001679 }
1680
1681 } else {
1682
1683 /* Failed SUBSCRIBE request!
1684 *
1685 * The RFC 3265 says that if outgoing SUBSCRIBE fails with status
1686 * other than 481, the subscription is still considered valid for
1687 * the duration of the last Expires.
1688 *
1689 * Since we send refresh about 5 seconds (TIME_UAC_REFRESH) before
1690 * expiration, theoritically the expiration is still valid for the
1691 * next 5 seconds even when we receive non-481 failed response.
1692 *
1693 * Ah, what the heck!
1694 *
1695 * Just terminate now!
1696 *
1697 */
1698
1699 if (sub->state == PJSIP_EVSUB_STATE_TERMINATED) {
1700 /* Ignore, has been handled before */
1701 return;
1702 }
1703
Benny Prijono69b98ab2006-03-03 10:23:35 +00001704 /* Ignore 490 (Request Updated) status.
1705 * This happens when application sends SUBSCRIBE/REFER while
1706 * another one is still in progress.
1707 */
1708 if (tsx->status_code == PJSIP_SC_REQUEST_UPDATED) {
1709 return;
1710 }
1711
Benny Prijono834aee32006-02-19 01:38:06 +00001712 /* Kill any timer. */
1713 set_timer(sub, TIMER_TYPE_NONE, 0);
1714
1715 /* Set state to TERMINATED */
1716 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED,
Benny Prijono75130572008-07-17 13:53:41 +00001717 NULL, event, &tsx->status_text);
Benny Prijono834aee32006-02-19 01:38:06 +00001718
1719 }
1720
1721 } else if (pjsip_method_cmp(&tsx->method, &pjsip_notify_method) == 0) {
1722
1723 /* Incoming NOTIFY.
1724 * This can be the result of:
1725 * - Initial subscription response
1726 * - UAS updating the resource info.
1727 * - Unsubscription response.
1728 */
1729 int st_code = 200;
1730 pj_str_t *st_text = NULL;
1731 pjsip_hdr res_hdr;
1732 pjsip_msg_body *body = NULL;
1733
1734 pjsip_rx_data *rdata;
1735 pjsip_msg *msg;
1736 pjsip_sub_state_hdr *sub_state;
1737
1738 pjsip_evsub_state new_state;
1739 pj_str_t *new_state_str;
1740
1741 pjsip_tx_data *tdata;
1742 pj_status_t status;
1743 int next_refresh;
1744
1745 /* Only want to handle initial NOTIFY receive event. */
1746 if (tsx->state != PJSIP_TSX_STATE_TRYING)
1747 return;
1748
1749
1750 rdata = event->body.tsx_state.src.rdata;
1751 msg = rdata->msg_info.msg;
1752
1753 pj_list_init(&res_hdr);
1754
1755 /* Get subscription state header. */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001756 sub_state = (pjsip_sub_state_hdr*)
1757 pjsip_msg_find_hdr_by_name(msg, &STR_SUB_STATE, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001758 if (sub_state == NULL) {
1759
1760 pjsip_warning_hdr *warn_hdr;
1761 pj_str_t warn_text = { "Missing Subscription-State header", 33};
1762
1763 /* Bad request! Add warning header. */
1764 st_code = PJSIP_SC_BAD_REQUEST;
1765 warn_hdr = pjsip_warning_hdr_create(rdata->tp_info.pool, 399,
1766 pjsip_endpt_name(sub->endpt),
1767 &warn_text);
1768 pj_list_push_back(&res_hdr, warn_hdr);
1769 }
1770
1771 /* Call application registered callback to handle incoming NOTIFY,
1772 * if any.
1773 */
Benny Prijonod4e0abd2006-03-05 11:53:36 +00001774 if (st_code==200 && sub->user.on_rx_notify && sub->call_cb) {
Benny Prijono834aee32006-02-19 01:38:06 +00001775 (*sub->user.on_rx_notify)(sub, rdata, &st_code, &st_text,
1776 &res_hdr, &body);
1777
1778 /* Application MUST specify final response! */
1779 PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; });
1780
1781 /* Must be a valid status code */
1782 PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; });
1783 }
1784
1785
1786 /* If non-2xx should be returned, then send the response.
1787 * No need to update server subscription state.
1788 */
1789 if (st_code >= 300) {
1790 status = create_response(sub, rdata, st_code, st_text, &res_hdr,
1791 body, &tdata);
1792 if (status == PJ_SUCCESS) {
1793 status = pjsip_dlg_send_response(sub->dlg, tsx, tdata);
1794 }
1795
1796 /* Start timer to terminate subscription, just in case server
1797 * is not able to generate NOTIFY to our response.
1798 */
1799 if (status == PJ_SUCCESS) {
1800 unsigned timeout = TIME_UAC_WAIT_NOTIFY;
1801 set_timer(sub, TIMER_TYPE_UAC_WAIT_NOTIFY, timeout);
1802 } else {
Benny Prijono75130572008-07-17 13:53:41 +00001803 char errmsg[PJ_ERR_MSG_SIZE];
1804 pj_str_t reason;
1805
1806 reason = pj_strerror(status, errmsg, sizeof(errmsg));
1807 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL,
1808 &reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001809 }
1810
1811 return;
1812 }
1813
1814 /* Update expiration from the value of expires param in
1815 * Subscription-State header, but ONLY when subscription state
1816 * is "active" or "pending", AND the header contains expires param.
1817 */
1818 if (sub->expires->ivalue != 0 &&
1819 sub_state->expires_param >= 0 &&
1820 (pj_stricmp(&sub_state->sub_state, &STR_ACTIVE)==0 ||
1821 pj_stricmp(&sub_state->sub_state, &STR_PENDING)==0))
1822 {
1823 next_refresh = sub_state->expires_param;
1824
1825 } else {
1826 next_refresh = sub->expires->ivalue;
1827 }
1828
1829 /* Update time */
1830 update_expires(sub, next_refresh);
1831
1832 /* Start UAC refresh timer, only when we're not unsubscribing */
1833 if (sub->expires->ivalue != 0) {
1834 unsigned timeout = (next_refresh > TIME_UAC_REFRESH) ?
1835 next_refresh - TIME_UAC_REFRESH : next_refresh;
1836
1837 PJ_LOG(5,(sub->obj_name, "Will refresh in %d seconds", timeout));
1838 set_timer(sub, TIMER_TYPE_UAC_REFRESH, timeout);
1839 }
1840
1841 /* Find out the state */
1842 get_hdr_state(sub_state, &new_state, &new_state_str);
1843
1844 /* Send response. */
1845 status = create_response(sub, rdata, st_code, st_text, &res_hdr,
1846 body, &tdata);
1847 if (status == PJ_SUCCESS)
1848 status = pjsip_dlg_send_response(sub->dlg, tsx, tdata);
1849
1850 /* Set the state */
1851 if (status == PJ_SUCCESS) {
Benny Prijono75130572008-07-17 13:53:41 +00001852 set_state(sub, new_state, new_state_str, event,
1853 &sub_state->reason_param);
Benny Prijono834aee32006-02-19 01:38:06 +00001854 } else {
Benny Prijono75130572008-07-17 13:53:41 +00001855 char errmsg[PJ_ERR_MSG_SIZE];
1856 pj_str_t reason;
1857
1858 reason = pj_strerror(status, errmsg, sizeof(errmsg));
1859 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event,
1860 &reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001861 }
1862
1863
1864 } else {
1865
1866 /*
1867 * Unexpected method!
1868 */
1869 PJ_LOG(4,(sub->obj_name, "Unexpected transaction method %.*s",
1870 (int)tsx->method.name.slen, tsx->method.name.ptr));
1871 }
1872}
1873
1874
1875/*
1876 * Transaction event processing by UAS, after subscription is accepted.
1877 */
1878static void on_tsx_state_uas( pjsip_evsub *sub, pjsip_transaction *tsx,
1879 pjsip_event *event)
1880{
1881
Benny Prijono736d0f72006-09-13 22:45:38 +00001882 if (pjsip_method_cmp(&tsx->method, &sub->method) == 0 ||
1883 pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method) == 0)
1884 {
Benny Prijono834aee32006-02-19 01:38:06 +00001885
1886 /*
1887 * Incoming request (e.g. SUBSCRIBE or REFER) to refresh subsciption.
1888 *
1889 */
1890 pjsip_rx_data *rdata;
1891 pjsip_event_hdr *event_hdr;
1892 pjsip_expires_hdr *expires;
1893 pjsip_msg *msg;
1894 pjsip_tx_data *tdata;
1895 int st_code = 200;
1896 pj_str_t *st_text = NULL;
1897 pjsip_hdr res_hdr;
1898 pjsip_msg_body *body = NULL;
1899 pjsip_evsub_state old_state;
1900 pj_str_t old_state_str;
Benny Prijono75130572008-07-17 13:53:41 +00001901 pj_str_t reason = { NULL, 0 };
Benny Prijono834aee32006-02-19 01:38:06 +00001902 pj_status_t status;
1903
1904
1905 /* Only wants to handle the first event when the request is
1906 * received.
1907 */
1908 if (tsx->state != PJSIP_TSX_STATE_TRYING)
1909 return;
1910
1911 rdata = event->body.tsx_state.src.rdata;
1912 msg = rdata->msg_info.msg;
1913
1914 /* Set expiration time based on client request (in Expires header),
1915 * or package default expiration time.
1916 */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001917 event_hdr = (pjsip_event_hdr*)
Benny Prijono0c13f3d2008-07-16 22:39:45 +00001918 pjsip_msg_find_hdr_by_names(msg, &STR_EVENT,
1919 &STR_EVENT, NULL);
Benny Prijono9d4469d2007-05-02 05:14:29 +00001920 expires = (pjsip_expires_hdr*)
1921 pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
Benny Prijono834aee32006-02-19 01:38:06 +00001922 if (event_hdr && expires) {
1923 struct evpkg *evpkg;
1924
1925 evpkg = find_pkg(&event_hdr->event_type);
1926 if (evpkg) {
1927 if (expires->ivalue < (pj_int32_t)evpkg->pkg_expires)
1928 sub->expires->ivalue = expires->ivalue;
1929 else
1930 sub->expires->ivalue = evpkg->pkg_expires;
1931 }
1932 }
1933
1934 /* Update time (before calling on_rx_refresh, since application
1935 * will send NOTIFY.
1936 */
1937 update_expires(sub, sub->expires->ivalue);
1938
1939
1940 /* Save old state.
1941 * If application respond with non-2xx, revert to old state.
1942 */
1943 old_state = sub->state;
1944 old_state_str = sub->state_str;
1945
1946 if (sub->expires->ivalue == 0) {
1947 sub->state = PJSIP_EVSUB_STATE_TERMINATED;
1948 sub->state_str = evsub_state_names[sub->state];
1949 } else if (sub->state == PJSIP_EVSUB_STATE_NULL) {
1950 sub->state = PJSIP_EVSUB_STATE_ACCEPTED;
1951 sub->state_str = evsub_state_names[sub->state];
1952 }
1953
1954 /* Call application's on_rx_refresh, just in case it wants to send
1955 * response other than 200 (OK)
1956 */
1957 pj_list_init(&res_hdr);
1958
Benny Prijonod4e0abd2006-03-05 11:53:36 +00001959 if (sub->user.on_rx_refresh && sub->call_cb) {
1960 (*sub->user.on_rx_refresh)(sub, rdata, &st_code, &st_text,
1961 &res_hdr, &body);
1962 }
Benny Prijono834aee32006-02-19 01:38:06 +00001963
1964 /* Application MUST specify final response! */
1965 PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; });
1966
1967 /* Must be a valid status code */
1968 PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; });
1969
1970
1971 /* Create and send response */
1972 status = create_response(sub, rdata, st_code, st_text, &res_hdr,
1973 body, &tdata);
1974 if (status == PJ_SUCCESS) {
1975 /* Add expires header: */
Benny Prijono9d4469d2007-05-02 05:14:29 +00001976 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
Benny Prijono834aee32006-02-19 01:38:06 +00001977 pjsip_hdr_shallow_clone(tdata->pool,
1978 sub->expires));
1979
1980 /* Send */
1981 status = pjsip_dlg_send_response(sub->dlg, tsx, tdata);
1982 }
1983
1984 /* Update state or revert state */
1985 if (st_code/100==2) {
1986
1987 if (sub->expires->ivalue == 0) {
Benny Prijono75130572008-07-17 13:53:41 +00001988 set_state(sub, sub->state, NULL, event, &reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001989 } else if (sub->state == PJSIP_EVSUB_STATE_NULL) {
Benny Prijono75130572008-07-17 13:53:41 +00001990 set_state(sub, sub->state, NULL, event, &reason);
Benny Prijono834aee32006-02-19 01:38:06 +00001991 }
1992
1993 /* Set UAS timeout timer, when state is not terminated. */
1994 if (sub->state != PJSIP_EVSUB_STATE_TERMINATED) {
1995 PJ_LOG(5,(sub->obj_name, "UAS timeout in %d seconds",
1996 sub->expires->ivalue));
1997 set_timer(sub, TIMER_TYPE_UAS_TIMEOUT,
1998 sub->expires->ivalue);
1999 }
2000
2001 } else {
2002 sub->state = old_state;
2003 sub->state_str = old_state_str;
2004 }
2005
2006
2007 } else if (pjsip_method_cmp(&tsx->method, &pjsip_notify_method)==0) {
2008
2009 /* Handle authentication */
2010 if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
2011 (tsx->status_code==401 || tsx->status_code==407))
2012 {
2013 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
2014 pjsip_tx_data *tdata;
2015 pj_status_t status;
2016
2017 status = pjsip_auth_clt_reinit_req( &sub->dlg->auth_sess, rdata,
2018 tsx->last_tx, &tdata);
2019 if (status == PJ_SUCCESS)
Benny Prijono64158af2006-04-04 11:06:34 +00002020 status = pjsip_dlg_send_request( sub->dlg, tdata, -1, NULL );
Benny Prijono834aee32006-02-19 01:38:06 +00002021
2022 if (status != PJ_SUCCESS) {
2023 /* Can't authenticate. Terminate session (?) */
Benny Prijono75130572008-07-17 13:53:41 +00002024 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL,
2025 &tsx->status_text);
Benny Prijono441ce002006-03-07 15:15:01 +00002026 return;
Benny Prijono834aee32006-02-19 01:38:06 +00002027 }
Benny Prijono26ff9062006-02-21 23:47:00 +00002028
2029 }
2030 /*
2031 * Terminate event usage if we receive 481, 408, and 7 class
2032 * responses.
2033 */
2034 if (sub->state != PJSIP_EVSUB_STATE_TERMINATED &&
2035 (tsx->status_code==481 || tsx->status_code==408 ||
2036 tsx->status_code/100 == 7))
2037 {
Benny Prijono75130572008-07-17 13:53:41 +00002038 set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event,
2039 &tsx->status_text);
Benny Prijono441ce002006-03-07 15:15:01 +00002040 return;
Benny Prijono26ff9062006-02-21 23:47:00 +00002041 }
Benny Prijono834aee32006-02-19 01:38:06 +00002042
2043 } else {
2044
2045 /*
2046 * Unexpected method!
2047 */
2048 PJ_LOG(4,(sub->obj_name, "Unexpected transaction method %.*s",
2049 (int)tsx->method.name.slen, tsx->method.name.ptr));
2050
2051 }
2052}
2053
2054
2055/*
2056 * Notification when transaction state has changed!
2057 */
2058static void mod_evsub_on_tsx_state(pjsip_transaction *tsx, pjsip_event *event)
2059{
2060 pjsip_evsub *sub = pjsip_tsx_get_evsub(tsx);
2061
2062 if (sub == NULL) {
2063 sub = on_new_transaction(tsx, event);
2064 if (sub == NULL)
2065 return;
2066 }
2067
2068
2069 /* Call on_tsx_state callback, if any. */
Benny Prijonod4e0abd2006-03-05 11:53:36 +00002070 if (sub->user.on_tsx_state && sub->call_cb)
Benny Prijono834aee32006-02-19 01:38:06 +00002071 (*sub->user.on_tsx_state)(sub, tsx, event);
2072
2073
2074 /* Process the event: */
2075
2076 if (sub->role == PJSIP_ROLE_UAC) {
2077 on_tsx_state_uac(sub, tsx, event);
2078 } else {
2079 on_tsx_state_uas(sub, tsx, event);
2080 }
2081
2082
2083 /* Check transaction TERMINATE event */
2084 if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
2085
2086 --sub->pending_tsx;
2087
2088 if (sub->state == PJSIP_EVSUB_STATE_TERMINATED &&
2089 sub->pending_tsx == 0)
2090 {
2091 evsub_destroy(sub);
2092 }
2093
2094 }
2095}
2096
2097