Newer
Older
if (add_mwi_datastore(sub)) {
ast_log(LOG_WARNING, "Unable to add datastore for MWI subscription to %s\n",
sub->id);
ao2_ref(sub, -1);
return NULL;
}
return sub;
}
static struct mwi_subscription *mwi_subscribe_single(
struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub, const char *name)
struct ast_sip_aor *aor;
struct mwi_subscription *sub;
aor = find_aor_for_resource(endpoint, name);
ast_log(LOG_WARNING, "Unable to locate aor %s. MWI subscription failed.\n", name);
sub = mwi_create_subscription(endpoint, sip_sub);
if (sub) {
mwi_on_aor(aor, sub, 0);
return sub;
}
static struct mwi_subscription *mwi_subscribe_all(
struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
struct mwi_subscription *sub;
sub = mwi_create_subscription(endpoint, sip_sub);
if (!sub) {
return NULL;
}
ast_sip_for_each_aor(endpoint->aors, mwi_on_aor, sub);
return sub;
}
static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
const char *resource)
RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
if (ast_strlen_zero(resource)) {
if (ast_sip_for_each_aor(endpoint->aors, mwi_validate_for_aor, endpoint)) {
return 500;
}
return 200;
aor = find_aor_for_resource(endpoint, resource);
ast_debug(1, "Unable to locate aor %s. MWI subscription failed.\n", resource);
return 404;
}
if (ast_strlen_zero(aor->mailboxes)) {
ast_debug(1, "AOR %s has no configured mailboxes. MWI subscription failed.\n",
if (mwi_validate_for_aor(aor, endpoint, 0)) {
return 500;
}
static int mwi_subscription_established(struct ast_sip_subscription *sip_sub)
const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
struct mwi_subscription *sub;
struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
/* no aor in uri? subscribe to all on endpoint */
if (ast_strlen_zero(resource)) {
sub = mwi_subscribe_all(endpoint, sip_sub);
} else {
sub = mwi_subscribe_single(endpoint, sip_sub, resource);
if (!sub) {
ao2_cleanup(endpoint);
return -1;
}
if (!ao2_container_count(sub->stasis_subs)) {
/*
* We setup no MWI subscriptions so remove the MWI datastore
* to break the ref loop.
*/
ast_sip_subscription_remove_datastore(sip_sub, MWI_DATASTORE);
}
Kevin Harwell
committed
ao2_link(solicited_mwi, sub);
ao2_cleanup(sub);
ao2_cleanup(endpoint);
return 0;
static void *mwi_get_notify_data(struct ast_sip_subscription *sub)
struct ast_sip_message_accumulator *counter;
struct mwi_subscription *mwi_sub;
struct ast_datastore *mwi_datastore;
George Joseph
committed
struct ast_sip_aor *aor;
struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sub);
mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
if (!mwi_datastore) {
return NULL;
}
mwi_sub = mwi_datastore->data;
counter = ao2_alloc(sizeof(*counter), NULL);
if (!counter) {
ao2_cleanup(mwi_datastore);
if ((aor = find_aor_for_resource(endpoint, ast_sip_subscription_get_resource_name(sub)))) {
George Joseph
committed
pjsip_dialog *dlg = ast_sip_subscription_get_dialog(sub);
pjsip_sip_uri *sip_uri = ast_sip_subscription_get_sip_uri(sub);
if (dlg && sip_uri) {
set_voicemail_extension(dlg->pool, sip_uri, counter, aor->voicemail_extension);
}
ao2_ref(aor, -1);
}
ao2_cleanup(endpoint);
George Joseph
committed
ao2_callback(mwi_sub->stasis_subs, OBJ_NODATA, get_message_count, counter);
ao2_cleanup(mwi_datastore);
return counter;
static void mwi_subscription_mailboxes_str(struct ao2_container *stasis_subs,
struct ast_str **str)
{
Richard Mudgett
committed
int is_first = 1;
struct mwi_stasis_subscription *node;
struct ao2_iterator i = ao2_iterator_init(stasis_subs, 0);
while ((node = ao2_iterator_next(&i))) {
Richard Mudgett
committed
if (is_first) {
is_first = 0;
ast_str_append(str, 0, "%s", node->mailbox);
Richard Mudgett
committed
} else {
ast_str_append(str, 0, ",%s", node->mailbox);
}
ao2_ref(node, -1);
}
ao2_iterator_destroy(&i);
}
static void mwi_to_ami(struct ast_sip_subscription *sub,
struct ast_str **buf)
{
struct mwi_subscription *mwi_sub;
struct ast_datastore *mwi_datastore;
mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
if (!mwi_datastore) {
return;
}
mwi_sub = mwi_datastore->data;
ast_str_append(buf, 0, "SubscriptionType: mwi\r\n");
ast_str_append(buf, 0, "Mailboxes: ");
mwi_subscription_mailboxes_str(mwi_sub->stasis_subs, buf);
ast_str_append(buf, 0, "\r\n");
ao2_ref(mwi_datastore, -1);
static int serialized_notify(void *userdata)
{
struct mwi_subscription *mwi_sub = userdata;
send_mwi_notify(mwi_sub);
ao2_ref(mwi_sub, -1);
return 0;
}
static int serialized_cleanup(void *userdata)
{
struct mwi_subscription *mwi_sub = userdata;
/* This is getting rid of the reference that was added
* just before this serialized task was pushed.
*/
ao2_cleanup(mwi_sub);
/* This is getting rid of the reference held by the
* stasis subscription
*/
ao2_cleanup(mwi_sub);
return 0;
}
Joshua Colp
committed
static int send_notify(void *obj, void *arg, int flags)
{
struct mwi_subscription *mwi_sub = obj;
Richard Mudgett
committed
struct ast_taskprocessor *serializer = mwi_sub->is_solicited
? ast_sip_subscription_get_serializer(mwi_sub->sip_sub)
: get_mwi_serializer();
Joshua Colp
committed
if (ast_sip_push_task(serializer, serialized_notify, ao2_bump(mwi_sub))) {
ao2_ref(mwi_sub, -1);
}
return 0;
}
static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
struct stasis_message *msg)
{
struct mwi_subscription *mwi_sub = userdata;
if (stasis_subscription_final_message(sub, msg)) {
Joshua Colp
committed
if (ast_sip_push_task(NULL, serialized_cleanup, ao2_bump(mwi_sub))) {
ao2_ref(mwi_sub, -1);
}
if (ast_mwi_state_type() == stasis_message_type(msg)) {
Joshua Colp
committed
send_notify(mwi_sub, NULL, 0);
Kevin Harwell
committed
/*!
* \internal
* \brief Create unsolicited MWI subscriptions for an endpoint
*
* \note Call with the unsolicited_mwi container lock held.
*
* \param endpoint An endpoint object
* \param recreate Whether or not unsolicited subscriptions are potentially being recreated
* \param send_now Whether or not to send a notify once the subscription is created
*
* \retval 0
*/
static int create_unsolicited_mwi_subscriptions(struct ast_sip_endpoint *endpoint,
int recreate, int send_now)
{
RAII_VAR(struct mwi_subscription *, aggregate_sub, NULL, ao2_cleanup);
Kevin Harwell
committed
char *mailboxes;
char *mailbox;
int sub_added = 0;
if (ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
if (endpoint->subscription.mwi.aggregate) {
const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
Kevin Harwell
committed
/* Check if aggregate subscription exists */
aggregate_sub = ao2_find(unsolicited_mwi, endpoint_id, OBJ_SEARCH_KEY | OBJ_NOLOCK);
Kevin Harwell
committed
/*
* If enabled there should only ever exist a single aggregate subscription object
* for an endpoint. So if it exists just return unless subscriptions are potentially
* being added back in. If that's the case then continue.
*/
if (aggregate_sub && !recreate) {
return 0;
}
Kevin Harwell
committed
Kevin Harwell
committed
aggregate_sub = mwi_subscription_alloc(endpoint, 0, NULL);
if (!aggregate_sub) {
return 0; /* No MWI aggregation for you */
}
Kevin Harwell
committed
/* Lock solicited so we don't potentially add to both containers */
ao2_lock(solicited_mwi);
mailboxes = ast_strdupa(endpoint->subscription.mwi.mailboxes);
while ((mailbox = ast_strip(strsep(&mailboxes, ",")))) {
struct mwi_subscription *sub;
struct mwi_stasis_subscription *mwi_stasis_sub;
Kevin Harwell
committed
if (!is_unsolicited_allowed(endpoint, mailbox)) {
continue;
}
sub = aggregate_sub ?: mwi_subscription_alloc(endpoint, 0, NULL);
Kevin Harwell
committed
if (!sub) {
continue;
}
mwi_stasis_sub = mwi_stasis_subscription_alloc(mailbox, sub);
if (mwi_stasis_sub) {
ao2_link(sub->stasis_subs, mwi_stasis_sub);
ao2_ref(mwi_stasis_sub, -1);
Kevin Harwell
committed
if (!aggregate_sub) {
ao2_link_flags(unsolicited_mwi, sub, OBJ_NOLOCK);
Kevin Harwell
committed
if (send_now) {
send_notify(sub, NULL, 0);
}
ao2_ref(sub, -1);
Kevin Harwell
committed
if (aggregate_sub && !sub_added) {
/* If aggregation track if at least one subscription has been added */
sub_added = 1;
}
Kevin Harwell
committed
Mark Michelson
committed
if (aggregate_sub) {
Kevin Harwell
committed
if (ao2_container_count(aggregate_sub->stasis_subs)) {
ao2_link_flags(unsolicited_mwi, aggregate_sub, OBJ_NOLOCK);
if (send_now && sub_added) {
send_notify(aggregate_sub, NULL, 0);
}
} else {
/* No stasis subscriptions then no MWI data to aggregate */
ao2_ref(aggregate_sub, -1);
}
Mark Michelson
committed
}
Kevin Harwell
committed
ao2_unlock(solicited_mwi);
Kevin Harwell
committed
static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, int flags)
{
return create_unsolicited_mwi_subscriptions(obj, 0, 0);
}
static int unsubscribe(void *obj, void *arg, int flags)
{
struct mwi_subscription *mwi_sub = obj;
ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
Joshua Colp
committed
return CMP_MATCH;
}
static void create_mwi_subscriptions(void)
{
struct ao2_container *endpoints;
struct ast_variable *var;
var = ast_variable_new("mailboxes !=", "", "");
endpoints = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "endpoint",
AST_RETRIEVE_FLAG_MULTIPLE, var);
ast_variables_destroy(var);
if (!endpoints) {
return;
}
/* We remove all the old stasis subscriptions first before applying the new configuration. This
* prevents a situation where there might be multiple overlapping stasis subscriptions for an
* endpoint for mailboxes. Though there may be mailbox changes during the gap between unsubscribing
* and resubscribing, up-to-date mailbox state will be sent out to the endpoint when the
* new stasis subscription is established
*/
ao2_lock(unsolicited_mwi);
ao2_callback(unsolicited_mwi, OBJ_NOLOCK | OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
ao2_callback(endpoints, OBJ_NODATA, create_mwi_subscriptions_for_endpoint, NULL);
ao2_unlock(unsolicited_mwi);
ao2_ref(endpoints, -1);
Joshua Colp
committed
/*! \brief Function called to send MWI NOTIFY on any unsolicited mailboxes relating to this AOR */
static int send_contact_notify(void *obj, void *arg, int flags)
{
struct mwi_subscription *mwi_sub = obj;
const char *aor = arg;
if (!mwi_sub->aors || !strstr(mwi_sub->aors, aor)) {
return 0;
}
if (ast_sip_push_task(get_mwi_serializer(), serialized_notify, ao2_bump(mwi_sub))) {
Joshua Colp
committed
ao2_ref(mwi_sub, -1);
}
return 0;
}
/*! \brief Create mwi subscriptions and notify */
static void mwi_contact_changed(const struct ast_sip_contact *contact)
Joshua Colp
committed
{
char *id = ast_strdupa(ast_sorcery_object_get_id(contact));
char *aor = NULL;
struct ast_sip_endpoint *endpoint = NULL;
if (contact->endpoint) {
endpoint = ao2_bump(contact->endpoint);
} else {
if (!ast_strlen_zero(contact->endpoint_name)) {
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", contact->endpoint_name);
}
}
if (!endpoint || ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
ao2_cleanup(endpoint);
Joshua Colp
committed
return;
}
ao2_lock(unsolicited_mwi);
create_mwi_subscriptions_for_endpoint(endpoint, NULL, 0);
ao2_unlock(unsolicited_mwi);
ao2_cleanup(endpoint);
aor = strsep(&id, ";@");
ao2_callback(unsolicited_mwi, OBJ_NODATA, send_contact_notify, aor);
}
/*! \brief Function called when a contact is updated */
static void mwi_contact_updated(const void *object)
{
mwi_contact_changed(object);
}
/*! \brief Function called when a contact is added */
static void mwi_contact_added(const void *object)
{
mwi_contact_changed(object);
Joshua Colp
committed
}
Alexei Gradinari
committed
/*! \brief Function called when a contact is deleted */
static void mwi_contact_deleted(const void *object)
{
const struct ast_sip_contact *contact = object;
struct ao2_iterator *mwi_subs;
struct mwi_subscription *mwi_sub;
struct ast_sip_endpoint *endpoint = NULL;
struct ast_sip_contact *found_contact;
Alexei Gradinari
committed
if (contact->endpoint) {
endpoint = ao2_bump(contact->endpoint);
} else {
if (!ast_strlen_zero(contact->endpoint_name)) {
endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", contact->endpoint_name);
}
}
if (!endpoint || ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
ao2_cleanup(endpoint);
Alexei Gradinari
committed
return;
}
/* Check if there is another contact */
found_contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
ao2_cleanup(endpoint);
Alexei Gradinari
committed
if (found_contact) {
ao2_cleanup(found_contact);
Alexei Gradinari
committed
return;
}
ao2_lock(unsolicited_mwi);
mwi_subs = ao2_find(unsolicited_mwi, contact->endpoint_name,
OBJ_SEARCH_KEY | OBJ_MULTIPLE | OBJ_NOLOCK | OBJ_UNLINK);
if (mwi_subs) {
for (; (mwi_sub = ao2_iterator_next(mwi_subs)); ao2_cleanup(mwi_sub)) {
unsubscribe(mwi_sub, NULL, 0);
}
ao2_iterator_destroy(mwi_subs);
}
ao2_unlock(unsolicited_mwi);
}
Joshua Colp
committed
/*! \brief Observer for contacts so unsolicited MWI is sent when a contact changes */
static const struct ast_sorcery_observer mwi_contact_observer = {
.created = mwi_contact_added,
.updated = mwi_contact_updated,
Alexei Gradinari
committed
.deleted = mwi_contact_deleted,
Joshua Colp
committed
};
/*! \brief Task invoked to send initial MWI NOTIFY for unsolicited */
static int send_initial_notify_all(void *obj)
{
ao2_callback(unsolicited_mwi, OBJ_NODATA, send_notify, NULL);
Joshua Colp
committed
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
return 0;
}
/*! \brief Event callback which fires initial unsolicited MWI NOTIFY messages when we're fully booted */
static void mwi_startup_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
{
struct ast_json_payload *payload;
const char *type;
if (stasis_message_type(message) != ast_manager_get_generic_type()) {
return;
}
payload = stasis_message_data(message);
type = ast_json_string_get(ast_json_object_get(payload->json, "type"));
if (strcmp(type, "FullyBooted")) {
return;
}
ast_sip_push_task(NULL, send_initial_notify_all, NULL);
stasis_unsubscribe(sub);
}
George Joseph
committed
static void global_loaded(const char *object_type)
{
ast_free(default_voicemail_extension);
default_voicemail_extension = ast_sip_get_default_voicemail_extension();
mwi_serializer_set_alert_levels();
George Joseph
committed
}
static struct ast_sorcery_observer global_observer = {
.loaded = global_loaded,
};
static int reload(void)
{
Alexei Gradinari
committed
if (!ast_sip_get_mwi_disable_initial_unsolicited()) {
create_mwi_subscriptions();
}
return 0;
}
static int load_module(void)
{
if (ast_sip_register_subscription_handler(&mwi_handler)) {
return AST_MODULE_LOAD_DECLINE;
}
if (mwi_serializer_pool_setup()) {
ast_log(AST_LOG_WARNING, "Failed to create MWI serializer pool. The default SIP pool will be used for MWI\n");
}
Kevin Harwell
committed
solicited_mwi = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MWI_BUCKETS,
mwi_sub_hash, NULL, mwi_sub_cmp);
if (!solicited_mwi) {
mwi_serializer_pool_shutdown();
ast_sip_unregister_subscription_handler(&mwi_handler);
return AST_MODULE_LOAD_DECLINE;
}
unsolicited_mwi = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MWI_BUCKETS,
mwi_sub_hash, NULL, mwi_sub_cmp);
if (!unsolicited_mwi) {
mwi_serializer_pool_shutdown();
ast_sip_unregister_subscription_handler(&mwi_handler);
Kevin Harwell
committed
ao2_ref(solicited_mwi, -1);
return AST_MODULE_LOAD_DECLINE;
}
Joshua Colp
committed
ast_sorcery_observer_add(ast_sip_get_sorcery(), "contact", &mwi_contact_observer);
George Joseph
committed
ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &global_observer);
ast_sorcery_reload_object(ast_sip_get_sorcery(), "global");
Joshua Colp
committed
if (!ast_sip_get_mwi_disable_initial_unsolicited()) {
Alexei Gradinari
committed
create_mwi_subscriptions();
if (ast_test_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED)) {
ast_sip_push_task(NULL, send_initial_notify_all, NULL);
} else {
struct stasis_subscription *sub;
sub = stasis_subscribe_pool(ast_manager_get_topic(), mwi_startup_event_cb, NULL);
stasis_subscription_accept_message_type(sub, ast_manager_get_generic_type());
stasis_subscription_set_filter(sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
Joshua Colp
committed
}
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &global_observer);
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "contact", &mwi_contact_observer);
ao2_callback(unsolicited_mwi, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
ao2_ref(unsolicited_mwi, -1);
unsolicited_mwi = NULL;
Kevin Harwell
committed
ao2_cleanup(solicited_mwi);
mwi_serializer_pool_shutdown();
ast_sip_unregister_subscription_handler(&mwi_handler);
George Joseph
committed
ast_free(default_voicemail_extension);
default_voicemail_extension = NULL;
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
Rodrigo Ramírez Norambuena
committed
.support_level = AST_MODULE_SUPPORT_CORE,
.load = load_module,
.unload = unload_module,
.reload = reload,
.load_pri = AST_MODPRI_CHANNEL_DEPEND + 5,
.requires = "res_pjsip,res_pjsip_pubsub",