<p>Friendly Automation <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/16265">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Kevin Harwell: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved
Friendly Automation: Approved for Submit
</div><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_existing 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, 187 insertions(+), 9 deletions(-)<br><br></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..80888c3 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..8c5b775 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>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..bae8a2d 100644</span><br><span>--- a/res/res_pjsip/location.c</span><br><span>+++ b/res/res_pjsip/location.c</span><br><span>@@ -1410,6 +1410,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..605c0fa 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>@@ -483,7 +487,48 @@</span><br><span> struct ast_sip_contact *right_contact = right;</span><br><span> </span><br><span> /* Sort from soonest to expire to last to expire */</span><br><span style="color: hsl(0, 100%, 40%);">- return ast_tvcmp(left_contact->expiration_time, right_contact->expiration_time);</span><br><span style="color: hsl(120, 100%, 40%);">+ int time_sorted = 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%);">+ 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 = 0;</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 style="color: hsl(120, 100%, 40%);">+</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%);">+ return time_sorted;</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%);">+ /* Get contact status if available */</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%);">+ if (!left_status) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return time_sorted;</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%);">+ right_status = ast_sip_get_contact_status(right_contact);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!right_status) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(left_status, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ return time_sorted;</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%);">+ 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_ref(left_status, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(right_status, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (left_unreachable != right_unreachable) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Set unavailable contact to top of vector */</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%);">+ /* Either both available or both unavailable */</span><br><span style="color: hsl(120, 100%, 40%);">+ return time_sorted;</span><br><span> }</span><br><span> </span><br><span> static int vec_contact_add(void *obj, void *arg, int flags)</span><br><span>@@ -512,7 +557,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 +566,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 +590,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>@@ -574,6 +623,29 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Internal callback function which adds any contact which is unreachable */</span><br><span style="color: hsl(120, 100%, 40%);">+static int registrar_add_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 ao2_container *container = arg;</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%);">+ if (unreachable) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_link(container, contact);</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%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> struct aor_core_response {</span><br><span> /*! Tx data to use for statefull response. NULL for stateless response. */</span><br><span> pjsip_tx_data *tdata;</span><br><span>@@ -596,6 +668,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,6 +739,33 @@</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 style="color: hsl(120, 100%, 40%);">+ if (contact_count > aor->max_contacts && aor->remove_unavailable) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Get unavailable 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 = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,</span><br><span style="color: hsl(120, 100%, 40%);">+ NULL, ast_sorcery_object_id_compare);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!unavail_contacts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ response->code = 500;</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%);">+ return;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_callback(contacts, OBJ_NODATA, registrar_add_unreachable, unavail_contacts);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (unavail_contacts) {</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%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check to see if removing unavailable contacts will help */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (contact_count - unavail_count <= aor->max_contacts) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Remove any unavailable 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 style="color: hsl(120, 100%, 40%);">+ /* We're only here if !aor->remove_existing so this count is correct */</span><br><span style="color: hsl(120, 100%, 40%);">+ contact_count = ao2_container_count(contacts) - permanent + added - deleted;</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> if (contact_count > aor->max_contacts) {</span><br><span> /* Enforce the maximum number of contacts */</span><br><span> ast_sip_report_failed_acl(endpoint, rdata, "registrar_attempt_exceeds_maximum_configured_contacts");</span><br><span>@@ -867,8 +967,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><div style="white-space:pre-wrap"></div><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/16265">change 16265</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/+/16265"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 19 </div>
<div style="display:none"> Gerrit-Change-Id: Ia2711b08f2b4d1177411b1be23e970d7fdff5784 </div>
<div style="display:none"> Gerrit-Change-Number: 16265 </div>
<div style="display:none"> Gerrit-PatchSet: 6 </div>
<div style="display:none"> Gerrit-Owner: Joe <ynadiv@corpit.xyz> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>