forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmi.c
580 lines (471 loc) · 13.1 KB
/
mi.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
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
574
575
576
577
578
579
580
/*
* Copyright (C) 2006 Voice Sistem SRL
* Copyright (C) 2018 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.
*
*
*/
/*
* OpenSIPS Management Interface
*
* The OpenSIPS management interface (MI) is a plugin architecture with a few different
* handlers that gives access to the management interface over various transports.
*
* The OpenSIPS core and modules register commands to the interface at runtime.
* Look into the various module documentation files for information of these
* commands.
*
*/
#include <string.h>
#include "../dprint.h"
#include "../mem/mem.h"
#include "../mem/shm_mem.h"
#include "../lib/cJSON.h"
#include "../lib/osips_malloc.h"
#include "mi.h"
#include "mi_trace.h"
static struct mi_cmd* mi_cmds = 0;
static int mi_cmds_no = 0;
static cJSON_Hooks sys_mem_hooks = {
.malloc_fn = malloc,
.free_fn = free,
};
static cJSON_Hooks shm_mem_hooks = {
.malloc_fn = osips_shm_malloc,
.free_fn = osips_shm_free,
};
void _init_mi_sys_mem_hooks(void)
{
cJSON_InitHooks(&sys_mem_hooks);
}
void _init_mi_shm_mem_hooks(void)
{
cJSON_InitHooks(&shm_mem_hooks);
}
void _init_mi_pkg_mem_hooks(void)
{
cJSON_InitHooks(NULL);
}
static inline int get_mi_id( char *name, int len)
{
int n;
int i;
for( n=0,i=0 ; i<len ; n+=name[i] ,i++ );
return n;
}
static inline struct mi_cmd* lookup_mi_cmd_id(int id,char *name, int len)
{
int i;
for( i=0 ; i<mi_cmds_no ; i++ ) {
if ( id==mi_cmds[i].id && len==mi_cmds[i].name.len &&
memcmp(mi_cmds[i].name.s,name,len)==0 )
return &mi_cmds[i];
}
return 0;
}
int register_mi_mod( char *mod_name, mi_export_t *mis)
{
int ret;
int i;
if (mis==0)
return 0;
for ( i=0 ; mis[i].name ; i++ ) {
ret = register_mi_cmd(mis[i].name, mis[i].help, mis[i].flags,
mis[i].init_f, mis[i].recipes, mod_name);
if (ret!=0) {
LM_ERR("failed to register cmd <%s> for module %s\n",
mis[i].name,mod_name);
}
}
return 0;
}
int init_mi_child(void)
{
int i;
for ( i=0 ; i<mi_cmds_no ; i++ ) {
if ( mi_cmds[i].init_f && mi_cmds[i].init_f()!=0 ) {
LM_ERR("failed to init <%.*s>\n",
mi_cmds[i].name.len,mi_cmds[i].name.s);
return -1;
}
}
return 0;
}
int register_mi_cmd(char *name, char *help, unsigned int flags,
mi_child_init_f in, mi_recipe_t *recipes, char* mod_name)
{
struct mi_cmd *cmds;
int id;
int len;
if (recipes==0 || name==0) {
LM_ERR("invalid params recipes=%p, name=%s\n", recipes, name);
return -1;
}
len = strlen(name);
id = get_mi_id(name,len);
if (lookup_mi_cmd_id( id, name, len)) {
LM_ERR("command <%.*s> already registered\n", len, name);
return -1;
}
cmds = (struct mi_cmd*)pkg_realloc( mi_cmds,
(mi_cmds_no+1)*sizeof(struct mi_cmd) );
if (cmds==0) {
LM_ERR("no more pkg memory\n");
return -1;
}
mi_cmds = cmds;
mi_cmds_no++;
cmds = &cmds[mi_cmds_no-1];
cmds->init_f = in;
cmds->flags = flags;
cmds->name.s = name;
cmds->name.len = len;
cmds->module.s = mod_name;
cmds->module.len = strlen(mod_name);
cmds->help.s = help;
cmds->help.len = help ? strlen(help) : 0;
cmds->id = id;
cmds->recipes = recipes;
/**
* FIXME we should check if trace_api is loaded not to lose unnecessary space
* but some mi commands might have been already registered before loading the api
* an example is the statistics mi commands
*/
cmds->trace_mask = shm_malloc(sizeof(volatile unsigned char));
if ( !cmds->trace_mask ) {
LM_ERR("no more shm mem!\n");
return -1;
}
/* by default all commands are traced */
*cmds->trace_mask = (~(volatile unsigned char)0);
return 0;
}
struct mi_cmd* lookup_mi_cmd( char *name, int len)
{
int id;
id = get_mi_id(name,len);
return lookup_mi_cmd_id( id, name, len);
}
void get_mi_cmds( struct mi_cmd** cmds, int *size)
{
*cmds = mi_cmds;
*size = mi_cmds_no;
}
int parse_mi_request(const char *req, const char **end_ptr, mi_request_t *parsed)
{
mi_item_t *req_jsonrpc;
_init_mi_sys_mem_hooks();
parsed->req_obj = cJSON_ParseWithOpts(req, end_ptr, 0);
if (!parsed->req_obj) {
_init_mi_pkg_mem_hooks();
return -1;
}
/* check if the request is a valid JSON-RPC Request object */
/* get request id (if absent -> notification) */
parsed->id = cJSON_GetObjectItem(parsed->req_obj, JSONRPC_ID_S);
if (parsed->id && !(parsed->id->type & (cJSON_NULL|cJSON_Number|cJSON_String)))
parsed->invalid = 1;
/* check 'jsonrpc' member */
req_jsonrpc = cJSON_GetObjectItem(parsed->req_obj, JSONRPC_S);
if (!req_jsonrpc || !(req_jsonrpc->type & cJSON_String) ||
strcmp(req_jsonrpc->valuestring, JSONRPC_VERS_S))
parsed->invalid = 1;
/* check 'method' member */
parsed->method = cJSON_GetObjectItem(parsed->req_obj, JSONRPC_METHOD_S);
if (!parsed->method || !(parsed->method->type & cJSON_String)) {
parsed->method = NULL;
parsed->invalid = 1;
}
/* check 'params' member */
parsed->params = cJSON_GetObjectItem(parsed->req_obj, JSONRPC_PARAMS_S);
if (parsed->params) {
if (!(parsed->params->type & (cJSON_Array|cJSON_Object)))
parsed->invalid = 1;
else if (!parsed->params->child) {
parsed->params = NULL;
}
}
_init_mi_pkg_mem_hooks();
return 0;
}
char *mi_get_req_method(mi_request_t *req)
{
if (!req || !req->method)
return NULL;
return req->method->valuestring;
}
static int match_named_params(mi_recipe_t *recipe, mi_item_t *req_params)
{
mi_item_t *param;
int i;
for (i = 0; recipe->params[i]; i++) {
for (param = req_params->child; param; param = param->next)
if (param->string && !strcmp(recipe->params[i], param->string))
break;
if (!param)
return 0;
}
return 1;
}
static int match_no_params(mi_recipe_t *recipe, mi_item_t *req_params)
{
mi_item_t *param;
int i, j;
for (i = 0; recipe->params[i]; i++) ;
for (param = req_params->child, j = 0; param; param = param->next, j++) ;
return i == j;
}
static mi_recipe_t *get_cmd_recipe(mi_recipe_t *recipes, mi_item_t *req_params,
int pos_params, int *params_err)
{
mi_recipe_t *match = NULL;
int i;
for (i = 0; recipes[i].cmd; i++) {
if (!req_params) {
if (recipes[i].params[0] == NULL)
return &recipes[i];
else
continue;
} else {
if (recipes[i].params[0] == NULL)
continue;
}
if (pos_params) {
if (match_no_params(&recipes[i], req_params)) {
if (match) {
*params_err = -2;
return NULL;
} else {
match = &recipes[i];
}
}
} else {
if (match_no_params(&recipes[i], req_params))
*params_err = -3;
else
continue;
if (match_named_params(&recipes[i], req_params))
return &recipes[i];
}
}
return match;
}
static mi_response_t *build_err_resp(int code, const char *msg, int msg_len,
const char *details, int details_len)
{
mi_response_t *err_resp;
err_resp = init_mi_error_extra(code, msg, msg_len, details, details_len);
if (!err_resp)
LM_ERR("Failed to build MI error response object\n");
return err_resp;
}
mi_response_t *handle_mi_request(mi_request_t *req, struct mi_cmd *cmd,
struct mi_handler *async_hdl)
{
mi_response_t *resp;
mi_recipe_t *cmd_recipe;
mi_params_t cmd_params;
int params_err = -1;
int pos_params;
if (!req->req_obj) { /* error parsing the request JSON text */
LM_ERR("Failed to parse the request JSON text\n");
return build_err_resp(JSONRPC_PARSE_ERR_CODE,
MI_SSTR(JSONRPC_PARSE_ERR_MSG), NULL, 0);
}
if (req->invalid) { /* invalid jsonrpc request */
LM_ERR("Invalid JSON-RPC request\n");
return build_err_resp(JSONRPC_INVAL_REQ_CODE,
MI_SSTR(JSONRPC_INVAL_REQ_MSG), NULL, 0);
}
if (!cmd) {
LM_ERR("Command not found\n");
return build_err_resp(JSONRPC_NOT_FOUND_CODE,
MI_SSTR(JSONRPC_NOT_FOUND_MSG), NULL, 0);
}
pos_params = req->params ? req->params->type & cJSON_Array : 0;
if (pos_params && (cmd->flags & MI_NAMED_PARAMS_ONLY)) {
LM_ERR("Command only supports named parameters\n");
return build_err_resp(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR(ERR_DET_POS_PARAMS_S));
}
/* use the correct 'recipe' of the command based
* on the received parameters */
cmd_recipe = get_cmd_recipe(cmd->recipes, req->params, pos_params,
¶ms_err);
if (!cmd_recipe) {
LM_ERR("Invalid parameters\n");
if (params_err == -1)
return build_err_resp(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG), MI_SSTR(ERR_DET_NO_PARAMS_S));
else if (params_err == -2)
return build_err_resp(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR(ERR_DET_AMBIG_CALL_S));
else
return build_err_resp(JSONRPC_INVAL_PARAMS_CODE,
MI_SSTR(JSONRPC_INVAL_PARAMS_MSG),
MI_SSTR(ERR_DET_MATCH_PARAMS_S));
}
cmd_params.item = req->params;
cmd_params.list = cmd_recipe->params;
resp = cmd_recipe->cmd(&cmd_params, async_hdl);
if (resp == NULL) {
LM_ERR("Command failed\n");
return build_err_resp(JSONRPC_SERVER_ERR_CODE,
MI_SSTR(JSONRPC_SERVER_ERR_MSG), MI_SSTR(ERR_DET_CMD_NULL_S));
} else
return resp;
}
int add_id_to_response(mi_item_t *id, mi_response_t *resp)
{
if (!id) {
if (add_mi_null(resp, MI_SSTR(JSONRPC_ID_S)) < 0) {
LM_ERR("Failed to add null value to MI item\n");
return -1;
}
return 0;
}
switch ((id->type) & 0xFF) {
case cJSON_Number:
if (add_mi_number(resp, MI_SSTR(JSONRPC_ID_S), id->valueint) < 0) {
LM_ERR("Failed to add int value to MI item\n");
return -1;
}
break;
case cJSON_String:
if (add_mi_string(resp, MI_SSTR(JSONRPC_ID_S), id->valuestring,
strlen(id->valuestring)) < 0) {
LM_ERR("Failed to add string value to MI item\n");
return -1;
}
break;
case cJSON_NULL:
if (add_mi_null(resp, MI_SSTR(JSONRPC_ID_S)) < 0) {
LM_ERR("Failed to add null value to MI item\n");
return -1;
}
break;
default:
LM_ERR("'id' must be a String, Number or Null value\n");
return -1;
}
return 0;
}
static int prepare_mi_response(mi_response_t *resp, mi_item_t *id)
{
mi_item_t *res_err, *res_err_code = NULL;
res_err = cJSON_GetObjectItem(resp, JSONRPC_ERROR_S);
if (res_err) {
res_err_code = cJSON_GetObjectItem(res_err, JSONRPC_ERR_CODE_S);
if (!res_err_code) {
LM_ERR("no error code for MI error response\n");
return -1;
}
}
if (!id) {
/* this is a jsonrpc notification (no id but valid request otherwise)
* -> no response */
if (!res_err)
return MI_NO_RPL;
if (res_err_code->valueint != JSONRPC_PARSE_ERR_CODE &&
res_err_code->valueint != JSONRPC_INVAL_REQ_CODE)
return MI_NO_RPL;
}
if (add_id_to_response(id, resp) < 0)
return -1;
return 0;
}
int print_mi_response(mi_response_t *resp, mi_item_t *id, str *buf, int pretty)
{
int ret = prepare_mi_response(resp, id);
if (ret != 0)
return ret;
if (cJSON_PrintPreallocated(resp, buf->s, buf->len, pretty) == 0) {
LM_ERR("Failed to print JSON\n");
return -1;
}
return 0;
}
int print_mi_response_flush(mi_response_t *resp, mi_item_t *id,
mi_flush_f *func, void *func_p, str *buf, int pretty)
{
int ret = prepare_mi_response(resp, id);
if (ret != 0)
return ret;
if (cJSON_PrintFlushed(resp, buf->s, buf->len, pretty, func, func_p) == 0) {
LM_ERR("Failed to print JSON\n");
return -1;
}
return 0;
}
void free_mi_request_parsed(mi_request_t *request)
{
_init_mi_sys_mem_hooks();
if (request->req_obj)
cJSON_Delete(request->req_obj);
_init_mi_pkg_mem_hooks();
}
#define MI_HELP_STR "Usage: help mi_cmd - " \
"returns information about 'mi_cmd'"
#define MI_UNKNOWN_CMD "unknown MI command"
#define MI_NO_HELP "not available for this command"
#define MI_MODULE_STR "by \"%.*s\" module"
mi_response_t *w_mi_help(const mi_params_t *params,
struct mi_handler *async_hdl)
{
return init_mi_result_string(MI_SSTR(MI_HELP_STR));
}
mi_response_t *w_mi_help_1(const mi_params_t *params,
struct mi_handler *async_hdl)
{
mi_response_t *resp;
mi_item_t *resp_obj;
struct mi_cmd *cmd;
str cmd_s;
if (get_mi_string_param(params, "mi_cmd", &cmd_s.s, &cmd_s.len) < 0)
return init_mi_param_error();
/* search the command */
cmd = lookup_mi_cmd(cmd_s.s, cmd_s.len);
if (!cmd)
return init_mi_error(404, MI_SSTR(MI_UNKNOWN_CMD));
resp = init_mi_result_object(&resp_obj);
if (!resp)
return 0;
if (cmd->help.s) {
if (add_mi_string(resp_obj, MI_SSTR("Help"), cmd->help.s, cmd->help.len)
< 0) {
LM_ERR("cannot add mi item\n");
goto error;
}
} else {
if (add_mi_string(resp_obj, MI_SSTR("Help"), MI_SSTR(MI_NO_HELP)) < 0) {
LM_ERR("cannot add mi item\n");
goto error;
}
}
if (cmd->module.len && cmd->module.s && add_mi_string(resp_obj,
MI_SSTR("Exported by"), cmd->module.s, cmd->module.len) < 0) {
LM_ERR("cannot add mi item\n");
goto error;
}
return resp;
error:
free_mi_response(resp);
return 0;
}