kamailio-basic-kemi-python3s.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. ## Kamailio - equivalent of routing blocks in Python
  2. ##
  3. ## KSR - the new dynamic object exporting Kamailio functions
  4. ## Router - the old object exporting Kamailio functions
  5. ##
  6. ## Relevant remarks:
  7. ## * return code -255 is used to propagate the 'exit' behaviour to the
  8. ## parent route block function. The alternative is to use the native
  9. ## Python function sys.exit() (or exit()) -- it throws an exception that
  10. ## is caught by Kamailio and prevents the stop of the interpreter.
  11. import sys
  12. import KSR as KSR
  13. # global variables corresponding to defined values (e.g., flags) in kamailio.cfg
  14. FLT_ACC=1
  15. FLT_ACCMISSED=2
  16. FLT_ACCFAILED=3
  17. FLT_NATS=5
  18. FLB_NATB=6
  19. FLB_NATSIPPING=7
  20. # SIP request routing
  21. # -- equivalent of request_route{}
  22. def ksr_request_route():
  23. # KSR.info("===== request - from kamailio python script\n")
  24. # KSR.info("===== method [%s] r-uri [%s]\n" % (KSR.pv.get("$rm"),KSR.pv.get("$ru")))
  25. # per request initial checks
  26. if ksr_route_reqinit()==-255 :
  27. return 1
  28. # NAT detection
  29. if ksr_route_natdetect()==-255 :
  30. return 1
  31. # CANCEL processing
  32. if KSR.is_CANCEL() :
  33. if KSR.tm.t_check_trans()>0 :
  34. ksr_route_relay()
  35. return 1
  36. # handle retransmissions
  37. if not KSR.is_ACK() :
  38. if KSR.tmx.t_precheck_trans()>0 :
  39. KSR.tm.t_check_trans()
  40. return 1
  41. if KSR.tm.t_check_trans()==0 :
  42. return 1
  43. # handle requests within SIP dialogs
  44. if ksr_route_withindlg()==-255 :
  45. return 1
  46. # -- only initial requests (no To tag)
  47. # authentication
  48. if ksr_route_auth()==-255 :
  49. return 1
  50. # record routing for dialog forming requests (in case they are routed)
  51. # - remove preloaded route headers
  52. KSR.hdr.remove("Route")
  53. if KSR.is_method_in("IS") :
  54. KSR.rr.record_route()
  55. # account only INVITEs
  56. if KSR.is_INVITE() :
  57. KSR.setflag(FLT_ACC); # do accounting
  58. # dispatch requests to foreign domains
  59. if ksr_route_sipout()==-255 :
  60. return 1
  61. # # requests for my local domains
  62. # handle registrations
  63. if ksr_route_registrar()==-255 :
  64. return 1
  65. if KSR.corex.has_ruri_user() < 0 :
  66. # request with no Username in RURI
  67. KSR.sl.sl_send_reply(484, "Address Incomplete")
  68. return 1
  69. # user location service
  70. ksr_route_location()
  71. return 1
  72. # wrapper around tm relay function
  73. def ksr_route_relay():
  74. # enable additional event routes for forwarded requests
  75. # - serial forking, RTP relaying handling, a.s.o.
  76. if KSR.is_method_in("IBSU") :
  77. if KSR.tm.t_is_set("branch_route")<0 :
  78. KSR.tm.t_on_branch("ksr_branch_manage")
  79. if KSR.is_method_in("ISU") :
  80. if KSR.tm.t_is_set("onreply_route")<0 :
  81. KSR.tm.t_on_reply("ksr_onreply_manage")
  82. if KSR.is_INVITE() :
  83. if KSR.tm.t_is_set("failure_route")<0 :
  84. KSR.tm.t_on_failure("ksr_failure_manage")
  85. if KSR.tm.t_relay()<0 :
  86. KSR.sl.sl_reply_error()
  87. return -255
  88. # Per SIP request initial checks
  89. def ksr_route_reqinit():
  90. # no connect for sending replies
  91. KSR.set_reply_no_connect()
  92. # enforce symmetric signaling
  93. # send back replies to the source address of request
  94. KSR.force_rport()
  95. if not KSR.is_myself_srcip() :
  96. srcip = KSR.kx.get_srcip()
  97. if KSR.htable.sht_match_name("ipban", "eq", srcip) > 0 :
  98. # ip is already blocked
  99. KSR.dbg("request from blocked IP - " + KSR.kx.get_method()
  100. + " from " + KSR.kx.get_furi() + " (IP:"
  101. + srcip + ":" + str(KSR.pv.get("$sp")) + ")\n")
  102. return -255
  103. if KSR.pike.pike_check_req()<0 :
  104. KSR.err("ALERT: pike blocking " + KSR.kx.get_method()
  105. + " from " + KSR.kx.get_furi() + " (IP:"
  106. + srcip + ":" + str(KSR.pv.get("$sp")) + ")\n")
  107. KSR.htable.sht_seti("ipban", srcip, 1)
  108. return -255
  109. if KSR.corex.has_user_agent() > 0 :
  110. ua = KSR.kx.gete_ua()
  111. if (ua.find("friendly")!=-1 or ua.find("scanner")!=-1
  112. or ua.find("sipcli")!=-1 or ua.find("sipvicious")!=-1
  113. or ua.find("VaxSIPUserAgent")!=-1 or ua.find("pplsip")!=-1) :
  114. KSR.sl.sl_send_reply(200, "Processed")
  115. return -255
  116. if KSR.maxfwd.process_maxfwd(10) < 0 :
  117. KSR.sl.sl_send_reply(483, "Too Many Hops")
  118. return -255
  119. if (KSR.is_OPTIONS()
  120. and KSR.is_myself_ruri()
  121. and KSR.corex.has_ruri_user() < 0) :
  122. KSR.sl.sl_send_reply(200, "Keepalive")
  123. return -255
  124. if KSR.sanity.sanity_check(17895, 7)<0 :
  125. KSR.err("Malformed SIP message from "
  126. + KSR.pv.get("$si") + ":" + str(KSR.pv.get("$sp")) +"\n")
  127. return -255
  128. # Handle requests within SIP dialogs
  129. def ksr_route_withindlg():
  130. if KSR.siputils.has_totag()<0 :
  131. return 1
  132. # sequential request withing a dialog should
  133. # take the path determined by record-routing
  134. if KSR.rr.loose_route()>0 :
  135. if ksr_route_dlguri()==-255 :
  136. return -255
  137. if KSR.is_BYE() :
  138. # do accounting ...
  139. KSR.setflag(FLT_ACC)
  140. # ... even if the transaction fails
  141. KSR.setflag(FLT_ACCFAILED)
  142. elif KSR.is_ACK() :
  143. # ACK is forwarded statelessly
  144. if ksr_route_natmanage()==-255 :
  145. return -255
  146. elif KSR.is_NOTIFY() :
  147. # Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  148. KSR.rr.record_route()
  149. ksr_route_relay()
  150. return -255
  151. if KSR.is_ACK() :
  152. if KSR.tm.t_check_trans() >0 :
  153. # no loose-route, but stateful ACK
  154. # must be an ACK after a 487
  155. # or e.g. 404 from upstream server
  156. ksr_route_relay()
  157. return -255
  158. else:
  159. # ACK without matching transaction ... ignore and discard
  160. return -255
  161. KSR.sl.sl_send_reply(404, "Not here")
  162. return -255
  163. # Handle SIP registrations
  164. def ksr_route_registrar():
  165. if not KSR.is_REGISTER() :
  166. return 1
  167. if KSR.isflagset(FLT_NATS) :
  168. KSR.setbflag(FLB_NATB)
  169. # do SIP NAT pinging
  170. KSR.setbflag(FLB_NATSIPPING)
  171. if KSR.registrar.save("location", 0)<0 :
  172. KSR.sl.sl_reply_error()
  173. return -255
  174. # User location service
  175. def ksr_route_location():
  176. rc = KSR.registrar.lookup("location")
  177. if rc<0 :
  178. KSR.tm.t_newtran()
  179. if rc==-1 or rc==-3 :
  180. KSR.sl.send_reply(404, "Not Found")
  181. return -255
  182. elif rc==-2 :
  183. KSR.sl.send_reply(405, "Method Not Allowed")
  184. return -255
  185. # when routing via usrloc, log the missed calls also
  186. if KSR.is_INVITE() :
  187. KSR.setflag(FLT_ACCMISSED)
  188. ksr_route_relay()
  189. return -255
  190. # IP authorization and user uthentication
  191. def ksr_route_auth():
  192. if not KSR.is_REGISTER() :
  193. if KSR.permissions.allow_source_address(1)>0 :
  194. # source IP allowed
  195. return 1
  196. if KSR.is_REGISTER() or KSR.is_myself_furi() :
  197. # authenticate requests
  198. if KSR.auth_db.auth_check(KSR.pv.get("$fd"), "subscriber", 1)<0 :
  199. KSR.auth.auth_challenge(KSR.pv.get("$fd"), 0)
  200. return -255
  201. # user authenticated - remove auth header
  202. if not KSR.is_method_in("RP") :
  203. KSR.auth.consume_credentials()
  204. # if caller is not local subscriber, then check if it calls
  205. # a local destination, otherwise deny, not an open relay here
  206. if (not KSR.is_myself_furi()) and (not KSR.is_myself_ruri()) :
  207. KSR.sl.sl_send_reply(403, "Not relaying")
  208. return -255
  209. return 1
  210. # Caller NAT detection
  211. def ksr_route_natdetect():
  212. KSR.force_rport()
  213. if KSR.nathelper.nat_uac_test(19)>0 :
  214. if KSR.is_REGISTER() :
  215. KSR.nathelper.fix_nated_register()
  216. elif KSR.siputils.is_first_hop()>0 :
  217. KSR.nathelper.set_contact_alias()
  218. KSR.setflag(FLT_NATS)
  219. return 1
  220. # RTPProxy control
  221. def ksr_route_natmanage():
  222. if KSR.siputils.is_request()>0 :
  223. if KSR.siputils.has_totag()>0 :
  224. if KSR.rr.check_route_param("nat=yes")>0 :
  225. KSR.setbflag(FLB_NATB)
  226. if (not (KSR.isflagset(FLT_NATS) or KSR.isbflagset(FLB_NATB))) :
  227. return 1
  228. KSR.rtpproxy.rtpproxy_manage("co")
  229. if KSR.siputils.is_request()>0 :
  230. if not KSR.siputils.has_totag() :
  231. if KSR.tmx.t_is_branch_route()>0 :
  232. KSR.rr.add_rr_param(";nat=yes")
  233. if KSR.siputils.is_reply()>0 :
  234. if KSR.isbflagset(FLB_NATB) :
  235. KSR.nathelper.set_contact_alias()
  236. return 1
  237. # URI update for dialog requests
  238. def ksr_route_dlguri():
  239. if not KSR.isdsturiset() :
  240. KSR.nathelper.handle_ruri_alias()
  241. return 1
  242. # Routing to foreign domains
  243. def ksr_route_sipout():
  244. if KSR.is_myself_ruri() :
  245. return 1
  246. KSR.hdr.append("P-Hint: outbound\r\n")
  247. ksr_route_relay()
  248. return -255
  249. # Manage outgoing branches
  250. # -- equivalent of branch_route[...]{}
  251. def ksr_branch_manage():
  252. KSR.dbg("new branch ["+ str(KSR.pv.get("$T_branch_idx"))
  253. + "] to "+ KSR.pv.get("$ru") + "\n")
  254. ksr_route_natmanage()
  255. return 1
  256. # Manage incoming replies
  257. # -- equivalent of onreply_route[...]{}
  258. def ksr_onreply_manage():
  259. KSR.dbg("incoming reply\n")
  260. scode = KSR.pv.get("$rs")
  261. if scode>100 and scode<299 :
  262. ksr_route_natmanage()
  263. return 1
  264. # Manage failure routing cases
  265. # -- equivalent of failure_route[...]{}
  266. def ksr_failure_manage():
  267. if ksr_route_natmanage()==-255 : return 1
  268. if KSR.tm.t_is_canceled()>0 :
  269. return 1
  270. return 1
  271. # SIP response handling
  272. # -- equivalent of reply_route{}
  273. def ksr_reply_route():
  274. KSR.dbg("response handling - python script\n")
  275. if KSR.sanity.sanity_check(17604, 6)<0 :
  276. KSR.err("Malformed SIP response from "
  277. + KSR.pv.get("$si") + ":" + str(KSR.pv.get("$sp")) +"\n")
  278. KSR.set_drop()
  279. return -255
  280. return 1
  281. # global helper function for debugging purposes
  282. def dumpObj(obj):
  283. for attr in dir(obj):
  284. KSR.info("obj.%s = %s\n" % (attr, getattr(obj, attr)))