ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
negotiate.c File Reference

Host negotiation logic for discovery mode. More...

Go to the source code of this file.

Functions

void negotiate_init (negotiate_ctx_t *ctx, const uint8_t session_id[SESSION_ID_SIZE], const uint8_t participant_id[SESSION_ID_SIZE], bool is_initiator)
 negotiate ctx is intentionally a small, concrete record. Fields such as is_initiator, our_quality, peer_quality, peer_quality_received give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystem—not independently managed.
asciichat_error_t negotiate_determine_result (negotiate_ctx_t *ctx)
 Determine negotiation result.

Detailed Description

Host negotiation logic for discovery mode.

Definition in file negotiate.c.

Function Documentation

◆ negotiate_determine_result()

asciichat_error_t negotiate_determine_result ( negotiate_ctx_t * ctx)

Determine negotiation result.

Called once bootstrap observations are available. Sets we_are_host and related fields.

Parameters
ctxNegotiation context
Returns
ASCIICHAT_OK on success

Definition at line 61 of file negotiate.c.

61 {
62 if (!ctx) {
63 return SET_ERRNO(ERROR_INVALID_PARAM, "ctx is NULL");
64 }
65
67 return SET_ERRNO(ERROR_INVALID_STATE, "Cannot determine result: detection incomplete");
68 }
69
71
72 // Compare NAT qualities
73 int result = nat_compare_quality(&ctx->our_quality, &ctx->peer_quality, ctx->is_initiator);
74
75 if (result <= 0) {
76 // We should host
77 ctx->we_are_host = true;
79
80 // Set our address as host address
81 if (ctx->our_quality.public_address[0]) {
83 } else {
84 // Fall back to localhost for testing
85 SAFE_STRNCPY(ctx->host_address, "127.0.0.1", sizeof(ctx->host_address));
86 }
87
89 ctx->connection_type = determine_connection_type(&ctx->our_quality);
90
91 log_info("Negotiation result: WE ARE HOST (addr=%s:%u, type=%d)", ctx->host_address, ctx->host_port, ctx->connection_type);
92 } else {
93 // They should host
94 ctx->we_are_host = false;
96
97 // Set peer address as host address
98 if (ctx->peer_quality.public_address[0]) {
100 }
101
103 ctx->connection_type = determine_connection_type(&ctx->peer_quality);
104
105 log_info("Negotiation result: THEY ARE HOST (addr=%s:%u, type=%d)", ctx->host_address, ctx->host_port, ctx->connection_type);
106 }
107
109 return ASCIICHAT_OK;
110}
#define SET_ERRNO(code,...)
#define SAFE_STRNCPY(dst, src, size)
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define ACIP_HOST_DEFAULT_PORT
Default port for discovery mode hosts (use OPT_PORT_INT_DEFAULT from options.h).
Definition acds.h:1105
int nat_compare_quality(const nat_quality_t *ours, const nat_quality_t *theirs, bool we_are_initiator)
Compare two NAT qualities and determine who should host.
Definition nat.c:61
@ NEGOTIATE_STATE_WE_HOST
We won, becoming host.
Definition negotiate.h:35
@ NEGOTIATE_STATE_THEY_HOST
They won, connecting as client.
Definition negotiate.h:36
@ NEGOTIATE_STATE_COMPARING
Comparing qualities.
Definition negotiate.h:34
@ NEGOTIATE_STATE_COMPLETE
Negotiation complete.
Definition negotiate.h:38
bool detection_complete
All probes finished.
Definition nat.h:52
bool upnp_available
UPnP/NAT-PMP mapping succeeded.
Definition nat.h:31
char public_address[IP_ADDRESS_BUFFER_SIZE]
Public IP address.
Definition nat.h:36
uint16_t upnp_mapped_port
Mapped external port (if upnp_available).
Definition nat.h:32
bool we_are_host
True if we should become host.
Definition negotiate.h:60
uint8_t connection_type
acip_connection_type_t
Definition negotiate.h:63
char host_address[64]
Host's address (ours if we_are_host).
Definition negotiate.h:61
nat_quality_t our_quality
Our NAT quality.
Definition negotiate.h:51
bool peer_quality_received
Have we received peer's quality?
Definition negotiate.h:53
nat_quality_t peer_quality
Peer's NAT quality (when received).
Definition negotiate.h:52
bool is_initiator
Did we create this session?
Definition negotiate.h:48
negotiate_state_t state
Definition negotiate.h:56
uint16_t host_port
Host's port.
Definition negotiate.h:62

References ACIP_HOST_DEFAULT_PORT, ASCIICHAT_OK, negotiate_ctx_t::connection_type, nat_quality_t::detection_complete, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, negotiate_ctx_t::host_address, negotiate_ctx_t::host_port, negotiate_ctx_t::is_initiator, log_info, nat_compare_quality(), NEGOTIATE_STATE_COMPARING, NEGOTIATE_STATE_COMPLETE, NEGOTIATE_STATE_THEY_HOST, NEGOTIATE_STATE_WE_HOST, negotiate_ctx_t::our_quality, negotiate_ctx_t::peer_quality, negotiate_ctx_t::peer_quality_received, nat_quality_t::public_address, SAFE_STRNCPY, SET_ERRNO, negotiate_ctx_t::state, nat_quality_t::upnp_available, nat_quality_t::upnp_mapped_port, and negotiate_ctx_t::we_are_host.

◆ negotiate_init()

void negotiate_init ( negotiate_ctx_t * ctx,
const uint8_t session_id[SESSION_ID_SIZE],
const uint8_t participant_id[SESSION_ID_SIZE],
bool is_initiator )

negotiate ctx is intentionally a small, concrete record. Fields such as is_initiator, our_quality, peer_quality, peer_quality_received give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystem—not independently managed.

Initialize negotiation context

Parameters
ctxContext to initialize
session_idSession UUID
participant_idOur participant UUID
is_initiatorTrue if we created the session

Definition at line 16 of file negotiate.c.

16 {
17 if (!ctx)
18 return;
19
20 memset(ctx, 0, sizeof(negotiate_ctx_t));
21
22 if (session_id)
26
27 ctx->is_initiator = is_initiator;
29 ctx->peer_quality_received = false;
30 ctx->we_are_host = false;
31
34
35 log_debug("Negotiation initialized (initiator=%d)", is_initiator);
36}
#define SESSION_ID_SIZE
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
void nat_quality_init(nat_quality_t *quality)
Connectivity measurements collected while judging a peer's NAT path: public-IP and UPnP availability,...
Definition nat.c:39
@ NEGOTIATE_STATE_INIT
Initial state.
Definition negotiate.h:31
uint8_t session_id[SESSION_ID_SIZE]
uint8_t participant_id[SESSION_ID_SIZE]
Host negotiation context.
Definition negotiate.h:44
uint8_t participant_id[SESSION_ID_SIZE]
Definition negotiate.h:47
uint8_t session_id[SESSION_ID_SIZE]
Definition negotiate.h:46

References negotiate_ctx_t::is_initiator, log_debug, nat_quality_init(), NEGOTIATE_STATE_INIT, negotiate_ctx_t::our_quality, negotiate_ctx_t::participant_id, participant_id, negotiate_ctx_t::peer_quality, negotiate_ctx_t::peer_quality_received, negotiate_ctx_t::session_id, session_id, SESSION_ID_SIZE, negotiate_ctx_t::state, and negotiate_ctx_t::we_are_host.