1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/**
* $Id$
*
* Copyright (C) 2011 Flowroute LLC (flowroute.com)
*
* This file is part of Kamailio, a free SIP server.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "../../mod_fix.h"
#include "../../pvar.h"
#include "../../lvalue.h"
#include "../tm/tm_load.h"
#include "jsonrpc_request.h"
#include "jsonrpc_io.h"
struct tm_binds tmb;
static char *shm_strdup(str *src);
int memory_error() {
LM_ERR("Out of memory!");
return -1;
}
int jsonrpc_request(struct sip_msg* _m, char* _method, char* _params, char* _cb_route, char* _err_route, char* _cb_pv)
{
str method;
str params;
str cb_route;
str err_route;
if (fixup_get_svalue(_m, (gparam_p)_method, &method) != 0) {
LM_ERR("cannot get method value\n");
return -1;
}
if (fixup_get_svalue(_m, (gparam_p)_params, ¶ms) != 0) {
LM_ERR("cannot get params value\n");
return -1;
}
if (fixup_get_svalue(_m, (gparam_p)_cb_route, &cb_route) != 0) {
LM_ERR("cannot get cb_route value\n");
return -1;
}
if (fixup_get_svalue(_m, (gparam_p)_err_route, &err_route) != 0) {
LM_ERR("cannot get err_route value\n");
return -1;
}
tm_cell_t *t = 0;
t = tmb.t_gett();
if (t==NULL || t==T_UNDEFINED)
{
if(tmb.t_newtran(_m)<0)
{
LM_ERR("cannot create the transaction\n");
return -1;
}
t = tmb.t_gett();
if (t==NULL || t==T_UNDEFINED)
{
LM_ERR("cannot look up the transaction\n");
return -1;
}
}
unsigned int hash_index;
unsigned int label;
if (tmb.t_suspend(_m, &hash_index, &label) < 0) {
LM_ERR("t_suspend() failed\n");
return -1;
}
struct jsonrpc_pipe_cmd *cmd;
if (!(cmd = (struct jsonrpc_pipe_cmd *) shm_malloc(sizeof(struct jsonrpc_pipe_cmd))))
return memory_error();
memset(cmd, 0, sizeof(struct jsonrpc_pipe_cmd));
pv_spec_t *cb_pv = (pv_spec_t*)shm_malloc(sizeof(pv_spec_t));
if (!cb_pv)
return memory_error();
cb_pv = memcpy(cb_pv, (pv_spec_t *)_cb_pv, sizeof(pv_spec_t));
cmd->method = shm_strdup(&method);
cmd->params = shm_strdup(¶ms);
cmd->cb_route = shm_strdup(&cb_route);
cmd->err_route = shm_strdup(&err_route);
cmd->cb_pv = cb_pv;
cmd->msg = _m;
cmd->t_hash = hash_index;
cmd->t_label = label;
if (write(cmd_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
LM_ERR("failed to write to io pipe: %s\n", strerror(errno));
return -1;
}
return 0;
}
int jsonrpc_notification(struct sip_msg* _m, char* _method, char* _params)
{
str method;
str params;
if (fixup_get_svalue(_m, (gparam_p)_method, &method) != 0) {
LM_ERR("cannot get method value\n");
return -1;
}
if (fixup_get_svalue(_m, (gparam_p)_params, ¶ms) != 0) {
LM_ERR("cannot get params value\n");
return -1;
}
struct jsonrpc_pipe_cmd *cmd;
if (!(cmd = (struct jsonrpc_pipe_cmd *) shm_malloc(sizeof(struct jsonrpc_pipe_cmd))))
return memory_error();
memset(cmd, 0, sizeof(struct jsonrpc_pipe_cmd));
cmd->method = shm_strdup(&method);
cmd->params = shm_strdup(¶ms);
cmd->notify_only = 1;
if (write(cmd_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
LM_ERR("failed to write to io pipe: %s\n", strerror(errno));
return -1;
}
return 1;
}
static char *shm_strdup(str *src)
{
char *res;
if (!src || !src->s)
return NULL;
if (!(res = (char *) shm_malloc(src->len + 1)))
return NULL;
strncpy(res, src->s, src->len);
res[src->len] = 0;
return res;
}
|