ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
handoff.c File Reference

Go to the source code of this file.

Functions

asciichat_error_t consensus_handoff_decode (const void *data, size_t length, consensus_handoff_message_t *m)
void consensus_handoff_encode (const consensus_handoff_message_t *m, uint8_t out[CONSENSUS_HANDOFF_HEADER])
void consensus_handoff_init (consensus_handoff_t *s, const uint8_t session[SESSION_ID_SIZE], const uint8_t host[SESSION_ID_SIZE])
asciichat_error_t consensus_handoff_join (consensus_handoff_t *s, const uint8_t id[SESSION_ID_SIZE])
void consensus_handoff_leave (consensus_handoff_t *s, const uint8_t id[SESSION_ID_SIZE])
void consensus_handoff_host_lost (consensus_handoff_t *s, const uint8_t host[SESSION_ID_SIZE])
 Record loss of the incumbent, ignoring delayed reports about old hosts. The caller authenticates the reporter before the next tick starts recovery.
asciichat_error_t consensus_handoff_receive (consensus_handoff_t *s, const consensus_handoff_message_t *m, size_t length, uint64_t now)
 Accept a member's probe or preparation reply using receiver-clock time. Probe metadata becomes eligible only with a matched, timely body. Preparation replies must match the current generation, round, and selected successor.
bool consensus_handoff_tick (consensus_handoff_t *s, uint64_t now, consensus_handoff_message_t *a)
 Advance measured successor selection and bounded host-loss recovery. Fresh eligible peers prepare within five seconds; all recovery attempts share one 15-second budget. Only a fresh prepared peer can commit, and failure is terminal.

Function Documentation

◆ consensus_handoff_decode()

asciichat_error_t consensus_handoff_decode ( const void * data,
size_t length,
consensus_handoff_message_t * m )

Definition at line 16 of file handoff.c.

16 {
17 if (!data || !m || length < CONSENSUS_HANDOFF_HEADER)
18 return SET_ERRNO(ERROR_INVALID_PARAM, "Truncated handoff frame");
19 const uint8_t *p = data;
22 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Invalid handoff version, kind or length");
23 memset(m, 0, sizeof(*m));
24 m->kind = p[1];
25 memcpy(m->session, p + 4, SESSION_ID_SIZE);
26 memcpy(m->participant, p + 20, SESSION_ID_SIZE);
27 m->generation = read32(p + 36);
28 m->sequence = read32(p + 40);
29 m->upload_kbps = read32(p + 44);
30 m->rtt_ms = read32(p + 48);
31 m->capabilities = p[52];
32 m->nat_tier = p[53];
33 m->port = (uint16_t)((p[54] << 8) | p[55]);
34 return ASCIICHAT_OK;
35}
#define SET_ERRNO(code,...)
unsigned short uint16_t
unsigned char uint8_t
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define SESSION_ID_SIZE
#define CONSENSUS_HANDOFF_HEADER
Definition handoff.h:15
#define CONSENSUS_HANDOFF_PROBE_BYTES
Definition handoff.h:16
#define CONSENSUS_HANDOFF_VERSION
Definition handoff.h:14
@ HANDOFF_SAMPLE_DATA
Definition handoff.h:23
@ HANDOFF_SAMPLE_ACK
Definition handoff.h:30
@ HANDOFF_SAMPLE_START
Definition handoff.h:22
uint8_t session[SESSION_ID_SIZE]
Definition handoff.h:35
uint8_t participant[SESSION_ID_SIZE]
Definition handoff.h:36

References ASCIICHAT_OK, consensus_handoff_message_t::capabilities, CONSENSUS_HANDOFF_HEADER, CONSENSUS_HANDOFF_PROBE_BYTES, CONSENSUS_HANDOFF_VERSION, ERROR_INVALID_PARAM, ERROR_NETWORK_PROTOCOL, consensus_handoff_message_t::generation, HANDOFF_SAMPLE_ACK, HANDOFF_SAMPLE_DATA, HANDOFF_SAMPLE_START, consensus_handoff_message_t::kind, consensus_handoff_message_t::nat_tier, consensus_handoff_message_t::participant, consensus_handoff_message_t::port, consensus_handoff_message_t::rtt_ms, consensus_handoff_message_t::sequence, consensus_handoff_message_t::session, SESSION_ID_SIZE, SET_ERRNO, and consensus_handoff_message_t::upload_kbps.

