1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. If you had a PIAF Forum account in the vBulletin days, log in with your old credentials. Otherwise, sign up again and we'll get you back in business as soon as we can.
  3. A serious FreePBX vulnerability has been reported. Update your Framework Module immediately. Click here for details.
  4. Critical FreePBX vulnerability! Update your server immediately. Details here.

QUESTION Help extracting the Diversion Header (RDNIS) from an INVITE packet

Discussion in 'Help' started by jehowe, Jun 14, 2013.

  1. jehowe Guru

    I have only inbound forwarded calls coming into a 1.8.x PIAF box and need to figure out a way to pull the originally dialed number (called RDNIS, the number doing the forwarding) from the INVITE packet.

    The diversion header itself looks like this:

    Code:
    Diversion: <sip:+13125551212@44.55.12.89:5060>;privacy=full;screen=no; reason=unknown; counter=1
    As inbound calls come in, I'd like to be able to read the RDNIS number (3125551212) into a context as it comes in for upstream processing.
  2. lgaetz Pundit

    Some vague ideas to get you started...
    There is the asterisk cmd 'SIP_HEADER' which may be able to extract that info
    In extensions_custom.conf possibly in [from-pstn-custom] add some lines like this:
    Code:
    exten => _X.,1,Set(varname=${SIP_HEADER(Diversion)}
    exten => _X.,n,noop(Incoming call diverted from ${varname})
    That might work, I can't test easily without setting up a forwarded PSTN number. You probably want to clean up the variable, which will be easy to do if the phone number is always the same length by setting varname to
    Code:
    ${SIP_HEADER(Diversion):offset:length} **
    ** see post below
    Last edited by lgaetz, Jun 14, 2013
  3. jehowe Guru

    Thank you lgaetz! I'll have time to work with this tonight or over the weekend, and will post the results.

    Jeff
  4. lgaetz Pundit

    I think I made an error with the second bit, it maybe should be:

    Code:
    ${${SIP_HEADER(Diversion)}:offset:length}
  5. jehowe Guru

    Thanks again lgaetz for getting me started, I had a real adventure getting it all sorted. I needed to extract the phone numbers from the 'Diversion', 'From', and 'To' headers, two of which had variable lengths, meaning the offset:length fields weren't going to work.

    I couldn't get regx to work at all and ended up using a perl script and the 'shell' function to get things cleaned up.

    For the record, here's the perl script contents I made (testcall.pl in this case) that filters the headers- finding a minimum of 10 (maximum of 15) consecutive digits within a string variable (foo) passed from asterisk.


    #!/usr/bin/perl
    $str = $ARGV[0];
    ($callnum) = ($str =~ /.+:\+?(\d{10,15}).+/);
    print "$callnum";

    Below are the lines in asterisk, the first reading the header into 'foo', the second passing the string to perl, creating'foo2' with the clean results:

    exten => _X.,n,Set(foo=${SIP_HEADER(From)})
    exten => _X.,n,Set(foo2=${SHELL(/usr/bin/perl /var/lib/asterisk/agi-bin/testcall.pl '${foo}')})

    One nagging question that cost me a couple hours was why in the line above, double quoting around the variable: "${foo}" was leading to inconsistent results depending on the header contents- the perl script was not the issue. I ended up using single quotes, '${foo}' which resolved it. I don't remember ever having issues with " variables in the past.
    lgaetz likes this.
  6. lgaetz Pundit


    Single quotes, double quotes and no quotes within Asterisk are an utter mystery to me. I struggled with Asterisk and regex's before, and FINALLY found a working example of using a regex within asterisk to extract a specific part of a string. In your case it would take this form:

    Code:
    set(string=Diversion: <sip:+13125551212@44.55.12.89:5060>;privacy=full;screen=no; reason=unknown; counter=1)
    noop(${string})
    Set(regex=^.+?:\+(.+?)@.*$)
    noop(${regex})
    Set(foo=$["${string}" : "${regex}"])
    noop(${foo})
    
    Output:
    Code:
        -- Executing [90210@injection-6:3] NoOp("SIP/202-00000077", "Diversion: <sip:+13125551212@44.55.12.89:5060>") in new stack
        -- Executing [90210@injection-6:5] NoOp("SIP/202-00000077", "^.+?:\+(.+?)@.*$") in new stack
        -- Executing [90210@injection-6:7] NoOp("SIP/202-00000077", "13125551212") in new stack
    
  7. jehowe Guru

    Thanks for the regex example! I found a lot of contradictory posts about what to try, what works/doesn't work using regex and Asterisk, and just could not get it working yesterday.

Share This Page