-
Notifications
You must be signed in to change notification settings - Fork 3
/
c10rpc.xml
573 lines (555 loc) · 15.8 KB
/
c10rpc.xml
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
<chapter id="c10rpcmi">
<title>RPC and MI Control Interfaces</title>
<para>
Control interfaces are channels to communicate with &kamailio; SIP server for
administrative purposes. At this moment are two control interfaces:
</para>
<itemizedlist>
<listitem>
<para>
MI - management interface - it is no longer the recommended control
interface and it is planned to be obsoleted in the near future
</para>
</listitem>
<listitem>
<para>
RPC - remote procedure call - a more standardized option to
execute commands. It is the recommended control interface.
</para>
</listitem>
</itemizedlist>
<section id="c10rpc">
<title>RPC Control Interface</title>
<para>
RPC is designed as scanner-printer communication channel - RPC commands will
scan the input for parameters as needed and will print back.
</para>
<itemizedlist>
<listitem>
<para>
fifo - (<ulink url="http://kamailio.org/docs/modules/3.2.x/modules/ctl.html">ctl module</ulink>)
- the communication is done via FIFO file using a
simple text-based, line oriented protocol.
</para>
</listitem>
<listitem>
<para>
datagram - (<ulink url="http://kamailio.org/docs/modules/3.2.x/modules/ctl.html">ctl module</ulink>)
- the communication is done via unix socket files
or UDP sockets.
</para>
</listitem>
<listitem>
<para>
tcp - (<ulink url="http://kamailio.org/docs/modules/3.2.x/modules/ctl.html">ctl module</ulink>)
- the communication is done via unix socket files
or TCP sockets.
</para>
</listitem>
<listitem>
<para>
xmlrpc - (<ulink url="http://kamailio.org/docs/modules/3.2.x/modules/xmlrpc.html">xmlrpc module</ulink>)
- the communication is done via XMLRPC
</para>
</listitem>
</itemizedlist>
<para>
RPC API is very well documented at:
<ulink url="http://www.kamailio.org/docs/docbooks/3.2.x/rpc_api/rpc_api.html">http://www.kamailio.org/docs/docbooks/3.2.x/rpc_api/rpc_api.html</ulink>.
We will show next just an example of implementing a RPC command:
<emphasis>pkg.stats</emphasis> - dump usage statistics of PKG (private) memory,
implemented in modules_k/kex.
</para>
<example id="c10rpc_command_code">
<title>Example of RPC command - pkg.stats</title>
<programlisting format="linespecific">
<![CDATA[
...
/**
*
*/
static const char* rpc_pkg_stats_doc[2] = {
"Private memory (pkg) statistics per process",
0
};
/**
*
*/
int pkg_proc_get_pid_index(unsigned int pid)
{
int i;
for(i=0; i<_pkg_proc_stats_no; i++)
{
if(_pkg_proc_stats_list[i].pid == pid)
return i;
}
return -1;
}
/**
*
*/
static void rpc_pkg_stats(rpc_t* rpc, void* ctx)
{
int i;
int limit;
int cval;
str cname;
void* th;
int mode;
if(_pkg_proc_stats_list==NULL)
{
rpc->fault(ctx, 500, "Not initialized");
return;
}
i = 0;
mode = 0;
cval = 0;
limit = _pkg_proc_stats_no;
if (rpc->scan(ctx, "*S", &cname) == 1)
{
if(cname.len==3 && strncmp(cname.s, "pid", 3)==0)
mode = 1;
else if(cname.len==4 && strncmp(cname.s, "rank", 4)==0)
mode = 2;
else if(cname.len==5 && strncmp(cname.s, "index", 5)==0)
mode = 3;
else {
rpc->fault(ctx, 500, "Invalid filter type");
return;
}
if (rpc->scan(ctx, "d", &cval) < 1)
{
rpc->fault(ctx, 500, "One more parameter expected");
return;
}
if(mode==1)
{
i = pkg_proc_get_pid_index((unsigned int)cval);
if(i<0)
{
rpc->fault(ctx, 500, "No such pid");
return;
}
limit = i + 1;
} else if(mode==3) {
i=cval;
limit = i + 1;
}
}
for(; i<limit; i++)
{
/* add entry node */
if(mode!=2 || _pkg_proc_stats_list[i].rank==cval)
{
if (rpc->add(ctx, "{", &th) < 0)
{
rpc->fault(ctx, 500, "Internal error creating rpc");
return;
}
if(rpc->struct_add(th, "dddddd",
"entry", i,
"pid", _pkg_proc_stats_list[i].pid,
"rank", _pkg_proc_stats_list[i].rank,
"used", _pkg_proc_stats_list[i].used,
"free", _pkg_proc_stats_list[i].available,
"real_used", _pkg_proc_stats_list[i].real_used
)<0)
{
rpc->fault(ctx, 500, "Internal error creating rpc");
return;
}
}
}
}
/**
*
*/
rpc_export_t kex_pkg_rpc[] = {
{"pkg.stats", rpc_pkg_stats, rpc_pkg_stats_doc, 0},
{0, 0, 0, 0}
};
/**
*
*/
int pkg_proc_stats_init_rpc(void)
{
if (rpc_register_array(kex_pkg_rpc)!=0)
{
LM_ERR("failed to register RPC commands\n");
return -1;
}
return 0;
}
...
]]>
</programlisting>
</example>
<para>
To add new RPC commands to control interface, you have to register them. One
option, which is used here, is to build an array with the new commands and
the help messages for each one and the use rpc_register_array(...) function.
You have the register the commands in mod_init() function - in our example
it is done via pkg_proc_stats_init(), which is a wrapper function called from
mod_init().
</para>
<para>
pkg.stats commands has optional parameters, which can be used to specify the pid,
internal rank or position in internal process table (index) of the application
process for which to dump the private memory statistics. If there is no parameter
given, the the statistics for all processes will be printed.
</para>
<para>
The command itself is implemented in C function rpc_pkg_stats(...). In the first
part, it reads the parameters. You can see there the search for optional
parameter is done by using '*' in the front of the type of parameter (str):
</para>
<programlisting format="linespecific">
rpc->scan(ctx, "*S", &cname)
</programlisting>
<para>
If this parameter is found, then there has to be another one, which this time
is no longer optional.
</para>
<programlisting format="linespecific">
rpc->scan(ctx, "d", &cval)
</programlisting>
<para>
Once input is read, follows the printing of the result in RPC structures. When
kex module is loaded, one can use pkg.stats command via sercmd tool like:
</para>
<programlisting format="linespecific">
sercmd pkg.stats
sercmd pkg.stats index 2
sercmd pkg.stats rank 4
sercmd pkg.stats pid 8492
</programlisting>
</section>
<section id="c10mi">
<title>MI - Management Interface</title>
<para>
The <emphasis role="strong">Management Interface</emphasis> is an abstract layer
introduced to allow interaction between &kamailio; and external applications like
shell terminal or web applications. It is an old alternative to RPC control interface,
which is going to be obsoleted in the future.
</para>
<para>
MI is built on a tree architecture - the input is parsed completely and stored as
a tree in memory. The commands get access to the tree and build another tree with
the response, which is then printed back to the transport layer.
</para>
<para>
In the past, there were two ways to interact
with such applications: via <emphasis role="strong">FIFO</emphasis> file
and via <emphasis role="strong">unix sockets</emphasis>.
</para>
<para>
<emphasis role="strong">MI</emphasis> came and introduced an abstractization between
the transport and application levels. All MI commands are available for all
available transports. At this moment the following transports are available:
</para>
<itemizedlist>
<listitem>
<para>
fifo - (mi_fifo mofule) - the communication is done via FIFO file using a
simple text-based, line oriented protocol.
</para>
</listitem>
<listitem>
<para>
xmlrpc - (mi_xmlrpc) - the communication is done via XMLRPC
</para>
</listitem>
<listitem>
<para>
datagram - (mi_datagram) - the communication is done via unix socket files
or UDP sockets.
</para>
</listitem>
</itemizedlist>
<para>
To implement a new command for <emphasis role="strong">MI</emphasis> you don't need
to interact with the transports. <emphasis role="strong">MI</emphasis> will pass to
your function a compiled version of the command, in the form of a tree. The functions
walks through the tree, finds the input parameters, executes the actions accordingly,
and returns a new <emphasis role="strong">MI</emphasis> tree with the response to
the command.
</para>
<section id="c10mi_command_function">
<title>MI Command Function</title>
<para>
The function you have to implement for a new <emphasis role="strong">MI</emphasis>
command has a simple prototype.
</para>
<programlisting format="linespecific">
...
struct mi_root my_my_function(struct mi_root *tree, void *param);
...
</programlisting>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
tree - the tree with the input parameters sent via the transport layer.
The <emphasis role="strong">struct mi_root</emphasis> is defined in
<emphasis role="strong">mi/three.h</emphasis>.
</para>
<programlisting format="linespecific">
...
struct mi_node {
str value; /* value of node (parameter) */
str name; /* name of node (parameter) */
struct mi_node *kids; /* children nodes */
struct mi_node *next; /* sibling nodes */
struct mi_node *last; /* last node */
struct mi_attr *attributes; /* the attributes of the node */
};
struct mi_root {
unsigned int code; /* return code of the command */
str reason; /* reason code */
struct mi_handler *async_hdl; /* handler function for asynchronous replying */
struct mi_node node; /* head of the tree with parameters (nodes) */
};
...
</programlisting>
</listitem>
<listitem>
<para>
param - parameter given when registering the command to
<emphasis role="strong">MI</emphasis>.
</para>
</listitem>
</itemizedlist>
<para>
Returns a tree containing the response to be send back for that command or NULL in
case of error.
</para>
</section>
<section id="c10register_command">
<title>Register MI Command</title>
<para>
It is recommended to register new <emphasis role="strong">MI</emphasis> commands via
&kamailio; module interface - described later, in the chapted about
<emphasis role="strong">module development</emphasis>.
</para>
<para>
The alternative is to use <emphasis role="strong">register_mi_cmd(...)</emphasis>,
defined in file <emphasis role="strong">mi/mi.h</emphasis>.
</para>
<programlisting format="linespecific">
...
typedef struct mi_root* (mi_cmd_f)(struct mi_root*, void *param);
typedef int (mi_child_init_f)(void);
int register_mi_cmd(mi_cmd_f f, char *name, void *param,
mi_child_init_f in, unsigned int flags);
...
</programlisting>
<para>
Parameters:
</para>
<itemizedlist>
<listitem>
<para>
f - function to be called when the command is received from the transport layer
</para>
</listitem>
<listitem>
<para>
name - the name of the command
</para>
</listitem>
<listitem>
<para>
param - parameter to be given when function is executed
</para>
</listitem>
<listitem>
<para>
in - function to be executed at &kamailio; initialization time
</para>
</listitem>
<listitem>
<para>
flags - set of flags describing properties of the command
</para>
<programlisting format="linespecific">
...
#define MI_ASYNC_RPL_FLAG (1<<0) // - the reply to command is asynchronous
#define MI_NO_INPUT_FLAG (1<<1) // - the command does not get any input parameters
...
</programlisting>
</listitem>
</itemizedlist>
<para>
Returns 0 if the MI command was successfully registered, <0 in case of error.
</para>
</section>
<section id="c10example">
<title>Example of MI Command Function</title>
<para>
We look at a MI command exported by <emphasis role="strong">dispatcher</emphasis>
module. The command allow to set the state (active/inactive) for a destination
address in the dispatching list (read documentation for
<emphasis role="strong">dispatcher</emphasis> module).
</para>
<para>
The command expects 3 parameters:
</para>
<itemizedlist>
<listitem>
<para>
state - the state to set to the destination address
</para>
</listitem>
<listitem>
<para>
group - the id of the group (destination set) the destination belongs to
</para>
</listitem>
<listitem>
<para>
address - the address of the destination to change the state for it
</para>
</listitem>
</itemizedlist>
<para>
The function expects to find in the input tree, one flag (state), one number (group)
and a string (the address). If not, an reply containing error message is sent back.
If the parameters are ok and the destination is found, the state is changed accordingly
and successful code and message is sent back, in not, an appropriate error code and
message is sent back.
</para>
<para>
Code is shown below (in the sources look in file
<emphasis role="strong">modules/dispatcher/dispatcher.c</emphasis>).
</para>
<programlisting format="linespecific">
<![CDATA[
...
static struct mi_root* ds_mi_set(struct mi_root* cmd_tree, void* param)
{
str sp;
int ret;
unsigned int group, state;
struct mi_node* node;
node = cmd_tree->node.kids;
if(node == NULL)
return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
sp = node->value;
if(sp.len<=0 || !sp.s)
{
LM_ERR("bad state value\n");
return init_mi_tree(500, "bad state value", 15);
}
state = 0;
if(sp.s[0]=='0' || sp.s[0]=='I' || sp.s[0]=='i') {
/* set inactive */
state |= DS_INACTIVE_DST;
if((sp.len>1) && (sp.s[1]=='P' || sp.s[1]=='p'))
state |= DS_PROBING_DST;
} else if(sp.s[0]=='1' || sp.s[0]=='A' || sp.s[0]=='a') {
/* set active */
if((sp.len>1) && (sp.s[1]=='P' || sp.s[1]=='p'))
state |= DS_PROBING_DST;
} else if(sp.s[0]=='2' || sp.s[0]=='D' || sp.s[0]=='d') {
/* set disabled */
state |= DS_DISABLED_DST;
} else if(sp.s[0]=='3' || sp.s[0]=='T' || sp.s[0]=='t') {
/* set trying */
state |= DS_TRYING_DST;
if((sp.len>1) && (sp.s[1]=='P' || sp.s[1]=='p'))
state |= DS_PROBING_DST;
} else {
LM_ERR("unknow state value\n");
return init_mi_tree(500, "unknown state value", 19);
}
node = node->next;
if(node == NULL)
return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
sp = node->value;
if(sp.s == NULL)
{
return init_mi_tree(500, "group not found", 15);
}
if(str2int(&sp, &group))
{
LM_ERR("bad group value\n");
return init_mi_tree( 500, "bad group value", 16);
}
node= node->next;
if(node == NULL)
return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
sp = node->value;
if(sp.s == NULL)
{
return init_mi_tree(500,"address not found", 18 );
}
ret = ds_reinit_state(group, &sp, state);
if(ret!=0)
{
return init_mi_tree(404, "destination not found", 21);
}
return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
}
...
]]>
</programlisting>
</section>
<section id="c10mi_fifo_command">
<title>MI FIFO Command</title>
<para>
The structure of the command that has to be sent to transport layer depends
on the implementation. Check the documentation of the modules implementing the
MI transports.
</para>
<para>
For FIFO, the structure is line oriented, command being plain text.
</para>
<programlisting format="linespecific">
...
:_command_name_:_reply_fifo_file_
_parameters_
_empty_line_
...
</programlisting>
<para>
MI FIFO command structure:
</para>
<itemizedlist>
<listitem>
<para>
_command_name_ - the name of the command
</para>
</listitem>
<listitem>
<para>
_reply_fifo_file_ - the FIFO file where to write the reply message
</para>
</listitem>
<listitem>
<para>
_parameters_ - values for parameters, one per line
</para>
</listitem>
<listitem>
<para>
_empty_line_ - an empty line to mark the end of the command
</para>
</listitem>
</itemizedlist>
<para>
For the command described in the previous section, it can look like:
</para>
<programlisting format="linespecific">
...
:ds_set_state:kamailio_fifo_reply
i
2
sip:10.10.10.10:5080
\n
...
</programlisting>
</section>
</section>
</chapter>