Line data Source code
1 : #ifndef HEADER_fd_src_discof_failover_fd_failover_channel_h 2 : #define HEADER_fd_src_discof_failover_fd_failover_channel_h 3 : 4 : /* Authenticated nonblocking TCP channel for the failover pair. */ 5 : 6 : #include "fd_failover_wire.h" 7 : #include "../../waltz/tls/fd_tls.h" 8 : 9 : #include <time.h> 10 : 11 336 : #define FD_FAILOVER_CHANNEL_MAGIC (0xF17EDA2CE5FA170AUL) 12 : 13 : /* Candidate and TLS session slots */ 14 888546 : #define FD_FAILOVER_CHANNEL_CANDIDATE_MAX (1024UL) 15 876 : #define FD_FAILOVER_CHANNEL_SESSION_MAX (4UL) /* OUTBOUND_IDX, ADMITTED_IDX, two spares */ 16 : 17 1401 : #define FD_FAILOVER_NANOS_PER_SECOND (1000000000UL) 18 3 : #define FD_FAILOVER_LOG_INTERVAL_NANOS (60L*(long)FD_FAILOVER_NANOS_PER_SECOND) 19 : 20 : /* Rate limits */ 21 168 : #define FD_FAILOVER_ACCEPT_RATE (8192UL) /* number of num accept4 calls */ 22 168 : #define FD_FAILOVER_ACCEPT_BURST (128UL) 23 168 : #define FD_FAILOVER_VERIFY_RATE (2048UL) /* number of Ed25519 proofs */ 24 168 : #define FD_FAILOVER_VERIFY_BURST (128UL) 25 126 : #define FD_FAILOVER_ACCEPT_PER_POLL (16UL) 26 33732 : #define FD_FAILOVER_SERVICE_PER_POLL (4UL) 27 12 : #define FD_FAILOVER_LISTEN_BACKLOG (256) 28 : 29 42 : #define FD_FAILOVER_ADMISSION_START_NANOS ((long)FD_FAILOVER_NANOS_PER_SECOND) 30 3 : #define FD_FAILOVER_ADMISSION_PROOF_NANOS (2L*(long)FD_FAILOVER_NANOS_PER_SECOND) 31 : 32 : struct fd_failover_proof_signer { 33 : void * ctx; 34 : void (* sign_fn)( void * ctx, uchar sig[ 64 ], uchar const msg[ FD_KEYGUARD_FAILOVER_PROOF_SZ ] ); 35 : }; 36 : typedef struct fd_failover_proof_signer fd_failover_proof_signer_t; 37 : 38 : /* Monotonic clock in nanoseconds. */ 39 : static inline long 40 630 : fd_failover_clock( void ) { 41 630 : struct timespec ts; 42 630 : clock_gettime( CLOCK_MONOTONIC, &ts ); 43 630 : return (long)ts.tv_sec*(long)FD_FAILOVER_NANOS_PER_SECOND + (long)ts.tv_nsec; 44 630 : } 45 : 46 : /* Handshake, operation and retry limits in nanoseconds. */ 47 390 : #define FD_FAILOVER_CHANNEL_IDLE_NANOS ( 64000000000L) 48 231 : #define FD_FAILOVER_CHANNEL_BACKOFF_MIN_NANOS ( 800000000L) 49 3 : #define FD_FAILOVER_CHANNEL_BACKOFF_MAX_NANOS ( 12800000000L) 50 27 : #define FD_FAILOVER_CHANNEL_HELLO_TIMEOUT_NANOS ( 4000000000L) 51 135 : #define FD_FAILOVER_CHANNEL_KEEPALIVE_NANOS (FD_FAILOVER_CHANNEL_IDLE_NANOS/4L) 52 : 53 : struct fd_failover_channel; 54 : typedef struct fd_failover_channel fd_failover_channel_t; 55 : 56 : FD_PROTOTYPES_BEGIN 57 : 58 : FD_FN_CONST ulong 59 : fd_failover_channel_align( void ); 60 : 61 : FD_FN_CONST ulong 62 : fd_failover_channel_footprint( void ); 63 : 64 : void * 65 : fd_failover_channel_new( void * shmem ); 66 : 67 : fd_failover_channel_t * 68 : fd_failover_channel_join( void * shch ); 69 : 70 : /* Opens the listener on address:port. */ 71 : void 72 : fd_failover_channel_init_listener( fd_failover_channel_t * channel, 73 : uint address, 74 : ushort port ); 75 : 76 : /* Starts dialing and retrying at address:port. */ 77 : void 78 : fd_failover_channel_init_dialer( fd_failover_channel_t * channel, 79 : uint address, 80 : ushort port ); 81 : 82 : /* Configures admission and mutual TLS, both using the staked_pubkey, 83 : with separate proof and TLS signers. */ 84 : int 85 : fd_failover_channel_set_identity( fd_failover_channel_t * channel, 86 : fd_tls_sign_t signer, 87 : fd_failover_proof_signer_t proof_signer, 88 : fd_failover_hello_t const * hello ); 89 : 90 : /* Closes all sockets and releases the TLS context. */ 91 : void 92 : fd_failover_channel_fini( fd_failover_channel_t * channel ); 93 : 94 : /* Updates who holds the identity. */ 95 : void 96 : fd_failover_channel_set_holds( fd_failover_channel_t * channel, 97 : int holds ); 98 : 99 : /* Drives the channel, now is the caller's clock in nanos. Sets 100 : *charge_busy when it made progress. Returns 1 with *out_type, 101 : out_payload and *out_payload_sz filled when a verified post HELLO frame 102 : arrived, else 0. out_payload must hold FD_FAILOVER_PAYLOAD_MAX bytes. */ 103 : int 104 : fd_failover_channel_poll( fd_failover_channel_t * channel, 105 : long now, 106 : int * charge_busy, 107 : ushort * out_type, 108 : uchar * out_payload, 109 : ulong * out_payload_sz ); 110 : 111 : /* Writes one authenticated frame, now is the caller's clock in nanos. 112 : Returns 0 on success, -1 when the channel is not paired, already has a 113 : pending frame, or encoding or writing fails. Encoding and write 114 : failures tear down the session. */ 115 : int 116 : fd_failover_channel_send( fd_failover_channel_t * channel, 117 : long now, 118 : ushort type, 119 : uchar const * payload, 120 : ulong payload_sz ); 121 : 122 : /* Accessors. */ 123 : FD_FN_PURE ulong fd_failover_channel_state ( fd_failover_channel_t const * channel ); 124 : FD_FN_PURE int fd_failover_channel_listen_fd ( fd_failover_channel_t const * channel ); 125 : FD_FN_PURE int fd_failover_channel_tx_pending( fd_failover_channel_t const * channel ); 126 : FD_FN_PURE fd_failover_hello_t const * fd_failover_channel_peer_hello( fd_failover_channel_t const * channel ); 127 : 128 : /* Changes whenever a new authenticated session replaces the previous one. */ 129 : FD_FN_PURE ulong fd_failover_channel_generation( fd_failover_channel_t const * channel ); 130 : 131 : /* When the paired peer last sent a frame. Zero without a paired session. */ 132 : FD_FN_PURE long fd_failover_channel_peer_heard_at( fd_failover_channel_t const * channel ); 133 : 134 : /* Gets the bound port from the listen socket. */ 135 : ushort 136 : fd_failover_channel_listen_port( fd_failover_channel_t const * channel ); 137 : 138 : /* Closes the connection. */ 139 : void 140 : fd_failover_channel_hangup( fd_failover_channel_t * channel, 141 : long now ); 142 : 143 : /* Drops the session after an error. */ 144 : void 145 : fd_failover_channel_protocol_error( fd_failover_channel_t * channel, 146 : long now ); 147 : 148 : FD_PROTOTYPES_END 149 : 150 : #endif /* HEADER_fd_src_discof_failover_fd_failover_channel_h */