ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
util/validation.h
Go to the documentation of this file.
1
31
32#pragma once
33
34#include "../common.h"
35#include <stddef.h>
36#include <stdint.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* Forward declarations to avoid circular dependencies */
43typedef struct client_info client_info_t;
44
45/* Function declaration - defined in src/server/protocol.c */
46void disconnect_client_for_bad_data(client_info_t *client, const char *format,
47 ...);
48
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())
61
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())
75
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())
89
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())
102
107#define VALIDATE_AUDIO_SAMPLE_COUNT(client, num_samples, max_samples, \
108 packet_name) \
109 do { \
110 if ((num_samples) <= 0 || (num_samples) > (max_samples)) { \
111 disconnect_client_for_bad_data( \
112 (client), "%s invalid sample count: %d (max %d)", (packet_name), \
113 (num_samples), (max_samples)); \
114 return; \
115 } \
116 } while (asciichat_macro_block_continue())
117
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())
131
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())
144
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())
163
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())
176
181#define VALIDATE_RANGE(client, value, min_val, max_val, value_name, \
182 packet_name) \
183 do { \
184 if ((value) < (min_val) || (value) > (max_val)) { \
185 disconnect_client_for_bad_data( \
186 (client), "%s %s out of range: %u (valid: %u-%u)", (packet_name), \
187 (value_name), (unsigned)(value), (unsigned)(min_val), \
188 (unsigned)(max_val)); \
189 return; \
190 } \
191 } while (asciichat_macro_block_continue())
192
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())
207
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())
221
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))
231
233
234/* ============================================================================
235 * Image Dimension Validation (Function Implementation)
236 * ============================================================================
237 */
238
244
246
247#ifdef __cplusplus
248}
249#endif
struct client_info client_info_t
void disconnect_client_for_bad_data(client_info_t *client, const char *format,...)
⚙️ Common definitions, error codes, macros, and types shared throughout the application