<div dir="ltr">Hi, I am trying to use Kamailio specific function 'subst_str' for replacing some media attributes in SDP in my module.<div><br></div><div>Finally I have resolved how to create 'subst_expr' structure so I can pass it to 'subst_str' function. I am doing something like this:<br><br><pre style="font-family:'Source Code Pro';font-size:10.5pt;background-color:rgb(43,43,43)"><span style="color:rgb(204,120,50);font-weight:bold">      struct </span><span style="color:rgb(181,182,227)">subst_expr</span><font color="#808080"> </font><font color="#a9b7c6">*seMedia</font><span style="color:rgb(204,120,50)">;
</span><pre style="font-family:'Source Code Pro';font-size:10.5pt"><span style="color:rgb(204,120,50);font-weight:bold">      char </span><font color="#a9b7c6">*pattern</font><font color="#cc7832">;
</font><span style="font-size:10.5pt"><font color="#cc7832">      </font></span><span style="color:rgb(169,183,198);font-size:10.5pt">asprintf(&pattern</span><span style="color:rgb(204,120,50);font-size:10.5pt">, </span><span style="color:rgb(106,135,89);font-size:10.5pt">"/m=audio ([0-9]{0,5})/m=audio </span><span style="color:rgb(204,120,50);font-size:10.5pt">%s</span><span style="color:rgb(106,135,89);font-size:10.5pt">/"</span><span style="color:rgb(204,120,50);font-size:10.5pt">, </span><span style="color:rgb(169,183,198);font-size:10.5pt">_host_port.</span><span style="color:rgb(147,115,165);font-size:10.5pt">s</span><span style="color:rgb(169,183,198);font-size:10.5pt">)</span><span style="color:rgb(204,120,50);font-size:10.5pt">;</span></pre><pre style="color:rgb(169,183,198);font-family:'Source Code Pro';font-size:10.5pt"><span style="color:rgb(204,120,50)">      </span><span style="color:rgb(185,188,209)">str </span>*subst<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">      </span>subst = (<span style="color:rgb(185,188,209)">str </span>*) <span style="color:rgb(144,139,37)">pkg_malloc</span>(<span style="color:rgb(204,120,50);font-weight:bold">sizeof</span>(<span style="color:rgb(185,188,209)">str</span>))<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">      </span>subst-><span style="color:rgb(147,115,165)">s </span>= pattern<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">      </span>subst-><span style="color:rgb(147,115,165)">len </span>= strlen(pattern)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">      </span>seMedia = subst_parser(subst)<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(128,128,128)"><br></span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">      </span><span style="color:rgb(204,120,50);font-weight:bold">int </span>count = <span style="color:rgb(104,151,187)">0</span><span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">      </span><span style="color:rgb(185,188,209)">str </span>*tmpBody<span style="color:rgb(204,120,50)">;</span><span style="color:rgb(128,128,128)"><br></span><span style="color:rgb(128,128,128)">      </span><span style="color:rgb(204,120,50);font-weight:bold">char const </span>*oldBody = (<span style="color:rgb(204,120,50);font-weight:bold">char const </span>*) msg-><span style="color:rgb(147,115,165)">body</span><span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)">      </span>tmpBody = subst_str(oldBody<span style="color:rgb(204,120,50)">, </span>msg<span style="color:rgb(204,120,50)">, </span>seMedia<span style="color:rgb(204,120,50)">, </span>&count)<span style="color:rgb(204,120,50)">;<br></span><span style="color:rgb(204,120,50)"><br></span><span style="color:rgb(204,120,50)">      </span><span style="color:rgb(204,120,50);font-weight:bold">if </span>(count > <span style="color:rgb(104,151,187)">0</span>) {</pre><span style="color:rgb(204,120,50)">           // do other stuff</span></pre><div>I am trying to replace all "m=audio {port number}" attributes with port 5060. I think that the regular expression is correct. But unfortunately in Kamailio log I am getting something like this:<br><br><div> 0(25049) DEBUG: <core> [re.c:329]: subst_parser(): subst_parser: ok, se is 0xb72941d8</div><div> 0(25049) DEBUG: <core> [re.c:454]: subst_run(): subst_run: running. r=1<br></div><div> 0(25049) DEBUG: <core> [re.c:523]: subst_str(): subst_str: no match</div></div><div><br></div><div>It seems like regex was not matched but after this I am printing out whole message and I can see that in SDP 'm=audio' is present. I also noticed another thing when I am printing out 'seMedia->replacement.s' in log I am getting my replacement "m=audio 5060" but also at the end of the string I can see some weird character:<br><br><img src="cid:ii_14c661d510d56261" alt="Inline image 1" style="margin-right: 25px;"><br></div><div><br></div><div>If someone came across this issue in the past and resolved it, it would be great if he/she can share his/her knowledge with me because seriously this issue is driving me crazy.</div><div><br></div><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 12, 2015 at 9:17 PM, Cockhootec Yahrabee <span dir="ltr"><<a href="mailto:cockootec@gmail.com" target="_blank">cockootec@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Thanks a lot. I am sorry I didn't code with pointers quite a long time so I need to refresh my knowledge a bit :)<div>But unfortunately it was not the main problem.</div><div><br></div><div>In Kamailio log I am getting something like this:</div><div><br></div><div><div> 0(17809) ERROR: <core> [re.c:181]: parse_repl(): subst_parser: bad specifier in replace part /$1 5060/gm</div></div><div><br></div><div>I think, that problem is on this line <a href="https://gist.github.com/ivanbarlog/1b552b57e89bf4670872#file-test-c-L5" target="_blank">https://gist.github.com/ivanbarlog/1b552b57e89bf4670872#file-test-c-L5</a> where I am trying to use $1 as a placeholder for first matched group.</div><div><br></div><div>I also tried it without it - just replace `m=audio` (not m=*) but it also didn't worked.</div><div><br></div><div>I am also not sure if I am correctly re-transmitting updated SIP message. I looked at other modules which perform the same action and I tried to inspire with their code but unfortunately it's not working.</div><div><br></div><div>Could you please review this code sample <a href="https://gist.github.com/ivanbarlog/4040ae5b735386a88ea0" target="_blank">https://gist.github.com/ivanbarlog/4040ae5b735386a88ea0</a> ?</div><div><br></div><div>Or maybe if you can point me to some resource where I can read more about it. Also it would be great if there was some tutorial for developing modules.</div><div><br></div><div>Thanks for help</div><div><div class="h5"><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 11, 2015 at 7:20 PM, Daniel-Constantin Mierla <span dir="ltr"><<a href="mailto:miconda@gmail.com" target="_blank">miconda@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    Hello,<br>
    <br>
    at least one problem is the next line in your code:<br>
    <br>
    subst->len = sizeof(pattern);<br>
    <br>
    should be:<br>
    <br>
    subst->len = strlen(pattern);<br>
    <br>
    Because patter is declared as 'char*', sizeof returns 4 or 8 (if 32b
    or 64b architecture).<br>
    <br>
    Try with that change and see how far it goes.<br>
    <br>
    Cheers,<br>
    Daniel<div><div><br>
    <br>
    <div>On 11/03/15 00:43, Cockhootec Yahrabee
      wrote:<br>
    </div>
    </div></div><blockquote type="cite"><div><div>
      <div dir="ltr">Hi guys,
        <div>I am trying to 'search&replace' some patterns in
          message body but unfortunately with no result (but bunch of
          errors).</div>
        <div><br>
        </div>
        <div>First of all I am trying to perform regex substitution as
          one that can be seen at <a href="https://regex101.com/r/vF1kZ7/1" target="_blank">https://regex101.com/r/vF1kZ7/1</a></div>
        <div><br>
        </div>
        <div>Since I have regular expression I am trying to convert it
          so it will work with `sub_str` function from re.h</div>
        <div><br>
        </div>
        <div>Here is my attempt: <a href="https://gist.github.com/ivanbarlog/1b552b57e89bf4670872#file-test-c-L12" target="_blank">https://gist.github.com/ivanbarlog/1b552b57e89bf4670872#file-test-c-L12</a></div>
        <div><br>
        </div>
        <div>I had quite lot of errors but last error I am getting is
          that `subst_parser` cannot find separator <a href="https://github.com/kamailio/kamailio/blob/master/re.c#L240" target="_blank">https://github.com/kamailio/kamailio/blob/master/re.c#L240</a></div>
        <div><br>
        </div>
        <div>Since the `struct subst_expr` is not parsed right the next
          error is segfault in `subst_str` function.</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div>If someone can explain me how exactly I should use
          `subst_str` function with my regex I'll be really grateful.
          Little example code would be also great.</div>
        <div><br>
        </div>
        <div>Thanks in advance</div>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      </div></div><pre>_______________________________________________
sr-dev mailing list
<a href="mailto:sr-dev@lists.sip-router.org" target="_blank">sr-dev@lists.sip-router.org</a>
<a href="http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev" target="_blank">http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev</a><span><font color="#888888">
</font></span></pre><span><font color="#888888">
    </font></span></blockquote><span><font color="#888888">
    <br>
    <pre cols="72">-- 
Daniel-Constantin Mierla
<a href="http://twitter.com/#!/miconda" target="_blank">http://twitter.com/#!/miconda</a> - <a href="http://www.linkedin.com/in/miconda" target="_blank">http://www.linkedin.com/in/miconda</a>
Kamailio World Conference, May 27-29, 2015
Berlin, Germany - <a href="http://www.kamailioworld.com" target="_blank">http://www.kamailioworld.com</a></pre>
  </font></span></div>

</blockquote></div><br></div></div></div></div>
</blockquote></div><br></div></div></div>