res_pjsip_messaging.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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_session</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. /*** DOCUMENTATION
  25. <info name="MessageDestinationInfo" language="en_US" tech="PJSIP">
  26. <para>The <literal>destination</literal> parameter is used to construct
  27. the Request URI for an outgoing message. It can be in one of the following
  28. formats, all prefixed with the <literal>pjsip:</literal> message tech.</para>
  29. <para>
  30. </para>
  31. <enumlist>
  32. <enum name="endpoint">
  33. <para>Request URI comes from the endpoint's default aor and contact.</para>
  34. </enum>
  35. <enum name="endpoint/aor">
  36. <para>Request URI comes from the specific aor/contact.</para>
  37. </enum>
  38. <enum name="endpoint@domain">
  39. <para>Request URI from the endpoint's default aor and contact. The domain is discarded.</para>
  40. </enum>
  41. </enumlist>
  42. <para>
  43. </para>
  44. <para>These all use the endpoint to send the message with the specified URI:</para>
  45. <para>
  46. </para>
  47. <enumlist>
  48. <enum name="endpoint/&lt;sip[s]:host&gt;>"/>
  49. <enum name="endpoint/&lt;sip[s]:user@host&gt;"/>
  50. <enum name="endpoint/&quot;display name&quot; &lt;sip[s]:host&gt;"/>
  51. <enum name="endpoint/&quot;display name&quot; &lt;sip[s]:user@host&gt;"/>
  52. <enum name="endpoint/sip[s]:host"/>
  53. <enum name="endpoint/sip[s]:user@host"/>
  54. <enum name="endpoint/host"/>
  55. <enum name="endpoint/user@host"/>
  56. </enumlist>
  57. <para>
  58. </para>
  59. <para>These all use the default endpoint to send the message with the specified URI:</para>
  60. <para>
  61. </para>
  62. <enumlist>
  63. <enum name="&lt;sip[s]:host&gt;"/>
  64. <enum name="&lt;sip[s]:user@host&gt;"/>
  65. <enum name="&quot;display name&quot; &lt;sip[s]:host&gt;"/>
  66. <enum name="&quot;display name&quot; &lt;sip[s]:user@host&gt;"/>
  67. <enum name="sip[s]:host"/>
  68. <enum name="sip[s]:user@host"/>
  69. </enumlist>
  70. <para>
  71. </para>
  72. <para>These use the default endpoint to send the message with the specified host:</para>
  73. <para>
  74. </para>
  75. <enumlist>
  76. <enum name="host"/>
  77. <enum name="user@host"/>
  78. </enumlist>
  79. <para>
  80. </para>
  81. <para>This form is similar to a dialstring:</para>
  82. <para>
  83. </para>
  84. <enumlist>
  85. <enum name="PJSIP/user@endpoint"/>
  86. </enumlist>
  87. <para>
  88. </para>
  89. <para>You still need to prefix the destination with
  90. the <literal>pjsip:</literal> message technology prefix. For example:
  91. <literal>pjsip:PJSIP/8005551212@myprovider</literal>.
  92. The endpoint contact's URI will have the <literal>user</literal> inserted
  93. into it and will become the Request URI. If the contact URI already has
  94. a user specified, it will be replaced.
  95. </para>
  96. <para>
  97. </para>
  98. </info>
  99. <info name="MessageFromInfo" language="en_US" tech="PJSIP">
  100. <para>The <literal>from</literal> parameter is used to specity the <literal>From:</literal>
  101. header in the outgoing SIP MESSAGE. It will override the value specified in
  102. MESSAGE(from) which itself will override any <literal>from</literal> value from
  103. an incoming SIP MESSAGE.
  104. </para>
  105. <para>
  106. </para>
  107. </info>
  108. <info name="MessageToInfo" language="en_US" tech="PJSIP">
  109. <para>The <literal>to</literal> parameter is used to specity the <literal>To:</literal>
  110. header in the outgoing SIP MESSAGE. It will override the value specified in
  111. MESSAGE(to) which itself will override any <literal>to</literal> value from
  112. an incoming SIP MESSAGE.
  113. </para>
  114. <para>
  115. </para>
  116. </info>
  117. ***/
  118. #include "asterisk.h"
  119. #include <pjsip.h>
  120. #include <pjsip_ua.h>
  121. #include "asterisk/message.h"
  122. #include "asterisk/module.h"
  123. #include "asterisk/pbx.h"
  124. #include "asterisk/res_pjsip.h"
  125. #include "asterisk/res_pjsip_session.h"
  126. #include "asterisk/taskprocessor.h"
  127. #include "asterisk/test.h"
  128. #include "asterisk/uri.h"
  129. const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
  130. #define MAX_HDR_SIZE 512
  131. #define MAX_BODY_SIZE 1024
  132. #define MAX_USER_SIZE 128
  133. static struct ast_taskprocessor *message_serializer;
  134. /*!
  135. * \internal
  136. * \brief Checks to make sure the request has the correct content type.
  137. *
  138. * \details This module supports the following media types: "text/plain".
  139. * Return unsupported otherwise.
  140. *
  141. * \param rdata The SIP request
  142. */
  143. static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
  144. {
  145. int res;
  146. if (rdata->msg_info.msg->body && rdata->msg_info.msg->body->len) {
  147. res = ast_sip_is_content_type(
  148. &rdata->msg_info.msg->body->content_type, "text", "plain");
  149. } else {
  150. res = rdata->msg_info.ctype &&
  151. ast_sip_is_content_type(
  152. &rdata->msg_info.ctype->media, "text", "plain");
  153. }
  154. return res ? PJSIP_SC_OK : PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
  155. }
  156. /*!
  157. * \internal
  158. * \brief Checks to make sure the request has the correct content type.
  159. *
  160. * \details This module supports the following media types: "text/\*", "application/\*".
  161. * Return unsupported otherwise.
  162. *
  163. * \param rdata The SIP request
  164. */
  165. static enum pjsip_status_code check_content_type_in_dialog(const pjsip_rx_data *rdata)
  166. {
  167. int res = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
  168. static const pj_str_t text = { "text", 4};
  169. static const pj_str_t application = { "application", 11};
  170. if (!(rdata->msg_info.msg->body && rdata->msg_info.msg->body->len > 0)) {
  171. return res;
  172. }
  173. /* We'll accept any text/ or application/ content type */
  174. if (pj_stricmp(&rdata->msg_info.msg->body->content_type.type, &text) == 0
  175. || pj_stricmp(&rdata->msg_info.msg->body->content_type.type, &application) == 0) {
  176. res = PJSIP_SC_OK;
  177. } else if (rdata->msg_info.ctype
  178. && (pj_stricmp(&rdata->msg_info.ctype->media.type, &text) == 0
  179. || pj_stricmp(&rdata->msg_info.ctype->media.type, &application) == 0)) {
  180. res = PJSIP_SC_OK;
  181. }
  182. return res;
  183. }
  184. /*!
  185. * \internal
  186. * \brief Update the display name in the To uri in the tdata with the one from the supplied uri
  187. *
  188. * \param tdata the outbound message data structure
  189. * \param to uri containing the display name to replace in the the To uri
  190. *
  191. * \return 0: success, -1: failure
  192. */
  193. static int update_to_display_name(pjsip_tx_data *tdata, char *to)
  194. {
  195. pjsip_name_addr *parsed_name_addr;
  196. parsed_name_addr = (pjsip_name_addr *) pjsip_parse_uri(tdata->pool, to, strlen(to),
  197. PJSIP_PARSE_URI_AS_NAMEADDR);
  198. if (parsed_name_addr) {
  199. if (pj_strlen(&parsed_name_addr->display)) {
  200. pjsip_name_addr *name_addr =
  201. (pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri;
  202. pj_strdup(tdata->pool, &name_addr->display, &parsed_name_addr->display);
  203. }
  204. return 0;
  205. }
  206. return -1;
  207. }
  208. /*!
  209. * \internal
  210. * \brief Checks if the given msg var name should be blocked.
  211. *
  212. * \details Some headers are not allowed to be overriden by the user.
  213. * Determine if the given var header name from the user is blocked for
  214. * an outgoing MESSAGE.
  215. *
  216. * \param name name of header to see if it is blocked.
  217. *
  218. * \retval TRUE if the given header is blocked.
  219. */
  220. static int is_msg_var_blocked(const char *name)
  221. {
  222. int i;
  223. /* Don't block the Max-Forwards header because the user can override it */
  224. static const char *hdr[] = {
  225. "To",
  226. "From",
  227. "Via",
  228. "Route",
  229. "Contact",
  230. "Call-ID",
  231. "CSeq",
  232. "Allow",
  233. "Content-Length",
  234. "Content-Type",
  235. "Request-URI",
  236. };
  237. for (i = 0; i < ARRAY_LEN(hdr); ++i) {
  238. if (!strcasecmp(name, hdr[i])) {
  239. /* Block addition of this header. */
  240. return 1;
  241. }
  242. }
  243. return 0;
  244. }
  245. /*!
  246. * \internal
  247. * \brief Copies any other msg vars over to the request headers.
  248. *
  249. * \param msg The msg structure to copy headers from
  250. * \param tdata The SIP transmission data
  251. */
  252. static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata)
  253. {
  254. const char *name;
  255. const char *value;
  256. int max_forwards;
  257. struct ast_msg_var_iterator *iter;
  258. for (iter = ast_msg_var_iterator_init(msg);
  259. ast_msg_var_iterator_next(msg, iter, &name, &value);
  260. ast_msg_var_unref_current(iter)) {
  261. if (!strcasecmp(name, "Max-Forwards")) {
  262. /* Decrement Max-Forwards for SIP loop prevention. */
  263. if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) {
  264. ast_msg_var_iterator_destroy(iter);
  265. ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero. MESSAGE not sent.\n");
  266. return -1;
  267. }
  268. sprintf((char *) value, "%d", max_forwards);
  269. ast_sip_add_header(tdata, name, value);
  270. } else if (!is_msg_var_blocked(name)) {
  271. ast_sip_add_header(tdata, name, value);
  272. }
  273. }
  274. ast_msg_var_iterator_destroy(iter);
  275. return PJSIP_SC_OK;
  276. }
  277. /*!
  278. * \internal
  279. * \brief Copies any other request header data over to ast_msg structure.
  280. *
  281. * \param rdata The SIP request
  282. * \param msg The msg structure to copy headers into
  283. */
  284. static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg)
  285. {
  286. char *c;
  287. char name[MAX_HDR_SIZE];
  288. char buf[MAX_HDR_SIZE];
  289. int res = 0;
  290. pjsip_hdr *h = rdata->msg_info.msg->hdr.next;
  291. pjsip_hdr *end= &rdata->msg_info.msg->hdr;
  292. while (h != end) {
  293. if ((res = pjsip_hdr_print_on(h, buf, sizeof(buf)-1)) > 0) {
  294. buf[res] = '\0';
  295. if ((c = strchr(buf, ':'))) {
  296. ast_copy_string(buf, ast_skip_blanks(c + 1), sizeof(buf));
  297. }
  298. ast_copy_pj_str(name, &h->name, sizeof(name));
  299. if ((res = ast_msg_set_var(msg, name, buf)) != 0) {
  300. break;
  301. }
  302. }
  303. h = h->next;
  304. }
  305. return 0;
  306. }
  307. /*!
  308. * \internal
  309. * \brief Prints the message body into the given char buffer.
  310. *
  311. * \details Copies body content from the received data into the given
  312. * character buffer removing any extra carriage return/line feeds.
  313. *
  314. * \param rdata The SIP request
  315. * \param buf Buffer to fill
  316. * \param len The length of the buffer
  317. */
  318. static int print_body(pjsip_rx_data *rdata, char *buf, int len)
  319. {
  320. int res;
  321. if (!rdata->msg_info.msg->body || !rdata->msg_info.msg->body->len) {
  322. return 0;
  323. }
  324. if ((res = rdata->msg_info.msg->body->print_body(
  325. rdata->msg_info.msg->body, buf, len)) < 0) {
  326. return res;
  327. }
  328. /* remove any trailing carriage return/line feeds */
  329. while (res > 0 && ((buf[--res] == '\r') || (buf[res] == '\n')));
  330. buf[++res] = '\0';
  331. return res;
  332. }
  333. /*!
  334. * \internal
  335. * \brief Converts a 'sip:' uri to a 'pjsip:' so it can be found by
  336. * the message tech.
  337. *
  338. * \param buf uri to insert 'pjsip' into
  339. * \param size length of the uri in buf
  340. * \param capacity total size of buf
  341. */
  342. static char *sip_to_pjsip(char *buf, int size, int capacity)
  343. {
  344. int count;
  345. const char *scheme;
  346. char *res = buf;
  347. /* remove any wrapping brackets */
  348. if (*buf == '<') {
  349. ++buf;
  350. --size;
  351. }
  352. scheme = strncmp(buf, "sip", 3) ? "pjsip:" : "pj";
  353. count = strlen(scheme);
  354. if (count + size >= capacity) {
  355. ast_log(LOG_WARNING, "Unable to handle MESSAGE- incoming uri "
  356. "too large for given buffer\n");
  357. return NULL;
  358. }
  359. memmove(res + count, buf, size);
  360. memcpy(res, scheme, count);
  361. buf += size - 1;
  362. if (*buf == '>') {
  363. *buf = '\0';
  364. }
  365. return res;
  366. }
  367. /*!
  368. * \internal
  369. * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
  370. *
  371. * \details Attempts to fill in as much information as possible into the given
  372. * msg structure copied from the given request data.
  373. *
  374. * \param rdata The SIP request
  375. * \param msg The asterisk message structure to fill in.
  376. */
  377. static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
  378. {
  379. RAII_VAR(struct ast_sip_endpoint *, endpt, NULL, ao2_cleanup);
  380. pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
  381. pjsip_name_addr *name_addr;
  382. char buf[MAX_BODY_SIZE];
  383. const char *field;
  384. const char *context;
  385. char exten[AST_MAX_EXTENSION];
  386. int res = 0;
  387. int size;
  388. if (!ast_sip_is_allowed_uri(ruri)) {
  389. return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
  390. }
  391. ast_copy_pj_str(exten, ast_sip_pjsip_uri_get_username(ruri), AST_MAX_EXTENSION);
  392. /*
  393. * We may want to match in the dialplan without any user
  394. * options getting in the way.
  395. */
  396. AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
  397. endpt = ast_pjsip_rdata_get_endpoint(rdata);
  398. ast_assert(endpt != NULL);
  399. context = S_OR(endpt->message_context, endpt->context);
  400. res |= ast_msg_set_context(msg, "%s", context);
  401. res |= ast_msg_set_exten(msg, "%s", exten);
  402. /* to header */
  403. name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
  404. size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
  405. if (size <= 0) {
  406. return PJSIP_SC_INTERNAL_SERVER_ERROR;
  407. }
  408. buf[size] = '\0';
  409. res |= ast_msg_set_to(msg, "%s", sip_to_pjsip(buf, ++size, sizeof(buf) - 1));
  410. /* from header */
  411. name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
  412. size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
  413. if (size <= 0) {
  414. return PJSIP_SC_INTERNAL_SERVER_ERROR;
  415. }
  416. buf[size] = '\0';
  417. res |= ast_msg_set_from(msg, "%s", buf);
  418. field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf) - 1, 3);
  419. res |= ast_msg_set_var(msg, "PJSIP_RECVADDR", field);
  420. switch (rdata->tp_info.transport->key.type) {
  421. case PJSIP_TRANSPORT_UDP:
  422. case PJSIP_TRANSPORT_UDP6:
  423. field = "udp";
  424. break;
  425. case PJSIP_TRANSPORT_TCP:
  426. case PJSIP_TRANSPORT_TCP6:
  427. field = "tcp";
  428. break;
  429. case PJSIP_TRANSPORT_TLS:
  430. case PJSIP_TRANSPORT_TLS6:
  431. field = "tls";
  432. break;
  433. default:
  434. field = rdata->tp_info.transport->type_name;
  435. }
  436. ast_msg_set_var(msg, "PJSIP_TRANSPORT", field);
  437. if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
  438. res |= ast_msg_set_body(msg, "%s", buf);
  439. }
  440. /* endpoint name */
  441. res |= ast_msg_set_tech(msg, "%s", "PJSIP");
  442. res |= ast_msg_set_endpoint(msg, "%s", ast_sorcery_object_get_id(endpt));
  443. if (endpt->id.self.name.valid) {
  444. res |= ast_msg_set_var(msg, "PJSIP_ENDPOINT", endpt->id.self.name.str);
  445. }
  446. res |= headers_to_vars(rdata, msg);
  447. return !res ? PJSIP_SC_OK : PJSIP_SC_INTERNAL_SERVER_ERROR;
  448. }
  449. struct msg_data {
  450. struct ast_msg *msg;
  451. char *destination;
  452. char *from;
  453. };
  454. static void msg_data_destroy(void *obj)
  455. {
  456. struct msg_data *mdata = obj;
  457. ast_free(mdata->from);
  458. ast_free(mdata->destination);
  459. ast_msg_destroy(mdata->msg);
  460. }
  461. static struct msg_data *msg_data_create(const struct ast_msg *msg, const char *destination, const char *from)
  462. {
  463. char *uri_params;
  464. struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
  465. if (!mdata) {
  466. return NULL;
  467. }
  468. /* typecast to suppress const warning */
  469. mdata->msg = ast_msg_ref((struct ast_msg *) msg);
  470. /* To starts with 'pjsip:' which needs to be removed. */
  471. if (!(destination = strchr(destination, ':'))) {
  472. ao2_ref(mdata, -1);
  473. return NULL;
  474. }
  475. ++destination;/* Now skip the ':' */
  476. mdata->destination = ast_strdup(destination);
  477. mdata->from = ast_strdup(from);
  478. /*
  479. * Sometimes from URI can contain URI parameters, so remove them.
  480. *
  481. * sip:user;user-options@domain;uri-parameters
  482. */
  483. uri_params = strchr(mdata->from, '@');
  484. if (uri_params && (uri_params = strchr(mdata->from, ';'))) {
  485. *uri_params = '\0';
  486. }
  487. return mdata;
  488. }
  489. static void update_content_type(pjsip_tx_data *tdata, struct ast_msg *msg, struct ast_sip_body *body)
  490. {
  491. static const pj_str_t CONTENT_TYPE = { "Content-Type", sizeof("Content-Type") - 1 };
  492. const char *content_type = ast_msg_get_var(msg, pj_strbuf(&CONTENT_TYPE));
  493. if (content_type) {
  494. pj_str_t type, subtype;
  495. pjsip_ctype_hdr *parsed;
  496. /* Let pjsip do the parsing for us */
  497. parsed = pjsip_parse_hdr(tdata->pool, &CONTENT_TYPE,
  498. ast_strdupa(content_type), strlen(content_type),
  499. NULL);
  500. if (!parsed) {
  501. ast_log(LOG_WARNING, "Failed to parse '%s' as a content type. Using text/plain\n",
  502. content_type);
  503. return;
  504. }
  505. /* We need to turn type and subtype into zero-terminated strings */
  506. pj_strdup_with_null(tdata->pool, &type, &parsed->media.type);
  507. pj_strdup_with_null(tdata->pool, &subtype, &parsed->media.subtype);
  508. body->type = pj_strbuf(&type);
  509. body->subtype = pj_strbuf(&subtype);
  510. }
  511. }
  512. /*!
  513. * \internal
  514. * \brief Send a MESSAGE
  515. *
  516. * \param data The outbound message data structure
  517. *
  518. * \return 0: success, -1: failure
  519. *
  520. * mdata contains the To and From specified in the call to the MessageSend
  521. * dialplan app. It also contains the ast_msg object that contains the
  522. * message body and may contain the To and From from the channel datastore,
  523. * usually set with the MESSAGE or MESSAGE_DATA dialplan functions but
  524. * could also come from an incoming sip MESSAGE.
  525. *
  526. * The mdata->to is always used as the basis for the Request URI
  527. * while the mdata->msg->to is used for the To header. If
  528. * mdata->msg->to isn't available, mdata->to is used for the To header.
  529. *
  530. */
  531. static int msg_send(void *data)
  532. {
  533. struct msg_data *mdata = data; /* The caller holds a reference */
  534. struct ast_sip_body body = {
  535. .type = "text",
  536. .subtype = "plain",
  537. .body_text = ast_msg_get_body(mdata->msg)
  538. };
  539. pjsip_tx_data *tdata;
  540. RAII_VAR(char *, uri, NULL, ast_free);
  541. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  542. ast_debug(3, "mdata From: %s msg From: %s mdata Destination: %s msg To: %s\n",
  543. mdata->from, ast_msg_get_from(mdata->msg), mdata->destination, ast_msg_get_to(mdata->msg));
  544. endpoint = ast_sip_get_endpoint(mdata->destination, 1, &uri);
  545. if (!endpoint) {
  546. ast_log(LOG_ERROR,
  547. "PJSIP MESSAGE - Could not find endpoint '%s' and no default outbound endpoint configured\n",
  548. mdata->destination);
  549. ast_test_suite_event_notify("MSG_ENDPOINT_URI_FAIL",
  550. "MdataFrom: %s\r\n"
  551. "MsgFrom: %s\r\n"
  552. "MdataDestination: %s\r\n"
  553. "MsgTo: %s\r\n",
  554. mdata->from,
  555. ast_msg_get_from(mdata->msg),
  556. mdata->destination,
  557. ast_msg_get_to(mdata->msg));
  558. return -1;
  559. }
  560. ast_debug(3, "Request URI: %s\n", uri);
  561. if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
  562. ast_log(LOG_WARNING, "PJSIP MESSAGE - Could not create request\n");
  563. return -1;
  564. }
  565. /* If there was a To in the actual message, */
  566. if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) {
  567. char *msg_to = ast_strdupa(ast_msg_get_to(mdata->msg));
  568. /*
  569. * It's possible that the message To was copied from
  570. * an incoming MESSAGE in which case it'll have the
  571. * pjsip: tech prepended to it. We need to remove it.
  572. */
  573. if (ast_begins_with(msg_to, "pjsip:")) {
  574. msg_to += 6;
  575. }
  576. ast_sip_update_to_uri(tdata, msg_to);
  577. } else {
  578. /*
  579. * If there was no To in the message, it's still possible
  580. * that there is a display name in the mdata To. If so,
  581. * we'll copy the URI display name to the tdata To.
  582. */
  583. update_to_display_name(tdata, uri);
  584. }
  585. if (!ast_strlen_zero(mdata->from)) {
  586. ast_sip_update_from(tdata, mdata->from);
  587. } else if (!ast_strlen_zero(ast_msg_get_from(mdata->msg))) {
  588. ast_sip_update_from(tdata, (char *)ast_msg_get_from(mdata->msg));
  589. }
  590. #ifdef TEST_FRAMEWORK
  591. {
  592. pjsip_name_addr *tdata_name_addr;
  593. pjsip_sip_uri *tdata_sip_uri;
  594. char touri[128];
  595. char fromuri[128];
  596. tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri;
  597. tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri);
  598. pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, touri, sizeof(touri));
  599. tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
  600. tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri);
  601. pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, fromuri, sizeof(fromuri));
  602. ast_test_suite_event_notify("MSG_FROMTO_URI",
  603. "MdataFrom: %s\r\n"
  604. "MsgFrom: %s\r\n"
  605. "MdataDestination: %s\r\n"
  606. "MsgTo: %s\r\n"
  607. "Endpoint: %s\r\n"
  608. "RequestURI: %s\r\n"
  609. "ToURI: %s\r\n"
  610. "FromURI: %s\r\n",
  611. mdata->from,
  612. ast_msg_get_from(mdata->msg),
  613. mdata->destination,
  614. ast_msg_get_to(mdata->msg),
  615. ast_sorcery_object_get_id(endpoint),
  616. uri,
  617. touri,
  618. fromuri
  619. );
  620. }
  621. #endif
  622. update_content_type(tdata, mdata->msg, &body);
  623. if (ast_sip_add_body(tdata, &body)) {
  624. pjsip_tx_data_dec_ref(tdata);
  625. ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not add body to request\n");
  626. return -1;
  627. }
  628. /*
  629. * This copies any headers set with MESSAGE_DATA() to the
  630. * tdata.
  631. */
  632. vars_to_headers(mdata->msg, tdata);
  633. ast_debug(1, "Sending message to '%s' (via endpoint %s) from '%s'\n",
  634. uri, ast_sorcery_object_get_id(endpoint), mdata->from);
  635. if (ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL)) {
  636. ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request\n");
  637. return -1;
  638. }
  639. return 0;
  640. }
  641. static int sip_msg_send(const struct ast_msg *msg, const char *destination, const char *from)
  642. {
  643. struct msg_data *mdata;
  644. int res;
  645. if (ast_strlen_zero(destination)) {
  646. ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI must be specified\n");
  647. return -1;
  648. }
  649. mdata = msg_data_create(msg, destination, from);
  650. if (!mdata) {
  651. return -1;
  652. }
  653. res = ast_sip_push_task_wait_serializer(message_serializer, msg_send, mdata);
  654. ao2_ref(mdata, -1);
  655. return res;
  656. }
  657. static const struct ast_msg_tech msg_tech = {
  658. .name = "pjsip",
  659. .msg_send = sip_msg_send,
  660. };
  661. static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
  662. pjsip_dialog *dlg, pjsip_transaction *tsx)
  663. {
  664. pjsip_tx_data *tdata;
  665. pj_status_t status;
  666. status = ast_sip_create_response(rdata, code, NULL, &tdata);
  667. if (status != PJ_SUCCESS) {
  668. ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
  669. return status;
  670. }
  671. if (dlg && tsx) {
  672. status = pjsip_dlg_send_response(dlg, tsx, tdata);
  673. } else {
  674. struct ast_sip_endpoint *endpoint;
  675. endpoint = ast_pjsip_rdata_get_endpoint(rdata);
  676. status = ast_sip_send_stateful_response(rdata, tdata, endpoint);
  677. ao2_cleanup(endpoint);
  678. }
  679. if (status != PJ_SUCCESS) {
  680. ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
  681. }
  682. return status;
  683. }
  684. static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
  685. {
  686. enum pjsip_status_code code;
  687. struct ast_msg *msg;
  688. /* if not a MESSAGE, don't handle */
  689. if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
  690. return PJ_FALSE;
  691. }
  692. code = check_content_type(rdata);
  693. if (code != PJSIP_SC_OK) {
  694. send_response(rdata, code, NULL, NULL);
  695. return PJ_TRUE;
  696. }
  697. msg = ast_msg_alloc();
  698. if (!msg) {
  699. send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
  700. return PJ_TRUE;
  701. }
  702. code = rx_data_to_ast_msg(rdata, msg);
  703. if (code != PJSIP_SC_OK) {
  704. send_response(rdata, code, NULL, NULL);
  705. ast_msg_destroy(msg);
  706. return PJ_TRUE;
  707. }
  708. if (!ast_msg_has_destination(msg)) {
  709. ast_debug(1, "MESSAGE request received, but no handler wanted it\n");
  710. send_response(rdata, PJSIP_SC_NOT_FOUND, NULL, NULL);
  711. ast_msg_destroy(msg);
  712. return PJ_TRUE;
  713. }
  714. /* Send it to the messaging core.
  715. *
  716. * If we are unable to send a response, the most likely reason is that we
  717. * are handling a retransmission of an incoming MESSAGE and were unable to
  718. * create a transaction due to a duplicate key. If we are unable to send
  719. * a response, we should not queue the message to the dialplan
  720. */
  721. if (!send_response(rdata, PJSIP_SC_ACCEPTED, NULL, NULL)) {
  722. ast_msg_queue(msg);
  723. }
  724. return PJ_TRUE;
  725. }
  726. static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  727. {
  728. enum pjsip_status_code code;
  729. int rc;
  730. pjsip_dialog *dlg = session->inv_session->dlg;
  731. pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
  732. struct ast_msg_data *msg;
  733. struct ast_party_caller *caller;
  734. pjsip_name_addr *name_addr;
  735. size_t from_len;
  736. size_t to_len;
  737. struct ast_msg_data_attribute attrs[4];
  738. int pos = 0;
  739. int body_pos;
  740. if (!session->channel) {
  741. send_response(rdata, PJSIP_SC_NOT_FOUND, dlg, tsx);
  742. return 0;
  743. }
  744. code = check_content_type_in_dialog(rdata);
  745. if (code != PJSIP_SC_OK) {
  746. send_response(rdata, code, dlg, tsx);
  747. return 0;
  748. }
  749. caller = ast_channel_caller(session->channel);
  750. name_addr = (pjsip_name_addr *) rdata->msg_info.from->uri;
  751. from_len = pj_strlen(&name_addr->display);
  752. if (from_len) {
  753. attrs[pos].type = AST_MSG_DATA_ATTR_FROM;
  754. from_len++;
  755. attrs[pos].value = ast_alloca(from_len);
  756. ast_copy_pj_str(attrs[pos].value, &name_addr->display, from_len);
  757. pos++;
  758. } else if (caller->id.name.valid && !ast_strlen_zero(caller->id.name.str)) {
  759. attrs[pos].type = AST_MSG_DATA_ATTR_FROM;
  760. attrs[pos].value = caller->id.name.str;
  761. pos++;
  762. }
  763. name_addr = (pjsip_name_addr *) rdata->msg_info.to->uri;
  764. to_len = pj_strlen(&name_addr->display);
  765. if (to_len) {
  766. attrs[pos].type = AST_MSG_DATA_ATTR_TO;
  767. to_len++;
  768. attrs[pos].value = ast_alloca(to_len);
  769. ast_copy_pj_str(attrs[pos].value, &name_addr->display, to_len);
  770. pos++;
  771. }
  772. attrs[pos].type = AST_MSG_DATA_ATTR_CONTENT_TYPE;
  773. attrs[pos].value = ast_alloca(rdata->msg_info.msg->body->content_type.type.slen
  774. + rdata->msg_info.msg->body->content_type.subtype.slen + 2);
  775. sprintf(attrs[pos].value, "%.*s/%.*s",
  776. (int)rdata->msg_info.msg->body->content_type.type.slen,
  777. rdata->msg_info.msg->body->content_type.type.ptr,
  778. (int)rdata->msg_info.msg->body->content_type.subtype.slen,
  779. rdata->msg_info.msg->body->content_type.subtype.ptr);
  780. pos++;
  781. body_pos = pos;
  782. attrs[pos].type = AST_MSG_DATA_ATTR_BODY;
  783. attrs[pos].value = ast_malloc(rdata->msg_info.msg->body->len + 1);
  784. if (!attrs[pos].value) {
  785. send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
  786. return 0;
  787. }
  788. ast_copy_string(attrs[pos].value, rdata->msg_info.msg->body->data, rdata->msg_info.msg->body->len + 1);
  789. pos++;
  790. msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_IN_DIALOG, attrs, pos);
  791. if (!msg) {
  792. ast_free(attrs[body_pos].value);
  793. send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
  794. return 0;
  795. }
  796. ast_debug(1, "Received in-dialog MESSAGE from '%s:%s': %s %s\n",
  797. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
  798. ast_channel_name(session->channel),
  799. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO),
  800. ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY));
  801. rc = ast_msg_data_queue_frame(session->channel, msg);
  802. ast_free(attrs[body_pos].value);
  803. ast_free(msg);
  804. if (rc != 0) {
  805. send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
  806. } else {
  807. send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
  808. }
  809. return 0;
  810. }
  811. static struct ast_sip_session_supplement messaging_supplement = {
  812. .method = "MESSAGE",
  813. .incoming_request = incoming_in_dialog_request
  814. };
  815. static pjsip_module messaging_module = {
  816. .name = {"Messaging Module", 16},
  817. .id = -1,
  818. .priority = PJSIP_MOD_PRIORITY_APPLICATION,
  819. .on_rx_request = module_on_rx_request,
  820. };
  821. static int load_module(void)
  822. {
  823. if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
  824. return AST_MODULE_LOAD_DECLINE;
  825. }
  826. if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(),
  827. NULL, PJSIP_H_ALLOW, NULL, 1,
  828. &pjsip_message_method.name) != PJ_SUCCESS) {
  829. ast_sip_unregister_service(&messaging_module);
  830. return AST_MODULE_LOAD_DECLINE;
  831. }
  832. if (ast_msg_tech_register(&msg_tech)) {
  833. ast_sip_unregister_service(&messaging_module);
  834. return AST_MODULE_LOAD_DECLINE;
  835. }
  836. message_serializer = ast_sip_create_serializer("pjsip/messaging");
  837. if (!message_serializer) {
  838. ast_sip_unregister_service(&messaging_module);
  839. ast_msg_tech_unregister(&msg_tech);
  840. return AST_MODULE_LOAD_DECLINE;
  841. }
  842. ast_sip_session_register_supplement(&messaging_supplement);
  843. return AST_MODULE_LOAD_SUCCESS;
  844. }
  845. static int unload_module(void)
  846. {
  847. ast_sip_session_unregister_supplement(&messaging_supplement);
  848. ast_msg_tech_unregister(&msg_tech);
  849. ast_sip_unregister_service(&messaging_module);
  850. ast_taskprocessor_unreference(message_serializer);
  851. return 0;
  852. }
  853. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support",
  854. .support_level = AST_MODULE_SUPPORT_CORE,
  855. .load = load_module,
  856. .unload = unload_module,
  857. .load_pri = AST_MODPRI_APP_DEPEND,
  858. .requires = "res_pjsip,res_pjsip_session",
  859. );