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

Go to the source code of this file.

Data Structures

struct  tcp_transport_data_t
 tcp transport data is intentionally a small, concrete record. Fields such as sockfd, owns_socket, is_connected, zerocopy_disabled give callers the identity and current value they need. It is normally borrowed from, or owned by, the surrounding subsystemβ€”not independently managed. More...

Functions

asciichat_error_t acip_tcp_transport_set_zerocopy (acip_transport_t *transport, bool enabled)
acip_transport_t * acip_tcp_transport_create (const char *name, socket_t sockfd, crypto_context_t *crypto_ctx)
 Create TCP transport from existing socket.
acip_transport_t * acip_tcp_transport_create_owned (const char *name, socket_t sockfd, crypto_context_t *crypto_ctx)
 Create a TCP transport that owns its connected socket.
void acip_transport_destroy (acip_transport_t *transport)
 Destroy transport and free all resources.

Function Documentation

◆ acip_tcp_transport_create()

acip_transport_t * acip_tcp_transport_create ( const char * name,
socket_t sockfd,
crypto_context_t * crypto_ctx )

Create TCP transport from existing socket.

Parameters
nameDebug name for tracking this transport (required, e.g., "transport_client_5")
sockfdConnected socket descriptor
crypto_ctxOptional crypto context (may be NULL)
Returns
Transport instance or NULL on error
Note
Caller retains socket ownership (transport doesn't close on destroy)

Definition at line 282 of file tcp/transport.c.

282 {
283 if (!name) {
284 SET_ERRNO(ERROR_INVALID_STATE, "Transport name is required");
285 return NULL;
286 }
287
288 if (sockfd == INVALID_SOCKET_VALUE) {
289 log_error("[TCP_CREATE_STATE] ❌ INVALID_SOCKET: sockfd=%d", sockfd);
290 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid socket descriptor");
291 return NULL;
292 }
293
294 log_info("[TCP_CREATE_STATE] 🟒 CREATE_START: sockfd=%d, crypto=%s", sockfd, crypto_ctx ? "yes" : "no");
295
296 // Allocate transport structure
298 if (!transport) {
299 log_error("[TCP_CREATE_STATE] ❌ ALLOC_TRANSPORT_FAILED: size=%zu", sizeof(acip_transport_t));
300 SET_ERRNO(ERROR_MEMORY, "Failed to allocate TCP transport");
301 return NULL;
302 }
303
304 log_debug("[TCP_CREATE_STATE] 🟒 TRANSPORT_ALLOCATED: transport=%p", (void *)transport);
305
306 // Allocate TCP-specific data
308 if (!tcp_data) {
309 log_error("[TCP_CREATE_STATE] ❌ ALLOC_TCP_DATA_FAILED: size=%zu", sizeof(tcp_transport_data_t));
310 SAFE_FREE(transport);
311 SET_ERRNO(ERROR_MEMORY, "Failed to allocate TCP transport data");
312 return NULL;
313 }
314
315 log_debug("[TCP_CREATE_STATE] 🟒 TCP_DATA_ALLOCATED: tcp_data=%p", (void *)tcp_data);
316
317 // Initialize TCP data
318 tcp_data->sockfd = sockfd;
319 tcp_data->is_connected = true;
320 tcp_data->owns_socket = false;
321 tcp_data->zerocopy_disabled = false;
322
323 // Initialize send mutex to protect concurrent sends from multiple threads
324 int mutex_result = mutex_init(&tcp_data->send_mutex, "tcp_send");
325 if (mutex_result != 0) {
326 log_error("[TCP_CREATE_STATE] ❌ MUTEX_INIT_FAILED: result=%d", mutex_result);
327 SAFE_FREE(tcp_data);
328 SAFE_FREE(transport);
329 SET_ERRNO(ERROR_MEMORY, "Failed to initialize send mutex");
330 return NULL;
331 }
332
333 log_info("[TCP_CREATE_STATE] 🟒 TCP_DATA_INITIALIZED: sockfd=%d, is_connected=true, send_mutex=initialized", sockfd);
334
335 // Enable TCP_NODELAY to disable Nagle's algorithm
336 // This ensures small packets are sent immediately instead of being buffered
337 int nodelay = 1;
338 if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (const char *)&nodelay, sizeof(nodelay)) < 0) {
339 log_warn("[TCP_CREATE_STATE] ⚠️ TCP_NODELAY_FAILED: sockfd=%d, %s", sockfd, SAFE_STRERROR(errno));
340 // Continue anyway - this is not fatal
341 } else {
342 log_debug("[TCP_CREATE_STATE] 🟒 TCP_NODELAY_SET: sockfd=%d", sockfd);
343 }
344
345 // Initialize transport
346 transport->methods = &tcp_methods;
347 transport->stats_peer = stats_runtime_peer_open("TCP");
348 transport->crypto_ctx = crypto_ctx;
349 transport->impl_data = tcp_data;
350 int send_buffer = SOCKET_STREAM_SEND_BUFFER;
351 if (socket_setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &send_buffer, sizeof(send_buffer)) != 0) {
352 acip_transport_destroy(transport);
353 SET_ERRNO_SYS(ERROR_NETWORK, "Cannot bound TCP send buffer");
354 return NULL;
355 }
356
357 // Register transport and impl_data before logging so named replacements work
358 NAMED_REGISTER_TRANSPORT(transport, name, NULL);
359 NAMED_REGISTER(tcp_data, "impl", "tcp_impl", "0x%tx", (uintptr_t)(const void *)(transport));
360
361 log_info("[TCP_CREATE_STATE] βœ… CREATE_COMPLETE: transport=%p, sockfd=%d, is_connected=true, crypto=%s", (void *)transport, sockfd, crypto_ctx ? "enabled" : "disabled");
362
363#ifndef __EMSCRIPTEN__
364 if (transport && acip_outbound_create(transport) != ASCIICHAT_OK) {
365 acip_transport_destroy(transport);
366 return NULL;
367 }
368#endif
369 return transport;
370}
#define SET_ERRNO(code,...)
#define SET_ERRNO_SYS(code,...)
asciichat_error_t acip_outbound_create(acip_transport_t *transport)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
#define SAFE_STRERROR(errnum)
#define SAFE_CALLOC(count, size, cast)
#define NAMED_REGISTER_TRANSPORT(transport, name, parent_ptr)
Register a transport with automatic format specifier.
#define NAMED_REGISTER(ptr, name, type, fmt, parent_ptr)
Register any pointer with base name, type, format spec, and location (auto-suffix).
@ ERROR_INVALID_STATE
@ ERROR_NETWORK
Definition error_codes.h:76
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
stats_peer_t * stats_runtime_peer_open(const char *transport)
Definition runtime.c:149
void * impl_data
Transport-specific state.
stats_peer_t * stats_peer
Owned statistics handle, destroyed with transport.
const acip_transport_methods_t * methods
Method table (virtual functions).
crypto_context_t * crypto_ctx
Optional encryption context.
tcp transport data is intentionally a small, concrete record. Fields such as sockfd,...
bool owns_socket
Close the socket when this transport is closed or destroyed.
socket_t sockfd
Connected socket descriptor.
bool is_connected
Connection state.
mutex_t send_mutex
Mutex to protect concurrent sends (multiple threads may send packets).
bool zerocopy_disabled
Disable copy avoidance after unsupported or copied completion.
void acip_transport_destroy(acip_transport_t *transport)
Destroy transport and free all resources.

