{"id":30,"date":"2015-03-27T08:30:28","date_gmt":"2015-03-27T12:30:28","guid":{"rendered":"http:\/\/blog.voipxswitch.com\/?p=30"},"modified":"2015-06-18T09:33:02","modified_gmt":"2015-06-18T13:33:02","slug":"kamailio-basic-sip-proxy-all-requests-setup","status":"publish","type":"post","link":"https:\/\/blog.voipxswitch.com\/?p=30","title":{"rendered":"Kamailio: Basic SIP Proxy (all requests) Setup"},"content":{"rendered":"<p><b>In this example, I will share how to setup Kamailio to proxy SIP requests to a SIP switch (such as FreeSWITCH or Asterisk).<\/b><br \/>\n<b>192.168.1.101 is the IP of Kamailio<\/b><br \/>\n<b>192.168.1.102 is the IP of FreeSWITCH or Asterisk<\/b><br \/>\n<b>Here are snippets from the main config script, kamailio.cfg:<\/b><\/p>\n<pre>...\r\n \r\n\r\n#!define IPADDRESS \"192.168.1.101\"\r\n\r\n#!define SWITCH_IPADDRESS \"192.168.1.102\"\r\n\r\n\r\n#!define FLAG_FROM_SWITCH 1\r\n#!define FLAG_FROM_USER 2\r\n\r\n# ------------------ module loading ----------------------------------\r\nloadmodule \"tm.so\"\r\nloadmodule \"rr.so\"\r\nloadmodule \"pv.so\"\r\nloadmodule \"sl.so\"\r\nloadmodule \"maxfwd.so\"\r\nloadmodule \"nathelper.so\"\r\nloadmodule \"textops.so\"\r\nloadmodule \"siputils.so\"\r\nloadmodule \"xlog.so\"\r\nloadmodule \"sanity.so\"\r\nloadmodule \"path.so\"\r\n\r\n# ----------------- setting module-specific parameters ---------------\r\nmodparam(\"nathelper|registrar\", \"received_avp\", \"$avp(s:rcv)\")\r\n# -------------------------  request routing logic -------------------\r\n# main routing logic\r\n\r\nroute {\r\n\r\n        # per request initial checks\r\n        route(SANITY_CHECK);\r\n\r\n        # CANCEL processing\r\n        if (is_method(\"CANCEL\")) {\r\n                if (t_check_trans()) {\r\n                        t_relay();\r\n                }\r\n                exit;\r\n        }\r\n\r\n        route(CHECK_SOURCE_IP);\r\n\r\n        ##################################\r\n        ### HANDLE SEQUENTIAL REQUESTS ###\r\n        route(WITHINDLG);\r\n\r\n        ###############################\r\n        ### HANDLE INITIAL REQUESTS ###\r\n        t_check_trans();\r\n\r\n        if (is_method(\"INVITE|REFER\")) {\r\n                record_route();\r\n        }\r\n       \r\n        if (is_method(\"REGISTER\")) {\r\n            add_path();\r\n        }\r\n\r\n        if (isflagset(FLAG_FROM_SWITCH)) {\r\n                # don't send INVITE from SWITCH back to SWITCH, set reply route to handle NAT and forward them along\r\n                t_on_reply(\"EXTERNAL_REPLY\");\r\n        } else {\r\n                # set destination to your SWITCH\r\n                $du = \"sip:192.168.1.102:5060\";\r\n        }\r\n\r\n        route(RELAY);\r\n\r\n\r\n}\r\n\r\n\r\nroute[SANITY_CHECK]\r\n{\r\n        if (!sanity_check()) {\r\n                #xlog(\"L_WARN\", \"$ci|end|message is insane\");\r\n                exit;\r\n        }\r\n\r\n        if (!mf_process_maxfwd_header(\"10\")) {\r\n                #xlog(\"L_WARN\", \"$ci|end|too much hops, not enough barley\");\r\n                send_reply(\"483\", \"Too Many Hops\");\r\n                exit;\r\n        }\r\n\r\n        if ($ua == \"friendly-scanner\" ||\r\n                $ua == \"sundayddr\" ||\r\n                $ua =~ \"sipcli\" ) {\r\n                #xlog(\"L_WARN\", \"$ci|end|dropping message with user-agent $ua\");\r\n                exit;\r\n        }\r\n\r\n        if ($si == IPADDRESS) {\r\n                #xlog(\"L_WARN\", \"$ci|end|dropping message\");\r\n                exit;\r\n        }\r\n\r\n}\r\n\r\n\r\nroute[CHECK_SOURCE_IP]\r\n{\r\n        if ($si == SWITCH_IPADDRESS) {\r\n                setflag(FLAG_FROM_SWITCH);\r\n        } else {\r\n                setflag(FLAG_FROM_USER);\r\n        }\r\n}\r\n\r\n# Handle requests within SIP dialogs\r\nroute[WITHINDLG]\r\n{\r\n        if (has_totag()) {\r\n                # sequential request withing a dialog should\r\n                # take the path determined by record-routing\r\n                if (loose_route()) {\r\n                        route(RELAY);\r\n                } else {\r\n                        if (is_method(\"NOTIFY\")) {\r\n                                route(RELAY);\r\n                        }\r\n                        if (is_method(\"SUBSCRIBE\") &amp;&amp; uri == myself) {\r\n                                # in-dialog subscribe requests\r\n                                exit;\r\n                        }\r\n                        if (is_method(\"ACK\")) {\r\n                                if (t_check_trans()) {\r\n                                        # no loose-route, but stateful ACK;\r\n                                        # must be an ACK after a 487\r\n                                        # or e.g. 404 from upstream server\r\n                                        t_relay();\r\n                                        exit;\r\n                                } else {\r\n                                        # ACK without matching transaction ... ignore and discard\r\n                                        #xlog(\"ACK without matching transaction ... ignore and discard\");\r\n                                        exit;\r\n                                }\r\n                        }\r\n                        sl_send_reply(\"404\",\"Not here\");\r\n                }\r\n                exit;\r\n        }\r\n}\r\n\r\nonreply_route[EXTERNAL_REPLY]\r\n{\r\n        route(NAT_TEST_AND_CORRECT);\r\n}\r\n\r\n\r\nroute[NAT_TEST_AND_CORRECT]\r\n{\r\n        if (nat_uac_test(\"3\")) {\r\n                if (is_method(\"REGISTER\")) {\r\n                        fix_nated_register();\r\n                } else {\r\n                        fix_nated_contact();\r\n                }\r\n                force_rport();\r\n        }\r\n        if (has_body(\"application\/sdp\") &amp;&amp; nat_uac_test(\"8\")) {\r\n                fix_nated_sdp(\"10\");\r\n        }\r\n}\r\n\r\nroute[RELAY]\r\n{\r\n        if (!t_relay()) {\r\n                sl_reply_error();\r\n        }\r\n        exit;\r\n}\r\n<\/pre>\n<p><b>This example will proxy all SIP requests to the switch. Typically, this is not recommended as Kamailio is able to handle the SIP REGISTERs but I&#8217;ll save that for another tutorial.<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, I will share how to setup Kamailio to proxy SIP requests to a SIP switch (such as FreeSWITCH or Asterisk). 192.168.1.101 is the IP of Kamailio 192.168.1.102 is the IP of FreeSWITCH or Asterisk Here are snippets from the main config script, kamailio.cfg: &#8230; #!define IPADDRESS &#8220;192.168.1.101&#8221; #!define SWITCH_IPADDRESS &#8220;192.168.1.102&#8221; #!define FLAG_FROM_SWITCH [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,25,15],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-kamailio","category-sip","category-voip"],"_links":{"self":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":3,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}