ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
util/validation.h
Go to the documentation of this file.
1
32#pragma once
33
34#include "../common.h"
35#include <stdint.h>
36#include <stddef.h>
37
38/* Forward declarations to avoid circular dependencies */
40
41/* Function declaration - defined in src/server/protocol.c */
42void disconnect_client_for_bad_data(client_info_t *client, const char *format, ...);
43
48#define VALIDATE_NOTNULL_DATA(client, data, packet_name) \
49 do { \
50 if (!(data)) { \
51 disconnect_client_for_bad_data((client), "%s payload missing", (packet_name)); \
52 return; \
53 } \
54 } while (0)
55
60#define VALIDATE_MIN_SIZE(client, len, min_size, packet_name) \
61 do { \
62 if ((len) < (min_size)) { \
63 disconnect_client_for_bad_data((client), "%s payload too small (len=%zu, min=%zu)", (packet_name), (len), \
64 (min_size)); \
65 return; \
66 } \
67 } while (0)
68
73#define VALIDATE_EXACT_SIZE(client, len, expected_size, packet_name) \
74 do { \
75 if ((len) != (expected_size)) { \
76 disconnect_client_for_bad_data((client), "%s payload size mismatch (len=%zu, expected=%zu)", (packet_name), \
77 (len), (expected_size)); \
78 return; \
79 } \
80 } while (0)
81
86#define VALIDATE_AUDIO_STREAM_ENABLED(client, packet_name) \
87 do { \
88 if (!atomic_load(&(client)->is_sending_audio)) { \
89 disconnect_client_for_bad_data((client), "%s received before audio stream enabled", (packet_name)); \
90 return; \
91 } \
92 } while (0)
93
98#define VALIDATE_AUDIO_SAMPLE_COUNT(client, num_samples, max_samples, packet_name) \
99 do { \
100 if ((num_samples) <= 0 || (num_samples) > (max_samples)) { \
101 disconnect_client_for_bad_data((client), "%s invalid sample count: %d (max %d)", (packet_name), (num_samples), \
102 (max_samples)); \
103 return; \
104 } \
105 } while (0)
106
111#define VALIDATE_AUDIO_ALIGNMENT(client, len, sample_size, packet_name) \
112 do { \
113 if ((len) % (sample_size) != 0) { \
114 disconnect_client_for_bad_data((client), "%s payload not aligned (len=%zu, sample_size=%zu)", (packet_name), \
115 (len), (sample_size)); \
116 return; \
117 } \
118 } while (0)
119
124#define VALIDATE_RESOURCE_INITIALIZED(client, resource, resource_name) \
125 do { \
126 if (!(resource)) { \
127 disconnect_client_for_bad_data((client), "%s not initialized", (resource_name)); \
128 return; \
129 } \
130 } while (0)
131
136#define VALIDATE_PACKET_SIZE(client, data, len, expected_size, packet_name) \
137 do { \
138 if (!(data)) { \
139 disconnect_client_for_bad_data((client), packet_name " payload missing"); \
140 return; \
141 } \
142 if ((len) != (expected_size)) { \
143 disconnect_client_for_bad_data((client), packet_name " payload size %zu (expected %zu)", (len), \
144 (expected_size)); \
145 return; \
146 } \
147 } while (0)
148
153#define VALIDATE_NONZERO(client, value, value_name, packet_name) \
154 do { \
155 if ((value) == 0) { \
156 disconnect_client_for_bad_data((client), "%s %s cannot be zero", (packet_name), (value_name)); \
157 return; \
158 } \
159 } while (0)
160
165#define VALIDATE_RANGE(client, value, min_val, max_val, value_name, packet_name) \
166 do { \
167 if ((value) < (min_val) || (value) > (max_val)) { \
168 disconnect_client_for_bad_data((client), "%s %s out of range: %u (valid: %u-%u)", (packet_name), (value_name), \
169 (unsigned)(value), (unsigned)(min_val), (unsigned)(max_val)); \
170 return; \
171 } \
172 } while (0)
173
178#define VALIDATE_CAPABILITY_FLAGS(client, flags, valid_mask, packet_name) \
179 do { \
180 if (((flags) & (valid_mask)) == 0) { \
181 disconnect_client_for_bad_data((client), "%s no valid capability flags set (flags=0x%x, valid=0x%x)", \
182 (packet_name), (unsigned)(flags), (unsigned)(valid_mask)); \
183 return; \
184 } \
185 } while (0)
186
191#define VALIDATE_FLAGS_MASK(client, flags, valid_mask, packet_name) \
192 do { \
193 if (((flags) & ~(valid_mask)) != 0) { \
194 disconnect_client_for_bad_data((client), "%s unknown flags set (flags=0x%x, valid=0x%x)", (packet_name), \
195 (unsigned)(flags), (unsigned)(valid_mask)); \
196 return; \
197 } \
198 } while (0)
199
204#define VALIDATE_PACKET_NOT_NULL(client, data, packet_name) \
205 ({ \
206 int _validation_failed = 0; \
207 if (!(data)) { \
208 disconnect_client_for_bad_data((client), packet_name " payload missing"); \
209 _validation_failed = 1; \
210 } \
211 _validation_failed; \
212 })
213
216/* ============================================================================
217 * Image Dimension Validation (Function Implementation)
218 * ============================================================================ */
219
void disconnect_client_for_bad_data(client_info_t *client, const char *format,...)
Per-client state structure for server-side client management.