ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
acds_server.c
Go to the documentation of this file.
1
17#include "network/packet.h"
18#include "log/logging.h"
19#include "common.h"
20
21#include <string.h>
22
23// ============================================================================
24// Packet Validation
25// ============================================================================
26
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}
54
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}
72
73// ============================================================================
74// Error Response Helpers
75// ============================================================================
76
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
acip_session_join_t
acip_error_code_t
ACIP error codes.
#define ACIP_MAX_SESSION_STRING_LEN
Maximum session string length (e.g., "swift-river-mountain" = 20 chars)
acip_session_create_t
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:77
asciichat_error_t acip_server_validate_session_join(const acip_session_join_t *req)
Validate ACIP session join request.
Definition acds_server.c:55
asciichat_error_t acip_server_validate_session_create(const acip_session_create_t *req)
Validate ACIP session create request.
Definition acds_server.c:27
unsigned char uint8_t
Definition common.h:56
#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)
Definition error_codes.h:46
@ 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
int socket_t
Socket handle type (POSIX: int)
Definition socket.h:50
#define INVALID_SOCKET_VALUE
Invalid socket value (POSIX: -1)
Definition socket.h:52
📝 Logging API with multiple log levels and terminal output control
Packet protocol implementation with encryption and compression support.