<p>George Joseph <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/14838">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve
Kevin Harwell: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved; Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_diversion: implement support for History-Info<br><br>Implemention of History-Info capable of interworking with Diversion<br>Header following RFC7544<br><br>ASTERISK-29027 #close<br><br>Change-Id: I2296369582d4b295c5ea1e60bec391dd1d318fa6<br>---<br>A contrib/ast-db-manage/config/versions/e658c26033ca_create_history_info_flag.py<br>M include/asterisk/res_pjsip.h<br>M res/res_pjsip.c<br>M res/res_pjsip/pjsip_configuration.c<br>M res/res_pjsip_diversion.c<br>5 files changed, 343 insertions(+), 26 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/contrib/ast-db-manage/config/versions/e658c26033ca_create_history_info_flag.py b/contrib/ast-db-manage/config/versions/e658c26033ca_create_history_info_flag.py</span><br><span>new file mode 100644</span><br><span>index 0000000..fa3d492</span><br><span>--- /dev/null</span><br><span>+++ b/contrib/ast-db-manage/config/versions/e658c26033ca_create_history_info_flag.py</span><br><span>@@ -0,0 +1,38 @@</span><br><span style="color: hsl(120, 100%, 40%);">+"""create history info flag</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Revision ID: e658c26033ca</span><br><span style="color: hsl(120, 100%, 40%);">+Revises: 1ae0609b6646</span><br><span style="color: hsl(120, 100%, 40%);">+Create Date: 2020-08-13 10:53:21.032591</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 = 'e658c26033ca'</span><br><span style="color: hsl(120, 100%, 40%);">+down_revision = '1ae0609b6646'</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%);">+# We'll just ignore the n/y and f/t abbreviations as Asterisk does not write</span><br><span style="color: hsl(120, 100%, 40%);">+# those aliases.</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%);">+ ############################# Enums ##############################</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ # ast_bool_values has already been created, so use postgres enum object</span><br><span style="color: hsl(120, 100%, 40%);">+ # type to get around "already created" issue - works okay with mysql</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%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ op.add_column('ps_endpoints', sa.Column('send_history_info', ast_bool_values))</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%);">+def downgrade():</span><br><span style="color: hsl(120, 100%, 40%);">+ if op.get_context().bind.dialect.name == 'mssql':</span><br><span style="color: hsl(120, 100%, 40%);">+ op.drop_constraint('ck_ps_endpoints_send_history_info_ast_bool_values','ps_endpoints')</span><br><span style="color: hsl(120, 100%, 40%);">+ op.drop_column('ps_endpoints', 'send_history_info')</span><br><span>diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h</span><br><span>index 48da005..ddcf02f 100644</span><br><span>--- a/include/asterisk/res_pjsip.h</span><br><span>+++ b/include/asterisk/res_pjsip.h</span><br><span>@@ -648,6 +648,8 @@</span><br><span> unsigned int send_connected_line;</span><br><span> /*! When performing connected line update, which method should be used */</span><br><span> enum ast_sip_session_refresh_method refresh_method;</span><br><span style="color: hsl(120, 100%, 40%);">+ /*! Do we add History-Info headers to applicable outgoing requests/responses? */</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int send_history_info;</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 d7655ad..9d3a6c2 100644</span><br><span>--- a/res/res_pjsip.c</span><br><span>+++ b/res/res_pjsip.c</span><br><span>@@ -659,6 +659,10 @@</span><br><span> <synopsis>Send the Diversion header, conveying the diversion</span><br><span> information to the called user agent</synopsis></span><br><span> </configOption></span><br><span style="color: hsl(120, 100%, 40%);">+ <configOption name="send_history_info" default="no"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis>Send the History-Info header, conveying the diversion</span><br><span style="color: hsl(120, 100%, 40%);">+ information to the called and calling user agents</synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ </configOption></span><br><span> <configOption name="send_pai" default="no"></span><br><span> <synopsis>Send the P-Asserted-Identity header</synopsis></span><br><span> </configOption></span><br><span>diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c</span><br><span>index b23de7e..344f319 100644</span><br><span>--- a/res/res_pjsip/pjsip_configuration.c</span><br><span>+++ b/res/res_pjsip/pjsip_configuration.c</span><br><span>@@ -2051,6 +2051,7 @@</span><br><span> ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));</span><br><span> ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rpid_immediate", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.rpid_immediate));</span><br><span> ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_history_info", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_history_info));</span><br><span> ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));</span><br><span> ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "voicemail_extension", "", voicemail_extension_handler, voicemail_extension_to_str, NULL, 0, 0);</span><br><span> ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));</span><br><span>diff --git a/res/res_pjsip_diversion.c b/res/res_pjsip_diversion.c</span><br><span>index 3f74871..9c69f52 100644</span><br><span>--- a/res/res_pjsip_diversion.c</span><br><span>+++ b/res/res_pjsip_diversion.c</span><br><span>@@ -36,6 +36,8 @@</span><br><span> #include "asterisk/strings.h"</span><br><span> </span><br><span> static const pj_str_t diversion_name = { "Diversion", 9 };</span><br><span style="color: hsl(120, 100%, 40%);">+static const pj_str_t history_info_name = { "History-Info", 12 };</span><br><span style="color: hsl(120, 100%, 40%);">+static pj_str_t HISTINFO_SUPPORTED_NAME = { "histinfo", 8 };</span><br><span> </span><br><span> /*!</span><br><span> * \internal</span><br><span>@@ -80,22 +82,62 @@</span><br><span> static const struct reasons {</span><br><span> enum AST_REDIRECTING_REASON code;</span><br><span> const char *text;</span><br><span style="color: hsl(120, 100%, 40%);">+ const unsigned int cause;</span><br><span> } reason_table[] = {</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_UNKNOWN, "unknown" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_USER_BUSY, "user-busy" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_NO_ANSWER, "no-answer" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_DEFLECTION, "deflection" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_AWAY, "away" },</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_CALL_FWD_DTE, "cf_dte" }, /* Non-standard */</span><br><span style="color: hsl(0, 100%, 40%);">- { AST_REDIRECTING_REASON_SEND_TO_VM, "send_to_vm" }, /* Non-standard */</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_UNKNOWN, "unknown", 404 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_USER_BUSY, "user-busy", 486 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_NO_ANSWER, "no-answer", 408 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_UNAVAILABLE, "unavailable", 503 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_UNCONDITIONAL, "unconditional", 302 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_TIME_OF_DAY, "time-of-day", 404 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_DO_NOT_DISTURB, "do-not-disturb", 404 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_DEFLECTION, "deflection", 480 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_FOLLOW_ME, "follow-me", 404 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_OUT_OF_ORDER, "out-of-service", 404 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_AWAY, "away", 404 },</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_CALL_FWD_DTE, "cf_dte", 404 }, /* Non-standard */</span><br><span style="color: hsl(120, 100%, 40%);">+ { AST_REDIRECTING_REASON_SEND_TO_VM, "send_to_vm", 404 }, /* Non-standard */</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static enum AST_REDIRECTING_REASON cause_to_reason(const unsigned long cause) {</span><br><span style="color: hsl(120, 100%, 40%);">+ switch(cause) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case 302:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_REDIRECTING_REASON_UNCONDITIONAL;</span><br><span style="color: hsl(120, 100%, 40%);">+ case 486:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_REDIRECTING_REASON_USER_BUSY;</span><br><span style="color: hsl(120, 100%, 40%);">+ case 408:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_REDIRECTING_REASON_NO_ANSWER;</span><br><span style="color: hsl(120, 100%, 40%);">+ case 480:</span><br><span style="color: hsl(120, 100%, 40%);">+ case 487:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_REDIRECTING_REASON_DEFLECTION;</span><br><span style="color: hsl(120, 100%, 40%);">+ case 503:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_REDIRECTING_REASON_UNAVAILABLE;</span><br><span style="color: hsl(120, 100%, 40%);">+ default:</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_REDIRECTING_REASON_UNKNOWN;</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%);">+static int add_supported(pjsip_tx_data *tdata)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_supported_hdr *hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!hdr) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* insert a new Supported header */</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr = pjsip_supported_hdr_create(tdata->pool);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!hdr) {</span><br><span style="color: hsl(120, 100%, 40%);">+ 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%);">+ pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);</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%);">+ /* add on to the existing Supported header */</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_strassign(&hdr->values[hdr->count++], &HISTINFO_SUPPORTED_NAME);</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> static const char *reason_code_to_str(const struct ast_party_redirecting_reason *reason)</span><br><span> {</span><br><span> int idx;</span><br><span>@@ -116,6 +158,21 @@</span><br><span> return "unknown";</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static const unsigned int reason_code_to_cause(const struct ast_party_redirecting_reason *reason)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int idx;</span><br><span style="color: hsl(120, 100%, 40%);">+ int code;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ code = reason->code;</span><br><span style="color: hsl(120, 100%, 40%);">+ for (idx = 0; idx < ARRAY_LEN(reason_table); ++idx) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (code == reason_table[idx].code) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return reason_table[idx].cause;</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%);">+ return 404;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)</span><br><span> {</span><br><span> static const pj_str_t from_name = { "From", 4 };</span><br><span>@@ -135,6 +192,61 @@</span><br><span> pj_strlen(&value), &size);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/* Asterisk keeps track of 2 things. The redirected from address and</span><br><span style="color: hsl(120, 100%, 40%);">+ * the redirected to address. If first=0 method will get the most recent</span><br><span style="color: hsl(120, 100%, 40%);">+ * redirection target for use as the redirected to address. If first=1</span><br><span style="color: hsl(120, 100%, 40%);">+ * then this method will get the original redirection target (index=1)</span><br><span style="color: hsl(120, 100%, 40%);">+ * for use as the redirected from address.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static pjsip_fromto_hdr *get_history_info_header(pjsip_rx_data *rdata, const unsigned int first)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ static const pj_str_t from_name = { "From", 4 };</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_fromto_hdr * result_hdr = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_generic_string_hdr *hdr = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &history_info_name, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!hdr) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</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%);">+ do {</span><br><span style="color: hsl(120, 100%, 40%);">+ static const pj_str_t index_name = { "index", 5 };</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_str_t value;</span><br><span style="color: hsl(120, 100%, 40%);">+ int size;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_fromto_hdr * fromto_hdr = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_param * index = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* parse as a fromto header */</span><br><span style="color: hsl(120, 100%, 40%);">+ fromto_hdr = pjsip_parse_hdr(rdata->tp_info.pool, &from_name, value.ptr,</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_strlen(&value), &size);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (fromto_hdr == NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</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%);">+ index = pjsip_param_find(&fromto_hdr->other_param, &index_name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (index) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!pj_strcmp2(&index->value, "1")) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!first) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ return fromto_hdr;</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%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ result_hdr = fromto_hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ } while ((hdr = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &history_info_name, hdr->next)));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return result_hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static void set_redirecting_value(char **dst, const pj_str_t *src)</span><br><span> {</span><br><span> ast_free(*dst);</span><br><span>@@ -197,14 +309,36 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static void set_redirecting_reason(pjsip_fromto_hdr *hdr,</span><br><span style="color: hsl(120, 100%, 40%);">+static void set_redirecting_reason_by_cause(pjsip_name_addr *name_addr,</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_party_redirecting_reason *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ static const pj_str_t cause_name = { "cause", 5 };</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_sip_uri *uri = pjsip_uri_get_uri(name_addr);</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_param *cause = pjsip_param_find(&uri->other_param, &cause_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned long cause_value;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!cause) {</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%);">+ cause_value = pj_strtoul(&cause->value);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ data->code = cause_to_reason(cause_value);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(data->str);</span><br><span style="color: hsl(120, 100%, 40%);">+ data->str = ast_strdup("");</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%);">+static void set_redirecting_reason(pjsip_fromto_hdr *from_info, pjsip_name_addr *to_info,</span><br><span> struct ast_party_redirecting_reason *data)</span><br><span> {</span><br><span> static const pj_str_t reason_name = { "reason", 6 };</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_param *reason = pjsip_param_find(&hdr->other_param, &reason_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_param *reason = pjsip_param_find(&from_info->other_param, &reason_name);</span><br><span> char *reason_str;</span><br><span> </span><br><span> if (!reason) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (to_info) {</span><br><span style="color: hsl(120, 100%, 40%);">+ set_redirecting_reason_by_cause(to_info, data);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> return;</span><br><span> }</span><br><span> </span><br><span>@@ -246,12 +380,15 @@</span><br><span> if (from_info) {</span><br><span> set_redirecting_id((pjsip_name_addr*)from_info->uri,</span><br><span> &data.from, &update.from);</span><br><span style="color: hsl(0, 100%, 40%);">- set_redirecting_reason(from_info, &data.reason);</span><br><span style="color: hsl(120, 100%, 40%);">+ set_redirecting_reason(from_info, to_info, &data.reason);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_set_party_id_all(&update.priv_to);</span><br><span> } else {</span><br><span> copy_redirecting_id(&data.from, &session->id, &update.from);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- set_redirecting_id(to_info, &data.to, &update.to);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (to_info) {</span><br><span style="color: hsl(120, 100%, 40%);">+ set_redirecting_id(to_info, &data.to, &update.to);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> ast_set_party_id_all(&update.priv_orig);</span><br><span> ast_set_party_id_all(&update.priv_from);</span><br><span>@@ -273,6 +410,17 @@</span><br><span> if (hdr) {</span><br><span> set_redirecting(session, hdr, (pjsip_name_addr*)</span><br><span> PJSIP_MSG_TO_HDR(rdata->msg_info.msg)->uri);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_fromto_hdr *history_info_to;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_fromto_hdr *history_info_from;</span><br><span style="color: hsl(120, 100%, 40%);">+ history_info_to = get_history_info_header(rdata, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (history_info_to) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If History-Info is present, then it will also include the original</span><br><span style="color: hsl(120, 100%, 40%);">+ redirected-from in addition to the redirected-to */</span><br><span style="color: hsl(120, 100%, 40%);">+ history_info_from = get_history_info_header(rdata, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ set_redirecting(session, history_info_from, (pjsip_name_addr*)history_info_to->uri);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> </span><br><span> return 0;</span><br><span>@@ -285,6 +433,8 @@</span><br><span> </span><br><span> pjsip_status_line status = rdata->msg_info.msg->line.status;</span><br><span> pjsip_fromto_hdr *div_hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_fromto_hdr *history_info_to;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_fromto_hdr *history_info_from;</span><br><span> pjsip_contact_hdr *contact_hdr;</span><br><span> </span><br><span> if ((status.code != 302) && (status.code != 181)) {</span><br><span>@@ -292,15 +442,35 @@</span><br><span> }</span><br><span> </span><br><span> /* use the diversion header info if there is one. if not one then use the</span><br><span style="color: hsl(0, 100%, 40%);">- session caller id info. if that doesn't exist use info from the To hdr*/</span><br><span style="color: hsl(0, 100%, 40%);">- if (!(div_hdr = get_diversion_header(rdata)) && !session->id.number.valid) {</span><br><span style="color: hsl(0, 100%, 40%);">- div_hdr = PJSIP_MSG_TO_HDR(rdata->msg_info.msg);</span><br><span style="color: hsl(120, 100%, 40%);">+ the history-info, if that doesn't exist, use session caller id info. if</span><br><span style="color: hsl(120, 100%, 40%);">+ that doesn't exist use info from the To hdr*/</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(div_hdr = get_diversion_header(rdata))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ history_info_to = get_history_info_header(rdata, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (history_info_to) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* If History-Info is present, then it will also include the original</span><br><span style="color: hsl(120, 100%, 40%);">+ redirected-from in addition to the redirected-to */</span><br><span style="color: hsl(120, 100%, 40%);">+ history_info_from = get_history_info_header(rdata, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ set_redirecting(session, history_info_from, (pjsip_name_addr*)history_info_to->uri);</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%);">+ if (!div_hdr && !session->id.number.valid) {</span><br><span style="color: hsl(120, 100%, 40%);">+ div_hdr = PJSIP_MSG_TO_HDR(rdata->msg_info.msg);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- contact_hdr = pjsip_msg_find_hdr_by_names(rdata->msg_info.msg, &contact_name, &contact_name_s, NULL);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- set_redirecting(session, div_hdr, contact_hdr ? (pjsip_name_addr*)contact_hdr->uri :</span><br><span style="color: hsl(0, 100%, 40%);">- (pjsip_name_addr*)PJSIP_MSG_FROM_HDR(rdata->msg_info.msg)->uri);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (status.code == 302) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* With 302, Contact indicates the final destination and possibly Diversion indicates the hop before */</span><br><span style="color: hsl(120, 100%, 40%);">+ contact_hdr = pjsip_msg_find_hdr_by_names(rdata->msg_info.msg, &contact_name, &contact_name_s, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ set_redirecting(session, div_hdr, contact_hdr ? (pjsip_name_addr*)contact_hdr->uri :</span><br><span style="color: hsl(120, 100%, 40%);">+ (pjsip_name_addr*)PJSIP_MSG_FROM_HDR(rdata->msg_info.msg)->uri);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* With 181, Diversion is non-standard, but if present indicates the new final destination, and To indicating the original */</span><br><span style="color: hsl(120, 100%, 40%);">+ set_redirecting(session, PJSIP_MSG_TO_HDR(rdata->msg_info.msg),</span><br><span style="color: hsl(120, 100%, 40%);">+ div_hdr ? (pjsip_name_addr*)div_hdr->uri : NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> </span><br><span> /*!</span><br><span>@@ -312,6 +482,8 @@</span><br><span> */</span><br><span> static void add_diversion_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ static const pj_str_t reason_name = { "reason", 6 };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> pjsip_fromto_hdr *hdr;</span><br><span> pjsip_name_addr *name_addr;</span><br><span> pjsip_sip_uri *uri;</span><br><span>@@ -320,9 +492,17 @@</span><br><span> const char *reason_str;</span><br><span> const char *quote_str;</span><br><span> char *reason_buf;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_uri *base;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_party_id *id = &data->from;</span><br><span style="color: hsl(0, 100%, 40%);">- pjsip_uri *base = PJSIP_MSG_FROM_HDR(tdata->msg)->uri;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_party_id *id = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (tdata->msg->type == PJSIP_REQUEST_MSG) {</span><br><span style="color: hsl(120, 100%, 40%);">+ id = &data->from;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* In responses indicate the new destination */</span><br><span style="color: hsl(120, 100%, 40%);">+ id = &data->to;</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%);">+ base = PJSIP_MSG_FROM_HDR(tdata->msg)->uri;</span><br><span> </span><br><span> if (!id->number.valid || ast_strlen_zero(id->number.str)) {</span><br><span> return;</span><br><span>@@ -339,7 +519,7 @@</span><br><span> pj_strdup2(tdata->pool, &uri->user, id->number.str);</span><br><span> </span><br><span> param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);</span><br><span style="color: hsl(0, 100%, 40%);">- param->name = pj_str("reason");</span><br><span style="color: hsl(120, 100%, 40%);">+ param->name = reason_name;</span><br><span> </span><br><span> reason_str = reason_code_to_str(&data->reason);</span><br><span> </span><br><span>@@ -361,14 +541,106 @@</span><br><span> pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Adds history-info header information to an outbound SIP message</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param tdata The outbound message</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param data The redirecting data used to fill parts of the history-info header</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void add_history_info_header(pjsip_tx_data *tdata, struct ast_party_redirecting *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ static const pj_str_t index_name = { "index", 5 };</span><br><span style="color: hsl(120, 100%, 40%);">+ static const pj_str_t cause_name = { "cause", 5 };</span><br><span style="color: hsl(120, 100%, 40%);">+ static const pj_str_t first_index = { "1", 1 };</span><br><span style="color: hsl(120, 100%, 40%);">+ static const pj_str_t last_index = { "1.1", 3 };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_fromto_hdr *hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_name_addr *name_addr;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_sip_uri *uri;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_param *param;</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_fromto_hdr *old_hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+ unsigned int cause;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *cause_buf;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_party_id *to = &data->to;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_party_id *from = &data->from;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_uri *base = PJSIP_MSG_TO_HDR(tdata->msg)->uri;</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%);">+ hdr = pjsip_from_hdr_create(tdata->pool);</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr->type = PJSIP_H_OTHER;</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr->sname = hdr->name = history_info_name;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ name_addr = pjsip_uri_clone(tdata->pool, base);</span><br><span style="color: hsl(120, 100%, 40%);">+ uri = pjsip_uri_get_uri(name_addr->uri);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* if no redirecting information, then TO is the original destination */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (from->number.valid && !ast_strlen_zero(from->number.str)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_strdup2(tdata->pool, &name_addr->display, from->name.str);</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_strdup2(tdata->pool, &uri->user, from->number.str);</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%);">+ param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);</span><br><span style="color: hsl(120, 100%, 40%);">+ param->name = index_name;</span><br><span style="color: hsl(120, 100%, 40%);">+ param->value = first_index;</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%);">+ pj_list_insert_before(&hdr->other_param, param);</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr->uri = (pjsip_uri *) name_addr;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ while ((old_hdr = pjsip_msg_find_hdr_by_name(tdata->msg, &history_info_name, NULL)) != NULL) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_list_erase(old_hdr);</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%);">+ pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!to->number.valid || ast_strlen_zero(to->number.str)) {</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%);">+ hdr = pjsip_from_hdr_create(tdata->pool);</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr->type = PJSIP_H_OTHER;</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr->sname = hdr->name = history_info_name;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ name_addr = pjsip_uri_clone(tdata->pool, base);</span><br><span style="color: hsl(120, 100%, 40%);">+ uri = pjsip_uri_get_uri(name_addr->uri);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_strdup2(tdata->pool, &name_addr->display, to->name.str);</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_strdup2(tdata->pool, &uri->user, to->number.str);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);</span><br><span style="color: hsl(120, 100%, 40%);">+ param->name = index_name;</span><br><span style="color: hsl(120, 100%, 40%);">+ param->value = last_index;</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_list_insert_before(&hdr->other_param, param);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ param = PJ_POOL_ALLOC_T(tdata->pool, pjsip_param);</span><br><span style="color: hsl(120, 100%, 40%);">+ param->name = cause_name;</span><br><span style="color: hsl(120, 100%, 40%);">+ cause = reason_code_to_cause(&data->reason);</span><br><span style="color: hsl(120, 100%, 40%);">+ cause_buf = pj_pool_alloc(tdata->pool, 4);</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(cause_buf, 4, "%ud", cause);</span><br><span style="color: hsl(120, 100%, 40%);">+ param->value = pj_str(cause_buf);</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_list_insert_before(&uri->other_param, param);</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr->uri = (pjsip_uri *) name_addr;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static void get_redirecting_add_diversion(struct ast_sip_session *session, pjsip_tx_data *tdata)</span><br><span> {</span><br><span> struct ast_party_redirecting *data;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ add_supported(tdata);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> if (session->channel && session->endpoint->id.send_diversion &&</span><br><span> (data = ast_channel_redirecting(session->channel))->count) {</span><br><span> add_diversion_header(tdata, data);</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (session->channel && session->endpoint->id.send_history_info) {</span><br><span style="color: hsl(120, 100%, 40%);">+ data = ast_channel_redirecting(session->channel);</span><br><span style="color: hsl(120, 100%, 40%);">+ add_history_info_header(tdata, data);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> }</span><br><span> </span><br><span> /*!</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/14838">change 14838</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/+/14838"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I2296369582d4b295c5ea1e60bec391dd1d318fa6 </div>
<div style="display:none"> Gerrit-Change-Number: 14838 </div>
<div style="display:none"> Gerrit-PatchSet: 11 </div>
<div style="display:none"> Gerrit-Owner: Torrey Searle <tsearle@gmail.com> </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-Reviewer: Richard Mudgett <rmudgett@digium.com> </div>
<div style="display:none"> Gerrit-CC: Stanislav Abramenkov <stas.abramenkov@gmail.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>