Debian Codegraph @

src/modules/jsonrpcs/README

raw
1 JSONRPC-S (jsonrpc server) Module
2
3 Daniel-Constantin Mierla
4
5 <[email protected]>
6
7 Edited by
8
9 Daniel-Constantin Mierla
10
11 <[email protected]>
12
13 Copyright © 2014 asipto.com
14 __________________________________________________________________
15
16 Table of Contents
17
18 1. Admin Guide
19
20 1. Overview
21 2. Important Remarks
22 3. Dependencies
23
24 3.1. Kamailio Modules
25 3.2. External Libraries or Applications
26
27 4. Parameters
28
29 4.1. pretty_format (int)
30 4.2. transport (int)
31 4.3. fifo_name (str)
32 4.4. fifo_mode (int)
33 4.5. fifo_group (int or str)
34 4.6. fifo_user (int or str)
35 4.7. fifo_reply_dir (str)
36 4.8. dgram_socket (str)
37 4.9. dgram_workers (str)
38 4.10. dgram_mode (int)
39 4.11. dgram_group (int) dgram_group (str)
40 4.12. dgram_user (int) dgram_group (str)
41 4.13. dgram_timeout (int)
42 4.14. tcp_socket (str)
43
44 5. Functions
45
46 5.1. jsonrpc_dispatch()
47 5.2. jsonrpc_exec(cmd)
48
49 6. JSONRPC Commands
50 7. JSONRPC Transports
51
52 7.1. JSONRPC Over HTTP
53 7.2. JSONRPC Over FIFO
54 7.3. JSONRPC Over DATAGRAM
55
56 List of Examples
57
58 1.1. Set pretty_format parameter
59 1.2. Set transport parameter
60 1.3. Set fifo_name parameter
61 1.4. Set fifo_mode parameter
62 1.5. Set fifo_group parameter
63 1.6. Set fifo_user parameter
64 1.7. Set fifo_reply_dir parameter
65 1.8. Set dgram_socket parameter
66 1.9. Set dgram_workers parameter
67 1.10. Set dgram_mode parameter
68 1.11. Set dgram_group parameter
69 1.12. Set dgram_user parameter
70 1.13. Set dgram_timeout parameter
71 1.14. Set tcp_socket parameter
72 1.15. jsonrpc_dispatch usage
73 1.16. jsonrpc_exec usage
74 1.17. JSONRPC Commands - Examples
75 1.18. JSONRPC Over Fifo Command
76 1.19. JSONRPC Over Fifo Command From Terminal
77
78 Chapter 1. Admin Guide
79
80 Table of Contents
81
82 1. Overview
83 2. Important Remarks
84 3. Dependencies
85
86 3.1. Kamailio Modules
87 3.2. External Libraries or Applications
88
89 4. Parameters
90
91 4.1. pretty_format (int)
92 4.2. transport (int)
93 4.3. fifo_name (str)
94 4.4. fifo_mode (int)
95 4.5. fifo_group (int or str)
96 4.6. fifo_user (int or str)
97 4.7. fifo_reply_dir (str)
98 4.8. dgram_socket (str)
99 4.9. dgram_workers (str)
100 4.10. dgram_mode (int)
101 4.11. dgram_group (int) dgram_group (str)
102 4.12. dgram_user (int) dgram_group (str)
103 4.13. dgram_timeout (int)
104 4.14. tcp_socket (str)
105
106 5. Functions
107
108 5.1. jsonrpc_dispatch()
109 5.2. jsonrpc_exec(cmd)
110
111 6. JSONRPC Commands
112 7. JSONRPC Transports
113
114 7.1. JSONRPC Over HTTP
115 7.2. JSONRPC Over FIFO
116 7.3. JSONRPC Over DATAGRAM
117
118 1. Overview
119
120 This module provides a JSON-RPC v2 server, tailored for the needs of
121 Kamailio. It implements the Kamailio RPC interface over JSON-RPC.
122
123 The specification for JSON-RPC is available at:
124 http://www.jsonrpc.org/specification.
125
126 The transports supported by the module are: FIFO file, datagram (UDP)
127 over unix socket files or network sockets, HTTP and HTTPS.
128
129 The JSONRPCS module requires the xHTTP module to handle HTTP/S
130 requests. Read the documentation of the xHTTP module for more details.
131
132 2. Important Remarks
133
134 * This module implements the support for asynchronous RPC commands
135 only for HTTP and HTTPS transports.
136 * This module does not accept parameters embedded in a structure (see
137 the RPC documentation for more info about how parameters can be
138 passed to RPC).
139 * For the RPC interface implemented by Kamailio, the order of
140 parameters is important. If the parameters are given with names,
141 the names are ignored.
142
143 3. Dependencies
144
145 3.1. Kamailio Modules
146 3.2. External Libraries or Applications
147
148 3.1. Kamailio Modules
149
150 The following modules must be loaded before this module:
151 * xhttp - xHTTP (optional, required when http transport is wanted).
152
153 3.2. External Libraries or Applications
154
155 The following libraries or applications must be installed before
156 running Kamailio with this module loaded:
157 * None
158
159 4. Parameters
160
161 4.1. pretty_format (int)
162 4.2. transport (int)
163 4.3. fifo_name (str)
164 4.4. fifo_mode (int)
165 4.5. fifo_group (int or str)
166 4.6. fifo_user (int or str)
167 4.7. fifo_reply_dir (str)
168 4.8. dgram_socket (str)
169 4.9. dgram_workers (str)
170 4.10. dgram_mode (int)
171 4.11. dgram_group (int) dgram_group (str)
172 4.12. dgram_user (int) dgram_group (str)
173 4.13. dgram_timeout (int)
174 4.14. tcp_socket (str)
175
176 4.1. pretty_format (int)
177
178 Pretty format for JSON-RPC response document if set to 1. To disable,
179 set to 0.
180
181 Default value is '1' (on).
182
183 Example 1.1. Set pretty_format parameter
184 ...
185 modparam("jsonrpcs", "pretty_format", 0)
186 ...
187
188 4.2. transport (int)
189
190 Control what transports are enabled. The value can be:
191 * 0 - all transports that can be enabled. For http, the xhttp module
192 must be loaded. For FIFO, the fifo_name parameter must be set. For
193 DATAGRAM, the dgram_socket parameter must be set. For TCP, the
194 tcp_socket parameter must be set.
195 * 1 - only HTTP transport
196 * 2 - only FIFO transport
197 * 4 - only DATAGRAM transport
198 * 8 - only TCP transport
199
200 The value can be also a combination of specific transports. Make the
201 sum of the desired transports to enable them. For example, enabling
202 FIFO and DATAGRAM can be done setting transport=6.
203
204 Default value is '6' (fifo and datagram transport).
205
206 Example 1.2. Set transport parameter
207 ...
208 modparam("jsonrpcs", "transport", 1)
209 ...
210
211 4.3. fifo_name (str)
212
213 The name of the FIFO file to be created for listening and reading
214 external commands. If the given path is not absolute, the fifo file is
215 created relative to run_dir (global parameter).
216
217 Default value is "kamailio_rpc.fifo".
218
219 Example 1.3. Set fifo_name parameter
220 ...
221 modparam("jsonrpcs", "fifo_name", "/tmp/kamailio_jsonrpc.fifo")
222 ...
223
224 4.4. fifo_mode (int)
225
226 Permission to be used for creating the listening FIFO file. It follows
227 the UNIX conventions.
228
229 Default value is 0660 (rw-rw----).
230
231 Example 1.4. Set fifo_mode parameter
232 ...
233 modparam("jsonrpcs", "fifo_mode", 0600)
234 ...
235
236 4.5. fifo_group (int or str)
237
238 System Group to be used for creating the listening FIFO file.
239
240 Default value is the inherited one (process group).
241
242 Example 1.5. Set fifo_group parameter
243 ...
244 modparam("jsonrpcs", "fifo_group", 0)
245 modparam("jsonrpcs", "fifo_group", "root")
246 ...
247
248 4.6. fifo_user (int or str)
249
250 System User to be used for creating the listening FIFO file.
251
252 Default value is the inherited one (process user).
253
254 Example 1.6. Set fifo_user parameter
255 ...
256 modparam("jsonrpcs", "fifo_user", 0)
257 modparam("jsonrpcs", "fifo_user", "root")
258 ...
259
260 4.7. fifo_reply_dir (str)
261
262 Directory to be used for creating the reply FIFO files.
263
264 Default value is “/tmp/”
265
266 Example 1.7. Set fifo_reply_dir parameter
267 ...
268 modparam("jsonrpcs", "fifo_reply_dir", "/home/kamailio/tmp/")
269 ...
270
271 4.8. dgram_socket (str)
272
273 The name of a Unix socket file or an IP address. The Unix datagram or
274 UDP socket will be created using this parameter in order to read the
275 external commands. Both IPv4 and IPv6 are supported. If the given path
276 for Unix socket is not absolute, then it is created relative to run_dir
277 (global parameter).
278
279 Default value is "kamailio_rpc.sock".
280
281 Example 1.8. Set dgram_socket parameter
282 ...
283 modparam("jsonrpcs", "dgram_socket", "/tmp/kamailio_rpc.sock")
284 ...
285 modparam("jsonrpcs", "dgram_socket", "udp:1.2.3.4:8090")
286 ...
287
288 4.9. dgram_workers (str)
289
290 The number of worker processes to be created. Each worker process will
291 be a datagram server.
292
293 Default value is 1.
294
295 Example 1.9. Set dgram_workers parameter
296 ...
297 modparam("jsonrpcs", "dgram_workers", 4)
298 ...
299
300 4.10. dgram_mode (int)
301
302 Permission to be used for creating the listening UNIX datagram socket.
303 Not necessary for a UDP socket. It follows the UNIX conventions.
304
305 Default value is 0660 (rw-rw----).
306
307 Example 1.10. Set dgram_mode parameter
308 ...
309 modparam("jsonrpcs", "dgram_mode", 0600)
310 ...
311
312 4.11. dgram_group (int) dgram_group (str)
313
314 Group to be used for creating the listening UNIX socket.
315
316 Default value is the inherited one.
317
318 Example 1.11. Set dgram_group parameter
319 ...
320 modparam("jsonrpcs", "dgram_group", 0)
321 modparam("jsonrpcs", "dgram_group", "root")
322 ...
323
324 4.12. dgram_user (int) dgram_group (str)
325
326 User to be used for creating the listening UNIX socket.
327
328 Default value is the inherited one.
329
330 Example 1.12. Set dgram_user parameter
331 ...
332 modparam("jsonrpcs", "dgram_user", 0)
333 modparam("jsonrpcs", "dgram_user", "root")
334 ...
335
336 4.13. dgram_timeout (int)
337
338 The reply will expire after trying to send it for socket_timeout
339 milliseconds.
340
341 Default value is 2000.
342
343 Example 1.13. Set dgram_timeout parameter
344 ...
345 modparam("jsonrpcs", "dgram_timeout", 2000)
346 ...
347
348 4.14. tcp_socket (str)
349
350 The name of tcp socket to listen on for RPC commands. Only IPv4/IPv6
351 sockets are supported, unix file stream socket not yet.
352
353 Default value is "".
354
355 Example 1.14. Set tcp_socket parameter
356 ...
357 modparam("jsonrpcs", "tcp_socket", "tcp:1.2.3.4:5042")
358 ...
359
360 5. Functions
361
362 5.1. jsonrpc_dispatch()
363 5.2. jsonrpc_exec(cmd)
364
365 5.1. jsonrpc_dispatch()
366
367 Handle the JSONRPC request and generate a response.
368
369 Example 1.15. jsonrpc_dispatch usage
370 ...
371 #!KAMAILIO
372
373 memdbg=5
374 memlog=5
375
376 debug=3
377 log_stderror=yes
378
379 fork=yes
380 children=2
381
382 tcp_accept_no_cl=yes
383
384 loadmodule "sl.so"
385 loadmodule "pv.so"
386 loadmodule "xhttp.so"
387 loadmodule "jsonrpcs.so"
388
389 modparam("jsonrpcs", "transport", 1)
390
391 request_route {
392 send_reply("404", "not found");
393 exit;
394 }
395
396 event_route[xhttp:request] {
397 if(src_ip!=127.0.0.1) {
398 xhttp_reply("403", "Forbidden", "text/html",
399 "<html><body>Not allowed from $si</body></html>");
400 exit;
401 }
402 if ($hu =~ "^/RPC") {
403 jsonrpc_dispatch();
404 } else {
405 xhttp_reply("200", "OK", "text/html",
406 "<html><body>Wrong URL $hu</body></html>");
407 }
408 return;
409 }
410 ...
411
412 5.2. jsonrpc_exec(cmd)
413
414 Execute a JSON-RPC command given as a parameter.
415
416 The parameter has to be a valid full JSON-RPC document. It can be a
417 dynamic string with variables. The result of the command can be
418 accessed via $jsonrpl(key) pseudo variables.
419
420 Example 1.16. jsonrpc_exec usage
421 ...
422 jsonrpc_exec('{"jsonrpc": "2.0", "method": "dispatcher.reload", "id": 1}');
423 xlog("jsonrpc response code: $jsonrpl(code) - the body is: $jsonrpl(body)\n");
424 jsonrpc_exec('{"jsonrpc": "2.0", "method": "corex.debug", "params": [ 2 ], "id":
425 1}');
426 ...
427
428 6. JSONRPC Commands
429
430 Here are some examples of RPC commands with the equivalent of running
431 them with kamctl and the corresponding JSON document for them. It is
432 important to be aware that the name of the parameters doesn't matter,
433 only the order of the values must be the one expected by Kamailio RPC
434 command.
435
436 Example 1.17. JSONRPC Commands - Examples
437 ...
438 # kamctl rpc core.psx
439
440 {
441 "jsonrpc": "2.0",
442 "method": "core.psx",
443 "id": 1
444 }
445 ...
446 ## - prototype: kamctl rpc dispatcher.set_state _state_ _group_ _address_
447 # kamctl rpc dispatcher.set_state ip 2 sip:127.0.0.1:5080
448
449 {
450 "jsonrpc": "2.0",
451 "method": "dispatcher.set_state",
452 "params": ["ip", 2, "sip:127.0.0.1:5080"],
453 "id": 1
454 }
455
456 ## - or:
457
458 {
459 "jsonrpc": "2.0",
460 "method": "dispatcher.set_state",
461 "params": {
462 "state": "ip",
463 "group": 2,
464 "address": "sip:127.0.0.1:5080"
465 },
466 "id": 1
467 }
468 ...
469
470 7. JSONRPC Transports
471
472 7.1. JSONRPC Over HTTP
473 7.2. JSONRPC Over FIFO
474 7.3. JSONRPC Over DATAGRAM
475
476 JSONRPC specifications do not enforce a specific transport to carry the
477 JSON documents. Very common is JSONRPC over HTTP or HTTPS, and they are
478 supported by Kamailio. In addition, Kamailio JSONRPC over a local FIFO
479 file and over UDP/datagram using unix socket files or IPv4/IPv6
480 sockets.
481
482 An extenssion to the JSONRPC protocol is available: when "store_path"
483 field is provided in the command, instead of returning it, the result
484 is stored in a file on server at the path provided as the value of the
485 field. It can be useful for troubleshooting or when the result is very
486 large.
487
488 7.1. JSONRPC Over HTTP
489
490 It requires that XHTTP module is loaded. HTTPS can be used if you
491 enable TLS for Kamailio. The JSONRPC requests have to be sent to the
492 TCP or TLS ports of Kamailio.
493
494 The 'transport' parameter has to be 0 or 1.
495
496 The format of the JSON document must follow the JSONRPC specifications.
497
498 7.2. JSONRPC Over FIFO
499
500 This module can retrieve JSONRPC requests via a local FIFO file. To
501 enable this feature, 'fifo_name' parameter must be set and 'transport'
502 parameter has to be 0 or 2.
503
504 The format of the JSON document must follow the JSONRPC specifications
505 plus an additional field named 'reply_name'. The value for this field
506 must be the name of the FIFO file were to write the JSONRPC response.
507 The reply FIFO file is created in the folder specified by
508 'fifo_reply_dir'. Next is an example showing a JSONRPC command to be
509 sent via FIFO transport.
510
511 Example 1.18. JSONRPC Over Fifo Command
512 ...
513 {
514 "jsonrpc": "2.0",
515 "method": "core.psx",
516 "reply_name": "kamailio_jsonrpc_reply_fifo",
517 "id": 1
518 }
519 ...
520
521 Next is an example of how to test it from a terminal, assuming that the
522 parameter 'fifo_name' is set to '/tmp/kamailio_jsonrpc_fifo'.
523
524 Example 1.19. JSONRPC Over Fifo Command From Terminal
525 ...
526 mkfifo /tmp/kamailio_jsonrpc_reply_fifo
527 cat /tmp/kamailio_jsonrpc_reply_fifo &
528 echo '{"jsonrpc": "2.0", "method": "core.psx", \
529 "reply_name": "kamailio_jsonrpc_reply_fifo", "id": 1}' \
530 > /tmp/kamailio_jsonrpc_fifo
531 rm /tmp/kamailio_jsonrpc_reply_fifo
532
533 7.3. JSONRPC Over DATAGRAM
534
535 This module can retrieve JSONRPC requests via a UNIX socket file or UDP
536 IPv4/IPv6 socket. To enable this feature, 'dgram_socket' parameter must
537 be set and 'transport' parameter has to be 0 or 4.
538
539 The 'transport' parameter has to be 0 or 4.
540
541 The format of the JSON document must follow the JSONRPC specifications.