2
0

java_native_methods.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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 "../../str.h"
  25. #include "../../sr_module.h"
  26. #include "../../ip_addr.h"
  27. #include "../../flags.h"
  28. #include <jni.h>
  29. #include "global.h"
  30. #include "utils.h"
  31. #include "java_mod.h"
  32. #include "java_iface.h"
  33. #include "java_support.h"
  34. #include "java_msgobj.h"
  35. #include "java_native_methods.h"
  36. #include "java_sig_parser.h"
  37. //// native methods ////
  38. /*
  39. java: native void LM_XXXXXX(Params XXXX);
  40. c: JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1XXXXXX(JNIEnv *jenv, jobject this, Params XXXX)
  41. Why (for example) Java_Kamailio_LM_1ERR but not Java_Kamailio_LM_ERR?
  42. See explaination here: http://qscribble.blogspot.ca/2012/04/underscores-in-jni-method-names.html
  43. Also, from here: http://192.9.162.55/docs/books/jni/html/design.html
  44. The JNI adopts a simple name-encoding scheme to ensure that all Unicode characters
  45. translate into valid C function names. The underscore ("_") character separates the
  46. components of fully qualified class names. Because a name or type descriptor never
  47. begins with a number, we can use _0, ..., _9 for escape sequences, as illustrated below:
  48. +-------------------+------------------------------------+
  49. | Escape Sequence | Denotes |
  50. +-------------------+------------------------------------+
  51. | _0XXXX | a Unicode character XXXX |
  52. | _1 | the character "_" |
  53. | _2 | the character ";" in descriptors |
  54. | _3 | the character "[" in descriptors |
  55. +-------------------+------------------------------------+
  56. */
  57. /*
  58. *** Java API ***
  59. Package: org.siprouter
  60. Class: NativeMethods
  61. Method: LM_ERR(Ljava/lang/String;)V
  62. Prototype: public static native void LM_ERR(String s);
  63. */
  64. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1ERR(JNIEnv *jenv, jobject this, jstring js)
  65. {
  66. const char *s;
  67. jboolean iscopy;
  68. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  69. if ((*jenv)->ExceptionCheck(jenv))
  70. {
  71. handle_exception();
  72. return;
  73. }
  74. LM_ERR("%s", s == NULL ? "null\n" : s);
  75. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  76. }
  77. /*
  78. *** Java API ***
  79. Package: org.siprouter
  80. Class: NativeMethods
  81. Method: LM_WARN(Ljava/lang/String;)V
  82. Prototype: public static native void LM_WARN(String s);
  83. */
  84. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1WARN(JNIEnv *jenv, jobject this, jstring js)
  85. {
  86. const char *s;
  87. jboolean iscopy;
  88. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  89. if ((*jenv)->ExceptionCheck(jenv))
  90. {
  91. handle_exception();
  92. return;
  93. }
  94. LM_WARN("%s", s == NULL ? "null\n" : s);
  95. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  96. }
  97. /*
  98. *** Java API ***
  99. Package: org.siprouter
  100. Class: NativeMethods
  101. Method: LM_NOTICE(Ljava/lang/String;)V
  102. Prototype: public static native void LM_NOTICE(String s);
  103. */
  104. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1NOTICE(JNIEnv *jenv, jobject this, jstring js)
  105. {
  106. const char *s;
  107. jboolean iscopy;
  108. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  109. if ((*jenv)->ExceptionCheck(jenv))
  110. {
  111. handle_exception();
  112. return;
  113. }
  114. LM_NOTICE("%s", s == NULL ? "null\n" : s);
  115. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  116. }
  117. /*
  118. *** Java API ***
  119. Package: org.siprouter
  120. Class: NativeMethods
  121. Method: LM_INFO(Ljava/lang/String;)V
  122. Prototype: public static native void LM_INFO(String s);
  123. */
  124. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1INFO(JNIEnv *jenv, jobject this, jstring js)
  125. {
  126. const char *s;
  127. jboolean iscopy;
  128. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  129. if ((*jenv)->ExceptionCheck(jenv))
  130. {
  131. handle_exception();
  132. return;
  133. }
  134. LM_INFO("%s", s == NULL ? "null\n" : s);
  135. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  136. }
  137. /*
  138. *** Java API ***
  139. Package: org.siprouter
  140. Class: NativeMethods
  141. Method: LM_DBG(Ljava/lang/String;)V
  142. Prototype: public static native void LM_DBG(String s);
  143. */
  144. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1DBG(JNIEnv *jenv, jobject this, jstring js)
  145. {
  146. const char *s;
  147. jboolean iscopy;
  148. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  149. if ((*jenv)->ExceptionCheck(jenv))
  150. {
  151. handle_exception();
  152. return;
  153. }
  154. LM_DBG("%s", s == NULL ? "null\n" : s);
  155. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  156. }
  157. /*
  158. *** Java API ***
  159. Package: org.siprouter
  160. Class: NativeMethods
  161. Method: LM_CRIT(Ljava/lang/String;)V
  162. Prototype: public static native void LM_CRIT(String s);
  163. */
  164. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1CRIT(JNIEnv *jenv, jobject this, jstring js)
  165. {
  166. const char *s;
  167. jboolean iscopy;
  168. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  169. if ((*jenv)->ExceptionCheck(jenv))
  170. {
  171. handle_exception();
  172. return;
  173. }
  174. LM_CRIT("%s", s == NULL ? "null\n" : s);
  175. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  176. }
  177. #ifdef LM_ALERT
  178. /*
  179. *** Java API ***
  180. Package: org.siprouter
  181. Class: NativeMethods
  182. Method: LM_ALERT(Ljava/lang/String;)V
  183. Prototype: public static native void LM_ALERT(String s);
  184. */
  185. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1ALERT(JNIEnv *jenv, jobject this, jstring js)
  186. {
  187. const char *s;
  188. jboolean iscopy;
  189. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  190. if ((*jenv)->ExceptionCheck(jenv))
  191. {
  192. handle_exception();
  193. return;
  194. }
  195. LM_ALERT("%s", s == NULL ? "null\n" : s);
  196. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  197. }
  198. #endif
  199. /*
  200. *** Java API ***
  201. Package: org.siprouter
  202. Class: NativeMethods
  203. Method: LM_GEN2(ILjava/lang/String;)V
  204. Prototype: public static native void LM_GEN1(int logLevel, String s);
  205. */
  206. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1GEN1(JNIEnv *jenv, jobject this, jint ll, jstring js)
  207. {
  208. const char *s;
  209. jboolean iscopy;
  210. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  211. if ((*jenv)->ExceptionCheck(jenv))
  212. {
  213. handle_exception();
  214. return;
  215. }
  216. LM_GEN1((int)ll, "%s", s == NULL ? "null\n" : s);
  217. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  218. }
  219. /*
  220. *** Java API ***
  221. Package: org.siprouter
  222. Class: NativeMethods
  223. Method: LM_GEN2(IILjava/lang/String;)V
  224. Prototype: public static native void LM_GEN2(int logLevel, int logFacility, String s);
  225. */
  226. JNIEXPORT void JNICALL Java_org_siprouter_NativeMethods_LM_1GEN2(JNIEnv *jenv, jobject this, jint ll, jint lf, jstring js)
  227. {
  228. const char *s;
  229. jboolean iscopy;
  230. s = (*jenv)->GetStringUTFChars(jenv, js, &iscopy);
  231. if ((*jenv)->ExceptionCheck(jenv))
  232. {
  233. handle_exception();
  234. return;
  235. }
  236. LM_GEN2((int)ll, (int)lf, "%s", s == NULL ? "null\n" : s);
  237. (*jenv)->ReleaseStringUTFChars(jenv, js, s);
  238. }
  239. /*
  240. *** Java API ***
  241. Package: org.siprouter
  242. Class: NativeMethods
  243. Method: KamExec(Ljava/lang/String;[Ljava/lang/String;)I
  244. Prototype: public static native int KamExec(String fname, String... params);
  245. */
  246. JNIEXPORT jint JNICALL Java_org_siprouter_NativeMethods_KamExec(JNIEnv *jenv, jobject this, jstring jfname, jobjectArray strArrParams)
  247. {
  248. int retval;
  249. char *fname;
  250. int argc;
  251. jsize pc;
  252. int i;
  253. char *argv[MAX_ACTIONS];
  254. jboolean is_copy;
  255. jstring strp;
  256. char *strc;
  257. if (jfname == NULL)
  258. {
  259. LM_ERR("%s: KamExec() required at least 1 argument (function name)\n", APP_NAME);
  260. return -1;
  261. }
  262. fname = (char *)(*jenv)->GetStringUTFChars(jenv, jfname, &is_copy);
  263. if ((*jenv)->ExceptionCheck(jenv))
  264. {
  265. handle_exception();
  266. return -1;
  267. }
  268. memset(argv, 0, MAX_ACTIONS * sizeof(char *));
  269. argc = 0;
  270. pc = (*jenv)->GetArrayLength(jenv, strArrParams);
  271. if (pc >= 6)
  272. {
  273. pc = 6;
  274. }
  275. for (i=0; i<pc; i++)
  276. {
  277. strp = (jstring)(*jenv)->GetObjectArrayElement(jenv, strArrParams, i);
  278. if ((*jenv)->ExceptionCheck(jenv))
  279. {
  280. handle_exception();
  281. return -1;
  282. }
  283. strc = (char *)(*jenv)->GetStringUTFChars(jenv, strp, &is_copy);
  284. if ((*jenv)->ExceptionCheck(jenv))
  285. {
  286. handle_exception();
  287. return -1;
  288. }
  289. if (strc)
  290. {
  291. argv[argc++] = strc;
  292. }
  293. }
  294. retval = KamExec(jenv, fname, argc, argv);
  295. (*jenv)->ReleaseStringUTFChars(jenv, jfname, fname);
  296. return (jint)retval;
  297. }
  298. int KamExec(JNIEnv *jenv, char *fname, int argc, char **argv)
  299. {
  300. sr31_cmd_export_t *fexport;
  301. unsigned mod_ver;
  302. int rval;
  303. int mod_type;
  304. struct action *act;
  305. struct run_act_ctx ra_ctx;
  306. int i;
  307. if (!msg)
  308. return -1;
  309. fexport = find_export_record(fname, argc, 0, &mod_ver);
  310. if (!fexport)
  311. {
  312. LM_ERR("%s: KamExec(): '%s' - no such function\n", APP_NAME, fname);
  313. return -1;
  314. }
  315. /* check fixups */
  316. if (force_cmd_exec == 0 && fexport->fixup != NULL && fexport->free_fixup == NULL)
  317. {
  318. LM_ERR("%s: KamExec(): function '%s' has fixup - cannot be used\n", APP_NAME, fname);
  319. return -1;
  320. }
  321. switch(fexport->param_no)
  322. {
  323. case 0: mod_type = MODULE0_T; break;
  324. case 1: mod_type = MODULE1_T; break;
  325. case 2: mod_type = MODULE2_T; break;
  326. case 3: mod_type = MODULE3_T; break;
  327. case 4: mod_type = MODULE4_T; break;
  328. case 5: mod_type = MODULE5_T; break;
  329. case 6: mod_type = MODULE6_T; break;
  330. case VAR_PARAM_NO: mod_type = MODULEX_T; break;
  331. default:
  332. LM_ERR("%s: KamExec(): unknown/bad definition for function '%s' (%d params)\n", APP_NAME, fname, fexport->param_no);
  333. return -1;
  334. }
  335. act = mk_action(mod_type, (argc+2), /* number of (type, value) pairs */
  336. MODEXP_ST, fexport, /* function */
  337. NUMBER_ST, argc, /* parameter number */
  338. STRING_ST, argv[0], /* param. 1 */
  339. STRING_ST, argv[1], /* param. 2 */
  340. STRING_ST, argv[2], /* param. 3 */
  341. STRING_ST, argv[3], /* param. 4 */
  342. STRING_ST, argv[4], /* param. 5 */
  343. STRING_ST, argv[5] /* param. 6 */
  344. );
  345. if (!act)
  346. {
  347. LM_ERR("%s: KamExec(): action structure couldn't be created\n", APP_NAME);
  348. return -1;
  349. }
  350. /* handle fixups */
  351. if (fexport->fixup)
  352. {
  353. if (argc == 0)
  354. {
  355. rval = fexport->fixup(0, 0);
  356. if (rval < 0)
  357. {
  358. LM_ERR("%s: KamExec(): (no params) Error in fixup (0) for '%s'\n", APP_NAME, fname);
  359. return -1;
  360. }
  361. }
  362. else
  363. {
  364. for (i=0; i<=argc; i++)
  365. {
  366. if (act->val[i+2].u.data != 0x0)
  367. {
  368. rval = fexport->fixup(&(act->val[i+2].u.data), i+1);
  369. if (rval < 0)
  370. {
  371. LM_ERR("%s: KamExec(): (params: %d) Error in fixup (%d) for '%s'\n", APP_NAME, argc, i+1, fname);
  372. return -1;
  373. }
  374. act->val[i+2].type = MODFIXUP_ST;
  375. }
  376. }
  377. }
  378. }
  379. init_run_actions_ctx(&ra_ctx);
  380. rval = do_action(&ra_ctx, act, msg);
  381. /* free fixups */
  382. if (fexport->free_fixup)
  383. {
  384. for (i=0; i<=argc; i++)
  385. {
  386. if ((act->val[i+2].type == MODFIXUP_ST) && (act->val[i+2].u.data))
  387. {
  388. fexport->free_fixup(&(act->val[i+2].u.data), i+1);
  389. }
  390. }
  391. }
  392. pkg_free(act);
  393. return rval;
  394. }
  395. /*
  396. *** Java API ***
  397. Package: org.siprouter
  398. Class: SipMsg
  399. Method: ParseSipMsg()Lorg/siprouter/SipMsg;
  400. Prototype: public static native org.siprouter.SipMsg ParseSipMsg();
  401. */
  402. JNIEXPORT jobject JNICALL Java_org_siprouter_SipMsg_ParseSipMsg(JNIEnv *jenv, jobject this)
  403. {
  404. if (!msg)
  405. return NULL;
  406. return fill_sipmsg_object(jenv, msg);
  407. }
  408. /*
  409. *** Java API ***
  410. Package: org.siprouter
  411. Class: SipMsg
  412. Method: getMsgType()Ljava/org/String;
  413. Prototype: public static native String getMsgType();
  414. */
  415. JNIEXPORT jstring JNICALL Java_org_siprouter_SipMsg_getMsgType(JNIEnv *jenv, jobject this)
  416. {
  417. char *cs;
  418. jstring js;
  419. if (!msg)
  420. return NULL;
  421. switch ((msg->first_line).type)
  422. {
  423. case SIP_REQUEST:
  424. cs = "SIP_REQUEST";
  425. break;
  426. case SIP_REPLY:
  427. cs = "SIP_REPLY";
  428. break;
  429. default:
  430. cs = "SIP_INVALID";
  431. break;
  432. }
  433. js = (*jenv)->NewStringUTF(jenv, cs);
  434. if ((*jenv)->ExceptionCheck(jenv))
  435. {
  436. handle_exception();
  437. return NULL;
  438. }
  439. return js;
  440. }
  441. /*
  442. *** Java API ***
  443. Package: org.siprouter
  444. Class: SipMsg
  445. Method: getStatus()Ljava/org/String;
  446. Prototype: public static native String getStatus();
  447. */
  448. JNIEXPORT jstring JNICALL Java_org_siprouter_SipMsg_getStatus(JNIEnv *jenv, jobject this)
  449. {
  450. str *cs;
  451. jstring js;
  452. if (!msg)
  453. return NULL;
  454. if ((msg->first_line).type != SIP_REQUEST)
  455. {
  456. LM_ERR("%s: getStatus(): Unable to fetch status. Error: Not a request message - no method available.\n", APP_NAME);
  457. return NULL;
  458. }
  459. cs = &((msg->first_line).u.request.method);
  460. js = (*jenv)->NewStringUTF(jenv, (cs && cs->s && cs->len > 0) ? cs->s : "");
  461. if ((*jenv)->ExceptionCheck(jenv))
  462. {
  463. handle_exception();
  464. return NULL;
  465. }
  466. return js;
  467. }
  468. /*
  469. *** Java API ***
  470. Package: org.siprouter
  471. Class: SipMsg
  472. Method: getRURI()Ljava/org/String;
  473. Prototype: public static native String getRURI();
  474. */
  475. JNIEXPORT jstring JNICALL Java_org_siprouter_SipMsg_getRURI(JNIEnv *jenv, jobject this)
  476. {
  477. str *cs;
  478. jstring js;
  479. if (!msg)
  480. return NULL;
  481. if ((msg->first_line).type != SIP_REQUEST)
  482. {
  483. LM_ERR("%s: getRURI(): Unable to fetch ruri. Error: Not a request message - no method available.\n", APP_NAME);
  484. return NULL;
  485. }
  486. cs = &((msg->first_line).u.request.uri);
  487. js = (*jenv)->NewStringUTF(jenv, (cs && cs->s && cs->len > 0) ? cs->s : "");
  488. if ((*jenv)->ExceptionCheck(jenv))
  489. {
  490. handle_exception();
  491. return NULL;
  492. }
  493. return js;
  494. }
  495. /*
  496. *** Java API ***
  497. Package: org.siprouter
  498. Class: SipMsg
  499. Method: getSrcAddress()Lorg/siprouter/IPPair;
  500. Prototype: public static native org.siprouter.IPPair getSrcAddress();
  501. */
  502. JNIEXPORT jobject JNICALL Java_org_siprouter_SipMsg_getSrcAddress(JNIEnv *jenv, jobject this)
  503. {
  504. jclass ippair_cls;
  505. jmethodID ippair_cls_id;
  506. jobject ippair_cls_instance;
  507. char *ip;
  508. jstring jip;
  509. int port;
  510. if (!msg)
  511. return NULL;
  512. ippair_cls = (*jenv)->FindClass(jenv, "org/siprouter/IPPair");
  513. if ((*jenv)->ExceptionCheck(jenv))
  514. {
  515. handle_exception();
  516. return NULL;
  517. }
  518. ippair_cls_id = (*jenv)->GetMethodID(jenv, ippair_cls, "<init>", "(Ljava/lang/String;I)V");
  519. if (!ippair_cls_id || (*jenv)->ExceptionCheck(jenv))
  520. {
  521. handle_exception();
  522. return NULL;
  523. }
  524. ip = ip_addr2a(&msg->rcv.src_ip);
  525. if (!ip)
  526. {
  527. LM_ERR("%s: getSrcAddress(): Unable to fetch src ip address.\n", APP_NAME);
  528. return NULL;
  529. }
  530. jip = (*jenv)->NewStringUTF(jenv, ip);
  531. if ((*jenv)->ExceptionCheck(jenv))
  532. {
  533. handle_exception();
  534. return NULL;
  535. }
  536. port = msg->rcv.src_port;
  537. if (port == 0x0)
  538. {
  539. LM_ERR("%s: getSrcAddress(): Unable to fetch src port.\n", APP_NAME);
  540. return NULL;
  541. }
  542. // calling constructor
  543. ippair_cls_instance = (*jenv)->NewObject(jenv, ippair_cls, ippair_cls_id, (jstring)jip, (jint)port);
  544. if (!ippair_cls_instance || (*jenv)->ExceptionCheck(jenv))
  545. {
  546. handle_exception();
  547. return NULL;
  548. }
  549. return ippair_cls_instance;
  550. }
  551. /*
  552. *** Java API ***
  553. Package: org.siprouter
  554. Class: SipMsg
  555. Method: getDstAddress()Lorg/siprouter/IPPair;
  556. Prototype: public static native org.siprouter.IPPair getDstAddress();
  557. */
  558. JNIEXPORT jobject JNICALL Java_org_siprouter_SipMsg_getDstAddress(JNIEnv *jenv, jobject this)
  559. {
  560. jclass ippair_cls;
  561. jmethodID ippair_cls_id;
  562. jobject ippair_cls_instance;
  563. char *ip;
  564. jstring jip;
  565. int port;
  566. if (!msg)
  567. return NULL;
  568. ippair_cls = (*jenv)->FindClass(jenv, "org/siprouter/IPPair");
  569. if ((*jenv)->ExceptionCheck(jenv))
  570. {
  571. handle_exception();
  572. return NULL;
  573. }
  574. ippair_cls_id = (*jenv)->GetMethodID(jenv, ippair_cls, "<init>", "(Ljava/lang/String;I)V");
  575. if (!ippair_cls_id || (*jenv)->ExceptionCheck(jenv))
  576. {
  577. handle_exception();
  578. return NULL;
  579. }
  580. ip = ip_addr2a(&msg->rcv.dst_ip);
  581. if (!ip)
  582. {
  583. LM_ERR("%s: getDstAddress(): Unable to fetch src ip address.\n", APP_NAME);
  584. return NULL;
  585. }
  586. jip = (*jenv)->NewStringUTF(jenv, ip);
  587. if ((*jenv)->ExceptionCheck(jenv))
  588. {
  589. handle_exception();
  590. return NULL;
  591. }
  592. port = msg->rcv.dst_port;
  593. if (port == 0x0)
  594. {
  595. LM_ERR("%s: getDstAddress(): Unable to fetch src port.\n", APP_NAME);
  596. return NULL;
  597. }
  598. // calling constructor
  599. ippair_cls_instance = (*jenv)->NewObject(jenv, ippair_cls, ippair_cls_id, (jstring)jip, (jint)port);
  600. if (!ippair_cls_instance || (*jenv)->ExceptionCheck(jenv))
  601. {
  602. handle_exception();
  603. return NULL;
  604. }
  605. return ippair_cls_instance;
  606. }
  607. /*
  608. *** Java API ***
  609. Package: org.siprouter
  610. Class: SipMsg
  611. Method: getBuffer()Ljava/org/String;
  612. Prototype: public static native String getBuffer();
  613. */
  614. JNIEXPORT jstring JNICALL Java_org_siprouter_SipMsg_getBuffer(JNIEnv *jenv, jobject this)
  615. {
  616. jstring js;
  617. if (!msg)
  618. return NULL;
  619. if ((msg->first_line).type != SIP_REQUEST)
  620. {
  621. LM_ERR("%s: getRURI(): Unable to fetch ruri. Error: Not a request message - no method available.\n", APP_NAME);
  622. return NULL;
  623. }
  624. js = (*jenv)->NewStringUTF(jenv, msg->buf ? msg->buf : "");
  625. if ((*jenv)->ExceptionCheck(jenv))
  626. {
  627. handle_exception();
  628. return NULL;
  629. }
  630. return js;
  631. }
  632. ///// Core Functions /////
  633. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  634. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  635. /*
  636. *** Java API ***
  637. Package: org.siprouter
  638. Class: CoreMethods
  639. Method: seturi(Ljava/org/String;)I
  640. Prototype: public static native int seturi(String uri);
  641. */
  642. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_seturi(JNIEnv *jenv, jobject this, jstring juri)
  643. {
  644. return cf_seturi(jenv, this, juri, "seturi");
  645. }
  646. /*
  647. *** Java API ***
  648. Package: org.siprouter
  649. Class: CoreMethods
  650. Method: rewriteuri(Ljava/org/String;)I
  651. Prototype: public static native int rewriteuri(String uri);
  652. */
  653. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_rewriteuri(JNIEnv *jenv, jobject this, jstring juri)
  654. {
  655. return cf_seturi(jenv, this, juri, "rewriteuri");
  656. }
  657. /* wrapped function */
  658. jint cf_seturi(JNIEnv *jenv, jobject this, jstring juri, char *fname)
  659. {
  660. struct action act;
  661. struct run_act_ctx ra_ctx;
  662. int retval;
  663. jboolean is_copy;
  664. char *curi;
  665. if (!msg)
  666. {
  667. LM_ERR("%s: %s: Can't process, msg=NULL\n", APP_NAME, fname);
  668. return -1;
  669. }
  670. curi = (char *)(*jenv)->GetStringUTFChars(jenv, juri, &is_copy);
  671. if ((*jenv)->ExceptionCheck(jenv))
  672. {
  673. handle_exception();
  674. return -1;
  675. }
  676. memset(&act, 0, sizeof(act));
  677. act.type = SET_URI_T;
  678. act.val[0].type = STRING_ST;
  679. act.val[0].u.str.s = curi;
  680. act.val[0].u.str.len = strlen(curi);
  681. init_run_actions_ctx(&ra_ctx);
  682. retval = do_action(&ra_ctx, &act, msg);
  683. (*jenv)->ReleaseStringUTFChars(jenv, juri, curi);
  684. return (jint)retval;
  685. }
  686. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  687. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  688. /*
  689. *** Java API ***
  690. Package: org.siprouter
  691. Class: CoreMethods
  692. Method: add_local_rport()I
  693. Prototype: public static native int add_local_rport();
  694. */
  695. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_add_1local_1rport(JNIEnv *jenv, jobject this)
  696. {
  697. struct action act;
  698. struct run_act_ctx ra_ctx;
  699. int retval;
  700. if (!msg)
  701. {
  702. LM_ERR("%s: add_local_rport: Can't process, msg=NULL\n", APP_NAME);
  703. return -1;
  704. }
  705. memset(&act, 0, sizeof(act));
  706. act.type = ADD_LOCAL_RPORT_T;
  707. init_run_actions_ctx(&ra_ctx);
  708. retval = do_action(&ra_ctx, &act, msg);
  709. return (jint)retval;
  710. }
  711. /*
  712. *** Java API ***
  713. Package: org.siprouter
  714. Class: CoreMethods
  715. Method: append_branch()I
  716. Method(o): append_branch(Ljava/lang/String)I
  717. Prototype: public static native int append_branch();
  718. Prototype(o): public static native int append_branch(String branch);
  719. */
  720. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_append_1branch(JNIEnv *jenv, jobject this, jstring jbranch)
  721. {
  722. struct action act;
  723. struct run_act_ctx ra_ctx;
  724. int retval;
  725. jboolean is_copy;
  726. char *cbranch;
  727. if (!msg)
  728. {
  729. LM_ERR("%s: append_branch: Can't process, msg=NULL\n", APP_NAME);
  730. return -1;
  731. }
  732. memset(&act, 0, sizeof(act));
  733. act.type = APPEND_BRANCH_T;
  734. cbranch = NULL;
  735. if (jbranch)
  736. {
  737. cbranch = (char *)(*jenv)->GetStringUTFChars(jenv, jbranch, &is_copy);
  738. if ((*jenv)->ExceptionCheck(jenv))
  739. {
  740. handle_exception();
  741. return -1;
  742. }
  743. act.val[0].type = STR_ST;
  744. act.val[0].u.str.s = cbranch;
  745. act.val[0].u.str.len = strlen(cbranch);
  746. }
  747. init_run_actions_ctx(&ra_ctx);
  748. retval = do_action(&ra_ctx, &act, msg);
  749. if (cbranch)
  750. {
  751. (*jenv)->ReleaseStringUTFChars(jenv, jbranch, cbranch);
  752. }
  753. return (jint)retval;
  754. }
  755. /*
  756. *** Java API ***
  757. Package: org.siprouter
  758. Class: CoreMethods
  759. Method: drop()I
  760. Prototype: public static native int drop();
  761. Returns:
  762. 0 if action -> end of list(e.g DROP)
  763. > 0 to continue processing next actions
  764. < 0 on error
  765. */
  766. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_drop(JNIEnv *jenv, jobject this)
  767. {
  768. struct action act;
  769. struct run_act_ctx ra_ctx;
  770. int retval;
  771. if (!msg)
  772. {
  773. LM_ERR("%s: drop: Can't process, msg=NULL\n", APP_NAME);
  774. return -1;
  775. }
  776. memset(&act, 0, sizeof(act));
  777. act.type = DROP_T;
  778. act.val[0].type = NUMBER_ST;
  779. act.val[0].u.number = 0;
  780. init_run_actions_ctx(&ra_ctx);
  781. retval = do_action(&ra_ctx, &act, msg);
  782. return (jint)retval;
  783. }
  784. /*
  785. *** Java API ***
  786. Package: org.siprouter
  787. Class: CoreMethods
  788. Method: force_rport()I
  789. Prototype: public static native int force_rport();
  790. */
  791. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_force_1rport(JNIEnv *jenv, jobject this)
  792. {
  793. return cf_force_rport(jenv, this, "force_rport");
  794. }
  795. /*
  796. *** Java API ***
  797. Package: org.siprouter
  798. Class: CoreMethods
  799. Method: add_rport()I
  800. Prototype: public static native int add_rport();
  801. */
  802. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_add_1rport(JNIEnv *jenv, jobject this)
  803. {
  804. return cf_force_rport(jenv, this, "add_rport");
  805. }
  806. /* wrapped function */
  807. jint cf_force_rport(JNIEnv *jenv, jobject this, char *fname)
  808. {
  809. struct action act;
  810. struct run_act_ctx ra_ctx;
  811. int retval;
  812. if (!msg)
  813. {
  814. LM_ERR("%s: %s: Can't process, msg=NULL\n", APP_NAME, fname);
  815. return -1;
  816. }
  817. memset(&act, 0, sizeof(act));
  818. act.type = FORCE_RPORT_T;
  819. init_run_actions_ctx(&ra_ctx);
  820. retval = do_action(&ra_ctx, &act, msg);
  821. return (jint)retval;
  822. }
  823. /*
  824. *** Java API ***
  825. Package: org.siprouter
  826. Class: CoreMethods
  827. Method: force_send_socket(Ljava/lang/String;I)I
  828. Prototype: public static native int force_send_socket(String srchost, int srcport);
  829. */
  830. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_force_1send_1socket(JNIEnv *jenv, jobject this, jstring jsrchost, jint jsrcport)
  831. {
  832. struct action act;
  833. struct run_act_ctx ra_ctx;
  834. int retval;
  835. jboolean is_copy;
  836. struct socket_id *si;
  837. struct name_lst *nl;
  838. if (!msg)
  839. {
  840. LM_ERR("%s: force_send_socket: Can't process, msg=NULL\n", APP_NAME);
  841. return -1;
  842. }
  843. nl = (struct name_lst *)pkg_malloc(sizeof(struct name_lst));
  844. if (!nl)
  845. {
  846. LM_ERR("%s: force_send_socket: pkg_malloc() has failed. Not enough memory!\n", APP_NAME);
  847. return -1;
  848. }
  849. si = (struct socket_id *)pkg_malloc(sizeof(struct socket_id));
  850. if (!si)
  851. {
  852. LM_ERR("%s: force_send_socket: pkg_malloc() has failed. Not enough memory!\n", APP_NAME);
  853. return -1;
  854. }
  855. memset(&act, 0, sizeof(act));
  856. act.type = FORCE_SEND_SOCKET_T;
  857. nl->name = (char *)(*jenv)->GetStringUTFChars(jenv, jsrchost, &is_copy);
  858. if ((*jenv)->ExceptionCheck(jenv))
  859. {
  860. handle_exception();
  861. return -1;
  862. }
  863. nl->next = NULL;
  864. nl->flags = 0;
  865. si->addr_lst = nl;
  866. si->flags = 0;
  867. si->proto = PROTO_NONE;
  868. si->port = (int)jsrcport;
  869. act.val[0].type = SOCKETINFO_ST;
  870. act.val[0].u.data = si;
  871. init_run_actions_ctx(&ra_ctx);
  872. retval = do_action(&ra_ctx, &act, msg);
  873. (*jenv)->ReleaseStringUTFChars(jenv, jsrchost, nl->name);
  874. pkg_free(nl);
  875. pkg_free(si);
  876. return (jint)retval;
  877. }
  878. /*
  879. *** Java API ***
  880. Package: org.siprouter
  881. Class: CoreMethods
  882. Method: forward()I
  883. Method(o): forward(Ljava/lang/String;I)I
  884. Prototype: public static native int forward();
  885. Prototype(o): public static native int forward(String ruri, int i);
  886. */
  887. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_forward(JNIEnv *jenv, jobject this, jstring jrurihost, jint juriport)
  888. {
  889. struct action act;
  890. struct run_act_ctx ra_ctx;
  891. int retval;
  892. jboolean is_copy;
  893. char *crurihost;
  894. if (!msg)
  895. {
  896. LM_ERR("%s: forward: Can't process, msg=NULL\n", APP_NAME);
  897. return -1;
  898. }
  899. memset(&act, 0, sizeof(act));
  900. act.type = FORWARD_T;
  901. crurihost = NULL;
  902. if (jrurihost)
  903. {
  904. crurihost = (char *)(*jenv)->GetStringUTFChars(jenv, jrurihost, &is_copy);
  905. if ((*jenv)->ExceptionCheck(jenv))
  906. {
  907. handle_exception();
  908. return -1;
  909. }
  910. act.val[0].type = URIHOST_ST;
  911. act.val[0].u.str.s = crurihost;
  912. act.val[0].u.str.len = strlen(crurihost);
  913. act.val[1].type = NUMBER_ST;
  914. act.val[1].u.number = (int)juriport;
  915. }
  916. init_run_actions_ctx(&ra_ctx);
  917. retval = do_action(&ra_ctx, &act, msg);
  918. if (crurihost)
  919. {
  920. (*jenv)->ReleaseStringUTFChars(jenv, jrurihost, crurihost);
  921. }
  922. return (jint)retval;
  923. }
  924. /*
  925. *** Java API ***
  926. Package: org.siprouter
  927. Class: CoreMethods
  928. Method: isflagset(I)Z
  929. Prototype: public static native boolean isflagset(int flag);
  930. */
  931. JNIEXPORT jboolean JNICALL Java_org_siprouter_CoreMethods_isflagset(JNIEnv *jenv, jobject this, jint jflag)
  932. {
  933. if (!msg)
  934. {
  935. LM_ERR("%s: isflagset: Can't process, msg=NULL\n", APP_NAME);
  936. return -1;
  937. }
  938. return isflagset(msg, (int)jflag) == 1 ? JNI_TRUE : JNI_FALSE;
  939. }
  940. /*
  941. *** Java API ***
  942. Package: org.siprouter
  943. Class: CoreMethods
  944. Method: setflag(I)V
  945. Prototype: public static native void setflag(int flag);
  946. */
  947. JNIEXPORT void JNICALL Java_org_siprouter_CoreMethods_setflag(JNIEnv *jenv, jobject this, jint jflag)
  948. {
  949. if (!msg)
  950. {
  951. LM_ERR("%s: setflag: Can't process, msg=NULL\n", APP_NAME);
  952. return;
  953. }
  954. setflag(msg, (int)jflag);
  955. }
  956. /*
  957. *** Java API ***
  958. Package: org.siprouter
  959. Class: CoreMethods
  960. Method: resetflag(I)V
  961. Prototype: public static native void resetflag(int flag);
  962. */
  963. JNIEXPORT void JNICALL Java_org_siprouter_CoreMethods_resetflag(JNIEnv *jenv, jobject this, jint jflag)
  964. {
  965. if (!msg)
  966. {
  967. LM_ERR("%s: resetflag: Can't process, msg=NULL\n", APP_NAME);
  968. return;
  969. }
  970. resetflag(msg, (int)jflag);
  971. }
  972. /*
  973. *** Java API ***
  974. Package: org.siprouter
  975. Class: CoreMethods
  976. Method: revert_uri()I
  977. Prototype: public static native int revert_uri();
  978. */
  979. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_revert_1uri(JNIEnv *jenv, jobject this)
  980. {
  981. struct action act;
  982. struct run_act_ctx ra_ctx;
  983. int retval;
  984. if (!msg)
  985. {
  986. LM_ERR("%s: revert_uri: Can't process, msg=NULL\n", APP_NAME);
  987. return -1;
  988. }
  989. memset(&act, 0, sizeof(act));
  990. act.type = REVERT_URI_T;
  991. init_run_actions_ctx(&ra_ctx);
  992. retval = do_action(&ra_ctx, &act, msg);
  993. return (jint)retval;
  994. }
  995. /*
  996. *** Java API ***
  997. Package: org.siprouter
  998. Class: CoreMethods
  999. Method: route(Ljava/lang/String)I
  1000. Prototype: public static native int route(String target);
  1001. */
  1002. JNIEXPORT jint JNICALL Java_org_siprouter_CoreMethods_route(JNIEnv *jenv, jobject this, jstring jtarget)
  1003. {
  1004. struct action act;
  1005. struct run_act_ctx ra_ctx;
  1006. int retval;
  1007. jboolean is_copy;
  1008. char *ctarget;
  1009. ctarget = (char *)(*jenv)->GetStringUTFChars(jenv, jtarget, &is_copy);
  1010. if ((*jenv)->ExceptionCheck(jenv))
  1011. {
  1012. handle_exception();
  1013. return -1;
  1014. }
  1015. retval = route_lookup(&main_rt, ctarget);
  1016. if (retval == -1) // route index lookup failed.
  1017. {
  1018. LM_ERR("%s: route: failed to find route name '%s'\n", APP_NAME, ctarget);
  1019. (*jenv)->ReleaseStringUTFChars(jenv, jtarget, ctarget);
  1020. return -1;
  1021. }
  1022. act.type = ROUTE_T;
  1023. act.val[0].type = NUMBER_ST;
  1024. act.val[0].u.number = retval;
  1025. init_run_actions_ctx(&ra_ctx);
  1026. retval = do_action(&ra_ctx, &act, msg);
  1027. (*jenv)->ReleaseStringUTFChars(jenv, jtarget, ctarget);
  1028. return retval;
  1029. }