res_pjsip_mwi.c 45 KB

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