res_pjsip_exten_state.c 15 KB

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