res_pjsip_messaging.c 20 KB

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