◆ consensus_handoff_encode()

◆ consensus_handoff_host_lost()

void consensus_handoff_host_lost ( consensus_handoff_t * s,
const uint8_t host[SESSION_ID_SIZE] )

Record loss of the incumbent, ignoring delayed reports about old hosts. The caller authenticates the reporter before the next tick starts recovery.

Definition at line 103 of file handoff.c.

103 {
104 if (!memcmp(s->host, host, sizeof(s->host)))
105 s->host_lost = true;
106}
uint8_t host[SESSION_ID_SIZE]
Definition handoff.h:74

References consensus_handoff_t::host, consensus_handoff_t::host_lost, and SESSION_ID_SIZE.

◆ consensus_handoff_init()

void consensus_handoff_init ( consensus_handoff_t * s,
const uint8_t session[SESSION_ID_SIZE],
const uint8_t host[SESSION_ID_SIZE] )

Definition at line 51 of file handoff.c.

51 {
52 memset(s, 0, sizeof(*s));
53 memcpy(s->session, session, SESSION_ID_SIZE);
54 memcpy(s->host, host, SESSION_ID_SIZE);
55 s->generation = 1;
56}
uint8_t session[SESSION_ID_SIZE]
Definition handoff.h:73
uint32_t generation
Definition handoff.h:76

References consensus_handoff_t::generation, consensus_handoff_t::host, consensus_handoff_t::session, and SESSION_ID_SIZE.

◆ consensus_handoff_join()

asciichat_error_t consensus_handoff_join ( consensus_handoff_t * s,
const uint8_t id[SESSION_ID_SIZE] )

Definition at line 63 of file handoff.c.

63 {
64 if (find_peer(s, id))
65 return ASCIICHAT_OK;
67 return SET_ERRNO(ERROR_INVALID_STATE, "Handoff membership full");
69 memset(p, 0, sizeof(*p));
70 memcpy(p->id, id, SESSION_ID_SIZE);
71 p->nat_tier = 255;
72 ++s->generation;
73 s->ready = false;
74 memset(s->successor, 0, SESSION_ID_SIZE);
75 return ASCIICHAT_OK;
76}
@ ERROR_INVALID_STATE
#define MAX_SESSION_PARTICIPANTS
consensus handoff message is intentionally a small, concrete record. Fields such as kind,...
Definition handoff.h:53
uint8_t id[SESSION_ID_SIZE]
Definition handoff.h:54
uint8_t successor[SESSION_ID_SIZE]
Definition handoff.h:75
consensus_handoff_peer_t peers[MAX_SESSION_PARTICIPANTS]
Definition handoff.h:86

References ASCIICHAT_OK, consensus_handoff_t::count, ERROR_INVALID_STATE, consensus_handoff_t::generation, consensus_handoff_peer_t::id, MAX_SESSION_PARTICIPANTS, consensus_handoff_peer_t::nat_tier, consensus_handoff_t::peers, consensus_handoff_t::ready, SESSION_ID_SIZE, SET_ERRNO, and consensus_handoff_t::successor.

◆ consensus_handoff_leave()

void consensus_handoff_leave ( consensus_handoff_t * s,
const uint8_t id[SESSION_ID_SIZE] )

Definition at line 77 of file handoff.c.

77 {
78 // The incumbent can leave before its first measurement creates a peer entry.
79 if (memcmp(s->host, id, SESSION_ID_SIZE) == 0)
80 s->host_lost = true;
81 for (size_t i = 0; i < s->count; ++i) {
82 if (memcmp(s->peers[i].id, id, SESSION_ID_SIZE) != 0)
83 continue;
84 memmove(&s->peers[i], &s->peers[i + 1], (s->count - i - 1) * sizeof(s->peers[0]));
85 --s->count;
86 ++s->generation;
87 if (memcmp(s->host, id, SESSION_ID_SIZE) == 0)
88 s->host_lost = true;
89 // Pending acknowledgements carry the old membership generation. Reissue
90 // their preparation instead of rejecting the late reply and exhausting
91 // recovery without trying the still-eligible successor again.
92 if (!s->ready || memcmp(s->successor, id, SESSION_ID_SIZE) == 0) {
93 memset(s->successor, 0, SESSION_ID_SIZE);
94 s->ready = false;
95 }
96 return;
97 }
98}

