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 |
|
36 /* Messages module. |
|
37 * |
|
38 * This module allows to manipulate the msg and avp structures that represents a Diameter message in memory. |
|
39 */ |
|
40 |
|
41 #include "libfd.h" |
|
42 |
|
43 #include <sys/param.h> |
|
44 |
|
45 /* Type of object */ |
|
46 enum msg_objtype { |
|
47 MSG_MSG = 1, |
|
48 MSG_AVP |
|
49 }; |
|
50 |
|
51 /* Chaining of elements as a free hierarchy */ |
|
52 struct msg_avp_chain { |
|
53 struct fd_list chaining; /* Chaining information at this level. */ |
|
54 struct fd_list children; /* sentinel for the children of this object */ |
|
55 enum msg_objtype type; /* Type of this object, _MSG_MSG or _MSG_AVP */ |
|
56 }; |
|
57 |
|
58 /* Return the chain information from an AVP or MSG. Since it's the first field, we just cast */ |
|
59 #define _C(_x) ((struct msg_avp_chain *)(_x)) |
|
60 |
|
61 /* Some details about chaining: |
|
62 * |
|
63 * A message is made of a header ( msg ) and 0 or more AVPs ( avp ). |
|
64 * The structure is a kind of tree, where some AVPs (grouped AVPs) can contain other AVPs. |
|
65 * Exemple: |
|
66 * msg |
|
67 * |-avp |
|
68 * |-gavp |
|
69 * | |-avp |
|
70 * | |-avp |
|
71 * | \-avp |
|
72 * |-avp |
|
73 * \-avp |
|
74 * |
|
75 * Each item (msg or avp) structure begins with a msg_avp_chain structure. |
|
76 * The element at the top of the hierarchy (msg in our example) has all the fields of its "chaining" equal to the same value. |
|
77 * |
|
78 * All elements at the same level are linked by their "chaining" list. |
|
79 * The "children" list is the sentinel for the lists of children of this element. |
|
80 */ |
|
81 |
|
82 /* The following definitions are used to recognize objects in memory. */ |
|
83 #define MSG_MSG_EYEC (0x11355463) |
|
84 #define MSG_AVP_EYEC (0x11355467) |
|
85 |
|
86 /* The following structure represents an AVP instance. */ |
|
87 struct avp { |
|
88 struct msg_avp_chain avp_chain; /* Chaining information of this AVP */ |
|
89 int avp_eyec; /* Must be equal to MSG_AVP_EYEC */ |
|
90 struct dict_object *avp_model; /* If not NULL, pointer to the dictionary object of this avp */ |
|
91 struct avp_hdr avp_public; /* AVP data that can be managed by other modules */ |
|
92 |
|
93 uint8_t *avp_source; /* If the message was parsed from a buffer, pointer to the AVP data start in the buffer. */ |
|
94 uint8_t *avp_rawdata; /* when the data can not be interpreted, the raw data is copied here. The header is not part of it. */ |
|
95 size_t avp_rawlen; /* The length of the raw buffer. */ |
|
96 union avp_value avp_storage; /* To avoid many alloc/free, store the integer values here and set avp_public.avp_data to &storage */ |
|
97 int avp_mustfreeos; /* 1 if an octetstring is malloc'd in avp_storage and must be freed. */ |
|
98 }; |
|
99 |
|
100 /* Macro to compute the AVP header size */ |
|
101 #define AVPHDRSZ_NOVEND 8 |
|
102 #define AVPHDRSZ_VENDOR 12 |
|
103 #define GETAVPHDRSZ( _flag ) ((_flag & AVP_FLAG_VENDOR) ? AVPHDRSZ_VENDOR : AVPHDRSZ_NOVEND) |
|
104 |
|
105 /* Macro to cast a msg_avp_t */ |
|
106 #define _A(_x) ((struct avp *)(_x)) |
|
107 /* Check the type and eyecatcher */ |
|
108 #define CHECK_AVP(_x) ((_C(_x)->type == MSG_AVP) && (_A(_x)->avp_eyec == MSG_AVP_EYEC)) |
|
109 |
|
110 /* The following structure represents an instance of a message (command and children AVPs). */ |
|
111 struct msg { |
|
112 struct msg_avp_chain msg_chain; /* List of the AVPs in the message */ |
|
113 int msg_eyec; /* Must be equal to MSG_MSG_EYEC */ |
|
114 struct dict_object *msg_model; /* If not NULL, pointer to the dictionary object of this message */ |
|
115 struct msg_hdr msg_public; /* Message data that can be managed by extensions. */ |
|
116 |
|
117 uint8_t *msg_rawbuffer; /* data buffer that was received, saved during fd_msg_parse_buffer and freed in fd_msg_parse_dict */ |
|
118 int msg_routable; /* Is this a routable message? (0: undef, 1: routable, 2: non routable) */ |
|
119 struct msg *msg_query; /* the associated query if the message is a received answer */ |
|
120 struct fd_list *msg_rtlist; /* Routing list for the query */ |
|
121 struct { |
|
122 void (*fct)(void *, struct msg **); |
|
123 void * data; |
|
124 } msg_cb; /* Callback to be called when an answer is received, if not NULL */ |
|
125 char * msg_src_id; /* Diameter Id of the peer this message was received from. This string is malloc'd and must be freed */ |
|
126 uint32_t msg_src_hash; /* Hash of the msg_src_id value */ |
|
127 }; |
|
128 |
|
129 /* Macro to compute the message header size */ |
|
130 #define GETMSGHDRSZ() 20 |
|
131 |
|
132 /* Macro to cast a msg_avp_t */ |
|
133 #define _M(_x) ((struct msg *)(_x)) |
|
134 /* Check the type and eyecatcher */ |
|
135 #define CHECK_MSG(_x) ((_C(_x)->type == MSG_MSG) && (_M(_x)->msg_eyec == MSG_MSG_EYEC)) |
|
136 |
|
137 #define VALIDATE_OBJ(_x) ( (CHECK_MSG(_x)) || (CHECK_AVP(_x)) ) |
|
138 |
|
139 |
|
140 /* Macro to validate a MSGFL_ value */ |
|
141 #define CHECK_MSGFL(_fl) ( ((_fl) & (- (MSGFL_MAX << 1) )) == 0 ) |
|
142 |
|
143 |
|
144 /* initial sizes of AVP from their types, in bytes. */ |
|
145 static int avp_value_sizes[] = { |
|
146 0, /* AVP_TYPE_GROUPED: size is dynamic */ |
|
147 0, /* AVP_TYPE_OCTETSTRING: size is dynamic */ |
|
148 4, /* AVP_TYPE_INTEGER32: size is 32 bits */ |
|
149 8, /* AVP_TYPE_INTEGER64: size is 64 bits */ |
|
150 4, /* AVP_TYPE_UNSIGNED32: size is 32 bits */ |
|
151 8, /* AVP_TYPE_UNSIGNED64: size is 64 bits */ |
|
152 4, /* AVP_TYPE_FLOAT32: size is 32 bits */ |
|
153 8 /* AVP_TYPE_FLOAT64: size is 64 bits */ |
|
154 }; |
|
155 #define CHECK_BASETYPE( _type ) ( ((_type) <= AVP_TYPE_MAX) && ((_type) >= 0) ) |
|
156 #define GETINITIALSIZE( _type, _vend ) (avp_value_sizes[ CHECK_BASETYPE(_type) ? (_type) : 0] + GETAVPHDRSZ(_vend)) |
|
157 |
|
158 /* Forward declaration */ |
|
159 static int parsedict_do_msg(struct dictionary * dict, struct msg * msg, int only_hdr); |
|
160 |
|
161 /***************************************************************************************************************/ |
|
162 /* Creating objects */ |
|
163 |
|
164 /* Initialize a msg_avp_chain structure */ |
|
165 static void init_chain(struct msg_avp_chain * chain, int type) |
|
166 { |
|
167 fd_list_init( &chain->chaining, (void *)chain); |
|
168 fd_list_init( &chain->children, (void *)chain); |
|
169 chain->type = type; |
|
170 } |
|
171 |
|
172 /* Initialize a new AVP object */ |
|
173 static void init_avp ( struct avp * avp ) |
|
174 { |
|
175 TRACE_ENTRY("%p", avp); |
|
176 |
|
177 memset(avp, 0, sizeof(struct avp)); |
|
178 init_chain( &avp->avp_chain, MSG_AVP); |
|
179 avp->avp_eyec = MSG_AVP_EYEC; |
|
180 } |
|
181 |
|
182 /* Initialize a new MSG object */ |
|
183 static void init_msg ( struct msg * msg ) |
|
184 { |
|
185 TRACE_ENTRY("%p", msg); |
|
186 |
|
187 memset(msg, 0, sizeof(struct msg)); |
|
188 init_chain( &msg->msg_chain, MSG_MSG); |
|
189 msg->msg_eyec = MSG_MSG_EYEC; |
|
190 } |
|
191 |
|
192 |
|
193 /* Create a new AVP instance */ |
|
194 int fd_msg_avp_new ( struct dict_object * model, int flags, struct avp ** avp ) |
|
195 { |
|
196 struct avp *new = NULL; |
|
197 |
|
198 TRACE_ENTRY("%p %x %p", model, flags, avp); |
|
199 |
|
200 /* Check the parameters */ |
|
201 CHECK_PARAMS( avp && CHECK_MSGFL(flags) ); |
|
202 |
|
203 if (model) { |
|
204 enum dict_object_type dicttype; |
|
205 CHECK_PARAMS( (fd_dict_gettype(model, &dicttype) == 0) && (dicttype == DICT_AVP) ); |
|
206 } |
|
207 |
|
208 /* Create a new object */ |
|
209 CHECK_MALLOC( new = malloc (sizeof(struct avp)) ); |
|
210 |
|
211 /* Initialize the fields */ |
|
212 init_avp(new); |
|
213 |
|
214 if (model) { |
|
215 struct dict_avp_data dictdata; |
|
216 |
|
217 CHECK_FCT( fd_dict_getval(model, &dictdata) ); |
|
218 |
|
219 new->avp_model = model; |
|
220 new->avp_public.avp_code = dictdata.avp_code; |
|
221 new->avp_public.avp_flags = dictdata.avp_flag_val; |
|
222 new->avp_public.avp_len = GETINITIALSIZE(dictdata.avp_basetype, dictdata.avp_flag_val ); |
|
223 new->avp_public.avp_vendor = dictdata.avp_vendor; |
|
224 } |
|
225 |
|
226 /* The new object is ready, return */ |
|
227 *avp = new; |
|
228 return 0; |
|
229 } |
|
230 |
|
231 /* Create a new message instance */ |
|
232 int fd_msg_new ( struct dict_object * model, int flags, struct msg ** msg ) |
|
233 { |
|
234 struct msg * new = NULL; |
|
235 |
|
236 TRACE_ENTRY("%p %x %p", model, flags, msg); |
|
237 |
|
238 /* Check the parameters */ |
|
239 CHECK_PARAMS( msg && CHECK_MSGFL(flags) ); |
|
240 |
|
241 if (model) { |
|
242 enum dict_object_type dicttype; |
|
243 CHECK_PARAMS( (fd_dict_gettype(model, &dicttype) == 0) && (dicttype == DICT_COMMAND) ); |
|
244 } |
|
245 |
|
246 /* Create a new object */ |
|
247 CHECK_MALLOC( new = malloc (sizeof(struct msg)) ); |
|
248 |
|
249 /* Initialize the fields */ |
|
250 init_msg(new); |
|
251 new->msg_public.msg_version = DIAMETER_VERSION; |
|
252 new->msg_public.msg_length = GETMSGHDRSZ(); /* This will be updated later */ |
|
253 |
|
254 if (model) { |
|
255 struct dictionary *dict; |
|
256 struct dict_cmd_data dictdata; |
|
257 struct dict_object *dictappl; |
|
258 |
|
259 CHECK_FCT( fd_dict_getdict(model, &dict) ); |
|
260 CHECK_FCT( fd_dict_getval(model, &dictdata) ); |
|
261 |
|
262 new->msg_model = model; |
|
263 new->msg_public.msg_flags = dictdata.cmd_flag_val; |
|
264 new->msg_public.msg_code = dictdata.cmd_code; |
|
265 |
|
266 /* Initialize application from the parent, if any */ |
|
267 CHECK_FCT( fd_dict_search( dict, DICT_APPLICATION, APPLICATION_OF_COMMAND, model, &dictappl, 0) ); |
|
268 if (dictappl != NULL) { |
|
269 struct dict_application_data appdata; |
|
270 CHECK_FCT( fd_dict_getval(dictappl, &appdata) ); |
|
271 new->msg_public.msg_appl = appdata.application_id; |
|
272 } |
|
273 } |
|
274 |
|
275 if (flags & MSGFL_ALLOC_ETEID) { |
|
276 new->msg_public.msg_eteid = fd_msg_eteid_get(); |
|
277 } |
|
278 |
|
279 /* The new object is ready, return */ |
|
280 *msg = new; |
|
281 return 0; |
|
282 } |
|
283 |
|
284 /* Create answer from a request */ |
|
285 int fd_msg_new_answer_from_req ( struct dictionary * dict, struct msg ** msg, int flags ) |
|
286 { |
|
287 struct dict_object * model = NULL; |
|
288 struct msg *qry, *ans; |
|
289 |
|
290 TRACE_ENTRY("%p %x", msg, flags); |
|
291 |
|
292 /* Check the parameters */ |
|
293 CHECK_PARAMS( msg ); |
|
294 qry = *msg; |
|
295 CHECK_PARAMS( CHECK_MSG(qry) && (qry->msg_public.msg_flags & CMD_FLAG_REQUEST) ); |
|
296 |
|
297 /* Find the model for the answer */ |
|
298 if (flags & MSGFL_ANSW_ERROR) { |
|
299 /* The model is the generic error format */ |
|
300 CHECK_FCT( fd_dict_get_error_cmd(dict, &model) ); |
|
301 } else { |
|
302 /* The model is the answer corresponding to the query. It supposes that these are defined in the dictionary */ |
|
303 CHECK_FCT_DO( parsedict_do_msg( dict, qry, 1), /* continue */ ); |
|
304 if (qry->msg_model) { |
|
305 CHECK_FCT( fd_dict_search ( dict, DICT_COMMAND, CMD_ANSWER, qry->msg_model, &model, EINVAL ) ); |
|
306 } |
|
307 } |
|
308 |
|
309 /* Create the answer */ |
|
310 CHECK_FCT( fd_msg_new( model, flags, &ans ) ); |
|
311 |
|
312 /* Set informations in the answer as in the query */ |
|
313 ans->msg_public.msg_code = qry->msg_public.msg_code; /* useful for MSGFL_ANSW_ERROR */ |
|
314 ans->msg_public.msg_appl = qry->msg_public.msg_appl; |
|
315 ans->msg_public.msg_eteid = qry->msg_public.msg_eteid; |
|
316 ans->msg_public.msg_hbhid = qry->msg_public.msg_hbhid; |
|
317 |
|
318 /* associate with query */ |
|
319 /* may do CHECK_FCT( msg_answ_associate( *msg, (msg_t *)qry ) ); but this is quicker */ |
|
320 ans->msg_query = qry; |
|
321 |
|
322 /* Done */ |
|
323 *msg = ans; |
|
324 return 0; |
|
325 } |
|
326 |
|
327 /***************************************************************************************************************/ |
|
328 |
|
329 /* Explore a message */ |
|
330 int fd_msg_browse_internal ( msg_or_avp * reference, enum msg_brw_dir dir, msg_or_avp ** found, int * depth ) |
|
331 { |
|
332 struct msg_avp_chain *result = NULL; |
|
333 int diff = 0; |
|
334 struct fd_list *li = NULL; |
|
335 |
|
336 TRACE_ENTRY("%p %d %p %p", reference, dir, found, depth); |
|
337 |
|
338 /* Initialize the "found" result if any */ |
|
339 if (found) |
|
340 *found = NULL; |
|
341 |
|
342 /* Check the parameters */ |
|
343 CHECK_PARAMS( VALIDATE_OBJ(reference) ); |
|
344 |
|
345 TRACE_DEBUG(FCTS, "chaining(%p): nxt:%p prv:%p hea:%p top:%p", |
|
346 &_C(reference)->chaining, |
|
347 _C(reference)->chaining.next, |
|
348 _C(reference)->chaining.prev, |
|
349 _C(reference)->chaining.head, |
|
350 _C(reference)->chaining.o); |
|
351 TRACE_DEBUG(FCTS, "children(%p): nxt:%p prv:%p hea:%p top:%p", |
|
352 &_C(reference)->children, |
|
353 _C(reference)->children.next, |
|
354 _C(reference)->children.prev, |
|
355 _C(reference)->children.head, |
|
356 _C(reference)->children.o); |
|
357 |
|
358 /* Now search */ |
|
359 switch (dir) { |
|
360 case MSG_BRW_NEXT: |
|
361 /* Check the reference is an AVP */ |
|
362 CHECK_PARAMS( _C(reference)->type == MSG_AVP ); |
|
363 |
|
364 li = &_C(reference)->chaining; |
|
365 |
|
366 /* Check if the next element is not the sentinel ( ==> the parent) */ |
|
367 if (li->next != li->head) |
|
368 result = _C(li->next->o); |
|
369 break; |
|
370 |
|
371 case MSG_BRW_PREV: |
|
372 /* Check the reference is an AVP */ |
|
373 CHECK_PARAMS( _C(reference)->type == MSG_AVP ); |
|
374 |
|
375 li = &_C(reference)->chaining; |
|
376 |
|
377 /* Check if the prev element is not the sentinel ( ==> the parent) */ |
|
378 if (li->prev != li->head) |
|
379 result = _C(li->prev->o); |
|
380 break; |
|
381 |
|
382 case MSG_BRW_FIRST_CHILD: |
|
383 li = &_C(reference)->children; |
|
384 if (! FD_IS_LIST_EMPTY(li)) { |
|
385 result = _C(li->next->o); |
|
386 diff = 1; |
|
387 } |
|
388 break; |
|
389 |
|
390 case MSG_BRW_LAST_CHILD: |
|
391 li = &_C(reference)->children; |
|
392 if (! FD_IS_LIST_EMPTY(li)) { |
|
393 result = _C(li->prev->o); |
|
394 diff = 1; |
|
395 } |
|
396 break; |
|
397 |
|
398 case MSG_BRW_PARENT: |
|
399 /* If the object is not chained, it has no parent */ |
|
400 li = &_C(reference)->chaining; |
|
401 if (li != li->head) { |
|
402 /* The sentinel is the parent's children list */ |
|
403 result = _C(li->head->o); |
|
404 diff = -1; |
|
405 } |
|
406 break; |
|
407 |
|
408 case MSG_BRW_WALK: |
|
409 /* First, try to find a child */ |
|
410 li = &_C(reference)->children; |
|
411 if ( ! FD_IS_LIST_EMPTY(li) ) { |
|
412 result = _C(li->next->o); |
|
413 diff = 1; |
|
414 break; |
|
415 } |
|
416 |
|
417 /* Then try to find a "next" at this level or one of the parent's */ |
|
418 li = &_C(reference)->chaining; |
|
419 do { |
|
420 /* If this element has a "next" element, return it */ |
|
421 if (li->next != li->head) { |
|
422 result = _C(li->next->o); |
|
423 break; |
|
424 } |
|
425 /* otherwise, check if we have a parent */ |
|
426 if (li == li->head) { |
|
427 /* no parent */ |
|
428 break; |
|
429 } |
|
430 /* Go to the parent's chaining information and loop */ |
|
431 diff -= 1; |
|
432 li = &_C(li->head->o)->chaining; |
|
433 } while (1); |
|
434 break; |
|
435 |
|
436 default: |
|
437 /* Other directions are invalid */ |
|
438 CHECK_PARAMS( dir = 0 ); |
|
439 } |
|
440 |
|
441 /* Save the found object, if any */ |
|
442 if (found && result) |
|
443 *found = (void *)result; |
|
444 |
|
445 /* Modify the depth according to the walk direction */ |
|
446 if (depth && diff) |
|
447 (*depth) += diff; |
|
448 |
|
449 /* Return ENOENT if found was NULL */ |
|
450 if ((!found) && (!result)) |
|
451 return ENOENT; |
|
452 else |
|
453 return 0; |
|
454 } |
|
455 |
|
456 /* Add an AVP into a tree */ |
|
457 int fd_msg_avp_add ( msg_or_avp * reference, enum msg_brw_dir dir, struct avp *avp) |
|
458 { |
|
459 TRACE_ENTRY("%p %d %p", reference, dir, avp); |
|
460 |
|
461 /* Check the parameters */ |
|
462 CHECK_PARAMS( VALIDATE_OBJ(reference) && CHECK_AVP(avp) && FD_IS_LIST_EMPTY(&avp->avp_chain.chaining) ); |
|
463 |
|
464 /* Now insert */ |
|
465 switch (dir) { |
|
466 case MSG_BRW_NEXT: |
|
467 /* Check the reference is an AVP -- we do not chain AVPs at same level as msgs. */ |
|
468 CHECK_PARAMS( _C(reference)->type == MSG_AVP ); |
|
469 |
|
470 /* Insert the new avp after the reference */ |
|
471 fd_list_insert_after( &_A(reference)->avp_chain.chaining, &avp->avp_chain.chaining ); |
|
472 break; |
|
473 |
|
474 case MSG_BRW_PREV: |
|
475 /* Check the reference is an AVP */ |
|
476 CHECK_PARAMS( _C(reference)->type == MSG_AVP ); |
|
477 |
|
478 /* Insert the new avp before the reference */ |
|
479 fd_list_insert_before( &_A(reference)->avp_chain.chaining, &avp->avp_chain.chaining ); |
|
480 break; |
|
481 |
|
482 case MSG_BRW_FIRST_CHILD: |
|
483 /* Insert the new avp after the children sentinel */ |
|
484 fd_list_insert_after( &_C(reference)->children, &avp->avp_chain.chaining ); |
|
485 break; |
|
486 |
|
487 case MSG_BRW_LAST_CHILD: |
|
488 /* Insert the new avp before the children sentinel */ |
|
489 fd_list_insert_before( &_C(reference)->children, &avp->avp_chain.chaining ); |
|
490 break; |
|
491 |
|
492 default: |
|
493 /* Other directions are invalid */ |
|
494 CHECK_PARAMS( dir = 0 ); |
|
495 } |
|
496 |
|
497 return 0; |
|
498 } |
|
499 |
|
500 /* Search a given AVP model in a message */ |
|
501 int fd_msg_search_avp ( struct msg * msg, struct dict_object * what, struct avp ** avp ) |
|
502 { |
|
503 struct avp * nextavp; |
|
504 struct dict_avp_data dictdata; |
|
505 enum dict_object_type dicttype; |
|
506 |
|
507 TRACE_ENTRY("%p %p %p", msg, what, avp); |
|
508 |
|
509 CHECK_PARAMS( CHECK_MSG(msg) && what ); |
|
510 |
|
511 CHECK_PARAMS( (fd_dict_gettype(what, &dicttype) == 0) && (dicttype == DICT_AVP) ); |
|
512 CHECK_FCT( fd_dict_getval(what, &dictdata) ); |
|
513 |
|
514 /* Loop on all top AVPs */ |
|
515 CHECK_FCT( fd_msg_browse(msg, MSG_BRW_FIRST_CHILD, (void *)&nextavp, NULL) ); |
|
516 while (nextavp) { |
|
517 |
|
518 if ( (nextavp->avp_public.avp_code == dictdata.avp_code) |
|
519 && (nextavp->avp_public.avp_vendor == dictdata.avp_vendor) ) /* always 0 if no V flag */ |
|
520 break; |
|
521 |
|
522 /* Otherwise move to next AVP in the message */ |
|
523 CHECK_FCT( fd_msg_browse(nextavp, MSG_BRW_NEXT, (void *)&nextavp, NULL) ); |
|
524 } |
|
525 |
|
526 if (avp) |
|
527 *avp = nextavp; |
|
528 |
|
529 if (avp && nextavp) { |
|
530 struct dictionary * dict; |
|
531 CHECK_FCT( fd_dict_getdict( what, &dict) ); |
|
532 CHECK_FCT_DO( fd_msg_parse_dict( nextavp, dict ), /* nothing */ ); |
|
533 } |
|
534 |
|
535 if (avp || nextavp) |
|
536 return 0; |
|
537 else |
|
538 return ENOENT; |
|
539 } |
|
540 |
|
541 |
|
542 /***************************************************************************************************************/ |
|
543 /* Deleting objects */ |
|
544 |
|
545 /* Destroy and free an AVP or message */ |
|
546 static int destroy_obj (struct msg_avp_chain * obj ) |
|
547 { |
|
548 TRACE_ENTRY("%p", obj); |
|
549 |
|
550 /* Check the parameter is a valid object */ |
|
551 CHECK_PARAMS( VALIDATE_OBJ(obj) && FD_IS_LIST_EMPTY( &obj->children ) ); |
|
552 |
|
553 /* Unlink this object if needed */ |
|
554 fd_list_unlink( &obj->chaining ); |
|
555 |
|
556 /* Free the octetstring if needed */ |
|
557 if ((obj->type == MSG_AVP) && (_A(obj)->avp_mustfreeos == 1)) { |
|
558 free(_A(obj)->avp_storage.os.data); |
|
559 } |
|
560 /* Free the rawdata if needed */ |
|
561 if ((obj->type == MSG_AVP) && (_A(obj)->avp_rawdata != NULL)) { |
|
562 free(_A(obj)->avp_rawdata); |
|
563 } |
|
564 if ((obj->type == MSG_MSG) && (_M(obj)->msg_rawbuffer != NULL)) { |
|
565 free(_M(obj)->msg_rawbuffer); |
|
566 } |
|
567 |
|
568 if ((obj->type == MSG_MSG) && (_M(obj)->msg_src_id != NULL)) { |
|
569 free(_M(obj)->msg_src_id); |
|
570 } |
|
571 |
|
572 if ((obj->type == MSG_MSG) && (_M(obj)->msg_rtlist != NULL)) { |
|
573 while (! FD_IS_LIST_EMPTY(_M(obj)->msg_rtlist) ) { |
|
574 struct fd_list * li = _M(obj)->msg_rtlist->next; |
|
575 fd_list_unlink(li); |
|
576 free(li); |
|
577 } |
|
578 |
|
579 free(_M(obj)->msg_rtlist); |
|
580 } |
|
581 |
|
582 /* free the object */ |
|
583 free(obj); |
|
584 |
|
585 return 0; |
|
586 } |
|
587 |
|
588 /* Destroy an object and all its children */ |
|
589 static void destroy_tree(struct msg_avp_chain * obj) |
|
590 { |
|
591 struct fd_list *rem; |
|
592 |
|
593 TRACE_ENTRY("%p", obj); |
|
594 |
|
595 /* Destroy any subtree */ |
|
596 while ( (rem = obj->children.next) != &obj->children) |
|
597 destroy_tree(_C(rem->o)); |
|
598 |
|
599 /* Then unlink and destroy the object */ |
|
600 CHECK_FCT_DO( destroy_obj(obj), /* nothing */ ); |
|
601 } |
|
602 |
|
603 /* Free an object and its tree */ |
|
604 int fd_msg_free ( msg_or_avp * object ) |
|
605 { |
|
606 TRACE_ENTRY("%p", object); |
|
607 |
|
608 if (CHECK_MSG(object)) { |
|
609 if (_M(object)->msg_query) { |
|
610 CHECK_FCT( fd_msg_free( _M(object)->msg_query ) ); |
|
611 _M(object)->msg_query = NULL; |
|
612 } |
|
613 } |
|
614 |
|
615 destroy_tree(_C(object)); |
|
616 return 0; |
|
617 } |
|
618 |
|
619 |
|
620 /***************************************************************************************************************/ |
|
621 /* Debug functions: dumping */ |
|
622 |
|
623 /* indent inside an object */ |
|
624 #define INOBJHDR "%*s " |
|
625 #define INOBJHDRVAL indent<0 ? 1 : indent, indent<0 ? "-" : "|" |
|
626 |
|
627 /* Dump a msg_t object */ |
|
628 static void obj_dump_msg (struct msg * msg, int indent ) |
|
629 { |
|
630 int ret = 0; |
|
631 |
|
632 fd_log_debug("%*sMSG: %p\n", INOBJHDRVAL, msg); |
|
633 |
|
634 if (!CHECK_MSG(msg)) { |
|
635 fd_log_debug(INOBJHDR "INVALID!\n", INOBJHDRVAL); |
|
636 return; |
|
637 } |
|
638 |
|
639 if (!msg->msg_model) { |
|
640 |
|
641 fd_log_debug(INOBJHDR "(no model)\n", INOBJHDRVAL); |
|
642 |
|
643 } else { |
|
644 |
|
645 enum dict_object_type dicttype; |
|
646 struct dict_cmd_data dictdata; |
|
647 ret = fd_dict_gettype(msg->msg_model, &dicttype); |
|
648 if (ret || (dicttype != DICT_COMMAND)) { |
|
649 fd_log_debug(INOBJHDR "(invalid model: %d %d)\n", INOBJHDRVAL, ret, dicttype); |
|
650 goto public; |
|
651 } |
|
652 ret = fd_dict_getval(msg->msg_model, &dictdata); |
|
653 if (ret != 0) { |
|
654 fd_log_debug(INOBJHDR "(error getting model data: %s)\n", INOBJHDRVAL, strerror(ret)); |
|
655 goto public; |
|
656 } |
|
657 fd_log_debug(INOBJHDR "model : v/m:" DUMP_CMDFL_str "/" DUMP_CMDFL_str ", %u \"%s\"\n", INOBJHDRVAL, |
|
658 DUMP_CMDFL_val(dictdata.cmd_flag_val), DUMP_CMDFL_val(dictdata.cmd_flag_mask), dictdata.cmd_code, dictdata.cmd_name); |
|
659 } |
|
660 public: |
|
661 fd_log_debug(INOBJHDR "public: V:%d L:%d fl:" DUMP_CMDFL_str " CC:%u A:%d hi:%x ei:%x\n", INOBJHDRVAL, |
|
662 msg->msg_public.msg_version, |
|
663 msg->msg_public.msg_length, |
|
664 DUMP_CMDFL_val(msg->msg_public.msg_flags), |
|
665 msg->msg_public.msg_code, |
|
666 msg->msg_public.msg_appl, |
|
667 msg->msg_public.msg_hbhid, |
|
668 msg->msg_public.msg_eteid |
|
669 ); |
|
670 fd_log_debug(INOBJHDR "intern: rwb:%p rt:%d cb:%p(%p) qry:%p h:%x src:%s\n", |
|
671 INOBJHDRVAL, msg->msg_rawbuffer, msg->msg_routable, msg->msg_cb.fct, msg->msg_cb.data, msg->msg_query, msg->msg_src_hash, msg->msg_src_id?:"(nil)"); |
|
672 } |
|
673 |
|
674 #define DUMP_VALUE(_format, _parms...) fd_log_debug(INOBJHDR "value : t:'%s' v:'" _format "'\n", INOBJHDRVAL, typename, ## _parms); |
|
675 /* Dump an AVP value that is not a constant */ |
|
676 static void dump_basic_type(union avp_value * value, enum dict_avp_basetype type, const char * typename, int indent) |
|
677 { |
|
678 switch (type) { |
|
679 case AVP_TYPE_GROUPED: |
|
680 DUMP_VALUE("%s", "error: grouped AVP with a value!"); |
|
681 break; |
|
682 |
|
683 case AVP_TYPE_OCTETSTRING: |
|
684 { |
|
685 /* Dump only up to 16 bytes of the buffer */ |
|
686 unsigned char buf[8]; |
|
687 memset(buf, 0, sizeof(buf)); |
|
688 memcpy(buf, value->os.data, value->os.len < sizeof(buf) ? value->os.len : sizeof(buf) ); |
|
689 DUMP_VALUE("l:%d, v:%02.2X %02.2X %02.2X %02.2X %02.2X %02.2X %02.2X %02.2X ... ('%-*.*s')", |
|
690 value->os.len, |
|
691 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], |
|
692 value->os.len, value->os.len, value->os.data |
|
693 ); |
|
694 } |
|
695 break; |
|
696 |
|
697 case AVP_TYPE_INTEGER32: |
|
698 DUMP_VALUE("%i",value->i32); |
|
699 break; |
|
700 |
|
701 case AVP_TYPE_INTEGER64: |
|
702 DUMP_VALUE("%lli (0x%llx)",value->i64,value->i64); |
|
703 break; |
|
704 |
|
705 case AVP_TYPE_UNSIGNED32: |
|
706 DUMP_VALUE("%u",value->u32); |
|
707 break; |
|
708 |
|
709 case AVP_TYPE_UNSIGNED64: |
|
710 DUMP_VALUE("%llu",value->u64); |
|
711 break; |
|
712 |
|
713 case AVP_TYPE_FLOAT32: |
|
714 DUMP_VALUE("%f",value->f32); |
|
715 break; |
|
716 |
|
717 case AVP_TYPE_FLOAT64: |
|
718 DUMP_VALUE("%g",value->f64); |
|
719 break; |
|
720 |
|
721 default: |
|
722 DUMP_VALUE("%s %d", "error: invalid type :", type); |
|
723 } |
|
724 } |
|
725 |
|
726 /* Dump an AVP value that is a constant */ |
|
727 #define DUMP_CONST(_format, _parms...) fd_log_debug(INOBJHDR "value : t:'%s' v:'%s' ( " _format " )\n", INOBJHDRVAL, typename, value->enum_name, ## _parms); |
|
728 static void dump_constant_type(struct dict_enumval_data * value, enum dict_avp_basetype type, char * typename, int indent) |
|
729 { |
|
730 switch (type) { |
|
731 case AVP_TYPE_GROUPED: |
|
732 DUMP_CONST("%s", "error: grouped AVP with a constant value!"); |
|
733 break; |
|
734 case AVP_TYPE_OCTETSTRING: |
|
735 DUMP_CONST("%s", "value skipped"); |
|
736 break; |
|
737 |
|
738 case AVP_TYPE_INTEGER32: |
|
739 DUMP_CONST("%i",value->enum_value.i32); |
|
740 break; |
|
741 |
|
742 case AVP_TYPE_INTEGER64: |
|
743 DUMP_CONST("%li",value->enum_value.i64); |
|
744 break; |
|
745 |
|
746 case AVP_TYPE_UNSIGNED32: |
|
747 DUMP_CONST("%u",value->enum_value.u32); |
|
748 break; |
|
749 |
|
750 case AVP_TYPE_UNSIGNED64: |
|
751 DUMP_CONST("%lu",value->enum_value.u64); |
|
752 break; |
|
753 |
|
754 case AVP_TYPE_FLOAT32: |
|
755 DUMP_CONST("%f",value->enum_value.f32); |
|
756 break; |
|
757 |
|
758 case AVP_TYPE_FLOAT64: |
|
759 DUMP_CONST("%g",value->enum_value.f64); |
|
760 break; |
|
761 |
|
762 default: |
|
763 DUMP_CONST("%s %d", "error: invalid type :", type); |
|
764 } |
|
765 } |
|
766 |
|
767 /* Dump an avp object */ |
|
768 static void obj_dump_avp ( struct avp * avp, int indent ) |
|
769 { |
|
770 int ret = 0; |
|
771 enum dict_avp_basetype type = -1; |
|
772 |
|
773 if (!CHECK_AVP(avp)) { |
|
774 fd_log_debug(INOBJHDR "INVALID!\n", INOBJHDRVAL); |
|
775 return; |
|
776 } |
|
777 |
|
778 if (!avp->avp_model) { |
|
779 |
|
780 fd_log_debug(INOBJHDR "(no model)\n", INOBJHDRVAL); |
|
781 |
|
782 } else { |
|
783 |
|
784 enum dict_object_type dicttype; |
|
785 struct dict_avp_data dictdata; |
|
786 ret = fd_dict_gettype(avp->avp_model, &dicttype); |
|
787 if (ret || (dicttype != DICT_AVP)) { |
|
788 fd_log_debug(INOBJHDR "(invalid model: %d %d)\n", INOBJHDRVAL, ret, dicttype); |
|
789 goto public; |
|
790 } |
|
791 ret = fd_dict_getval(avp->avp_model, &dictdata); |
|
792 if (ret != 0) { |
|
793 fd_log_debug(INOBJHDR "(error getting model data: %s)\n", INOBJHDRVAL, strerror(ret)); |
|
794 goto public; |
|
795 } |
|
796 fd_log_debug(INOBJHDR "model : v/m:" DUMP_AVPFL_str "/" DUMP_AVPFL_str ", %12s, %u \"%s\"\n", INOBJHDRVAL, |
|
797 DUMP_AVPFL_val(dictdata.avp_flag_val), |
|
798 DUMP_AVPFL_val(dictdata.avp_flag_mask), |
|
799 type_base_name[dictdata.avp_basetype], |
|
800 dictdata.avp_code, |
|
801 dictdata.avp_name ); |
|
802 type = dictdata.avp_basetype; |
|
803 } |
|
804 public: |
|
805 fd_log_debug(INOBJHDR "public: C:%u fl:" DUMP_AVPFL_str " L:%d V:%u data:@%p\n", INOBJHDRVAL, |
|
806 avp->avp_public.avp_code, |
|
807 DUMP_AVPFL_val(avp->avp_public.avp_flags), |
|
808 avp->avp_public.avp_len, |
|
809 avp->avp_public.avp_vendor, |
|
810 avp->avp_public.avp_value |
|
811 ); |
|
812 /* Dump the value if set */ |
|
813 if (avp->avp_public.avp_value) { |
|
814 if (!avp->avp_model) { |
|
815 fd_log_debug(INOBJHDR "(data set but no model: ERROR)\n", INOBJHDRVAL); |
|
816 } else { |
|
817 /* Try and find a constant name for this value */ |
|
818 struct dictionary * dict = NULL; |
|
819 struct dict_object * avp_type = NULL; |
|
820 struct dict_object * avp_constant = NULL; |
|
821 struct dict_type_data type_data; |
|
822 struct dict_enumval_request request; |
|
823 ret = fd_dict_getdict(avp->avp_model, & dict); |
|
824 if (ret != 0) { |
|
825 dump_basic_type(avp->avp_public.avp_value, type, type_base_name[type], indent); |
|
826 goto end; |
|
827 } |
|
828 ret = fd_dict_search(dict, DICT_TYPE, TYPE_OF_AVP, avp->avp_model, &avp_type, ENOENT); |
|
829 if (ret != 0) { |
|
830 dump_basic_type(avp->avp_public.avp_value, type, type_base_name[type], indent); |
|
831 goto end; |
|
832 } |
|
833 ret = fd_dict_getval(avp_type, &type_data); |
|
834 if (ret != 0) { |
|
835 dump_basic_type(avp->avp_public.avp_value, type, "(error getting type data)", indent); |
|
836 goto end; |
|
837 } |
|
838 if (type_data.type_base != type) { |
|
839 dump_basic_type(avp->avp_public.avp_value, type, "(mismatching type information!)", indent); |
|
840 goto end; |
|
841 } |
|
842 /* Create a query for a constant */ |
|
843 memset(&request, 0, sizeof(request)); |
|
844 request.type_obj = avp_type; |
|
845 memcpy(&request.search.enum_value, avp->avp_public.avp_value, sizeof(union avp_value)); |
|
846 ret = fd_dict_search(dict, DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &request, &avp_constant, ENOENT); |
|
847 if (ret != 0) { |
|
848 dump_basic_type(avp->avp_public.avp_value, type, type_data.type_name, indent); |
|
849 goto end; |
|
850 } |
|
851 /* get the constant's information; we re-use request.search field */ |
|
852 ret = fd_dict_getval(avp_constant, &request.search); |
|
853 if (ret != 0) { |
|
854 dump_basic_type(avp->avp_public.avp_value, type, "(error getting constant data)", indent); |
|
855 goto end; |
|
856 } |
|
857 dump_constant_type(&request.search, type, type_data.type_name, indent); |
|
858 } |
|
859 } |
|
860 end: |
|
861 fd_log_debug(INOBJHDR "intern: src:%p mf:%d raw:%p(%d)\n", INOBJHDRVAL, avp->avp_source, avp->avp_mustfreeos, avp->avp_rawdata, avp->avp_rawlen); |
|
862 } |
|
863 |
|
864 /* Dump a single object content */ |
|
865 static void msg_dump_intern ( int level, msg_or_avp * obj, int indent ) |
|
866 { |
|
867 /* Log only if we are at least at level */ |
|
868 if ( ! TRACE_BOOL(level) ) |
|
869 return; |
|
870 |
|
871 /* Check the object */ |
|
872 if (!VALIDATE_OBJ(obj)) { |
|
873 fd_log_debug( ">>> invalid object (%p)!.\n", obj); |
|
874 return; |
|
875 } |
|
876 |
|
877 /* Dump the object */ |
|
878 switch (_C(obj)->type) { |
|
879 case MSG_AVP: |
|
880 obj_dump_avp ( _A(obj), indent ); |
|
881 break; |
|
882 |
|
883 case MSG_MSG: |
|
884 obj_dump_msg ( _M(obj), indent ); |
|
885 break; |
|
886 |
|
887 default: |
|
888 ASSERT(0); |
|
889 } |
|
890 } |
|
891 |
|
892 /* Dump a message content -- for debug mostly */ |
|
893 void fd_msg_dump_walk ( int level, msg_or_avp *obj ) |
|
894 { |
|
895 msg_or_avp * ref = obj; |
|
896 int indent = 1; |
|
897 |
|
898 TRACE_DEBUG(level, "------ Dumping object %p (w)-------", obj); |
|
899 do { |
|
900 msg_dump_intern ( level, ref, indent ); |
|
901 |
|
902 /* Now find the next object */ |
|
903 CHECK_FCT_DO( fd_msg_browse ( ref, MSG_BRW_WALK, &ref, &indent ), break ); |
|
904 |
|
905 /* dump next object */ |
|
906 } while (ref); |
|
907 |
|
908 TRACE_DEBUG(level, "------ /end of object %p -------", obj); |
|
909 } |
|
910 |
|
911 /* Dump a single object content -- for debug mostly */ |
|
912 void fd_msg_dump_one ( int level, msg_or_avp * obj ) |
|
913 { |
|
914 TRACE_DEBUG(level, "------ Dumping object %p (s)-------", obj); |
|
915 msg_dump_intern ( level, obj, 1 ); |
|
916 TRACE_DEBUG(level, "------ /end of object %p -------", obj); |
|
917 } |
|
918 |
|
919 |
|
920 /***************************************************************************************************************/ |
|
921 /* Simple meta-data management */ |
|
922 |
|
923 /* Retrieve the model of an object */ |
|
924 int fd_msg_model ( msg_or_avp * reference, struct dict_object ** model ) |
|
925 { |
|
926 TRACE_ENTRY("%p %p", reference, model); |
|
927 |
|
928 /* Check the parameters */ |
|
929 CHECK_PARAMS( model && VALIDATE_OBJ(reference) ); |
|
930 |
|
931 /* copy the model reference */ |
|
932 switch (_C(reference)->type) { |
|
933 case MSG_AVP: |
|
934 *model = _A(reference)->avp_model; |
|
935 break; |
|
936 |
|
937 case MSG_MSG: |
|
938 *model = _M(reference)->msg_model; |
|
939 break; |
|
940 |
|
941 default: |
|
942 CHECK_PARAMS(0); |
|
943 } |
|
944 |
|
945 return 0; |
|
946 } |
|
947 |
|
948 /* Retrieve the address of the msg_public field of a message */ |
|
949 int fd_msg_hdr ( struct msg *msg, struct msg_hdr **pdata ) |
|
950 { |
|
951 TRACE_ENTRY("%p %p", msg, pdata); |
|
952 CHECK_PARAMS( CHECK_MSG(msg) && pdata ); |
|
953 |
|
954 *pdata = &msg->msg_public; |
|
955 return 0; |
|
956 } |
|
957 |
|
958 /* Retrieve the address of the avp_public field of an avp */ |
|
959 int fd_msg_avp_hdr ( struct avp *avp, struct avp_hdr **pdata ) |
|
960 { |
|
961 TRACE_ENTRY("%p %p", avp, pdata); |
|
962 CHECK_PARAMS( CHECK_AVP(avp) && pdata ); |
|
963 |
|
964 *pdata = &avp->avp_public; |
|
965 return 0; |
|
966 } |
|
967 |
|
968 /* Associate answers and queries */ |
|
969 int fd_msg_answ_associate( struct msg * answer, struct msg * query ) |
|
970 { |
|
971 TRACE_ENTRY( "%p %p", answer, query ); |
|
972 |
|
973 CHECK_PARAMS( CHECK_MSG(answer) && CHECK_MSG(query) && (answer->msg_query == NULL ) ); |
|
974 |
|
975 answer->msg_query = query; |
|
976 |
|
977 return 0; |
|
978 } |
|
979 |
|
980 int fd_msg_answ_getq( struct msg * answer, struct msg ** query ) |
|
981 { |
|
982 TRACE_ENTRY( "%p %p", answer, query ); |
|
983 |
|
984 CHECK_PARAMS( CHECK_MSG(answer) && query ); |
|
985 |
|
986 *query = answer->msg_query; |
|
987 |
|
988 return 0; |
|
989 } |
|
990 |
|
991 int fd_msg_answ_detach( struct msg * answer ) |
|
992 { |
|
993 TRACE_ENTRY( "%p", answer ); |
|
994 |
|
995 CHECK_PARAMS( CHECK_MSG(answer) ); |
|
996 |
|
997 answer->msg_query = NULL; |
|
998 |
|
999 return 0; |
|
1000 } |
|
1001 |
|
1002 /* Associate / get answer callbacks */ |
|
1003 int fd_msg_anscb_associate( struct msg * msg, void ( *anscb)(void *, struct msg **), void * data ) |
|
1004 { |
|
1005 TRACE_ENTRY("%p %p %p", msg, anscb, data); |
|
1006 |
|
1007 /* Check the parameters */ |
|
1008 CHECK_PARAMS( CHECK_MSG(msg) && anscb ); |
|
1009 CHECK_PARAMS( ! (msg->msg_public.msg_flags & CMD_FLAG_REQUEST) ); |
|
1010 CHECK_PARAMS( msg->msg_cb.fct == NULL ); /* No cb is already registered */ |
|
1011 |
|
1012 /* Associate callback and data with the message, if any */ |
|
1013 msg->msg_cb.fct = anscb; |
|
1014 msg->msg_cb.data = data; |
|
1015 |
|
1016 return 0; |
|
1017 } |
|
1018 |
|
1019 int fd_msg_anscb_get( struct msg * msg, void (**anscb)(void *, struct msg **), void ** data ) |
|
1020 { |
|
1021 TRACE_ENTRY("%p %p %p", msg, anscb, data); |
|
1022 |
|
1023 /* Check the parameters */ |
|
1024 CHECK_PARAMS( CHECK_MSG(msg) && anscb && data ); |
|
1025 |
|
1026 /* Copy the result */ |
|
1027 *anscb = msg->msg_cb.fct; |
|
1028 *data = msg->msg_cb.data; |
|
1029 |
|
1030 return 0; |
|
1031 } |
|
1032 |
|
1033 /* Associate routing lists */ |
|
1034 int fd_msg_rt_associate( struct msg * msg, struct fd_list ** list ) |
|
1035 { |
|
1036 TRACE_ENTRY( "%p %p", msg, list ); |
|
1037 |
|
1038 CHECK_PARAMS( CHECK_MSG(msg) && list ); |
|
1039 |
|
1040 msg->msg_rtlist = *list; |
|
1041 *list = NULL; |
|
1042 |
|
1043 return 0; |
|
1044 } |
|
1045 |
|
1046 int fd_msg_rt_get( struct msg * msg, struct fd_list ** list ) |
|
1047 { |
|
1048 TRACE_ENTRY( "%p %p", msg, list ); |
|
1049 |
|
1050 CHECK_PARAMS( CHECK_MSG(msg) && list ); |
|
1051 |
|
1052 *list = msg->msg_rtlist; |
|
1053 msg->msg_rtlist = NULL; |
|
1054 |
|
1055 return 0; |
|
1056 } |
|
1057 |
|
1058 /* Find if a message is routable */ |
|
1059 int fd_msg_is_routable ( struct msg * msg ) |
|
1060 { |
|
1061 TRACE_ENTRY("%p", msg); |
|
1062 |
|
1063 CHECK_PARAMS_DO( CHECK_MSG(msg), return 0 /* pretend the message is not routable */ ); |
|
1064 |
|
1065 if ( ! msg->msg_routable ) { |
|
1066 /* To define if a message is routable, we rely on the "PXY" command flag yet. */ |
|
1067 msg->msg_routable = (msg->msg_public.msg_flags & CMD_FLAG_PROXIABLE) ? 1 : 2; |
|
1068 |
|
1069 /* Note : the 'real' criteria according to the Diameter I-D is that the message is |
|
1070 routable if and only if the "Destination-Realm" AVP is required by the command ABNF. |
|
1071 We could make a test for this here, but it's more computational work and our test |
|
1072 seems accurate (until proven otherwise...) */ |
|
1073 } |
|
1074 |
|
1075 return (msg->msg_routable == 1) ? 1 : 0; |
|
1076 } |
|
1077 |
|
1078 /* Associate source peer */ |
|
1079 int fd_msg_source_set( struct msg * msg, char * diamid, uint32_t hash, int add_rr, struct dictionary * dict ) |
|
1080 { |
|
1081 TRACE_ENTRY( "%p %p %x %d %p", msg, diamid, hash, add_rr, dict); |
|
1082 |
|
1083 /* Check we received a valid message */ |
|
1084 CHECK_PARAMS( CHECK_MSG(msg) && dict ); |
|
1085 |
|
1086 /* Cleanup any previous source */ |
|
1087 free(msg->msg_src_id); msg->msg_src_id = NULL; |
|
1088 |
|
1089 /* If the request is to cleanup the source, we are done */ |
|
1090 if (diamid == NULL) { |
|
1091 msg->msg_src_hash = 0; |
|
1092 return 0; |
|
1093 } |
|
1094 |
|
1095 /* Otherwise save the new informations */ |
|
1096 CHECK_MALLOC( msg->msg_src_id = strdup(diamid) ); |
|
1097 msg->msg_src_hash = hash; |
|
1098 |
|
1099 if (add_rr) { |
|
1100 struct dict_object *avp_rr_model; |
|
1101 avp_code_t code = AC_ROUTE_RECORD; |
|
1102 struct avp *avp; |
|
1103 union avp_value val; |
|
1104 |
|
1105 /* Find the model for Route-Record in the dictionary */ |
|
1106 CHECK_FCT( fd_dict_search ( dict, DICT_AVP, AVP_BY_CODE, &code, &avp_rr_model, ENOENT) ); |
|
1107 |
|
1108 /* Create the AVP with this model */ |
|
1109 CHECK_FCT( fd_msg_avp_new ( avp_rr_model, 0, &avp ) ); |
|
1110 |
|
1111 /* Set the AVP value with the diameter id */ |
|
1112 memset(&val, 0, sizeof(val)); |
|
1113 val.os.data = (unsigned char *)diamid; |
|
1114 val.os.len = strlen(diamid); |
|
1115 CHECK_FCT( fd_msg_avp_setvalue( avp, &val ) ); |
|
1116 |
|
1117 /* Add the AVP in the message */ |
|
1118 CHECK_FCT( fd_msg_avp_add( msg, MSG_BRW_LAST_CHILD, avp ) ); |
|
1119 } |
|
1120 |
|
1121 /* done */ |
|
1122 return 0; |
|
1123 } |
|
1124 |
|
1125 int fd_msg_source_get( struct msg * msg, char ** diamid, uint32_t *hash ) |
|
1126 { |
|
1127 TRACE_ENTRY( "%p %p %p", msg, diamid, hash); |
|
1128 |
|
1129 /* Check we received valid parameters */ |
|
1130 CHECK_PARAMS( CHECK_MSG(msg) ); |
|
1131 CHECK_PARAMS( diamid ); |
|
1132 |
|
1133 /* Copy the informations */ |
|
1134 *diamid = msg->msg_src_id; |
|
1135 if (hash) |
|
1136 *hash = msg->msg_src_hash; |
|
1137 |
|
1138 /* done */ |
|
1139 return 0; |
|
1140 } |
|
1141 |
|
1142 /******************* End-to-end counter *********************/ |
|
1143 uint32_t fd_eteid; |
|
1144 pthread_mutex_t fd_eteid_lck = PTHREAD_MUTEX_INITIALIZER; |
|
1145 |
|
1146 void fd_msg_eteid_init(void) |
|
1147 { |
|
1148 fd_eteid = ((uint32_t)time(NULL) << 20) | ((uint32_t)lrand48() & ( (1 << 20) - 1 )); |
|
1149 } |
|
1150 |
|
1151 uint32_t fd_msg_eteid_get ( void ) |
|
1152 { |
|
1153 uint32_t ret; |
|
1154 |
|
1155 CHECK_POSIX_DO( pthread_mutex_lock(&fd_eteid_lck), /* continue */ ); |
|
1156 |
|
1157 ret = fd_eteid ++; |
|
1158 |
|
1159 CHECK_POSIX_DO( pthread_mutex_unlock(&fd_eteid_lck), /* continue */ ); |
|
1160 |
|
1161 return ret; |
|
1162 } |
|
1163 |
|
1164 /***************************************************************************************************************/ |
|
1165 /* Manage AVPs values */ |
|
1166 |
|
1167 /* Set the value of an AVP */ |
|
1168 int fd_msg_avp_setvalue ( struct avp *avp, union avp_value *value ) |
|
1169 { |
|
1170 enum dict_avp_basetype type = -1; |
|
1171 |
|
1172 TRACE_ENTRY("%p %p", avp, value); |
|
1173 |
|
1174 /* Check parameter */ |
|
1175 CHECK_PARAMS( CHECK_AVP(avp) && avp->avp_model ); |
|
1176 |
|
1177 /* Retrieve information from the AVP model */ |
|
1178 { |
|
1179 enum dict_object_type dicttype; |
|
1180 struct dict_avp_data dictdata; |
|
1181 |
|
1182 CHECK_PARAMS( (fd_dict_gettype(avp->avp_model, &dicttype) == 0) && (dicttype == DICT_AVP) ); |
|
1183 CHECK_FCT( fd_dict_getval(avp->avp_model, &dictdata) ); |
|
1184 type = dictdata.avp_basetype; |
|
1185 CHECK_PARAMS( type != AVP_TYPE_GROUPED ); |
|
1186 } |
|
1187 |
|
1188 /* First, clean any previous value */ |
|
1189 if (avp->avp_mustfreeos != 0) { |
|
1190 free(avp->avp_storage.os.data); |
|
1191 avp->avp_mustfreeos = 0; |
|
1192 } |
|
1193 |
|
1194 memset(&avp->avp_storage, 0, sizeof(union avp_value)); |
|
1195 |
|
1196 /* If the request was to delete a value: */ |
|
1197 if (!value) { |
|
1198 avp->avp_public.avp_value = NULL; |
|
1199 return 0; |
|
1200 } |
|
1201 |
|
1202 /* Now we have to set the value */ |
|
1203 memcpy(&avp->avp_storage, value, sizeof(union avp_value)); |
|
1204 |
|
1205 /* Copy an octetstring if needed. */ |
|
1206 if (type == AVP_TYPE_OCTETSTRING) { |
|
1207 CHECK_MALLOC( avp->avp_storage.os.data = malloc(value->os.len) ); |
|
1208 avp->avp_mustfreeos = 1; |
|
1209 memcpy(avp->avp_storage.os.data, value->os.data, value->os.len); |
|
1210 } |
|
1211 |
|
1212 /* Set the data pointer of the public part */ |
|
1213 avp->avp_public.avp_value = &avp->avp_storage; |
|
1214 |
|
1215 return 0; |
|
1216 } |
|
1217 |
|
1218 /* Set the value of an AVP, using formatted data */ |
|
1219 int fd_msg_avp_value_encode ( void *data, struct avp *avp ) |
|
1220 { |
|
1221 enum dict_avp_basetype type = -1; |
|
1222 struct dict_type_data type_data; |
|
1223 |
|
1224 TRACE_ENTRY("%p %p", data, avp); |
|
1225 |
|
1226 /* Check parameter */ |
|
1227 CHECK_PARAMS( CHECK_AVP(avp) && avp->avp_model ); |
|
1228 |
|
1229 /* Retrieve information from the AVP model and it's parent type */ |
|
1230 { |
|
1231 enum dict_object_type dicttype; |
|
1232 struct dict_avp_data dictdata; |
|
1233 struct dictionary * dict; |
|
1234 struct dict_object * parenttype = NULL; |
|
1235 |
|
1236 /* First check the base type of the AVP */ |
|
1237 CHECK_PARAMS( (fd_dict_gettype(avp->avp_model, &dicttype) == 0) && (dicttype == DICT_AVP) ); |
|
1238 CHECK_FCT( fd_dict_getval(avp->avp_model, &dictdata) ); |
|
1239 type = dictdata.avp_basetype; |
|
1240 CHECK_PARAMS( type != AVP_TYPE_GROUPED ); |
|
1241 |
|
1242 /* Then retrieve information about the parent's type (= derived type) */ |
|
1243 CHECK_FCT( fd_dict_getdict( avp->avp_model, &dict ) ); |
|
1244 CHECK_FCT( fd_dict_search( dict, DICT_TYPE, TYPE_OF_AVP, avp->avp_model, &parenttype, EINVAL) ); |
|
1245 CHECK_FCT( fd_dict_getval(parenttype, &type_data) ); |
|
1246 if (type_data.type_encode == NULL) { |
|
1247 TRACE_DEBUG(INFO, "This AVP type does not provide a callback to encode formatted data. ENOTSUP."); |
|
1248 return ENOTSUP; |
|
1249 } |
|
1250 } |
|
1251 |
|
1252 /* Ok, now we can encode the value */ |
|
1253 |
|
1254 /* First, clean any previous value */ |
|
1255 if (avp->avp_mustfreeos != 0) { |
|
1256 free(avp->avp_storage.os.data); |
|
1257 avp->avp_mustfreeos = 0; |
|
1258 } |
|
1259 avp->avp_public.avp_value = NULL; |
|
1260 memset(&avp->avp_storage, 0, sizeof(union avp_value)); |
|
1261 |
|
1262 /* Now call the type's callback to encode the data */ |
|
1263 CHECK_FCT( (*type_data.type_encode)(data, &avp->avp_storage) ); |
|
1264 |
|
1265 /* If an octetstring has been allocated, let's mark it to be freed */ |
|
1266 if (type == AVP_TYPE_OCTETSTRING) |
|
1267 avp->avp_mustfreeos = 1; |
|
1268 |
|
1269 /* Set the data pointer of the public part */ |
|
1270 avp->avp_public.avp_value = &avp->avp_storage; |
|
1271 |
|
1272 return 0; |
|
1273 } |
|
1274 |
|
1275 /* Interpret the value of an AVP into formatted data */ |
|
1276 int fd_msg_avp_value_interpret ( struct avp *avp, void *data ) |
|
1277 { |
|
1278 struct dict_type_data type_data; |
|
1279 |
|
1280 TRACE_ENTRY("%p %p", avp, data); |
|
1281 |
|
1282 /* Check parameter */ |
|
1283 CHECK_PARAMS( CHECK_AVP(avp) && avp->avp_model && avp->avp_public.avp_value ); |
|
1284 |
|
1285 /* Retrieve information about the AVP parent type */ |
|
1286 { |
|
1287 struct dictionary * dict; |
|
1288 struct dict_object * parenttype = NULL; |
|
1289 |
|
1290 CHECK_FCT( fd_dict_getdict( avp->avp_model, &dict ) ); |
|
1291 CHECK_FCT( fd_dict_search( dict, DICT_TYPE, TYPE_OF_AVP, avp->avp_model, &parenttype, EINVAL) ); |
|
1292 CHECK_FCT( fd_dict_getval(parenttype, &type_data) ); |
|
1293 if (type_data.type_interpret == NULL) { |
|
1294 TRACE_DEBUG(INFO, "This AVP type does not provide a callback to interpret value in formatted data. ENOTSUP."); |
|
1295 return ENOTSUP; |
|
1296 } |
|
1297 } |
|
1298 |
|
1299 /* Ok, now we can interpret the value */ |
|
1300 |
|
1301 CHECK_FCT( (*type_data.type_interpret)(avp->avp_public.avp_value, data) ); |
|
1302 |
|
1303 return 0; |
|
1304 } |
|
1305 |
|
1306 /***************************************************************************************************************/ |
|
1307 /* Creating a buffer from memory objects (bufferize a struct msg) */ |
|
1308 |
|
1309 /* Following macros are used to store 32 and 64 bit fields into a buffer in network byte order */ |
|
1310 #define PUT_in_buf_32( _u32data, _bufptr ) { \ |
|
1311 *(uint32_t *)(_bufptr) = htonl((uint32_t)(_u32data)); \ |
|
1312 } |
|
1313 #define PUT_in_buf_64( _u64data, _bufptr ) { \ |
|
1314 *(uint64_t *)(_bufptr) = htonll((uint64_t)(_u64data)); \ |
|
1315 } |
|
1316 |
|
1317 /* Write a message header in the buffer */ |
|
1318 static int bufferize_msg(unsigned char * buffer, size_t buflen, size_t * offset, struct msg * msg) |
|
1319 { |
|
1320 TRACE_ENTRY("%p %d %p %p", buffer, buflen, offset, msg); |
|
1321 |
|
1322 if ((buflen - *offset) < GETMSGHDRSZ()) |
|
1323 return ENOSPC; |
|
1324 |
|
1325 if (*offset & 0x3) |
|
1326 return EFAULT; /* We are supposed to start on 32 bit boundaries */ |
|
1327 |
|
1328 PUT_in_buf_32(msg->msg_public.msg_length, buffer + *offset); |
|
1329 buffer[*offset] = msg->msg_public.msg_version; |
|
1330 *offset += 4; |
|
1331 |
|
1332 PUT_in_buf_32(msg->msg_public.msg_code, buffer + *offset); |
|
1333 buffer[*offset] = msg->msg_public.msg_flags; |
|
1334 *offset += 4; |
|
1335 |
|
1336 PUT_in_buf_32(msg->msg_public.msg_appl, buffer + *offset); |
|
1337 *offset += 4; |
|
1338 |
|
1339 PUT_in_buf_32(msg->msg_public.msg_hbhid, buffer + *offset); |
|
1340 *offset += 4; |
|
1341 |
|
1342 PUT_in_buf_32(msg->msg_public.msg_eteid, buffer + *offset); |
|
1343 *offset += 4; |
|
1344 |
|
1345 return 0; |
|
1346 } |
|
1347 |
|
1348 static int bufferize_chain(unsigned char * buffer, size_t buflen, size_t * offset, struct fd_list * list); |
|
1349 |
|
1350 /* Write an AVP in the buffer */ |
|
1351 static int bufferize_avp(unsigned char * buffer, size_t buflen, size_t * offset, struct avp * avp) |
|
1352 { |
|
1353 struct dict_avp_data dictdata; |
|
1354 |
|
1355 TRACE_ENTRY("%p %d %p %p", buffer, buflen, offset, avp); |
|
1356 |
|
1357 if ((buflen - *offset) < avp->avp_public.avp_len) |
|
1358 return ENOSPC; |
|
1359 |
|
1360 /* Write the header */ |
|
1361 PUT_in_buf_32(avp->avp_public.avp_code, buffer + *offset); |
|
1362 *offset += 4; |
|
1363 |
|
1364 PUT_in_buf_32(avp->avp_public.avp_len, buffer + *offset); |
|
1365 buffer[*offset] = avp->avp_public.avp_flags; |
|
1366 *offset += 4; |
|
1367 |
|
1368 if (avp->avp_public.avp_flags & AVP_FLAG_VENDOR) { |
|
1369 PUT_in_buf_32(avp->avp_public.avp_vendor, buffer + *offset); |
|
1370 *offset += 4; |
|
1371 } |
|
1372 |
|
1373 /* Then we must write the AVP value */ |
|
1374 |
|
1375 if (avp->avp_model == NULL) { |
|
1376 /* In the case where we don't know the type of AVP, just copy the raw data or source */ |
|
1377 CHECK_PARAMS( avp->avp_source || avp->avp_rawdata ); |
|
1378 |
|
1379 if ( avp->avp_source != NULL ) { |
|
1380 /* the message was not parsed completely */ |
|
1381 size_t datalen = avp->avp_public.avp_len - GETAVPHDRSZ(avp->avp_public.avp_flags); |
|
1382 memcpy(&buffer[*offset], avp->avp_source, datalen); |
|
1383 *offset += PAD4(datalen); |
|
1384 } else { |
|
1385 /* the content was stored in rawdata */ |
|
1386 memcpy(&buffer[*offset], avp->avp_rawdata, avp->avp_rawlen); |
|
1387 *offset += PAD4(avp->avp_rawlen); |
|
1388 } |
|
1389 |
|
1390 } else { |
|
1391 /* The AVP is defined in the dictionary */ |
|
1392 CHECK_FCT( fd_dict_getval(avp->avp_model, &dictdata) ); |
|
1393 |
|
1394 CHECK_PARAMS( ( dictdata.avp_basetype == AVP_TYPE_GROUPED ) || avp->avp_public.avp_value ); |
|
1395 |
|
1396 switch (dictdata.avp_basetype) { |
|
1397 case AVP_TYPE_GROUPED: |
|
1398 return bufferize_chain(buffer, buflen, offset, &avp->avp_chain.children); |
|
1399 |
|
1400 case AVP_TYPE_OCTETSTRING: |
|
1401 memcpy(&buffer[*offset], avp->avp_public.avp_value->os.data, avp->avp_public.avp_value->os.len); |
|
1402 *offset += PAD4(avp->avp_public.avp_value->os.len); |
|
1403 break; |
|
1404 |
|
1405 case AVP_TYPE_INTEGER32: |
|
1406 PUT_in_buf_32(avp->avp_public.avp_value->i32, buffer + *offset); |
|
1407 *offset += 4; |
|
1408 break; |
|
1409 |
|
1410 case AVP_TYPE_INTEGER64: |
|
1411 PUT_in_buf_64(avp->avp_public.avp_value->i64, buffer + *offset); |
|
1412 *offset += 8; |
|
1413 break; |
|
1414 |
|
1415 case AVP_TYPE_UNSIGNED32: |
|
1416 PUT_in_buf_32(avp->avp_public.avp_value->u32, buffer + *offset); |
|
1417 *offset += 4; |
|
1418 break; |
|
1419 |
|
1420 case AVP_TYPE_UNSIGNED64: |
|
1421 PUT_in_buf_64(avp->avp_public.avp_value->u64, buffer + *offset); |
|
1422 *offset += 8; |
|
1423 break; |
|
1424 |
|
1425 case AVP_TYPE_FLOAT32: |
|
1426 /* We read the f32 as "u32" here to avoid casting to uint make decimals go away. |
|
1427 The alternative would be something like "*(uint32_t *)(& f32)" but |
|
1428 then the compiler complains about strict-aliasing rules. */ |
|
1429 PUT_in_buf_32(avp->avp_public.avp_value->u32, buffer + *offset); |
|
1430 *offset += 4; |
|
1431 break; |
|
1432 |
|
1433 case AVP_TYPE_FLOAT64: |
|
1434 /* Same remark as previously */ |
|
1435 PUT_in_buf_64(avp->avp_public.avp_value->u64, buffer + *offset); |
|
1436 *offset += 8; |
|
1437 break; |
|
1438 |
|
1439 default: |
|
1440 ASSERT(0); |
|
1441 } |
|
1442 } |
|
1443 return 0; |
|
1444 } |
|
1445 |
|
1446 /* Write a chain of AVPs in the buffer */ |
|
1447 static int bufferize_chain(unsigned char * buffer, size_t buflen, size_t * offset, struct fd_list * list) |
|
1448 { |
|
1449 struct fd_list * avpch; |
|
1450 |
|
1451 TRACE_ENTRY("%p %d %p %p", buffer, buflen, offset, list); |
|
1452 |
|
1453 for (avpch = list->next; avpch != list; avpch = avpch->next) { |
|
1454 /* Bufferize the AVP */ |
|
1455 CHECK_FCT( bufferize_avp(buffer, buflen, offset, _A(avpch->o)) ); |
|
1456 } |
|
1457 return 0; |
|
1458 } |
|
1459 |
|
1460 /* Create the message buffer, in network-byte order. We browse the tree twice, this could be probably improved if needed */ |
|
1461 int fd_msg_bufferize ( struct msg * msg, unsigned char ** buffer, size_t * len ) |
|
1462 { |
|
1463 int ret = 0; |
|
1464 unsigned char * buf = NULL; |
|
1465 size_t offset = 0; |
|
1466 |
|
1467 TRACE_ENTRY("%p %p %p", msg, buffer, len); |
|
1468 |
|
1469 /* Check the parameters */ |
|
1470 CHECK_PARAMS( buffer && CHECK_MSG(msg) ); |
|
1471 |
|
1472 /* Update the length. This also checks that all AVP have their values set */ |
|
1473 CHECK_FCT( fd_msg_update_length(msg) ); |
|
1474 |
|
1475 /* Now allocate a buffer to store the message */ |
|
1476 CHECK_MALLOC( buf = malloc(msg->msg_public.msg_length) ); |
|
1477 |
|
1478 /* Clear the memory, so that the padding is always 0 (should not matter) */ |
|
1479 memset(buf, 0, msg->msg_public.msg_length); |
|
1480 |
|
1481 /* Write the message header in the buffer */ |
|
1482 CHECK_FCT_DO( ret = bufferize_msg(buf, msg->msg_public.msg_length, &offset, msg), |
|
1483 { |
|
1484 free(buf); |
|
1485 return ret; |
|
1486 } ); |
|
1487 |
|
1488 /* Write the list of AVPs */ |
|
1489 CHECK_FCT_DO( ret = bufferize_chain(buf, msg->msg_public.msg_length, &offset, &msg->msg_chain.children), |
|
1490 { |
|
1491 free(buf); |
|
1492 return ret; |
|
1493 } ); |
|
1494 |
|
1495 ASSERT(offset == msg->msg_public.msg_length); /* or the msg_update_length is buggy */ |
|
1496 |
|
1497 if (len) { |
|
1498 *len = offset; |
|
1499 } |
|
1500 |
|
1501 *buffer = buf; |
|
1502 return 0; |
|
1503 } |
|
1504 |
|
1505 |
|
1506 /***************************************************************************************************************/ |
|
1507 /* Parsing buffers and building AVP objects lists (not parsing the AVP values which requires dictionary knowledge) */ |
|
1508 |
|
1509 /* Parse a buffer containing a supposed list of AVPs */ |
|
1510 static int parsebuf_list(unsigned char * buf, size_t buflen, struct fd_list * head) |
|
1511 { |
|
1512 size_t offset = 0; |
|
1513 |
|
1514 TRACE_ENTRY("%p %d %p", buf, buflen, head); |
|
1515 |
|
1516 while (offset < buflen) { |
|
1517 struct avp * avp; |
|
1518 |
|
1519 if (buflen - offset <= AVPHDRSZ_NOVEND) { |
|
1520 TRACE_DEBUG(INFO, "truncated buffer: remaining only %d bytes", buflen - offset); |
|
1521 return EBADMSG; |
|
1522 } |
|
1523 |
|
1524 /* Create a new AVP object */ |
|
1525 CHECK_MALLOC( avp = malloc (sizeof(struct avp)) ); |
|
1526 |
|
1527 init_avp(avp); |
|
1528 |
|
1529 /* Initialize the header */ |
|
1530 avp->avp_public.avp_code = ntohl(*(uint32_t *)(buf + offset)); |
|
1531 avp->avp_public.avp_flags = buf[offset + 4]; |
|
1532 avp->avp_public.avp_len = ((uint32_t)buf[offset+5]) << 16 | ((uint32_t)buf[offset+6]) << 8 | ((uint32_t)buf[offset+7]) ; |
|
1533 |
|
1534 offset += 8; |
|
1535 |
|
1536 if (avp->avp_public.avp_flags & AVP_FLAG_VENDOR) { |
|
1537 if (buflen - offset <= 4) { |
|
1538 TRACE_DEBUG(INFO, "truncated buffer: remaining only %d bytes for vendor and data", buflen - offset); |
|
1539 free(avp); |
|
1540 return EBADMSG; |
|
1541 } |
|
1542 avp->avp_public.avp_vendor = ntohl(*(uint32_t *)(buf + offset)); |
|
1543 offset += 4; |
|
1544 } |
|
1545 |
|
1546 /* Check there is enough remaining data in the buffer */ |
|
1547 if (buflen - offset < avp->avp_public.avp_len - GETAVPHDRSZ(avp->avp_public.avp_flags)) { |
|
1548 TRACE_DEBUG(INFO, "truncated buffer: remaining only %d bytes for data, and avp data size is %d", |
|
1549 buflen - offset, |
|
1550 avp->avp_public.avp_len - GETAVPHDRSZ(avp->avp_public.avp_flags)); |
|
1551 free(avp); |
|
1552 return EBADMSG; |
|
1553 } |
|
1554 |
|
1555 /* buf[offset] is now the beginning of the data */ |
|
1556 avp->avp_source = &buf[offset]; |
|
1557 |
|
1558 /* Now eat the data and eventual padding */ |
|
1559 offset += PAD4(avp->avp_public.avp_len - GETAVPHDRSZ(avp->avp_public.avp_flags)); |
|
1560 |
|
1561 /* And insert this avp in the list, at the end */ |
|
1562 fd_list_insert_before( head, &avp->avp_chain.chaining ); |
|
1563 } |
|
1564 |
|
1565 return 0; |
|
1566 } |
|
1567 |
|
1568 /* Create a message object from a buffer. Dictionary objects are not resolved, AVP contents are not interpreted, buffer is saved in msg */ |
|
1569 int fd_msg_parse_buffer ( unsigned char ** buffer, size_t buflen, struct msg ** msg ) |
|
1570 { |
|
1571 struct msg * new = NULL; |
|
1572 int ret = 0; |
|
1573 uint32_t msglen = 0; |
|
1574 unsigned char * buf; |
|
1575 |
|
1576 TRACE_ENTRY("%p %d %p", buffer, buflen, msg); |
|
1577 |
|
1578 CHECK_PARAMS( buffer && *buffer && msg && (buflen >= GETMSGHDRSZ()) ); |
|
1579 buf = *buffer; |
|
1580 *buffer = NULL; |
|
1581 |
|
1582 if ( buf[0] != DIAMETER_VERSION) { |
|
1583 TRACE_DEBUG(INFO, "Invalid version in message: %d (supported: %d)", buf[0], DIAMETER_VERSION); |
|
1584 free(buf); |
|
1585 return EBADMSG; |
|
1586 } |
|
1587 |
|
1588 msglen = ntohl(*(uint32_t *)buf) & 0x00ffffff; |
|
1589 if ( buflen < msglen ) { |
|
1590 TRACE_DEBUG(INFO, "Truncated message (%d / %d)", buflen, msglen ); |
|
1591 free(buf); |
|
1592 return EBADMSG; |
|
1593 } |
|
1594 |
|
1595 /* Create a new object */ |
|
1596 CHECK_MALLOC_DO( new = malloc (sizeof(struct msg)), { free(buf); return ENOMEM; } ); |
|
1597 |
|
1598 /* Initialize the fields */ |
|
1599 init_msg(new); |
|
1600 |
|
1601 /* Now read from the buffer */ |
|
1602 new->msg_public.msg_version = buf[0]; |
|
1603 new->msg_public.msg_length = msglen; |
|
1604 |
|
1605 new->msg_public.msg_flags = buf[4]; |
|
1606 new->msg_public.msg_code = ntohl(*(uint32_t *)(buf+4)) & 0x00ffffff; |
|
1607 |
|
1608 new->msg_public.msg_appl = ntohl(*(uint32_t *)(buf+8)); |
|
1609 new->msg_public.msg_hbhid = ntohl(*(uint32_t *)(buf+12)); |
|
1610 new->msg_public.msg_eteid = ntohl(*(uint32_t *)(buf+16)); |
|
1611 |
|
1612 new->msg_rawbuffer = buf; |
|
1613 |
|
1614 /* Parse the AVP list */ |
|
1615 CHECK_FCT_DO( ret = parsebuf_list(buf + GETMSGHDRSZ(), buflen - GETMSGHDRSZ(), &new->msg_chain.children), { destroy_tree(_C(new)); return ret; } ); |
|
1616 |
|
1617 *msg = new; |
|
1618 return 0; |
|
1619 } |
|
1620 |
|
1621 |
|
1622 /***************************************************************************************************************/ |
|
1623 /* Parsing messages and AVP with dictionary information */ |
|
1624 |
|
1625 /* Resolve dictionary objects of the cmd and avp instances, from their headers. |
|
1626 * When the model is found, the data is interpreted from the avp_source buffer and copied to avp_storage. |
|
1627 * When the model is not found, the data is copied as rawdata and saved (in case we FW the message). |
|
1628 * Therefore, after this function has been called, the source buffer can be freed. |
|
1629 * For command, if the dictionary model is not found, an error is returned. |
|
1630 */ |
|
1631 |
|
1632 static int parsedict_do_chain(struct dictionary * dict, struct fd_list * head, int mandatory); |
|
1633 |
|
1634 /* Process an AVP. If we are not in recheck, the avp_source must be set. */ |
|
1635 static int parsedict_do_avp(struct dictionary * dict, struct avp * avp, int mandatory) |
|
1636 { |
|
1637 struct dict_avp_data dictdata; |
|
1638 |
|
1639 TRACE_ENTRY("%p %p %d", dict, avp, mandatory); |
|
1640 |
|
1641 /* First check we received an AVP as input */ |
|
1642 CHECK_PARAMS( CHECK_AVP(avp) ); |
|
1643 |
|
1644 if (avp->avp_model != NULL) { |
|
1645 /* the model has already been resolved. we do check it is still valid */ |
|
1646 |
|
1647 CHECK_FCT( fd_dict_getval(avp->avp_model, &dictdata) ); |
|
1648 |
|
1649 if ( avp->avp_public.avp_code == dictdata.avp_code ) { |
|
1650 /* Ok then just process the children if any */ |
|
1651 return parsedict_do_chain(dict, &avp->avp_chain.children, mandatory && (avp->avp_public.avp_flags & AVP_FLAG_MANDATORY)); |
|
1652 } else { |
|
1653 /* We just erase the old model */ |
|
1654 avp->avp_model = NULL; |
|
1655 } |
|
1656 } |
|
1657 |
|
1658 /* Now try and resolve the model from the avp code and vendor */ |
|
1659 if (avp->avp_public.avp_flags & AVP_FLAG_VENDOR) { |
|
1660 struct dict_avp_request avpreq; |
|
1661 avpreq.avp_vendor = avp->avp_public.avp_vendor; |
|
1662 avpreq.avp_code = avp->avp_public.avp_code; |
|
1663 CHECK_FCT( fd_dict_search ( dict, DICT_AVP, AVP_BY_CODE_AND_VENDOR, &avpreq, &avp->avp_model, 0)); |
|
1664 } else { |
|
1665 /* no vendor */ |
|
1666 CHECK_FCT( fd_dict_search ( dict, DICT_AVP, AVP_BY_CODE, &avp->avp_public.avp_code, &avp->avp_model, 0)); |
|
1667 } |
|
1668 |
|
1669 /* First handle the case where we have not found this AVP in the dictionary */ |
|
1670 if (!avp->avp_model) { |
|
1671 |
|
1672 if (mandatory && (avp->avp_public.avp_flags & AVP_FLAG_MANDATORY)) { |
|
1673 TRACE_DEBUG(INFO, "Unsupported mandatory AVP found:"); |
|
1674 msg_dump_intern(INFO, avp, 2); |
|
1675 return ENOTSUP; |
|
1676 } |
|
1677 |
|
1678 if (avp->avp_source) { |
|
1679 /* we must copy the data from the source to the internal buffer area */ |
|
1680 CHECK_PARAMS( !avp->avp_rawdata ); |
|
1681 |
|
1682 avp->avp_rawlen = avp->avp_public.avp_len - GETAVPHDRSZ( avp->avp_public.avp_flags ); |
|
1683 |
|
1684 CHECK_MALLOC( avp->avp_rawdata = malloc(avp->avp_rawlen) ); |
|
1685 |
|
1686 memcpy(avp->avp_rawdata, avp->avp_source, avp->avp_rawlen); |
|
1687 avp->avp_source = NULL; |
|
1688 |
|
1689 TRACE_DEBUG(FULL, "Unsupported optional AVP found, raw source data saved in avp_rawdata."); |
|
1690 } |
|
1691 |
|
1692 return 0; |
|
1693 } |
|
1694 |
|
1695 /* Ok we have resolved the object. Now we need to interpret its content. */ |
|
1696 |
|
1697 CHECK_FCT( fd_dict_getval(avp->avp_model, &dictdata) ); |
|
1698 |
|
1699 if (avp->avp_rawdata) { |
|
1700 /* This happens if the dictionary object was defined after the first check */ |
|
1701 avp->avp_source = avp->avp_rawdata; |
|
1702 } |
|
1703 |
|
1704 /* A bit of sanity here... */ |
|
1705 ASSERT(CHECK_BASETYPE(dictdata.avp_basetype)); |
|
1706 |
|
1707 /* Check the size is valid */ |
|
1708 if ((avp_value_sizes[dictdata.avp_basetype] != 0) && |
|
1709 (avp->avp_public.avp_len - GETAVPHDRSZ( avp->avp_public.avp_flags ) != avp_value_sizes[dictdata.avp_basetype])) { |
|
1710 TRACE_DEBUG(INFO, "The AVP size is not suitable for the type. EBADMSG."); |
|
1711 return EBADMSG; |
|
1712 } |
|
1713 |
|
1714 /* Now get the value inside */ |
|
1715 switch (dictdata.avp_basetype) { |
|
1716 case AVP_TYPE_GROUPED: |
|
1717 /* This is a grouped AVP, so let's parse the list of AVPs inside */ |
|
1718 CHECK_FCT( parsebuf_list(avp->avp_source, avp->avp_public.avp_len - GETAVPHDRSZ( avp->avp_public.avp_flags ), &avp->avp_chain.children) ); |
|
1719 |
|
1720 return parsedict_do_chain(dict, &avp->avp_chain.children, mandatory && (avp->avp_public.avp_flags & AVP_FLAG_MANDATORY)); |
|
1721 |
|
1722 case AVP_TYPE_OCTETSTRING: |
|
1723 /* We just have to copy the string into the storage area */ |
|
1724 CHECK_PARAMS( avp->avp_public.avp_len > GETAVPHDRSZ( avp->avp_public.avp_flags ) ); |
|
1725 avp->avp_storage.os.len = avp->avp_public.avp_len - GETAVPHDRSZ( avp->avp_public.avp_flags ); |
|
1726 CHECK_MALLOC( avp->avp_storage.os.data = malloc(avp->avp_storage.os.len) ); |
|
1727 avp->avp_mustfreeos = 1; |
|
1728 memcpy(avp->avp_storage.os.data, avp->avp_source, avp->avp_storage.os.len); |
|
1729 break; |
|
1730 |
|
1731 case AVP_TYPE_INTEGER32: |
|
1732 avp->avp_storage.i32 = (int32_t)ntohl(*(uint32_t *)avp->avp_source); |
|
1733 break; |
|
1734 |
|
1735 case AVP_TYPE_INTEGER64: |
|
1736 avp->avp_storage.i64 = (int64_t)ntohll(*(uint64_t *)avp->avp_source); |
|
1737 break; |
|
1738 |
|
1739 case AVP_TYPE_UNSIGNED32: |
|
1740 case AVP_TYPE_FLOAT32: /* For float, we must not cast, or the value is changed. Instead we use implicit cast by changing the member of the union */ |
|
1741 avp->avp_storage.u32 = (uint32_t)ntohl(*(uint32_t *)avp->avp_source); |
|
1742 break; |
|
1743 |
|
1744 case AVP_TYPE_UNSIGNED64: |
|
1745 case AVP_TYPE_FLOAT64: /* same as 32 bits */ |
|
1746 avp->avp_storage.u64 = (uint64_t)ntohll(*(uint64_t *)avp->avp_source); |
|
1747 break; |
|
1748 |
|
1749 } |
|
1750 |
|
1751 /* The value is now set, so set the data pointer and return 0 */ |
|
1752 avp->avp_public.avp_value = &avp->avp_storage; |
|
1753 return 0; |
|
1754 } |
|
1755 |
|
1756 /* Process a list of AVPs */ |
|
1757 static int parsedict_do_chain(struct dictionary * dict, struct fd_list * head, int mandatory) |
|
1758 { |
|
1759 struct fd_list * avpch; |
|
1760 |
|
1761 TRACE_ENTRY("%p %p %d", dict, head, mandatory); |
|
1762 |
|
1763 /* Sanity check */ |
|
1764 ASSERT ( head == head->head ); |
|
1765 |
|
1766 /* Now process the list */ |
|
1767 for (avpch=head->next; avpch != head; avpch = avpch->next) { |
|
1768 CHECK_FCT( parsedict_do_avp(dict, _A(avpch->o), mandatory) ); |
|
1769 } |
|
1770 |
|
1771 /* Done */ |
|
1772 return 0; |
|
1773 } |
|
1774 |
|
1775 /* Process a msg header. */ |
|
1776 static int parsedict_do_msg(struct dictionary * dict, struct msg * msg, int only_hdr) |
|
1777 { |
|
1778 int ret = 0; |
|
1779 |
|
1780 TRACE_ENTRY("%p %p %d", dict, msg, only_hdr); |
|
1781 |
|
1782 CHECK_PARAMS( CHECK_MSG(msg) ); |
|
1783 |
|
1784 /* Look for the model from the header */ |
|
1785 CHECK_FCT( fd_dict_search ( dict, DICT_COMMAND, |
|
1786 (msg->msg_public.msg_flags & CMD_FLAG_REQUEST) ? CMD_BY_CODE_R : CMD_BY_CODE_A, |
|
1787 &msg->msg_public.msg_code, |
|
1788 &msg->msg_model, ENOTSUP) ); |
|
1789 |
|
1790 if (!only_hdr) { |
|
1791 /* Then process the children */ |
|
1792 ret = parsedict_do_chain(dict, &msg->msg_chain.children, 1); |
|
1793 |
|
1794 /* Free the raw buffer if any */ |
|
1795 if ((ret == 0) && (msg->msg_rawbuffer != NULL)) { |
|
1796 free(msg->msg_rawbuffer); |
|
1797 msg->msg_rawbuffer=NULL; |
|
1798 } |
|
1799 } |
|
1800 |
|
1801 return ret; |
|
1802 } |
|
1803 |
|
1804 int fd_msg_parse_dict ( msg_or_avp * object, struct dictionary * dict ) |
|
1805 { |
|
1806 TRACE_ENTRY("%p %p", dict, object); |
|
1807 |
|
1808 CHECK_PARAMS( VALIDATE_OBJ(object) ); |
|
1809 |
|
1810 switch (_C(object)->type) { |
|
1811 case MSG_MSG: |
|
1812 return parsedict_do_msg(dict, _M(object), 0); |
|
1813 |
|
1814 case MSG_AVP: |
|
1815 return parsedict_do_avp(dict, _A(object), 0); |
|
1816 |
|
1817 default: |
|
1818 ASSERT(0); |
|
1819 } |
|
1820 return EINVAL; |
|
1821 } |
|
1822 |
|
1823 /***************************************************************************************************************/ |
|
1824 /* Parsing messages and AVP for rules (ABNF) compliance */ |
|
1825 |
|
1826 /* This function is used to get stats (first occurence position, last occurence position, number of occurences) |
|
1827 of AVP instances of a given model in a chain of AVP */ |
|
1828 static void parserules_stat_avps( struct dict_object * model_avp, struct fd_list *list, int * count, int * firstpos, int * lastpos) |
|
1829 { |
|
1830 struct fd_list * li; |
|
1831 int curpos = 0; /* The current position in the list */ |
|
1832 |
|
1833 TRACE_ENTRY("%p %p %p %p %p", model_avp, list, count, firstpos, lastpos); |
|
1834 |
|
1835 *count = 0; /* number of instances found */ |
|
1836 *firstpos = 0; /* position of the first instance */ |
|
1837 *lastpos = 0; /* position of the last instance, starting from the end */ |
|
1838 |
|
1839 for (li = list->next; li != list; li = li->next) { |
|
1840 /* Increment the current position counter */ |
|
1841 curpos++; |
|
1842 |
|
1843 /* If we previously saved a "lastpos" information, increment it */ |
|
1844 if (*lastpos != 0) |
|
1845 (*lastpos)++; |
|
1846 |
|
1847 /* Check the type of the next AVP. We can compare the references directly, it is safe. */ |
|
1848 if (_A(li->o)->avp_model == model_avp) { |
|
1849 |
|
1850 /* This AVP is of the type we are searching */ |
|
1851 (*count)++; |
|
1852 |
|
1853 /* If we don't have yet a "firstpos", save it */ |
|
1854 if (*firstpos == 0) |
|
1855 *firstpos = curpos; |
|
1856 |
|
1857 /* Reset the lastpos */ |
|
1858 (*lastpos) = 1; |
|
1859 } |
|
1860 } |
|
1861 } |
|
1862 |
|
1863 /* We use this structure as parameter for the next function */ |
|
1864 struct parserules_data { |
|
1865 struct fd_list * sentinel; /* Sentinel of the list of children AVP */ |
|
1866 struct dict_object * ruleavp; /* If the rule conflicts, save the rule_avp here (we don't have direct access to the rule but it can be searched) */ |
|
1867 }; |
|
1868 |
|
1869 /* Check that a list of AVPs is compliant with a given rule -- will be iterated on the list of rules */ |
|
1870 static int parserules_check_one_rule(void * data, struct dict_rule_data *rule) |
|
1871 { |
|
1872 int ret = 0, count, first, last, min; |
|
1873 struct parserules_data * pr_data = (struct parserules_data *) data; |
|
1874 |
|
1875 TRACE_ENTRY("%p %p", data, rule); |
|
1876 |
|
1877 /* Get statistics of the AVP concerned by this rule in the message instance */ |
|
1878 parserules_stat_avps( rule->rule_avp, pr_data->sentinel, &count, &first, &last); |
|
1879 |
|
1880 if (TRACE_BOOL(ANNOYING)) |
|
1881 { |
|
1882 struct dict_avp_data avpdata; |
|
1883 ret = fd_dict_getval(rule->rule_avp, &avpdata); |
|
1884 |
|
1885 TRACE_DEBUG(ANNOYING, "Checking rule: p:%d(%d) m/M:%2d/%2d. Counted %d (first: %d, last:%d) of AVP '%s'", |
|
1886 rule->rule_position, |
|
1887 rule->rule_order, |
|
1888 rule->rule_min, |
|
1889 rule->rule_max, |
|
1890 count, |
|
1891 first, |
|
1892 last, |
|
1893 (ret == 0) ? avpdata.avp_name : "???" |
|
1894 ); |
|
1895 } |
|
1896 |
|
1897 /* Now check the rule is not conflicting */ |
|
1898 ret = 0; |
|
1899 |
|
1900 /* Check the "min" value */ |
|
1901 if ((min = rule->rule_min) == -1) { |
|
1902 if (rule->rule_position == RULE_OPTIONAL) |
|
1903 min = 0; |
|
1904 else |
|
1905 min = 1; |
|
1906 } |
|
1907 if (count < min) { |
|
1908 TRACE_DEBUG(INFO, "Conflicting rule: the number of occurences (%d) is < the rule min (%d).", count, min); |
|
1909 ret = EBADMSG; |
|
1910 goto end; |
|
1911 } |
|
1912 |
|
1913 /* Check the "max" value */ |
|
1914 if ((rule->rule_max != -1) && (count > rule->rule_max)) { |
|
1915 TRACE_DEBUG(INFO, "Conflicting rule: the number of occurences (%d) is > the rule max (%d).", count, rule->rule_max); |
|
1916 ret = EBADMSG; |
|
1917 goto end; |
|
1918 } |
|
1919 |
|
1920 /* Check the position and order (if relevant) */ |
|
1921 switch (rule->rule_position) { |
|
1922 case RULE_OPTIONAL: |
|
1923 case RULE_REQUIRED: |
|
1924 /* No special position constraints */ |
|
1925 break; |
|
1926 |
|
1927 case RULE_FIXED_HEAD: |
|
1928 /* Since "0*1<fixed>" is a valid rule specifier, we only reject cases where the AVP appears *after* its fixed position */ |
|
1929 if (first > rule->rule_order) { |
|
1930 TRACE_DEBUG(INFO, "Conflicting rule: the FIXED_HEAD AVP appears first in (%d) position, the rule requires (%d).", first, rule->rule_order); |
|
1931 ret = EBADMSG; |
|
1932 goto end; |
|
1933 } |
|
1934 break; |
|
1935 |
|
1936 case RULE_FIXED_TAIL: |
|
1937 /* Since "0*1<fixed>" is a valid rule specifier, we only reject cases where the AVP appears *before* its fixed position */ |
|
1938 if (last > rule->rule_order) { /* We have a ">" here because we count in reverse order (i.e. from the end) */ |
|
1939 TRACE_DEBUG(INFO, "Conflicting rule: the FIXED_TAIL AVP appears last in (%d) position, the rule requires (%d).", last, rule->rule_order); |
|
1940 ret = EBADMSG; |
|
1941 goto end; |
|
1942 } |
|
1943 break; |
|
1944 |
|
1945 default: |
|
1946 /* What is this position ??? */ |
|
1947 ASSERT(0); |
|
1948 ret = ENOTSUP; |
|
1949 } |
|
1950 |
|
1951 /* We've checked all the parameters */ |
|
1952 end: |
|
1953 if (ret == EBADMSG) { |
|
1954 pr_data->ruleavp = rule->rule_avp; |
|
1955 } |
|
1956 |
|
1957 return ret; |
|
1958 } |
|
1959 |
|
1960 /* Check the rules recursively */ |
|
1961 static int parserules_do ( struct dictionary * dict, msg_or_avp * object, struct dict_object ** conflict_rule, int mandatory) |
|
1962 { |
|
1963 int ret = 0; |
|
1964 struct parserules_data data; |
|
1965 struct dict_object * model = NULL; |
|
1966 |
|
1967 TRACE_ENTRY("%p %p %p %d", dict, object, conflict_rule, mandatory); |
|
1968 |
|
1969 /* object has already been checked and dict-parsed when we are called. */ |
|
1970 |
|
1971 /* First, handle the cases where there is no model */ |
|
1972 { |
|
1973 if (CHECK_MSG(object)) { |
|
1974 if ( _M(object)->msg_public.msg_flags & CMD_FLAG_ERROR ) { |
|
1975 /* The case of error messages: the ABNF is different */ |
|
1976 CHECK_FCT( fd_dict_get_error_cmd(dict, &model) ); |
|
1977 } else { |
|
1978 model = _M(object)->msg_model; |
|
1979 } |
|
1980 /* Commands MUST be supported in the dictionary */ |
|
1981 if (model == NULL) { |
|
1982 TRACE_DEBUG(INFO, "Message with no dictionary model. EBADMSG"); |
|
1983 return EBADMSG; |
|
1984 } |
|
1985 } |
|
1986 |
|
1987 /* AVP with the 'M' flag must also be recognized in the dictionary -- except inside an optional grouped AVP */ |
|
1988 if (CHECK_AVP(object) && ((model = _A(object)->avp_model) == NULL)) { |
|
1989 if ( mandatory && (_A(object)->avp_public.avp_flags & AVP_FLAG_MANDATORY)) { |
|
1990 /* Return an error in this case */ |
|
1991 TRACE_DEBUG(INFO, "Mandatory AVP with no dictionary model. EBADMSG"); |
|
1992 return EBADMSG; |
|
1993 } else { |
|
1994 /* We don't know any rule for this object, so assume OK */ |
|
1995 TRACE_DEBUG(FULL, "Unknown informational AVP, ignoring..."); |
|
1996 return 0; |
|
1997 } |
|
1998 } |
|
1999 } |
|
2000 |
|
2001 /* At this point we know "model" is set and points to the object's model */ |
|
2002 |
|
2003 /* If we are an AVP with no children, just return OK */ |
|
2004 if (CHECK_AVP(object)) { |
|
2005 struct dict_avp_data dictdata; |
|
2006 CHECK_FCT( fd_dict_getval(model, &dictdata) ); |
|
2007 if (dictdata.avp_basetype != AVP_TYPE_GROUPED) { |
|
2008 /* This object has no children and no rules */ |
|
2009 return 0; |
|
2010 } |
|
2011 } |
|
2012 |
|
2013 /* If this object has children, first check the rules for all its children */ |
|
2014 { |
|
2015 int is_child_mand = 0; |
|
2016 struct fd_list * ch = NULL; |
|
2017 if ( CHECK_MSG(object) |
|
2018 || (mandatory && (_A(object)->avp_public.avp_flags & AVP_FLAG_MANDATORY)) ) |
|
2019 is_child_mand = 1; |
|
2020 for (ch = _C(object)->children.next; ch != &_C(object)->children; ch = ch->next) { |
|
2021 CHECK_FCT( parserules_do ( dict, _C(ch->o), conflict_rule, is_child_mand ) ); |
|
2022 } |
|
2023 } |
|
2024 |
|
2025 /* Now check all rules of this object */ |
|
2026 data.sentinel = &_C(object)->children; |
|
2027 data.ruleavp = NULL; |
|
2028 ret = fd_dict_iterate_rules ( model, &data, parserules_check_one_rule ); |
|
2029 |
|
2030 /* Save the reference to the eventual conflicting rule; otherwise set to NULL */ |
|
2031 if (conflict_rule && data.ruleavp) { |
|
2032 /* data.ruleavp contains the AVP, and model is the parent */ |
|
2033 struct dict_object * rule = NULL; |
|
2034 struct dict_rule_request req = { model, data.ruleavp }; |
|
2035 |
|
2036 CHECK_FCT_DO( fd_dict_search ( dict, DICT_RULE, RULE_BY_AVP_AND_PARENT, &req, &rule, ENOENT), rule = NULL ); |
|
2037 |
|
2038 *conflict_rule = rule; |
|
2039 } |
|
2040 |
|
2041 return ret; |
|
2042 } |
|
2043 |
|
2044 int fd_msg_parse_rules ( msg_or_avp * object, struct dictionary * dict, struct dict_object ** rule) |
|
2045 { |
|
2046 TRACE_ENTRY("%p %p", object, rule); |
|
2047 |
|
2048 /* Resolve the dictionary objects when missing. This also validates the object. */ |
|
2049 CHECK_FCT( fd_msg_parse_dict ( object, dict ) ); |
|
2050 |
|
2051 /* Call the recursive function */ |
|
2052 return parserules_do ( dict, object, rule, 1 ) ; |
|
2053 } |
|
2054 |
|
2055 /***************************************************************************************************************/ |
|
2056 |
|
2057 /* Compute the lengh of an object and its subtree. */ |
|
2058 int fd_msg_update_length ( msg_or_avp * object ) |
|
2059 { |
|
2060 size_t sz = 0; |
|
2061 struct dict_object * model; |
|
2062 union { |
|
2063 struct dict_cmd_data cmddata; |
|
2064 struct dict_avp_data avpdata; |
|
2065 } dictdata; |
|
2066 |
|
2067 TRACE_ENTRY("%p", object); |
|
2068 |
|
2069 /* Get the model of the object. This also validates the object */ |
|
2070 CHECK_FCT( fd_msg_model ( object, &model ) ); |
|
2071 |
|
2072 /* Get the information of the model */ |
|
2073 if (model) { |
|
2074 CHECK_FCT( fd_dict_getval(model, &dictdata) ); |
|
2075 } else { |
|
2076 /* For unknown AVP, just don't change the size */ |
|
2077 if (_C(object)->type == MSG_AVP) |
|
2078 return 0; |
|
2079 } |
|
2080 |
|
2081 /* Deal with easy cases: AVPs without children */ |
|
2082 if ((_C(object)->type == MSG_AVP) && (dictdata.avpdata.avp_basetype != AVP_TYPE_GROUPED)) { |
|
2083 /* Sanity check */ |
|
2084 ASSERT(FD_IS_LIST_EMPTY(&_A(object)->avp_chain.children)); |
|
2085 |
|
2086 /* Now check that the data is set in the AVP */ |
|
2087 CHECK_PARAMS( _A(object)->avp_public.avp_value ); |
|
2088 |
|
2089 sz = GETAVPHDRSZ( _A(object)->avp_public.avp_flags ); |
|
2090 |
|
2091 switch (dictdata.avpdata.avp_basetype) { |
|
2092 case AVP_TYPE_OCTETSTRING: |
|
2093 sz += _A(object)->avp_public.avp_value->os.len; |
|
2094 break; |
|
2095 |
|
2096 case AVP_TYPE_INTEGER32: |
|
2097 case AVP_TYPE_INTEGER64: |
|
2098 case AVP_TYPE_UNSIGNED32: |
|
2099 case AVP_TYPE_UNSIGNED64: |
|
2100 case AVP_TYPE_FLOAT32: |
|
2101 case AVP_TYPE_FLOAT64: |
|
2102 sz += avp_value_sizes[dictdata.avpdata.avp_basetype]; |
|
2103 break; |
|
2104 |
|
2105 default: |
|
2106 /* Something went wrong... */ |
|
2107 ASSERT(0); |
|
2108 } |
|
2109 } |
|
2110 else /* message or grouped AVP */ |
|
2111 { |
|
2112 struct fd_list * ch = NULL; |
|
2113 |
|
2114 /* First, compute the header size */ |
|
2115 if (_C(object)->type == MSG_AVP) { |
|
2116 sz = GETAVPHDRSZ( _A(object)->avp_public.avp_flags ); |
|
2117 } else { |
|
2118 sz = GETMSGHDRSZ( ); |
|
2119 } |
|
2120 |
|
2121 /* Recurse in all children and update the sz information */ |
|
2122 for (ch = _C(object)->children.next; ch != &_C(object)->children; ch = ch->next) { |
|
2123 CHECK_FCT( fd_msg_update_length ( ch->o ) ); |
|
2124 |
|
2125 /* Add the padded size to the parent */ |
|
2126 sz += PAD4( _A(ch->o)->avp_public.avp_len ); |
|
2127 } |
|
2128 } |
|
2129 |
|
2130 /* When we arrive here, the "sz" variable contains the size to write in the object */ |
|
2131 if (_C(object)->type == MSG_AVP) |
|
2132 _A(object)->avp_public.avp_len = sz; |
|
2133 else |
|
2134 _M(object)->msg_public.msg_length = sz; |
|
2135 |
|
2136 return 0; |
|
2137 } |
|
2138 |
|
2139 /***************************************************************************************************************/ |