2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 * \brief Opaque structure representing an RFC 3265 SIP subscription
25 #include <pjsip_simple.h>
28 #include "asterisk/res_sip_pubsub.h"
29 #include "asterisk/module.h"
30 #include "asterisk/linkedlists.h"
31 #include "asterisk/astobj2.h"
32 #include "asterisk/datastore.h"
33 #include "asterisk/uuid.h"
34 #include "asterisk/taskprocessor.h"
35 #include "asterisk/res_sip.h"
37 static pj_bool_t sub_on_rx_request(pjsip_rx_data *rdata);
39 static struct pjsip_module sub_module = {
40 .name = { "PubSub Module", 13 },
41 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
42 .on_rx_request = sub_on_rx_request,
46 * \brief Structure representing a SIP subscription
48 struct ast_sip_subscription {
49 /*! Subscription datastores set up by handlers */
50 struct ao2_container *datastores;
51 /*! The endpoint with which the subscription is communicating */
52 struct ast_sip_endpoint *endpoint;
53 /*! Serializer on which to place operations for this subscription */
54 struct ast_taskprocessor *serializer;
55 /*! The handler for this subscription */
56 const struct ast_sip_subscription_handler *handler;
57 /*! The role for this subscription */
58 enum ast_sip_subscription_role role;
59 /*! The underlying PJSIP event subscription structure */
61 /*! The underlying PJSIP dialog */
65 #define DATASTORE_BUCKETS 53
67 #define DEFAULT_EXPIRES 3600
69 static int datastore_hash(const void *obj, int flags)
71 const struct ast_datastore *datastore = obj;
72 const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
74 ast_assert(uid != NULL);
76 return ast_str_hash(uid);
79 static int datastore_cmp(void *obj, void *arg, int flags)
81 const struct ast_datastore *datastore1 = obj;
82 const struct ast_datastore *datastore2 = arg;
83 const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
85 ast_assert(datastore1->uid != NULL);
86 ast_assert(uid2 != NULL);
88 return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
91 static void subscription_destructor(void *obj)
93 struct ast_sip_subscription *sub = obj;
95 ast_debug(3, "Destroying SIP subscription\n");
97 ao2_cleanup(sub->datastores);
98 ao2_cleanup(sub->endpoint);
101 /* This is why we keep the dialog on the subscription. When the subscription
102 * is destroyed, there is no guarantee that the underlying dialog is ready
103 * to be destroyed. Furthermore, there's no guarantee in the opposite direction
104 * either. The dialog could be destroyed before our subscription is. We fix
105 * this problem by keeping a reference to the dialog until it is time to
106 * destroy the subscription. We need to have the dialog available when the
107 * subscription is destroyed so that we can guarantee that our attempt to
108 * remove the serializer will be successful.
110 ast_sip_dialog_set_serializer(sub->dlg, NULL);
111 pjsip_dlg_dec_session(sub->dlg, &sub_module);
113 ast_taskprocessor_unreference(sub->serializer);
116 static void pubsub_on_evsub_state(pjsip_evsub *sub, pjsip_event *event);
117 static void pubsub_on_tsx_state(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event);
118 static void pubsub_on_rx_refresh(pjsip_evsub *sub, pjsip_rx_data *rdata,
119 int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
120 static void pubsub_on_rx_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
121 pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
122 static void pubsub_on_client_refresh(pjsip_evsub *sub);
123 static void pubsub_on_server_timeout(pjsip_evsub *sub);
126 static pjsip_evsub_user pubsub_cb = {
127 .on_evsub_state = pubsub_on_evsub_state,
128 .on_tsx_state = pubsub_on_tsx_state,
129 .on_rx_refresh = pubsub_on_rx_refresh,
130 .on_rx_notify = pubsub_on_rx_notify,
131 .on_client_refresh = pubsub_on_client_refresh,
132 .on_server_timeout = pubsub_on_server_timeout,
135 static pjsip_evsub *allocate_evsub(const char *event, enum ast_sip_subscription_role role,
136 struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_dialog *dlg)
139 /* PJSIP is kind enough to have some built-in support for certain
140 * events. We need to use the correct initialization function for the
143 if (role == AST_SIP_NOTIFIER) {
144 if (!strcmp(event, "message-summary")) {
145 pjsip_mwi_create_uas(dlg, &pubsub_cb, rdata, &evsub);
146 } else if (!strcmp(event, "presence")) {
147 pjsip_pres_create_uas(dlg, &pubsub_cb, rdata, &evsub);
149 pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &evsub);
152 if (!strcmp(event, "message-summary")) {
153 pjsip_mwi_create_uac(dlg, &pubsub_cb, 0, &evsub);
154 } else if (!strcmp(event, "presence")) {
155 pjsip_pres_create_uac(dlg, &pubsub_cb, 0, &evsub);
158 pj_cstr(&pj_event, event);
159 pjsip_evsub_create_uac(dlg, &pubsub_cb, &pj_event, 0, &evsub);
165 struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
166 enum ast_sip_subscription_role role, struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
168 struct ast_sip_subscription *sub = ao2_alloc(sizeof(*sub), subscription_destructor);
174 sub->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp);
175 if (!sub->datastores) {
179 sub->serializer = ast_sip_create_serializer();
180 if (!sub->serializer) {
185 if (role == AST_SIP_NOTIFIER) {
186 pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, NULL, &dlg);
188 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
190 contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
191 if (!contact || ast_strlen_zero(contact->uri)) {
192 ast_log(LOG_WARNING, "No contacts configured for endpoint %s. Unable to create SIP subsription\n",
193 ast_sorcery_object_get_id(endpoint));
197 dlg = ast_sip_create_dialog(endpoint, contact->uri, NULL);
200 ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
204 sub->evsub = allocate_evsub(handler->event_name, role, endpoint, rdata, dlg);
205 /* We keep a reference to the dialog until our subscription is destroyed. See
206 * the subscription_destructor for more details
208 pjsip_dlg_inc_session(dlg, &sub_module);
210 ast_sip_dialog_set_serializer(dlg, sub->serializer);
211 pjsip_evsub_set_mod_data(sub->evsub, sub_module.id, sub);
212 ao2_ref(endpoint, +1);
213 sub->endpoint = endpoint;
214 sub->handler = handler;
218 struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub)
220 ast_assert(sub->endpoint != NULL);
221 ao2_ref(sub->endpoint, +1);
222 return sub->endpoint;
225 struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub)
227 ast_assert(sub->serializer != NULL);
228 return sub->serializer;
231 pjsip_evsub *ast_sip_subscription_get_evsub(struct ast_sip_subscription *sub)
236 int ast_sip_subscription_send_request(struct ast_sip_subscription *sub, pjsip_tx_data *tdata)
238 return pjsip_evsub_send_request(ast_sip_subscription_get_evsub(sub),
239 tdata) == PJ_SUCCESS ? 0 : -1;
242 static void subscription_datastore_destroy(void *obj)
244 struct ast_datastore *datastore = obj;
246 /* Using the destroy function (if present) destroy the data */
247 if (datastore->info->destroy != NULL && datastore->data != NULL) {
248 datastore->info->destroy(datastore->data);
249 datastore->data = NULL;
252 ast_free((void *) datastore->uid);
253 datastore->uid = NULL;
256 struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
258 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
259 const char *uid_ptr = uid;
265 datastore = ao2_alloc(sizeof(*datastore), subscription_datastore_destroy);
270 datastore->info = info;
271 if (ast_strlen_zero(uid)) {
272 /* They didn't provide an ID so we'll provide one ourself */
273 struct ast_uuid *uuid = ast_uuid_generate();
274 char uuid_buf[AST_UUID_STR_LEN];
278 uid_ptr = ast_uuid_to_str(uuid, uuid_buf, sizeof(uuid_buf));
282 datastore->uid = ast_strdup(uid_ptr);
283 if (!datastore->uid) {
287 ao2_ref(datastore, +1);
291 int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore)
293 ast_assert(datastore != NULL);
294 ast_assert(datastore->info != NULL);
295 ast_assert(ast_strlen_zero(datastore->uid) == 0);
297 if (!ao2_link(subscription->datastores, datastore)) {
303 struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name)
305 return ao2_find(subscription->datastores, name, OBJ_KEY);
308 void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name)
310 ao2_callback(subscription->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
313 AST_RWLIST_HEAD_STATIC(subscription_handlers, ast_sip_subscription_handler);
315 static void add_handler(struct ast_sip_subscription_handler *handler)
317 SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
318 AST_RWLIST_INSERT_TAIL(&subscription_handlers, handler, next);
319 ast_module_ref(ast_module_info->self);
322 static int handler_exists_for_event_name(const char *event_name)
324 struct ast_sip_subscription_handler *iter;
325 SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
327 AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
328 if (!strcmp(iter->event_name, event_name)) {
335 int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler)
338 pj_str_t accept[AST_SIP_MAX_ACCEPT];
341 if (ast_strlen_zero(handler->event_name)) {
342 ast_log(LOG_ERROR, "No event package specifief for subscription handler. Cannot register\n");
346 if (ast_strlen_zero(handler->accept[0])) {
347 ast_log(LOG_ERROR, "Subscription handler must supply at least one 'Accept' format\n");
351 if (handler_exists_for_event_name(handler->event_name)) {
352 ast_log(LOG_ERROR, "A subscription handler for event %s already exists. Not registering "
353 "new subscription handler\n", handler->event_name);
357 pj_cstr(&event, handler->event_name);
358 for (i = 0; i < AST_SIP_MAX_ACCEPT && !ast_strlen_zero(handler->accept[i]); ++i) {
359 pj_cstr(&accept[i], handler->accept[i]);
362 if (!strcmp(handler->event_name, "message-summary")) {
363 pjsip_mwi_init_module(ast_sip_get_pjsip_endpoint(), pjsip_evsub_instance());
364 } else if (!strcmp(handler->event_name, "presence")) {
365 pjsip_pres_init_module(ast_sip_get_pjsip_endpoint(), pjsip_evsub_instance());
367 pjsip_evsub_register_pkg(&sub_module, &event, DEFAULT_EXPIRES, i, accept);
370 add_handler(handler);
374 void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler)
376 struct ast_sip_subscription_handler *iter;
377 SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
378 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscription_handlers, iter, next) {
379 if (handler == iter) {
380 AST_RWLIST_REMOVE_CURRENT(next);
381 ast_module_unref(ast_module_info->self);
385 AST_RWLIST_TRAVERSE_SAFE_END;
388 static struct ast_sip_subscription_handler *find_handler(const char *event, char accept[AST_SIP_MAX_ACCEPT][64], size_t num_accept)
390 struct ast_sip_subscription_handler *iter;
392 SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
393 AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
396 if (strcmp(event, iter->event_name)) {
397 ast_debug(3, "Event %s does not match %s\n", event, iter->event_name);
400 ast_debug(3, "Event name match: %s = %s\n", event, iter->event_name);
401 for (i = 0; i < num_accept; ++i) {
402 for (j = 0; j < num_accept; ++j) {
403 if (ast_strlen_zero(iter->accept[i])) {
404 ast_debug(3, "Breaking because subscription handler has run out of 'accept' types\n");
407 if (!strcmp(accept[j], iter->accept[i])) {
408 ast_debug(3, "Accept headers match: %s = %s\n", accept[j], iter->accept[i]);
412 ast_debug(3, "Accept %s does not match %s\n", accept[j], iter->accept[i]);
426 static pj_bool_t sub_on_rx_request(pjsip_rx_data *rdata)
428 static const pj_str_t event_name = { "Event", 5 };
430 char accept[AST_SIP_MAX_ACCEPT][64];
431 pjsip_accept_hdr *accept_header;
432 pjsip_event_hdr *event_header;
433 struct ast_sip_subscription_handler *handler;
434 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
435 struct ast_sip_subscription *sub;
438 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_subscribe_method())) {
442 endpoint = ast_pjsip_rdata_get_endpoint(rdata);
443 ast_assert(endpoint != NULL);
445 event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &event_name, rdata->msg_info.msg->hdr.next);
447 ast_log(LOG_WARNING, "Incoming SUBSCRIBE request with no Event header\n");
448 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
452 accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, rdata->msg_info.msg->hdr.next);
453 if (!accept_header) {
454 ast_log(LOG_WARNING, "Incoming SUBSCRIBE request with no Accept header\n");
455 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL);
459 ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
460 for (i = 0; i < accept_header->count; ++i) {
461 ast_copy_pj_str(accept[i], &accept_header->values[i], sizeof(accept[i]));
464 handler = find_handler(event, accept, accept_header->count);
466 ast_log(LOG_WARNING, "No registered handler for event %s\n", event);
467 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
470 sub = handler->new_subscribe(endpoint, rdata);
472 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
477 static void pubsub_on_evsub_state(pjsip_evsub *evsub, pjsip_event *event)
479 struct ast_sip_subscription *sub;
480 if (pjsip_evsub_get_state(evsub) != PJSIP_EVSUB_STATE_TERMINATED) {
484 sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
489 if (event->type == PJSIP_EVENT_RX_MSG) {
490 sub->handler->subscription_terminated(sub, event->body.rx_msg.rdata);
493 if (event->type == PJSIP_EVENT_TSX_STATE &&
494 event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
495 sub->handler->subscription_terminated(sub, event->body.tsx_state.src.rdata);
498 if (sub->handler->subscription_shutdown) {
499 sub->handler->subscription_shutdown(sub);
501 pjsip_evsub_set_mod_data(evsub, sub_module.id, NULL);
504 static void pubsub_on_tsx_state(pjsip_evsub *evsub, pjsip_transaction *tsx, pjsip_event *event)
506 struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
512 if (tsx->role == PJSIP_ROLE_UAC && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
513 sub->handler->notify_response(sub, event->body.tsx_state.src.rdata);
517 static void set_parameters_from_response_data(pj_pool_t *pool, int *p_st_code,
518 pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body,
519 struct ast_sip_subscription_response_data *response_data)
521 ast_assert(response_data->status_code >= 200 && response_data->status_code <= 699);
522 *p_st_code = response_data->status_code;
524 if (!ast_strlen_zero(response_data->status_text)) {
525 pj_strdup2(pool, *p_st_text, response_data->status_text);
528 if (response_data->headers) {
529 struct ast_variable *iter;
530 for (iter = response_data->headers; iter; iter = iter->next) {
531 pj_str_t header_name;
532 pj_str_t header_value;
533 pjsip_generic_string_hdr *hdr;
535 pj_cstr(&header_name, iter->name);
536 pj_cstr(&header_value, iter->value);
537 hdr = pjsip_generic_string_hdr_create(pool, &header_name, &header_value);
538 pj_list_insert_before(res_hdr, hdr);
542 if (response_data->body) {
547 pj_cstr(&type, response_data->body->type);
548 pj_cstr(&subtype, response_data->body->subtype);
549 pj_cstr(&body_text, response_data->body->body_text);
551 *p_body = pjsip_msg_body_create(pool, &type, &subtype, &body_text);
555 static int response_data_changed(struct ast_sip_subscription_response_data *response_data)
557 if (response_data->status_code != 200 ||
558 !ast_strlen_zero(response_data->status_text) ||
559 response_data->headers ||
560 response_data->body) {
566 static void pubsub_on_rx_refresh(pjsip_evsub *evsub, pjsip_rx_data *rdata,
567 int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
569 struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
570 struct ast_sip_subscription_response_data response_data = {
578 sub->handler->resubscribe(sub, rdata, &response_data);
580 if (!response_data_changed(&response_data)) {
584 set_parameters_from_response_data(rdata->tp_info.pool, p_st_code, p_st_text,
585 res_hdr, p_body, &response_data);
588 static void pubsub_on_rx_notify(pjsip_evsub *evsub, pjsip_rx_data *rdata, int *p_st_code,
589 pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
591 struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
592 struct ast_sip_subscription_response_data response_data = {
596 if (!sub|| !sub->handler->notify_request) {
600 sub->handler->notify_request(sub, rdata, &response_data);
602 if (!response_data_changed(&response_data)) {
606 set_parameters_from_response_data(rdata->tp_info.pool, p_st_code, p_st_text,
607 res_hdr, p_body, &response_data);
610 static int serialized_pubsub_on_client_refresh(void *userdata)
612 struct ast_sip_subscription *sub = userdata;
614 sub->handler->refresh_subscription(sub);
619 static void pubsub_on_client_refresh(pjsip_evsub *evsub)
621 struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
624 ast_sip_push_task(sub->serializer, serialized_pubsub_on_client_refresh, sub);
627 static int serialized_pubsub_on_server_timeout(void *userdata)
629 struct ast_sip_subscription *sub = userdata;
631 sub->handler->subscription_timeout(sub);
636 static void pubsub_on_server_timeout(pjsip_evsub *evsub)
638 struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
641 ast_sip_push_task(sub->serializer, serialized_pubsub_on_server_timeout, sub);
644 static int load_module(void)
646 pjsip_evsub_init_module(ast_sip_get_pjsip_endpoint());
647 if (ast_sip_register_service(&sub_module)) {
648 return AST_MODULE_LOAD_DECLINE;
650 return AST_MODULE_LOAD_SUCCESS;
653 static int unload_module(void)
658 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "SIP event resource",
660 .unload = unload_module,
661 .load_pri = AST_MODPRI_CHANNEL_DEPEND,