[OpenSIPS-Users] help with drouting

Justin Moore jmoore at sagisys.com
Tue Dec 1 23:46:22 CET 2009


Hello,

I'm trying to adapt my current Opensips 1.5 installation to use drouting such that a call will try the least cost route first, and if that gateway is not available, then it will fail over to the next gateway in the list. I've already set up Opensips-CP and have made the necessary database modifications, as well as added the proper loadmodule and modparam blocks for drouting.so, but I'm not sure how to proceed in the opensips.cfg, and I haven't been able to find any tutorials that cover this. If someone could point me in the right direction of a good tutorial, or make recommendations about my opensips.cfg file (routing portion included below) I would appreciate it.

Thank you!

Justin






########################################################################
# Request route 'main'
########################################################################
route[0]
{


        $avp(s:billing_party) = $Au;
        $avp(s:can_uri) = $ru;
        $avp(s:secs)="7";



        xlog("L_INFO", "New request - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
        force_rport();
        if(msg:len > max_len)
        {

                xlog("L_INFO", "Message too big - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                sl_send_reply("513", "Message Too Big");
                exit;
        }
        if (!mf_process_maxfwd_header("10"))
        {

                xlog("L_INFO", "Too many hops - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                sl_send_reply("483", "Too Many Hops");
                exit;
        }
        if(!is_method("REGISTER"))
        {
                if(nat_uac_test("19"))
                {
                        record_route(";nat=yes");
                }
                else
                {
                        record_route();
                }
        }
        if(is_method("CANCEL") || is_method("BYE"))
        {


                #Account BYE transactions
                sleep("$avp(s:secs)");
                xlog("L_INFO", "SENT TO RADIUS ACCOUNTING");
                $avp(s:billing_party) = $Au;
                $avp(s:sip_proxy_ip)="38.110.28.140";
                $avp(s:can_uri) = $ru;
                setflag(25);
                setflag(26);
                #end_media_session();
        }
        if(loose_route())
        {

                 if (is_method("BYE")) {

                        $avp(s:billing_party) = $Au;
                        $avp(s:can_uri) = $ru;
                        $avp(s:sip_proxy_ip)="38.110.28.140";
                        setflag(25);
                        setflag(26);
                }


                if(!has_totag())
                {

                        xlog("L_INFO", "Initial loose-routing rejected - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                        sl_send_reply("403", "Initial Loose-Routing Rejected");
                        exit;
                }
                if(nat_uac_test("19") || search("^Route:.*;nat=yes"))
                {
                        fix_nated_contact();
                        setbflag(6);
                }

                route(3);
        }
        if(is_method("REGISTER"))
        {
                route(2);
        }
        if(is_method("INVITE"))
        {

                engage_media_proxy();
               # Canonical URI
                $avp(s:can_uri) = $ru;
                $avp(s:billing_party) = $Au;
                $avp(s:sip_proxy_ip)="38.110.28.140";
                setflag(25);
                setflag(26);
                setflag(4);
                route(4);
        }
        if(is_method("CANCEL") || is_method("ACK"))
        {

                # Canonical URI
                $avp(s:can_uri) = $ru;
                $avp(s:billing_party) = $Au;
                $avp(s:sip_proxy_ip)="38.110.28.140";
                setflag(25);
                setflag(26);

                route(8);
        }


        $avp(s:can_uri) = $ru;
        $avp(s:billing_party) = $Au;
        $avp(s:sip_proxy_ip)="38.110.28.140";
        setflag(25);
        setflag(26);



        route(9);
}

########################################################################
# Request route 'stop-media-proxy'
########################################################################
route[1]
{
        if(isflagset(22))
        {
                #end_media_session();
        }

}

########################################################################
# Request route 'base-route-register'
########################################################################
route[2]
{
        sl_send_reply("100", "Trying");
        if(!www_authorize("", "subscriber")) 
        {

                xlog("L_INFO", "Register authentication failed - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                www_challenge("", "0");
                exit;
        }
        if(!check_to()) 
        {

                xlog("L_INFO", "Spoofed To-URI detected - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                sl_send_reply("403", "Spoofed To-URI Detected");
                exit;
        }
        consume_credentials();
        if(!search("^Contact:[ ]*\*") && nat_uac_test("19")) 
        {
                fix_nated_register();
                setbflag(6);
        }
        if(!save("location")) 
        {

                xlog("L_ERR", "Saving contact failed - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                sl_reply_error();
                exit;
        }

        xlog("L_INFO", "Registration successful - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
        exit;

}

########################################################################
# Request route 'base-outbound'
########################################################################
route[3]
{
        if(isbflagset(6))
        {
                if(!isflagset(22) && !search("^Content-Length:[ ]*0"))
                {
                        setflag(22);
                        #use_media_proxy();
                }

                t_on_reply("2");
        }
        else
        {

                t_on_reply("1");
        }
        if(!isflagset(21))
        {

                t_on_failure("2");
        }
        if(isflagset(29))
        {
                append_branch();
        }
        if(is_present_hf("Proxy-Authorization"))
        {
                consume_credentials();
        }

        xlog("L_INFO", "Request leaving server, D-URI='$du' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
        # no 100 (we already sent it) and no DNS blacklisting
        if(!t_relay("0x05"))
        {
                sl_reply_error();
                if(is_method("INVITE") && isbflagset(6))
                {
                        #end_media_session();
                }
        }
        exit;

}

########################################################################
# Request route 'base-route-invite'
########################################################################
route[4]
{
        sl_send_reply("100", "Trying");
        if(from_gw())
        {

                xlog("L_INFO", "Call from PSTN' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                setflag(23);
        }
        else
        {
                if(allow_trusted())
                {

                        xlog("L_INFO", "Call from trusted peer - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                        if(!is_domain_local("$rd"))
                        {

                                xlog("L_INFO", "Rejecting peering attempt with non-local request domain - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                                sl_send_reply("403", "Relaying Denied");
                                exit;
                        }
                        setflag(23);
                }
                else
                {
                        if(!proxy_authorize("", "subscriber")) 
                        {

                                xlog("L_INFO", "Proxy authentication failed - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                                proxy_challenge("", "0");
                                exit;
                        }

                        # if(!check_from()) 
                        # {

                        #       xlog("L_INFO", "Spoofed From-URI detected - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                        #       sl_send_reply("403", "Spoofed From-URI Detected");
                        #       exit;
                        # }
                }
        }
        if(nat_uac_test("19")) 
        {
                fix_nated_contact();
                setbflag(6);
        }

        route(5);
}

########################################################################
# Request route 'invite-find-callee'
########################################################################
route[5]
{
        if(alias_db_lookup("dbaliases"))
        {

                xlog("L_INFO", "Callee was aliased - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
        }
        if(!is_domain_local("$rd"))
        {
                setflag(20);

                route(7);
        }
        if(does_uri_exist())
        {

                xlog("L_INFO", "Callee is local - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                route(6);
        }
        else
        {

                xlog("L_INFO", "Callee is not local - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                route(7);
        }
        exit;

}

########################################################################
# Request route 'invite-to-internal'
########################################################################
route[6]
{
        if(!lookup("location")) 
        {

                xlog("L_INFO", "Local user offline - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                sl_send_reply("404", "User Offline");
        }
        else
        {

                xlog("L_INFO", "Local user online - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                route(3);
        }
        exit;

}

########################################################################
# Request route 'invite-to-external'
########################################################################
route[7]
{
        if(isflagset(20))
        {

                xlog("L_INFO", "Call to foreign domain - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                route(3);
                exit;
        }
        if(!isflagset(23))
        {
                # don't allow calls relaying from PSTN to PSTN, if not explicitely forwarded
                if(uri =~ "^sip:[0-9]+@")
                {
                        # only route numeric users to PSTN
                        if(!load_gws())
                        {

                                xlog("L_ERR", "Error loading PSTN gateways - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                                sl_send_reply("503", "PSTN Termination Currently Unavailable");
                                exit;
                        }
                        if(!next_gw())
                        {

                                xlog("L_ERR", "No PSTN gateways available - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                                sl_send_reply("503", "PSTN Termination Currently Unavailable");
                                exit;
                        }
                        setflag(21);

                        t_on_failure("1");
                        route(3);
                }
        }

        xlog("L_INFO", "Call to unknown user - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
        sl_send_reply("404", "User Not Found");
        exit;

}

########################################################################
# Request route 'base-route-local'
########################################################################
route[8]
{
        t_on_reply("1");
        if(t_check_trans())
        {

                xlog("L_INFO", "Request leaving server - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                if(!t_relay())
                {
                        sl_reply_error();
                }
        }
        else
        {

                xlog("L_INFO", "Dropping mis-routed request - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
        }
        exit;

}

########################################################################
# Request route 'base-route-generic'
########################################################################
route[9]
{
        xlog("L_INFO", "Method not supported - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
        sl_send_reply("501", "Method Not Supported Here");
        exit;

}

########################################################################
# Request route 'base-filter-failover'
########################################################################
route[10]
{
        if(!t_check_status("408|500|503"))
        {

                xlog("L_INFO", "No failover routing needed for this response code - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                route(1);
                exit;
        }

}

########################################################################
# Reply route 'base-standard-reply'
########################################################################
onreply_route[1]
{
        xlog("L_INFO", "Reply - S=$rs D=$rr F=$fu T=$tu IP=$si ID=$ci\n");
        exit;

}

########################################################################
# Reply route 'base-nat-reply'
########################################################################
onreply_route[2]
{
        xlog("L_INFO", "NAT-Reply - S=$rs D=$rr F=$fu T=$tu IP=$si ID=$ci\n");
        if(nat_uac_test("1"))
        {
                fix_nated_contact();
        }
        if(isbflagset(6) && status=~"(180)|(183)|2[0-9][0-9]") 
        {
                if(!search("^Content-Length:[ ]*0"))
                {
                        #use_media_proxy();
                }
        }
        exit;

}

########################################################################
# Failure route 'pstn-failover'
########################################################################
failure_route[1]
{
        xlog("L_INFO", "Failure route for PSTN entered - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
        route(10);
        if(!next_gw())
        {

                xlog("L_ERR", "Failed to select next PSTN gateway - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
                route(1);
                exit;
        }

        t_on_failure("1");
        route(3);
}

########################################################################
# Failure route 'base-standard-failure'
########################################################################
failure_route[2]
{
        route(10);
        route(1);
}



More information about the Users mailing list