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