res_pjsip_mwi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <depend>res_pjsip_pubsub</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. #include "asterisk.h"
  25. #include <pjsip.h>
  26. #include <pjsip_simple.h>
  27. #include <pjlib.h>
  28. #include "asterisk/res_pjsip.h"
  29. #include "asterisk/res_pjsip_pubsub.h"
  30. #include "asterisk/res_pjsip_body_generator_types.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/logger.h"
  33. #include "asterisk/astobj2.h"
  34. #include "asterisk/sorcery.h"
  35. #include "asterisk/stasis.h"
  36. #include "asterisk/app.h"
  37. struct mwi_subscription;
  38. AO2_GLOBAL_OBJ_STATIC(unsolicited_mwi);
  39. #define STASIS_BUCKETS 13
  40. #define MWI_BUCKETS 53
  41. #define MWI_TYPE "application"
  42. #define MWI_SUBTYPE "simple-message-summary"
  43. #define MWI_DATASTORE "MWI datastore"
  44. static void mwi_subscription_shutdown(struct ast_sip_subscription *sub);
  45. static void mwi_to_ami(struct ast_sip_subscription *sub, struct ast_str **buf);
  46. static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
  47. const char *resource);
  48. static int mwi_subscription_established(struct ast_sip_subscription *sub);
  49. static void *mwi_get_notify_data(struct ast_sip_subscription *sub);
  50. static struct ast_sip_notifier mwi_notifier = {
  51. .default_accept = MWI_TYPE"/"MWI_SUBTYPE,
  52. .new_subscribe = mwi_new_subscribe,
  53. .subscription_established = mwi_subscription_established,
  54. .get_notify_data = mwi_get_notify_data,
  55. };
  56. static struct ast_sip_subscription_handler mwi_handler = {
  57. .event_name = "message-summary",
  58. .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
  59. .accept = { MWI_TYPE"/"MWI_SUBTYPE, },
  60. .subscription_shutdown = mwi_subscription_shutdown,
  61. .to_ami = mwi_to_ami,
  62. .notifier = &mwi_notifier,
  63. };
  64. /*!
  65. * \brief Wrapper for stasis subscription
  66. *
  67. * An MWI subscription has a container of these. This
  68. * represents a stasis subscription for MWI state.
  69. */
  70. struct mwi_stasis_subscription {
  71. /*! The MWI stasis subscription */
  72. struct stasis_subscription *stasis_sub;
  73. /*! The mailbox corresponding with the MWI subscription. Used as a hash key */
  74. char mailbox[1];
  75. };
  76. /*!
  77. * \brief A subscription for MWI
  78. *
  79. * This subscription is the basis for MWI for an endpoint. Each
  80. * endpoint that uses MWI will have a corresponding mwi_subscription.
  81. *
  82. * This structure acts as the owner for the underlying SIP subscription.
  83. * When the mwi_subscription is destroyed, the SIP subscription dies, too.
  84. * The mwi_subscription's lifetime is governed by its underlying stasis
  85. * subscriptions. When all stasis subscriptions are destroyed, the
  86. * mwi_subscription is destroyed as well.
  87. */
  88. struct mwi_subscription {
  89. /*! Container of \ref mwi_stasis_subscription structures.
  90. * A single MWI subscription may be fore multiple mailboxes, thus
  91. * requiring multiple stasis subscriptions
  92. */
  93. struct ao2_container *stasis_subs;
  94. /*! The SIP subscription. Unsolicited MWI does not use this */
  95. struct ast_sip_subscription *sip_sub;
  96. /*! Is the MWI solicited (i.e. Initiated with an external SUBSCRIBE) ? */
  97. unsigned int is_solicited;
  98. /*! Identifier for the subscription.
  99. * The identifier is the same as the corresponding endpoint's stasis ID.
  100. * Used as a hash key
  101. */
  102. char id[1];
  103. };
  104. static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
  105. struct stasis_message *msg);
  106. static struct mwi_stasis_subscription *mwi_stasis_subscription_alloc(const char *mailbox, struct mwi_subscription *mwi_sub)
  107. {
  108. struct mwi_stasis_subscription *mwi_stasis_sub;
  109. struct stasis_topic *topic;
  110. if (!mwi_sub) {
  111. return NULL;
  112. }
  113. mwi_stasis_sub = ao2_alloc(sizeof(*mwi_stasis_sub) + strlen(mailbox), NULL);
  114. if (!mwi_stasis_sub) {
  115. return NULL;
  116. }
  117. topic = ast_mwi_topic(mailbox);
  118. /* Safe strcpy */
  119. strcpy(mwi_stasis_sub->mailbox, mailbox);
  120. ao2_ref(mwi_sub, +1);
  121. ast_debug(3, "Creating stasis MWI subscription to mailbox %s for endpoint %s\n", mailbox, mwi_sub->id);
  122. mwi_stasis_sub->stasis_sub = stasis_subscribe(topic, mwi_stasis_cb, mwi_sub);
  123. return mwi_stasis_sub;
  124. }
  125. static int stasis_sub_hash(const void *obj, const int flags)
  126. {
  127. const struct mwi_stasis_subscription *object;
  128. const char *key;
  129. switch (flags & OBJ_SEARCH_MASK) {
  130. case OBJ_SEARCH_KEY:
  131. key = obj;
  132. break;
  133. case OBJ_SEARCH_OBJECT:
  134. object = obj;
  135. key = object->mailbox;
  136. break;
  137. default:
  138. ast_assert(0);
  139. return 0;
  140. }
  141. return ast_str_hash(key);
  142. }
  143. static int stasis_sub_cmp(void *obj, void *arg, int flags)
  144. {
  145. const struct mwi_stasis_subscription *sub_left = obj;
  146. const struct mwi_stasis_subscription *sub_right = arg;
  147. const char *right_key = arg;
  148. int cmp;
  149. switch (flags & OBJ_SEARCH_MASK) {
  150. case OBJ_SEARCH_OBJECT:
  151. right_key = sub_right->mailbox;
  152. /* Fall through */
  153. case OBJ_SEARCH_KEY:
  154. cmp = strcmp(sub_left->mailbox, right_key);
  155. break;
  156. case OBJ_SEARCH_PARTIAL_KEY:
  157. cmp = strncmp(sub_left->mailbox, right_key, strlen(right_key));
  158. break;
  159. default:
  160. cmp = 0;
  161. break;
  162. }
  163. if (cmp) {
  164. return 0;
  165. }
  166. return CMP_MATCH;
  167. }
  168. static void mwi_subscription_destructor(void *obj)
  169. {
  170. struct mwi_subscription *sub = obj;
  171. ast_debug(3, "Destroying MWI subscription for endpoint %s\n", sub->id);
  172. ao2_cleanup(sub->sip_sub);
  173. ao2_cleanup(sub->stasis_subs);
  174. }
  175. static struct mwi_subscription *mwi_subscription_alloc(struct ast_sip_endpoint *endpoint,
  176. unsigned int is_solicited, struct ast_sip_subscription *sip_sub)
  177. {
  178. struct mwi_subscription *sub;
  179. const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
  180. sub = ao2_alloc(sizeof(*sub) + strlen(endpoint_id),
  181. mwi_subscription_destructor);
  182. if (!sub) {
  183. return NULL;
  184. }
  185. /* Safe strcpy */
  186. strcpy(sub->id, endpoint_id);
  187. /* Unsolicited MWI doesn't actually result in a SIP subscription being
  188. * created. This is because a SIP subscription associates with a dialog.
  189. * Most devices expect unsolicited MWI NOTIFYs to appear out of dialog. If
  190. * they receive an in-dialog MWI NOTIFY (i.e. with a to-tag), then they
  191. * will reject the NOTIFY with a 481, thus resulting in message-waiting
  192. * state not being updated on the device
  193. */
  194. if (is_solicited) {
  195. sub->sip_sub = ao2_bump(sip_sub);
  196. }
  197. sub->stasis_subs = ao2_container_alloc(STASIS_BUCKETS, stasis_sub_hash, stasis_sub_cmp);
  198. if (!sub->stasis_subs) {
  199. ao2_cleanup(sub);
  200. return NULL;
  201. }
  202. sub->is_solicited = is_solicited;
  203. ast_debug(3, "Created %s MWI subscription for endpoint %s\n", is_solicited ? "solicited" : "unsolicited", sub->id);
  204. return sub;
  205. }
  206. static int mwi_sub_hash(const void *obj, const int flags)
  207. {
  208. const struct mwi_subscription *object;
  209. const char *key;
  210. switch (flags & OBJ_SEARCH_MASK) {
  211. case OBJ_SEARCH_KEY:
  212. key = obj;
  213. break;
  214. case OBJ_SEARCH_OBJECT:
  215. object = obj;
  216. key = object->id;
  217. break;
  218. default:
  219. ast_assert(0);
  220. return 0;
  221. }
  222. return ast_str_hash(key);
  223. }
  224. static int mwi_sub_cmp(void *obj, void *arg, int flags)
  225. {
  226. const struct mwi_subscription *sub_left = obj;
  227. const struct mwi_subscription *sub_right = arg;
  228. const char *right_key = arg;
  229. int cmp;
  230. switch (flags & OBJ_SEARCH_MASK) {
  231. case OBJ_SEARCH_OBJECT:
  232. right_key = sub_right->id;
  233. /* Fall through */
  234. case OBJ_SEARCH_KEY:
  235. cmp = strcmp(sub_left->id, right_key);
  236. break;
  237. case OBJ_SEARCH_PARTIAL_KEY:
  238. cmp = strncmp(sub_left->id, right_key, strlen(right_key));
  239. break;
  240. default:
  241. cmp = 0;
  242. break;
  243. }
  244. if (cmp) {
  245. return 0;
  246. }
  247. return CMP_MATCH;
  248. }
  249. static int get_message_count(void *obj, void *arg, int flags)
  250. {
  251. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  252. struct mwi_stasis_subscription *mwi_stasis = obj;
  253. struct ast_sip_message_accumulator *counter = arg;
  254. struct ast_mwi_state *mwi_state;
  255. msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(), mwi_stasis->mailbox);
  256. if (!msg) {
  257. return 0;
  258. }
  259. mwi_state = stasis_message_data(msg);
  260. counter->old_msgs += mwi_state->old_msgs;
  261. counter->new_msgs += mwi_state->new_msgs;
  262. return 0;
  263. }
  264. struct unsolicited_mwi_data {
  265. struct mwi_subscription *sub;
  266. struct ast_sip_endpoint *endpoint;
  267. pjsip_evsub_state state;
  268. const struct ast_sip_body *body;
  269. };
  270. static int send_unsolicited_mwi_notify_to_contact(void *obj, void *arg, int flags)
  271. {
  272. struct unsolicited_mwi_data *mwi_data = arg;
  273. struct mwi_subscription *sub = mwi_data->sub;
  274. struct ast_sip_endpoint *endpoint = mwi_data->endpoint;
  275. pjsip_evsub_state state = mwi_data->state;
  276. const struct ast_sip_body *body = mwi_data->body;
  277. struct ast_sip_contact *contact = obj;
  278. const char *state_name;
  279. pjsip_tx_data *tdata;
  280. pjsip_sub_state_hdr *sub_state;
  281. pjsip_event_hdr *event;
  282. const pjsip_hdr *allow_events = pjsip_evsub_get_allow_events_hdr(NULL);
  283. if (ast_sip_create_request("NOTIFY", NULL, endpoint, NULL, contact, &tdata)) {
  284. ast_log(LOG_WARNING, "Unable to create unsolicited NOTIFY request to endpoint %s URI %s\n", sub->id, contact->uri);
  285. return 0;
  286. }
  287. if (!ast_strlen_zero(endpoint->subscription.mwi.fromuser)) {
  288. pjsip_fromto_hdr *from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, NULL);
  289. pjsip_name_addr *from_name_addr = (pjsip_name_addr *) from->uri;
  290. pjsip_sip_uri *from_uri = pjsip_uri_get_uri(from_name_addr->uri);
  291. pj_strdup2(tdata->pool, &from_uri->user, endpoint->subscription.mwi.fromuser);
  292. }
  293. switch (state) {
  294. case PJSIP_EVSUB_STATE_ACTIVE:
  295. state_name = "active";
  296. break;
  297. case PJSIP_EVSUB_STATE_TERMINATED:
  298. default:
  299. state_name = "terminated";
  300. break;
  301. }
  302. sub_state = pjsip_sub_state_hdr_create(tdata->pool);
  303. pj_cstr(&sub_state->sub_state, state_name);
  304. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) sub_state);
  305. event = pjsip_event_hdr_create(tdata->pool);
  306. pj_cstr(&event->event_type, "message-summary");
  307. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) event);
  308. pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_shallow_clone(tdata->pool, allow_events));
  309. ast_sip_add_body(tdata, body);
  310. ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL);
  311. return 0;
  312. }
  313. static void send_unsolicited_mwi_notify(struct mwi_subscription *sub,
  314. struct ast_sip_message_accumulator *counter)
  315. {
  316. RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
  317. "endpoint", sub->id), ao2_cleanup);
  318. char *endpoint_aors;
  319. char *aor_name;
  320. struct ast_sip_body body;
  321. struct ast_str *body_text;
  322. struct ast_sip_body_data body_data = {
  323. .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
  324. .body_data = counter,
  325. };
  326. if (!endpoint) {
  327. ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because endpoint does not exist\n",
  328. sub->id);
  329. return;
  330. }
  331. if (ast_strlen_zero(endpoint->aors)) {
  332. ast_log(LOG_WARNING, "Unable to send unsolicited MWI to %s because the endpoint has no"
  333. " configured AORs\n", sub->id);
  334. return;
  335. }
  336. body.type = MWI_TYPE;
  337. body.subtype = MWI_SUBTYPE;
  338. body_text = ast_str_create(64);
  339. if (!body_text) {
  340. return;
  341. }
  342. if (ast_sip_pubsub_generate_body_content(body.type, body.subtype, &body_data, &body_text)) {
  343. ast_log(LOG_WARNING, "Unable to generate SIP MWI NOTIFY body.\n");
  344. ast_free(body_text);
  345. return;
  346. }
  347. body.body_text = ast_str_buffer(body_text);
  348. endpoint_aors = ast_strdupa(endpoint->aors);
  349. ast_debug(5, "Sending unsolicited MWI NOTIFY to endpoint %s, new messages: %d, old messages: %d\n",
  350. sub->id, counter->new_msgs, counter->old_msgs);
  351. while ((aor_name = strsep(&endpoint_aors, ","))) {
  352. RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
  353. RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
  354. struct unsolicited_mwi_data mwi_data = {
  355. .sub = sub,
  356. .endpoint = endpoint,
  357. .body = &body,
  358. };
  359. if (!aor) {
  360. ast_log(LOG_WARNING, "Unable to locate AOR %s for unsolicited MWI\n", aor_name);
  361. continue;
  362. }
  363. contacts = ast_sip_location_retrieve_aor_contacts(aor);
  364. if (!contacts || (ao2_container_count(contacts) == 0)) {
  365. ast_log(LOG_WARNING, "No contacts bound to AOR %s. Cannot send unsolicited MWI.\n", aor_name);
  366. continue;
  367. }
  368. ao2_callback(contacts, OBJ_NODATA, send_unsolicited_mwi_notify_to_contact, &mwi_data);
  369. }
  370. ast_free(body_text);
  371. }
  372. static void send_mwi_notify(struct mwi_subscription *sub)
  373. {
  374. struct ast_sip_message_accumulator counter = {
  375. .old_msgs = 0,
  376. .new_msgs = 0,
  377. };
  378. struct ast_sip_body_data data = {
  379. .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
  380. .body_data = &counter,
  381. };
  382. ao2_callback(sub->stasis_subs, OBJ_NODATA, get_message_count, &counter);
  383. if (sub->is_solicited) {
  384. ast_sip_subscription_notify(sub->sip_sub, &data, 0);
  385. return;
  386. }
  387. send_unsolicited_mwi_notify(sub, &counter);
  388. }
  389. static int unsubscribe_stasis(void *obj, void *arg, int flags)
  390. {
  391. struct mwi_stasis_subscription *mwi_stasis = obj;
  392. if (mwi_stasis->stasis_sub) {
  393. ast_debug(3, "Removing stasis subscription to mailbox %s\n", mwi_stasis->mailbox);
  394. mwi_stasis->stasis_sub = stasis_unsubscribe(mwi_stasis->stasis_sub);
  395. }
  396. return CMP_MATCH;
  397. }
  398. static void mwi_subscription_shutdown(struct ast_sip_subscription *sub)
  399. {
  400. struct mwi_subscription *mwi_sub;
  401. RAII_VAR(struct ast_datastore *, mwi_datastore,
  402. ast_sip_subscription_get_datastore(sub, MWI_DATASTORE), ao2_cleanup);
  403. if (!mwi_datastore) {
  404. return;
  405. }
  406. mwi_sub = mwi_datastore->data;
  407. ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
  408. }
  409. static struct ast_datastore_info mwi_ds_info = { };
  410. static int add_mwi_datastore(struct mwi_subscription *sub)
  411. {
  412. RAII_VAR(struct ast_datastore *, mwi_datastore, NULL, ao2_cleanup);
  413. mwi_datastore = ast_sip_subscription_alloc_datastore(&mwi_ds_info, MWI_DATASTORE);
  414. if (!mwi_datastore) {
  415. return -1;
  416. }
  417. mwi_datastore->data = sub;
  418. ast_sip_subscription_add_datastore(sub->sip_sub, mwi_datastore);
  419. return 0;
  420. }
  421. /*!
  422. * \brief Determines if an endpoint is receiving unsolicited MWI for a particular mailbox.
  423. *
  424. * \param endpoint The endpoint to check
  425. * \param mailbox The candidate mailbox
  426. * \retval 0 The endpoint does not receive unsolicited MWI for this mailbox
  427. * \retval 1 The endpoint receives unsolicited MWI for this mailbox
  428. */
  429. static int endpoint_receives_unsolicited_mwi_for_mailbox(struct ast_sip_endpoint *endpoint,
  430. const char *mailbox)
  431. {
  432. struct ao2_container *unsolicited = ao2_global_obj_ref(unsolicited_mwi);
  433. struct ao2_iterator *mwi_subs;
  434. struct mwi_subscription *mwi_sub;
  435. const char *endpoint_id = ast_sorcery_object_get_id(endpoint);
  436. int ret = 0;
  437. if (!unsolicited) {
  438. return 0;
  439. }
  440. mwi_subs = ao2_find(unsolicited, endpoint_id, OBJ_SEARCH_KEY | OBJ_MULTIPLE);
  441. ao2_cleanup(unsolicited);
  442. if (!mwi_subs) {
  443. return 0;
  444. }
  445. for (; (mwi_sub = ao2_iterator_next(mwi_subs)) && !ret; ao2_cleanup(mwi_sub)) {
  446. struct mwi_stasis_subscription *mwi_stasis;
  447. mwi_stasis = ao2_find(mwi_sub->stasis_subs, mailbox, OBJ_SEARCH_KEY);
  448. if (mwi_stasis) {
  449. ret = 1;
  450. ao2_cleanup(mwi_stasis);
  451. }
  452. }
  453. ao2_iterator_destroy(mwi_subs);
  454. return ret;
  455. }
  456. /*!
  457. * \brief Determine if an endpoint is a candidate to be able to subscribe for MWI
  458. *
  459. * Currently, this just makes sure that the endpoint is not already receiving unsolicted
  460. * MWI for any of an AOR's configured mailboxes.
  461. *
  462. * \param obj The AOR to which the endpoint is subscribing.
  463. * \param arg The endpoint that is attempting to subscribe.
  464. * \param flags Unused.
  465. * \retval 0 Endpoint is a candidate to subscribe to MWI on the AOR.
  466. * \retval -1 The endpoint cannot subscribe to MWI on the AOR.
  467. */
  468. static int mwi_validate_for_aor(void *obj, void *arg, int flags)
  469. {
  470. struct ast_sip_aor *aor = obj;
  471. struct ast_sip_endpoint *endpoint = arg;
  472. char *mailboxes;
  473. char *mailbox;
  474. if (ast_strlen_zero(aor->mailboxes)) {
  475. return 0;
  476. }
  477. mailboxes = ast_strdupa(aor->mailboxes);
  478. while ((mailbox = strsep(&mailboxes, ","))) {
  479. if (endpoint_receives_unsolicited_mwi_for_mailbox(endpoint, mailbox)) {
  480. ast_log(LOG_NOTICE, "Endpoint '%s' already configured for unsolicited MWI for mailbox '%s'. "
  481. "Denying MWI subscription to %s\n", ast_sorcery_object_get_id(endpoint), mailbox,
  482. ast_sorcery_object_get_id(aor));
  483. return -1;
  484. }
  485. }
  486. return 0;
  487. }
  488. static int mwi_on_aor(void *obj, void *arg, int flags)
  489. {
  490. struct ast_sip_aor *aor = obj;
  491. struct mwi_subscription *sub = arg;
  492. char *mailboxes;
  493. char *mailbox;
  494. if (ast_strlen_zero(aor->mailboxes)) {
  495. return 0;
  496. }
  497. mailboxes = ast_strdupa(aor->mailboxes);
  498. while ((mailbox = strsep(&mailboxes, ","))) {
  499. RAII_VAR(struct mwi_stasis_subscription *, mwi_stasis_sub,
  500. mwi_stasis_subscription_alloc(mailbox, sub), ao2_cleanup);
  501. if (mwi_stasis_sub) {
  502. ao2_link(sub->stasis_subs, mwi_stasis_sub);
  503. }
  504. }
  505. return 0;
  506. }
  507. static struct mwi_subscription *mwi_create_subscription(
  508. struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
  509. {
  510. struct mwi_subscription *sub = mwi_subscription_alloc(endpoint, 1, sip_sub);
  511. if (!sub) {
  512. return NULL;
  513. }
  514. if (add_mwi_datastore(sub)) {
  515. ast_log(LOG_WARNING, "Unable to allocate datastore on MWI "
  516. "subscription from %s\n", sub->id);
  517. ao2_ref(sub, -1);
  518. return NULL;
  519. }
  520. return sub;
  521. }
  522. static struct mwi_subscription *mwi_subscribe_single(
  523. struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub, const char *name)
  524. {
  525. RAII_VAR(struct ast_sip_aor *, aor,
  526. ast_sip_location_retrieve_aor(name), ao2_cleanup);
  527. struct mwi_subscription *sub;
  528. if (!aor) {
  529. /*! I suppose it's possible for the AOR to disappear on us
  530. * between accepting the subscription and sending the first
  531. * NOTIFY...
  532. */
  533. ast_log(LOG_WARNING, "Unable to locate aor %s. MWI "
  534. "subscription failed.\n", name);
  535. return NULL;
  536. }
  537. if (!(sub = mwi_create_subscription(endpoint, sip_sub))) {
  538. return NULL;
  539. }
  540. mwi_on_aor(aor, sub, 0);
  541. return sub;
  542. }
  543. static struct mwi_subscription *mwi_subscribe_all(
  544. struct ast_sip_endpoint *endpoint, struct ast_sip_subscription *sip_sub)
  545. {
  546. struct mwi_subscription *sub;
  547. sub = mwi_create_subscription(endpoint, sip_sub);
  548. if (!sub) {
  549. return NULL;
  550. }
  551. ast_sip_for_each_aor(endpoint->aors, mwi_on_aor, sub);
  552. return sub;
  553. }
  554. static int mwi_new_subscribe(struct ast_sip_endpoint *endpoint,
  555. const char *resource)
  556. {
  557. struct ast_sip_aor *aor;
  558. if (ast_strlen_zero(resource)) {
  559. if (ast_sip_for_each_aor(endpoint->aors, mwi_validate_for_aor, endpoint)) {
  560. return 500;
  561. }
  562. return 200;
  563. }
  564. aor = ast_sip_location_retrieve_aor(resource);
  565. if (!aor) {
  566. ast_log(LOG_WARNING, "Unable to locate aor %s. MWI "
  567. "subscription failed.\n", resource);
  568. return 404;
  569. }
  570. if (ast_strlen_zero(aor->mailboxes)) {
  571. ast_log(LOG_WARNING, "AOR %s has no configured mailboxes. "
  572. "MWI subscription failed\n", resource);
  573. return 404;
  574. }
  575. if (mwi_validate_for_aor(aor, endpoint, 0)) {
  576. return 500;
  577. }
  578. return 200;
  579. }
  580. static int mwi_subscription_established(struct ast_sip_subscription *sip_sub)
  581. {
  582. const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
  583. struct mwi_subscription *sub;
  584. struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
  585. /* no aor in uri? subscribe to all on endpoint */
  586. if (ast_strlen_zero(resource)) {
  587. sub = mwi_subscribe_all(endpoint, sip_sub);
  588. } else {
  589. sub = mwi_subscribe_single(endpoint, sip_sub, resource);
  590. }
  591. if (!sub) {
  592. ao2_cleanup(endpoint);
  593. return -1;
  594. }
  595. ao2_cleanup(sub);
  596. ao2_cleanup(endpoint);
  597. return 0;
  598. }
  599. static void *mwi_get_notify_data(struct ast_sip_subscription *sub)
  600. {
  601. struct ast_sip_message_accumulator *counter;
  602. struct mwi_subscription *mwi_sub;
  603. struct ast_datastore *mwi_datastore;
  604. mwi_datastore = ast_sip_subscription_get_datastore(sub, MWI_DATASTORE);
  605. if (!mwi_datastore) {
  606. return NULL;
  607. }
  608. mwi_sub = mwi_datastore->data;
  609. counter = ao2_alloc(sizeof(*counter), NULL);
  610. if (!counter) {
  611. ao2_cleanup(mwi_datastore);
  612. return NULL;
  613. }
  614. ao2_callback(mwi_sub->stasis_subs, OBJ_NODATA, get_message_count, counter);
  615. ao2_cleanup(mwi_datastore);
  616. return counter;
  617. }
  618. static void mwi_subscription_mailboxes_str(struct ao2_container *stasis_subs,
  619. struct ast_str **str)
  620. {
  621. int num = ao2_container_count(stasis_subs);
  622. struct mwi_stasis_subscription *node;
  623. struct ao2_iterator i = ao2_iterator_init(stasis_subs, 0);
  624. while ((node = ao2_iterator_next(&i))) {
  625. if (--num) {
  626. ast_str_append(str, 0, "%s,", node->mailbox);
  627. } else {
  628. ast_str_append(str, 0, "%s", node->mailbox);
  629. }
  630. ao2_ref(node, -1);
  631. }
  632. ao2_iterator_destroy(&i);
  633. }
  634. static void mwi_to_ami(struct ast_sip_subscription *sub,
  635. struct ast_str **buf)
  636. {
  637. struct mwi_subscription *mwi_sub;
  638. RAII_VAR(struct ast_datastore *, mwi_datastore,
  639. ast_sip_subscription_get_datastore(sub, MWI_DATASTORE), ao2_cleanup);
  640. if (!mwi_datastore) {
  641. return;
  642. }
  643. mwi_sub = mwi_datastore->data;
  644. ast_str_append(buf, 0, "SubscriptionType: mwi\r\n");
  645. ast_str_append(buf, 0, "Mailboxes: ");
  646. mwi_subscription_mailboxes_str(mwi_sub->stasis_subs, buf);
  647. ast_str_append(buf, 0, "\r\n");
  648. }
  649. static int serialized_notify(void *userdata)
  650. {
  651. struct mwi_subscription *mwi_sub = userdata;
  652. send_mwi_notify(mwi_sub);
  653. ao2_ref(mwi_sub, -1);
  654. return 0;
  655. }
  656. static int serialized_cleanup(void *userdata)
  657. {
  658. struct mwi_subscription *mwi_sub = userdata;
  659. /* This is getting rid of the reference that was added
  660. * just before this serialized task was pushed.
  661. */
  662. ao2_cleanup(mwi_sub);
  663. /* This is getting rid of the reference held by the
  664. * stasis subscription
  665. */
  666. ao2_cleanup(mwi_sub);
  667. return 0;
  668. }
  669. static void mwi_stasis_cb(void *userdata, struct stasis_subscription *sub,
  670. struct stasis_message *msg)
  671. {
  672. struct mwi_subscription *mwi_sub = userdata;
  673. if (stasis_subscription_final_message(sub, msg)) {
  674. ao2_ref(mwi_sub, +1);
  675. ast_sip_push_task(NULL, serialized_cleanup, mwi_sub);
  676. return;
  677. }
  678. if (ast_mwi_state_type() == stasis_message_type(msg)) {
  679. struct ast_taskprocessor *serializer = mwi_sub->is_solicited ? ast_sip_subscription_get_serializer(mwi_sub->sip_sub) : NULL;
  680. ao2_ref(mwi_sub, +1);
  681. ast_sip_push_task(serializer, serialized_notify, mwi_sub);
  682. }
  683. }
  684. static int create_mwi_subscriptions_for_endpoint(void *obj, void *arg, int flags)
  685. {
  686. RAII_VAR(struct mwi_subscription *, aggregate_sub, NULL, ao2_cleanup);
  687. struct ast_sip_endpoint *endpoint = obj;
  688. struct ao2_container *mwi_subscriptions = arg;
  689. char *mailboxes;
  690. char *mailbox;
  691. if (ast_strlen_zero(endpoint->subscription.mwi.mailboxes)) {
  692. return 0;
  693. }
  694. if (endpoint->subscription.mwi.aggregate) {
  695. aggregate_sub = mwi_subscription_alloc(endpoint, 0, NULL);
  696. if (!aggregate_sub) {
  697. return 0;
  698. }
  699. }
  700. mailboxes = ast_strdupa(endpoint->subscription.mwi.mailboxes);
  701. while ((mailbox = strsep(&mailboxes, ","))) {
  702. struct mwi_subscription *sub = aggregate_sub ?:
  703. mwi_subscription_alloc(endpoint, 0, NULL);
  704. RAII_VAR(struct mwi_stasis_subscription *, mwi_stasis_sub,
  705. mwi_stasis_subscription_alloc(mailbox, sub), ao2_cleanup);
  706. if (mwi_stasis_sub) {
  707. ao2_link(sub->stasis_subs, mwi_stasis_sub);
  708. }
  709. if (!aggregate_sub) {
  710. ao2_link(mwi_subscriptions, sub);
  711. ao2_cleanup(sub);
  712. }
  713. }
  714. if (aggregate_sub) {
  715. ao2_link(mwi_subscriptions, aggregate_sub);
  716. }
  717. return 0;
  718. }
  719. static int unsubscribe(void *obj, void *arg, int flags)
  720. {
  721. struct mwi_subscription *mwi_sub = obj;
  722. ao2_callback(mwi_sub->stasis_subs, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe_stasis, NULL);
  723. return CMP_MATCH;
  724. }
  725. static void create_mwi_subscriptions(void)
  726. {
  727. struct ao2_container *mwi_subscriptions = ao2_container_alloc(MWI_BUCKETS, mwi_sub_hash, mwi_sub_cmp);
  728. RAII_VAR(struct ao2_container *, old_mwi_subscriptions, ao2_global_obj_ref(unsolicited_mwi), ao2_cleanup);
  729. RAII_VAR(struct ao2_container *, endpoints, ast_sorcery_retrieve_by_fields(
  730. ast_sip_get_sorcery(), "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL),
  731. ao2_cleanup);
  732. if (!mwi_subscriptions) {
  733. return;
  734. }
  735. /* We remove all the old stasis subscriptions first before applying the new configuration. This
  736. * prevents a situation where there might be multiple overlapping stasis subscriptions for an
  737. * endpoint for mailboxes. Though there may be mailbox changes during the gap between unsubscribing
  738. * and resubscribing, up-to-date mailbox state will be sent out to the endpoint when the
  739. * new stasis subscription is established
  740. */
  741. if (old_mwi_subscriptions) {
  742. ao2_callback(old_mwi_subscriptions, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
  743. }
  744. ao2_callback(endpoints, OBJ_NODATA, create_mwi_subscriptions_for_endpoint, mwi_subscriptions);
  745. ao2_global_obj_replace_unref(unsolicited_mwi, mwi_subscriptions);
  746. ao2_ref(mwi_subscriptions, -1);
  747. }
  748. static int reload(void)
  749. {
  750. create_mwi_subscriptions();
  751. return 0;
  752. }
  753. static int load_module(void)
  754. {
  755. CHECK_PJSIP_MODULE_LOADED();
  756. if (ast_sip_register_subscription_handler(&mwi_handler)) {
  757. return AST_MODULE_LOAD_DECLINE;
  758. }
  759. create_mwi_subscriptions();
  760. return AST_MODULE_LOAD_SUCCESS;
  761. }
  762. static int unload_module(void)
  763. {
  764. RAII_VAR(struct ao2_container *, mwi_subscriptions, ao2_global_obj_ref(unsolicited_mwi), ao2_cleanup);
  765. if (mwi_subscriptions) {
  766. ao2_callback(mwi_subscriptions, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, unsubscribe, NULL);
  767. ao2_global_obj_release(unsolicited_mwi);
  768. }
  769. ast_sip_unregister_subscription_handler(&mwi_handler);
  770. return 0;
  771. }
  772. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP MWI resource",
  773. .support_level = AST_MODULE_SUPPORT_CORE,
  774. .load = load_module,
  775. .unload = unload_module,
  776. .reload = reload,
  777. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  778. );