References consensus_handoff_t::count, consensus_handoff_t::generation, consensus_handoff_t::host, consensus_handoff_t::host_lost, consensus_handoff_peer_t::id, consensus_handoff_t::peers, consensus_handoff_t::ready, SESSION_ID_SIZE, and consensus_handoff_t::successor.

◆ consensus_handoff_receive()

asciichat_error_t consensus_handoff_receive ( consensus_handoff_t * s,
const consensus_handoff_message_t * m,
size_t length,
uint64_t now )

Accept a member's probe or preparation reply using receiver-clock time. Probe metadata becomes eligible only with a matched, timely body. Preparation replies must match the current generation, round, and selected successor.

Returns
ASCIICHAT_OK if accepted, or a protocol error without accepting the frame.

Definition at line 114 of file handoff.c.

114 {
115 if (memcmp(s->session, m->session, SESSION_ID_SIZE) != 0)
116 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Wrong handoff session");
117 consensus_handoff_peer_t *p = find_peer(s, m->participant);
118 if (!p)
119 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Handoff sender is not a member");
120 if (m->kind == HANDOFF_SAMPLE_START) {
121 if (m->sequence <= p->sequence || (p->measured_ms && now - p->measured_ms < 1000))
122 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Repeated or excessive handoff probe");
123 p->sequence = m->sequence;
124 p->probe_started_ms = now;
125 p->measuring = true;
127 p->pending_nat_tier = m->nat_tier <= 4 ? m->nat_tier : 255;
128 p->pending_port = m->port;
129 } else if (m->kind == HANDOFF_SAMPLE_DATA) {
131 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Unmatched handoff probe");
132 p->measuring = false;
133 uint64_t elapsed = now - p->probe_started_ms;
134 if (elapsed == 0)
135 elapsed = 1;
136 p->valid = elapsed <= 5000;
137 if (p->valid) {
140 p->port = p->pending_port;
141 // A quality refresh cannot extend a failed recovery by retrying the same host.
142 if (!s->host_lost)
143 p->rejected = false;
144 }
146 p->measured_ms = now;
147 } else if (m->kind == HANDOFF_READY || m->kind == HANDOFF_REJECT) {
148 if (m->generation != s->generation || m->sequence != s->round || memcmp(m->participant, s->successor, SESSION_ID_SIZE))
149 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Obsolete handoff preparation");
150 if (m->kind == HANDOFF_READY && now <= s->prepare_deadline_ms)
151 s->ready = true;
152 else {
153 p->rejected = true;
154 s->ready = false;
155 memset(s->successor, 0, SESSION_ID_SIZE);
156 }
157 } else
158 return SET_ERRNO(ERROR_NETWORK_PROTOCOL, "Client cannot announce a handoff decision");
159 return ASCIICHAT_OK;
160}
unsigned int uint32_t
unsigned long long uint64_t
@ HANDOFF_REJECT
Definition handoff.h:28
@ HANDOFF_READY
Definition handoff.h:25
uint64_t probe_started_ms
Definition handoff.h:63
uint8_t pending_capabilities
Definition handoff.h:58

