Removing registrar_expire from basic-pbx config
[asterisk/asterisk.git] / res / res_pjsip_messaging.c
index 00923b2..0e10a8f 100644 (file)
 /*** MODULEINFO
        <depend>pjproject</depend>
        <depend>res_pjsip</depend>
-       <depend>res_pjsip_session</depend>
        <support_level>core</support_level>
  ***/
 
+/*** DOCUMENTATION
+       <info name="MessageFromInfo" language="en_US" tech="PJSIP">
+               <para>The <literal>from</literal> parameter can be a configured endpoint
+               or in the form of "display-name" &lt;URI&gt;.</para>
+       </info>
+       <info name="MessageToInfo" language="en_US" tech="PJSIP">
+               <para>Specifying a prefix of <literal>pjsip:</literal> will send the
+               message as a SIP MESSAGE request.</para>
+       </info>
+ ***/
 #include "asterisk.h"
 
 #include "pjsua-lib/pjsua.h"
 #include "asterisk/pbx.h"
 #include "asterisk/res_pjsip.h"
 #include "asterisk/res_pjsip_session.h"
+#include "asterisk/taskprocessor.h"
 
 const pjsip_method pjsip_message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} };
 
 #define MAX_HDR_SIZE 512
 #define MAX_BODY_SIZE 1024
-#define MAX_EXTEN_SIZE 256
 #define MAX_USER_SIZE 128
 
+static struct ast_taskprocessor *message_serializer;
+
 /*!
  * \internal
- * \brief Determine where in the dialplan a call should go
+ * \brief Checks to make sure the request has the correct content type.
  *
- * \details This uses the username in the request URI to try to match
- * an extension in an endpoint's context in order to route the call.
+ * \details This module supports the following media types: "text/plain".
+ * Return unsupported otherwise.
  *
  * \param rdata The SIP request
- * \param context The context to use
- * \param exten The extension to use
  */
-static enum pjsip_status_code get_destination(const pjsip_rx_data *rdata, const char *context, char *exten)
+static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
 {
-       pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
-       pjsip_sip_uri *sip_ruri;
-
-       if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
-               return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
+       int res;
+       if (rdata->msg_info.msg->body && rdata->msg_info.msg->body->len) {
+               res = ast_sip_is_content_type(
+                       &rdata->msg_info.msg->body->content_type, "text", "plain");
+       } else {
+               res = rdata->msg_info.ctype &&
+                       ast_sip_is_content_type(
+                               &rdata->msg_info.ctype->media, "text", "plain");
        }
 
-       sip_ruri = pjsip_uri_get_uri(ruri);
-       ast_copy_pj_str(exten, &sip_ruri->user, MAX_EXTEN_SIZE);
-
-       if (ast_exists_extension(NULL, context, exten, 1, NULL)) {
-               return PJSIP_SC_OK;
-       }
-       return PJSIP_SC_NOT_FOUND;
+       return res ? PJSIP_SC_OK : PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
 }
 
 /*!
  * \internal
  * \brief Checks to make sure the request has the correct content type.
  *
- * \details This module supports the following media types: "text/plain".
+ * \details This module supports the following media types: "text/\*", "application/\*".
  * Return unsupported otherwise.
  *
  * \param rdata The SIP request
  */
