Removing registrar_expire from basic-pbx config
[asterisk/asterisk.git] / res / res_pjsip_messaging.c
index 7532e39..0e10a8f 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;
@@ -78,6 +77,35 @@ static enum pjsip_status_code check_content_type(const pjsip_rx_data *rdata)
 
 /*!
  * \internal
+ * \brief Checks to make sure the request has the correct content type.
+ *
+ * \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_in_dialog(const pjsip_rx_data *rdata)
+{
+       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;
+}
+
+/*!
+ * \internal
  * \brief Puts pointer past 'sip[s]:' string that should be at the
  * front of the given 'fromto' parameter
  *
@@ -106,8 +134,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 +145,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 +221,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 +249,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 +283,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 +509,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 +540,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 +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) {
@@ -518,22 +603,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;
 }
@@ -550,17 +644,18 @@ 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;
        }
 
        if (ast_sip_create_request("MESSAGE", NULL, endpoint, uri, NULL, &tdata)) {
-               ast_log(LOG_ERROR, "PJSIP MESSAGE - Could not create request\n");
+               ast_log(LOG_WARNING, "PJSIP MESSAGE - Could not create request\n");
                return -1;
        }
 
@@ -597,7 +692,7 @@ 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(message_serializer, msg_send, mdata)) {
-               ao2_ref(mdata, -1);
+               ao2_cleanup(mdata);
                return -1;
        }
        return 0;
@@ -689,39 +784,98 @@ 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)
 {
-       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 */
-               send_response(rdata, PJSIP_SC_REQUEST_ENTITY_TOO_LARGE, dlg, tsx);
+       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++;
+
+       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;
        }
 
-       ast_debug(3, "Received in dialog SIP message\n");
+       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));
 
-       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);
+       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;
 }
 
@@ -739,8 +893,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;
        }
@@ -783,4 +935,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",
 );