<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Hello,</p>
    <p>thanks for sharing, might help others facing same issue!</p>
    <p>Cheers,<br>
      Daniel<br>
    </p>
    <br>
    <div class="moz-cite-prefix">On 26.06.18 18:43, Pali Rohár wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:20180626164348.jhqutnvmroez2al7@pali">
      <pre wrap="">Hi! Thank you very much for hints. I was able to fix those invalid XML
bodies produced by Linphone by following kamailio.cfg code:

  # Fix invalid XML produced by Linphone
  if(has_body("application/pidf+xml") && search_hf("User-Agent", "Linphone", "a")) {
          subst_body('/(<presence.*entity=".*)<(<a class="moz-txt-link-freetext" href="sip:.*">sip:.*</a>)>(".*>)/\1&lt;\2&gt;\3/g');
          subst_body('/(<contact[^>]*>.*)<(<a class="moz-txt-link-freetext" href="sip:.*">sip:.*</a>)>(.*<\/contact>)/\1&lt;\2&gt;\3/g');
          msg_apply_changes();
  }

I put it into "route[PRESENCE]" section inside "if(is_method("PUBLISH")) {".

It is a hack, but it is working fine. Linphone now can show online
status correctly.

On Tuesday 26 June 2018 10:45:07 Daniel-Constantin Mierla wrote:
</pre>
      <blockquote type="cite">
        <pre wrap="">Hello,

if you want to try to fix it inside kamailio.cfg, look at the
subst/replace functions from textops module and once those operations
are done, do msg_apply_changes() from textopsx module.

Cheers,
Daniel


On 25.06.18 16:03, Pali Rohár wrote:
</pre>
        <blockquote type="cite">
          <pre wrap="">Ah, seems you are right. It is invalid XML. I have not spotted that when
doing packet dumps.

So I would report bug to linphone project. I (as a user) have no control
on what linphone SIP client send to server...

It looks like that linphone forgot to encode entity="..." and content of
<contact>. Is there any way to "fix" these two problems in kamailio
server? Probably as doing some replace filter in kamailio config file
for incoming packets?

Because current version of linphone is widely used and it would take
time if linphone fixes this issue and users updates current broken
version.

On Monday 25 June 2018 15:47:19 Daniel-Constantin Mierla wrote:
</pre>
          <blockquote type="cite">
            <pre wrap="">Hello,

the error is printed because the parsing of the XML body in PUBLISH
fails -- an invalid XML document. Quickly looking at it:


<?xml version="1.0" encoding="UTF-8"?>
        <presence xmlns="urn:ietf:params:xml:ns:pidf"
xmlns:dm="urn:ietf:params:xml:ns:pidf:data-model"
xmlns:rpid="urn:ietf:params:xml:ns:pidf:rpid" entity="User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>">
        <tuple id="sg89ae">
        <status><basic>closed</basic></status>
        <contact priority="0.8">User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a></contact>
        </tuple>
        </presence>


the issue likely is the content of the <contact>...</contact>, it has
<a class="moz-txt-link-rfc2396E" href="sip:..."><sip:...></a> which is an opening tag from XML point of view. Either encode
< and > there, or put the sip address without them.

Cheers,
Daniel

On 23.06.18 22:21, Pali Rohár wrote:
</pre>
            <blockquote type="cite">
              <pre wrap="">Hi!

I'm using linphone 3.6.1 as SIP client and Kamailio 4.4.4 as proxy
server and registrar.

And every time I try to change online status (presence) in linphone
client, I see following error messages in kamailio server log:

ERROR: presence_xml [add_events.c:167]: xml_publ_handl(): bad body format
ERROR: presence [publish.c:443]: handle_publish(): in event specific publish handling
ERROR: tm [t_reply.c:533]: _reply_light(): ERROR: _reply_light: can't generate 500 reply when a final 415 was sent out
ERROR: sl [sl.c:269]: send_reply(): failed to reply stateful (tm)
ERROR: presence [utils_func.c:146]: send_error_reply(): sending 500 Server Internal Error reply
ERROR: presence [publish.c:492]: handle_publish(): failed to send error reply

So it looks like kamailio is not able to parse presence change which is
linphone doing. I captured traffic via tcpdump what linphone sends:

21:58:58.704792 IP (tos 0x68, ttl 64, id 64109, offset 0, flags [DF], proto UDP (17), length 32)
    LINPHONE_IP_ADDRESS.5060 > KAMAILIO_IP_ADDRESS.5060: SIP
21:58:58.704919 IP (tos 0x68, ttl 64, id 64110, offset 0, flags [DF], proto UDP (17), length 752)
    LINPHONE_IP_ADDRESS.5060 > KAMAILIO_IP_ADDRESS.5060: SIP, length: 724
        PUBLISH <a class="moz-txt-link-freetext" href="sip:USER@HOST">sip:USER@HOST</a> SIP/2.0
        Via: SIP/2.0/UDP LINPHONE_IP_ADDRESS:5060;rport;branch=z9hG4bK2049418743
        From: User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>;tag=184282924
        To: User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>
        Call-ID: 1435474953
        CSeq: 26 PUBLISH
        Content-Type: application/pidf+xml
        Max-Forwards: 70
        User-Agent: Linphone/3.6.1 (eXosip2/4.1.0)
        Expires: 600
        Event: presence
        Content-Length:   353

        <?xml version="1.0" encoding="UTF-8"?>
        <presence xmlns="urn:ietf:params:xml:ns:pidf" xmlns:dm="urn:ietf:params:xml:ns:pidf:data-model" xmlns:rpid="urn:ietf:params:xml:ns:pidf:rpid" entity="User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>">
        <tuple id="sg89ae">
        <status><basic>closed</basic></status>
        <contact priority="0.8">User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a></contact>
        </tuple>
        </presence>
