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: 91d4f9c837aafcfa73a0187d7913ee5eb7ba2140 $
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};
128
129#define CONN_TRIGGER(_state) do { \
130 trigger(unlang_interpret_get_thread_default(), \
131 conn->trigger_cs, fr_table_str_by_value(connection_trigger_names, _state, "<INVALID>"), true, conn->trigger_args); \
132} while (0)
133
134#define STATE_TRANSITION(_new) \
135do { \
136 DEBUG2("Connection changed state %s -> %s", \
137 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"), \
138 fr_table_str_by_value(connection_states, _new, "<INVALID>")); \
139 conn->pub.prev = conn->pub.state; \
140 conn->pub.state = _new; \
141 CONN_TRIGGER(_new); \
142} while (0)
143
144#define BAD_STATE_TRANSITION(_new) \
145do { \
146 if (!fr_cond_assert_msg(0, "Connection %" PRIu64 " invalid transition %s -> %s", \
147 conn->pub.id, \
148 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"), \
149 fr_table_str_by_value(connection_states, _new, "<INVALID>"))) return; \
150} while (0)
151
152#define DEFER_SIGNALS(_conn) ((_conn)->in_handler || (_conn)->signals_pause)
153
154/** Deferred signals
155 *
156 */
157typedef enum {
158 CONNECTION_DSIGNAL_INIT, //!< Restart a halted connection.
159 CONNECTION_DSIGNAL_CONNECTED, //!< Signal that a connection is connected.
160 CONNECTION_DSIGNAL_RECONNECT_FAILED, //!< Reconnect a failed connection.
161 CONNECTION_DSIGNAL_RECONNECT_EXPIRED, //!< Reconnect an expired connection (gracefully).
162 CONNECTION_DSIGNAL_SHUTDOWN, //!< Close a connection (gracefully).
163 CONNECTION_DSIGNAL_HALT, //!< Close a connection (ungracefully).
164 CONNECTION_DSIGNAL_FREE //!< Free a connection (no further dsignals processed).
166
168 { L("INIT"), CONNECTION_DSIGNAL_INIT },
169 { L("CONNECTED"), CONNECTION_DSIGNAL_CONNECTED },
170 { L("RECONNECT-FAILED"), CONNECTION_DSIGNAL_RECONNECT_FAILED },
171 { L("RECONNECT-EXPIRED"), CONNECTION_DSIGNAL_RECONNECT_EXPIRED },
172 { L("SHUTDOWN"), CONNECTION_DSIGNAL_SHUTDOWN },
173 { L("HALT"), CONNECTION_DSIGNAL_HALT },
174 { L("FREE"), CONNECTION_DSIGNAL_FREE }
175};
177
178/** Holds a signal from a handler until it's safe to process it
179 *
180 */
181typedef struct {
182 fr_dlist_t entry; //!< Entry in the signals list.
183 connection_dsignal_t signal; //!< Signal that was deferred.
185
186/*
187 * State transition functions
188 */
197
198/** Add a deferred signal to the signal list
199 *
200 * Processing signals whilst in handlers usually leads to weird
201 * inconsistent states within the connection.
202 *
203 * If a public signal function is called, and detects its being called
204 * from within the handler, it instead adds a deferred signal entry
205 * and immediately returns.
206 *
207 * Once the handler is complete, and all pending C stack state changes
208 * are complete, the deferred signals are drained and processed.
209 */
211{
212 connection_dsignal_entry_t *dsignal, *prev;
213
214 prev = fr_dlist_tail(&conn->deferred_signals);
215 if (prev && (prev->signal == signal)) return; /* Don't insert duplicates */
216
217 MEM(dsignal = talloc_zero(conn, connection_dsignal_entry_t));
218 dsignal->signal = signal;
219 fr_dlist_insert_tail(&conn->deferred_signals, dsignal);
220
221// DEBUG4("Adding deferred signal - %s", fr_table_str_by_value(connection_dsignals, signal, "<INVALID>"));
222}
223
224/** Notification function to tell connection_deferred_signal_process that the connection has been freed
225 *
226 */
229 UNUSED connection_state_t state, void *uctx)
230{
231 bool *freed = uctx;
232 *freed = true;
233}
234
235/** Process any deferred signals
236 *
237 */
239{
241 bool freed = false;
242
243 /*
244 * We're inside and an instance of this function
245 * higher in the call stack. Don't do anything.
246 */
247 if (conn->processing_signals) return;
248
249 /*
250 * Get notified if the connection gets freed
251 * out from under us...
252 */
254 conn->processing_signals = true;
255
256 while ((dsignal = fr_dlist_head(&conn->deferred_signals))) {
258 fr_dlist_remove(&conn->deferred_signals, dsignal);
259 signal = dsignal->signal;
260 talloc_free(dsignal);
261
262 DEBUG4("Processing deferred signal - %s",
263 fr_table_str_by_value(connection_dsignals, signal, "<INVALID>"));
264
265 switch (signal) {
268 break;
269
272 break;
273
274 case CONNECTION_DSIGNAL_RECONNECT_FAILED: /* Reconnect - Failed */
276 break;
277
278 case CONNECTION_DSIGNAL_RECONNECT_EXPIRED: /* Reconnect - Expired */
280 break;
281
284 break;
285
288 break;
289
290 case CONNECTION_DSIGNAL_FREE: /* Freed */
291 talloc_free(conn);
292 return;
293 }
294
295 /*
296 * One of the signal handlers freed the connection
297 * return immediately.
298 */
299 /* coverity[dead_error_line] */
300 if (freed) return;
301 }
302
303 conn->processing_signals = false;
305}
306
307/** Pause processing of deferred signals
308 *
309 * @param[in] conn to pause signal processing for.
310 */
312{
313 conn->signals_pause++;
314}
315
316/** Resume processing of deferred signals
317 *
318 * @param[in] conn to resume signal processing for.
319 */
321{
322 if (conn->signals_pause > 0) conn->signals_pause--;
323 if (conn->signals_pause > 0) return;
324
325 /*
326 * If we're not in a handler process the
327 * deferred signals now.
328 */
329 if (!conn->in_handler) {
331 return;
332 }
333}
334
335/** Called when we enter a handler
336 *
337 */
338#define HANDLER_BEGIN(_conn, _func) \
339void *_prev_handler = (_conn)->in_handler; \
340do { \
341 (_conn)->in_handler = (void *)(_func); \
342} while (0)
343
344/** Called when we exit a handler
345 *
346 */
347#define HANDLER_END(_conn) \
348do { \
349 (_conn)->in_handler = _prev_handler; \
350 if (!(_conn)->signals_pause && (!(_conn)->in_handler)) connection_deferred_signal_process(_conn); \
351} while(0)
352
353
354/** Call a list of watch functions associated with a state
355 *
356 */
357CC_NO_UBSAN(function) /* UBSAN: false positive - Public/private version of connection_t trips -fsanitize=function */
358static inline void connection_watch_call(connection_t *conn, fr_dlist_head_t *list)
359{
360 /*
361 * Nested watcher calls are not allowed
362 * and shouldn't be possible because of
363 * deferred signal processing.
364 */
365 fr_assert(conn->next_watcher == NULL);
366
367 while ((conn->next_watcher = fr_dlist_next(list, conn->next_watcher))) {
368 connection_watch_entry_t *entry = conn->next_watcher;
369 bool oneshot = entry->oneshot; /* Watcher could be freed, so store now */
370
371 if (!entry->enabled) continue;
372 if (oneshot) conn->next_watcher = fr_dlist_remove(list, entry);
373
374/*
375 DEBUG4("Notifying %swatcher - (%p)(conn=%p, prev=%s, state=%s, uctx=%p)",
376 entry->oneshot ? "oneshot " : "",
377 entry->func,
378 conn,
379 fr_table_str_by_value(connection_states, conn->pub.prev, "<INVALID>"),
380 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"),
381 entry->uctx);
382*/
383
384 entry->func(conn, conn->pub.prev, conn->pub.state, entry->uctx);
385
386 if (oneshot) talloc_free(entry);
387 }
388 conn->next_watcher = NULL;
389}
390
391/** Call the pre handler watch functions
392 *
393 */
394#define WATCH_PRE(_conn) \
395do { \
396 if (fr_dlist_empty(&(_conn)->watch_pre[(_conn)->pub.state])) break; \
397 { \
398 HANDLER_BEGIN(conn, &(_conn)->watch_pre[(_conn)->pub.state]); \
399 connection_watch_call((_conn), &(_conn)->watch_pre[(_conn)->pub.state]); \
400 HANDLER_END(conn); \
401 } \
402} while(0)
403
404/** Call the post handler watch functions
405 *
406 */
407#define WATCH_POST(_conn) \
408do { \
409 if (fr_dlist_empty(&(_conn)->watch_post[(_conn)->pub.state])) break; \
410 { \
411 HANDLER_BEGIN(conn, &(_conn)->watch_post[(_conn)->pub.state]); \
412 connection_watch_call((_conn), &(_conn)->watch_post[(_conn)->pub.state]); \
413 HANDLER_END(conn); \
414 } \
415} while(0)
416
417/** Remove a watch function from a pre/post[state] list
418 *
419 */
420static int connection_del_watch(connection_t *conn, fr_dlist_head_t *state_lists,
422{
423 connection_watch_entry_t *entry = NULL;
424 fr_dlist_head_t *list = &state_lists[state];
425
426 while ((entry = fr_dlist_next(list, entry))) {
427 if (entry->func == watch) {
428/*
429 DEBUG4("Removing %s watcher %p",
430 fr_table_str_by_value(connection_states, state, "<INVALID>"),
431 watch);
432*/
433 if (conn->next_watcher == entry) {
434 conn->next_watcher = fr_dlist_remove(list, entry);
435 } else {
436 fr_dlist_remove(list, entry);
437 }
438 talloc_free(entry);
439 return 0;
440 }
441 }
442
443 return -1;
444}
445
446/** Remove a watch function from a pre list
447 *
448 * @param[in] conn The connection to remove the watcher from.
449 * @param[in] state to remove the watch from.
450 * @param[in] watch Function to remove.
451 * @return
452 * - 0 if the function was removed successfully.
453 * - -1 if the function wasn't present in the watch list.
454 * - -2 an invalid state was passed.
455 */
457{
458 if (state >= CONNECTION_STATE_MAX) return -2;
459
460 return connection_del_watch(conn, conn->watch_pre, state, watch);
461}
462
463/** Remove a watch function from a post list
464 *
465 * @param[in] conn The connection to remove the watcher from.
466 * @param[in] state to remove the watch from.
467 * @param[in] watch Function to remove.
468 * @return
469 * - 0 if the function was removed successfully.
470 * - -1 if the function wasn't present in the watch list.
471 * - -2 an invalid state was passed.
472 */
474{
475 if (state >= CONNECTION_STATE_MAX) return -2;
476
477 return connection_del_watch(conn, conn->watch_post, state, watch);
478}
479
480/** Add a watch entry to the pre/post[state] list
481 *
482 */
484 connection_watch_t watch, bool oneshot, void const *uctx)
485{
487
488 MEM(entry = talloc_zero(conn, connection_watch_entry_t));
489
490 entry->func = watch;
491 entry->oneshot = oneshot;
492 entry->enabled = true;
493 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
494
495 fr_dlist_insert_tail(list, entry);
496
497 return entry;
498}
499
500/** Add a callback to be executed before a state function has been called
501 *
502 * @param[in] conn to add watcher to.
503 * @param[in] state to call watcher on entering.
504 * @param[in] watch function to call.
505 * @param[in] oneshot If true, remove the function after calling.
506 * @param[in] uctx to pass to callbacks.
507 * @return
508 * - NULL if state value is invalid.
509 * - A new watch entry handle.
510 */
512 connection_watch_t watch, bool oneshot, void const *uctx)
513{
514 if (state >= CONNECTION_STATE_MAX) return NULL;
515
516 return connection_add_watch(conn, &conn->watch_pre[state], watch, oneshot, uctx);
517}
518
519/** Add a callback to be executed after a state function has been called
520 *
521 * Where a user callback is executed on state change, the post function
522 * is only called if the callback succeeds.
523 *
524 * @param[in] conn to add watcher to.
525 * @param[in] state to call watcher on entering.
526 * @param[in] watch function to call.
527 * @param[in] oneshot If true, remove the function after calling.
528 * @param[in] uctx to pass to callbacks.
529 * @return
530 * - NULL if state value is invalid.
531 * - A new watch entry handle.
532 */
534 connection_watch_t watch, bool oneshot, void const *uctx)
535{
536 if (state >= CONNECTION_STATE_MAX) return NULL;
537
538 return connection_add_watch(conn, &conn->watch_post[state], watch, oneshot, uctx);
539}
540
541/** Enable a watcher
542 *
543 * @param[in] entry to enabled.
544 */
546{
547 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
548 entry->enabled = true;
549}
550
551/** Disable a watcher
552 *
553 * @param[in] entry to disable.
554 */
556{
557 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
558 entry->enabled = false;
559}
560
561/** Enable a watcher and replace the uctx
562 *
563 * @param[in] entry to enabled.
564 * @param[in] uctx Opaque data to pass to the callback.
565 */
567{
568 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
569 entry->enabled = true;
570 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
571}
572
573/** Change the uctx of an entry
574 *
575 * @param[in] entry to enabled.
576 * @param[in] uctx Opaque data to pass to the callback.
577 */
579{
580 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
581 memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
582}
583
584/** Return the state of a watch entry
585 *
586 * @param[in] entry to return state of.
587 * @return
588 * - true if enabled.
589 * - false if disabled.
590 */
592{
593 (void)talloc_get_type_abort(entry, connection_watch_entry_t);
594 return entry->enabled;
595}
596
597/** Return the number of times we've attempted to establish or re-establish this connection
598 *
599 * @param[in] conn to get count from.
600 * @return the number of times the connection has reconnected.
601 */
603{
604 if (conn->pub.reconnected == 0) return 0; /* Has never been initialised */
605
606 return conn->pub.reconnected - 1; /* We don't count the first connection attempt */
607}
608
609/** Return the number of times this connection has timed out whilst connecting
610 *
611 * @param[in] conn to get count from.
612 * @return the number of times the connection has timed out whilst connecting.
613 */
615{
616 return conn->pub.timed_out;
617}
618
619/** The requisite period of time has passed, try and re-open the connection
620 *
621 * @param[in] tl containing the timer event.
622 * @param[in] now The current time.
623 * @param[in] uctx The #connection_t the fd is associated with.
624 */
626{
627 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
628
629 switch (conn->pub.state) {
633 break;
634
635 default:
637 break;
638 }
639}
640
641/** Close the connection, then wait for another state change
642 *
643 */
645{
646 switch (conn->pub.state) {
650 break;
651
652 default:
654 return;
655 }
656
658
659 FR_TIMER_DISARM(conn->ev);
660
661 /*
662 * If there's a close callback, call it, so that the
663 * API client can free any resources associated
664 * with the connection handle.
665 */
666 WATCH_PRE(conn);
667
668 /*
669 * is_closed is for pure paranoia. If everything
670 * is working correctly this state should never
671 * be entered if the connection is closed.
672 */
673 fr_assert(!conn->is_closed);
674 if (conn->close && !conn->is_closed) {
675 HANDLER_BEGIN(conn, conn->close);
676 DEBUG4("Calling close(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
677 conn->close(conn->pub.el, conn->pub.h, conn->uctx);
678 conn->is_closed = true; /* Ensure close doesn't get called twice if the connection is freed */
679 HANDLER_END(conn);
680 } else {
681 conn->is_closed = true;
682 }
683 WATCH_POST(conn);
684}
685
686/** Connection timeout
687 *
688 * Connection wasn't opened within the configured period of time
689 *
690 * @param[in] tl timer list the event belonged to.
691 * @param[in] now The current time.
692 * @param[in] uctx The #connection_t the fd is associated with.
693 */
695{
696 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
697
699}
700
701/** Gracefully shutdown the handle
702 *
703 */
705{
707
708 switch (conn->pub.state) {
710 break;
711
712 default:
714 return;
715 }
716
718
719 WATCH_PRE(conn);
720 {
721 HANDLER_BEGIN(conn, conn->shutdown);
722 DEBUG4("Calling shutdown(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
723 ret = conn->shutdown(conn->pub.el, conn->pub.h, conn->uctx);
724 HANDLER_END(conn);
725 }
726 switch (ret) {
728 break;
729
730 default:
732 return;
733 }
734 WATCH_POST(conn);
735
736 /*
737 * If there's a connection timeout,
738 * set, then add the timer.
739 *
740 * The connection may be bad, in which
741 * case we want to automatically fail
742 * if it doesn't shutdown within the
743 * timeout period.
744 */
746 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
747 conn->connection_timeout, false, _connection_timeout, conn) < 0) {
748 /*
749 * Can happen when the event loop is exiting
750 */
751 PERROR("Failed setting connection_timeout timer, closing connection");
753 }
754 }
755}
756
757/** Connection failed
758 *
759 * Transition to the CONNECTION_STATE_FAILED state.
760 *
761 * If the connection was open, or couldn't be opened wait for reconnection_delay before transitioning
762 * back to init.
763 *
764 * If no reconnection_delay was set, transition to halted.
765 *
766 * @param[in] conn that failed.
767 */
769{
772
774
775 /*
776 * Explicit error occurred, delete the connection timer
777 */
778 FR_TIMER_DISARM(conn->ev);
779
780 /*
781 * Record what state the connection is currently in
782 * so we can figure out what to do next.
783 */
784 prev = conn->pub.state;
785
786 /*
787 * Now transition to failed
788 */
790
791 /*
792 * If there's a failed callback, give it the
793 * opportunity to suspend/destroy the
794 * connection.
795 */
796 WATCH_PRE(conn);
797 if (conn->failed) {
798 HANDLER_BEGIN(conn, conn->failed);
799 DEBUG4("Calling failed(h=%p, state=%s, uctx=%p)", conn->pub.h,
800 fr_table_str_by_value(connection_states, prev, "<INVALID>"), conn->uctx);
801 ret = conn->failed(conn->pub.h, prev, conn->uctx);
802 HANDLER_END(conn);
803 }
804 WATCH_POST(conn);
805
806 /*
807 * Enter the closed state if we failed during
808 * connecting, or when we were connected.
809 */
810 switch (prev) {
813 case CONNECTION_STATE_TIMEOUT: /* Timeout means the connection progress past init */
814 case CONNECTION_STATE_SHUTDOWN: /* Shutdown means the connection failed whilst shutting down */
816 break;
817
818 default:
819 break;
820 }
821
822 if (conn->failed) {
823 switch (ret) {
824 /*
825 * The callback signalled it wants the
826 * connection to be reinitialised
827 * after reconnection_delay, or
828 * immediately if the failure was due
829 * to a connection timeout.
830 */
832 break;
833
834 /*
835 * The callback signalled it wants the
836 * connection to stop.
837 */
839 default:
841 return;
842 }
843 }
844
845 /*
846 * What previous state we were in
847 * determines if we need to apply the
848 * reconnect timeout.
849 */
850 switch (prev) {
851 case CONNECTION_STATE_INIT: /* Failed during initialisation */
852 case CONNECTION_STATE_CONNECTED: /* Failed after connecting */
853 case CONNECTION_STATE_CONNECTING: /* Failed during connecting */
854 case CONNECTION_STATE_SHUTDOWN: /* Failed during shutdown */
856 DEBUG2("Delaying reconnection by %pVs", fr_box_time_delta(conn->reconnection_delay));
857 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
858 conn->reconnection_delay, false, _reconnect_delay_done, conn) < 0) {
859 /*
860 * Can happen when the event loop is exiting
861 */
862 PERROR("Failed inserting reconnection_delay timer event, halting connection");
864 }
865 return;
866 }
867
868 /*
869 * If there's no reconnection
870 * delay, then don't automatically
871 * reconnect, and wait to be
872 * signalled.
873 */
875 break;
876
877 case CONNECTION_STATE_TIMEOUT: /* Failed during connecting due to timeout */
879 break;
880
881 default:
882 fr_assert(0);
883 }
884}
885
886/** Enter the timeout state
887 *
888 * The connection took took long to open. Timeout the attempt and transition
889 * to the failed state.
890 */
892{
893 switch (conn->pub.state) {
896 break;
897
898 default:
900 }
901
902 ERROR("Connection failed - timed out after %pVs", fr_box_time_delta(conn->connection_timeout));
903
905
906 conn->pub.timed_out++;
907
909}
910
911/** Enter the halted state
912 *
913 * Here we wait, until signalled by connection_signal_reconnect.
914 */
916{
917 fr_assert(conn->is_closed);
918
919 switch (conn->pub.state) {
920 case CONNECTION_STATE_FAILED: /* Init failure */
922 break;
923
924 default:
926 }
927
928 FR_TIMER_DISARM(conn->ev);
929
931 WATCH_PRE(conn);
932 WATCH_POST(conn);
933}
934
935/** Enter the connected state
936 *
937 * The connection is now fully connected. At this point we call the open callback
938 * so that the API client can install its normal set of I/O callbacks to deal with
939 * sending/receiving actual data.
940 *
941 * After this, the connection will only transition states if an API client
942 * explicitly calls connection_signal_reconnect.
943 *
944 * The connection API cannot monitor the connection for failure conditions.
945 *
946 * @param[in] conn Entering the connecting state.
947 */
949{
950 int ret;
951
953
955
956 FR_TIMER_DISARM(conn->ev);
957 WATCH_PRE(conn);
958 if (conn->open) {
959 HANDLER_BEGIN(conn, conn->open);
960 DEBUG4("Calling open(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
961 ret = conn->open(conn->pub.el, conn->pub.h, conn->uctx);
962 HANDLER_END(conn);
963 } else {
965 }
966
967 switch (ret) {
968 /*
969 * Callback agrees everything is connected
970 */
972 DEBUG2("Connection established");
973 WATCH_POST(conn); /* Only call if we successfully connected */
974 return;
975
976 /*
977 * Open callback failed
978 */
980 default:
981 PERROR("Connection failed");
983 return;
984 }
985}
986
987/** Enter the connecting state
988 *
989 * After this function returns we wait to be signalled with connection_singal_connected
990 * or for the connection timer to expire.
991 *
992 * @param[in] conn Entering the connecting state.
993 */
995{
996 switch (conn->pub.state) {
998 break;
999
1000 default:
1002 return;
1003 }
1004
1006
1007 WATCH_PRE(conn);
1008 WATCH_POST(conn);
1009
1010 /*
1011 * If there's a connection timeout,
1012 * set, then add the timer.
1013 */
1015 if (fr_timer_in(conn, conn->pub.el->tl, &conn->ev,
1016 conn->connection_timeout, false, _connection_timeout, conn) < 0) {
1017 PERROR("Failed setting connection_timeout event, failing connection");
1018
1019 /*
1020 * This can happen when the event loop
1021 * is exiting.
1022 *
1023 * Entering fail will close partially
1024 * open connection and then, if we still
1025 * can't insert a timer, then the connection
1026 * will be halted and sit idle until its
1027 * freed.
1028 */
1030 }
1031 }
1032}
1033
1034/** Initial state of the connection
1035 *
1036 * Calls the init function we were passed to allocate a library specific handle or
1037 * file descriptor.
1038 *
1039 * @param[in] conn To initialise.
1040 */
1042{
1044
1045 switch (conn->pub.state) {
1049 break;
1050
1051 default:
1053 return;
1054 }
1055
1056 /*
1057 * Increment every time we enter
1058 * We have to do this, as we don't know
1059 * whether the connection was halted by
1060 * the failed callback, and is now being
1061 * reconnected, or was automatically
1062 * reconnected.
1063 */
1064 conn->pub.reconnected++;
1065
1067
1068 /*
1069 * If we have an init callback, call it.
1070 */
1071 WATCH_PRE(conn);
1072 if (conn->init) {
1073 HANDLER_BEGIN(conn, conn->init);
1074 DEBUG4("Calling init(h_out=%p, conn=%p, uctx=%p)", &conn->pub.h, conn, conn->uctx);
1075 ret = conn->init(&conn->pub.h, conn, conn->uctx);
1076 HANDLER_END(conn);
1077 } else {
1079 }
1080
1081 switch (ret) {
1083 conn->is_closed = false; /* We now have a handle */
1084 WATCH_POST(conn); /* Only call if we successfully initialised the handle */
1086 return;
1087
1089 conn->is_closed = false; /* We now have a handle */
1090 WATCH_POST(conn); /* Only call if we successfully initialised the handle */
1092 return;
1093
1094 /*
1095 * Initialisation callback failed
1096 */
1098 default:
1099 PERROR("Connection initialisation failed");
1101 break;
1102 }
1103}
1104
1105/** Asynchronously signal a halted connection to start
1106 *
1107 */
1109{
1110 DEBUG2("Signalled to start from %s state",
1111 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1112
1113 if (DEFER_SIGNALS(conn)) {
1115 return;
1116 }
1117
1118 switch (conn->pub.state) {
1121 break;
1122
1123 default:
1124 break;
1125 }
1126}
1127
1128/** Asynchronously signal that the connection is open
1129 *
1130 * Some libraries like libldap are extremely annoying and only return control
1131 * to the caller after a connection is open.
1132 *
1133 * For these libraries, we can't use an I/O handler to determine when the
1134 * connection is open so we rely on callbacks built into the library to
1135 * signal that the transition has occurred.
1136 *
1137 */
1139{
1140 fr_assert(!conn->open); /* Use one or the other not both! */
1141
1142 DEBUG2("Signalled connected from %s state",
1143 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1144
1145 if (DEFER_SIGNALS(conn)) {
1147 return;
1148 }
1149
1150 switch (conn->pub.state) {
1153 break;
1154
1155 default:
1156 break;
1157 }
1158}
1159
1160/** Asynchronously signal the connection should be reconnected
1161 *
1162 * Should be called if the caller has knowledge that the connection is bad
1163 * and should be reconnected.
1164 *
1165 * @param[in] conn to reconnect.
1166 * @param[in] reason Why the connection was signalled to reconnect.
1167 */
1169{
1170 DEBUG2("Signalled to reconnect from %s state",
1171 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1172
1173 if (DEFER_SIGNALS(conn)) {
1174 if ((reason == CONNECTION_EXPIRED) && conn->shutdown) {
1176 return;
1177 }
1178
1180 return;
1181 }
1182
1183 switch (conn->pub.state) {
1184 case CONNECTION_STATE_CLOSED: /* Don't circumvent reconnection_delay */
1185 case CONNECTION_STATE_INIT: /* Already initialising */
1186 break;
1187
1190 break;
1191
1193 if (reason == CONNECTION_EXPIRED) break; /* Already shutting down */
1195 break;
1196
1198 if (reason == CONNECTION_EXPIRED) {
1199 if (conn->shutdown) {
1201 break;
1202 }
1204 break;
1205 }
1207
1212 break;
1213
1215 fr_assert(0);
1216 return;
1217 }
1218}
1219
1220/** Shuts down a connection gracefully
1221 *
1222 * If a shutdown function has been provided, it is called.
1223 * It's then up to the shutdown function to install I/O handlers to signal
1224 * when the connection has finished shutting down and should be closed
1225 * via #connection_signal_halt.
1226 *
1227 * @param[in] conn to shutdown.
1228 */
1230{
1231 DEBUG2("Signalled to shutdown from %s state",
1232 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1233
1234 if (DEFER_SIGNALS(conn)) {
1236 return;
1237 }
1238
1239 switch (conn->pub.state) {
1242 break;
1243
1246 break;
1247
1248 /*
1249 * If the connection is connected it needs to be
1250 * shutdown first.
1251 *
1252 * The shutdown callback or an FD event it inserts then
1253 * to signal that the connection should be closed.
1254 */
1256 if (conn->shutdown) {
1258 break;
1259 }
1261
1262 /*
1263 * If the connection is any of these states it
1264 * must have completed INIT which means it has
1265 * an active handle which needs to be closed before
1266 * the connection is halted.
1267 */
1272 fr_assert(conn->is_closed);
1273
1277 break;
1278
1280 fr_assert(0);
1281 return;
1282 }
1283}
1284
1285/** Shuts down a connection ungracefully
1286 *
1287 * If a connection is in an open or connection state it will be closed immediately.
1288 * Otherwise the connection will transition directly to the halted state.
1289 *
1290 * @param[in] conn to halt.
1291 */
1293{
1294 DEBUG2("Signalled to halt from %s state",
1295 fr_table_str_by_value(connection_states, conn->pub.state, "<INVALID>"));
1296
1297 if (DEFER_SIGNALS(conn)) {
1299 return;
1300 }
1301
1302 switch (conn->pub.state) {
1304 break;
1305
1309 break;
1310
1311 /*
1312 * If the connection is any of these states it
1313 * must have completed INIT which means it has
1314 * an active handle which needs to be closed before
1315 * the connection is halted.
1316 *
1317 * The exception is when a connection fails to open
1318 * so goes from INIT -> FAILED, means is_closed
1319 * is true, as the connection has never opened.
1320 */
1326 if (!conn->is_closed) connection_state_enter_closed(conn);
1327 fr_assert(conn->is_closed);
1329 break;
1330
1332 fr_assert(0);
1333 return;
1334 }
1335}
1336/** Receive an error notification when we're connecting a socket
1337 *
1338 * @param[in] el event list the I/O event occurred on.
1339 * @param[in] fd the I/O even occurred for.
1340 * @param[in] flags from_kevent.
1341 * @param[in] fd_errno from kevent.
1342 * @param[in] uctx The #connection_t this fd is associated with.
1343 */
1344static void _connection_error(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, int fd_errno, void *uctx)
1345{
1346 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
1347
1348 ERROR("Connection failed for fd (%d): %s", fd, fr_syserror(fd_errno));
1350}
1351
1352/** Receive a write notification after a socket is connected
1353 *
1354 * @param[in] el event list the I/O event occurred on.
1355 * @param[in] fd the I/O even occurred for.
1356 * @param[in] flags from kevent.
1357 * @param[in] uctx The #connection_t this fd is associated with.
1358 */
1359static void _connection_writable(fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
1360{
1361 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
1362
1365}
1366
1367/** Remove the FD we were watching for connection open/fail from the event loop
1368 *
1369 */
1371 UNUSED connection_state_t prev, connection_state_t state, void *uctx)
1372{
1373 int fd = *(talloc_get_type_abort(uctx, int));
1374
1375 /*
1376 * Two states can trigger a cleanup
1377 * Remove the watch on the one that didn't
1378 */
1379 switch (state) {
1382 break;
1383
1386 break;
1387
1388 default:
1389 fr_assert(0);
1390 break;
1391 }
1392
1394 talloc_free(uctx);
1395}
1396
1397/** Setup the connection to change states to connected or failed based on I/O events
1398 *
1399 * Will automatically cleanup after itself, in preparation for
1400 * new I/O handlers to be installed in the open() callback.
1401 *
1402 * @return
1403 * - 0 on success.
1404 * - -1 on failure.
1405 */
1407{
1408 int *fd_s;
1409
1410 /*
1411 * If connection becomes writable we
1412 * assume it's open.
1413 */
1414 if (fr_event_fd_insert(conn, NULL, conn->pub.el, fd,
1415 NULL,
1418 conn) < 0) {
1419 PERROR("Failed inserting fd (%d) into event loop %p",
1420 fd, conn->pub.el);
1422 return -1;
1423 }
1424
1425 /*
1426 * Stop the static analysis tools
1427 * complaining about assigning ints
1428 * to pointers.
1429 */
1430 MEM(fd_s = talloc_zero(conn, int));
1431 *fd_s = fd;
1432
1433 /*
1434 * Add a oneshot watcher to remove
1435 * the I/O handlers if the connection
1436 * fails, or is connected.
1437 */
1442 return 0;
1443}
1444
1445/** Tell a failed connection to move to CONNECTION_STATE_INIT
1446 *
1447 */
1452
1453/** Close a connection if it's freed
1454 *
1455 * @param[in] conn to free.
1456 * @return
1457 * - 0 connection was freed immediately.
1458 * - 1 connection free was deferred.
1459 */
1461{
1462 /*
1463 * Explicitly cancel any pending events
1464 */
1465 FR_TIMER_DELETE_RETURN(&conn->ev);
1466 /*
1467 * Don't allow the connection to be
1468 * arbitrarily freed by a callback.
1469 *
1470 * Add a deferred signal to free the
1471 * connection later.
1472 */
1473 if (DEFER_SIGNALS(conn)) {
1475 return -1;
1476 }
1477
1478 switch (conn->pub.state) {
1480 break;
1481
1482 /*
1483 * Need to close the connection first
1484 */
1489
1490 default:
1492 break;
1493 }
1494 return 0;
1495}
1496
1497/** Allocate a new connection
1498 *
1499 * After the connection has been allocated, it should be started with a call to #connection_signal_init.
1500 *
1501 * The connection state machine can detect when the connection is open in one of two ways.
1502 * - You can install a generic socket open/fail callback, using connection_signal_on_fd.
1503 * - You can call either #connection_signal_connected or connection_signal_recommend.
1504 * This allows the connection state machine to work with more difficult library APIs,
1505 * which may not return control to the caller as connections are opened.
1506 *
1507 * @param[in] ctx to allocate connection handle in. If the connection
1508 * handle is freed, and the #connection_state_t is
1509 * #CONNECTION_STATE_CONNECTING or #CONNECTION_STATE_CONNECTED the
1510 * close callback will be called.
1511 * @param[in] el to use for timer events, and to pass to the #connection_open_t callback.
1512 * @param[in] funcs callback functions.
1513 * @param[in] conf our configuration.
1514 * @param[in] log_prefix To prepend to log messages.
1515 * @param[in] uctx User context to pass to callbacks.
1516 * @return
1517 * - A new #connection_t on success.
1518 * - NULL on failure.
1519 */
1521 connection_funcs_t const *funcs,
1522 connection_conf_t const *conf,
1523 char const *log_prefix,
1524 void const *uctx)
1525{
1526 size_t i;
1527 connection_t *conn;
1528 uint64_t id;
1529
1530 fr_assert(el);
1531
1532 MEM(conn = talloc(ctx, connection_t));
1533 talloc_set_destructor(conn, _connection_free);
1534
1536
1537 *conn = (connection_t){
1538 .pub = {
1539 .id = id,
1540 .state = CONNECTION_STATE_HALTED,
1541 .el = el
1542 },
1543 .reconnection_delay = conf->reconnection_delay,
1544 .connection_timeout = conf->connection_timeout,
1545 .init = funcs->init,
1546 .open = funcs->open,
1547 .close = funcs->close,
1548 .failed = funcs->failed,
1549 .shutdown = funcs->shutdown,
1550 .is_closed = true, /* Starts closed */
1551 .trigger_args = conf->trigger_args,
1552 .trigger_cs = conf->trigger_cs,
1553 .pub.name = talloc_asprintf(conn, "%s - [%" PRIu64 "]", log_prefix, id)
1554 };
1555 memcpy(&conn->uctx, &uctx, sizeof(conn->uctx));
1556
1557 for (i = 0; i < NUM_ELEMENTS(conn->watch_pre); i++) {
1559 }
1560 for (i = 0; i < NUM_ELEMENTS(conn->watch_post); i++) {
1562 }
1564
1565 /*
1566 * Pre-allocate a on_halt watcher for deferred signal processing
1567 */
1570 connection_watch_disable(conn->on_halted); /* Start disabled */
1571
1572 return conn;
1573}
#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:211
connection_state_t(* connection_failed_t)(void *h, connection_state_t state, void *uctx)
Notification that a connection attempt has failed.
Definition connection.h:174
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:188
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:123
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:137
connection_state_t _CONST state
Current connection state.
Definition connection.h:72
connection_init_t init
Definition connection.h:194
connection_shutdown_t shutdown
Definition connection.h:196
connection_failed_t failed
Definition connection.h:197
connection_open_t open
Definition connection.h:195
uint64_t _CONST id
Unique identifier for the connection.
Definition connection.h:74
connection_close_t close
Definition connection.h:198
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:156
Holds a complete set of functions for a connection.
Definition connection.h:193
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:358
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:545
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:420
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:614
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:152
static fr_table_num_ordered_t const connection_dsignals[]
Definition connection.c:167
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:227
void connection_watch_disable(connection_watch_entry_t *entry)
Disable a watcher.
Definition connection.c:555
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:473
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:694
bool connection_watch_is_enabled(connection_watch_entry_t *entry)
Return the state of a watch entry.
Definition connection.c:591
static atomic_uint_fast64_t connection_counter
Definition connection.c:73
static size_t connection_dsignals_len
Definition connection.c:176
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:768
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:625
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:407
void connection_signals_resume(connection_t *conn)
Resume processing of deferred signals.
Definition connection.c:320
#define HANDLER_BEGIN(_conn, _func)
Called when we enter a handler.
Definition connection.c:338
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:483
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:602
static void connection_state_enter_connecting(connection_t *conn)
Enter the connecting state.
Definition connection.c:994
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:394
static void connection_deferred_signal_process(connection_t *conn)
Process any deferred signals.
Definition connection.c:238
connection_dsignal_t signal
Signal that was deferred.
Definition connection.c:183
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:578
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:644
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:157
@ CONNECTION_DSIGNAL_RECONNECT_FAILED
Reconnect a failed connection.
Definition connection.c:160
@ CONNECTION_DSIGNAL_HALT
Close a connection (ungracefully).
Definition connection.c:163
@ CONNECTION_DSIGNAL_INIT
Restart a halted connection.
Definition connection.c:158
@ CONNECTION_DSIGNAL_FREE
Free a connection (no further dsignals processed).
Definition connection.c:164
@ CONNECTION_DSIGNAL_SHUTDOWN
Close a connection (gracefully).
Definition connection.c:162
@ CONNECTION_DSIGNAL_CONNECTED
Signal that a connection is connected.
Definition connection.c:159
@ CONNECTION_DSIGNAL_RECONNECT_EXPIRED
Reconnect an expired connection (gracefully).
Definition connection.c:161
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:347
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:134
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:915
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:210
fr_dlist_t entry
Entry in the signals list.
Definition connection.c:182
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:511
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:533
static void connection_state_enter_connected(connection_t *conn)
Enter the connected state.
Definition connection.c:948
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:456
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:144
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:566
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:704
void connection_signals_pause(connection_t *conn)
Pause processing of deferred signals.
Definition connection.c:311
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:891
Holds a signal from a handler until it's safe to process it.
Definition connection.c:181
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:364