pjsip: Add 'PJSIP_AOR' and 'PJSIP_CONTACT' dialplan functions.
authorJoshua Colp <jcolp@digium.com>
Mon, 5 Jan 2015 17:53:42 +0000 (17:53 +0000)
committerJoshua Colp <jcolp@digium.com>
Mon, 5 Jan 2015 17:53:42 +0000 (17:53 +0000)
The PJSIP_AOR dialplan function allows inspection of configured AORs including
what contacts are currently bound to them.

The PJSIP_CONTACT dialplan function allows inspection of contacts in existence.
These can include both externally added (by way of registration) or permanent
ones.

ASTERISK-24341
Reported by: xrobau

Review: https://reviewboard.asterisk.org/r/4308/
........

Merged revisions 430179 from http://svn.asterisk.org/svn/asterisk/branches/13

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@430180 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/pjsip/dialplan_functions.c
funcs/func_pjsip_aor.c [new file with mode: 0644]
funcs/func_pjsip_contact.c [new file with mode: 0644]
include/asterisk/res_pjsip.h
include/asterisk/res_pjsip_session.h
res/res_pjsip/location.c
res/res_pjsip_session.c

index 018b1e6..704f6a4 100644 (file)
                        Use the <replaceable>PJSIP_ENDPOINT</replaceable> function to obtain
                        further endpoint related information.</para>
                </enum>
+               <enum name="contact">
+                       <para>R/O The name of the contact associated with this channel.
+                       Use the <replaceable>PJSIP_CONTACT</replaceable> function to obtain
+                       further contact related information. Note this may not be present and if so
+                       is only available on outgoing legs.</para>
+               </enum>
+               <enum name="aor">
+                       <para>R/O The name of the AOR associated with this channel.
+                       Use the <replaceable>PJSIP_AOR</replaceable> function to obtain
+                       further AOR related information. Note this may not be present and if so
+                       is only available on outgoing legs.</para>
+               </enum>
                <enum name="pjsip">
                        <para>R/O Obtain information about the current PJSIP channel and its
                        session.</para>
@@ -671,6 +683,28 @@ static int read_pjsip(void *data)
                        return -1;
                }
                snprintf(func_args->buf, func_args->len, "%s", ast_sorcery_object_get_id(pvt->session->endpoint));
