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