Add missing module dependencies to various res_sip* modules
[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 /*** MODULEINFO
23         <depend>pjproject</depend>
24         <depend>res_sip</depend>
25         <support_level>core</support_level>
26  ***/
27
28 #include "asterisk.h"
29
30 #include <pjsip.h>
31 #include <pjsip_simple.h>
32 #include <pjlib.h>
33
34 #include "asterisk/res_sip_pubsub.h"
35 #include "asterisk/module.h"
36 #include "asterisk/linkedlists.h"
37 #include "asterisk/astobj2.h"
38 #include "asterisk/datastore.h"
39 #include "asterisk/uuid.h"
40 #include "asterisk/taskprocessor.h"
41 #include "asterisk/res_sip.h"
42
43 static pj_bool_t sub_on_rx_request(pjsip_rx_data *rdata);
44
45 static struct pjsip_module sub_module = {
46         .name = { "PubSub Module", 13 },
47         .priority = PJSIP_MOD_PRIORITY_APPLICATION,
48         .on_rx_request = sub_on_rx_request,
49 };
50
51 /*!
52  * \brief Structure representing a SIP subscription
53  */
54 struct ast_sip_subscription {
55         /*! Subscription datastores set up by handlers */
56         struct ao2_container *datastores;
57         /*! The endpoint with which the subscription is communicating */
58         struct ast_sip_endpoint *endpoint;
59         /*! Serializer on which to place operations for this subscription */
60         struct ast_taskprocessor *serializer;
61         /*! The handler for this subscription */
62         const struct ast_sip_subscription_handler *handler;
63         /*! The role for this subscription */
64         enum ast_sip_subscription_role role;
65         /*! The underlying PJSIP event subscription structure */
66         pjsip_evsub *evsub;
67         /*! The underlying PJSIP dialog */
68         pjsip_dialog *dlg;
69 };
70
71 #define DATASTORE_BUCKETS 53
72
73 #define DEFAULT_EXPIRES 3600
74
75 static int datastore_hash(const void *obj, int flags)
76 {
77         const struct ast_datastore *datastore = obj;
78         const char *uid = flags & OBJ_KEY ? obj : datastore->uid;
79
80         ast_assert(uid != NULL);
81
82         return ast_str_hash(uid);
83 }
84
85 static int datastore_cmp(void *obj, void *arg, int flags)
86 {
87         const struct ast_datastore *datastore1 = obj;
88         const struct ast_datastore *datastore2 = arg;
89         const char *uid2 = flags & OBJ_KEY ? arg : datastore2->uid;
90
91         ast_assert(datastore1->uid != NULL);
92         ast_assert(uid2 != NULL);
93
94         return strcmp(datastore1->uid, uid2) ? 0 : CMP_MATCH | CMP_STOP;
95 }
96
97 static void subscription_destructor(void *obj)
98 {
99         struct ast_sip_subscription *sub = obj;
100
101         ast_debug(3, "Destroying SIP subscription\n");
102
103         ao2_cleanup(sub->datastores);
104         ao2_cleanup(sub->endpoint);
105
106         if (sub->dlg) {
107                 /* This is why we keep the dialog on the subscription. When the subscription
108                  * is destroyed, there is no guarantee that the underlying dialog is ready
109                  * to be destroyed. Furthermore, there's no guarantee in the opposite direction
110                  * either. The dialog could be destroyed before our subscription is. We fix
111                  * this problem by keeping a reference to the dialog until it is time to
112                  * destroy the subscription. We need to have the dialog available when the
113                  * subscription is destroyed so that we can guarantee that our attempt to
114                  * remove the serializer will be successful.
115                  */
116                 ast_sip_dialog_set_serializer(sub->dlg, NULL);
117                 pjsip_dlg_dec_session(sub->dlg, &sub_module);
118         }
119         ast_taskprocessor_unreference(sub->serializer);
120 }
121
122 static void pubsub_on_evsub_state(pjsip_evsub *sub, pjsip_event *event);
123 static void pubsub_on_tsx_state(pjsip_evsub *sub, pjsip_transaction *tsx, pjsip_event *event);
124 static void pubsub_on_rx_refresh(pjsip_evsub *sub, pjsip_rx_data *rdata,
125                 int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
126 static void pubsub_on_rx_notify(pjsip_evsub *sub, pjsip_rx_data *rdata, int *p_st_code,
127                 pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body);
128 static void pubsub_on_client_refresh(pjsip_evsub *sub);
129 static void pubsub_on_server_timeout(pjsip_evsub *sub);
130
131
132 static pjsip_evsub_user pubsub_cb = {
133         .on_evsub_state = pubsub_on_evsub_state,
134         .on_tsx_state = pubsub_on_tsx_state,
135         .on_rx_refresh = pubsub_on_rx_refresh,
136         .on_rx_notify = pubsub_on_rx_notify,
137         .on_client_refresh = pubsub_on_client_refresh,
138         .on_server_timeout = pubsub_on_server_timeout,
139 };
140
141 static pjsip_evsub *allocate_evsub(const char *event, enum ast_sip_subscription_role role,
142                 struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_dialog *dlg)
143 {
144         pjsip_evsub *evsub;
145         /* PJSIP is kind enough to have some built-in support for certain
146          * events. We need to use the correct initialization function for the
147          * built-in events
148          */
149         if (role == AST_SIP_NOTIFIER) {
150                 if (!strcmp(event, "message-summary")) {
151                         pjsip_mwi_create_uas(dlg, &pubsub_cb, rdata, &evsub);
152                 } else if (!strcmp(event, "presence")) {
153                         pjsip_pres_create_uas(dlg, &pubsub_cb, rdata, &evsub);
154                 } else {
155                         pjsip_evsub_create_uas(dlg, &pubsub_cb, rdata, 0, &evsub);
156                 }
157         } else {
158                 if (!strcmp(event, "message-summary")) {
159                         pjsip_mwi_create_uac(dlg, &pubsub_cb, 0, &evsub);
160                 } else if (!strcmp(event, "presence")) {
161                         pjsip_pres_create_uac(dlg, &pubsub_cb, 0, &evsub);
162                 } else {
163                         pj_str_t pj_event;
164                         pj_cstr(&pj_event, event);
165                         pjsip_evsub_create_uac(dlg, &pubsub_cb, &pj_event, 0, &evsub);
166                 }
167         }
168         return evsub;
169 }
170
171 struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler,
172         enum ast_sip_subscription_role role, struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
173 {
174         struct ast_sip_subscription *sub = ao2_alloc(sizeof(*sub), subscription_destructor);
175         pjsip_dialog *dlg;
176
177         if (!sub) {
178                 return NULL;
179         }
180         sub->datastores = ao2_container_alloc(DATASTORE_BUCKETS, datastore_hash, datastore_cmp);
181         if (!sub->datastores) {
182                 ao2_ref(sub, -1);
183                 return NULL;
184         }
185         sub->serializer = ast_sip_create_serializer();
186         if (!sub->serializer) {
187                 ao2_ref(sub, -1);
188                 return NULL;
189         }
190         sub->role = role;
191         if (role == AST_SIP_NOTIFIER) {
192                 pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, NULL, &dlg);
193         } else {
194                 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
195
196                 contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
197                 if (!contact || ast_strlen_zero(contact->uri)) {
198                         ast_log(LOG_WARNING, "No contacts configured for endpoint %s. Unable to create SIP subsription\n",
199                                         ast_sorcery_object_get_id(endpoint));
200                         ao2_ref(sub, -1);
201                         return NULL;
202                 }
203                 dlg = ast_sip_create_dialog(endpoint, contact->uri, NULL);
204         }
205         if (!dlg) {
206                 ast_log(LOG_WARNING, "Unable to create dialog for SIP subscription\n");
207                 ao2_ref(sub, -1);
208                 return NULL;
209         }
210         sub->evsub = allocate_evsub(handler->event_name, role, endpoint, rdata, dlg);
211         /* We keep a reference to the dialog until our subscription is destroyed. See
212          * the subscription_destructor for more details
213          */
214         pjsip_dlg_inc_session(dlg, &sub_module);
215         sub->dlg = dlg;
216         ast_sip_dialog_set_serializer(dlg, sub->serializer);
217         pjsip_evsub_set_mod_data(sub->evsub, sub_module.id, sub);
218         ao2_ref(endpoint, +1);
219         sub->endpoint = endpoint;
220         sub->handler = handler;
221         return sub;
222 }
223  
224 struct ast_sip_endpoint *ast_sip_subscription_get_endpoint(struct ast_sip_subscription *sub)
225 {
226         ast_assert(sub->endpoint != NULL);
227         ao2_ref(sub->endpoint, +1);
228         return sub->endpoint;
229 }
230  
231 struct ast_taskprocessor *ast_sip_subscription_get_serializer(struct ast_sip_subscription *sub)
232 {
233         ast_assert(sub->serializer != NULL);
234         return sub->serializer;
235 }
236
237 pjsip_evsub *ast_sip_subscription_get_evsub(struct ast_sip_subscription *sub)
238 {
239         return sub->evsub;
240 }
241
242 int ast_sip_subscription_send_request(struct ast_sip_subscription *sub, pjsip_tx_data *tdata)
243 {
244         return pjsip_evsub_send_request(ast_sip_subscription_get_evsub(sub),
245                         tdata) == PJ_SUCCESS ? 0 : -1;
246 }
247
248 static void subscription_datastore_destroy(void *obj)
249 {
250         struct ast_datastore *datastore = obj;
251
252         /* Using the destroy function (if present) destroy the data */
253         if (datastore->info->destroy != NULL && datastore->data != NULL) {
254                 datastore->info->destroy(datastore->data);
255                 datastore->data = NULL;
256         }
257
258         ast_free((void *) datastore->uid);
259         datastore->uid = NULL;
260 }
261
262 struct ast_datastore *ast_sip_subscription_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
263 {
264         RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
265         const char *uid_ptr = uid;
266
267         if (!info) {
268                 return NULL;
269         }
270
271         datastore = ao2_alloc(sizeof(*datastore), subscription_datastore_destroy);
272         if (!datastore) {
273                 return NULL;
274         }
275
276         datastore->info = info;
277         if (ast_strlen_zero(uid)) {
278                 /* They didn't provide an ID so we'll provide one ourself */
279                 struct ast_uuid *uuid = ast_uuid_generate();
280                 char uuid_buf[AST_UUID_STR_LEN];
281                 if (!uuid) {
282                         return NULL;
283                 }
284                 uid_ptr = ast_uuid_to_str(uuid, uuid_buf, sizeof(uuid_buf));
285                 ast_free(uuid);
286         }
287
288         datastore->uid = ast_strdup(uid_ptr);
289         if (!datastore->uid) {
290                 return NULL;
291         }
292
293         ao2_ref(datastore, +1);
294         return datastore;
295 }
296  
297 int ast_sip_subscription_add_datastore(struct ast_sip_subscription *subscription, struct ast_datastore *datastore)
298 {
299         ast_assert(datastore != NULL);
300         ast_assert(datastore->info != NULL);
301         ast_assert(ast_strlen_zero(datastore->uid) == 0);
302
303         if (!ao2_link(subscription->datastores, datastore)) {
304                 return -1;
305         }
306         return 0;
307 }
308
309 struct ast_datastore *ast_sip_subscription_get_datastore(struct ast_sip_subscription *subscription, const char *name)
310 {
311         return ao2_find(subscription->datastores, name, OBJ_KEY);
312 }
313
314 void ast_sip_subscription_remove_datastore(struct ast_sip_subscription *subscription, const char *name)
315 {
316         ao2_callback(subscription->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
317 }
318
319 AST_RWLIST_HEAD_STATIC(subscription_handlers, ast_sip_subscription_handler);
320
321 static void add_handler(struct ast_sip_subscription_handler *handler)
322 {
323         SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
324         AST_RWLIST_INSERT_TAIL(&subscription_handlers, handler, next);
325         ast_module_ref(ast_module_info->self);
326 }
327
328 static int handler_exists_for_event_name(const char *event_name)
329 {
330         struct ast_sip_subscription_handler *iter;
331         SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
332
333         AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
334                 if (!strcmp(iter->event_name, event_name)) {
335                         return 1;
336                 }
337         }
338         return 0;
339 }
340
341 int ast_sip_register_subscription_handler(struct ast_sip_subscription_handler *handler)
342 {
343         pj_str_t event;
344         pj_str_t accept[AST_SIP_MAX_ACCEPT];
345         int i;
346
347         if (ast_strlen_zero(handler->event_name)) {
348                 ast_log(LOG_ERROR, "No event package specifief for subscription handler. Cannot register\n");
349                 return -1;
350         }
351
352         if (ast_strlen_zero(handler->accept[0])) {
353                 ast_log(LOG_ERROR, "Subscription handler must supply at least one 'Accept' format\n");
354                 return -1;
355         }
356
357         if (handler_exists_for_event_name(handler->event_name)) {
358                 ast_log(LOG_ERROR, "A subscription handler for event %s already exists. Not registering "
359                                 "new subscription handler\n", handler->event_name);
360                 return -1;
361         }
362
363         pj_cstr(&event, handler->event_name);
364         for (i = 0; i < AST_SIP_MAX_ACCEPT && !ast_strlen_zero(handler->accept[i]); ++i) {
365                 pj_cstr(&accept[i], handler->accept[i]);
366         }
367
368         if (!strcmp(handler->event_name, "message-summary")) {
369                 pjsip_mwi_init_module(ast_sip_get_pjsip_endpoint(), pjsip_evsub_instance());
370         } else if (!strcmp(handler->event_name, "presence")) {
371                 pjsip_pres_init_module(ast_sip_get_pjsip_endpoint(), pjsip_evsub_instance());
372         } else {
373                 pjsip_evsub_register_pkg(&sub_module, &event, DEFAULT_EXPIRES, i, accept);
374         }
375
376         add_handler(handler);
377         return 0;
378 }
379  
380 void ast_sip_unregister_subscription_handler(struct ast_sip_subscription_handler *handler)
381 {
382         struct ast_sip_subscription_handler *iter;
383         SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
384         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&subscription_handlers, iter, next) {
385                 if (handler == iter) {
386                         AST_RWLIST_REMOVE_CURRENT(next);
387                         ast_module_unref(ast_module_info->self);
388                         break;
389                 }
390         }
391         AST_RWLIST_TRAVERSE_SAFE_END;
392 }
393
394 static struct ast_sip_subscription_handler *find_handler(const char *event, char accept[AST_SIP_MAX_ACCEPT][64], size_t num_accept)
395 {
396         struct ast_sip_subscription_handler *iter;
397         int match = 0;
398         SCOPED_LOCK(lock, &subscription_handlers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
399         AST_RWLIST_TRAVERSE(&subscription_handlers, iter, next) {
400                 int i;
401                 int j;
402                 if (strcmp(event, iter->event_name)) {
403                         ast_debug(3, "Event %s does not match %s\n", event, iter->event_name);
404                         continue;
405                 }
406                 ast_debug(3, "Event name match: %s = %s\n", event, iter->event_name);
407                 for (i = 0; i < num_accept; ++i) {
408                         for (j = 0; j < num_accept; ++j) {
409                                 if (ast_strlen_zero(iter->accept[i])) {
410                                         ast_debug(3, "Breaking because subscription handler has run out of 'accept' types\n");
411                                         break;
412                                 }
413                                 if (!strcmp(accept[j], iter->accept[i])) {
414                                         ast_debug(3, "Accept headers match: %s = %s\n", accept[j], iter->accept[i]);
415                                         match = 1;
416                                         break;
417                                 }
418                                 ast_debug(3, "Accept %s does not match %s\n", accept[j], iter->accept[i]);
419                         }
420                         if (match) {
421                                 break;
422                         }
423                 }
424                 if (match) {
425                         break;
426                 }
427         }
428
429         return iter;
430 }
431
432 static pj_bool_t sub_on_rx_request(pjsip_rx_data *rdata)
433 {
434         static const pj_str_t event_name = { "Event", 5 };
435         char event[32];
436         char accept[AST_SIP_MAX_ACCEPT][64];
437         pjsip_accept_hdr *accept_header;
438         pjsip_event_hdr *event_header;
439         struct ast_sip_subscription_handler *handler;
440         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
441         struct ast_sip_subscription *sub;
442         int i;
443
444         if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, pjsip_get_subscribe_method())) {
445                 return PJ_FALSE;
446         }
447
448         endpoint = ast_pjsip_rdata_get_endpoint(rdata);
449         ast_assert(endpoint != NULL);
450
451         event_header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &event_name, rdata->msg_info.msg->hdr.next);
452         if (!event_header) {
453                 ast_log(LOG_WARNING, "Incoming SUBSCRIBE request with no Event header\n");
454                 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
455                 return PJ_TRUE;
456         }
457
458         accept_header = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, rdata->msg_info.msg->hdr.next);
459         if (!accept_header) {
460                 ast_log(LOG_WARNING, "Incoming SUBSCRIBE request with no Accept header\n");
461                 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400, NULL, NULL, NULL);
462                 return PJ_TRUE;
463         }
464
465         ast_copy_pj_str(event, &event_header->event_type, sizeof(event));
466         for (i = 0; i < accept_header->count; ++i) {
467                 ast_copy_pj_str(accept[i], &accept_header->values[i], sizeof(accept[i]));
468         }
469
470         handler = find_handler(event, accept, accept_header->count);
471         if (!handler) {
472                 ast_log(LOG_WARNING, "No registered handler for event %s\n", event);
473                 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 489, NULL, NULL, NULL);
474                 return PJ_TRUE;
475         }
476         sub = handler->new_subscribe(endpoint, rdata);
477         if (!sub) {
478                 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
479         }
480         return PJ_TRUE;
481 }
482
483 static void pubsub_on_evsub_state(pjsip_evsub *evsub, pjsip_event *event)
484 {
485         struct ast_sip_subscription *sub;
486         if (pjsip_evsub_get_state(evsub) != PJSIP_EVSUB_STATE_TERMINATED) {
487                 return;
488         }
489
490         sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
491         if (!sub) {
492                 return;
493         }
494
495         if (event->type == PJSIP_EVENT_RX_MSG) {
496                 sub->handler->subscription_terminated(sub, event->body.rx_msg.rdata);
497         }
498
499         if (event->type == PJSIP_EVENT_TSX_STATE &&
500                         event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
501                 sub->handler->subscription_terminated(sub, event->body.tsx_state.src.rdata);
502         }
503
504         if (sub->handler->subscription_shutdown) {
505                 sub->handler->subscription_shutdown(sub);
506         }
507         pjsip_evsub_set_mod_data(evsub, sub_module.id, NULL);
508 }
509
510 static void pubsub_on_tsx_state(pjsip_evsub *evsub, pjsip_transaction *tsx, pjsip_event *event)
511 {
512         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
513
514         if (!sub) {
515                 return;
516         }
517
518         if (tsx->role == PJSIP_ROLE_UAC && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
519                 sub->handler->notify_response(sub, event->body.tsx_state.src.rdata);
520         }
521 }
522
523 static void set_parameters_from_response_data(pj_pool_t *pool, int *p_st_code,
524                 pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body,
525                 struct ast_sip_subscription_response_data *response_data)
526 {
527         ast_assert(response_data->status_code >= 200 && response_data->status_code <= 699);
528         *p_st_code = response_data->status_code;
529
530         if (!ast_strlen_zero(response_data->status_text)) {
531                 pj_strdup2(pool, *p_st_text, response_data->status_text);
532         }
533
534         if (response_data->headers) {
535                 struct ast_variable *iter;
536                 for (iter = response_data->headers; iter; iter = iter->next) {
537                         pj_str_t header_name;
538                         pj_str_t header_value;  
539                         pjsip_generic_string_hdr *hdr;
540
541                         pj_cstr(&header_name, iter->name);
542                         pj_cstr(&header_value, iter->value);
543                         hdr = pjsip_generic_string_hdr_create(pool, &header_name, &header_value);
544                         pj_list_insert_before(res_hdr, hdr);
545                 }
546         }
547
548         if (response_data->body) {
549                 pj_str_t type;
550                 pj_str_t subtype;
551                 pj_str_t body_text;
552
553                 pj_cstr(&type, response_data->body->type);
554                 pj_cstr(&subtype, response_data->body->subtype);
555                 pj_cstr(&body_text, response_data->body->body_text);
556
557                 *p_body = pjsip_msg_body_create(pool, &type, &subtype, &body_text);
558         }
559 }
560
561 static int response_data_changed(struct ast_sip_subscription_response_data *response_data)
562 {
563         if (response_data->status_code != 200 ||
564                         !ast_strlen_zero(response_data->status_text) ||
565                         response_data->headers ||
566                         response_data->body) {
567                 return 1;
568         }
569         return 0;
570 }
571
572 static void pubsub_on_rx_refresh(pjsip_evsub *evsub, pjsip_rx_data *rdata,
573                 int *p_st_code, pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
574 {
575         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
576         struct ast_sip_subscription_response_data response_data = {
577                 .status_code = 200,
578         };
579
580         if (!sub) {
581                 return;
582         }
583
584         sub->handler->resubscribe(sub, rdata, &response_data);
585
586         if (!response_data_changed(&response_data)) {
587                 return;
588         }
589
590         set_parameters_from_response_data(rdata->tp_info.pool, p_st_code, p_st_text,
591                         res_hdr, p_body, &response_data);
592 }
593
594 static void pubsub_on_rx_notify(pjsip_evsub *evsub, pjsip_rx_data *rdata, int *p_st_code,
595                 pj_str_t **p_st_text, pjsip_hdr *res_hdr, pjsip_msg_body **p_body)
596 {
597         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
598         struct ast_sip_subscription_response_data response_data = {
599                 .status_code = 200,
600         };
601
602         if (!sub|| !sub->handler->notify_request) {
603                 return;
604         }
605
606         sub->handler->notify_request(sub, rdata, &response_data);
607
608         if (!response_data_changed(&response_data)) {
609                 return;
610         }
611
612         set_parameters_from_response_data(rdata->tp_info.pool, p_st_code, p_st_text,
613                         res_hdr, p_body, &response_data);
614 }
615
616 static int serialized_pubsub_on_client_refresh(void *userdata)
617 {
618         struct ast_sip_subscription *sub = userdata;
619
620         sub->handler->refresh_subscription(sub);
621         ao2_cleanup(sub);
622         return 0;
623 }
624
625 static void pubsub_on_client_refresh(pjsip_evsub *evsub)
626 {
627         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
628
629         ao2_ref(sub, +1);
630         ast_sip_push_task(sub->serializer, serialized_pubsub_on_client_refresh, sub);
631 }
632
633 static int serialized_pubsub_on_server_timeout(void *userdata)
634 {
635         struct ast_sip_subscription *sub = userdata;
636
637         sub->handler->subscription_timeout(sub);
638         ao2_cleanup(sub);
639         return 0;
640 }
641
642 static void pubsub_on_server_timeout(pjsip_evsub *evsub)
643 {
644         struct ast_sip_subscription *sub = pjsip_evsub_get_mod_data(evsub, sub_module.id);
645
646         ao2_ref(sub, +1);
647         ast_sip_push_task(sub->serializer, serialized_pubsub_on_server_timeout, sub);
648 }
649
650 static int load_module(void)
651 {
652         pjsip_evsub_init_module(ast_sip_get_pjsip_endpoint());
653         if (ast_sip_register_service(&sub_module)) {
654                 return AST_MODULE_LOAD_DECLINE;
655         }
656         return AST_MODULE_LOAD_SUCCESS;
657 }
658
659 static int unload_module(void)
660 {
661         return 0;
662 }
663
664 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "SIP event resource",
665                 .load = load_module,
666                 .unload = unload_module,
667                 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
668 );