Mercurial > hg > freeDiameter
annotate include/freeDiameter/libfreeDiameter.h @ 1:bafb831ba688
Fix names to proper case for freeDiameter
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Mon, 31 Aug 2009 11:31:10 +0900 |
parents | include/freediameter/libfreediameter.h@13530e1f02e3 |
children | d8ce06172629 |
rev | line source |
---|---|
0 | 1 /********************************************************************************************************* |
2 * Software License Agreement (BSD License) * | |
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> * | |
4 * * | |
5 * Copyright (c) 2009, WIDE Project and NICT * | |
6 * All rights reserved. * | |
7 * * | |
8 * Redistribution and use of this software in source and binary forms, with or without modification, are * | |
9 * permitted provided that the following conditions are met: * | |
10 * * | |
11 * * Redistributions of source code must retain the above * | |
12 * copyright notice, this list of conditions and the * | |
13 * following disclaimer. * | |
14 * * | |
15 * * Redistributions in binary form must reproduce the above * | |
16 * copyright notice, this list of conditions and the * | |
17 * following disclaimer in the documentation and/or other * | |
18 * materials provided with the distribution. * | |
19 * * | |
20 * * Neither the name of the WIDE Project or NICT nor the * | |
21 * names of its contributors may be used to endorse or * | |
22 * promote products derived from this software without * | |
23 * specific prior written permission of WIDE Project and * | |
24 * NICT. * | |
25 * * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * | |
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * | |
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * | |
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * | |
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * | |
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * | |
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * | |
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * | |
34 *********************************************************************************************************/ | |
35 | |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
36 /* This file contains the definitions of functions and types used by the libfreeDiameter library. |
0 | 37 * |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
38 * This library is meant to be used by both the freeDiameter daemon and its extensions. |
0 | 39 * |
40 * It provides the tools to manipulate Diameter messages and related data. | |
41 * | |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
42 * This file should always be included as #include <freeDiameter/libfreeDiameter.h> |
0 | 43 */ |
44 | |
45 #ifndef _LIBFREEDIAMETER_H | |
46 #define _LIBFREEDIAMETER_H | |
47 | |
48 #ifndef FD_IS_CONFIG | |
1
bafb831ba688
Fix names to proper case for freeDiameter
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
0
diff
changeset
|
49 #error "You must include 'freeDiameter-host.h' before this file." |
0 | 50 #endif /* FD_IS_CONFIG */ |
51 | |
52 #include <pthread.h> | |
53 #include <string.h> | |
54 #include <assert.h> | |
55 #include <errno.h> | |
56 #include <arpa/inet.h> | |
57 #include <sys/socket.h> | |
58 #include <netdb.h> | |
59 #include <stdio.h> | |
60 #include <stdlib.h> | |
61 #include <unistd.h> | |
62 | |
63 /*============================================================*/ | |
64 /* DEBUG */ | |
65 /*============================================================*/ | |
66 #ifndef ASSERT | |
67 #define ASSERT(x) assert(x) | |
68 #endif /* ASSERT */ | |
69 | |
70 /* | |
71 * FUNCTION: fd_log_debug | |
72 * | |
73 * PARAMETERS: | |
74 * format : Same format string as in the printf function | |
75 * ... : Same list as printf | |
76 * | |
77 * DESCRIPTION: | |
78 * Log internal information for use of developpers only. | |
79 * The format and arguments may contain UTF-8 encoded data. The | |
80 * output medium (file or console) is expected to support this encoding. | |
81 * | |
82 * This function assumes that a global mutex called "fd_log_lock" exists | |
83 * in the address space of the current process. | |
84 * | |
85 * RETURN VALUE: | |
86 * None. | |
87 */ | |
88 void fd_log_debug ( char * format, ... ); | |
89 extern pthread_mutex_t fd_log_lock; | |
90 | |
91 /* | |
92 * FUNCTION: fd_log_threadname | |
93 * | |
94 * PARAMETERS: | |
95 * name : \0-terminated string containing a name to identify the current thread. | |
96 * | |
97 * DESCRIPTION: | |
98 * Name the current thread, useful for debugging multi-threaded problems. | |
99 * | |
100 * This function assumes that a global thread-specific key called "fd_log_thname" exists | |
101 * in the address space of the current process. | |
102 * | |
103 * RETURN VALUE: | |
104 * None. | |
105 */ | |
106 void fd_log_threadname ( char * name ); | |
107 extern pthread_key_t fd_log_thname; | |
108 | |
109 /* | |
110 * FUNCTION: fd_log_time | |
111 * | |
112 * PARAMETERS: | |
113 * buf : An array where the time must be stored | |
114 * len : size of the buffer | |
115 * | |
116 * DESCRIPTION: | |
117 * Writes the current timestamp (in human readable format) in a buffer. | |
118 * | |
119 * RETURN VALUE: | |
120 * pointer to buf. | |
121 */ | |
122 char * fd_log_time ( char * buf, size_t len ); | |
123 | |
124 /************************** DEBUG MACROS ************************************/ | |
125 | |
126 /* levels definitions */ | |
127 #define NONE 0 /* Display no debug message */ | |
128 #define INFO 1 /* Display errors only */ | |
129 #define FULL 2 /* Display additional information to follow code execution */ | |
130 #define ANNOYING 4 /* Very verbose, for example in loops */ | |
131 #define FCTS 6 /* Display entry parameters of most functions */ | |
132 #define CALL 9 /* Display calls to most functions (with CHECK macros) */ | |
133 | |
134 /* Default level is INFO */ | |
135 #ifndef TRACE_LEVEL | |
136 #define TRACE_LEVEL INFO | |
137 #endif /* TRACE_LEVEL */ | |
138 | |
139 /* The level of the file being compiled */ | |
140 static int local_debug_level = TRACE_LEVEL; | |
141 | |
142 /* helper macros (pre-processor hacks) */ | |
143 #define __str( arg ) #arg | |
144 #define _stringize( arg ) __str( arg ) | |
145 #define __agr( arg1, arg2 ) arg1 ## arg2 | |
146 #define _aggregate( arg1, arg2 ) __agr( arg1, arg2 ) | |
147 | |
148 /* Some portability tricks to get nice function name in __PRETTY_FUNCTION__ */ | |
149 #if __STDC_VERSION__ < 199901L | |
150 # if __GNUC__ >= 2 | |
151 # define __func__ __FUNCTION__ | |
152 # else /* __GNUC__ >= 2 */ | |
153 # define __func__ "<unknown>" | |
154 # endif /* __GNUC__ >= 2 */ | |
155 #endif /* __STDC_VERSION__ < 199901L */ | |
156 #ifndef __PRETTY_FUNCTION__ | |
157 #define __PRETTY_FUNCTION__ __func__ | |
158 #endif /* __PRETTY_FUNCTION__ */ | |
159 | |
160 /* Boolean for tracing at a certain level */ | |
161 #define TRACE_BOOL(_level_) ( (_level_) <= local_debug_level ) | |
162 | |
163 /* The general debug macro, each call results in two lines of debug messages */ | |
164 #define TRACE_DEBUG(level,format,args... ) { \ | |
165 if ( TRACE_BOOL(level) ) { \ | |
166 char __buf[25]; \ | |
167 char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed"); \ | |
168 fd_log_debug("\t | th:%-30s\t%s\tin %s@%s:%d\n" \ | |
169 "\t%s|%*s" format "\n", \ | |
170 __thn, fd_log_time(__buf, sizeof(__buf)), __PRETTY_FUNCTION__, __FILE__, __LINE__, \ | |
171 (level < FULL)?"@":" ",level, "", ## args); \ | |
172 } \ | |
173 } | |
174 | |
175 /* Helper for function entry */ | |
176 #define TRACE_ENTRY(_format,_args... ) \ | |
177 TRACE_DEBUG(FCTS, "->%s (" #_args ") = (" _format ") >", __PRETTY_FUNCTION__, ##_args ); | |
178 | |
179 /* Helper for debugging by adding traces */ | |
180 #define TRACE_HERE() \ | |
181 TRACE_DEBUG(INFO, " -- debug checkpoint -- "); | |
182 | |
183 /* Helper for tracing the CHECK_* macros bellow */ | |
184 #define TRACE_DEBUG_ALL( str ) \ | |
185 TRACE_DEBUG(CALL, str ); | |
186 | |
187 | |
188 /* Macros to check a return value and branch out in case of error. | |
189 * These macro must be used only when errors are highly improbable, not for expected errors. | |
190 */ | |
191 | |
192 /* Check the return value of a system function and execute fallback in case of error */ | |
193 #define CHECK_SYS_DO( __call__, __fallback__ ) { \ | |
194 int __ret__; \ | |
195 TRACE_DEBUG_ALL( "Check SYS: " #__call__ ); \ | |
196 __ret__ = (__call__); \ | |
197 if (__ret__ < 0) { \ | |
198 int __err__ = errno; /* We may handle EINTR here */ \ | |
199 TRACE_DEBUG(NONE, "ERROR: in '" #__call__ "' :\t%s", strerror(__err__));\ | |
200 __fallback__; \ | |
201 } \ | |
202 } | |
203 /* Check the return value of a system function, return error code on error */ | |
204 #define CHECK_SYS( __call__ ) { \ | |
205 int __ret__; \ | |
206 TRACE_DEBUG_ALL( "Check SYS: " #__call__ ); \ | |
207 __ret__ = (__call__); \ | |
208 if (__ret__ < 0) { \ | |
209 int __err__ = errno; /* We may handle EINTR here */ \ | |
210 TRACE_DEBUG(NONE, "ERROR: in '" #__call__ "' :\t%s", strerror(__err__));\ | |
211 return __err__; \ | |
212 } \ | |
213 } | |
214 | |
215 /* Check the return value of a POSIX function and execute fallback in case of error or special value */ | |
216 #define CHECK_POSIX_DO2( __call__, __speval__, __fallback1__, __fallback2__ ) { \ | |
217 int __ret__; \ | |
218 TRACE_DEBUG_ALL( "Check POSIX: " #__call__ ); \ | |
219 __ret__ = (__call__); \ | |
220 if (__ret__ != 0) { \ | |
221 if (__ret__ == (__speval__)) { \ | |
222 __fallback1__; \ | |
223 } else { \ | |
224 TRACE_DEBUG(NONE, "ERROR: in '" #__call__ "':\t%s", strerror(__ret__)); \ | |
225 __fallback2__; \ | |
226 } \ | |
227 } \ | |
228 } | |
229 | |
230 /* Check the return value of a POSIX function and execute fallback in case of error */ | |
231 #define CHECK_POSIX_DO( __call__, __fallback__ ) \ | |
232 CHECK_POSIX_DO2( (__call__), 0, , __fallback__ ); | |
233 | |
234 /* Check the return value of a POSIX function and return it if error */ | |
235 #define CHECK_POSIX( __call__ ) { \ | |
236 int __v__; \ | |
237 CHECK_POSIX_DO( __v__ = (__call__), return __v__ ); \ | |
238 } | |
239 | |
240 /* Check that a memory allocator did not return NULL, otherwise log an error and execute fallback */ | |
241 #define CHECK_MALLOC_DO( __call__, __fallback__ ) { \ | |
242 void * __ret__; \ | |
243 TRACE_DEBUG_ALL( "Check MALLOC: " #__call__ ); \ | |
244 __ret__ = (void *)( __call__ ); \ | |
245 if (__ret__ == NULL) { \ | |
246 int __err__ = errno; \ | |
247 TRACE_DEBUG(NONE, "ERROR: in '" #__call__ "':\t%s", strerror(__err__)); \ | |
248 __fallback__; \ | |
249 } \ | |
250 } | |
251 | |
252 /* Check that a memory allocator did not return NULL, otherwise return ENOMEM */ | |
253 #define CHECK_MALLOC( __call__ ) \ | |
254 CHECK_MALLOC_DO( __call__, return ENOMEM ); | |
255 | |
256 | |
257 /* The next macros can be used also for expected errors */ | |
258 | |
259 /* Check parameters at function entry, execute fallback on error */ | |
260 #define CHECK_PARAMS_DO( __bool__, __fallback__ ) \ | |
261 TRACE_DEBUG_ALL( "Check PARAMS: " #__bool__ ); \ | |
262 if ( ! (__bool__) ) { \ | |
263 TRACE_DEBUG(INFO, "Invalid parameter received in '" #__bool__ "'"); \ | |
264 __fallback__; \ | |
265 } | |
266 /* Check parameters at function entry, return EINVAL if the boolean is false (similar to assert) */ | |
267 #define CHECK_PARAMS( __bool__ ) \ | |
268 CHECK_PARAMS_DO( __bool__, return EINVAL ); | |
269 | |
270 /* Check the return value of an internal function, log and propagate */ | |
271 #define CHECK_FCT_DO( __call__, __fallback__ ) { \ | |
272 int __ret__; \ | |
273 TRACE_DEBUG_ALL( "Check FCT: " #__call__ ); \ | |
274 __ret__ = (__call__); \ | |
275 if (__ret__ != 0) { \ | |
276 TRACE_DEBUG(INFO, "Error in '" #__call__ "':\t%s", strerror(__ret__)); \ | |
277 __fallback__; \ | |
278 } \ | |
279 } | |
280 /* Check the return value of a function call, return any error code */ | |
281 #define CHECK_FCT( __call__ ) { \ | |
282 int __v__; \ | |
283 CHECK_FCT_DO( __v__ = (__call__), return __v__ ); \ | |
284 } | |
285 | |
286 /****************************** Socket helpers ************************************/ | |
287 | |
288 /* Some aliases to socket addresses structures */ | |
289 #define sSS struct sockaddr_storage | |
290 #define sSA struct sockaddr | |
291 #define sSA4 struct sockaddr_in | |
292 #define sSA6 struct sockaddr_in6 | |
293 | |
294 /* Dump one sockaddr */ | |
295 #define sSA_DUMP( level, text, sa ) { \ | |
296 sSA * __sa = (sSA *)(sa); \ | |
297 char *__str, __addrbuf[INET6_ADDRSTRLEN]; \ | |
298 if (__sa) { \ | |
299 int __rc = getnameinfo(__sa, \ | |
300 sizeof(sSS), \ | |
301 __addrbuf, \ | |
302 sizeof(__addrbuf), \ | |
303 NULL, \ | |
304 0, \ | |
305 0); \ | |
306 if (__rc) \ | |
307 __str = (char *)gai_strerror(__rc); \ | |
308 else \ | |
309 __str = &__addrbuf[0]; \ | |
310 } else { \ | |
311 __str = "(NULL / ANY)"; \ | |
312 } \ | |
313 TRACE_DEBUG(level, text "%s", __str); \ | |
314 } | |
315 | |
316 /* The sockaddr length of a sSS structure */ | |
317 #define sSSlen( _ss_ ) \ | |
318 ( (socklen_t) ( ((_ss_)->ss_family == AF_INET) ? (sizeof(sSA4)) : \ | |
319 (((_ss_)->ss_family == AF_INET6) ? (sizeof(sSA6)) : \ | |
320 0 ) ) ) | |
321 | |
322 /* Define the value of IP loopback address */ | |
323 #ifndef INADDR_LOOPBACK | |
324 #define INADDR_LOOPBACK inet_addr("127.0.0.1") | |
325 #endif /* INADDR_LOOPBACK */ | |
326 | |
327 /* create a V4MAPPED address */ | |
328 #define IN6_ADDR_V4MAP( a6, a4 ) { \ | |
329 ((uint32_t *)(a6))[0] = 0; \ | |
330 ((uint32_t *)(a6))[1] = 0; \ | |
331 ((uint32_t *)(a6))[2] = htonl(0xffff); \ | |
332 ((uint32_t *)(a6))[3] = (uint32_t)(a4); \ | |
333 } | |
334 | |
335 /* Retrieve a v4 value from V4MAPPED address ( takes a s6_addr as param) */ | |
336 #define IN6_ADDR_V4UNMAP( a6 ) \ | |
337 (((in_addr_t *)(a6))[3]) | |
338 | |
339 /* | |
340 * Other macros | |
341 */ | |
342 | |
343 /* We provide macros to convert 64 bit values to and from network byte-order, on systems where it is not already provided. */ | |
344 #ifndef HAVE_NTOHLL /* Defined in config.h, if the ntohll symbol is defined on the system */ | |
345 # if HOST_BIG_ENDIAN | |
346 /* In big-endian systems, we don't have to change the values, since the order is the same as network */ | |
347 # define ntohll(x) (x) | |
348 # define htonll(x) (x) | |
349 # else /* HOST_BIG_ENDIAN */ | |
350 /* For these systems, we must reverse the bytes. Use ntohl and htonl on sub-32 blocs, and inverse these blocs. */ | |
351 # define ntohll(x) (typeof (x))( (((uint64_t)ntohl( (uint32_t)(x))) << 32 ) | ((uint64_t) ntohl( ((uint64_t)(x)) >> 32 ))) | |
352 # define htonll(x) (typeof (x))( (((uint64_t)htonl( (uint32_t)(x))) << 32 ) | ((uint64_t) htonl( ((uint64_t)(x)) >> 32 ))) | |
353 # endif /* HOST_BIG_ENDIAN */ | |
354 #endif /* HAVE_NTOHLL */ | |
355 | |
356 /* This macro will pad a size to the next multiple of 4. */ | |
357 #define PAD4(_x) ((_x) + ( (4 - (_x)) & 3 ) ) | |
358 | |
359 /* Useful to display as ASCII some bytes values */ | |
360 #define ASCII(_c) ( ((_c < 32) || (_c > 127)) ? ( _c ? '?' : ' ' ) : _c ) | |
361 | |
362 /* Compare timespec structures */ | |
363 #define TS_IS_INFERIOR( ts1, ts2 ) \ | |
364 ( ((ts1)->tv_sec < (ts2)->tv_sec ) \ | |
365 || ((ts1)->tv_nsec < (ts2)->tv_nsec) ) | |
366 | |
367 /* Some constants for dumping flags and values */ | |
368 #define DUMP_AVPFL_str "%c%c" | |
369 #define DUMP_AVPFL_val(_val) (_val & AVP_FLAG_VENDOR)?'V':'-' , (_val & AVP_FLAG_MANDATORY)?'M':'-' | |
370 #define DUMP_CMDFL_str "%c%c%c%c" | |
371 #define DUMP_CMDFL_val(_val) (_val & CMD_FLAG_REQUEST)?'R':'-' , (_val & CMD_FLAG_PROXIABLE)?'P':'-' , (_val & CMD_FLAG_ERROR)?'E':'-' , (_val & CMD_FLAG_RETRANSMIT)?'T':'-' | |
372 | |
373 /*============================================================*/ | |
374 /* THREADS */ | |
375 /*============================================================*/ | |
376 | |
377 /* Terminate a thread */ | |
378 static __inline__ int fd_thr_term(pthread_t * th) | |
379 { | |
380 int ret = 0; | |
381 void * th_ret = NULL; | |
382 | |
383 CHECK_PARAMS(th); | |
384 | |
385 /* Test if it was already terminated */ | |
386 if (*th == (pthread_t)NULL) | |
387 return 0; | |
388 | |
389 /* Cancel the thread if it is still running - ignore error if it was already terminated */ | |
390 (void) pthread_cancel(*th); | |
391 | |
392 /* Then join the thread */ | |
393 CHECK_POSIX_DO( ret = pthread_join(*th, &th_ret), /* continue */ ); | |
394 | |
395 if (th_ret != NULL) { | |
396 TRACE_DEBUG(FULL, "The thread returned the following value: %p (ignored)", th_ret); | |
397 } | |
398 | |
399 /* Clean the location */ | |
400 *th = (pthread_t)NULL; | |
401 | |
402 return ret; | |
403 } | |
404 | |
405 /* Cleanups for cancellation (all threads should be safely cancelable!) */ | |
406 static __inline__ void fd_cleanup_mutex( void * mutex ) | |
407 { | |
408 CHECK_POSIX_DO( pthread_mutex_unlock((pthread_mutex_t *)mutex), /* */); | |
409 } | |
410 | |
411 static __inline__ void fd_cleanup_rwlock( void * rwlock ) | |
412 { | |
413 CHECK_POSIX_DO( pthread_rwlock_unlock((pthread_rwlock_t *)rwlock), /* */); | |
414 } | |
415 | |
416 static __inline__ void fd_cleanup_buffer( void * buffer ) | |
417 { | |
418 free(buffer); | |
419 } | |
420 | |
421 /*============================================================*/ | |
422 /* LISTS */ | |
423 /*============================================================*/ | |
424 | |
425 /* The following structure represents a chained list element */ | |
426 struct fd_list { | |
427 struct fd_list *next; /* next element in the list */ | |
428 struct fd_list *prev; /* previous element in the list */ | |
429 struct fd_list *head; /* head of the list */ | |
430 void *o; /* additional avialbe pointer used for start of the parento object or other purpose */ | |
431 }; | |
432 | |
433 #define FD_LIST( _li ) ((struct fd_list *)( _li )) | |
434 | |
435 /* Initialize a list element */ | |
436 void fd_list_init ( struct fd_list * list, void *obj ); | |
437 | |
438 /* Return boolean, true if the list is empty */ | |
439 #define FD_IS_LIST_EMPTY( _list ) (((FD_LIST(_list))->head == (_list)) && ((FD_LIST(_list))->next == (_list))) | |
440 | |
441 /* Insert an item in a list at known position */ | |
442 void fd_list_insert_after ( struct fd_list * ref, struct fd_list * item ); | |
443 void fd_list_insert_before ( struct fd_list * ref, struct fd_list * item ); | |
444 | |
445 /* Insert an item in an ordered list -- ordering function provided. If duplicate object found, EEXIST and it is returned in ref_duplicate */ | |
446 int fd_list_insert_ordered( struct fd_list * head, struct fd_list * item, int (*cmp_fct)(void *, void *), void ** ref_duplicate); | |
447 | |
448 /* Unlink an item from a list */ | |
449 void fd_list_unlink ( struct fd_list * item ); | |
450 | |
451 /* Compute a hash value of a string (session id, diameter id, ...) */ | |
452 uint32_t fd_hash ( char * string, size_t len ); | |
453 | |
454 | |
455 | |
456 /*============================================================*/ | |
457 /* DICTIONARY */ | |
458 /*============================================================*/ | |
459 /* Structure that contains the complete dictionary definitions */ | |
460 struct dictionary; | |
461 | |
462 /* Structure that contains a dictionary object */ | |
463 struct dict_object; | |
464 | |
465 /* Types of object in the dictionary. */ | |
466 enum dict_object_type { | |
467 DICT_VENDOR = 1, /* Vendor */ | |
468 DICT_APPLICATION, /* Diameter Application */ | |
469 DICT_TYPE, /* AVP data type */ | |
470 DICT_ENUMVAL, /* Named constant (value of an enumerated AVP type) */ | |
471 DICT_AVP, /* AVP */ | |
472 DICT_COMMAND, /* Diameter Command */ | |
473 DICT_RULE /* a Rule for AVP in command or grouped AVP */ | |
474 #define DICT_TYPE_MAX DICT_RULE | |
475 }; | |
476 | |
477 /* Initialize a dictionary */ | |
478 int fd_dict_init(struct dictionary ** dict); | |
479 /* Destroy a dictionary */ | |
480 int fd_dict_fini(struct dictionary ** dict); | |
481 | |
482 /* | |
483 * FUNCTION: fd_dict_new | |
484 * | |
485 * PARAMETERS: | |
486 * dict : Pointer to the dictionnary where the object is created | |
487 * type : What kind of object must be created | |
488 * data : pointer to the data for the object. | |
489 * type parameter is used to determine the type of data (see bellow for detail). | |
490 * parent : a reference to a parent object, if needed. | |
491 * ref : upon successful creation, reference to new object is stored here if !null. | |
492 * | |
493 * DESCRIPTION: | |
494 * Create a new object in the dictionary. | |
495 * See following object sections in this header file for more information on data and parent parameters format. | |
496 * | |
497 * RETURN VALUE: | |
498 * 0 : The object is created in the dictionary. | |
499 * EINVAL : A parameter is invalid. | |
500 * EEXIST : This object is already defined in the dictionary (with conflicting data). | |
501 * If "ref" is not NULL, it points to the existing element on return. | |
502 * (other standard errors may be returned, too, with their standard meaning. Example: | |
503 * ENOMEM : Memory allocation for the new object element failed.) | |
504 */ | |
505 int fd_dict_new ( struct dictionary * dict, enum dict_object_type type, void * data, struct dict_object * parent, struct dict_object **ref ); | |
506 | |
507 /* | |
508 * FUNCTION: fd_dict_search | |
509 * | |
510 * PARAMETERS: | |
511 * dict : Pointer to the dictionnary where the object is searched | |
512 * type : type of object that is being searched | |
513 * criteria : how the object must be searched. See object-related sections bellow for more information. | |
514 * what : depending on criteria, the data that must be searched. | |
515 * result : On successful return, pointer to the object is stored here. | |
516 * retval : this value is returned if the object is not found and result is not NULL. | |
517 * | |
518 * DESCRIPTION: | |
519 * Perform a search in the dictionary. | |
520 * See the object-specific sections bellow to find how to look for each objects. | |
521 * If the "result" parameter is NULL, the function is used to check if an object is in the dictionary. | |
522 * Otherwise, a reference to the object is stored in result if found. | |
523 * If result is not NULL and the object is not found, retval is returned (should be 0 or ENOENT usually) | |
524 * | |
525 * RETURN VALUE: | |
526 * 0 : The object has been found in the dictionary, or *result is NULL. | |
527 * EINVAL : A parameter is invalid. | |
528 * ENOENT : No matching object has been found, and result was NULL. | |
529 */ | |
530 int fd_dict_search ( struct dictionary * dict, enum dict_object_type type, int criteria, void * what, struct dict_object **result, int retval ); | |
531 | |
532 /* Special case: get the generic error command object */ | |
533 int fd_dict_get_error_cmd(struct dictionary * dict, struct dict_object **obj); | |
534 | |
535 /* | |
536 * FUNCTION: fd_dict_getval | |
537 * | |
538 * PARAMETERS: | |
539 * object : Pointer to a dictionary object. | |
540 * data : pointer to a structure to hold the data for the object. | |
541 * The type is the same as "data" parameter in fd_dict_new function. | |
542 * | |
543 * DESCRIPTION: | |
544 * Retrieve content of a dictionary object. | |
545 * See following object sections in this header file for more information on data and parent parameters format. | |
546 * | |
547 * RETURN VALUE: | |
548 * 0 : The content of the object has been retrieved. | |
549 * EINVAL : A parameter is invalid. | |
550 */ | |
551 int fd_dict_getval ( struct dict_object * object, void * val); | |
552 int fd_dict_gettype ( struct dict_object * object, enum dict_object_type * type); | |
553 int fd_dict_getdict ( struct dict_object * object, struct dictionary ** dict); | |
554 | |
555 /* Debug functions */ | |
556 void fd_dict_dump_object(struct dict_object * obj); | |
557 void fd_dict_dump(struct dictionary * dict); | |
558 | |
559 | |
560 /* | |
561 *************************************************************************** | |
562 * | |
563 * Vendor object | |
564 * | |
565 * These types are used to manage vendors in the dictionary | |
566 * | |
567 *************************************************************************** | |
568 */ | |
569 | |
570 /* Type to hold a Vendor ID: "SMI Network Management Private Enterprise Codes" (RFC3232) */ | |
571 typedef uint32_t vendor_id_t; | |
572 | |
573 /* Type to hold data associated to a vendor */ | |
574 struct dict_vendor_data { | |
575 vendor_id_t vendor_id; /* ID of a vendor */ | |
576 char *vendor_name; /* The name of this vendor */ | |
577 }; | |
578 | |
579 /* The criteria for searching a vendor object in the dictionary */ | |
580 enum { | |
581 VENDOR_BY_ID = 10, /* "what" points to a vendor_id_t */ | |
582 VENDOR_BY_NAME, /* "what" points to a string */ | |
583 VENDOR_OF_APPLICATION /* "what" points to a struct dict_object containing an application (see bellow) */ | |
584 }; | |
585 | |
586 /*** | |
587 * API usage : | |
588 | |
589 Note: the value of "vendor_name" is copied when the object is created, and the string may be disposed afterwards. | |
590 On the other side, when value is retrieved with dict_getval, the string is not copied and MUST NOT be freed. It will | |
591 be freed automatically along with the object itself with call to dict_fini later. | |
592 | |
593 - dict_new: | |
594 The "parent" parameter is not used for vendors. | |
595 Sample code to create a vendor: | |
596 { | |
597 int ret; | |
598 struct dict_object * myvendor; | |
599 struct dict_vendor_data myvendordata = { 23455, "my vendor name" }; -- just an example... | |
600 ret = dict_new ( DICT_VENDOR, &myvendordata, NULL, &myvendor ); | |
601 } | |
602 | |
603 - dict_search: | |
604 Sample codes to look for a vendor object, by its id or name: | |
605 { | |
606 int ret; | |
607 struct dict_object * vendor_found; | |
608 vendor_id_t vendorid = 23455; | |
609 ret = dict_search ( DICT_VENDOR, VENDOR_BY_ID, &vendorid, &vendor_found, ENOENT); | |
610 - or - | |
611 ret = dict_search ( DICT_VENDOR, VENDOR_BY_NAME, "my vendor name", &vendor_found, ENOENT); | |
612 } | |
613 | |
614 - dict_getval: | |
615 Sample code to retrieve the data from a vendor object: | |
616 { | |
617 int ret; | |
618 struct dict_object * myvendor; | |
619 struct dict_vendor_data myvendordata; | |
620 ret = dict_search ( DICT_VENDOR, VENDOR_BY_NAME, "my vendor name", &myvendor, ENOENT); | |
621 ret = dict_getval ( myvendor, &myvendordata ); | |
622 printf("my vendor id: %d\n", myvendordata.vendor_id ); | |
623 } | |
624 | |
625 | |
626 */ | |
627 | |
628 /* | |
629 *************************************************************************** | |
630 * | |
631 * Application object | |
632 * | |
633 * These types are used to manage Diameter applications in the dictionary | |
634 * | |
635 *************************************************************************** | |
636 */ | |
637 | |
638 /* Type to hold a Diameter application ID: IANA assigned value for this application. */ | |
639 typedef uint32_t application_id_t; | |
640 | |
641 /* Type to hold data associated to an application */ | |
642 struct dict_application_data { | |
643 application_id_t application_id; /* ID of the application */ | |
644 char *application_name; /* The name of this application */ | |
645 }; | |
646 | |
647 /* The criteria for searching an application object in the dictionary */ | |
648 enum { | |
649 APPLICATION_BY_ID = 20, /* "what" points to a application_id_t */ | |
650 APPLICATION_BY_NAME, /* "what" points to a string */ | |
651 APPLICATION_OF_TYPE, /* "what" points to a struct dict_object containing a type object (see bellow) */ | |
652 APPLICATION_OF_COMMAND /* "what" points to a struct dict_object containing a command (see bellow) */ | |
653 }; | |
654 | |
655 /*** | |
656 * API usage : | |
657 | |
658 The "parent" parameter of dict_new may point to a vendor object to inform of what vendor defines the application. | |
659 for standard-track applications, the "parent" parameter should be NULL. | |
660 The vendor associated to an application is retrieved with VENDOR_OF_APPLICATION search criteria on vendors. | |
661 | |
662 - dict_new: | |
663 Sample code for application creation: | |
664 { | |
665 int ret; | |
666 struct dict_object * vendor; | |
667 struct dict_object * appl; | |
668 struct dict_vendor_data vendor_data = { | |
669 23455, | |
670 "my vendor name" | |
671 }; | |
672 struct dict_application_data app_data = { | |
673 9789, | |
674 "my vendor's application" | |
675 }; | |
676 | |
677 ret = dict_new ( DICT_VENDOR, &vendor_data, NULL, &vendor ); | |
678 ret = dict_new ( DICT_APPLICATION, &app_data, vendor, &appl ); | |
679 } | |
680 | |
681 - dict_search: | |
682 Sample code to retrieve the vendor of an application | |
683 { | |
684 int ret; | |
685 struct dict_object * vendor, * appli; | |
686 | |
687 ret = dict_search ( DICT_APPLICATION, APPLICATION_BY_NAME, "my vendor's application", &appli, ENOENT); | |
688 ret = dict_search ( DICT_VENDOR, VENDOR_OF_APPLICATION, appli, &vendor, ENOENT); | |
689 } | |
690 | |
691 - dict_getval: | |
692 Sample code to retrieve the data from an application object: | |
693 { | |
694 int ret; | |
695 struct dict_object * appli; | |
696 struct dict_application_data appl_data; | |
697 ret = dict_search ( DICT_APPLICATION, APPLICATION_BY_NAME, "my vendor's application", &appli, ENOENT); | |
698 ret = dict_getval ( appli, &appl_data ); | |
699 printf("my application id: %s\n", appl_data.application_id ); | |
700 } | |
701 | |
702 */ | |
703 | |
704 /* | |
705 *************************************************************************** | |
706 * | |
707 * Type object | |
708 * | |
709 * These types are used to manage AVP data types in the dictionary | |
710 * | |
711 *************************************************************************** | |
712 */ | |
713 | |
714 /* Type to store any AVP value */ | |
715 union avp_value { | |
716 struct { | |
717 uint8_t *data; /* bytes buffer */ | |
718 size_t len; /* length of the data buffer */ | |
719 } os; /* Storage for an octet string, data is alloc'd and must be freed */ | |
720 int32_t i32; /* integer 32 */ | |
721 int64_t i64; /* integer 64 */ | |
722 uint32_t u32; /* unsigned 32 */ | |
723 uint64_t u64; /* unsigned 64 */ | |
724 float f32; /* float 32 */ | |
725 double f64; /* float 64 */ | |
726 }; | |
727 | |
728 /* These are the basic AVP types defined in RFC3588bis */ | |
729 enum dict_avp_basetype { | |
730 AVP_TYPE_GROUPED, | |
731 AVP_TYPE_OCTETSTRING, | |
732 AVP_TYPE_INTEGER32, | |
733 AVP_TYPE_INTEGER64, | |
734 AVP_TYPE_UNSIGNED32, | |
735 AVP_TYPE_UNSIGNED64, | |
736 AVP_TYPE_FLOAT32, | |
737 AVP_TYPE_FLOAT64 | |
738 #define AVP_TYPE_MAX AVP_TYPE_FLOAT64 | |
739 }; | |
740 | |
741 /* Callbacks that can be associated with a derived type to easily interpret the AVP value. */ | |
742 /* | |
743 * CALLBACK: dict_avpdata_interpret | |
744 * | |
745 * PARAMETERS: | |
746 * val : Pointer to the AVP value that must be interpreted. | |
747 * interpreted : The result of interpretation is stored here. The format and meaning depends on each type. | |
748 * | |
749 * DESCRIPTION: | |
750 * This callback can be provided with a derived type in order to facilitate the interpretation of formated data. | |
751 * For example, when an AVP of type "Address" is received, it can be used to convert the octetstring into a struct sockaddr. | |
752 * This callback is not called directly, but through the message's API msg_avp_value_interpret function. | |
753 * | |
754 * RETURN VALUE: | |
755 * 0 : Operation complete. | |
756 * !0 : An error occurred, the error code is returned. | |
757 */ | |
758 typedef int (*dict_avpdata_interpret) (union avp_value * value, void * interpreted); | |
759 /* | |
760 * CALLBACK: dict_avpdata_encode | |
761 * | |
762 * PARAMETERS: | |
763 * data : The formated data that must be stored in the AVP value. | |
764 * val : Pointer to the AVP value storage area where the data must be stored. | |
765 * | |
766 * DESCRIPTION: | |
767 * This callback can be provided with a derived type in order to facilitate the encoding of formated data. | |
768 * For example, it can be used to convert a struct sockaddr in an AVP value of type Address. | |
769 * This callback is not called directly, but through the message's API msg_avp_value_encode function. | |
770 * If the callback is defined for an OctetString based type, the created string must be malloc'd. free will be called | |
771 * automatically later. | |
772 * | |
773 * RETURN VALUE: | |
774 * 0 : Operation complete. | |
775 * !0 : An error occurred, the error code is returned. | |
776 */ | |
777 typedef int (*dict_avpdata_encode) (void * data, union avp_value * val); | |
778 | |
779 | |
780 /* Type to hold data associated to a derived AVP data type */ | |
781 struct dict_type_data { | |
782 enum dict_avp_basetype type_base; /* How the data of such AVP must be interpreted */ | |
783 char *type_name; /* The name of this type */ | |
784 dict_avpdata_interpret type_interpret;/* cb to convert the AVP value in more comprehensive format (or NULL) */ | |
785 dict_avpdata_encode type_encode; /* cb to convert formatted data into an AVP value (or NULL) */ | |
786 }; | |
787 | |
788 /* The criteria for searching a type object in the dictionary */ | |
789 enum { | |
790 TYPE_BY_NAME = 30, /* "what" points to a string */ | |
791 TYPE_OF_ENUMVAL, /* "what" points to a struct dict_object containing an enumerated constant (DICT_ENUMVAL, see bellow). */ | |
792 TYPE_OF_AVP /* "what" points to a struct dict_object containing an AVP object. */ | |
793 }; | |
794 | |
795 | |
796 /*** | |
797 * API usage : | |
798 | |
799 - dict_new: | |
800 The "parent" parameter may point to an application object, when a type is defined by a Diameter application. | |
801 | |
802 Sample code: | |
803 { | |
804 int ret; | |
805 struct dict_object * mytype; | |
806 struct dict_type_data mytypedata = | |
807 { | |
808 AVP_TYPE_OCTETSTRING, | |
809 "Address", | |
810 NULL, | |
811 NULL | |
812 }; | |
813 ret = dict_new ( DICT_TYPE, &mytypedata, NULL, &mytype ); | |
814 } | |
815 | |
816 - dict_search: | |
817 Sample code: | |
818 { | |
819 int ret; | |
820 struct dict_object * address_type; | |
821 ret = dict_search ( DICT_TYPE, TYPE_BY_NAME, "Address", &address_type, ENOENT); | |
822 } | |
823 | |
824 */ | |
825 | |
826 /* | |
827 *************************************************************************** | |
828 * | |
829 * Enumerated values object | |
830 * | |
831 * These types are used to manage named constants of some AVP, | |
832 * for enumerated types. Waaad allows contants for types others than Unsigned32 | |
833 * | |
834 *************************************************************************** | |
835 */ | |
836 | |
837 /* Type to hold data of named constants for AVP */ | |
838 struct dict_enumval_data { | |
839 char *enum_name; /* The name of this constant */ | |
840 union avp_value enum_value; /* Value of the constant. Union term depends on parent type's base type. */ | |
841 }; | |
842 | |
843 /* The criteria for searching a constant in the dictionary */ | |
844 enum { | |
845 ENUMVAL_BY_STRUCT = 40, /* "what" points to a struct dict_enumval_request as defined bellow */ | |
846 }; | |
847 | |
848 struct dict_enumval_request { | |
849 /* Identifier of the parent type, one of the following must not be NULL */ | |
850 struct dict_object *type_obj; | |
851 char *type_name; | |
852 | |
853 /* Search criteria for the constant */ | |
854 struct dict_enumval_data search; /* search.enum_value is used only if search.enum_name == NULL */ | |
855 }; | |
856 | |
857 /*** | |
858 * API usage : | |
859 | |
860 - dict_new: | |
861 The "parent" parameter must point to a derived type object. | |
862 Sample code to create a type "Boolean" with two constants "True" and "False": | |
863 { | |
864 int ret; | |
865 struct dict_object * type_boolean; | |
866 struct dict_type_data type_boolean_data = | |
867 { | |
868 AVP_TYPE_INTEGER32, | |
869 "Boolean", | |
870 NULL, | |
871 NULL | |
872 }; | |
873 struct dict_enumval_data boolean_false = | |
874 { | |
875 .enum_name="False", | |
876 .enum_value.i32 = 0 | |
877 }; | |
878 struct dict_enumval_data boolean_true = | |
879 { | |
880 .enum_name="True", | |
881 .enum_value.i32 = -1 | |
882 }; | |
883 ret = dict_new ( DICT_TYPE, &type_boolean_data, NULL, &type_boolean ); | |
884 ret = dict_new ( DICT_ENUMVAL, &boolean_false, type_boolean, NULL ); | |
885 ret = dict_new ( DICT_ENUMVAL, &boolean_true , type_boolean, NULL ); | |
886 | |
887 } | |
888 | |
889 - dict_search: | |
890 Sample code to look for a constant name, by its value: | |
891 { | |
892 int ret; | |
893 struct dict_object * value_found; | |
894 struct dict_enumval_request boolean_by_value = | |
895 { | |
896 .type_name = "Boolean", | |
897 .search.enum_name=NULL, | |
898 .search.enum_value.i32 = -1 | |
899 }; | |
900 | |
901 ret = dict_search ( DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &boolean_by_value, &value_found, ENOENT); | |
902 } | |
903 | |
904 - dict_getval: | |
905 Sample code to retrieve the data from a constant object: | |
906 { | |
907 int ret; | |
908 struct dict_object * value_found; | |
909 struct dict_enumval_data boolean_data = NULL; | |
910 struct dict_enumval_request boolean_by_value = | |
911 { | |
912 .type_name = "Boolean", | |
913 .search.enum_name=NULL, | |
914 .search.enum_value.i32 = 0 | |
915 }; | |
916 | |
917 ret = dict_search ( DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &boolean_by_value, &value_found, ENOENT); | |
918 ret = dict_getval ( value_found, &boolean_data ); | |
919 printf(" Boolean with value 0: %s", boolean_data.enum_name ); | |
920 } | |
921 */ | |
922 | |
923 /* | |
924 *************************************************************************** | |
925 * | |
926 * AVP object | |
927 * | |
928 * These objects are used to manage AVP definitions in the dictionary | |
929 * | |
930 *************************************************************************** | |
931 */ | |
932 | |
933 /* Type to hold an AVP code. For vendor 0, these codes are assigned by IANA. Otherwise, it is managed by the vendor */ | |
934 typedef uint32_t avp_code_t; | |
935 | |
936 /* Values of AVP flags */ | |
937 #define AVP_FLAG_VENDOR 0x80 | |
938 #define AVP_FLAG_MANDATORY 0x40 | |
939 #define AVP_FLAG_RESERVED3 0x20 | |
940 #define AVP_FLAG_RESERVED4 0x10 | |
941 #define AVP_FLAG_RESERVED5 0x08 | |
942 #define AVP_FLAG_RESERVED6 0x04 | |
943 #define AVP_FLAG_RESERVED7 0x02 | |
944 #define AVP_FLAG_RESERVED8 0x01 | |
945 | |
946 | |
947 /* Type to hold data associated to an avp */ | |
948 struct dict_avp_data { | |
949 avp_code_t avp_code; /* Code of the avp */ | |
950 vendor_id_t avp_vendor; /* Vendor of the AVP, or 0 */ | |
951 char *avp_name; /* Name of this AVP */ | |
952 uint8_t avp_flag_mask; /* Mask of fixed AVP flags */ | |
953 uint8_t avp_flag_val; /* Values of the fixed flags */ | |
954 enum dict_avp_basetype avp_basetype; /* Basic type of data found in the AVP */ | |
955 }; | |
956 | |
957 /* The criteria for searching an avp object in the dictionary */ | |
958 enum { | |
959 AVP_BY_CODE = 50, /* "what" points to an avp_code_t, vendor is always 0 */ | |
960 AVP_BY_NAME, /* "what" points to a string, vendor is always 0 */ | |
961 AVP_BY_CODE_AND_VENDOR, /* "what" points to a struct dict_avp_request (see bellow), where avp_vendor and avp_code are set */ | |
962 AVP_BY_NAME_AND_VENDOR /* "what" points to a struct dict_avp_request (see bellow), where avp_vendor and avp_name are set */ | |
963 }; | |
964 | |
965 /* Struct used for some researchs */ | |
966 struct dict_avp_request { | |
967 vendor_id_t avp_vendor; | |
968 avp_code_t avp_code; | |
969 char *avp_name; | |
970 }; | |
971 | |
972 | |
973 /*** | |
974 * API usage : | |
975 | |
976 If "parent" parameter is not NULL during AVP creation, it must point to a DICT_TYPE object. | |
977 The extended type is then attached to the AVP. In case where it is an enumerated type, the value of | |
978 AVP is automatically interpreted in debug messages, and in message checks. | |
979 The derived type of an AVP can be retrieved with: dict_search ( DICT_TYPE, TYPE_OF_AVP, avp, ... ) | |
980 | |
981 To create the rules (ABNF) for children of Grouped AVP, see the DICT_RULE related part. | |
982 | |
983 - dict_new: | |
984 Sample code for AVP creation: | |
985 { | |
986 int ret; | |
987 struct dict_object * user_name_avp; | |
988 struct dict_object * boolean_type; | |
989 struct dict_object * sample_boolean_avp; | |
990 struct dict_avp_data user_name_data = { | |
991 1, // code | |
992 0, // vendor | |
993 "User-Name", // name | |
994 AVP_FLAG_VENDOR | AVP_FLAG_MANDATORY, // fixed mask: V and M values must always be defined as follow. other flags can be set or cleared | |
995 AVP_FLAG_MANDATORY, // the V flag must be cleared, the M flag must be set. | |
996 AVP_TYPE_OCTETSTRING // User-Name AVP contains OctetString data (further precision such as UTF8String can be given with a parent derived type) | |
997 }; | |
998 struct dict_avp_data sample_boolean_data = { | |
999 31337, | |
1000 23455, | |
1001 "Sample-Boolean", | |
1002 AVP_FLAG_VENDOR | AVP_FLAG_MANDATORY, | |
1003 AVP_FLAG_VENDOR, | |
1004 AVP_TYPE_INTEGER32 // This MUST be the same as parent type's | |
1005 }; | |
1006 | |
1007 -- Create an AVP with a base type -- | |
1008 ret = dict_new ( DICT_AVP, &user_name_data, NULL, &user_name_avp ); | |
1009 | |
1010 -- Create an AVP with a derived type -- | |
1011 ret = dict_search ( DICT_TYPE, TYPE_BY_NAME, "Boolean", &boolean_type, ENOENT); | |
1012 ret = dict_new ( DICT_AVP, &sample_boolean_data , boolean_type, &sample_boolean_avp ); | |
1013 | |
1014 } | |
1015 | |
1016 - dict_search: | |
1017 Sample code to look for an AVP | |
1018 { | |
1019 int ret; | |
1020 struct dict_object * avp_username; | |
1021 struct dict_object * avp_sampleboolean; | |
1022 struct dict_avp_request avpvendorboolean = | |
1023 { | |
1024 .avp_vendor = 23455, | |
1025 .avp_name = "Sample-Boolean" | |
1026 }; | |
1027 | |
1028 ret = dict_search ( DICT_AVP, AVP_BY_NAME, "User-Name", &avp_username, ENOENT); | |
1029 | |
1030 ret = dict_search ( DICT_AVP, AVP_BY_NAME_AND_VENDOR, &avpvendorboolean, &avp_sampleboolean, ENOENT); | |
1031 | |
1032 } | |
1033 | |
1034 - dict_getval: | |
1035 Sample code to retrieve the data from an AVP object: | |
1036 { | |
1037 int ret; | |
1038 struct dict_object * avp_username; | |
1039 struct dict_avp_data user_name_data; | |
1040 ret = dict_search ( DICT_AVP, AVP_BY_NAME, "User-Name", &avp_username, ENOENT); | |
1041 ret = dict_getval ( avp_username, &user_name_data ); | |
1042 printf("User-Name code: %d\n", user_name_data.avp_code ); | |
1043 } | |
1044 | |
1045 */ | |
1046 | |
1047 /* | |
1048 *************************************************************************** | |
1049 * | |
1050 * Command object | |
1051 * | |
1052 * These types are used to manage commands objects in the dictionary | |
1053 * | |
1054 *************************************************************************** | |
1055 */ | |
1056 | |
1057 /* Type to hold a Diameter command code: IANA assigned values. 0x0-0x7fffff=standard, 0x800000-0xfffffd=vendors, 0xfffffe-0xffffff=experimental */ | |
1058 typedef uint32_t command_code_t; | |
1059 | |
1060 /* Values of command flags */ | |
1061 #define CMD_FLAG_REQUEST 0x80 | |
1062 #define CMD_FLAG_PROXIABLE 0x40 | |
1063 #define CMD_FLAG_ERROR 0x20 | |
1064 #define CMD_FLAG_RETRANSMIT 0x10 | |
1065 #define CMD_FLAG_RESERVED5 0x08 | |
1066 #define CMD_FLAG_RESERVED6 0x04 | |
1067 #define CMD_FLAG_RESERVED7 0x02 | |
1068 #define CMD_FLAG_RESERVED8 0x01 | |
1069 | |
1070 /* Type to hold data associated to a command */ | |
1071 struct dict_cmd_data { | |
1072 command_code_t cmd_code; /* code of the command */ | |
1073 char *cmd_name; /* Name of the command */ | |
1074 uint8_t cmd_flag_mask; /* Mask of fixed-value flags */ | |
1075 uint8_t cmd_flag_val; /* values of the fixed flags */ | |
1076 }; | |
1077 | |
1078 /* The criteria for searching an avp object in the dictionary */ | |
1079 enum { | |
1080 CMD_BY_NAME = 60, /* "what" points to a string */ | |
1081 CMD_BY_CODE_R, /* "what" points to a command_code_t. The "Request" command is returned. */ | |
1082 CMD_BY_CODE_A, /* "what" points to a command_code_t. The "Answer" command is returned. */ | |
1083 CMD_ANSWER /* "what" points to a struct dict_object of a request command. The corresponding "Answer" command is returned. */ | |
1084 }; | |
1085 | |
1086 | |
1087 /*** | |
1088 * API usage : | |
1089 | |
1090 The "parent" parameter of dict_new may point to an application object to inform of what application defines the command. | |
1091 The application associated to a command is retrieved with APPLICATION_OF_COMMAND search criteria on applications. | |
1092 | |
1093 To create the rules for children of commands, see the DICT_RULE related part. | |
1094 | |
1095 Note that the "Request" and "Answer" commands are two independant objects. This allows to have different rules for each. | |
1096 | |
1097 - dict_new: | |
1098 Sample code for command creation: | |
1099 { | |
1100 int ret; | |
1101 struct dict_object * cer; | |
1102 struct dict_object * cea; | |
1103 struct dict_cmd_data ce_data = { | |
1104 257, // code | |
1105 "Capabilities-Exchange-Request", // name | |
1106 CMD_FLAG_REQUEST, // mask | |
1107 CMD_FLAG_REQUEST // value. Only the "R" flag is constrained here, set. | |
1108 }; | |
1109 | |
1110 ret = dict_new ( DICT_COMMAND, &ce_data, NULL, &cer ); | |
1111 | |
1112 ce_data.cmd_name = "Capabilities-Exchange-Answer"; | |
1113 ce_data.cmd_flag_val = 0; // Same constraint on "R" flag, but this time it must be cleared. | |
1114 | |
1115 ret = dict_new ( DICT_COMMAND, &ce_data, NULL, &cea ); | |
1116 } | |
1117 | |
1118 - dict_search: | |
1119 Sample code to look for a command | |
1120 { | |
1121 int ret; | |
1122 struct dict_object * cer, * cea; | |
1123 command_code_t code = 257; | |
1124 ret = dict_search ( DICT_COMMAND, CMD_BY_NAME, "Capabilities-Exchange-Request", &cer, ENOENT); | |
1125 ret = dict_search ( DICT_COMMAND, CMD_BY_CODE_R, &code, &cer, ENOENT); | |
1126 } | |
1127 | |
1128 - dict_getval: | |
1129 Sample code to retrieve the data from a command object: | |
1130 { | |
1131 int ret; | |
1132 struct dict_object * cer; | |
1133 struct dict_object * cea; | |
1134 struct dict_cmd_data cea_data; | |
1135 ret = dict_search ( DICT_COMMAND, CMD_BY_NAME, "Capabilities-Exchange-Request", &cer, ENOENT); | |
1136 ret = dict_search ( DICT_COMMAND, CMD_ANSWER, cer, &cea, ENOENT); | |
1137 ret = dict_getval ( cea, &cea_data ); | |
1138 printf("Answer to CER: %s\n", cea_data.cmd_name ); | |
1139 } | |
1140 | |
1141 */ | |
1142 | |
1143 /* | |
1144 *************************************************************************** | |
1145 * | |
1146 * Rule object | |
1147 * | |
1148 * These objects are used to manage rules in the dictionary (ABNF implementation) | |
1149 * This is used for checking messages validity (more powerful than a DTD) | |
1150 * | |
1151 *************************************************************************** | |
1152 */ | |
1153 | |
1154 /* This defines the kind of rule that is defined */ | |
1155 enum rule_position { | |
1156 RULE_FIXED_HEAD = 1, /* The AVP must be at the head of the group. The rule_order field is used to specify the position. */ | |
1157 RULE_REQUIRED, /* The AVP must be present in the parent, but its position is not defined. */ | |
1158 RULE_OPTIONAL, /* The AVP may be present in the message. Used to specify a max number of occurences for example */ | |
1159 RULE_FIXED_TAIL /* The AVP must be at the end of the group. The rule_order field is used to specify the position. */ | |
1160 }; | |
1161 | |
1162 /* Content of a RULE object data */ | |
1163 struct dict_rule_data { | |
1164 struct dict_object *rule_avp; /* Pointer to the AVP object that is concerned by this rule */ | |
1165 enum rule_position rule_position; /* The position in which the rule_avp must appear in the parent */ | |
1166 unsigned rule_order; /* for RULE_FIXED_* rules, the place. 1,2,3.. for HEAD rules; ...,3,2,1 for TAIL rules. */ | |
1167 int rule_min; /* Minimum number of occurences. -1 means "default": 0 for optional rules, 1 for other rules */ | |
1168 int rule_max; /* Maximum number of occurences. -1 means no maximum. 0 means the AVP is forbidden. */ | |
1169 }; | |
1170 | |
1171 /* The criteria for searching a rule in the dictionary */ | |
1172 enum { | |
1173 RULE_BY_AVP_AND_PARENT = 70 /* "what" points to a struct dict_rule_request -- see bellow. This is used to query "what is the rule for this AVP in this group?" */ | |
1174 }; | |
1175 | |
1176 /* Structure for querying the dictionary about a rule */ | |
1177 struct dict_rule_request { | |
1178 struct dict_object *rule_parent; /* The grouped avp or command to which the rule apply */ | |
1179 struct dict_object *rule_avp; /* The AVP concerned by this rule */ | |
1180 }; | |
1181 | |
1182 | |
1183 /*** | |
1184 * API usage : | |
1185 | |
1186 The "parent" parameter can not be NULL. It points to the object (grouped avp or command) to which this rule apply (i.e. for which the ABNF is defined). | |
1187 | |
1188 - dict_new: | |
1189 Sample code for rule creation. Let's create the Proxy-Info grouped AVP for example. | |
1190 { | |
1191 int ret; | |
1192 struct dict_object * proxy_info_avp; | |
1193 struct dict_object * proxy_host_avp; | |
1194 struct dict_object * proxy_state_avp; | |
1195 struct dict_object * diameteridentity_type; | |
1196 struct dict_rule_data rule_data; | |
1197 struct dict_type_data di_type_data = { AVP_TYPE_OCTETSTRING, "DiameterIdentity", NULL, NULL }; | |
1198 struct dict_avp_data proxy_info_data = { 284, 0, "Proxy-Info", AVP_FLAG_VENDOR | AVP_FLAG_MANDATORY, AVP_FLAG_MANDATORY, AVP_TYPE_GROUPED }; | |
1199 struct dict_avp_data proxy_host_data = { 280, 0, "Proxy-Host", AVP_FLAG_VENDOR | AVP_FLAG_MANDATORY, AVP_FLAG_MANDATORY, AVP_TYPE_OCTETSTRING }; | |
1200 struct dict_avp_data proxy_state_data = { 33, 0, "Proxy-State",AVP_FLAG_VENDOR | AVP_FLAG_MANDATORY, AVP_FLAG_MANDATORY, AVP_TYPE_OCTETSTRING }; | |
1201 | |
1202 -- Create the parent AVP | |
1203 ret = dict_new ( DICT_AVP, &proxy_info_data, NULL, &proxy_info_avp ); | |
1204 | |
1205 -- Create the first child AVP. | |
1206 ret = dict_new ( DICT_TYPE, &di_type_data, NULL, &diameteridentity_type ); | |
1207 ret = dict_new ( DICT_AVP, &proxy_host_data, diameteridentity_type, &proxy_host_avp ); | |
1208 | |
1209 -- Create the other child AVP | |
1210 ret = dict_new ( DICT_AVP, &proxy_state_data, NULL, &proxy_state_avp ); | |
1211 | |
1212 -- Now we can create the rules. Both children AVP are mandatory. | |
1213 rule_data.rule_position = RULE_REQUIRED; | |
1214 rule_data.rule_min = -1; | |
1215 rule_data.rule_max = -1; | |
1216 | |
1217 rule_data.rule_avp = proxy_host_avp; | |
1218 ret = dict_new ( DICT_RULE, &rule_data, proxy_info_avp, NULL ); | |
1219 | |
1220 rule_data.rule_avp = proxy_state_avp; | |
1221 ret = dict_new ( DICT_RULE, &rule_data, proxy_info_avp, NULL ); | |
1222 } | |
1223 | |
1224 - dict_search and dict_getval are similar to previous examples. | |
1225 | |
1226 */ | |
1227 | |
1228 /* Define some hard-coded values */ | |
1229 /* Commands Codes */ | |
1230 #define CC_CAPABILITIES_EXCHANGE 257 | |
1231 #define CC_RE_AUTH 258 | |
1232 #define CC_ACCOUNTING 271 | |
1233 #define CC_ABORT_SESSION 274 | |
1234 #define CC_SESSION_TERMINATION 275 | |
1235 #define CC_DEVICE_WATCHDOG 280 | |
1236 #define CC_DISCONNECT_PEER 282 | |
1237 | |
1238 /* AVPs (Vendor 0) */ | |
1239 #define AC_PROXY_STATE 33 | |
1240 #define AC_HOST_IP_ADDRESS 257 | |
1241 #define AC_AUTH_APPLICATION_ID 258 | |
1242 #define AC_ACCT_APPLICATION_ID 259 | |
1243 #define AC_VENDOR_SPECIFIC_APPLICATION_ID 260 | |
1244 #define AC_REDIRECT_HOST_USAGE 261 | |
1245 #define AC_REDIRECT_MAX_CACHE_TIME 262 | |
1246 #define AC_SESSION_ID 263 | |
1247 #define AC_ORIGIN_HOST 264 | |
1248 #define AC_SUPPORTED_VENDOR_ID 265 | |
1249 #define AC_VENDOR_ID 266 | |
1250 #define AC_FIRMWARE_REVISION 267 | |
1251 #define AC_RESULT_CODE 268 | |
1252 #define AC_PRODUCT_NAME 269 | |
1253 #define AC_DISCONNECT_CAUSE 273 | |
1254 #define ACV_DC_REBOOTING 0 | |
1255 #define ACV_DC_BUSY 1 | |
1256 #define ACV_DC_NOT_FRIEND 2 | |
1257 #define AC_ORIGIN_STATE_ID 278 | |
1258 #define AC_FAILED_AVP 279 | |
1259 #define AC_PROXY_HOST 280 | |
1260 #define AC_ERROR_MESSAGE 281 | |
1261 #define AC_ROUTE_RECORD 282 | |
1262 #define AC_DESTINATION_REALM 283 | |
1263 #define AC_PROXY_INFO 284 | |
1264 #define AC_REDIRECT_HOST 292 | |
1265 #define AC_DESTINATION_HOST 293 | |
1266 #define AC_ERROR_REPORTING_HOST 294 | |
1267 #define AC_ORIGIN_REALM 296 | |
1268 #define AC_INBAND_SECURITY_ID 299 | |
1269 | |
1270 /* Error codes */ | |
1271 #define ER_DIAMETER_SUCCESS 2001 | |
1272 #define ER_DIAMETER_REALM_NOT_SERVED 3003 | |
1273 #define ER_DIAMETER_TOO_BUSY 3004 | |
1274 #define ER_DIAMETER_REDIRECT_INDICATION 3006 | |
1275 | |
1276 /* Iterator on the rules of a parent object */ | |
1277 int fd_dict_iterate_rules ( struct dict_object *parent, void * data, int (*cb)(void *, struct dict_rule_data *) ); | |
1278 | |
1279 | |
1280 /*============================================================*/ | |
1281 /* MESSAGES */ | |
1282 /*============================================================*/ | |
1283 | |
1284 /* The following types are opaque */ | |
1285 struct msg; /* A message: command with children AVPs (possibly grand children) */ | |
1286 struct avp; /* AVP object */ | |
1287 | |
1288 /* Some details about chaining: | |
1289 * | |
1290 * A message is made of a header ( msg ) and 0 or more AVPs ( avp ). | |
1291 * The structure is a kind of tree, where some AVPs (grouped AVPs) can contain other AVPs. | |
1292 * Exemple: | |
1293 * msg | |
1294 * |-avp | |
1295 * |-gavp | |
1296 * | |-avp | |
1297 * | |-avp | |
1298 * | \-avp | |
1299 * |-avp | |
1300 * \-avp | |
1301 * | |
1302 */ | |
1303 | |
1304 /* The following type is used to point to either a msg or an AVP */ | |
1305 typedef void msg_or_avp; | |
1306 | |
1307 /* The Diameter protocol version */ | |
1308 #define DIAMETER_VERSION 1 | |
1309 | |
1310 /* In the two following types, some fields are marked (READONLY). | |
1311 * This means that the content of these fields will be overwritten by the daemon so modifying it is useless. | |
1312 */ | |
1313 | |
1314 /* The following structure represents the header of a message. All data is in host byte order. */ | |
1315 struct msg_hdr { | |
1316 uint8_t msg_version; /* (READONLY) Version of Diameter: must be DIAMETER_VERSION. */ | |
1317 uint32_t msg_length; /* (READONLY)(3 bytes) indicates the length of the message */ | |
1318 uint8_t msg_flags; /* Message flags: CMD_FLAG_* */ | |
1319 command_code_t msg_code; /* (3 bytes) the command-code. See dictionary-api.h for more detail */ | |
1320 application_id_t msg_appl; /* The application issuing this message */ | |
1321 uint32_t msg_hbhid; /* The Hop-by-Hop identifier of the message */ | |
1322 uint32_t msg_eteid; /* The End-to-End identifier of the message */ | |
1323 }; | |
1324 | |
1325 /* The following structure represents the visible content of an AVP. All data is in host byte order. */ | |
1326 struct avp_hdr { | |
1327 avp_code_t avp_code; /* the AVP Code */ | |
1328 uint8_t avp_flags; /* AVP_FLAG_* flags */ | |
1329 uint32_t avp_len; /* (READONLY)(Only 3 bytes are used) the length of the AVP as described in the RFC */ | |
1330 vendor_id_t avp_vendor; /* Only used if AVP_FLAG_VENDOR is present */ | |
1331 union avp_value *avp_value; /* pointer to the value of the AVP. NULL means that the value is not set / not understood. | |
1332 One should not directly change this value. Use the msg_avp_setvalue function instead. | |
1333 The content of the pointed structure can be changed directly, with this restriction: | |
1334 if the AVP is an OctetString, and you change the value of the pointer avp_value->os.data, then | |
1335 you must call free() on the previous value, and the new one must be free()-able. | |
1336 */ | |
1337 }; | |
1338 | |
1339 /* The following enum is used to browse inside message hierarchy (msg, gavp, avp) */ | |
1340 enum msg_brw_dir { | |
1341 MSG_BRW_NEXT = 1, /* Get the next element at the same level, or NULL if this is the last element. */ | |
1342 MSG_BRW_PREV, /* Get the previous element at the same level, or NULL if this is the first element. */ | |
1343 MSG_BRW_FIRST_CHILD, /* Get the first child AVP of this element, if any. */ | |
1344 MSG_BRW_LAST_CHILD, /* Get the last child AVP of this element, if any. */ | |
1345 MSG_BRW_PARENT, /* Get the parent element of this element, if any. Only the msg_t object has no parent. */ | |
1346 MSG_BRW_WALK /* This is equivalent to FIRST_CHILD or NEXT or PARENT->next, first that is not NULL. Use this to walk inside all AVPs. */ | |
1347 }; | |
1348 | |
1349 /* Some flags used in the functions bellow */ | |
1350 #define MSGFL_ALLOC_ETEID 0x01 /* When creating a message, a new end-to-end ID is allocated and set in the message */ | |
1351 #define MSGFL_ANSW_ERROR 0x02 /* When creating an answer message, set the 'E' bit and use the generic error ABNF instead of command-specific ABNF */ | |
1352 #define MSGFL_MAX MSGFL_ANSW_ERROR /* The biggest valid flag value */ | |
1353 | |
1354 /**************************************************/ | |
1355 /* Message creation, manipulation, disposal */ | |
1356 /**************************************************/ | |
1357 /* | |
1358 * FUNCTION: fd_msg_avp_new | |
1359 * | |
1360 * PARAMETERS: | |
1361 * model : Pointer to a DICT_AVP dictionary object describing the avp to create, or NULL. | |
1362 * flags : Flags to use in creation (not used yet, should be 0). | |
1363 * avp : Upon success, pointer to the new avp is stored here. | |
1364 * | |
1365 * DESCRIPTION: | |
1366 * Create a new AVP instance. | |
1367 * | |
1368 * RETURN VALUE: | |
1369 * 0 : The AVP is created. | |
1370 * EINVAL : A parameter is invalid. | |
1371 * (other standard errors may be returned, too, with their standard meaning. Example: | |
1372 * ENOMEM : Memory allocation for the new avp failed.) | |
1373 */ | |
1374 int fd_msg_avp_new ( struct dict_object * model, int flags, struct avp ** avp ); | |
1375 | |
1376 /* | |
1377 * FUNCTION: fd_msg_new | |
1378 * | |
1379 * PARAMETERS: | |
1380 * model : Pointer to a DICT_COMMAND dictionary object describing the message to create, or NULL. | |
1381 * flags : combination of MSGFL_* flags. | |
1382 * msg : Upon success, pointer to the new message is stored here. | |
1383 * | |
1384 * DESCRIPTION: | |
1385 * Create a new empty Diameter message. | |
1386 * | |
1387 * RETURN VALUE: | |
1388 * 0 : The message is created. | |
1389 * EINVAL : A parameter is invalid. | |
1390 * (other standard errors may be returned, too, with their standard meaning. Example: | |
1391 * ENOMEM : Memory allocation for the new message failed.) | |
1392 */ | |
1393 int fd_msg_new ( struct dict_object * model, int flags, struct msg ** msg ); | |
1394 | |
1395 /* | |
1396 * FUNCTION: msg_new_answer_from_req | |
1397 * | |
1398 * PARAMETERS: | |
1399 * dict : Pointer to the dictionary containing the model of the query. | |
1400 * msg : The location of the query on function call. Updated by the location of answer message on return. | |
1401 * flag : Pass MSGFL_ANSW_ERROR to indicate if the answer is an error message (will set the 'E' bit) | |
1402 * | |
1403 * DESCRIPTION: | |
1404 * This function creates the empty answer message corresponding to a request. | |
1405 * The header is set properly (R flag, ccode, appid, hbhid, eteid) | |
1406 * The Session-Id AVP is copied if present. | |
1407 * The calling code should usually call fd_msg_rescode_set function on the answer. | |
1408 * Upon return, the original query may be retrieved by calling fd_msg_answ_getq on the message. | |
1409 * | |
1410 * RETURN VALUE: | |
1411 * 0 : Operation complete. | |
1412 * !0 : an error occurred. | |
1413 */ | |
1414 int fd_msg_new_answer_from_req ( struct dictionary * dict, struct msg ** msg, int flag ); | |
1415 | |
1416 /* | |
1417 * FUNCTION: fd_msg_browse | |
1418 * | |
1419 * PARAMETERS: | |
1420 * reference : Pointer to a struct msg or struct avp. | |
1421 * dir : Direction for browsing | |
1422 * found : If not NULL, updated with the element that has been found, if any, or NULL if no element was found / an error occurred. | |
1423 * depth : If not NULL, points to an integer representing the "depth" of this object in the tree. This is a relative value, updated on return. | |
1424 * | |
1425 * DESCRIPTION: | |
1426 * Explore the content of a message object (hierarchy). If "found" is null, only error checking is performed. | |
1427 * If "depth" is provided, it is updated as follow on successful function return: | |
1428 * - not modified for MSG_BRW_NEXT and MSG_BRW_PREV. | |
1429 * - *depth = *depth + 1 for MSG_BRW_FIRST_CHILD and MSG_BRW_LAST_CHILD. | |
1430 * - *depth = *depth - 1 for MSG_BRW_PARENT. | |
1431 * - *depth = *depth + X for MSG_BRW_WALK, with X between 1 (returned the 1st child) and -N (returned the Nth parent's next). | |
1432 * | |
1433 * RETURN VALUE: | |
1434 * 0 : found has been updated (if non NULL). | |
1435 * EINVAL : A parameter is invalid. | |
1436 * ENOENT : No element has been found where requested, and "found" was NULL (otherwise, *found is set to NULL and 0 is returned). | |
1437 */ | |
1438 int fd_msg_browse_internal ( msg_or_avp * reference, enum msg_brw_dir dir, msg_or_avp ** found, int * depth ); | |
1439 /* Macro to avoid having to cast the third parameter everywhere */ | |
1440 #define fd_msg_browse( ref, dir, found, depth ) \ | |
1441 fd_msg_browse_internal( (ref), (dir), (void *)(found), (depth) ) | |
1442 | |
1443 | |
1444 /* | |
1445 * FUNCTION: fd_msg_avp_add | |
1446 * | |
1447 * PARAMETERS: | |
1448 * reference : Pointer to a valid msg or avp. | |
1449 * dir : location where the new AVP should be inserted, relative to the reference. MSG_BRW_PARENT and MSG_BRW_WALK are not valid. | |
1450 * avp : pointer to the AVP object that must be inserted. | |
1451 * | |
1452 * DESCRIPTION: | |
1453 * Adds an AVP into an object that can contain it: grouped AVP or message. | |
1454 * | |
1455 * RETURN VALUE: | |
1456 * 0 : The AVP has been added. | |
1457 * EINVAL : A parameter is invalid. | |
1458 */ | |
1459 int fd_msg_avp_add ( msg_or_avp * reference, enum msg_brw_dir dir, struct avp *avp); | |
1460 | |
1461 /* | |
1462 * FUNCTION: fd_msg_search_avp | |
1463 * | |
1464 * PARAMETERS: | |
1465 * msg : The message structure in which to search the AVP. | |
1466 * what : The dictionary model of the AVP to search. | |
1467 * avp : location where the AVP reference is stored if found. | |
1468 * | |
1469 * DESCRIPTION: | |
1470 * Search the first top-level AVP of a given model inside a message. | |
1471 * Note: only the first instance of the AVP is returned by this function. | |
1472 * Note: only top-level AVPs are searched, not inside grouped AVPs. | |
1473 * Use msg_browse if you need more advanced research features. | |
1474 * | |
1475 * RETURN VALUE: | |
1476 * 0 : The AVP has been found. | |
1477 * EINVAL : A parameter is invalid. | |
1478 * ENOENT : No AVP has been found, and "avp" was NULL (otherwise, *avp is set to NULL and 0 returned). | |
1479 */ | |
1480 int fd_msg_search_avp ( struct msg * msg, struct dict_object * what, struct avp ** avp ); | |
1481 | |
1482 /* | |
1483 * FUNCTION: fd_msg_free | |
1484 * | |
1485 * PARAMETERS: | |
1486 * object : pointer to the message or AVP object that must be unlinked and freed. | |
1487 * | |
1488 * DESCRIPTION: | |
1489 * Unlink and free a message or AVP object and its children. | |
1490 * If the object is an AVP linked into a message, the AVP is removed before being freed. | |
1491 * | |
1492 * RETURN VALUE: | |
1493 * 0 : The message has been freed. | |
1494 * EINVAL : A parameter is invalid. | |
1495 */ | |
1496 int fd_msg_free ( msg_or_avp * object ); | |
1497 | |
1498 /***************************************/ | |
1499 /* Dump functions */ | |
1500 /***************************************/ | |
1501 /* | |
1502 * FUNCTION: fd_msg_dump_* | |
1503 * | |
1504 * PARAMETERS: | |
1505 * level : the log level (INFO, FULL, ...) at which the object is dumped | |
1506 * obj : A msg or avp object. | |
1507 * | |
1508 * DESCRIPTION: | |
1509 * These functions dump the content of a message to the debug log | |
1510 * either recursively or only the object itself. | |
1511 * | |
1512 * RETURN VALUE: | |
1513 * - | |
1514 */ | |
1515 void fd_msg_dump_walk ( int level, msg_or_avp *obj ); | |
1516 void fd_msg_dump_one ( int level, msg_or_avp *obj ); | |
1517 | |
1518 | |
1519 /*********************************************/ | |
1520 /* Message metadata management functions */ | |
1521 /*********************************************/ | |
1522 /* | |
1523 * FUNCTION: fd_msg_model | |
1524 * | |
1525 * PARAMETERS: | |
1526 * reference : Pointer to a valid msg or avp. | |
1527 * model : on success, pointer to the dictionary model of this command or AVP. NULL if the model is unknown. | |
1528 * | |
1529 * DESCRIPTION: | |
1530 * Retrieve the dictionary object describing this message or avp. If the object is unknown or the fd_msg_parse_dict has not been called, | |
1531 * *model is set to NULL. | |
1532 * | |
1533 * RETURN VALUE: | |
1534 * 0 : The model has been set. | |
1535 * EINVAL : A parameter is invalid. | |
1536 */ | |
1537 int fd_msg_model ( msg_or_avp * reference, struct dict_object ** model ); | |
1538 | |
1539 /* | |
1540 * FUNCTION: fd_msg_hdr | |
1541 * | |
1542 * PARAMETERS: | |
1543 * msg : Pointer to a valid message object. | |
1544 * pdata : Upon success, pointer to the msg_hdr structure of this message. The fields may be modified. | |
1545 * | |
1546 * DESCRIPTION: | |
1547 * Retrieve location of modifiable section of a message. | |
1548 * | |
1549 * RETURN VALUE: | |
1550 * 0 : The location has been written. | |
1551 * EINVAL : A parameter is invalid. | |
1552 */ | |
1553 int fd_msg_hdr ( struct msg *msg, struct msg_hdr **pdata ); | |
1554 | |
1555 /* | |
1556 * FUNCTION: fd_msg_avp_hdr | |
1557 * | |
1558 * PARAMETERS: | |
1559 * avp : Pointer to a valid avp object. | |
1560 * pdata : Upon success, pointer to the avp_hdr structure of this avp. The fields may be modified. | |
1561 * | |
1562 * DESCRIPTION: | |
1563 * Retrieve location of modifiable data of an avp. | |
1564 * | |
1565 * RETURN VALUE: | |
1566 * 0 : The location has been written. | |
1567 * EINVAL : A parameter is invalid. | |
1568 */ | |
1569 int fd_msg_avp_hdr ( struct avp *avp, struct avp_hdr **pdata ); | |
1570 | |
1571 /* | |
1572 * FUNCTION: fd_msg_answ_associate, fd_msg_answ_getq, fd_msg_answ_detach | |
1573 * | |
1574 * PARAMETERS: | |
1575 * answer : the received answer message | |
1576 * query : the corresponding query that had been sent | |
1577 * | |
1578 * DESCRIPTION: | |
1579 * fd_msg_answ_associate associates a query msg with the received answer. | |
1580 * Query is retrieved with fd_msg_answ_getq. | |
1581 * If answer message is freed, the query is also freed. | |
1582 * If the msg_answ_detach function is called, the association is removed. | |
1583 * This is meant to be called from the daemon only. | |
1584 * | |
1585 * RETURN VALUE: | |
1586 * 0 : ok | |
1587 * EINVAL: a parameter is invalid | |
1588 */ | |
1589 int fd_msg_answ_associate( struct msg * answer, struct msg * query ); | |
1590 int fd_msg_answ_getq ( struct msg * answer, struct msg ** query ); | |
1591 int fd_msg_answ_detach ( struct msg * answer ); | |
1592 | |
1593 /* | |
1594 * FUNCTION: fd_msg_anscb_associate, fd_msg_anscb_get | |
1595 * | |
1596 * PARAMETERS: | |
1597 * msg : the answer message | |
1598 * anscb : the callback to associate with the message | |
1599 * data : the data to pass to the callback | |
1600 * | |
1601 * DESCRIPTION: | |
1602 * Associate or retrieve a callback with an answer message. | |
1603 * This is meant to be called from the daemon only. | |
1604 * | |
1605 * RETURN VALUE: | |
1606 * 0 : ok | |
1607 * EINVAL: a parameter is invalid | |
1608 */ | |
1609 int fd_msg_anscb_associate( struct msg * msg, void ( *anscb)(void *, struct msg **), void * data ); | |
1610 int fd_msg_anscb_get ( struct msg * msg, void (**anscb)(void *, struct msg **), void ** data ); | |
1611 | |
1612 /* | |
1613 * FUNCTION: fd_msg_rt_associate, fd_msg_rt_get | |
1614 * | |
1615 * PARAMETERS: | |
1616 * msg : the query message to be sent | |
1617 * list : the ordered list of possible next-peers | |
1618 * | |
1619 * DESCRIPTION: | |
1620 * Associate a routing list with a query, and retrieve it. | |
1621 * If the message is freed, the list is also freed. | |
1622 * | |
1623 * RETURN VALUE: | |
1624 * 0 : ok | |
1625 * EINVAL: a parameter is invalid | |
1626 */ | |
1627 int fd_msg_rt_associate( struct msg * msg, struct fd_list ** list ); | |
1628 int fd_msg_rt_get ( struct msg * msg, struct fd_list ** list ); | |
1629 | |
1630 /* | |
1631 * FUNCTION: fd_msg_is_routable | |
1632 * | |
1633 * PARAMETERS: | |
1634 * msg : A msg object. | |
1635 * | |
1636 * DESCRIPTION: | |
1637 * This function returns a boolean telling if a given message is routable in the Diameter network, | |
1638 * or if it is a local link message only (ex: CER/CEA, DWR/DWA, ...). | |
1639 * | |
1640 * RETURN VALUE: | |
1641 * 0 : The message is not routable / an error occurred. | |
1642 * 1 : The message is routable. | |
1643 */ | |
1644 int fd_msg_is_routable ( struct msg * msg ); | |
1645 | |
1646 /* | |
1647 * FUNCTION: fd_msg_source_(g/s)et | |
1648 * | |
1649 * PARAMETERS: | |
1650 * msg : A msg object. | |
1651 * diamid : The diameter id of the peer from which this message was received. | |
1652 * hash : The hash for the diamid value. | |
1653 * add_rr : if true, a Route-Record AVP is added to the message with content diamid. In that case, dict must be supplied. | |
1654 * dict : a dictionary with definition of Route-Record AVP (if add_rr is true) | |
1655 * | |
1656 * DESCRIPTION: | |
1657 * Store or retrieve the diameted id of the peer from which this message was received. | |
1658 * Will be used for example by the routing module to add the Route-Record AVP in forwarded requests, | |
1659 * or to direct answers to the appropriate peer. | |
1660 * | |
1661 * RETURN VALUE: | |
1662 * 0 : Operation complete. | |
1663 * !0 : an error occurred. | |
1664 */ | |
1665 int fd_msg_source_set( struct msg * msg, char * diamid, uint32_t hash, int add_rr, struct dictionary * dict ); | |
1666 int fd_msg_source_get( struct msg * msg, char ** diamid, uint32_t *hash ); | |
1667 | |
1668 /* | |
1669 * FUNCTION: fd_msg_eteid_get | |
1670 * | |
1671 * PARAMETERS: | |
1672 * - | |
1673 * | |
1674 * DESCRIPTION: | |
1675 * Get a new unique end-to-end id value for the local peer. | |
1676 * | |
1677 * RETURN VALUE: | |
1678 * The new assigned value. No error code is defined. | |
1679 */ | |
1680 uint32_t fd_msg_eteid_get ( void ); | |
1681 | |
1682 | |
1683 /***************************************/ | |
1684 /* Manage AVP values */ | |
1685 /***************************************/ | |
1686 | |
1687 /* | |
1688 * FUNCTION: fd_msg_avp_setvalue | |
1689 * | |
1690 * PARAMETERS: | |
1691 * avp : Pointer to a valid avp object with a NULL avp_value pointer. The model must be known. | |
1692 * value : pointer to an avp_value. The content will be COPIED into the internal storage area. | |
1693 * If data type is an octetstring, the data is also copied. | |
1694 * If value is a NULL pointer, the previous data is erased and value is unset in the AVP. | |
1695 * | |
1696 * DESCRIPTION: | |
1697 * Initialize the avp_value field of an AVP header. | |
1698 * | |
1699 * RETURN VALUE: | |
1700 * 0 : The avp_value pointer has been set. | |
1701 * EINVAL : A parameter is invalid. | |
1702 */ | |
1703 int fd_msg_avp_setvalue ( struct avp *avp, union avp_value *value ); | |
1704 | |
1705 /* | |
1706 * FUNCTION: fd_msg_avp_value_encode | |
1707 * | |
1708 * PARAMETERS: | |
1709 * avp : Pointer to a valid avp object with a NULL avp_value. The model must be known. | |
1710 * data : Pointer to the data that must be encoded as AVP value and stored in the AVP. | |
1711 * This is only valid for AVPs of derived type for which type_data_encode callback is set. (ex: Address type) | |
1712 * | |
1713 * DESCRIPTION: | |
1714 * Initialize the avp_value field of an AVP object from formatted data, using the AVP's type "type_data_encode" callback. | |
1715 * | |
1716 * RETURN VALUE: | |
1717 * 0 : The avp_value has been set. | |
1718 * EINVAL : A parameter is invalid. | |
1719 * ENOTSUP : There is no appropriate callback registered with this AVP's type. | |
1720 */ | |
1721 int fd_msg_avp_value_encode ( void *data, struct avp *avp ); | |
1722 | |
1723 /* | |
1724 * FUNCTION: fd_msg_avp_value_interpret | |
1725 * | |
1726 * PARAMETERS: | |
1727 * avp : Pointer to a valid avp object with a non-NULL avp_value value. | |
1728 * data : Upon success, formatted interpretation of the AVP value is stored here. | |
1729 * | |
1730 * DESCRIPTION: | |
1731 * Interpret the content of an AVP of Derived type and store the result in data pointer. The structure | |
1732 * of the data pointer is dependent on the AVP type. This function calls the "type_data_interpret" callback | |
1733 * of the type. | |
1734 * | |
1735 * RETURN VALUE: | |
1736 * 0 : The avp_value has been set. | |
1737 * EINVAL : A parameter is invalid. | |
1738 * ENOTSUP : There is no appropriate callback registered with this AVP's type. | |
1739 */ | |
1740 int fd_msg_avp_value_interpret ( struct avp *avp, void *data ); | |
1741 | |
1742 | |
1743 /***************************************/ | |
1744 /* Message parsing functions */ | |
1745 /***************************************/ | |
1746 | |
1747 /* | |
1748 * FUNCTION: fd_msg_bufferize | |
1749 * | |
1750 * PARAMETERS: | |
1751 * msg : A valid msg object. All AVPs must have a value set. | |
1752 * buffer : Upon success, this points to a buffer (malloc'd) containing the message ready for network transmission (or security transformations). | |
1753 * The buffer may be freed after use. | |
1754 * len : if not NULL, the size of the buffer is written here. In any case, this size is updated in the msg header. | |
1755 * | |
1756 * DESCRIPTION: | |
1757 * Renders a message in memory as a buffer that can be sent over the network to the next peer. | |
1758 * | |
1759 * RETURN VALUE: | |
1760 * 0 : The location has been written. | |
1761 * EINVAL : The buffer does not contain a valid Diameter message. | |
1762 * ENOMEM : Unable to allocate enough memory to create the buffer object. | |
1763 */ | |
1764 int fd_msg_bufferize ( struct msg * msg, unsigned char ** buffer, size_t * len ); | |
1765 | |
1766 /* | |
1767 * FUNCTION: fd_msg_parse_buffer | |
1768 * | |
1769 * PARAMETERS: | |
1770 * buffer : Pointer to a buffer containing a message received from the network. | |
1771 * buflen : the size in bytes of the buffer. | |
1772 * msg : Upon success, this points to a valid msg object. No AVP value is resolved in this object, nor grouped AVP. | |
1773 * | |
1774 * DESCRIPTION: | |
1775 * This function parses a buffer an creates a msg object to represent the structure of the message. | |
1776 * Since no dictionary lookup is performed, the values of the AVPs are not interpreted. To interpret the values, | |
1777 * the returned message object must be passed to fd_msg_parse_dict function. | |
1778 * The buffer pointer is saved inside the message and will be freed when not needed anymore. | |
1779 * | |
1780 * RETURN VALUE: | |
1781 * 0 : The location has been written. | |
1782 * ENOMEM : Unable to allocate enough memory to create the msg object. | |
1783 * EBADMSG : The buffer does not contain a valid Diameter message (or is truncated). | |
1784 * EINVAL : A parameter is invalid. | |
1785 */ | |
1786 int fd_msg_parse_buffer ( unsigned char ** buffer, size_t buflen, struct msg ** msg ); | |
1787 | |
1788 /* | |
1789 * FUNCTION: fd_msg_parse_dict | |
1790 * | |
1791 * PARAMETERS: | |
1792 * object : A msg or AVP object as returned by fd_msg_parse_buffer. | |
1793 * dict : the dictionary containing the objects definitions to use for resolving all AVPs. | |
1794 * | |
1795 * DESCRIPTION: | |
1796 * This function looks up for the command and each children AVP definitions in the dictionary. | |
1797 * If the dictionary definition is found, avp_model is set and the value of the AVP is interpreted accordingly and: | |
1798 * - for grouped AVPs, the children AVP are created and interpreted also. | |
1799 * - for numerical AVPs, the value is converted to host byte order and saved in the avp_value field. | |
1800 * - for octetstring AVPs, the string is copied into a new buffer and its address is saved in avp_value. | |
1801 * If the dictionary definition is not found, avp_model is set to NULL and | |
1802 * the content of the AVP is saved as an octetstring in an internal structure. avp_value is NULL. | |
1803 * As a result, after this function has been called, there is no more dependency of the msg object to the message buffer, that is be freed. | |
1804 * | |
1805 * RETURN VALUE: | |
1806 * 0 : The message has been fully parsed as described. | |
1807 * EINVAL : The msg parameter is invalid for this operation. | |
1808 * ENOMEM : Unable to allocate enough memory to complete the operation. | |
1809 * ENOTSUP : No dictionary definition for the command or one of the mandatory AVP was found. | |
1810 */ | |
1811 int fd_msg_parse_dict ( msg_or_avp * object, struct dictionary * dict ); | |
1812 | |
1813 /* | |
1814 * FUNCTION: fd_msg_parse_rules | |
1815 * | |
1816 * PARAMETERS: | |
1817 * object : A msg or grouped avp object that must be verified. | |
1818 * dict : The dictionary containing the rules definitions. | |
1819 * rule : If not NULL, the first conflicting rule will be saved here if a conflict is found. | |
1820 * | |
1821 * DESCRIPTION: | |
1822 * Check that the children of the object do not conflict with the dictionary rules (ABNF compliance). | |
1823 * | |
1824 * RETURN VALUE: | |
1825 * 0 : The message has been fully parsed and complies to the defined rules. | |
1826 * EBADMSG : A conflict was detected, or a mandatory AVP is unknown in the dictionary. | |
1827 * EINVAL : The msg or avp object is invalid for this operation. | |
1828 * ENOMEM : Unable to allocate enough memory to complete the operation. | |
1829 */ | |
1830 int fd_msg_parse_rules ( msg_or_avp * object, struct dictionary * dict, struct dict_object ** rule); | |
1831 | |
1832 | |
1833 /* | |
1834 * FUNCTION: fd_msg_update_length | |
1835 * | |
1836 * PARAMETERS: | |
1837 * object : Pointer to a valid msg or avp. | |
1838 * | |
1839 * DESCRIPTION: | |
1840 * Update the length field of the object passed as parameter. | |
1841 * As a side effect, all children objects are also updated. Therefore, all avp_value fields of | |
1842 * the children AVPs must be set, or an error will occur. | |
1843 * | |
1844 * RETURN VALUE: | |
1845 * 0 : The size has been recomputed. | |
1846 * EINVAL : A parameter is invalid. | |
1847 */ | |
1848 int fd_msg_update_length ( msg_or_avp * object ); | |
1849 | |
1850 | |
1851 | |
1852 /*============================================================*/ | |
1853 /* MESSAGE QUEUES */ | |
1854 /*============================================================*/ | |
1855 | |
1856 /* Management of queues of messages */ | |
1857 | |
1858 /* A message queue is an opaque object */ | |
1859 struct mqueue; | |
1860 | |
1861 /* | |
1862 * FUNCTION: fd_mq_new | |
1863 * | |
1864 * PARAMETERS: | |
1865 * queue : Upon success, a pointer to the new message queue is saved here. | |
1866 * | |
1867 * DESCRIPTION: | |
1868 * Create a new empty message queue. | |
1869 * | |
1870 * RETURN VALUE : | |
1871 * 0 : The message queue has been initialized successfully. | |
1872 * EINVAL : The parameter is invalid. | |
1873 * ENOMEM : Not enough memory to complete the creation. | |
1874 */ | |
1875 int fd_mq_new ( struct mqueue ** queue ); | |
1876 | |
1877 /* | |
1878 * FUNCTION: fd_mq_del | |
1879 * | |
1880 * PARAMETERS: | |
1881 * queue : Pointer to an empty message queue to delete. | |
1882 * | |
1883 * DESCRIPTION: | |
1884 * Destroys a message queue. This is only possible if no thread is waiting for a message, | |
1885 * and the queue is empty. | |
1886 * | |
1887 * RETURN VALUE: | |
1888 * 0 : The message queue has been destroyed successfully. | |
1889 * EINVAL : The parameter is invalid. | |
1890 */ | |
1891 int fd_mq_del ( struct mqueue ** queue ); | |
1892 | |
1893 /* | |
1894 * FUNCTION: fd_mq_length | |
1895 * | |
1896 * PARAMETERS: | |
1897 * queue : The queue from which to retrieve the length. | |
1898 * length : Upon success, the current number of messages in the queue is stored here. | |
1899 * | |
1900 * DESCRIPTION: | |
1901 * Retrieve the number of messages pending in a queue. | |
1902 * | |
1903 * RETURN VALUE: | |
1904 * 0 : The length of the queue has been written. | |
1905 * EINVAL : A parameter is invalid. | |
1906 */ | |
1907 int fd_mq_length ( struct mqueue * queue, int * length ); | |
1908 int fd_mq_length_noerr ( struct mqueue * queue ); /* alternate with no error checking */ | |
1909 | |
1910 /* | |
1911 * FUNCTION: fd_mq_setthrhd | |
1912 * | |
1913 * PARAMETERS: | |
1914 * queue : The queue for which the thresholds are being set. | |
1915 * data : An opaque pointer that is passed to h_cb and l_cb callbacks. | |
1916 * high : The high-level threshold. If the number of elements in the queue increase to this value, h_cb is called. | |
1917 * h_cb : if not NULL, a callback to call when the queue lengh is bigger than "high". | |
1918 * low : The low-level threshold. Must be < high. | |
1919 * l_cb : If the number of elements decrease to low, this callback is called. | |
1920 * | |
1921 * DESCRIPTION: | |
1922 * This function allows to adjust the number of producer / consumer threads of a queue. | |
1923 * If the consumer are slower than the producers, the number of messages in the queue increase. | |
1924 * By setting a "high" value, we allow a callback to be called when this number is too high. | |
1925 * The typical use would be to create an additional consumer thread in this callback. | |
1926 * If the queue continues to grow, the callback will be called again when the length is 2 * high, then 3*high, ... N * high | |
1927 * (the callback itself may implement a limit on the number of consumers that can be created) | |
1928 * When the queue starts to decrease, and the number of elements go under ((N - 1) * high + low, the l_cb callback is called | |
1929 * and would typially stop one of the consumer threads. If the queue continue to reduce, l_cb is again called at (N-2)*high + low, | |
1930 * and so on. | |
1931 * | |
1932 * Since there is no destructor for the data pointer, if cleanup operations are required, they should be performed in | |
1933 * l_cb when the length of the queue is becoming < low. | |
1934 * | |
1935 * Note that the callbacks are called synchronously, during fd_mq_post or fd_mq_get. Their operation should be quick. | |
1936 * | |
1937 * RETURN VALUE: | |
1938 * 0 : The thresholds have been set | |
1939 * EINVAL : A parameter is invalid. | |
1940 */ | |
1941 int fd_mq_setthrhd ( struct mqueue * queue, void * data, uint16_t high, void (*h_cb)(struct mqueue *, void **), uint16_t low, void (*l_cb)(struct mqueue *, void **) ); | |
1942 | |
1943 /* | |
1944 * FUNCTION: fd_mq_post | |
1945 * | |
1946 * PARAMETERS: | |
1947 * queue : The queue in which the message must be posted. | |
1948 * msg : The message that is put in the queue. | |
1949 * | |
1950 * DESCRIPTION: | |
1951 * A message is added in a queue. Messages are retrieved from the queue (in FIFO order) | |
1952 * with the fd_mq_get, fd_mq_tryget, or fd_mq_timedget functions. | |
1953 * | |
1954 * RETURN VALUE: | |
1955 * 0 : The message is queued. | |
1956 * EINVAL : A parameter is invalid. | |
1957 * ENOMEM : Not enough memory to complete the operation. | |
1958 */ | |
1959 int fd_mq_post ( struct mqueue * queue, struct msg ** msg ); | |
1960 | |
1961 /* | |
1962 * FUNCTION: fd_mq_get | |
1963 * | |
1964 * PARAMETERS: | |
1965 * queue : The queue from which the message must be retrieved. | |
1966 * msg : On return, the first message of the queue is stored here. | |
1967 * | |
1968 * DESCRIPTION: | |
1969 * This function retrieves a message from a queue. If the queue is empty, the function will block the | |
1970 * thread until a new message is posted to the queue, or until the thread is canceled (in which case the | |
1971 * function does not return). | |
1972 * | |
1973 * RETURN VALUE: | |
1974 * 0 : A new message has been retrieved. | |
1975 * EINVAL : A parameter is invalid. | |
1976 */ | |
1977 int fd_mq_get ( struct mqueue * queue, struct msg ** msg ); | |
1978 | |
1979 /* | |
1980 * FUNCTION: fd_mq_tryget | |
1981 * | |
1982 * PARAMETERS: | |
1983 * queue : The queue from which the message must be retrieved. | |
1984 * msg : On return, the message is stored here. | |
1985 * | |
1986 * DESCRIPTION: | |
1987 * This function is similar to fd_mq_get, except that it will not block if | |
1988 * the queue is empty, but return EWOULDBLOCK instead. | |
1989 * | |
1990 * RETURN VALUE: | |
1991 * 0 : A new message has been retrieved. | |
1992 * EINVAL : A parameter is invalid. | |
1993 * EWOULDBLOCK : The queue was empty. | |
1994 */ | |
1995 int fd_mq_tryget ( struct mqueue * queue, struct msg ** msg ); | |
1996 | |
1997 /* | |
1998 * FUNCTION: fd_mq_timedget | |
1999 * | |
2000 * PARAMETERS: | |
2001 * queue : The queue from which the message must be retrieved. | |
2002 * msg : On return, the message is stored here. | |
2003 * abstime : the absolute time until which we allow waiting for a message. | |
2004 * | |
2005 * DESCRIPTION: | |
2006 * This function is similar to fd_mq_get, except that it will block if the queue is empty | |
2007 * only until the absolute time abstime (see pthread_cond_timedwait for + info). | |
2008 * If the queue is still empty when the time expires, the function returns ETIMEDOUT | |
2009 * | |
2010 * RETURN VALUE: | |
2011 * 0 : A new message has been retrieved. | |
2012 * EINVAL : A parameter is invalid. | |
2013 * ETIMEDOUT : The time out has passed and no message has been received. | |
2014 */ | |
2015 int fd_mq_timedget ( struct mqueue * queue, struct msg ** msg, const struct timespec *abstime ); | |
2016 | |
2017 #endif /* _LIBFREEDIAMETER_H */ |