kamailio-minimal-anycast.cfg 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #!KAMAILIO
  2. #
  3. # Kamailio (OpenSER) SIP Server v5.2 - default configuration script
  4. # - web: https://www.kamailio.org
  5. # - git: https://github.com/kamailio/kamailio
  6. #
  7. # Direct your questions about this file to: <[email protected]>
  8. #
  9. # Refer to the Core CookBook at https://www.kamailio.org/wiki/
  10. # for an explanation of possible statements, functions and parameters.
  11. #
  12. ####### Defined Values #########
  13. # the specific address for this node
  14. #!define IPADDR_THISNODE "192.168.178.75"
  15. #!subst "/IPADDR_THISNODE/192.168.178.75/"
  16. # the specific address of the pair node in the anycast deployment
  17. #!define IPADDR_PAIRNODE "192.168.178.74"
  18. #!subst "/IPADDR_PAIRNODE/192.168.178.74/"
  19. # the anycast address
  20. #!define IPADDR_ANYCAST "192.168.178.80"
  21. #!subst "/IPADDR_ANYCAST/192.168.178.80/"
  22. # the address of the main sip server where to route requests to
  23. # [UA1] <===> [ANYCASTNODE] <===> [MAINSRV] <===> [ANYCASTNODE] <===> [UA2]
  24. #!define IPADDR_MAINSRV "192.168.178.40"
  25. #!subst "/IPADDR_MAINSRV/192.168.178.40/"
  26. #!define MULTIDOMAIN 0
  27. # - flags
  28. # FLT_ - per transaction (message) flags
  29. # FLB_ - per branch flags
  30. #!define FLT_ACC 1
  31. #!define FLT_ACCMISSED 2
  32. #!define FLT_ACCFAILED 3
  33. #!define FLT_NATS 5
  34. #!define FLB_NATB 6
  35. #!define FLB_NATSIPPING 7
  36. ####### Global Parameters #########
  37. ### LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR
  38. #!ifdef WITH_DEBUG
  39. debug=4
  40. log_stderror=yes
  41. #!else
  42. debug=2
  43. log_stderror=no
  44. #!endif
  45. memdbg=5
  46. memlog=5
  47. log_facility=LOG_LOCAL0
  48. log_prefix="{$mt $hdr(CSeq) $ci} "
  49. /* number of SIP routing processes */
  50. children=8
  51. server_id = 10
  52. xavp_via_params = "via"
  53. /* uncomment the next line to disable TCP (default on) */
  54. # disable_tcp=yes
  55. /* uncomment the next line to disable the auto discovery of local aliases
  56. * based on reverse DNS on IPs (default on) */
  57. # auto_aliases=no
  58. /* add local domain aliases */
  59. # alias="sip.mydomain.com"
  60. /* listen on local and anycast addresses */
  61. listen=udp:IPADDR_THISNODE:5060
  62. # listen=udp:IPADDR_ANYCAST:5060
  63. ####### Custom Parameters #########
  64. /* These parameters can be modified runtime via RPC interface
  65. * - see the documentation of 'cfg_rpc' module.
  66. *
  67. * Format: group.id = value 'desc' description
  68. * Access: $sel(cfg_get.group.id) or @cfg_get.group.id */
  69. ####### Modules Section ########
  70. /* set paths to location of modules */
  71. loadmodule "jsonrpcs.so"
  72. loadmodule "kex.so"
  73. loadmodule "corex.so"
  74. loadmodule "tm.so"
  75. loadmodule "tmx.so"
  76. loadmodule "sl.so"
  77. loadmodule "rr.so"
  78. loadmodule "pv.so"
  79. loadmodule "maxfwd.so"
  80. loadmodule "textops.so"
  81. loadmodule "siputils.so"
  82. loadmodule "xlog.so"
  83. loadmodule "sanity.so"
  84. loadmodule "ctl.so"
  85. loadmodule "cfg_rpc.so"
  86. loadmodule "acc.so"
  87. loadmodule "counters.so"
  88. # ----------------- setting module-specific parameters ---------------
  89. # ----- jsonrpcs params -----
  90. modparam("jsonrpcs", "pretty_format", 1)
  91. /* set the path to RPC fifo control file */
  92. # modparam("jsonrpcs", "fifo_name", "/run/kamailio/kamailio_rpc.fifo")
  93. /* set the path to RPC unix socket control file */
  94. # modparam("jsonrpcs", "dgram_socket", "/run/kamailio/kamailio_rpc.sock")
  95. # ----- ctl params -----
  96. /* set the path to RPC unix socket control file */
  97. # modparam("ctl", "binrpc", "unix:/run/kamailio/kamailio_ctl")
  98. # ----- tm params -----
  99. # auto-discard branches from previous serial forking leg
  100. modparam("tm", "failure_reply_mode", 3)
  101. # default retransmission timeout: 30sec
  102. modparam("tm", "fr_timer", 30000)
  103. # default invite retransmission timeout after 1xx: 120sec
  104. modparam("tm", "fr_inv_timer", 120000)
  105. # ----- rr params -----
  106. # set next param to 1 to add value to ;lr param (helps with some UAs)
  107. modparam("rr", "enable_full_lr", 0)
  108. # do not append from tag to the RR (no need for this script)
  109. modparam("rr", "append_fromtag", 0)
  110. # ----- acc params -----
  111. /* what special events should be accounted ? */
  112. modparam("acc", "early_media", 0)
  113. modparam("acc", "report_ack", 0)
  114. modparam("acc", "report_cancels", 0)
  115. /* by default ww do not adjust the direct of the sequential requests.
  116. * if you enable this parameter, be sure the enable "append_fromtag"
  117. * in "rr" module */
  118. modparam("acc", "detect_direction", 0)
  119. /* account triggers (flags) */
  120. modparam("acc", "log_flag", FLT_ACC)
  121. modparam("acc", "log_missed_flag", FLT_ACCMISSED)
  122. modparam("acc", "log_extra",
  123. "src_user=$fU;src_domain=$fd;src_ip=$si;"
  124. "dst_ouser=$tU;dst_user=$rU;dst_domain=$rd")
  125. modparam("acc", "failed_transaction_flag", FLT_ACCFAILED)
  126. ####### Routing Logic ########
  127. /* Main SIP request routing logic
  128. * - processing of any incoming SIP request starts with this route
  129. * - note: this is the same as route { ... } */
  130. request_route {
  131. # per request initial checks
  132. route(REQINIT);
  133. # CANCEL processing
  134. if (is_method("CANCEL")) {
  135. if (t_check_trans()) {
  136. route(RELAY);
  137. }
  138. exit;
  139. }
  140. # handle retransmissions
  141. if (!is_method("ACK")) {
  142. if(t_precheck_trans()) {
  143. t_check_trans();
  144. exit;
  145. }
  146. t_check_trans();
  147. }
  148. # handle requests within SIP dialogs
  149. route(WITHINDLG);
  150. ### only initial requests (no To tag)
  151. # record routing for dialog forming requests (in case they are routed)
  152. # - remove preloaded route headers
  153. remove_hf("Route");
  154. if (is_method("INVITE|SUBSCRIBE")) {
  155. record_route();
  156. }
  157. # account only INVITEs
  158. if (is_method("INVITE")) {
  159. setflag(FLT_ACC); # do accounting
  160. }
  161. if ($rU==$null) {
  162. # request with no Username in RURI
  163. sl_send_reply("484","Address Incomplete");
  164. exit;
  165. }
  166. if($si != IPADDR_MAINSRV) {
  167. # received from client - send to main sip server
  168. $du = "sip:" + IPADDR_MAINSRV;
  169. route(RELAY);
  170. exit;
  171. }
  172. # received from main server - send to client
  173. # add via tokens for anycast handling
  174. via_add_srvid("1");
  175. $xavp(via=>node) = IPADDR_THISNODE;
  176. via_add_xavp_params("1");
  177. route(RELAY);
  178. exit;
  179. }
  180. # Wrapper for relaying requests
  181. route[RELAY] {
  182. # enable additional event routes for forwarded requests
  183. # - serial forking, RTP relaying handling, a.s.o.
  184. if (is_method("INVITE|BYE|SUBSCRIBE|UPDATE")) {
  185. if(!t_is_set("branch_route")) t_on_branch("MANAGE_BRANCH");
  186. }
  187. if (is_method("INVITE|SUBSCRIBE|UPDATE")) {
  188. if(!t_is_set("onreply_route")) t_on_reply("MANAGE_REPLY");
  189. }
  190. if (is_method("INVITE")) {
  191. if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
  192. }
  193. if (!t_relay()) {
  194. sl_reply_error();
  195. }
  196. exit;
  197. }
  198. # Per SIP request initial checks
  199. route[REQINIT] {
  200. if($ua =~ "friendly-scanner|sipcli|VaxSIPUserAgent") {
  201. # silent drop for scanners - uncomment next line if want to reply
  202. # sl_send_reply("200", "OK");
  203. exit;
  204. }
  205. if (!mf_process_maxfwd_header("10")) {
  206. sl_send_reply("483","Too Many Hops");
  207. exit;
  208. }
  209. if(is_method("OPTIONS") && uri==myself && $rU==$null) {
  210. sl_send_reply("200","Keepalive");
  211. exit;
  212. }
  213. if(!sanity_check("1511", "7")) {
  214. xlog("Malformed SIP message from $si:$sp\n");
  215. exit;
  216. }
  217. }
  218. # Handle requests within SIP dialogs
  219. route[WITHINDLG] {
  220. if (!has_totag()) return;
  221. # sequential request withing a dialog should
  222. # take the path determined by record-routing
  223. if (loose_route()) {
  224. if (is_method("BYE")) {
  225. setflag(FLT_ACC); # do accounting ...
  226. setflag(FLT_ACCFAILED); # ... even if the transaction fails
  227. } else if ( is_method("NOTIFY") ) {
  228. # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  229. record_route();
  230. }
  231. route(RELAY);
  232. exit;
  233. }
  234. if ( is_method("ACK") ) {
  235. if ( t_check_trans() ) {
  236. # no loose-route, but stateful ACK;
  237. # must be an ACK after a 487
  238. # or e.g. 404 from upstream server
  239. route(RELAY);
  240. exit;
  241. } else {
  242. # ACK without matching transaction ... ignore and discard
  243. exit;
  244. }
  245. }
  246. sl_send_reply("404","Not here");
  247. exit;
  248. }
  249. # TM manage for outgoing branches
  250. branch_route[MANAGE_BRANCH] {
  251. xdbg("new branch [$T_branch_idx] to $ru\n");
  252. }
  253. # TM manage for incoming replies
  254. onreply_route[MANAGE_REPLY] {
  255. xdbg("incoming reply\n");
  256. }
  257. # TM manage for failure routing cases
  258. failure_route[MANAGE_FAILURE] {
  259. if (t_is_canceled()) exit;
  260. }
  261. # Core handling for incoming replies
  262. reply_route {
  263. if($si != IPADDR_MAINSRV) {
  264. if($si == IPADDR_PAIRNODE) {
  265. # receiving a transparent proxied request from pair server
  266. if(is_present_hf("X-Src-Addr")) {
  267. # set the original source IP address
  268. xinfo("setting source address: $hdr(X-Src-Addr)\n");
  269. set_source_address("$hdr(X-Src-Addr)");
  270. remove_hf("X-Src-Addr");
  271. }
  272. # switch received socket to anycast address
  273. set_recv_socket("udp:IPADDR_ANYCAST:5060");
  274. } else {
  275. # receiving response from client
  276. # response belongs to a transaction?
  277. # - yes: route it, request was handled here
  278. # - no: send to pair server
  279. if(!t_check_trans()) {
  280. append_hf("X-Src-Addr: $su\r\n");
  281. # $var(vd) = $(hdr(Via){s.select, ,-1}{param.value,node});
  282. $var(vd) = $sel(via.params["node"]);
  283. xinfo("via param domain: $var(vd)\n");
  284. sendx("sip:$var(vd)", "udp:IPADDR_THISNODE:5060", "$mbu");
  285. drop();
  286. }
  287. }
  288. }
  289. # from now on, the usual sip response processing
  290. }