/
connection.c
1509 lines (1308 loc) · 42.5 KB
/
connection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
*
* @file src/lib/server/connection.c
* @brief Simple state machine for managing connection states.
*
* @copyright 2017-2019 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
*/
#define LOG_PREFIX "%s - [%" PRIu64 "] "
#define LOG_PREFIX_ARGS conn->pub.log_prefix, conn->pub.id
typedef struct fr_connection_s fr_connection_t;
#define _CONNECTION_PRIVATE 1
#include <freeradius-devel/server/connection.h>
#include <freeradius-devel/server/log.h>
#include <freeradius-devel/server/rad_assert.h>
#include <freeradius-devel/server/cond_eval.h>
#include <freeradius-devel/util/base.h>
#include <freeradius-devel/util/event.h>
#ifdef HAVE_STDATOMIC_H
# include <stdatomic.h>
#else
# include <freeradius-devel/util/stdatomic.h>
#endif
#include <talloc.h>
fr_table_num_ordered_t const fr_connection_states[] = {
{ "INIT", FR_CONNECTION_STATE_INIT },
{ "CONNECTING", FR_CONNECTION_STATE_CONNECTING },
{ "TIMEOUT", FR_CONNECTION_STATE_TIMEOUT },
{ "CONNECTED", FR_CONNECTION_STATE_CONNECTED },
{ "SHUTDOWN", FR_CONNECTION_STATE_SHUTDOWN },
{ "FAILED", FR_CONNECTION_STATE_FAILED },
{ "CLOSED", FR_CONNECTION_STATE_CLOSED },
{ "HALTED", FR_CONNECTION_STATE_HALTED },
};
size_t fr_connection_states_len = NUM_ELEMENTS(fr_connection_states);
static atomic_uint_fast64_t connection_counter = ATOMIC_VAR_INIT(1);
/** An entry in a watch function list
*
*/
typedef struct fr_connection_watch_entry_s {
fr_dlist_t entry; //!< List entry.
fr_connection_watch_t func; //!< Function to call when a connection enters
///< the state this list belongs to
bool oneshot; //!< Remove the function after it's called once.
bool enabled; //!< Whether the watch entry is enabled.
void *uctx; //!< User data to pass to the function.
} fr_connection_watch_entry_t;
struct fr_connection_s {
struct fr_connection_pub_s pub; //!< Public fields
void *uctx; //!< User data.
void *in_handler; //!< Connection is currently in a callback.
bool is_closed; //!< The close callback has previously been called.
bool processing_signals; //!< Processing deferred signals, don't let the deferred
///< signal processor be called multiple times.
fr_dlist_head_t watch_pre[FR_CONNECTION_STATE_MAX]; //!< Function called before state callback.
fr_dlist_head_t watch_post[FR_CONNECTION_STATE_MAX]; //!< Function called after state callback.
fr_connection_watch_entry_t *next_watcher; //!< Hack to insulate watcher iterator from deletions.
fr_connection_init_t init; //!< Callback for initialising a connection.
fr_connection_open_t open; //!< Callback for 'open' notification.
fr_connection_close_t close; //!< Callback to close a connection.
fr_connection_shutdown_t shutdown; //!< Signal the connection handle to start shutting down.
fr_connection_failed_t failed; //!< Callback for 'failed' notification.
fr_event_timer_t const *ev; //!< State transition timer.
fr_time_delta_t connection_timeout; //!< How long to wait in the
//!< #FR_CONNECTION_STATE_CONNECTING state.
fr_time_delta_t reconnection_delay; //!< How long to wait in the
//!< #FR_CONNECTION_STATE_FAILED state.
fr_dlist_head_t deferred_signals; //!< A list of signals we received whilst we were in
///< a handler.
fr_connection_watch_entry_t *on_halted; //!< Used by the deferred signal processor to learn
///< if a function deeper in the call stack freed
///< the connection.
bool signals_pause; //!< Temporarily stop processing of signals.
};
#define STATE_TRANSITION(_new) \
do { \
DEBUG2("Connection changed state %s -> %s", \
fr_table_str_by_value(fr_connection_states, conn->pub.state, "<INVALID>"), \
fr_table_str_by_value(fr_connection_states, _new, "<INVALID>")); \
conn->pub.state = _new; \
} while (0)
#define BAD_STATE_TRANSITION(_new) \
do { \
if (!fr_cond_assert_msg(0, "Connection %" PRIu64 " invalid transition %s -> %s", \
conn->pub.id, \
fr_table_str_by_value(fr_connection_states, conn->pub.state, "<INVALID>"), \
fr_table_str_by_value(fr_connection_states, _new, "<INVALID>"))) return; \
} while (0)
#define DEFER_SIGNALS(_conn) ((_conn)->in_handler || (_conn)->signals_pause)
/** Deferred signals
*
*/
typedef enum {
CONNECTION_DSIGNAL_INIT, //!< Restart a halted connection.
CONNECTION_DSIGNAL_CONNECTED, //!< Signal that a connection is connected.
CONNECTION_DSIGNAL_RECONNECT_FAILED, //!< Reconnect a failed connection.
CONNECTION_DSIGNAL_RECONNECT_EXPIRED, //!< Reconnect an expired connection (gracefully).
CONNECTION_DSIGNAL_SHUTDOWN, //!< Close a connection (gracefully).
CONNECTION_DSIGNAL_HALT, //!< Close a connection (ungracefully).
CONNECTION_DSIGNAL_FREE //!< Free a connection (no further dsignals processed).
} connection_dsignal_t;
static fr_table_num_ordered_t const connection_dsignals[] = {
{ "INIT", CONNECTION_DSIGNAL_INIT },
{ "CONNECTING", CONNECTION_DSIGNAL_CONNECTED },
{ "RECONNECT-FAILED", CONNECTION_DSIGNAL_RECONNECT_FAILED },
{ "RECONNECT-EXPIRED", CONNECTION_DSIGNAL_RECONNECT_EXPIRED },
{ "SHUTDOWN", CONNECTION_DSIGNAL_SHUTDOWN },
{ "HALT", CONNECTION_DSIGNAL_HALT },
{ "FREE", CONNECTION_DSIGNAL_FREE }
};
static size_t connection_dsignals_len = NUM_ELEMENTS(connection_dsignals);
/** Holds a signal from a handler until it's safe to process it
*
*/
typedef struct {
fr_dlist_t entry; //!< Entry in the signals list.
connection_dsignal_t signal; //!< Signal that was deferred.
} connection_dsignal_entry_t;
/*
* State transition functions
*/
static void connection_state_enter_closed(fr_connection_t *conn);
static void connection_state_enter_failed(fr_connection_t *conn);
static void connection_state_enter_timeout(fr_connection_t *conn);
static void connection_state_enter_connected(fr_connection_t *conn);
static void connection_state_enter_shutdown(fr_connection_t *conn);
static void connection_state_enter_connecting(fr_connection_t *conn);
static void connection_state_enter_halted(fr_connection_t *conn);
static void connection_state_enter_init(fr_connection_t *conn);
/** Add a deferred signal to the signal list
*
* Processing signals whilst in handlers usually leads to weird
* inconsistent states within the connection.
*
* If a public signal function is called, and detects its being called
* from within the handler, it instead adds a deferred signal entry
* and immediately returns.
*
* Once the handler is complete, and all pending C stack state changes
* are complete, the deferred signals are drained and processed.
*/
static inline void connection_deferred_signal_add(fr_connection_t *conn, connection_dsignal_t signal)
{
connection_dsignal_entry_t *dsignal;
dsignal = talloc_zero(conn, connection_dsignal_entry_t);
dsignal->signal = signal;
fr_dlist_insert_tail(&conn->deferred_signals, dsignal);
// DEBUG4("Adding deferred signal - %s", fr_table_str_by_value(connection_dsignals, signal, "<INVALID>"));
}
/** Notification function to tell connection_deferred_signal_process that the connection has been freed
*
*/
static void _deferred_signal_connection_on_halted(UNUSED fr_connection_t *conn,
UNUSED fr_connection_state_t state, void *uctx)
{
bool *freed = uctx;
*freed = true;
}
/** Process any deferred signals
*
*/
static void connection_deferred_signal_process(fr_connection_t *conn)
{
connection_dsignal_entry_t *dsignal;
bool freed = false;
/*
* We're inside and an instance of this function
* higher in the call stack. Don't do anything.
*/
if (conn->processing_signals) return;
/*
* Get notified if the connection gets freed
* out from under us...
*/
fr_connection_watch_enable_set_uctx(conn->on_halted, &freed);
conn->processing_signals = true;
while ((dsignal = fr_dlist_head(&conn->deferred_signals))) {
connection_dsignal_t signal;
fr_dlist_remove(&conn->deferred_signals, dsignal);
signal = dsignal->signal;
talloc_free(dsignal);
DEBUG4("Processing deferred signal - %s",
fr_table_str_by_value(connection_dsignals, signal, "<INVALID>"));
switch (signal) {
case CONNECTION_DSIGNAL_INIT:
fr_connection_signal_init(conn);
break;
case CONNECTION_DSIGNAL_CONNECTED:
fr_connection_signal_connected(conn);
break;
case CONNECTION_DSIGNAL_RECONNECT_FAILED: /* Reconnect - Failed */
fr_connection_signal_reconnect(conn, FR_CONNECTION_FAILED);
break;
case CONNECTION_DSIGNAL_RECONNECT_EXPIRED: /* Reconnect - Expired */
fr_connection_signal_reconnect(conn, FR_CONNECTION_EXPIRED);
break;
case CONNECTION_DSIGNAL_SHUTDOWN:
fr_connection_signal_shutdown(conn);
break;
case CONNECTION_DSIGNAL_HALT:
fr_connection_signal_halt(conn);
break;
case CONNECTION_DSIGNAL_FREE: /* Freed */
talloc_free(conn);
return;
}
/*
* One of the signal handlers freed the connection
* return immediately.
*/
if (freed) return;
}
conn->processing_signals = false;
fr_connection_watch_disable(conn->on_halted);
}
/** Pause processing of deferred signals
*
* @param[in] conn to pause signal processing for.
*/
void fr_connection_signals_pause(fr_connection_t *conn)
{
conn->signals_pause = true;
}
/** Resume processing of deferred signals
*
* @param[in] conn to resume signal processing for.
*/
void fr_connection_deferred_signals_resume(fr_connection_t *conn)
{
/*
* If we were paused, and we're not in a handler
* then process the signals now.
*/
if (!conn->in_handler && conn->signals_pause) {
conn->signals_pause = false;
connection_deferred_signal_process(conn);
return;
}
conn->signals_pause = false;
}
/** Called when we enter a handler
*
*/
#define HANDLER_BEGIN(_conn, _func) \
void *_prev_handler = (_conn)->in_handler; \
do { \
(_conn)->in_handler = (void *)(_func); \
} while (0)
/** Called when we exit a handler
*
*/
#define HANDLER_END(_conn) \
do { \
(_conn)->in_handler = _prev_handler; \
if (!(_conn)->signals_pause && (!(_conn)->in_handler)) connection_deferred_signal_process(_conn); \
} while(0)
/** Call a list of watch functions associated with a state
*
*/
static inline void connection_watch_call(fr_connection_t *conn, fr_dlist_head_t *list)
{
/*
* Nested watcher calls are not allowed
* and shouldn't be possible because of
* deferred signal processing.
*/
rad_assert(conn->next_watcher == NULL);
while ((conn->next_watcher = fr_dlist_next(list, conn->next_watcher))) {
fr_connection_watch_entry_t *entry = conn->next_watcher;
bool oneshot = entry->oneshot; /* Watcher could be freed, so store now */
if (!entry->enabled) continue;
if (oneshot) conn->next_watcher = fr_dlist_remove(list, entry);
/*
DEBUG4("Notifying %swatcher - (%p)(conn=%p, state=%s, uctx=%p)",
entry->oneshot ? "oneshot " : "",
entry->func,
conn,
fr_table_str_by_value(fr_connection_states, conn->pub.state, "<INVALID>"),
entry->uctx);
*/
entry->func(conn, conn->pub.state, entry->uctx);
if (oneshot) talloc_free(entry);
}
conn->next_watcher = NULL;
}
/** Call the pre handler watch functions
*
*/
#define WATCH_PRE(_conn) \
do { \
if (fr_dlist_empty(&(_conn)->watch_pre[(_conn)->pub.state])) break; \
HANDLER_BEGIN(conn, &(_conn)->watch_pre[(_conn)->pub.state]); \
connection_watch_call((_conn), &(_conn)->watch_pre[(_conn)->pub.state]); \
HANDLER_END(conn); \
} while(0)
/** Call the post handler watch functions
*
*/
#define WATCH_POST(_conn) \
do { \
if (fr_dlist_empty(&(_conn)->watch_post[(_conn)->pub.state])) break; \
HANDLER_BEGIN(conn, &(_conn)->watch_post[(_conn)->pub.state]); \
connection_watch_call((_conn), &(_conn)->watch_post[(_conn)->pub.state]); \
HANDLER_END(conn); \
} while(0)
/** Remove a watch function from a pre/post[state] list
*
*/
static int connection_del_watch(fr_connection_t *conn, fr_dlist_head_t *state_lists,
fr_connection_state_t state, fr_connection_watch_t watch)
{
fr_connection_watch_entry_t *entry = NULL;
fr_dlist_head_t *list = &state_lists[state];
while ((entry = fr_dlist_next(list, entry))) {
if (entry->func == watch) {
/*
DEBUG4("Removing %s watcher %p",
fr_table_str_by_value(fr_connection_states, state, "<INVALID>"),
watch);
*/
if (conn->next_watcher == entry) {
conn->next_watcher = fr_dlist_remove(list, entry);
} else {
fr_dlist_remove(list, entry);
}
talloc_free(entry);
return 0;
}
}
return -1;
}
/** Remove a watch function from a pre list
*
* @param[in] conn The connection to remove the watcher from.
* @param[in] state to remove the watch from.
* @param[in] watch Function to remove.
* @return
* - 0 if the function was removed successfully.
* - -1 if the function wasn't present in the watch list.
* - -2 an invalid state was passed.
*/
int fr_connection_del_watch_pre(fr_connection_t *conn, fr_connection_state_t state, fr_connection_watch_t watch)
{
if (state >= FR_CONNECTION_STATE_MAX) return -2;
return connection_del_watch(conn, conn->watch_pre, state, watch);
}
/** Remove a watch function from a post list
*
* @param[in] conn The connection to remove the watcher from.
* @param[in] state to remove the watch from.
* @param[in] watch Function to remove.
* @return
* - 0 if the function was removed successfully.
* - -1 if the function wasn't present in the watch list.
* - -2 an invalid state was passed.
*/
int fr_connection_del_watch_post(fr_connection_t *conn, fr_connection_state_t state, fr_connection_watch_t watch)
{
if (state >= FR_CONNECTION_STATE_MAX) return -2;
return connection_del_watch(conn, conn->watch_post, state, watch);
}
/** Add a watch entry to the pre/post[state] list
*
*/
static fr_connection_watch_entry_t *connection_add_watch(fr_connection_t *conn, fr_dlist_head_t *list,
fr_connection_watch_t watch, bool oneshot, void const *uctx)
{
fr_connection_watch_entry_t *entry;
MEM(entry = talloc_zero(conn, fr_connection_watch_entry_t));
entry->func = watch;
entry->oneshot = oneshot;
entry->enabled = true;
memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
fr_dlist_insert_tail(list, entry);
return entry;
}
/** Add a callback to be executed before a state function has been called
*
* @param[in] conn to add watcher to.
* @param[in] state to call watcher on entering.
* @param[in] watch function to call.
* @param[in] oneshot If true, remove the function after calling.
* @param[in] uctx to pass to callbacks.
* @return
* - NULL if state value is invalid.
* - A new watch entry handle.
*/
fr_connection_watch_entry_t *fr_connection_add_watch_pre(fr_connection_t *conn, fr_connection_state_t state,
fr_connection_watch_t watch, bool oneshot, void const *uctx)
{
if (state >= FR_CONNECTION_STATE_MAX) return NULL;
return connection_add_watch(conn, &conn->watch_pre[state], watch, oneshot, uctx);
}
/** Add a callback to be executed after a state function has been called
*
* Where a user callback is executed on state change, the post function
* is only called if the callback succeeds.
*
* @param[in] conn to add watcher to.
* @param[in] state to call watcher on entering.
* @param[in] watch function to call.
* @param[in] oneshot If true, remove the function after calling.
* @param[in] uctx to pass to callbacks.
* @return
* - NULL if state value is invalid.
* - A new watch entry handle.
*/
fr_connection_watch_entry_t *fr_connection_add_watch_post(fr_connection_t *conn, fr_connection_state_t state,
fr_connection_watch_t watch, bool oneshot, void const *uctx)
{
if (state >= FR_CONNECTION_STATE_MAX) return NULL;
return connection_add_watch(conn, &conn->watch_post[state], watch, oneshot, uctx);
}
/** Enable a watcher
*
* @param[in] entry to enabled.
*/
void fr_connection_watch_enable(fr_connection_watch_entry_t *entry)
{
(void)talloc_get_type_abort(entry, fr_connection_watch_entry_t);
entry->enabled = true;
}
/** Disable a watcher
*
* @param[in] entry to disable.
*/
void fr_connection_watch_disable(fr_connection_watch_entry_t *entry)
{
(void)talloc_get_type_abort(entry, fr_connection_watch_entry_t);
entry->enabled = false;
}
/** Enable a watcher and replace the uctx
*
* @param[in] entry to enabled.
*/
void fr_connection_watch_enable_set_uctx(fr_connection_watch_entry_t *entry, void const *uctx)
{
(void)talloc_get_type_abort(entry, fr_connection_watch_entry_t);
entry->enabled = true;
memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
}
/** Change the uctx of an entry
*
* @param[in] entry to enabled.
*/
void fr_connection_watch_set_uctx(fr_connection_watch_entry_t *entry, void const *uctx)
{
(void)talloc_get_type_abort(entry, fr_connection_watch_entry_t);
memcpy(&entry->uctx, &uctx, sizeof(entry->uctx));
}
/** Return the state of a watch entry
*
* @param[in] entry to return state of.
* @return
* - true if enabled.
* - false if disabled.
*/
bool fr_connection_watch_is_enabled(fr_connection_watch_entry_t *entry)
{
(void)talloc_get_type_abort(entry, fr_connection_watch_entry_t);
return entry->enabled;
}
/** Return the number of times we've attempted to establish or re-establish this connection
*
* @param[in] conn to get count from.
* @return the number of times the connection has reconnected.
*/
uint64_t fr_connection_get_num_reconnected(fr_connection_t const *conn)
{
if (conn->pub.reconnected == 0) return 0; /* Has never been initialised */
return conn->pub.reconnected - 1; /* We don't count the first connection attempt */
}
/** Return the number of times this connection has timed out whilst connecting
*
* @param[in] conn to get count from.
* @return the number of times the connection has timed out whilst connecting.
*/
uint64_t fr_connection_get_num_timed_out(fr_connection_t const *conn)
{
return conn->pub.timed_out;
}
/** The requisite period of time has passed, try and re-open the connection
*
* @param[in] el the time event ocurred on.
* @param[in] now The current time.
* @param[in] uctx The #fr_connection_t the fd is associated with.
*/
static void _reconnect_delay_done(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
{
fr_connection_t *conn = talloc_get_type_abort(uctx, fr_connection_t);
switch (conn->pub.state) {
case FR_CONNECTION_STATE_FAILED:
case FR_CONNECTION_STATE_CLOSED:
connection_state_enter_init(conn);
break;
default:
BAD_STATE_TRANSITION(FR_CONNECTION_STATE_INIT);
break;
}
}
/** Close the connection, then wait for another state change
*
*/
static void connection_state_enter_closed(fr_connection_t *conn)
{
switch (conn->pub.state) {
case FR_CONNECTION_STATE_CONNECTING:
case FR_CONNECTION_STATE_CONNECTED:
case FR_CONNECTION_STATE_FAILED:
break;
default:
BAD_STATE_TRANSITION(FR_CONNECTION_STATE_CLOSED);
return;
}
STATE_TRANSITION(FR_CONNECTION_STATE_CLOSED);
fr_event_timer_delete(&conn->ev);
/*
* If there's a close callback, call it, so that the
* API client can free any resources associated
* with the connection handle.
*/
WATCH_PRE(conn);
/*
* is_closed is for pure paranoia. If everything
* is working correctly this state should never
* be entered if the connection is closed.
*/
rad_assert(!conn->is_closed);
if (conn->close && !conn->is_closed) {
HANDLER_BEGIN(conn, conn->close);
DEBUG4("Calling close(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
conn->close(conn->pub.el, conn->pub.h, conn->uctx);
conn->is_closed = true; /* Ensure close doesn't get called twice if the connection is freed */
HANDLER_END(conn);
} else {
conn->is_closed = true;
}
WATCH_POST(conn);
}
/** Connection timeout
*
* Connection wasn't opened within the configured period of time
*
* @param[in] el the time event ocurred on.
* @param[in] now The current time.
* @param[in] uctx The #fr_connection_t the fd is associated with.
*/
static void _connection_timeout(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
{
fr_connection_t *conn = talloc_get_type_abort(uctx, fr_connection_t);
connection_state_enter_timeout(conn);
}
/** Gracefully shutdown the handle
*
*/
static void connection_state_enter_shutdown(fr_connection_t *conn)
{
fr_connection_state_t ret;
switch (conn->pub.state) {
case FR_CONNECTION_STATE_CONNECTED:
break;
default:
BAD_STATE_TRANSITION(FR_CONNECTION_STATE_SHUTDOWN);
return;
}
STATE_TRANSITION(FR_CONNECTION_STATE_SHUTDOWN);
WATCH_PRE(conn);
{
HANDLER_BEGIN(conn, conn->shutdown);
DEBUG4("Calling shutdown(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
ret = conn->shutdown(conn->pub.el, conn->pub.h, conn->uctx);
HANDLER_END(conn);
}
switch (ret) {
case FR_CONNECTION_STATE_SHUTDOWN:
break;
default:
connection_state_enter_failed(conn);
return;
}
WATCH_POST(conn);
/*
* If there's a connection timeout,
* set, then add the timer.
*
* The connection may be bad, in which
* case we want to automatically fail
* if it doesn't shutdown within the
* timeout period.
*/
if (conn->connection_timeout) {
if (fr_event_timer_in(conn, conn->pub.el, &conn->ev,
conn->connection_timeout, _connection_timeout, conn) < 0) {
/*
* Can happen when the event loop is exiting
*/
PERROR("Failed setting connection_timeout timer, closing connection");
connection_state_enter_closed(conn);
}
}
}
/** Connection failed
*
* Transition to the FR_CONNECTION_STATE_FAILED state.
*
* If the connection was open, or couldn't be opened wait for reconnection_delay before transitioning
* back to init.
*
* If no reconnection_delay was set, transition to halted.
*
* @param[in] conn that failed.
*/
static void connection_state_enter_failed(fr_connection_t *conn)
{
fr_connection_state_t prev;
fr_connection_state_t ret = FR_CONNECTION_STATE_INIT;
rad_assert(conn->pub.state != FR_CONNECTION_STATE_FAILED);
/*
* Explicit error occurred, delete the connection timer
*/
fr_event_timer_delete(&conn->ev);
/*
* Record what state the connection is currently in
* so we can figure out what to do next.
*/
prev = conn->pub.state;
/*
* Now transition to failed
*/
STATE_TRANSITION(FR_CONNECTION_STATE_FAILED);
/*
* If there's a failed callback, give it the
* opportunity to suspend/destroy the
* connection.
*/
WATCH_PRE(conn);
if (conn->failed) {
HANDLER_BEGIN(conn, conn->failed);
DEBUG4("Calling failed(h=%p, state=%s, uctx=%p)", conn->pub.h,
fr_table_str_by_value(fr_connection_states, prev, "<INVALID>"), conn->uctx);
ret = conn->failed(conn->pub.h, prev, conn->uctx);
HANDLER_END(conn);
}
WATCH_POST(conn);
/*
* Enter the closed state if we failed during
* connecting, or when we were connected.
*/
switch (prev) {
case FR_CONNECTION_STATE_CONNECTED:
case FR_CONNECTION_STATE_CONNECTING:
case FR_CONNECTION_STATE_TIMEOUT: /* Timeout means the connection progress past init */
case FR_CONNECTION_STATE_SHUTDOWN: /* Shutdown means the connection failed whilst shutting down */
connection_state_enter_closed(conn);
break;
default:
break;
}
if (conn->failed) {
switch (ret) {
/*
* The callback signalled it wants the
* connection to be reinitialised
* after reconnection_delay, or
* immediately if the failure was due
* to a connection timeout.
*/
case FR_CONNECTION_STATE_INIT:
break;
/*
* The callback signalled it wants the
* connection to stop.
*/
case FR_CONNECTION_STATE_HALTED:
default:
connection_state_enter_halted(conn);
return;
}
}
/*
* What previous state we were in
* determines if we need to apply the
* reconnect timeout.
*/
switch (prev) {
case FR_CONNECTION_STATE_INIT: /* Failed during initialisation */
case FR_CONNECTION_STATE_CONNECTED: /* Failed after connecting */
case FR_CONNECTION_STATE_CONNECTING: /* Failed during connecting */
case FR_CONNECTION_STATE_SHUTDOWN: /* Failed during shutdown */
if (conn->reconnection_delay) {
DEBUG2("Delaying reconnection by %pVs", fr_box_time_delta(conn->reconnection_delay));
if (fr_event_timer_in(conn, conn->pub.el, &conn->ev,
conn->reconnection_delay, _reconnect_delay_done, conn) < 0) {
/*
* Can happen when the event loop is exiting
*/
PERROR("Failed inserting reconnection_delay timer event, halting connection");
connection_state_enter_halted(conn);
}
return;
}
/*
* If there's no reconnection
* delay, then don't automatically
* reconnect, and wait to be
* signalled.
*/
connection_state_enter_halted(conn);
break;
case FR_CONNECTION_STATE_TIMEOUT: /* Failed during connecting due to timeout */
connection_state_enter_init(conn);
break;
default:
rad_assert(0);
}
}
/** Enter the timeout state
*
* The connection took took long to open. Timeout the attempt and transition
* to the failed state.
*/
static void connection_state_enter_timeout(fr_connection_t *conn)
{
switch (conn->pub.state) {
case FR_CONNECTION_STATE_CONNECTING:
case FR_CONNECTION_STATE_SHUTDOWN:
break;
default:
BAD_STATE_TRANSITION(FR_CONNECTION_STATE_TIMEOUT);
}
ERROR("Connection failed - timed out after %pVs", fr_box_time_delta(conn->connection_timeout));
STATE_TRANSITION(FR_CONNECTION_STATE_TIMEOUT);
conn->pub.timed_out++;
connection_state_enter_failed(conn);
}
/** Enter the halted state
*
* Here we wait, until signalled by fr_connection_signal_reconnect.
*/
static void connection_state_enter_halted(fr_connection_t *conn)
{
rad_assert(conn->is_closed);
switch (conn->pub.state) {
case FR_CONNECTION_STATE_FAILED: /* Init failure */
case FR_CONNECTION_STATE_CLOSED:
break;
default:
BAD_STATE_TRANSITION(FR_CONNECTION_STATE_HALTED);
}
fr_event_timer_delete(&conn->ev);
STATE_TRANSITION(FR_CONNECTION_STATE_HALTED);
WATCH_PRE(conn);
WATCH_POST(conn);
}
/** Enter the connected state
*
* The connection is now fully connected. At this point we call the open callback
* so that the API client can install its normal set of I/O callbacks to deal with
* sending/receiving actual data.
*
* After this, the connection will only transition states if an API client
* explicitly calls fr_connection_signal_reconnect.
*
* The connection API cannot monitor the connection for failure conditions.
*
* @param[in] conn Entering the connecting state.
*/
static void connection_state_enter_connected(fr_connection_t *conn)
{
int ret;
rad_assert(conn->pub.state == FR_CONNECTION_STATE_CONNECTING);
STATE_TRANSITION(FR_CONNECTION_STATE_CONNECTED);
fr_event_timer_delete(&conn->ev);
WATCH_PRE(conn);
if (conn->open) {
HANDLER_BEGIN(conn, conn->open);
DEBUG4("Calling open(el=%p, h=%p, uctx=%p)", conn->pub.el, conn->pub.h, conn->uctx);
ret = conn->open(conn->pub.el, conn->pub.h, conn->uctx);
HANDLER_END(conn);
} else {
ret = FR_CONNECTION_STATE_CONNECTED;
}
switch (ret) {
/*
* Callback agrees everything is connected
*/
case FR_CONNECTION_STATE_CONNECTED:
DEBUG2("Connection established");
WATCH_POST(conn); /* Only call if we successfully connected */
return;
/*
* Open callback failed
*/
case FR_CONNECTION_STATE_FAILED:
default:
PERROR("Connection failed");
connection_state_enter_failed(conn);
return;
}
}
/** Enter the connecting state
*
* After this function returns we wait to be signalled with fr_connection_singal_connected
* or for the connection timer to expire.
*
* @param[in] conn Entering the connecting state.
*/
static void connection_state_enter_connecting(fr_connection_t *conn)
{
switch (conn->pub.state) {
case FR_CONNECTION_STATE_INIT:
break;
default:
BAD_STATE_TRANSITION(FR_CONNECTION_STATE_CONNECTING);
return;
}
STATE_TRANSITION(FR_CONNECTION_STATE_CONNECTING);
WATCH_PRE(conn);
WATCH_POST(conn);
/*
* If there's a connection timeout,
* set, then add the timer.
*/
if (conn->connection_timeout) {
if (fr_event_timer_in(conn, conn->pub.el, &conn->ev,
conn->connection_timeout, _connection_timeout, conn) < 0) {
PERROR("Failed setting connection_timeout event, failing connection");
/*
* This can happen when the event loop
* is exiting.
*
* Entering fail will close partially
* open connection and then, if we still
* can't insert a timer, then the connection
* will be halted and sit idle until its
* freed.
*/
connection_state_enter_failed(conn);
}
}
}
/** Initial state of the connection
*
* Calls the init function we were passed to allocate a library specific handle or
* file descriptor.
*
* @param[in] conn To initialise.