Skip to content

Commit

Permalink
edge proxy kamailio
Browse files Browse the repository at this point in the history
  • Loading branch information
altanai committed Sep 11, 2019
1 parent fd02748 commit cf357ad
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 0 deletions.
2 changes: 2 additions & 0 deletions edge_proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SIP Edge proxy Server on kamailio

332 changes: 332 additions & 0 deletions edge_proxy/kamailio.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
#!KAMAILIO

include_file "vars.cfg"

#!define FLT_NATS 5
#!define FLB_NATB 6
#!define FLB_NATSIPPING 7

####### Global Parameters #########
debug=2
log_stderror=no

memdbg=5
memlog=5

log_facility=LOG_LOCAL0

fork=yes
children=2

listen=udp:EDGE_IP:UDP_PORT

#!ifdef WITH_TLS
enable_tls=yes
#!endif

####### Modules Section ########

mpath="/usr/local/kamailio/lib64/kamailio/modules/"

#!ifdef WITH_TLS
loadmodule "tls.so"
#!endif

loadmodule "mi_fifo.so"
loadmodule "kex.so"
loadmodule "corex.so"
loadmodule "tm.so"
loadmodule "tmx.so"
loadmodule "sl.so"
loadmodule "rr.so"
loadmodule "pv.so"
loadmodule "maxfwd.so"
loadmodule "dialog.so"
loadmodule "textops.so"
loadmodule "siputils.so"
loadmodule "xlog.so"
loadmodule "sanity.so"
loadmodule "ctl.so"
loadmodule "cfg_rpc.so"
loadmodule "mi_rpc.so"
loadmodule "nathelper.so"
loadmodule "nat_traversal.so"
loadmodule "path.so"
loadmodule "rtpengine.so"
loadmodule "debugger.so"


# ----------------- setting module-specific parameters ---------------

modparam("ctl", "binrpc", "unix:/var/run/kamailio_ctl")

modparam("tm", "failure_reply_mode", 3)
modparam("tm", "fr_timer", 7000)
modparam("tm", "fr_inv_timer", 35000)
modparam("tm", "auto_inv_100", 0)

# ----- rr params -----
# add value to ;lr param to cope with most of the UAs
modparam("rr", "enable_full_lr", 1)
# do not append from tag to the RR
modparam("rr", "append_fromtag", 0)

modparam("rtpengine", "rtpengine_sock", "udp:RTPENGINE_IP:RTPENGINE_PORT")

modparam("nat_traversal", "keepalive_interval", 25)
modparam("nat_traversal", "keepalive_from", "sip:[email protected]")

modparam("path", "use_received", 1)

modparam("dialog", "dlg_flag", 4)


####### Routing Logic ########

request_route {

# per request initial checks
route(REQINIT);

# NAT detection
route(NATDETECT);

# CANCEL processing
if (is_method("CANCEL")) {
rtpengine_delete();
if (t_check_trans()) {
route(RELAY);
}
exit;
}

# handle requests within SIP dialogs
route(WITHINDLG);

### only initial requests (no To tag)

# handle retransmissions
if(t_precheck_trans()) {

t_check_trans();
exit;
}
t_check_trans();

remove_hf("Route");
if (is_method("INVITE|SUBSCRIBE"))
record_route();

# dispatch requests to foreign domains
route(SIPOUT);

# handle registrations
if(is_method("REGISTER")) {
route(REGISTRAR);
exit;
}

if(is_method("INVITE")) {
route(INVITE);
exit;
}

if ($rU==$null) {
# request with no Username in RURI
sl_send_reply("484","Address Incomplete");
exit;
}
}


route[RELAY] {
# enable additional event routes for forwarded requests
# - serial forking, RTP relaying handling, a.s.o.
if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
}
if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
}
if (is_method("INVITE")) {
if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
}

if (!t_relay()) {
sl_reply_error();
}
exit;
}

# Per SIP request initial checks
route[REQINIT] {

if (!mf_process_maxfwd_header("10")) {
sl_send_reply("483","Too Many Hops");
exit;
}

if(is_method("OPTIONS") && uri==myself && $rU==$null) {
sl_send_reply("200","Keepalive");
exit;
}

if(!sanity_check("1511", "7")) {
xlog("Malformed SIP message from $si:$sp\n");
exit;
}
}

