res_pjsip_messaging.c 25 KB

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