Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -21,9 +21,9 @@
21 21
  */
22 22
 #include <stdio.h>
23 23
 
24
-#include "../../pvar.h"
25
-#include "../../mem/shm_mem.h"
26
-#include "../../xavp.h"
24
+#include "../../core/pvar.h"
25
+#include "../../core/mem/shm_mem.h"
26
+#include "../../core/xavp.h"
27 27
 #include "../pv/pv_xavp.h"
28 28
 
29 29
 #include "debugger_api.h"
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,423 @@
1
+/**
2
+ *
3
+ * Copyright (C) 2013-2015 Victor Seva (sipwise.com)
4
+ *
5
+ * This file is part of Kamailio, a free SIP server.
6
+ *
7
+ * This file is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * This file is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
+ *
21
+ */
22
+#include <stdio.h>
23
+
24
+#include "../../pvar.h"
25
+#include "../../mem/shm_mem.h"
26
+#include "../../xavp.h"
27
+#include "../pv/pv_xavp.h"
28
+
29
+#include "debugger_api.h"
30
+#include "debugger_json.h"
31
+
32
+int _dbg_get_array_avp_vals(struct sip_msg *msg,
33
+		pv_param_t *param, srjson_doc_t *jdoc, srjson_t **jobj,
34
+		str *item_name)
35
+{
36
+	struct usr_avp *avp;
37
+	unsigned short name_type;
38
+	int_str avp_name;
39
+	int_str avp_value;
40
+	struct search_state state;
41
+	srjson_t *jobjt;
42
+	memset(&state, 0, sizeof(struct search_state));
43
+
44
+	if(pv_get_avp_name(msg, param, &avp_name, &name_type)!=0)
45
+	{
46
+		LM_ERR("invalid name\n");
47
+		return -1;
48
+	}
49
+	*jobj = srjson_CreateArray(jdoc);
50
+	if(*jobj==NULL)
51
+	{
52
+		LM_ERR("cannot create json object\n");
53
+		return -1;
54
+	}
55
+	if ((avp=search_first_avp(name_type, avp_name, &avp_value, &state))==0)
56
+	{
57
+		goto ok;
58
+	}
59
+	do
60
+	{
61
+		if(avp->flags & AVP_VAL_STR)
62
+		{
63
+			jobjt = srjson_CreateStr(jdoc, avp_value.s.s, avp_value.s.len);
64
+			if(jobjt==NULL)
65
+			{
66
+				LM_ERR("cannot create json object\n");
67
+				return -1;
68
+			}
69
+		} else {
70
+			jobjt = srjson_CreateNumber(jdoc, avp_value.n);
71
+			if(jobjt==NULL)
72
+			{
73
+				LM_ERR("cannot create json object\n");
74
+				return -1;
75
+			}
76
+		}
77
+		srjson_AddItemToArray(jdoc, *jobj, jobjt);
78
+	} while ((avp=search_next_avp(&state, &avp_value))!=0);
79
+ok:
80
+	item_name->s = avp_name.s.s;
81
+	item_name->len = avp_name.s.len;
82
+	return 0;
83
+}
84
+#define DBG_XAVP_DUMP_SIZE 32
85
+static str* _dbg_xavp_dump[DBG_XAVP_DUMP_SIZE];
86
+int _dbg_xavp_dump_lookup(pv_param_t *param)
87
+{
88
+	unsigned int i = 0;
89
+	pv_xavp_name_t *xname;
90
+
91
+	if(param==NULL)
92
+		return -1;
93
+
94
+	xname = (pv_xavp_name_t*)param->pvn.u.dname;
95
+
96
+	while(i<DBG_XAVP_DUMP_SIZE && _dbg_xavp_dump[i]!=NULL)
97
+	{
98
+		if(_dbg_xavp_dump[i]->len==xname->name.len)
99
+		{
100
+			if(strncmp(_dbg_xavp_dump[i]->s, xname->name.s, xname->name.len)==0)
101
+				return 1; /* already dump before */
102
+		}
103
+		i++;
104
+	}
105
+	if(i==DBG_XAVP_DUMP_SIZE)
106
+	{
107
+		LM_WARN("full _dbg_xavp_dump cache array\n");
108
+		return 0; /* end cache names */
109
+	}
110
+	_dbg_xavp_dump[i] = &xname->name;
111
+	return 0;
112
+}
113
+
114
+void _dbg_get_obj_xavp_val(sr_xavp_t *avp, srjson_doc_t *jdoc, srjson_t **jobj)
115
+{
116
+	static char _pv_xavp_buf[128];
117
+	int result = 0;
118
+
119
+	switch(avp->val.type) {
120
+		case SR_XTYPE_NULL:
121
+			*jobj = srjson_CreateNull(jdoc);
122
+		break;
123
+		case SR_XTYPE_INT:
124
+			*jobj = srjson_CreateNumber(jdoc, avp->val.v.i);
125
+		break;
126
+		case SR_XTYPE_STR:
127
+			*jobj = srjson_CreateStr(jdoc, avp->val.v.s.s, avp->val.v.s.len);
128
+		break;
129
+		case SR_XTYPE_TIME:
130
+			result = snprintf(_pv_xavp_buf, 128, "%lu", (long unsigned)avp->val.v.t);
131
+		break;
132
+		case SR_XTYPE_LONG:
133
+			result = snprintf(_pv_xavp_buf, 128, "%ld", (long unsigned)avp->val.v.l);
134
+		break;
135
+		case SR_XTYPE_LLONG:
136
+			result = snprintf(_pv_xavp_buf, 128, "%lld", avp->val.v.ll);
137
+		break;
138
+		case SR_XTYPE_XAVP:
139
+			result = snprintf(_pv_xavp_buf, 128, "<<xavp:%p>>", avp->val.v.xavp);
140
+		break;
141
+		case SR_XTYPE_DATA:
142
+			result = snprintf(_pv_xavp_buf, 128, "<<data:%p>>", avp->val.v.data);
143
+		break;
144
+		default:
145
+			LM_WARN("unknown data type\n");
146
+			*jobj = srjson_CreateNull(jdoc);
147
+	}
148
+	if(result<0)
149
+	{
150
+		LM_ERR("cannot convert to str\n");
151
+		*jobj = srjson_CreateNull(jdoc);
152
+	}
153
+	else if(*jobj==NULL)
154
+	{
155
+		*jobj = srjson_CreateStr(jdoc, _pv_xavp_buf, 128);
156
+	}
157
+}
158
+
159
+int _dbg_get_obj_avp_vals(str name, sr_xavp_t *xavp, srjson_doc_t *jdoc, srjson_t **jobj)
160
+{
161
+	sr_xavp_t *avp = NULL;
162
+	srjson_t *jobjt = NULL;
163
+
164
+	*jobj = srjson_CreateArray(jdoc);
165
+	if(*jobj==NULL)
166
+	{
167
+		LM_ERR("cannot create json object\n");
168
+		return -1;
169
+	}
170
+	avp = xavp;
171
+	while(avp!=NULL&&!STR_EQ(avp->name,name))
172
+	{
173
+		avp = avp->next;
174
+	}
175
+	while(avp!=NULL)
176
+	{
177
+		_dbg_get_obj_xavp_val(avp, jdoc, &jobjt);
178
+		srjson_AddItemToArray(jdoc, *jobj, jobjt);
179
+		jobjt = NULL;
180
+		avp = xavp_get_next(avp);
181
+	}
182
+
183
+	return 0;
184
+}
185
+
186
+int _dbg_get_obj_xavp_vals(struct sip_msg *msg,
187
+		pv_param_t *param, srjson_doc_t *jdoc, srjson_t **jobjr,
188
+		str *item_name)
189
+{
190
+	pv_xavp_name_t *xname = (pv_xavp_name_t*)param->pvn.u.dname;
191
+	sr_xavp_t *xavp = NULL;
192
+	sr_xavp_t *avp = NULL;
193
+	srjson_t *jobj = NULL;
194
+	srjson_t *jobjt = NULL;
195
+	struct str_list *keys;
196
+	struct str_list *k;
197
+
198
+	*jobjr = srjson_CreateArray(jdoc);
199
+	if(*jobjr==NULL)
200
+	{
201
+		LM_ERR("cannot create json object\n");
202
+		return -1;
203
+	}
204
+
205
+	item_name->s = xname->name.s;
206
+	item_name->len = xname->name.len;
207
+	xavp = xavp_get_by_index(&xname->name, 0, NULL);
208
+	if(xavp==NULL)
209
+	{
210
+		return 0; /* empty */
211
+	}
212
+
213
+	do
214
+	{
215
+		if(xavp->val.type==SR_XTYPE_XAVP)
216
+		{
217
+			avp = xavp->val.v.xavp;
218
+			jobj = srjson_CreateObject(jdoc);
219
+			if(jobj==NULL)
220
+			{
221
+				LM_ERR("cannot create json object\n");
222
+				return -1;
223
+			}
224
+			keys = xavp_get_list_key_names(xavp);
225
+			if(keys!=NULL)
226
+			{
227
+				do
228
+				{
229
+					_dbg_get_obj_avp_vals(keys->s, avp, jdoc, &jobjt);
230
+					srjson_AddStrItemToObject(jdoc, jobj, keys->s.s,
231
+						keys->s.len, jobjt);
232
+					k = keys;
233
+					keys = keys->next;
234
+					pkg_free(k);
235
+					jobjt = NULL;
236
+				}while(keys!=NULL);
237
+			}
238
+		}
239
+		if(jobj!=NULL)
240
+		{
241
+			srjson_AddItemToArray(jdoc, *jobjr, jobj);
242
+			jobj = NULL;
243
+		}
244
+	}while((xavp = xavp_get_next(xavp))!=0);
245
+
246
+	return 0;
247
+}
248
+
249
+int dbg_get_json(struct sip_msg* msg, unsigned int mask, srjson_doc_t *jdoc,
250
+	srjson_t *head)
251
+{
252
+	int i;
253
+	pv_value_t value;
254
+	pv_cache_t **_pv_cache = pv_cache_get_table();
255
+	pv_cache_t *el = NULL;
256
+	srjson_t *jobj = NULL;
257
+	str item_name = STR_NULL;
258
+	static char iname[128];
259
+
260
+	if(_pv_cache==NULL)
261
+	{
262
+		LM_ERR("cannot access pv_cache\n");
263
+		return -1;
264
+	}
265
+	if(jdoc==NULL){
266
+		LM_ERR("jdoc is null\n");
267
+		return -1;
268
+	}
269
+	if(head==NULL){
270
+		LM_ERR("head is null\n");
271
+		return -1;
272
+	}
273
+
274
+	memset(_dbg_xavp_dump, 0, sizeof(str*)*DBG_XAVP_DUMP_SIZE);
275
+	for(i=0;i<PV_CACHE_SIZE;i++)
276
+	{
277
+		el = _pv_cache[i];
278
+		while(el)
279
+		{
280
+			if(!(el->spec.type==PVT_AVP||
281
+				el->spec.type==PVT_SCRIPTVAR||
282
+				el->spec.type==PVT_XAVP||
283
+				el->spec.type==PVT_OTHER)||
284
+				!((el->spec.type==PVT_AVP&&mask&DBG_DP_AVP)||
285
+				(el->spec.type==PVT_XAVP&&mask&DBG_DP_XAVP)||
286
+				(el->spec.type==PVT_SCRIPTVAR&&mask&DBG_DP_SCRIPTVAR)||
287
+				(el->spec.type==PVT_OTHER&&mask&DBG_DP_OTHER))||
288
+				(el->spec.trans!=NULL))
289
+			{
290
+				el = el->next;
291
+				continue;
292
+			}
293
+			jobj = NULL;
294
+			item_name.len = 0;
295
+			item_name.s = 0;
296
+			iname[0] = '\0';
297
+			if(el->spec.type==PVT_AVP)
298
+			{
299
+				if(el->spec.pvp.pvi.type==PV_IDX_ALL||
300
+					(el->spec.pvp.pvi.type==PV_IDX_INT&&el->spec.pvp.pvi.u.ival!=0))
301
+				{
302
+					el = el->next;
303
+					continue;
304
+				}
305
+				else
306
+				{
307
+					if(_dbg_get_array_avp_vals(msg, &el->spec.pvp, jdoc, &jobj, &item_name)!=0)
308
+					{
309
+						LM_WARN("can't get value[%.*s]\n", el->pvname.len, el->pvname.s);
310
+						el = el->next;
311
+						continue;
312
+					}
313
+					if(srjson_GetArraySize(jdoc, jobj)==0 && !(mask&DBG_DP_NULL))
314
+					{
315
+						el = el->next;
316
+						continue;
317
+					}
318
+					snprintf(iname, 128, "$avp(%.*s)", item_name.len, item_name.s);
319
+				}
320
+			}
321
+			else if(el->spec.type==PVT_XAVP)
322
+			{
323
+				if(_dbg_xavp_dump_lookup(&el->spec.pvp)!=0)
324
+				{
325
+					el = el->next;
326
+					continue;
327
+				}
328
+				if(_dbg_get_obj_xavp_vals(msg, &el->spec.pvp, jdoc, &jobj, &item_name)!=0)
329
+				{
330
+					LM_WARN("can't get value[%.*s]\n", el->pvname.len, el->pvname.s);
331
+					el = el->next;
332
+					continue;
333
+				}
334
+				if(srjson_GetArraySize(jdoc, jobj)==0 && !(mask&DBG_DP_NULL))
335
+				{
336
+					el = el->next;
337
+					continue;
338
+				}
339
+				snprintf(iname, 128, "$xavp(%.*s)", item_name.len, item_name.s);
340
+			}
341
+			else
342
+			{
343
+				if(pv_get_spec_value(msg, &el->spec, &value)!=0)
344
+				{
345
+					LM_WARN("can't get value[%.*s]\n", el->pvname.len, el->pvname.s);
346
+					el = el->next;
347
+					continue;
348
+				}
349
+				if(value.flags&(PV_VAL_NULL|PV_VAL_EMPTY|PV_VAL_NONE))
350
+				{
351
+					if(mask&DBG_DP_NULL)
352
+					{
353
+						jobj = srjson_CreateNull(jdoc);
354
+					}
355
+					else
356
+					{
357
+						el = el->next;
358
+						continue;
359
+					}
360
+				}else if(value.flags&(PV_VAL_INT)){
361
+					jobj = srjson_CreateNumber(jdoc, value.ri);
362
+				}else if(value.flags&(PV_VAL_STR)){
363
+					jobj = srjson_CreateStr(jdoc, value.rs.s, value.rs.len);
364
+				}else {
365
+					LM_WARN("el->pvname[%.*s] value[%d] unhandled\n", el->pvname.len, el->pvname.s,
366
+						value.flags);
367
+					el = el->next;
368
+					continue;
369
+				}
370
+				if(jobj==NULL)
371
+				{
372
+					LM_ERR("el->pvname[%.*s] empty json object\n", el->pvname.len,
373
+						el->pvname.s);
374
+					goto error;
375
+				}
376
+				snprintf(iname, 128, "%.*s", el->pvname.len, el->pvname.s);
377
+			}
378
+			if(jobj!=NULL)
379
+			{
380
+				srjson_AddItemToObject(jdoc, head, iname, jobj);
381
+			}
382
+			el = el->next;
383
+		}
384
+	}
385
+	return 0;
386
+
387
+error:
388
+	srjson_Delete(jdoc, head);
389
+	return -1;
390
+}
391
+
392
+int dbg_dump_json(struct sip_msg* msg, unsigned int mask, int level)
393
+{
394
+	char *output = NULL;
395
+	srjson_doc_t jdoc;
396
+
397
+	srjson_InitDoc(&jdoc, NULL);
398
+	if(jdoc.root==NULL)
399
+	{
400
+		jdoc.root = srjson_CreateObject(&jdoc);
401
+		if(jdoc.root==NULL)
402
+		{
403
+			LM_ERR("cannot create json root\n");
404
+			goto error;
405
+		}
406
+	}
407
+
408
+	if(dbg_get_json(msg, mask, &jdoc, jdoc.root)<0) goto error;
409
+	output = srjson_PrintUnformatted(&jdoc, jdoc.root);
410
+	if(output==NULL)
411
+	{
412
+		LM_ERR("cannot print json doc\n");
413
+		srjson_DestroyDoc(&jdoc);
414
+	}
415
+	LOG(level, "%s\n", output);
416
+	jdoc.free_fn(output);
417
+	srjson_DestroyDoc(&jdoc);
418
+	return 0;
419
+
420
+error:
421
+	srjson_DestroyDoc(&jdoc);
422
+	return -1;
423
+}