References ASCIICHAT_OK, consensus_handoff_message_t::capabilities, consensus_handoff_peer_t::capabilities, CONSENSUS_HANDOFF_HEADER, CONSENSUS_HANDOFF_PROBE_BYTES, ERROR_NETWORK_PROTOCOL, consensus_handoff_message_t::generation, consensus_handoff_t::generation, HANDOFF_READY, HANDOFF_REJECT, HANDOFF_SAMPLE_DATA, HANDOFF_SAMPLE_START, consensus_handoff_t::host_lost, consensus_handoff_message_t::kind, consensus_handoff_peer_t::measured_ms, consensus_handoff_peer_t::measuring, consensus_handoff_message_t::nat_tier, consensus_handoff_peer_t::nat_tier, consensus_handoff_message_t::participant, consensus_handoff_peer_t::pending_capabilities, consensus_handoff_peer_t::pending_nat_tier, consensus_handoff_peer_t::pending_port, consensus_handoff_message_t::port, consensus_handoff_peer_t::port, consensus_handoff_peer_t::probe_started_ms, consensus_handoff_t::ready, consensus_handoff_peer_t::rejected, consensus_handoff_t::round, consensus_handoff_message_t::sequence, consensus_handoff_peer_t::sequence, consensus_handoff_message_t::session, consensus_handoff_t::session, SESSION_ID_SIZE, SET_ERRNO, consensus_handoff_t::successor, consensus_handoff_peer_t::upload_kbps, and consensus_handoff_peer_t::valid.

◆ consensus_handoff_tick()

bool consensus_handoff_tick ( consensus_handoff_t * s,
uint64_t now,
consensus_handoff_message_t * a )

Advance measured successor selection and bounded host-loss recovery. Fresh eligible peers prepare within five seconds; all recovery attempts share one 15-second budget. Only a fresh prepared peer can commit, and failure is terminal.

Parameters
nowMonotonic receiver time in milliseconds.
aOutput action to send when the function returns true.
Returns
True if a PREPARE, COMMIT, or terminal FAILED action was produced.

Definition at line 194 of file handoff.c.

194 {
195 memset(a, 0, sizeof(*a));
196 memcpy(a->session, s->session, SESSION_ID_SIZE);
197 a->generation = s->generation;
198 if (s->failure_announced)
199 return false; // Terminal until the session is recreated.
200 if (!s->monitoring) {
201 s->monitoring = true;
202 s->monitoring_started_ms = now;
203 }
204 consensus_handoff_peer_t *incumbent = find_peer(s, s->host);
205 uint64_t last_host_sample = incumbent && incumbent->valid ? incumbent->measured_ms : s->monitoring_started_ms;
206 if (now - last_host_sample > CONSENSUS_HANDOFF_FRESH_MS)
207 s->host_lost = true;
208 if (s->host_lost && !s->failure_deadline_ms)
210 // The recovery budget is shared by all candidates, including silent/rejecting
211 // candidates that continue sending fresh probes. A late READY cannot revive it.
212 if (s->host_lost && now >= s->failure_deadline_ms) {
213 s->failure_announced = true;
214 s->ready = false;
215 memset(s->successor, 0, sizeof(s->successor));
216 a->kind = HANDOFF_FAILED;
217 a->sequence = s->round;
218 handoff_record_action(s, a, now, 0);
219 return true;
220 }
221 if (s->ready && s->host_lost) {
222 consensus_handoff_peer_t *p = find_peer(s, s->successor);
223 if (p && p->valid && p->capabilities && now - p->measured_ms <= CONSENSUS_HANDOFF_FRESH_MS) {
224 a->kind = HANDOFF_COMMIT;
225 memcpy(a->participant, s->successor, SESSION_ID_SIZE);
226 a->sequence = s->round;
227 a->port = p->port;
229 handoff_record_action(s, a, now, 1);
230 s->host_sequence = a->sequence;
231 memcpy(s->host, s->successor, SESSION_ID_SIZE);
232 s->host_lost = false;
233 s->monitoring_started_ms = now;
234 s->ready = false;
235 s->failure_deadline_ms = 0;
236 s->failure_announced = false;
237 for (size_t i = 0; i < s->count; ++i)
238 s->peers[i].rejected = false;
239 memset(s->successor, 0, SESSION_ID_SIZE);
240 return true;
241 }
242 s->ready = false;
243 memset(s->successor, 0, SESSION_ID_SIZE);
244 }
245 uint8_t zero[SESSION_ID_SIZE] = {0};
246 if (memcmp(s->successor, zero, sizeof(s->successor))) {
247 consensus_handoff_peer_t *p = find_peer(s, s->successor);
248 if (p && p->valid && p->capabilities && now - p->measured_ms <= CONSENSUS_HANDOFF_FRESH_MS && (s->ready || now < s->prepare_deadline_ms))
249 return false;
250 if (p && !s->ready)
251 p->rejected = true;
252 memset(s->successor, 0, SESSION_ID_SIZE);
253 s->ready = false;
254 }
255 participant_metrics_t metrics[MAX_SESSION_PARTICIPANTS];
256 size_t count = 0;
257 for (size_t i = 0; i < s->count; ++i) {
259 if (!p->valid || p->rejected || !p->capabilities || now - p->measured_ms > CONSENSUS_HANDOFF_FRESH_MS || memcmp(p->id, s->host, SESSION_ID_SIZE) == 0)
260 continue;
261 participant_metrics_t *m = &metrics[count++];
262 memset(m, 0, sizeof(*m));
263 memcpy(m->participant_id, p->id, SESSION_ID_SIZE);
264 m->nat_tier = p->nat_tier <= 4 ? p->nat_tier : 4;
265 m->upload_kbps = p->upload_kbps;
266 m->rtt_ns = 500000000; /* Unknown RTT earns no latency bonus. */
267 }
268 if (!count)
269
270 return false;
271 int best = -1, backup = -1;
272 if (consensus_election_choose_hosts(metrics, (int)count, &best, &backup) != ASCIICHAT_OK)
273 return false;
274 memcpy(s->successor, metrics[best].participant_id, SESSION_ID_SIZE);
275 ++s->round;
278 memcpy(a->participant, s->successor, SESSION_ID_SIZE);
279 a->sequence = s->round;
280 handoff_record_action(s, a, now, count);
281 return true;
282}
asciichat_error_t consensus_election_choose_hosts(const participant_metrics_t *metrics, int num_metrics, int *out_best_index, int *out_backup_index)
Elect best and backup host from metrics.
Definition election.c:78
#define CONSENSUS_HANDOFF_PREPARE_MS
Definition handoff.h:18
#define CONSENSUS_HANDOFF_FRESH_MS
Definition handoff.h:17
#define CONSENSUS_HANDOFF_RECOVERY_MS
Definition handoff.h:19
@ HANDOFF_COMMIT
Definition handoff.h:27
@ HANDOFF_FAILED
Definition handoff.h:29
@ HANDOFF_PREPARE
Definition handoff.h:24
uint32_t host_sequence
Last committed round; preparation does not change host authority.
Definition handoff.h:78
uint64_t monitoring_started_ms
Definition handoff.h:84
uint64_t prepare_deadline_ms
Definition handoff.h:79
uint64_t failure_deadline_ms
Definition handoff.h:83

