perlfunc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * $Id$
  3. *
  4. * Perl module for Kamailio
  5. *
  6. * Copyright (C) 2006 Collax GmbH
  7. * (Bastian Friedrich <[email protected]>)
  8. *
  9. * This file is part of Kamailio, a free SIP server.
  10. *
  11. * Kamailio is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * Kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. */
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include "../../mem/mem.h"
  29. #include "../../data_lump.h"
  30. #include "../../parser/parse_param.h"
  31. #include "../../parser/msg_parser.h"
  32. #include "../../dprint.h"
  33. #include "../../action.h"
  34. #include "../../config.h"
  35. #include "../../parser/parse_uri.h"
  36. #include "perlfunc.h"
  37. #include "app_perl_mod.h"
  38. /*
  39. * Check for existence of a function.
  40. */
  41. int perl_checkfnc(char *fnc) {
  42. if (get_cv(fnc, 0)) {
  43. return 1;
  44. } else {
  45. return 0;
  46. }
  47. }
  48. /*
  49. * Run function without paramters
  50. */
  51. int perl_exec_simple(char* fnc, char* args[], int flags) {
  52. app_perl_reset_interpreter();
  53. if (perl_checkfnc(fnc)) {
  54. LM_DBG("running perl function \"%s\"", fnc);
  55. call_argv(fnc, flags, args);
  56. } else {
  57. LM_ERR("unknown function '%s' called.\n", fnc);
  58. return -1;
  59. }
  60. return 1;
  61. }
  62. int perl_exec_simple1(struct sip_msg* _msg, char* fnc, char* str2) {
  63. char *args[] = { NULL };
  64. return perl_exec_simple(fnc, args, G_DISCARD | G_NOARGS | G_EVAL);
  65. }
  66. int perl_exec_simple2(struct sip_msg* _msg, char* fnc, char* param) {
  67. char *args[] = { param, NULL };
  68. return perl_exec_simple(fnc, args, G_DISCARD | G_EVAL);
  69. }
  70. /*
  71. * Run function, with current SIP message as a parameter
  72. */
  73. int perl_exec1(struct sip_msg* _msg, char* fnc, char *foobar) {
  74. return perl_exec2(_msg, fnc, NULL);
  75. }
  76. int perl_exec2(struct sip_msg* _msg, char* fnc, char* mystr) {
  77. int retval;
  78. SV *m;
  79. str reason;
  80. app_perl_reset_interpreter();
  81. dSP;
  82. if (!perl_checkfnc(fnc)) {
  83. LM_ERR("unknown perl function called.\n");
  84. reason.s = "Internal error";
  85. reason.len = sizeof("Internal error")-1;
  86. if (slb.freply(_msg, 500, &reason) == -1)
  87. {
  88. LM_ERR("failed to send reply\n");
  89. }
  90. return -1;
  91. }
  92. switch ((_msg->first_line).type) {
  93. case SIP_REQUEST:
  94. if (parse_sip_msg_uri(_msg) < 0) {
  95. LM_ERR("failed to parse Request-URI\n");
  96. reason.s = "Bad Request-URI";
  97. reason.len = sizeof("Bad Request-URI")-1;
  98. if (slb.freply(_msg, 400, &reason) == -1) {
  99. LM_ERR("failed to send reply\n");
  100. }
  101. return -1;
  102. }
  103. break;
  104. case SIP_REPLY:
  105. break;
  106. default:
  107. LM_ERR("invalid firstline");
  108. return -1;
  109. }
  110. ENTER; /* everything created after here */
  111. SAVETMPS; /* ...is a temporary variable. */
  112. PUSHMARK(SP); /* remember the stack pointer */
  113. m = sv_newmortal();
  114. sv_setref_pv(m, "Kamailio::Message", (void *)_msg);
  115. SvREADONLY_on(SvRV(m));
  116. XPUSHs(m); /* Our reference to the stack... */
  117. if (mystr)
  118. XPUSHs(sv_2mortal(newSVpv(mystr, strlen(mystr))));
  119. /* Our string to the stack... */
  120. PUTBACK; /* make local stack pointer global */
  121. call_pv(fnc, G_EVAL|G_SCALAR); /* call the function */
  122. SPAGAIN; /* refresh stack pointer */
  123. /* pop the return value from stack */
  124. retval = POPi;
  125. PUTBACK;
  126. FREETMPS; /* free that return value */
  127. LEAVE; /* ...and the XPUSHed "mortal" args.*/
  128. return retval;
  129. }