kamailio-basic-kemi-python.py 12 KB

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