References acip_outbound_create(), acip_transport_destroy(), ASCIICHAT_OK, acip_transport::crypto_ctx, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_NETWORK, acip_transport::impl_data, tcp_transport_data_t::is_connected, log_debug, log_error, log_info, log_warn, acip_transport::methods, NAMED_REGISTER, NAMED_REGISTER_TRANSPORT, tcp_transport_data_t::owns_socket, SAFE_CALLOC, SAFE_FREE, SAFE_MALLOC, SAFE_STRERROR, tcp_transport_data_t::send_mutex, SET_ERRNO, SET_ERRNO_SYS, tcp_transport_data_t::sockfd, acip_transport::stats_peer, stats_runtime_peer_open(), and tcp_transport_data_t::zerocopy_disabled.

Referenced by acip_tcp_transport_create_owned().

◆ acip_tcp_transport_create_owned()

acip_transport_t * acip_tcp_transport_create_owned ( const char * name,
socket_t sockfd,
crypto_context_t * crypto_ctx )

Create a TCP transport that owns its connected socket.

Parameters
nameDebug name for the transport
sockfdConnected socket; ownership transfers only on success
crypto_ctxOptional borrowed crypto context
Returns
Transport instance or NULL on error Closing or destroying the transport closes the socket.

Definition at line 372 of file tcp/transport.c.

372 {
373 acip_transport_t *transport = acip_tcp_transport_create(name, sockfd, crypto_ctx);
374 if (transport)
375 ((tcp_transport_data_t *)transport->impl_data)->owns_socket = true;
376 return transport;
377}
acip_transport_t * acip_tcp_transport_create(const char *name, socket_t sockfd, crypto_context_t *crypto_ctx)
Create TCP transport from existing socket.

References acip_tcp_transport_create(), and acip_transport::impl_data.

◆ acip_tcp_transport_set_zerocopy()

asciichat_error_t acip_tcp_transport_set_zerocopy ( acip_transport_t * transport,
bool enabled )

Override the automatic kernel copy-avoidance policy for this TCP transport. Large Linux encrypted sends may pin pages; other platforms fall back to vectors. Copy avoidance is attempted automatically and disabled after copied completions.

Definition at line 110 of file tcp/transport.c.