References ASCIICHAT_OK, consensus_handoff_message_t::capabilities, consensus_handoff_peer_t::capabilities, consensus_election_choose_hosts(), CONSENSUS_HANDOFF_FRESH_MS, CONSENSUS_HANDOFF_PREPARE_MS, CONSENSUS_HANDOFF_RECOVERY_MS, consensus_handoff_t::count, consensus_handoff_t::failure_announced, consensus_handoff_t::failure_deadline_ms, consensus_handoff_message_t::generation, consensus_handoff_t::generation, HANDOFF_COMMIT, HANDOFF_FAILED, HANDOFF_PREPARE, consensus_handoff_t::host, consensus_handoff_t::host_lost, consensus_handoff_t::host_sequence, consensus_handoff_peer_t::id, consensus_handoff_message_t::kind, MAX_SESSION_PARTICIPANTS, consensus_handoff_peer_t::measured_ms, consensus_handoff_t::monitoring, consensus_handoff_t::monitoring_started_ms, consensus_handoff_peer_t::nat_tier, consensus_handoff_message_t::participant, consensus_handoff_t::peers, consensus_handoff_message_t::port, consensus_handoff_peer_t::port, consensus_handoff_t::prepare_deadline_ms, consensus_handoff_t::ready, consensus_handoff_peer_t::rejected, consensus_handoff_t::round, consensus_handoff_message_t::sequence, consensus_handoff_message_t::session, consensus_handoff_t::session, SESSION_ID_SIZE, consensus_handoff_t::successor, consensus_handoff_peer_t::upload_kbps, and consensus_handoff_peer_t::valid.