ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
Validation Helpers

Reusable validation macros for protocol handlers. More...

Topics

 Image Validation Functions

Files

file  validation.h
 Common validation macros to reduce duplication in protocol handlers.

Macros

#define VALIDATE_NOTNULL_DATA(client, data, packet_name)
#define VALIDATE_MIN_SIZE(client, len, min_size, packet_name)
#define VALIDATE_EXACT_SIZE(client, len, expected_size, packet_name)
#define VALIDATE_AUDIO_STREAM_ENABLED(client, packet_name)
#define VALIDATE_AUDIO_SAMPLE_COUNT(client, num_samples, max_samples, packet_name)
#define VALIDATE_AUDIO_ALIGNMENT(client, len, sample_size, packet_name)
#define VALIDATE_RESOURCE_INITIALIZED(client, resource, resource_name)
#define VALIDATE_PACKET_SIZE(client, data, len, expected_size, packet_name)
#define VALIDATE_NONZERO(client, value, value_name, packet_name)
#define VALIDATE_RANGE(client, value, min_val, max_val, value_name, packet_name)
#define VALIDATE_CAPABILITY_FLAGS(client, flags, valid_mask, packet_name)
#define VALIDATE_FLAGS_MASK(client, flags, valid_mask, packet_name)
#define VALIDATE_PACKET_NOT_NULL(client, data, packet_name)

Typedefs

typedef struct client_info client_info_t

Functions

void disconnect_client_for_bad_data (client_info_t *client, const char *format,...)

Detailed Description

Reusable validation macros for protocol handlers.

Provides standardized validation macros for protocol handlers to:

  • Check for NULL pointers
  • Validate payload sizes
  • Verify client state flags
  • Reduce code duplication across handlers

All macros call disconnect_client_for_bad_data() on validation failure and return from the calling function immediately.

Usage:

void handle_packet(client_info_t *client, const void *data, size_t len) {
VALIDATE_NOTNULL_DATA(client, data, "PACKET_TYPE");
VALIDATE_MIN_SIZE(client, len, 16, "PACKET_TYPE");
VALIDATE_AUDIO_STREAM_ENABLED(client, "PACKET_TYPE");
// ... continue processing ...
}
struct client_info client_info_t
#define VALIDATE_AUDIO_STREAM_ENABLED(client, packet_name)
#define VALIDATE_MIN_SIZE(client, len, min_size, packet_name)
#define VALIDATE_NOTNULL_DATA(client, data, packet_name)

Macro Definition Documentation

◆ VALIDATE_AUDIO_ALIGNMENT

