/
rtimer_mod.c
368 lines (334 loc) · 7.82 KB
/
rtimer_mod.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
/**
* $Id$
*
* Copyright (C) 2009 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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 <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <fcntl.h>
#include "../../sr_module.h"
#include "../../timer.h"
#include "../../dprint.h"
#include "../../route.h"
#include "../../receive.h"
#include "../../action.h"
#include "../../socket_info.h"
#include "../../dset.h"
#include "../../pt.h"
#include "../../usr_avp.h"
#include "../../xavp.h"
#include "../../timer_proc.h"
#include "../../script_cb.h"
#include "../../parser/parse_param.h"
#include "../../lib/kcore/faked_msg.h"
MODULE_VERSION
typedef struct _stm_route {
str timer;
unsigned int route;
struct _stm_route *next;
} stm_route_t;
typedef struct _stm_timer {
str name;
unsigned int mode;
unsigned int flags;
unsigned int interval;
stm_route_t *rt;
struct _stm_timer *next;
} stm_timer_t;
#define RTIMER_INTERVAL_USEC (1<<0)
stm_timer_t *_stm_list = NULL;
/** module functions */
static int mod_init(void);
static int child_init(int);
int stm_t_param(modparam_t type, void* val);
int stm_e_param(modparam_t type, void* val);
void stm_timer_exec(unsigned int ticks, void *param);
static param_export_t params[]={
{"timer", PARAM_STRING|USE_FUNC_PARAM, (void*)stm_t_param},
{"exec", PARAM_STRING|USE_FUNC_PARAM, (void*)stm_e_param},
{0,0,0}
};
/** module exports */
struct module_exports exports= {
"rtimer",
DEFAULT_DLFLAGS, /* dlopen flags */
0,
params,
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* extra processes */
mod_init, /* module initialization function */
0,
0,
child_init /* per-child init function */
};
/**
* init module function
*/
static int mod_init(void)
{
stm_timer_t *it;
if(_stm_list==NULL)
return 0;
/* init faked sip msg */
if(faked_msg_init()<0)
{
LM_ERR("failed to init timer local sip msg\n");
return -1;
}
/* register timers */
it = _stm_list;
while(it)
{
if(it->mode==0)
{
if(register_timer(stm_timer_exec, (void*)it, it->interval)<0)
{
LM_ERR("failed to register timer function\n");
return -1;
}
} else {
register_basic_timers(it->mode);
}
it = it->next;
}
return 0;
}
static int child_init(int rank)
{
stm_timer_t *it;
int i;
char si_desc[MAX_PT_DESC];
if(_stm_list==NULL)
return 0;
if (rank!=PROC_MAIN)
return 0;
it = _stm_list;
while(it)
{
for(i=0; i<it->mode; i++)
{
snprintf(si_desc, MAX_PT_DESC, "RTIMER EXEC child=%d timer=%.*s",
i, it->name.len, it->name.s);
if(it->flags & RTIMER_INTERVAL_USEC)
{
if(fork_basic_utimer(PROC_TIMER, si_desc, 1 /*socks flag*/,
stm_timer_exec, (void*)it, it->interval
/*usec*/)<0) {
LM_ERR("failed to start utimer routine as process\n");
return -1; /* error */
}
} else {
if(fork_basic_timer(PROC_TIMER, si_desc, 1 /*socks flag*/,
stm_timer_exec, (void*)it, it->interval
/*sec*/)<0) {
LM_ERR("failed to start timer routine as process\n");
return -1; /* error */
}
}
}
it = it->next;
}
return 0;
}
void stm_timer_exec(unsigned int ticks, void *param)
{
stm_timer_t *it;
stm_route_t *rt;
sip_msg_t *fmsg;
if(param==NULL)
return;
it = (stm_timer_t*)param;
if(it->rt==NULL)
return;
for(rt=it->rt; rt; rt=rt->next)
{
fmsg = faked_msg_next();
if (exec_pre_script_cb(fmsg, REQUEST_CB_TYPE)==0 )
continue; /* drop the request */
set_route_type(REQUEST_ROUTE);
run_top_route(main_rt.rlist[rt->route], fmsg, 0);
exec_post_script_cb(fmsg, REQUEST_CB_TYPE);
reset_avps();
#ifdef WITH_XAVP
xavp_reset_list();
#endif
}
}
int stm_t_param(modparam_t type, void *val)
{
param_t* params_list = NULL;
param_hooks_t phooks;
param_t *pit=NULL;
stm_timer_t tmp;
stm_timer_t *nt;
str s;
if(val==NULL)
return -1;
s.s = (char*)val;
s.len = strlen(s.s);
if(s.s[s.len-1]==';')
s.len--;
if (parse_params(&s, CLASS_ANY, &phooks, ¶ms_list)<0)
return -1;
memset(&tmp, 0, sizeof(stm_timer_t));
for (pit = params_list; pit; pit=pit->next)
{
if (pit->name.len==4
&& strncasecmp(pit->name.s, "name", 4)==0) {
tmp.name = pit->body;
} else if(pit->name.len==4
&& strncasecmp(pit->name.s, "mode", 4)==0) {
if(tmp.mode==0) {
if (str2int(&pit->body, &tmp.mode) < 0) {
LM_ERR("invalid mode: %.*s\n", pit->body.len, pit->body.s);
return -1;
}
}
} else if(pit->name.len==8
&& strncasecmp(pit->name.s, "interval", 8)==0) {
if(pit->body.s[pit->body.len-1]=='u'
|| pit->body.s[pit->body.len-1]=='U') {
pit->body.len--;
tmp.flags |= RTIMER_INTERVAL_USEC;
if (tmp.mode==0) {
tmp.mode = 1;
}
}
if (str2int(&pit->body, &tmp.interval) < 0) {
LM_ERR("invalid interval: %.*s\n", pit->body.len, pit->body.s);
return -1;
}
}
}
if(tmp.name.s==NULL)
{
LM_ERR("invalid timer name\n");
free_params(params_list);
return -1;
}
/* check for same timer */
nt = _stm_list;
while(nt) {
if(nt->name.len==tmp.name.len
&& strncasecmp(nt->name.s, tmp.name.s, tmp.name.len)==0)
break;
nt = nt->next;
}
if(nt!=NULL)
{
LM_ERR("duplicate timer with same name: %.*s\n",
tmp.name.len, tmp.name.s);
free_params(params_list);
return -1;
}
if(tmp.interval==0)
tmp.interval = 120;
nt = (stm_timer_t*)pkg_malloc(sizeof(stm_timer_t));
if(nt==0)
{
LM_ERR("no more pkg memory\n");
free_params(params_list);
return -1;
}
memcpy(nt, &tmp, sizeof(stm_timer_t));
nt->next = _stm_list;
_stm_list = nt;
free_params(params_list);
return 0;
}
int stm_e_param(modparam_t type, void *val)
{
param_t* params_list = NULL;
param_hooks_t phooks;
param_t *pit=NULL;
stm_route_t tmp;
stm_route_t *rt;
stm_timer_t *nt;
str s;
char c;
if(val==NULL)
return -1;
s.s = (char*)val;
s.len = strlen(s.s);
if(s.s[s.len-1]==';')
s.len--;
if (parse_params(&s, CLASS_ANY, &phooks, ¶ms_list)<0)
return -1;
memset(&tmp, 0, sizeof(stm_route_t));
for (pit = params_list; pit; pit=pit->next)
{
if (pit->name.len==5
&& strncasecmp(pit->name.s, "timer", 5)==0) {
tmp.timer = pit->body;
} else if(pit->name.len==5
&& strncasecmp(pit->name.s, "route", 5)==0) {
s = pit->body;
}
}
if(tmp.timer.s==NULL)
{
LM_ERR("invalid timer name\n");
free_params(params_list);
return -1;
}
/* get the timer */
nt = _stm_list;
while(nt) {
if(nt->name.len==tmp.timer.len
&& strncasecmp(nt->name.s, tmp.timer.s, tmp.timer.len)==0)
break;
nt = nt->next;
}
if(nt==NULL)
{
LM_ERR("timer not found - name: %.*s\n",
tmp.timer.len, tmp.timer.s);
free_params(params_list);
return -1;
}
c = s.s[s.len];
s.s[s.len] = '\0';
tmp.route = route_get(&main_rt, s.s);
s.s[s.len] = c;
if(tmp.route == -1)
{
LM_ERR("invalid route: %.*s\n",
s.len, s.s);
free_params(params_list);
return -1;
}
rt = (stm_route_t*)pkg_malloc(sizeof(stm_route_t));
if(rt==0)
{
LM_ERR("no more pkg memory\n");
free_params(params_list);
return -1;
}
memcpy(rt, &tmp, sizeof(stm_route_t));
rt->next = nt->rt;
nt->rt = rt;
free_params(params_list);
return 0;
}