-
Notifications
You must be signed in to change notification settings - Fork 565
/
mi_trace.c
501 lines (401 loc) · 10.6 KB
/
mi_trace.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
/*
* Copyright (C) 2016 - OpenSIPS Foundation
* Copyright (C) 2001-2003 FhG Fokus
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* History:
* -------
* 2016-09-19 first version (Ionut Ionita)
*/
#include "../ut.h"
#include "mi_trace.h"
#define TRACE_API_MODULE "proto_hep"
#define MI_ID_S "mi"
#define MI_TRACE_BUF_SIZE (1 << 10)
#define CORR_BUF_SIZE 64
/* CORR - magic for internally generated correltion id */
#define CORR_MAGIC "\x43\x4F\x52\x52"
trace_proto_t* mi_trace_api=NULL;
int mi_message_id;
static char* correlation_name = "correlation_id";
str correlation_value;
int correlation_id=-1, correlation_vendor=-1;
#if 0
static char trace_buf[MI_TRACE_BUF_SIZE];
#endif
void try_load_trace_api(void)
{
/* already loaded */
if ( mi_trace_api )
return;
mi_trace_api = pkg_malloc(sizeof(trace_proto_t));
if (mi_trace_api == NULL)
return;
memset(mi_trace_api, 0, sizeof(trace_proto_t));
if (trace_prot_bind(TRACE_API_MODULE, mi_trace_api) < 0) {
LM_DBG("No tracing module used!\n");
return;
}
mi_message_id = mi_trace_api->get_message_id(MI_ID_S);
}
#if 0
#define CHECK_OVERFLOW(_len) \
do { \
if ( _len >= MI_TRACE_BUF_SIZE ) { \
LM_ERR("not enough room in command buffer!\n"); \
return 0; \
} \
} while (0);
char* build_mi_trace_request( str* cmd, struct mi_root* mi_req, str* backend)
#endif
struct mi_trace_req* build_mi_trace_request( str* cmd,
struct mi_root* mi_req, str* backend)
{
int len=0, new=0;
struct mi_node* node;
if ( !cmd || !backend )
return 0;
/* not intereseted if it doesn't fit */
snprintf( mi_treq.cmd, MAX_TRACE_FIELD, "%.*s", cmd->len, cmd->s);
snprintf( mi_treq.backend, MAX_TRACE_FIELD, "%.*s", backend->len, backend->s);
if ( mi_req ) {
node = mi_req->node.kids;
while ( node &&
(( new = snprintf( mi_treq.params + len, MAX_TRACE_FIELD - len,
"%.*s", node->value.len, node->value.s) )
< MAX_TRACE_FIELD - len) ) {
if ( new < 0) {
LM_ERR("snprintf failed!\n");
return 0;
}
node = node->next;
len += new;
}
} else {
memset( mi_treq.params, 0, MAX_TRACE_FIELD);
}
return &mi_treq;
#if 0
len = snprintf( trace_buf, MI_TRACE_BUF_SIZE,
"(%.*s) %.*s\n",
backend->len, backend->s,
cmd->len, cmd->s);
CHECK_OVERFLOW(len);
if ( mi_req ) {
node = mi_req->node.kids;
while ( node ) {
/* FIXME should we also put the name here? */
new = snprintf( trace_buf+len, MI_TRACE_BUF_SIZE - len,
"%.*s ", node->value.len, node->value.s);
len += new;
CHECK_OVERFLOW(len);
node = node->next;
}
}
return trace_buf;
#endif
}
struct mi_trace_rpl* build_mi_trace_reply( int code, str* reason, str* rpl_msg )
{
#if 0
int len, new;
#endif
if ( !reason )
return 0;
#if 0
len = snprintf( trace_buf, MI_TRACE_BUF_SIZE,
"(%d:%.*s)\n",
code, reason->len, reason->s);
CHECK_OVERFLOW(len);
if ( rpl_msg ) {
new = snprintf( trace_buf+len, MI_TRACE_BUF_SIZE,
"%.*s...\n",
rpl_msg->len > MAX_RPL_CHARS ? MAX_RPL_CHARS : rpl_msg->len,
rpl_msg->s);
len += new;
CHECK_OVERFLOW(len);
}
return trace_buf;
#endif
snprintf( mi_trpl.code, MAX_TRACE_FIELD, "%d", code);
snprintf( mi_trpl.reason, MAX_TRACE_FIELD, "%.*s", reason->len, reason->s);
if ( rpl_msg ) {
snprintf( mi_trpl.rpl, MAX_TRACE_FIELD, "%.*s", rpl_msg->len, rpl_msg->s);
} else {
memset( mi_trpl.rpl, 0, MAX_TRACE_FIELD);
}
return &mi_trpl;
}
char* generate_correlation_id(int* len)
{
static char corr_buf[CORR_BUF_SIZE];
if ( !len )
return 0;
*len = snprintf(corr_buf, CORR_BUF_SIZE, "%s%d", CORR_MAGIC, rand());
if ( *len >= CORR_BUF_SIZE ) {
LM_ERR("not enough space in correlation buffer!\n");
return 0;
}
return corr_buf;
}
int trace_mi_message(union sockaddr_union* src, union sockaddr_union* dst,
struct mi_trace_param* pld_param, str* correlation_value, trace_dest trace_dst)
{
/* FIXME is this the case for all mi impelementations?? */
const int proto = IPPROTO_TCP;
union sockaddr_union tmp, *to_su, *from_su;
static str fake_body = str_init("fake");
trace_message message;
if (mi_trace_api->create_trace_message == NULL ||
mi_trace_api->send_message == NULL) {
LM_DBG("trace api not loaded!\n");
return 0;
}
if (src == NULL || dst == NULL) {
tmp.sin.sin_addr.s_addr = TRACE_INADDR_LOOPBACK;
tmp.sin.sin_port = 0;
tmp.sin.sin_family = AF_INET;
}
/* FIXME src and/or dst port might be in htons form */
if (src)
from_su = src;
else
from_su = &tmp;
if (dst)
to_su = dst;
else
to_su = &tmp;
message = mi_trace_api->create_trace_message(from_su, to_su,
proto, &fake_body, mi_message_id, trace_dst);
if (message == NULL) {
LM_ERR("failed to create trace message!\n");
return -1;
}
if ( correlation_value ) {
if ( correlation_id < 0 || correlation_vendor < 0 ) {
if ( load_correlation_id() < 0 ) {
LM_ERR("can't load correlation id!\n");
return -1;
}
}
if ( mi_trace_api->add_trace_data( message, correlation_value->s,
correlation_value->len, TRACE_TYPE_STR,
correlation_id, correlation_vendor) < 0 ) {
LM_ERR("can't set the correlation id!\n");
return -1;
}
}
if ( pld_param->type == MI_TRACE_REQ ) {
mi_trace_api->add_trace_payload(message, "command", pld_param->d.req->cmd);
mi_trace_api->add_trace_payload(message, "backend", pld_param->d.req->backend);
if ( pld_param->d.req->params[0] ) {
mi_trace_api->add_trace_payload(message,
"parameters", pld_param->d.req->params);
}
} else {
mi_trace_api->add_trace_payload(message, "code", pld_param->d.rpl->code);
mi_trace_api->add_trace_payload(message, "reason", pld_param->d.rpl->reason);
if ( pld_param->d.rpl->rpl[0] ) {
mi_trace_api->add_trace_payload(message,
"reply", pld_param->d.rpl->rpl);
}
}
if (mi_trace_api->send_message(message, trace_dst, 0) < 0) {
LM_ERR("failed to send trace message!\n");
return -1;
}
mi_trace_api->free_message(message);
return 0;
}
int load_correlation_id(void)
{
/* already looked for them */
if (correlation_id > 0 && correlation_vendor > 0)
return 0;
return mi_trace_api->get_data_id(correlation_name, &correlation_vendor, &correlation_id);
}
static int mi_mods_no=0;
static int is_id_valid(int id)
{
/* FIXME is this valid? */
if ( id >= 8 * sizeof( *(((struct mi_cmd *)0)->trace_mask) ) ||
id < 0 )
return 0;
return 1;
}
/**
* returns an id that should internally be stored by each module implementing
* the mi interface
*/
int register_mi_trace_mod(void)
{
if ( !is_id_valid(mi_mods_no) ) {
LM_BUG("can't register any more mods; change trace mask data type"
" from struct mi_cmd!\n");
return -1;
}
return mi_mods_no++;
}
/**
*
* initialise mask to 0 or 1 depending on list type
* if whitelist all mi cmds will be initially set to 0
* if blacklist all mi cmds will be initially set to 1
*
*/
int init_mod_trace_cmds(int id, int white)
{
int idx, len;
struct mi_cmd* mi_cmds;
if ( !is_id_valid(id) ) {
LM_BUG("Invalid module id!\n");
return -1;
}
get_mi_cmds(&mi_cmds, &len);
for ( idx = 0; idx < len; idx++) {
if (white) {
*mi_cmds[idx].trace_mask &= ~(1 << id);
} else {
*mi_cmds[idx].trace_mask |= (1 << id);
}
}
return 0;
}
/**
*
* block an mi command having its name
*
*/
int block_mi_cmd_trace(int id, char* name, int len)
{
struct mi_cmd* cmd;
if ( !is_id_valid(id) ) {
LM_BUG("Invalid module id!\n");
return -1;
}
if ( !(cmd = lookup_mi_cmd(name, len)) ) {
LM_ERR("command (%.*s) not found!\n", len, name);
return -1;
}
*cmd->trace_mask &= ~(1 << id);
return 0;
}
/**
*
* allow an mi command having its name
*
*/
int allow_mi_cmd_trace(int id, char* name, int len)
{
struct mi_cmd* cmd;
if ( !is_id_valid(id) ) {
LM_BUG("Invalid module id!\n");
return -1;
}
if ( !(cmd = lookup_mi_cmd(name, len)) ) {
LM_ERR("command (%.*s) not found!\n", len, name);
return -1;
}
*cmd->trace_mask |= (1 << id);
return 0;
}
unsigned char is_mi_cmd_traced(int id, struct mi_cmd* cmd)
{
return (1 << id) & *cmd->trace_mask;
}
/**
*
* all mi modules that trace their commands must use this functions to parse
* their blacklist
*
*/
int parse_mi_cmd_bwlist(int id, char* bw_string, int len)
{
char* tok_end;
str token, list;
int white;
static const char type_delim = ':';
static const char list_delim = ',';
struct mi_cmd* cmd;
if ( bw_string == NULL || len == 0 ) {
LM_ERR("empty mi command list!\n");
return -1;
}
tok_end = q_memchr(bw_string, type_delim, len);
if ( !tok_end ) {
LM_ERR("missing list type: either blacklist( b ) or whitelist ( w )!\n");
return -1;
}
token.s = bw_string;
token.len = tok_end - bw_string;
str_trim_spaces_lr(token);
if ( token.len != 1 ) {
goto invalid_list;
} else if ( token.s[0] == 'w' || token.s[0] == 'W' ) {
white = 1;
} else if ( token.s[0] == 'b' || token.s[0] == 'B' ) {
white = 0;
} else {
goto invalid_list;
}
if ( init_mod_trace_cmds(id, white) < 0 ) {
LM_ERR("failed to initialise trace mask for mi commands!\n");
return -1;
}
if ( (tok_end - bw_string) >= len || tok_end + 1 == 0) {
LM_ERR("no command in list!\n");
return -1;
}
list.s = tok_end + 1;
list.len = len - ((tok_end + 1) - bw_string);
while ( list.s != NULL && list.len > 0 ) {
tok_end = q_memchr( list.s, list_delim, list.len );
if ( tok_end ) {
token.s = list.s;
token.len = tok_end - list.s;
list.s = tok_end + 1;
list.len -= token.len + 1;
} else {
token = list;
list.s = NULL;
list.len = 0;
}
str_trim_spaces_lr( token );
cmd = lookup_mi_cmd( token.s, token.len );
if ( cmd == NULL ) {
LM_ERR("can't find mi command [%.*s]!\n", token.len, token.s);
return -1;
}
if ( !cmd->trace_mask ) {
LM_ERR("command <%.*s> doesn't have it's trace mask allocated!\n",
token.len, token.s);
continue;
}
if ( white ) {
*cmd->trace_mask |= ( 1 << id );
} else {
*cmd->trace_mask &= ~( 1 << id );
}
}
return 0;
invalid_list:
LM_ERR("Invalid list type <%.*s>! Either b (blacklist) or w (whitelist)!\n",
token.len, token.s);
return -1;
}