comparison libfdproto/messages.c @ 658:f198d16fa7f4

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