The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
connection.c
Go to the documentation of this file.
1/*
2 * This program is 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: 76b8ab22a475b877e10095632e9c5bdb0e503a31 $
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 prev = fr_dlist_tail(&conn->deferred_signals);
216 if (prev && (prev->signal == signal)) return; /* Don't insert duplicates */
217
218 MEM(dsignal = talloc_zero(conn, connection_dsignal_entry_t));
219 dsignal->signal = signal;
220 fr_dlist_insert_tail(&conn->deferred_signals, dsignal);
221
222// DEBUG4("Adding deferred signal - %s", fr_table_str_by_value(connection_dsignals, signal, "<INVALID>"));
223}
224
225/** Notification function to tell connection_deferred_signal_process that the connection has been freed
226 *
227 */
230 UNUSED connection_state_t state, void *uctx)
231{
232 bool *freed = uctx;
233 *freed = true;
234}
235
236/** Process any deferred signals
237 *
238 */
240{
242 bool freed = false;
243
244 /*
245 * We're inside and an instance of this function
246 * higher in the call stack. Don't do anything.
247 */
248 if (conn->processing_signals) return;
249
250 /*
251 * Get notified if the connection gets freed
252 * out from under us...
253 */
255 conn->processing_signals = true;
256
257 while ((dsignal = fr_dlist_head(&conn->deferred_signals))) {
259 fr_dlist_remove(&conn->deferred_signals, dsignal);
260 signal = dsignal->signal;
261 talloc_free(dsignal);
262
263 DEBUG4("Processing deferred signal - %s",
264 fr_table_str_by_value(connection_dsignals, signal, "<INVALID>"));
265
266 switch (signal) {
269 break;
270
273 break;
274
275 case CONNECTION_DSIGNAL_RECONNECT_FAILED: /* Reconnect - Failed */
277 break;
278
279 case CONNECTION_DSIGNAL_RECONNECT_EXPIRED: /* Reconnect - Expired */
281 break;
282
285 break;
286
289 break;
290
291 case CONNECTION_DSIGNAL_FREE: /* Freed */
292 talloc_free(conn);
293 return;
294 }
295
296 /*
297 * One of the signal handlers freed the connection
298 * return immediately.
299 */
300 /* coverity[dead_error_line] */
301 if (freed) return;
302 }
303
304 conn->processing_signals = false;
306}
307
308/** Pause processing of deferred signals
309 *
310 * @param[in] conn to pause signal processing for.
311 */
313{
314 conn->signals_pause++;
315}
316
317/** Resume processing of deferred signals
318 *
319 * @param[in] conn to resume signal processing for.
320 */
322{
323 if (conn->signals_pause > 0) conn->signals_pause--;
324 if (conn->signals_pause > 0) return;
325
326 /*
327 * If we're not in a handler process the
328 * deferred signals now.
329 */
330 if (!conn->in_handler) {
332 return;
333 }
334}
335
336/** Called when we enter a handler
337 *
338 */
339#define HANDLER_BEGIN(_conn, _func) \
340void *_prev_handler = (_conn)->in_handler; \
341do { \
342 (_conn)->in_handler = (void *)(_func); \
343} while (0)
344
345/** Called when we exit a handler
346 *
347 */
348#define HANDLER_END(_conn) \
349do { \
350 (_conn)->in_handler = _prev_handler; \
351 if (!(_conn)->signals_pause && (!(_conn)->in_handler)) connection_deferred_signal_process(_conn); \
352} while(0)
353
354
355/** Call a list of watch functions associated with a state
356 *
357 */
358CC_NO_UBSAN(function) /* UBSAN: false positive - Public/private version of connection_t trips -fsanitize=function */
359static inline void connection_watch_call(connection_t *conn, fr_dlist_head_t *list)
360{
361 /*
362 * Nested watcher calls are not allowed
363 * and shouldn't be possible because of
364 * deferred signal processing.
365 */
366 fr_assert(conn->next_watcher == NULL);
367
368 while ((conn->next_watcher = fr_dlist_next(list, conn->next_watcher))) {
369 connection_watch_entry_t *entry = conn->next_watcher;
370 bool oneshot = entry->oneshot; /* Watcher could be freed, so store now */
371
372 if (!entry->enabled) continue;
373 if (oneshot) conn->next_watcher = fr_dlist_remove(list, entry);
374
375/*
376 DEBUG4("Notifying %swatcher - (%p)(conn=%p, prev=%s, state=%s, uctx=%p)",
377 entry->oneshot ? "oneshot " : "",
378 entry->func,
379 conn,
380 fr_table_str_by_value(connection_states, conn->pub.prev, "<INVALID>"),
381 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"),
382 entry->uctx);
383*/
384
385 entry->func(conn, conn->pub.prev, conn->pub.state, entry->uctx);
386
387 if (oneshot) talloc_free(entry);
388 }
389 conn->next_watcher = NULL;
390}
391
392/** Call the pre handler watch functions
393 *
394 */
395#define WATCH_PRE(_conn) \
396do { \
397 if (fr_dlist_empty(&(_conn)->watch_pre[(_conn)->pub.state])) break; \
398 { \
399 HANDLER_BEGIN(conn, &(_conn)->watch_pre[(_conn)->pub.state]); \
400 connection_watch_call((_conn), &(_conn)->watch_pre[(_conn)->pub.state]); \
401 HANDLER_END(conn); \
402 } \
403} while(0)
404
405/** Call the post handler watch functions
406 *
407 */
408#define WATCH_POST(_conn) \
409do { \
410 if (fr_dlist_empty(&(_conn)->watch_post[(_conn)->pub.state])) break; \
411 { \
412 HANDLER_BEGIN(conn, &(_conn)->watch_post[(_conn)->pub.state]); \
413 connection_watch_call((_conn), &(_conn)->watch_post[(_conn)->pub.state]); \
414 HANDLER_END(conn); \
415 } \
416} while(0)
417
418/** Remove a watch function from a pre/post[state] list
419 *
420 */
421static int connection_del_watch(connection_t *conn, fr_dlist_head_t *state_lists,
423{
424 connection_watch_entry_t *entry = NULL;
425 fr_dlist_head_t *list = &state_lists[state];
426
427 while ((entry = fr_dlist_next(list, entry))) {
428 if (entry->func == watch) {
429/*
430 DEBUG4("Removing %s watcher %p",
431 fr_table_str_by_value(connection_states, state, "<INVALID>"),
432 watch);
433*/
434 if (conn->next_watcher == entry) {
435 conn->next_watcher = fr_dlist_remove(list, entry);
436 } else {
437 fr_dlist_remove(list, entry);
438 }
439 talloc_free(entry);
440 return 0;
441 }
442 }
443
444 return -1;
445}
446
447/** Remove a watch function from a pre list
448 *
449 * @param[in] conn The connection to remove the watcher from.
450 * @param[in] state to remove the watch from.
451 * @param[in] watch Function to remove.
452 * @return
453 * - 0 if the function was removed successfully.
454 * - -1 if the function wasn't present in the watch list.
455 * - -2 an invalid state was passed.
456 */
458{
459 if (state >= CONNECTION_STATE_MAX) return -2;
460
461 return connection_del_watch(conn, conn->watch_pre, state, watch);
462}
463
464/** Remove a watch function from a post list
465 *
466 * @param[in] conn The connection to remove the watcher from.
467 * @param[in] state to remove the watch from.
468 * @param[in] watch Function to remove.
469 * @return
470 * - 0 if the function was removed successfully.
471 * - -1 if the function wasn't present in the watch list.
472 * - -2 an invalid state was passed.
473 */
475{
476 if (state >= CONNECTION_STATE_MAX) return -2;
477
478 return connection_del_watch(conn, conn->watch_post, state, watch);
479}
480
481/** Add a watch entry to the pre/post[state] list
482 *
483 */
485 connection_watch_t watch, bool oneshot, void const *uctx)
486{
488
489 MEM(entry = talloc_zero(conn, connection_watch_entry_t));
490
491 entry->func = watch;
492 entry->oneshot = oneshot;
493 entry->enabled = true;
494 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
495
496 fr_dlist_insert_tail(list, entry);
497
498 return entry;
499}
500
501/** Add a callback to be executed before a state function has been called
502 *
503 * @param[in] conn to add watcher to.
504 * @param[in] state to call watcher on entering.
505 * @param[in] watch function to call.
506 * @param[in] oneshot If true, remove the function after calling.
507 * @param[in] uctx to pass to callbacks.
508 * @return
509 * - NULL if state value is invalid.
510 * - A new watch entry handle.
511 */
513 connection_watch_t watch, bool oneshot, void const *uctx)
514{
515 if (state >= CONNECTION_STATE_MAX) return NULL;
516
517 return connection_add_watch(conn, &conn->watch_pre[state], watch, oneshot, uctx);
518}
519
520/** Add a callback to be executed after a state function has been called
521 *
522 * Where a user callback is executed on state change, the post function
523 * is only called if the callback succeeds.
524 *
525 * @param[in] conn to add watcher to.
526 * @param[in] state to call watcher on entering.
527 * @param[in] watch function to call.
528 * @param[in] oneshot If true, remove the function after calling.
529 * @param[in] uctx to pass to callbacks.
530 * @return
531 * - NULL if state value is invalid.
532 * - A new watch entry handle.
533 */
535 connection_watch_t watch, bool oneshot, void const *uctx)
536{
537 if (state >= CONNECTION_STATE_MAX) return NULL;
538
539 return connection_add_watch(conn, &conn->watch_post[state], watch, oneshot, uctx);
540}
541
542/** Enable a watcher
543 *
544 * @param[in] entry to enabled.
545 */
547{
548 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
549 entry->enabled = true;
550}
551
552/** Disable a watcher
553 *
554 * @param[in] entry to disable.
555 */
557{
558 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
559 entry->enabled = false;
560}
561
562/** Enable a watcher and replace the uctx
563 *
564 * @param[in] entry to enabled.
565 * @param[in] uctx Opaque data to pass to the callback.
566 */
568{
569 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
570 entry->enabled = true;
571 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
572}
573
574/** Change the uctx of an entry
575 *
576 * @param[in] entry to enabled.
577 * @param[in] uctx Opaque data to pass to the callback.
578 */
580{
581 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
582 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
583}
584
585/** Return the state of a watch entry
586 *
587 * @param[in] entry to return state of.
588 * @return
589 * - true if enabled.
590 * - false if disabled.
591 */
593{
594 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
595 return entry->enabled;
596}
597
598/** Return the number of times we've attempted to establish or re-establish this connection
599 *
600 * @param[in] conn to get count from.
601 * @return the number of times the connection has reconnected.
602 */
604{
605 if (conn->pub.reconnected == 0) return 0; /* Has never been initialised */
606
607 return conn->pub.reconnected - 1; /* We don't count the first connection attempt */
608}
609
610/** Return the number of times this connection has timed out whilst connecting
611 *
612 * @param[in] conn to get count from.
613 * @return the number of times the connection has timed out whilst connecting.
614 */
616{
617 return conn->pub.timed_out;
618}
619
620/** The requisite period of time has passed, try and re-open the connection
621 *
622 * @param[in] tl containing the timer event.
623 * @param[in] now The current time.
624 * @param[in] uctx The #connection_t the fd is associated with.
625 */
627{
628 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
629
630 switch (conn->pub.state) {
634 break;
635
636 default:
638 break;
639 }
640}
641
642/** Close the connection, then wait for another state change
643 *
644 */
646{
647 switch (conn->pub.state) {
651 break;
652
653 default:
655 return;
656 }
657
659
660 FR_TIMER_DISARM(conn->ev);
661
662 /*
663 * If there's a close callback, call it, so that the
664 * API client can free any resources associated
665 * with the connection handle.
666 */
667 WATCH_PRE(conn);
668
669 /*
670 * is_closed is for pure paranoia. If everything
671 * is working correctly this state should never
672 * be entered if the connection is closed.
673 */
674 fr_assert(!conn->is_closed);
675 if (conn->close && !conn->is_closed) {
676 HANDLER_BEGIN(conn, conn->close);
677 DEBUG4("Calling close(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
678 conn->close(conn->pub.el, conn->pub.h, conn->uctx);
679 conn->is_closed = true; /* Ensure close doesn't get called twice if the connection is freed */
680 HANDLER_END(conn);
681 } else {
682 conn->is_closed = true;
683 }
684 WATCH_POST(conn);
685}
686
687/** Connection timeout
688 *
689 * Connection wasn't opened within the configured period of time
690 *
691 * @param[in] tl timer list the event belonged to.
692 * @param[in] now The current time.
693 * @param[in] uctx The #connection_t the fd is associated with.
694 */
696{
697 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
698
700}
701
702/** Gracefully shutdown the handle
703 *
704 */
706{
708
709 switch (conn->pub.state) {
711 break;
712
713 default:
715 return;
716 }
717
719
720 WATCH_PRE(conn);
721 {
722 HANDLER_BEGIN(conn, conn->shutdown);
723 DEBUG4("Calling shutdown(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
724 ret = conn->shutdown(conn->pub.el, conn->pub.h, conn->uctx);
725 HANDLER_END(conn);
726 }
727 switch (ret) {
729 break;
730
731 default:
733 return;
734 }
735 WATCH_POST(conn);
736
737 /*
738 * If there's a connection timeout,
739 * set, then add the timer.
740 *
741 * The connection may be bad, in which
742 * case we want to automatically fail
743 * if it doesn't shutdown within the
744 * timeout period.
745 */
747 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
748 conn->connection_timeout, false, _connection_timeout, conn) < 0) {
749 /*
750 * Can happen when the event loop is exiting
751 */
752 PERROR("Failed setting connection_timeout timer, closing connection");
754 }
755 }
756}
757
758/** Connection failed
759 *
760 * Transition to the CONNECTION_STATE_FAILED state.
761 *
762 * If the connection was open, or couldn't be opened wait for reconnection_delay before transitioning
763 * back to init.
764 *
765 * If no reconnection_delay was set, transition to halted.
766 *
767 * @param[in] conn that failed.
768 */
770{
773
775
776 /*
777 * Explicit error occurred, delete the connection timer
778 */
779 FR_TIMER_DISARM(conn->ev);
780
781 /*
782 * Record what state the connection is currently in
783 * so we can figure out what to do next.
784 */
785 prev = conn->pub.state;
786
787 /*
788 * Now transition to failed
789 */
791
792 /*
793 * If there's a failed callback, give it the
794 * opportunity to suspend/destroy the
795 * connection.
796 */
797 WATCH_PRE(conn);
798 if (conn->failed) {
799 HANDLER_BEGIN(conn, conn->failed);
800 DEBUG4("Calling failed(h=%p, state=%s, uctx=%p)", conn->pub.h,
801 fr_table_str_by_value(connection_states, prev, "<INVALID>"), conn->uctx);
802 ret = conn->failed(conn->pub.h, prev, conn->uctx);
803 HANDLER_END(conn);
804 }
805 WATCH_POST(conn);
806
807 /*
808 * Enter the closed state if we failed during
809 * connecting, or when we were connected.
810 */
811 switch (prev) {
814 case CONNECTION_STATE_TIMEOUT: /* Timeout means the connection progress past init */
815 case CONNECTION_STATE_SHUTDOWN: /* Shutdown means the connection failed whilst shutting down */
817 break;
818
819 default:
820 break;
821 }
822
823 if (conn->failed) {
824 switch (ret) {
825 /*
826 * The callback signalled it wants the
827 * connection to be reinitialised
828 * after reconnection_delay, or
829 * immediately if the failure was due
830 * to a connection timeout.
831 */
833 break;
834
835 /*
836 * The callback signalled it wants the
837 * connection to stop.
838 */
840 default:
842 return;
843 }
844 }
845
846 /*
847 * What previous state we were in
848 * determines if we need to apply the
849 * reconnect timeout.
850 */
851 switch (prev) {
852 case CONNECTION_STATE_INIT: /* Failed during initialisation */
853 case CONNECTION_STATE_CONNECTED: /* Failed after connecting */
854 case CONNECTION_STATE_CONNECTING: /* Failed during connecting */
855 case CONNECTION_STATE_SHUTDOWN: /* Failed during shutdown */
857 DEBUG2("Delaying reconnection by %pVs", fr_box_time_delta(conn->reconnection_delay));
858 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
859 conn->reconnection_delay, false, _reconnect_delay_done, conn) < 0) {
860 /*
861 * Can happen when the event loop is exiting
862 */
863 PERROR("Failed inserting reconnection_delay timer event, halting connection");
865 }
866 return;
867 }
868
869 /*
870 * If there's no reconnection
871 * delay, then don't automatically
872 * reconnect, and wait to be
873 * signalled.
874 */
876 break;
877
878 case CONNECTION_STATE_TIMEOUT: /* Failed during connecting due to timeout */
880 break;
881
882 default:
883 fr_assert(0);
884 }
885}
886
887/** Enter the timeout state
888 *
889 * The connection took took long to open. Timeout the attempt and transition
890 * to the failed state.
891 */
893{
894 switch (conn->pub.state) {
897 break;
898
899 default:
901 }
902
903 ERROR("Connection failed - timed out after %pVs", fr_box_time_delta(conn->connection_timeout));
904
906
907 conn->pub.timed_out++;
908
910}
911
912/** Enter the halted state
913 *
914 * Here we wait, until signalled by connection_signal_reconnect.
915 */
917{
918 fr_assert(conn->is_closed);
919
920 switch (conn->pub.state) {
921 case CONNECTION_STATE_FAILED: /* Init failure */
923 break;
924
925 default:
927 }
928
929 FR_TIMER_DISARM(conn->ev);
930
932 WATCH_PRE(conn);
933 WATCH_POST(conn);
934}
935
936/** Enter the connected state
937 *
938 * The connection is now fully connected. At this point we call the open callback
939 * so that the API client can install its normal set of I/O callbacks to deal with
940 * sending/receiving actual data.
941 *
942 * After this, the connection will only transition states if an API client
943 * explicitly calls connection_signal_reconnect.
944 *
945 * The connection API cannot monitor the connection for failure conditions.
946 *
947 * @param[in] conn Entering the connecting state.
948 */
950{
951 int ret;
952
954
956
957 FR_TIMER_DISARM(conn->ev);
958 WATCH_PRE(conn);
959 if (conn->open) {
960 HANDLER_BEGIN(conn, conn->open);
961 DEBUG4("Calling open(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
962 ret = conn->open(conn->pub.el, conn->pub.h, conn->uctx);
963 HANDLER_END(conn);
964 } else {
966 }
967
968 switch (ret) {
969 /*
970 * Callback agrees everything is connected
971 */
973 DEBUG2("Connection established");
974 WATCH_POST(conn); /* Only call if we successfully connected */
975 return;
976
977 /*
978 * Open callback failed
979 */
981 default:
982 PERROR("Connection failed");
984 return;
985 }
986}
987
988/** Enter the connecting state
989 *
990 * After this function returns we wait to be signalled with connection_singal_connected
991 * or for the connection timer to expire.
992 *
993 * @param[in] conn Entering the connecting state.
994 */
996{
997 switch (conn->pub.state) {
999 break;
1000
1001 default:
1003 return;
1004 }
1005
1007
1008 WATCH_PRE(conn);
1009 WATCH_POST(conn);
1010
1011 /*
1012 * If there's a connection timeout,
1013 * set, then add the timer.
1014 */
1016 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
1017 conn->connection_timeout, false, _connection_timeout, conn) < 0) {
1018 PERROR("Failed setting connection_timeout event, failing connection");
1019
1020 /*
1021 * This can happen when the event loop
1022 * is exiting.
1023 *
1024 * Entering fail will close partially
1025 * open connection and then, if we still
1026 * can't insert a timer, then the connection
1027 * will be halted and sit idle until its
1028 * freed.
1029 */
1031 }
1032 }
1033}
1034
1035/** Initial state of the connection
1036 *
1037 * Calls the init function we were passed to allocate a library specific handle or
1038 * file descriptor.
1039 *
1040 * @param[in] conn To initialise.
1041 */
1043{
1045
1046 switch (conn->pub.state) {
1050 break;
1051
1052 default:
1054 return;
1055 }
1056
1057 /*
1058 * Increment every time we enter
1059 * We have to do this, as we don't know
1060 * whether the connection was halted by
1061 * the failed callback, and is now being
1062 * reconnected, or was automatically
1063 * reconnected.
1064 */
1065 conn->pub.reconnected++;
1066
1068
1069 /*
1070 * If we have an init callback, call it.
1071 */
1072 WATCH_PRE(conn);
1073 if (conn->init) {
1074 HANDLER_BEGIN(conn, conn->init);
1075 DEBUG4("Calling init(h_out=%p, conn=%p, uctx=%p)", &conn->pub.h, conn, conn->uctx);
1076 ret = conn->init(&conn->pub.h, conn, conn->uctx);
1077 HANDLER_END(conn);
1078 } else {
1080 }
1081
1082 switch (ret) {
1084 conn->is_closed = false; /* We now have a handle */
1085 WATCH_POST(conn); /* Only call if we successfully initialised the handle */
1087 return;
1088
1090 conn->is_closed = false; /* We now have a handle */
1091 WATCH_POST(conn); /* Only call if we successfully initialised the handle */
1093 return;
1094
1095 /*
1096 * Initialisation callback failed
1097 */
1099 default:
1100 PERROR("Connection initialisation failed");
1102 break;
1103 }
1104}
1105
1106/** Asynchronously signal a halted connection to start
1107 *
1108 */
1110{
1111 DEBUG2("Signalled to start from %s state",
1112 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1113
1114 if (DEFER_SIGNALS(conn)) {
1116 return;
1117 }
1118
1119 switch (conn->pub.state) {
1122 break;
1123
1124 default:
1125 break;
1126 }
1127}
1128
1129/** Asynchronously signal that the connection is open
1130 *
1131 * Some libraries like libldap are extremely annoying and only return control
1132 * to the caller after a connection is open.
1133 *
1134 * For these libraries, we can't use an I/O handler to determine when the
1135 * connection is open so we rely on callbacks built into the library to
1136 * signal that the transition has occurred.
1137 *
1138 */
1140{
1141 fr_assert(!conn->open); /* Use one or the other not both! */
1142
1143 DEBUG2("Signalled connected from %s state",
1144 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1145
1146 if (DEFER_SIGNALS(conn)) {
1148 return;
1149 }
1150
1151 switch (conn->pub.state) {
1154 break;
1155
1156 default:
1157 break;
1158 }
1159}
1160
1161/** Asynchronously signal the connection should be reconnected
1162 *
1163 * Should be called if the caller has knowledge that the connection is bad
1164 * and should be reconnected.
1165 *
1166 * @param[in] conn to reconnect.
1167 * @param[in] reason Why the connection was signalled to reconnect.
1168 */
1170{
1171 DEBUG2("Signalled to reconnect from %s state",
1172 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1173
1174 if (DEFER_SIGNALS(conn)) {
1175 if ((reason == CONNECTION_EXPIRED) && conn->shutdown) {
1177 return;
1178 }
1179
1181 return;
1182 }
1183
1184 switch (conn->pub.state) {
1185 case CONNECTION_STATE_CLOSED: /* Don't circumvent reconnection_delay */
1186 case CONNECTION_STATE_INIT: /* Already initialising */
1187 break;
1188
1191 break;
1192
1194 if (reason == CONNECTION_EXPIRED) break; /* Already shutting down */
1196 break;
1197
1199 if (reason == CONNECTION_EXPIRED) {
1200 if (conn->shutdown) {
1202 break;
1203 }
1205 break;
1206 }
1208
1213 break;
1214
1216 fr_assert(0);
1217 return;
1218 }
1219}
1220
1221/** Shuts down a connection gracefully
1222 *
1223 * If a shutdown function has been provided, it is called.
1224 * It's then up to the shutdown function to install I/O handlers to signal
1225 * when the connection has finished shutting down and should be closed
1226 * via #connection_signal_halt.
1227 *
1228 * @param[in] conn to shutdown.
1229 */
1231{
1232 DEBUG2("Signalled to shutdown from %s state",
1233 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1234
1235 if (DEFER_SIGNALS(conn)) {
1237 return;
1238 }
1239
1240 switch (conn->pub.state) {
1243 break;
1244
1247 break;
1248
1249 /*
1250 * If the connection is connected it needs to be
1251 * shutdown first.
1252 *
1253 * The shutdown callback or an FD event it inserts then
1254 * to signal that the connection should be closed.
1255 */
1257 if (conn->shutdown) {
1259 break;
1260 }
1262
1263 /*
1264 * If the connection is any of these states it
1265 * must have completed INIT which means it has
1266 * an active handle which needs to be closed before
1267 * the connection is halted.
1268 */
1273 fr_assert(conn->is_closed);
1274
1278 break;
1279
1281 fr_assert(0);
1282 return;
1283 }
1284}
1285
1286/** Shuts down a connection ungracefully
1287 *
1288 * If a connection is in an open or connection state it will be closed immediately.
1289 * Otherwise the connection will transition directly to the halted state.
1290 *
1291 * @param[in] conn to halt.
1292 */
1294{
1295 DEBUG2("Signalled to halt from %s state",
1296 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1297
1298 if (DEFER_SIGNALS(conn)) {
1300 return;
1301 }
1302
1303 switch (conn->pub.state) {
1305 break;
1306
1310 break;
1311
1312 /*
1313 * If the connection is any of these states it
1314 * must have completed INIT which means it has
1315 * an active handle which needs to be closed before
1316 * the connection is halted.
1317 *
1318 * The exception is when a connection fails to open
1319 * so goes from INIT -> FAILED, means is_closed
1320 * is true, as the connection has never opened.
1321 */
1327 if (!conn->is_closed) connection_state_enter_closed(conn);
1328 fr_assert(conn->is_closed);
1330 break;
1331
1333 fr_assert(0);
1334 return;
1335 }
1336}
1337/** Receive an error notification when we're connecting a socket
1338 *
1339 * @param[in] el event list the I/O event occurred on.
1340 * @param[in] fd the I/O even occurred for.
1341 * @param[in] flags from_kevent.
1342 * @param[in] fd_errno from kevent.
1343 * @param[in] uctx The #connection_t this fd is associated with.
1344 */
1345static void _connection_error(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, int fd_errno, void *uctx)
1346{
1347 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
1348
1349 ERROR("Connection failed for fd (%d): %s", fd, fr_syserror(fd_errno));
1351}
1352
1353/** Receive a write notification after a socket is connected
1354 *
1355 * @param[in] el event list the I/O event occurred on.
1356 * @param[in] fd the I/O even occurred for.
1357 * @param[in] flags from kevent.
1358 * @param[in] uctx The #connection_t this fd is associated with.
1359 */
1360static void _connection_writable(fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
1361{
1362 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
1363
1366}
1367
1368/** Remove the FD we were watching for connection open/fail from the event loop
1369 *
1370 */
1372 UNUSED connection_state_t prev, connection_state_t state, void *uctx)
1373{
1374 int fd = *(talloc_get_type_abort(uctx, int));
1375
1376 /*
1377 * Two states can trigger a cleanup
1378 * Remove the watch on the one that didn't
1379 */
1380 switch (state) {
1383 break;
1384
1387 break;
1388
1389 default:
1390 fr_assert(0);
1391 break;
1392 }
1393
1395 talloc_free(uctx);
1396}
1397
1398/** Setup the connection to change states to connected or failed based on I/O events
1399 *
1400 * Will automatically cleanup after itself, in preparation for
1401 * new I/O handlers to be installed in the open() callback.
1402 *
1403 * @return
1404 * - 0 on success.
1405 * - -1 on failure.
1406 */
1408{
1409 int *fd_s;
1410
1411 /*
1412 * If connection becomes writable we
1413 * assume it's open.
1414 */
1415 if (fr_event_fd_insert(conn, NULL, conn->pub.el, fd,
1416 NULL,
1419 conn) < 0) {
1420 PERROR("Failed inserting fd (%d) into event loop %p",
1421 fd, conn->pub.el);
1423 return -1;
1424 }
1425
1426 /*
1427 * Stop the static analysis tools
1428 * complaining about assigning ints
1429 * to pointers.
1430 */
1431 MEM(fd_s = talloc_zero(conn, int));
1432 *fd_s = fd;
1433
1434 /*
1435 * Add a oneshot watcher to remove
1436 * the I/O handlers if the connection
1437 * fails, or is connected.
1438 */
1443 return 0;
1444}
1445
1446/** Tell a failed connection to move to CONNECTION_STATE_INIT
1447 *
1448 */
1453
1454/** Close a connection if it's freed
1455 *
1456 * @param[in] conn to free.
1457 * @return
1458 * - 0 connection was freed immediately.
1459 * - 1 connection free was deferred.
1460 */
1462{
1463 /*
1464 * Explicitly cancel any pending events
1465 */
1466 FR_TIMER_DELETE_RETURN(&conn->ev);
1467 /*
1468 * Don't allow the connection to be
1469 * arbitrarily freed by a callback.
1470 *
1471 * Add a deferred signal to free the
1472 * connection later.
1473 */
1474 if (DEFER_SIGNALS(conn)) {
1476 return -1;
1477 }
1478
1479 switch (conn->pub.state) {
1481 break;
1482
1483 /*
1484 * Need to close the connection first
1485 */
1490
1491 default:
1493 break;
1494 }
1495 return 0;
1496}
1497
1498/** Allocate a new connection
1499 *
1500 * After the connection has been allocated, it should be started with a call to #connection_signal_init.
1501 *
1502 * The connection state machine can detect when the connection is open in one of two ways.
1503 * - You can install a generic socket open/fail callback, using connection_signal_on_fd.
1504 * - You can call either #connection_signal_connected or connection_signal_recommend.
1505 * This allows the connection state machine to work with more difficult library APIs,
1506 * which may not return control to the caller as connections are opened.
1507 *
1508 * @param[in] ctx to allocate connection handle in. If the connection
1509 * handle is freed, and the #connection_state_t is
1510 * #CONNECTION_STATE_CONNECTING or #CONNECTION_STATE_CONNECTED the
1511 * close callback will be called.
1512 * @param[in] el to use for timer events, and to pass to the #connection_open_t callback.
1513 * @param[in] funcs callback functions.
1514 * @param[in] conf our configuration.
1515 * @param[in] log_prefix To prepend to log messages.
1516 * @param[in] uctx User context to pass to callbacks.
1517 * @return
1518 * - A new #connection_t on success.
1519 * - NULL on failure.
1520 */
1522 connection_funcs_t const *funcs,
1523 connection_conf_t const *conf,
1524 char const *log_prefix,
1525 void const *uctx)
1526{
1527 size_t i;
1528 connection_t *conn;
1529 uint64_t id;
1530
1531 fr_assert(el);
1532
1533 MEM(conn = talloc(ctx, connection_t));
1534 talloc_set_destructor(conn, _connection_free);
1535
1537
1538 *conn = (connection_t){
1539 .pub = {
1540 .id = id,
1541 .state = CONNECTION_STATE_HALTED,
1542 .el = el
1543 },
1544 .reconnection_delay = conf->reconnection_delay,
1545 .connection_timeout = conf->connection_timeout,
1546 .init = funcs->init,
1547 .open = funcs->open,
1548 .close = funcs->close,
1549 .failed = funcs->failed,
1550 .shutdown = funcs->shutdown,
1551 .is_closed = true, /* Starts closed */
1552 .triggers = conf->triggers,
1553 .trigger_args = conf->trigger_args,
1554 .trigger_cs = conf->trigger_cs,
1555 .pub.name = talloc_asprintf(conn, "%s - [%" PRIu64 "]", log_prefix, id)
1556 };
1557 memcpy(&conn->uctx, &uctx, sizeof(conn->uctx));
1558
1559 for (i = 0; i < NUM_ELEMENTS(conn->watch_pre); i++) {
1561 }
1562 for (i = 0; i < NUM_ELEMENTS(conn->watch_post); i++) {
1564 }
1566
1567 /*
1568 * Pre-allocate a on_halt watcher for deferred signal processing
1569 */
1572 connection_watch_disable(conn->on_halted); /* Start disabled */
1573
1574 return conn;
1575}
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define CC_NO_UBSAN(_sanitize)
Definition build.h:428
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
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:36
#define ERROR(fmt,...)
Definition dhcpclient.c:41
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:486
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:638
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:531
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:378
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:275
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:555
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:248
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:84
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG4(_fmt,...)
Definition log.h:267
talloc_free(reap)
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:38
#define DEBUG2(fmt,...)
Definition radclient.h:43
static rs_t * conf
Definition radsniff.c:53
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:359
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:546
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:421
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:615
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:228
void connection_watch_disable(connection_watch_entry_t *entry)
Disable a watcher.
Definition connection.c:556
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:474
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:695
bool connection_watch_is_enabled(connection_watch_entry_t *entry)
Return the state of a watch entry.
Definition connection.c:592
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:769
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:626
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:408
void connection_signals_resume(connection_t *conn)
Resume processing of deferred signals.
Definition connection.c:321
#define HANDLER_BEGIN(_conn, _func)
Called when we enter a handler.
Definition connection.c:339
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:484
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:603
bool triggers
Do we run triggers.
Definition connection.c:127
static void connection_state_enter_connecting(connection_t *conn)
Enter the connecting state.
Definition connection.c:995
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:395
static void connection_deferred_signal_process(connection_t *conn)
Process any deferred signals.
Definition connection.c:239
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:579
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:645
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
connection_state_t connection_failed_reinit(UNUSED void *handle, UNUSED connection_state_t state, UNUSED void *uctx)
Tell a failed connection to move to CONNECTION_STATE_INIT.
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:348
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:916
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:512
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:534
static void connection_state_enter_connected(connection_t *conn)
Enter the connected state.
Definition connection.c:949
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:457
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:567
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:705
void connection_signals_pause(connection_t *conn)
Pause processing of deferred signals.
Definition connection.c:312
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:892
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 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:50
A timer event.
Definition timer.c:84
#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:365