res_pjsip_messaging.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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="PJSIPMessageFromInfo" 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="PJSIPMessageToInfo" 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. !pj_strcmp2(&rdata->msg_info.ctype->media.type, "text") &&
  65. !pj_strcmp2(&rdata->msg_info.ctype->media.subtype, "plain");
  66. }
  67. return res ? PJSIP_SC_OK : PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
  68. }
  69. /*!
  70. * \internal
  71. * \brief Puts pointer past 'sip[s]:' string that should be at the
  72. * front of the given 'fromto' parameter
  73. *
  74. * \param fromto 'From' or 'To' field containing 'sip:'
  75. */
  76. static const char *skip_sip(const char *fromto)
  77. {
  78. const char *p;
  79. /* need to be one past 'sip:' or 'sips:' */
  80. if (!(p = strstr(fromto, "sip"))) {
  81. return fromto;
  82. }
  83. p += 3;
  84. if (*p == 's') {
  85. ++p;
  86. }
  87. return ++p;
  88. }
  89. /*!
  90. * \internal
  91. * \brief Retrieves an endpoint if specified in the given 'to'
  92. *
  93. * Expects the given 'to' to be in one of the following formats:
  94. * sip[s]:endpoint[/aor]
  95. * sip[s]:endpoint[/uri]
  96. * sip[s]:uri <-- will use default outbound endpoint
  97. *
  98. * If an optional aor is given it will try to find an associated uri
  99. * to return. If an optional uri is given then that will be returned,
  100. * otherwise uri will be NULL.
  101. *
  102. * \param to 'From' or 'To' field with possible endpoint
  103. * \param uri Optional uri to return
  104. */
  105. static struct ast_sip_endpoint* get_outbound_endpoint(
  106. const char *to, char **uri)
  107. {
  108. char *name, *aor_uri;
  109. struct ast_sip_endpoint* endpoint;
  110. RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
  111. RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
  112. name = ast_strdupa(skip_sip(to));
  113. /* attempt to extract the endpoint name */
  114. if ((aor_uri = strchr(name, '/'))) {
  115. /* format was 'endpoint/' */
  116. *aor_uri++ = '\0';
  117. } else if ((aor_uri = strchr(name, '@'))) {
  118. /* format was 'endpoint@' - don't use the rest */
  119. *aor_uri = '\0';
  120. }
  121. /* at this point, if name is not empty then it
  122. might be an endpoint, so try to retrieve it */
  123. if (ast_strlen_zero(name) || !(endpoint = ast_sorcery_retrieve_by_id(
  124. ast_sip_get_sorcery(), "endpoint", name))) {
  125. /* an endpoint was not found, so assume sending directly
  126. to a uri and use the default outbound endpoint */
  127. *uri = ast_strdup(to);
  128. return ast_sip_default_outbound_endpoint();
  129. }
  130. *uri = aor_uri;
  131. if (*uri) {
  132. char *end = strchr(*uri, '>');
  133. if (end) {
  134. *end++ = '\0';
  135. }
  136. /* if what's in 'uri' is a retrievable aor use the uri on it
  137. instead, otherwise assume what's there is already a uri*/
  138. if ((aor = ast_sip_location_retrieve_aor(*uri)) &&
  139. (contact = ast_sip_location_retrieve_first_aor_contact(aor))) {
  140. *uri = (char*)contact->uri;
  141. }
  142. /* need to copy because underlying uri goes away */
  143. *uri = ast_strdup(*uri);
  144. }
  145. return endpoint;
  146. }
  147. /*!
  148. * \internal
  149. * \brief Overwrite fields in the outbound 'To' header
  150. *
  151. * Updates display name in an outgoing To header.
  152. *
  153. * \param tdata the outbound message data structure
  154. * \param to info to copy into the header
  155. */
  156. static void update_to(pjsip_tx_data *tdata, char *to)
  157. {
  158. pjsip_name_addr *name_addr = (pjsip_name_addr *)
  159. PJSIP_MSG_TO_HDR(tdata->msg)->uri;
  160. pjsip_uri *parsed;
  161. if ((parsed = pjsip_parse_uri(tdata->pool, to, strlen(to),
  162. PJSIP_PARSE_URI_AS_NAMEADDR))) {
  163. pjsip_name_addr *parsed_name_addr = (pjsip_name_addr *)parsed;
  164. if (pj_strlen(&parsed_name_addr->display)) {
  165. pj_strdup(tdata->pool, &name_addr->display,
  166. &parsed_name_addr->display);
  167. }
  168. }
  169. }
  170. /*!
  171. * \internal
  172. * \brief Overwrite fields in the outbound 'From' header
  173. *
  174. * The outbound 'From' header is created/added in ast_sip_create_request with
  175. * default data. If available that data may be info specified in the 'from_user'
  176. * and 'from_domain' options found on the endpoint. That information will be
  177. * overwritten with data in the given 'from' parameter.
  178. *
  179. * \param tdata the outbound message data structure
  180. * \param from info to copy into the header
  181. */
  182. static void update_from(pjsip_tx_data *tdata, char *from)
  183. {
  184. pjsip_name_addr *name_addr = (pjsip_name_addr *)
  185. PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
  186. pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr);
  187. pjsip_uri *parsed;
  188. if (ast_strlen_zero(from)) {
  189. return;
  190. }
  191. if ((parsed = pjsip_parse_uri(tdata->pool, from, strlen(from),
  192. PJSIP_PARSE_URI_AS_NAMEADDR))) {
  193. pjsip_name_addr *parsed_name_addr = (pjsip_name_addr *)parsed;
  194. pjsip_sip_uri *parsed_uri = pjsip_uri_get_uri(parsed_name_addr->uri);
  195. if (pj_strlen(&parsed_name_addr->display)) {
  196. pj_strdup(tdata->pool, &name_addr->display,
  197. &parsed_name_addr->display);
  198. }
  199. pj_strdup(tdata->pool, &uri->user, &parsed_uri->user);
  200. pj_strdup(tdata->pool, &uri->host, &parsed_uri->host);
  201. uri->port = parsed_uri->port;
  202. } else {
  203. /* assume it is 'user[@domain]' format */
  204. char *domain = strchr(from, '@');
  205. if (domain) {
  206. *domain++ = '\0';
  207. pj_strdup2(tdata->pool, &uri->host, domain);
  208. }
  209. pj_strdup2(tdata->pool, &uri->user, from);
  210. }
  211. }
  212. /*!
  213. * \internal
  214. * \brief Checks if the given msg var name should be blocked.
  215. *
  216. * \details Some headers are not allowed to be overriden by the user.
  217. * Determine if the given var header name from the user is blocked for
  218. * an outgoing MESSAGE.
  219. *
  220. * \param name name of header to see if it is blocked.
  221. *
  222. * \retval TRUE if the given header is blocked.
  223. */
  224. static int is_msg_var_blocked(const char *name)
  225. {
  226. int i;
  227. /*
  228. * Don't block Content-Type or Max-Forwards headers because the
  229. * user can override them.
  230. */
  231. static const char *hdr[] = {
  232. "To",
  233. "From",
  234. "Via",
  235. "Route",
  236. "Contact",
  237. "Call-ID",
  238. "CSeq",
  239. "Allow",
  240. "Content-Length",
  241. "Request-URI",
  242. };
  243. for (i = 0; i < ARRAY_LEN(hdr); ++i) {
  244. if (!strcasecmp(name, hdr[i])) {
  245. /* Block addition of this header. */
  246. return 1;
  247. }
  248. }
  249. return 0;
  250. }
  251. /*!
  252. * \internal
  253. * \brief Copies any other msg vars over to the request headers.
  254. *
  255. * \param msg The msg structure to copy headers from
  256. * \param tdata The SIP transmission data
  257. */
  258. static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata)
  259. {
  260. const char *name;
  261. const char *value;
  262. int max_forwards;
  263. struct ast_msg_var_iterator *iter;
  264. for (iter = ast_msg_var_iterator_init(msg);
  265. ast_msg_var_iterator_next(msg, iter, &name, &value);
  266. ast_msg_var_unref_current(iter)) {
  267. if (!strcasecmp(name, "Max-Forwards")) {
  268. /* Decrement Max-Forwards for SIP loop prevention. */
  269. if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) {
  270. ast_msg_var_iterator_destroy(iter);
  271. ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero. MESSAGE not sent.\n");
  272. return -1;
  273. }
  274. sprintf((char *) value, "%d", max_forwards);
  275. ast_sip_add_header(tdata, name, value);
  276. } else if (!is_msg_var_blocked(name)) {
  277. ast_sip_add_header(tdata, name, value);
  278. }
  279. }
  280. ast_msg_var_iterator_destroy(iter);
  281. return PJSIP_SC_OK;
  282. }
  283. /*!
  284. * \internal
  285. * \brief Copies any other request header data over to ast_msg structure.
  286. *
  287. * \param rdata The SIP request
  288. * \param msg The msg structure to copy headers into
  289. */
  290. static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg)
  291. {
  292. char *c;
  293. char name[MAX_HDR_SIZE];
  294. char buf[MAX_HDR_SIZE];
  295. int res = 0;
  296. pjsip_hdr *h = rdata->msg_info.msg->hdr.next;
  297. pjsip_hdr *end= &rdata->msg_info.msg->hdr;
  298. while (h != end) {
  299. if ((res = pjsip_hdr_print_on(h, buf, sizeof(buf)-1)) > 0) {
  300. buf[res] = '\0';
  301. if ((c = strchr(buf, ':'))) {
  302. ast_copy_string(buf, ast_skip_blanks(c + 1), sizeof(buf));
  303. }
  304. ast_copy_pj_str(name, &h->name, sizeof(name));
  305. if ((res = ast_msg_set_var(msg, name, buf)) != 0) {
  306. break;
  307. }
  308. }
  309. h = h->next;
  310. }
  311. return 0;
  312. }
  313. /*!
  314. * \internal
  315. * \brief Prints the message body into the given char buffer.
  316. *
  317. * \details Copies body content from the received data into the given
  318. * character buffer removing any extra carriage return/line feeds.
  319. *
  320. * \param rdata The SIP request
  321. * \param buf Buffer to fill
  322. * \param len The length of the buffer
  323. */
  324. static int print_body(pjsip_rx_data *rdata, char *buf, int len)
  325. {
  326. int res;
  327. if (!rdata->msg_info.msg->body || !rdata->msg_info.msg->body->len) {
  328. return 0;
  329. }
  330. if ((res = rdata->msg_info.msg->body->print_body(
  331. rdata->msg_info.msg->body, buf, len)) < 0) {
  332. return res;
  333. }
  334. /* remove any trailing carriage return/line feeds */
  335. while (res > 0 && ((buf[--res] == '\r') || (buf[res] == '\n')));
  336. buf[++res] = '\0';
  337. return res;
  338. }
  339. /*!
  340. * \internal
  341. * \brief Converts a 'sip:' uri to a 'pjsip:' so it can be found by
  342. * the message tech.
  343. *
  344. * \param buf uri to insert 'pjsip' into
  345. * \param size length of the uri in buf
  346. * \param capacity total size of buf
  347. */
  348. static char *sip_to_pjsip(char *buf, int size, int capacity)
  349. {
  350. int count;
  351. const char *scheme;
  352. char *res = buf;
  353. /* remove any wrapping brackets */
  354. if (*buf == '<') {
  355. ++buf;
  356. --size;
  357. }
  358. scheme = strncmp(buf, "sip", 3) ? "pjsip:" : "pj";
  359. count = strlen(scheme);
  360. if (count + size >= capacity) {
  361. ast_log(LOG_WARNING, "Unable to handle MESSAGE- incoming uri "
  362. "too large for given buffer\n");
  363. return NULL;
  364. }
  365. memmove(res + count, buf, size);
  366. memcpy(res, scheme, count);
  367. buf += size - 1;
  368. if (*buf == '>') {
  369. *buf = '\0';
  370. }
  371. return res;
  372. }
  373. /*!
  374. * \internal
  375. * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
  376. *
  377. * \details Attempts to fill in as much information as possible into the given
  378. * msg structure copied from the given request data.
  379. *
  380. * \param rdata The SIP request
  381. * \param msg The asterisk message structure to fill in.
  382. */
  383. static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
  384. {
  385. RAII_VAR(struct ast_sip_endpoint *, endpt, NULL, ao2_cleanup);
  386. pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
  387. pjsip_sip_uri *sip_ruri;
  388. pjsip_name_addr *name_addr;
  389. char buf[MAX_BODY_SIZE];
  390. const char *field;
  391. const char *context;
  392. char exten[AST_MAX_EXTENSION];
  393. int res = 0;
  394. int size;
  395. if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
  396. return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
  397. }
  398. sip_ruri = pjsip_uri_get_uri(ruri);
  399. ast_copy_pj_str(exten, &sip_ruri->user, AST_MAX_EXTENSION);
  400. endpt = ast_pjsip_rdata_get_endpoint(rdata);
  401. ast_assert(endpt != NULL);
  402. context = S_OR(endpt->message_context, endpt->context);
  403. res |= ast_msg_set_context(msg, "%s", context);
  404. res |= ast_msg_set_exten(msg, "%s", exten);
  405. /* to header */
  406. name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
  407. size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
  408. if (size <= 0) {
  409. return PJSIP_SC_INTERNAL_SERVER_ERROR;
  410. }
  411. buf[size] = '\0';
  412. res |= ast_msg_set_to(msg, "%s", sip_to_pjsip(buf, ++size, sizeof(buf) - 1));
  413. /* from header */
  414. name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
  415. size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
  416. if (size <= 0) {
  417. return PJSIP_SC_INTERNAL_SERVER_ERROR;
  418. }
  419. buf[size] = '\0';
  420. res |= ast_msg_set_from(msg, "%s", buf);
  421. field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf) - 1, 1);
  422. res |= ast_msg_set_var(msg, "PJSIP_RECVADDR", field);
  423. if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
  424. res |= ast_msg_set_body(msg, "%s", buf);
  425. }
  426. /* endpoint name */
  427. res |= ast_msg_set_tech(msg, "%s", "PJSIP");
  428. res |= ast_msg_set_endpoint(msg, "%s", ast_sorcery_object_get_id(endpt));
  429. if (endpt->id.self.name.valid) {
  430. res |= ast_msg_set_var(msg, "PJSIP_ENDPOINT", endpt->id.self.name.str);
  431. }
  432. res |= headers_to_vars(rdata, msg);
  433. return !res ? PJSIP_SC_OK : PJSIP_SC_INTERNAL_SERVER_ERROR;
  434. }
  435. struct msg_data {
  436. struct ast_msg *msg;
  437. char *to;
  438. char *from;
  439. };
  440. static void msg_data_destroy(void *obj)
  441. {
  442. struct msg_data *mdata = obj;
  443. ast_free(mdata->from);
  444. ast_free(mdata->to);
  445. ast_msg_destroy(mdata->msg);
  446. }
  447. static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
  448. {
  449. char *tag;
  450. struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
  451. if (!mdata) {
  452. return NULL;
  453. }
  454. /* typecast to suppress const warning */
  455. mdata->msg = ast_msg_ref((struct ast_msg*)msg);
  456. /* starts with 'pjsip:' the 'pj' needs to be removed and maybe even
  457. the entire string. */
  458. if (!(to = strchr(to, ':'))) {
  459. ao2_ref(mdata, -1);
  460. return NULL;
  461. }
  462. /* Make sure we start with sip: */
  463. mdata->to = ast_begins_with(to, "sip:") ? ast_strdup(++to) : ast_strdup(to - 3);
  464. mdata->from = ast_strdup(from);
  465. /* sometimes from can still contain the tag at this point, so remove it */
  466. if ((tag = strchr(mdata->from, ';'))) {
  467. *tag = '\0';
  468. }
  469. return mdata;
  470. }
  471. static int msg_send(void *data)
  472. {
  473. RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);
  474. const struct ast_sip_body body = {
  475. .type = "text",
  476. .subtype = "plain",
  477. .body_text = ast_msg_get_body(mdata->msg)
  478. };
  479. pjsip_tx_data *tdata;
  480. RAII_VAR(char *, uri, NULL, ast_free);
  481. RAII_VAR(struct ast_sip_endpoint *, endpoint, get_outbound_endpoint(
  482. mdata->to, &uri), ao2_cleanup);
  483. if (!endpoint) {
  484. ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not find endpoint '%s' and "
  485. "no default outbound endpoint configured\n", mdata->to);
  486. return -1;
  487. }
  488. if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
  489. ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not create request\n");
  490. return -1;
  491. }
  492. update_to(tdata, mdata->to);
  493. update_from(tdata, mdata->from);
  494. if (ast_sip_add_body(tdata, &body)) {
  495. pjsip_tx_data_dec_ref(tdata);
  496. ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not add body to request\n");
  497. return -1;
  498. }
  499. vars_to_headers(mdata->msg, tdata);
  500. ast_debug(1, "Sending message to '%s' (via endpoint %s) from '%s'\n",
  501. mdata->to, ast_sorcery_object_get_id(endpoint), mdata->from);
  502. if (ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL)) {
  503. ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request\n");
  504. return -1;
  505. }
  506. return PJ_SUCCESS;
  507. }
  508. static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *from)
  509. {
  510. struct msg_data *mdata;
  511. if (ast_strlen_zero(to)) {
  512. ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI must be specified\n");
  513. return -1;
  514. }
  515. if (!(mdata = msg_data_create(msg, to, from)) ||
  516. ast_sip_push_task(message_serializer, msg_send, mdata)) {
  517. ao2_ref(mdata, -1);
  518. return -1;
  519. }
  520. return 0;
  521. }
  522. static const struct ast_msg_tech msg_tech = {
  523. .name = "pjsip",
  524. .msg_send = sip_msg_send,
  525. };
  526. static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
  527. pjsip_dialog *dlg, pjsip_transaction *tsx)
  528. {
  529. pjsip_tx_data *tdata;
  530. pj_status_t status;
  531. status = ast_sip_create_response(rdata, code, NULL, &tdata);
  532. if (status != PJ_SUCCESS) {
  533. ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
  534. return status;
  535. }
  536. if (dlg && tsx) {
  537. status = pjsip_dlg_send_response(dlg, tsx, tdata);
  538. } else {
  539. struct ast_sip_endpoint *endpoint;
  540. endpoint = ast_pjsip_rdata_get_endpoint(rdata);
  541. status = ast_sip_send_stateful_response(rdata, tdata, endpoint);
  542. ao2_cleanup(endpoint);
  543. }
  544. if (status != PJ_SUCCESS) {
  545. ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
  546. }
  547. return status;
  548. }
  549. static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
  550. {
  551. enum pjsip_status_code code;
  552. struct ast_msg *msg;
  553. /* if not a MESSAGE, don't handle */
  554. if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
  555. return PJ_FALSE;
  556. }
  557. code = check_content_type(rdata);
  558. if (code != PJSIP_SC_OK) {
  559. send_response(rdata, code, NULL, NULL);
  560. return PJ_TRUE;
  561. }
  562. msg = ast_msg_alloc();
  563. if (!msg) {
  564. send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
  565. return PJ_TRUE;
  566. }
  567. code = rx_data_to_ast_msg(rdata, msg);
  568. if (code != PJSIP_SC_OK) {
  569. send_response(rdata, code, NULL, NULL);
  570. ast_msg_destroy(msg);
  571. return PJ_TRUE;
  572. }
  573. if (!ast_msg_has_destination(msg)) {
  574. ast_debug(1, "MESSAGE request received, but no handler wanted it\n");
  575. send_response(rdata, PJSIP_SC_NOT_FOUND, NULL, NULL);
  576. ast_msg_destroy(msg);
  577. return PJ_TRUE;
  578. }
  579. /* Send it to the messaging core.
  580. *
  581. * If we are unable to send a response, the most likely reason is that we
  582. * are handling a retransmission of an incoming MESSAGE and were unable to
  583. * create a transaction due to a duplicate key. If we are unable to send
  584. * a response, we should not queue the message to the dialplan
  585. */
  586. if (!send_response(rdata, PJSIP_SC_ACCEPTED, NULL, NULL)) {
  587. ast_msg_queue(msg);
  588. }
  589. return PJ_TRUE;
  590. }
  591. static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  592. {
  593. char buf[MAX_BODY_SIZE];
  594. enum pjsip_status_code code;
  595. struct ast_frame f;
  596. pjsip_dialog *dlg = session->inv_session->dlg;
  597. pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
  598. if (!session->channel) {
  599. send_response(rdata, PJSIP_SC_NOT_FOUND, dlg, tsx);
  600. return 0;
  601. }
  602. if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
  603. send_response(rdata, code, dlg, tsx);
  604. return 0;
  605. }
  606. if (print_body(rdata, buf, sizeof(buf)-1) < 1) {
  607. /* invalid body size */
  608. send_response(rdata, PJSIP_SC_REQUEST_ENTITY_TOO_LARGE, dlg, tsx);
  609. return 0;
  610. }
  611. ast_debug(3, "Received in dialog SIP message\n");
  612. memset(&f, 0, sizeof(f));
  613. f.frametype = AST_FRAME_TEXT;
  614. f.subclass.integer = 0;
  615. f.offset = 0;
  616. f.data.ptr = buf;
  617. f.datalen = strlen(buf) + 1;
  618. ast_queue_frame(session->channel, &f);
  619. send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
  620. return 0;
  621. }
  622. static struct ast_sip_session_supplement messaging_supplement = {
  623. .method = "MESSAGE",
  624. .incoming_request = incoming_in_dialog_request
  625. };
  626. static pjsip_module messaging_module = {
  627. .name = {"Messaging Module", 16},
  628. .id = -1,
  629. .priority = PJSIP_MOD_PRIORITY_APPLICATION,
  630. .on_rx_request = module_on_rx_request,
  631. };
  632. static int load_module(void)
  633. {
  634. CHECK_PJSIP_SESSION_MODULE_LOADED();
  635. if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
  636. return AST_MODULE_LOAD_DECLINE;
  637. }
  638. if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(),
  639. NULL, PJSIP_H_ALLOW, NULL, 1,
  640. &pjsip_message_method.name) != PJ_SUCCESS) {
  641. ast_sip_unregister_service(&messaging_module);
  642. return AST_MODULE_LOAD_DECLINE;
  643. }
  644. if (ast_msg_tech_register(&msg_tech)) {
  645. ast_sip_unregister_service(&messaging_module);
  646. return AST_MODULE_LOAD_DECLINE;
  647. }
  648. message_serializer = ast_sip_create_serializer();
  649. if (!message_serializer) {
  650. ast_sip_unregister_service(&messaging_module);
  651. ast_msg_tech_unregister(&msg_tech);
  652. return AST_MODULE_LOAD_DECLINE;
  653. }
  654. ast_sip_session_register_supplement(&messaging_supplement);
  655. return AST_MODULE_LOAD_SUCCESS;
  656. }
  657. static int unload_module(void)
  658. {
  659. ast_sip_session_unregister_supplement(&messaging_supplement);
  660. ast_msg_tech_unregister(&msg_tech);
  661. ast_sip_unregister_service(&messaging_module);
  662. ast_taskprocessor_unreference(message_serializer);
  663. return 0;
  664. }
  665. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support",
  666. .support_level = AST_MODULE_SUPPORT_CORE,
  667. .load = load_module,
  668. .unload = unload_module,
  669. .load_pri = AST_MODPRI_APP_DEPEND,
  670. );