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