forked from EXTERNAL/bareiron

* Implement recv_buffer size limit and input validation * Add readLengthPrefixedData helper and refactor some read flows * Change error to be more accurate * Add newline to error message * fix long chat messages kicking clients * style nitpicks --------- Co-authored-by: p2r3 <p2r3@p2r3.com> Co-authored-by: p2r3 <41925384+p2r3@users.noreply.github.com>
50 lines
1.3 KiB
C
50 lines
1.3 KiB
C
#ifndef H_TOOLS
|
|
#define H_TOOLS
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "globals.h"
|
|
|
|
static inline int mod_abs (int a, int b) {
|
|
return ((a % b) + b) % b;
|
|
}
|
|
static inline int div_floor (int a, int b) {
|
|
return a % b < 0 ? (a - b) / b : a / b;
|
|
}
|
|
|
|
extern uint64_t total_bytes_received;
|
|
ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first);
|
|
ssize_t send_all (int client_fd, const void *buf, ssize_t len);
|
|
|
|
ssize_t writeByte (int client_fd, uint8_t byte);
|
|
ssize_t writeUint16 (int client_fd, uint16_t num);
|
|
ssize_t writeUint32 (int client_fd, uint32_t num);
|
|
ssize_t writeUint64 (int client_fd, uint64_t num);
|
|
ssize_t writeFloat (int client_fd, float num);
|
|
ssize_t writeDouble (int client_fd, double num);
|
|
|
|
uint8_t readByte (int client_fd);
|
|
uint16_t readUint16 (int client_fd);
|
|
int16_t readInt16 (int client_fd);
|
|
uint32_t readUint32 (int client_fd);
|
|
uint64_t readUint64 (int client_fd);
|
|
int64_t readInt64 (int client_fd);
|
|
float readFloat (int client_fd);
|
|
double readDouble (int client_fd);
|
|
|
|
ssize_t readLengthPrefixedData (int client_fd);
|
|
void readString (int client_fd);
|
|
void readStringN (int client_fd, uint32_t max_length);
|
|
|
|
uint32_t fast_rand ();
|
|
uint64_t splitmix64 (uint64_t state);
|
|
|
|
#ifdef ESP_PLATFORM
|
|
#include "esp_timer.h"
|
|
#define get_program_time esp_timer_get_time
|
|
#else
|
|
int64_t get_program_time ();
|
|
#endif
|
|
|
|
#endif
|