110 {
111 if (!transport || acip_transport_get_type(transport) != ACIP_TRANSPORT_TCP)
112 return SET_ERRNO(ERROR_INVALID_PARAM, "Expected TCP transport");
113 tcp_transport_data_t *tcp = transport->impl_data;
114 mutex_lock(&tcp->send_mutex);
116 mutex_unlock(&tcp->send_mutex);
117 return ASCIICHAT_OK;
118}
bool enabled
Is filtering active?
Definition grep.c:62
@ ACIP_TRANSPORT_TCP
Raw TCP socket.

References ACIP_TRANSPORT_TCP, ASCIICHAT_OK, enabled, ERROR_INVALID_PARAM, acip_transport::impl_data, tcp_transport_data_t::send_mutex, SET_ERRNO, and tcp_transport_data_t::zerocopy_disabled.

◆ acip_transport_destroy()

void acip_transport_destroy ( acip_transport_t * transport)

Destroy transport and free all resources.

Parameters
transportTransport to destroy (may be NULL)
Note
Calls close() first if still connected
Frees the transport structure itself
Safe to pass NULL

Definition at line 383 of file tcp/transport.c.

383 {
384 if (!transport) {
385 log_debug("[TRANSPORT_DESTROY] ⚠️ NULL_TRANSPORT: nothing to destroy");
386 return;
387 }
388
389 log_debug("[TRANSPORT_DESTROY] πŸ”΄ DESTROY_START: transport=%p, impl_data=%p", (void *)transport, transport->impl_data);
390
391#ifndef __EMSCRIPTEN__
392 acip_outbound_destroy(transport);
393#endif
394
395 // Get type before we destroy for logging
396 acip_transport_type_t type = 0;
397 if (transport->methods && transport->methods->get_type) {
398 type = transport->methods->get_type(transport);
399 log_debug("[TRANSPORT_DESTROY] πŸ“‹ TRANSPORT_TYPE: type=%d (1=TCP, 2=WebSocket, 3=WebRTC, ...)", type);
400 }
401
402 // Close if still connected
403 if (transport->methods && transport->methods->close && transport->methods->is_connected && transport->methods->is_connected(transport)) {
404 log_debug("[TRANSPORT_DESTROY] πŸ”΄ STILL_CONNECTED: calling close() first (type=%d)", type);
405 asciichat_error_t close_result = transport->methods->close(transport);
406 if (close_result != ASCIICHAT_OK)
407 log_warn("Transport close during cleanup failed: %d", close_result);
408 log_debug("[TRANSPORT_DESTROY] πŸ”΄ CLOSE_CALLED: result=%d", close_result != ASCIICHAT_OK ? -1 : 0);
409 } else {
410 log_debug("[TRANSPORT_DESTROY] βœ… ALREADY_CLOSED: skipping close (type=%d)", type);
411 }
412
413 // Call custom destroy implementation if provided
414 if (transport->methods && transport->methods->destroy_impl) {
415 log_debug("[TRANSPORT_DESTROY] πŸ”§ CALLING_DESTROY_IMPL: type=%d", type);
416 transport->methods->destroy_impl(transport);
417 log_debug("[TRANSPORT_DESTROY] βœ… DESTROY_IMPL_COMPLETE: type=%d", type);
418 } else {
419 log_debug("[TRANSPORT_DESTROY] ⏭️ NO_CUSTOM_DESTROY: type=%d (using generic cleanup)", type);
420 }
421
422 // Free implementation data
423 if (transport->impl_data) {
424 log_debug("[TRANSPORT_DESTROY] πŸ—‘οΈ FREEING_IMPL_DATA: %p", transport->impl_data);
425 NAMED_UNREGISTER(transport->impl_data);
426 SAFE_FREE(transport->impl_data);
427 log_debug("[TRANSPORT_DESTROY] βœ… IMPL_DATA_FREED");
428 }
429
431 transport->stats_peer = NULL;
432
433 // Free transport structure
434 log_debug("[TRANSPORT_DESTROY] πŸ—‘οΈ FREEING_TRANSPORT: %p", (void *)transport);
435 SAFE_FREE(transport);
436
437 log_debug("[TRANSPORT_DESTROY] βœ… DESTROY_COMPLETE: type=%d", type);
438}
void acip_outbound_destroy(acip_transport_t *transport)
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
acip_transport_type_t
Transport method table (virtual function table).
void stats_runtime_peer_close(stats_peer_t *peer)
Definition runtime.c:177
bool(* is_connected)(acip_transport_t *transport)
Check if transport is connected.
asciichat_error_t(* close)(acip_transport_t *transport)
Close this transport.
void(* destroy_impl)(acip_transport_t *transport)
acip_transport_type_t(* get_type)(acip_transport_t *transport)
Get transport type.

References acip_outbound_destroy(), ASCIICHAT_OK, acip_transport_methods_t::close, acip_transport_methods_t::destroy_impl, acip_transport_methods_t::get_type, acip_transport::impl_data, acip_transport_methods_t::is_connected, log_debug, log_warn, acip_transport::methods, NAMED_UNREGISTER, SAFE_FREE, acip_transport::stats_peer, and stats_runtime_peer_close().

Referenced by acip_tcp_transport_create().