Ejemplo n.º 1
0
/* searches the module list and returns pointer to the "name" function or
 * 0 if not found 
 * flags parameter is OR value of all flags that must match
 */
cmd_function find_export(char* name, int param_no, int flags)
{
	cmd_export_t* cmd;

	cmd = find_cmd_export_t(name, param_no, flags);
	if (cmd==0)
		return 0;
	return cmd->function;
}
Ejemplo n.º 2
0
static int l_siplua_moduleFunc(lua_State *L)
{
  struct sipapi_object *o;
  const char *func;
  int n, nargs;
  cmd_export_t *exp_func_struct;
  action_elem_t elems[MAX_ACTION_ELEMS];
  const char *s, *msg;
  char *str;
  int i;
  struct action *act;
  int retval;

  o = luaL_checkudata(L, 1, "siplua.api");
  func = luaL_checkstring(L, 2);
  n = lua_gettop(L);
  nargs = n - 2;
  if (n - 1 > MAX_ACTION_ELEMS)
    return luaL_error(L, "function '%s' called with too many arguments [%d > %d]",
	       func, nargs, MAX_ACTION_ELEMS - 1);
  exp_func_struct = find_cmd_export_t((char *)func, nargs, 0);
  if (!exp_func_struct)
    {
      return luaL_error(L, "function '%s' called, but not available.");
    }
  elems[0].type = CMD_ST;
  elems[0].u.data = exp_func_struct;
  memset(&elems[1], '\0', nargs * sizeof(action_elem_t));
  for (i = 0; i < nargs; ++i)
    {
      s = lua_tostring(L, 3 + i);
      if (!s)
	{
	  siplua_moduleFunc_free(func, exp_func_struct, elems, nargs);
	  msg = lua_pushfstring(L, "%s expected, got %s",
				lua_typename(L, LUA_TSTRING), luaL_typename(L, 3 + i));
	  return luaL_argerror(L, 3 + i, msg);
	}
      str = pkg_malloc(strlen(s) + 1);
      if (!str)
	{
	  siplua_moduleFunc_free(func, exp_func_struct, elems, nargs);
	  return luaL_error(L, "Not enough memory");
	}
      strcpy(str, s);
      /* We should maybe try STR_ST and elems[].u.str.{s,len} */
      elems[i + 1].type = STRING_ST;
      elems[i + 1].u.data = str; /* elems[].u.string */
    }
  act = mk_action(MODULE_T, n - 2 + 1, elems, 0, "lua");
  if (!act)
    {
      siplua_moduleFunc_free(func, exp_func_struct, elems, nargs);
      return luaL_error(L, "action structure could not be created. Error.");
    }
/*   siplua_log(L_DBG, "fixup/%p free_fixup/%p", */
/* 	     exp_func_struct->fixup, */
/* 	     exp_func_struct->free_fixup); */
  if (exp_func_struct->fixup)
    {
      if (!siplua_unsafemodfnc && !exp_func_struct->free_fixup)
	{
	  siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs);
	  return luaL_error(L, "Module function '%s' is unsafe. Call is refused.\n", func);
	}
      if (nargs == 0)
	{
	  retval = exp_func_struct->fixup(0, 0);
	  if (retval < 0)
	    {
	      siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs);
	      return luaL_error(L, "Error in fixup (0)\n");
	    }
	}
      for (i = 0; i < nargs; ++i)
	{
	  retval = exp_func_struct->fixup(&act->elem[i + 1].u.data, i + 1);
	  if (retval < 0)
	    {
	      siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs);
	      return luaL_error(L, "Error in fixup (%d)\n", i + 1);
	    }
	  act->elem[i + 1].type = MODFIXUP_ST;
	}
    }
  retval = do_action(act, o->msg);
  siplua_moduleFunc_free(func, exp_func_struct, act->elem, nargs);
  pkg_free(act);
  lua_pushinteger(L, retval);
  return 1;
}
Ejemplo n.º 3
0
int moduleFunc(struct sip_msg *m, char *func,
	       char *param1, char *param2,
	       int *retval) {

	cmd_export_t *exp_func_struct;
	struct action *act;
	char *argv[2];
	int argc = 0;
	action_elem_t elems[MAX_ACTION_ELEMS];

	if (!func) {
		LM_ERR("moduleFunc called with null function name. Error.");
		return -1;
	}

	if ((!param1) && param2) {
		LM_ERR("moduleFunc called with parameter 1 UNSET and"
			   " parameter 2 SET. Error.");
		return -1;
	}


	if (param1) {
		argv[0] = (char *)pkg_malloc(strlen(param1)+1);
		strcpy(argv[0], param1);
		argc++;
	} else {
		argv[0] = NULL;
	}

	if (param2) {
		argv[1] = (char *)pkg_malloc(strlen(param2)+1);
		strcpy(argv[1], param2);
		argc++;
	} else {
		argv[1] = NULL;
	}

	exp_func_struct = find_cmd_export_t(func, argc, 0);
	if (!exp_func_struct) {
		LM_ERR("function '%s' called, but not available.", func);
		*retval = -1;
		if (argv[0]) pkg_free(argv[0]);
		if (argv[1]) pkg_free(argv[1]);
		return -1;
	}

	elems[0].type = CMD_ST;
	elems[0].u.data = exp_func_struct;
	elems[1].type = STRING_ST;
	elems[1].u.data = argv[0];
	elems[2].type = STRING_ST;
	elems[2].u.data = argv[1];
	act = mk_action(	MODULE_T,
				3,
				elems,
				0);


	if (!act) {
		LM_ERR("action structure could not be created. Error.");
		if (argv[0]) pkg_free(argv[0]);
		if (argv[1]) pkg_free(argv[1]);
		return -1;
	}


	if (exp_func_struct->fixup) {
		if (!unsafemodfnc) {
			LM_ERR("Module function '%s' is unsafe. Call is refused.\n", func);
			if (argv[0]) pkg_free(argv[0]);
			if (argv[1]) pkg_free(argv[1]);
			*retval = -1;
			return -1;
		}

		if (argc>=2) {
			*retval = exp_func_struct->fixup(&(act->elem[2].u.data), 2);
			if (*retval < 0) {
				LM_ERR("Error in fixup (2)\n");
				return -1;
			}
			act->elem[2].type = MODFIXUP_ST;
		}
		if (argc>=1) {
			*retval = exp_func_struct->fixup(&(act->elem[1].u.data), 1);
			if (*retval < 0) {
				LM_ERR("Error in fixup (1)\n");
				return -1;
			}
			act->elem[1].type = MODFIXUP_ST;
		}
		if (argc==0) {
			*retval = exp_func_struct->fixup(0, 0);
			if (*retval < 0) {
				LM_ERR("Error in fixup (0)\n");
				return -1;
			}
		}
	}

	*retval = do_action(act, m);

	if ((act->elem[2].type == MODFIXUP_ST) && (act->elem[2].u.data)) {
		/* pkg_free(act->elem[2].u.data); */
		LM_WARN("moduleFunction: A fixup function was called. "
				"This currently creates a memory leak.\n");
	}

	if ((act->elem[1].type == MODFIXUP_ST) && (act->elem[1].u.data)) {
		/* pkg_free(act->elem[1].u.data); */
		LM_WARN("moduleFunction: A fixup function was called. "
				"This currently creates a memory leak.\n");
	}

	if (argv[0]) pkg_free(argv[0]);
	if (argv[1]) pkg_free(argv[1]);

	pkg_free(act);
	
	return 1;
}
Ejemplo n.º 4
0
static PyObject *
msg_call_function(msgobject *self, PyObject *args)
{
/* TODO: adapt to new cmd_export and params interface */
#if 0
    int i, rval;
    char *fname, *arg1, *arg2;
    cmd_export_t *fexport;
    struct action *act;
    action_elem_t elems[MAX_ACTION_ELEMS];

    if (self->msg == NULL) {
        PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
        Py_INCREF(Py_None);
        return Py_None;
    }

    i = PySequence_Size(args);
    if (i < 1 || i > 3) {
        PyErr_SetString(PyExc_RuntimeError, "call_function() should " \
          "have from 1 to 3 arguments");
        Py_INCREF(Py_None);
        return Py_None;
    }

    if(!PyArg_ParseTuple(args, "s|ss:call_function", &fname, &arg1, &arg2))
        return NULL;

    fexport = find_cmd_export_t(fname, 0);
    if (fexport == NULL) {
        PyErr_SetString(PyExc_RuntimeError, "no such function");
        Py_INCREF(Py_None);
        return Py_None;
    }

    elems[0].type = CMD_ST;
    elems[0].u.data = fexport;
    elems[1].type = STRING_ST;
    elems[1].u.data = arg1;
    elems[2].type = STRING_ST;
    elems[2].u.data = arg2;
    act = mk_action(MODULE_T, 3, elems, 0, "python");

    if (act == NULL) {
        PyErr_SetString(PyExc_RuntimeError,
          "action structure could not be created");
        Py_INCREF(Py_None);
        return Py_None;
    }

    if (fexport->fixup != NULL) {
        if (i >= 3) {
            rval = fexport->fixup(&(act->elem[2].u.data), 2);
            if (rval < 0) {
                PyErr_SetString(PyExc_RuntimeError, "Error in fixup (2)");
                Py_INCREF(Py_None);
                return Py_None;
            }
            act->elem[2].type = MODFIXUP_ST;
        }
        if (i >= 2) {
            rval = fexport->fixup(&(act->elem[1].u.data), 1);
            if (rval < 0) {
                PyErr_SetString(PyExc_RuntimeError, "Error in fixup (1)");
                Py_INCREF(Py_None);
                return Py_None;
            }
            act->elem[1].type = MODFIXUP_ST;
        }
        if (i == 1) {
            rval = fexport->fixup(0, 0);
            if (rval < 0) {
                PyErr_SetString(PyExc_RuntimeError, "Error in fixup (0)");
                Py_INCREF(Py_None);
                return Py_None;
            }
        }
    }

    rval = do_action(act, self->msg);

    if ((act->elem[2].type == MODFIXUP_ST) && (act->elem[2].u.data) &&
      (act->elem[2].u.data != arg2)) {
       pkg_free(act->elem[2].u.data);
    }

    if ((act->elem[1].type == MODFIXUP_ST) && (act->elem[1].u.data)) {
        pkg_free(act->elem[1].u.data);
    }

    pkg_free(act);

    return PyInt_FromLong(rval);
#endif

    return NULL;
}