Asterisk - The Open Source Telephony Project GIT-master-4f2b068
Loading...
Searching...
No Matches
res_pjsip_messaging.c
Go to the documentation of this file.
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
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"
132#include "asterisk/test.h"
133#include "asterisk/uri.h"
134
135const 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
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 */
152static 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) {
157 &rdata->msg_info.msg->body->content_type, "text", "plain");
158 } else {
159 res = rdata->msg_info.ctype &&
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 */
176static 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 */
208static 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 */
241static 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 */
276static 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;
282
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) {
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);
295 } else if (!is_msg_var_blocked(name)) {
297 }
298 }
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 */
311static 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 */
348static 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 */
378static 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 */
419static 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
438
439 /*
440 * We may want to match in the dialplan without any user
441 * options getting in the way.
442 */
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 */
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
544struct msg_data {
545 struct ast_msg *msg;
547 char *from;
548};
549
550static 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
560static 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
579 mdata->from = ast_strdup(from);
580
581 return mdata;
582}
583
584/*!
585 * \internal
586 * \brief Callback data for MESSAGE response handling
587 */
595
596static 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) {
607 }
608}
609
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
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);
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
647static 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,
659 NULL);
660
661 if (!parsed) {
662 ast_log(LOG_WARNING, "Failed to parse '%s' as a content type. Using text/plain\n",
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 */
677static 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 */
688static 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",
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 */
788static 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",
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",
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",
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 */
828static 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;
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 */
901static 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_tx_data *tdata;
916 RAII_VAR(char *, uri, NULL, ast_free);
917 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
918 RAII_VAR(struct ast_str *, content_type_buf , ast_str_create(128), ast_free);
919
920 ast_debug(3, "mdata From: %s msg From: %s mdata Destination: %s msg To: %s\n",
921 mdata->from, ast_msg_get_from(mdata->msg), mdata->destination, ast_msg_get_to(mdata->msg));
922
923 endpoint = ast_sip_get_endpoint(mdata->destination, 1, &uri);
924 if (!endpoint) {
926 "PJSIP MESSAGE - Could not find endpoint '%s' and no default outbound endpoint configured\n",
927 mdata->destination);
928
929 ast_test_suite_event_notify("MSG_ENDPOINT_URI_FAIL",
930 "MdataFrom: %s\r\n"
931 "MsgFrom: %s\r\n"
932 "MdataDestination: %s\r\n"
933 "MsgTo: %s\r\n",
934 mdata->from,
935 ast_msg_get_from(mdata->msg),
936 mdata->destination,
937 ast_msg_get_to(mdata->msg));
938
939 return -1;
940 }
941
942 ast_debug(3, "Request URI: %s\n", uri);
943
944 if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
945 ast_log(LOG_WARNING, "PJSIP MESSAGE - Could not create request\n");
946 return -1;
947 }
948
949 /* If there was a To in the actual message, */
950 if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) {
951 char *msg_to = ast_strdupa(ast_msg_get_to(mdata->msg));
952
953 /*
954 * It's possible that the message To was copied from
955 * an incoming MESSAGE in which case it'll have the
956 * pjsip: tech prepended to it. We need to remove it.
957 */
958 if (ast_begins_with(msg_to, "pjsip:")) {
959 msg_to += 6;
960 }
961 ast_sip_update_to_uri(tdata, msg_to);
962 } else {
963 /*
964 * If there was no To in the message, it's still possible
965 * that there is a display name in the mdata To. If so,
966 * we'll copy the URI display name to the tdata To.
967 */
968 update_to_display_name(tdata, uri);
969 }
970
971 if (!ast_strlen_zero(mdata->from)) {
972 ast_sip_update_from(tdata, mdata->from);
973 } else if (!ast_strlen_zero(ast_msg_get_from(mdata->msg))) {
974 ast_sip_update_from(tdata, (char *)ast_msg_get_from(mdata->msg));
975 }
976
977#ifdef TEST_FRAMEWORK
978 {
979 pjsip_name_addr *tdata_name_addr;
980 pjsip_sip_uri *tdata_sip_uri;
981 char touri[128];
982 char fromuri[128];
983
984 tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri;
985 tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri);
986 pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, touri, sizeof(touri));
987 tdata_name_addr = (pjsip_name_addr *) PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
988 tdata_sip_uri = pjsip_uri_get_uri(tdata_name_addr->uri);
989 pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, tdata_sip_uri, fromuri, sizeof(fromuri));
990
991 ast_test_suite_event_notify("MSG_FROMTO_URI",
992 "MdataFrom: %s\r\n"
993 "MsgFrom: %s\r\n"
994 "MdataDestination: %s\r\n"
995 "MsgTo: %s\r\n"
996 "Endpoint: %s\r\n"
997 "RequestURI: %s\r\n"
998 "ToURI: %s\r\n"
999 "FromURI: %s\r\n",
1000 mdata->from,
1001 ast_msg_get_from(mdata->msg),
1002 mdata->destination,
1003 ast_msg_get_to(mdata->msg),
1004 ast_sorcery_object_get_id(endpoint),
1005 uri,
1006 touri,
1007 fromuri
1008 );
1009 }
1010#endif
1011
1012 update_content_type(tdata, mdata->msg, &body);
1013
1014 ast_sip_add_body(tdata, &body);
1015 if (!tdata->msg->body) {
1016 pjsip_tx_data_dec_ref(tdata);
1017 ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not add body to request\n");
1018 return -1;
1019 }
1020
1021 /*
1022 * This copies any headers set with MESSAGE_DATA() to the
1023 * tdata.
1024 */
1025 vars_to_headers(mdata->msg, tdata);
1026
1027 ast_debug(1, "Sending message to '%s' (via endpoint %s) from '%s'\n",
1028 uri, ast_sorcery_object_get_id(endpoint), mdata->from);
1029
1030 /* Determine From URI */
1031 if (!ast_strlen_zero(mdata->from)) {
1032 from_uri = mdata->from;
1033 } else if (!ast_strlen_zero(ast_msg_get_from(mdata->msg))) {
1034 from_uri = ast_msg_get_from(mdata->msg);
1035 } else {
1036 from_uri = "";
1037 }
1038
1039 /* Determine To URI */
1040 if (!ast_strlen_zero(ast_msg_get_to(mdata->msg))) {
1041 to_uri = ast_msg_get_to(mdata->msg);
1042 } else {
1043 to_uri = "";
1044 }
1045
1046 /* Build content type string */
1047 ast_str_set(&content_type_buf, 0, "%s/%s", body.type, body.subtype);
1048
1049 /* Create callback data */
1050 resp_data = message_response_data_create(endpoint, from_uri, to_uri,
1051 body.body_text, ast_str_buffer(content_type_buf), uri);
1052
1053 if (!resp_data) {
1054 pjsip_tx_data_dec_ref(tdata);
1055 ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not allocate callback data for endpoint '%s'\n",
1056 ast_sorcery_object_get_id(endpoint));
1057 return -1;
1058 }
1059
1060 /* Send with callback for redirect handling */
1061 if (ast_sip_send_request(tdata, NULL, endpoint, resp_data, msg_response_callback)) {
1062 ao2_ref(resp_data, -1);
1063 ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request on endpoint '%s'\n", ast_sorcery_object_get_id(endpoint));
1064 return -1;
1065 }
1066
1067 return 0;
1068}
1069
1070static int sip_msg_send(const struct ast_msg *msg, const char *destination, const char *from)
1071{
1072 struct msg_data *mdata;
1073 int res;
1074
1076 ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI must be specified\n");
1077 return -1;
1078 }
1079
1081 if (!mdata) {
1082 return -1;
1083 }
1084
1086 ao2_ref(mdata, -1);
1087
1088 return res;
1089}
1090
1091static const struct ast_msg_tech msg_tech = {
1092 .name = "pjsip",
1093 .msg_send = sip_msg_send,
1094};
1095
1096static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
1097 pjsip_dialog *dlg, pjsip_transaction *tsx)
1098{
1099 pjsip_tx_data *tdata;
1100 pj_status_t status;
1101
1102 status = ast_sip_create_response(rdata, code, NULL, &tdata);
1103 if (status != PJ_SUCCESS) {
1104 ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
1105 return status;
1106 }
1107
1108 if (dlg && tsx) {
1109 status = pjsip_dlg_send_response(dlg, tsx, tdata);
1110 } else {
1111 struct ast_sip_endpoint *endpoint;
1112
1113 endpoint = ast_pjsip_rdata_get_endpoint(rdata);
1114 status = ast_sip_send_stateful_response(rdata, tdata, endpoint);
1115 ao2_cleanup(endpoint);
1116 }
1117
1118 if (status != PJ_SUCCESS) {
1119 ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
1120 }
1121
1122 return status;
1123}
1124
1125static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
1126{
1127 enum pjsip_status_code code;
1128 struct ast_msg *msg;
1129
1130 /* if not a MESSAGE, don't handle */
1131 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
1132 return PJ_FALSE;
1133 }
1134
1135 code = check_content_type(rdata);
1136 if (code != PJSIP_SC_OK) {
1137 send_response(rdata, code, NULL, NULL);
1138 return PJ_TRUE;
1139 }
1140
1141 msg = ast_msg_alloc();
1142 if (!msg) {
1143 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
1144 return PJ_TRUE;
1145 }
1146
1147 code = rx_data_to_ast_msg(rdata, msg);
1148 if (code != PJSIP_SC_OK) {
1149 send_response(rdata, code, NULL, NULL);
1150 ast_msg_destroy(msg);
1151 return PJ_TRUE;
1152 }
1153
1154 if (!ast_msg_has_destination(msg)) {
1155 ast_debug(1, "MESSAGE request received, but no handler wanted it\n");
1156 send_response(rdata, PJSIP_SC_NOT_FOUND, NULL, NULL);
1157 ast_msg_destroy(msg);
1158 return PJ_TRUE;
1159 }
1160
1161 /* Send it to the messaging core.
1162 *
1163 * If we are unable to send a response, the most likely reason is that we
1164 * are handling a retransmission of an incoming MESSAGE and were unable to
1165 * create a transaction due to a duplicate key. If we are unable to send
1166 * a response, we should not queue the message to the dialplan
1167 */
1168 if (!send_response(rdata, PJSIP_SC_ACCEPTED, NULL, NULL)) {
1169 ast_msg_queue(msg);
1170 }
1171
1172 return PJ_TRUE;
1173}
1174
1175static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
1176{
1177 enum pjsip_status_code code;
1178 int rc;
1179 pjsip_dialog *dlg = session->inv_session->dlg;
1180 pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
1181 struct ast_msg_data *msg;
1182 struct ast_party_caller *caller;
1183 pjsip_name_addr *name_addr;
1184 size_t from_len;
1185 size_t to_len;
1186 struct ast_msg_data_attribute attrs[4];
1187 int pos = 0;
1188 int body_pos;
1189
1190 if (!session->channel) {
1191 send_response(rdata, PJSIP_SC_NOT_FOUND, dlg, tsx);
1192 return 0;
1193 }
1194
1195 code = check_content_type_in_dialog(rdata);
1196 if (code != PJSIP_SC_OK) {
1197 send_response(rdata, code, dlg, tsx);
1198 return 0;
1199 }
1200
1201 caller = ast_channel_caller(session->channel);
1202
1203 name_addr = (pjsip_name_addr *) rdata->msg_info.from->uri;
1204 from_len = pj_strlen(&name_addr->display);
1205 if (from_len) {
1206 attrs[pos].type = AST_MSG_DATA_ATTR_FROM;
1207 from_len++;
1208 attrs[pos].value = ast_alloca(from_len);
1209 ast_copy_pj_str(attrs[pos].value, &name_addr->display, from_len);
1210 pos++;
1211 } else if (caller->id.name.valid && !ast_strlen_zero(caller->id.name.str)) {
1212 attrs[pos].type = AST_MSG_DATA_ATTR_FROM;
1213 attrs[pos].value = caller->id.name.str;
1214 pos++;
1215 }
1216
1217 name_addr = (pjsip_name_addr *) rdata->msg_info.to->uri;
1218 to_len = pj_strlen(&name_addr->display);
1219 if (to_len) {
1220 attrs[pos].type = AST_MSG_DATA_ATTR_TO;
1221 to_len++;
1222 attrs[pos].value = ast_alloca(to_len);
1223 ast_copy_pj_str(attrs[pos].value, &name_addr->display, to_len);
1224 pos++;
1225 }
1226
1228 attrs[pos].value = ast_alloca(rdata->msg_info.msg->body->content_type.type.slen
1229 + rdata->msg_info.msg->body->content_type.subtype.slen + 2);
1230 sprintf(attrs[pos].value, "%.*s/%.*s",
1231 (int)rdata->msg_info.msg->body->content_type.type.slen,
1232 rdata->msg_info.msg->body->content_type.type.ptr,
1233 (int)rdata->msg_info.msg->body->content_type.subtype.slen,
1234 rdata->msg_info.msg->body->content_type.subtype.ptr);
1235 pos++;
1236
1237 body_pos = pos;
1238 attrs[pos].type = AST_MSG_DATA_ATTR_BODY;
1239 attrs[pos].value = ast_malloc(rdata->msg_info.msg->body->len + 1);
1240 if (!attrs[pos].value) {
1241 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
1242 return 0;
1243 }
1244 ast_copy_string(attrs[pos].value, rdata->msg_info.msg->body->data, rdata->msg_info.msg->body->len + 1);
1245 pos++;
1246
1248 if (!msg) {
1249 ast_free(attrs[body_pos].value);
1250 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
1251 return 0;
1252 }
1253
1254 ast_debug(1, "Received in-dialog MESSAGE from '%s:%s': %s %s\n",
1256 ast_channel_name(session->channel),
1259
1260 rc = ast_msg_data_queue_frame(session->channel, msg);
1261 ast_free(attrs[body_pos].value);
1262 ast_free(msg);
1263 if (rc != 0) {
1264 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
1265 } else {
1266 send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
1267 }
1268
1269 return 0;
1270}
1271
1273 .method = "MESSAGE",
1274 .incoming_request = incoming_in_dialog_request
1275};
1276
1277static pjsip_module messaging_module = {
1278 .name = {"Messaging Module", 16},
1279 .id = -1,
1280 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
1281 .on_rx_request = module_on_rx_request,
1282};
1283
1284static int load_module(void)
1285{
1286 if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
1288 }
1289
1290 if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(),
1291 NULL, PJSIP_H_ALLOW, NULL, 1,
1292 &pjsip_message_method.name) != PJ_SUCCESS) {
1293
1296 }
1297
1301 }
1302
1303 message_serializer = ast_sip_create_serializer("pjsip/messaging");
1304 if (!message_serializer) {
1308 }
1309
1312}
1313
1322
1323AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support",
1324 .support_level = AST_MODULE_SUPPORT_CORE,
1325 .load = load_module,
1326 .unload = unload_module,
1327 .load_pri = AST_MODPRI_APP_DEPEND,
1328 .requires = "res_pjsip,res_pjsip_session",
jack_status_t status
Definition app_jack.c:149
char * text
Definition app_queue.c:1791
Asterisk main include file. File version handling, generic pbx functions.
static struct ast_mansession session
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition astmm.h:288
#define ast_free(a)
Definition astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
#define ast_malloc(len)
A wrapper for malloc()
Definition astmm.h:191
#define ast_log
Definition astobj2.c:42
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition astobj2.h:367
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition astobj2.h:404
#define ao2_alloc(data_size, destructor_fn)
Definition astobj2.h:409
static const char type[]
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
#define AST_MAX_EXTENSION
Definition channel.h:134
char * end
Definition eagi_proxy.c:73
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static const char name[]
Definition format_mp3.c:68
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const char * ast_msg_data_get_attribute(struct ast_msg_data *msg, enum ast_msg_data_attribute_type attribute_type)
Get attribute from ast_msg_data.
struct ast_msg_data * ast_msg_data_alloc(enum ast_msg_data_source_type source, struct ast_msg_data_attribute attributes[], size_t count)
Allocates an ast_msg_data structure.
int ast_msg_data_queue_frame(struct ast_channel *channel, struct ast_msg_data *msg)
Queue an AST_FRAME_TEXT_DATA frame containing an ast_msg_data structure.
@ AST_MSG_DATA_ATTR_BODY
Definition message.h:458
@ AST_MSG_DATA_ATTR_TO
Definition message.h:455
@ AST_MSG_DATA_ATTR_FROM
Definition message.h:456
@ AST_MSG_DATA_ATTR_CONTENT_TYPE
Definition message.h:457
@ AST_MSG_DATA_SOURCE_TYPE_IN_DIALOG
Definition message.h:449
struct ast_taskprocessor * ast_sip_create_serializer(const char *name)
Create a new serializer for SIP tasks.
Definition res_pjsip.c:2088
#define ast_sip_push_task_wait_serializer(serializer, sip_task, task_data)
Definition res_pjsip.h:2189
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_NOTICE
#define LOG_WARNING
Out-of-call text message support.
int ast_msg_set_from(struct ast_msg *msg, const char *fmt,...)
Set the 'from' URI of a message.
const char * ast_msg_get_var(struct ast_msg *msg, const char *name)
Get the specified variable on the message.
const char * ast_msg_get_from(const struct ast_msg *msg)
Retrieve the source of this message.
void ast_msg_var_iterator_destroy(struct ast_msg_var_iterator *iter)
Destroy a message variable iterator.
struct ast_msg * ast_msg_destroy(struct ast_msg *msg)
Destroy an ast_msg.
int ast_msg_set_endpoint(struct ast_msg *msg, const char *fmt,...)
Set the technology's endpoint associated with this message.
int ast_msg_var_iterator_next(const struct ast_msg *msg, struct ast_msg_var_iterator *iter, const char **name, const char **value)
Get the next variable name and value that is set for sending outbound.
int ast_msg_set_exten(struct ast_msg *msg, const char *fmt,...)
Set the dialplan extension for this message.
int ast_msg_set_tech(struct ast_msg *msg, const char *fmt,...)
Set the technology associated with this message.
int ast_msg_has_destination(const struct ast_msg *msg)
Determine if a particular message has a destination via some handler.
struct ast_msg * ast_msg_alloc(void)
Allocate a message.
int ast_msg_tech_unregister(const struct ast_msg_tech *tech)
Unregister a message technology.
int ast_msg_set_var(struct ast_msg *msg, const char *name, const char *value)
Set a variable on the message going to the dialplan.
const char * ast_msg_get_to(const struct ast_msg *msg)
Retrieve the destination of this message.
int ast_msg_tech_register(const struct ast_msg_tech *tech)
Register a message technology.
const char * ast_msg_get_body(const struct ast_msg *msg)
Get the body of a message.
int ast_msg_set_context(struct ast_msg *msg, const char *fmt,...)
Set the dialplan context for this message.
int ast_msg_set_body(struct ast_msg *msg, const char *fmt,...)
Set the 'body' text of a message (in UTF-8)
int ast_msg_set_to(struct ast_msg *msg, const char *fmt,...)
Set the 'to' URI of a message.
void ast_msg_var_unref_current(struct ast_msg_var_iterator *iter)
Unref a message var from inside an iterator loop.
struct ast_msg_var_iterator * ast_msg_var_iterator_init(const struct ast_msg *msg)
Create a new message variable iterator.
int ast_msg_queue(struct ast_msg *msg)
Queue a message for routing through the dialplan.
struct ast_msg * ast_msg_ref(struct ast_msg *msg)
Bump a msg's ref count.
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition module.h:557
@ AST_MODPRI_APP_DEPEND
Definition module.h:342
@ AST_MODULE_SUPPORT_CORE
Definition module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
Core PBX routines and definitions.
struct ast_sip_endpoint * ast_sip_get_endpoint(const char *to, int get_default_outbound, char **uri)
Retrieves an endpoint and URI from the "to" string.
Definition res_pjsip.c:3220
const pj_str_t * ast_sip_pjsip_uri_get_username(pjsip_uri *uri)
Get the user portion of the pjsip_uri.
Definition res_pjsip.c:3448
void ast_sip_unregister_service(pjsip_module *module)
Definition res_pjsip.c:127
int ast_sip_create_response(const pjsip_rx_data *rdata, int st_code, struct ast_sip_contact *contact, pjsip_tx_data **p_tdata)
General purpose method for creating a SIP response.
Definition res_pjsip.c:2439
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
Definition res_pjsip.c:111
int ast_sip_is_allowed_uri(pjsip_uri *uri)
Check whether a pjsip_uri is allowed or not.
Definition res_pjsip.c:3443
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition res_pjsip.c:514
#define AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(str)
Truncate the URI user field options string if enabled.
Definition res_pjsip.h:3529
int ast_sip_update_from(pjsip_tx_data *tdata, char *from)
Overwrite fields in the outbound 'From' header.
Definition res_pjsip.c:3354
struct ast_sip_endpoint * ast_pjsip_rdata_get_endpoint(pjsip_rx_data *rdata)
Get the looked-up endpoint on an out-of dialog request or response.
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition res_pjsip.c:2172
int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint, void *token, void(*callback)(void *token, pjsip_event *e))
General purpose method for sending a SIP request.
Definition res_pjsip.c:1973
int ast_sip_add_body(pjsip_tx_data *tdata, const struct ast_sip_body *body)
Add a body to an outbound SIP message.
Definition res_pjsip.c:2046
int ast_sip_update_to_uri(pjsip_tx_data *tdata, const char *to)
Replace the To URI in the tdata with the supplied one.
Definition res_pjsip.c:3294
int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint, const char *uri, struct ast_sip_contact *contact, pjsip_tx_data **tdata)
General purpose method for creating a SIP request.
Definition res_pjsip.c:1429
int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
Add a header to an outbound SIP message.
Definition res_pjsip.c:2002
const pj_str_t * ast_sip_pjsip_uri_get_hostname(pjsip_uri *uri)
Get the host portion of the pjsip_uri.
Definition res_pjsip.c:3467
#define PJSTR_PRINTF_VAR(_v)
Definition res_pjsip.h:72
int ast_sip_send_stateful_response(pjsip_rx_data *rdata, pjsip_tx_data *tdata, struct ast_sip_endpoint *sip_endpoint)
Send a stateful response to an out of dialog request.
Definition res_pjsip.c:2395
int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
Checks if the given content type matches type/subtype.
Definition res_pjsip.c:2219
#define PJSTR_PRINTF_SPEC
Definition res_pjsip.h:71
static struct message_response_data * message_response_data_create(struct ast_sip_endpoint *endpoint, const char *from, const char *to, const char *body, const char *content_type, const char *initial_uri)
static void msg_response_callback(void *token, pjsip_event *e)
static struct ast_sip_session_supplement messaging_supplement
static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata)
static void handle_message_redirect(struct message_response_data *resp_data, pjsip_rx_data *rdata)
static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
static enum pjsip_status_code check_content_type_in_dialog(const pjsip_rx_data *rdata)
static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
static int print_body(pjsip_rx_data *rdata, char *buf, int len)
static void msg_data_destroy(void *obj)
static pjsip_module messaging_module
static int msg_send(void *data)
static void message_response_data_destroy(void *obj)
static void update_content_type(pjsip_tx_data *tdata, struct ast_msg *msg, struct ast_sip_body *body)
#define MAX_HDR_SIZE
static int load_module(void)
static int send_message_to_uri(struct message_response_data *resp_data, const char *target_uri)
#define MAX_BODY_SIZE
static struct msg_data * msg_data_create(const struct ast_msg *msg, const char *destination, const char *from)
static struct ast_taskprocessor * message_serializer
static int unload_module(void)
static const struct ast_msg_tech msg_tech
static int is_msg_var_blocked(const char *name)
const pjsip_method pjsip_message_method
static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg)
static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code, pjsip_dialog *dlg, pjsip_transaction *tsx)
static int update_to_display_name(pjsip_tx_data *tdata, char *to)
static int sip_msg_send(const struct ast_msg *msg, const char *destination, const char *from)
int ast_sip_redirect_parse_3xx(pjsip_rx_data *rdata, struct ast_sip_redirect_state *state)
Parse a 3xx redirect response and extract contacts.
Definition redirect.c:338
int ast_sip_redirect_get_next_uri(struct ast_sip_redirect_state *state, char **uri_out)
Get the next redirect URI to try.
Definition redirect.c:398
#define AST_SIP_MAX_REDIRECT_HOPS
Maximum number of redirect hops allowed.
struct ast_sip_endpoint * ast_sip_redirect_get_endpoint(const struct ast_sip_redirect_state *state)
Get the endpoint from the redirect state.
Definition redirect.c:439
int ast_sip_redirect_get_hop_count(const struct ast_sip_redirect_state *state)
Get the current hop count.
Definition redirect.c:434
void ast_sip_redirect_state_destroy(struct ast_sip_redirect_state *state)
Destroy a redirect state and free all resources.
Definition redirect.c:444
struct ast_sip_redirect_state * ast_sip_redirect_state_create(struct ast_sip_endpoint *endpoint, const char *initial_uri)
Create a new redirect state.
Definition redirect.c:59
#define ast_sip_session_register_supplement(supplement)
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
#define NULL
Definition resample.c:96
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition sorcery.c:2381
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition strings.h:80
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition strings.h:659
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition strings.h:1113
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition strings.h:425
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Checks whether a string begins with another.
Definition strings.h:97
char *attribute_pure ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition strings.h:161
enum ast_msg_data_attribute_type type
Definition message.h:463
Structure used to transport a message through the frame core.
A message technology.
Definition message.h:52
const char *const name
Name of this message technology.
Definition message.h:61
struct ao2_iterator iter
A message.
Caller Party information.
Definition channel.h:420
struct ast_party_id id
Caller party ID.
Definition channel.h:422
struct ast_party_name name
Subscriber name.
Definition channel.h:342
unsigned char valid
TRUE if the name information is valid/present.
Definition channel.h:281
char * str
Subscriber name (Malloced)
Definition channel.h:266
SIP body description.
Definition res_pjsip.h:2469
const char * type
Definition res_pjsip.h:2471
const char * body_text
Definition res_pjsip.h:2475
const char * subtype
Definition res_pjsip.h:2473
An entity with which Asterisk communicates.
Definition res_pjsip.h:1061
Redirect state structure.
Definition redirect.c:52
A supplement to SIP message processing.
struct ast_module *const char * method
A structure describing a SIP session.
Support for dynamic strings.
Definition strings.h:623
A ast_taskprocessor structure is a singleton by name.
Channel datastore data for max forwards.
struct ast_sip_redirect_state * redirect_state
struct ast_msg * msg
struct test_val * next
int value
Definition syslog.c:37
An API for managing task processing threads that can be shared across modules.
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
Test Framework API.
#define ast_test_suite_event_notify(s, f,...)
Definition test.h:189
static struct test_val c
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:981
#define ast_assert(a)
Definition utils.h:779
#define ARRAY_LEN(a)
Definition utils.h:706