#define VALIDATE_AUDIO_ALIGNMENT ( client,
len,
sample_size,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if ((len) % (sample_size) != 0) { \
disconnect_client_for_bad_data( \
(client), "%s payload not aligned (len=%zu, sample_size=%zu)", \
(packet_name), (len), (sample_size)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that audio sample alignment is correct (must be multiple of sample size). Disconnects client and returns if alignment is incorrect.

Definition at line 122 of file util/validation.h.

122#define VALIDATE_AUDIO_ALIGNMENT(client, len, sample_size, packet_name) \
123 do { \
124 if ((len) % (sample_size) != 0) { \
125 disconnect_client_for_bad_data( \
126 (client), "%s payload not aligned (len=%zu, sample_size=%zu)", \
127 (packet_name), (len), (sample_size)); \
128 return; \
129 } \
130 } while (asciichat_macro_block_continue())

Referenced by handle_audio_packet().

◆ VALIDATE_AUDIO_SAMPLE_COUNT

#define VALIDATE_AUDIO_SAMPLE_COUNT ( client,
num_samples,
max_samples,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if ((num_samples) <= 0 || (num_samples) > (max_samples)) { \
disconnect_client_for_bad_data( \
(client), "%s invalid sample count: %d (max %d)", (packet_name), \
(num_samples), (max_samples)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that audio sample count is within acceptable bounds. Disconnects client and returns if sample count is invalid.

Definition at line 107 of file util/validation.h.

Referenced by handle_audio_packet().

◆ VALIDATE_AUDIO_STREAM_ENABLED

#define VALIDATE_AUDIO_STREAM_ENABLED ( client,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if (!atomic_load_bool(&(client)->is_sending_audio)) { \
disconnect_client_for_bad_data( \
(client), "%s received before audio stream enabled", (packet_name)); \
return; \
} \
} while (asciichat_macro_block_continue())
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.

Validate that audio stream is enabled for this client. Disconnects client and returns if audio stream is not enabled.

Definition at line 94 of file util/validation.h.

94#define VALIDATE_AUDIO_STREAM_ENABLED(client, packet_name) \
95 do { \
96 if (!atomic_load_bool(&(client)->is_sending_audio)) { \
97 disconnect_client_for_bad_data( \
98 (client), "%s received before audio stream enabled", (packet_name)); \
99 return; \
100 } \
101 } while (asciichat_macro_block_continue())

Referenced by handle_audio_batch_packet(), handle_audio_opus_batch_packet(), and handle_audio_packet().

◆ VALIDATE_CAPABILITY_FLAGS

#define VALIDATE_CAPABILITY_FLAGS ( client,
flags,
valid_mask,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if (((flags) & (valid_mask)) == 0) { \
disconnect_client_for_bad_data( \
(client), \
"%s no valid capability flags set (flags=0x%x, valid=0x%x)", \
(packet_name), (unsigned)(flags), (unsigned)(valid_mask)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that at least one capability flag is set. Disconnects client and returns if all flags are zero or invalid.

Definition at line 197 of file util/validation.h.

197#define VALIDATE_CAPABILITY_FLAGS(client, flags, valid_mask, packet_name) \
198 do { \
199 if (((flags) & (valid_mask)) == 0) { \
200 disconnect_client_for_bad_data( \
201 (client), \
202 "%s no valid capability flags set (flags=0x%x, valid=0x%x)", \
203 (packet_name), (unsigned)(flags), (unsigned)(valid_mask)); \
204 return; \
205 } \
206 } while (asciichat_macro_block_continue())

Referenced by handle_client_join_packet(), handle_stream_start_packet(), and handle_stream_stop_packet().

◆ VALIDATE_EXACT_SIZE

#define VALIDATE_EXACT_SIZE ( client,
len,
expected_size,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if ((len) != (expected_size)) { \
disconnect_client_for_bad_data( \
(client), "%s payload size mismatch (len=%zu, expected=%zu)", \
(packet_name), (len), (expected_size)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that payload size is exactly the expected size. Disconnects client and returns if len != expected_size.

Definition at line 80 of file util/validation.h.

80#define VALIDATE_EXACT_SIZE(client, len, expected_size, packet_name) \
81 do { \
82 if ((len) != (expected_size)) { \
83 disconnect_client_for_bad_data( \
84 (client), "%s payload size mismatch (len=%zu, expected=%zu)", \
85 (packet_name), (len), (expected_size)); \
86 return; \
87 } \
88 } while (asciichat_macro_block_continue())

◆ VALIDATE_FLAGS_MASK

#define VALIDATE_FLAGS_MASK ( client,
flags,
valid_mask,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if (((flags) & ~(valid_mask)) != 0) { \
disconnect_client_for_bad_data( \
(client), "%s unknown flags set (flags=0x%x, valid=0x%x)", \
(packet_name), (unsigned)(flags), (unsigned)(valid_mask)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that only known/valid flags are set. Disconnects client and returns if unknown flags are present.

Definition at line 212 of file util/validation.h.

212#define VALIDATE_FLAGS_MASK(client, flags, valid_mask, packet_name) \
213 do { \
214 if (((flags) & ~(valid_mask)) != 0) { \
215 disconnect_client_for_bad_data( \
216 (client), "%s unknown flags set (flags=0x%x, valid=0x%x)", \
217 (packet_name), (unsigned)(flags), (unsigned)(valid_mask)); \
218 return; \
219 } \
220 } while (asciichat_macro_block_continue())

Referenced by handle_client_join_packet(), handle_stream_start_packet(), and handle_stream_stop_packet().

◆ VALIDATE_MIN_SIZE

#define VALIDATE_MIN_SIZE ( client,
len,
min_size,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if ((len) < (min_size)) { \
disconnect_client_for_bad_data( \
(client), "%s payload too small (len=%zu, min=%zu)", (packet_name), \
(len), (min_size)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that payload size is at least the minimum required. Disconnects client and returns if len < min_size.

Definition at line 66 of file util/validation.h.

66#define VALIDATE_MIN_SIZE(client, len, min_size, packet_name) \
67 do { \
68 if ((len) < (min_size)) { \
69 disconnect_client_for_bad_data( \
70 (client), "%s payload too small (len=%zu, min=%zu)", (packet_name), \
71 (len), (min_size)); \
72 return; \
73 } \
74 } while (asciichat_macro_block_continue())

Referenced by handle_audio_batch_packet().

◆ VALIDATE_NONZERO

#define VALIDATE_NONZERO ( client,
value,
value_name,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if ((value) == 0) { \
disconnect_client_for_bad_data((client), "%s %s cannot be zero", \
(packet_name), (value_name)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that a numeric value is non-zero (for dimensions, counts, etc). Disconnects client and returns if value is zero.

Definition at line 168 of file util/validation.h.

168#define VALIDATE_NONZERO(client, value, value_name, packet_name) \
169 do { \
170 if ((value) == 0) { \
171 disconnect_client_for_bad_data((client), "%s %s cannot be zero", \
172 (packet_name), (value_name)); \
173 return; \
174 } \
175 } while (asciichat_macro_block_continue())

Referenced by handle_audio_batch_packet(), handle_audio_opus_batch_packet(), and handle_size_packet().

◆ VALIDATE_NOTNULL_DATA

#define VALIDATE_NOTNULL_DATA ( client,
data,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if (!(data)) { \
disconnect_client_for_bad_data((client), "%s payload missing", \
(packet_name)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that payload data pointer is not NULL. Disconnects client and returns if data is NULL.

Definition at line 53 of file util/validation.h.

53#define VALIDATE_NOTNULL_DATA(client, data, packet_name) \
54 do { \
55 if (!(data)) { \
56 disconnect_client_for_bad_data((client), "%s payload missing", \
57 (packet_name)); \
58 return; \
59 } \
60 } while (asciichat_macro_block_continue())

Referenced by handle_audio_batch_packet(), handle_audio_opus_batch_packet(), and handle_audio_packet().

◆ VALIDATE_PACKET_NOT_NULL

#define VALIDATE_PACKET_NOT_NULL ( client,
data,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
((data) ? 0 \
packet_name " payload missing"), \
1))
void disconnect_client_for_bad_data(client_info_t *client, const char *format,...)

Validate packet payload pointer is not NULL. Returns true if validation failed (data is NULL), false if valid.

Definition at line 226 of file util/validation.h.

226#define VALIDATE_PACKET_NOT_NULL(client, data, packet_name) \
227 ((data) ? 0 \
228 : (disconnect_client_for_bad_data((client), \
229 packet_name " payload missing"), \
230 1))

Referenced by handle_audio_opus_packet().

◆ VALIDATE_PACKET_SIZE

#define VALIDATE_PACKET_SIZE ( client,
data,
len,
expected_size,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if (!(data)) { \
disconnect_client_for_bad_data((client), \
packet_name " payload missing"); \
return; \
} \
if ((len) != (expected_size)) { \
disconnect_client_for_bad_data( \
(client), packet_name " payload size %zu (expected %zu)", (len), \
(expected_size)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate packet payload size and presence. Checks if data pointer is non-NULL and payload matches expected size.

Definition at line 149 of file util/validation.h.

149#define VALIDATE_PACKET_SIZE(client, data, len, expected_size, packet_name) \
150 do { \
151 if (!(data)) { \
152 disconnect_client_for_bad_data((client), \
153 packet_name " payload missing"); \
154 return; \
155 } \
156 if ((len) != (expected_size)) { \
157 disconnect_client_for_bad_data( \
158 (client), packet_name " payload size %zu (expected %zu)", (len), \
159 (expected_size)); \
160 return; \
161 } \
162 } while (asciichat_macro_block_continue())

Referenced by handle_client_join_packet(), handle_size_packet(), handle_stream_start_packet(), and handle_stream_stop_packet().

◆ VALIDATE_RANGE

#define VALIDATE_RANGE ( client,
value,
min_val,
max_val,
value_name,
packet_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if ((value) < (min_val) || (value) > (max_val)) { \
disconnect_client_for_bad_data( \
(client), "%s %s out of range: %u (valid: %u-%u)", (packet_name), \
(value_name), (unsigned)(value), (unsigned)(min_val), \
(unsigned)(max_val)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that a numeric value is within a specified range (inclusive). Disconnects client and returns if value is outside range.

Definition at line 181 of file util/validation.h.

Referenced by handle_audio_opus_batch_packet(), and handle_size_packet().

◆ VALIDATE_RESOURCE_INITIALIZED

#define VALIDATE_RESOURCE_INITIALIZED ( client,
resource,
resource_name )

#include <include/ascii-chat/util/validation.h>

Value:
do { \
if (!(resource)) { \
disconnect_client_for_bad_data((client), "%s not initialized", \
(resource_name)); \
return; \
} \
} while (asciichat_macro_block_continue())

Validate that a required resource/buffer is initialized. Disconnects client and returns if resource is NULL.

Definition at line 136 of file util/validation.h.

136#define VALIDATE_RESOURCE_INITIALIZED(client, resource, resource_name) \
137 do { \
138 if (!(resource)) { \
139 disconnect_client_for_bad_data((client), "%s not initialized", \
140 (resource_name)); \
141 return; \
142 } \
143 } while (asciichat_macro_block_continue())

Referenced by handle_audio_opus_batch_packet(), and handle_audio_packet().

Typedef Documentation

◆ client_info_t

Function Documentation

◆ disconnect_client_for_bad_data()

void disconnect_client_for_bad_data ( client_info_t * client,
const char * format,
... )

#include <include/ascii-chat/util/validation.h>

Definition at line 167 of file server/protocol.c.

168 {
169 if (!client) {
170 return;
171 }
172
173 protocol_cleanup_thread_locals();
174
175 // Atomically set the disconnect flag and get the old value
176 // This ensures only one thread proceeds to disconnect (the first one)
177 bool already_requested =
179 if (already_requested) {
180 return; // Already disconnecting, don't do it again
181 }
182
183 char reason[BUFFER_SIZE_SMALL] = {0};
184 if (format) {
185 va_list args;
186 va_start(args, format);
187 safe_vsnprintf(reason, sizeof(reason), format, args);
188 va_end(args);
189 } else {
190 SAFE_STRNCPY(reason, "Protocol violation", sizeof(reason));
191 }
192
193 const char *reason_str = reason[0] != '\0' ? reason : "Protocol violation";
194 const char *client_id = client->client_id;
195
196 log_warn("Disconnecting client %s due to protocol violation: %s", client_id,
197 reason_str);
198 mutex_lock(&client->send_mutex);
199 if (client->transport)
201 mutex_unlock(&client->send_mutex);
202
203 log_debug("Setting active=false (client_id=%s, reason=%s)", client_id,
204 reason_str);
205 atomic_store_bool(&client->active, false);
206 atomic_store_bool(&client->shutting_down, true);
210}
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
bool atomic_exchange_bool(atomic_t *a, bool new_value)
Atomically exchange a boolean and return the old value.
#define BUFFER_SIZE_SMALL
Small buffer size (256 bytes).
#define SAFE_STRNCPY(dst, src, size)
@ ERROR_NETWORK_PROTOCOL
Definition error_codes.h:81
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
void network_connection_request_stop(network_connection_t *c)
action_args_t args
asciichat_error_t acip_send_error(acip_transport_t *transport, uint32_t error_code, const char *message)
Send error message packet.
Definition send.c:402
network_connection_t * network_connection
char client_id[MAX_CLIENT_ID_LEN]

References acip_send_error(), client_info::active, args, atomic_exchange_bool(), atomic_store_bool(), client_info::audio_render_thread_running, BUFFER_SIZE_SMALL, client_info::client_id, ERROR_NETWORK_PROTOCOL, log_debug, log_warn, client_info::network_connection, network_connection_request_stop(), client_info::protocol_disconnect_requested, SAFE_STRNCPY, client_info::send_mutex, client_info::shutting_down, client_info::transport, and client_info::video_render_thread_running.

Referenced by handle_audio_batch_packet(), handle_audio_opus_batch_packet(), handle_audio_opus_packet(), handle_client_capabilities_packet(), handle_client_join_packet(), handle_image_frame_h264_packet(), handle_image_frame_h265_packet(), handle_image_frame_packet(), handle_protocol_version_packet(), handle_remote_log_packet_from_client(), and process_decrypted_packet().