opensips.cfg 4.83 KB
####### Routing Logic ########
# main request routing logic
route{
    if (!mf_process_maxfwd_header("10")) {
        send_reply("483","Too Many Hops");
        exit;
    }

    if (is_method("OPTIONS") && is_myself("$rd")) {
        sl_send_reply("200","NAT Ping");
        exit;
    }

    t_on_branch("rtpengine");

    if (is_method("REGISTER")) {
        if (!ds_is_in_list("$si", "", "1")) {
            cache_store("local", "ip_$(fu{uri.user})", "$si");
            cache_store("local", "port_$(fu{uri.user})", "$sp");
        }
    }

    if (is_method("CANCEL|BYE")) {
        cache_remove("local", "ip_$(fu{uri.user})");
        cache_remove("local", "port_$(fu{uri.user})");
    }

    if (!has_totag()) {
        record_route();
    } else {
        if (is_method("CANCEL|BYE")) {
            rtpengine_delete();
        }
        loose_route();
        route(send_relay);
    }

    if (is_method("CANCEL")) {
        if (t_check_trans()) route(send_relay);
    }

    if (is_method("INVITE")) {
        route(send_relay);
        route(dispatcher_next);
    }

    if (is_method("REGISTER")) {
        if (client_nat_test("3")) {
            force_rport();
            fix_contact();
        }
        route(send_relay);
        route(dispatcher_next);
    }
    t_on_failure("DR_failed");
}

route[send_relay] {
    if (ds_is_in_list("$si", "", "1")) {
        xlog("L_INFO", "$ci|log|originated from internal freeswitch sources");
        setflag(26);
    } else if (ds_is_in_list("$si", "", "3")) {
        xlog("L_INFO", "$ci|log|originated from internal vos sources");
        setflag(27);
    } else {
        xlog("L_INFO", "$ci|log|originated from external sources");
    }

    t_on_reply("reply");

    if (isflagset(26)) {
        #to vos
        if ($(ru{uri.user}) == "3301xxxxxxxxxxx") {
            if (ds_select_dst("3", "0")) {
                if (!t_relay()) {
                    sl_reply_error();
                }
            }
            exit;
        }

        #to client
        if (ds_select_dst("2", "0")) {
            if ( (cache_fetch("local", "ip_$(ru{uri.user})", $var(real_ip))) &&
                 (cache_fetch("local", "port_$(ru{uri.user})", $var(real_port))) ) {
                $du = "sip:" + $var(real_ip) + ":" + $var(real_port);
                if (!t_relay()) {
                    sl_reply_error();
                }
            } else {
                send_reply("503","Service Unavailable");
            }
            exit;
        }
    } else {
        if (ds_select_dst("1", "0")) {
            if (!t_relay()) {
                sl_reply_error();
            }
            exit;
        }
    }
}

branch_route[rtpengine] {
    resetbflag(sdp_offer);

    if ( ds_is_in_list("$si", "", "1") ) {
        $var(rtpengine_flags) = "RTP/AVP replace-session-connection replace-origin ICE=remove address-family=IP4 out-iface=pub in-iface=pub";
    } else {
        $var(rtpengine_flags) = "RTP/AVP replace-session-connection replace-origin ICE=remove address-family=IP4 in-iface=priv out-iface=prive";
    }

    if ( ( (ds_is_in_list("$si", "", "1")) && ($(ru{uri.user}) == "3301xxxxxxxxxxx") ) || 
         ( ds_is_in_list("$si", "", "3") ) ) {
        xlog("=========vos <-> fs pass========\n");
    } else {
        if ( (is_method("INVITE") || is_method("ACK") || is_method("PRACK")) && has_body("application/sdp") ) {
            if( is_method("ACK") || is_method("PRACK") ) {
                rtpengine_answer("$var(rtpengine_flags)");
            } else {
                rtpengine_offer("$var(rtpengine_flags)");
                setbflag(sdp_offer);
            }
        }
    }
}

onreply_route["reply"] {
    if (ds_is_in_list("$si", "", "1")) {
        $var(rtpengine_flags) = "RTP/AVP replace-session-connection replace-origin ICE=remove address-family=IP4 out-iface=pub in-iface=pub";
    } else {
        $var(rtpengine_flags) = "RTP/AVP replace-session-connection replace-origin ICE=remove address-family=IP4 in-iface=priv out-iface=priv";
    }

    if ( ( (ds_is_in_list("$si", "", "1")) && ($(ru{uri.user}) == "3301xxxxxxxxxxx") ) || 
         ( ds_is_in_list("$si", "", "3") ) ) {
        xlog("=========vos <-> fs pass========\n");
    } else {
        # rtp proxying for responses
        if ( has_body("application/sdp") ) {
            if ( $rs=="183" && !isbflagset(sdp_offer) ) {
                rtpengine_offer("$var(rtpengine_flags)");
            } else if ( $rs=="200" || $rs=="183" ) {
                rtpengine_answer("$var(rtpengine_flags)");
            }
        }
    }
}

route[dispatcher_next] {
    if (ds_next_dst()) {
        route(send_relay);
    } else {
        send_reply("503","Service Unavailable");
        exit;
    }
}

failure_route[DR_failed] {
    if (t_was_cancelled()) {
        exit;
    }

    if (t_check_status("[56][0-9][0-9]") || (t_check_status("408") && t_local_replied("all"))) {
        route(dispatcher_next);
    }
}