res_pjsip_notify.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include <pjsip.h>
  25. #include <pjsip_ua.h>
  26. #include "asterisk/cli.h"
  27. #include "asterisk/config.h"
  28. #include "asterisk/manager.h"
  29. #include "asterisk/module.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/res_pjsip.h"
  32. #include "asterisk/res_pjsip_session.h"
  33. #include "asterisk/sorcery.h"
  34. /*** DOCUMENTATION
  35. <manager name="PJSIPNotify" language="en_US">
  36. <synopsis>
  37. Send a NOTIFY to either an endpoint, an arbitrary URI, or inside a SIP dialog.
  38. </synopsis>
  39. <syntax>
  40. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  41. <parameter name="Endpoint" required="false">
  42. <para>The endpoint to which to send the NOTIFY.</para>
  43. </parameter>
  44. <parameter name="URI" required="false">
  45. <para>Abritrary URI to which to send the NOTIFY.</para>
  46. </parameter>
  47. <parameter name="channel" required="false">
  48. <para>Channel name to send the NOTIFY. Must be a PJSIP channel.</para>
  49. </parameter>
  50. <parameter name="Variable" required="true">
  51. <para>Appends variables as headers/content to the NOTIFY. If the variable is
  52. named <literal>Content</literal>, then the value will compose the body
  53. of the message if another variable sets <literal>Content-Type</literal>.
  54. <replaceable>name</replaceable>=<replaceable>value</replaceable></para>
  55. </parameter>
  56. </syntax>
  57. <description>
  58. <para>Sends a NOTIFY to an endpoint, an arbitrary URI, or inside a SIP dialog.</para>
  59. <para>All parameters for this event must be specified in the body of this
  60. request via multiple <literal>Variable: name=value</literal> sequences.</para>
  61. <note><para>One (and only one) of <literal>Endpoint</literal>,
  62. <literal>URI</literal>, or <literal>Channel</literal> must be specified.
  63. If <literal>URI</literal> is used, the default outbound endpoint will be used
  64. to send the message. If the default outbound endpoint isn't configured, this command
  65. can not send to an arbitrary URI.</para></note>
  66. </description>
  67. </manager>
  68. <configInfo name="res_pjsip_notify" language="en_US">
  69. <synopsis>Module that supports sending NOTIFY requests to endpoints from external sources</synopsis>
  70. <configFile name="pjsip_notify.conf">
  71. <configObject name="general">
  72. <synopsis>Unused, but reserved.</synopsis>
  73. </configObject>
  74. <configObject name="notify">
  75. <synopsis>Configuration of a NOTIFY request.</synopsis>
  76. <description>
  77. <para>Each key-value pair in a <literal>notify</literal>
  78. configuration section defines either a SIP header to send
  79. in the request or a line of content in the request message
  80. body. A key of <literal>Content</literal> is treated
  81. as part of the message body and is appended in sequential
  82. order; any other header is treated as part of the SIP
  83. request.</para>
  84. </description>
  85. <configOption name="">
  86. <synopsis>A key/value pair to add to a NOTIFY request.</synopsis>
  87. <description>
  88. <para>If the key is <literal>Content</literal>,
  89. it will be treated as part of the message body. Otherwise,
  90. it will be added as a header in the NOTIFY request.</para>
  91. <para>The following headers are reserved and cannot be
  92. specified:</para>
  93. <enumlist>
  94. <enum name="Call-ID" />
  95. <enum name="Contact" />
  96. <enum name="CSeq" />
  97. <enum name="To" />
  98. <enum name="From" />
  99. <enum name="Record-Route" />
  100. <enum name="Route" />
  101. <enum name="Via" />
  102. </enumlist>
  103. </description>
  104. </configOption>
  105. </configObject>
  106. </configFile>
  107. </configInfo>
  108. ***/
  109. #define CONTENT_TYPE_SIZE 64
  110. #define CONTENT_SIZE 512
  111. /*!
  112. * \internal
  113. * \brief The configuration file containing NOTIFY payload types to send.
  114. */
  115. static const char notify_config[] = "pjsip_notify.conf";
  116. struct notify_option_item {
  117. const char *name;
  118. const char *value;
  119. char buf[0];
  120. };
  121. struct notify_option {
  122. /*! Contains header and/or content information */
  123. struct ao2_container *items;
  124. /*! The name of the notify option */
  125. char name[0];
  126. };
  127. static int notify_option_hash(const void *obj, int flags)
  128. {
  129. const struct notify_option *option = obj;
  130. return ast_str_case_hash(flags & OBJ_KEY ? obj : option->name);
  131. }
  132. static int notify_option_cmp(void *obj, void *arg, int flags)
  133. {
  134. struct notify_option *option1 = obj;
  135. struct notify_option *option2 = arg;
  136. const char *key = flags & OBJ_KEY ? arg : option2->name;
  137. return strcasecmp(option1->name, key) ? 0 : CMP_MATCH;
  138. }
  139. static void notify_option_destroy(void *obj)
  140. {
  141. struct notify_option *option = obj;
  142. ao2_cleanup(option->items);
  143. }
  144. static void *notify_option_alloc(const char *category)
  145. {
  146. int category_size = strlen(category) + 1;
  147. struct notify_option *option = ao2_alloc(
  148. sizeof(*option) + category_size, notify_option_destroy);
  149. if (!option) {
  150. return NULL;
  151. }
  152. ast_copy_string(option->name, category, category_size);
  153. if (!(option->items = ao2_container_alloc_list(
  154. AO2_ALLOC_OPT_LOCK_NOLOCK,
  155. AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW, NULL, NULL))) {
  156. ao2_cleanup(option);
  157. return NULL;
  158. }
  159. return option;
  160. }
  161. static void *notify_option_find(struct ao2_container *container, const char *category)
  162. {
  163. return ao2_find(container, category, OBJ_KEY);
  164. }
  165. static int notify_option_handler(const struct aco_option *opt,
  166. struct ast_variable *var, void *obj)
  167. {
  168. struct notify_option *option = obj;
  169. int name_size = strlen(var->name) + 1;
  170. int value_size = strlen(var->value) + 1;
  171. RAII_VAR(struct notify_option_item *, item,
  172. ao2_alloc(sizeof(*item) + name_size + value_size,
  173. NULL), ao2_cleanup);
  174. item->name = item->buf;
  175. item->value = item->buf + name_size;
  176. ast_copy_string(item->buf, var->name, name_size);
  177. ast_copy_string(item->buf + name_size, var->value, value_size);
  178. if (!ao2_link(option->items, item)) {
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. struct notify_cfg {
  184. struct ao2_container *notify_options;
  185. };
  186. static void notify_cfg_destroy(void *obj)
  187. {
  188. struct notify_cfg *cfg = obj;
  189. ao2_cleanup(cfg->notify_options);
  190. }
  191. static void *notify_cfg_alloc(void)
  192. {
  193. struct notify_cfg *cfg;
  194. if (!(cfg = ao2_alloc(sizeof(*cfg), notify_cfg_destroy))) {
  195. return NULL;
  196. }
  197. cfg->notify_options = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
  198. 20, notify_option_hash, NULL, notify_option_cmp);
  199. if (!cfg->notify_options) {
  200. ao2_cleanup(cfg);
  201. return NULL;
  202. }
  203. return cfg;
  204. }
  205. static struct aco_type notify_option = {
  206. .type = ACO_ITEM,
  207. .name = "notify",
  208. .category_match = ACO_BLACKLIST_EXACT,
  209. .category = "general",
  210. .item_offset = offsetof(struct notify_cfg, notify_options),
  211. .item_alloc = notify_option_alloc,
  212. .item_find = notify_option_find
  213. };
  214. static struct aco_type *notify_options[] = ACO_TYPES(&notify_option);
  215. static struct aco_file module_conf = {
  216. .filename = notify_config,
  217. .types = ACO_TYPES(&notify_option),
  218. };
  219. AO2_GLOBAL_OBJ_STATIC(globals);
  220. CONFIG_INFO_STANDARD(notify_cfg, globals, notify_cfg_alloc,
  221. .files = ACO_FILES(&module_conf)
  222. );
  223. /*!
  224. * \internal
  225. * \brief Structure to hold task data for notifications.
  226. */
  227. struct notify_data {
  228. /*! The endpoint being notified */
  229. struct ast_sip_endpoint *endpoint;
  230. /*! The info of headers, types and content */
  231. void *info;
  232. /*! Function to help build notify request */
  233. void (*build_notify)(pjsip_tx_data *, void *);
  234. };
  235. /*!
  236. * \internal
  237. * \brief Destroy the notify CLI data releasing any resources.
  238. */
  239. static void notify_cli_data_destroy(void *obj)
  240. {
  241. struct notify_data *data = obj;
  242. ao2_cleanup(data->endpoint);
  243. ao2_cleanup(data->info);
  244. }
  245. /*!
  246. * \internal
  247. * \brief Structure to hold task data for notifications (URI variant)
  248. */
  249. struct notify_uri_data {
  250. char *uri;
  251. void *info;
  252. void (*build_notify)(pjsip_tx_data *, void *);
  253. };
  254. /*!
  255. * \internal
  256. * \brief Structure to hold task data for notifications (channel variant)
  257. */
  258. struct notify_channel_data {
  259. struct ast_sip_session *session;
  260. void *info;
  261. void (*build_notify)(pjsip_tx_data *, void *);
  262. };
  263. static void notify_cli_uri_data_destroy(void *obj)
  264. {
  265. struct notify_uri_data *data = obj;
  266. ast_free(data->uri);
  267. ao2_cleanup(data->info);
  268. }
  269. /*!
  270. * \internal
  271. * \brief Destroy the notify CLI data releasing any resources (URI variant)
  272. */
  273. static void build_cli_notify(pjsip_tx_data *tdata, void *info);
  274. /*!
  275. * \internal
  276. * \brief Construct a notify data object for CLI.
  277. */
  278. static struct notify_data* notify_cli_data_create(
  279. struct ast_sip_endpoint *endpoint, void *info)
  280. {
  281. struct notify_data *data = ao2_alloc(sizeof(*data),
  282. notify_cli_data_destroy);
  283. if (!data) {
  284. return NULL;
  285. }
  286. data->endpoint = endpoint;
  287. ao2_ref(data->endpoint, +1);
  288. data->info = info;
  289. ao2_ref(data->info, +1);
  290. data->build_notify = build_cli_notify;
  291. return data;
  292. }
  293. /*!
  294. * \internal
  295. * \brief Construct a notify URI data object for CLI.
  296. */
  297. static struct notify_uri_data* notify_cli_uri_data_create(
  298. const char *uri, void *info)
  299. {
  300. struct notify_uri_data *data = ao2_alloc(sizeof(*data),
  301. notify_cli_uri_data_destroy);
  302. if (!data) {
  303. return NULL;
  304. }
  305. data->uri = ast_strdup(uri);
  306. if (!data->uri) {
  307. ao2_ref(data, -1);
  308. return NULL;
  309. }
  310. data->info = info;
  311. ao2_ref(data->info, +1);
  312. data->build_notify = build_cli_notify;
  313. return data;
  314. }
  315. /*!
  316. * \internal
  317. * \brief Destroy the notify AMI data releasing any resources.
  318. */
  319. static void notify_ami_data_destroy(void *obj)
  320. {
  321. struct notify_data *data = obj;
  322. struct ast_variable *info = data->info;
  323. ao2_cleanup(data->endpoint);
  324. ast_variables_destroy(info);
  325. }
  326. /*!
  327. * \internal
  328. * \brief Destroy the notify AMI URI data releasing any resources.
  329. */
  330. static void notify_ami_uri_data_destroy(void *obj)
  331. {
  332. struct notify_uri_data *data = obj;
  333. struct ast_variable *info = data->info;
  334. ast_free(data->uri);
  335. ast_variables_destroy(info);
  336. }
  337. /*!
  338. * \internal
  339. * \brief Destroy the notify AMI channel data releasing any resources.
  340. */
  341. static void notify_ami_channel_data_destroy(void *obj)
  342. {
  343. struct notify_channel_data *data = obj;
  344. struct ast_variable *info = data->info;
  345. ao2_cleanup(data->session);
  346. ast_variables_destroy(info);
  347. }
  348. static void build_ami_notify(pjsip_tx_data *tdata, void *info);
  349. /*!
  350. * \internal
  351. * \brief Construct a notify data object for AMI.
  352. */
  353. static struct notify_data* notify_ami_data_create(
  354. struct ast_sip_endpoint *endpoint, void *info)
  355. {
  356. struct notify_data *data = ao2_alloc(sizeof(*data),
  357. notify_ami_data_destroy);
  358. if (!data) {
  359. return NULL;
  360. }
  361. data->endpoint = endpoint;
  362. ao2_ref(data->endpoint, +1);
  363. data->info = info;
  364. data->build_notify = build_ami_notify;
  365. return data;
  366. }
  367. /*!
  368. * \internal
  369. * \brief Construct a notify URI data object for AMI.
  370. */
  371. static struct notify_uri_data* notify_ami_uri_data_create(
  372. const char *uri, void *info)
  373. {
  374. struct notify_uri_data *data = ao2_alloc(sizeof(*data),
  375. notify_ami_uri_data_destroy);
  376. if (!data) {
  377. return NULL;
  378. }
  379. data->uri = ast_strdup(uri);
  380. if (!data->uri) {
  381. ao2_ref(data, -1);
  382. return NULL;
  383. }
  384. data->info = info;
  385. data->build_notify = build_ami_notify;
  386. return data;
  387. }
  388. /*!
  389. * \internal
  390. * \brief Construct a notify channel data object for AMI.
  391. */
  392. static struct notify_channel_data *notify_ami_channel_data_create(
  393. struct ast_sip_session *session, void *info)
  394. {
  395. struct notify_channel_data *data;
  396. data = ao2_alloc_options(sizeof(*data), notify_ami_channel_data_destroy,
  397. AO2_ALLOC_OPT_LOCK_NOLOCK);
  398. if (!data) {
  399. return NULL;
  400. }
  401. data->session = session;
  402. data->info = info;
  403. data->build_notify = build_ami_notify;
  404. return data;
  405. }
  406. /*!
  407. * \internal
  408. * \brief Checks if the given header name is not allowed.
  409. *
  410. * \details Some headers are not allowed to be set by the user within the
  411. * scope of a NOTIFY request. If the given var header name is
  412. * found in the "not allowed" list then return true.
  413. */
  414. static int not_allowed(const char *name)
  415. {
  416. int i;
  417. static const char *names[] = {
  418. "Call-ID",
  419. "Contact",
  420. "CSeq",
  421. "To",
  422. "From",
  423. "Record-Route",
  424. "Route",
  425. "Request-URI",
  426. "Via",
  427. };
  428. for (i = 0; i < ARRAY_LEN(names); ++i) {
  429. if (!strcasecmp(name, names[i])) {
  430. return 1;
  431. }
  432. }
  433. return 0;
  434. }
  435. /*!
  436. * \internal
  437. * \brief If a content type was specified add it and the content body to the
  438. * NOTIFY request.
  439. */
  440. static void build_notify_body(pjsip_tx_data *tdata, struct ast_str *content_type,
  441. struct ast_str *content)
  442. {
  443. if (content_type) {
  444. char *p;
  445. struct ast_sip_body body;
  446. if (content) {
  447. body.body_text = ast_str_buffer(content);
  448. }
  449. body.type = ast_str_buffer(content_type);
  450. if ((p = strchr(body.type, '/'))) {
  451. *p++ = '\0';
  452. body.subtype = p;
  453. }
  454. ast_sip_add_body(tdata, &body);
  455. }
  456. }
  457. /*!
  458. * \internal
  459. * \brief Build the NOTIFY request adding content or header info.
  460. */
  461. static void build_notify(pjsip_tx_data *tdata, const char *name, const char *value,
  462. struct ast_str **content_type, struct ast_str **content)
  463. {
  464. if (not_allowed(name)) {
  465. ast_log(LOG_WARNING, "Cannot specify %s header, "
  466. "ignoring\n", name);
  467. return;
  468. }
  469. if (!strcasecmp(name, "Content-type")) {
  470. if (!(*content_type)) {
  471. *content_type = ast_str_create(CONTENT_TYPE_SIZE);
  472. }
  473. ast_str_set(content_type, 0,"%s", value);
  474. } else if (!strcasecmp(name, "Content")) {
  475. if (!(*content)) {
  476. *content = ast_str_create(CONTENT_SIZE);
  477. }
  478. if (ast_str_strlen(*content)) {
  479. ast_str_append(content, 0, "\r\n");
  480. }
  481. ast_str_append(content, 0, "%s", value);
  482. } else {
  483. ast_sip_add_header(tdata, name, value);
  484. }
  485. }
  486. /*!
  487. * \internal
  488. * \brief Build the NOTIFY request from CLI info adding header and content
  489. * when specified.
  490. */
  491. static void build_cli_notify(pjsip_tx_data *tdata, void *info)
  492. {
  493. struct notify_option *option = info;
  494. RAII_VAR(struct ast_str *, content_type, NULL, ast_free);
  495. RAII_VAR(struct ast_str *, content, NULL, ast_free);
  496. struct notify_option_item *item;
  497. struct ao2_iterator i = ao2_iterator_init(option->items, 0);
  498. while ((item = ao2_iterator_next(&i))) {
  499. build_notify(tdata, item->name, item->value,
  500. &content_type, &content);
  501. ao2_cleanup(item);
  502. }
  503. ao2_iterator_destroy(&i);
  504. build_notify_body(tdata, content_type, content);
  505. }
  506. /*!
  507. * \internal
  508. * \brief Build the NOTIFY request from AMI info adding header and content
  509. * when specified.
  510. */
  511. static void build_ami_notify(pjsip_tx_data *tdata, void *info)
  512. {
  513. struct ast_variable *vars = info;
  514. RAII_VAR(struct ast_str *, content_type, NULL, ast_free);
  515. RAII_VAR(struct ast_str *, content, NULL, ast_free);
  516. struct ast_variable *i;
  517. for (i = vars; i; i = i->next) {
  518. if (!strcasecmp(i->name, "Content-Length")) {
  519. ast_log(LOG_NOTICE, "It is not necessary to specify Content-Length, ignoring.\n");
  520. continue;
  521. }
  522. build_notify(tdata, i->name, i->value,
  523. &content_type, &content);
  524. }
  525. build_notify_body(tdata, content_type, content);
  526. }
  527. /*!
  528. * \internal
  529. * \brief Build and send a NOTIFY request to a contact.
  530. */
  531. static int notify_contact(void *obj, void *arg, int flags)
  532. {
  533. struct ast_sip_contact *contact = obj;
  534. struct notify_data *data = arg;
  535. pjsip_tx_data *tdata;
  536. if (ast_sip_create_request("NOTIFY", NULL, data->endpoint,
  537. NULL, contact, &tdata)) {
  538. ast_log(LOG_WARNING, "SIP NOTIFY - Unable to create request for "
  539. "contact %s\n", contact->uri);
  540. return -1;
  541. }
  542. ast_sip_add_header(tdata, "Subscription-State", "terminated");
  543. data->build_notify(tdata, data->info);
  544. if (ast_sip_send_request(tdata, NULL, data->endpoint, NULL, NULL)) {
  545. ast_log(LOG_ERROR, "SIP NOTIFY - Unable to send request for "
  546. "contact %s\n", contact->uri);
  547. return -1;
  548. }
  549. return 0;
  550. }
  551. /*!
  552. * \internal
  553. * \brief Send a NOTIFY request to the endpoint.
  554. *
  555. * \detail Iterates over an endpoint's AORs sending a NOTIFY request
  556. * with the appropriate payload information to each contact.
  557. */
  558. static int notify_endpoint(void *obj)
  559. {
  560. RAII_VAR(struct notify_data *, data, obj, ao2_cleanup);
  561. char *aor_name, *aors;
  562. if (ast_strlen_zero(data->endpoint->aors)) {
  563. ast_log(LOG_WARNING, "Unable to NOTIFY - "
  564. "endpoint has no configured AORs\n");
  565. return -1;
  566. }
  567. aors = ast_strdupa(data->endpoint->aors);
  568. while ((aor_name = ast_strip(strsep(&aors, ",")))) {
  569. RAII_VAR(struct ast_sip_aor *, aor,
  570. ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
  571. RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
  572. if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
  573. continue;
  574. }
  575. ao2_callback(contacts, OBJ_NODATA, notify_contact, data);
  576. }
  577. return 0;
  578. }
  579. /*!
  580. * \internal
  581. * \brief Send a notify request to the URI.
  582. */
  583. static int notify_uri(void *obj)
  584. {
  585. RAII_VAR(struct notify_uri_data *, data, obj, ao2_cleanup);
  586. RAII_VAR(struct ast_sip_endpoint *, endpoint,
  587. ast_sip_default_outbound_endpoint(), ao2_cleanup);
  588. pjsip_tx_data *tdata;
  589. if (!endpoint) {
  590. ast_log(LOG_WARNING, "No default outbound endpoint set, can not send "
  591. "NOTIFY requests to arbitrary URIs.\n");
  592. return -1;
  593. }
  594. if (ast_strlen_zero(data->uri)) {
  595. ast_log(LOG_WARNING, "Unable to NOTIFY - URI is blank.\n");
  596. return -1;
  597. }
  598. if (ast_sip_create_request("NOTIFY", NULL, endpoint,
  599. data->uri, NULL, &tdata)) {
  600. ast_log(LOG_WARNING, "SIP NOTIFY - Unable to create request for "
  601. "uri %s\n", data->uri);
  602. return -1;
  603. }
  604. ast_sip_add_header(tdata, "Subscription-State", "terminated");
  605. data->build_notify(tdata, data->info);
  606. if (ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL)) {
  607. ast_log(LOG_ERROR, "SIP NOTIFY - Unable to send request for "
  608. "uri %s\n", data->uri);
  609. return -1;
  610. }
  611. return 0;
  612. }
  613. /*!
  614. * \internal
  615. * \brief Send a notify request to a channel.
  616. */
  617. static int notify_channel(void *obj)
  618. {
  619. RAII_VAR(struct notify_channel_data *, data, obj, ao2_cleanup);
  620. pjsip_tx_data *tdata;
  621. struct pjsip_dialog *dlg;
  622. if (!data->session->channel
  623. || !data->session->inv_session
  624. || data->session->inv_session->state < PJSIP_INV_STATE_EARLY
  625. || data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  626. return -1;
  627. }
  628. ast_debug(1, "Sending notify on channel %s\n", ast_channel_name(data->session->channel));
  629. dlg = data->session->inv_session->dlg;
  630. if (ast_sip_create_request("NOTIFY", dlg, NULL, NULL, NULL, &tdata)) {
  631. return -1;
  632. }
  633. ast_sip_add_header(tdata, "Subscription-State", "terminated");
  634. data->build_notify(tdata, data->info);
  635. if (ast_sip_send_request(tdata, dlg, NULL, NULL, NULL)) {
  636. return -1;
  637. }
  638. return 0;
  639. }
  640. enum notify_result {
  641. SUCCESS,
  642. INVALID_ENDPOINT,
  643. INVALID_CHANNEL,
  644. ALLOC_ERROR,
  645. TASK_PUSH_ERROR
  646. };
  647. typedef struct notify_data *(*task_data_create)(
  648. struct ast_sip_endpoint *, void *info);
  649. typedef struct notify_uri_data *(*task_uri_data_create)(
  650. const char *uri, void *info);
  651. typedef struct notify_channel_data *(*task_channel_data_create)(
  652. struct ast_sip_session *session, void *info);
  653. /*!
  654. * \internal
  655. * \brief Send a NOTIFY request to the endpoint within a threaded task.
  656. */
  657. static enum notify_result push_notify(const char *endpoint_name, void *info,
  658. task_data_create data_create)
  659. {
  660. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  661. struct notify_data *data;
  662. if (!(endpoint = ast_sorcery_retrieve_by_id(
  663. ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
  664. return INVALID_ENDPOINT;
  665. }
  666. if (!(data = data_create(endpoint, info))) {
  667. return ALLOC_ERROR;
  668. }
  669. if (ast_sip_push_task(NULL, notify_endpoint, data)) {
  670. ao2_cleanup(data);
  671. return TASK_PUSH_ERROR;
  672. }
  673. return SUCCESS;
  674. }
  675. /*!
  676. * \internal
  677. * \brief Send a NOTIFY request to the URI within an threaded task.
  678. */
  679. static enum notify_result push_notify_uri(const char *uri, void *info,
  680. task_uri_data_create data_create)
  681. {
  682. struct notify_uri_data *data;
  683. if (!(data = data_create(uri, info))) {
  684. return ALLOC_ERROR;
  685. }
  686. if (ast_sip_push_task(NULL, notify_uri, data)) {
  687. ao2_cleanup(data);
  688. return TASK_PUSH_ERROR;
  689. }
  690. return SUCCESS;
  691. }
  692. /*!
  693. * \internal
  694. * \brief Send a NOTIFY request in a channel within an threaded task.
  695. */
  696. static enum notify_result push_notify_channel(const char *channel_name, void *info,
  697. task_channel_data_create data_create)
  698. {
  699. struct notify_channel_data *data;
  700. struct ast_channel *ch;
  701. struct ast_sip_session *session;
  702. struct ast_sip_channel_pvt *ch_pvt;
  703. /* note: this increases the refcount of the channel */
  704. ch = ast_channel_get_by_name(channel_name);
  705. if (!ch) {
  706. ast_debug(1, "No channel found with name %s", channel_name);
  707. return INVALID_CHANNEL;
  708. }
  709. if (strcmp(ast_channel_tech(ch)->type, "PJSIP")) {
  710. ast_log(LOG_WARNING, "Channel was a non-PJSIP channel: %s\n", channel_name);
  711. ast_channel_unref(ch);
  712. return INVALID_CHANNEL;
  713. }
  714. ast_channel_lock(ch);
  715. ch_pvt = ast_channel_tech_pvt(ch);
  716. session = ch_pvt->session;
  717. if (!session || !session->inv_session
  718. || session->inv_session->state < PJSIP_INV_STATE_EARLY
  719. || session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  720. ast_debug(1, "No active session for channel %s\n", channel_name);
  721. ast_channel_unlock(ch);
  722. ast_channel_unref(ch);
  723. return INVALID_CHANNEL;
  724. }
  725. ao2_ref(session, +1);
  726. ast_channel_unlock(ch);
  727. /* don't keep a reference to the channel, we've got a reference to the session */
  728. ast_channel_unref(ch);
  729. /*
  730. * data_create will take ownership of the session,
  731. * and take care of releasing the ref.
  732. */
  733. data = data_create(session, info);
  734. if (!data) {
  735. ao2_ref(session, -1);
  736. return ALLOC_ERROR;
  737. }
  738. if (ast_sip_push_task(session->serializer, notify_channel, data)) {
  739. ao2_ref(data, -1);
  740. return TASK_PUSH_ERROR;
  741. }
  742. return SUCCESS;
  743. }
  744. /*!
  745. * \internal
  746. * \brief Do completion on the endpoint.
  747. */
  748. static char *cli_complete_endpoint(const char *word)
  749. {
  750. int wordlen = strlen(word);
  751. struct ao2_container * endpoints;
  752. struct ast_sip_endpoint *endpoint;
  753. struct ao2_iterator i;
  754. endpoints = ast_sorcery_retrieve_by_prefix(ast_sip_get_sorcery(),
  755. "endpoint", word, wordlen);
  756. if (endpoints == NULL) {
  757. return NULL;
  758. }
  759. i = ao2_iterator_init(endpoints, 0);
  760. while ((endpoint = ao2_iterator_next(&i))) {
  761. ast_cli_completion_add(
  762. ast_strdup(ast_sorcery_object_get_id(endpoint)));
  763. ao2_cleanup(endpoint);
  764. }
  765. ao2_iterator_destroy(&i);
  766. ao2_ref(endpoints, -1);
  767. return NULL;
  768. }
  769. /*!
  770. * \internal
  771. * \brief Do completion on the notify CLI command.
  772. */
  773. static char *cli_complete_notify(const char *line, const char *word,
  774. int pos, int state, int using_uri)
  775. {
  776. char *c = NULL;
  777. if (pos == 3) {
  778. int which = 0;
  779. int wordlen = strlen(word);
  780. RAII_VAR(struct notify_cfg *, cfg,
  781. ao2_global_obj_ref(globals), ao2_cleanup);
  782. struct notify_option *option;
  783. /* do completion for notify type */
  784. struct ao2_iterator i = ao2_iterator_init(cfg->notify_options, 0);
  785. while ((option = ao2_iterator_next(&i))) {
  786. if (!strncasecmp(word, option->name, wordlen) && ++which > state) {
  787. c = ast_strdup(option->name);
  788. }
  789. ao2_cleanup(option);
  790. if (c) {
  791. break;
  792. }
  793. }
  794. ao2_iterator_destroy(&i);
  795. return c;
  796. }
  797. if (pos == 4) {
  798. int wordlen = strlen(word);
  799. if (ast_strlen_zero(word)) {
  800. if (state == 0) {
  801. c = ast_strdup("endpoint");
  802. } else if (state == 1) {
  803. c = ast_strdup("uri");
  804. }
  805. } else if (state == 0) {
  806. if (!strncasecmp(word, "endpoint", wordlen)) {
  807. c = ast_strdup("endpoint");
  808. } else if (!strncasecmp(word, "uri", wordlen)) {
  809. c = ast_strdup("uri");
  810. }
  811. }
  812. return c;
  813. }
  814. return pos > 4 && !using_uri ? cli_complete_endpoint(word) : NULL;
  815. }
  816. /*!
  817. * \internal
  818. * \brief CLI command to send a SIP notify to an endpoint.
  819. *
  820. * \details Attempts to match the "type" given in the CLI command to a
  821. * configured one. If found, sends a NOTIFY to the endpoint
  822. * with the associated payload.
  823. */
  824. static char *cli_notify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  825. {
  826. RAII_VAR(struct notify_cfg *, cfg, NULL, ao2_cleanup);
  827. RAII_VAR(struct notify_option *, option, NULL, ao2_cleanup);
  828. int i;
  829. int using_uri = 0;
  830. switch (cmd) {
  831. case CLI_INIT:
  832. e->command = "pjsip send notify";
  833. e->usage =
  834. "Usage: pjsip send notify <type> {endpoint|uri} <peer> [<peer>...]\n"
  835. " Send a NOTIFY request to an endpoint\n"
  836. " Message types are defined in pjsip_notify.conf\n";
  837. return NULL;
  838. case CLI_GENERATE:
  839. if (a->argc > 4 && (!strcasecmp(a->argv[4], "uri"))) {
  840. using_uri = 1;
  841. }
  842. return cli_complete_notify(a->line, a->word, a->pos, a->n, using_uri);
  843. }
  844. if (a->argc < 6) {
  845. return CLI_SHOWUSAGE;
  846. }
  847. if (!strcasecmp(a->argv[4], "uri")) {
  848. using_uri = 1;
  849. } else if (strcasecmp(a->argv[4], "endpoint")) {
  850. return CLI_SHOWUSAGE;
  851. }
  852. cfg = ao2_global_obj_ref(globals);
  853. if (!(option = notify_option_find(cfg->notify_options, a->argv[3])))
  854. {
  855. ast_cli(a->fd, "Unable to find notify type '%s'\n",
  856. a->argv[3]);
  857. return CLI_FAILURE;
  858. }
  859. for (i = 5; i < a->argc; ++i) {
  860. ast_cli(a->fd, "Sending NOTIFY of type '%s' to '%s'\n",
  861. a->argv[3], a->argv[i]);
  862. switch (using_uri ? push_notify_uri(a->argv[i], option, notify_cli_uri_data_create) :
  863. push_notify(a->argv[i], option, notify_cli_data_create)) {
  864. case INVALID_ENDPOINT:
  865. ast_cli(a->fd, "Unable to retrieve endpoint %s\n",
  866. a->argv[i]);
  867. break;
  868. case ALLOC_ERROR:
  869. ast_cli(a->fd, "Unable to allocate NOTIFY task data\n");
  870. return CLI_FAILURE;
  871. case TASK_PUSH_ERROR:
  872. ast_cli(a->fd, "Unable to push NOTIFY task\n");
  873. return CLI_FAILURE;
  874. default:
  875. break;
  876. }
  877. }
  878. return CLI_SUCCESS;
  879. }
  880. static struct ast_cli_entry cli_options[] = {
  881. AST_CLI_DEFINE(cli_notify, "Send a NOTIFY request to a SIP endpoint")
  882. };
  883. /*!
  884. * \interanl
  885. * \brief Completes SIPNotify AMI command in Endpoint mode.
  886. */
  887. static void manager_notify_endpoint(struct mansession *s,
  888. const struct message *m, const char *endpoint_name)
  889. {
  890. struct ast_variable *vars = astman_get_variables_order(m, ORDER_NATURAL);
  891. if (!strncasecmp(endpoint_name, "sip/", 4)) {
  892. endpoint_name += 4;
  893. }
  894. if (!strncasecmp(endpoint_name, "pjsip/", 6)) {
  895. endpoint_name += 6;
  896. }
  897. switch (push_notify(endpoint_name, vars, notify_ami_data_create)) {
  898. case INVALID_CHANNEL:
  899. /* Shouldn't be possible. */
  900. ast_assert(0);
  901. break;
  902. case INVALID_ENDPOINT:
  903. ast_variables_destroy(vars);
  904. astman_send_error_va(s, m, "Unable to retrieve endpoint %s",
  905. endpoint_name);
  906. break;
  907. case ALLOC_ERROR:
  908. ast_variables_destroy(vars);
  909. astman_send_error(s, m, "Unable to allocate NOTIFY task data");
  910. break;
  911. case TASK_PUSH_ERROR:
  912. /* Don't need to destroy vars since it is handled by cleanup in push_notify */
  913. astman_send_error(s, m, "Unable to push NOTIFY task");
  914. break;
  915. case SUCCESS:
  916. astman_send_ack(s, m, "NOTIFY sent");
  917. break;
  918. }
  919. }
  920. /*!
  921. * \internal
  922. * \brief Completes SIPNotify AMI command in URI mode.
  923. */
  924. static void manager_notify_uri(struct mansession *s,
  925. const struct message *m, const char *uri)
  926. {
  927. struct ast_variable *vars = astman_get_variables_order(m, ORDER_NATURAL);
  928. switch (push_notify_uri(uri, vars, notify_ami_uri_data_create)) {
  929. case INVALID_CHANNEL:
  930. /* Shouldn't be possible. */
  931. ast_assert(0);
  932. break;
  933. case INVALID_ENDPOINT:
  934. /* Shouldn't be possible. */
  935. ast_assert(0);
  936. break;
  937. case ALLOC_ERROR:
  938. ast_variables_destroy(vars);
  939. astman_send_error(s, m, "Unable to allocate NOTIFY task data");
  940. break;
  941. case TASK_PUSH_ERROR:
  942. /* Don't need to destroy vars since it is handled by cleanup in push_notify_uri */
  943. astman_send_error(s, m, "Unable to push Notify task");
  944. break;
  945. case SUCCESS:
  946. astman_send_ack(s, m, "NOTIFY sent");
  947. break;
  948. }
  949. }
  950. /*!
  951. * \internal
  952. * \brief Completes SIPNotify AMI command in channel mode.
  953. */
  954. static void manager_notify_channel(struct mansession *s,
  955. const struct message *m, const char *channel)
  956. {
  957. struct ast_variable *vars = astman_get_variables_order(m, ORDER_NATURAL);
  958. switch (push_notify_channel(channel, vars, notify_ami_channel_data_create)) {
  959. case INVALID_CHANNEL:
  960. ast_variables_destroy(vars);
  961. astman_send_error(s, m, "Channel not found");
  962. break;
  963. case INVALID_ENDPOINT:
  964. /* Shouldn't be possible. */
  965. ast_assert(0);
  966. break;
  967. case ALLOC_ERROR:
  968. ast_variables_destroy(vars);
  969. astman_send_error(s, m, "Unable to allocate NOTIFY task data");
  970. break;
  971. case TASK_PUSH_ERROR:
  972. /* Don't need to destroy vars since it is handled by cleanup in push_notify_channel */
  973. astman_send_error(s, m, "Unable to push Notify task");
  974. break;
  975. case SUCCESS:
  976. astman_send_ack(s, m, "NOTIFY sent");
  977. break;
  978. }
  979. }
  980. /*!
  981. * \internal
  982. * \brief AMI entry point to send a SIP notify to an endpoint.
  983. */
  984. static int manager_notify(struct mansession *s, const struct message *m)
  985. {
  986. const char *endpoint_name = astman_get_header(m, "Endpoint");
  987. const char *uri = astman_get_header(m, "URI");
  988. const char *channel = astman_get_header(m, "Channel");
  989. int count = 0;
  990. if (!ast_strlen_zero(endpoint_name)) {
  991. ++count;
  992. }
  993. if (!ast_strlen_zero(uri)) {
  994. ++count;
  995. }
  996. if (!ast_strlen_zero(channel)) {
  997. ++count;
  998. }
  999. if (1 < count) {
  1000. astman_send_error(s, m,
  1001. "PJSIPNotify requires either an endpoint name, a SIP URI, or a channel. "
  1002. "You must use only one of them.");
  1003. } else if (!ast_strlen_zero(endpoint_name)) {
  1004. manager_notify_endpoint(s, m, endpoint_name);
  1005. } else if (!ast_strlen_zero(uri)) {
  1006. manager_notify_uri(s, m, uri);
  1007. } else if (!ast_strlen_zero(channel)) {
  1008. manager_notify_channel(s, m, channel);
  1009. } else {
  1010. astman_send_error(s, m,
  1011. "PJSIPNotify requires either an endpoint name, a SIP URI, or a channel.");
  1012. }
  1013. return 0;
  1014. }
  1015. static int load_module(void)
  1016. {
  1017. CHECK_PJSIP_MODULE_LOADED();
  1018. if (aco_info_init(&notify_cfg)) {
  1019. return AST_MODULE_LOAD_DECLINE;
  1020. }
  1021. aco_option_register_custom(&notify_cfg, "", ACO_PREFIX, notify_options,
  1022. "", notify_option_handler, 0);
  1023. if (aco_process_config(&notify_cfg, 0)) {
  1024. aco_info_destroy(&notify_cfg);
  1025. return AST_MODULE_LOAD_DECLINE;
  1026. }
  1027. ast_cli_register_multiple(cli_options, ARRAY_LEN(cli_options));
  1028. ast_manager_register_xml("PJSIPNotify", EVENT_FLAG_SYSTEM, manager_notify);
  1029. return AST_MODULE_LOAD_SUCCESS;
  1030. }
  1031. static int reload_module(void)
  1032. {
  1033. if (aco_process_config(&notify_cfg, 1) == ACO_PROCESS_ERROR) {
  1034. return AST_MODULE_LOAD_DECLINE;
  1035. }
  1036. return 0;
  1037. }
  1038. static int unload_module(void)
  1039. {
  1040. ast_manager_unregister("PJSIPNotify");
  1041. ast_cli_unregister_multiple(cli_options, ARRAY_LEN(cli_options));
  1042. aco_info_destroy(&notify_cfg);
  1043. ao2_global_obj_release(globals);
  1044. return 0;
  1045. }
  1046. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "CLI/AMI PJSIP NOTIFY Support",
  1047. .support_level = AST_MODULE_SUPPORT_CORE,
  1048. .load = load_module,
  1049. .reload = reload_module,
  1050. .unload = unload_module,
  1051. .load_pri = AST_MODPRI_APP_DEPEND,
  1052. );