<h3>Description</h3>
<p>The issue started with this improvement:<br>
<a href="https://github.com/kamailio/kamailio/commit/839cf89b02f8817156487a960ff62013e3cde530" class="commit-link"><tt>839cf89</tt></a><br>
(it works fine for single dialog bodies, but make a mess with multiple-dialog bodies)</p>
<h3>Troubleshooting</h3>
<p><strong>Test scheme, preconditions:</strong></p>
<ul>
<li>SIP server: kamailio + Asterisk</li>
<li>3 SIP phones. UA-A, UA-B, UA-C</li>
</ul>
<p>Kamailio is configured with presence+presence_dialoginfo modules.<br>
Asterisk sends PUBLISHes regarding changes of dialog's state, kamailio process it (handle_publish), and send NOTIFIYs to all subscribed endpoints</p>
<p><strong>Bug description</strong> (the issue is considered from the point of view of UA-B's state):</p>
<ul>
<li>UA-A calls to UA-B => Asterisk send PUBLISHes (dialog UA-B1), UA-B (states on-the Trying -> Proceeding -> Confirmed)</li>
<li>UA-B makes attended transfer to UA-C (call answered) => Asterisk send PUBLISHes with complex XML (2 dialogs state in a body. UA-B1(Confirmed) and new UA-B2 (Confirmed))</li>
<li>UA-B completes the transfer, as a result, Asterisk sends (bodies from this 2 PUBLISHes are below, in the SIP traffic section):
<ol>
<li>PUBLISH with multiple dialog body (UA-B1 - terminated, UA-B2 - confirmed) => processed OK</li>
<li>PUBLISH with single dialog body (UA-B2 - terminated.) => <strong>Bug is here</strong><br>
so now:</li>
</ol>
</li>
</ul>
<pre><code>presentity.c:1084 checks is_dialog_terminated // check is dialog terminated for event: dialog
  presentity.c:549 calls get_dialog_state 
       //   make a db query with the previous state (that query returnes: (UA-B1 - terminated, UA-B2 - confirmed))
       //   loop through result returned from DB, call parse_dialog_state_from_body
         presentity.c:307  - at this function:
                    ... 
                    node = xmlNodeGetChildByName(node, "dialog"); // return part of xml with 2 dialogs
                    ...
                    childNode = xmlNodeGetChildByName(node, "state"); // return state of the first! dialog in a list, not the current dialog
                    ...
due to obtained state is "terminated", if(is_dialog_terminated(presentity))  returnes TRUE
and subsequent code sends silently 200OK.
</code></pre>
<p><strong>Result:</strong></p>
<ul>
<li>those subscribed to dialog state of UA-B, do not recieve NOTIFY with a valid state (BLFs remain "lighting")</li>
</ul>
<p><strong>Attempts of workaround:</strong></p>
<ul>
<li>I tried to go to "send_412" logic when  is_dialog_terminated (TRUE).</li>
<li>But in the subsequent PUBLISH (state=terminated) Asterisk does not insert "call-id, to-tag, from-tag" parameters, and endpoint fails to recognize which dialog is terminated, and BLF remains to light.</li>
</ul>
<h4>SIP Traffic</h4>
<p>not SIP, but only relevant part of XML bodies.<br>
Multiple dialogs body, this one is last to succeed (look at the state of <strong>first</strong> dialog)</p>
<pre><code><?xml version="1.0"?>
<dialog-info xmlns="urn:ietf:params:xml:ns:dialog-info" version="36" state="full" entity="252">
<dialog id="3bf1a5de53394e394f7b0241067faccf" call-id="3bf1a5de53394e394f7b0241067faccf" local-tag="as27f0e6f0" remote-tag="1349583034" remote-uri="sip:252@10.100.1.24:5064" local-uri="sip:251@127.0.0.1:6050" direction="recipient">
<note>Ready</note>
<remote>
<identity>252</identity>
<target uri="252">
</target>
</remote>
<local>
<identity>251</identity>
<target uri="251"/>
</local>
<state>terminated</state>
</dialog>
<dialog id="4070150262@10.100.1.24" call-id="4070150262@10.100.1.24" local-tag="as7c372911" remote-tag="3861741811" remote-uri="sip:252@10.100.1.24:5064" local-uri="sip:254@10.100.1.85:6050" direction="initiator">
<note>On the phone</note>
<remote>
<identity display="user252">sip:252@10.100.1.85</identity>
<target uri="sip:252@10.100.1.85">
</target>
</remote>
<local>
<identity display="user254">sip:254@10.100.1.85</identity>
<target uri="sip:254@10.100.1.85"/>
</local>
<state>confirmed</state>
</dialog>
</dialog-info>
</code></pre>
<p>Next PUBLISH is with single dialog, and its update is ignored (as incorrect state evaluated)</p>
<pre><code><?xml version="1.0"?>
<dialog-info xmlns="urn:ietf:params:xml:ns:dialog-info" version="37" state="full" entity="252">
<dialog id="4070150262@10.100.1.24" call-id="4070150262@10.100.1.24" local-tag="as7c372911" remote-tag="3861741811" remote-uri="sip:252@10.100.1.24:5064" local-uri="sip:254@10.100.1.85:6050" direction="initiator">
<note>Ready</note>
<remote>
<identity>252</identity>
<target uri="252">
</target>
</remote>
<local>
<identity>254</identity>
<target uri="254"/>
</local>
<state reason="local-bye">terminated</state>
</dialog>
</dialog-info>
</code></pre>
<h3>Possible Solutions</h3>
<p>Sorry for the long story, but for now I need your advice, with the logic for the fix (I already tried to start to fix, but get stuck with the logical concept).<br>
My current insight is quite cumbersome:</p>
<ul>
<li>before is_dialog_terminated => get an array of dialogs call-ids from the body of current PUBLISH. Give it as input parameter for is_dialog_terminated</li>
<li>while processing get_dialog_state/parse_dialog_state_from_body = try to match state of matched dialogs</li>
<li>in some way try to update not complete body in a DB, but make some "partial" update. (update only those dialogs  whose old  state is NOT "terminated")</li>
</ul>
<p>Thanks in advance for your advice. If the issue unclear from my description, please ask for details.</p>
<h3>Additional Information</h3>
<ul>
<li><strong>Kamailio Version</strong> - output of <code>kamailio -v</code></li>
</ul>
<p>kamailio -v<br>
version: kamailio 5.0.3 (i386/linux)<br>
flags: STATS: Off, EXTRA_DEBUG, USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MEM, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLACKLIST, HAVE_RESOLV_RES<br>
ADAPTIVE_WAIT_LOOPS=1024, MAX_RECV_BUFFER_SIZE 262144, MAX_LISTEN 16, MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB<br>
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.<br>
id: unknown<br>
compiled on 11:27:38 Jan 30 2018 with gcc 4.3.2</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/1427">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZYPTvXfJs4rBQBhQ5YdootmdHBOrks5tRt0JgaJpZM4R5QB3">mute the thread</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AF36ZYkxeiJOy7ux84erS_09f4rrojSbks5tRt0JgaJpZM4R5QB3.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/kamailio/kamailio/issues/1427"></link>
  <meta itemprop="name" content="View Issue"></meta>
</div>
<meta itemprop="description" content="View this Issue on GitHub"></meta>
</div>

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/kamailio/kamailio","title":"kamailio/kamailio","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/kamailio/kamailio"}},"updates":{"snippets":[{"icon":"DESCRIPTION","message":"presence: incorrect dialog state for multiple dialogs xml body (#1427)"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1427"}}}</script>