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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/**
* Copyright (C) 2014 Daniel-Constantin Mierla (asipto.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 <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ev.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "../../pt.h"
#include "../../pvar.h"
#include "../../mem/shm_mem.h"
#include "../../mod_fix.h"
#include "../../pvar.h"
#include "../../cfg/cfg_struct.h"
#include "../../lib/kcore/faked_msg.h"
#include "../../modules/tm/tm_load.h"
#include "evapi_dispatch.h"
MODULE_VERSION
static int _evapi_workers = 1;
static char *_evapi_bind_addr = "127.0.0.1";
static int _evapi_bind_port = 8448;
static char *_evapi_bind_param = NULL;
static int _evapi_netstring_format_param = 1;
static tm_api_t tmb;
static int mod_init(void);
static int child_init(int);
static void mod_destroy(void);
static int w_evapi_relay(sip_msg_t* msg, char* evdata, char* p2);
static int w_evapi_async_relay(sip_msg_t* msg, char* evdata, char* p2);
static int w_evapi_close(sip_msg_t* msg, char* p1, char* p2);
static int fixup_evapi_relay(void** param, int param_no);
static cmd_export_t cmds[]={
{"evapi_relay", (cmd_function)w_evapi_relay, 1, fixup_evapi_relay,
0, ANY_ROUTE},
{"evapi_async_relay", (cmd_function)w_evapi_async_relay, 1, fixup_evapi_relay,
0, REQUEST_ROUTE},
{"evapi_close", (cmd_function)w_evapi_close, 1, NULL,
0, ANY_ROUTE},
{0, 0, 0, 0, 0, 0}
};
static param_export_t params[]={
{"workers", INT_PARAM, &_evapi_workers},
{"bind_addr", PARAM_STRING, &_evapi_bind_param},
{"netstring_format", INT_PARAM, &_evapi_netstring_format_param},
{0, 0, 0}
};
static pv_export_t mod_pvs[] = {
{ {"evapi", (sizeof("evapi")-1)}, PVT_OTHER, pv_get_evapi,
pv_set_evapi, pv_parse_evapi_name, 0, 0, 0},
{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
};
struct module_exports exports = {
"evapi",
DEFAULT_DLFLAGS, /* dlopen flags */
cmds,
params,
0,
0, /* exported MI functions */
mod_pvs, /* exported pseudo-variables */
0, /* extra processes */
mod_init, /* module initialization function */
0, /* response function */
mod_destroy, /* destroy function */
child_init /* per child init function */
};
/**
* init module function
*/
static int mod_init(void)
{
char *p;
/* init faked sip msg */
if(faked_msg_init()<0) {
LM_ERR("failed to init faked sip msg\n");
return -1;
}
if(load_tm_api( &tmb ) < 0) {
LM_INFO("cannot load the TM-functions - async relay disabled\n");
memset(&tmb, 0, sizeof(tm_api_t));
}
if(_evapi_bind_param!=NULL) {
p = strchr(_evapi_bind_param, ':');
if(p!=NULL) {
*p++ = '\0';
_evapi_bind_port = (short)atoi(p);
if (_evapi_bind_port <= 0) {
LM_ERR("invalid port: %d\n", _evapi_bind_port);
return -1;
}
}
_evapi_bind_addr = _evapi_bind_param;
}
/* add space for one extra process */
register_procs(1 + _evapi_workers);
/* add child to update local config framework structures */
cfg_register_child(1 + _evapi_workers);
evapi_init_environment(_evapi_netstring_format_param);
return 0;
}
/**
* @brief Initialize async module children
*/
static int child_init(int rank)
{
int pid;
int i;
if (rank==PROC_INIT) {
if(evapi_init_notify_sockets()<0) {
LM_ERR("failed to initialize notify sockets\n");
return -1;
}
return 0;
}
if (rank!=PROC_MAIN) {
evapi_close_notify_sockets_parent();
return 0;
}
pid=fork_process(PROC_NOCHLDINIT, "EvAPI Dispatcher", 1);
if (pid<0)
return -1; /* error */
if(pid==0) {
/* child */
/* initialize the config framework */
if (cfg_child_init())
return -1;
/* main function for dispatcher */
evapi_close_notify_sockets_child();
if(evapi_run_dispatcher(_evapi_bind_addr, _evapi_bind_port)<0) {
LM_ERR("failed to initialize disptacher process\n");
return -1;
}
}
for(i=0; i<_evapi_workers; i++) {
pid=fork_process(PROC_RPC, "EvAPI Worker", 1);
if (pid<0)
return -1; /* error */
if(pid==0) {
/* child */
/* initialize the config framework */
if (cfg_child_init())
return -1;
/* main function for workers */
if(evapi_run_worker(i+1)<0) {
LM_ERR("failed to initialize worker process: %d\n", i);
return -1;
}
}
}
return 0;
}
/**
* destroy module function
*/
static void mod_destroy(void)
{
}
/**
*
*/
static int w_evapi_relay(sip_msg_t *msg, char *evdata, char *p2)
{
str sdata;
if(evdata==0) {
LM_ERR("invalid parameters\n");
return -1;
}
if(fixup_get_svalue(msg, (gparam_t*)evdata, &sdata)!=0) {
LM_ERR("unable to get data\n");
return -1;
}
if(sdata.s==NULL || sdata.len == 0) {
LM_ERR("invalid data parameter\n");
return -1;
}
if(evapi_relay(&sdata)<0) {
LM_ERR("failed to relay event: %.*s\n", sdata.len, sdata.s);
return -1;
}
return 1;
}
/**
*
*/
static int w_evapi_async_relay(sip_msg_t *msg, char *evdata, char *p2)
{
str sdata;
unsigned int tindex;
unsigned int tlabel;
tm_cell_t *t = 0;
if(evdata==0) {
LM_ERR("invalid parameters\n");
return -1;
}
if(tmb.t_suspend==NULL) {
LM_ERR("evapi async relay is disabled - tm module not loaded\n");
return -1;
}
t = tmb.t_gett();
if (t==NULL || t==T_UNDEFINED)
{
if(tmb.t_newtran(msg)<0)
{
LM_ERR("cannot create the transaction\n");
return -1;
}
t = tmb.t_gett();
if (t==NULL || t==T_UNDEFINED)
{
LM_ERR("cannot lookup the transaction\n");
return -1;
}
}
if(tmb.t_suspend(msg, &tindex, &tlabel)<0)
{
LM_ERR("failed to suppend request processing\n");
return -1;
}
LM_DBG("transaction suspended [%u:%u]\n", tindex, tlabel);
if(fixup_get_svalue(msg, (gparam_t*)evdata, &sdata)!=0) {
LM_ERR("unable to get data\n");
return -1;
}
if(sdata.s==NULL || sdata.len == 0) {
LM_ERR("invalid data parameter\n");
return -1;
}
if(evapi_relay(&sdata)<0) {
LM_ERR("failed to relay event: %.*s\n", sdata.len, sdata.s);
return -2;
}
return 1;
}
/**
*
*/
static int fixup_evapi_relay(void** param, int param_no)
{
return fixup_spve_null(param, param_no);
}
/**
*
*/
static int w_evapi_close(sip_msg_t* msg, char* p1, char* p2)
{
int ret;
ret = evapi_cfg_close(msg);
if(ret>=0)
return ret+1;
return ret;
}
|