[Asterisk-code-review] pjsip: Add option global/regcontext (asterisk[13])

Daniel Journo asteriskteam at digium.com
Sun Jan 10 16:24:46 CST 2016


Daniel Journo has uploaded a new change for review.

  https://gerrit.asterisk.org/1984

Change subject: pjsip:  Add option global/regcontext
......................................................................

pjsip:  Add option global/regcontext

Added new global option (regcontext) to pjsip. When set, Asterisk will
dynamically create and destroy a NoOp priority 1 extension
for a given endpoint who registers or unregisters with us.

ASTERISK-25670
Reported-by: Daniel Journo

Change-Id: Ib1530c5b45340625805c057f8ff1fb240a43ea62
---
M CHANGES
M configs/samples/pjsip.conf.sample
M include/asterisk/res_pjsip.h
M res/res_pjsip.c
M res/res_pjsip/config_global.c
M res/res_pjsip/pjsip_configuration.c
6 files changed, 63 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/84/1984/1

diff --git a/CHANGES b/CHANGES
index d9150a9..b411ccc 100644
--- a/CHANGES
+++ b/CHANGES
@@ -12,6 +12,13 @@
 --- Functionality changes from Asterisk 13.7.0 to Asterisk 13.8.0 ------------
 ------------------------------------------------------------------------------
 
+res_pjsip
+------------------
+
+ * Added new global option (regcontext) to pjsip. When set, Asterisk will 
+   dynamically create and destroy a NoOp priority 1 extension 
+   for a given endpoint who registers or unregisters with us.
+
 res_pjsip_history
 ------------------
  * A new module, res_pjsip_history, has been added that provides SIP history
diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample
index 859635c..e536f27 100644
--- a/configs/samples/pjsip.conf.sample
+++ b/configs/samples/pjsip.conf.sample
@@ -885,6 +885,10 @@
                             ; startup that qualifies should be attempted on all
                             ; contacts.  If greater than the qualify_frequency
                             ; for an aor, qualify_frequency will be used instead.
+; If regcontext is specified, Asterisk will dynamically create and destroy a
+; NoOp priority 1 extension for a given endpoint who registers or unregisters 
+; with us. The extension added is the name of the endpoint.
+;regcontext=sipregistrations
 
 ; MODULE PROVIDING BELOW SECTION(S): res_pjsip_acl
 ;==========================ACL SECTION OPTIONS=========================
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index 6ca56bd..0752357 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -2042,6 +2042,13 @@
 char *ast_sip_get_debug(void);
 
 /*!
+ * \brief Retrieve the global regcontext setting.
+ *
+ * \retval the global regcontext setting
+ */
+char *ast_sip_get_regcontext(void);
+
+/*!
  * \brief Retrieve the global endpoint_identifier_order setting.
  *
  * Specifies the order by which endpoint identifiers should be regarded.
diff --git a/res/res_pjsip.c b/res/res_pjsip.c
index 8e99c55..ef564c5 100644
--- a/res/res_pjsip.c
+++ b/res/res_pjsip.c
@@ -1271,6 +1271,10 @@
 				<configOption name="user_agent" default="Asterisk <Asterisk Version>">
 					<synopsis>Value used in User-Agent header for SIP requests and Server header for SIP responses.</synopsis>
 				</configOption>
+				<configOption name="regcontext" default="">
+                                        <synopsis>When set, Asterisk will dynamically create and destroy a NoOp priority 1 extension for a given 
+					peer who registers or unregisters with us.</synopsis>
+                                </configOption>
 				<configOption name="default_outbound_endpoint" default="default_outbound_endpoint">
 					<synopsis>Endpoint to use when sending an outbound request to a URI without a specified endpoint.</synopsis>
 				</configOption>
diff --git a/res/res_pjsip/config_global.c b/res/res_pjsip/config_global.c
index ef706f0..679acc0 100644
--- a/res/res_pjsip/config_global.c
+++ b/res/res_pjsip/config_global.c
@@ -35,6 +35,7 @@
 #define DEFAULT_ENDPOINT_IDENTIFIER_ORDER "ip,username,anonymous"
 #define DEFAULT_MAX_INITIAL_QUALIFY_TIME 0
 #define DEFAULT_FROM_USER "asterisk"
+#define DEFAULT_REGCONTEXT ""
 
 static char default_useragent[256];
 
@@ -42,6 +43,7 @@
 	SORCERY_OBJECT(details);
 	AST_DECLARE_STRING_FIELDS(
 		AST_STRING_FIELD(useragent);
+		AST_STRING_FIELD(regcontext);
 		AST_STRING_FIELD(default_outbound_endpoint);
 		/*! Debug logging yes|no|host */
 		AST_STRING_FIELD(debug);