+       } else if (!strcmp(func_args->param, "contact")) {
+               struct ast_sip_channel_pvt *pvt = ast_channel_tech_pvt(func_args->chan);
+
+               if (!pvt) {
+                       ast_log(AST_LOG_WARNING, "Channel %s has no pvt!\n", ast_channel_name(func_args->chan));
+                       return -1;
+               }
+               if (!pvt->session || !pvt->session->contact) {
+                       return 0;
+               }
+               snprintf(func_args->buf, func_args->len, "%s", ast_sorcery_object_get_id(pvt->session->contact));
+       } else if (!strcmp(func_args->param, "aor")) {
+               struct ast_sip_channel_pvt *pvt = ast_channel_tech_pvt(func_args->chan);
+
+               if (!pvt) {
+                       ast_log(AST_LOG_WARNING, "Channel %s has no pvt!\n", ast_channel_name(func_args->chan));
+                       return -1;
+               }
+               if (!pvt->session || !pvt->session->aor) {
+                       return 0;
+               }
+               snprintf(func_args->buf, func_args->len, "%s", ast_sorcery_object_get_id(pvt->session->aor));
        } else if (!strcmp(func_args->param, "pjsip")) {
                func_args->ret = channel_read_pjsip(func_args->chan, func_args->type,
                                                    func_args->field, func_args->buf,
diff --git a/funcs/func_pjsip_aor.c b/funcs/func_pjsip_aor.c
new file mode 100644 (file)
index 0000000..e44ee94
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2015, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Get information about a PJSIP AOR
+ *
+ * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
+ *
+ * \ingroup functions
+ *
+ */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+       <depend>pjproject</depend>
+       <depend>res_pjsip</depend>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <pjsip.h>
+#include <pjlib.h>
+
+#include "asterisk/app.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/res_pjsip.h"
+
+/*** DOCUMENTATION
+       <function name="PJSIP_AOR" language="en_US">
+               <synopsis>
+                       Get information about a PJSIP AOR
+               </synopsis>
+               <syntax>
+                       <parameter name="name" required="true">
+                               <para>The name of the AOR to query.</para>
+                       </parameter>
+                       <parameter name="field" required="true">
+                               <para>The configuration option for the AOR to query for.
+                               Supported options are those fields on the
+                               <replaceable>aor</replaceable> object in
+                               <filename>pjsip.conf</filename>.</para>
+                               <enumlist>
+                                       <configOptionToEnum>
+                                               <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption)"/>
+                                       </configOptionToEnum>
+                               </enumlist>
+                       </parameter>
+               </syntax>
+       </function>
+***/
+
+static int pjsip_aor_function_read(struct ast_channel *chan,
+       const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+       struct ast_sorcery *pjsip_sorcery;
+       char *parsed_data = ast_strdupa(data);
+       RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
+       int res = 0;
+
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(aor_name);
+               AST_APP_ARG(field_name);
+       );
+
+       /* Check for zero arguments */
+       if (ast_strlen_zero(parsed_data)) {
+               ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
+               return -1;
+       }
+
+       AST_STANDARD_APP_ARGS(args, parsed_data);
+
+       if (ast_strlen_zero(args.aor_name)) {
+               ast_log(AST_LOG_ERROR, "Cannot call %s without an AOR name to query\n", cmd);
+               return -1;
+       }
+
+       if (ast_strlen_zero(args.field_name)) {
+               ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
+               return -1;
+       }
+
+       pjsip_sorcery = ast_sip_get_sorcery();
+       if (!pjsip_sorcery) {
+               ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
+               return -1;
+       }
+
+       aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", args.aor_name);
+       if (!aor_obj) {
+               ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s'\n", args.aor_name);
+               return -1;
+       }
+
+       if (!strcmp(args.field_name, "contact")) {
+               /* The multiple fields handler for contact does not provide a list of contact object names, which is what we want, so we
+                * handle contact specifically to provide this.
+                */
+               RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
+               struct ao2_iterator i;
+               struct ast_sip_contact *contact;
+               int first = 1;
+
+               contacts = ast_sip_location_retrieve_aor_contacts(aor_obj);
+               if (!contacts) {
+                       ast_log(LOG_WARNING, "Failed to retrieve contacts for AOR '%s'\n", args.aor_name);
+                       return -1;
+               }
+
+               i = ao2_iterator_init(contacts, 0);
+               while ((contact = ao2_iterator_next(&i))) {
+                       if (!first) {
+                               ast_str_append(buf, len, "%s", ",");
+                       }
+
+                       ast_str_append(buf, len, "%s", ast_sorcery_object_get_id(contact));
+                       first = 0;
+               }
+               ao2_iterator_destroy(&i);
+       } else {
+               struct ast_variable *change_set;
+               struct ast_variable *it_change_set;
+
+               change_set = ast_sorcery_objectset_create(pjsip_sorcery, aor_obj);
+               if (!change_set) {
+                       ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s': change set is NULL\n", args.aor_name);
+                       return -1;
+               }
+
+               for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
+                       if (!strcmp(it_change_set->name, args.field_name)) {
+                               ast_str_set(buf, len, "%s", it_change_set->value);
+                               break;
+                       }
+               }
+
+               if (!it_change_set) {
+                       ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP AOR\n", args.field_name);
+                       res = 1;
+               }
+
+               ast_variables_destroy(change_set);
+       }
+
+       return res;
+}
+
+
+static struct ast_custom_function pjsip_aor_function = {
+       .name = "PJSIP_AOR",
+       .read2 = pjsip_aor_function_read,
+};
+
+static int unload_module(void)
+{
+       return ast_custom_function_unregister(&pjsip_aor_function);
+}
+
+static int load_module(void)
+{
+       return ast_custom_function_register(&pjsip_aor_function);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP AOR");
diff --git a/funcs/func_pjsip_contact.c b/funcs/func_pjsip_contact.c
new file mode 100644 (file)
index 0000000..0f5a204
--- /dev/null
@@ -0,0 +1,209 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2015, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Get information about a PJSIP contact
+ *
+ * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
+ *
+ * \ingroup functions
+ *
+ */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+       <depend>pjproject</depend>
+       <depend>res_pjsip</depend>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <pjsip.h>
+#include <pjlib.h>
+
+#include "asterisk/app.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/res_pjsip.h"
+
+/*** DOCUMENTATION
+       <function name="PJSIP_CONTACT" language="en_US">
+               <synopsis>
+                       Get information about a PJSIP contact
+               </synopsis>
+               <syntax>
+                       <parameter name="name" required="true">
+                               <para>The name of the contact to query.</para>
+                       </parameter>
+                       <parameter name="field" required="true">
+                               <para>The configuration option for the contact to query for.
+                               Supported options are those fields on the
+                               <replaceable>contact</replaceable> object.</para>
+                               <enumlist>
+                                       <configOptionToEnum>
+                                               <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='contact']/configOption)"/>
+                                       </configOptionToEnum>
+                               </enumlist>
+                       </parameter>
+               </syntax>
+       </function>
+***/
+
+static int contact_function_get_permanent(void *obj, void *arg, int flags)
+{
+       const char *id = arg;
+
+       if (!strcmp(ast_sorcery_object_get_id(obj), id)) {
+               return CMP_MATCH | CMP_STOP;
+       }
+
+       return 0;
+}
+
+static int pjsip_contact_function_read(struct ast_channel *chan,
+       const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+       struct ast_sorcery *pjsip_sorcery;
+       char *parsed_data = ast_strdupa(data);
+       char *contact_name;
+       RAII_VAR(struct ast_sip_contact *, contact_obj, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_sip_contact_status *, contact_status, NULL, ao2_cleanup);
+       int res = 0;
+
+       AST_DECLARE_APP_ARGS(args,
+               AST_APP_ARG(contact_name);
+               AST_APP_ARG(field_name);
+       );
+
+       /* Check for zero arguments */
+       if (ast_strlen_zero(parsed_data)) {
+               ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
+               return -1;
+       }
+
+       AST_STANDARD_APP_ARGS(args, parsed_data);
+
+       if (ast_strlen_zero(args.contact_name)) {
+               ast_log(AST_LOG_ERROR, "Cannot call %s without a contact name to query\n", cmd);
+               return -1;
+       }
+
+       if (ast_strlen_zero(args.field_name)) {
+               ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
+               return -1;
+       }
+
+       pjsip_sorcery = ast_sip_get_sorcery();
+       if (!pjsip_sorcery) {
+               ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
+               return -1;
+       }
+
+       /* Determine if this is a permanent contact or a normal contact */
+       if ((contact_name = strstr(args.contact_name, "@@"))) {
+               size_t aor_name_len = contact_name - args.contact_name;
+               char aor_name[aor_name_len + 1];
+               RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
+
+               /* Grab only the AOR name so we can retrieve the AOR which will give us the contact */
+               strncpy(aor_name, args.contact_name, aor_name_len);
+               aor_name[aor_name_len] = '\0';
+
+               aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", aor_name);
+               if (!aor_obj) {
+                       ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
+                       return -1;
+               }
+
+               contact_obj = ao2_callback(aor_obj->permanent_contacts, 0, contact_function_get_permanent, args.contact_name);
+       } else {
+               contact_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "contact", args.contact_name);
+       }
+
+       if (!contact_obj) {
+               ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
+               return -1;
+       }
+
+       contact_status = ast_sorcery_retrieve_by_id(pjsip_sorcery, CONTACT_STATUS, ast_sorcery_object_get_id(contact_obj));
+
+       if (!strcmp(args.field_name, "status")) {
+               if (!contact_status) {
+                       ast_str_set(buf, len, "%s", "Unknown");
+               } else if (contact_status->status == UNAVAILABLE) {
+                       ast_str_set(buf, len, "%s", "Unreachable");
+               } else if (contact_status->status == AVAILABLE) {
+                       ast_str_set(buf, len, "%s", "Reachable");
+               }
+       } else if (!strcmp(args.field_name, "rtt")) {
+               if (!contact_status) {
+                       ast_str_set(buf, len, "%s", "N/A");
+               } else {
+                       ast_str_set(buf, len, "%" PRId64, contact_status->rtt);
+               }
+       } else {
+               struct ast_variable *change_set;
+               struct ast_variable *it_change_set;
+
+               change_set = ast_sorcery_objectset_create(pjsip_sorcery, contact_obj);
+
+               if (!change_set) {
+                       ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s': change set is NULL\n", args.contact_name);
+                       return -1;
+               }
+
+               for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
+                       if (!strcmp(it_change_set->name, args.field_name)) {
+                               ast_str_set(buf, len, "%s", it_change_set->value);
+                               break;
+                       }
+               }
+
+               if (!it_change_set) {
+                       ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP contact\n", args.field_name);
+
+                       res = 1;
+               }
+
+               ast_variables_destroy(change_set);
+       }
+
+       return res;
+}
+
+
+static struct ast_custom_function pjsip_contact_function = {
+       .name = "PJSIP_CONTACT",
+       .read2 = pjsip_contact_function_read,
+};
+
+static int unload_module(void)
+{
+       return ast_custom_function_unregister(&pjsip_contact_function);
+}
+
+static int load_module(void)
+{
+       return ast_custom_function_register(&pjsip_contact_function);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP contact");
index 37830c7..3962b76 100644 (file)
@@ -918,6 +918,16 @@ struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_si
 struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list);
 
 /*!
+ * \brief Retrieve the first bound contact AND the AOR chosen from a list of AORs
+ *
+ * \param aor_list A comma-separated list of AOR names
+ * \param aor The chosen AOR
+ * \param contact The chosen contact
+ */
+ void ast_sip_location_retrieve_contact_and_aor_from_list(const char *aor_list, struct ast_sip_aor **aor,
+       struct ast_sip_contact **contact);
+
+/*!
  * \brief Retrieve a named contact
  *
  * \param contact_name Name of the contact
index 990c9fe..8efecb6 100644 (file)
@@ -107,6 +107,8 @@ struct ast_sip_session {
        char exten[AST_MAX_EXTENSION];
        /*! The endpoint with which Asterisk is communicating */
        struct ast_sip_endpoint *endpoint;