-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)
 {
-       int res;
-       if (rdata->msg_info.msg->body && rdata->msg_info.msg->body->len) {
-               res = ast_sip_is_content_type(
-                       &rdata->msg_info.msg->body->content_type, "text", "plain");
-       } else {
-               res = rdata->msg_info.ctype &&
-                       !pj_strcmp2(&rdata->msg_info.ctype->media.type, "text") &&
-                       !pj_strcmp2(&rdata->msg_info.ctype->media.subtype, "plain");
+       int res = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
+       static const pj_str_t text = { "text", 4};
+       static const pj_str_t application = { "application", 11};
+
+       /* We'll accept any text/ or application/ content type */
+       if (rdata->msg_info.msg->body && rdata->msg_info.msg->body->len
+               && (pj_stricmp(&rdata->msg_info.msg->body->content_type.type, &text) == 0
+                       || pj_stricmp(&rdata->msg_info.msg->body->content_type.type, &application) == 0)) {
+               res = PJSIP_SC_OK;
+       } else if (rdata->msg_info.ctype
+               && (pj_stricmp(&rdata->msg_info.ctype->media.type, &text) == 0
+               || pj_stricmp(&rdata->msg_info.ctype->media.type, &application) == 0)) {
+               res = PJSIP_SC_OK;
        }
 
-       return res ? PJSIP_SC_OK : PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
+       return res;
 }
 
 /*!
@@ -100,9 +111,9 @@ static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
  *
  * \param fromto 'From' or 'To' field containing 'sip:'
  */
-static char* skip_sip(char *fromto)
+static const char *skip_sip(const char *fromto)
 {
-       char *p;
+       const char *p;
 
        /* need to be one past 'sip:' or 'sips:' */
        if (!(p = strstr(fromto, "sip"))) {
@@ -113,56 +124,87 @@ static char* skip_sip(char *fromto)
        if (*p == 's') {
                ++p;
        }
+
        return ++p;
 }
 
 /*!
  * \internal
- * \brief Retrieves an endpoint if specified in the given 'fromto'
+ * \brief Retrieves an endpoint if specified in the given 'to'
  *
- * Expects the given 'fromto' to be in one of the following formats:
+ * Expects the given 'to' to be in one of the following formats:
  *      sip[s]:endpoint[/aor]
- *      sip[s]:endpoint[/uri]
- *      sip[s]:uri <-- will use default outbound endpoint
+ *      sip[s]:endpoint[/uri] - Where uri is: sip[s]:user@domain
+ *      sip[s]:endpoint[@domain]
+ *      sip[s]:unknown_user@domain <-- will use default outbound endpoint
  *
  * If an optional aor is given it will try to find an associated uri
  * to return.  If an optional uri is given then that will be returned,
  * otherwise uri will be NULL.
  *
- * \param fromto 'From' or 'To' field with possible endpoint
+ * \param to 'From' or 'To' field with possible endpoint
  * \param uri Optional uri to return
  */
-static struct ast_sip_endpoint* get_endpoint(char *fromto, char **uri)
+static struct ast_sip_endpoint *get_outbound_endpoint(const char *to, char **uri)
 {
-       char *name, *aor_uri;
-       struct ast_sip_endpoint* endpoint;
-       RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
-       RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
+       char *name;
+       char *aor_uri;
+       struct ast_sip_endpoint *endpoint;
+
+       name = ast_strdupa(skip_sip(to));
 
-       name = skip_sip(fromto);
+       /* attempt to extract the endpoint name */
        if ((aor_uri = strchr(name, '/'))) {
+               /* format was 'endpoint/(aor_name | uri)' */
                *aor_uri++ = '\0';
        } else if ((aor_uri = strchr(name, '@'))) {
-               /* format was endpoint@ */
+               /* format was 'endpoint@domain' - discard the domain */
                *aor_uri = '\0';
-       }
 
-       if (ast_strlen_zero(name) || !(endpoint = ast_sorcery_retrieve_by_id(
-                     ast_sip_get_sorcery(), "endpoint", name))) {
-               /* assume sending to direct uri -
-                  use default outbound endpoint */
-               *uri = ast_strdup(fromto);
+               /*
+                * We may want to match without any user options getting
+                * in the way.
+                */
+               AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(name);
+       }
+
+       /* at this point, if name is not empty then it
+          might be an endpoint, so try to retrieve it */
+       if (ast_strlen_zero(name)
+               || !(endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
+                       name))) {
+               /* an endpoint was not found, so assume sending directly
+                  to a uri and use the default outbound endpoint */
+               *uri = ast_strdup(to);
                return ast_sip_default_outbound_endpoint();
        }
 
-       *uri = aor_uri;
-       if (*uri) {
-               if ((aor = ast_sip_location_retrieve_aor(*uri)) &&
-                       (contact = ast_sip_location_retrieve_first_aor_contact(aor))) {
-                       *uri = (char*)contact->uri;
+       if (ast_strlen_zero(aor_uri)) {
+               *uri = NULL;
+       } else {
+               struct ast_sip_aor *aor;
+               struct ast_sip_contact *contact = NULL;
+               char *end;
+
+               /* Trim off any stray angle bracket that shouldn't be here */
+               end = strchr(aor_uri, '>');
+               if (end) {
+                       *end = '\0';
+               }
+
+               /*
+                * if what's in 'uri' is a retrievable aor use the uri on it
+                * instead, otherwise assume what's there is already a uri
+                */
+               aor = ast_sip_location_retrieve_aor(aor_uri);
+               if (aor && (contact = ast_sip_location_retrieve_first_aor_contact(aor))) {
+                       aor_uri = (char *) contact->uri;
                }
-               /* need to copy because contact-uri might go away*/
-               *uri = ast_strdup(*uri);
+               /* need to copy because underlying uri goes away */
+               *uri = ast_strdup(aor_uri);
+
+               ao2_cleanup(contact);
+               ao2_cleanup(aor);
        }
 
        return endpoint;
@@ -170,110 +212,89 @@ static struct ast_sip_endpoint* get_endpoint(char *fromto, char **uri)
 
 /*!
  * \internal
- * \brief Updates fields in an outgoing 'From' header.
+ * \brief Overwrite fields in the outbound 'To' header
+ *
+ * Updates display name in an outgoing To header.
  *
- * \param tdata The outgoing message data structure
- * \param from Info to potentially copy into the 'From' header
+ * \param tdata the outbound message data structure
+ * \param to info to copy into the header
+ */
+static void update_to(pjsip_tx_data *tdata, char *to)
+{
+       pjsip_name_addr *parsed_name_addr;
+
+       parsed_name_addr = (pjsip_name_addr *) pjsip_parse_uri(tdata->pool, to, strlen(to),
+               PJSIP_PARSE_URI_AS_NAMEADDR);
+       if (parsed_name_addr) {
+               if (pj_strlen(&parsed_name_addr->display)) {
+                       pjsip_name_addr *name_addr =
+                               (pjsip_name_addr *) PJSIP_MSG_TO_HDR(tdata->msg)->uri;
+
+                       pj_strdup(tdata->pool, &name_addr->display, &parsed_name_addr->display);
+               }
+       }
+}
+
+/*!
+ * \internal
+ * \brief Overwrite fields in the outbound 'From' header
+ *
+ * The outbound 'From' header is created/added in ast_sip_create_request with
+ * default data.  If available that data may be info specified in the 'from_user'
+ * and 'from_domain' options found on the endpoint.  That information will be
+ * overwritten with data in the given 'from' parameter.
+ *
+ * \param tdata the outbound message data structure
+ * \param from info to copy into the header
  */
 static void update_from(pjsip_tx_data *tdata, char *from)
 {
-       pjsip_name_addr *from_name_addr;
-       pjsip_sip_uri *from_uri;
-       pjsip_uri *parsed;
-       RAII_VAR(char *, uri, NULL, ast_free);
-       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
+       pjsip_name_addr *name_addr;
+       pjsip_sip_uri *uri;
+       pjsip_name_addr *parsed_name_addr;
 
        if (ast_strlen_zero(from)) {
                return;
        }
 
-       if (!(endpoint = get_endpoint(from, &uri))) {
-               return;
-       }
+       name_addr = (pjsip_name_addr *) PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
+       uri = pjsip_uri_get_uri(name_addr);
 
-       if (ast_strlen_zero(uri)) {
-               /* if no aor/uri was specified get one from the endpoint */
-               RAII_VAR(struct ast_sip_contact *, contact,
-                        ast_sip_location_retrieve_contact_from_aor_list(
-                                endpoint->aors), ao2_cleanup);
+       parsed_name_addr = (pjsip_name_addr *) pjsip_parse_uri(tdata->pool, from,
+               strlen(from), PJSIP_PARSE_URI_AS_NAMEADDR);
+       if (parsed_name_addr) {
+               pjsip_sip_uri *parsed_uri;
 
-               if (!contact || ast_strlen_zero(contact->uri)) {
-                       ast_log(LOG_WARNING, "No contact found for endpoint %s\n",
-                               ast_sorcery_object_get_id(endpoint));
+               if (!PJSIP_URI_SCHEME_IS_SIP(parsed_name_addr->uri)
+                               && !PJSIP_URI_SCHEME_IS_SIPS(parsed_name_addr->uri)) {
+                       ast_log(LOG_WARNING, "From address '%s' is not a valid SIP/SIPS URI\n", from);
                        return;
                }
 
-               if (uri) {
-                       ast_free(uri);
-               }
-               uri = ast_strdup(contact->uri);
-       }
-
-       /* get current 'from' hdr & uri - going to overwrite some fields */
-       from_name_addr = (pjsip_name_addr *)PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
-       from_uri = pjsip_uri_get_uri(from_name_addr);
+               parsed_uri = pjsip_uri_get_uri(parsed_name_addr->uri);
 
-       /* check to see if uri is in 'name <sip:user@domain>' format */
-       if ((parsed = pjsip_parse_uri(tdata->pool, uri, strlen(uri), PJSIP_PARSE_URI_AS_NAMEADDR))) {
-               pjsip_name_addr *name_addr = (pjsip_name_addr *)parsed;
-               pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(name_addr->uri);
+               if (pj_strlen(&parsed_name_addr->display)) {
+                       pj_strdup(tdata->pool, &name_addr->display, &parsed_name_addr->display);
+               }
 
-               pj_strdup(tdata->pool, &from_name_addr->display, &name_addr->display);
-               pj_strdup(tdata->pool, &from_uri->user, &sip_uri->user);
-               pj_strdup(tdata->pool, &from_uri->host, &sip_uri->host);
-               from_uri->port = sip_uri->port;
+               pj_strdup(tdata->pool, &uri->user, &parsed_uri->user);
+               pj_strdup(tdata->pool, &uri->host, &parsed_uri->host);
+               uri->port = parsed_uri->port;
        } else {
                /* assume it is 'user[@domain]' format */
-               char *domain = strchr(uri, '@');
-               if (domain) {
-                       *domain++ = '\0';
-                       pj_strdup2(tdata->pool, &from_uri->host, domain);
-               }
-               pj_strdup2(tdata->pool, &from_uri->user, uri);
-       }
-}
+               char *domain = strchr(from, '@');
 
-static char *scheme_sip_to_pjsip(pjsip_rx_data *rdata, char *buf, unsigned int size)
-{
-       char *res = buf;
-       pjsip_name_addr *name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
-       pjsip_sip_uri *sip_uri = pjsip_uri_get_uri(name_addr->uri);
+               if (domain) {
+                       pj_str_t pj_from;
 
-       const pj_str_t *scheme = pjsip_uri_get_scheme(rdata->msg_info.to->uri);
-       size_t size_scheme = pj_strlen(scheme);
-       size_t size_user = pj_strlen(&sip_uri->user);
-       size_t size_host = pj_strlen(&sip_uri->host);
+                       pj_strset3(&pj_from, from, domain);
+                       pj_strdup(tdata->pool, &uri->user, &pj_from);
 
-       /* 5 = count of 'p' 'j' ':' '@' '\0' */
-       if (size < size_scheme + size_user + size_host + 5) {
-               /* won't fit */
-               ast_log(LOG_WARNING, "Unable to handle MESSAGE- incoming uri "
-                       "too large for given buffer\n");
-               return NULL;
+                       pj_strdup2(tdata->pool, &uri->host, domain + 1);
+               } else {
+                       pj_strdup2(tdata->pool, &uri->user, from);
+               }
        }
-
-       *buf++ = 'p';
-       *buf++ = 'j';
-
-       memcpy(buf, pj_strbuf(scheme), size_scheme);
-       buf += size_scheme;
-       *buf++ = ':';
-
-       memcpy(buf, pj_strbuf(&sip_uri->user), size_user);
-       buf += size_user;
-       *buf++ = '@';
-
-       memcpy(buf, pj_strbuf(&sip_uri->host), size_host);
-       buf += size_host;
-       *buf = '\0';
-
-       return res;
-}
-
-static char *scheme_pjsip_to_sip(const char *uri)
-{
-       ast_assert(!strncmp(uri, "pjsip", 5));
-       return ast_strdup(uri + 2);
 }
 
 /*!
@@ -330,28 +351,26 @@ static enum pjsip_status_code vars_to_headers(const struct ast_msg *msg, pjsip_t
        const char *name;
        const char *value;
        int max_forwards;
+       struct ast_msg_var_iterator *iter;
 
-       RAII_VAR(struct ast_msg_var_iterator *, i, ast_msg_var_iterator_init(msg), ast_msg_var_iterator_destroy);
-       while (ast_msg_var_iterator_next(msg, i, &name, &value)) {
+       for (iter = ast_msg_var_iterator_init(msg);
+               ast_msg_var_iterator_next(msg, iter, &name, &value);
+               ast_msg_var_unref_current(iter)) {
                if (!strcasecmp(name, "Max-Forwards")) {
                        /* Decrement Max-Forwards for SIP loop prevention. */
                        if (sscanf(value, "%30d", &max_forwards) != 1 || --max_forwards == 0) {
+                               ast_msg_var_iterator_destroy(iter);
                                ast_log(LOG_NOTICE, "MESSAGE(Max-Forwards) reached zero.  MESSAGE not sent.\n");
                                return -1;
                        }
-                       sprintf((char*)value, "%d", max_forwards);
+                       sprintf((char *) value, "%d", max_forwards);
                        ast_sip_add_header(tdata, name, value);
-               } else if (!strcasecmp(name, "To")) {
-                       char *to = scheme_pjsip_to_sip(value);
-                       if (to) {
-                               ast_sip_add_header(tdata, name, to);
-                               ast_free(to);
-                       }
                } else if (!is_msg_var_blocked(name)) {
                        ast_sip_add_header(tdata, name, value);
                }
-               ast_msg_var_unref_current(i);
        }
+       ast_msg_var_iterator_destroy(iter);
+
        return PJSIP_SC_OK;
 }
 
@@ -422,6 +441,46 @@ static int print_body(pjsip_rx_data *rdata, char *buf, int len)
 
 /*!
  * \internal
+ * \brief Converts a 'sip:' uri to a 'pjsip:' so it can be found by
+ * the message tech.
+ *
+ * \param buf uri to insert 'pjsip' into
+ * \param size length of the uri in buf
+ * \param capacity total size of buf
+ */
+static char *sip_to_pjsip(char *buf, int size, int capacity)
+{
+       int count;
+       const char *scheme;
+       char *res = buf;
+
+       /* remove any wrapping brackets */
+       if (*buf == '<') {
+               ++buf;
+               --size;
+       }
+
+       scheme = strncmp(buf, "sip", 3) ? "pjsip:" : "pj";
+       count = strlen(scheme);
+       if (count + size >= capacity) {
+               ast_log(LOG_WARNING, "Unable to handle MESSAGE- incoming uri "
+                       "too large for given buffer\n");
+               return NULL;
+       }
+
+       memmove(res + count, buf, size);
+       memcpy(res, scheme, count);
+
+       buf += size - 1;
+       if (*buf == '>') {
+               *buf = '\0';
+       }
+
+       return res;
+}
+
+/*!
+ * \internal
  * \brief Converts a pjsip_rx_data structure to an ast_msg structure.
  *
  * \details Attempts to fill in as much information as possible into the given
@@ -432,52 +491,90 @@ static int print_body(pjsip_rx_data *rdata, char *buf, int len)
  */
 static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct ast_msg *msg)
 {
-
-#define CHECK_RES(z_) do { if (z_) { ast_msg_destroy(msg); \
-               return PJSIP_SC_INTERNAL_SERVER_ERROR; } } while (0)
-
-       int size;
-       char buf[MAX_BODY_SIZE];
+       RAII_VAR(struct ast_sip_endpoint *, endpt, NULL, ao2_cleanup);
+       pjsip_uri *ruri = rdata->msg_info.msg->line.req.uri;
+       pjsip_sip_uri *sip_ruri;
        pjsip_name_addr *name_addr;
+       char buf[MAX_BODY_SIZE];
        const char *field;
-       pjsip_status_code code;
-       struct ast_sip_endpoint *endpt = ast_pjsip_rdata_get_endpoint(rdata);
+       const char *context;
+       char exten[AST_MAX_EXTENSION];
+       int res = 0;
+       int size;
 
-       /* make sure there is an appropriate context and extension*/
-       if ((code = get_destination(rdata, endpt->context, buf)) != PJSIP_SC_OK) {
-               return code;
+       if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
+               return PJSIP_SC_UNSUPPORTED_URI_SCHEME;
        }
 
-       CHECK_RES(ast_msg_set_context(msg, "%s", endpt->context));
-       CHECK_RES(ast_msg_set_exten(msg, "%s", buf));
+       sip_ruri = pjsip_uri_get_uri(ruri);
+       ast_copy_pj_str(exten, &sip_ruri->user, AST_MAX_EXTENSION);
+
+       /*
+        * We may want to match in the dialplan without any user
+        * options getting in the way.
+        */
+       AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten);
+
+       endpt = ast_pjsip_rdata_get_endpoint(rdata);
+       ast_assert(endpt != NULL);
+
+       context = S_OR(endpt->message_context, endpt->context);
+       res |= ast_msg_set_context(msg, "%s", context);
+       res |= ast_msg_set_exten(msg, "%s", exten);
 
        /* to header */
