2
0

java_msgobj.c 9.7 KB

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