2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Kevin Harwell <kharwell@digium.com>
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.
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.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_session</depend>
23 <support_level>core</support_level>
28 #include "pjsua-lib/pjsua.h"
30 #include "asterisk/message.h"
31 #include "asterisk/module.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/res_pjsip.h"
34 #include "asterisk/res_pjsip_session.h"
36 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
38 #define MAX_HDR_SIZE 512
39 #define MAX_BODY_SIZE 1024
40 #define MAX_EXTEN_SIZE 256
41 #define MAX_USER_SIZE 128
45 * \brief Determine where in the dialplan a call should go
47 * \details This uses the username in the request URI to try to match
48 * an extension in an endpoint's context in order to route the call.
50 * \param rdata The SIP request
51 * \param context The context to use
52 * \param exten The extension to use
54 static enum pjsip_status_code get_destination(const pjsip_rx_data *rdata, const char *context, char *exten)
56 pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
57 pjsip_sip_uri *sip_ruri;
59 if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
60 return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
63 sip_ruri = pjsip_uri_get_uri(ruri);
64 ast_copy_pj_str(exten, &sip_ruri->user, MAX_EXTEN_SIZE);
66 if (ast_exists_extension(NULL, context, exten, 1, NULL)) {
69 return PJSIP_SC_NOT_FOUND;
74 * \brief Checks to make sure the request has the correct content type.
76 * \details This module supports the following media types: "text/plain".
77 * Return unsupported otherwise.
79 * \param rdata The SIP request
81 static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
83 if (ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
88 return PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
94 * \brief Puts pointer past 'sip[s]:' string that should be at the
95 * front of the given 'fromto' parameter
97 * \param fromto 'From' or 'To' field containing 'sip:'
99 static const char* skip_sip(const char *fromto)
103 /* need to be one past 'sip:' or 'sips:' */
104 if (!(p = strstr(fromto, "sip"))) {
117 * \brief Retrieves an endpoint if specified in the given 'fromto'
119 * Expects the given 'fromto' to be in one of the following formats:
120 * sip[s]:endpoint[/aor]
121 * sip[s]:endpoint[/uri]
123 * If an optional aor is given it will try to find an associated uri
124 * to return. If an optional uri is given then that will be returned,
125 * otherwise uri will be NULL.
127 * \param fromto 'From' or 'To' field with possible endpoint
128 * \param uri Optional uri to return
130 static struct ast_sip_endpoint* get_endpoint(const char *fromto, char **uri)
132 const char *name = skip_sip(fromto);
133 struct ast_sip_endpoint* endpoint;
134 struct ast_sip_aor *aor;
136 if ((*uri = strchr(name, '/'))) {
138 } else if ((*uri = strchr(name, '@'))) {
142 /* endpoint is required */
143 if (ast_strlen_zero(name)) {
147 if (!(endpoint = ast_sorcery_retrieve_by_id(
148 ast_sip_get_sorcery(), "endpoint", name))) {
152 if (*uri && (aor = ast_sip_location_retrieve_aor(*uri))) {
153 *uri = (char*)ast_sip_location_retrieve_first_aor_contact(aor)->uri;
161 * \brief Updates fields in an outgoing 'From' header.
163 * \param tdata The outgoing message data structure
164 * \param from Info to potentially copy into the 'From' header
166 static void update_from(pjsip_tx_data *tdata, const char *from)
168 pjsip_name_addr *from_name_addr;
169 pjsip_sip_uri *from_uri;
173 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
175 if (ast_strlen_zero(from)) {
179 if (!(endpoint = get_endpoint(from, &uri))) {
183 if (ast_strlen_zero(uri)) {
184 /* if no aor/uri was specified get one from the endpoint */
185 uri = (char*)ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors)->uri;
188 /* get current 'from' hdr & uri - going to overwrite some fields */
189 from_name_addr = (pjsip_name_addr *)PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
190 from_uri = pjsip_uri_get_uri(from_name_addr);
192 /* check to see if uri is in 'name <sip:user@domain>' format */
193 if ((parsed = pjsip_parse_uri(tdata->pool, uri, strlen(uri), PJSIP_PARSE_URI_AS_NAMEADDR))) {
194 pjsip_name_addr *name_addr = (pjsip_name_addr *)parsed;
195 pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(name_addr->uri);
197 pj_strdup(tdata->pool, &from_name_addr->display, &name_addr->display);
198 pj_strdup(tdata->pool, &from_uri->user, &sip_uri->user);
199 pj_strdup(tdata->pool, &from_uri->host, &sip_uri->host);
200 from_uri->port = sip_uri->port;
202 /* assume it is 'user[@domain]' format */
203 char *domain = strchr(uri, '@');
206 pj_strdup2(tdata->pool, &from_uri->host, domain);
208 pj_strdup2(tdata->pool, &from_uri->user, uri);
212 static char *scheme_sip_to_pjsip(pjsip_rx_data *rdata, char *buf, unsigned int size)
215 pjsip_name_addr *name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
216 pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(name_addr->uri);
218 const pj_str_t *scheme = pjsip_uri_get_scheme(rdata->msg_info.to->uri);
219 size_t size_scheme = pj_strlen(scheme);
220 size_t size_user = pj_strlen(&sip_uri->user);
221 size_t size_host = pj_strlen(&sip_uri->host);
223 /* 5 = count of 'p' 'j' ':' '@' '\0' */
224 if (size < size_scheme + size_user + size_host + 5) {
226 ast_log(LOG_WARNING, "Unable to handle MESSAGE- incoming uri "
227 "too large for given buffer\n");
234 memcpy(buf, pj_strbuf(scheme), size_scheme);
238 memcpy(buf, pj_strbuf(&sip_uri->user), size_user);
242 memcpy(buf, pj_strbuf(&sip_uri->host), size_host);
249 static char *scheme_pjsip_to_sip(const char *uri)
251 ast_assert(!strncmp(uri, "pjsip", 5));
252 return ast_strdup(uri + 2);
257 * \brief Checks if the given msg var name should be blocked.
259 * \details Some headers are not allowed to be overriden by the user.
260 * Determine if the given var header name from the user is blocked for
261 * an outgoing MESSAGE.
263 * \param name name of header to see if it is blocked.
265 * \retval TRUE if the given header is blocked.
267 static int is_msg_var_blocked(const char *name)
272 * Don't block Content-Type or Max-Forwards headers because the
273 * user can override them.
275 static const char *hdr[] = {
288 for (i = 0; i < ARRAY_LEN(hdr); ++i) {
289 if (!strcasecmp(name, hdr[i])) {
290 /* Block addition of this header. */
299 * \brief Copies any other msg vars over to the request headers.
301 * \param msg The msg structure to copy headers from
302 * \param tdata The SIP transmission data
304 static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_tx_data *tdata)
310 RAII_VAR(struct ast_msg_var_iterator *, i, ast_msg_var_iterator_init(msg), ast_msg_var_iterator_destroy);
311 while (ast_msg_var_iterator_next(msg, i, &name, &value)) {
312 if (!strcasecmp(name, "Max-Forwards")) {
313 /* Decrement Max-Forwards for SIP loop prevention. */
314 if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) {
315 ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero. MESSAGE not sent.\n");
318 sprintf((char*)value, "%d", max_forwards);
319 ast_sip_add_header(tdata, name, value);
320 } else if (!strcasecmp(name, "To")) {
321 char *to = scheme_pjsip_to_sip(value);
323 ast_sip_add_header(tdata, name, to);
326 } else if (!is_msg_var_blocked(name)) {
327 ast_sip_add_header(tdata, name, value);
329 ast_msg_var_unref_current(i);
336 * \brief Copies any other request header data over to ast_msg structure.
338 * \param rdata The SIP request
339 * \param msg The msg structure to copy headers into
341 static int headers_to_vars(const pjsip_rx_data *rdata, struct ast_msg *msg)
344 char buf[MAX_HDR_SIZE];
346 pjsip_hdr *h = rdata->msg_info.msg->hdr.next;
347 pjsip_hdr *end= &rdata->msg_info.msg->hdr;
350 if ((res = pjsip_hdr_print_on(h, buf, sizeof(buf)-1)) > 0) {
352 if ((c = strchr(buf, ':'))) {
353 ast_copy_string(buf, ast_skip_blanks(c + 1), sizeof(buf)-(c-buf));
356 if ((res = ast_msg_set_var(msg, pj_strbuf(&h->name), buf)) != 0) {
367 * \brief Prints the message body into the given char buffer.
369 * \details Copies body content from the received data into the given
370 * character buffer removing any extra carriage return/line feeds.
372 * \param rdata The SIP request
373 * \param buf Buffer to fill
374 * \param len The length of the buffer
376 static int print_body(pjsip_rx_data *rdata, char *buf, int len)
378 int res = rdata->msg_info.msg->body->print_body(
379 rdata->msg_info.msg->body, buf, len);
385 /* remove any trailing carriage return/line feeds */
386 while (res > 0 && ((buf[--res] == '\r') || (buf[res] == '\n')));
395 * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
397 * \details Attempts to fill in as much information as possible into the given
398 * msg structure copied from the given request data.
400 * \param rdata The SIP request
401 * \param msg The asterisk message structure to fill in.
403 static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
406 #define CHECK_RES(z_) do { if (z_) { ast_msg_destroy(msg); \
407 return PJSIP_SC_INTERNAL_SERVER_ERROR; } } while (0)
410 char buf[MAX_BODY_SIZE];
411 pjsip_name_addr *name_addr;
413 pjsip_status_code code;
414 struct ast_sip_endpoint *endpt = ast_pjsip_rdata_get_endpoint(rdata);
416 /* make sure there is an appropriate context and extension*/
417 if ((code = get_destination(rdata, endpt->context, buf)) != PJSIP_SC_OK) {
421 CHECK_RES(ast_msg_set_context(msg, "%s", endpt->context));
422 CHECK_RES(ast_msg_set_exten(msg, "%s", buf));
425 CHECK_RES(ast_msg_set_to(msg, "%s", scheme_sip_to_pjsip(rdata, buf, sizeof(buf))));
428 name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
429 if ((size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf)-1)) > 0) {
431 CHECK_RES(ast_msg_set_from(msg, "%s", buf));
435 if ((size = pjsip_hdr_print_on(pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL), buf, sizeof(buf)-1)) > 0) {
437 CHECK_RES(ast_msg_set_var(msg, "SIP_FULLCONTACT", buf));
440 /* receive address */
441 field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf)-1, 1);
442 CHECK_RES(ast_msg_set_var(msg, "SIP_RECVADDR", field));
445 if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
446 CHECK_RES(ast_msg_set_body(msg, "%s", buf));
450 if (endpt->id.self.name.valid) {
451 CHECK_RES(ast_msg_set_var(msg, "SIP_PEERNAME", endpt->id.self.name.str));
454 CHECK_RES(headers_to_vars(rdata, msg));
465 static void msg_data_destroy(void *obj)
467 struct msg_data *mdata = obj;
469 ast_free(mdata->from);
472 ast_msg_destroy(mdata->msg);
475 static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
478 struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
484 /* typecast to suppress const warning */
485 mdata->msg = ast_msg_ref((struct ast_msg*)msg);
487 mdata->to = scheme_pjsip_to_sip(to);
488 mdata->from = ast_strdup(from);
490 /* sometimes from can still contain the tag at this point, so remove it */
491 if ((tag = strchr(mdata->from, ';'))) {
498 static int msg_send(void *data)
500 RAII_VAR(struct msg_data *, mdata, data, ao2_cleanup);
502 const struct ast_sip_body body = {
505 .body_text = ast_msg_get_body(mdata->msg)
508 pjsip_tx_data *tdata;
511 RAII_VAR(struct ast_sip_endpoint *, endpoint, get_endpoint(
512 mdata->to, &uri), ao2_cleanup);
514 ast_log(LOG_ERROR, "SIP MESSAGE - Endpoint not found in %s\n", mdata->to);
518 if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, &tdata)) {
519 ast_log(LOG_ERROR, "SIP MESSAGE - Could not create request\n");
523 if (ast_sip_add_body(tdata, &body)) {
524 pjsip_tx_data_dec_ref(tdata);
525 ast_log(LOG_ERROR, "SIP MESSAGE - Could not add body to request\n");
529 update_from(tdata, mdata->from);
530 vars_to_headers(mdata->msg, tdata);
531 if (ast_sip_send_request(tdata, NULL, endpoint)) {
532 ast_log(LOG_ERROR, "SIP MESSAGE - Could not send request\n");
539 static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *from)
541 struct msg_data *mdata;
543 if (ast_strlen_zero(to)) {
544 ast_log(LOG_ERROR, "SIP MESSAGE - a 'To' URI must be specified\n");
548 if (!(mdata = msg_data_create(msg, to, from)) ||
549 ast_sip_push_task(NULL, msg_send, mdata)) {
556 static const struct ast_msg_tech msg_tech = {
558 .msg_send = sip_msg_send,
561 static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code code,
562 pjsip_dialog *dlg, pjsip_transaction *tsx)
564 pjsip_tx_data *tdata;
566 pjsip_response_addr res_addr;
568 pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
570 status = pjsip_endpt_create_response(endpt, rdata, code, NULL, &tdata);
571 if (status != PJ_SUCCESS) {
572 ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
577 status = pjsip_dlg_send_response(dlg, tsx, tdata);
579 /* Get where to send request. */
580 status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
581 if (status != PJ_SUCCESS) {
582 ast_log(LOG_ERROR, "Unable to get response address (%d)\n", status);
585 status = pjsip_endpt_send_response(endpt, &res_addr, tdata, NULL, NULL);
588 if (status != PJ_SUCCESS) {
589 ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
595 static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
597 enum pjsip_status_code code;
600 /* if not a MESSAGE, don't handle */
601 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_message_method)) {
605 msg = ast_msg_alloc();
607 send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
611 if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
612 send_response(rdata, code, NULL, NULL);
616 if ((code = rx_data_to_ast_msg(rdata, msg)) == PJSIP_SC_OK) {
617 /* send it to the dialplan */
619 code = PJSIP_SC_ACCEPTED;
622 send_response(rdata, code, NULL, NULL);
626 static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
628 char buf[MAX_BODY_SIZE];
629 enum pjsip_status_code code;
632 pjsip_dialog *dlg = session->inv_session->dlg;
633 pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
635 if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
636 send_response(rdata, code, dlg, tsx);
640 if (print_body(rdata, buf, sizeof(buf)-1) < 1) {
641 /* invalid body size */
645 memset(&f, 0, sizeof(f));
646 f.frametype = AST_FRAME_TEXT;
647 f.subclass.integer = 0;
650 f.datalen = strlen(buf) + 1;
651 ast_queue_frame(session->channel, &f);
653 send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
657 static struct ast_sip_session_supplement messaging_supplement = {
659 .incoming_request = incoming_in_dialog_request
662 static pjsip_module messaging_module = {
663 .name = {"Messaging Module", 16},
665 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
666 .on_rx_request = module_on_rx_request,
669 static int load_module(void)
671 if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
672 return AST_MODULE_LOAD_DECLINE;
675 if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(),
676 NULL, PJSIP_H_ALLOW, NULL, 1,
677 &pjsip_message_method.name) != PJ_SUCCESS) {
679 ast_sip_unregister_service(&messaging_module);
680 return AST_MODULE_LOAD_DECLINE;
683 if (ast_msg_tech_register(&msg_tech)) {
684 ast_sip_unregister_service(&messaging_module);
685 return AST_MODULE_LOAD_DECLINE;
688 ast_sip_session_register_supplement(&messaging_supplement);
689 return AST_MODULE_LOAD_SUCCESS;
692 static int unload_module(void)
694 ast_sip_session_unregister_supplement(&messaging_supplement);
695 ast_msg_tech_unregister(&msg_tech);
696 ast_sip_unregister_service(&messaging_module);
700 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support",
702 .unload = unload_module,
703 .load_pri = AST_MODPRI_APP_DEPEND,