<h3>Description</h3>

<p>I received a wireshark trace from a team member showing that a tel: uri was translated to a sip: uri in a very strange (invalid) way:</p>
<p>INVITE tel:491234567891;phone-context=ims.mnc001.mcc001.3gppnetwork.org</p>
<p>Was translated to:</p>
<p>INVITE  sip:491234567891;phone-context=<a href="mailto:ims.mnc001.mcc001.3gppnetwork.org@ims.mnc001.mcc001.3gppnetwork.org">ims.mnc001.mcc001.3gppnetwork.org@ims.mnc001.mcc001.3gppnetwork.org</a>;user=phone</p>
<h3>Troubleshooting</h3>
<p>I first thought this was definitely an error, and only the phone number should be copied to the sip: uri. But, on further reading of the specification, (<a href="https://www.ietf.org/rfc/rfc3261.txt">https://www.ietf.org/rfc/rfc3261.txt</a>), I see that it is in fact legal to leave all parameters in. Though, when the phone-context parameter is simply a domain, it would at least seem the cleanest thing would be to remove it when the exact same domain is also inserted after the @ in the sip message.</p>
<p>So, looking to find where the message is translated, I saw in the scscf.cfg configuration file that there is a translation specified.</p>
<p>I found that "tel2sip" is mapped to the C function of the same name tel2sip()</p>
<p>In this function, the translation will do exactly what I found in the trace, in other words, it will leave in all parameters separated by semi-colon, inserted before the @ symbol.</p>
<p>But, reading the spec, I see that the spec is not being followed in any case. The spec states that all parameters included when translating, must be switched to alphabetic order, with the exception that<br>
isdn-subaddress and post-dial, must occur first and in that order.  There is no code in tel2sip() to perform that sorting.</p>
<p>Also, the parser removes '(', ')', '.', and '-' in the phone number, as specified, but, these same characters could be scattered anywhere in invalid places, and they are just silently removed, '+' can scattered anywhere and it is just left alone, Other characters can be scattered anywhere and they will just be left as-is, no error. So, I feel there should be some minimal checking of the phone number, trying to follow the spec (if we could just execute the BNF from the spec!).</p>
<h4>Reproduction</h4>

<p>This can be reproduced by sending a tel: uri with parameters inserted separated by semicolon.</p>
<h4>Debugging Data</h4>

<pre><code>(none)
</code></pre>
<h4>Log Messages</h4>

<pre><code>(none)
</code></pre>
<h4>SIP Traffic</h4>

<pre><code>See attached file
</code></pre>
<h3>Possible Solutions</h3>

<p>The solution would be to modify tel2sip(), adding the sorting of parameters, putting the two exceptions first if they exist, before inserting back into the string.</p>
<p>We should also throw errors on obvious bad formatting and ilegal characters, etc, in the input tel: uri</p>
<p>Should we remove the "phone-context" if it is a domain and also exactly the same as the domain inserted after the @?</p>
<h3>Additional Information</h3>
<ul>
<li><strong>Kamailio Version</strong> - output of <code>kamailio -v</code></li>
</ul>
<pre><code>version: kamailio 5.0.0-dev5 (x86_64/linux) da4ef4-dirty
flags: STATS: Off, USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, 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
ADAPTIVE_WAIT_LOOPS=1024, MAX_RECV_BUFFER_SIZE 262144, MAX_LISTEN 16, MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.
id: da4ef4 -dirty
compiled on 10:37:40 May 12 2017 with gcc 5.4.0
</code></pre>
<p>SIP spec:<br>
<a href="https://www.ietf.org/rfc/rfc3261.txtWireshark">https://www.ietf.org/rfc/rfc3261.txtWireshark</a> output:</p>
<p><a href="https://user-images.githubusercontent.com/4886876/27705203-cfd3c150-5cd2-11e7-9f77-1664a51e7ac3.png" target="_blank"><img src="https://user-images.githubusercontent.com/4886876/27705203-cfd3c150-5cd2-11e7-9f77-1664a51e7ac3.png" alt="wireshark-tel2sip" style="max-width:100%;"></a></p>
<pre><code>In RFC 3966, Tel URI format:
 
   The "tel" URI has the following syntax:
 
   telephone-uri        = "tel:" telephone-subscriber
   telephone-subscriber = global-number / local-number
   global-number        = global-number-digits *par
   local-number         = local-number-digits *par context *par
   par                  = parameter / extension / isdn-subaddress
   isdn-subaddress      = ";isub=" 1*uric
   extension            = ";ext=" 1*phonedigit
   context              = ";phone-context=" descriptor
   descriptor           = domainname / global-number-digits
   global-number-digits = "+" *phonedigit DIGIT *phonedigit
   local-number-digits  =
      *phonedigit-hex (HEXDIG / "*" / "#")*phonedigit-hex
   domainname           = *( domainlabel "." ) toplabel [ "." ]
   domainlabel          = alphanum
                          / alphanum *( alphanum / "-" ) alphanum
   toplabel             = ALPHA / ALPHA *( alphanum / "-" ) alphanum
   parameter            = ";" pname ["=" pvalue ]
   pname                = 1*( alphanum / "-" )
   pvalue               = 1*paramchar
   paramchar            = param-unreserved / unreserved / pct-encoded
   unreserved           = alphanum / mark
   mark                 = "-" / "_" / "." / "!" / "~" / "*" /
                          "'" / "(" / ")"
   pct-encoded          = "%" HEXDIG HEXDIG
   param-unreserved     = "[" / "]" / "/" / ":" / "&" / "+" / "$"
   phonedigit           = DIGIT / [ visual-separator ]
   phonedigit-hex       = HEXDIG / "*" / "#" / [ visual-separator ]
   visual-separator     = "-" / "." / "(" / ")"
   alphanum             = ALPHA / DIGIT
   reserved             = ";" / "/" / "?" / ":" / "@" / "&" /
                          "=" / "+" / "$" / ","
   uric                 = reserved / unreserved / pct-encoded
</code></pre>
<ul>
<li><strong>Operating System</strong>:</li>
</ul>

<pre><code># uname -a
Linux epc-enablers 4.4.0-51-generic #72-Ubuntu SMP Thu Nov 24 18:29:54 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
</code></pre>

<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/1173">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZRN2gy73TVmUx1bnRtiuR64RYIw0ks5sJAOYgaJpZM4OJzVh">mute the thread</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AF36ZQk5iDpyrreoNamHyYN-R4-RpP0pks5sJAOYgaJpZM4OJzVh.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/1173"></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":"Incorrect translation of tel: uri to sip: uri in tel2sip() (#1173)"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1173"}}}</script>