Mercurial > hg > freeDiameter
comparison extensions/dbg_dict_dump_json/dbg_dict_dump_json.cc @ 1452:159d80986d85
Add dbg_dict_dump_json extension.
This extension dumps the whole dictionary to stdout during startup,
in JSON format for dict_json.
TODO: it throws quite a bit of warnings
author | Thomas Klausner <tk@giga.or.at> |
---|---|
date | Thu, 27 Feb 2020 15:41:12 +0100 |
parents | |
children | 8627338e36ab |
comparison
equal
deleted
inserted
replaced
1451:6c3485887511 | 1452:159d80986d85 |
---|---|
1 /********************************************************************************************************** | |
2 * Software License Agreement (BSD License) * | |
3 * Author: Thomas Klausner <tk@giga.or.at> * | |
4 * * | |
5 * Copyright (c) 2020 Thomas Klausner * | |
6 * All rights reserved. * | |
7 * * | |
8 * Written under contract by nfotex IT GmbH, http://nfotex.com/ * | |
9 * * | |
10 * Redistribution and use of this software in source and binary forms, with or without modification, are * | |
11 * permitted provided that the following conditions are met: * | |
12 * * | |
13 * * Redistributions of source code must retain the above * | |
14 * copyright notice, this list of conditions and the * | |
15 * following disclaimer. * | |
16 * * | |
17 * * Redistributions in binary form must reproduce the above * | |
18 * copyright notice, this list of conditions and the * | |
19 * following disclaimer in the documentation and/or other * | |
20 * materials provided with the distribution. * | |
21 * * | |
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * | |
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * | |
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * | |
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * | |
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * | |
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * | |
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * | |
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * | |
30 **********************************************************************************************************/ | |
31 | |
32 | |
33 /* | |
34 * Dump Diameter dictionary to JSON file. | |
35 * | |
36 * TODO: find out reason for many warnings in log | |
37 */ | |
38 | |
39 #include <freeDiameter/extension.h> | |
40 #include "dictionary-internal.h" | |
41 #include <json/json.h> | |
42 #include <sys/stat.h> | |
43 | |
44 #include <iomanip> | |
45 #include <sstream> | |
46 | |
47 | |
48 void | |
49 dump_vendor(dict_object *self, struct dict_vendor_data *data, struct dict_object *parent, int depth, Json::Value &main) | |
50 { | |
51 Json::Value vendor; | |
52 | |
53 vendor["Code"] = Json::Value(data->vendor_id); | |
54 vendor["Name"] = Json::Value(data->vendor_name); | |
55 main["Vendors"].append(vendor); | |
56 } | |
57 | |
58 void | |
59 dump_application(dict_object *self, struct dict_application_data *data, struct dict_object *parent, int depth, Json::Value &main) | |
60 { | |
61 Json::Value application; | |
62 | |
63 application["Code"] = Json::Value(data->application_id); | |
64 application["Name"] = Json::Value(data->application_name); | |
65 main["Applications"].append(application); | |
66 } | |
67 | |
68 void | |
69 dump_enumval(dict_object *self, struct dict_enumval_data *data, struct dict_object *parent, int depth, Json::Value &main) | |
70 { | |
71 enum dict_avp_basetype type; | |
72 struct dict_type_data type_data; | |
73 Json::Value enumval; | |
74 bool is_printable; | |
75 | |
76 type = parent->data.type.type_base; | |
77 | |
78 /* Standard only allows Integer32 Enumerated values, but freeDiameter is more permissive */ | |
79 switch (type) { | |
80 case AVP_TYPE_OCTETSTRING: | |
81 is_printable = true; | |
82 for (size_t i = 0; i < data->enum_value.os.len; i++) { | |
83 if (isprint(data->enum_value.os.data[i]) == 0) { | |
84 is_printable = false; | |
85 break; | |
86 } | |
87 } | |
88 if (is_printable) { | |
89 std::string output((const char*)data->enum_value.os.data, (size_t)data->enum_value.os.len); | |
90 enumval["Code"] = Json::Value(output); | |
91 } else { | |
92 std::stringstream quoted_string; | |
93 quoted_string << "<"; | |
94 for (size_t i = 0; i < data->enum_value.os.len; i++) { | |
95 quoted_string << std::hex << std::setfill('0') << std::setw(2) << (int)data->enum_value.os.data[i]; | |
96 } | |
97 quoted_string << ">"; | |
98 enumval["Code"] = Json::Value(quoted_string.str()); | |
99 } | |
100 break; | |
101 | |
102 case AVP_TYPE_INTEGER32: | |
103 enumval["Code"] = Json::Value(data->enum_value.i32); | |
104 break; | |
105 | |
106 case AVP_TYPE_INTEGER64: | |
107 enumval["Code"] = Json::Value(data->enum_value.i64); | |
108 break; | |
109 | |
110 case AVP_TYPE_UNSIGNED32: | |
111 enumval["Code"] = Json::Value(data->enum_value.u32); | |
112 break; | |
113 | |
114 case AVP_TYPE_UNSIGNED64: | |
115 enumval["Code"] = Json::Value(data->enum_value.u64); | |
116 break; | |
117 | |
118 case AVP_TYPE_FLOAT32: | |
119 enumval["Code"] = Json::Value(data->enum_value.f32); | |
120 break; | |
121 | |
122 case AVP_TYPE_FLOAT64: | |
123 enumval["Code"] = Json::Value(data->enum_value.f64); | |
124 break; | |
125 | |
126 default: | |
127 printf("??? (ERROR unknown type %d)", type); | |
128 break; | |
129 } | |
130 if (fd_dict_getval(parent, &type_data) != 0) { | |
131 /* TODO: fd_dict_getval error */ | |
132 return; | |
133 } | |
134 if (!main.isMember("Types") || !main["Types"].isMember(type_data.type_name)) { | |
135 /* TODO: missing type error */ | |
136 return; | |
137 } | |
138 | |
139 enumval["Name"] = Json::Value(data->enum_name); | |
140 main["Types"][type_data.type_name]["EnumValues"].append(enumval); | |
141 } | |
142 | |
143 void | |
144 dump_type(dict_object *self, struct dict_type_data *data, struct dict_object *parent, int depth, Json::Value &main) | |
145 { | |
146 Json::Value type; | |
147 | |
148 type["Name"] = Json::Value(data->type_name); | |
149 type["Base"] = Json::Value(type_base_name[data->type_base]); | |
150 main["Types"][data->type_name] = type; | |
151 } | |
152 | |
153 #define AVPFL_str "%s%s" | |
154 #define AVPFL_val(_val) (_val & AVP_FLAG_VENDOR)?"V":"" , (_val & AVP_FLAG_MANDATORY)?"M":"" | |
155 | |
156 void | |
157 dump_avp(dict_object *self, struct dict_avp_data *data, struct dict_object *parent, int depth, Json::Value &main) | |
158 { | |
159 struct dict_object *type = NULL; | |
160 struct dict_type_data type_data; | |
161 Json::Value avp; | |
162 char flags[10]; | |
163 | |
164 fd_dict_search(fd_g_config->cnf_dict, DICT_TYPE, TYPE_OF_AVP, self, &type, ENOENT); | |
165 if (fd_dict_getval(type, &type_data) != 0) { | |
166 avp["Type"] = Json::Value(type_base_name[data->avp_basetype]); | |
167 } else { | |
168 if (strstr(type_data.type_name, "Enumerated") != 0) { | |
169 if (data->avp_basetype == AVP_TYPE_INTEGER32) { | |
170 avp["Type"] = "Enumerated"; | |
171 } else { | |
172 /* freeDiameter allows enumerated types not based on integer32; | |
173 * write those out with their basic type for dict_json */ | |
174 avp["Type"] = Json::Value(type_base_name[data->avp_basetype]); | |
175 } | |
176 } else { | |
177 avp["Type"] = Json::Value(type_data.type_name); | |
178 } | |
179 if (main["Types"].isMember(type_data.type_name) && main["Types"][type_data.type_name].isMember("EnumValues")) { | |
180 avp["EnumValues"] = main["Types"][type_data.type_name]["EnumValues"]; | |
181 } | |
182 } | |
183 | |
184 avp["Code"] = Json::Value(data->avp_code); | |
185 if (data->avp_vendor != 0) { | |
186 avp["Vendor"] = Json::Value(data->avp_vendor); | |
187 } | |
188 avp["Name"] = Json::Value(data->avp_name); | |
189 snprintf(flags, sizeof(flags), AVPFL_str, AVPFL_val(data->avp_flag_val)); | |
190 avp["Flags"]["Must"] = Json::Value(flags); | |
191 snprintf(flags, sizeof(flags), AVPFL_str, AVPFL_val(data->avp_flag_mask & ~data->avp_flag_val)); | |
192 avp["Flags"]["MustNot"] = Json::Value(flags); | |
193 | |
194 main["AVPs"].append(avp); | |
195 } | |
196 | |
197 #define CMDFL_str "%s%s%s%s" | |
198 #define CMDFL_val(_val) (_val & CMD_FLAG_REQUEST)?"R":"" , (_val & CMD_FLAG_PROXIABLE)?"P":"" , (_val & CMD_FLAG_ERROR)?"E":"" , (_val & CMD_FLAG_RETRANSMIT)?"T":"" | |
199 | |
200 void | |
201 dump_command(dict_object *self, struct dict_cmd_data *data, struct dict_object *parent, int depth, Json::Value &main) | |
202 { | |
203 Json::Value command; | |
204 char flags[10]; | |
205 | |
206 command["Code"] = Json::Value(data->cmd_code); | |
207 command["Name"] = Json::Value(data->cmd_name); | |
208 snprintf(flags, sizeof(flags), CMDFL_str, CMDFL_val(data->cmd_flag_val)); | |
209 command["Flags"]["Must"] = Json::Value(flags); | |
210 snprintf(flags, sizeof(flags), CMDFL_str, CMDFL_val(data->cmd_flag_mask & ~data->cmd_flag_val)); | |
211 command["Flags"]["MustNot"] = Json::Value(flags); | |
212 main["Commands"].append(command); | |
213 } | |
214 | |
215 bool dump_object(dict_object *self, int depth, Json::Value &main); | |
216 | |
217 void | |
218 dump_rule(dict_object *self, struct dict_rule_data *data, struct dict_object *parent, int depth, Json::Value &main) | |
219 { | |
220 Json::Value avp_rule; | |
221 struct dict_avp_data avp_data; | |
222 const char *slot, *entry, *parent_avp_name; | |
223 vendor_id_t parent_avp_vendor; | |
224 unsigned int i; | |
225 bool found = false; | |
226 enum dict_object_type parent_type; | |
227 | |
228 if (fd_dict_getval(data->rule_avp, &avp_data) != 0) { | |
229 /* TODO: fd_dict_getval error */ | |
230 return; | |
231 } | |
232 | |
233 if (avp_data.avp_vendor != 0) { | |
234 avp_rule["Vendor"] = Json::Value(avp_data.avp_vendor); | |
235 } | |
236 avp_rule["AVP"] = Json::Value(avp_data.avp_name); | |
237 /* TODO is this correct? what does rule_order specify? */ | |
238 if (data->rule_position == RULE_FIXED_HEAD) { | |
239 avp_rule["First"] = Json::Value(true); | |
240 } | |
241 /* TODO: insert "unbounded" for -1? */ | |
242 if (data->rule_min != -1) { | |
243 avp_rule["Min"] = Json::Value(data->rule_min); | |
244 } | |
245 if (data->rule_max != -1) { | |
246 avp_rule["Max"] = Json::Value(data->rule_max); | |
247 } | |
248 | |
249 int ret = fd_dict_gettype(parent, &parent_type); | |
250 if (ret != 0) { | |
251 /* TODO: fd_dict_gettype error */ | |
252 return; | |
253 } | |
254 | |
255 if (parent_type == DICT_AVP) { | |
256 struct dict_avp_data parent_data; | |
257 | |
258 slot = "AVPRules"; | |
259 entry = "AVP"; | |
260 if (fd_dict_getval(parent, &parent_data) != 0) { | |
261 /* TODO: fd_dict_getval error */ | |
262 return; | |
263 } | |
264 parent_avp_name = parent_data.avp_name; | |
265 parent_avp_vendor = parent_data.avp_vendor; | |
266 } else if (parent_type == DICT_COMMAND) { | |
267 struct dict_cmd_data parent_data; | |
268 | |
269 slot = "CommandRules"; | |
270 entry = "Command"; | |
271 if (fd_dict_getval(parent, &parent_data) != 0) { | |
272 /* TODO: fd_dict_getval error */ | |
273 return; | |
274 } | |
275 parent_avp_name = parent_data.cmd_name; | |
276 parent_avp_vendor = 0; | |
277 } else { | |
278 /* TODO: error, unknown case */ | |
279 return; | |
280 } | |
281 | |
282 for (i=0; i<main[slot].size(); i++) { | |
283 if (main[slot][i][entry] == parent_avp_name | |
284 && (parent_avp_vendor == 0 || !main[slot][i].isMember("Vendor") || parent_avp_vendor == main[slot][i]["Vendor"].asUInt())) { | |
285 found = true; | |
286 main[slot][i]["Content"].append(avp_rule); | |
287 break; | |
288 } | |
289 } | |
290 if (!found) { | |
291 Json::Value parent_avp; | |
292 parent_avp[entry] = parent_avp_name; | |
293 if (parent_avp_vendor != 0) { | |
294 parent_avp["Vendor"] = Json::Value(parent_avp_vendor); | |
295 } | |
296 parent_avp["Content"].append(avp_rule); | |
297 main[slot].append(parent_avp); | |
298 } | |
299 | |
300 } | |
301 | |
302 bool | |
303 dump_list(struct fd_list *list, const char *type, int depth, Json::Value &main) { | |
304 fd_list *li; | |
305 for (li = list->next; li != list; li = li->next) { | |
306 if (!dump_object((dict_object *)li->o, depth, main)) { | |
307 LOG_E("error dumping %s", type); | |
308 return false; | |
309 } | |
310 } | |
311 return true; | |
312 } | |
313 | |
314 bool | |
315 dump_object(dict_object *self, int depth, Json::Value &main) | |
316 { | |
317 enum dict_object_type t; | |
318 int ret = fd_dict_gettype (self, &t); | |
319 if (ret != 0) { | |
320 return false; | |
321 } | |
322 | |
323 switch (t) { | |
324 #define DUMP_OBJECT_CASE(TYPE, STRUCT, DUMP_FUNCTION) \ | |
325 case TYPE: { \ | |
326 struct STRUCT * data = NULL; \ | |
327 int i; \ | |
328 data = (struct STRUCT *)malloc(sizeof(struct STRUCT)); \ | |
329 if (!data) { \ | |
330 /* TODO: malloc error */ \ | |
331 return false; \ | |
332 } \ | |
333 ret = fd_dict_getval(self, data); \ | |
334 if (ret != 0) { \ | |
335 /* TODO: fd_dict_getval error */ \ | |
336 free(data); \ | |
337 return false; \ | |
338 } \ | |
339 DUMP_FUNCTION(self, data, self->parent, depth, main); \ | |
340 if (depth > 0) { \ | |
341 for (i=0; i<NB_LISTS_PER_OBJ; i++) { \ | |
342 if ((self->list[i].o == NULL) && (self->list[i].next != &self->list[i])) { \ | |
343 dump_list(&self->list[i], 0, depth-1, main); \ | |
344 break; \ | |
345 } \ | |
346 } \ | |
347 } \ | |
348 } \ | |
349 break; | |
350 | |
351 DUMP_OBJECT_CASE(DICT_VENDOR, dict_vendor_data, dump_vendor); | |
352 DUMP_OBJECT_CASE(DICT_APPLICATION, dict_application_data, dump_application); | |
353 DUMP_OBJECT_CASE(DICT_TYPE, dict_type_data, dump_type); | |
354 DUMP_OBJECT_CASE(DICT_ENUMVAL, dict_enumval_data, dump_enumval); | |
355 DUMP_OBJECT_CASE(DICT_AVP, dict_avp_data, dump_avp); | |
356 DUMP_OBJECT_CASE(DICT_COMMAND, dict_cmd_data, dump_command); | |
357 DUMP_OBJECT_CASE(DICT_RULE, dict_rule_data, dump_rule); | |
358 default: | |
359 /* TODO: unhandled type error */ | |
360 return false; | |
361 } | |
362 return true; | |
363 } | |
364 | |
365 bool | |
366 dump_dictionary_to_json(dictionary *dict, Json::Value &main) | |
367 { | |
368 if (dict == NULL) { | |
369 return false; | |
370 } | |
371 | |
372 if (pthread_rwlock_rdlock(&dict->dict_lock) != 0) { | |
373 return false; | |
374 } | |
375 | |
376 dump_list(&(dict->dict_types), "types", 2, main); | |
377 dump_object(&(dict->dict_vendors), 3, main); | |
378 dump_list(&(dict->dict_vendors.list[0]), "vendors", 3, main); | |
379 dump_object(&(dict->dict_applications), 1, main); | |
380 dump_list(&(dict->dict_applications.list[0]), "applications", 1, main); | |
381 dump_list(&(dict->dict_cmd_code), "commands", 1, main); | |
382 | |
383 if (pthread_rwlock_unlock( &dict->dict_lock) != 0) { | |
384 return false; | |
385 } | |
386 | |
387 return true; | |
388 } | |
389 | |
390 | |
391 static int | |
392 dbg_dict_dump_json_entry(char * conffile) | |
393 { | |
394 TRACE_ENTRY("%p", conffile); | |
395 Json::Value main; | |
396 Json::Value types = Json::Value::null; | |
397 Json::StyledWriter writer; | |
398 FILE *out = stdout; | |
399 | |
400 if (conffile) { | |
401 if ((out=fopen(conffile, "w")) == NULL) { | |
402 LOG_E("cannot open output file '%s' for writing", conffile); | |
403 return EINVAL; | |
404 } | |
405 } | |
406 | |
407 /* get main dictionary */ | |
408 struct dictionary * dict = fd_g_config->cnf_dict; | |
409 if (!dump_dictionary_to_json(dict, main)) { | |
410 LOG_E("error dumping dictionary to JSON"); | |
411 return EINVAL; | |
412 } | |
413 /* remove enumerated types before dumping, they are in AVPs */ | |
414 /* convert remaining ones to array */ | |
415 Json::Value::Members members = main["Types"].getMemberNames(); | |
416 for (auto it = members.begin() ; it != members.end(); ++it) { | |
417 if (strncmp("Enumerated", main["Types"][*it]["Name"].asCString(), strlen("Enumerated")) == 0) { | |
418 main["Types"].removeMember(*it); | |
419 } else { | |
420 types.append(main["Types"][*it]); | |
421 } | |
422 } | |
423 main.removeMember("Types"); | |
424 main["Types"] = types; | |
425 | |
426 std::string value_str = writer.write(main); | |
427 fprintf(out, "%s\n", value_str.c_str()); | |
428 | |
429 if (conffile) { | |
430 fclose(out); | |
431 } | |
432 | |
433 LOG_N("Extension 'Dump dictionaries to JSON' initialized"); | |
434 return 0; | |
435 } | |
436 | |
437 extern "C" { | |
438 EXTENSION_ENTRY("dbg_dict_dump_json", dbg_dict_dump_json_entry); | |
439 } |