res_pjsip_exten_state.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Kevin Harwell <kharwell@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/app.h"
  36. #include "asterisk/taskprocessor.h"
  37. #define BODY_SIZE 1024
  38. #define EVENT_TYPE_SIZE 50
  39. /*!
  40. * \brief A subscription for extension state
  41. *
  42. * This structure acts as the owner for the underlying SIP subscription. It
  43. * also keeps a pointer to an associated "provider" so when a state changes
  44. * a notify data creator is quickly accessible.
  45. */
  46. struct exten_state_subscription {
  47. /*! Watcher id when registering for extension state changes */
  48. int id;
  49. /*! The SIP subscription */
  50. struct ast_sip_subscription *sip_sub;
  51. /*! The serializer to use for notifications */
  52. struct ast_taskprocessor *serializer;
  53. /*! Context in which subscription looks for updates */
  54. char context[AST_MAX_CONTEXT];
  55. /*! Extension within the context to receive updates from */
  56. char exten[AST_MAX_EXTENSION];
  57. /*! The subscription's user agent */
  58. char *user_agent;
  59. /*! The last known extension state */
  60. enum ast_extension_states last_exten_state;
  61. /*! The last known presence state */
  62. enum ast_presence_state last_presence_state;
  63. };
  64. #define DEFAULT_PRESENCE_BODY "application/pidf+xml"
  65. #define DEFAULT_DIALOG_BODY "application/dialog-info+xml"
  66. static void subscription_shutdown(struct ast_sip_subscription *sub);
  67. static int new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource);
  68. static int subscription_established(struct ast_sip_subscription *sub);
  69. static void *get_notify_data(struct ast_sip_subscription *sub);
  70. static void to_ami(struct ast_sip_subscription *sub,
  71. struct ast_str **buf);
  72. struct ast_sip_notifier presence_notifier = {
  73. .default_accept = DEFAULT_PRESENCE_BODY,
  74. .new_subscribe = new_subscribe,
  75. .subscription_established = subscription_established,
  76. .get_notify_data = get_notify_data,
  77. };
  78. struct ast_sip_notifier dialog_notifier = {
  79. .default_accept = DEFAULT_DIALOG_BODY,
  80. .new_subscribe = new_subscribe,
  81. .subscription_established = subscription_established,
  82. .get_notify_data = get_notify_data,
  83. };
  84. struct ast_sip_subscription_handler presence_handler = {
  85. .event_name = "presence",
  86. .body_type = AST_SIP_EXTEN_STATE_DATA,
  87. .accept = { DEFAULT_PRESENCE_BODY, },
  88. .subscription_shutdown = subscription_shutdown,
  89. .to_ami = to_ami,
  90. .notifier = &presence_notifier,
  91. };
  92. struct ast_sip_subscription_handler dialog_handler = {
  93. .event_name = "dialog",
  94. .body_type = AST_SIP_EXTEN_STATE_DATA,
  95. .accept = { DEFAULT_DIALOG_BODY, },
  96. .subscription_shutdown = subscription_shutdown,
  97. .to_ami = to_ami,
  98. .notifier = &dialog_notifier,
  99. };
  100. static void exten_state_subscription_destructor(void *obj)
  101. {
  102. struct exten_state_subscription *sub = obj;
  103. ast_free(sub->user_agent);
  104. ast_sip_subscription_destroy(sub->sip_sub);
  105. ast_taskprocessor_unreference(sub->serializer);
  106. }
  107. static char *get_user_agent(const struct ast_sip_subscription *sip_sub)
  108. {
  109. size_t size;
  110. char *user_agent = NULL;
  111. pjsip_user_agent_hdr *user_agent_hdr = ast_sip_subscription_get_header(
  112. sip_sub, "User-Agent");
  113. if (!user_agent_hdr) {
  114. return NULL;
  115. }
  116. size = pj_strlen(&user_agent_hdr->hvalue) + 1;
  117. user_agent = ast_malloc(size);
  118. ast_copy_pj_str(user_agent, &user_agent_hdr->hvalue, size);
  119. return ast_str_to_lower(user_agent);
  120. }
  121. /*!
  122. * \internal
  123. * \brief Initialize the last extension state to something outside
  124. * its usual states.
  125. */
  126. #define INITIAL_LAST_EXTEN_STATE -3
  127. /*!
  128. * \internal
  129. * \brief Allocates an exten_state_subscription object.
  130. *
  131. * Creates the underlying SIP subscription for the given request. First makes
  132. * sure that there are registered handler and provider objects available.
  133. */
  134. static struct exten_state_subscription *exten_state_subscription_alloc(
  135. struct ast_sip_subscription *sip_sub, struct ast_sip_endpoint *endpoint)
  136. {
  137. struct exten_state_subscription * exten_state_sub;
  138. exten_state_sub = ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor);
  139. if (!exten_state_sub) {
  140. return NULL;
  141. }
  142. exten_state_sub->sip_sub = sip_sub;
  143. /* We keep our own reference to the serializer as there is no guarantee in state_changed
  144. * that the subscription tree is still valid when it is called. This can occur when
  145. * the subscription is terminated at around the same time as the state_changed
  146. * callback is invoked.
  147. */
  148. exten_state_sub->serializer = ao2_bump(ast_sip_subscription_get_serializer(sip_sub));
  149. exten_state_sub->last_exten_state = INITIAL_LAST_EXTEN_STATE;
  150. exten_state_sub->last_presence_state = AST_PRESENCE_NOT_SET;
  151. exten_state_sub->user_agent = get_user_agent(sip_sub);
  152. return exten_state_sub;
  153. }
  154. struct notify_task_data {
  155. struct ast_sip_exten_state_data exten_state_data;
  156. struct exten_state_subscription *exten_state_sub;
  157. int terminate;
  158. };
  159. static void notify_task_data_destructor(void *obj)
  160. {
  161. struct notify_task_data *task_data = obj;
  162. ao2_ref(task_data->exten_state_sub, -1);
  163. ao2_cleanup(task_data->exten_state_data.device_state_info);
  164. ast_free(task_data->exten_state_data.presence_subtype);
  165. ast_free(task_data->exten_state_data.presence_message);
  166. ast_free(task_data->exten_state_data.user_agent);
  167. }
  168. static struct notify_task_data *alloc_notify_task_data(char *exten, struct exten_state_subscription *exten_state_sub,
  169. struct ast_state_cb_info *info)
  170. {
  171. struct notify_task_data *task_data =
  172. ao2_alloc(sizeof(*task_data), notify_task_data_destructor);
  173. if (!task_data) {
  174. ast_log(LOG_WARNING, "Unable to create notify task data\n");
  175. return NULL;
  176. }
  177. task_data->exten_state_sub = exten_state_sub;
  178. task_data->exten_state_sub->last_exten_state = info->exten_state;
  179. task_data->exten_state_sub->last_presence_state = info->presence_state;
  180. ao2_ref(task_data->exten_state_sub, +1);
  181. task_data->exten_state_data.exten = exten_state_sub->exten;
  182. task_data->exten_state_data.exten_state = info->exten_state;
  183. task_data->exten_state_data.presence_state = info->presence_state;
  184. task_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
  185. task_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
  186. task_data->exten_state_data.user_agent = ast_strdup(exten_state_sub->user_agent);
  187. task_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
  188. task_data->exten_state_data.sub = exten_state_sub->sip_sub;
  189. if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
  190. (info->exten_state == AST_EXTENSION_REMOVED)) {
  191. ast_verb(2, "Watcher for hint %s %s\n", exten, info->exten_state
  192. == AST_EXTENSION_REMOVED ? "removed" : "deactivated");
  193. task_data->terminate = 1;
  194. }
  195. return task_data;
  196. }
  197. static int notify_task(void *obj)
  198. {
  199. RAII_VAR(struct notify_task_data *, task_data, obj, ao2_cleanup);
  200. struct ast_sip_body_data data = {
  201. .body_type = AST_SIP_EXTEN_STATE_DATA,
  202. .body_data = &task_data->exten_state_data,
  203. };
  204. /* Terminated subscriptions are no longer associated with a valid tree, and sending
  205. * NOTIFY messages on a subscription which has already been terminated won't work.
  206. */
  207. if (ast_sip_subscription_is_terminated(task_data->exten_state_sub->sip_sub)) {
  208. return 0;
  209. }
  210. /* All access to the subscription must occur within a task executed within its serializer */
  211. ast_sip_subscription_get_local_uri(task_data->exten_state_sub->sip_sub,
  212. task_data->exten_state_data.local, sizeof(task_data->exten_state_data.local));
  213. ast_sip_subscription_get_remote_uri(task_data->exten_state_sub->sip_sub,
  214. task_data->exten_state_data.remote, sizeof(task_data->exten_state_data.remote));
  215. /* Pool allocation has to happen here so that we allocate within a PJLIB thread */
  216. task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
  217. "exten_state", 1024, 1024);
  218. if (!task_data->exten_state_data.pool) {
  219. return -1;
  220. }
  221. task_data->exten_state_data.sub = task_data->exten_state_sub->sip_sub;
  222. ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &data,
  223. task_data->terminate);
  224. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),
  225. task_data->exten_state_data.pool);
  226. return 0;
  227. }
  228. /*!
  229. * \internal
  230. * \brief Callback for exten/device state changes.
  231. *
  232. * Upon state change, send the appropriate notification to the subscriber.
  233. */
  234. static int state_changed(char *context, char *exten,
  235. struct ast_state_cb_info *info, void *data)
  236. {
  237. struct notify_task_data *task_data;
  238. struct exten_state_subscription *exten_state_sub = data;
  239. if (!(task_data = alloc_notify_task_data(exten, exten_state_sub, info))) {
  240. return -1;
  241. }
  242. /* safe to push this async since we copy the data from info and
  243. add a ref for the device state info */
  244. if (ast_sip_push_task(task_data->exten_state_sub->serializer, notify_task,
  245. task_data)) {
  246. ao2_cleanup(task_data);
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. static void state_changed_destroy(int id, void *data)
  252. {
  253. struct exten_state_subscription *exten_state_sub = data;
  254. ao2_cleanup(exten_state_sub);
  255. }
  256. static struct ast_datastore_info ds_info = { };
  257. static const char ds_name[] = "exten state datastore";
  258. /*!
  259. * \internal
  260. * \brief Add a datastore for exten exten_state_subscription.
  261. *
  262. * Adds the exten_state_subscription wrapper object to a datastore so it can be retrieved
  263. * later based upon its association with the ast_sip_subscription.
  264. */
  265. static int add_datastore(struct exten_state_subscription *exten_state_sub)
  266. {
  267. RAII_VAR(struct ast_datastore *, datastore,
  268. ast_sip_subscription_alloc_datastore(&ds_info, ds_name), ao2_cleanup);
  269. if (!datastore) {
  270. return -1;
  271. }
  272. datastore->data = exten_state_sub;
  273. ast_sip_subscription_add_datastore(exten_state_sub->sip_sub, datastore);
  274. ao2_ref(exten_state_sub, +1);
  275. return 0;
  276. }
  277. /*!
  278. * \internal
  279. * \brief Get the exten_state_subscription object associated with the given
  280. * ast_sip_subscription in the datastore.
  281. */
  282. static struct exten_state_subscription *get_exten_state_sub(
  283. struct ast_sip_subscription *sub)
  284. {
  285. RAII_VAR(struct ast_datastore *, datastore,
  286. ast_sip_subscription_get_datastore(sub, ds_name), ao2_cleanup);
  287. return datastore ? datastore->data : NULL;
  288. }
  289. static void subscription_shutdown(struct ast_sip_subscription *sub)
  290. {
  291. struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
  292. if (!exten_state_sub) {
  293. return;
  294. }
  295. ast_extension_state_del(exten_state_sub->id, state_changed);
  296. ast_sip_subscription_remove_datastore(exten_state_sub->sip_sub, ds_name);
  297. /* remove data store reference */
  298. ao2_cleanup(exten_state_sub);
  299. }
  300. static int new_subscribe(struct ast_sip_endpoint *endpoint,
  301. const char *resource)
  302. {
  303. const char *context = S_OR(endpoint->subscription.context, endpoint->context);
  304. if (!ast_exists_extension(NULL, context, resource, PRIORITY_HINT, NULL)) {
  305. ast_log(LOG_NOTICE, "Endpoint '%s' state subscription failed: "
  306. "Extension '%s' does not exist in context '%s' or has no associated hint\n",
  307. ast_sorcery_object_get_id(endpoint), resource, context);
  308. return 404;
  309. }
  310. return 200;
  311. }
  312. static int subscription_established(struct ast_sip_subscription *sip_sub)
  313. {
  314. struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
  315. const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
  316. struct exten_state_subscription *exten_state_sub;
  317. if (!(exten_state_sub = exten_state_subscription_alloc(sip_sub, endpoint))) {
  318. ao2_cleanup(endpoint);
  319. return -1;
  320. }
  321. ast_copy_string(exten_state_sub->context,
  322. S_OR(endpoint->subscription.context, endpoint->context),
  323. sizeof(exten_state_sub->context));
  324. ast_copy_string(exten_state_sub->exten, resource, sizeof(exten_state_sub->exten));
  325. if ((exten_state_sub->id = ast_extension_state_add_destroy_extended(
  326. exten_state_sub->context, exten_state_sub->exten,
  327. state_changed, state_changed_destroy, exten_state_sub)) < 0) {
  328. ast_log(LOG_WARNING, "Unable to subscribe endpoint '%s' to extension '%s@%s'\n",
  329. ast_sorcery_object_get_id(endpoint), exten_state_sub->exten,
  330. exten_state_sub->context);
  331. ao2_cleanup(endpoint);
  332. ao2_cleanup(exten_state_sub);
  333. return -1;
  334. }
  335. /* Go ahead and cleanup the endpoint since we don't need it anymore */
  336. ao2_cleanup(endpoint);
  337. /* bump the ref since ast_extension_state_add holds a reference */
  338. ao2_ref(exten_state_sub, +1);
  339. if (add_datastore(exten_state_sub)) {
  340. ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n");
  341. ao2_cleanup(exten_state_sub);
  342. return -1;
  343. }
  344. ao2_cleanup(exten_state_sub);
  345. return 0;
  346. }
  347. static void exten_state_data_destructor(void *obj)
  348. {
  349. struct ast_sip_exten_state_data *exten_state_data = obj;
  350. ao2_cleanup(exten_state_data->device_state_info);
  351. ast_free(exten_state_data->presence_subtype);
  352. ast_free(exten_state_data->presence_message);
  353. if (exten_state_data->pool) {
  354. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data->pool);
  355. }
  356. }
  357. static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_subscription *sip_sub,
  358. struct exten_state_subscription *exten_state_sub)
  359. {
  360. struct ast_sip_exten_state_data *exten_state_data;
  361. char *subtype = NULL;
  362. char *message = NULL;
  363. int presence_state;
  364. exten_state_data = ao2_alloc(sizeof(*exten_state_data), exten_state_data_destructor);
  365. if (!exten_state_data) {
  366. return NULL;
  367. }
  368. exten_state_data->exten = exten_state_sub->exten;
  369. presence_state = ast_hint_presence_state(NULL, exten_state_sub->context, exten_state_sub->exten, &subtype, &message);
  370. if (presence_state == -1 || presence_state == AST_PRESENCE_INVALID) {
  371. ao2_cleanup(exten_state_data);
  372. return NULL;
  373. }
  374. exten_state_data->presence_state = presence_state;
  375. exten_state_data->presence_subtype = subtype;
  376. exten_state_data->presence_message = message;
  377. exten_state_data->user_agent = exten_state_sub->user_agent;
  378. ast_sip_subscription_get_local_uri(sip_sub, exten_state_data->local,
  379. sizeof(exten_state_data->local));
  380. ast_sip_subscription_get_remote_uri(sip_sub, exten_state_data->remote,
  381. sizeof(exten_state_data->remote));
  382. exten_state_data->sub = sip_sub;
  383. exten_state_data->exten_state = ast_extension_state_extended(
  384. NULL, exten_state_sub->context, exten_state_sub->exten,
  385. &exten_state_data->device_state_info);
  386. if (exten_state_data->exten_state < 0) {
  387. ao2_cleanup(exten_state_data);
  388. return NULL;
  389. }
  390. exten_state_data->pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
  391. "exten_state", 1024, 1024);
  392. if (!exten_state_data->pool) {
  393. ao2_cleanup(exten_state_data);
  394. return NULL;
  395. }
  396. return exten_state_data;
  397. }
  398. static void *get_notify_data(struct ast_sip_subscription *sub)
  399. {
  400. struct exten_state_subscription *exten_state_sub;
  401. exten_state_sub = get_exten_state_sub(sub);
  402. if (!exten_state_sub) {
  403. return NULL;
  404. }
  405. return exten_state_data_alloc(sub, exten_state_sub);
  406. }
  407. static void to_ami(struct ast_sip_subscription *sub,
  408. struct ast_str **buf)
  409. {
  410. struct exten_state_subscription *exten_state_sub =
  411. get_exten_state_sub(sub);
  412. if (!exten_state_sub) {
  413. return;
  414. }
  415. ast_str_append(buf, 0, "SubscriptionType: extension_state\r\n"
  416. "Extension: %s\r\nExtensionStates: %s\r\n",
  417. exten_state_sub->exten, ast_extension_state2str(
  418. exten_state_sub->last_exten_state));
  419. }
  420. static int load_module(void)
  421. {
  422. CHECK_PJSIP_MODULE_LOADED();
  423. if (ast_sip_register_subscription_handler(&presence_handler)) {
  424. ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
  425. presence_handler.event_name);
  426. return AST_MODULE_LOAD_DECLINE;
  427. }
  428. if (ast_sip_register_subscription_handler(&dialog_handler)) {
  429. ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
  430. dialog_handler.event_name);
  431. ast_sip_unregister_subscription_handler(&presence_handler);
  432. return AST_MODULE_LOAD_DECLINE;
  433. }
  434. return AST_MODULE_LOAD_SUCCESS;
  435. }
  436. static int unload_module(void)
  437. {
  438. ast_sip_unregister_subscription_handler(&dialog_handler);
  439. ast_sip_unregister_subscription_handler(&presence_handler);
  440. return 0;
  441. }
  442. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Notifications",
  443. .support_level = AST_MODULE_SUPPORT_CORE,
  444. .load = load_module,
  445. .unload = unload_module,
  446. .load_pri = AST_MODPRI_CHANNEL_DEPEND + 5,
  447. );