Debian Codegraph @

res/res_pjsip_messaging.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Kevin Harwell <[email protected]>
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
19 /*** MODULEINFO
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_session</depend>
23 <support_level>core</support_level>
24 ***/
25
26 /*** DOCUMENTATION
27 <info name="MessageDestinationInfo" language="en_US" tech="PJSIP">
28 <para>The <literal>destination</literal> parameter is used to construct
29 the Request URI for an outgoing message. It can be in one of the following
30 formats, all prefixed with the <literal>pjsip:</literal> message tech.</para>
31 <para>
32 </para>
33 <enumlist>
34 <enum name="endpoint">
35 <para>Request URI comes from the endpoint's default aor and contact.</para>
36 </enum>
37 <enum name="endpoint/aor">
38 <para>Request URI comes from the specific aor/contact.</para>
39 </enum>
40 <enum name="endpoint@domain">
41 <para>Request URI from the endpoint's default aor and contact. The domain is discarded.</para>
42 </enum>
43 </enumlist>
44 <para>
45 </para>
46 <para>These all use the endpoint to send the message with the specified URI:</para>
47 <para>
48 </para>
49 <enumlist>
50 <enum name="endpoint/&lt;sip[s]:host&gt;>"/>
51 <enum name="endpoint/&lt;sip[s]:user@host&gt;"/>
52 <enum name="endpoint/&quot;display name&quot; &lt;sip[s]:host&gt;"/>
53 <enum name="endpoint/&quot;display name&quot; &lt;sip[s]:user@host&gt;"/>
54 <enum name="endpoint/sip[s]:host"/>
55 <enum name="endpoint/sip[s]:user@host"/>
56 <enum name="endpoint/host"/>
57 <enum name="endpoint/user@host"/>
58 </enumlist>
59 <para>
60 </para>
61 <para>These all use the default endpoint to send the message with the specified URI:</para>
62 <para>
63 </para>
64 <enumlist>
65 <enum name="&lt;sip[s]:host&gt;"/>
66 <enum name="&lt;sip[s]:user@host&gt;"/>
67 <enum name="&quot;display name&quot; &lt;sip[s]:host&gt;"/>
68 <enum name="&quot;display name&quot; &lt;sip[s]:user@host&gt;"/>
69 <enum name="sip[s]:host"/>
70 <enum name="sip[s]:user@host"/>
71 </enumlist>
72 <para>
73 </para>
74 <para>These use the default endpoint to send the message with the specified host:</para>
75 <para>
76 </para>
77 <enumlist>
78 <enum name="host"/>
79 <enum name="user@host"/>
80 </enumlist>
81 <para>
82 </para>
83 <para>This form is similar to a dialstring:</para>
84 <para>
85 </para>
86 <enumlist>
87 <enum name="PJSIP/user@endpoint"/>
88 </enumlist>
89 <para>
90 </para>
91 <para>You still need to prefix the destination with
92 the <literal>pjsip:</literal> message technology prefix. For example:
93 <literal>pjsip:PJSIP/8005551212@myprovider</literal>.
94 The endpoint contact's URI will have the <literal>user</literal> inserted
95 into it and will become the Request URI. If the contact URI already has
96 a user specified, it will be replaced.
97 </para>
98 <para>
99 </para>
100 </info>
101 <info name="MessageFromInfo" language="en_US" tech="PJSIP">
102 <para>The <literal>from</literal> parameter is used to specity the <literal>From:</literal>
103 header in the outgoing SIP MESSAGE. It will override the value specified in
104 MESSAGE(from) which itself will override any <literal>from</literal> value from
105 an incoming SIP MESSAGE.
106 </para>
107 <para>
108 </para>
109 </info>
110 <info name="MessageToInfo" language="en_US" tech="PJSIP">
111 <para>The <literal>to</literal> parameter is used to specity the <literal>To:</literal>
112 header in the outgoing SIP MESSAGE. It will override the value specified in
113 MESSAGE(to) which itself will override any <literal>to</literal> value from
114 an incoming SIP MESSAGE.
115 </para>
116 <para>
117 </para>
118 </info>
119 ***/
120 #include "asterisk.h"
121
122 #include <pjsip.h>
123 #include <pjsip_ua.h>
124
125 #include "asterisk/message.h"
126 #include "asterisk/module.h"
127 #include "asterisk/pbx.h"
128 #include "asterisk/res_pjsip.h"
129 #include "asterisk/res_pjsip_redirect.h"
130 #include "asterisk/res_pjsip_session.h"
131 #include "asterisk/taskprocessor.h"
132 #include "asterisk/test.h"
133 #include "asterisk/uri.h"
134
135 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
136
137 #define MAX_HDR_SIZE 512
138 #define MAX_BODY_SIZE 1024
139 #define MAX_USER_SIZE 128
140
141 static struct ast_taskprocessor *message_serializer;
142
143 /*!
144 * \internal
145 * \brief Checks to make sure the request has the correct content type.
146 *
147 * \details This module supports the following media types: "text/plain".
148 * Return unsupported otherwise.
149 *
150 * \param rdata The SIP request
151 */
152 static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
153 {
154 int res;
155 if (rdata->msg_info.msg->body && rdata->msg_info.msg->body->len) {
156 res = ast_sip_is_content_type(
157 &rdata->msg_info.msg->body->content_type, "text", "plain");
158 } else {
159 res = rdata->msg_info.ctype &&
160 ast_sip_is_content_type(
161 &rdata->msg_info.ctype->media, "text", "plain");
162 }
163
164 return res ? PJSIP_SC_OK : PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
165 }
166
167 /*!
168 * \internal
169 * \brief Checks to make sure the request has the correct content type.
170 *
171 * \details This module supports the following media types: "text/\*", "application/\*".
172 * Return unsupported otherwise.
173 *
174 * \param rdata The SIP request
175 */
176 static enum pjsip_status_code check_content_type_in_dialog(const pjsip_rx_data *rdata)
177 {
178 int res = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
179 static const pj_str_t text = { "text", 4};
180 static const pj_str_t application = { "application", 11};
181
182 if (!(rdata->msg_info.msg->body && rdata->msg_info.msg->body->len > 0)) {
183 return res;
184 }
185
186 /* We'll accept any text/ or application/ content type */
187 if (pj_stricmp(&rdata->msg_info.msg->body->content_type.type, &text) == 0
188 || pj_stricmp(&rdata->msg_info.msg->body->content_type.type, &application) == 0) {
189 res = PJSIP_SC_OK;
190 } else if (rdata->msg_info.ctype
191 && (pj_stricmp(&rdata->msg_info.ctype->media.type, &text) == 0
192 || pj_stricmp(&rdata->msg_info.ctype->media.type, &application) == 0)) {
193 res = PJSIP_SC_OK;
194 }
195
196 return res;
197 }
198
199 /*!
200 * \internal
201 * \brief Update the display name in the To uri in the tdata with the one from the supplied uri
202 *
203 * \param tdata the outbound message data structure
204 * \param to uri containing the display name to replace in the the To uri
205 *
206 * \return 0: success, -1: failure
207 */
208 static int update_to_display_name(pjsip_tx_data *tdata, char *to)
209 {
210 pjsip_name_addr *parsed_name_addr;
211
212 parsed_name_addr = (pjsip_name_addr *) pjsip_parse_uri(tdata->pool, to, strlen(to),
213 PJSIP_PARSE_URI_AS_NAMEADDR);
214
215 if (parsed_name_addr) {
216 if (pj_strlen(&parsed_name_addr->display)) {
217 pjsip_name_addr *name_addr =
218 (pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri;
219
220 pj_strdup(tdata->pool, &name_addr->display, &parsed_name_addr->display);
221
222 }
223 return 0;
224 }
225
226 return -1;
227 }
228
229 /*!
230 * \internal
231 * \brief Checks if the given msg var name should be blocked.
232 *
233 * \details Some headers are not allowed to be overriden by the user.
234 * Determine if the given var header name from the user is blocked for
235 * an outgoing MESSAGE.
236 *
237 * \param name name of header to see if it is blocked.
238 *
239 * \retval TRUE if the given header is blocked.
240 */
241 static int is_msg_var_blocked(const char *name)
242 {
243 int i;
244
245 /* Don't block the Max-Forwards header because the user can override it */
246 static const char *hdr[] = {
247 "To",
248 "From",
249 "Via",
250 "Route",
251 "Contact",
252 "Call-ID",
253 "CSeq",
254 "Allow",
255 "Content-Length",
256 "Content-Type",
257 "Request-URI",
258 };
259
260 for (i = 0; i < ARRAY_LEN(hdr); ++i) {
261 if (!strcasecmp(name, hdr[i])) {
262 /* Block addition of this header. */
263 return 1;
264 }
265 }
266 return 0;
267 }
268
269 /*!
270 * \internal
271 * \brief Copies any other msg vars over to the request headers.
272 *
273 * \param msg The msg structure to copy headers from
274 * \param tdata The SIP transmission data
275 */
276 static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata)
277 {
278 const char *name;
279 const char *value;
280 int max_forwards;
281 struct ast_msg_var_iterator *iter;
282
283 for (iter = ast_msg_var_iterator_init(msg);
284 ast_msg_var_iterator_next(msg, iter, &name, &value);
285 ast_msg_var_unref_current(iter)) {
286 if (!strcasecmp(name, "Max-Forwards")) {
287 /* Decrement Max-Forwards for SIP loop prevention. */
288 if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) {
289 ast_msg_var_iterator_destroy(iter);
290 ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero. MESSAGE not sent.\n");
291 return -1;
292 }
293 sprintf((char *) value, "%d", max_forwards);
294 ast_sip_add_header(tdata, name, value);
295 } else if (!is_msg_var_blocked(name)) {
296 ast_sip_add_header(tdata, name, value);
297 }
298 }
299 ast_msg_var_iterator_destroy(iter);
300
301 return PJSIP_SC_OK;
302 }
303
304 /*!
305 * \internal
306 * \brief Copies any other request header data over to ast_msg structure.
307 *
308 * \param rdata The SIP request
309 * \param msg The msg structure to copy headers into
310 */
311 static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg)
312 {
313 char *c;
314 char name[MAX_HDR_SIZE];
315 char buf[MAX_HDR_SIZE];
316 int res = 0;
317 pjsip_hdr *h = rdata->msg_info.msg->hdr.next;
318 pjsip_hdr *end= &rdata->msg_info.msg->hdr;
319
320 while (h != end) {
321 if ((res = pjsip_hdr_print_on(h, buf, sizeof(buf)-1)) > 0) {
322 buf[res] = '\0';
323 if ((c = strchr(buf, ':'))) {
324 ast_copy_string(buf, ast_skip_blanks(c + 1), sizeof(buf));
325 }
326
327 ast_copy_pj_str(name, &h->name, sizeof(name));
328 if ((res = ast_msg_set_var(msg, name, buf)) != 0) {
329 break;
330 }
331 }
332 h = h->next;
333 }
334 return 0;
335 }
336
337 /*!
338 * \internal
339 * \brief Prints the message body into the given char buffer.
340 *
341 * \details Copies body content from the received data into the given
342 * character buffer removing any extra carriage return/line feeds.
343 *
344 * \param rdata The SIP request
345 * \param buf Buffer to fill
346 * \param len The length of the buffer
347 */
348 static int print_body(pjsip_rx_data *rdata, char *buf, int len)
349 {
350 int res;
351
352 if (!rdata->msg_info.msg->body || !rdata->msg_info.msg->body->len) {
353 return 0;
354 }
355
356 if ((res = rdata->msg_info.msg->body->print_body(
357 rdata->msg_info.msg->body, buf, len)) < 0) {
358 return res;
359 }
360
361 /* remove any trailing carriage return/line feeds */
362 while (res > 0 && ((buf[--res] == '\r') || (buf[res] == '\n')));
363
364 buf[++res] = '\0';
365
366 return res;
367 }
368
369 /*!
370 * \internal
371 * \brief Converts a 'sip:' uri to a 'pjsip:' so it can be found by
372 * the message tech.
373 *
374 * \param buf uri to insert 'pjsip' into
375 * \param size length of the uri in buf
376 * \param capacity total size of buf
377 */
378 static char *sip_to_pjsip(char *buf, int size, int capacity)
379 {
380 int count;
381 const char *scheme;
382 char *res = buf;
383
384 /* remove any wrapping brackets */
385 if (*buf == '<') {
386 ++buf;
387 --size;
388 }
389
390 scheme = strncmp(buf, "sip", 3) ? "pjsip:" : "pj";
391 count = strlen(scheme);
392 if (count + size >= capacity) {
393 ast_log(LOG_WARNING, "Unable to handle MESSAGE- incoming uri "
394 "too large for given buffer\n");
395 return NULL;
396 }
397
398 memmove(res + count, buf, size);
399 memcpy(res, scheme, count);
400
401 buf += size - 1;
402 if (*buf == '>') {
403 *buf = '\0';
404 }
405
406 return res;
407 }
408
409 /*!
410 * \internal
411 * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
412 *
413 * \details Attempts to fill in as much information as possible into the given
414 * msg structure copied from the given request data.
415 *
416 * \param rdata The SIP request
417 * \param msg The asterisk message structure to fill in.
418 */
419 static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
420 {
421 RAII_VAR(struct ast_sip_endpoint *, endpt, NULL, ao2_cleanup);
422 pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
423 pjsip_name_addr *name_addr;
424 pjsip_sip_uri *suri;
425 char *display_name;
426 char buf[MAX_BODY_SIZE];
427 const char *field;
428 const char *context;
429 char exten[AST_MAX_EXTENSION];
430 int res = 0;
431 int size;
432
433 if (!ast_sip_is_allowed_uri(ruri)) {
434 return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
435 }
436
437 ast_copy_pj_str(exten, ast_sip_pjsip_uri_get_username(ruri), AST_MAX_EXTENSION);
438
439 /*
440 * We may want to match in the dialplan without any user
441 * options getting in the way.
442 */
443 AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
444
445 endpt = ast_pjsip_rdata_get_endpoint(rdata);
446 ast_assert(endpt != NULL);
447
448 context = S_OR(endpt->message_context, endpt->context);
449 res |= ast_msg_set_context(msg, "%s", context);
450 res |= ast_msg_set_exten(msg, "%s", exten);
451
452 /* to header */
453 name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
454 size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
455 if (size <= 0) {
456 return PJSIP_SC_INTERNAL_SERVER_ERROR;
457 }
458 buf[size] = '\0';
459 res |= ast_msg_set_to(msg, "%s", sip_to_pjsip(buf, ++size, sizeof(buf) - 1));
460
461 /*
462 * We need to sanitize the from header's display name
463 * by replacing any control characters, including NULLs,
464 * with spaces. Since the display name is a pj_str_t, we
465 * can't modify it in place, so we need to copy it to a
466 * temporary buffer first. The good news is that we can't
467 * accidentally run over the end of the buffer, even if
468 * there's a NULL in the middle, because the display name
469 * is a pj_str_t and we know its length.
470 */
471 name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
472 suri = pjsip_uri_get_uri((pjsip_uri *)name_addr);
473 if (name_addr->display.slen > 0) {
474 int i = 0;
475 char *temp_name = ast_alloca(name_addr->display.slen + 1);
476 for (i = 0; i < name_addr->display.slen; i++) {
477 if (name_addr->display.ptr[i] < 32) {
478 temp_name[i] = ' ';
479 } else {
480 temp_name[i] = name_addr->display.ptr[i];
481 }
482 }
483 temp_name[name_addr->display.slen] = '\0';
484 /*
485 * We need space for each double quote, the display name,
486 * the trailing space and the NULL terminator.
487 */
488 display_name = ast_alloca(name_addr->display.slen + 5);
489 size = sprintf(display_name, "\"%s\" ", temp_name); /* Safe */
490 } else {
491 display_name = "";
492 }
493
494 /*
495 * In the end, the string should look like...
496 * "display name" <scheme:user@host>
497 * If there's no display name, it and its double quotes
498 * will be suppressed.
499 * Note that the port is not included.
500 */
501 res |= ast_msg_set_from(msg, "%s<" PJSTR_PRINTF_SPEC ":" PJSTR_PRINTF_SPEC "@" PJSTR_PRINTF_SPEC ">",
502 display_name,
503 PJSTR_PRINTF_VAR(*pjsip_uri_get_scheme(suri)),
504 PJSTR_PRINTF_VAR(*ast_sip_pjsip_uri_get_username((pjsip_uri *)name_addr)),
505 PJSTR_PRINTF_VAR(*ast_sip_pjsip_uri_get_hostname((pjsip_uri *)name_addr)));
506
507 field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf) - 1, 3);
508 res |= ast_msg_set_var(msg, "PJSIP_RECVADDR", field);
509
510 switch (rdata->tp_info.transport->key.type) {
511 case PJSIP_TRANSPORT_UDP:
512 case PJSIP_TRANSPORT_UDP6:
513 field = "udp";
514 break;
515 case PJSIP_TRANSPORT_TCP:
516 case PJSIP_TRANSPORT_TCP6:
517 field = "tcp";
518 break;
519 case PJSIP_TRANSPORT_TLS:
520 case PJSIP_TRANSPORT_TLS6:
521 field = "tls";
522 break;
523 default:
524 field = rdata->tp_info.transport->type_name;
525 }
526 ast_msg_set_var(msg, "PJSIP_TRANSPORT", field);
527
528 if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
529 res |= ast_msg_set_body(msg, "%s", buf);
530 }
531
532 /* endpoint name */
533 res |= ast_msg_set_tech(msg, "%s", "PJSIP");
534 res |= ast_msg_set_endpoint(msg, "%s", ast_sorcery_object_get_id(endpt));
535 if (endpt->id.self.name.valid) {
536 res |= ast_msg_set_var(msg, "PJSIP_ENDPOINT", endpt->id.self.name.str);
537 }
538
539 res |= headers_to_vars(rdata, msg);
540
541 return !res ? PJSIP_SC_OK : PJSIP_SC_INTERNAL_SERVER_ERROR;
542 }
543
544 struct msg_data {
545 struct ast_msg *msg;
546 char *destination;
547 char *from;
548 };
549
550 static void msg_data_destroy(void *obj)
551 {
552 struct msg_data *mdata = obj;
553
554 ast_free(mdata->from);
555 ast_free(mdata->destination);
556
557 ast_msg_destroy(mdata->msg);
558 }
559
560 static struct msg_data *msg_data_create(const struct ast_msg *msg, const char *destination, const char *from)
561 {
562 struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
563
564 if (!mdata) {
565 return NULL;
566 }
567
568 /* typecast to suppress const warning */
569 mdata->msg = ast_msg_ref((struct ast_msg *) msg);
570
571 /* To starts with 'pjsip:' which needs to be removed. */
572 if (!(destination = strchr(destination, ':'))) {
573 ao2_ref(mdata, -1);
574 return NULL;
575 }
576 ++destination;/* Now skip the ':' */
577
578 mdata->destination = ast_strdup(destination);
579 mdata->from = ast_strdup(from);
580
581 return mdata;
582 }
583
584 /*!
585 * \internal
586 * \brief Callback data for MESSAGE response handling
587 */
588 struct message_response_data {
589 char *from;
590 char *to;
591 char *body;
592 char *content_type;
593 struct ast_sip_redirect_state *redirect_state;
594 };
595
596 static void message_response_data_destroy(void *obj)
597 {
598 struct message_response_data *resp_data = obj;
599
600 ast_free(resp_data->from);
601 ast_free(resp_data->to);
602 ast_free(resp_data->body);
603 ast_free(resp_data->content_type);
604
605 if (resp_data->redirect_state) {
606 ast_sip_redirect_state_destroy(resp_data->redirect_state);
607 }
608 }
609
610 static struct message_response_data *message_response_data_create(
611 struct ast_sip_endpoint *endpoint,
612 const char *from,
613 const char *to,
614 const char *body,
615 const char *content_type,
616 const char *initial_uri)
617 {
618 struct message_response_data *resp_data;
619
620 resp_data = ao2_alloc_options(sizeof(*resp_data), message_response_data_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
621 if (!resp_data) {
622 return NULL;
623 }
624
625 resp_data->from = ast_strdup(from);
626 resp_data->to = ast_strdup(to);
627 resp_data->body = ast_strdup(body);
628 resp_data->content_type = ast_strdup(content_type);
629
630 if (!resp_data->from || !resp_data->to || !resp_data->body || !resp_data->content_type) {
631 ast_log(LOG_ERROR, "Failed to allocate memory for response data strings on endpoint '%s'.\n",
632 ast_sorcery_object_get_id(endpoint));
633 ao2_ref(resp_data, -1);
634 return NULL;
635 }
636
637 resp_data->redirect_state = ast_sip_redirect_state_create(endpoint, initial_uri);
638 if (!resp_data->redirect_state) {
639 ast_log(LOG_ERROR, "Failed to create redirect state for endpoint '%s'.\n", ast_sorcery_object_get_id(endpoint));
640 ao2_ref(resp_data, -1);
641 return NULL;
642 }
643
644 return resp_data;
645 }
646
647 static void update_content_type(pjsip_tx_data *tdata, struct ast_msg *msg, struct ast_sip_body *body)
648 {
649 static const pj_str_t CONTENT_TYPE = { "Content-Type", sizeof("Content-Type") - 1 };
650
651 const char *content_type = ast_msg_get_var(msg, pj_strbuf(&CONTENT_TYPE));
652 if (content_type) {
653 pj_str_t type, subtype;
654 pjsip_ctype_hdr *parsed;
655
656 /* Let pjsip do the parsing for us */
657 parsed = pjsip_parse_hdr(tdata->pool, &CONTENT_TYPE,
658 ast_strdupa(content_type), strlen(content_type),
659 NULL);
660
661 if (!parsed) {
662 ast_log(LOG_WARNING, "Failed to parse '%s' as a content type. Using text/plain\n",
663 content_type);
664 return;
665 }
666
667 /* We need to turn type and subtype into zero-terminated strings */
668 pj_strdup_with_null(tdata->pool, &type, &parsed->media.type);
669 pj_strdup_with_null(tdata->pool, &subtype, &parsed->media.subtype);
670
671 body->type = pj_strbuf(&type);
672 body->subtype = pj_strbuf(&subtype);
673 }
674 }
675
676 /* Forward declaration for callback */
677 static void msg_response_callback(void *token, pjsip_event *e);
678
679 /*!
680 * \internal
681 * \brief Send a MESSAGE to a redirect target
682 *
683 * \param resp_data Response data containing redirect state and message info
684 * \param target_uri The URI to send the message to
685 *
686 * \return 0: success, -1: failure
687 */
688 static int send_message_to_uri(struct message_response_data *resp_data, const char *target_uri)
689 {
690 pjsip_tx_data *tdata;
691 struct message_response_data *new_resp_data;
692 struct ast_sip_endpoint *endpoint;
693 struct ast_sip_body body = {
694 .type = "text",
695 .subtype = "plain",
696 .body_text = resp_data->body
697 };
698
699 if (!resp_data->redirect_state) {
700 ast_log(LOG_ERROR, "No redirect state available for sending a redirect message.\n");
701 return -1;
702 }
703
704 endpoint = ast_sip_redirect_get_endpoint(resp_data->redirect_state);
705
706 ast_debug(1, "Sending redirected MESSAGE to '%s' (hop %d) on endpoint '%s'\n",
707 target_uri, ast_sip_redirect_get_hop_count(resp_data->redirect_state), ast_sorcery_object_get_id(endpoint));
708
709 if (ast_sip_create_request("MESSAGE", NULL, endpoint, target_uri, NULL, &tdata)) {
710 ast_log(LOG_WARNING, "Could not create redirect request for endpoint '%s'.\n",
711 ast_sorcery_object_get_id(endpoint));
712 return -1;
713 }
714
715 /* Update To header if we have one */
716 if (!ast_strlen_zero(resp_data->to)) {
717 ast_sip_update_to_uri(tdata, resp_data->to);
718 }
719
720 /* Update From header if we have one */
721 if (!ast_strlen_zero(resp_data->from)) {
722 ast_sip_update_from(tdata, resp_data->from);
723 }
724
725 /* Parse and set content type if provided */
726 if (!ast_strlen_zero(resp_data->content_type)) {
727 char *type_copy = ast_strdupa(resp_data->content_type);
728 char *subtype = strchr(type_copy, '/');
729 if (subtype) {
730 *subtype = '\0';
731 subtype++;
732 body.type = type_copy;
733 body.subtype = subtype;
734 }
735 }
736 ast_sip_add_body(tdata, &body);
737 if (!tdata->msg->body) {
738 pjsip_tx_data_dec_ref(tdata);
739 ast_log(LOG_ERROR, "Could not add body to redirect request on endpoint '%s'.\n", ast_sorcery_object_get_id(endpoint));
740 return -1;
741 }
742
743 /* Create new callback data - the redirect state is passed along */
744 new_resp_data = ao2_alloc(sizeof(*new_resp_data), message_response_data_destroy);
745 if (!new_resp_data) {
746 pjsip_tx_data_dec_ref(tdata);
747 ast_log(LOG_ERROR, "Could not allocate redirect callback data for endpoint '%s'.\n", ast_sorcery_object_get_id(endpoint));
748 return -1;
749 }
750
751 /* Copy message-specific data */
752 new_resp_data->from = ast_strdup(resp_data->from);
753 new_resp_data->to = ast_strdup(resp_data->to);
754 new_resp_data->body = ast_strdup(resp_data->body);
755 new_resp_data->content_type = ast_strdup(resp_data->content_type);
756
757 /* Check for allocation failures */
758 if (!new_resp_data->from || !new_resp_data->to || !new_resp_data->body || !new_resp_data->content_type) {
759 pjsip_tx_data_dec_ref(tdata);
760 ao2_ref(new_resp_data, -1);
761 ast_log(LOG_ERROR, "Failed to allocate memory for redirect callback strings for endpoint '%s'.\n",
762 ast_sorcery_object_get_id(endpoint));
763 return -1;
764 }
765
766 /* Transfer the redirect state to the new response data */
767 new_resp_data->redirect_state = resp_data->redirect_state;
768 resp_data->redirect_state = NULL;
769
770 /* Send with callback for potential further redirects */
771 if (ast_sip_send_request(tdata, NULL, endpoint, new_resp_data, msg_response_callback)) {
772 ao2_ref(new_resp_data, -1);
773 ast_log(LOG_ERROR, "Failed to send redirect request to '%s' on endpoint '%s'.\n",
774 new_resp_data->to, ast_sorcery_object_get_id(endpoint));
775 return -1;
776 }
777
778 return 0;
779 }
780
781 /*!
782 * \internal
783 * \brief Handle a 3xx redirect response to a MESSAGE
784 *
785 * \param resp_data Response callback data
786 * \param rdata The redirect response data
787 */
788 static void handle_message_redirect(struct message_response_data *resp_data, pjsip_rx_data *rdata)
789 {
790 char *uri = NULL;
791 int hop_count;
792
793 if (!resp_data->redirect_state) {
794 ast_log(LOG_ERROR, "MESSAGE redirect: no redirect state available\n");
795 return;
796 }
797
798 /* Parse the redirect response and extract contacts */
799 if (ast_sip_redirect_parse_3xx(rdata, resp_data->redirect_state)) {
800 ast_debug(1, "MESSAGE redirect on endpoint '%s': not following redirect (parse failed or conditions not met)\n",
801 ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state)));
802 return;
803 }
804
805 /* Get the first URI to try */
806 if (ast_sip_redirect_get_next_uri(resp_data->redirect_state, &uri)) {
807 ast_log(LOG_WARNING, "MESSAGE redirect on endpoint '%s': no valid URIs to try\n",
808 ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state)));
809 return;
810 }
811
812 hop_count = ast_sip_redirect_get_hop_count(resp_data->redirect_state);
813 ast_log(LOG_NOTICE, "MESSAGE redirect on endpoint '%s': Following redirect to '%s' (hop %d/%d)\n",
814 ast_sorcery_object_get_id(ast_sip_redirect_get_endpoint(resp_data->redirect_state)), uri, hop_count, AST_SIP_MAX_REDIRECT_HOPS);
815
816 /* Try the first contact */
817 send_message_to_uri(resp_data, uri);
818 ast_free(uri);
819 }
820
821 /*!
822 * \internal
823 * \brief Callback for MESSAGE responses
824 *
825 * \param token Callback data
826 * \param e The pjsip event
827 */
828 static void msg_response_callback(void *token, pjsip_event *e)
829 {
830 struct message_response_data *resp_data = token;
831 struct ast_sip_redirect_state *state = resp_data->redirect_state;
832 struct ast_sip_endpoint *endpoint = ast_sip_redirect_get_endpoint(state);
833 pjsip_rx_data *rdata;
834 int status_code;
835 char *next_uri = NULL;
836
837 /* Check event type */
838 if (e->body.tsx_state.type == PJSIP_EVENT_TIMER) {
839 ast_debug(1, "MESSAGE request on endpoint '%s' timed out\n", ast_sorcery_object_get_id(endpoint));
840 /* Try next pending contact if available */
841 if (state && !ast_sip_redirect_get_next_uri(state, &next_uri)) {
842 ast_log(LOG_NOTICE, "MESSAGE timed out on endpoint '%s', trying next Contact: '%s'\n",
843 ast_sorcery_object_get_id(endpoint), next_uri);
844 send_message_to_uri(resp_data, next_uri);
845 ast_free(next_uri);
846 }
847 ao2_ref(resp_data, -1);
848 return;
849 }
850
851 if (e->body.tsx_state.type != PJSIP_EVENT_RX_MSG) {
852 ast_debug(3, "MESSAGE response event type %d (not RX_MSG) on endpoint '%s'.\n",
853 e->body.tsx_state.type, ast_sorcery_object_get_id(endpoint));
854 ao2_ref(resp_data, -1);
855 return;
856 }
857
858 rdata = e->body.tsx_state.src.rdata;
859 status_code = e->body.tsx_state.tsx->status_code;
860
861 ast_debug(3, "Received MESSAGE response %d on endpoint '%s'.\n", status_code, ast_sorcery_object_get_id(endpoint));
862
863 /* Handle 3xx redirects */
864 if (PJSIP_IS_STATUS_IN_CLASS(status_code, 300)) {
865 handle_message_redirect(resp_data, rdata);
866 }
867 /* If non-2xx response and we have pending contacts, try the next one */
868 else if (status_code >= 400 && state && !ast_sip_redirect_get_next_uri(state, &next_uri)) {
869 ast_log(LOG_NOTICE, "MESSAGE to redirect target failed (%d) on endpoint '%s', trying next Contact: '%s'\n",
870 status_code, ast_sorcery_object_get_id(endpoint), next_uri);
871 send_message_to_uri(resp_data, next_uri);
872 ast_free(next_uri);
873 }
874 /* Success (2xx) - don't try other contacts */
875 else if (PJSIP_IS_STATUS_IN_CLASS(status_code, 200)) {
876 ast_debug(1, "MESSAGE successfully delivered (%d) for endpoint '%s'.\n", status_code, ast_sorcery_object_get_id(endpoint));
877 }
878
879 ao2_ref(resp_data, -1);
880 }
881
882 /*!
883 * \internal
884 * \brief Send a MESSAGE
885 *
886 * \param data The outbound message data structure
887 *
888 * \return 0: success, -1: failure
889 *
890 * mdata contains the To and From specified in the call to the MessageSend
891 * dialplan app. It also contains the ast_msg object that contains the
892 * message body and may contain the To and From from the channel datastore,
893 * usually set with the MESSAGE or MESSAGE_DATA dialplan functions but
894 * could also come from an incoming sip MESSAGE.
895 *
896 * The mdata->to is always used as the basis for the Request URI
897 * while the mdata->msg->to is used for the To header. If
898 * mdata->msg->to isn't available, mdata->to is used for the To header.
899 *
900 */
901 static int msg_send(void *data)
902 {
903 struct msg_data *mdata = data; /* The caller holds a reference */
904 /* Callback data for redirect handling */
905 struct message_response_data *resp_data;
906 const char *from_uri;
907 const char *to_uri;
908
909 struct ast_sip_body body = {
910 .type = "text",
911 .subtype = "plain",
912 .body_text = ast_msg_get_body(mdata->msg)
913 };
914
915 pjsip_hdr *contact;
916 pjsip_tx_data *tdata;
917 RAII_VAR(char *, uri, NULL, ast_free);
918 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
919 RAII_VAR(struct ast_str *, content_type_buf , ast_str_create(128), ast_free);
920
921 ast_debug(3, "mdata From: %s msg From: %s mdata Destination: %s msg To: %s\n",
922 mdata->from, ast_msg_get_from(mdata->msg), mdata->destination, ast_msg_get_to(mdata->msg));
923
924 endpoint = ast_sip_get_endpoint(mdata->destination, 1, &uri);
925 if (!endpoint) {
926 ast_log(LOG_ERROR,
927 "PJSIP MESSAGE - Could not find endpoint '%s' and no default outbound endpoint configured\n",
928 mdata->destination);
929
930 ast_test_suite_event_notify("MSG_ENDPOINT_URI_FAIL",
931 "MdataFrom: %s\r\n"
932 "MsgFrom: %s\r\n"
933 "MdataDestination: %s\r\n"
934 "MsgTo: %s\r\n",
935 mdata->from,
936 ast_msg_get_from(mdata->msg),
937 mdata->destination,
938 ast_msg_get_to(mdata->msg));
939
940 return -1;
941 }
942
943 ast_debug(3, "Request URI: %s\n", uri);
944
945 if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
946 ast_log(LOG_WARNING, "PJSIP MESSAGE - Could not create request\n");
947 return -1;
948 }
949
950 /* If there was a To in the actual message, */
951 if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) {
952 char *msg_to = ast_strdupa(ast_msg_get_to(mdata->msg));
953
954 /*
955 * It's possible that the message To was copied from
956 * an incoming MESSAGE in which case it'll have the
957 * pjsip: tech prepended to it. We need to remove it.
958 */
959 if (ast_begins_with(msg_to, "pjsip:")) {
960 msg_to += 6;
961 }
962 /*
963 * Only attempt to update the To URI if it's actually a SIP/SIPS URI.
964 * When sending via ARI, the To field is also used as destination
965 * (MessageDestinationInfo) and therefore might not contain a SIP URI.
966 * ast_sip_create_request still sets the correct To header.
967 */
968 if (ast_begins_with(msg_to, "sip:") || ast_begins_with(msg_to, "sips:")) {
969 ast_sip_update_to_uri(tdata, msg_to);
970 }
971 } else {
972 /*
973 * If there was no To in the message, it's still possible
974 * that there is a display name in the mdata To. If so,
975 * we'll copy the URI display name to the tdata To.
976 */
977 update_to_display_name(tdata, uri);
978 }
979
980 if (!ast_strlen_zero(mdata->from)) {
981 ast_sip_update_from(tdata, mdata->from);
982 } else if (!ast_strlen_zero(ast_msg_get_from(mdata->msg))) {
983 ast_sip_update_from(tdata, (char *)ast_msg_get_from(mdata->msg));
984 }
985
986 #ifdef TEST_FRAMEWORK
987 {
988 pjsip_name_addr *tdata_name_addr;
989 pjsip_sip_uri *tdata_sip_uri;
990 char touri[128];
991 char fromuri[128];
992
993 tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri;
994 tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri);
995 pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, touri, sizeof(touri));
996 tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
997 tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri);
998 pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, fromuri, sizeof(fromuri));
999
1000 ast_test_suite_event_notify("MSG_FROMTO_URI",
1001 "MdataFrom: %s\r\n"
1002 "MsgFrom: %s\r\n"
1003 "MdataDestination: %s\r\n"
1004 "MsgTo: %s\r\n"
1005 "Endpoint: %s\r\n"
1006 "RequestURI: %s\r\n"
1007 "ToURI: %s\r\n"
1008 "FromURI: %s\r\n",
1009 mdata->from,
1010 ast_msg_get_from(mdata->msg),
1011 mdata->destination,
1012 ast_msg_get_to(mdata->msg),
1013 ast_sorcery_object_get_id(endpoint),
1014 uri,
1015 touri,
1016 fromuri
1017 );
1018 }
1019 #endif
1020
1021 update_content_type(tdata, mdata->msg, &body);
1022
1023 ast_sip_add_body(tdata, &body);
1024 if (!tdata->msg->body) {
1025 pjsip_tx_data_dec_ref(tdata);
1026 ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not add body to request\n");
1027 return -1;
1028 }
1029
1030 /*
1031 * This copies any headers set with MESSAGE_DATA() to the
1032 * tdata.
1033 */
1034 vars_to_headers(mdata->msg, tdata);
1035 /* Remove Contact header fields as per RFC 3428*/
1036 contact = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, NULL);
1037 if (contact) {
1038 pj_list_erase(contact);
1039 }
1040 ast_debug(1, "Sending message to '%s' (via endpoint %s) from '%s'\n",
1041 uri, ast_sorcery_object_get_id(endpoint), mdata->from);
1042
1043 /* Determine From URI */
1044 if (!ast_strlen_zero(mdata->from)) {
1045 from_uri = mdata->from;
1046 } else if (!ast_strlen_zero(ast_msg_get_from(mdata->msg))) {
1047 from_uri = ast_msg_get_from(mdata->msg);
1048 } else {
1049 from_uri = "";
1050 }
1051
1052 /* Determine To URI */
1053 if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) {
1054 to_uri = ast_msg_get_to(mdata->msg);
1055 } else {
1056 to_uri = "";
1057 }
1058
1059 /* Build content type string */
1060 ast_str_set(&content_type_buf, 0, "%s/%s", body.type, body.subtype);
1061
1062 /* Create callback data */
1063 resp_data = message_response_data_create(endpoint, from_uri, to_uri,
1064 body.body_text, ast_str_buffer(content_type_buf), uri);
1065
1066 if (!resp_data) {
1067 pjsip_tx_data_dec_ref(tdata);
1068 ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not allocate callback data for endpoint '%s'\n",
1069 ast_sorcery_object_get_id(endpoint));
1070 return -1;
1071 }
1072
1073 /* Send with callback for redirect handling */
1074 if (ast_sip_send_request(tdata, NULL, endpoint, resp_data, msg_response_callback)) {
1075 ao2_ref(resp_data, -1);
1076 ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request on endpoint '%s'\n", ast_sorcery_object_get_id(endpoint));
1077 return -1;
1078 }
1079
1080 return 0;
1081 }
1082
1083 static int sip_msg_send(const struct ast_msg *msg, const char *destination, const char *from)
1084 {
1085 struct msg_data *mdata;
1086 int res;
1087
1088 if (ast_strlen_zero(destination)) {
1089 ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI must be specified\n");
1090 return -1;
1091 }
1092
1093 mdata = msg_data_create(msg, destination, from);
1094 if (!mdata) {
1095 return -1;
1096 }
1097
1098 res = ast_sip_push_task_wait_serializer(message_serializer, msg_send, mdata);
1099 ao2_ref(mdata, -1);
1100
1101 return res;
1102 }
1103
1104 static const struct ast_msg_tech msg_tech = {
1105 .name = "pjsip",
1106 .msg_send = sip_msg_send,
1107 };
1108
1109 static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
1110 pjsip_dialog *dlg, pjsip_transaction *tsx)
1111 {
1112 pjsip_tx_data *tdata;
1113 pj_status_t status;
1114
1115 status = ast_sip_create_response(rdata, code, NULL, &tdata);
1116 if (status != PJ_SUCCESS) {
1117 ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
1118 return status;
1119 }
1120
1121 if (dlg && tsx) {
1122 status = pjsip_dlg_send_response(dlg, tsx, tdata);
1123 } else {
1124 struct ast_sip_endpoint *endpoint;
1125
1126 endpoint = ast_pjsip_rdata_get_endpoint(rdata);
1127 status = ast_sip_send_stateful_response(rdata, tdata, endpoint);
1128 ao2_cleanup(endpoint);
1129 }
1130
1131 if (status != PJ_SUCCESS) {
1132 ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
1133 }
1134
1135 return status;
1136 }
1137
1138 static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
1139 {
1140 enum pjsip_status_code code;
1141 struct ast_msg *msg;
1142
1143 /* if not a MESSAGE, don't handle */
1144 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
1145 return PJ_FALSE;
1146 }
1147
1148 code = check_content_type(rdata);
1149 if (code != PJSIP_SC_OK) {
1150 send_response(rdata, code, NULL, NULL);
1151 return PJ_TRUE;
1152 }
1153
1154 msg = ast_msg_alloc();
1155 if (!msg) {
1156 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
1157 return PJ_TRUE;
1158 }
1159
1160 code = rx_data_to_ast_msg(rdata, msg);
1161 if (code != PJSIP_SC_OK) {
1162 send_response(rdata, code, NULL, NULL);
1163 ast_msg_destroy(msg);
1164 return PJ_TRUE;
1165 }
1166
1167 if (!ast_msg_has_destination(msg)) {
1168 ast_debug(1, "MESSAGE request received, but no handler wanted it\n");
1169 send_response(rdata, PJSIP_SC_NOT_FOUND, NULL, NULL);
1170 ast_msg_destroy(msg);
1171 return PJ_TRUE;
1172 }
1173
1174 /* Send it to the messaging core.
1175 *
1176 * If we are unable to send a response, the most likely reason is that we
1177 * are handling a retransmission of an incoming MESSAGE and were unable to
1178 * create a transaction due to a duplicate key. If we are unable to send
1179 * a response, we should not queue the message to the dialplan
1180 */
1181 if (!send_response(rdata, PJSIP_SC_ACCEPTED, NULL, NULL)) {
1182 ast_msg_queue(msg);
1183 }
1184
1185 return PJ_TRUE;
1186 }
1187
1188 static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
1189 {
1190 enum pjsip_status_code code;
1191 int rc;
1192 pjsip_dialog *dlg = session->inv_session->dlg;
1193 pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
1194 struct ast_msg_data *msg;
1195 struct ast_party_caller *caller;
1196 pjsip_name_addr *name_addr;
1197 size_t from_len;
1198 size_t to_len;
1199 struct ast_msg_data_attribute attrs[4];
1200 int pos = 0;
1201 int body_pos;
1202
1203 if (!session->channel) {
1204 send_response(rdata, PJSIP_SC_NOT_FOUND, dlg, tsx);
1205 return 0;
1206 }
1207
1208 code = check_content_type_in_dialog(rdata);
1209 if (code != PJSIP_SC_OK) {
1210 send_response(rdata, code, dlg, tsx);
1211 return 0;
1212 }
1213
1214 caller = ast_channel_caller(session->channel);
1215
1216 name_addr = (pjsip_name_addr *) rdata->msg_info.from->uri;
1217 from_len = pj_strlen(&name_addr->display);
1218 if (from_len) {
1219 attrs[pos].type = AST_MSG_DATA_ATTR_FROM;
1220 from_len++;
1221 attrs[pos].value = ast_alloca(from_len);
1222 ast_copy_pj_str(attrs[pos].value, &name_addr->display, from_len);
1223 pos++;
1224 } else if (caller->id.name.valid && !ast_strlen_zero(caller->id.name.str)) {
1225 attrs[pos].type = AST_MSG_DATA_ATTR_FROM;
1226 attrs[pos].value = caller->id.name.str;
1227 pos++;
1228 }
1229
1230 name_addr = (pjsip_name_addr *) rdata->msg_info.to->uri;
1231 to_len = pj_strlen(&name_addr->display);
1232 if (to_len) {
1233 attrs[pos].type = AST_MSG_DATA_ATTR_TO;
1234 to_len++;
1235 attrs[pos].value = ast_alloca(to_len);
1236 ast_copy_pj_str(attrs[pos].value, &name_addr->display, to_len);
1237 pos++;
1238 }
1239
1240 attrs[pos].type = AST_MSG_DATA_ATTR_CONTENT_TYPE;
1241 attrs[pos].value = ast_alloca(rdata->msg_info.msg->body->content_type.type.slen
1242 + rdata->msg_info.msg->body->content_type.subtype.slen + 2);
1243 sprintf(attrs[pos].value, "%.*s/%.*s",
1244 (int)rdata->msg_info.msg->body->content_type.type.slen,
1245 rdata->msg_info.msg->body->content_type.type.ptr,
1246 (int)rdata->msg_info.msg->body->content_type.subtype.slen,
1247 rdata->msg_info.msg->body->content_type.subtype.ptr);
1248 pos++;
1249
1250 body_pos = pos;
1251 attrs[pos].type = AST_MSG_DATA_ATTR_BODY;
1252 attrs[pos].value = ast_malloc(rdata->msg_info.msg->body->len + 1);
1253 if (!attrs[pos].value) {
1254 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
1255 return 0;
1256 }
1257 ast_copy_string(attrs[pos].value, rdata->msg_info.msg->body->data, rdata->msg_info.msg->body->len + 1);
1258 pos++;
1259
1260 msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_IN_DIALOG, attrs, pos);
1261 if (!msg) {
1262 ast_free(attrs[body_pos].value);
1263 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
1264 return 0;
1265 }
1266
1267 ast_debug(1, "Received in-dialog MESSAGE from '%s:%s': %s %s\n",
1268 ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
1269 ast_channel_name(session->channel),
1270 ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO),
1271 ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY));
1272
1273 rc = ast_msg_data_queue_frame(session->channel, msg);
1274 ast_free(attrs[body_pos].value);
1275 ast_free(msg);
1276 if (rc != 0) {
1277 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
1278 } else {
1279 send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
1280 }
1281
1282 return 0;
1283 }
1284
1285 static struct ast_sip_session_supplement messaging_supplement = {
1286 .method = "MESSAGE",
1287 .incoming_request = incoming_in_dialog_request
1288 };
1289
1290 static pjsip_module messaging_module = {
1291 .name = {"Messaging Module", 16},
1292 .id = -1,
1293 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
1294 .on_rx_request = module_on_rx_request,
1295 };
1296
1297 static int load_module(void)
1298 {
1299 if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
1300 return AST_MODULE_LOAD_DECLINE;
1301 }
1302
1303 if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(),
1304 NULL, PJSIP_H_ALLOW, NULL, 1,
1305 &pjsip_message_method.name) != PJ_SUCCESS) {
1306
1307 ast_sip_unregister_service(&messaging_module);
1308 return AST_MODULE_LOAD_DECLINE;
1309 }
1310
1311 if (ast_msg_tech_register(&msg_tech)) {
1312 ast_sip_unregister_service(&messaging_module);
1313 return AST_MODULE_LOAD_DECLINE;
1314 }
1315
1316 message_serializer = ast_sip_create_serializer("pjsip/messaging");
1317 if (!message_serializer) {
1318 ast_sip_unregister_service(&messaging_module);
1319 ast_msg_tech_unregister(&msg_tech);
1320 return AST_MODULE_LOAD_DECLINE;
1321 }
1322
1323 ast_sip_session_register_supplement(&messaging_supplement);
1324 return AST_MODULE_LOAD_SUCCESS;
1325 }
1326
1327 static int unload_module(void)
1328 {
1329 ast_sip_session_unregister_supplement(&messaging_supplement);
1330 ast_msg_tech_unregister(&msg_tech);
1331 ast_sip_unregister_service(&messaging_module);
1332 ast_taskprocessor_unreference(message_serializer);
1333 return 0;
1334 }
1335
1336 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support",
1337 .support_level = AST_MODULE_SUPPORT_CORE,
1338 .load = load_module,
1339 .unload = unload_module,
1340 .load_pri = AST_MODPRI_APP_DEPEND,
1341 .requires = "res_pjsip,res_pjsip_session",
1342 );