res_pjsip_session: properly handle SDP from a forked call with early media
[asterisk/asterisk.git] / res / res_pjsip_messaging.c
index f72f3f0..cbaed83 100644 (file)
 /*** MODULEINFO
        <depend>pjproject</depend>
        <depend>res_pjsip</depend>
-       <depend>res_pjsip_session</depend>
        <support_level>core</support_level>
  ***/
 
 /*** DOCUMENTATION
-       <info name="PJSIPMessageFromInfo" language="en_US" tech="PJSIP">
+       <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="PJSIPMessageToInfo" language="en_US" tech="PJSIP">
+       <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>
@@ -69,8 +68,8 @@ static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
                        &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");
+                       ast_sip_is_content_type(
+                               &rdata->msg_info.ctype->media, "text", "plain");
        }
 
        return res ? PJSIP_SC_OK : PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
@@ -106,8 +105,9 @@ static const char *skip_sip(const char *fromto)
  *
  * 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,
@@ -116,50 +116,66 @@ static const char *skip_sip(const char *fromto)
  * \param to 'From' or 'To' field with possible endpoint
  * \param uri Optional uri to return
  */
-static struct ast_sip_endpoint* get_outbound_endpoint(
-       const char *to, 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));
 
        /* attempt to extract the endpoint name */
        if ((aor_uri = strchr(name, '/'))) {
-               /* format was 'endpoint/' */
+               /* format was 'endpoint/(aor_name | uri)' */
                *aor_uri++ = '\0';
        } else if ((aor_uri = strchr(name, '@'))) {
-               /* format was 'endpoint@' - don't use the rest */
+               /* format was 'endpoint@domain' - discard the domain */
                *aor_uri = '\0';
+
+               /*
+                * 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))) {
+       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) {
-               char *end = strchr(*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';
+                       *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*/
-               if ((aor = ast_sip_location_retrieve_aor(*uri)) &&
-                       (contact = ast_sip_location_retrieve_first_aor_contact(aor))) {
-                       *uri = (char*)contact->uri;
+               /*
+                * 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 underlying uri goes away */
-               *uri = ast_strdup(*uri);
+               *uri = ast_strdup(aor_uri);
+
+               ao2_cleanup(contact);
+               ao2_cleanup(aor);
        }
 
        return endpoint;
@@ -176,16 +192,16 @@ static struct ast_sip_endpoint* get_outbound_endpoint(
  */
 static void update_to(pjsip_tx_data *tdata, char *to)
 {
-       pjsip_name_addr *name_addr = (pjsip_name_addr *)
-               PJSIP_MSG_TO_HDR(tdata->msg)->uri;
-       pjsip_uri *parsed;
+       pjsip_name_addr *parsed_name_addr;
 
-       if ((parsed = pjsip_parse_uri(tdata->pool, to, strlen(to),
-                                     PJSIP_PARSE_URI_AS_NAMEADDR))) {
-               pjsip_name_addr *parsed_name_addr = (pjsip_name_addr *)parsed;
+       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)) {
-                       pj_strdup(tdata->pool, &name_addr->display,
-                                 &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);
                }
        }
 }
@@ -204,22 +220,32 @@ static void update_to(pjsip_tx_data *tdata, char *to)
  */
 static void update_from(pjsip_tx_data *tdata, char *from)
 {
-       pjsip_name_addr *name_addr = (pjsip_name_addr *)
-               PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
-       pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr);
-       pjsip_uri *parsed;
+       pjsip_name_addr *name_addr;
+       pjsip_sip_uri *uri;
+       pjsip_name_addr *parsed_name_addr;
 
        if (ast_strlen_zero(from)) {
                return;
        }
 
-       if ((parsed = pjsip_parse_uri(tdata->pool, from, strlen(from),
-                                     PJSIP_PARSE_URI_AS_NAMEADDR))) {
-               pjsip_name_addr *parsed_name_addr = (pjsip_name_addr *)parsed;
-               pjsip_sip_uri *parsed_uri = pjsip_uri_get_uri(parsed_name_addr->uri);
+       name_addr = (pjsip_name_addr *) PJSIP_MSG_FROM_HDR(tdata->msg)->uri;
+       uri = pjsip_uri_get_uri(name_addr);
+
+       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 (!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;
+               }
+
+               parsed_uri = pjsip_uri_get_uri(parsed_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, &name_addr->display, &parsed_name_addr->display);
                }
 
                pj_strdup(tdata->pool, &uri->user, &parsed_uri->user);
@@ -228,11 +254,17 @@ static void update_from(pjsip_tx_data *tdata, char *from)
        } else {
                /* assume it is 'user[@domain]' format */
                char *domain = strchr(from, '@');
+
                if (domain) {
-                       *domain++ = '\0';
-                       pj_strdup2(tdata->pool, &uri->host, domain);
+                       pj_str_t pj_from;
+
+                       pj_strset3(&pj_from, from, domain);
+                       pj_strdup(tdata->pool, &uri->user, &pj_from);
+
+                       pj_strdup2(tdata->pool, &uri->host, domain + 1);
+               } else {
+                       pj_strdup2(tdata->pool, &uri->user, from);
                }
-               pj_strdup2(tdata->pool, &uri->user, from);
        }
 }
 
@@ -448,6 +480,12 @@ static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct as
        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);
 
@@ -473,9 +511,27 @@ static enum pjsip_status_code rx_data_to_ast_msg(pjsip_rx_data *rdata, struct as
        buf[size] = '\0';
        res |= ast_msg_set_from(msg, "%s", buf);
 
-       field = pj_sockaddr_print(&rdata->pkt_info.src_addr, buf, sizeof(buf) - 1, 1);
+       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);
+
        if (print_body(rdata, buf, sizeof(buf) - 1) > 0) {
                res |= ast_msg_set_body(msg, "%s", buf);
        }
@@ -508,9 +564,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) {
@@ -518,26 +574,31 @@ 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);
 
-       /* starts with 'pjsip:' the 'pj' needs to be removed and maybe even
-          the entire string. */
+       /* To starts with 'pjsip:' which needs to be removed. */
        if (!(to = strchr(to, ':'))) {
                ao2_ref(mdata, -1);
                return NULL;
        }
+       ++to;/* Now skip the ':' */
 
        /* Make sure we start with sip: */
-       mdata->to = ast_begins_with(to, "sip:") ? ast_strdup(++to) : ast_strdup(to - 3);
+       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 can still contain the tag at this point, so remove it */
-       if ((tag = strchr(mdata->from, ';'))) {
-               *tag = '\0';
+       /*
+        * 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;
 }
@@ -554,12 +615,13 @@ 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_outbound_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 '%s' and "
-                       "no default outbound endpoint configured\n", mdata->to);
+               ast_log(LOG_ERROR,
+                       "PJSIP MESSAGE - Could not find endpoint '%s' and no default outbound endpoint configured\n",
+                       mdata->to);
                return -1;
        }
 
@@ -743,8 +805,6 @@ static pjsip_module messaging_module = {
 
 static int load_module(void)
 {
-       CHECK_PJSIP_SESSION_MODULE_LOADED();
-
        if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
                return AST_MODULE_LOAD_DECLINE;
        }
@@ -787,4 +847,5 @@ AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Messaging Suppo
        .load = load_module,
        .unload = unload_module,
        .load_pri = AST_MODPRI_APP_DEPEND,
+       .requires = "res_pjsip,res_pjsip_session",
 );