@@ -136,6 +138,22 @@
 	ao2_ref(cfg, -1);
 	return res;
 }
+
+char *ast_sip_get_regcontext(void)
+{
+        char *res;
+        struct global_config *cfg;
+
+        cfg = get_global_cfg();
+        if (!cfg) {
+                return ast_strdup(DEFAULT_REGCONTEXT);
+        }
+
+        res = ast_strdup(cfg->regcontext);
+        ao2_ref(cfg, -1);
+        return res;
+}
+
 
 char *ast_sip_get_endpoint_identifier_order(void)
 {
@@ -310,6 +328,9 @@
 		OPT_UINT_T, 0, FLDSET(struct global_config, max_initial_qualify_time));
 	ast_sorcery_object_field_register(sorcery, "global", "default_from_user", DEFAULT_FROM_USER,
 		OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_from_user));
+	ast_sorcery_object_field_register(sorcery, "global", "regcontext", DEFAULT_REGCONTEXT,
+                OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, regcontext));
+
 
 	if (ast_sorcery_instance_observer_add(sorcery, &observer_callbacks_global)) {
 		return -1;
diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c
index 746a457..9a7b01b 100644
--- a/res/res_pjsip/pjsip_configuration.c
+++ b/res/res_pjsip/pjsip_configuration.c
@@ -21,6 +21,7 @@
 #include "asterisk/callerid.h"
 #include "asterisk/test.h"
 #include "asterisk/statsd.h"
+#include "asterisk/extconf.h"
 
 /*! \brief Number of buckets for persistent endpoint information */
 #define PERSISTENT_BUCKETS 53
@@ -119,10 +120,29 @@
 	if (state == AST_ENDPOINT_ONLINE) {
 		ast_endpoint_set_state(endpoint, AST_ENDPOINT_ONLINE);
 		blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
+
+		char *regcontext = ast_sip_get_regcontext();
+
+		if ( regcontext != "" ) {
+                        if (!ast_exists_extension(NULL, regcontext, ast_endpoint_get_resource(endpoint), 1, NULL)) {
+                                ast_add_extension(regcontext, 1, ast_endpoint_get_resource(endpoint), 1, NULL, NULL, "Noop", ast_strdup(ast_endpoint_get_resource(endpoint)), ast_free_ptr, "SIP");
+                        }
+                }
+
 		ast_verb(1, "Endpoint %s is now Reachable\n", ast_endpoint_get_resource(endpoint));
 	} else {
 		ast_endpoint_set_state(endpoint, AST_ENDPOINT_OFFLINE);
 		blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
+
+		char *regcontext = ast_sip_get_regcontext();
+
+		if ( regcontext != "" ) {
+                        struct pbx_find_info q = { .stacklen = 0 };
+                        if (pbx_find_extension(NULL, NULL, &q, regcontext, ast_endpoint_get_resource(endpoint), 1, NULL, "", E_MATCH)) {
+                                ast_context_remove_extension(regcontext, ast_endpoint_get_resource(endpoint), 1, NULL);
+                        }
+                }
+
 		ast_verb(1, "Endpoint %s is now Unreachable\n", ast_endpoint_get_resource(endpoint));
 	}
 

-- 
To view, visit https://gerrit.asterisk.org/1984
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib1530c5b45340625805c057f8ff1fb240a43ea62
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Daniel Journo <dan at keshercommunications.com>



More information about the asterisk-code-review mailing list