utils.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "../../sr_module.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_native_methods.h"
  35. #include "java_sig_parser.h"
  36. char **split(char *str, char *sep)
  37. {
  38. char **buf = NULL;
  39. char *token = NULL;
  40. char *saveptr = NULL;
  41. int i;
  42. buf = (char **)pkg_malloc(sizeof(char *));
  43. if (!buf)
  44. {
  45. LM_ERR("pkg_malloc() has failed. Not enough memory!\n");
  46. return NULL;
  47. }
  48. memset(&buf, 0, sizeof(char *));
  49. if (str == NULL)
  50. return buf;
  51. if (strncmp(str, sep, strlen(sep)) <= 0)
  52. {
  53. // string doesn't contains a separator
  54. buf[0] = strdup(str);
  55. return buf;
  56. }
  57. token = strdup(str);
  58. for (i=0; token != NULL; token = saveptr, i++)
  59. {
  60. token = strtok_r(token, (const char *)sep, &saveptr);
  61. if (token == NULL || !strcmp(token, ""))
  62. break;
  63. buf = (char **)pkg_realloc(buf, (i+1) * sizeof(char *));
  64. if (!buf)
  65. {
  66. LM_ERR("pkg_realloc() has failed. Not enough memory!\n");
  67. return NULL;
  68. }
  69. buf[i] = strdup(token);
  70. }
  71. buf[i] = NULL;
  72. free(token);
  73. return buf;
  74. }
  75. void ThrowNewException(JNIEnv *env, char *fmt, ...)
  76. {
  77. va_list ap;
  78. char buf[1024];
  79. memset(buf, 0, sizeof(char));
  80. va_start(ap, fmt);
  81. vsnprintf(buf, 1024, fmt, ap);
  82. va_end(ap);
  83. (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/Exception"), buf);
  84. }
  85. struct sip_msg *get_struct_sip_msg(JNIEnv *jenv)
  86. {
  87. jfieldID fid;
  88. int msgptr;
  89. jclass cls;
  90. cls = (*jenv)->GetObjectClass(jenv, KamailioClassInstance);
  91. fid = (*jenv)->GetFieldID(jenv, cls, "mop", "I");
  92. if (!fid)
  93. {
  94. (*jenv)->ExceptionClear(jenv);
  95. LM_ERR("Failed to find protected field org.siprouter.NativeMethods.mop\n");
  96. return NULL;
  97. }
  98. (*jenv)->DeleteLocalRef(jenv, cls);
  99. msgptr = (int)(*jenv)->GetIntField(jenv, KamailioClassInstance, fid);
  100. if ((*jenv)->ExceptionCheck(jenv))
  101. {
  102. handle_exception();
  103. return NULL;
  104. }
  105. if (msgptr == 0x0)
  106. {
  107. LM_ERR("app_java: KamExec(): Unable to execute. Internal Error: msgptr is NULL\n");
  108. return NULL;
  109. }
  110. return FORCE_CAST_O2P(msgptr, struct sip_msg *);
  111. }