-       CHECK_RES(ast_msg_set_to(msg, "%s", scheme_sip_to_pjsip(rdata, buf, sizeof(buf))));
+       name_addr = (pjsip_name_addr *)rdata->msg_info.to->uri;
+       size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
+       if (size <= 0) {
+               return PJSIP_SC_INTERNAL_SERVER_ERROR;
+       }
+       buf[size] = '\0';
+       res |= ast_msg_set_to(msg, "%s", sip_to_pjsip(buf, ++size, sizeof(buf) - 1));
 
        /* from header */
        name_addr = (pjsip_name_addr *)rdata->msg_info.from->uri;
-       if ((size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf)-1)) > 0) {
-               buf[size] = '\0';
-               CHECK_RES(ast_msg_set_from(msg, "%s", buf));
-       }
-
-       /* receive address */
-       field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf)-1, 1);
-       CHECK_RES(ast_msg_set_var(msg, "PJSIP_RECVADDR", field));
+       size = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, name_addr, buf, sizeof(buf) - 1);
+       if (size <= 0) {
+               return PJSIP_SC_INTERNAL_SERVER_ERROR;
+       }
+       buf[size] = '\0';
+       res |= ast_msg_set_from(msg, "%s", buf);
+
+       field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf) - 1, 3);
+       res |= ast_msg_set_var(msg, "PJSIP_RECVADDR", field);
+
+       switch (rdata->tp_info.transport->key.type) {
+       case PJSIP_TRANSPORT_UDP:
+       case PJSIP_TRANSPORT_UDP6:
+               field = "udp";
+               break;
+       case PJSIP_TRANSPORT_TCP:
+       case PJSIP_TRANSPORT_TCP6:
+               field = "tcp";
+               break;
+       case PJSIP_TRANSPORT_TLS:
+       case PJSIP_TRANSPORT_TLS6:
+               field = "tls";
+               break;
+       default:
+               field = rdata->tp_info.transport->type_name;
+       }
+       ast_msg_set_var(msg, "PJSIP_TRANSPORT", field);
 
