[Asterisk-code-review] res pjsip: Prune TCP/TLS contacts in astdb at startup (asterisk[master])

George Joseph asteriskteam at digium.com
Thu Feb 18 19:40:54 CST 2016


George Joseph has uploaded a new change for review.

  https://gerrit.asterisk.org/2280

Change subject: res_pjsip: Prune TCP/TLS contacts in astdb at startup
......................................................................

res_pjsip: Prune TCP/TLS contacts in astdb at startup

Unlike UDP contacts, TCP and TLS dynamic contacts can't be reused at startup
because their sockets were closed when Asterisk terminated.  If left in astdb,
when Asterisk restarts, we'll be trying to open a new socket to a port on the
client that most likely doesn't exist.  The result is more useless messages
and continued retries until the contact expires or is replaced.

ast_res_pjsip_initialize_configuration() now calls a new prune_tcp_contacts()
function just before returning that cleans out the TCP and TLS contacts from
sorcery.

Change-Id: I33fb70ba8380b8c61d574845346e380c10828bd9
---
M res/res_pjsip/pjsip_configuration.c
1 file changed, 46 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/80/2280/1

diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c
index 2a81cfd..c2ffae4 100644
--- a/res/res_pjsip/pjsip_configuration.c
+++ b/res/res_pjsip/pjsip_configuration.c
@@ -1809,6 +1809,50 @@
 struct ast_sip_cli_formatter_entry *channel_formatter;
 struct ast_sip_cli_formatter_entry *endpoint_formatter;
 
+static void prune_tcp_contacts(void)
+{
+	struct ao2_container *contacts = ast_sorcery_retrieve_by_fields(sip_sorcery, "contact",
+		AST_RETRIEVE_FLAG_MULTIPLE, NULL);
+	struct ao2_iterator i;
+	struct ast_sip_contact *contact;
+	pjsip_uri *uri;
+	pjsip_sip_uri *sip_uri;
+	pj_pool_t *pool;
+
+	if (!contacts) {
+		return;
+	}
+
+	if (!(pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "contact pruning", 512, 512))) {
+		ast_log(LOG_ERROR, "Unable to allocate pool for contact pruning\n");
+		ao2_ref(contacts, -1);
+		return;
+	}
+
+	i = ao2_iterator_init(contacts, 0);
+	while ((contact = ao2_iterator_next(&i))) {
+		if (!(uri = pjsip_parse_uri(pool, (char *) contact->uri, strlen(contact->uri), 0)) ||
+			(!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
+			ao2_ref(contact, -1);
+			continue;
+		}
+
+		sip_uri = pjsip_uri_get_uri(uri);
+
+		if (sip_uri->transport_param.slen
+			&& (!strncasecmp(sip_uri->transport_param.ptr, "tcp", sip_uri->transport_param.slen)
+				|| !strncasecmp(sip_uri->transport_param.ptr, "tls", sip_uri->transport_param.slen))) {
+			ast_debug(1, "Pruning tcp/tls contact at startup: %s\n", contact->uri);
+			ast_sorcery_delete(sip_sorcery, contact);
+		}
+
+		ao2_ref(contact, -1);
+	}
+	ao2_iterator_destroy(&i);
+	ao2_ref(contacts, -1);
+	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
+}
+
 int ast_res_pjsip_initialize_configuration(void)
 {
 	if (ast_manager_register_xml(AMI_SHOW_ENDPOINTS, EVENT_FLAG_SYSTEM, ami_show_endpoints) ||
@@ -2019,6 +2063,8 @@
 
 	ast_sorcery_load(sip_sorcery);
 
+	prune_tcp_contacts();
+
 	return 0;
 }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I33fb70ba8380b8c61d574845346e380c10828bd9
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: George Joseph <george.joseph at fairview5.com>



More information about the asterisk-code-review mailing list