+       /*! The AOR associated with this session */
+       struct ast_sip_aor *aor;
        /*! The contact associated with this session */
        struct ast_sip_contact *contact;
        /*! The PJSIP details of the session, which includes the dialog */
index d036ffa..6ffb8fe 100644 (file)
@@ -144,30 +144,46 @@ struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_si
        return contacts;
 }
 
-struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
+void ast_sip_location_retrieve_contact_and_aor_from_list(const char *aor_list, struct ast_sip_aor **aor,
+       struct ast_sip_contact **contact)
 {
        char *aor_name;
        char *rest;
-       struct ast_sip_contact *contact = NULL;
 
        /* If the location is still empty we have nowhere to go */
        if (ast_strlen_zero(aor_list) || !(rest = ast_strdupa(aor_list))) {
                ast_log(LOG_WARNING, "Unable to determine contacts from empty aor list\n");
-               return NULL;
+               return;
        }
 
+       *aor = NULL;
+       *contact = NULL;
+
        while ((aor_name = strsep(&rest, ","))) {
-               RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
+               *aor = ast_sip_location_retrieve_aor(aor_name);
 
-               if (!aor) {
+               if (!(*aor)) {
                        continue;
                }
-               contact = ast_sip_location_retrieve_first_aor_contact(aor);
+               *contact = ast_sip_location_retrieve_first_aor_contact(*aor);
                /* If a valid contact is available use its URI for dialing */
-               if (contact) {
+               if (*contact) {
                        break;
                }
+
+               ao2_ref(*aor, -1);
+               *aor = NULL;
        }
+}
+
+struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
+{
+       struct ast_sip_aor *aor;
+       struct ast_sip_contact *contact;
+
+       ast_sip_location_retrieve_contact_and_aor_from_list(aor_list, &aor, &contact);
+
+       ao2_cleanup(aor);
 
        return contact;
 }
index 46973ad..6060977 100644 (file)
@@ -956,6 +956,7 @@ static void session_destructor(void *obj)
        }
        ast_party_id_free(&session->id);
        ao2_cleanup(session->endpoint);
+       ao2_cleanup(session->aor);
        ao2_cleanup(session->contact);
        ao2_cleanup(session->req_caps);
        ao2_cleanup(session->direct_media_cap);
@@ -1213,6 +1214,7 @@ struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint
        struct ast_format_cap *req_caps)
 {
        const char *uri = NULL;
+       RAII_VAR(struct ast_sip_aor *, found_aor, NULL, ao2_cleanup);
        RAII_VAR(struct ast_sip_contact *, found_contact, NULL, ao2_cleanup);
        pjsip_timer_setting timer;
        pjsip_dialog *dlg;
@@ -1223,7 +1225,7 @@ struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint
        if (location || !contact) {
                location = S_OR(location, endpoint->aors);
 
-               found_contact = ast_sip_location_retrieve_contact_from_aor_list(location);
+               ast_sip_location_retrieve_contact_and_aor_from_list(location, &found_aor, &found_contact);
                if (!found_contact || ast_strlen_zero(found_contact->uri)) {
                        uri = location;
                } else {
@@ -1264,6 +1266,7 @@ struct ast_sip_session *ast_sip_session_create_outgoing(struct ast_sip_endpoint
                pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
                return NULL;
        }
+       session->aor = ao2_bump(found_aor);
        ast_party_id_copy(&session->id, &endpoint->id.self);
 
        if (ast_format_cap_count(req_caps)) {