-       /* body */
        if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
-               CHECK_RES(ast_msg_set_body(msg, "%s", buf));
+               res |= ast_msg_set_body(msg, "%s", buf);
        }
 
        /* endpoint name */
+       res |= ast_msg_set_tech(msg, "%s", "PJSIP");
+       res |= ast_msg_set_endpoint(msg, "%s", ast_sorcery_object_get_id(endpt));
        if (endpt->id.self.name.valid) {
-               CHECK_RES(ast_msg_set_var(msg, "PJSIP_PEERNAME", endpt->id.self.name.str));
+               res |= ast_msg_set_var(msg, "PJSIP_ENDPOINT", endpt->id.self.name.str);
        }
 
-       CHECK_RES(headers_to_vars(rdata, msg));
+       res |= headers_to_vars(rdata, msg);
 
-       return PJSIP_SC_OK;
+       return !res ? PJSIP_SC_OK : PJSIP_SC_INTERNAL_SERVER_ERROR;
 }
 
 struct msg_data {
@@ -496,9 +593,9 @@ static void msg_data_destroy(void *obj)
        ast_msg_destroy(mdata->msg);
 }
 
-static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
+static struct msg_data *msg_data_create(const struct ast_msg *msg, const char *to, const char *from)
 {
-       char *tag;
+       char *uri_params;
        struct msg_data *mdata = ao2_alloc(sizeof(*mdata), msg_data_destroy);
 
        if (!mdata) {
@@ -506,16 +603,32 @@ static struct msg_data* msg_data_create(const struct ast_msg *msg, const char *t
        }
 
        /* typecast to suppress const warning */
-       mdata->msg = ast_msg_ref((struct ast_msg*)msg);
+       mdata->msg = ast_msg_ref((struct ast_msg *) msg);
 
-       mdata->to = scheme_pjsip_to_sip(to);
-       mdata->from = ast_strdup(from);
+       /* To starts with 'pjsip:' which needs to be removed. */
+       if (!(to = strchr(to, ':'))) {
+               ao2_ref(mdata, -1);
+               return NULL;
+       }
+       ++to;/* Now skip the ':' */
 
-       /* sometimes from can still contain the tag at this point, so remove it */
-       if ((tag = strchr(mdata->from, ';'))) {
-               *tag = '\0';
+       /* Make sure we start with sip: */
+       mdata->to = ast_begins_with(to, "sip:") ? ast_strdup(to) : ast_strdup(to - 4);
+       mdata->from = ast_strdup(from);
+       if (!mdata->to || !mdata->from) {
+               ao2_ref(mdata, -1);
+               return NULL;
        }
 
+       /*
+        * Sometimes from URI can contain URI parameters, so remove them.
+        *
+        * sip:user;user-options@domain;uri-parameters
+        */
+       uri_params = strchr(mdata->from, '@');
+       if (uri_params && (uri_params = strchr(mdata->from, ';'))) {
+               *uri_params = '\0';
+       }
        return mdata;
 }
 
@@ -531,30 +644,36 @@ static int msg_send(void *data)
 
        pjsip_tx_data *tdata;
        RAII_VAR(char *, uri, NULL, ast_free);
-       RAII_VAR(struct ast_sip_endpoint *, endpoint, get_endpoint(
-                        mdata->to, &uri), ao2_cleanup);
+       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
 
+       endpoint = get_outbound_endpoint(mdata->to, &uri);
        if (!endpoint) {
-               ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not find endpoint and "
-                       "no default outbound endpoint configured\n");
+               ast_log(LOG_ERROR,
+                       "PJSIP MESSAGE - Could not find endpoint '%s' and no default outbound endpoint configured\n",
+                       mdata->to);
                return -1;
        }
 
-       if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, &tdata)) {
-               ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not create request\n");
+       if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
+               ast_log(LOG_WARNING, "PJSIP MESSAGE - Could not create request\n");
                return -1;
        }
 
