Debian Codegraph @

res/res_pjsip_mwi.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Mark Michelson <[email protected]>
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 /*** MODULEINFO
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_pubsub</depend>
23 <support_level>core</support_level>
24 ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_simple.h>
30 #include <pjlib.h>
31
32 #include "asterisk/res_pjsip.h"
33 #include "asterisk/res_pjsip_pubsub.h"
34 #include "asterisk/res_pjsip_body_generator_types.h"
35 #include "asterisk/module.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/astobj2.h"
38 #include "asterisk/taskprocessor.h"
39 #include "asterisk/serializer.h"
40 #include "asterisk/sorcery.h"
41 #include "asterisk/stasis.h"
42 #include "asterisk/mwi.h"
43
44 struct mwi_subscription;
45
46 AO2_GLOBAL_OBJ_STATIC(mwi_unsolicited);
47 AO2_GLOBAL_OBJ_STATIC(mwi_solicited);
48
49 static char *default_voicemail_extension;
50
51 #define STASIS_BUCKETS 13
52 #define MWI_BUCKETS 53
53
54 #define MWI_TYPE "application"
55 #define MWI_SUBTYPE "simple-message-summary"
56
57 #define MWI_DATASTORE "MWI datastore"
58
59 /*! Number of serializers in pool if one not supplied. */
60 #define MWI_SERIALIZER_POOL_SIZE 8
61
62 /*! Max timeout for all threads to join during an unload. */
63 #define MAX_UNLOAD_TIMEOUT_TIME 10 /* Seconds */
64
65 /*! Pool of serializers to use if not supplied. */
66 static struct ast_serializer_pool *mwi_serializer_pool;
67
68 static void mwi_subscription_shutdown(struct ast_sip_subscription *sub);
69 static void mwi_to_ami(struct ast_sip_subscription *sub, struct ast_str **buf);
70 static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
71 const char *resource);
72 static int mwi_subscription_established(struct ast_sip_subscription *sub);
73 static void *mwi_get_notify_data(struct ast_sip_subscription *sub);
74
75 static struct ast_sip_notifier mwi_notifier = {
76 .default_accept = MWI_TYPE"/"MWI_SUBTYPE,
77 .new_subscribe = mwi_new_subscribe,
78 .subscription_established = mwi_subscription_established,
79 .get_notify_data = mwi_get_notify_data,
80 };
81
82 static struct ast_sip_subscription_handler mwi_handler = {
83 .event_name = "message-summary",
84 .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
85 .accept = { MWI_TYPE"/"MWI_SUBTYPE, },
86 .subscription_shutdown = mwi_subscription_shutdown,
87 .to_ami = mwi_to_ami,
88 .notifier = &mwi_notifier,
89 };
90
91 /*!
92 * \brief Wrapper for stasis subscription
93 *
94 * An MWI subscription has a container of these. This
95 * represents a stasis subscription for MWI state.
96 */
97 struct mwi_stasis_subscription {
98 /*! The MWI stasis subscription */
99 struct ast_mwi_subscriber *mwi_subscriber;
100 /*! The mailbox corresponding with the MWI subscription. Used as a hash key */
101 char mailbox[1];
102 };
103
104 /*!
105 * \brief A subscription for MWI
106 *
107 * This subscription is the basis for MWI for an endpoint. Each
108 * endpoint that uses MWI will have a corresponding mwi_subscription.
109 *
110 * This structure acts as the owner for the underlying SIP subscription.
111 * When the mwi_subscription is destroyed, the SIP subscription dies, too.
112 * The mwi_subscription's lifetime is governed by its underlying stasis
113 * subscriptions. When all stasis subscriptions are destroyed, the
114 * mwi_subscription is destroyed as well.
115 */
116 struct mwi_subscription {
117 /*! Container of \ref mwi_stasis_subscription structures.
118 * A single MWI subscription may be for multiple mailboxes, thus
119 * requiring multiple stasis subscriptions
120 */
121 struct ao2_container *stasis_subs;
122 /*! The SIP subscription. Unsolicited MWI does not use this */
123 struct ast_sip_subscription *sip_sub;
124 /*! AORs we should react to for unsolicited MWI NOTIFY */
125 char *aors;
126 /*! Is the MWI solicited (i.e. Initiated with an external SUBSCRIBE) ? */
127 unsigned int is_solicited;
128 /*! True if this subscription is to be terminated */
129 unsigned int terminate;
130 /*! Identifier for the subscription.
131 * The identifier is the same as the corresponding endpoint's stasis ID.
132 * Used as a hash key
133 */
134 char id[1];
135 };
136
137 static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
138 struct stasis_message *msg);
139
140 static struct mwi_stasis_subscription *mwi_stasis_subscription_alloc(const char *mailbox, struct mwi_subscription *mwi_sub)
141 {
142 struct mwi_stasis_subscription *mwi_stasis_sub;
143
144 if (!mwi_sub) {
145 return NULL;
146 }
147
148 mwi_stasis_sub = ao2_alloc(sizeof(*mwi_stasis_sub) + strlen(mailbox), NULL);
149 if (!mwi_stasis_sub) {
150 return NULL;
151 }
152
153 /* Safe strcpy */
154 strcpy(mwi_stasis_sub->mailbox, mailbox);
155
156 ao2_ref(mwi_sub, +1);
157 mwi_stasis_sub->mwi_subscriber = ast_mwi_subscribe_pool(mailbox, mwi_stasis_cb, mwi_sub);
158 if (!mwi_stasis_sub->mwi_subscriber) {
159 /* Failed to subscribe. */
160 ao2_ref(mwi_stasis_sub, -1);
161 ao2_ref(mwi_sub, -1);
162 return NULL;
163 }
164
165 stasis_subscription_accept_message_type(
166 ast_mwi_subscriber_subscription(mwi_stasis_sub->mwi_subscriber),
167 stasis_subscription_change_type());
168
169 return mwi_stasis_sub;
170 }
171
172 static int stasis_sub_hash(const void *obj, const int flags)
173 {
174 const struct mwi_stasis_subscription *object;
175 const char *key;
176
177 switch (flags & OBJ_SEARCH_MASK) {
178 case OBJ_SEARCH_KEY:
179 key = obj;
180 break;
181 case OBJ_SEARCH_OBJECT:
182 object = obj;
183 key = object->mailbox;
184 break;
185 default:
186 ast_assert(0);
187 return 0;
188 }
189 return ast_str_hash(key);
190 }
191
192 static int stasis_sub_cmp(void *obj, void *arg, int flags)
193 {
194 const struct mwi_stasis_subscription *sub_left = obj;
195 const struct mwi_stasis_subscription *sub_right = arg;
196 const char *right_key = arg;
197 int cmp;
198
199 switch (flags & OBJ_SEARCH_MASK) {
200 case OBJ_SEARCH_OBJECT:
201 right_key = sub_right->mailbox;
202 /* Fall through */
203 case OBJ_SEARCH_KEY:
204 cmp = strcmp(sub_left->mailbox, right_key);
205 break;
206 case OBJ_SEARCH_PARTIAL_KEY:
207 cmp = strncmp(sub_left->mailbox, right_key, strlen(right_key));
208 break;
209 default:
210 cmp = 0;
211 break;
212 }
213 if (cmp) {
214 return 0;
215 }
216 return CMP_MATCH;
217 }
218
219 static void mwi_subscription_destructor(void *obj)
220 {
221 struct mwi_subscription *sub = obj;
222
223 ast_debug(3, "Destroying MWI subscription for endpoint %s\n", sub->id);
224 if (sub->is_solicited) {
225 ast_sip_subscription_destroy(sub->sip_sub);
226 }
227 ao2_cleanup(sub->stasis_subs);
228 ast_free(sub->aors);
229 }
230
231 static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint,
232 unsigned int is_solicited, struct ast_sip_subscription *sip_sub)
233 {
234 struct mwi_subscription *sub;
235 const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
236
237 sub = ao2_alloc(sizeof(*sub) + strlen(endpoint_id),
238 mwi_subscription_destructor);
239
240 if (!sub) {
241 return NULL;
242 }
243
244 /* Safe strcpy */
245 strcpy(sub->id, endpoint_id);
246
247 /* Unsolicited MWI doesn't actually result in a SIP subscription being
248 * created. This is because a SIP subscription associates with a dialog.
249 * Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If
250 * they receive an in-dialog MWI NOTIFY (i.e. with a to-tag), then they
251 * will reject the NOTIFY with a 481, thus resulting in message-waiting
252 * state not being updated on the device
253 */
254 if (is_solicited) {
255 sub->sip_sub = sip_sub;
256 }
257
258 sub->stasis_subs = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
259 STASIS_BUCKETS, stasis_sub_hash, NULL, stasis_sub_cmp);
260 if (!sub->stasis_subs) {
261 ao2_cleanup(sub);
262 return NULL;
263 }
264 sub->is_solicited = is_solicited;
265
266 if (!is_solicited && !ast_strlen_zero(endpoint->aors)) {
267 sub->aors = ast_strdup(endpoint->aors);
268 if (!sub->aors) {
269 ao2_ref(sub, -1);
270 return NULL;
271 }
272 }
273
274 ast_debug(3, "Created %s MWI subscription for endpoint %s\n", is_solicited ? "solicited" : "unsolicited", sub->id);
275
276 return sub;
277 }
278
279 static int mwi_sub_hash(const void *obj, const int flags)
280 {
281 const struct mwi_subscription *object;
282 const char *key;
283
284 switch (flags & OBJ_SEARCH_MASK) {
285 case OBJ_SEARCH_KEY:
286 key = obj;
287 break;
288 case OBJ_SEARCH_OBJECT:
289 object = obj;
290 key = object->id;
291 break;
292 default:
293 ast_assert(0);
294 return 0;
295 }
296 return ast_str_hash(key);
297 }
298
299 static int mwi_sub_cmp(void *obj, void *arg, int flags)
300 {
301 const struct mwi_subscription *sub_left = obj;
302 const struct mwi_subscription *sub_right = arg;
303 const char *right_key = arg;
304 int cmp;
305
306 switch (flags & OBJ_SEARCH_MASK) {
307 case OBJ_SEARCH_OBJECT:
308 right_key = sub_right->id;
309 /* Fall through */
310 case OBJ_SEARCH_KEY:
311 cmp = strcmp(sub_left->id, right_key);
312 break;
313 case OBJ_SEARCH_PARTIAL_KEY:
314 cmp = strncmp(sub_left->id, right_key, strlen(right_key));
315 break;
316 default:
317 cmp = 0;
318 break;
319 }
320 if (cmp) {
321 return 0;
322 }
323 return CMP_MATCH;
324 }
325
326 static int get_message_count(void *obj, void *arg, int flags)
327 {
328 struct mwi_stasis_subscription *mwi_stasis = obj;
329 struct ast_sip_message_accumulator *counter = arg;
330 struct ast_mwi_state *mwi_state;
331
332 mwi_state = ast_mwi_subscriber_data(mwi_stasis->mwi_subscriber);
333 if (!mwi_state) {
334 return 0;
335 }
336
337 counter->old_msgs += mwi_state->old_msgs;
338 counter->new_msgs += mwi_state->new_msgs;
339
340 ao2_ref(mwi_state, -1);
341
342 return 0;
343 }
344
345 static void set_voicemail_extension(pj_pool_t *pool, pjsip_sip_uri *local_uri,
346 struct ast_sip_message_accumulator *counter, const char *voicemail_extension)
347 {
348 pjsip_sip_uri *account_uri;
349 const char *vm_exten;
350
351 if (ast_strlen_zero(voicemail_extension)) {
352 vm_exten = default_voicemail_extension;
353 } else {
354 vm_exten = voicemail_extension;
355 }
356
357 if (!ast_strlen_zero(vm_exten)) {
358 account_uri = pjsip_uri_clone(pool, local_uri);
359 pj_strdup2(pool, &account_uri->user, vm_exten);
360 pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, account_uri, counter->message_account, sizeof(counter->message_account));
361 }
362 }
363
364 struct unsolicited_mwi_data {
365 struct mwi_subscription *sub;
366 struct ast_sip_endpoint *endpoint;
367 pjsip_evsub_state state;
368 struct ast_sip_message_accumulator *counter;
369 };
370
371 static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flags)
372 {
373 struct unsolicited_mwi_data *mwi_data = arg;
374 struct mwi_subscription *sub = mwi_data->sub;
375 struct ast_sip_endpoint *endpoint = mwi_data->endpoint;
376 pjsip_evsub_state state = mwi_data->state;
377 struct ast_sip_contact *contact = obj;
378 const char *state_name;
379 pjsip_tx_data *tdata;
380 pjsip_sub_state_hdr *sub_state;
381 pjsip_event_hdr *event;
382 pjsip_from_hdr *from;
383 pjsip_sip_uri *from_uri;
384 const pjsip_hdr *allow_events = pjsip_evsub_get_allow_events_hdr(NULL);
385 struct ast_sip_body body;
386 struct ast_str *body_text;
387 struct ast_sip_body_data body_data = {
388 .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
389 .body_data = mwi_data->counter,
390 };
391
392 if (ast_sip_create_request("NOTIFY", NULL, endpoint, NULL, contact, &tdata)) {
393 ast_log(LOG_WARNING, "Unable to create unsolicited NOTIFY request to endpoint %s URI %s\n", sub->id, contact->uri);
394 return 0;
395 }
396
397 body.type = MWI_TYPE;
398 body.subtype = MWI_SUBTYPE;
399 body_text = ast_str_create(64);
400 if (!body_text) {
401 pjsip_tx_data_dec_ref(tdata);
402 return 0;
403 }
404
405 from = PJSIP_MSG_FROM_HDR(tdata->msg);
406 from_uri = pjsip_uri_get_uri(from->uri);
407
408 if (!ast_strlen_zero(endpoint->subscription.mwi.fromuser)) {
409 pj_strdup2(tdata->pool, &from_uri->user, endpoint->subscription.mwi.fromuser);
410 }
411
412 set_voicemail_extension(tdata->pool, from_uri, mwi_data->counter, endpoint->subscription.mwi.voicemail_extension);
413
414 if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, &body_data, &body_text)) {
415 ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
416 ast_free(body_text);
417 pjsip_tx_data_dec_ref(tdata);
418 return 0;
419 }
420
421 body.body_text = ast_str_buffer(body_text);
422
423 switch (state) {
424 case PJSIP_EVSUB_STATE_ACTIVE:
425 state_name = "active";
426 break;
427 case PJSIP_EVSUB_STATE_TERMINATED:
428 default:
429 state_name = "terminated";
430 break;
431 }
432
433 sub_state = pjsip_sub_state_hdr_create(tdata->pool);
434 pj_cstr(&sub_state->sub_state, state_name);
435 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state);
436
437 event = pjsip_event_hdr_create(tdata->pool);
438 pj_cstr(&event->event_type, "message-summary");
439 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) event);
440
441 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_shallow_clone(tdata->pool, allow_events));
442 ast_sip_add_body(tdata, &body);
443 ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL);
444
445 ast_free(body_text);
446
447 return 0;
448 }
449
450 static struct ast_sip_aor *find_aor_for_resource(struct ast_sip_endpoint *endpoint, const char *resource)
451 {
452 struct ast_sip_aor *aor;
453 char *aor_name;
454 char *aors_copy;
455
456 /* Direct match */
457 if ((aor = ast_sip_location_retrieve_aor(resource))) {
458 return aor;
459 }
460
461 if (!endpoint) {
462 return NULL;
463 }
464
465 /*
466 * This may be a subscribe to the voicemail_extension. If so,
467 * look for an aor belonging to this endpoint that has a matching
468 * voicemail_extension.
469 */
470 aors_copy = ast_strdupa(endpoint->aors);
471 while ((aor_name = ast_strip(strsep(&aors_copy, ",")))) {
472 struct ast_sip_aor *check_aor = ast_sip_location_retrieve_aor(aor_name);
473
474 if (!check_aor) {
475 continue;
476 }
477
478 if (!ast_strlen_zero(check_aor->voicemail_extension)
479 && !strcasecmp(check_aor->voicemail_extension, resource)) {
480 ast_debug(1, "Found an aor (%s) that matches voicemail_extension %s\n", aor_name, resource);
481 return check_aor;
482 }
483
484 ao2_ref(check_aor, -1);
485 }
486
487 return NULL;
488 }
489
490 static void send_unsolicited_mwi_notify(struct mwi_subscription *sub,
491 struct ast_sip_message_accumulator *counter)
492 {
493 RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
494 "endpoint", sub->id), ao2_cleanup);
495 char *endpoint_aors;
496 char *aor_name;
497
498 if (!endpoint) {
499 ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
500 sub->id);
501 return;
502 }
503 if (ast_strlen_zero(endpoint->aors)) {
504 ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because the endpoint has no"
505 " configured AORs\n", sub->id);
506 return;
507 }
508
509 endpoint_aors = ast_strdupa(endpoint->aors);
510
511 ast_debug(5, "Sending unsolicited MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
512 sub->id, counter->new_msgs, counter->old_msgs);
513
514 while ((aor_name = ast_strip(strsep(&endpoint_aors, ",")))) {
515 RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
516 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
517 struct unsolicited_mwi_data mwi_data = {
518 .sub = sub,
519 .endpoint = endpoint,
520 .counter = counter,
521 };
522
523 if (!aor) {
524 ast_log(LOG_WARNING, "Unable to locate AOR %s for unsolicited MWI\n", aor_name);
525 continue;
526 }
527
528 contacts = ast_sip_location_retrieve_aor_contacts(aor);
529 if (!contacts || (ao2_container_count(contacts) == 0)) {
530 ast_debug(1, "No contacts bound to AOR %s. Cannot send unsolicited MWI until a contact registers.\n", aor_name);
531 continue;
532 }
533
534 ao2_callback(contacts, OBJ_NODATA, send_unsolicited_mwi_notify_to_contact, &mwi_data);
535 }
536 }
537
538 static void send_mwi_notify(struct mwi_subscription *sub)
539 {
540 struct ast_sip_message_accumulator counter = {
541 .old_msgs = 0,
542 .new_msgs = 0,
543 .message_account[0] = '\0',
544 };
545 struct ast_sip_body_data data = {
546 .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
547 .body_data = &counter,
548 };
549
550 ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);
551
552 if (sub->is_solicited) {
553 const char *resource = ast_sip_subscription_get_resource_name(sub->sip_sub);
554 struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sub->sip_sub);
555 struct ast_sip_aor *aor = find_aor_for_resource(endpoint, resource);
556 pjsip_dialog *dlg = ast_sip_subscription_get_dialog(sub->sip_sub);
557 pjsip_sip_uri *sip_uri = ast_sip_subscription_get_sip_uri(sub->sip_sub);
558
559 if (aor && dlg && sip_uri) {
560 set_voicemail_extension(dlg->pool, sip_uri, &counter, aor->voicemail_extension);
561 }
562
563 ao2_cleanup(aor);
564 ao2_cleanup(endpoint);
565 ast_sip_subscription_notify(sub->sip_sub, &data, sub->terminate);
566
567 return;
568 }
569
570 send_unsolicited_mwi_notify(sub, &counter);
571 }
572
573 static int unsubscribe_stasis(void *obj, void *arg, int flags)
574 {
575 struct mwi_stasis_subscription *mwi_stasis = obj;
576
577 if (mwi_stasis->mwi_subscriber) {
578 ast_debug(3, "Removing stasis subscription to mailbox %s\n", mwi_stasis->mailbox);
579 mwi_stasis->mwi_subscriber = ast_mwi_unsubscribe_and_join(mwi_stasis->mwi_subscriber);
580 }
581 return CMP_MATCH;
582 }
583
584 static int create_unsolicited_mwi_subscriptions(struct ast_sip_endpoint *endpoint,
585 int recreate, int send_now, struct ao2_container *unsolicited_mwi, struct ao2_container *solicited_mwi);
586
587 static void mwi_subscription_shutdown(struct ast_sip_subscription *sub)
588 {
589 struct mwi_subscription *mwi_sub;
590 struct ast_datastore *mwi_datastore;
591 struct ast_sip_endpoint *endpoint = NULL;
592 struct ao2_container *unsolicited_mwi;
593 struct ao2_container *solicited_mwi;
594
595 mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
596 if (!mwi_datastore) {
597 return;
598 }
599
600 mwi_sub = mwi_datastore->data;
601
602 ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
603 ast_sip_subscription_remove_datastore(sub, MWI_DATASTORE);
604 endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", mwi_sub->id);
605
606 ao2_ref(mwi_datastore, -1);
607
608 solicited_mwi = ao2_global_obj_ref(mwi_solicited);
609 if (solicited_mwi) {
610 ao2_unlink(solicited_mwi, mwi_sub);
611 }
612
613 /*
614 * When a solicited subscription is removed it's possible an unsolicited one
615 * needs to be [re-]created. Attempt to establish unsolicited MWI.
616 */
617 unsolicited_mwi = ao2_global_obj_ref(mwi_unsolicited);
618 if (unsolicited_mwi && endpoint) {
619 ao2_lock(unsolicited_mwi);
620 create_unsolicited_mwi_subscriptions(endpoint, 1, 1, unsolicited_mwi, solicited_mwi);
621 ao2_unlock(unsolicited_mwi);
622 ao2_ref(unsolicited_mwi, -1);
623 }
624
625 ao2_cleanup(solicited_mwi);
626 ao2_cleanup(endpoint);
627 }
628
629 static void mwi_ds_destroy(void *data)
630 {
631 struct mwi_subscription *sub = data;
632
633 ao2_ref(sub, -1);
634 }
635
636 static struct ast_datastore_info mwi_ds_info = {
637 .destroy = mwi_ds_destroy,
638 };
639
640 static int add_mwi_datastore(struct mwi_subscription *sub)
641 {
642 struct ast_datastore *mwi_datastore;
643 int res;
644
645 mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, MWI_DATASTORE);
646 if (!mwi_datastore) {
647 return -1;
648 }
649 ao2_ref(sub, +1);
650 mwi_datastore->data = sub;
651
652 /*
653 * NOTE: Adding the datastore to the subscription creates a ref loop
654 * that must be manually broken.
655 */
656 res = ast_sip_subscription_add_datastore(sub->sip_sub, mwi_datastore);
657 ao2_ref(mwi_datastore, -1);
658 return res;
659 }
660
661 /*!
662 * \internal
663 * \brief Determine if an MWI subscription already exists for the given endpoint/mailbox
664 *
665 * Search the given container, and attempt to find out if the given endpoint has a
666 * current subscription within. If so pass back the associated mwi_subscription and
667 * mwi_stasis_subscription objects.
668 *
669 * \note If a subscription is located then the caller is responsible for removing the
670 * references to the passed back mwi_subscription and mwi_stasis_subscription objects.
671 *
672 * \note Must be called with the given container already locked.
673 *
674 * \param container The ao2_container to search
675 * \param endpoint The endpoint to find
676 * \param mailbox The mailbox potentially subscribed
677 * \param[out] mwi_sub May contain the located mwi_subscription
678 * \param[out] mwi_stasis May contain the located mwi_stasis_subscription
679 *
680 * \retval 1 if a subscription was located, 0 otherwise
681 */
682 static int has_mwi_subscription(struct ao2_container *container,
683 struct ast_sip_endpoint *endpoint, const char *mailbox,
684 struct mwi_subscription **mwi_sub, struct mwi_stasis_subscription **mwi_stasis)
685 {
686 struct ao2_iterator *mwi_subs;
687
688 *mwi_sub = NULL;
689 *mwi_stasis = NULL;
690
691 if (!container) {
692 return 0;
693 }
694
695 mwi_subs = ao2_find(container, ast_sorcery_object_get_id(endpoint),
696 OBJ_SEARCH_KEY | OBJ_MULTIPLE | OBJ_NOLOCK);
697 if (!mwi_subs) {
698 return 0;
699 }
700
701 while ((*mwi_sub = ao2_iterator_next(mwi_subs))) {
702 *mwi_stasis = ao2_find((*mwi_sub)->stasis_subs, mailbox, OBJ_SEARCH_KEY);
703 if (*mwi_stasis) {
704 /* If found then caller is responsible for unrefs of passed back objects */
705 break;
706 }
707 ao2_ref(*mwi_sub, -1);
708 }
709
710 ao2_iterator_destroy(mwi_subs);
711 return *mwi_stasis ? 1 : 0;
712 }
713
714 /*!
715 * \internal
716 * \brief Allow and/or replace the unsolicited subscription
717 *
718 * Checks to see if solicited subscription is allowed. If allowed, and an
719 * unsolicited one exists then prepare for replacement by removing the
720 * current unsolicited subscription.
721 *
722 * \param endpoint The endpoint
723 * \param mailbox The mailbox
724 * \param unsolicited_mwi A container of unsolicited mwi objects
725 *
726 * \retval 1 if a solicited subscription is allowed for the endpoint/mailbox
727 * 0 otherwise
728 */
729 static int allow_and_or_replace_unsolicited(struct ast_sip_endpoint *endpoint, const char *mailbox,
730 struct ao2_container *unsolicited_mwi)
731 {
732 struct mwi_subscription *mwi_sub;
733 struct mwi_stasis_subscription *mwi_stasis;
734
735 if (!has_mwi_subscription(unsolicited_mwi, endpoint, mailbox, &mwi_sub, &mwi_stasis)) {
736 /* If no unsolicited subscription then allow the solicited one */
737 return 1;
738 }
739
740 if (!endpoint->subscription.mwi.subscribe_replaces_unsolicited) {
741 /* Has unsolicited subscription and can't replace, so disallow */
742 ao2_ref(mwi_stasis, -1);
743 ao2_ref(mwi_sub, -1);
744 return 0;
745 }
746
747 /*
748 * The unsolicited subscription exists, and it is allowed to be replaced.
749 * So, first remove the unsolicited stasis subscription, and if aggregation
750 * is not enabled then also remove the mwi_subscription object as well.
751 */
752 ast_debug(1, "Unsolicited subscription being replaced by solicited for "
753 "endpoint '%s' mailbox '%s'\n", ast_sorcery_object_get_id(endpoint), mailbox);
754
755 unsubscribe_stasis(mwi_stasis, NULL, 0);
756 ao2_unlink(mwi_sub->stasis_subs, mwi_stasis);
757
758 if (!endpoint->subscription.mwi.aggregate) {
759 ao2_unlink(unsolicited_mwi, mwi_sub);
760 }
761
762 ao2_ref(mwi_stasis, -1);
763 ao2_ref(mwi_sub, -1);
764
765 /* This solicited subscription is replacing an unsolicited one, so allow */
766 return 1;
767 }
768
769 static int send_notify(void *obj, void *arg, int flags);
770
771 /*!
772 * \internal
773 * \brief Determine if an unsolicited MWI subscription is allowed
774 *
775 * \param endpoint The endpoint
776 * \param mailbox The mailbox
777 * \param unsolicited_mwi A container of unsolicited mwi objects
778 * \param solicited_mwi A container of solicited mwi objects
779 *
780 * \retval 1 if an unsolicited subscription is allowed for the endpoint/mailbox
781 * 0 otherwise
782 */
783 static int is_unsolicited_allowed(struct ast_sip_endpoint *endpoint, const char *mailbox,
784 struct ao2_container *unsolicited_mwi, struct ao2_container *solicited_mwi)
785 {
786 struct mwi_subscription *mwi_sub;
787 struct mwi_stasis_subscription *mwi_stasis;
788
789 if (ast_strlen_zero(mailbox)) {
790 return 0;
791 }
792
793 /*
794 * First check if an unsolicited subscription exists. If it does then we don't
795 * want to add another one.
796 */
797 if (has_mwi_subscription(unsolicited_mwi, endpoint, mailbox, &mwi_sub, &mwi_stasis)) {
798 ao2_ref(mwi_stasis, -1);
799 ao2_ref(mwi_sub, -1);
800 return 0;
801 }
802
803 /*
804 * If there is no unsolicited subscription, next check to see if a solicited
805 * subscription exists for the endpoint/mailbox. If not, then allow.
806 */
807 if (!has_mwi_subscription(solicited_mwi, endpoint, mailbox, &mwi_sub, &mwi_stasis)) {
808 return 1;
809 }
810
811 /*
812 * If however, a solicited subscription does exist then we'll need to see if that
813 * subscription is allowed to replace the unsolicited one. If is allowed to replace
814 * then disallow the unsolicited one.
815 */
816 if (endpoint->subscription.mwi.subscribe_replaces_unsolicited) {
817 ao2_ref(mwi_stasis, -1);
818 ao2_ref(mwi_sub, -1);
819 return 0;
820 }
821
822 /* Otherwise, shutdown the solicited subscription and allow the unsolicited */
823 mwi_sub->terminate = 1;
824 send_notify(mwi_sub, NULL, 0);
825
826 ao2_ref(mwi_stasis, -1);
827 ao2_ref(mwi_sub, -1);
828
829 return 1;
830 }
831
832 /*!
833 * \brief Determine if an endpoint is a candidate to be able to subscribe for MWI
834 *
835 * Currently, this just makes sure that the endpoint is not already receiving unsolicted
836 * MWI for any of an AOR's configured mailboxes.
837 *
838 * \param obj The AOR to which the endpoint is subscribing.
839 * \param arg The endpoint that is attempting to subscribe.
840 * \param flags Unused.
841 * \retval 0 Endpoint is a candidate to subscribe to MWI on the AOR.
842 * \retval -1 The endpoint cannot subscribe to MWI on the AOR.
843 */
844 static int mwi_validate_for_aor(void *obj, void *arg, int flags)
845 {
846 struct ast_sip_aor *aor = obj;
847 struct ast_sip_endpoint *endpoint = arg;
848 char *mailboxes;
849 char *mailbox;
850 struct ao2_container *unsolicited_mwi;
851
852 if (ast_strlen_zero(aor->mailboxes)) {
853 return 0;
854 }
855
856 /* A reload could be taking place so lock while checking if allowed */
857 unsolicited_mwi = ao2_global_obj_ref(mwi_unsolicited);
858 if (unsolicited_mwi) {
859 ao2_lock(unsolicited_mwi);
860 }
861
862 mailboxes = ast_strdupa(aor->mailboxes);
863 while ((mailbox = ast_strip(strsep(&mailboxes, ",")))) {
864 if (ast_strlen_zero(mailbox)) {
865 continue;
866 }
867
868 if (!allow_and_or_replace_unsolicited(endpoint, mailbox, unsolicited_mwi)) {
869 ast_debug(1, "Endpoint '%s' already configured for unsolicited MWI for mailbox '%s'. "
870 "Denying MWI subscription to %s\n", ast_sorcery_object_get_id(endpoint), mailbox,
871 ast_sorcery_object_get_id(aor));
872
873 if (unsolicited_mwi) {
874 ao2_unlock(unsolicited_mwi);
875 ao2_ref(unsolicited_mwi, -1);
876 }
877 return -1;
878 }
879 }
880
881 if (unsolicited_mwi) {
882 ao2_unlock(unsolicited_mwi);
883 ao2_ref(unsolicited_mwi, -1);
884 }
885
886 return 0;
887 }
888
889 static int mwi_on_aor(void *obj, void *arg, int flags)
890 {
891 struct ast_sip_aor *aor = obj;
892 struct mwi_subscription *sub = arg;
893 char *mailboxes;
894 char *mailbox;
895
896 if (ast_strlen_zero(aor->mailboxes)) {
897 return 0;
898 }
899
900 mailboxes = ast_strdupa(aor->mailboxes);
901 while ((mailbox = ast_strip(strsep(&mailboxes, ",")))) {
902 struct mwi_stasis_subscription *mwi_stasis_sub;
903
904 if (ast_strlen_zero(mailbox)) {
905 continue;
906 }
907
908 mwi_stasis_sub = mwi_stasis_subscription_alloc(mailbox, sub);
909 if (!mwi_stasis_sub) {
910 continue;
911 }
912
913 ao2_link(sub->stasis_subs, mwi_stasis_sub);
914 ao2_ref(mwi_stasis_sub, -1);
915 }
916
917 return 0;
918 }
919
920 static struct mwi_subscription *mwi_create_subscription(
921 struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
922 {
923 struct mwi_subscription *sub = mwi_subscription_alloc(endpoint, 1, sip_sub);
924
925 if (!sub) {
926 return NULL;
927 }
928
929 if (add_mwi_datastore(sub)) {
930 ast_log(LOG_WARNING, "Unable to add datastore for MWI subscription to %s\n",
931 sub->id);
932 ao2_ref(sub, -1);
933 return NULL;
934 }
935
936 return sub;
937 }
938
939 static struct mwi_subscription *mwi_subscribe_single(
940 struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub, const char *name)
941 {
942 struct ast_sip_aor *aor;
943 struct mwi_subscription *sub;
944
945 aor = find_aor_for_resource(endpoint, name);
946 if (!aor) {
947 ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n", name);
948 return NULL;
949 }
950
951 sub = mwi_create_subscription(endpoint, sip_sub);
952 if (sub) {
953 mwi_on_aor(aor, sub, 0);
954 }
955
956 ao2_ref(aor, -1);
957 return sub;
958 }
959
960 static struct mwi_subscription *mwi_subscribe_all(
961 struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
962 {
963 struct mwi_subscription *sub;
964
965 sub = mwi_create_subscription(endpoint, sip_sub);
966 if (!sub) {
967 return NULL;
968 }
969
970 ast_sip_for_each_aor(endpoint->aors, mwi_on_aor, sub);
971 return sub;
972 }
973
974 static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
975 const char *resource)
976 {
977 RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
978
979 if (ast_strlen_zero(resource)) {
980 if (ast_sip_for_each_aor(endpoint->aors, mwi_validate_for_aor, endpoint)) {
981 return 500;
982 }
983 return 200;
984 }
985
986 aor = find_aor_for_resource(endpoint, resource);
987 if (!aor) {
988 ast_debug(1, "Unable to locate aor %s. MWI subscription failed.\n", resource);
989 return 404;
990 }
991
992 if (ast_strlen_zero(aor->mailboxes)) {
993 ast_debug(1, "AOR %s has no configured mailboxes. MWI subscription failed.\n",
994 resource);
995 return 404;
996 }
997
998 if (mwi_validate_for_aor(aor, endpoint, 0)) {
999 return 500;
1000 }
1001
1002 return 200;
1003 }
1004
1005 static int mwi_subscription_established(struct ast_sip_subscription *sip_sub)
1006 {
1007 const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
1008 struct mwi_subscription *sub;
1009 struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
1010 struct ao2_container *solicited_mwi;
1011
1012 /* no aor in uri? subscribe to all on endpoint */
1013 if (ast_strlen_zero(resource)) {
1014 sub = mwi_subscribe_all(endpoint, sip_sub);
1015 } else {
1016 sub = mwi_subscribe_single(endpoint, sip_sub, resource);
1017 }
1018 if (!sub) {
1019 ao2_cleanup(endpoint);
1020 return -1;
1021 }
1022
1023 if (!ao2_container_count(sub->stasis_subs)) {
1024 /*
1025 * We setup no MWI subscriptions so remove the MWI datastore
1026 * to break the ref loop.
1027 */
1028 ast_sip_subscription_remove_datastore(sip_sub, MWI_DATASTORE);
1029 }
1030
1031 solicited_mwi = ao2_global_obj_ref(mwi_solicited);
1032 if (solicited_mwi) {
1033 ao2_link(solicited_mwi, sub);
1034 ao2_ref(solicited_mwi, -1);
1035 }
1036
1037 ao2_cleanup(sub);
1038 ao2_cleanup(endpoint);
1039 return 0;
1040 }
1041
1042 static void *mwi_get_notify_data(struct ast_sip_subscription *sub)
1043 {
1044 struct ast_sip_message_accumulator *counter;
1045 struct mwi_subscription *mwi_sub;
1046 struct ast_datastore *mwi_datastore;
1047 struct ast_sip_aor *aor;
1048 struct ast_sip_endpoint *endpoint = NULL;
1049
1050 mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
1051 if (!mwi_datastore) {
1052 return NULL;
1053 }
1054 mwi_sub = mwi_datastore->data;
1055
1056 counter = ao2_alloc(sizeof(*counter), NULL);
1057 if (!counter) {
1058 ao2_cleanup(mwi_datastore);
1059 return NULL;
1060 }
1061
1062 endpoint = ast_sip_subscription_get_endpoint(sub);
1063 if ((aor = find_aor_for_resource(endpoint, ast_sip_subscription_get_resource_name(sub)))) {
1064 pjsip_dialog *dlg = ast_sip_subscription_get_dialog(sub);
1065 pjsip_sip_uri *sip_uri = ast_sip_subscription_get_sip_uri(sub);
1066
1067 if (dlg && sip_uri) {
1068 set_voicemail_extension(dlg->pool, sip_uri, counter, aor->voicemail_extension);
1069 }
1070 ao2_ref(aor, -1);
1071 }
1072 ao2_cleanup(endpoint);
1073
1074 ao2_callback(mwi_sub->stasis_subs, OBJ_NODATA, get_message_count, counter);
1075 ao2_cleanup(mwi_datastore);
1076 return counter;
1077 }
1078
1079 static void mwi_subscription_mailboxes_str(struct ao2_container *stasis_subs,
1080 struct ast_str **str)
1081 {
1082 int is_first = 1;
1083 struct mwi_stasis_subscription *node;
1084 struct ao2_iterator i = ao2_iterator_init(stasis_subs, 0);
1085
1086 while ((node = ao2_iterator_next(&i))) {
1087 if (is_first) {
1088 is_first = 0;
1089 ast_str_append(str, 0, "%s", node->mailbox);
1090 } else {
1091 ast_str_append(str, 0, ",%s", node->mailbox);
1092 }
1093 ao2_ref(node, -1);
1094 }
1095 ao2_iterator_destroy(&i);
1096 }
1097
1098 static void mwi_to_ami(struct ast_sip_subscription *sub,
1099 struct ast_str **buf)
1100 {
1101 struct mwi_subscription *mwi_sub;
1102 struct ast_datastore *mwi_datastore;
1103
1104 mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
1105 if (!mwi_datastore) {
1106 return;
1107 }
1108
1109 mwi_sub = mwi_datastore->data;
1110
1111 ast_str_append(buf, 0, "SubscriptionType: mwi\r\n");
1112 ast_str_append(buf, 0, "Mailboxes: ");
1113 mwi_subscription_mailboxes_str(mwi_sub->stasis_subs, buf);
1114 ast_str_append(buf, 0, "\r\n");
1115
1116 ao2_ref(mwi_datastore, -1);
1117 }
1118
1119 static int serialized_notify(void *userdata)
1120 {
1121 struct mwi_subscription *mwi_sub = userdata;
1122
1123 send_mwi_notify(mwi_sub);
1124 ao2_ref(mwi_sub, -1);
1125 return 0;
1126 }
1127
1128 static int serialized_cleanup(void *userdata)
1129 {
1130 struct mwi_subscription *mwi_sub = userdata;
1131
1132 /* This is getting rid of the reference that was added
1133 * just before this serialized task was pushed.
1134 */
1135 ao2_cleanup(mwi_sub);
1136 /* This is getting rid of the reference held by the
1137 * stasis subscription
1138 */
1139 ao2_cleanup(mwi_sub);
1140 return 0;
1141 }
1142
1143 static int send_notify(void *obj, void *arg, int flags)
1144 {
1145 struct mwi_subscription *mwi_sub = obj;
1146 struct ast_taskprocessor *serializer = mwi_sub->is_solicited
1147 ? ast_sip_subscription_get_serializer(mwi_sub->sip_sub)
1148 : ast_serializer_pool_get(mwi_serializer_pool);
1149
1150 if (ast_sip_push_task(serializer, serialized_notify, ao2_bump(mwi_sub))) {
1151 ao2_ref(mwi_sub, -1);
1152 }
1153
1154 return 0;
1155 }
1156
1157 static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
1158 struct stasis_message *msg)
1159 {
1160 struct mwi_subscription *mwi_sub = userdata;
1161
1162 if (stasis_subscription_final_message(sub, msg)) {
1163 if (ast_sip_push_task(ast_serializer_pool_get(mwi_serializer_pool),
1164 serialized_cleanup, ao2_bump(mwi_sub))) {
1165 ao2_ref(mwi_sub, -1);
1166 }
1167 return;
1168 }
1169
1170 if (ast_mwi_state_type() == stasis_message_type(msg)) {
1171 send_notify(mwi_sub, NULL, 0);
1172 }
1173 }
1174
1175 /*!
1176 * \internal
1177 * \brief Create unsolicited MWI subscriptions for an endpoint
1178 *
1179 * \note Call with the unsolicited_mwi container lock held.
1180 *
1181 * \param endpoint An endpoint object
1182 * \param recreate Whether or not unsolicited subscriptions are potentially being recreated
1183 * \param send_now Whether or not to send a notify once the subscription is created
1184 * \param unsolicited_mwi A container of unsolicited mwi objects
1185 * \param solicited_mwi A container of solicited mwi objects
1186 *
1187 * \retval 0
1188 */
1189 static int create_unsolicited_mwi_subscriptions(struct ast_sip_endpoint *endpoint,
1190 int recreate, int send_now, struct ao2_container *unsolicited_mwi, struct ao2_container *solicited_mwi)
1191 {
1192 RAII_VAR(struct mwi_subscription *, aggregate_sub, NULL, ao2_cleanup);
1193 char *mailboxes;
1194 char *mailbox;
1195 int sub_added = 0;
1196
1197 if (ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
1198 return 0;
1199 }
1200
1201 if (endpoint->subscription.mwi.aggregate) {
1202 const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
1203
1204 /* Check if aggregate subscription exists */
1205 aggregate_sub = ao2_find(unsolicited_mwi, endpoint_id, OBJ_SEARCH_KEY | OBJ_NOLOCK);
1206
1207 /*
1208 * If enabled there should only ever exist a single aggregate subscription object
1209 * for an endpoint. So if it exists just return unless subscriptions are potentially
1210 * being added back in. If that's the case then continue.
1211 */
1212 if (aggregate_sub && !recreate) {
1213 return 0;
1214 }
1215
1216 if (!aggregate_sub) {
1217 aggregate_sub = mwi_subscription_alloc(endpoint, 0, NULL);
1218 if (!aggregate_sub) {
1219 return 0; /* No MWI aggregation for you */
1220 }
1221
1222 /*
1223 * Just in case we somehow get in the position of recreating with no previous
1224 * aggregate object, set recreate to false here in order to allow the new
1225 * object to be linked into the container below
1226 */
1227 recreate = 0;
1228 }
1229 }
1230
1231 /* Lock solicited so we don't potentially add to both containers */
1232 if (solicited_mwi) {
1233 ao2_lock(solicited_mwi);
1234 }
1235
1236 mailboxes = ast_strdupa(endpoint->subscription.mwi.mailboxes);
1237 while ((mailbox = ast_strip(strsep(&mailboxes, ",")))) {
1238 struct mwi_subscription *sub;
1239 struct mwi_stasis_subscription *mwi_stasis_sub;
1240
1241 if (!is_unsolicited_allowed(endpoint, mailbox, unsolicited_mwi, solicited_mwi)) {
1242 continue;
1243 }
1244
1245 sub = aggregate_sub ?: mwi_subscription_alloc(endpoint, 0, NULL);
1246 if (!sub) {
1247 continue;
1248 }
1249
1250 mwi_stasis_sub = mwi_stasis_subscription_alloc(mailbox, sub);
1251 if (mwi_stasis_sub) {
1252 ao2_link(sub->stasis_subs, mwi_stasis_sub);
1253 ao2_ref(mwi_stasis_sub, -1);
1254 }
1255 if (!aggregate_sub) {
1256 ao2_link_flags(unsolicited_mwi, sub, OBJ_NOLOCK);
1257 if (send_now) {
1258 send_notify(sub, NULL, 0);
1259 }
1260 ao2_ref(sub, -1);
1261 }
1262
1263 if (aggregate_sub && !sub_added) {
1264 /* If aggregation track if at least one subscription has been added */
1265 sub_added = 1;
1266 }
1267 }
1268
1269 if (aggregate_sub) {
1270 if (ao2_container_count(aggregate_sub->stasis_subs)) {
1271 /* Only link if we're dealing with a new aggregate object */
1272 if (!recreate) {
1273 ao2_link_flags(unsolicited_mwi, aggregate_sub, OBJ_NOLOCK);
1274 }
1275 if (send_now && sub_added) {
1276 send_notify(aggregate_sub, NULL, 0);
1277 }
1278 }
1279 }
1280
1281 if (solicited_mwi) {
1282 ao2_unlock(solicited_mwi);
1283 }
1284
1285 return 0;
1286 }
1287
1288 static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, void *data, int flags)
1289 {
1290 return create_unsolicited_mwi_subscriptions(obj, 0, 0, arg, data);
1291 }
1292
1293 static int unsubscribe(void *obj, void *arg, int flags)
1294 {
1295 struct mwi_subscription *mwi_sub = obj;
1296
1297 ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
1298
1299 return CMP_MATCH;
1300 }
1301
1302 static void create_mwi_subscriptions(void)
1303 {
1304 struct ao2_container *unsolicited_mwi;
1305 struct ao2_container *solicited_mwi;
1306 struct ao2_container *endpoints;
1307 struct ast_variable *var;
1308
1309 unsolicited_mwi = ao2_global_obj_ref(mwi_unsolicited);
1310 if (!unsolicited_mwi) {
1311 return;
1312 }
1313
1314 var = ast_variable_new("mailboxes !=", "", "");
1315
1316 endpoints = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "endpoint",
1317 AST_RETRIEVE_FLAG_MULTIPLE, var);
1318
1319 ast_variables_destroy(var);
1320 if (!endpoints) {
1321 ao2_ref(unsolicited_mwi, -1);
1322 return;
1323 }
1324
1325 solicited_mwi = ao2_global_obj_ref(mwi_solicited);
1326
1327 /* We remove all the old stasis subscriptions first before applying the new configuration. This
1328 * prevents a situation where there might be multiple overlapping stasis subscriptions for an
1329 * endpoint for mailboxes. Though there may be mailbox changes during the gap between unsubscribing
1330 * and resubscribing, up-to-date mailbox state will be sent out to the endpoint when the
1331 * new stasis subscription is established
1332 */
1333 ao2_lock(unsolicited_mwi);
1334 ao2_callback(unsolicited_mwi, OBJ_NOLOCK | OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
1335 ao2_callback_data(endpoints, OBJ_NODATA, create_mwi_subscriptions_for_endpoint, unsolicited_mwi, solicited_mwi);
1336 ao2_unlock(unsolicited_mwi);
1337
1338 ao2_ref(endpoints, -1);
1339 ao2_cleanup(solicited_mwi);
1340 ao2_ref(unsolicited_mwi, -1);
1341 }
1342
1343 /*! \brief Function called to send MWI NOTIFY on any unsolicited mailboxes relating to this AOR */
1344 static int send_contact_notify(void *obj, void *arg, int flags)
1345 {
1346 struct mwi_subscription *mwi_sub = obj;
1347 const char *aor = arg;
1348
1349 if (!mwi_sub->aors || !strstr(mwi_sub->aors, aor)) {
1350 return 0;
1351 }
1352
1353 if (ast_sip_push_task(ast_serializer_pool_get(mwi_serializer_pool),
1354 serialized_notify, ao2_bump(mwi_sub))) {
1355 ao2_ref(mwi_sub, -1);
1356 }
1357
1358 return 0;
1359 }
1360
1361 /*! \brief Create mwi subscriptions and notify */
1362 static void mwi_contact_changed(const struct ast_sip_contact *contact)
1363 {
1364 char *id = ast_strdupa(ast_sorcery_object_get_id(contact));
1365 char *aor = NULL;
1366 struct ast_sip_endpoint *endpoint = NULL;
1367 struct ao2_container *unsolicited_mwi;
1368 struct ao2_container *solicited_mwi;
1369
1370 if (contact->endpoint) {
1371 endpoint = ao2_bump(contact->endpoint);
1372 } else {
1373 if (!ast_strlen_zero(contact->endpoint_name)) {
1374 endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", contact->endpoint_name);
1375 }
1376 }
1377
1378 if (!endpoint || ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
1379 ao2_cleanup(endpoint);
1380 return;
1381 }
1382
1383 unsolicited_mwi = ao2_global_obj_ref(mwi_unsolicited);
1384 if (!unsolicited_mwi) {
1385 ao2_cleanup(endpoint);
1386 return;
1387 }
1388
1389 solicited_mwi = ao2_global_obj_ref(mwi_solicited);
1390
1391 ao2_lock(unsolicited_mwi);
1392 create_unsolicited_mwi_subscriptions(endpoint, 0, 0, unsolicited_mwi, solicited_mwi);
1393 ao2_unlock(unsolicited_mwi);
1394 ao2_cleanup(endpoint);
1395 ao2_cleanup(solicited_mwi);
1396 ao2_ref(unsolicited_mwi, -1);
1397
1398 aor = strsep(&id, ";@");
1399 ao2_callback(unsolicited_mwi, OBJ_NODATA, send_contact_notify, aor);
1400 }
1401
1402 /*! \brief Function called when a contact is updated */
1403 static void mwi_contact_updated(const void *object)
1404 {
1405 mwi_contact_changed(object);
1406 }
1407
1408 /*! \brief Function called when a contact is added */
1409 static void mwi_contact_added(const void *object)
1410 {
1411 mwi_contact_changed(object);
1412 }
1413
1414 /*! \brief Function called when a contact is deleted */
1415 static void mwi_contact_deleted(const void *object)
1416 {
1417 const struct ast_sip_contact *contact = object;
1418 struct ao2_iterator *mwi_subs;
1419 struct mwi_subscription *mwi_sub;
1420 struct ast_sip_endpoint *endpoint = NULL;
1421 struct ast_sip_contact *found_contact;
1422 struct ao2_container *unsolicited_mwi;
1423
1424 if (contact->endpoint) {
1425 endpoint = ao2_bump(contact->endpoint);
1426 } else {
1427 if (!ast_strlen_zero(contact->endpoint_name)) {
1428 endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", contact->endpoint_name);
1429 }
1430 }
1431
1432 if (!endpoint || ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
1433 ao2_cleanup(endpoint);
1434 return;
1435 }
1436
1437 /* Check if there is another contact */
1438 found_contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
1439 ao2_cleanup(endpoint);
1440 if (found_contact) {
1441 ao2_cleanup(found_contact);
1442 return;
1443 }
1444
1445 unsolicited_mwi = ao2_global_obj_ref(mwi_unsolicited);
1446 if (!unsolicited_mwi) {
1447 return;
1448 }
1449
1450 ao2_lock(unsolicited_mwi);
1451 mwi_subs = ao2_find(unsolicited_mwi, contact->endpoint_name,
1452 OBJ_SEARCH_KEY | OBJ_MULTIPLE | OBJ_NOLOCK | OBJ_UNLINK);
1453 if (mwi_subs) {
1454 for (; (mwi_sub = ao2_iterator_next(mwi_subs)); ao2_cleanup(mwi_sub)) {
1455 unsubscribe(mwi_sub, NULL, 0);
1456 }
1457 ao2_iterator_destroy(mwi_subs);
1458 }
1459 ao2_unlock(unsolicited_mwi);
1460 ao2_ref(unsolicited_mwi, -1);
1461 }
1462
1463 /*! \brief Observer for contacts so unsolicited MWI is sent when a contact changes */
1464 static const struct ast_sorcery_observer mwi_contact_observer = {
1465 .created = mwi_contact_added,
1466 .updated = mwi_contact_updated,
1467 .deleted = mwi_contact_deleted,
1468 };
1469
1470 /*! \brief Task invoked to send initial MWI NOTIFY for unsolicited */
1471 static int send_initial_notify_all(void *obj)
1472 {
1473 struct ao2_container *unsolicited_mwi = ao2_global_obj_ref(mwi_unsolicited);
1474
1475 if (unsolicited_mwi) {
1476 ao2_callback(unsolicited_mwi, OBJ_NODATA, send_notify, NULL);
1477 ao2_ref(unsolicited_mwi, -1);
1478 }
1479
1480 return 0;
1481 }
1482
1483 /*! \brief Event callback which fires initial unsolicited MWI NOTIFY messages when we're fully booted */
1484 static void mwi_startup_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
1485 {
1486 struct ast_json_payload *payload;
1487 const char *type;
1488
1489 if (stasis_message_type(message) != ast_manager_get_generic_type()) {
1490 return;
1491 }
1492
1493 payload = stasis_message_data(message);
1494 type = ast_json_string_get(ast_json_object_get(payload->json, "type"));
1495
1496 if (strcmp(type, "FullyBooted")) {
1497 return;
1498 }
1499
1500 ast_sip_push_task(ast_serializer_pool_get(mwi_serializer_pool),
1501 send_initial_notify_all, NULL);
1502
1503 stasis_unsubscribe(sub);
1504 }
1505
1506 static void global_loaded(const char *object_type)
1507 {
1508 ast_free(default_voicemail_extension);
1509 default_voicemail_extension = ast_sip_get_default_voicemail_extension();
1510 ast_serializer_pool_set_alerts(mwi_serializer_pool,
1511 ast_sip_get_mwi_tps_queue_high(), ast_sip_get_mwi_tps_queue_low());
1512 }
1513
1514 static struct ast_sorcery_observer global_observer = {
1515 .loaded = global_loaded,
1516 };
1517
1518 static int reload(void)
1519 {
1520 if (!ast_sip_get_mwi_disable_initial_unsolicited()) {
1521 create_mwi_subscriptions();
1522 }
1523 return 0;
1524 }
1525
1526 static int unload_module(void)
1527 {
1528 /*
1529 * pjsip_evsub_register_pkg is called by ast_sip_register_subscription_handler
1530 * but there is no corresponding unregister function, so unloading
1531 * a module does not remove the event package. If this module is ever
1532 * loaded again, then pjproject will assert and cause a crash.
1533 * For that reason, we must only be allowed to unload when
1534 * asterisk is shutting down. If a pjsip_evsub_unregister_pkg
1535 * API is added in the future then we should go back to unloading
1536 * the module as intended.
1537 */
1538
1539 if (ast_shutdown_final()) {
1540 struct ao2_container *unsolicited_mwi;
1541
1542 ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &global_observer);
1543 ast_sorcery_observer_remove(ast_sip_get_sorcery(), "contact", &mwi_contact_observer);
1544
1545 unsolicited_mwi = ao2_global_obj_replace(mwi_unsolicited, NULL);
1546 if (unsolicited_mwi) {
1547 ao2_callback(unsolicited_mwi, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
1548 ao2_ref(unsolicited_mwi, -1);
1549 }
1550
1551 ao2_global_obj_release(mwi_solicited);
1552
1553 if (ast_serializer_pool_destroy(mwi_serializer_pool)) {
1554 ast_log(LOG_WARNING, "Unload incomplete. Try again later\n");
1555 return -1;
1556 }
1557 mwi_serializer_pool = NULL;
1558
1559 ast_sip_unregister_subscription_handler(&mwi_handler);
1560
1561 ast_free(default_voicemail_extension);
1562 default_voicemail_extension = NULL;
1563 return 0;
1564 } else {
1565 return -1;
1566 }
1567 }
1568
1569 static int load_module(void)
1570 {
1571 struct ao2_container *mwi_container;
1572
1573 if (ast_sip_register_subscription_handler(&mwi_handler)) {
1574 return AST_MODULE_LOAD_DECLINE;
1575 }
1576
1577 mwi_serializer_pool = ast_serializer_taskpool_create("pjsip/mwi",
1578 MWI_SERIALIZER_POOL_SIZE, ast_sip_taskpool(), MAX_UNLOAD_TIMEOUT_TIME);
1579 if (!mwi_serializer_pool) {
1580 ast_log(AST_LOG_WARNING, "Failed to create MWI serializer pool. The default SIP pool will be used for MWI\n");
1581 }
1582
1583 mwi_container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MWI_BUCKETS,
1584 mwi_sub_hash, NULL, mwi_sub_cmp);
1585 if (!mwi_container) {
1586 unload_module();
1587 return AST_MODULE_LOAD_DECLINE;
1588 }
1589 ao2_global_obj_replace_unref(mwi_solicited, mwi_container);
1590 ao2_ref(mwi_container, -1);
1591
1592 mwi_container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MWI_BUCKETS,
1593 mwi_sub_hash, NULL, mwi_sub_cmp);
1594 if (!mwi_container) {
1595 unload_module();
1596 return AST_MODULE_LOAD_DECLINE;
1597 }
1598 ao2_global_obj_replace_unref(mwi_unsolicited, mwi_container);
1599 ao2_ref(mwi_container, -1);
1600
1601 ast_sorcery_observer_add(ast_sip_get_sorcery(), "contact", &mwi_contact_observer);
1602 ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &global_observer);
1603 ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
1604
1605 if (!ast_sip_get_mwi_disable_initial_unsolicited()) {
1606 create_mwi_subscriptions();
1607 if (ast_fully_booted) {
1608 ast_sip_push_task(ast_serializer_pool_get(mwi_serializer_pool),
1609 send_initial_notify_all, NULL);
1610 } else {
1611 struct stasis_subscription *sub;
1612
1613 sub = stasis_subscribe_pool(ast_manager_get_topic(), mwi_startup_event_cb, NULL);
1614 stasis_subscription_accept_message_type(sub, ast_manager_get_generic_type());
1615 stasis_subscription_set_filter(sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
1616 }
1617 }
1618
1619 if (!mwi_serializer_pool) {
1620 /*
1621 * If the mwi serializer pool was unable to be established then the module will
1622 * use the default serializer pool. If this happens prevent manual unloading
1623 * since there would now exist the potential for a crash on unload.
1624 */
1625 ast_module_shutdown_ref(ast_module_info->self);
1626 }
1627
1628 return AST_MODULE_LOAD_SUCCESS;
1629 }
1630
1631 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
1632 .support_level = AST_MODULE_SUPPORT_CORE,
1633 .load = load_module,
1634 .unload = unload_module,
1635 .reload = reload,
1636 .load_pri = AST_MODPRI_CHANNEL_DEPEND + 5,
1637 .requires = "res_pjsip,res_pjsip_pubsub",
1638 );