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