route[NATDETECT] {

force_rport();
if (nat_uac_test(19)) {
fix_contact();
nat_keepalive();
}

if (nat_uac_test(64)) {
add_contact_alias();
}

return;
}


# Handle requests within SIP dialogs
route[WITHINDLG] {
if (!has_totag()) return;

# sequential request withing a dialog should take the path determined by record-routing
if (loose_route()) {
if ($du == "") {
if (!handle_ruri_alias()) {
xlog("L_ERR", "Bad alias <$ru>\n");
sl_send_reply("400", "Bad Request");
exit;
}
}

route(DLGURI);
if (is_method("BYE")) {
xlog("L_INFO", "BYE Received");
if (is_method("BYE")) {
rtpengine_delete();
t_relay();
exit;
}
}
else if ( is_method("ACK") ) {
# ACK is forwarded statelessy
xlog("L_INFO", "Relay ACK Statelessly");
}

if (@via[2] != "") {
xlog("L_INFO", "Loose Route from Core");
t_on_reply("MANAGE_REPLY");
} else {
xlog("L_INFO", "Probably a Re-INVITE from the client");
}

t_relay();
exit;
}
if ( is_method("ACK") ) {
if ( t_check_trans() ) {
route(RELAY);
exit;
} else {
exit;
}
}
sl_send_reply("404", "Not here");
exit;
}

# Handle SIP registrations
route[REGISTRAR] {

$du = "sip:$tU@CORE_IP:CORE_PORT";
remove_hf("Route");
insert_hf("X-Src-IP: $si\r\n");
add_path();
t_relay_to_udp();
exit;
}

route[INVITE] {
if (@via[2] == "") {

$var(rtpengine_flags) = "replace-session-connection replace-origin";
rtpengine_offer("$var(rtpengine_flags)");

t_on_reply("REPLY_ROUTE");

$du = "sip:$rU@CORE_IP:CORE_PORT";

remove_hf("Route");
sl_send_reply("100", "Sending to Core...");

t_relay_to_udp();
exit;
} else {
xlog("L_INFO", "Call from REGISTRAR, relay to user...");
t_on_reply("MANAGE_REPLY");
t_on_failure("MANAGE_FAILURE");
t_relay();
exit;
}
}


# URI update for in-dialog requests
route[DLGURI] {
if(!isdsturiset()) {
handle_ruri_alias();
}
return;
}

# Routing to foreign domains
route[SIPOUT] {
if (uri==myself) return;

if($si == "CORE_IP") {
xlog("L_INFO", "$rm came from core proxy, relay to user...");
t_on_reply("MANAGE_REPLY");
t_on_failure("MANAGE_FAILURE");
t_relay();
exit;
} else {
xlog("L_INFO", "Not from our core server, just drop");
exit;
}
}

# Manage incoming replies
onreply_route[MANAGE_REPLY] {
xdbg("incoming reply\n");
if (client_nat_test("7")) {
fix_nated_contact();
}
}

onreply_route[REPLY_ROUTE] {
if(status=~"2[0-9][0-9]") {
$var(rtpengine_flags) = "replace-session-connection replace-origin";
rtpengine_answer("$var(rtpengine_flags)");
}
}

# Manage failure routing cases
failure_route[MANAGE_FAILURE] {

if (t_is_canceled()) {
exit;
}

if (t_branch_timeout()) {
xlog("L_INFO", "No Reply on Branch");
send_reply("430", "Flow Failed");
}
}
8 changes: 8 additions & 0 deletions edge_proxy/vars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!substdef "!CORE_IP!10.20.30.40!g"
#!substdef "!CORE_PORT!5060!g"

#!substdef "!EDGE_IP!20.30.40.50!g"
#!substdef "!UDP_PORT!5060!g"

#!substdef "!RTPENGINE_IP!130.40.50.60!g"
#!substdef "!RTPENGINE_PORT!22222!g"

0 comments on commit cf357ad

Please sign in to comment.