-
Notifications
You must be signed in to change notification settings - Fork 564
/
event_route.c
376 lines (324 loc) · 9.34 KB
/
event_route.c
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (C) 2012 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips 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.
*
* opensips 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.
*
*
* history:
* ---------
* 2012-12-xx created (razvancrainea)
*/
#include "../../sr_module.h"
#include "../../evi/evi_transport.h"
#include "../../evi/evi_modules.h"
#include "../../ut.h"
#include "event_route.h"
#include "route_send.h"
#include <string.h>
#include <fcntl.h>
#include <arpa/inet.h>
/* default PVAR names */
/**
* module functions
*/
static int mod_init(void);
static int child_init(int rank);
/**
* exported functions
*/
static evi_reply_sock* scriptroute_parse(str socket);
static int scriptroute_raise(struct sip_msg *msg, str* ev_name,
evi_reply_sock *sock, evi_params_t * params);
static int scriptroute_match(evi_reply_sock *sock1, evi_reply_sock *sock2);
static str scriptroute_print(evi_reply_sock *sock);
#define SR_SOCK_ROUTE(_s) ((int)(unsigned long)(_s->params))
/**
* * module process
* */
static proc_export_t procs[] = {
{0,0,0,0,0,0}
};
/**
* module exported functions
*/
static cmd_export_t cmds[]={
{0,0,{{0,0,0}},0}
};
/**
* module exports
*/
struct module_exports exports= {
"event_route", /* module name */
MOD_TYPE_DEFAULT,/* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
0, /* load function */
NULL, /* OpenSIPS module dependencies */
cmds, /* exported functions */
0, /* exported async functions */
0, /* exported parameters */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* exported transformations */
procs, /* extra processes */
0, /* module pre-initialization function */
mod_init, /* module initialization function */
0, /* response handling function */
0, /* destroy function */
child_init, /* per-child init function */
0 /* reload confirm function */
};
/**
* exported functions for core event interface
*/
static evi_export_t trans_export_scriptroute = {
SCRIPTROUTE_NAME_STR, /* transport module name */
scriptroute_raise, /* raise function */
scriptroute_parse, /* parse function */
scriptroute_match, /* sockets match function */
0, /* no free function */
scriptroute_print, /* socket print function */
SCRIPTROUTE_FLAG /* flags */
};
/**
* init module function
*/
static int mod_init(void)
{
LM_NOTICE("initializing module ...\n");
if (register_event_mod(&trans_export_scriptroute)) {
LM_ERR("cannot register transport functions for SCRIPTROUTE\n");
return -1;
}
return 0;
}
static int child_init(int rank)
{
char buffer[EV_SCRIPTROUTE_MAX_SOCK];
str sock_name;
str event_name;
int idx;
/*
* Only the first process registers the subscribers
*
* We do this in child init because here we are sure that all
* the events were subscribed
*/
if (rank != 1)
return 0;
/* init the socket buffer */
sock_name.s = buffer;
memcpy(buffer, SCRIPTROUTE_NAME, sizeof(SCRIPTROUTE_NAME) - 1);
buffer[sizeof(SCRIPTROUTE_NAME) - 1] = COLON_C;
/* subscribe the route events - idx starts at 1 */
for (idx = 1; sroutes->event[idx].a && sroutes->event[idx].name; idx++) {
/* build the socket */
event_name.s = sroutes->event[idx].name;
event_name.len = strlen(sroutes->event[idx].name);
/* first check if the event exists */
if (evi_get_id(&event_name) == EVI_ERROR) {
LM_ERR("Event %s not registered\n", event_name.s);
return -1;
}
LM_DBG("Registering event %s\n", sroutes->event[idx].name);
if (sizeof(SCRIPTROUTE_NAME)+event_name.len > EV_SCRIPTROUTE_MAX_SOCK) {
LM_ERR("socket name too big %d (max: %d)\n",
(int)(sizeof(SCRIPTROUTE_NAME) + event_name.len),
EV_SCRIPTROUTE_MAX_SOCK);
return -1;
}
memcpy(buffer + sizeof(SCRIPTROUTE_NAME), event_name.s, event_name.len);
sock_name.len = event_name.len + sizeof(SCRIPTROUTE_NAME);
/* register the subscriber - does not expire */
if (evi_event_subscribe(event_name, sock_name, 0, 0) < 0) {
LM_ERR("cannot subscribe to event %s\n", event_name.s);
return -1;
}
}
return 0;
}
/* returns 0 if sockets match */
static int scriptroute_match(evi_reply_sock *sock1, evi_reply_sock *sock2)
{
if (!sock1 || !sock2)
return 0;
if (!(sock1->flags & EVI_PARAMS) || !(sock2->flags & EVI_PARAMS) ||
SR_SOCK_ROUTE(sock1) != SR_SOCK_ROUTE(sock2))
return 0;
return 1;
}
static evi_reply_sock* scriptroute_parse(str socket)
{
evi_reply_sock *sock = NULL;
static char *dummy_buffer = 0, *name;
int idx;
if (!socket.len || !socket.s) {
LM_ERR("no socket specified\n");
return NULL;
}
/* try to normalize the route name */
name = pkg_realloc(dummy_buffer, socket.len + 1);
if (!name) {
LM_ERR("no more pkg memory\n");
return NULL;
}
memcpy(name, socket.s, socket.len);
name[socket.len] = '\0';
dummy_buffer = name;
/* try to "resolve" the name of the route */
idx = get_script_route_ID_by_name(name,sroutes->event,EVENT_RT_NO);
if (idx < 0) {
LM_ERR("cannot find route %s\n", name);
return NULL;
}
sock = shm_malloc(sizeof(evi_reply_sock) + socket.len + 1);
if (!sock) {
LM_ERR("no more memory for socket\n");
return NULL;
}
memset(sock, 0, sizeof(evi_reply_sock));
sock->address.s = (char *)(sock + 1);
memcpy(sock->address.s, name, socket.len+1);
sock->address.len = socket.len;
sock->params = (void *)(unsigned long)idx;
sock->flags |= EVI_PARAMS;
LM_DBG("route is <%.*s> idx %d\n", sock->address.len, sock->address.s, idx);
sock->flags |= EVI_ADDRESS;
sock->flags |= SCRIPTROUTE_FLAG;
return sock;
}
static str scriptroute_print(evi_reply_sock *sock)
{
/* return only the route's name */
return sock->address;
}
/* static parameters list retrieved by the fetch_event_params */
evi_params_t *parameters = NULL;
str *event_name = NULL; // mostly used for debugging
int event_route_param_get(struct sip_msg *msg, pv_param_t *ip,
pv_value_t *res, void *params, void *extra)
{
static str event_name_error = str_init("E_ERROR");
evi_params_t *parameters = (evi_params_t *)params;
str *event_name = (str *)extra;
evi_param_t *it;
pv_value_t tv;
int index;
if (!parameters)
{
LM_DBG("no parameter specified for this route\n");
return pv_get_null(msg, ip, res);
}
if (!event_name)
{
event_name = &event_name_error;
LM_WARN("invalid event's name, using %.*s\n", event_name->len, event_name->s);
}
if(ip->pvn.type==PV_NAME_INTSTR)
{
if (ip->pvn.u.isname.type != 0)
{
tv.rs = ip->pvn.u.isname.name.s;
tv.flags = PV_VAL_STR;
} else
{
tv.ri = ip->pvn.u.isname.name.n;
tv.flags = PV_VAL_INT|PV_TYPE_INT;
}
}
else
{
/* pvar -> it might be another $param variable! */
if(pv_get_spec_value(msg, (pv_spec_p)(ip->pvn.u.dname), &tv)!=0)
{
LM_ERR("cannot get spec value\n");
return -1;
}
if(tv.flags&PV_VAL_NULL || tv.flags&PV_VAL_EMPTY)
{
LM_ERR("null or empty name\n");
return -1;
}
}
it = parameters->first;
/* search for the param we want top add, based on index */
if (tv.flags & PV_VAL_INT) {
for (index = 1; it && index != tv.ri; it = it->next, index++);
if (!it) {
LM_WARN("Parameter %d not found for event %.*s - max %d\n",
tv.ri, event_name->len, event_name->s, index);
return pv_get_null(msg, ip, res);
}
} else {
/* search by name */
for (; it; it = it->next) {
if (it->name.s && it->name.len == tv.rs.len &&
memcmp(it->name.s, tv.rs.s, it->name.len) == 0)
break;
}
if (!it) {
LM_WARN("Parameter <%.*s> not found for event <%.*s>\n",
tv.rs.len, tv.rs.s,
event_name->len, event_name->s);
return pv_get_null(msg, ip, res);
}
}
/* parameter found - populate it */
if (it->flags & EVI_INT_VAL) {
res->rs.s = int2str(it->val.n, &res->rs.len);
res->ri = it->val.n;
res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
} else {
res->rs.s = it->val.s.s;
res->rs.len = it->val.s.len;
res->flags = PV_VAL_STR;
}
return 0;
}
void route_run(struct action* a, struct sip_msg* msg,
evi_params_t *params, str *event)
{
int old_route_type;
route_params_push_level(params, event, event_route_param_get);
swap_route_type(old_route_type, EVENT_ROUTE);
run_top_route(a, msg);
set_route_type(old_route_type);
route_params_pop_level();
}
static int scriptroute_raise(struct sip_msg *msg, str* ev_name,
evi_reply_sock *sock, evi_params_t *params)
{
route_send_t *buf = NULL;
if (!sock || !(sock->flags & EVI_PARAMS)) {
LM_ERR("no socket found\n");
return -1;
}
/* check the socket type */
if (!(sock->flags & SCRIPTROUTE_FLAG)) {
LM_ERR("invalid socket type\n");
return -1;
}
if (route_build_buffer(ev_name, sock, params, &buf) < 0) {
LM_ERR("failed to serialize event route triggering\n");
return -1;
}
buf->ev_route_id = SR_SOCK_ROUTE(sock);
if (route_send(buf) < 0)
return -1;
return 0;
}