callerid in rpid / opensips / kamailio

callerid in rpid / opensips / kamailio

  • Written by
    Walter Doekes
  • Published on

For reuse, an OpenSIPS/Kamailio snippet to translate commonly used SIP (VoIP) phone Caller-ID (CLI) headers into a single one (Remote-Party-ID).

It tries these headers in order, to do a best guess of what the caller wants:
P-Asserted-Identity (with Privacy)
P-Preferred-Identity (with Privacy)
Remote-Party-ID
From

Of course you’ll have to run the found CLI against an allow list, but this code expects that to be done on the next hop.

route[sub_cli_as_rpid] {
    $var(tmp_name) = ""; # (nothing)
    $var(tmp_num) = "Anonymous"; # (unknown)
    $var(tmp_clir) = 0;

    # PAI/PPI-privacy
    if (is_present_hf("Privacy")) {
        if (!is_privacy("none")) {
            $var(tmp_name) = "Anonymous";
            $var(tmp_clir) = 1;
        }
    # RPID-privacy
    } else if ($re) {
        if ($(hdr(Remote-Party-ID)[0]) =~ ";privacy=" &&
            !($(hdr(Remote-Party-ID)[0]) =~ ";privacy=off(;|$)")) {
            $var(tmp_name) = "Anonymous";
            $var(tmp_clir) = 1;
        }
    # From-privacy
    } else if ($fU == "Anonymous" || $fU == "anonymous" || $fU == "Unavailable") {
        $var(tmp_name) = "Anonymous";
        $var(tmp_clir) = 1;
    }

    # PAI-cli
    if ($ai) {
        $var(tmp_name) = $(hdr(P-Asserted-Identity)[0]{nameaddr.name}); # including dquotes
        $var(tmp_num) = $(ai{uri.user});
    # PPI-cli
    } else if ($pU) {
        $var(tmp_name) = $pn; # including dquotes
        $var(tmp_num) = $pU;
    # RPID-cli
    } else if ($re) {
        $var(tmp_name) = $(hdr(Remote-Party-ID)[0]{nameaddr.name}); # including dquotes
        $var(tmp_num) = $(re{uri.user});
    # From-cli
    } else {
        if ($fn) {
            $var(tmp_name) = $fn;
        }
        $var(tmp_num) = $fU;

        # Privacy from fromname?
        if ($fn == "Anonymous" || $fn == "\"Anonymous\"" ||
            $fn == "anonymous" || $fn == "\"anonymous\"") {
            $var(tmp_clir) = 1;
        }
    }

    # Remove all related headers.
    remove_hf("Privacy");
    remove_hf("P-Asserted-Identity");
    remove_hf("P-Preferred-Identity");
    remove_hf("Remote-Party-ID");

    # Add a single new one.
    if ($var(tmp_name)) { pv_printf("$var(tmp_name)", "$var(tmp_name) "); } # add space
    if ($var(tmp_clir)) { $var(tmp_clir) = "full"; } else { $var(tmp_clir) = "off"; }
    # (We could add "screen=yes" or "screen=no", but that's beyond the
    # scope of this.)
    append_hf("Remote-Party-ID: $var(tmp_name)<sip:$var(tmp_num)@$si>;privacy=$var(tmp_clir);screen=no\r\n");
}

This was tested with OpenSIPS 1.7, but it should be compatible with newer version and Kamailio as well.


Back to overview Newer post: darwin / sed / limited regular expressions Older post: more or less useless tips and tricks 2