Kamailio.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import java.lang.*;
  2. import java.io.*;
  3. import org.siprouter.*;
  4. import org.siprouter.NativeInterface.*;
  5. //import org.siprouter.CoreMethods.*;
  6. public class Kamailio extends NativeMethods
  7. {
  8. static
  9. {
  10. System.load("/opt/kamailio/lib/kamailio/modules/app_java.so");
  11. }
  12. /* Constructor. Do not remove !!! */
  13. public Kamailio()
  14. {
  15. }
  16. public int child_init(int rank)
  17. {
  18. return 1;
  19. }
  20. public int TestMethod()
  21. {
  22. int retval = 0;
  23. boolean boolstate = false;
  24. /*
  25. LM_INFO(String.format("Msg Type: %s\n", SipMsg.getMsgType()));
  26. IPPair src = SipMsg.getSrcAddress();
  27. if (src != null)
  28. {
  29. LM_INFO(String.format("src address=%s, src port=%d\n", src.ip, src.port));
  30. }
  31. else
  32. {
  33. LM_ERR("IPPair src is null!");
  34. }
  35. IPPair dst = SipMsg.getDstAddress();
  36. if (dst != null)
  37. {
  38. LM_INFO(String.format("dst address=%s, dst port=%d\n", dst.ip, dst.port));
  39. }
  40. else
  41. {
  42. LM_ERR("IPPair dst is null!");
  43. }
  44. LM_INFO(String.format("buffer:\n%s\n", SipMsg.getBuffer().trim()));
  45. SipMsg msg = SipMsg.ParseSipMsg();
  46. if (msg != null)
  47. {
  48. LM_INFO("msg:\n");
  49. LM_INFO(String.format("\tid=%d\n", msg.id));
  50. LM_INFO(String.format("\tpid=%d\n", msg.pid));
  51. LM_INFO(String.format("\teoh='%s'\n", msg.eoh));
  52. LM_INFO(String.format("\tunparsed='%s'\n", msg.unparsed));
  53. LM_INFO(String.format("\tbuf='%s'\n", msg.buf));
  54. LM_INFO(String.format("\tlen=%d\n", msg.len));
  55. LM_INFO(String.format("\tnew_uri='%s'\n", msg.new_uri));
  56. LM_INFO(String.format("\tdst_uri='%s'\n", msg.dst_uri));
  57. LM_INFO(String.format("\tparsed_uri_ok=%d\n", msg.parsed_uri_ok));
  58. LM_INFO(String.format("\tparsed_orig_ruri_ok=%d\n", msg.parsed_orig_ruri_ok));
  59. LM_INFO(String.format("\tadd_to_branch_s='%s'\n", msg.add_to_branch_s));
  60. LM_INFO(String.format("\tadd_to_branch_len=%d\n", msg.add_to_branch_len));
  61. LM_INFO(String.format("\thash_index=%d\n", msg.hash_index));
  62. LM_INFO(String.format("\tmsg_flags=%d\n", msg.msg_flags));
  63. LM_INFO(String.format("\tset_global_address='%s'\n", msg.set_global_address));
  64. LM_INFO(String.format("\tset_global_port='%s'\n", msg.set_global_port));
  65. }
  66. else
  67. {
  68. LM_ERR("SipMsg msg is null!\n");
  69. }
  70. */
  71. // retval = KamExec("append_hf", "P-hint: VOICEMAIL\r\n");
  72. // KamExec("sl_send_reply", "404", "Not relaying");
  73. // retval = KamExec("is_method", "INVITE|SUBSCRIBE");
  74. // LM_INFO(String.format("return value: %d\n", retval));
  75. // retval = CoreMethods.rewriteuri("sip:[email protected]:5060");
  76. // retval = KamExec("rewriteuri", "sip:[email protected]:5060");
  77. // retval = KamExec("sl_send_reply", "404", "relaying failed");
  78. // LM_INFO(String.format("return value: %d\n", retval));
  79. // retval = CoreMethods.add_local_rport();
  80. // retval = CoreMethods.drop();
  81. // retval = CoreMethods.force_rport();
  82. // retval = CoreMethods.force_send_socket("192.168.254.9", 50349);
  83. // retval = CoreMethods.forward("198.61.206.9", 5060);
  84. // retval = CoreMethods.forward();
  85. // CoreMethods.setflag(3);
  86. // boolstate = CoreMethods.isflagset(3);
  87. // LM_INFO("return state: " + boolstate + "\n");
  88. // retval = CoreMethods.revert_uri();
  89. // retval = CoreMethods.route("NATDETECT");
  90. // retval = CoreMethods.route("5");
  91. retval = KamExec("is_method", "INVITE");
  92. LM_INFO(String.format("return value: %d\n", retval));
  93. return 1;
  94. }
  95. public static final int FLT_ACC = 1;
  96. public static final int FLT_ACCMISSED = 2;
  97. public static final int FLT_ACCFAILED = 3;
  98. public static final int FLT_NATS = 5;
  99. public static final int FLB_NATB = 6;
  100. public static final int FLB_NATSIPPING = 7;
  101. /// route ///
  102. public int route()
  103. {
  104. return 1;
  105. }
  106. /// request_route ///
  107. public int request_route()
  108. {
  109. // per request initial checks
  110. CoreMethods.route("REQINIT");
  111. // NAT detection
  112. CoreMethods.route("NATDETECT");
  113. // CANCEL processing
  114. if (WrappedMethods.is_method("CANCEL"))
  115. {
  116. if (WrappedMethods.t_check_trans())
  117. {
  118. WrappedMethods.t_relay();
  119. }
  120. return 1;
  121. }
  122. /*
  123. // handle requests within SIP dialogs
  124. CoreMethods.route("WITHINDLG");
  125. //### only initial requests (no To tag)
  126. WrappedMethods.t_check_trans();
  127. // authentication
  128. CoreMethods.route("AUTH");
  129. // record routing for dialog forming requests (in case they are routed)
  130. // - remove preloaded route headers
  131. WrappedMethods.remove_hf("Route");
  132. if (WrappedMethods.is_method("INVITE|SUBSCRIBE"))
  133. {
  134. WrappedMethods.record_route();
  135. }
  136. // account only INVITEs
  137. if (WrappedMethods.is_method("INVITE"))
  138. {
  139. CoreMethods.setflag(FLT_ACC); // do accounting
  140. }
  141. // dispatch requests to foreign domains
  142. CoreMethods.route("SIPOUT");
  143. // ### requests for my local domains
  144. // handle presence related requests
  145. CoreMethods.route("PRESENCE");
  146. String ruri = SipMsg.getRURI();
  147. if (ruri == null || ruri.length() <= 0)
  148. {
  149. // request with no Username in RURI
  150. WrappedMethods.sl_send_reply("484", "Address Incomplete");
  151. return 1;
  152. }
  153. // dispatch destinations to PSTN
  154. CoreMethods.route("PSTN");
  155. // user location service
  156. CoreMethods.route("LOCATION");
  157. CoreMethods.route("RELAY");
  158. */
  159. return 1;
  160. }
  161. public int Route_REQINIT()
  162. {
  163. return 1;
  164. }
  165. public int Route_NATDETECT()
  166. {
  167. return 1;
  168. }
  169. public int Route_WITHINDLG()
  170. {
  171. if (WrappedMethods.has_totag())
  172. {
  173. // sequential request withing a dialog should
  174. // take the path determined by record-routing
  175. if (WrappedMethods.loose_route())
  176. {
  177. CoreMethods.route("DLGURI");
  178. if (WrappedMethods.is_method("BYE"))
  179. {
  180. CoreMethods.setflag(FLT_ACC); // do accounting ...
  181. CoreMethods.setflag(FLT_ACCFAILED); // ... even if the transaction fails
  182. }
  183. else if (WrappedMethods.is_method("ACK"))
  184. {
  185. // ACK is forwarded statelessy
  186. CoreMethods.route("NATMANAGE");
  187. }
  188. else if (WrappedMethods.is_method("NOTIFY"))
  189. {
  190. // Add Record-Route for in-dialog NOTIFY as per RFC 6665.
  191. WrappedMethods.record_route();
  192. }
  193. CoreMethods.route("RELAY");
  194. }
  195. else
  196. {
  197. /* // this block would not work -- 'uri' and 'myself' aren't implemented yet //
  198. if (is_method("SUBSCRIBE") && uri == myself) {
  199. # in-dialog subscribe requests
  200. route(PRESENCE);
  201. exit;
  202. }
  203. */
  204. if (WrappedMethods.is_method("ACK"))
  205. {
  206. if (WrappedMethods.t_check_trans())
  207. {
  208. // no loose-route, but stateful ACK;
  209. // must be an ACK after a 487
  210. // or e.g. 404 from upstream server
  211. WrappedMethods.t_relay();
  212. }
  213. else
  214. {
  215. // ACK without matching transaction ... ignore and discard
  216. return 1;
  217. }
  218. }
  219. WrappedMethods.sl_send_reply("404", "Not here");
  220. }
  221. }
  222. return 1;
  223. }
  224. public int Route_AUTH()
  225. {
  226. return 1;
  227. }
  228. public int Route_SIPOUT()
  229. {
  230. return 1;
  231. }
  232. public int Route_PRESENCE()
  233. {
  234. return 1;
  235. }
  236. public int Route_PSTN()
  237. {
  238. return 1;
  239. }
  240. public int Route_LOCATION()
  241. {
  242. return 1;
  243. }
  244. public int Route_RELAY()
  245. {
  246. // enable additional event routes for forwarded requests
  247. // - serial forking, RTP relaying handling, a.s.o.
  248. if (WrappedMethods.is_method("INVITE|SUBSCRIBE"))
  249. {
  250. //// WrappedMethods.t_on_branch("MANAGE_BRANCH");
  251. //// WrappedMethods.t_on_reply("MANAGE_REPLY");
  252. }
  253. if (WrappedMethods.is_method("INVITE"))
  254. {
  255. //// WrappedMethods.t_on_failure("MANAGE_FAILURE");
  256. }
  257. if (!WrappedMethods.t_relay())
  258. {
  259. WrappedMethods.sl_reply_error();
  260. }
  261. return 1;
  262. }
  263. public int Route_REGISTRAR()
  264. {
  265. if (WrappedMethods.is_method("REGISTER"))
  266. {
  267. if (CoreMethods.isflagset(FLT_NATS))
  268. {
  269. CoreMethods.setflag(FLB_NATB);
  270. // uncomment next line to do SIP NAT pinging
  271. // CoreMethods.setflag(FLB_NATSIPPING);
  272. }
  273. if (!WrappedMethods.save("location"))
  274. {
  275. WrappedMethods.sl_reply_error();
  276. }
  277. }
  278. return 1;
  279. }
  280. }