ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
Acip_server

ACIP Packet Validation

asciichat_error_t acip_server_validate_session_create (const acip_session_create_t *req)
 Validate ACIP session create request.
 
asciichat_error_t acip_server_validate_session_join (const acip_session_join_t *req)
 Validate ACIP session join request.
 

ACIP Error Response Helpers

asciichat_error_t acip_server_send_error (socket_t sockfd, acip_error_code_t error_code, const char *error_message)
 Send ACIP error response to client.
 

Detailed Description

Server-side ACIP (ASCII-Chat IP Protocol) utilities for:

ACIP Protocol Overview:

Primary Use Case: Library functions for building ACIP servers (like ACDS - ASCII-Chat Discovery Service). Provides reusable validation and packet handling logic that any ACIP server implementation can use.

Integration:

Note
This module provides library-level utilities. The ACDS server application code is in src/acds/.
See also
network/acip/client.h for client-side ACIP library
network/acip/acds.h for ACDS message structures
network/acip/protocol.h for ACIP Packet Types types
Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026
Version
1.0 (ACIP Protocol Refactoring)

Function Documentation

◆ acip_server_send_error()

asciichat_error_t acip_server_send_error ( socket_t  sockfd,
acip_error_code_t  error_code,
const char *  error_message 
)

#include <acds_server.h>

Send ACIP error response to client.

Parameters
sockfdClient socket
error_codeACIP error code
error_messageHuman-readable error message (max 255 chars)
Returns
ASCIICHAT_OK on success, error code on failure

Sends PACKET_TYPE_ACIP_ERROR response to client with specified error code and message. Message will be truncated if longer than 255 characters.

Definition at line 77 of file acds_server.c.

77 {
78 if (sockfd == INVALID_SOCKET_VALUE) {
79 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid socket");
80 }
81
82 if (!error_message) {
83 error_message = "Unknown error";
84 }
85
86 acip_error_t error_packet;
87 memset(&error_packet, 0, sizeof(error_packet));
88 error_packet.error_code = (uint8_t)error_code;
89
90 // Safely copy error message (truncate if needed)
91 size_t msg_len = strlen(error_message);
92 if (msg_len >= sizeof(error_packet.error_message)) {
93 msg_len = sizeof(error_packet.error_message) - 1;
94 }
95 memcpy(error_packet.error_message, error_message, msg_len);
96 error_packet.error_message[msg_len] = '\0';
97
98 // Send ACIP_ERROR packet
99 int result = send_packet(sockfd, PACKET_TYPE_ACIP_ERROR, &error_packet, sizeof(error_packet));
100 if (result < 0) {
101 return SET_ERRNO(ERROR_NETWORK, "Failed to send ACIP error packet");
102 }
103
104 return ASCIICHAT_OK;
105}
asciichat_error_t error_code
unsigned char uint8_t
Definition common.h:56
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
@ ERROR_NETWORK
Definition error_codes.h:69
@ ASCIICHAT_OK
Definition error_codes.h:48
@ ERROR_INVALID_PARAM
int send_packet(socket_t sockfd, packet_type_t type, const void *data, size_t len)
Send a basic packet without encryption.
Definition packet.c:754
@ PACKET_TYPE_ACIP_ERROR
Generic error response (Discovery Server -> Client)
Definition packet.h:404
#define INVALID_SOCKET_VALUE
Invalid socket value (POSIX: -1)
Definition socket.h:52

References acip_error_t, ASCIICHAT_OK, error_code, ERROR_INVALID_PARAM, ERROR_NETWORK, INVALID_SOCKET_VALUE, PACKET_TYPE_ACIP_ERROR, send_packet(), and SET_ERRNO.

◆ acip_server_validate_session_create()

asciichat_error_t acip_server_validate_session_create ( const acip_session_create_t req)

#include <acds_server.h>

Validate ACIP session create request.

Parameters
reqSession create request packet
Returns
ASCIICHAT_OK if valid, error code if invalid

Validates that a SESSION_CREATE request has all required fields populated correctly (identity, signature, capabilities, etc.).

Definition at line 27 of file acds_server.c.

27 {
28 if (!req) {
29 return SET_ERRNO(ERROR_INVALID_PARAM, "Session create request is NULL");
30 }
31
32 // Validate capabilities (only bits 0-1 should be set)
33 if (req->capabilities & ~0x03) {
34 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid capabilities bits (only 0-1 allowed)");
35 }
36
37 // Validate max_participants (1-8)
38 if (req->max_participants < 1 || req->max_participants > 8) {
39 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid max_participants (must be 1-8)");
40 }
41
42 // Validate server address is not empty
43 if (req->server_address[0] == '\0') {
44 return SET_ERRNO(ERROR_INVALID_PARAM, "Server address is empty");
45 }
46
47 // Validate server port is non-zero
48 if (req->server_port == 0) {
49 return SET_ERRNO(ERROR_INVALID_PARAM, "Server port is zero");
50 }
51
52 return ASCIICHAT_OK;
53}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, and SET_ERRNO.

◆ acip_server_validate_session_join()

asciichat_error_t acip_server_validate_session_join ( const acip_session_join_t req)

#include <acds_server.h>

Validate ACIP session join request.

Parameters
reqSession join request packet
Returns
ASCIICHAT_OK if valid, error code if invalid

Validates that a SESSION_JOIN request has all required fields populated correctly (session string, identity, signature, etc.).

Definition at line 55 of file acds_server.c.

55 {
56 if (!req) {
57 return SET_ERRNO(ERROR_INVALID_PARAM, "Session join request is NULL");
58 }
59
60 // Validate session string length
61 if (req->session_string_len == 0 || req->session_string_len > ACIP_MAX_SESSION_STRING_LEN) {
62 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session string length");
63 }
64
65 // Validate session string is not empty
66 if (req->session_string[0] == '\0') {
67 return SET_ERRNO(ERROR_INVALID_PARAM, "Session string is empty");
68 }
69
70 return ASCIICHAT_OK;
71}
#define ACIP_MAX_SESSION_STRING_LEN
Maximum session string length (e.g., "swift-river-mountain" = 20 chars)

References ACIP_MAX_SESSION_STRING_LEN, ASCIICHAT_OK, ERROR_INVALID_PARAM, and SET_ERRNO.