Mercurial > hg > freeDiameter
annotate libfreeDiameter/dictionary.c @ 602:0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Thu, 25 Nov 2010 16:27:54 +0900 |
parents | 66f58cecf3cd |
children |
rev | line source |
---|---|
0 | 1 /********************************************************************************************************* |
2 * Software License Agreement (BSD License) * | |
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> * | |
4 * * | |
258
5df55136361b
Updated copyright information
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
173
diff
changeset
|
5 * Copyright (c) 2010, WIDE Project and NICT * |
0 | 6 * All rights reserved. * |
7 * * | |
8 * Redistribution and use of this software in source and binary forms, with or without modification, are * | |
9 * permitted provided that the following conditions are met: * | |
10 * * | |
11 * * Redistributions of source code must retain the above * | |
12 * copyright notice, this list of conditions and the * | |
13 * following disclaimer. * | |
14 * * | |
15 * * Redistributions in binary form must reproduce the above * | |
16 * copyright notice, this list of conditions and the * | |
17 * following disclaimer in the documentation and/or other * | |
18 * materials provided with the distribution. * | |
19 * * | |
20 * * Neither the name of the WIDE Project or NICT nor the * | |
21 * names of its contributors may be used to endorse or * | |
22 * promote products derived from this software without * | |
23 * specific prior written permission of WIDE Project and * | |
24 * NICT. * | |
25 * * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * | |
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * | |
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * | |
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * | |
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * | |
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * | |
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * | |
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * | |
34 *********************************************************************************************************/ | |
35 | |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
36 #include "libfD.h" |
0 | 37 |
38 /* Names of the base types */ | |
39 const char * type_base_name[] = { /* must keep in sync with dict_avp_basetype */ | |
40 "GROUPED", /* AVP_TYPE_GROUPED */ | |
41 "OCTETSTRING", /* AVP_TYPE_OCTETSTRING */ | |
42 "INTEGER32", /* AVP_TYPE_INTEGER32 */ | |
43 "INTEGER64", /* AVP_TYPE_INTEGER64 */ | |
44 "UNSIGNED32", /* AVP_TYPE_UNSIGNED32 */ | |
45 "UNSIGNED64", /* AVP_TYPE_UNSIGNED64 */ | |
46 "FLOAT32", /* AVP_TYPE_FLOAT32 */ | |
47 "FLOAT64" /* AVP_TYPE_FLOAT64 */ | |
48 }; | |
49 | |
50 /* The number of lists in an object */ | |
51 #define NB_LISTS_PER_OBJ 3 | |
52 | |
53 /* Some eye catchers definitions */ | |
54 #define OBJECT_EYECATCHER (0x0b13c7) | |
55 #define DICT_EYECATCHER (0x00d1c7) | |
56 | |
57 /* Definition of the dictionary objects */ | |
58 struct dict_object { | |
59 enum dict_object_type type; /* What type of object is this? */ | |
60 int objeyec;/* eyecatcher for this object */ | |
61 int typeyec;/* eyecatcher for this type of object */ | |
62 struct dictionary *dico; /* The dictionary this object belongs to */ | |
63 | |
64 union { | |
65 struct dict_vendor_data vendor; | |
66 struct dict_application_data application; | |
67 struct dict_type_data type; | |
68 struct dict_enumval_data enumval; | |
69 struct dict_avp_data avp; | |
70 struct dict_cmd_data cmd; | |
71 struct dict_rule_data rule; | |
72 } data; /* The data of this object */ | |
73 | |
74 struct dict_object * parent; /* The parent of this object, if any */ | |
75 | |
76 struct fd_list list[NB_LISTS_PER_OBJ];/* used to chain objects.*/ | |
77 /* More information about the lists : | |
78 | |
79 - the use for each list depends on the type of object. See detail bellow. | |
80 | |
81 - a sentinel for a list has its 'o' field cleared. (this is the criteria to detect end of a loop) | |
82 | |
83 - The lists are always ordered. The criteria are described bellow. the functions to order them are referenced in dict_obj_info | |
84 | |
85 - The dict_lock must be held for any list operation. | |
86 | |
87 => VENDORS: | |
88 list[0]: list of the vendors, ordered by their id. The sentinel is g_dict_vendors (vendor with id 0) | |
89 list[1]: sentinel for the list of AVPs from this vendor, ordered by AVP code. | |
90 list[2]: sentinel for the list of AVPs from this vendor, ordered by AVP name. | |
91 | |
92 => APPLICATIONS: | |
93 list[0]: list of the applications, ordered by their id. The sentinel is g_dict_applications (application with id 0) | |
94 list[1]: not used | |
95 list[2]: not used. | |
96 | |
97 => TYPES: | |
98 list[0]: list of the types, ordered by their names. The sentinel is g_list_types. | |
99 list[1]: sentinel for the type_enum list of this type, ordered by their constant name. | |
100 list[2]: sentinel for the type_enum list of this type, ordered by their constant value. | |
101 | |
102 => TYPE_ENUMS: | |
103 list[0]: list of the contants for a given type, ordered by the constant name. Sentinel is a (list[1]) element of a TYPE object. | |
104 list[1]: list of the contants for a given type, ordered by the constant value. Sentinel is a (list[2]) element of a TYPE object. | |
105 list[2]: not used | |
106 | |
107 => AVPS: | |
108 list[0]: list of the AVP from a given vendor, ordered by avp code. Sentinel is a list[1] element of a VENDOR object. | |
109 list[1]: list of the AVP from a given vendor, ordered by avp name. Sentinel is a list[2] element of a VENDOR object. | |
110 list[2]: sentinel for the rule list that apply to this AVP. | |
111 | |
112 => COMMANDS: | |
113 list[0]: list of the commands, ordered by their names. The sentinel is g_list_cmd_name. | |
114 list[1]: list of the commands, ordered by their command code and 'R' flag. The sentinel is g_list_cmd_code. | |
115 list[2]: sentinel for the rule list that apply to this command. | |
116 | |
117 => RULES: | |
118 list[0]: list of the rules for a given (grouped) AVP or Command, ordered by the AVP name to which they refer. sentinel is list[2] of a command or (grouped) avp. | |
119 list[1]: not used | |
120 list[2]: not used. | |
121 | |
122 */ | |
123 | |
124 /* Sentinel for the dispatch callbacks */ | |
125 struct fd_list disp_cbs; | |
126 | |
127 }; | |
128 | |
129 /* Definition of the dictionary structure */ | |
130 struct dictionary { | |
131 int dict_eyec; /* Eye-catcher for the dictionary (DICT_EYECATCHER) */ | |
132 | |
133 pthread_rwlock_t dict_lock; /* The global rwlock for the dictionary */ | |
134 | |
135 struct dict_object dict_vendors; /* Sentinel for the list of vendors, corresponding to vendor 0 */ | |
136 struct dict_object dict_applications; /* Sentinel for the list of applications, corresponding to app 0 */ | |
137 struct fd_list dict_types; /* Sentinel for the list of types */ | |
138 struct fd_list dict_cmd_name; /* Sentinel for the list of commands, ordered by names */ | |
139 struct fd_list dict_cmd_code; /* Sentinel for the list of commands, ordered by codes */ | |
140 | |
141 struct dict_object dict_cmd_error; /* Special command object for answers with the 'E' bit set */ | |
142 | |
446
66f58cecf3cd
Fix invalid array size (found by valgrind)
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
258
diff
changeset
|
143 int dict_count[DICT_TYPE_MAX + 1]; /* Number of objects of each type */ |
0 | 144 }; |
145 | |
146 /* Forward declarations of dump functions */ | |
147 static void dump_vendor_data ( void * data ); | |
148 static void dump_application_data ( void * data ); | |
149 static void dump_type_data ( void * data ); | |
150 /* the dump function for enum has a different prototype since it need the datatype */ | |
151 static void dump_avp_data ( void * data ); | |
152 static void dump_command_data ( void * data ); | |
153 static void dump_rule_data ( void * data ); | |
154 | |
155 /* Forward declarations of search functions */ | |
156 static int search_vendor ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ); | |
157 static int search_application ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ); | |
158 static int search_type ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ); | |
159 static int search_enumval ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ); | |
160 static int search_avp ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ); | |
161 static int search_cmd ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ); | |
162 static int search_rule ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ); | |
163 | |
164 /* The following array contains lot of data about the different types of objects, for automated handling */ | |
165 static struct { | |
166 enum dict_object_type type; /* information for this type */ | |
167 char * name; /* string describing this object, for debug */ | |
168 size_t datasize; /* The size of the data structure */ | |
169 int parent; /* 0: never; 1: may; 2: must */ | |
170 enum dict_object_type parenttype; /* The type of the parent, when relevant */ | |
171 int eyecatcher; /* A kind of signature for this object */ | |
172 void (*dump_data)(void * data ); /* The function to dump the data section */ | |
173 int (*search_fct)(struct dictionary * dict, int criteria, void * what, struct dict_object **result );; /* The function to search an object of this type */ | |
174 int haslist[NB_LISTS_PER_OBJ]; /* Tell if this list is used */ | |
175 } dict_obj_info[] = { { 0, "(error)", 0, 0, 0, 0, NULL, NULL, {0, 0, 0} } | |
176 | |
177 /* type name datasize parent parenttype | |
178 eyecatcher dump_data search_fct, haslist[] */ | |
179 | |
180 ,{ DICT_VENDOR, "VENDOR", sizeof(struct dict_vendor_data), 0, 0, | |
181 OBJECT_EYECATCHER + 1, dump_vendor_data, search_vendor, { 1, 0, 0 } } | |
182 | |
183 ,{ DICT_APPLICATION, "APPLICATION", sizeof(struct dict_application_data), 1, DICT_VENDOR, | |
184 OBJECT_EYECATCHER + 2, dump_application_data, search_application, { 1, 0, 0 } } | |
185 | |
186 ,{ DICT_TYPE, "TYPE", sizeof(struct dict_type_data), 1, DICT_APPLICATION, | |
187 OBJECT_EYECATCHER + 3, dump_type_data, search_type, { 1, 0, 0 } } | |
188 | |
189 ,{ DICT_ENUMVAL, "ENUMVAL", sizeof(struct dict_enumval_data), 2, DICT_TYPE, | |
190 OBJECT_EYECATCHER + 4, NULL, search_enumval, { 1, 1, 0 } } | |
191 | |
192 ,{ DICT_AVP, "AVP", sizeof(struct dict_avp_data), 1, DICT_TYPE, | |
193 OBJECT_EYECATCHER + 5, dump_avp_data, search_avp, { 1, 1, 0 } } | |
194 | |
195 ,{ DICT_COMMAND, "COMMAND", sizeof(struct dict_cmd_data), 1, DICT_APPLICATION, | |
196 OBJECT_EYECATCHER + 6, dump_command_data, search_cmd, { 1, 1, 0 } } | |
197 | |
198 ,{ DICT_RULE, "RULE", sizeof(struct dict_rule_data), 2, -1 /* special case: grouped avp or command */, | |
199 OBJECT_EYECATCHER + 7, dump_rule_data, search_rule, { 1, 0, 0 } } | |
200 | |
201 }; | |
202 | |
203 /* Macro to verify a "type" value */ | |
204 #define CHECK_TYPE( type ) ( ((type) > 0) && ((type) <= DICT_TYPE_MAX) ) | |
205 | |
206 /* Cast macro */ | |
207 #define _O( object ) ((struct dict_object *) (object)) | |
208 | |
209 /* Get information line for a given object */ | |
210 #define _OBINFO(object) (dict_obj_info[CHECK_TYPE(_O(object)->type) ? _O(object)->type : 0]) | |
211 | |
212 | |
213 | |
214 | |
215 /*******************************************************************************************************/ | |
216 /*******************************************************************************************************/ | |
217 /* */ | |
218 /* Objects management */ | |
219 /* */ | |
220 /*******************************************************************************************************/ | |
221 /*******************************************************************************************************/ | |
222 | |
223 /* Functions to manage the objects creation and destruction. */ | |
224 | |
225 /* Duplicate a string inplace */ | |
226 #define DUP_string( str ) { \ | |
227 char * __str = (str); \ | |
228 CHECK_MALLOC( (str) = strdup(__str) ); \ | |
229 } | |
230 | |
231 /* Initialize an object */ | |
232 static void init_object( struct dict_object * obj, enum dict_object_type type ) | |
233 { | |
234 int i; | |
235 | |
236 TRACE_ENTRY("%p %d", obj, type); | |
237 | |
238 /* Clean the object first */ | |
239 memset ( obj, 0, sizeof(struct dict_object)); | |
240 | |
241 CHECK_PARAMS_DO( CHECK_TYPE(type), return ); | |
242 | |
243 obj->type = type; | |
244 obj->objeyec = OBJECT_EYECATCHER; | |
245 obj->typeyec = _OBINFO(obj).eyecatcher; | |
246 | |
247 /* We don't initialize the data nor the parent here */ | |
248 | |
249 /* Now init the lists */ | |
250 for (i=0; i<NB_LISTS_PER_OBJ; i++) { | |
251 if (_OBINFO(obj).haslist[i] != 0) | |
252 fd_list_init(&obj->list[i], obj); | |
253 else | |
254 fd_list_init(&obj->list[i], NULL); | |
255 } | |
256 | |
257 fd_list_init(&obj->disp_cbs, NULL); | |
258 } | |
259 | |
260 /* Initialize the "data" part of an object */ | |
261 static int init_object_data(void * dest, void * source, enum dict_object_type type) | |
262 { | |
263 TRACE_ENTRY("%p %p %d", dest, source, type); | |
264 CHECK_PARAMS( dest && source && CHECK_TYPE(type) ); | |
265 | |
266 /* Generic: copy the full data structure */ | |
267 memcpy( dest, source, dict_obj_info[type].datasize ); | |
268 | |
269 /* Then strings must be duplicated, not copied */ | |
270 /* This function might be simplified by always defining the "name" field as the first field of the structures, but... it's error-prone */ | |
271 switch (type) { | |
272 case DICT_VENDOR: | |
273 DUP_string( ((struct dict_vendor_data *)dest)->vendor_name ); | |
274 break; | |
275 | |
276 case DICT_APPLICATION: | |
277 DUP_string( ((struct dict_application_data *)dest)->application_name ); | |
278 break; | |
279 | |
280 case DICT_TYPE: | |
281 DUP_string( ((struct dict_type_data *)dest)->type_name ); | |
282 break; | |
283 | |
284 case DICT_ENUMVAL: | |
285 DUP_string( ((struct dict_enumval_data *)dest)->enum_name ); | |
286 break; | |
287 | |
288 case DICT_AVP: | |
289 DUP_string( ((struct dict_avp_data *)dest)->avp_name ); | |
290 break; | |
291 | |
292 case DICT_COMMAND: | |
293 DUP_string( ((struct dict_cmd_data *)dest)->cmd_name ); | |
294 break; | |
295 | |
296 default: | |
297 /* Nothing to do for RULES */ | |
298 ; | |
299 } | |
300 | |
301 return 0; | |
302 } | |
303 | |
304 /* Check that an object is valid (1: OK, 0: error) */ | |
305 static int verify_object( struct dict_object * obj ) | |
306 { | |
307 TRACE_ENTRY("%p", obj); | |
308 | |
309 CHECK_PARAMS_DO( obj | |
310 && (obj->objeyec == OBJECT_EYECATCHER) | |
311 && CHECK_TYPE(obj->type) | |
312 && (obj->typeyec == dict_obj_info[obj->type].eyecatcher), | |
313 { | |
314 if (obj) { | |
315 TRACE_DEBUG(FULL, "Invalid object : %p\n" | |
316 " obj->objeyec : %x / %x\n" | |
317 " obj->type : %d\n" | |
318 " obj->objeyec : %x / %x\n" | |
319 " obj->typeyec : %x / %x", | |
320 obj, | |
321 obj->objeyec, OBJECT_EYECATCHER, | |
322 obj->type, | |
323 obj->objeyec, OBJECT_EYECATCHER, | |
324 obj->typeyec, _OBINFO(obj).eyecatcher); | |
325 } | |
326 return 0; | |
327 } ); | |
328 | |
329 /* The object is probably valid. */ | |
330 return 1; | |
331 } | |
332 | |
333 /* Free the data associated to an object */ | |
334 static void destroy_object_data(struct dict_object * obj) | |
335 { | |
336 /* TRACE_ENTRY("%p", obj); */ | |
337 | |
338 switch (obj->type) { | |
339 case DICT_VENDOR: | |
340 free( obj->data.vendor.vendor_name ); | |
341 break; | |
342 | |
343 case DICT_APPLICATION: | |
344 free( obj->data.application.application_name ); | |
345 break; | |
346 | |
347 case DICT_TYPE: | |
348 free( obj->data.type.type_name ); | |
349 break; | |
350 | |
351 case DICT_ENUMVAL: | |
352 free( obj->data.enumval.enum_name ); | |
353 break; | |
354 | |
355 case DICT_AVP: | |
356 free( obj->data.avp.avp_name ); | |
357 break; | |
358 | |
359 case DICT_COMMAND: | |
360 free( obj->data.cmd.cmd_name ); | |
361 break; | |
362 | |
363 default: | |
364 /* nothing to do */ | |
365 ; | |
366 } | |
367 } | |
368 | |
369 /* Forward declaration */ | |
370 static void destroy_object(struct dict_object * obj); | |
371 | |
372 /* Destroy all objects in a list - the lock must be held */ | |
373 static void destroy_list(struct fd_list * head) | |
374 { | |
375 /* TRACE_ENTRY("%p", head); */ | |
376 | |
377 /* loop in the list */ | |
378 while (!FD_IS_LIST_EMPTY(head)) | |
379 { | |
380 /* When destroying the object, it is unlinked from the list */ | |
381 destroy_object(_O(head->next->o)); | |
382 } | |
383 } | |
384 | |
385 /* Free an object and its sublists */ | |
386 static void destroy_object(struct dict_object * obj) | |
387 { | |
388 int i; | |
389 | |
390 /* TRACE_ENTRY("%p", obj); */ | |
391 | |
392 /* Update global count */ | |
393 if (obj->dico) | |
394 obj->dico->dict_count[obj->type]--; | |
395 | |
396 /* Mark the object as invalid */ | |
397 obj->objeyec = 0xdead; | |
398 | |
399 /* First, destroy the data associated to the object */ | |
400 destroy_object_data(obj); | |
401 | |
402 for (i=0; i<NB_LISTS_PER_OBJ; i++) { | |
403 if (_OBINFO(obj).haslist[i]) | |
404 /* unlink the element from the list */ | |
405 fd_list_unlink( &obj->list[i] ); | |
406 else | |
407 /* This is either a sentinel or unused (=emtpy) list, let's destroy it */ | |
408 destroy_list( &obj->list[i] ); | |
409 } | |
410 | |
411 /* Unlink all elements from the dispatch list; they will be freed when callback is unregistered */ | |
7
e5af94b04946
Added dispatch module and tests
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
412 CHECK_POSIX_DO( pthread_rwlock_wrlock(&fd_disp_lock), /* continue */ ); |
0 | 413 while (!FD_IS_LIST_EMPTY(&obj->disp_cbs)) { |
414 fd_list_unlink( obj->disp_cbs.next ); | |
415 } | |
7
e5af94b04946
Added dispatch module and tests
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
1
diff
changeset
|
416 CHECK_POSIX_DO( pthread_rwlock_unlock(&fd_disp_lock), /* continue */ ); |
0 | 417 |
418 /* Last, destroy the object */ | |
419 free(obj); | |
420 } | |
421 | |
422 /*******************************************************************************************************/ | |
423 /*******************************************************************************************************/ | |
424 /* */ | |
425 /* Compare functions */ | |
426 /* */ | |
427 /*******************************************************************************************************/ | |
428 /*******************************************************************************************************/ | |
429 | |
430 /* Compare two values */ | |
431 #define ORDER_scalar( i1, i2 ) \ | |
432 ((i1 < i2 ) ? -1 : ( i1 > i2 ? 1 : 0 )) | |
433 | |
434 | |
435 /* Compare two vendor objects by their id (checks already performed) */ | |
436 static int order_vendor_by_id ( struct dict_object *o1, struct dict_object *o2 ) | |
437 { | |
438 TRACE_ENTRY("%p %p", o1, o2); | |
439 | |
440 return ORDER_scalar( o1->data.vendor.vendor_id, o2->data.vendor.vendor_id ); | |
441 } | |
442 | |
443 /* Compare two application objects by their id (checks already performed) */ | |
444 static int order_appli_by_id ( struct dict_object *o1, struct dict_object *o2 ) | |
445 { | |
446 TRACE_ENTRY("%p %p", o1, o2); | |
447 | |
448 return ORDER_scalar( o1->data.application.application_id, o2->data.application.application_id ); | |
449 } | |
450 | |
451 /* Compare two type objects by their name (checks already performed) */ | |
452 static int order_type_by_name ( struct dict_object *o1, struct dict_object *o2 ) | |
453 { | |
454 TRACE_ENTRY("%p %p", o1, o2); | |
455 | |
456 return strcmp( o1->data.type.type_name, o2->data.type.type_name ); | |
457 } | |
458 | |
459 /* Compare two type_enum objects by their names (checks already performed) */ | |
460 static int order_enum_by_name ( struct dict_object *o1, struct dict_object *o2 ) | |
461 { | |
462 TRACE_ENTRY("%p %p", o1, o2); | |
463 | |
464 return strcmp( o1->data.enumval.enum_name, o2->data.enumval.enum_name ); | |
465 } | |
466 | |
467 /* Compare two type_enum objects by their values (checks already performed) */ | |
468 static int order_enum_by_val ( struct dict_object *o1, struct dict_object *o2 ) | |
469 { | |
470 size_t oslen; | |
471 int cmp = 0; | |
472 | |
473 TRACE_ENTRY("%p %p", o1, o2); | |
474 | |
475 /* The comparison function depends on the type of data */ | |
476 switch ( o1->parent->data.type.type_base ) { | |
477 case AVP_TYPE_OCTETSTRING: | |
478 oslen = o1->data.enumval.enum_value.os.len; | |
479 if (o2->data.enumval.enum_value.os.len < oslen) | |
480 oslen = o2->data.enumval.enum_value.os.len; | |
481 cmp = memcmp(o1->data.enumval.enum_value.os.data, o2->data.enumval.enum_value.os.data, oslen ); | |
482 return (cmp ? cmp : ORDER_scalar(o1->data.enumval.enum_value.os.len,o2->data.enumval.enum_value.os.len)); | |
483 | |
484 case AVP_TYPE_INTEGER32: | |
485 return ORDER_scalar( o1->data.enumval.enum_value.i32, o2->data.enumval.enum_value.i32 ); | |
486 | |
487 case AVP_TYPE_INTEGER64: | |
488 return ORDER_scalar( o1->data.enumval.enum_value.i64, o2->data.enumval.enum_value.i64 ); | |
489 | |
490 case AVP_TYPE_UNSIGNED32: | |
491 return ORDER_scalar( o1->data.enumval.enum_value.u32, o2->data.enumval.enum_value.u32 ); | |
492 | |
493 case AVP_TYPE_UNSIGNED64: | |
494 return ORDER_scalar( o1->data.enumval.enum_value.u64, o2->data.enumval.enum_value.u64 ); | |
495 | |
496 case AVP_TYPE_FLOAT32: | |
497 return ORDER_scalar( o1->data.enumval.enum_value.f32, o2->data.enumval.enum_value.f32 ); | |
498 | |
499 case AVP_TYPE_FLOAT64: | |
500 return ORDER_scalar( o1->data.enumval.enum_value.f64, o2->data.enumval.enum_value.f64 ); | |
501 | |
502 case AVP_TYPE_GROUPED: | |
503 default: | |
504 ASSERT(0); | |
505 } | |
506 return 0; | |
507 } | |
508 | |
509 /* Compare two avp objects by their codes (checks already performed) */ | |
510 static int order_avp_by_code ( struct dict_object *o1, struct dict_object *o2 ) | |
511 { | |
512 TRACE_ENTRY("%p %p", o1, o2); | |
513 | |
514 return ORDER_scalar( o1->data.avp.avp_code, o2->data.avp.avp_code ); | |
515 } | |
516 | |
517 /* Compare two avp objects by their names (checks already performed) */ | |
518 static int order_avp_by_name ( struct dict_object *o1, struct dict_object *o2 ) | |
519 { | |
520 TRACE_ENTRY("%p %p", o1, o2); | |
521 | |
522 return strcmp( o1->data.avp.avp_name, o2->data.avp.avp_name ); | |
523 } | |
524 | |
525 /* Compare two command objects by their names (checks already performed) */ | |
526 static int order_cmd_by_name ( struct dict_object *o1, struct dict_object *o2 ) | |
527 { | |
528 TRACE_ENTRY("%p %p", o1, o2); | |
529 | |
530 return strcmp( o1->data.cmd.cmd_name, o2->data.cmd.cmd_name ); | |
531 } | |
532 | |
533 /* Compare two command objects by their codes and flags (request or answer) (checks already performed) */ | |
534 static int order_cmd_by_codefl( struct dict_object *o1, struct dict_object *o2 ) | |
535 { | |
536 uint8_t fl1, fl2; | |
537 int cmp = 0; | |
538 | |
539 TRACE_ENTRY("%p %p", o1, o2); | |
540 | |
541 cmp = ORDER_scalar( o1->data.cmd.cmd_code, o2->data.cmd.cmd_code ); | |
542 if (cmp) | |
543 return cmp; | |
544 | |
545 /* Same command code, we must compare the value of the 'R' flag */ | |
546 fl1 = o1->data.cmd.cmd_flag_val & CMD_FLAG_REQUEST; | |
547 fl2 = o2->data.cmd.cmd_flag_val & CMD_FLAG_REQUEST; | |
548 | |
549 /* We want requests first, so we reverse the operators here */ | |
550 return ORDER_scalar(fl2, fl1); | |
551 | |
552 } | |
553 | |
554 /* Compare two rule object by the AVP name that they refer (checks already performed) */ | |
555 static int order_rule_by_avpn ( struct dict_object *o1, struct dict_object *o2 ) | |
556 { | |
557 TRACE_ENTRY("%p %p", o1, o2); | |
558 | |
559 return strcmp( o1->data.rule.rule_avp->data.avp.avp_name, o2->data.rule.rule_avp->data.avp.avp_name ); | |
560 } | |
561 | |
562 /*******************************************************************************************************/ | |
563 /*******************************************************************************************************/ | |
564 /* */ | |
565 /* Search functions */ | |
566 /* */ | |
567 /*******************************************************************************************************/ | |
568 /*******************************************************************************************************/ | |
569 | |
570 /* Functions used to search for objects in the lists, according to some criteria */ | |
571 | |
572 /* On a general note, if result is not NULL, ENOENT is not returned but *result is NULL. */ | |
573 | |
574 /* The following macros assume that "what", "ret", "result" (variables), and "end" (label) exist | |
575 in the local context where they are called. They are meant to be called only from the functions that follow. */ | |
576 | |
577 /* For searchs of type "xxx_OF_xxx": children's parent or default parent */ | |
578 #define SEARCH_childs_parent( type_of_child, default_parent ) { \ | |
579 struct dict_object *__child = (struct dict_object *) what; \ | |
580 CHECK_PARAMS_DO( verify_object(__child) && \ | |
581 (__child->type == (type_of_child)), \ | |
582 { ret = EINVAL; goto end; } ); \ | |
583 ret = 0; \ | |
584 if (result) \ | |
585 *result = (__child->parent ? __child->parent :(default_parent));\ | |
586 } | |
587 | |
588 /* For search of strings in lists. isindex= 1 if the string is the ordering key of the list */ | |
589 #define SEARCH_string( str, sentinel, datafield, isindex ) { \ | |
590 char * __str = (char *) str; \ | |
591 int __cmp; \ | |
592 struct fd_list * __li; \ | |
593 ret = 0; \ | |
602
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
594 for (__li = (sentinel)->next; __li != (sentinel); __li = __li->next) { \ |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
595 __cmp = strcmp(__str, _O(__li->o)->data. datafield ); \ |
0 | 596 if (__cmp == 0) { \ |
597 if (result) \ | |
602
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
598 *result = _O(__li->o); \ |
0 | 599 goto end; \ |
600 } \ | |
601 if ((isindex) && (__cmp < 0)) \ | |
602 break; \ | |
603 } \ | |
604 if (result) \ | |
605 *result = NULL; \ | |
606 else \ | |
607 ret = ENOENT; \ | |
608 } | |
609 | |
610 /* For search of octetstrings in lists (not \0 terminated). */ | |
611 #define SEARCH_ocstring( ostr, length, sentinel, osdatafield, isindex ) { \ | |
612 unsigned char * __ostr = (unsigned char *) ostr; \ | |
613 int __cmp; \ | |
614 size_t __len; \ | |
615 struct fd_list * __li; \ | |
616 ret = 0; \ | |
617 for (__li = (sentinel); __li->next != (sentinel); __li = __li->next) { \ | |
618 __len = _O(__li->next->o)->data. osdatafield .len; \ | |
619 if ( __len > (length) ) \ | |
620 __len = (length); \ | |
621 __cmp = memcmp(__ostr, \ | |
622 _O(__li->next->o)->data. osdatafield .data, \ | |
623 __len); \ | |
624 if (! __cmp) { \ | |
625 __cmp = ORDER_scalar( length, \ | |
626 _O(__li->next->o)->data. osdatafield .len); \ | |
627 } \ | |
628 if (__cmp == 0) { \ | |
629 if (result) \ | |
630 *result = _O(__li->next->o); \ | |
631 goto end; \ | |
632 } \ | |
633 if ((isindex) && (__cmp < 0)) \ | |
634 break; \ | |
635 } \ | |
636 if (result) \ | |
637 *result = NULL; \ | |
638 else \ | |
639 ret = ENOENT; \ | |
640 } | |
641 | |
642 /* For search of AVP name in rule lists. */ | |
643 #define SEARCH_ruleavpname( str, sentinel ) { \ | |
644 char * __str = (char *) str; \ | |
645 int __cmp; \ | |
646 struct fd_list * __li; \ | |
647 ret = 0; \ | |
648 for (__li = (sentinel); __li->next != (sentinel); __li = __li->next) { \ | |
649 __cmp = strcmp(__str, \ | |
650 _O(__li->next->o)->data.rule.rule_avp->data.avp.avp_name);\ | |
651 if (__cmp == 0) { \ | |
652 if (result) \ | |
653 *result = _O(__li->next->o); \ | |
654 goto end; \ | |
655 } \ | |
656 if (__cmp < 0) \ | |
657 break; \ | |
658 } \ | |
659 if (result) \ | |
660 *result = NULL; \ | |
661 else \ | |
662 ret = ENOENT; \ | |
663 } | |
664 | |
665 /* For search of scalars in lists. isindex= 1 if the value is the ordering key of the list */ | |
666 #define SEARCH_scalar( value, sentinel, datafield, isindex, defaultobj ) { \ | |
667 int __cmp; \ | |
668 struct fd_list * __li; \ | |
669 ret = 0; \ | |
670 if ( ((defaultobj) != NULL) \ | |
671 && (_O(defaultobj)->data. datafield == value)) { \ | |
672 if (result) \ | |
673 *result = _O(defaultobj); \ | |
674 goto end; \ | |
675 } \ | |
676 for (__li = (sentinel); __li->next != (sentinel); __li = __li->next) { \ | |
677 __cmp= ORDER_scalar(value, _O(__li->next->o)->data. datafield );\ | |
678 if (__cmp == 0) { \ | |
679 if (result) \ | |
680 *result = _O(__li->next->o); \ | |
681 goto end; \ | |
682 } \ | |
683 if ((isindex) && (__cmp < 0)) \ | |
684 break; \ | |
685 } \ | |
686 if (result) \ | |
687 *result = NULL; \ | |
688 else \ | |
689 ret = ENOENT; \ | |
690 } | |
691 | |
692 /* For search of commands in lists by code and flag. R_flag_val = 0 or CMD_FLAG_REQUEST */ | |
693 #define SEARCH_codefl( value, R_flag_val, sentinel) { \ | |
694 int __cmp; \ | |
695 struct fd_list * __li; \ | |
696 ret = 0; \ | |
697 for ( __li = (sentinel); \ | |
698 __li->next != (sentinel); \ | |
699 __li = __li->next) { \ | |
700 __cmp = ORDER_scalar(value, \ | |
701 _O(__li->next->o)->data.cmd.cmd_code ); \ | |
702 if (__cmp == 0) { \ | |
703 uint8_t __mask, __val; \ | |
704 __mask = _O(__li->next->o)->data.cmd.cmd_flag_mask; \ | |
705 __val = _O(__li->next->o)->data.cmd.cmd_flag_val; \ | |
706 if ( ! (__mask & CMD_FLAG_REQUEST) ) \ | |
707 continue; \ | |
708 if ( ( __val & CMD_FLAG_REQUEST ) != R_flag_val ) \ | |
709 continue; \ | |
710 if (result) \ | |
711 *result = _O(__li->next->o); \ | |
712 goto end; \ | |
713 } \ | |
714 if (__cmp < 0) \ | |
715 break; \ | |
716 } \ | |
717 if (result) \ | |
718 *result = NULL; \ | |
719 else \ | |
720 ret = ENOENT; \ | |
721 } | |
722 | |
723 static int search_vendor ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
724 { | |
725 int ret = 0; | |
726 vendor_id_t id; | |
727 | |
728 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
729 | |
730 switch (criteria) { | |
731 case VENDOR_BY_ID: | |
732 id = *(vendor_id_t *) what; | |
733 SEARCH_scalar( id, &dict->dict_vendors.list[0], vendor.vendor_id, 1, &dict->dict_vendors ); | |
734 break; | |
735 | |
736 case VENDOR_BY_NAME: | |
737 /* "what" is a vendor name */ | |
738 SEARCH_string( what, &dict->dict_vendors.list[0], vendor.vendor_name, 0); | |
739 break; | |
740 | |
741 case VENDOR_OF_APPLICATION: | |
742 /* "what" should be an application object */ | |
743 SEARCH_childs_parent( DICT_APPLICATION, &dict->dict_vendors ); | |
744 break; | |
745 | |
746 default: | |
747 /* Invalid criteria */ | |
748 CHECK_PARAMS( criteria = 0 ); | |
749 } | |
750 end: | |
751 return ret; | |
752 } | |
753 | |
754 static int search_application ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
755 { | |
756 int ret = 0; | |
757 application_id_t id; | |
758 | |
759 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
760 | |
761 switch (criteria) { | |
762 case APPLICATION_BY_ID: | |
763 id = *(application_id_t *) what; | |
764 | |
765 SEARCH_scalar( id, &dict->dict_applications.list[0], application.application_id, 1, &dict->dict_applications ); | |
766 break; | |
767 | |
768 case APPLICATION_BY_NAME: | |
769 /* "what" is an application name */ | |
770 SEARCH_string( what, &dict->dict_applications.list[0], application.application_name, 0); | |
771 break; | |
772 | |
773 case APPLICATION_OF_TYPE: | |
774 /* "what" should be a type object */ | |
775 SEARCH_childs_parent( DICT_TYPE, &dict->dict_applications ); | |
776 break; | |
777 | |
778 case APPLICATION_OF_COMMAND: | |
779 /* "what" should be a command object */ | |
780 SEARCH_childs_parent( DICT_COMMAND, &dict->dict_applications ); | |
781 break; | |
782 | |
783 default: | |
784 /* Invalid criteria */ | |
785 CHECK_PARAMS( criteria = 0 ); | |
786 } | |
787 end: | |
788 return ret; | |
789 } | |
790 | |
791 static int search_type ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
792 { | |
793 int ret = 0; | |
794 | |
795 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
796 | |
797 switch (criteria) { | |
798 case TYPE_BY_NAME: | |
799 /* "what" is a type name */ | |
800 SEARCH_string( what, &dict->dict_types, type.type_name, 1); | |
801 break; | |
802 | |
803 case TYPE_OF_ENUMVAL: | |
804 /* "what" should be a type_enum object */ | |
805 SEARCH_childs_parent( DICT_ENUMVAL, NULL ); | |
806 break; | |
807 | |
808 case TYPE_OF_AVP: | |
809 /* "what" should be an avp object */ | |
810 SEARCH_childs_parent( DICT_AVP, NULL ); | |
811 break; | |
812 | |
813 | |
814 default: | |
815 /* Invalid criteria */ | |
816 CHECK_PARAMS( criteria = 0 ); | |
817 } | |
818 end: | |
819 return ret; | |
820 } | |
821 | |
822 static int search_enumval ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
823 { | |
824 int ret = 0; | |
825 | |
826 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
827 | |
828 switch (criteria) { | |
829 case ENUMVAL_BY_STRUCT: | |
830 { | |
831 struct dict_object * parent = NULL; | |
832 struct dict_enumval_request * _what = (struct dict_enumval_request *) what; | |
833 | |
834 CHECK_PARAMS( _what && ( _what->type_obj || _what->type_name ) ); | |
835 | |
836 if (_what->type_obj != NULL) { | |
837 parent = _what->type_obj; | |
838 CHECK_PARAMS( verify_object(parent) && (parent->type == DICT_TYPE) ); | |
839 } else { | |
840 /* We received only the type name, we must find it first */ | |
841 CHECK_FCT_DO( search_type( dict, TYPE_BY_NAME, _what->type_name, &parent ), | |
842 CHECK_PARAMS( 0 ) ); | |
843 } | |
844 | |
845 /* From here the "parent" object is valid */ | |
846 | |
847 if ( _what->search.enum_name != NULL ) { | |
848 /* We are looking for this string */ | |
849 SEARCH_string( _what->search.enum_name, &parent->list[1], enumval.enum_name, 1 ); | |
850 } else { | |
851 /* We are looking for the value in enum_value */ | |
852 switch (parent->data.type.type_base) { | |
853 case AVP_TYPE_OCTETSTRING: | |
854 SEARCH_ocstring( _what->search.enum_value.os.data, | |
855 _what->search.enum_value.os.len, | |
856 &parent->list[2], | |
857 enumval.enum_value.os , | |
858 1 ); | |
859 break; | |
860 | |
861 case AVP_TYPE_INTEGER32: | |
862 SEARCH_scalar( _what->search.enum_value.i32, | |
863 &parent->list[2], | |
864 enumval.enum_value.i32, | |
865 1, | |
866 (struct dict_object *)NULL); | |
867 break; | |
868 | |
869 case AVP_TYPE_INTEGER64: | |
870 SEARCH_scalar( _what->search.enum_value.i64, | |
871 &parent->list[2], | |
872 enumval.enum_value.i64, | |
873 1, | |
874 (struct dict_object *)NULL); | |
875 break; | |
876 | |
877 case AVP_TYPE_UNSIGNED32: | |
878 SEARCH_scalar( _what->search.enum_value.u32, | |
879 &parent->list[2], | |
880 enumval.enum_value.u32, | |
881 1, | |
882 (struct dict_object *)NULL); | |
883 break; | |
884 | |
885 case AVP_TYPE_UNSIGNED64: | |
886 SEARCH_scalar( _what->search.enum_value.u64, | |
887 &parent->list[2], | |
888 enumval.enum_value.u64, | |
889 1, | |
890 (struct dict_object *)NULL); | |
891 break; | |
892 | |
893 case AVP_TYPE_FLOAT32: | |
894 SEARCH_scalar( _what->search.enum_value.f32, | |
895 &parent->list[2], | |
896 enumval.enum_value.f32, | |
897 1, | |
898 (struct dict_object *)NULL); | |
899 break; | |
900 | |
901 case AVP_TYPE_FLOAT64: | |
902 SEARCH_scalar( _what->search.enum_value.f64, | |
903 &parent->list[2], | |
904 enumval.enum_value.f64, | |
905 1, | |
906 (struct dict_object *)NULL); | |
907 break; | |
908 | |
909 default: | |
910 /* Invalid parent type basetype */ | |
911 CHECK_PARAMS( parent = NULL ); | |
912 } | |
913 } | |
914 | |
915 } | |
916 break; | |
917 | |
918 | |
919 default: | |
920 /* Invalid criteria */ | |
921 CHECK_PARAMS( criteria = 0 ); | |
922 } | |
923 end: | |
924 return ret; | |
925 } | |
926 | |
927 static int search_avp ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
928 { | |
929 int ret = 0; | |
930 | |
931 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
932 | |
933 switch (criteria) { | |
934 case AVP_BY_CODE: | |
935 { | |
936 avp_code_t code; | |
937 code = *(avp_code_t *) what; | |
938 | |
939 SEARCH_scalar( code, &dict->dict_vendors.list[1], avp.avp_code, 1, (struct dict_object *)NULL ); | |
940 } | |
941 break; | |
942 | |
943 case AVP_BY_NAME: | |
944 /* "what" is the AVP name, vendor 0 */ | |
945 SEARCH_string( what, &dict->dict_vendors.list[2], avp.avp_name, 1); | |
946 break; | |
947 | |
948 case AVP_BY_CODE_AND_VENDOR: | |
949 case AVP_BY_NAME_AND_VENDOR: | |
950 { | |
951 struct dict_avp_request * _what = (struct dict_avp_request *) what; | |
952 struct dict_object * vendor = NULL; | |
953 | |
954 CHECK_PARAMS( (criteria != AVP_BY_NAME_AND_VENDOR) || _what->avp_name ); | |
955 | |
956 /* Now look for the vendor first */ | |
957 CHECK_FCT( search_vendor( dict, VENDOR_BY_ID, &_what->avp_vendor, &vendor ) ); | |
958 if (vendor == NULL) { | |
959 if (result) | |
960 *result = NULL; | |
961 else | |
962 ret = ENOENT; | |
963 goto end; | |
964 } | |
965 | |
966 /* We now have our vendor = head of the appropriate avp list */ | |
967 if (criteria == AVP_BY_NAME_AND_VENDOR) { | |
968 SEARCH_string( _what->avp_name, &vendor->list[2], avp.avp_name, 1); | |
969 } else { | |
970 /* AVP_BY_CODE_AND_VENDOR */ | |
971 SEARCH_scalar( _what->avp_code, &vendor->list[1], avp.avp_code, 1, (struct dict_object *)NULL ); | |
972 } | |
973 } | |
974 break; | |
975 | |
602
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
976 case AVP_BY_NAME_ALL_VENDORS: |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
977 { |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
978 struct fd_list * li; |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
979 |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
980 /* First, search for vendor 0 */ |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
981 SEARCH_string( what, &dict->dict_vendors.list[2], avp.avp_name, 1); |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
982 |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
983 /* If not found, loop for all vendors, until found */ |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
984 for (li = dict->dict_vendors.list[0].next; li != &dict->dict_vendors.list[0]; li = li->next) { |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
985 SEARCH_string( what, &_O(li->o)->list[2], avp.avp_name, 1); |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
986 } |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
987 } |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
988 break; |
0b5d21c87855
Added new AVP search criteria in the dictionary: AVP_BY_NAME_ALL_VENDORS
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
446
diff
changeset
|
989 |
0 | 990 default: |
991 /* Invalid criteria */ | |
992 CHECK_PARAMS( criteria = 0 ); | |
993 } | |
994 end: | |
995 return ret; | |
996 } | |
997 | |
998 static int search_cmd ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
999 { | |
1000 int ret = 0; | |
1001 | |
1002 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
1003 | |
1004 switch (criteria) { | |
1005 case CMD_BY_NAME: | |
1006 /* "what" is a command name */ | |
1007 SEARCH_string( what, &dict->dict_cmd_name, cmd.cmd_name, 1); | |
1008 break; | |
1009 | |
1010 case CMD_BY_CODE_R: | |
1011 case CMD_BY_CODE_A: | |
1012 { | |
1013 command_code_t code; | |
1014 uint8_t searchfl = 0; | |
1015 | |
1016 /* The command code that we are searching */ | |
1017 code = *(command_code_t *) what; | |
1018 | |
1019 /* The flag (request or answer) of the command we are searching */ | |
1020 if (criteria == CMD_BY_CODE_R) { | |
1021 searchfl = CMD_FLAG_REQUEST; | |
1022 } | |
1023 | |
1024 /* perform the search */ | |
1025 SEARCH_codefl( code, searchfl, &dict->dict_cmd_code ); | |
1026 } | |
1027 break; | |
1028 | |
1029 case CMD_ANSWER: | |
1030 { | |
1031 /* "what" is a command object of type "request" */ | |
1032 struct dict_object * req = (struct dict_object *) what; | |
1033 struct dict_object * ans = NULL; | |
1034 | |
1035 CHECK_PARAMS( verify_object(req) | |
1036 && (req->type == DICT_COMMAND) | |
1037 && (req->data.cmd.cmd_flag_mask & CMD_FLAG_REQUEST) | |
1038 && (req->data.cmd.cmd_flag_val & CMD_FLAG_REQUEST) ); | |
1039 | |
1040 /* The answer is supposed to be the next element in the list, if it exists */ | |
1041 ans = req->list[1].next->o; | |
1042 if ( ans == NULL ) { | |
1043 TRACE_DEBUG( FULL, "the request was the last element in the list" ); | |
1044 ret = ENOENT; | |
1045 goto end; | |
1046 } | |
1047 | |
1048 /* Now check that the ans element is really the correct one */ | |
1049 if ( (ans->data.cmd.cmd_code != req->data.cmd.cmd_code) | |
1050 || (!(ans->data.cmd.cmd_flag_mask & CMD_FLAG_REQUEST)) | |
1051 || ( ans->data.cmd.cmd_flag_val & CMD_FLAG_REQUEST ) ) { | |
1052 TRACE_DEBUG( FULL, "the answer does not follow the request in the list" ); | |
1053 ret = ENOENT; | |
1054 goto end; | |
1055 } | |
1056 | |
1057 if (result) | |
1058 *result = ans; | |
1059 ret = 0; | |
1060 } | |
1061 break; | |
1062 | |
1063 default: | |
1064 /* Invalid criteria */ | |
1065 CHECK_PARAMS( criteria = 0 ); | |
1066 } | |
1067 end: | |
1068 return ret; | |
1069 } | |
1070 | |
1071 static int search_rule ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
1072 { | |
1073 int ret = 0; | |
1074 | |
1075 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
1076 | |
1077 switch (criteria) { | |
1078 case RULE_BY_AVP_AND_PARENT: | |
1079 { | |
1080 struct dict_object * parent = NULL; | |
1081 struct dict_object * avp = NULL; | |
1082 struct dict_rule_request * _what = (struct dict_rule_request *) what; | |
1083 | |
1084 CHECK_PARAMS( _what | |
1085 && (parent = _what->rule_parent) | |
1086 && (avp = _what->rule_avp ) ); | |
1087 | |
1088 CHECK_PARAMS( verify_object(parent) | |
1089 && ((parent->type == DICT_COMMAND) | |
1090 || ((parent->type == DICT_AVP) && (parent->data.avp.avp_basetype == AVP_TYPE_GROUPED))) ); | |
1091 | |
1092 CHECK_PARAMS( verify_object(avp) && (avp->type == DICT_AVP) ); | |
1093 | |
1094 /* Perform the search */ | |
1095 SEARCH_ruleavpname( avp->data.avp.avp_name, &parent->list[2]); | |
1096 | |
1097 } | |
1098 break; | |
1099 | |
1100 default: | |
1101 /* Invalid criteria */ | |
1102 CHECK_PARAMS( criteria = 0 ); | |
1103 } | |
1104 end: | |
1105 return ret; | |
1106 } | |
1107 | |
1108 /*******************************************************************************************************/ | |
1109 /*******************************************************************************************************/ | |
1110 /* */ | |
1111 /* Dump / debug functions */ | |
1112 /* */ | |
1113 /*******************************************************************************************************/ | |
1114 /*******************************************************************************************************/ | |
1115 /* The following functions are used to debug the module, and allow to print out the content of the dictionary */ | |
1116 static void dump_vendor_data ( void * data ) | |
1117 { | |
1118 struct dict_vendor_data * vendor = (struct dict_vendor_data *)data; | |
1119 | |
1120 fd_log_debug("data: %-6u \"%s\"", vendor->vendor_id, vendor->vendor_name); | |
1121 } | |
1122 static void dump_application_data ( void * data ) | |
1123 { | |
1124 struct dict_application_data * appli = (struct dict_application_data *) data; | |
1125 fd_log_debug("data: %-6u \"%s\"", appli->application_id, appli->application_name); | |
1126 } | |
1127 static void dump_type_data ( void * data ) | |
1128 { | |
1129 struct dict_type_data * type = ( struct dict_type_data * ) data; | |
1130 | |
1131 fd_log_debug("data: %-12s \"%s\"", | |
1132 type_base_name[type->type_base], | |
1133 type->type_name); | |
1134 } | |
1135 static void dump_enumval_data ( struct dict_enumval_data * enumval, enum dict_avp_basetype type ) | |
1136 { | |
1137 const int LEN_MAX = 20; | |
1138 fd_log_debug("data: (%-12s) \"%s\" -> ", type_base_name[type], enumval->enum_name); | |
1139 switch (type) { | |
1140 case AVP_TYPE_OCTETSTRING: | |
1141 { | |
1142 int i, n=LEN_MAX; | |
1143 if (enumval->enum_value.os.len < LEN_MAX) | |
1144 n = enumval->enum_value.os.len; | |
1145 for (i=0; i < n; i++) | |
1146 fd_log_debug("0x%02.2X/'%c' ", enumval->enum_value.os.data[i], ASCII(enumval->enum_value.os.data[i])); | |
1147 if (n == LEN_MAX) | |
1148 fd_log_debug("..."); | |
1149 } | |
1150 break; | |
1151 | |
1152 case AVP_TYPE_INTEGER32: | |
1153 fd_log_debug("%i", enumval->enum_value.i32); | |
1154 break; | |
1155 | |
1156 case AVP_TYPE_INTEGER64: | |
1157 fd_log_debug("%lli", enumval->enum_value.i64); | |
1158 break; | |
1159 | |
1160 case AVP_TYPE_UNSIGNED32: | |
1161 fd_log_debug("%u", enumval->enum_value.u32); | |
1162 break; | |
1163 | |
1164 case AVP_TYPE_UNSIGNED64: | |
1165 fd_log_debug("%llu", enumval->enum_value.u64); | |
1166 break; | |
1167 | |
1168 case AVP_TYPE_FLOAT32: | |
1169 fd_log_debug("%f", enumval->enum_value.f32); | |
1170 break; | |
1171 | |
1172 case AVP_TYPE_FLOAT64: | |
1173 fd_log_debug("%g", enumval->enum_value.f64); | |
1174 break; | |
1175 | |
1176 default: | |
1177 fd_log_debug("??? (ERROR unknown type %d)", type); | |
1178 } | |
1179 } | |
1180 static void dump_avp_data ( void * data ) | |
1181 { | |
1182 struct dict_avp_data * avp = (struct dict_avp_data * ) data; | |
1183 fd_log_debug("data: v/m:" DUMP_AVPFL_str "/" DUMP_AVPFL_str ", %12s, %-6u \"%s\"", | |
1184 DUMP_AVPFL_val(avp->avp_flag_val), | |
1185 DUMP_AVPFL_val(avp->avp_flag_mask), | |
1186 type_base_name[avp->avp_basetype], | |
1187 avp->avp_code, | |
1188 avp->avp_name ); | |
1189 } | |
1190 static void dump_command_data ( void * data ) | |
1191 { | |
1192 struct dict_cmd_data * cmd = (struct dict_cmd_data *) data; | |
1193 fd_log_debug("data: v/m:" DUMP_CMDFL_str "/" DUMP_CMDFL_str ", %-6u \"%s\"", | |
1194 DUMP_CMDFL_val(cmd->cmd_flag_val), DUMP_CMDFL_val(cmd->cmd_flag_mask), cmd->cmd_code, cmd->cmd_name); | |
1195 } | |
1196 static void dump_rule_data ( void * data ) | |
1197 { | |
1198 struct dict_rule_data * rule = (struct dict_rule_data * )data; | |
1199 fd_log_debug("data: pos:%d ord:%d m/M:%2d/%2d avp:\"%s\"", | |
1200 rule->rule_position, | |
1201 rule->rule_order, | |
1202 rule->rule_min, | |
1203 rule->rule_max, | |
1204 rule->rule_avp->data.avp.avp_name); | |
1205 } | |
1206 | |
1207 static void dump_object ( struct dict_object * obj, int parents, int depth, int indent ); | |
1208 | |
1209 static void dump_list ( struct fd_list * sentinel, int parents, int depth, int indent ) | |
1210 { | |
1211 struct fd_list * li = sentinel; | |
1212 /* We don't lock here, the caller must have taken the dictionary lock for reading already */ | |
1213 while (li->next != sentinel) | |
1214 { | |
1215 li = li->next; | |
1216 dump_object( _O(li->o), parents, depth, indent ); | |
1217 } | |
1218 } | |
1219 | |
1220 static void dump_object ( struct dict_object * obj, int parents, int depth, int indent ) | |
1221 { | |
1222 if (obj == NULL) | |
1223 return; | |
1224 | |
1225 if (parents) | |
1226 dump_object (obj->parent, parents-1, 0, indent + 1 ); | |
1227 | |
1228 fd_log_debug("%*s@%p: %s%s (p:%-9p) ", | |
1229 indent, | |
1230 "", | |
1231 obj, | |
1232 verify_object(obj) ? "" : "INVALID ", | |
1233 _OBINFO(obj).name, | |
1234 obj->parent); | |
1235 | |
1236 if (obj->type == DICT_ENUMVAL) | |
1237 dump_enumval_data ( &obj->data.enumval, obj->parent->data.type.type_base ); | |
1238 else | |
1239 _OBINFO(obj).dump_data(&obj->data); | |
1240 | |
1241 fd_log_debug("\n"); | |
1242 | |
1243 if (depth) { | |
1244 int i; | |
1245 for (i=0; i<NB_LISTS_PER_OBJ; i++) { | |
1246 if ((obj->list[i].o == NULL) && (obj->list[i].next != &obj->list[i])) { | |
1247 fd_log_debug("%*s>%p: list[%d]:\n", indent, "", obj, i); | |
1248 dump_list(&obj->list[i], parents, depth - 1, indent + 2); | |
1249 } | |
1250 } | |
1251 } | |
1252 } | |
1253 | |
1254 void fd_dict_dump_object(struct dict_object * obj) | |
1255 { | |
1256 fd_log_debug("Dictionary object %p dump:\n", obj); | |
1257 dump_object( obj, 1, 2, 2 ); | |
1258 } | |
1259 | |
1260 void fd_dict_dump(struct dictionary * dict) | |
1261 { | |
1262 int i; | |
1263 | |
1264 CHECK_PARAMS_DO(dict && (dict->dict_eyec == DICT_EYECATCHER), return); | |
1265 | |
1266 CHECK_POSIX_DO( pthread_rwlock_rdlock( &dict->dict_lock ), /* ignore */ ); | |
1267 | |
1268 fd_log_debug("######################################################\n"); | |
1269 fd_log_debug("###### Dumping vendors, AVPs and related rules #######\n"); | |
1270 | |
1271 dump_object( &dict->dict_vendors, 0, 3, 0 ); | |
1272 | |
1273 fd_log_debug("###### Dumping applications #######\n"); | |
1274 | |
1275 dump_object( &dict->dict_applications, 0, 1, 0 ); | |
1276 | |
1277 fd_log_debug("###### Dumping types #######\n"); | |
1278 | |
1279 dump_list( &dict->dict_types, 0, 2, 0 ); | |
1280 | |
1281 fd_log_debug("###### Dumping commands per name #######\n"); | |
1282 | |
1283 dump_list( &dict->dict_cmd_name, 0, 2, 0 ); | |
1284 | |
1285 fd_log_debug("###### Dumping commands per code and flags #######\n"); | |
1286 | |
1287 dump_list( &dict->dict_cmd_code, 0, 0, 0 ); | |
1288 | |
1289 fd_log_debug("###### Statistics #######\n"); | |
1290 | |
1291 for (i=1; i<=DICT_TYPE_MAX; i++) | |
1292 fd_log_debug(" %5d objects of type %s\n", dict->dict_count[i], dict_obj_info[i].name); | |
1293 | |
1294 fd_log_debug("######################################################\n"); | |
1295 | |
1296 /* Free the rwlock */ | |
1297 CHECK_POSIX_DO( pthread_rwlock_unlock( &dict->dict_lock ), /* ignore */ ); | |
1298 } | |
1299 | |
156
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1300 /**************************** Dump AVP values ********************************/ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1301 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1302 /* Default dump functions */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1303 static void dump_val_os(union avp_value * value) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1304 { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1305 int i; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1306 for (i = 0; i < value->os.len; i++) { |
173
4c0f358b8982
Display AVP values in decimal and hexa forms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
156
diff
changeset
|
1307 if (i == 24) { /* Dump only up to 24 bytes of the buffer */ |
4c0f358b8982
Display AVP values in decimal and hexa forms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
156
diff
changeset
|
1308 fd_log_debug("[...] (len=%zd)", value->os.len); |
156
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1309 break; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1310 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1311 fd_log_debug("%02.2X ", value->os.data[i]); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1312 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1313 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1314 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1315 static void dump_val_i32(union avp_value * value) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1316 { |
173
4c0f358b8982
Display AVP values in decimal and hexa forms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
156
diff
changeset
|
1317 fd_log_debug("%i (0x%x)", value->i32, value->i32); |
156
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1318 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1319 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1320 static void dump_val_i64(union avp_value * value) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1321 { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1322 fd_log_debug("%lli (0x%llx)", value->i64, value->i64); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1323 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1324 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1325 static void dump_val_u32(union avp_value * value) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1326 { |
173
4c0f358b8982
Display AVP values in decimal and hexa forms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
156
diff
changeset
|
1327 fd_log_debug("%u (0x%x)", value->u32, value->u32); |
156
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1328 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1329 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1330 static void dump_val_u64(union avp_value * value) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1331 { |
173
4c0f358b8982
Display AVP values in decimal and hexa forms
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
156
diff
changeset
|
1332 fd_log_debug("%llu (0x%llx)", value->u64, value->u64); |
156
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1333 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1334 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1335 static void dump_val_f32(union avp_value * value) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1336 { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1337 fd_log_debug("%f", value->f32); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1338 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1339 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1340 static void dump_val_f64(union avp_value * value) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1341 { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1342 fd_log_debug("%g", value->f64); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1343 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1344 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1345 /* Get the dump function for basic dict_avp_basetype */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1346 static void (*get_default_dump_val_cb(enum dict_avp_basetype datatype))(union avp_value *) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1347 { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1348 switch (datatype) { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1349 case AVP_TYPE_OCTETSTRING: |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1350 return &dump_val_os; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1351 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1352 case AVP_TYPE_INTEGER32: |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1353 return &dump_val_i32; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1354 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1355 case AVP_TYPE_INTEGER64: |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1356 return &dump_val_i64; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1357 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1358 case AVP_TYPE_UNSIGNED32: |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1359 return &dump_val_u32; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1360 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1361 case AVP_TYPE_UNSIGNED64: |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1362 return &dump_val_u64; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1363 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1364 case AVP_TYPE_FLOAT32: |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1365 return &dump_val_f32; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1366 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1367 case AVP_TYPE_FLOAT64: |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1368 return &dump_val_f64; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1369 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1370 case AVP_TYPE_GROUPED: |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1371 TRACE_DEBUG(FULL, "error: grouped AVP with a value!"); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1372 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1373 return NULL; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1374 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1375 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1376 /* indent inside an object (duplicate from messages.c) */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1377 #define INOBJHDR "%*s " |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1378 #define INOBJHDRVAL indent<0 ? 1 : indent, indent<0 ? "-" : "|" |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1379 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1380 /* Formater for the AVP value dump line */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1381 static void dump_avp_val(union avp_value *avp_value, void (*dump_val_cb)(union avp_value *avp_value), enum dict_avp_basetype datatype, char * type_name, char * const_name, int indent) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1382 { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1383 /* Header for all AVP values dumps: */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1384 fd_log_debug(INOBJHDR "value ", INOBJHDRVAL); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1385 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1386 /* If the type is provided, write it */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1387 if (type_name) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1388 fd_log_debug("t: '%s' ", type_name); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1389 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1390 /* Always give the base datatype anyway */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1391 fd_log_debug("(%s) ", type_base_name[datatype]); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1392 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1393 /* Now, the value */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1394 fd_log_debug("v: "); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1395 if (const_name) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1396 fd_log_debug("'%s' (", const_name); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1397 (*dump_val_cb)(avp_value); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1398 if (const_name) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1399 fd_log_debug(")"); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1400 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1401 /* Done! */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1402 fd_log_debug("\n"); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1403 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1404 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1405 /* Dump the value of an AVP of known type */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1406 void fd_dict_dump_avp_value(union avp_value *avp_value, struct dict_object * model, int indent) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1407 { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1408 void (*dump_val_cb)(union avp_value *avp_value); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1409 struct dict_object * type = NULL; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1410 char * type_name = NULL; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1411 char * const_name = NULL; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1412 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1413 /* Check the parameters are correct */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1414 CHECK_PARAMS_DO( avp_value && verify_object(model) && (model->type == DICT_AVP), return ); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1415 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1416 /* Default: display the value with the formatter for the AVP datatype */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1417 CHECK_PARAMS_DO( dump_val_cb = get_default_dump_val_cb(model->data.avp.avp_basetype), return ); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1418 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1419 /* Get the type definition of this AVP */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1420 type = model->parent; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1421 if (type) { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1422 struct dict_enumval_request request; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1423 struct dict_object * enumval = NULL; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1424 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1425 type_name = type->data.type.type_name; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1426 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1427 /* overwrite the dump function ? */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1428 if (type->data.type.type_dump) |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1429 dump_val_cb = type->data.type.type_dump; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1430 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1431 /* Now check if the AVP value matches a constant */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1432 memset(&request, 0, sizeof(request)); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1433 request.type_obj = type; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1434 memcpy(&request.search.enum_value, avp_value, sizeof(union avp_value)); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1435 /* bypass checks */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1436 if ((search_enumval( type->dico, ENUMVAL_BY_STRUCT, &request, &enumval ) == 0) && (enumval)) { |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1437 /* We found a cosntant, get its name */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1438 const_name = enumval->data.enumval.enum_name; |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1439 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1440 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1441 |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1442 /* And finally, dump the value */ |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1443 dump_avp_val(avp_value, dump_val_cb, model->data.avp.avp_basetype, type_name, const_name, indent); |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1444 } |
e2dc300819b3
Fix overwriten thread location
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
111
diff
changeset
|
1445 |
0 | 1446 /*******************************************************************************************************/ |
1447 /*******************************************************************************************************/ | |
1448 /* */ | |
1449 /* Exported functions */ | |
1450 /* */ | |
1451 /*******************************************************************************************************/ | |
1452 /*******************************************************************************************************/ | |
1453 | |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
1454 /* These are the functions exported outside libfreeDiameter. */ |
0 | 1455 |
1456 /* Get the data associated to an object */ | |
1457 int fd_dict_gettype ( struct dict_object * object, enum dict_object_type * type) | |
1458 { | |
1459 TRACE_ENTRY("%p %p", object, type); | |
1460 | |
1461 CHECK_PARAMS( type && verify_object(object) ); | |
1462 | |
1463 /* Copy the value and return */ | |
1464 *type = object->type; | |
1465 return 0; | |
1466 } | |
1467 | |
1468 int fd_dict_getdict ( struct dict_object * object, struct dictionary ** dict) | |
1469 { | |
1470 TRACE_ENTRY("%p %p", object, dict); | |
1471 | |
1472 CHECK_PARAMS( dict && verify_object(object) ); | |
1473 | |
1474 /* Copy the value and return */ | |
1475 *dict = object->dico; | |
1476 return 0; | |
1477 } | |
1478 | |
1479 | |
1480 /* Get the data associated to an object */ | |
1481 int fd_dict_getval ( struct dict_object * object, void * val) | |
1482 { | |
1483 TRACE_ENTRY("%p %p", object, val); | |
1484 | |
1485 CHECK_PARAMS( val && verify_object(object) ); | |
1486 | |
1487 /* Copy the value and return */ | |
1488 memcpy(val, &object->data, _OBINFO(object).datasize);; | |
1489 return 0; | |
1490 } | |
1491 | |
1492 /* Add a new object in the dictionary */ | |
1493 int fd_dict_new ( struct dictionary * dict, enum dict_object_type type, void * data, struct dict_object * parent, struct dict_object **ref ) | |
1494 { | |
1495 int ret = 0; | |
1496 struct dict_object * new = NULL; | |
1497 struct dict_object * vendor = NULL; | |
1498 | |
1499 TRACE_ENTRY("%p %d(%s) %p %p %p", dict, type, dict_obj_info[CHECK_TYPE(type) ? type : 0].name, data, parent, ref); | |
1500 | |
1501 /* Check parameters */ | |
1502 CHECK_PARAMS( dict && (dict->dict_eyec == DICT_EYECATCHER) && CHECK_TYPE(type) && data ); | |
1503 | |
1504 /* Check the "parent" parameter */ | |
1505 switch (dict_obj_info[type].parent) { | |
1506 case 0: /* parent is forbidden */ | |
1507 CHECK_PARAMS( parent == NULL ); | |
1508 | |
1509 case 1: /* parent is optional */ | |
1510 if (parent == NULL) | |
1511 break; | |
1512 | |
1513 case 2: /* parent is mandatory */ | |
1514 CHECK_PARAMS( verify_object(parent) ); | |
1515 | |
1516 if (type == DICT_RULE ) { /* Special case : grouped AVP or Command parents are allowed */ | |
1517 CHECK_PARAMS( (parent->type == DICT_COMMAND ) | |
1518 || ( (parent->type == DICT_AVP) && (parent->data.avp.avp_basetype == AVP_TYPE_GROUPED ) ) ); | |
1519 } else { | |
1520 CHECK_PARAMS( parent->type == dict_obj_info[type].parenttype ); | |
1521 } | |
1522 } | |
1523 | |
1524 /* For AVP object, we must also check that the "vendor" referenced exists */ | |
1525 if (type == DICT_AVP) { | |
1526 CHECK_FCT_DO( fd_dict_search( dict, DICT_VENDOR, VENDOR_BY_ID, &(((struct dict_avp_data *)data)->avp_vendor), (void*)&vendor, ENOENT ), | |
1527 CHECK_PARAMS( vendor = NULL ) ); | |
1528 | |
1529 /* Also check if a parent is provided, that the type are the same */ | |
1530 if (parent) { | |
1531 CHECK_PARAMS( parent->data.type.type_base == ((struct dict_avp_data *)data)->avp_basetype ); | |
1532 } | |
1533 } | |
1534 | |
1535 /* For RULE object, we must also check that the "avp" referenced exists */ | |
1536 if (type == DICT_RULE) { | |
1537 CHECK_PARAMS( verify_object(((struct dict_rule_data *)data)->rule_avp) ); | |
1538 CHECK_PARAMS( ((struct dict_rule_data *)data)->rule_avp->type == DICT_AVP ); | |
1539 } | |
1540 | |
1541 /* For COMMAND object, check that the 'R' flag is fixed */ | |
1542 if (type == DICT_COMMAND) { | |
1543 CHECK_PARAMS( ((struct dict_cmd_data *)data)->cmd_flag_mask & CMD_FLAG_REQUEST ); | |
1544 } | |
1545 | |
109
a522268ec95b
Less permissive in fd_dict_new
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
43
diff
changeset
|
1546 /* We have to check that the new values are not equal to the sentinels */ |
a522268ec95b
Less permissive in fd_dict_new
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
43
diff
changeset
|
1547 if (type == DICT_VENDOR) { |
a522268ec95b
Less permissive in fd_dict_new
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
43
diff
changeset
|
1548 CHECK_PARAMS( ((struct dict_vendor_data *)data)->vendor_id != 0 ); |
a522268ec95b
Less permissive in fd_dict_new
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
43
diff
changeset
|
1549 } |
a522268ec95b
Less permissive in fd_dict_new
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
43
diff
changeset
|
1550 if (type == DICT_APPLICATION) { |
a522268ec95b
Less permissive in fd_dict_new
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
43
diff
changeset
|
1551 CHECK_PARAMS( ((struct dict_application_data *)data)->application_id != 0 ); |
a522268ec95b
Less permissive in fd_dict_new
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
43
diff
changeset
|
1552 } |
a522268ec95b
Less permissive in fd_dict_new
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
43
diff
changeset
|
1553 |
0 | 1554 /* Parameters are valid, create the new object */ |
1555 CHECK_MALLOC( new = malloc(sizeof(struct dict_object)) ); | |
1556 | |
1557 /* Initialize the data of the new object */ | |
1558 init_object(new, type); | |
1559 init_object_data(&new->data, data, type); | |
1560 new->dico = dict; | |
1561 new->parent = parent; | |
1562 | |
1563 /* We will change the dictionary => acquire the write lock */ | |
1564 CHECK_POSIX_DO( ret = pthread_rwlock_wrlock(&dict->dict_lock), goto error_free ); | |
1565 | |
1566 /* Now link the object -- this also checks that no object with same keys already exists */ | |
1567 switch (type) { | |
1568 case DICT_VENDOR: | |
1569 /* A vendor object is linked in the g_dict_vendors.list[0], by their id */ | |
1570 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &dict->dict_vendors.list[0], &new->list[0], (int (*)(void*, void *))order_vendor_by_id, (void **)ref ), | |
1571 goto error_unlock ); | |
1572 break; | |
1573 | |
1574 case DICT_APPLICATION: | |
1575 /* An application object is linked in the g_dict_applciations.list[0], by their id */ | |
1576 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &dict->dict_applications.list[0], &new->list[0], (int (*)(void*, void *))order_appli_by_id, (void **)ref ), | |
1577 goto error_unlock ); | |
1578 break; | |
1579 | |
1580 case DICT_TYPE: | |
1581 /* A type object is linked in g_list_types by its name */ | |
1582 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &dict->dict_types, &new->list[0], (int (*)(void*, void *))order_type_by_name, (void **)ref ), | |
1583 goto error_unlock ); | |
1584 break; | |
1585 | |
1586 case DICT_ENUMVAL: | |
1587 /* A type_enum object is linked in it's parent 'type' object lists 1 and 2 by its name and values */ | |
1588 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &parent->list[1], &new->list[0], (int (*)(void*, void *))order_enum_by_name, (void **)ref ), | |
1589 goto error_unlock ); | |
1590 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &parent->list[2], &new->list[1], (int (*)(void*, void *))order_enum_by_val, (void **)ref ), | |
1591 { fd_list_unlink(&new->list[0]); goto error_unlock; } ); | |
1592 break; | |
1593 | |
1594 case DICT_AVP: | |
1595 /* An avp object is linked in lists 1 and 2 of its vendor, by code and name */ | |
1596 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &vendor->list[1], &new->list[0], (int (*)(void*, void *))order_avp_by_code, (void **)ref ), | |
1597 goto error_unlock ); | |
1598 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &vendor->list[2], &new->list[1], (int (*)(void*, void *))order_avp_by_name, (void **)ref ), | |
1599 { fd_list_unlink(&new->list[0]); goto error_unlock; } ); | |
1600 break; | |
1601 | |
1602 case DICT_COMMAND: | |
1603 /* A command object is linked in g_list_cmd_name and g_list_cmd_code by its name and code */ | |
1604 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &dict->dict_cmd_code, &new->list[1], (int (*)(void*, void *))order_cmd_by_codefl, (void **)ref ), | |
1605 goto error_unlock ); | |
1606 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &dict->dict_cmd_name, &new->list[0], (int (*)(void*, void *))order_cmd_by_name, (void **)ref ), | |
1607 { fd_list_unlink(&new->list[1]); goto error_unlock; } ); | |
1608 break; | |
1609 | |
1610 case DICT_RULE: | |
1611 /* A rule object is linked in list[2] of its parent command or AVP by the name of the AVP it refers */ | |
1612 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &parent->list[2], &new->list[0], (int (*)(void*, void *))order_rule_by_avpn, (void **)ref ), | |
1613 goto error_unlock ); | |
1614 break; | |
1615 | |
1616 default: | |
1617 ASSERT(0); | |
1618 } | |
1619 | |
1620 /* A new object has been created, increment the global counter */ | |
1621 dict->dict_count[type]++; | |
1622 | |
1623 /* Unlock the dictionary */ | |
1624 CHECK_POSIX_DO( ret = pthread_rwlock_unlock(&dict->dict_lock), goto error_free ); | |
1625 | |
1626 /* Save the pointer to the new object */ | |
1627 if (ref) | |
1628 *ref = new; | |
1629 | |
1630 return 0; | |
1631 | |
1632 error_unlock: | |
1633 CHECK_POSIX_DO( pthread_rwlock_unlock(&dict->dict_lock), /* continue */ ); | |
1634 error_free: | |
1635 free(new); | |
1636 return ret; | |
1637 } | |
1638 | |
1639 int fd_dict_search ( struct dictionary * dict, enum dict_object_type type, int criteria, void * what, struct dict_object **result, int retval ) | |
1640 { | |
1641 int ret = 0; | |
1642 | |
1643 TRACE_ENTRY("%p %d(%s) %d %p %p %d", dict, type, dict_obj_info[CHECK_TYPE(type) ? type : 0].name, criteria, what, result, retval); | |
1644 | |
1645 /* Check param */ | |
1646 CHECK_PARAMS( dict && (dict->dict_eyec == DICT_EYECATCHER) && CHECK_TYPE(type) ); | |
1647 | |
1648 /* Lock the dictionary for reading */ | |
1649 CHECK_POSIX( pthread_rwlock_rdlock(&dict->dict_lock) ); | |
1650 | |
1651 /* Now call the type-specific search function */ | |
1652 ret = dict_obj_info[type].search_fct (dict, criteria, what, result); | |
1653 | |
1654 /* Unlock */ | |
1655 CHECK_POSIX( pthread_rwlock_unlock(&dict->dict_lock) ); | |
1656 | |
1657 /* Update the return value as needed */ | |
1658 if ((result != NULL) && (*result == NULL)) | |
1659 ret = retval; | |
1660 | |
1661 return ret; | |
1662 } | |
1663 | |
1664 /*******************************************************************************************************/ | |
1665 /*******************************************************************************************************/ | |
1666 /* */ | |
1667 /* The init/fini functions */ | |
1668 /* */ | |
1669 /*******************************************************************************************************/ | |
1670 /*******************************************************************************************************/ | |
1671 | |
1672 /* Initialize the dictionary */ | |
1673 int fd_dict_init ( struct dictionary ** dict) | |
1674 { | |
1675 struct dictionary * new = NULL; | |
1676 | |
1677 TRACE_ENTRY(""); | |
1678 | |
1679 /* Sanity checks */ | |
1680 ASSERT( (sizeof(type_base_name) / sizeof(type_base_name[0])) == (AVP_TYPE_MAX + 1) ); | |
1681 ASSERT( (sizeof(dict_obj_info) / sizeof(dict_obj_info[0])) == (DICT_TYPE_MAX + 1) ); | |
1682 CHECK_PARAMS(dict); | |
1683 | |
1684 /* Allocate the memory for the dictionary */ | |
1685 CHECK_MALLOC( new = malloc(sizeof(struct dictionary)) ); | |
1686 memset(new, 0, sizeof(struct dictionary)); | |
1687 | |
1688 new->dict_eyec = DICT_EYECATCHER; | |
1689 | |
1690 /* Initialize the lock for the dictionary */ | |
1691 CHECK_POSIX( pthread_rwlock_init(&new->dict_lock, NULL) ); | |
1692 | |
1693 /* Initialize the sentinel for vendors and AVP lists */ | |
1694 init_object( &new->dict_vendors, DICT_VENDOR ); | |
1695 new->dict_vendors.data.vendor.vendor_name = "(no vendor)"; | |
1696 new->dict_vendors.list[0].o = NULL; /* overwrite since element is also sentinel for this list. */ | |
111
3b6174b6b35b
Fix fd_dict_getdict command for sentinels in the dictionary
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
109
diff
changeset
|
1697 new->dict_vendors.dico = new; |
0 | 1698 |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
7
diff
changeset
|
1699 /* Initialize the sentinel for applications */ |
0 | 1700 init_object( &new->dict_applications, DICT_APPLICATION ); |
1701 new->dict_applications.data.application.application_name = "Diameter Common Messages"; | |
1702 new->dict_applications.list[0].o = NULL; /* overwrite since since element is also sentinel for this list. */ | |
111
3b6174b6b35b
Fix fd_dict_getdict command for sentinels in the dictionary
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
109
diff
changeset
|
1703 new->dict_applications.dico = new; |
0 | 1704 |
1705 /* Initialize the sentinel for types */ | |
1706 fd_list_init ( &new->dict_types, NULL ); | |
1707 | |
1708 /* Initialize the sentinels for commands */ | |
1709 fd_list_init ( &new->dict_cmd_name, NULL ); | |
1710 fd_list_init ( &new->dict_cmd_code, NULL ); | |
1711 | |
1712 /* Initialize the error command object */ | |
1713 init_object( &new->dict_cmd_error, DICT_COMMAND ); | |
1714 new->dict_cmd_error.data.cmd.cmd_name="(generic error format)"; | |
1715 new->dict_cmd_error.data.cmd.cmd_flag_mask=CMD_FLAG_ERROR | CMD_FLAG_REQUEST | CMD_FLAG_RETRANSMIT; | |
1716 new->dict_cmd_error.data.cmd.cmd_flag_val =CMD_FLAG_ERROR; | |
111
3b6174b6b35b
Fix fd_dict_getdict command for sentinels in the dictionary
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
109
diff
changeset
|
1717 new->dict_cmd_error.dico = new; |
0 | 1718 |
1719 *dict = new; | |
1720 | |
1721 /* Done */ | |
1722 return 0; | |
1723 } | |
1724 | |
1725 /* Destroy a dictionary */ | |
1726 int fd_dict_fini ( struct dictionary ** dict) | |
1727 { | |
1728 int i; | |
1729 | |
1730 TRACE_ENTRY(""); | |
1731 CHECK_PARAMS( dict && *dict && ((*dict)->dict_eyec == DICT_EYECATCHER) ); | |
1732 | |
1733 /* Acquire the write lock to make sure no other operation is ongoing */ | |
1734 CHECK_POSIX( pthread_rwlock_wrlock(&(*dict)->dict_lock) ); | |
1735 | |
1736 /* Empty all the lists, free the elements */ | |
1737 destroy_list ( &(*dict)->dict_cmd_error.list[2] ); | |
1738 destroy_list ( &(*dict)->dict_cmd_code ); | |
1739 destroy_list ( &(*dict)->dict_cmd_name ); | |
1740 destroy_list ( &(*dict)->dict_types ); | |
1741 for (i=0; i< NB_LISTS_PER_OBJ; i++) { | |
1742 destroy_list ( &(*dict)->dict_applications.list[i] ); | |
1743 destroy_list ( &(*dict)->dict_vendors.list[i] ); | |
1744 } | |
1745 | |
1746 /* Dictionary is empty, now destroy the lock */ | |
1747 CHECK_POSIX( pthread_rwlock_unlock(&(*dict)->dict_lock) ); | |
1748 CHECK_POSIX( pthread_rwlock_destroy(&(*dict)->dict_lock) ); | |
1749 | |
1750 free(*dict); | |
1751 *dict = NULL; | |
1752 | |
1753 return 0; | |
1754 } | |
1755 | |
1756 /*******************************************************************************************************/ | |
1757 /*******************************************************************************************************/ | |
1758 /* */ | |
1759 /* Other functions */ | |
1760 /* */ | |
1761 /*******************************************************************************************************/ | |
1762 /*******************************************************************************************************/ | |
1763 | |
1764 /* Iterate a callback on the rules for an object */ | |
1765 int fd_dict_iterate_rules ( struct dict_object *parent, void * data, int (*cb)(void *, struct dict_rule_data *) ) | |
1766 { | |
1767 int ret = 0; | |
1768 struct fd_list * li; | |
1769 | |
1770 TRACE_ENTRY("%p %p %p", parent, data, cb); | |
1771 | |
1772 /* Check parameters */ | |
1773 CHECK_PARAMS( verify_object(parent) ); | |
1774 CHECK_PARAMS( (parent->type == DICT_COMMAND) | |
1775 || ((parent->type == DICT_AVP) && (parent->data.avp.avp_basetype == AVP_TYPE_GROUPED)) ); | |
1776 TRACE_DEBUG (FULL, "Iterating on rules of %s: '%s'.", | |
1777 _OBINFO(parent).name, | |
1778 parent->type == DICT_COMMAND ? | |
1779 parent->data.cmd.cmd_name | |
1780 : parent->data.avp.avp_name); | |
1781 | |
1782 /* Acquire the read lock */ | |
1783 CHECK_POSIX( pthread_rwlock_rdlock(&parent->dico->dict_lock) ); | |
1784 | |
1785 /* go through the list and call the cb on each rule data */ | |
1786 for (li = &(parent->list[2]); li->next != &(parent->list[2]); li = li->next) { | |
1787 ret = (*cb)(data, &(_O(li->next->o)->data.rule)); | |
1788 if (ret != 0) | |
1789 break; | |
1790 } | |
1791 | |
1792 /* Release the lock */ | |
1793 CHECK_POSIX( pthread_rwlock_unlock(&parent->dico->dict_lock) ); | |
1794 | |
1795 return ret; | |
1796 } | |
1797 | |
1798 /* Create the list of vendors. Returns a 0-terminated array, that must be freed after use. Returns NULL on error. */ | |
1799 uint32_t * fd_dict_get_vendorid_list(struct dictionary * dict) | |
1800 { | |
1801 uint32_t * ret = NULL; | |
1802 int i = 0; | |
1803 struct fd_list * li; | |
1804 | |
1805 TRACE_ENTRY(); | |
1806 | |
1807 /* Acquire the read lock */ | |
1808 CHECK_POSIX_DO( pthread_rwlock_rdlock(&dict->dict_lock), return NULL ); | |
1809 | |
1810 /* Allocate an array to contain all the elements */ | |
1811 CHECK_MALLOC_DO( ret = calloc( dict->dict_count[DICT_VENDOR] + 1, sizeof(uint32_t) ), goto out ); | |
1812 | |
1813 /* Copy the vendors IDs */ | |
1814 for (li = dict->dict_vendors.list[0].next; li != &(dict->dict_vendors.list[0]); li = li->next) { | |
1815 ret[i] = _O(li->o)->data.vendor.vendor_id; | |
1816 i++; | |
1817 ASSERT( i <= dict->dict_count[DICT_VENDOR] ); | |
1818 } | |
1819 out: | |
1820 /* Release the lock */ | |
1821 CHECK_POSIX_DO( pthread_rwlock_unlock(&dict->dict_lock), return NULL ); | |
1822 | |
1823 return ret; | |
1824 } | |
1825 | |
1826 /* Return the location of the cb list for an object, after checking its type */ | |
1827 int fd_dict_disp_cb(enum dict_object_type type, struct dict_object *obj, struct fd_list ** cb_list) | |
1828 { | |
1829 TRACE_ENTRY("%d %p %p", type, obj, cb_list); | |
1830 CHECK_PARAMS( verify_object(obj) ); | |
1831 CHECK_PARAMS( _OBINFO(obj).type == type ); | |
1832 CHECK_PARAMS( cb_list ); | |
1833 *cb_list = &obj->disp_cbs; | |
1834 return 0; | |
1835 } | |
1836 | |
1837 int fd_dict_get_error_cmd(struct dictionary * dict, struct dict_object **obj) | |
1838 { | |
1839 TRACE_ENTRY("%p %p", dict, obj); | |
1840 CHECK_PARAMS( dict && (dict->dict_eyec == DICT_EYECATCHER) && obj ); | |
1841 *obj = &dict->dict_cmd_error; | |
1842 return 0; | |
1843 } |