21:58:58.710097 IP (tos 0x10, ttl 53, id 10255, offset 0, flags [none], proto UDP (17), length 473)
    KAMAILIO_IP_ADDRESS.5060 > LINPHONE_IP_ADDRESS.5060: SIP, length: 445
        SIP/2.0 407 Proxy Authentication Required
        Via: SIP/2.0/UDP LINPHONE_IP_ADDRESS:5060;rport=5060;branch=z9hG4bK2049418743;received=EXTERNAL_IP_ADDRESS
        From: User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>;tag=184282924
        To: User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>;tag=515cb47c4c34ffa85b598d6b25676122.744d
        Call-ID: 1435474953
        CSeq: 26 PUBLISH
        Proxy-Authenticate: Digest realm="HOST", nonce="NONCE"
        Server: kamailio (4.4.4 (x86_64/linux))
        Content-Length: 0

21:58:58.718147 IP (tos 0x68, ttl 64, id 64111, offset 0, flags [DF], proto UDP (17), length 940)
    LINPHONE_IP_ADDRESS.5060 > KAMAILIO_IP_ADDRESS.5060: SIP, length: 912
        PUBLISH <a class="moz-txt-link-freetext" href="sip:USER@HOST">sip:USER@HOST</a> SIP/2.0
        Via: SIP/2.0/UDP LINPHONE_IP_ADDRESS:5060;rport;branch=z9hG4bK1165141043
        From: User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>;tag=184282924
        To: User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>
        Call-ID: 1435474953
        CSeq: 27 PUBLISH
        Proxy-Authorization: Digest username="USER", realm="HOST", nonce="NONCE", uri=<a class="moz-txt-link-rfc2396E" href="sip:USER@HOST">"sip:USER@HOST"</a>, response="RESP", algorithm=MD5
        Content-Type: application/pidf+xml
        Max-Forwards: 70
        User-Agent: Linphone/3.6.1 (eXosip2/4.1.0)
        Expires: 600
        Event: presence
        Content-Length:   353

        <?xml version="1.0" encoding="UTF-8"?>
        <presence xmlns="urn:ietf:params:xml:ns:pidf" xmlns:dm="urn:ietf:params:xml:ns:pidf:data-model" xmlns:rpid="urn:ietf:params:xml:ns:pidf:rpid" entity="User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>">
        <tuple id="sg89ae">
        <status><basic>closed</basic></status>
        <contact priority="0.8">User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a></contact>
        </tuple>
        </presence>

21:58:58.781668 IP (tos 0x10, ttl 53, id 10259, offset 0, flags [none], proto UDP (17), length 380)
    KAMAILIO_IP_ADDRESS.5060 > LINPHONE_IP_ADDRESS.5060: SIP, length: 352
        SIP/2.0 415 Unsupported media type
        Via: SIP/2.0/UDP LINPHONE_IP_ADDRESS:5060;rport=5060;branch=z9hG4bK1165141043;received=EXTERNAL_IP_ADDRESS
        From: User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>;tag=184282924
        To: User <a class="moz-txt-link-rfc2396E" href="sip:USER@HOST"><sip:USER@HOST></a>;tag=97d8e785fdf42bf9622a64c13c504961-3901
        Call-ID: 1435474953
        CSeq: 27 PUBLISH
        Server: kamailio (4.4.4 (x86_64/linux))
        Content-Length: 0

I replaced ip addresses in packets by KAMAILIO_IP_ADDRESS,
LINPHONE_IP_ADDRESS and EXTERNAL_IP_ADDRESS strings and also SIP account
by USER@HOST. Maybe it helps you.

Any idea why kamailio refuse presence update and reports those error
into error log?

Or is there any special setting which is needed for linphone or other
SIP clients for online status / presence support?



_______________________________________________
Kamailio (SER) - Users Mailing List
<a class="moz-txt-link-abbreviated" href="mailto:sr-users@lists.kamailio.org">sr-users@lists.kamailio.org</a>
<a class="moz-txt-link-freetext" href="https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users">https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users</a>
</pre>
            </blockquote>
          </blockquote>
        </blockquote>
        <pre wrap="">
</pre>
      </blockquote>
      <pre wrap="">
</pre>
      <!--'"--><br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Kamailio (SER) - Users Mailing List
<a class="moz-txt-link-abbreviated" href="mailto:sr-users@lists.kamailio.org">sr-users@lists.kamailio.org</a>
<a class="moz-txt-link-freetext" href="https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users">https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Daniel-Constantin Mierla -- <a class="moz-txt-link-abbreviated" href="http://www.asipto.com">www.asipto.com</a>
<a class="moz-txt-link-abbreviated" href="http://www.twitter.com/miconda">www.twitter.com/miconda</a> -- <a class="moz-txt-link-abbreviated" href="http://www.linkedin.com/in/miconda">www.linkedin.com/in/miconda</a>
Kamailio World Conference -- <a class="moz-txt-link-abbreviated" href="http://www.kamailioworld.com">www.kamailioworld.com</a></pre>
  </body>
</html>