+       update_to(tdata, mdata->to);
+       update_from(tdata, mdata->from);
+
        if (ast_sip_add_body(tdata, &body)) {
                pjsip_tx_data_dec_ref(tdata);
                ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not add body to request\n");
                return -1;
        }
 
-       update_from(tdata, mdata->from);
        vars_to_headers(mdata->msg, tdata);
 
-       if (ast_sip_send_request(tdata, NULL, endpoint)) {
+       ast_debug(1, "Sending message to '%s' (via endpoint %s) from '%s'\n",
+               mdata->to, ast_sorcery_object_get_id(endpoint), mdata->from);
+
+       if (ast_sip_send_request(tdata, NULL, endpoint, NULL, NULL)) {
                ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not send request\n");
                return -1;
        }
@@ -572,8 +691,8 @@ static int sip_msg_send(const struct ast_msg *msg, const char *to, const char *f
        }
 
        if (!(mdata = msg_data_create(msg, to, from)) ||
-           ast_sip_push_task(NULL, msg_send, mdata)) {
-               ao2_ref(mdata, -1);
+           ast_sip_push_task(message_serializer, msg_send, mdata)) {
+               ao2_cleanup(mdata);
                return -1;
        }
        return 0;
@@ -589,11 +708,8 @@ static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code co
 {
        pjsip_tx_data *tdata;
        pj_status_t status;
-       pjsip_response_addr res_addr;
-
-       pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
 
-       status = pjsip_endpt_create_response(endpt, rdata, code, NULL, &tdata);
+       status = ast_sip_create_response(rdata, code, NULL, &tdata);
        if (status != PJ_SUCCESS) {
                ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
                return status;
@@ -602,13 +718,11 @@ static pj_status_t send_response(pjsip_rx_data *rdata, enum pjsip_status_code co
        if (dlg && tsx) {
                status = pjsip_dlg_send_response(dlg, tsx, tdata);
        } else {
-               /* Get where to send request. */
-               status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
-               if (status != PJ_SUCCESS) {
-                       ast_log(LOG_ERROR, "Unable to get response address (%d)\n", status);
-                       return status;
-               }
-               status = pjsip_endpt_send_response(endpt, &res_addr, tdata, NULL, NULL);
+               struct ast_sip_endpoint *endpoint;
+
+               endpoint = ast_pjsip_rdata_get_endpoint(rdata);
+               status = ast_sip_send_stateful_response(rdata, tdata, endpoint);
+               ao2_cleanup(endpoint);
        }
 
        if (status != PJ_SUCCESS) {
@@ -628,57 +742,140 @@ static pj_bool_t module_on_rx_request(pjsip_rx_data *rdata)
                return PJ_FALSE;
        }
 
+       code = check_content_type(rdata);
+       if (code != PJSIP_SC_OK) {
+               send_response(rdata, code, NULL, NULL);
+               return PJ_TRUE;
+       }
+
        msg = ast_msg_alloc();
        if (!msg) {
                send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL);
                return PJ_TRUE;
        }
 
-       if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
+       code = rx_data_to_ast_msg(rdata, msg);
+       if (code != PJSIP_SC_OK) {
                send_response(rdata, code, NULL, NULL);
+               ast_msg_destroy(msg);
                return PJ_TRUE;
        }
 
-       if ((code = rx_data_to_ast_msg(rdata, msg)) == PJSIP_SC_OK) {
-               /* send it to the dialplan */
+       if (!ast_msg_has_destination(msg)) {
+               ast_debug(1, "MESSAGE request received, but no handler wanted it\n");
+               send_response(rdata, PJSIP_SC_NOT_FOUND, NULL, NULL);
+               ast_msg_destroy(msg);
+               return PJ_TRUE;
+       }
+
+       /* Send it to the messaging core.
+        *
+        * If we are unable to send a response, the most likely reason is that we
+        * are handling a retransmission of an incoming MESSAGE and were unable to
+        * create a transaction due to a duplicate key. If we are unable to send
+        * a response, we should not queue the message to the dialplan
+        */
+       if (!send_response(rdata, PJSIP_SC_ACCEPTED, NULL, NULL)) {
                ast_msg_queue(msg);
-               code = PJSIP_SC_ACCEPTED;
        }
 
-       send_response(rdata, code, NULL, NULL);
        return PJ_TRUE;
 }
 
 static int incoming_in_dialog_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
 {
-       char buf[MAX_BODY_SIZE];
        enum pjsip_status_code code;
-       struct ast_frame f;
-
+       int rc;
        pjsip_dialog *dlg = session->inv_session->dlg;
        pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
+       struct ast_msg_data *msg;
+       struct ast_party_caller *caller;
+       pjsip_name_addr *name_addr;
+       size_t from_len;
+       size_t to_len;
+       struct ast_msg_data_attribute attrs[4];
+       int pos = 0;
+       int body_pos;
+
+       if (!session->channel) {
+               send_response(rdata, PJSIP_SC_NOT_FOUND, dlg, tsx);
+               return 0;
+       }
 
-       if ((code = check_content_type(rdata)) != PJSIP_SC_OK) {
+       code = check_content_type_in_dialog(rdata);
+       if (code != PJSIP_SC_OK) {
                send_response(rdata, code, dlg, tsx);
                return 0;
        }
 
-       if (print_body(rdata, buf, sizeof(buf)-1) < 1) {
-               /* invalid body size */
+       caller = ast_channel_caller(session->channel);
+
+       name_addr = (pjsip_name_addr *) rdata->msg_info.from->uri;
+       from_len = pj_strlen(&name_addr->display);
+       if (from_len) {
+               attrs[pos].type = AST_MSG_DATA_ATTR_FROM;
+               from_len++;
+               attrs[pos].value = ast_alloca(from_len);
+               ast_copy_pj_str(attrs[pos].value, &name_addr->display, from_len);
+               pos++;
+       } else if (caller->id.name.valid && !ast_strlen_zero(caller->id.name.str)) {
+               attrs[pos].type = AST_MSG_DATA_ATTR_FROM;
+               attrs[pos].value = caller->id.name.str;
+               pos++;
+       }
+
+       name_addr = (pjsip_name_addr *) rdata->msg_info.to->uri;
+       to_len = pj_strlen(&name_addr->display);
+       if (to_len) {
+               attrs[pos].type = AST_MSG_DATA_ATTR_TO;
+               to_len++;
+               attrs[pos].value = ast_alloca(to_len);
+               ast_copy_pj_str(attrs[pos].value, &name_addr->display, to_len);
+               pos++;
+       }
+
+       attrs[pos].type = AST_MSG_DATA_ATTR_CONTENT_TYPE;
+       attrs[pos].value = ast_alloca(rdata->msg_info.msg->body->content_type.type.slen
+               + rdata->msg_info.msg->body->content_type.subtype.slen + 2);
+       sprintf(attrs[pos].value, "%.*s/%.*s",
+               (int)rdata->msg_info.msg->body->content_type.type.slen,
+               rdata->msg_info.msg->body->content_type.type.ptr,
+               (int)rdata->msg_info.msg->body->content_type.subtype.slen,
+               rdata->msg_info.msg->body->content_type.subtype.ptr);
+       pos++;
+
+       body_pos = pos;
+       attrs[pos].type = AST_MSG_DATA_ATTR_BODY;
+       attrs[pos].value = ast_malloc(rdata->msg_info.msg->body->len + 1);
+       if (!attrs[pos].value) {
+               send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
                return 0;
        }
+       ast_copy_string(attrs[pos].value, rdata->msg_info.msg->body->data, rdata->msg_info.msg->body->len + 1);
+       pos++;
 
-       ast_debug(3, "Received in dialog SIP message\n");
+       msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_IN_DIALOG, attrs, pos);
+       if (!msg) {
+               ast_free(attrs[body_pos].value);
+               send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
+               return 0;
+       }
 
-       memset(&f, 0, sizeof(f));
-       f.frametype = AST_FRAME_TEXT;
-       f.subclass.integer = 0;
-       f.offset = 0;
-       f.data.ptr = buf;
-       f.datalen = strlen(buf) + 1;
-       ast_queue_frame(session->channel, &f);
+       ast_debug(1, "Received in-dialog MESSAGE from '%s:%s': %s %s\n",
+               ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
+               ast_channel_name(session->channel),
+               ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO),
+               ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY));
+
+       rc = ast_msg_data_queue_frame(session->channel, msg);
+       ast_free(attrs[body_pos].value);
+       ast_free(msg);
+       if (rc != 0) {
+               send_response(rdata, PJSIP_SC_INTERNAL_SERVER_ERROR, dlg, tsx);
+       } else {
+               send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
+       }
 
-       send_response(rdata, PJSIP_SC_ACCEPTED, dlg, tsx);
        return 0;
 }
 
@@ -713,6 +910,13 @@ static int load_module(void)
                return AST_MODULE_LOAD_DECLINE;
        }
 
+       message_serializer = ast_sip_create_serializer("pjsip/messaging");
+       if (!message_serializer) {
+               ast_sip_unregister_service(&messaging_module);
+               ast_msg_tech_unregister(&msg_tech);
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
        ast_sip_session_register_supplement(&messaging_supplement);
        return AST_MODULE_LOAD_SUCCESS;
 }
@@ -722,11 +926,14 @@ static int unload_module(void)
        ast_sip_session_unregister_supplement(&messaging_supplement);
        ast_msg_tech_unregister(&msg_tech);
        ast_sip_unregister_service(&messaging_module);
+       ast_taskprocessor_unreference(message_serializer);
        return 0;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Support",
-               .load = load_module,
-               .unload = unload_module,
-               .load_pri = AST_MODPRI_APP_DEPEND,
-              );
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_APP_DEPEND,
+       .requires = "res_pjsip,res_pjsip_session",
+);