1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Check out the 6 new Certified Incredible PBX Builds for Asterisk 11 and 13 featuring CentOS 6, Ubuntu 14, Raspberry Pi 2, and Asterisk-NOW.
    Dismiss Notice

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

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

  1. jehowe

    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:

    Diversion: <sip:+13125551212@>;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

    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:
    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
    ${SIP_HEADER(Diversion):offset:length} **
    ** see post below
  3. jehowe

    jehowe Guru

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

  4. lgaetz

    lgaetz Pundit

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

  5. jehowe

    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.

    $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

    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:

    set(string=Diversion: <sip:+13125551212@>;privacy=full;screen=no; reason=unknown; counter=1)
    Set(foo=$["${string}" : "${regex}"])
        -- Executing [90210@injection-6:3] NoOp("SIP/202-00000077", "Diversion: <sip:+13125551212@>") 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

    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