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