java_msgobj.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2013 Konstantin Mosesov
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. *
  12. * This file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include "../../action.h"
  23. #include "../../mem/mem.h"
  24. #include "../../sr_module.h"
  25. #include "../../dset.h"
  26. #include "../../parser/msg_parser.h"
  27. #include <jni.h>
  28. #include "global.h"
  29. #include "utils.h"
  30. #include "app_java_mod.h"
  31. #include "java_iface.h"
  32. #include "java_support.h"
  33. #include "java_native_methods.h"
  34. #include "java_msgobj.h"
  35. jobject *fill_sipmsg_object(JNIEnv *env, struct sip_msg *msg)
  36. {
  37. jobject *SipMsgInstance;
  38. jclass SipMsgClass;
  39. jmethodID SipMsgClassID;
  40. jfieldID fid;
  41. jstring jStrParam;
  42. SipMsgInstance = (jobject *)pkg_malloc(sizeof(jobject));
  43. if (!SipMsgInstance)
  44. {
  45. LM_ERR("%s: pkg_malloc() has failed. Not enough memory!\n", APP_NAME);
  46. return NULL;
  47. }
  48. memset(SipMsgInstance, 0, sizeof(jobject));
  49. SipMsgClass = (*env)->FindClass(env, "org/siprouter/SipMsg");
  50. if (!SipMsgClass || (*env)->ExceptionCheck(env))
  51. {
  52. handle_exception();
  53. pkg_free(SipMsgInstance);
  54. return NULL;
  55. }
  56. SipMsgClassID = (*env)->GetMethodID(env, SipMsgClass, "<init>", "()V");
  57. if (!SipMsgClassID || (*env)->ExceptionCheck(env))
  58. {
  59. handle_exception();
  60. return NULL;
  61. }
  62. // calling constructor
  63. (*SipMsgInstance) = (*env)->NewObject(env, SipMsgClass, SipMsgClassID);
  64. if ((*env)->ExceptionCheck(env))
  65. {
  66. handle_exception();
  67. return NULL;
  68. }
  69. // msg->id => SipMsg.id
  70. fid = (*env)->GetFieldID(env, SipMsgClass, "id", "I");
  71. if (!fid)
  72. {
  73. (*env)->ExceptionClear(env);
  74. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.id\n", APP_NAME);
  75. return NULL;
  76. }
  77. (*env)->SetIntField(env, SipMsgInstance, fid, msg->id);
  78. if ((*env)->ExceptionCheck(env))
  79. {
  80. handle_exception();
  81. return NULL;
  82. }
  83. // msg->pid => SipMsg.pid
  84. fid = (*env)->GetFieldID(env, SipMsgClass, "pid", "I");
  85. if (!fid)
  86. {
  87. (*env)->ExceptionClear(env);
  88. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.pid\n", APP_NAME);
  89. return NULL;
  90. }
  91. (*env)->SetIntField(env, SipMsgInstance, fid, msg->pid);
  92. if ((*env)->ExceptionCheck(env))
  93. {
  94. handle_exception();
  95. return NULL;
  96. }
  97. // msg->eoh => SipMsg.eoh
  98. fid = (*env)->GetFieldID(env, SipMsgClass, "eoh", "Ljava/lang/String;");
  99. if (!fid)
  100. {
  101. (*env)->ExceptionClear(env);
  102. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.eoh\n", APP_NAME);
  103. return NULL;
  104. }
  105. jStrParam = (*env)->NewStringUTF(env, msg->eoh);
  106. (*env)->SetObjectField(env, SipMsgInstance, fid, jStrParam);
  107. if ((*env)->ExceptionCheck(env))
  108. {
  109. handle_exception();
  110. return NULL;
  111. }
  112. (*env)->DeleteLocalRef(env, jStrParam);
  113. // msg->unparsed => SipMsg.unparsed
  114. fid = (*env)->GetFieldID(env, SipMsgClass, "unparsed", "Ljava/lang/String;");
  115. if (!fid)
  116. {
  117. (*env)->ExceptionClear(env);
  118. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.unparsed\n", APP_NAME);
  119. return NULL;
  120. }
  121. jStrParam = (*env)->NewStringUTF(env, msg->unparsed);
  122. (*env)->SetObjectField(env, SipMsgInstance, fid, jStrParam);
  123. if ((*env)->ExceptionCheck(env))
  124. {
  125. handle_exception();
  126. return NULL;
  127. }
  128. (*env)->DeleteLocalRef(env, jStrParam);
  129. // msg->buf => SipMsg.buf
  130. fid = (*env)->GetFieldID(env, SipMsgClass, "buf", "Ljava/lang/String;");
  131. if (!fid)
  132. {
  133. (*env)->ExceptionClear(env);
  134. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.buf\n", APP_NAME);
  135. return NULL;
  136. }
  137. jStrParam = (*env)->NewStringUTF(env, msg->buf);
  138. (*env)->SetObjectField(env, SipMsgInstance, fid, jStrParam);
  139. if ((*env)->ExceptionCheck(env))
  140. {
  141. handle_exception();
  142. return NULL;
  143. }
  144. (*env)->DeleteLocalRef(env, jStrParam);
  145. // msg->len => SipMsg.len
  146. fid = (*env)->GetFieldID(env, SipMsgClass, "len", "I");
  147. if (!fid)
  148. {
  149. (*env)->ExceptionClear(env);
  150. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.len\n", APP_NAME);
  151. return NULL;
  152. }
  153. (*env)->SetIntField(env, SipMsgInstance, fid, msg->len);
  154. if ((*env)->ExceptionCheck(env))
  155. {
  156. handle_exception();
  157. return NULL;
  158. }
  159. // msg->new_uri => SipMsg.new_uri
  160. fid = (*env)->GetFieldID(env, SipMsgClass, "new_uri", "Ljava/lang/String;");
  161. if (!fid)
  162. {
  163. (*env)->ExceptionClear(env);
  164. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.new_uri\n", APP_NAME);
  165. return NULL;
  166. }
  167. jStrParam = (*env)->NewStringUTF(env, msg->new_uri.len <= 0 ? "" : msg->new_uri.s);
  168. (*env)->SetObjectField(env, SipMsgInstance, fid, jStrParam);
  169. if ((*env)->ExceptionCheck(env))
  170. {
  171. handle_exception();
  172. return NULL;
  173. }
  174. (*env)->DeleteLocalRef(env, jStrParam);
  175. // msg->dst_uri => SipMsg.dst_uri
  176. fid = (*env)->GetFieldID(env, SipMsgClass, "dst_uri", "Ljava/lang/String;");
  177. if (!fid)
  178. {
  179. (*env)->ExceptionClear(env);
  180. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.dst_uri\n", APP_NAME);
  181. return NULL;
  182. }
  183. jStrParam = (*env)->NewStringUTF(env, msg->dst_uri.len <= 0 ? "" : msg->dst_uri.s);
  184. (*env)->SetObjectField(env, SipMsgInstance, fid, jStrParam);
  185. if ((*env)->ExceptionCheck(env))
  186. {
  187. handle_exception();
  188. return NULL;
  189. }
  190. (*env)->DeleteLocalRef(env, jStrParam);
  191. // msg->parsed_orig_ruri_ok => SipMsg.parsed_orig_ruri_ok
  192. fid = (*env)->GetFieldID(env, SipMsgClass, "parsed_orig_ruri_ok", "I");
  193. if (!fid)
  194. {
  195. (*env)->ExceptionClear(env);
  196. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.parsed_orig_ruri_ok\n", APP_NAME);
  197. return NULL;
  198. }
  199. (*env)->SetIntField(env, SipMsgInstance, fid, msg->parsed_orig_ruri_ok);
  200. if ((*env)->ExceptionCheck(env))
  201. {
  202. handle_exception();
  203. return NULL;
  204. }
  205. // msg->add_to_branch_s => SipMsg.add_to_branch_s
  206. fid = (*env)->GetFieldID(env, SipMsgClass, "add_to_branch_s", "Ljava/lang/String;");
  207. if (!fid)
  208. {
  209. (*env)->ExceptionClear(env);
  210. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.add_to_branch_s\n", APP_NAME);
  211. return NULL;
  212. }
  213. jStrParam = (*env)->NewStringUTF(env, (msg->add_to_branch_len <= 0 || msg->add_to_branch_s == NULL) ? "" : strdup(msg->add_to_branch_s));
  214. (*env)->SetObjectField(env, SipMsgInstance, fid, jStrParam);
  215. if ((*env)->ExceptionCheck(env))
  216. {
  217. handle_exception();
  218. return NULL;
  219. }
  220. (*env)->DeleteLocalRef(env, jStrParam);
  221. // msg->add_to_branch_len => SipMsg.add_to_branch_len
  222. fid = (*env)->GetFieldID(env, SipMsgClass, "add_to_branch_len", "I");
  223. if (!fid)
  224. {
  225. (*env)->ExceptionClear(env);
  226. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.add_to_branch_len\n", APP_NAME);
  227. return NULL;
  228. }
  229. (*env)->SetIntField(env, SipMsgInstance, fid, msg->add_to_branch_len);
  230. if ((*env)->ExceptionCheck(env))
  231. {
  232. handle_exception();
  233. return NULL;
  234. }
  235. // msg->hash_index => SipMsg.hash_index
  236. fid = (*env)->GetFieldID(env, SipMsgClass, "hash_index", "I");
  237. if (!fid)
  238. {
  239. (*env)->ExceptionClear(env);
  240. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.hash_index\n", APP_NAME);
  241. return NULL;
  242. }
  243. (*env)->SetIntField(env, SipMsgInstance, fid, msg->hash_index);
  244. if ((*env)->ExceptionCheck(env))
  245. {
  246. handle_exception();
  247. return NULL;
  248. }
  249. // msg->msg_flags => SipMsg.msg_flags
  250. fid = (*env)->GetFieldID(env, SipMsgClass, "msg_flags", "I");
  251. if (!fid)
  252. {
  253. (*env)->ExceptionClear(env);
  254. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.msg_flags\n", APP_NAME);
  255. return NULL;
  256. }
  257. (*env)->SetIntField(env, SipMsgInstance, fid, msg->msg_flags);
  258. if ((*env)->ExceptionCheck(env))
  259. {
  260. handle_exception();
  261. return NULL;
  262. }
  263. // msg->set_global_address => SipMsg.set_global_address
  264. fid = (*env)->GetFieldID(env, SipMsgClass, "set_global_address", "Ljava/lang/String;");
  265. if (!fid)
  266. {
  267. (*env)->ExceptionClear(env);
  268. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.set_global_address\n", APP_NAME);
  269. return NULL;
  270. }
  271. jStrParam = (*env)->NewStringUTF(env, (msg->set_global_address.len <= 0 || msg->set_global_address.s == NULL) ? "" : msg->set_global_address.s);
  272. (*env)->SetObjectField(env, SipMsgInstance, fid, jStrParam);
  273. if ((*env)->ExceptionCheck(env))
  274. {
  275. handle_exception();
  276. return NULL;
  277. }
  278. (*env)->DeleteLocalRef(env, jStrParam);
  279. // msg->set_global_port => SipMsg.set_global_port
  280. fid = (*env)->GetFieldID(env, SipMsgClass, "set_global_port", "Ljava/lang/String;");
  281. if (!fid)
  282. {
  283. (*env)->ExceptionClear(env);
  284. LM_ERR("%s: Can't find symbol org.siprouter.SipMsg.set_global_port\n", APP_NAME);
  285. return NULL;
  286. }
  287. jStrParam = (*env)->NewStringUTF(env, (msg->set_global_port.len <= 0 || msg->set_global_port.s == NULL) ? "" : msg->set_global_port.s);
  288. (*env)->SetObjectField(env, SipMsgInstance, fid, jStrParam);
  289. if ((*env)->ExceptionCheck(env))
  290. {
  291. handle_exception();
  292. return NULL;
  293. }
  294. (*env)->DeleteLocalRef(env, jStrParam);
  295. return SipMsgInstance;
  296. }