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
Go to the documentation of this file.
1
6
8#include "negotiate.h"
9
10#include <ascii-chat/common.h>
11#include <ascii-chat/log/log.h>
13
14#include <string.h>
15
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}
37
47static uint8_t determine_connection_type(const nat_quality_t *quality) {
48 if (!quality) {
49 return ACIP_CONNECTION_TYPE_STUN; // Default fallback
50 }
51
52 if (quality->has_public_ip) {
54 } else if (quality->upnp_available) {
56 } else {
58 }
59}
60
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}
ascii-chat Discovery Service (ACDS) Protocol Message Formats
#define SET_ERRNO(code,...)
#define SAFE_STRNCPY(dst, src, size)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define SESSION_ID_SIZE
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define ACIP_HOST_DEFAULT_PORT
Default port for discovery mode hosts (use OPT_PORT_INT_DEFAULT from options.h).
Definition acds.h:1105
@ ACIP_CONNECTION_TYPE_UPNP
UPnP/NAT-PMP port mapping.
Definition acds.h:844
@ ACIP_CONNECTION_TYPE_DIRECT_PUBLIC
Direct public IP connection.
Definition acds.h:843
@ ACIP_CONNECTION_TYPE_STUN
STUN hole-punching.
Definition acds.h:845
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
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
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
asciichat_error_t negotiate_determine_result(negotiate_ctx_t *ctx)
Determine negotiation result.
Definition negotiate.c:61
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,...
Definition negotiate.c:16
Host negotiation logic for discovery mode.
@ NEGOTIATE_STATE_INIT
Initial state.
Definition negotiate.h:31
@ 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
uint8_t session_id[SESSION_ID_SIZE]
uint8_t participant_id[SESSION_ID_SIZE]
🔤 String Manipulation and Shell Escaping Utilities
NAT quality assessment result.
Definition nat.h:28
bool detection_complete
All probes finished.
Definition nat.h:52
bool has_public_ip
STUN reflexive == local IP.
Definition nat.h:30
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
Host negotiation context.
Definition negotiate.h:44
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
uint8_t participant_id[SESSION_ID_SIZE]
Definition negotiate.h:47
uint8_t session_id[SESSION_ID_SIZE]
Definition negotiate.h:46
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