Merge the pimp_my_sip branch into trunk.
[asterisk/asterisk.git] / res / res_sip_pubsub.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
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.
13  *
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.
17  */
18 /*!
19  * \brief Opaque structure representing an RFC 3265 SIP subscription
20  */
21
22 #include "asterisk.h"
23
24 #include <pjsip.h>
25 #include <pjsip_simple.h>
26 #include <pjlib.h>
27
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"
36
37 static pj_bool_t sub_on_rx_request(pjsip_rx_data *rdata);
38
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,
43 };
44
45 /*!
46  * \brief Structure representing a SIP subscription
47  */
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 */
60         pjsip_evsub *evsub;
61         /*! The underlying PJSIP dialog */
62         pjsip_dialog *dlg;
63 };
64
65 #define DATASTORE_BUCKETS 53
66
67 #define DEFAULT_EXPIRES 3600
68
69 static int datastore_hash(const void *obj, int flags)
70 {
71         const struct ast_datastore *datastore = obj;
72         const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
73
74         ast_assert(uid != NULL);
75
76         return ast_str_hash(uid);
77 }
78
79 static int datastore_cmp(void *obj, void *arg, int flags)
80 {
81         const struct ast_datastore *datastore1 = obj;
82         const struct ast_datastore *datastore2 = arg;
83         const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
84
85         ast_assert(datastore1->uid != NULL);
86         ast_assert(uid2 != NULL);
87
88         return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
89 }
90
91 static void subscription_destructor(void *obj)
92 {
93         struct ast_sip_subscription *sub = obj;
94
95         ast_debug(3, "Destroying SIP subscription\n");
96
97         ao2_cleanup(sub->datastores);
98         ao2_cleanup(sub->endpoint);
99
100         if (sub->dlg) {
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.
109                  */
110                 ast_sip_dialog_set_serializer(sub->dlg, NULL);
111                 pjsip_dlg_dec_session(sub->dlg, &sub_module);
112         }
113         ast_taskprocessor_unreference(sub->serializer);
114 }
115
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);
124
125
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,
133 };
134
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)
137 {
138         pjsip_evsub *evsub;
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
141          * built-in events
142          */
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);
148                 } else {
149                         pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &evsub);
150                 }
151         } else {
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);
156                 } else {
157                         pj_str_t pj_event;
158                         pj_cstr(&pj_event, event);
159                         pjsip_evsub_create_uac(dlg, &pubsub_cb, &pj_event, 0, &evsub);
160                 }
161         }
162         return evsub;
163 }
164
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)
167 {
168         struct ast_sip_subscription *sub = ao2_alloc(sizeof(*sub), subscription_destructor);
169         pjsip_dialog *dlg;
170
171         if (!sub) {
172                 return NULL;
173         }
174         sub->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp);
175         if (!sub->datastores) {
176                 ao2_ref(sub, -1);
177                 return NULL;
178         }
179         sub->serializer = ast_sip_create_serializer();
180         if (!sub->serializer) {
181                 ao2_ref(sub, -1);
182                 return NULL;
183         }
184         sub->role = role;
185         if (role == AST_SIP_NOTIFIER) {
186                 pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, NULL, &dlg);
187         } else {
188                 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
189
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));
194                         ao2_ref(sub, -1);
195                         return NULL;
196                 }
197                 dlg = ast_sip_create_dialog(endpoint, contact->uri, NULL);
198         }
199         if (!dlg) {
200                 ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
201                 ao2_ref(sub, -1);
202                 return NULL;
203         }
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
207          */
208         pjsip_dlg_inc_session(dlg, &sub_module);
209         sub->dlg = dlg;
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;
215         return sub;
216 }
217  
218 struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub)
219 {
220         ast_assert(sub->endpoint != NULL);
221         ao2_ref(sub->endpoint, +1);
222         return sub->endpoint;
223 }
224  
225 struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub)
226 {
227         ast_assert(sub->serializer != NULL);
228         return sub->serializer;
229 }
230
231 pjsip_evsub *ast_sip_subscription_get_evsub(struct ast_sip_subscription *sub)
232 {
233         return sub->evsub;
234 }
235
236 int ast_sip_subscription_send_request(struct ast_sip_subscription *sub, pjsip_tx_data *tdata)
237 {
238         return pjsip_evsub_send_request(ast_sip_subscription_get_evsub(sub),
239                         tdata) == PJ_SUCCESS ? 0 : -1;
240 }
241
242 static void subscription_datastore_destroy(void *obj)
243 {
244         struct ast_datastore *datastore = obj;
245
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;
250         }
251
252         ast_free((void *) datastore->uid);
253         datastore->uid = NULL;
254 }
255
256 struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
257 {
258         RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
259         const char *uid_ptr = uid;
260
261         if (!info) {
262                 return NULL;
263         }
264
265         datastore = ao2_alloc(sizeof(*datastore), subscription_datastore_destroy);
266         if (!datastore) {
267                 return NULL;
268         }
269
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];
275                 if (!uuid) {
276                         return NULL;
277                 }
278                 uid_ptr = ast_uuid_to_str(uuid, uuid_buf, sizeof(uuid_buf));
279                 ast_free(uuid);
280         }
281
282         datastore->uid = ast_strdup(uid_ptr);
283         if (!datastore->uid) {
284                 return NULL;
285         }
286
287         ao2_ref(datastore, +1);
288         return datastore;
289 }
290  
291 int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore)
292 {
293         ast_assert(datastore != NULL);
294         ast_assert(datastore->info != NULL);
295         ast_assert(ast_strlen_zero(datastore->uid) == 0);
296
297         if (!ao2_link(subscription->datastores, datastore)) {
298                 return -1;
299         }
300         return 0;
301 }
302
303 struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name)
304 {
305         return ao2_find(subscription->datastores, name, OBJ_KEY);
306 }
307
308 void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name)
309 {
310         ao2_callback(subscription->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
311 }
312
313 AST_RWLIST_HEAD_STATIC(subscription_handlers, ast_sip_subscription_handler);
314
315 static void add_handler(struct ast_sip_subscription_handler *handler)
316 {
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);
320 }
321
322 static int handler_exists_for_event_name(const char *event_name)
323 {
324         struct ast_sip_subscription_handler *iter;
325         SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
326
327         AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
328                 if (!strcmp(iter->event_name, event_name)) {
329                         return 1;
330                 }
331         }
332         return 0;
333 }
334
335 int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler)
336 {
337         pj_str_t event;
338         pj_str_t accept[AST_SIP_MAX_ACCEPT];
339         int i;
340
341         if (ast_strlen_zero(handler->event_name)) {
342                 ast_log(LOG_ERROR, "No event package specifief for subscription handler. Cannot register\n");
343                 return -1;
344         }
345
346         if (ast_strlen_zero(handler->accept[0])) {
347                 ast_log(LOG_ERROR, "Subscription handler must supply at least one 'Accept' format\n");
348                 return -1;
349         }
350
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);
354                 return -1;
355         }
356
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]);
360         }
361
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());
366         } else {
367                 pjsip_evsub_register_pkg(&sub_module, &event, DEFAULT_EXPIRES, i, accept);
368         }
369
370         add_handler(handler);
371         return 0;
372 }
373  
374 void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler)
375 {
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);
382                         break;
383                 }
384         }
385         AST_RWLIST_TRAVERSE_SAFE_END;
386 }
387
388 static struct ast_sip_subscription_handler *find_handler(const char *event, char accept[AST_SIP_MAX_ACCEPT][64], size_t num_accept)
389 {
390         struct ast_sip_subscription_handler *iter;
391         int match = 0;
392         SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
393         AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
394                 int i;
395                 int j;
396                 if (strcmp(event, iter->event_name)) {
397                         ast_debug(3, "Event %s does not match %s\n", event, iter->event_name);
398                         continue;
399                 }
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");
405                                         break;
406                                 }
407                                 if (!strcmp(accept[j], iter->accept[i])) {
408                                         ast_debug(3, "Accept headers match: %s = %s\n", accept[j], iter->accept[i]);
409                                         match = 1;
410                                         break;
411                                 }
412                                 ast_debug(3, "Accept %s does not match %s\n", accept[j], iter->accept[i]);
413                         }
414                         if (match) {
415                                 break;
416                         }
417                 }
418                 if (match) {
419                         break;
420                 }
421         }
422
423         return iter;
424 }
425
426 static pj_bool_t sub_on_rx_request(pjsip_rx_data *rdata)
427 {
428         static const pj_str_t event_name = { "Event", 5 };
429         char event[32];
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;
436         int i;
437
438         if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_subscribe_method())) {
439                 return PJ_FALSE;
440         }
441
442         endpoint = ast_pjsip_rdata_get_endpoint(rdata);
443         ast_assert(endpoint != NULL);
444
445         event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &event_name, rdata->msg_info.msg->hdr.next);
446         if (!event_header) {
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);
449                 return PJ_TRUE;
450         }
451
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);
456                 return PJ_TRUE;
457         }
458
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]));
462         }
463
464         handler = find_handler(event, accept, accept_header->count);
465         if (!handler) {
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);
468                 return PJ_TRUE;
469         }
470         sub = handler->new_subscribe(endpoint, rdata);
471         if (!sub) {
472                 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
473         }
474         return PJ_TRUE;
475 }
476
477 static void pubsub_on_evsub_state(pjsip_evsub *evsub, pjsip_event *event)
478 {
479         struct ast_sip_subscription *sub;
480         if (pjsip_evsub_get_state(evsub) != PJSIP_EVSUB_STATE_TERMINATED) {
481                 return;
482         }
483
484         sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
485         if (!sub) {
486                 return;
487         }
488
489         if (event->type == PJSIP_EVENT_RX_MSG) {
490                 sub->handler->subscription_terminated(sub, event->body.rx_msg.rdata);
491         }
492
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);
496         }
497
498         if (sub->handler->subscription_shutdown) {
499                 sub->handler->subscription_shutdown(sub);
500         }
501         pjsip_evsub_set_mod_data(evsub, sub_module.id, NULL);
502 }
503
504 static void pubsub_on_tsx_state(pjsip_evsub *evsub, pjsip_transaction *tsx, pjsip_event *event)
505 {
506         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
507
508         if (!sub) {
509                 return;
510         }
511
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);
514         }
515 }
516
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)
520 {
521         ast_assert(response_data->status_code >= 200 && response_data->status_code <= 699);
522         *p_st_code = response_data->status_code;
523
524         if (!ast_strlen_zero(response_data->status_text)) {
525                 pj_strdup2(pool, *p_st_text, response_data->status_text);
526         }
527
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;
534
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);
539                 }
540         }
541
542         if (response_data->body) {
543                 pj_str_t type;
544                 pj_str_t subtype;
545                 pj_str_t body_text;
546
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);
550
551                 *p_body = pjsip_msg_body_create(pool, &type, &subtype, &body_text);
552         }
553 }
554
555 static int response_data_changed(struct ast_sip_subscription_response_data *response_data)
556 {
557         if (response_data->status_code != 200 ||
558                         !ast_strlen_zero(response_data->status_text) ||
559                         response_data->headers ||
560                         response_data->body) {
561                 return 1;
562         }
563         return 0;
564 }
565
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)
568 {
569         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
570         struct ast_sip_subscription_response_data response_data = {
571                 .status_code = 200,
572         };
573
574         if (!sub) {
575                 return;
576         }
577
578         sub->handler->resubscribe(sub, rdata, &response_data);
579
580         if (!response_data_changed(&response_data)) {
581                 return;
582         }
583
584         set_parameters_from_response_data(rdata->tp_info.pool, p_st_code, p_st_text,
585                         res_hdr, p_body, &response_data);
586 }
587
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)
590 {
591         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
592         struct ast_sip_subscription_response_data response_data = {
593                 .status_code = 200,
594         };
595
596         if (!sub|| !sub->handler->notify_request) {
597                 return;
598         }
599
600         sub->handler->notify_request(sub, rdata, &response_data);
601
602         if (!response_data_changed(&response_data)) {
603                 return;
604         }
605
606         set_parameters_from_response_data(rdata->tp_info.pool, p_st_code, p_st_text,
607                         res_hdr, p_body, &response_data);
608 }
609
610 static int serialized_pubsub_on_client_refresh(void *userdata)
611 {
612         struct ast_sip_subscription *sub = userdata;
613
614         sub->handler->refresh_subscription(sub);
615         ao2_cleanup(sub);
616         return 0;
617 }
618
619 static void pubsub_on_client_refresh(pjsip_evsub *evsub)
620 {
621         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
622
623         ao2_ref(sub, +1);
624         ast_sip_push_task(sub->serializer, serialized_pubsub_on_client_refresh, sub);
625 }
626
627 static int serialized_pubsub_on_server_timeout(void *userdata)
628 {
629         struct ast_sip_subscription *sub = userdata;
630
631         sub->handler->subscription_timeout(sub);
632         ao2_cleanup(sub);
633         return 0;
634 }
635
636 static void pubsub_on_server_timeout(pjsip_evsub *evsub)
637 {
638         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
639
640         ao2_ref(sub, +1);
641         ast_sip_push_task(sub->serializer, serialized_pubsub_on_server_timeout, sub);
642 }
643
644 static int load_module(void)
645 {
646         pjsip_evsub_init_module(ast_sip_get_pjsip_endpoint());
647         if (ast_sip_register_service(&sub_module)) {
648                 return AST_MODULE_LOAD_DECLINE;
649         }
650         return AST_MODULE_LOAD_SUCCESS;
651 }
652
653 static int unload_module(void)
654 {
655         return 0;
656 }
657
658 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "SIP event resource",
659                 .load = load_module,
660                 .unload = unload_module,
661                 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
662 );