Minor connection API fixes
[freeradius.git] / src / include / connection.h
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 #ifndef FR_CONNECTION_H
17 #define FR_CONNECTION_H
18 /*
19  * $Id$
20  *
21  * @file connection.h
22  * @brief Structures, prototypes and global variables for server connection pools.
23  *
24  * @copyright 2012  The FreeRADIUS server project
25  * @copyright 2012  Alan DeKok <aland@deployingradius.com>
26  */
27
28 RCSIDH(connection_h, "$Id$")
29
30 #include <freeradius-devel/conffile.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 typedef struct fr_connection_pool_t fr_connection_pool_t;
37
38 /** Create a new connection handle
39  *
40  * This function will be called whenever the connection pool manager needs
41  * to spawn a new connection, and on reconnect.
42  *
43  * Memory should be talloced in the parent context to hold the module's
44  * connection structure. The parent context is allocated in the NULL
45  * context, but will be freed when fr_connection_t is freed.
46  *
47  * There is no delete callback, so operations such as closing sockets and
48  * freeing library connection handles should be done by a destructor attached
49  * to memory allocated beneath ctx.
50  *
51  * @note A function pointer matching this prototype must be passed
52  * to fr_connection_pool_init.
53  *
54  * @param[in,out] ctx to allocate memory in.
55  * @param[in] opaque pointer passed to fr_connection_pool_init.
56  * @return NULL on error, else a connection handle.
57  */
58 typedef void *(*fr_connection_create_t)(TALLOC_CTX *ctx, void *opaque);
59
60 /** Check a connection handle is still viable
61  *
62  * Should check the state  of a connection handle.
63  *
64  * @note NULL may be passed to fr_connection_init, if there is no way to check
65  * the state of a connection handle.
66  * @note Not currently use by connection pool manager.
67  * @param[in] opaque pointer passed to fr_connection_pool_init.
68  * @param[in] connection handle returned by fr_connection_create_t.
69  * @return < 0 on error or if the connection is unusable, else 0.
70  */
71 typedef int (*fr_connection_alive_t)(void *opaque, void *connection);
72
73 fr_connection_pool_t *fr_connection_pool_init(CONF_SECTION *cs,
74                                               void *opaque,
75                                               fr_connection_create_t c,
76                                               fr_connection_alive_t a,
77                                               char const *prefix);
78 void fr_connection_pool_delete(fr_connection_pool_t *pool);
79
80 void *fr_connection_get(fr_connection_pool_t *pool);
81 int fr_connection_get_num(fr_connection_pool_t *pool);
82 void fr_connection_release(fr_connection_pool_t *pool, void *conn);
83 void *fr_connection_reconnect(fr_connection_pool_t *pool, void *conn);
84 int fr_connection_del(fr_connection_pool_t *pool, void *conn);
85
86 #ifdef __cplusplus
87 }
88 #endif
89
90 #endif /* FR_CONNECTION_H*/