The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
connection.c
Go to the documentation of this file.
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 (at
5 * 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
17/**
18 * $Id: e554fb26034f751b86a07ba50ad7f1cdbfb647ac $
19 *
20 * @file src/lib/server/connection.c
21 * @brief Simple state machine for managing connection states.
22 *
23 * @copyright 2017-2019 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 */
25#define LOG_PREFIX conn->pub.name
26
28#define _CONNECTION_PRIVATE 1
29#include <freeradius-devel/server/connection.h>
30
31#include <freeradius-devel/server/log.h>
32#include <freeradius-devel/server/trigger.h>
33
34#include <freeradius-devel/util/debug.h>
35#include <freeradius-devel/util/syserror.h>
36
37#ifdef HAVE_STDATOMIC_H
38# include <stdatomic.h>
39# ifndef ATOMIC_VAR_INIT
40# define ATOMIC_VAR_INIT(_x) (_x)
41# endif
42#else
43# include <freeradius-devel/util/stdatomic.h>
44#endif
45
47 { L("HALTED"), CONNECTION_STATE_HALTED },
48 { L("INIT"), CONNECTION_STATE_INIT },
49 { L("CONNECTING"), CONNECTION_STATE_CONNECTING },
50 { L("TIMEOUT"), CONNECTION_STATE_TIMEOUT },
51 { L("CONNECTED"), CONNECTION_STATE_CONNECTED },
52 { L("SHUTDOWN"), CONNECTION_STATE_SHUTDOWN },
53 { L("FAILED"), CONNECTION_STATE_FAILED },
54 { L("CLOSED"), CONNECTION_STATE_CLOSED },
55};
57
58/** Map connection states to trigger names
59 *
60 */
62 [CONNECTION_STATE_HALTED] = { L("connection.halted"), CONNECTION_STATE_HALTED },
63 [CONNECTION_STATE_INIT] = { L("connection.init"), CONNECTION_STATE_INIT },
64 [CONNECTION_STATE_CONNECTING] = { L("connection.connecting"), CONNECTION_STATE_CONNECTING },
65 [CONNECTION_STATE_TIMEOUT] = { L("connection.timeout"), CONNECTION_STATE_TIMEOUT },
66 [CONNECTION_STATE_CONNECTED] = { L("connection.connected"), CONNECTION_STATE_CONNECTED },
67 [CONNECTION_STATE_SHUTDOWN] = { L("connection.shutdown"), CONNECTION_STATE_SHUTDOWN },
68 [CONNECTION_STATE_FAILED] = { L("connection.failed"), CONNECTION_STATE_FAILED },
69 [CONNECTION_STATE_CLOSED] = { L("connection.closed"), CONNECTION_STATE_CLOSED }
70};
72
73static atomic_uint_fast64_t connection_counter = ATOMIC_VAR_INIT(1);
74
75/** An entry in a watch function list
76 *
77 */
79 fr_dlist_t entry; //!< List entry.
80 connection_watch_t func; //!< Function to call when a connection enters
81 ///< the state this list belongs to
82 bool oneshot; //!< Remove the function after it's called once.
83 bool enabled; //!< Whether the watch entry is enabled.
84 void *uctx; //!< User data to pass to the function.
86
88 struct connection_pub_s pub; //!< Public fields
89
90 void *uctx; //!< User data.
91
92 void *in_handler; //!< Connection is currently in a callback.
93 bool is_closed; //!< The close callback has previously been called.
94 bool processing_signals; //!< Processing deferred signals, don't let the deferred
95 ///< signal processor be called multiple times.
96
97 fr_dlist_head_t watch_pre[CONNECTION_STATE_MAX]; //!< Function called before state callback.
98 fr_dlist_head_t watch_post[CONNECTION_STATE_MAX]; //!< Function called after state callback.
99 connection_watch_entry_t *next_watcher; //!< Hack to insulate watcher iterator from deletions.
100
101 connection_init_t init; //!< Callback for initialising a connection.
102 connection_open_t open; //!< Callback for 'open' notification.
103 connection_close_t close; //!< Callback to close a connection.
104 connection_shutdown_t shutdown; //!< Signal the connection handle to start shutting down.
105 connection_failed_t failed; //!< Callback for 'failed' notification.
106
107 fr_timer_t *ev; //!< State transition timer.
108
109 fr_time_delta_t connection_timeout; //!< How long to wait in the
110 //!< #CONNECTION_STATE_CONNECTING state.
111 fr_time_delta_t reconnection_delay; //!< How long to wait in the
112 //!< #CONNECTION_STATE_FAILED state.
113
114 fr_dlist_head_t deferred_signals; //!< A list of signals we received whilst we were in
115 ///< a handler.
116
117
118
119 connection_watch_entry_t *on_halted; //!< Used by the deferred signal processor to learn
120 ///< if a function deeper in the call stack freed
121 ///< the connection.
122
123 unsigned int signals_pause; //!< Temporarily stop processing of signals.
124
125 CONF_SECTION *trigger_cs; //!< Where to search locally for triggers.
126 fr_pair_list_t *trigger_args; //!< Arguments to pass to the trigger functions.
127 bool triggers; //!< Do we run triggers.
128};
129
130#define CONN_TRIGGER(_state) do { \
131 if (conn->triggers) trigger(unlang_interpret_get_thread_default(), \
132 conn->trigger_cs, NULL, fr_table_str_by_value(connection_trigger_names, _state, "<INVALID>"), true, conn->trigger_args); \
133} while (0)
134
135#define STATE_TRANSITION(_new) \
136do { \
137 DEBUG2("Connection changed state %s -> %s", \
138 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"), \
139 fr_table_str_by_value(connection_states, _new, "<INVALID>")); \
140 conn->pub.prev = conn->pub.state; \
141 conn->pub.state = _new; \
142 CONN_TRIGGER(_new); \
143} while (0)
144
145#define BAD_STATE_TRANSITION(_new) \
146do { \
147 if (!fr_cond_assert_msg(0, "Connection %" PRIu64 " invalid transition %s -> %s", \
148 conn->pub.id, \
149 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"), \
150 fr_table_str_by_value(connection_states, _new, "<INVALID>"))) return; \
151} while (0)
152
153#define DEFER_SIGNALS(_conn) ((_conn)->in_handler || (_conn)->signals_pause)
154
155/** Deferred signals
156 *
157 */
158typedef enum {
159 CONNECTION_DSIGNAL_INIT, //!< Restart a halted connection.
160 CONNECTION_DSIGNAL_CONNECTED, //!< Signal that a connection is connected.
161 CONNECTION_DSIGNAL_RECONNECT_FAILED, //!< Reconnect a failed connection.
162 CONNECTION_DSIGNAL_RECONNECT_EXPIRED, //!< Reconnect an expired connection (gracefully).
163 CONNECTION_DSIGNAL_SHUTDOWN, //!< Close a connection (gracefully).
164 CONNECTION_DSIGNAL_HALT, //!< Close a connection (ungracefully).
165 CONNECTION_DSIGNAL_FREE //!< Free a connection (no further dsignals processed).
167
169 { L("INIT"), CONNECTION_DSIGNAL_INIT },
170 { L("CONNECTED"), CONNECTION_DSIGNAL_CONNECTED },
171 { L("RECONNECT-FAILED"), CONNECTION_DSIGNAL_RECONNECT_FAILED },
172 { L("RECONNECT-EXPIRED"), CONNECTION_DSIGNAL_RECONNECT_EXPIRED },
173 { L("SHUTDOWN"), CONNECTION_DSIGNAL_SHUTDOWN },
174 { L("HALT"), CONNECTION_DSIGNAL_HALT },
175 { L("FREE"), CONNECTION_DSIGNAL_FREE }
176};
178
179/** Holds a signal from a handler until it's safe to process it
180 *
181 */
182typedef struct {
183 fr_dlist_t entry; //!< Entry in the signals list.
184 connection_dsignal_t signal; //!< Signal that was deferred.
186
187/*
188 * State transition functions
189 */
198
199/** Add a deferred signal to the signal list
200 *
201 * Processing signals whilst in handlers usually leads to weird
202 * inconsistent states within the connection.
203 *
204 * If a public signal function is called, and detects its being called
205 * from within the handler, it instead adds a deferred signal entry
206 * and immediately returns.
207 *
208 * Once the handler is complete, and all pending C stack state changes
209 * are complete, the deferred signals are drained and processed.
210 */
212{
213 connection_dsignal_entry_t *dsignal, *prev;
214
215 /*
216 * We only suppresses consecutive duplicates at the
217 * tail. A sequence such as [INIT, HALT, INIT] will push
218 * the second INIT, because the tail is HALT. The signal
219 * handlers are generally idempotent (they check current
220 * state), so redundant signals are harmless. Avoiding
221 * this corner case involves doing more work in the
222 * common case, in order to avoid a small amount of work
223 * in the rare case.
224 */
225 prev = fr_dlist_tail(&conn->deferred_signals);
226 if (prev && (prev->signal == signal)) return; /* Don't insert duplicates */
227
228 MEM(dsignal = talloc_zero(conn, connection_dsignal_entry_t));
229 dsignal->signal = signal;
230 fr_dlist_insert_tail(&conn->deferred_signals, dsignal);
231
232// DEBUG4("Adding deferred signal - %s", fr_table_str_by_value(connection_dsignals, signal, "<INVALID>"));
233}
234
235/** Notification function to tell connection_deferred_signal_process that the connection has been freed
236 *
237 */
240 UNUSED connection_state_t state, void *uctx)
241{
242 bool *freed = uctx;
243 *freed = true;
244}
245
246/** Process any deferred signals
247 *
248 */
250{
252 bool freed = false;
253
254 /*
255 * We're inside and an instance of this function
256 * higher in the call stack. Don't do anything.
257 */
258 if (conn->processing_signals) return;
259
260 /*
261 * Get notified if the connection gets freed
262 * out from under us...
263 */
265 conn->processing_signals = true;
266
267 while ((dsignal = fr_dlist_head(&conn->deferred_signals))) {
269 fr_dlist_remove(&conn->deferred_signals, dsignal);
270 signal = dsignal->signal;
271 talloc_free(dsignal);
272
273 DEBUG4("Processing deferred signal - %s",
274 fr_table_str_by_value(connection_dsignals, signal, "<INVALID>"));
275
276 switch (signal) {
279 break;
280
283 break;
284
285 case CONNECTION_DSIGNAL_RECONNECT_FAILED: /* Reconnect - Failed */
287 break;
288
289 case CONNECTION_DSIGNAL_RECONNECT_EXPIRED: /* Reconnect - Expired */
291 break;
292
295 break;
296
299 break;
300
301 case CONNECTION_DSIGNAL_FREE: /* Freed */
302 talloc_free(conn);
303 return;
304 }
305
306 /*
307 * One of the signal handlers freed the connection,
308 * reset the processing signals and return.
309 */
310 if (freed) break;
311 }
312
313 conn->processing_signals = false;
315}
316
317/** Pause processing of deferred signals
318 *
319 * @param[in] conn to pause signal processing for.
320 */
322{
323 conn->signals_pause++;
324}
325
326/** Resume processing of deferred signals
327 *
328 * @param[in] conn to resume signal processing for.
329 */
331{
332 if (conn->signals_pause > 0) conn->signals_pause--;
333 if (conn->signals_pause > 0) return;
334
335 /*
336 * If we're not in a handler process the
337 * deferred signals now.
338 */
339 if (!conn->in_handler) {
341 return;
342 }
343}
344
345/** Called when we enter a handler
346 *
347 */
348#define HANDLER_BEGIN(_conn, _func) \
349void *_prev_handler = (_conn)->in_handler; \
350do { \
351 (_conn)->in_handler = (void *)(_func); \
352} while (0)
353
354/** Called when we exit a handler
355 *
356 */
357#define HANDLER_END(_conn) \
358do { \
359 (_conn)->in_handler = _prev_handler; \
360 if (!(_conn)->signals_pause && (!(_conn)->in_handler)) connection_deferred_signal_process(_conn); \
361} while(0)
362
363
364/** Call a list of watch functions associated with a state
365 *
366 */
367CC_NO_UBSAN(function) /* UBSAN: false positive - Public/private version of connection_t trips -fsanitize=function */
368static inline void connection_watch_call(connection_t *conn, fr_dlist_head_t *list)
369{
370 /*
371 * Nested watcher calls are not allowed
372 * and shouldn't be possible because of
373 * deferred signal processing.
374 */
375 fr_assert(conn->next_watcher == NULL);
376
377 while ((conn->next_watcher = fr_dlist_next(list, conn->next_watcher))) {
378 connection_watch_entry_t *entry = conn->next_watcher;
379 bool oneshot = entry->oneshot; /* Watcher could be freed, so store now */
380
381 if (!entry->enabled) continue;
382 if (oneshot) conn->next_watcher = fr_dlist_remove(list, entry);
383
384/*
385 DEBUG4("Notifying %swatcher - (%p)(conn=%p, prev=%s, state=%s, uctx=%p)",
386 entry->oneshot ? "oneshot " : "",
387 entry->func,
388 conn,
389 fr_table_str_by_value(connection_states, conn->pub.prev, "<INVALID>"),
390 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"),
391 entry->uctx);
392*/
393
394 entry->func(conn, conn->pub.prev, conn->pub.state, entry->uctx);
395
396 if (oneshot) talloc_free(entry);
397 }
398 conn->next_watcher = NULL;
399}
400
401/** Call the pre handler watch functions
402 *
403 */
404#define WATCH_PRE(_conn) \
405do { \
406 if (fr_dlist_empty(&(_conn)->watch_pre[(_conn)->pub.state])) break; \
407 { \
408 HANDLER_BEGIN(conn, &(_conn)->watch_pre[(_conn)->pub.state]); \
409 connection_watch_call((_conn), &(_conn)->watch_pre[(_conn)->pub.state]); \
410 HANDLER_END(conn); \
411 } \
412} while(0)
413
414/** Call the post handler watch functions
415 *
416 */
417#define WATCH_POST(_conn) \
418do { \
419 if (fr_dlist_empty(&(_conn)->watch_post[(_conn)->pub.state])) break; \
420 { \
421 HANDLER_BEGIN(conn, &(_conn)->watch_post[(_conn)->pub.state]); \
422 connection_watch_call((_conn), &(_conn)->watch_post[(_conn)->pub.state]); \
423 HANDLER_END(conn); \
424 } \
425} while(0)
426
427/** Remove a watch function from a pre/post[state] list
428 *
429 */
430static int connection_del_watch(connection_t *conn, fr_dlist_head_t *state_lists,
432{
433 connection_watch_entry_t *entry = NULL;
434 fr_dlist_head_t *list = &state_lists[state];
435
436 while ((entry = fr_dlist_next(list, entry))) {
437 if (entry->func == watch) {
438/*
439 DEBUG4("Removing %s watcher %p",
440 fr_table_str_by_value(connection_states, state, "<INVALID>"),
441 watch);
442*/
443 if (conn->next_watcher == entry) {
444 conn->next_watcher = fr_dlist_remove(list, entry);
445 } else {
446 fr_dlist_remove(list, entry);
447 }
448 talloc_free(entry);
449 return 0;
450 }
451 }
452
453 return -1;
454}
455
456/** Remove a watch function from a pre list
457 *
458 * @param[in] conn The connection to remove the watcher from.
459 * @param[in] state to remove the watch from.
460 * @param[in] watch Function to remove.
461 * @return
462 * - 0 if the function was removed successfully.
463 * - -1 if the function wasn't present in the watch list.
464 * - -2 an invalid state was passed.
465 */
467{
468 if (state >= CONNECTION_STATE_MAX) return -2;
469
470 return connection_del_watch(conn, conn->watch_pre, state, watch);
471}
472
473/** Remove a watch function from a post list
474 *
475 * @param[in] conn The connection to remove the watcher from.
476 * @param[in] state to remove the watch from.
477 * @param[in] watch Function to remove.
478 * @return
479 * - 0 if the function was removed successfully.
480 * - -1 if the function wasn't present in the watch list.
481 * - -2 an invalid state was passed.
482 */
484{
485 if (state >= CONNECTION_STATE_MAX) return -2;
486
487 return connection_del_watch(conn, conn->watch_post, state, watch);
488}
489
490/** Add a watch entry to the pre/post[state] list
491 *
492 */
494 connection_watch_t watch, bool oneshot, void const *uctx)
495{
497
498 MEM(entry = talloc_zero(conn, connection_watch_entry_t));
499
500 entry->func = watch;
501 entry->oneshot = oneshot;
502 entry->enabled = true;
503 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
504
505 fr_dlist_insert_tail(list, entry);
506
507 return entry;
508}
509
510/** Add a callback to be executed before a state function has been called
511 *
512 * @param[in] conn to add watcher to.
513 * @param[in] state to call watcher on entering.
514 * @param[in] watch function to call.
515 * @param[in] oneshot If true, remove the function after calling.
516 * @param[in] uctx to pass to callbacks.
517 * @return
518 * - NULL if state value is invalid.
519 * - A new watch entry handle.
520 */
522 connection_watch_t watch, bool oneshot, void const *uctx)
523{
524 if (state >= CONNECTION_STATE_MAX) return NULL;
525
526 return connection_add_watch(conn, &conn->watch_pre[state], watch, oneshot, uctx);
527}
528
529/** Add a callback to be executed after a state function has been called
530 *
531 * Where a user callback is executed on state change, the post function
532 * is only called if the callback succeeds.
533 *
534 * @param[in] conn to add watcher to.
535 * @param[in] state to call watcher on entering.
536 * @param[in] watch function to call.
537 * @param[in] oneshot If true, remove the function after calling.
538 * @param[in] uctx to pass to callbacks.
539 * @return
540 * - NULL if state value is invalid.
541 * - A new watch entry handle.
542 */
544 connection_watch_t watch, bool oneshot, void const *uctx)
545{
546 if (state >= CONNECTION_STATE_MAX) return NULL;
547
548 return connection_add_watch(conn, &conn->watch_post[state], watch, oneshot, uctx);
549}
550
551/** Enable a watcher
552 *
553 * @param[in] entry to enabled.
554 */
556{
557 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
558 entry->enabled = true;
559}
560
561/** Disable a watcher
562 *
563 * @param[in] entry to disable.
564 */
566{
567 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
568 entry->enabled = false;
569}
570
571/** Enable a watcher and replace the uctx
572 *
573 * @param[in] entry to enabled.
574 * @param[in] uctx Opaque data to pass to the callback.
575 */
577{
578 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
579 entry->enabled = true;
580 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
581}
582
583/** Change the uctx of an entry
584 *
585 * @param[in] entry to enabled.
586 * @param[in] uctx Opaque data to pass to the callback.
587 */
589{
590 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
591 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
592}
593
594/** Return the state of a watch entry
595 *
596 * @param[in] entry to return state of.
597 * @return
598 * - true if enabled.
599 * - false if disabled.
600 */
602{
603 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
604 return entry->enabled;
605}
606
607/** Return the number of times we've attempted to establish or re-establish this connection
608 *
609 * @param[in] conn to get count from.
610 * @return the number of times the connection has reconnected.
611 */
613{
614 if (conn->pub.reconnected == 0) return 0; /* Has never been initialised */
615
616 return conn->pub.reconnected - 1; /* We don't count the first connection attempt */
617}
618
619/** Return the number of times this connection has timed out whilst connecting
620 *
621 * @param[in] conn to get count from.
622 * @return the number of times the connection has timed out whilst connecting.
623 */
625{
626 return conn->pub.timed_out;
627}
628
629/** The requisite period of time has passed, try and re-open the connection
630 *
631 * @param[in] tl containing the timer event.
632 * @param[in] now The current time.
633 * @param[in] uctx The #connection_t the fd is associated with.
634 */
636{
637 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
638
639 switch (conn->pub.state) {
643 break;
644
645 default:
647 break;
648 }
649}
650
651/** Close the connection, then wait for another state change
652 *
653 */
655{
656 switch (conn->pub.state) {
662 break;
663
664 default:
666 return;
667 }
668
670
671 FR_TIMER_DISARM(conn->ev);
672
673 /*
674 * If there's a close callback, call it, so that the
675 * API client can free any resources associated
676 * with the connection handle.
677 */
678 WATCH_PRE(conn);
679
680 /*
681 * We can reach "is_clised" if a connection is halted,
682 * then signaled to INIT, which fails, and then sits in
683 * the FAILED state. Eventually the connection is
684 * shutdown, and enter_shutdown calls this function.
685 */
686 if (conn->close && !conn->is_closed) {
687 HANDLER_BEGIN(conn, conn->close);
688 DEBUG4("Calling close(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
689 conn->close(conn->pub.el, conn->pub.h, conn->uctx);
690 conn->is_closed = true; /* Ensure close doesn't get called twice if the connection is freed */
691 HANDLER_END(conn);
692 } else {
693 conn->is_closed = true;
694 }
695 WATCH_POST(conn);
696}
697
698/** Connection timeout
699 *
700 * Connection wasn't opened within the configured period of time
701 *
702 * @param[in] tl timer list the event belonged to.
703 * @param[in] now The current time.
704 * @param[in] uctx The #connection_t the fd is associated with.
705 */
707{
708 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
709
711}
712
713/** Gracefully shutdown the handle
714 *
715 */
717{
719
720 switch (conn->pub.state) {
722 break;
723
724 default:
726 return;
727 }
728
730
731 WATCH_PRE(conn);
732 if (conn->shutdown) {
733 HANDLER_BEGIN(conn, conn->shutdown);
734 DEBUG4("Calling shutdown(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
735 ret = conn->shutdown(conn->pub.el, conn->pub.h, conn->uctx);
736 HANDLER_END(conn);
737 }
738 switch (ret) {
740 break;
741
742 default:
744 return;
745 }
746 WATCH_POST(conn);
747
748 /*
749 * If there's a connection timeout,
750 * set, then add the timer.
751 *
752 * The connection may be bad, in which
753 * case we want to automatically fail
754 * if it doesn't shutdown within the
755 * timeout period.
756 */
758 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
759 conn->connection_timeout, false, _connection_timeout, conn) < 0) {
760 /*
761 * Can happen when the event loop is exiting
762 */
763 PERROR("Failed setting connection_timeout timer, closing connection");
765 }
766 }
767}
768
769/** Connection failed
770 *
771 * Transition to the CONNECTION_STATE_FAILED state.
772 *
773 * If the connection was open, or couldn't be opened wait for reconnection_delay before transitioning
774 * back to init.
775 *
776 * If no reconnection_delay was set, transition to halted.
777 *
778 * @param[in] conn that failed.
779 */
781{
784
786
787 /*
788 * Explicit error occurred, delete the connection timer
789 */
790 FR_TIMER_DISARM(conn->ev);
791
792 /*
793 * Record what state the connection is currently in
794 * so we can figure out what to do next.
795 */
796 prev = conn->pub.state;
797
798 /*
799 * Now transition to failed
800 */
802
803 /*
804 * If there's a failed callback, give it the
805 * opportunity to suspend/destroy the
806 * connection.
807 */
808 WATCH_PRE(conn);
809 if (conn->failed) {
810 HANDLER_BEGIN(conn, conn->failed);
811 DEBUG4("Calling failed(h=%p, state=%s, uctx=%p)", conn->pub.h,
812 fr_table_str_by_value(connection_states, prev, "<INVALID>"), conn->uctx);
813 ret = conn->failed(conn->pub.h, prev, conn->uctx);
814 HANDLER_END(conn);
815 }
816 WATCH_POST(conn);
817
818 /*
819 * Enter the closed state if we failed during
820 * connecting, or when we were connected.
821 */
822 switch (prev) {
825 case CONNECTION_STATE_TIMEOUT: /* Timeout means the connection progress past init */
826 case CONNECTION_STATE_SHUTDOWN: /* Shutdown means the connection failed whilst shutting down */
828 break;
829
830 default:
831 break;
832 }
833
834 if (conn->failed) {
835 switch (ret) {
836 /*
837 * The callback signalled it wants the
838 * connection to be reinitialised
839 * after reconnection_delay, or
840 * immediately if the failure was due
841 * to a connection timeout.
842 */
844 break;
845
846 /*
847 * The callback signalled it wants the
848 * connection to stop.
849 */
851 default:
853 return;
854 }
855 }
856
857 /*
858 * What previous state we were in
859 * determines if we need to apply the
860 * reconnect timeout.
861 */
862 switch (prev) {
863 case CONNECTION_STATE_INIT: /* Failed during initialisation */
864 case CONNECTION_STATE_CONNECTED: /* Failed after connecting */
865 case CONNECTION_STATE_CONNECTING: /* Failed during connecting */
866 case CONNECTION_STATE_SHUTDOWN: /* Failed during shutdown */
868 DEBUG2("Delaying reconnection by %pVs", fr_box_time_delta(conn->reconnection_delay));
869 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
870 conn->reconnection_delay, false, _reconnect_delay_done, conn) < 0) {
871 /*
872 * Can happen when the event loop is exiting
873 */
874 PERROR("Failed inserting reconnection_delay timer event, halting connection");
876 }
877 return;
878 }
879
880 /*
881 * If there's no reconnection
882 * delay, then don't automatically
883 * reconnect, and wait to be
884 * signalled.
885 */
887 break;
888
889 case CONNECTION_STATE_TIMEOUT: /* Failed during connecting due to timeout */
891 break;
892
893 default:
894 fr_assert(0);
895 }
896}
897
898/** Enter the timeout state
899 *
900 * The connection took took long to open. Timeout the attempt and transition
901 * to the failed state.
902 */
904{
905 switch (conn->pub.state) {
908 break;
909
910 default:
912 break;
913 }
914
915 ERROR("Connection failed - timed out after %pVs", fr_box_time_delta(conn->connection_timeout));
916
918
919 conn->pub.timed_out++;
920
922}
923
924/** Enter the halted state
925 *
926 * Here we wait, until signalled by connection_signal_reconnect.
927 */
929{
930 fr_assert(conn->is_closed);
931
932 switch (conn->pub.state) {
934 case CONNECTION_STATE_FAILED: /* Init failure */
936 break;
937
938 default:
940 break;
941 }
942
943 FR_TIMER_DISARM(conn->ev);
944
946 WATCH_PRE(conn);
947 WATCH_POST(conn);
948}
949
950/** Enter the connected state
951 *
952 * The connection is now fully connected. At this point we call the open callback
953 * so that the API client can install its normal set of I/O callbacks to deal with
954 * sending/receiving actual data.
955 *
956 * After this, the connection will only transition states if an API client
957 * explicitly calls connection_signal_reconnect.
958 *
959 * The connection API cannot monitor the connection for failure conditions.
960 *
961 * @param[in] conn Entering the connecting state.
962 */
964{
965 int ret;
966
968
970
971 FR_TIMER_DISARM(conn->ev);
972 WATCH_PRE(conn);
973 if (conn->open) {
974 HANDLER_BEGIN(conn, conn->open);
975 DEBUG4("Calling open(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
976 ret = conn->open(conn->pub.el, conn->pub.h, conn->uctx);
977 HANDLER_END(conn);
978 } else {
980 }
981
982 switch (ret) {
983 /*
984 * Callback agrees everything is connected
985 */
987 DEBUG2("Connection established");
988 WATCH_POST(conn); /* Only call if we successfully connected */
989 return;
990
991 /*
992 * Open callback failed
993 */
995 default:
996 PERROR("Connection failed");
998 return;
999 }
1000}
1001
1002/** Enter the connecting state
1003 *
1004 * After this function returns we wait to be signalled with connection_singal_connected
1005 * or for the connection timer to expire.
1006 *
1007 * @param[in] conn Entering the connecting state.
1008 */
1010{
1011 switch (conn->pub.state) {
1013 break;
1014
1015 default:
1017 return;
1018 }
1019
1021
1022 WATCH_PRE(conn);
1023 WATCH_POST(conn);
1024
1025 /*
1026 * If there's a connection timeout,
1027 * set, then add the timer.
1028 */
1030 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
1031 conn->connection_timeout, false, _connection_timeout, conn) < 0) {
1032 PERROR("Failed setting connection_timeout event, failing connection");
1033
1034 /*
1035 * This can happen when the event loop
1036 * is exiting.
1037 *
1038 * Entering fail will close partially
1039 * open connection and then, if we still
1040 * can't insert a timer, then the connection
1041 * will be halted and sit idle until its
1042 * freed.
1043 */
1045 }
1046 }
1047}
1048
1049/** Initial state of the connection
1050 *
1051 * Calls the init function we were passed to allocate a library specific handle or
1052 * file descriptor.
1053 *
1054 * @param[in] conn To initialise.
1055 */
1057{
1059
1060 switch (conn->pub.state) {
1064 break;
1065
1066 default:
1068 return;
1069 }
1070
1071 /*
1072 * Increment every time we enter
1073 * We have to do this, as we don't know
1074 * whether the connection was halted by
1075 * the failed callback, and is now being
1076 * reconnected, or was automatically
1077 * reconnected.
1078 */
1079 conn->pub.reconnected++;
1080
1082
1083 /*
1084 * If we have an init callback, call it.
1085 */
1086 WATCH_PRE(conn);
1087 if (conn->init) {
1088 HANDLER_BEGIN(conn, conn->init);
1089 DEBUG4("Calling init(h_out=%p, conn=%p, uctx=%p)", &conn->pub.h, conn, conn->uctx);
1090 ret = conn->init(&conn->pub.h, conn, conn->uctx);
1091 HANDLER_END(conn);
1092 } else {
1094 }
1095
1096 switch (ret) {
1098 conn->is_closed = false; /* We now have a handle */
1099 WATCH_POST(conn); /* Only call if we successfully initialised the handle */
1101 return;
1102
1104 conn->is_closed = false; /* We now have a handle */
1105 WATCH_POST(conn); /* Only call if we successfully initialised the handle */
1107 return;
1108
1109 /*
1110 * Initialisation callback failed
1111 */
1113 default:
1114 PERROR("Connection initialisation failed");
1116 break;
1117 }
1118}
1119
1120/** Asynchronously signal a halted connection to start
1121 *
1122 */
1124{
1125 DEBUG2("Signalled to start from %s state",
1126 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1127
1128 if (DEFER_SIGNALS(conn)) {
1130 return;
1131 }
1132
1133 switch (conn->pub.state) {
1136 break;
1137
1138 default:
1139 break;
1140 }
1141}
1142
1143/** Asynchronously signal that the connection is open
1144 *
1145 * Some libraries like libldap are extremely annoying and only return control
1146 * to the caller after a connection is open.
1147 *
1148 * For these libraries, we can't use an I/O handler to determine when the
1149 * connection is open so we rely on callbacks built into the library to
1150 * signal that the transition has occurred.
1151 *
1152 */
1154{
1155 fr_assert(!conn->open); /* Use one or the other not both! */
1156
1157 DEBUG2("Signalled connected from %s state",
1158 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1159
1160 if (DEFER_SIGNALS(conn)) {
1162 return;
1163 }
1164
1165 switch (conn->pub.state) {
1168 break;
1169
1170 default:
1171 break;
1172 }
1173}
1174
1175/** Asynchronously signal the connection should be reconnected
1176 *
1177 * Should be called if the caller has knowledge that the connection is bad
1178 * and should be reconnected.
1179 *
1180 * @param[in] conn to reconnect.
1181 * @param[in] reason Why the connection was signalled to reconnect.
1182 */
1184{
1185 DEBUG2("Signalled to reconnect from %s state",
1186 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1187
1188 if (DEFER_SIGNALS(conn)) {
1189 if ((reason == CONNECTION_EXPIRED) && conn->shutdown) {
1191 return;
1192 }
1193
1195 return;
1196 }
1197
1198 switch (conn->pub.state) {
1199 case CONNECTION_STATE_CLOSED: /* Don't circumvent reconnection_delay */
1200 case CONNECTION_STATE_INIT: /* Already initialising */
1201 break;
1202
1205 break;
1206
1208 if (reason == CONNECTION_EXPIRED) break; /* Already shutting down */
1210 break;
1211
1213 if (reason == CONNECTION_EXPIRED) {
1214 if (conn->shutdown) {
1216 break;
1217 }
1219 break;
1220 }
1222
1226 break;
1227
1228 case CONNECTION_STATE_FAILED: /* already entered failed, don't re-enter */
1229 break;
1230
1232 fr_assert(0);
1233 return;
1234 }
1235}
1236
1237/** Shuts down a connection gracefully
1238 *
1239 * If a shutdown function has been provided, it is called.
1240 * It's then up to the shutdown function to install I/O handlers to signal
1241 * when the connection has finished shutting down and should be closed
1242 * via #connection_signal_halt.
1243 *
1244 * @param[in] conn to shutdown.
1245 */
1247{
1248 DEBUG2("Signalled to shutdown from %s state",
1249 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1250
1251 if (DEFER_SIGNALS(conn)) {
1253 return;
1254 }
1255
1256 switch (conn->pub.state) {
1259 break;
1260
1263 break;
1264
1265 /*
1266 * If the connection is connected it needs to be
1267 * shutdown first.
1268 *
1269 * The shutdown callback or an FD event it inserts then
1270 * to signal that the connection should be closed.
1271 */
1273 if (conn->shutdown) {
1275 break;
1276 }
1278
1279 /*
1280 * If the connection is any of these states it
1281 * must have completed INIT which means it has
1282 * an active handle which needs to be closed before
1283 * the connection is halted.
1284 */
1289 fr_assert(conn->is_closed);
1290
1294 break;
1295
1297 fr_assert(0);
1298 return;
1299 }
1300}
1301
1302/** Shuts down a connection ungracefully
1303 *
1304 * If a connection is in an open or connection state it will be closed immediately.
1305 * Otherwise the connection will transition directly to the halted state.
1306 *
1307 * @param[in] conn to halt.
1308 */
1310{
1311 DEBUG2("Signalled to halt from %s state",
1312 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1313
1314 if (DEFER_SIGNALS(conn)) {
1316 return;
1317 }
1318
1319 switch (conn->pub.state) {
1321 break;
1322
1326 break;
1327
1328 /*
1329 * If the connection is any of these states it
1330 * must have completed INIT which means it has
1331 * an active handle which needs to be closed before
1332 * the connection is halted.
1333 *
1334 * The exception is when a connection fails to open
1335 * so goes from INIT -> FAILED, means is_closed
1336 * is true, as the connection has never opened.
1337 */
1343 if (!conn->is_closed) connection_state_enter_closed(conn);
1344 fr_assert(conn->is_closed);
1346 break;
1347
1349 fr_assert(0);
1350 return;
1351 }
1352}
1353/** Receive an error notification when we're connecting a socket
1354 *
1355 * @param[in] el event list the I/O event occurred on.
1356 * @param[in] fd the I/O event occurred for.
1357 * @param[in] flags from kevent.
1358 * @param[in] fd_errno from kevent.
1359 * @param[in] uctx The #connection_t this fd is associated with.
1360 */
1361static void _connection_error(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, int fd_errno, void *uctx)
1362{
1363 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
1364
1365 ERROR("Connection failed for fd (%d): %s", fd, fr_syserror(fd_errno));
1367}
1368
1369/** Receive a write notification after a socket is connected
1370 *
1371 * @param[in] el event list the I/O event occurred on.
1372 * @param[in] fd the I/O event occurred for.
1373 * @param[in] flags from kevent.
1374 * @param[in] uctx The #connection_t this fd is associated with.
1375 */
1376static void _connection_writable(fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
1377{
1378 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
1379
1382}
1383
1384/** Remove the FD we were watching for connection open/fail from the event loop
1385 *
1386 */
1388 UNUSED connection_state_t prev, connection_state_t state, void *uctx)
1389{
1390 int fd = *(talloc_get_type_abort(uctx, int));
1391
1392 /*
1393 * Two states can trigger a cleanup
1394 * Remove the watch on the one that didn't
1395 */
1396 switch (state) {
1399 break;
1400
1403 break;
1404
1405 default:
1406 fr_assert(0);
1407 break;
1408 }
1409
1411 talloc_free(uctx);
1412}
1413
1414/** Setup the connection to change states to connected or failed based on I/O events
1415 *
1416 * Will automatically cleanup after itself, in preparation for
1417 * new I/O handlers to be installed in the open() callback.
1418 *
1419 * @return
1420 * - 0 on success.
1421 * - -1 on failure.
1422 */
1424{
1425 int *fd_s;
1426
1427 /*
1428 * If connection becomes writable we
1429 * assume it's open.
1430 */
1431 if (fr_event_fd_insert(conn, NULL, conn->pub.el, fd,
1432 NULL,
1435 conn) < 0) {
1436 PERROR("Failed inserting fd (%d) into event loop %p",
1437 fd, conn->pub.el);
1439 return -1;
1440 }
1441
1442 /*
1443 * Stop the static analysis tools
1444 * complaining about assigning ints
1445 * to pointers.
1446 */
1447 MEM(fd_s = talloc_zero(conn, int));
1448 *fd_s = fd;
1449
1450 /*
1451 * Add a oneshot watcher to remove
1452 * the I/O handlers if the connection
1453 * fails, or is connected.
1454 */
1459 return 0;
1460}
1461
1462/** Close a connection if it's freed
1463 *
1464 * @param[in] conn to free.
1465 * @return
1466 * - 0 connection was freed immediately.
1467 * - 1 connection free was deferred.
1468 */
1470{
1471 /*
1472 * Explicitly cancel any pending events
1473 */
1474 FR_TIMER_DELETE_RETURN(&conn->ev);
1475 /*
1476 * Don't allow the connection to be
1477 * arbitrarily freed by a callback.
1478 *
1479 * Add a deferred signal to free the
1480 * connection later.
1481 */
1482 if (DEFER_SIGNALS(conn)) {
1484 return -1;
1485 }
1486
1487 switch (conn->pub.state) {
1489 break;
1490
1491 /*
1492 * Need to close the connection first
1493 */
1500
1501 default:
1503 break;
1504 }
1505 return 0;
1506}
1507
1508/** Allocate a new connection
1509 *
1510 * After the connection has been allocated, it should be started with a call to #connection_signal_init.
1511 *
1512 * The connection state machine can detect when the connection is open in one of two ways.
1513 * - You can install a generic socket open/fail callback, using connection_signal_on_fd.
1514 * - You can call either #connection_signal_connected or connection_signal_recommend.
1515 * This allows the connection state machine to work with more difficult library APIs,
1516 * which may not return control to the caller as connections are opened.
1517 *
1518 * @param[in] ctx to allocate connection handle in. If the connection
1519 * handle is freed, and the #connection_state_t is
1520 * #CONNECTION_STATE_CONNECTING or #CONNECTION_STATE_CONNECTED the
1521 * close callback will be called.
1522 * @param[in] el to use for timer events, and to pass to the #connection_open_t callback.
1523 * @param[in] funcs callback functions.
1524 * @param[in] conf our configuration.
1525 * @param[in] log_prefix To prepend to log messages.
1526 * @param[in] uctx User context to pass to callbacks.
1527 * @return
1528 * - A new #connection_t on success.
1529 * - NULL on failure.
1530 */
1532 connection_funcs_t const *funcs,
1533 connection_conf_t const *conf,
1534 char const *log_prefix,
1535 void const *uctx)
1536{
1537 size_t i;
1538 connection_t *conn;
1539 uint64_t id;
1540
1541 fr_assert(el);
1542
1543 MEM(conn = talloc(ctx, connection_t));
1544 talloc_set_destructor(conn, _connection_free);
1545
1547
1548 *conn = (connection_t){
1549 .pub = {
1550 .id = id,
1551 .state = CONNECTION_STATE_HALTED,
1552 .el = el
1553 },
1554 .reconnection_delay = conf->reconnection_delay,
1555 .connection_timeout = conf->connection_timeout,
1556 .init = funcs->init,
1557 .open = funcs->open,
1558 .close = funcs->close,
1559 .failed = funcs->failed,
1560 .shutdown = funcs->shutdown,
1561 .is_closed = true, /* Starts closed */
1562 .triggers = conf->triggers,
1563 .trigger_args = conf->trigger_args,
1564 .trigger_cs = conf->trigger_cs,
1565 .pub.name = talloc_asprintf(conn, "%s - [%" PRIu64 "]", log_prefix, id)
1566 };
1567 memcpy(&conn->uctx, &uctx, sizeof(conn->uctx));
1568
1569 for (i = 0; i < NUM_ELEMENTS(conn->watch_pre); i++) {
1571 }
1572 for (i = 0; i < NUM_ELEMENTS(conn->watch_post); i++) {
1574 }
1576
1577 /*
1578 * Pre-allocate a on_halt watcher for deferred signal processing
1579 *
1580 * Note that we do NOT set "oneshot". This lets the watcher remain valid after the oneshot
1581 * watcher fires. Otherwise the connection is freed out from under the watcher.
1582 */
1585 connection_watch_disable(conn->on_halted); /* Start disabled */
1586
1587 return conn;
1588}
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:210
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:325
#define CC_NO_UBSAN(_sanitize)
Definition build.h:431
#define UNUSED
Definition build.h:318
#define NUM_ELEMENTS(_t)
Definition build.h:340
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
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.
Definition connection.h:213
connection_state_t(* connection_failed_t)(void *h, connection_state_t state, void *uctx)
Notification that a connection attempt has failed.
Definition connection.h:176
uint64_t _CONST timed_out
How many times has this connection timed out when connecting.
Definition connection.h:80
void(* connection_close_t)(fr_event_list_t *el, void *h, void *uctx)
Notification that the connection has errored and must be closed.
Definition connection.h:190
fr_event_list_t *_CONST el
Event list for timers and I/O events.
Definition connection.h:76
connection_state_t(* connection_init_t)(void **h_out, connection_t *conn, void *uctx)
Callback for the initialise state.
Definition connection.h:125
connection_state_t
Definition connection.h:47
@ CONNECTION_STATE_FAILED
Connection has failed.
Definition connection.h:56
@ CONNECTION_STATE_HALTED
The connection is in a halted stat.
Definition connection.h:48
@ CONNECTION_STATE_CLOSED
Connection has been closed.
Definition connection.h:57
@ CONNECTION_STATE_CONNECTED
File descriptor is open (ready for writing).
Definition connection.h:54
@ CONNECTION_STATE_TIMEOUT
Timeout during CONNECTION_STATE_CONNECTING.
Definition connection.h:53
@ CONNECTION_STATE_INIT
Init state, sets up connection.
Definition connection.h:51
@ CONNECTION_STATE_MAX
Definition connection.h:58
@ CONNECTION_STATE_CONNECTING
Waiting for connection to establish.
Definition connection.h:52
@ CONNECTION_STATE_SHUTDOWN
Connection is shutting down.
Definition connection.h:55
uint64_t _CONST reconnected
How many times we've attempted to establish or re-establish this connection.
Definition connection.h:78
void *_CONST h
Connection handle.
Definition connection.h:75
connection_reason_t
Definition connection.h:84
@ CONNECTION_EXPIRED
Connection is being reconnected because it's at the end of its life.
Definition connection.h:86
@ CONNECTION_FAILED
Connection is being reconnected because it failed.
Definition connection.h:85
connection_state_t(* connection_open_t)(fr_event_list_t *el, void *h, void *uctx)
Notification that the connection is now open.
Definition connection.h:139
connection_state_t _CONST state
Current connection state.
Definition connection.h:72
connection_init_t init
Definition connection.h:196
connection_shutdown_t shutdown
Definition connection.h:198
connection_failed_t failed
Definition connection.h:199
connection_open_t open
Definition connection.h:197
uint64_t _CONST id
Unique identifier for the connection.
Definition connection.h:74
connection_close_t close
Definition connection.h:200
connection_state_t(* connection_shutdown_t)(fr_event_list_t *el, void *h, void *uctx)
Start the process of gracefully shutting down the connection.
Definition connection.h:158
Holds a complete set of functions for a connection.
Definition connection.h:195
Public fields for the connection.
Definition connection.h:69
#define MEM(x)
Definition debug.h:46
#define ERROR(fmt,...)
Definition dhcpclient.c:40
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:620
static void * fr_dlist_tail(fr_dlist_head_t const *list_head)
Return the TAIL item of a list or NULL if the list is empty.
Definition dlist.h:513
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:257
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:537
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
#define fr_event_fd_insert(...)
Definition event.h:247
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:83
talloc_free(hp)
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG4(_fmt,...)
Definition log.h:267
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1203
Stores all information relating to an event list.
Definition event.c:377
#define fr_assert(_expr)
Definition rad_assert.h:37
#define DEBUG2(fmt,...)
static rs_t * conf
Definition radsniff.c:52
static void connection_watch_call(connection_t *conn, fr_dlist_head_t *list)
Call a list of watch functions associated with a state.
Definition connection.c:368
CONF_SECTION * trigger_cs
Where to search locally for triggers.
Definition connection.c:125
struct connection_watch_entry_s connection_watch_entry_t
An entry in a watch function list.
void connection_signal_shutdown(connection_t *conn)
Shuts down a connection gracefully.
fr_dlist_head_t watch_post[CONNECTION_STATE_MAX]
Function called after state callback.
Definition connection.c:98
void connection_watch_enable(connection_watch_entry_t *entry)
Enable a watcher.
Definition connection.c:555
static size_t connection_trigger_names_len
Definition connection.c:71
static int connection_del_watch(connection_t *conn, fr_dlist_head_t *state_lists, connection_state_t state, connection_watch_t watch)
Remove a watch function from a pre/post[state] list.
Definition connection.c:430
uint64_t connection_get_num_timed_out(connection_t const *conn)
Return the number of times this connection has timed out whilst connecting.
Definition connection.c:624
fr_dlist_t entry
List entry.
Definition connection.c:79
bool enabled
Whether the watch entry is enabled.
Definition connection.c:83
void * in_handler
Connection is currently in a callback.
Definition connection.c:92
#define DEFER_SIGNALS(_conn)
Definition connection.c:153
static fr_table_num_ordered_t const connection_dsignals[]
Definition connection.c:168
static void _deferred_signal_connection_on_halted(UNUSED connection_t *conn, UNUSED connection_state_t prev, UNUSED connection_state_t state, void *uctx)
Notification function to tell connection_deferred_signal_process that the connection has been freed.
Definition connection.c:238
void connection_watch_disable(connection_watch_entry_t *entry)
Disable a watcher.
Definition connection.c:565
fr_pair_list_t * trigger_args
Arguments to pass to the trigger functions.
Definition connection.c:126
int connection_del_watch_post(connection_t *conn, connection_state_t state, connection_watch_t watch)
Remove a watch function from a post list.
Definition connection.c:483
connection_shutdown_t shutdown
Signal the connection handle to start shutting down.
Definition connection.c:104
static void _connection_signal_on_fd_cleanup(connection_t *conn, UNUSED connection_state_t prev, connection_state_t state, void *uctx)
Remove the FD we were watching for connection open/fail from the event loop.
fr_time_delta_t connection_timeout
How long to wait in the CONNECTION_STATE_CONNECTING state.
Definition connection.c:109
static void _connection_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Connection timeout.
Definition connection.c:706
bool connection_watch_is_enabled(connection_watch_entry_t *entry)
Return the state of a watch entry.
Definition connection.c:601
static atomic_uint_fast64_t connection_counter
Definition connection.c:73
static size_t connection_dsignals_len
Definition connection.c:177
void * uctx
User data.
Definition connection.c:90
fr_dlist_head_t deferred_signals
A list of signals we received whilst we were in a handler.
Definition connection.c:114
static int _connection_free(connection_t *conn)
Close a connection if it's freed.
static void connection_state_enter_failed(connection_t *conn)
Connection failed.
Definition connection.c:780
static void _reconnect_delay_done(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
The requisite period of time has passed, try and re-open the connection.
Definition connection.c:635
void connection_signal_halt(connection_t *conn)
Shuts down a connection ungracefully.
#define WATCH_POST(_conn)
Call the post handler watch functions.
Definition connection.c:417
void connection_signals_resume(connection_t *conn)
Resume processing of deferred signals.
Definition connection.c:330
#define HANDLER_BEGIN(_conn, _func)
Called when we enter a handler.
Definition connection.c:348
connection_init_t init
Callback for initialising a connection.
Definition connection.c:101
static connection_watch_entry_t * connection_add_watch(connection_t *conn, fr_dlist_head_t *list, connection_watch_t watch, bool oneshot, void const *uctx)
Add a watch entry to the pre/post[state] list.
Definition connection.c:493
connection_close_t close
Callback to close a connection.
Definition connection.c:103
static void connection_state_enter_init(connection_t *conn)
Initial state of the connection.
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.
Definition connection.c:612
bool triggers
Do we run triggers.
Definition connection.c:127
static void connection_state_enter_connecting(connection_t *conn)
Enter the connecting state.
connection_watch_entry_t * next_watcher
Hack to insulate watcher iterator from deletions.
Definition connection.c:99
connection_watch_t func
Function to call when a connection enters the state this list belongs to.
Definition connection.c:80
#define WATCH_PRE(_conn)
Call the pre handler watch functions.
Definition connection.c:404
static void connection_deferred_signal_process(connection_t *conn)
Process any deferred signals.
Definition connection.c:249
connection_dsignal_t signal
Signal that was deferred.
Definition connection.c:184
static void _connection_error(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, int fd_errno, void *uctx)
Receive an error notification when we're connecting a socket.
connection_open_t open
Callback for 'open' notification.
Definition connection.c:102
connection_failed_t failed
Callback for 'failed' notification.
Definition connection.c:105
struct connection_s connection_t
Definition connection.c:27
void connection_watch_set_uctx(connection_watch_entry_t *entry, void const *uctx)
Change the uctx of an entry.
Definition connection.c:588
fr_time_delta_t reconnection_delay
How long to wait in the CONNECTION_STATE_FAILED state.
Definition connection.c:111
static void connection_state_enter_closed(connection_t *conn)
Close the connection, then wait for another state change.
Definition connection.c:654
void * uctx
User data to pass to the function.
Definition connection.c:84
void connection_signal_reconnect(connection_t *conn, connection_reason_t reason)
Asynchronously signal the connection should be reconnected.
connection_dsignal_t
Deferred signals.
Definition connection.c:158
@ CONNECTION_DSIGNAL_RECONNECT_FAILED
Reconnect a failed connection.
Definition connection.c:161
@ CONNECTION_DSIGNAL_HALT
Close a connection (ungracefully).
Definition connection.c:164
@ CONNECTION_DSIGNAL_INIT
Restart a halted connection.
Definition connection.c:159
@ CONNECTION_DSIGNAL_FREE
Free a connection (no further dsignals processed).
Definition connection.c:165
@ CONNECTION_DSIGNAL_SHUTDOWN
Close a connection (gracefully).
Definition connection.c:163
@ CONNECTION_DSIGNAL_CONNECTED
Signal that a connection is connected.
Definition connection.c:160
@ CONNECTION_DSIGNAL_RECONNECT_EXPIRED
Reconnect an expired connection (gracefully).
Definition connection.c:162
bool oneshot
Remove the function after it's called once.
Definition connection.c:82
fr_dlist_head_t watch_pre[CONNECTION_STATE_MAX]
Function called before state callback.
Definition connection.c:97
fr_table_num_ordered_t const connection_states[]
Definition connection.c:46
#define HANDLER_END(_conn)
Called when we exit a handler.
Definition connection.c:357
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.
bool is_closed
The close callback has previously been called.
Definition connection.c:93
#define STATE_TRANSITION(_new)
Definition connection.c:135
void connection_signal_init(connection_t *conn)
Asynchronously signal a halted connection to start.
static fr_table_num_indexed_t const connection_trigger_names[]
Map connection states to trigger names.
Definition connection.c:61
static void connection_state_enter_halted(connection_t *conn)
Enter the halted state.
Definition connection.c:928
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.
static void connection_deferred_signal_add(connection_t *conn, connection_dsignal_t signal)
Add a deferred signal to the signal list.
Definition connection.c:211
fr_dlist_t entry
Entry in the signals list.
Definition connection.c:183
static void _connection_writable(fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
Receive a write notification after a socket is connected.
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.
Definition connection.c:521
size_t connection_states_len
Definition connection.c:56
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.
Definition connection.c:543
static void connection_state_enter_connected(connection_t *conn)
Enter the connected state.
Definition connection.c:963
int connection_del_watch_pre(connection_t *conn, connection_state_t state, connection_watch_t watch)
Remove a watch function from a pre list.
Definition connection.c:466
connection_watch_entry_t * on_halted
Used by the deferred signal processor to learn if a function deeper in the call stack freed the conne...
Definition connection.c:119
#define BAD_STATE_TRANSITION(_new)
Definition connection.c:145
struct connection_pub_s pub
Public fields.
Definition connection.c:88
void connection_watch_enable_set_uctx(connection_watch_entry_t *entry, void const *uctx)
Enable a watcher and replace the uctx.
Definition connection.c:576
void connection_signal_connected(connection_t *conn)
Asynchronously signal that the connection is open.
static void connection_state_enter_shutdown(connection_t *conn)
Gracefully shutdown the handle.
Definition connection.c:716
void connection_signals_pause(connection_t *conn)
Pause processing of deferred signals.
Definition connection.c:321
bool processing_signals
Processing deferred signals, don't let the deferred signal processor be called multiple times.
Definition connection.c:94
unsigned int signals_pause
Temporarily stop processing of signals.
Definition connection.c:123
fr_timer_t * ev
State transition timer.
Definition connection.c:107
static void connection_state_enter_timeout(connection_t *conn)
Enter the timeout state.
Definition connection.c:903
Holds a signal from a handler until it's safe to process it.
Definition connection.c:182
An entry in a watch function list.
Definition connection.c:78
@ memory_order_relaxed
Definition stdatomic.h:127
#define atomic_fetch_add_explicit(object, operand, order)
Definition stdatomic.h:302
#define ATOMIC_VAR_INIT(value)
Definition stdatomic.h:88
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
An element in a table indexed by numeric value.
Definition table.h:92
An element in an arbitrarily ordered array of name to num mappings.
Definition table.h:57
#define talloc_asprintf
Definition talloc.h:144
#define fr_time_delta_ispos(_a)
Definition time.h:290
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:49
A timer event.
Definition timer.c:83
#define FR_TIMER_DELETE_RETURN(_ev_p)
Definition timer.h:110
#define fr_timer_in(...)
Definition timer.h:87
#define FR_TIMER_DISARM(_ev)
Definition timer.h:91
static fr_event_list_t * el
#define fr_box_time_delta(_val)
Definition value.h:366