40 size_t remaining = len;
42 while (remaining > 0) {
43 ssize_t sent =
socket_send(sockfd, ptr, remaining, 0);
52 remaining -= (size_t)sent;
65 if (!tcp->is_connected) {
80 bool should_encrypt =
false;
84 should_encrypt =
true;
89 if (!should_encrypt) {
90 return tcp_send_all(tcp->sockfd, data, len);
100 size_t ciphertext_len;
102 crypto_encrypt(transport->crypto_ctx, data, len, ciphertext, ciphertext_size, &ciphertext_len);
118 send_result = tcp_send_all(tcp->sockfd, &encrypted_header,
sizeof(encrypted_header));
120 send_result = tcp_send_all(tcp->sockfd, ciphertext, ciphertext_len);
130 void **out_allocated_buffer) {
133 if (!tcp->is_connected) {
139 bool enforce_encryption = (transport->crypto_ctx != NULL);
153 *buffer = envelope.
data;
154 *out_len = envelope.
len;
163 if (!tcp->is_connected) {
169 tcp->is_connected =
false;
171 log_debug(
"TCP transport marked as disconnected (socket not closed)");
187 return tcp->is_connected;
198 .get_type = tcp_get_type,
199 .get_socket = tcp_get_socket,
200 .is_connected = tcp_is_connected,
201 .destroy_impl = NULL,
230 tcp_data->
sockfd = sockfd;
234 transport->
methods = &tcp_methods;
238 log_debug(
"Created TCP transport for socket %d (crypto: %s)", sockfd, crypto_ctx ?
"enabled" :
"disabled");
🗃️ Lock-Free Unified Memory Buffer Pool with Lazy Allocation
Hardware-Accelerated CRC32 Checksum Computation.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U16(val)
#define HOST_TO_NET_U32(val)
#define NET_TO_HOST_U16(val)
void buffer_pool_free(buffer_pool_t *pool, void *data, size_t size)
Free a buffer back to the pool (lock-free)
void * buffer_pool_alloc(buffer_pool_t *pool, size_t size)
Allocate a buffer from the pool (lock-free fast path)
#define SAFE_MALLOC(size, cast)
crypto_result_t crypto_encrypt(crypto_context_t *ctx, const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext_out, size_t ciphertext_out_size, size_t *ciphertext_len_out)
Encrypt data using XSalsa20-Poly1305.
#define CRYPTO_NONCE_SIZE
Nonce size (XSalsa20)
const char * crypto_result_to_string(crypto_result_t result)
Convert crypto result to human-readable string.
crypto_result_t
Cryptographic operation result codes.
#define CRYPTO_MAC_SIZE
MAC size (Poly1305)
bool crypto_is_ready(const crypto_context_t *ctx)
Check if key exchange is complete and ready for encryption.
#define SET_ERRNO_SYS(code, context_msg,...)
Set error code with custom message and system error context.
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
#define LOG_RATE_SLOW
Log rate limit: 10 seconds (10,000,000 microseconds)
#define log_debug_every(interval_us, fmt,...)
Rate-limited DEBUG logging.
#define log_debug(...)
Log a DEBUG message.
uint32_t magic
Magic number (PACKET_MAGIC) for packet validation.
uint32_t client_id
Client ID (0 = server, >0 = client identifier)
uint32_t length
Payload data length in bytes (0 for header-only packets)
void * allocated_buffer
Buffer that needs to be freed by caller (may be NULL if not allocated)
size_t len
Length of payload data in bytes.
void * data
Packet payload data (decrypted and decompressed if applicable)
packet_recv_result_t receive_packet_secure(socket_t sockfd, void *crypto_ctx, bool enforce_encryption, packet_envelope_t *envelope)
Receive a packet with decryption and decompression support.
uint16_t type
Packet type (packet_type_t enumeration)
uint32_t crc32
CRC32 checksum of payload data (0 if length == 0)
packet_recv_result_t
Packet reception result codes.
@ PACKET_RECV_EOF
Connection closed (EOF)
@ PACKET_RECV_SECURITY_VIOLATION
Encryption policy violation (e.g., unencrypted packet when encryption required)
@ PACKET_RECV_SUCCESS
Packet received successfully.
packet_type_t
Network protocol packet type enumeration.
#define PACKET_MAGIC
Packet magic number (0xDEADBEEF)
@ PACKET_TYPE_ENCRYPTED
Encrypted packet (after handshake completion)
#define asciichat_crc32(data, len)
Main CRC32 dispatcher macro - use this in application code.
📝 Logging API with multiple log levels and terminal output control
🌐 Core network I/O operations with timeout support
Packet protocol implementation with encryption and compression support.
Cross-platform socket interface for ascii-chat.
Transport method table (virtual function table)
asciichat_error_t(* send)(acip_transport_t *transport, const void *data, size_t len)
Send data through this transport.
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)
Custom destroy implementation (optional)
Transport instance structure.
void * impl_data
Transport-specific state.
const acip_transport_methods_t * methods
Method table (virtual functions)
crypto_context_t * crypto_ctx
Optional encryption context.
Cryptographic context structure.
Packet envelope containing received packet data.
TCP transport implementation data.
socket_t sockfd
Socket descriptor (NOT owned - don't close)
bool is_connected
Connection state.
acip_transport_t * acip_tcp_transport_create(socket_t sockfd, crypto_context_t *crypto_ctx)
Create TCP transport from existing socket.
void acip_transport_destroy(acip_transport_t *transport)
Destroy transport and free all resources.
Transport abstraction layer for ACIP protocol.
acip_transport_type_t
Transport type enumeration.
@ ACIP_TRANSPORT_TCP
Raw TCP socket.