perlfunc.c

Go to the documentation of this file.
00001 /*
00002  * $Id: perlfunc.c 5601 2009-02-12 19:29:34Z miconda $
00003  *
00004  * Perl module for OpenSER
00005  *
00006  * Copyright (C) 2006 Collax GmbH 
00007  *                    (Bastian Friedrich <bastian.friedrich@collax.com>)
00008  *
00009  * This file is part of Kamailio, a free SIP server.
00010  *
00011  * Kamailio is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version
00015  *
00016  * Kamailio is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  */
00026 
00027 #include <string.h>
00028 #include <stdio.h>
00029 
00030 #include "../../mem/mem.h"
00031 #include "../../data_lump.h"
00032 #include "../../parser/parse_param.h"
00033 #include "../../parser/msg_parser.h"
00034 #include "../../dprint.h"
00035 #include "../../action.h"
00036 #include "../../config.h"
00037 #include "../../parser/parse_uri.h"
00038 
00039 #include "perlfunc.h"
00040 #include "perl.h"
00041 
00042 
00043 /*
00044  * Check for existence of a function.
00045  */
00046 int perl_checkfnc(char *fnc) {
00047 
00048    if (get_cv(fnc, 0)) {
00049       return 1;
00050    } else {
00051       return 0;
00052    }
00053 }
00054 
00055 /*
00056  * Run function without paramters
00057  */
00058 
00059 int perl_exec_simple(char* fnc, char* args[], int flags) {
00060 
00061    if (perl_checkfnc(fnc)) {
00062       LM_DBG("running perl function \"%s\"", fnc);
00063 
00064       call_argv(fnc, flags, args);
00065    } else {
00066       LM_ERR("unknown function '%s' called.\n", fnc);
00067       return -1;
00068    }
00069 
00070    return 1;
00071 }
00072 
00073 int perl_exec_simple1(struct sip_msg* _msg, char* fnc, char* str2) {
00074    char *args[] = { NULL };
00075 
00076    return perl_exec_simple(fnc, args, G_DISCARD | G_NOARGS | G_EVAL);
00077 }
00078 
00079 int perl_exec_simple2(struct sip_msg* _msg, char* fnc, char* param) {
00080    char *args[] = { param, NULL };
00081 
00082    return perl_exec_simple(fnc, args, G_DISCARD | G_EVAL);
00083 }
00084 
00085 /*
00086  * Run function, with current SIP message as a parameter
00087  */
00088 int perl_exec1(struct sip_msg* _msg, char* fnc, char *foobar) {
00089    return perl_exec2(_msg, fnc, NULL);
00090 }
00091 
00092 int perl_exec2(struct sip_msg* _msg, char* fnc, char* mystr) {
00093    int retval;
00094    SV *m;
00095    str reason;
00096 
00097    dSP;
00098 
00099    if (!perl_checkfnc(fnc)) {
00100       LM_ERR("unknown perl function called.\n");
00101       reason.s = "Internal error";
00102       reason.len = sizeof("Internal error")-1;
00103       if (slb.send_reply(_msg, 500, &reason) == -1)
00104       {
00105          LM_ERR("failed to send reply\n");
00106       }
00107       return -1;
00108    }
00109    
00110    switch ((_msg->first_line).type) {
00111    case SIP_REQUEST:
00112       if (parse_sip_msg_uri(_msg) < 0) {
00113          LM_ERR("failed to parse Request-URI\n");
00114 
00115          reason.s = "Bad Request-URI";
00116          reason.len = sizeof("Bad Request-URI")-1;
00117          if (slb.send_reply(_msg, 400, &reason) == -1) {
00118             LM_ERR("failed to send reply\n");
00119          }
00120          return -1;
00121       }
00122       break;
00123    case SIP_REPLY:
00124       break;
00125    default:
00126       LM_ERR("invalid firstline");
00127       return -1;
00128    }
00129 
00130    m = sv_newmortal();
00131    sv_setref_pv(m, "OpenSER::Message", (void *)_msg);
00132    SvREADONLY_on(SvRV(m));
00133 
00134 
00135    ENTER;            /* everything created after here */
00136    SAVETMPS;         /* ...is a temporary variable.   */
00137    PUSHMARK(SP);        /* remember the stack pointer    */
00138    XPUSHs(m);        /* Our reference to the stack... */
00139 
00140    if (mystr)
00141       XPUSHs(sv_2mortal(newSVpv(mystr, strlen(mystr))));
00142                /* Our string to the stack... */
00143 
00144    PUTBACK;       /* make local stack pointer global */
00145 
00146    call_pv(fnc, G_EVAL|G_SCALAR);      /* call the function     */
00147    SPAGAIN;       /* refresh stack pointer         */
00148    /* pop the return value from stack */
00149    retval = POPi;
00150 
00151    PUTBACK;
00152    FREETMPS;         /* free that return value        */
00153    LEAVE;            /* ...and the XPUSHed "mortal" args.*/
00154 
00155    return retval;
00156 }

Generated on Thu May 24 02:00:29 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6