The FreeRADIUS server
$Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
|
Simple state machine for managing connection states. More...
#include <freeradius-devel/util/event.h>
#include <freeradius-devel/util/table.h>
#include <freeradius-devel/util/talloc.h>
Go to the source code of this file.
Data Structures | |
struct | connection_conf_t |
struct | connection_funcs_t |
Holds a complete set of functions for a connection. More... | |
struct | connection_pub_s |
Public fields for the connection. More... | |
Macros | |
#define | _CONST const |
Typedefs | |
typedef void(* | connection_close_t) (fr_event_list_t *el, void *h, void *uctx) |
Notification that the connection has errored and must be closed. More... | |
typedef connection_state_t(* | connection_failed_t) (void *h, connection_state_t state, void *uctx) |
Notification that a connection attempt has failed. More... | |
typedef connection_state_t(* | connection_init_t) (void **h_out, connection_t *conn, void *uctx) |
Callback for the initialise state. More... | |
typedef connection_state_t(* | connection_open_t) (fr_event_list_t *el, void *h, void *uctx) |
Notification that the connection is now open. More... | |
typedef connection_state_t(* | connection_shutdown_t) (fr_event_list_t *el, void *h, void *uctx) |
Start the process of gracefully shutting down the connection. More... | |
typedef struct connection_pub_s | connection_t |
typedef struct connection_watch_entry_s | connection_watch_entry_t |
typedef void(* | connection_watch_t) (connection_t *conn, connection_state_t prev, connection_state_t state, void *uctx) |
Receive a notification when a connection enters a particular state. More... | |
Variables | |
fr_table_num_ordered_t const | connection_states [] |
size_t | connection_states_len |
Add watcher functions that get called before (pre) the state callback and after (post) | |
connection_watch_entry_t * | connection_add_watch_post (connection_t *conn, connection_state_t state, connection_watch_t watch, bool oneshot, void const *uctx) |
Add a callback to be executed after a state function has been called. More... | |
connection_watch_entry_t * | connection_add_watch_pre (connection_t *conn, connection_state_t state, connection_watch_t watch, bool oneshot, void const *uctx) |
Add a callback to be executed before a state function has been called. More... | |
int | connection_del_watch_post (connection_t *conn, connection_state_t state, connection_watch_t watch) |
Remove a watch function from a post list. More... | |
int | connection_del_watch_pre (connection_t *conn, connection_state_t state, connection_watch_t watch) |
Remove a watch function from a pre list. More... | |
void | connection_watch_disable (connection_watch_entry_t *entry) |
Disable a watcher. More... | |
void | connection_watch_enable (connection_watch_entry_t *entry) |
Enable a watcher. More... | |
void | connection_watch_enable_set_uctx (connection_watch_entry_t *entry, void const *uctx) |
Enable a watcher and replace the uctx. More... | |
bool | connection_watch_is_enabled (connection_watch_entry_t *entry) |
Return the state of a watch entry. More... | |
void | connection_watch_set_uctx (connection_watch_entry_t *entry, void const *uctx) |
Change the uctx of an entry. More... | |
Statistics | |
uint64_t | connection_get_num_reconnected (connection_t const *conn) |
Return the number of times we've attempted to establish or re-establish this connection. More... | |
uint64_t | connection_get_num_timed_out (connection_t const *conn) |
Return the number of times this connection has timed out whilst connecting. More... | |
Signal the connection to change states | |
void | connection_signal_connected (connection_t *conn) |
Asynchronously signal that the connection is open. More... | |
void | connection_signal_halt (connection_t *conn) |
Shuts down a connection ungracefully. More... | |
void | connection_signal_init (connection_t *conn) |
Asynchronously signal a halted connection to start. More... | |
void | connection_signal_reconnect (connection_t *conn, connection_reason_t reason) |
Asynchronously signal the connection should be reconnected. More... | |
void | connection_signal_shutdown (connection_t *conn) |
Shuts down a connection gracefully. More... | |
void | connection_signals_pause (connection_t *conn) |
Pause processing of deferred signals. More... | |
void | connection_signals_resume (connection_t *conn) |
Resume processing of deferred signals. More... | |
Install generic I/O events on an FD to signal state changes | |
int | connection_signal_on_fd (connection_t *conn, int fd) |
Setup the connection to change states to connected or failed based on I/O events. More... | |
Allocate a new connection | |
connection_t * | connection_alloc (TALLOC_CTX *ctx, fr_event_list_t *el, connection_funcs_t const *funcs, connection_conf_t const *conf, char const *log_prefix, void const *uctx) |
Allocate a new connection. More... | |
Simple state machine for managing connection states.
Definition in file connection.h.
struct connection_conf_t |
Definition at line 91 of file connection.h.
Data Fields | ||
---|---|---|
fr_time_delta_t | connection_timeout | How long to wait for the connection to open or for shutdown to close the connection. |
fr_time_delta_t | reconnection_delay | How long to wait after failures. |
struct connection_funcs_t |
Holds a complete set of functions for a connection.
Definition at line 186 of file connection.h.
Data Fields | ||
---|---|---|
connection_close_t | close | |
connection_failed_t | failed | |
connection_init_t | init | |
connection_open_t | open | |
connection_shutdown_t | shutdown |
struct connection_pub_s |
Public fields for the connection.
This saves the overhead of using accessors for commonly used fields in connections.
Though these fields are public, they should NOT be modified by clients of the connection API.
Definition at line 67 of file connection.h.
Data Fields | ||
---|---|---|
fr_event_list_t *_CONST | el | Event list for timers and I/O events. |
void *_CONST | h | Connection handle. |
uint64_t _CONST | id | Unique identifier for the connection. |
char const *_CONST | name | Prefix to add to log messages. |
connection_state_t _CONST | prev | The previous state the connection was in. |
uint64_t _CONST | reconnected | How many times we've attempted to establish or re-establish this connection. |
connection_state_t _CONST | state | Current connection state. |
uint64_t _CONST | timed_out | How many times has this connection timed out when connecting. |
bool _CONST | triggers | do we run the triggers? |
#define _CONST const |
Definition at line 40 of file connection.h.
typedef void(* connection_close_t) (fr_event_list_t *el, void *h, void *uctx) |
Notification that the connection has errored and must be closed.
This should be used to close the file descriptor. It is assumed that the file descriptor is invalid after this callback has been executed.
If this callback does not close the file descriptor, the server will leak file descriptors.
[in] | el | to use for inserting I/O events. |
[in] | h | Handle to close. |
[in] | uctx | User context. |
Definition at line 181 of file connection.h.
typedef connection_state_t(* connection_failed_t) (void *h, connection_state_t state, void *uctx) |
Notification that a connection attempt has failed.
[in] | h | Handle that failed. |
[in] | state | the connection was in when it failed. Usually one of:
|
[in] | uctx | User context. |
Definition at line 167 of file connection.h.
typedef connection_state_t(* connection_init_t) (void **h_out, connection_t *conn, void *uctx) |
Callback for the initialise state.
Should attempt to open a non-blocking connection and return it in fd_out.
[out] | h_out | Where to write the new handle. |
[in] | conn | If integrating with a 3rd party library that will trigger connection API state transitions, the connection should be passed as the uctx argument for library I/O callbacks. |
[in] | uctx | User context. |
Definition at line 116 of file connection.h.
typedef connection_state_t(* connection_open_t) (fr_event_list_t *el, void *h, void *uctx) |
Notification that the connection is now open.
This should be used to add any additional I/O events for the file descriptor to call other code if it becomes readable or writable.
[in] | el | to use for inserting I/O events. |
[in] | h | Handle that was successfully opened. |
[in] | uctx | User context. |
Definition at line 130 of file connection.h.
typedef connection_state_t(* connection_shutdown_t) (fr_event_list_t *el, void *h, void *uctx) |
Start the process of gracefully shutting down the connection.
This function is called when the connection is signalled to gracefully disconnect. It should place the connection in a state where pending I/O operations complete, and buffers are flushed.
After all pending events are complete, the connection should be signalled that the handle is in the closed state.
[in] | el | to use for inserting I/O events. |
[in] | h | Handle that needs to be closed. |
[in] | uctx | User context. |
Definition at line 149 of file connection.h.
typedef struct connection_pub_s connection_t |
Definition at line 1 of file connection.h.
typedef struct connection_watch_entry_s connection_watch_entry_t |
Definition at line 1 of file connection.h.
typedef void(* connection_watch_t) (connection_t *conn, connection_state_t prev, connection_state_t state, void *uctx) |
Receive a notification when a connection enters a particular state.
It is permitted for watchers to signal state changes, and/or to free the connection. The actual free will be deferred until the watcher returns.
[in] | conn | Being watched. |
[in] | prev | State we came from. |
[in] | state | State that was entered (the current state) |
[in] | uctx | that was passed to connection_add_watch_*. |
Definition at line 204 of file connection.h.
enum connection_reason_t |
Definition at line 83 of file connection.h.
enum connection_state_t |
Enumerator | |
---|---|
CONNECTION_STATE_HALTED | The connection is in a halted stat. It does not have a valid file descriptor, and it will not try and and create one. |
CONNECTION_STATE_INIT | Init state, sets up connection. |
CONNECTION_STATE_CONNECTING | Waiting for connection to establish. |
CONNECTION_STATE_TIMEOUT | Timeout during CONNECTION_STATE_CONNECTING. |
CONNECTION_STATE_CONNECTED | File descriptor is open (ready for writing). |
CONNECTION_STATE_SHUTDOWN | Connection is shutting down. |
CONNECTION_STATE_FAILED | Connection has failed. |
CONNECTION_STATE_CLOSED | Connection has been closed. |
CONNECTION_STATE_MAX |
Definition at line 45 of file connection.h.
connection_watch_entry_t* connection_add_watch_post | ( | connection_t * | conn, |
connection_state_t | state, | ||
connection_watch_t | watch, | ||
bool | oneshot, | ||
void const * | uctx | ||
) |
Add a callback to be executed after a state function has been called.
Where a user callback is executed on state change, the post function is only called if the callback succeeds.
[in] | conn | to add watcher to. |
[in] | state | to call watcher on entering. |
[in] | watch | function to call. |
[in] | oneshot | If true, remove the function after calling. |
[in] | uctx | to pass to callbacks. |
Definition at line 532 of file connection.c.
connection_watch_entry_t* connection_add_watch_pre | ( | connection_t * | conn, |
connection_state_t | state, | ||
connection_watch_t | watch, | ||
bool | oneshot, | ||
void const * | uctx | ||
) |
Add a callback to be executed before a state function has been called.
[in] | conn | to add watcher to. |
[in] | state | to call watcher on entering. |
[in] | watch | function to call. |
[in] | oneshot | If true, remove the function after calling. |
[in] | uctx | to pass to callbacks. |
Definition at line 510 of file connection.c.
connection_t* connection_alloc | ( | TALLOC_CTX * | ctx, |
fr_event_list_t * | el, | ||
connection_funcs_t const * | funcs, | ||
connection_conf_t const * | conf, | ||
char const * | log_prefix, | ||
void const * | uctx | ||
) |
Allocate a new connection.
After the connection has been allocated, it should be started with a call to connection_signal_init.
The connection state machine can detect when the connection is open in one of two ways.
[in] | ctx | to allocate connection handle in. If the connection handle is freed, and the connection_state_t is CONNECTION_STATE_CONNECTING or CONNECTION_STATE_CONNECTED the close callback will be called. |
[in] | el | to use for timer events, and to pass to the connection_open_t callback. |
[in] | funcs | callback functions. |
[in] | conf | our configuration. |
[in] | log_prefix | To prepend to log messages. |
[in] | uctx | User context to pass to callbacks. |
Definition at line 1512 of file connection.c.
int connection_del_watch_post | ( | connection_t * | conn, |
connection_state_t | state, | ||
connection_watch_t | watch | ||
) |
Remove a watch function from a post list.
[in] | conn | The connection to remove the watcher from. |
[in] | state | to remove the watch from. |
[in] | watch | Function to remove. |
Definition at line 472 of file connection.c.
int connection_del_watch_pre | ( | connection_t * | conn, |
connection_state_t | state, | ||
connection_watch_t | watch | ||
) |
Remove a watch function from a pre list.
[in] | conn | The connection to remove the watcher from. |
[in] | state | to remove the watch from. |
[in] | watch | Function to remove. |
Definition at line 455 of file connection.c.
uint64_t connection_get_num_reconnected | ( | connection_t const * | conn | ) |
Return the number of times we've attempted to establish or re-establish this connection.
[in] | conn | to get count from. |
Definition at line 601 of file connection.c.
uint64_t connection_get_num_timed_out | ( | connection_t const * | conn | ) |
Return the number of times this connection has timed out whilst connecting.
[in] | conn | to get count from. |
Definition at line 613 of file connection.c.
void connection_signal_connected | ( | connection_t * | conn | ) |
Asynchronously signal that the connection is open.
Some libraries like libldap are extremely annoying and only return control to the caller after a connection is open.
For these libraries, we can't use an I/O handler to determine when the connection is open so we rely on callbacks built into the library to signal that the transition has occurred.
Definition at line 1137 of file connection.c.
void connection_signal_halt | ( | connection_t * | conn | ) |
Shuts down a connection ungracefully.
If a connection is in an open or connection state it will be closed immediately. Otherwise the connection will transition directly to the halted state.
[in] | conn | to halt. |
Definition at line 1291 of file connection.c.
void connection_signal_init | ( | connection_t * | conn | ) |
Asynchronously signal a halted connection to start.
Definition at line 1107 of file connection.c.
int connection_signal_on_fd | ( | connection_t * | conn, |
int | fd | ||
) |
Setup the connection to change states to connected or failed based on I/O events.
Will automatically cleanup after itself, in preparation for new I/O handlers to be installed in the open() callback.
Definition at line 1405 of file connection.c.
void connection_signal_reconnect | ( | connection_t * | conn, |
connection_reason_t | reason | ||
) |
Asynchronously signal the connection should be reconnected.
Should be called if the caller has knowledge that the connection is bad and should be reconnected.
[in] | conn | to reconnect. |
[in] | reason | Why the connection was signalled to reconnect. |
Definition at line 1167 of file connection.c.
void connection_signal_shutdown | ( | connection_t * | conn | ) |
Shuts down a connection gracefully.
If a shutdown function has been provided, it is called. It's then up to the shutdown function to install I/O handlers to signal when the connection has finished shutting down and should be closed via connection_signal_halt.
[in] | conn | to shutdown. |
Definition at line 1228 of file connection.c.
void connection_signals_pause | ( | connection_t * | conn | ) |
Pause processing of deferred signals.
[in] | conn | to pause signal processing for. |
Definition at line 310 of file connection.c.
void connection_signals_resume | ( | connection_t * | conn | ) |
Resume processing of deferred signals.
[in] | conn | to resume signal processing for. |
Definition at line 319 of file connection.c.
void connection_watch_disable | ( | connection_watch_entry_t * | entry | ) |
Disable a watcher.
[in] | entry | to disable. |
Definition at line 554 of file connection.c.
void connection_watch_enable | ( | connection_watch_entry_t * | entry | ) |
void connection_watch_enable_set_uctx | ( | connection_watch_entry_t * | entry, |
void const * | uctx | ||
) |
Enable a watcher and replace the uctx.
[in] | entry | to enabled. |
[in] | uctx | Opaque data to pass to the callback. |
Definition at line 565 of file connection.c.
bool connection_watch_is_enabled | ( | connection_watch_entry_t * | entry | ) |
Return the state of a watch entry.
[in] | entry | to return state of. |
Definition at line 590 of file connection.c.
void connection_watch_set_uctx | ( | connection_watch_entry_t * | entry, |
void const * | uctx | ||
) |
Change the uctx of an entry.
[in] | entry | to enabled. |
[in] | uctx | Opaque data to pass to the callback. |
Definition at line 577 of file connection.c.
|
extern |
Definition at line 46 of file connection.c.
|
extern |
Definition at line 56 of file connection.c.