Mercurial > hg > freeDiameter
annotate libfreeDiameter/dictionary.c @ 43:2db15632a63d
Added a large part of connection establishment logic, to test
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Wed, 25 Nov 2009 19:07:09 +0900 |
parents | e5af94b04946 |
children | a522268ec95b |
rev | line source |
---|---|
0 | 1 /********************************************************************************************************* |
2 * Software License Agreement (BSD License) * | |
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> * | |
4 * * | |
5 * Copyright (c) 2009, WIDE Project and NICT * | |
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 | |
143 int dict_count[DICT_TYPE_MAX]; /* Number of objects of each type */ | |
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; \ | |
594 for (__li = (sentinel); __li->next != (sentinel); __li = __li->next) { \ | |
595 __cmp = strcmp(__str, _O(__li->next->o)->data. datafield ); \ | |
596 if (__cmp == 0) { \ | |
597 if (result) \ | |
598 *result = _O(__li->next->o); \ | |
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 | |
976 default: | |
977 /* Invalid criteria */ | |
978 CHECK_PARAMS( criteria = 0 ); | |
979 } | |
980 end: | |
981 return ret; | |
982 } | |
983 | |
984 static int search_cmd ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
985 { | |
986 int ret = 0; | |
987 | |
988 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
989 | |
990 switch (criteria) { | |
991 case CMD_BY_NAME: | |
992 /* "what" is a command name */ | |
993 SEARCH_string( what, &dict->dict_cmd_name, cmd.cmd_name, 1); | |
994 break; | |
995 | |
996 case CMD_BY_CODE_R: | |
997 case CMD_BY_CODE_A: | |
998 { | |
999 command_code_t code; | |
1000 uint8_t searchfl = 0; | |
1001 | |
1002 /* The command code that we are searching */ | |
1003 code = *(command_code_t *) what; | |
1004 | |
1005 /* The flag (request or answer) of the command we are searching */ | |
1006 if (criteria == CMD_BY_CODE_R) { | |
1007 searchfl = CMD_FLAG_REQUEST; | |
1008 } | |
1009 | |
1010 /* perform the search */ | |
1011 SEARCH_codefl( code, searchfl, &dict->dict_cmd_code ); | |
1012 } | |
1013 break; | |
1014 | |
1015 case CMD_ANSWER: | |
1016 { | |
1017 /* "what" is a command object of type "request" */ | |
1018 struct dict_object * req = (struct dict_object *) what; | |
1019 struct dict_object * ans = NULL; | |
1020 | |
1021 CHECK_PARAMS( verify_object(req) | |
1022 && (req->type == DICT_COMMAND) | |
1023 && (req->data.cmd.cmd_flag_mask & CMD_FLAG_REQUEST) | |
1024 && (req->data.cmd.cmd_flag_val & CMD_FLAG_REQUEST) ); | |
1025 | |
1026 /* The answer is supposed to be the next element in the list, if it exists */ | |
1027 ans = req->list[1].next->o; | |
1028 if ( ans == NULL ) { | |
1029 TRACE_DEBUG( FULL, "the request was the last element in the list" ); | |
1030 ret = ENOENT; | |
1031 goto end; | |
1032 } | |
1033 | |
1034 /* Now check that the ans element is really the correct one */ | |
1035 if ( (ans->data.cmd.cmd_code != req->data.cmd.cmd_code) | |
1036 || (!(ans->data.cmd.cmd_flag_mask & CMD_FLAG_REQUEST)) | |
1037 || ( ans->data.cmd.cmd_flag_val & CMD_FLAG_REQUEST ) ) { | |
1038 TRACE_DEBUG( FULL, "the answer does not follow the request in the list" ); | |
1039 ret = ENOENT; | |
1040 goto end; | |
1041 } | |
1042 | |
1043 if (result) | |
1044 *result = ans; | |
1045 ret = 0; | |
1046 } | |
1047 break; | |
1048 | |
1049 default: | |
1050 /* Invalid criteria */ | |
1051 CHECK_PARAMS( criteria = 0 ); | |
1052 } | |
1053 end: | |
1054 return ret; | |
1055 } | |
1056 | |
1057 static int search_rule ( struct dictionary * dict, int criteria, void * what, struct dict_object **result ) | |
1058 { | |
1059 int ret = 0; | |
1060 | |
1061 TRACE_ENTRY("%p %d %p %p", dict, criteria, what, result); | |
1062 | |
1063 switch (criteria) { | |
1064 case RULE_BY_AVP_AND_PARENT: | |
1065 { | |
1066 struct dict_object * parent = NULL; | |
1067 struct dict_object * avp = NULL; | |
1068 struct dict_rule_request * _what = (struct dict_rule_request *) what; | |
1069 | |
1070 CHECK_PARAMS( _what | |
1071 && (parent = _what->rule_parent) | |
1072 && (avp = _what->rule_avp ) ); | |
1073 | |
1074 CHECK_PARAMS( verify_object(parent) | |
1075 && ((parent->type == DICT_COMMAND) | |
1076 || ((parent->type == DICT_AVP) && (parent->data.avp.avp_basetype == AVP_TYPE_GROUPED))) ); | |
1077 | |
1078 CHECK_PARAMS( verify_object(avp) && (avp->type == DICT_AVP) ); | |
1079 | |
1080 /* Perform the search */ | |
1081 SEARCH_ruleavpname( avp->data.avp.avp_name, &parent->list[2]); | |
1082 | |
1083 } | |
1084 break; | |
1085 | |
1086 default: | |
1087 /* Invalid criteria */ | |
1088 CHECK_PARAMS( criteria = 0 ); | |
1089 } | |
1090 end: | |
1091 return ret; | |
1092 } | |
1093 | |
1094 /*******************************************************************************************************/ | |
1095 /*******************************************************************************************************/ | |
1096 /* */ | |
1097 /* Dump / debug functions */ | |
1098 /* */ | |
1099 /*******************************************************************************************************/ | |
1100 /*******************************************************************************************************/ | |
1101 /* The following functions are used to debug the module, and allow to print out the content of the dictionary */ | |
1102 static void dump_vendor_data ( void * data ) | |
1103 { | |
1104 struct dict_vendor_data * vendor = (struct dict_vendor_data *)data; | |
1105 | |
1106 fd_log_debug("data: %-6u \"%s\"", vendor->vendor_id, vendor->vendor_name); | |
1107 } | |
1108 static void dump_application_data ( void * data ) | |
1109 { | |
1110 struct dict_application_data * appli = (struct dict_application_data *) data; | |
1111 fd_log_debug("data: %-6u \"%s\"", appli->application_id, appli->application_name); | |
1112 } | |
1113 static void dump_type_data ( void * data ) | |
1114 { | |
1115 struct dict_type_data * type = ( struct dict_type_data * ) data; | |
1116 | |
1117 fd_log_debug("data: %-12s \"%s\"", | |
1118 type_base_name[type->type_base], | |
1119 type->type_name); | |
1120 } | |
1121 static void dump_enumval_data ( struct dict_enumval_data * enumval, enum dict_avp_basetype type ) | |
1122 { | |
1123 const int LEN_MAX = 20; | |
1124 fd_log_debug("data: (%-12s) \"%s\" -> ", type_base_name[type], enumval->enum_name); | |
1125 switch (type) { | |
1126 case AVP_TYPE_OCTETSTRING: | |
1127 { | |
1128 int i, n=LEN_MAX; | |
1129 if (enumval->enum_value.os.len < LEN_MAX) | |
1130 n = enumval->enum_value.os.len; | |
1131 for (i=0; i < n; i++) | |
1132 fd_log_debug("0x%02.2X/'%c' ", enumval->enum_value.os.data[i], ASCII(enumval->enum_value.os.data[i])); | |
1133 if (n == LEN_MAX) | |
1134 fd_log_debug("..."); | |
1135 } | |
1136 break; | |
1137 | |
1138 case AVP_TYPE_INTEGER32: | |
1139 fd_log_debug("%i", enumval->enum_value.i32); | |
1140 break; | |
1141 | |
1142 case AVP_TYPE_INTEGER64: | |
1143 fd_log_debug("%lli", enumval->enum_value.i64); | |
1144 break; | |
1145 | |
1146 case AVP_TYPE_UNSIGNED32: | |
1147 fd_log_debug("%u", enumval->enum_value.u32); | |
1148 break; | |
1149 | |
1150 case AVP_TYPE_UNSIGNED64: | |
1151 fd_log_debug("%llu", enumval->enum_value.u64); | |
1152 break; | |
1153 | |
1154 case AVP_TYPE_FLOAT32: | |
1155 fd_log_debug("%f", enumval->enum_value.f32); | |
1156 break; | |
1157 | |
1158 case AVP_TYPE_FLOAT64: | |
1159 fd_log_debug("%g", enumval->enum_value.f64); | |
1160 break; | |
1161 | |
1162 default: | |
1163 fd_log_debug("??? (ERROR unknown type %d)", type); | |
1164 } | |
1165 } | |
1166 static void dump_avp_data ( void * data ) | |
1167 { | |
1168 struct dict_avp_data * avp = (struct dict_avp_data * ) data; | |
1169 fd_log_debug("data: v/m:" DUMP_AVPFL_str "/" DUMP_AVPFL_str ", %12s, %-6u \"%s\"", | |
1170 DUMP_AVPFL_val(avp->avp_flag_val), | |
1171 DUMP_AVPFL_val(avp->avp_flag_mask), | |
1172 type_base_name[avp->avp_basetype], | |
1173 avp->avp_code, | |
1174 avp->avp_name ); | |
1175 } | |
1176 static void dump_command_data ( void * data ) | |
1177 { | |
1178 struct dict_cmd_data * cmd = (struct dict_cmd_data *) data; | |
1179 fd_log_debug("data: v/m:" DUMP_CMDFL_str "/" DUMP_CMDFL_str ", %-6u \"%s\"", | |
1180 DUMP_CMDFL_val(cmd->cmd_flag_val), DUMP_CMDFL_val(cmd->cmd_flag_mask), cmd->cmd_code, cmd->cmd_name); | |
1181 } | |
1182 static void dump_rule_data ( void * data ) | |
1183 { | |
1184 struct dict_rule_data * rule = (struct dict_rule_data * )data; | |
1185 fd_log_debug("data: pos:%d ord:%d m/M:%2d/%2d avp:\"%s\"", | |
1186 rule->rule_position, | |
1187 rule->rule_order, | |
1188 rule->rule_min, | |
1189 rule->rule_max, | |
1190 rule->rule_avp->data.avp.avp_name); | |
1191 } | |
1192 | |
1193 static void dump_object ( struct dict_object * obj, int parents, int depth, int indent ); | |
1194 | |
1195 static void dump_list ( struct fd_list * sentinel, int parents, int depth, int indent ) | |
1196 { | |
1197 struct fd_list * li = sentinel; | |
1198 /* We don't lock here, the caller must have taken the dictionary lock for reading already */ | |
1199 while (li->next != sentinel) | |
1200 { | |
1201 li = li->next; | |
1202 dump_object( _O(li->o), parents, depth, indent ); | |
1203 } | |
1204 } | |
1205 | |
1206 static void dump_object ( struct dict_object * obj, int parents, int depth, int indent ) | |
1207 { | |
1208 if (obj == NULL) | |
1209 return; | |
1210 | |
1211 if (parents) | |
1212 dump_object (obj->parent, parents-1, 0, indent + 1 ); | |
1213 | |
1214 fd_log_debug("%*s@%p: %s%s (p:%-9p) ", | |
1215 indent, | |
1216 "", | |
1217 obj, | |
1218 verify_object(obj) ? "" : "INVALID ", | |
1219 _OBINFO(obj).name, | |
1220 obj->parent); | |
1221 | |
1222 if (obj->type == DICT_ENUMVAL) | |
1223 dump_enumval_data ( &obj->data.enumval, obj->parent->data.type.type_base ); | |
1224 else | |
1225 _OBINFO(obj).dump_data(&obj->data); | |
1226 | |
1227 fd_log_debug("\n"); | |
1228 | |
1229 if (depth) { | |
1230 int i; | |
1231 for (i=0; i<NB_LISTS_PER_OBJ; i++) { | |
1232 if ((obj->list[i].o == NULL) && (obj->list[i].next != &obj->list[i])) { | |
1233 fd_log_debug("%*s>%p: list[%d]:\n", indent, "", obj, i); | |
1234 dump_list(&obj->list[i], parents, depth - 1, indent + 2); | |
1235 } | |
1236 } | |
1237 } | |
1238 } | |
1239 | |
1240 void fd_dict_dump_object(struct dict_object * obj) | |
1241 { | |
1242 fd_log_debug("Dictionary object %p dump:\n", obj); | |
1243 dump_object( obj, 1, 2, 2 ); | |
1244 } | |
1245 | |
1246 void fd_dict_dump(struct dictionary * dict) | |
1247 { | |
1248 int i; | |
1249 | |
1250 CHECK_PARAMS_DO(dict && (dict->dict_eyec == DICT_EYECATCHER), return); | |
1251 | |
1252 CHECK_POSIX_DO( pthread_rwlock_rdlock( &dict->dict_lock ), /* ignore */ ); | |
1253 | |
1254 fd_log_debug("######################################################\n"); | |
1255 fd_log_debug("###### Dumping vendors, AVPs and related rules #######\n"); | |
1256 | |
1257 dump_object( &dict->dict_vendors, 0, 3, 0 ); | |
1258 | |
1259 fd_log_debug("###### Dumping applications #######\n"); | |
1260 | |
1261 dump_object( &dict->dict_applications, 0, 1, 0 ); | |
1262 | |
1263 fd_log_debug("###### Dumping types #######\n"); | |
1264 | |
1265 dump_list( &dict->dict_types, 0, 2, 0 ); | |
1266 | |
1267 fd_log_debug("###### Dumping commands per name #######\n"); | |
1268 | |
1269 dump_list( &dict->dict_cmd_name, 0, 2, 0 ); | |
1270 | |
1271 fd_log_debug("###### Dumping commands per code and flags #######\n"); | |
1272 | |
1273 dump_list( &dict->dict_cmd_code, 0, 0, 0 ); | |
1274 | |
1275 fd_log_debug("###### Statistics #######\n"); | |
1276 | |
1277 for (i=1; i<=DICT_TYPE_MAX; i++) | |
1278 fd_log_debug(" %5d objects of type %s\n", dict->dict_count[i], dict_obj_info[i].name); | |
1279 | |
1280 fd_log_debug("######################################################\n"); | |
1281 | |
1282 /* Free the rwlock */ | |
1283 CHECK_POSIX_DO( pthread_rwlock_unlock( &dict->dict_lock ), /* ignore */ ); | |
1284 } | |
1285 | |
1286 /*******************************************************************************************************/ | |
1287 /*******************************************************************************************************/ | |
1288 /* */ | |
1289 /* Exported functions */ | |
1290 /* */ | |
1291 /*******************************************************************************************************/ | |
1292 /*******************************************************************************************************/ | |
1293 | |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
1294 /* These are the functions exported outside libfreeDiameter. */ |
0 | 1295 |
1296 /* Get the data associated to an object */ | |
1297 int fd_dict_gettype ( struct dict_object * object, enum dict_object_type * type) | |
1298 { | |
1299 TRACE_ENTRY("%p %p", object, type); | |
1300 | |
1301 CHECK_PARAMS( type && verify_object(object) ); | |
1302 | |
1303 /* Copy the value and return */ | |
1304 *type = object->type; | |
1305 return 0; | |
1306 } | |
1307 | |
1308 int fd_dict_getdict ( struct dict_object * object, struct dictionary ** dict) | |
1309 { | |
1310 TRACE_ENTRY("%p %p", object, dict); | |
1311 | |
1312 CHECK_PARAMS( dict && verify_object(object) ); | |
1313 | |
1314 /* Copy the value and return */ | |
1315 *dict = object->dico; | |
1316 return 0; | |
1317 } | |
1318 | |
1319 | |
1320 /* Get the data associated to an object */ | |
1321 int fd_dict_getval ( struct dict_object * object, void * val) | |
1322 { | |
1323 TRACE_ENTRY("%p %p", object, val); | |
1324 | |
1325 CHECK_PARAMS( val && verify_object(object) ); | |
1326 | |
1327 /* Copy the value and return */ | |
1328 memcpy(val, &object->data, _OBINFO(object).datasize);; | |
1329 return 0; | |
1330 } | |
1331 | |
1332 /* Add a new object in the dictionary */ | |
1333 int fd_dict_new ( struct dictionary * dict, enum dict_object_type type, void * data, struct dict_object * parent, struct dict_object **ref ) | |
1334 { | |
1335 int ret = 0; | |
1336 struct dict_object * new = NULL; | |
1337 struct dict_object * vendor = NULL; | |
1338 | |
1339 TRACE_ENTRY("%p %d(%s) %p %p %p", dict, type, dict_obj_info[CHECK_TYPE(type) ? type : 0].name, data, parent, ref); | |
1340 | |
1341 /* Check parameters */ | |
1342 CHECK_PARAMS( dict && (dict->dict_eyec == DICT_EYECATCHER) && CHECK_TYPE(type) && data ); | |
1343 | |
1344 /* Check the "parent" parameter */ | |
1345 switch (dict_obj_info[type].parent) { | |
1346 case 0: /* parent is forbidden */ | |
1347 CHECK_PARAMS( parent == NULL ); | |
1348 | |
1349 case 1: /* parent is optional */ | |
1350 if (parent == NULL) | |
1351 break; | |
1352 | |
1353 case 2: /* parent is mandatory */ | |
1354 CHECK_PARAMS( verify_object(parent) ); | |
1355 | |
1356 if (type == DICT_RULE ) { /* Special case : grouped AVP or Command parents are allowed */ | |
1357 CHECK_PARAMS( (parent->type == DICT_COMMAND ) | |
1358 || ( (parent->type == DICT_AVP) && (parent->data.avp.avp_basetype == AVP_TYPE_GROUPED ) ) ); | |
1359 } else { | |
1360 CHECK_PARAMS( parent->type == dict_obj_info[type].parenttype ); | |
1361 } | |
1362 } | |
1363 | |
1364 /* For AVP object, we must also check that the "vendor" referenced exists */ | |
1365 if (type == DICT_AVP) { | |
1366 CHECK_FCT_DO( fd_dict_search( dict, DICT_VENDOR, VENDOR_BY_ID, &(((struct dict_avp_data *)data)->avp_vendor), (void*)&vendor, ENOENT ), | |
1367 CHECK_PARAMS( vendor = NULL ) ); | |
1368 | |
1369 /* Also check if a parent is provided, that the type are the same */ | |
1370 if (parent) { | |
1371 CHECK_PARAMS( parent->data.type.type_base == ((struct dict_avp_data *)data)->avp_basetype ); | |
1372 } | |
1373 } | |
1374 | |
1375 /* For RULE object, we must also check that the "avp" referenced exists */ | |
1376 if (type == DICT_RULE) { | |
1377 CHECK_PARAMS( verify_object(((struct dict_rule_data *)data)->rule_avp) ); | |
1378 CHECK_PARAMS( ((struct dict_rule_data *)data)->rule_avp->type == DICT_AVP ); | |
1379 } | |
1380 | |
1381 /* For COMMAND object, check that the 'R' flag is fixed */ | |
1382 if (type == DICT_COMMAND) { | |
1383 CHECK_PARAMS( ((struct dict_cmd_data *)data)->cmd_flag_mask & CMD_FLAG_REQUEST ); | |
1384 } | |
1385 | |
1386 /* Parameters are valid, create the new object */ | |
1387 CHECK_MALLOC( new = malloc(sizeof(struct dict_object)) ); | |
1388 | |
1389 /* Initialize the data of the new object */ | |
1390 init_object(new, type); | |
1391 init_object_data(&new->data, data, type); | |
1392 new->dico = dict; | |
1393 new->parent = parent; | |
1394 | |
1395 /* We will change the dictionary => acquire the write lock */ | |
1396 CHECK_POSIX_DO( ret = pthread_rwlock_wrlock(&dict->dict_lock), goto error_free ); | |
1397 | |
1398 /* Now link the object -- this also checks that no object with same keys already exists */ | |
1399 switch (type) { | |
1400 case DICT_VENDOR: | |
1401 /* A vendor object is linked in the g_dict_vendors.list[0], by their id */ | |
1402 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 ), | |
1403 goto error_unlock ); | |
1404 break; | |
1405 | |
1406 case DICT_APPLICATION: | |
1407 /* An application object is linked in the g_dict_applciations.list[0], by their id */ | |
1408 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 ), | |
1409 goto error_unlock ); | |
1410 break; | |
1411 | |
1412 case DICT_TYPE: | |
1413 /* A type object is linked in g_list_types by its name */ | |
1414 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &dict->dict_types, &new->list[0], (int (*)(void*, void *))order_type_by_name, (void **)ref ), | |
1415 goto error_unlock ); | |
1416 break; | |
1417 | |
1418 case DICT_ENUMVAL: | |
1419 /* A type_enum object is linked in it's parent 'type' object lists 1 and 2 by its name and values */ | |
1420 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &parent->list[1], &new->list[0], (int (*)(void*, void *))order_enum_by_name, (void **)ref ), | |
1421 goto error_unlock ); | |
1422 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &parent->list[2], &new->list[1], (int (*)(void*, void *))order_enum_by_val, (void **)ref ), | |
1423 { fd_list_unlink(&new->list[0]); goto error_unlock; } ); | |
1424 break; | |
1425 | |
1426 case DICT_AVP: | |
1427 /* An avp object is linked in lists 1 and 2 of its vendor, by code and name */ | |
1428 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &vendor->list[1], &new->list[0], (int (*)(void*, void *))order_avp_by_code, (void **)ref ), | |
1429 goto error_unlock ); | |
1430 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &vendor->list[2], &new->list[1], (int (*)(void*, void *))order_avp_by_name, (void **)ref ), | |
1431 { fd_list_unlink(&new->list[0]); goto error_unlock; } ); | |
1432 break; | |
1433 | |
1434 case DICT_COMMAND: | |
1435 /* A command object is linked in g_list_cmd_name and g_list_cmd_code by its name and code */ | |
1436 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &dict->dict_cmd_code, &new->list[1], (int (*)(void*, void *))order_cmd_by_codefl, (void **)ref ), | |
1437 goto error_unlock ); | |
1438 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &dict->dict_cmd_name, &new->list[0], (int (*)(void*, void *))order_cmd_by_name, (void **)ref ), | |
1439 { fd_list_unlink(&new->list[1]); goto error_unlock; } ); | |
1440 break; | |
1441 | |
1442 case DICT_RULE: | |
1443 /* A rule object is linked in list[2] of its parent command or AVP by the name of the AVP it refers */ | |
1444 CHECK_FCT_DO( ret = fd_list_insert_ordered ( &parent->list[2], &new->list[0], (int (*)(void*, void *))order_rule_by_avpn, (void **)ref ), | |
1445 goto error_unlock ); | |
1446 break; | |
1447 | |
1448 default: | |
1449 ASSERT(0); | |
1450 } | |
1451 | |
1452 /* A new object has been created, increment the global counter */ | |
1453 dict->dict_count[type]++; | |
1454 | |
1455 /* Unlock the dictionary */ | |
1456 CHECK_POSIX_DO( ret = pthread_rwlock_unlock(&dict->dict_lock), goto error_free ); | |
1457 | |
1458 /* Save the pointer to the new object */ | |
1459 if (ref) | |
1460 *ref = new; | |
1461 | |
1462 return 0; | |
1463 | |
1464 error_unlock: | |
1465 CHECK_POSIX_DO( pthread_rwlock_unlock(&dict->dict_lock), /* continue */ ); | |
1466 error_free: | |
1467 free(new); | |
1468 return ret; | |
1469 } | |
1470 | |
1471 int fd_dict_search ( struct dictionary * dict, enum dict_object_type type, int criteria, void * what, struct dict_object **result, int retval ) | |
1472 { | |
1473 int ret = 0; | |
1474 | |
1475 TRACE_ENTRY("%p %d(%s) %d %p %p %d", dict, type, dict_obj_info[CHECK_TYPE(type) ? type : 0].name, criteria, what, result, retval); | |
1476 | |
1477 /* Check param */ | |
1478 CHECK_PARAMS( dict && (dict->dict_eyec == DICT_EYECATCHER) && CHECK_TYPE(type) ); | |
1479 | |
1480 /* Lock the dictionary for reading */ | |
1481 CHECK_POSIX( pthread_rwlock_rdlock(&dict->dict_lock) ); | |
1482 | |
1483 /* Now call the type-specific search function */ | |
1484 ret = dict_obj_info[type].search_fct (dict, criteria, what, result); | |
1485 | |
1486 /* Unlock */ | |
1487 CHECK_POSIX( pthread_rwlock_unlock(&dict->dict_lock) ); | |
1488 | |
1489 /* Update the return value as needed */ | |
1490 if ((result != NULL) && (*result == NULL)) | |
1491 ret = retval; | |
1492 | |
1493 return ret; | |
1494 } | |
1495 | |
1496 /*******************************************************************************************************/ | |
1497 /*******************************************************************************************************/ | |
1498 /* */ | |
1499 /* The init/fini functions */ | |
1500 /* */ | |
1501 /*******************************************************************************************************/ | |
1502 /*******************************************************************************************************/ | |
1503 | |
1504 /* Initialize the dictionary */ | |
1505 int fd_dict_init ( struct dictionary ** dict) | |
1506 { | |
1507 struct dictionary * new = NULL; | |
1508 | |
1509 TRACE_ENTRY(""); | |
1510 | |
1511 /* Sanity checks */ | |
1512 ASSERT( (sizeof(type_base_name) / sizeof(type_base_name[0])) == (AVP_TYPE_MAX + 1) ); | |
1513 ASSERT( (sizeof(dict_obj_info) / sizeof(dict_obj_info[0])) == (DICT_TYPE_MAX + 1) ); | |
1514 CHECK_PARAMS(dict); | |
1515 | |
1516 /* Allocate the memory for the dictionary */ | |
1517 CHECK_MALLOC( new = malloc(sizeof(struct dictionary)) ); | |
1518 memset(new, 0, sizeof(struct dictionary)); | |
1519 | |
1520 new->dict_eyec = DICT_EYECATCHER; | |
1521 | |
1522 /* Initialize the lock for the dictionary */ | |
1523 CHECK_POSIX( pthread_rwlock_init(&new->dict_lock, NULL) ); | |
1524 | |
1525 /* Initialize the sentinel for vendors and AVP lists */ | |
1526 init_object( &new->dict_vendors, DICT_VENDOR ); | |
1527 new->dict_vendors.data.vendor.vendor_name = "(no vendor)"; | |
1528 new->dict_vendors.list[0].o = NULL; /* overwrite since element is also sentinel for this list. */ | |
1529 | |
1530 | |
43
2db15632a63d
Added a large part of connection establishment logic, to test
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
7
diff
changeset
|
1531 /* Initialize the sentinel for applications */ |
0 | 1532 init_object( &new->dict_applications, DICT_APPLICATION ); |
1533 new->dict_applications.data.application.application_name = "Diameter Common Messages"; | |
1534 new->dict_applications.list[0].o = NULL; /* overwrite since since element is also sentinel for this list. */ | |
1535 | |
1536 /* Initialize the sentinel for types */ | |
1537 fd_list_init ( &new->dict_types, NULL ); | |
1538 | |
1539 /* Initialize the sentinels for commands */ | |
1540 fd_list_init ( &new->dict_cmd_name, NULL ); | |
1541 fd_list_init ( &new->dict_cmd_code, NULL ); | |
1542 | |
1543 /* Initialize the error command object */ | |
1544 init_object( &new->dict_cmd_error, DICT_COMMAND ); | |
1545 new->dict_cmd_error.data.cmd.cmd_name="(generic error format)"; | |
1546 new->dict_cmd_error.data.cmd.cmd_flag_mask=CMD_FLAG_ERROR | CMD_FLAG_REQUEST | CMD_FLAG_RETRANSMIT; | |
1547 new->dict_cmd_error.data.cmd.cmd_flag_val =CMD_FLAG_ERROR; | |
1548 | |
1549 *dict = new; | |
1550 | |
1551 /* Done */ | |
1552 return 0; | |
1553 } | |
1554 | |
1555 /* Destroy a dictionary */ | |
1556 int fd_dict_fini ( struct dictionary ** dict) | |
1557 { | |
1558 int i; | |
1559 | |
1560 TRACE_ENTRY(""); | |
1561 CHECK_PARAMS( dict && *dict && ((*dict)->dict_eyec == DICT_EYECATCHER) ); | |
1562 | |
1563 /* Acquire the write lock to make sure no other operation is ongoing */ | |
1564 CHECK_POSIX( pthread_rwlock_wrlock(&(*dict)->dict_lock) ); | |
1565 | |
1566 /* Empty all the lists, free the elements */ | |
1567 destroy_list ( &(*dict)->dict_cmd_error.list[2] ); | |
1568 destroy_list ( &(*dict)->dict_cmd_code ); | |
1569 destroy_list ( &(*dict)->dict_cmd_name ); | |
1570 destroy_list ( &(*dict)->dict_types ); | |
1571 for (i=0; i< NB_LISTS_PER_OBJ; i++) { | |
1572 destroy_list ( &(*dict)->dict_applications.list[i] ); | |
1573 destroy_list ( &(*dict)->dict_vendors.list[i] ); | |
1574 } | |
1575 | |
1576 /* Dictionary is empty, now destroy the lock */ | |
1577 CHECK_POSIX( pthread_rwlock_unlock(&(*dict)->dict_lock) ); | |
1578 CHECK_POSIX( pthread_rwlock_destroy(&(*dict)->dict_lock) ); | |
1579 | |
1580 free(*dict); | |
1581 *dict = NULL; | |
1582 | |
1583 return 0; | |
1584 } | |
1585 | |
1586 /*******************************************************************************************************/ | |
1587 /*******************************************************************************************************/ | |
1588 /* */ | |
1589 /* Other functions */ | |
1590 /* */ | |
1591 /*******************************************************************************************************/ | |
1592 /*******************************************************************************************************/ | |
1593 | |
1594 /* Iterate a callback on the rules for an object */ | |
1595 int fd_dict_iterate_rules ( struct dict_object *parent, void * data, int (*cb)(void *, struct dict_rule_data *) ) | |
1596 { | |
1597 int ret = 0; | |
1598 struct fd_list * li; | |
1599 | |
1600 TRACE_ENTRY("%p %p %p", parent, data, cb); | |
1601 | |
1602 /* Check parameters */ | |
1603 CHECK_PARAMS( verify_object(parent) ); | |
1604 CHECK_PARAMS( (parent->type == DICT_COMMAND) | |
1605 || ((parent->type == DICT_AVP) && (parent->data.avp.avp_basetype == AVP_TYPE_GROUPED)) ); | |
1606 TRACE_DEBUG (FULL, "Iterating on rules of %s: '%s'.", | |
1607 _OBINFO(parent).name, | |
1608 parent->type == DICT_COMMAND ? | |
1609 parent->data.cmd.cmd_name | |
1610 : parent->data.avp.avp_name); | |
1611 | |
1612 /* Acquire the read lock */ | |
1613 CHECK_POSIX( pthread_rwlock_rdlock(&parent->dico->dict_lock) ); | |
1614 | |
1615 /* go through the list and call the cb on each rule data */ | |
1616 for (li = &(parent->list[2]); li->next != &(parent->list[2]); li = li->next) { | |
1617 ret = (*cb)(data, &(_O(li->next->o)->data.rule)); | |
1618 if (ret != 0) | |
1619 break; | |
1620 } | |
1621 | |
1622 /* Release the lock */ | |
1623 CHECK_POSIX( pthread_rwlock_unlock(&parent->dico->dict_lock) ); | |
1624 | |
1625 return ret; | |
1626 } | |
1627 | |
1628 /* Create the list of vendors. Returns a 0-terminated array, that must be freed after use. Returns NULL on error. */ | |
1629 uint32_t * fd_dict_get_vendorid_list(struct dictionary * dict) | |
1630 { | |
1631 uint32_t * ret = NULL; | |
1632 int i = 0; | |
1633 struct fd_list * li; | |
1634 | |
1635 TRACE_ENTRY(); | |
1636 | |
1637 /* Acquire the read lock */ | |
1638 CHECK_POSIX_DO( pthread_rwlock_rdlock(&dict->dict_lock), return NULL ); | |
1639 | |
1640 /* Allocate an array to contain all the elements */ | |
1641 CHECK_MALLOC_DO( ret = calloc( dict->dict_count[DICT_VENDOR] + 1, sizeof(uint32_t) ), goto out ); | |
1642 | |
1643 /* Copy the vendors IDs */ | |
1644 for (li = dict->dict_vendors.list[0].next; li != &(dict->dict_vendors.list[0]); li = li->next) { | |
1645 ret[i] = _O(li->o)->data.vendor.vendor_id; | |
1646 i++; | |
1647 ASSERT( i <= dict->dict_count[DICT_VENDOR] ); | |
1648 } | |
1649 out: | |
1650 /* Release the lock */ | |
1651 CHECK_POSIX_DO( pthread_rwlock_unlock(&dict->dict_lock), return NULL ); | |
1652 | |
1653 return ret; | |
1654 } | |
1655 | |
1656 /* Return the location of the cb list for an object, after checking its type */ | |
1657 int fd_dict_disp_cb(enum dict_object_type type, struct dict_object *obj, struct fd_list ** cb_list) | |
1658 { | |
1659 TRACE_ENTRY("%d %p %p", type, obj, cb_list); | |
1660 CHECK_PARAMS( verify_object(obj) ); | |
1661 CHECK_PARAMS( _OBINFO(obj).type == type ); | |
1662 CHECK_PARAMS( cb_list ); | |
1663 *cb_list = &obj->disp_cbs; | |
1664 return 0; | |
1665 } | |
1666 | |
1667 int fd_dict_get_error_cmd(struct dictionary * dict, struct dict_object **obj) | |
1668 { | |
1669 TRACE_ENTRY("%p %p", dict, obj); | |
1670 CHECK_PARAMS( dict && (dict->dict_eyec == DICT_EYECATCHER) && obj ); | |
1671 *obj = &dict->dict_cmd_error; | |
1672 return 0; | |
1673 } |