ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
acds_server.c
Go to the documentation of this file.
1#include <ascii-chat/common.h>
16
19#include <ascii-chat/log/log.h>
20#include <ascii-chat/common.h>
21
22#include <string.h>
23
24// ============================================================================
25// Packet Validation
26// ============================================================================
27
29 if (!req) {
30 return SET_ERRNO(ERROR_INVALID_PARAM, "Session create request is NULL");
31 }
32
33 if (req->total_keys > MAX_SESSION_KEYS || req->key_index >= (req->total_keys ? req->total_keys : 1)) {
34 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session key count/index");
35 }
36
37 // Validate capabilities (only bits 0-1 should be set)
38 if (req->capabilities & ~0x03) {
39 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid capabilities bits (only 0-1 allowed)");
40 }
41
42 // Validate max_participants (1-128)
43 if (req->max_participants < 1 || req->max_participants > MAX_SESSION_PARTICIPANTS) {
44 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid max_participants (must be 1-%d)", MAX_SESSION_PARTICIPANTS);
45 }
46
47 // Server address can be empty - ACDS will auto-detect from connection source IP
48
49 // Validate server port is non-zero
50 if (req->server_port == 0) {
51 return SET_ERRNO(ERROR_INVALID_PARAM, "Server port is zero");
52 }
53
54 return ASCIICHAT_OK;
55}
56
58 if (!req) {
59 return SET_ERRNO(ERROR_INVALID_PARAM, "Session join request is NULL");
60 }
61
62 // Validate session string length
63 if (req->session_string_len == 0 || req->session_string_len > ACIP_MAX_SESSION_STRING_LEN) {
64 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session string length");
65 }
66
67 // Validate session string is not empty
68 if (req->session_string[0] == '\0') {
69 return SET_ERRNO(ERROR_INVALID_PARAM, "Session string is empty");
70 }
71
72 return ASCIICHAT_OK;
73}
74
75// ============================================================================
76// Error Response Helpers
77// ============================================================================
78
79asciichat_error_t acip_server_send_error(socket_t sockfd, acip_error_code_t error_code, const char *error_message) {
80 if (sockfd == INVALID_SOCKET_VALUE) {
81 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid socket");
82 }
83
84 if (!error_message) {
85 error_message = "Unknown error";
86 }
87
88 acip_error_t error_packet;
89 memset(&error_packet, 0, sizeof(error_packet));
90 error_packet.error_code = (uint8_t)error_code;
91
92 // Safely copy error message (truncate if needed)
93 size_t msg_len = strlen(error_message);
94 if (msg_len >= sizeof(error_packet.error_message)) {
95 msg_len = sizeof(error_packet.error_message) - 1;
96 }
97 memcpy(error_packet.error_message, error_message, msg_len);
98 error_packet.error_message[msg_len] = '\0';
99
100 // Send ACIP_ERROR packet
101 int result = send_packet(sockfd, PACKET_TYPE_ACIP_ERROR, &error_packet, sizeof(error_packet));
102 if (result < 0) {
103 return SET_ERRNO(ERROR_NETWORK, "Failed to send ACIP error packet");
104 }
105
106 return ASCIICHAT_OK;
107}
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.
Definition acds_server.c:79
asciichat_error_t acip_server_validate_session_join(const acip_session_join_t *req)
Validate ACIP session join request.
Definition acds_server.c:57
asciichat_error_t acip_server_validate_session_create(const acip_session_create_t *req)
Validate ACIP session create request.
Definition acds_server.c:28
ACIP server-side protocol library and ACDS server utilities.
#define ACIP_MAX_SESSION_STRING_LEN
Definition acds_sizes.h:10
#define SET_ERRNO(code,...)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_NETWORK
Definition error_codes.h:76
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define MAX_SESSION_PARTICIPANTS
#define MAX_SESSION_KEYS
acip_session_join_t
Definition acds.h:408
acip_error_t
Definition acds.h:1013
acip_error_code_t
ACIP error codes.
Definition acds.h:1073
acip_session_create_t
Definition acds.h:226
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:581
@ PACKET_TYPE_ACIP_ERROR
Generic error response (Discovery Server -> Client).
Definition packet.h:547
int socket_t
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
🔤 String Manipulation and Shell Escaping Utilities