perlfunc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. if (perl_checkfnc(fnc)) {
  53. LM_DBG("running perl function \"%s\"", fnc);
  54. call_argv(fnc, flags, args);
  55. } else {
  56. LM_ERR("unknown function '%s' called.\n", fnc);
  57. return -1;
  58. }
  59. return 1;
  60. }
  61. int perl_exec_simple1(struct sip_msg* _msg, char* fnc, char* str2) {
  62. char *args[] = { NULL };
  63. return perl_exec_simple(fnc, args, G_DISCARD | G_NOARGS | G_EVAL);
  64. }
  65. int perl_exec_simple2(struct sip_msg* _msg, char* fnc, char* param) {
  66. char *args[] = { param, NULL };
  67. return perl_exec_simple(fnc, args, G_DISCARD | G_EVAL);
  68. }
  69. /*
  70. * Run function, with current SIP message as a parameter
  71. */
  72. int perl_exec1(struct sip_msg* _msg, char* fnc, char *foobar) {
  73. return perl_exec2(_msg, fnc, NULL);
  74. }
  75. int perl_exec2(struct sip_msg* _msg, char* fnc, char* mystr) {
  76. int retval;
  77. SV *m;
  78. str reason;
  79. dSP;
  80. if (!perl_checkfnc(fnc)) {
  81. LM_ERR("unknown perl function called.\n");
  82. reason.s = "Internal error";
  83. reason.len = sizeof("Internal error")-1;
  84. if (slb.freply(_msg, 500, &reason) == -1)
  85. {
  86. LM_ERR("failed to send reply\n");
  87. }
  88. return -1;
  89. }
  90. switch ((_msg->first_line).type) {
  91. case SIP_REQUEST:
  92. if (parse_sip_msg_uri(_msg) < 0) {
  93. LM_ERR("failed to parse Request-URI\n");
  94. reason.s = "Bad Request-URI";
  95. reason.len = sizeof("Bad Request-URI")-1;
  96. if (slb.freply(_msg, 400, &reason) == -1) {
  97. LM_ERR("failed to send reply\n");
  98. }
  99. return -1;
  100. }
  101. break;
  102. case SIP_REPLY:
  103. break;
  104. default:
  105. LM_ERR("invalid firstline");
  106. return -1;
  107. }
  108. ENTER; /* everything created after here */
  109. SAVETMPS; /* ...is a temporary variable. */
  110. PUSHMARK(SP); /* remember the stack pointer */
  111. m = sv_newmortal();
  112. sv_setref_pv(m, "Kamailio::Message", (void *)_msg);
  113. SvREADONLY_on(SvRV(m));
  114. XPUSHs(m); /* Our reference to the stack... */
  115. if (mystr)
  116. XPUSHs(sv_2mortal(newSVpv(mystr, strlen(mystr))));
  117. /* Our string to the stack... */
  118. PUTBACK; /* make local stack pointer global */
  119. call_pv(fnc, G_EVAL|G_SCALAR); /* call the function */
  120. SPAGAIN; /* refresh stack pointer */
  121. /* pop the return value from stack */
  122. retval = POPi;
  123. PUTBACK;
  124. FREETMPS; /* free that return value */
  125. LEAVE; /* ...and the XPUSHed "mortal" args.*/
  126. return retval;
  127. }