<p>Joe has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/16264">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_registrar: Remove unavailable contacts if exceeds max_contacts<br><br>The behavior of max_contacts and remove_existing are connected. If<br>remove_existing is enabled, the soonest expiring contacts are removed.<br>This may occur when there is an unavailable contact. Similarly,<br>when remove_existing is not enabled, registrations from good<br>endpoints are rejected in favor of retaining unavailable contacts.<br><br>This commit adds a new AOR option remove_unavailable, and the effect<br>of this setting will depend on remove_existing. If remove_existing<br>is set to no, we will still remove unavailable contacts when they<br>exceed max_contacts, if there are any. If remove_exising is set to<br>yes, we will prioritize the removal of unavailable contacts before<br>those that are expiring soonest.<br><br>ASTERISK-29525<br><br>Change-Id: Ia2711b08f2b4d1177411b1be23e970d7fdff5784<br>---<br>M configs/samples/pjsip.conf.sample<br>A contrib/ast-db-manage/config/versions/f56d79a9f337_pjsip_create_remove_unavailable.py<br>A doc/CHANGES-staging/res_pjsip_registrar.txt<br>M include/asterisk/res_pjsip.h<br>M res/res_pjsip.c<br>M res/res_pjsip/location.c<br>M res/res_pjsip_registrar.c<br>7 files changed, 180 insertions(+), 18 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/64/16264/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample</span><br><span>index 874fcc3..a70d580 100644</span><br><span>--- a/configs/samples/pjsip.conf.sample</span><br><span>+++ b/configs/samples/pjsip.conf.sample</span><br><span>@@ -1109,6 +1109,16 @@</span><br><span> ; contact created by rewrite_contact that the device is</span><br><span> ; refreshing.</span><br><span> ; (default: "no")</span><br><span style="color: hsl(120, 100%, 40%);">+;remove_unavailable=no ; If remove_existing is disabled, will allow a registration</span><br><span style="color: hsl(120, 100%, 40%);">+ ; to succeed by removing only unavailable contacts when</span><br><span style="color: hsl(120, 100%, 40%);">+ ; max_contacts is exceeded. This will reject a registration</span><br><span style="color: hsl(120, 100%, 40%);">+ ; that exceeds max_contacts if no unavailable contacts are</span><br><span style="color: hsl(120, 100%, 40%);">+ ; present to remove. If remove_existing is enabled, will</span><br><span style="color: hsl(120, 100%, 40%);">+ ; prioritize removal of unavailable contacts before removing</span><br><span style="color: hsl(120, 100%, 40%);">+ ; expiring soonest. This tames the behavior of remove_existing</span><br><span style="color: hsl(120, 100%, 40%);">+ ; to only remove an available contact if an unavailable one is</span><br><span style="color: hsl(120, 100%, 40%);">+ ; not present.</span><br><span style="color: hsl(120, 100%, 40%);">+ ; (default: "no")</span><br><span> ;type= ; Must be of type aor (default: "")</span><br><span> ;qualify_frequency=0 ; Interval at which to qualify an AoR via OPTIONS requests.</span><br><span> ; (default: "0")</span><br><span>diff --git a/contrib/ast-db-manage/config/versions/f56d79a9f337_pjsip_create_remove_unavailable.py b/contrib/ast-db-manage/config/versions/f56d79a9f337_pjsip_create_remove_unavailable.py</span><br><span>new file mode 100644</span><br><span>index 0000000..fc82e7a</span><br><span>--- /dev/null</span><br><span>+++ b/contrib/ast-db-manage/config/versions/f56d79a9f337_pjsip_create_remove_unavailable.py</span><br><span>@@ -0,0 +1,30 @@</span><br><span style="color: hsl(120, 100%, 40%);">+"""pjsip create remove_unavailable</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Revision ID: f56d79a9f337</span><br><span style="color: hsl(120, 100%, 40%);">+Revises: c20d6e3992f4</span><br><span style="color: hsl(120, 100%, 40%);">+Create Date: 2021-07-28 02:09:11.082061</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+"""</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+# revision identifiers, used by Alembic.</span><br><span style="color: hsl(120, 100%, 40%);">+revision = 'f56d79a9f337'</span><br><span style="color: hsl(120, 100%, 40%);">+down_revision = 'c20d6e3992f4'</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+from alembic import op</span><br><span style="color: hsl(120, 100%, 40%);">+import sqlalchemy as sa</span><br><span style="color: hsl(120, 100%, 40%);">+from sqlalchemy.dialects.postgresql import ENUM</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+AST_BOOL_NAME = 'ast_bool_values'</span><br><span style="color: hsl(120, 100%, 40%);">+AST_BOOL_VALUES = [ '0', '1',</span><br><span style="color: hsl(120, 100%, 40%);">+ 'off', 'on',</span><br><span style="color: hsl(120, 100%, 40%);">+ 'false', 'true',</span><br><span style="color: hsl(120, 100%, 40%);">+ 'no', 'yes' ]</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+def upgrade():</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_bool_values = ENUM(*AST_BOOL_VALUES, name=AST_BOOL_NAME, create_type=False)</span><br><span style="color: hsl(120, 100%, 40%);">+ op.add_column('ps_aors', sa.Column('remove_unavailable', ast_bool_values))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+def downgrade():</span><br><span style="color: hsl(120, 100%, 40%);">+ op.drop_column('ps_aors', 'remove_unavailable')</span><br><span style="color: hsl(120, 100%, 40%);">+ pass</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>diff --git a/doc/CHANGES-staging/res_pjsip_registrar.txt b/doc/CHANGES-staging/res_pjsip_registrar.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..a80f69f</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/res_pjsip_registrar.txt</span><br><span>@@ -0,0 +1,7 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: res_pjsip_registrar</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Adds new PJSIP AOR option remove_unavailable to either</span><br><span style="color: hsl(120, 100%, 40%);">+remove unavailable contacts when a REGISTER exceeds</span><br><span style="color: hsl(120, 100%, 40%);">+max_contacts when remove_existing is disabled, or</span><br><span style="color: hsl(120, 100%, 40%);">+prioritize unavailable contacts over other existing</span><br><span style="color: hsl(120, 100%, 40%);">+contacts when remove_existing is enabled.</span><br><span>diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h</span><br><span>index 351ce09..e1d6b98 100644</span><br><span>--- a/include/asterisk/res_pjsip.h</span><br><span>+++ b/include/asterisk/res_pjsip.h</span><br><span>@@ -389,6 +389,8 @@</span><br><span> double qualify_timeout;</span><br><span> /*! Voicemail extension to set in Message-Account */</span><br><span> char *voicemail_extension;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Whether to remove unavailable contacts over max_contacts at all or first if remove_existing is enabled */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int remove_unavailable;</span><br><span> };</span><br><span> </span><br><span> /*!</span><br><span>@@ -1021,6 +1023,9 @@</span><br><span> </span><br><span> /*! \brief Return only reachable or unknown contacts */</span><br><span> AST_SIP_CONTACT_FILTER_REACHABLE = (1 << 0),</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! \brief Return only unreachable contacts */</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_SIP_CONTACT_FILTER_UNREACHABLE = (1 << 4),</span><br><span> };</span><br><span> </span><br><span> /*!</span><br><span>diff --git a/res/res_pjsip.c b/res/res_pjsip.c</span><br><span>index 84c2559..a375fe0 100644</span><br><span>--- a/res/res_pjsip.c</span><br><span>+++ b/res/res_pjsip.c</span><br><span>@@ -1997,8 +1997,9 @@</span><br><span> TLS. Unfortunately, refreshing a registration may register a</span><br><span> different contact address and exceed</span><br><span> <replaceable>max_contacts</replaceable>. The</span><br><span style="color: hsl(0, 100%, 40%);">- <replaceable>remove_existing</replaceable> option can help by</span><br><span style="color: hsl(0, 100%, 40%);">- removing the soonest to expire contact(s) over</span><br><span style="color: hsl(120, 100%, 40%);">+ <replaceable>remove_existing</replaceable> and</span><br><span style="color: hsl(120, 100%, 40%);">+ <replaceable>remove_unavailable</replaceable> options can help by</span><br><span style="color: hsl(120, 100%, 40%);">+ removing either the soonest to expire or unavailable contact(s) over</span><br><span> <replaceable>max_contacts</replaceable> which is likely the</span><br><span> old <replaceable>rewrite_contact</replaceable> contact source</span><br><span> address being refreshed.</span><br><span>@@ -2041,6 +2042,26 @@</span><br><span> </para></note></span><br><span> </description></span><br><span> </configOption></span><br><span style="color: hsl(120, 100%, 40%);">+ <configOption name="remove_unavailable" default="no"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis>Determines whether new contacts should replace unavailable ones.</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <description><para></span><br><span style="color: hsl(120, 100%, 40%);">+ The effect of this setting depends on the setting of</span><br><span style="color: hsl(120, 100%, 40%);">+ <replaceable>remove_existing</replaceable>.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>If <replaceable>remove_existing</replaceable> is set to</span><br><span style="color: hsl(120, 100%, 40%);">+ <literal>no</literal> (default), setting remove_unavailable to</span><br><span style="color: hsl(120, 100%, 40%);">+ <literal>yes</literal> will remove only unavailable contacts that exceed</span><br><span style="color: hsl(120, 100%, 40%);">+ <replaceable>max_contacts</replaceable> to allow an incoming</span><br><span style="color: hsl(120, 100%, 40%);">+ REGISTER to complete sucessfully.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>If <replaceable>remove_existing</replaceable> is set to</span><br><span style="color: hsl(120, 100%, 40%);">+ <literal>yes</literal>, setting remove_unavailable to</span><br><span style="color: hsl(120, 100%, 40%);">+ <literal>yes</literal> will prioritize unavailable contacts for removal</span><br><span style="color: hsl(120, 100%, 40%);">+ instead of just removing the contact that expires the soonest.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <note><para>See <replaceable>remove_existing</replaceable> and</span><br><span style="color: hsl(120, 100%, 40%);">+ <replaceable>max_contacts</replaceable> for further information about how</span><br><span style="color: hsl(120, 100%, 40%);">+ these 3 settings interact.</span><br><span style="color: hsl(120, 100%, 40%);">+ </para></note></span><br><span style="color: hsl(120, 100%, 40%);">+ </description></span><br><span style="color: hsl(120, 100%, 40%);">+ </configOption></span><br><span> <configOption name="type"></span><br><span> <synopsis>Must be of type 'aor'.</synopsis></span><br><span> </configOption></span><br><span>@@ -2444,6 +2465,9 @@</span><br><span> <parameter name="RemoveExisting"></span><br><span> <para><xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption[@name='remove_existing']/synopsis/node())"/></para></span><br><span> </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="RemoveUnavailable"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para><xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption[@name='remove_unavailable']/synopsis/node())"/></para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span> <parameter name="Mailboxes"></span><br><span> <para><xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption[@name='mailboxes']/synopsis/node())"/></para></span><br><span> </parameter></span><br><span>@@ -2904,6 +2928,9 @@</span><br><span> <parameter name="RemoveExisting"></span><br><span> <para><xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption[@name='remove_existing']/synopsis/node())"/></para></span><br><span> </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="RemoveUnavailable"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para><xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption[@name='remove_unavailable']/synopsis/node())"/></para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span> <parameter name="Mailboxes"></span><br><span> <para><xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption[@name='mailboxes']/synopsis/node())"/></para></span><br><span> </parameter></span><br><span>diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c</span><br><span>index cef5ad7..1d26b94 100644</span><br><span>--- a/res/res_pjsip/location.c</span><br><span>+++ b/res/res_pjsip/location.c</span><br><span>@@ -191,6 +191,24 @@</span><br><span> return unreachable ? CMP_MATCH : 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Internal callback function which contains only contact which is unreachable */</span><br><span style="color: hsl(120, 100%, 40%);">+static int contact_only_unreachable(void *obj, void *arg, int flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_contact *contact = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_contact_status *status;</span><br><span style="color: hsl(120, 100%, 40%);">+ int unreachable;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ status = ast_sip_get_contact_status(contact);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!status) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ unreachable = (status->status == UNAVAILABLE);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(status, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return unreachable ? 0 : CMP_MATCH;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct ast_sip_aor *aor)</span><br><span> {</span><br><span> return ast_sip_location_retrieve_first_aor_contact_filtered(aor, AST_SIP_CONTACT_FILTER_DEFAULT);</span><br><span>@@ -237,6 +255,10 @@</span><br><span> ao2_callback(aor->permanent_contacts, OBJ_NODATA, contact_link_static, contacts);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ if (flags & AST_SIP_CONTACT_FILTER_UNREACHABLE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_only_unreachable, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (flags & AST_SIP_CONTACT_FILTER_REACHABLE) {</span><br><span> ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_remove_unreachable, NULL);</span><br><span> }</span><br><span>@@ -1410,6 +1432,7 @@</span><br><span> ast_sorcery_object_field_register(sorcery, "aor", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, authenticate_qualify));</span><br><span> ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts));</span><br><span> ast_sorcery_object_field_register(sorcery, "aor", "remove_existing", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_existing));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sorcery_object_field_register(sorcery, "aor", "remove_unavailable", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_unavailable));</span><br><span> ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, contacts_to_str, contacts_to_var_list, 0, 0);</span><br><span> ast_sorcery_object_field_register(sorcery, "aor", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, mailboxes));</span><br><span> ast_sorcery_object_field_register_custom(sorcery, "aor", "voicemail_extension", "", voicemail_extension_handler, voicemail_extension_to_str, NULL, 0, 0);</span><br><span>diff --git a/res/res_pjsip_registrar.c b/res/res_pjsip_registrar.c</span><br><span>index 6fe4058..f894a1f 100644</span><br><span>--- a/res/res_pjsip_registrar.c</span><br><span>+++ b/res/res_pjsip_registrar.c</span><br><span>@@ -204,6 +204,7 @@</span><br><span> enum contact_delete_type {</span><br><span> CONTACT_DELETE_ERROR,</span><br><span> CONTACT_DELETE_EXISTING,</span><br><span style="color: hsl(120, 100%, 40%);">+ CONTACT_DELETE_UNAVAILABLE,</span><br><span> CONTACT_DELETE_EXPIRE,</span><br><span> CONTACT_DELETE_REQUEST,</span><br><span> CONTACT_DELETE_SHUTDOWN,</span><br><span>@@ -448,6 +449,9 @@</span><br><span> case CONTACT_DELETE_EXISTING:</span><br><span> reason = "remove existing";</span><br><span> break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case CONTACT_DELETE_UNAVAILABLE:</span><br><span style="color: hsl(120, 100%, 40%);">+ reason = "remove unavailable";</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span> case CONTACT_DELETE_EXPIRE:</span><br><span> reason = "expiration";</span><br><span> break;</span><br><span>@@ -481,8 +485,43 @@</span><br><span> {</span><br><span> struct ast_sip_contact *left_contact = left;</span><br><span> struct ast_sip_contact *right_contact = right;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_aor *aor = ast_sip_location_retrieve_aor(left_contact->aor);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_contact_status *left_status;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_contact_status *right_status;</span><br><span style="color: hsl(120, 100%, 40%);">+ int remove_unavailable = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ int left_unreachable;</span><br><span style="color: hsl(120, 100%, 40%);">+ int right_unreachable;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Sort from soonest to expire to last to expire */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (aor) {</span><br><span style="color: hsl(120, 100%, 40%);">+ remove_unavailable = aor->remove_unavailable;</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(aor, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!remove_unavailable) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Sort from soonest to expire to last to expire */</span><br><span style="color: hsl(120, 100%, 40%);">+ return ast_tvcmp(left_contact->expiration_time, right_contact->expiration_time);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If contact is unreachable move to top of list */</span><br><span style="color: hsl(120, 100%, 40%);">+ left_status = ast_sip_get_contact_status(left_contact);</span><br><span style="color: hsl(120, 100%, 40%);">+ right_status = ast_sip_get_contact_status(right_contact);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (left_status && right_status) {</span><br><span style="color: hsl(120, 100%, 40%);">+ left_unreachable = (left_status->status == UNAVAILABLE);</span><br><span style="color: hsl(120, 100%, 40%);">+ right_unreachable = (right_status->status == UNAVAILABLE);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(left_status);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(right_status);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (left_unreachable == right_unreachable) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Either both available or both unavailable */</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Sort from soonest to expire to last to expire */</span><br><span style="color: hsl(120, 100%, 40%);">+ return ast_tvcmp(left_contact->expiration_time, right_contact->expiration_time);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (left_unreachable) return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (right_unreachable) return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* No status from one or both contacts, return by expiration */</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(left_status);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(right_status);</span><br><span> return ast_tvcmp(left_contact->expiration_time, right_contact->expiration_time);</span><br><span> }</span><br><span> </span><br><span>@@ -512,7 +551,7 @@</span><br><span> </span><br><span> /*!</span><br><span> * \internal</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief Remove excess existing contacts that expire the soonest.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Remove excess existing contacts that are unavailable or expire soonest.</span><br><span> * \since 13.18.0</span><br><span> *</span><br><span> * \param contacts Container of unmodified contacts that could remove.</span><br><span>@@ -521,7 +560,7 @@</span><br><span> * \return Nothing</span><br><span> */</span><br><span> static void remove_excess_contacts(struct ao2_container *contacts, struct ao2_container *response_contacts,</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int to_remove)</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int to_remove, unsigned int remove_existing)</span><br><span> {</span><br><span> struct excess_contact_vector contact_vec;</span><br><span> </span><br><span>@@ -545,13 +584,17 @@</span><br><span> ast_assert(AST_VECTOR_SIZE(&contact_vec) == to_remove);</span><br><span> to_remove = AST_VECTOR_SIZE(&contact_vec);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Remove the excess contacts that expire the soonest */</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove the excess contacts that are unavailable or expire the soonest */</span><br><span> while (to_remove--) {</span><br><span> struct ast_sip_contact *contact;</span><br><span> </span><br><span> contact = AST_VECTOR_GET(&contact_vec, to_remove);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- registrar_contact_delete(CONTACT_DELETE_EXISTING, NULL, contact, contact->aor);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!remove_existing) {</span><br><span style="color: hsl(120, 100%, 40%);">+ registrar_contact_delete(CONTACT_DELETE_UNAVAILABLE, NULL, contact, contact->aor);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ registrar_contact_delete(CONTACT_DELETE_EXISTING, NULL, contact, contact->aor);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> ao2_unlink(response_contacts, contact);</span><br><span> }</span><br><span>@@ -596,6 +639,7 @@</span><br><span> int permanent = 0;</span><br><span> int contact_count;</span><br><span> struct ao2_container *existing_contacts = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ao2_container *unavail_contacts = NULL;</span><br><span> pjsip_contact_hdr *contact_hdr = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr;</span><br><span> struct registrar_contact_details details = { 0, };</span><br><span> pjsip_tx_data *tdata;</span><br><span>@@ -666,16 +710,31 @@</span><br><span> /* Total contacts after this registration */</span><br><span> contact_count = ao2_container_count(contacts) - permanent + added - deleted;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (contact_count > aor->max_contacts) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Enforce the maximum number of contacts */</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sip_report_failed_acl(endpoint, rdata, "registrar_attempt_exceeds_maximum_configured_contacts");</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Registration attempt from endpoint '%s' (%s:%d) to AOR '%s' will exceed max contacts of %u\n",</span><br><span style="color: hsl(0, 100%, 40%);">- ast_sorcery_object_get_id(endpoint), rdata->pkt_info.src_name, rdata->pkt_info.src_port,</span><br><span style="color: hsl(0, 100%, 40%);">- aor_name, aor->max_contacts);</span><br><span style="color: hsl(0, 100%, 40%);">- response->code = 403;</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_cleanup(existing_contacts);</span><br><span style="color: hsl(0, 100%, 40%);">- return;</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Get unreachable contact total */</span><br><span style="color: hsl(120, 100%, 40%);">+ int unavail_count = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ unavail_contacts = ast_sip_location_retrieve_aor_contacts_filtered(aor, AST_SIP_CONTACT_FILTER_UNREACHABLE);</span><br><span style="color: hsl(120, 100%, 40%);">+ unavail_count = ao2_container_count(unavail_contacts);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check to see if removing those will not help */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (contact_count - unavail_count > aor->max_contacts || !aor->remove_unavailable) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Enforce the maximum number of contacts */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_report_failed_acl(endpoint, rdata, "registrar_attempt_exceeds_maximum_configured_contacts");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Registration attempt from endpoint '%s' (%s:%d) to AOR '%s' will exceed max contacts of %u\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sorcery_object_get_id(endpoint), rdata->pkt_info.src_name, rdata->pkt_info.src_port,</span><br><span style="color: hsl(120, 100%, 40%);">+ aor_name, aor->max_contacts);</span><br><span style="color: hsl(120, 100%, 40%);">+ response->code = 403;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(unavail_contacts);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(existing_contacts);</span><br><span style="color: hsl(120, 100%, 40%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove any unreachable contacts */</span><br><span style="color: hsl(120, 100%, 40%);">+ remove_excess_contacts(unavail_contacts, contacts, contact_count - aor->max_contacts, aor->remove_existing);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup(unavail_contacts);</span><br><span> }</span><br><span> </span><br><span> user_agent_hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &USER_AGENT, NULL);</span><br><span>@@ -867,8 +926,9 @@</span><br><span> /* Total contacts after this registration */</span><br><span> contact_count = ao2_container_count(existing_contacts) + updated + added;</span><br><span> if (contact_count > aor->max_contacts) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Remove excess existing contacts that expire the soonest */</span><br><span style="color: hsl(0, 100%, 40%);">- remove_excess_contacts(existing_contacts, contacts, contact_count - aor->max_contacts);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove excess existing contacts that are unavailable or expire soonest */</span><br><span style="color: hsl(120, 100%, 40%);">+ remove_excess_contacts(existing_contacts, contacts, contact_count - aor->max_contacts,</span><br><span style="color: hsl(120, 100%, 40%);">+ aor->remove_existing);</span><br><span> }</span><br><span> ao2_ref(existing_contacts, -1);</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/16264">change 16264</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/16264"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: Ia2711b08f2b4d1177411b1be23e970d7fdff5784 </div>
<div style="display:none"> Gerrit-Change-Number: 16264 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Joe <ynadiv@corpit.xyz> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>