DDraceNetwork Docs
system.cpp File Reference
#include <atomic>
#include <cctype>
#include <charconv>
#include <chrono>
#include <cinttypes>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iterator>
#include <sstream>
#include <string_view>
#include "lock.h"
#include "logger.h"
#include "system.h"
#include <sys/types.h>
#include <csignal>
#include <locale>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <cerrno>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <dirent.h>
Include dependency graph for system.cpp:

Classes

struct  NETSOCKET_BUFFER
 
struct  NETSOCKET_INTERNAL
 
struct  ASYNCIO
 
struct  BUFFERS
 
struct  THREAD_RUN
 
struct  SECURE_RANDOM_DATA
 

Macros

#define VLEN   128
 
#define PACKETSIZE   1400
 
#define AF_WEBSOCKET_INET   (0xee)
 
#define ASYNC_BUFSIZE   (8 * 1024)
 
#define ASYNC_LOCAL_BUFSIZE   (64 * 1024)
 
#define str_format   str_format_opt
 
#define B(i, j)   buf[((j)&1) * (a_len + 1) + (i)]
 
#define MAX_PASSWORD_LENGTH   128
 

Enumerations

enum  { ASYNCIO_RUNNING , ASYNCIO_CLOSE , ASYNCIO_EXIT }
 

Functions

void net_buffer_init (NETSOCKET_BUFFER *buffer)
 
void net_buffer_reinit (NETSOCKET_BUFFER *buffer)
 
void net_buffer_simple (NETSOCKET_BUFFER *buffer, char **buf, int *size)
 
bool dbg_assert_has_failed ()
 
void dbg_assert_imp (const char *filename, int line, bool test, const char *msg)
 
void dbg_break ()
 
void dbg_assert_set_handler (DBG_ASSERT_HANDLER handler)
 
void dbg_msg (const char *sys, const char *fmt,...)
 
void mem_copy (void *dest, const void *source, size_t size)
 
void mem_move (void *dest, const void *source, size_t size)
 
int mem_comp (const void *a, const void *b, size_t size)
 
bool mem_has_null (const void *block, size_t size)
 
IOHANDLE io_open (const char *filename, int flags)
 
unsigned io_read (IOHANDLE io, void *buffer, unsigned size)
 
void io_read_all (IOHANDLE io, void **result, unsigned *result_len)
 
char * io_read_all_str (IOHANDLE io)
 
int io_skip (IOHANDLE io, int size)
 
int io_seek (IOHANDLE io, int offset, int origin)
 
long int io_tell (IOHANDLE io)
 
long int io_length (IOHANDLE io)
 
unsigned io_write (IOHANDLE io, const void *buffer, unsigned size)
 
bool io_write_newline (IOHANDLE io)
 
int io_close (IOHANDLE io)
 
int io_flush (IOHANDLE io)
 
int io_sync (IOHANDLE io)
 
int io_error (IOHANDLE io)
 
IOHANDLE io_stdin ()
 
IOHANDLE io_stdout ()
 
IOHANDLE io_stderr ()
 
IOHANDLE io_current_exe ()
 
static void buffer_ptrs (ASYNCIO *aio, struct BUFFERS *buffers)
 
static void aio_handle_free_and_unlock (ASYNCIO *aio)
 
static void aio_thread (void *user)
 
ASYNCIOaio_new (IOHANDLE io)
 
static unsigned int buffer_len (ASYNCIO *aio)
 
static unsigned int next_buffer_size (unsigned int cur_size, unsigned int need_size)
 
void aio_lock (ASYNCIO *aio)
 
void aio_unlock (ASYNCIO *aio)
 
void aio_write_unlocked (ASYNCIO *aio, const void *buffer, unsigned size)
 
void aio_write (ASYNCIO *aio, const void *buffer, unsigned size)
 
void aio_write_newline_unlocked (ASYNCIO *aio)
 
void aio_write_newline (ASYNCIO *aio)
 
int aio_error (ASYNCIO *aio)
 
void aio_close (ASYNCIO *aio)
 
void aio_wait (ASYNCIO *aio)
 
void aio_free (ASYNCIO *aio)
 
static void * thread_run (void *user)
 
void * thread_init (void(*threadfunc)(void *), void *u, const char *name)
 
void thread_wait (void *thread)
 
void thread_yield ()
 
void thread_detach (void *thread)
 
void thread_init_and_detach (void(*threadfunc)(void *), void *u, const char *name)
 
void sphore_init (SEMAPHORE *sem)
 
void sphore_wait (SEMAPHORE *sem)
 
void sphore_signal (SEMAPHORE *sem)
 
void sphore_destroy (SEMAPHORE *sem)
 
void set_new_tick ()
 
int64_t time_get_impl ()
 
int64_t time_get ()
 
int64_t time_freq ()
 
static void netaddr_to_sockaddr_in (const NETADDR *src, struct sockaddr_in *dest)
 
static void netaddr_to_sockaddr_in6 (const NETADDR *src, struct sockaddr_in6 *dest)
 
static void sockaddr_to_netaddr (const struct sockaddr *src, NETADDR *dst)
 
int net_addr_comp (const NETADDR *a, const NETADDR *b)
 
int net_addr_comp_noport (const NETADDR *a, const NETADDR *b)
 
void net_addr_str_v6 (const unsigned short ip[8], int port, char *buffer, int buffer_size)
 
bool net_addr_str (const NETADDR *addr, char *string, int max_length, int add_port)
 
static int priv_net_extract (const char *hostname, char *host, int max_host, int *port)
 
int net_host_lookup_impl (const char *hostname, NETADDR *addr, int types)
 
int net_host_lookup (const char *hostname, NETADDR *addr, int types)
 
static int parse_int (int *out, const char **str)
 
static int parse_char (char c, const char **str)
 
static int parse_uint8 (unsigned char *out, const char **str)
 
static int parse_uint16 (unsigned short *out, const char **str)
 
int net_addr_from_url (NETADDR *addr, const char *string, char *host_buf, size_t host_buf_size)
 
int net_addr_from_str (NETADDR *addr, const char *string)
 
static void priv_net_close_socket (int sock)
 
static int priv_net_close_all_sockets (NETSOCKET sock)
 
static int priv_net_create_socket (int domain, int type, struct sockaddr *addr, int sockaddrlen)
 
int net_socket_type (NETSOCKET sock)
 
NETSOCKET net_udp_create (NETADDR bindaddr)
 
int net_udp_send (NETSOCKET sock, const NETADDR *addr, const void *data, int size)
 
int net_udp_recv (NETSOCKET sock, NETADDR *addr, unsigned char **data)
 
int net_udp_close (NETSOCKET sock)
 
NETSOCKET net_tcp_create (NETADDR bindaddr)
 
static int net_set_blocking_impl (NETSOCKET sock, bool blocking)
 
int net_set_non_blocking (NETSOCKET sock)
 
int net_set_blocking (NETSOCKET sock)
 
int net_tcp_listen (NETSOCKET sock, int backlog)
 
int net_tcp_accept (NETSOCKET sock, NETSOCKET *new_sock, NETADDR *a)
 
int net_tcp_connect (NETSOCKET sock, const NETADDR *a)
 
int net_tcp_connect_non_blocking (NETSOCKET sock, NETADDR bindaddr)
 
int net_tcp_send (NETSOCKET sock, const void *data, int size)
 
int net_tcp_recv (NETSOCKET sock, void *data, int maxsize)
 
int net_tcp_close (NETSOCKET sock)
 
int net_errno ()
 
int net_would_block ()
 
void net_init ()
 
UNIXSOCKET net_unix_create_unnamed ()
 
int net_unix_send (UNIXSOCKET sock, UNIXSOCKETADDR *addr, void *data, int size)
 
void net_unix_set_addr (UNIXSOCKETADDR *addr, const char *path)
 
void net_unix_close (UNIXSOCKET sock)
 
void fs_listdir (const char *dir, FS_LISTDIR_CALLBACK cb, int type, void *user)
 
void fs_listdir_fileinfo (const char *dir, FS_LISTDIR_CALLBACK_FILEINFO cb, int type, void *user)
 
int fs_storage_path (const char *appname, char *path, int max)
 
int fs_makedir_rec_for (const char *path)
 
int fs_makedir (const char *path)
 
int fs_removedir (const char *path)
 
int fs_is_file (const char *path)
 
int fs_is_dir (const char *path)
 
int fs_is_relative_path (const char *path)
 
int fs_chdir (const char *path)
 
char * fs_getcwd (char *buffer, int buffer_size)
 
const char * fs_filename (const char *path)
 
void fs_split_file_extension (const char *filename, char *name, size_t name_size, char *extension, size_t extension_size)
 
int fs_parent_dir (char *path)
 
int fs_remove (const char *filename)
 
int fs_rename (const char *oldname, const char *newname)
 
int fs_file_time (const char *name, time_t *created, time_t *modified)
 
void swap_endian (void *data, unsigned elem_size, unsigned num)
 
int net_socket_read_wait (NETSOCKET sock, int time)
 
int64_t time_timestamp ()
 
static struct tm * time_localtime_threadlocal (time_t *time_data)
 
int time_houroftheday ()
 
static bool time_iseasterday (time_t time_data, struct tm *time_info)
 
ETimeSeason time_season ()
 
void str_append (char *dst, const char *src, int dst_size)
 
int str_copy (char *dst, const char *src, int dst_size)
 
void str_utf8_truncate (char *dst, int dst_size, const char *src, int truncation_len)
 
void str_truncate (char *dst, int dst_size, const char *src, int truncation_len)
 
int str_length (const char *str)
 
int str_format_v (char *buffer, int buffer_size, const char *format, va_list args)
 
int str_format_int (char *buffer, size_t buffer_size, int value)
 
int str_format (char *buffer, int buffer_size, const char *format,...)
 
const char * str_trim_words (const char *str, int words)
 
bool str_has_cc (const char *str)
 
void str_sanitize_cc (char *str_in)
 
void str_sanitize (char *str_in)
 
void str_sanitize_filename (char *str_in)
 
void str_clean_whitespaces (char *str_in)
 
char * str_skip_to_whitespace (char *str)
 
const char * str_skip_to_whitespace_const (const char *str)
 
char * str_skip_whitespaces (char *str)
 
const char * str_skip_whitespaces_const (const char *str)
 
int str_comp_nocase (const char *a, const char *b)
 
int str_comp_nocase_num (const char *a, const char *b, int num)
 
int str_comp (const char *a, const char *b)
 
int str_comp_num (const char *a, const char *b, int num)
 
int str_comp_filenames (const char *a, const char *b)
 
const char * str_startswith_nocase (const char *str, const char *prefix)
 
const char * str_startswith (const char *str, const char *prefix)
 
const char * str_endswith_nocase (const char *str, const char *suffix)
 
const char * str_endswith (const char *str, const char *suffix)
 
static int min3 (int a, int b, int c)
 
int str_utf8_dist (const char *a, const char *b)
 
static int str_to_utf32_unchecked (const char *str, int **out)
 
int str_utf32_dist_buffer (const int *a, int a_len, const int *b, int b_len, int *buf, int buf_len)
 
int str_utf8_dist_buffer (const char *a_utf8, const char *b_utf8, int *buf, int buf_len)
 
const char * str_find_nocase (const char *haystack, const char *needle)
 
const char * str_find (const char *haystack, const char *needle)
 
bool str_delimiters_around_offset (const char *haystack, const char *delim, int offset, int *start, int *end)
 
const char * str_rchr (const char *haystack, char needle)
 
int str_countchr (const char *haystack, char needle)
 
void str_hex (char *dst, int dst_size, const void *data, int data_size)
 
void str_hex_cstyle (char *dst, int dst_size, const void *data, int data_size, int bytes_per_line)
 
static int hexval (char x)
 
static int byteval (const char *hex, unsigned char *dst)
 
int str_hex_decode (void *dst, int dst_size, const char *src)
 
void str_base64 (char *dst, int dst_size, const void *data_raw, int data_size)
 
static int base64_digit_value (char digit)
 
int str_base64_decode (void *dst_raw, int dst_size, const char *data)
 
void str_timestamp_ex (time_t time_data, char *buffer, int buffer_size, const char *format)
 
void str_timestamp_format (char *buffer, int buffer_size, const char *format)
 
void str_timestamp (char *buffer, int buffer_size)
 
bool timestamp_from_str (const char *string, const char *format, time_t *timestamp)
 
int str_time (int64_t centisecs, int format, char *buffer, int buffer_size)
 
int str_time_float (float secs, int format, char *buffer, int buffer_size)
 
void str_escape (char **dst, const char *src, const char *end)
 
void net_stats (NETSTATS *stats_inout)
 
int str_isspace (char c)
 
char str_uppercase (char c)
 
bool str_isnum (char c)
 
int str_isallnum (const char *str)
 
int str_isallnum_hex (const char *str)
 
int str_toint (const char *str)
 
bool str_toint (const char *str, int *out)
 
int str_toint_base (const char *str, int base)
 
unsigned long str_toulong_base (const char *str, int base)
 
int64_t str_toint64_base (const char *str, int base)
 
float str_tofloat (const char *str)
 
bool str_tofloat (const char *str, float *out)
 
int str_utf8_comp_nocase (const char *a, const char *b)
 
int str_utf8_comp_nocase_num (const char *a, const char *b, int num)
 
const char * str_utf8_find_nocase (const char *haystack, const char *needle, const char **end)
 
int str_utf8_isspace (int code)
 
const char * str_utf8_skip_whitespaces (const char *str)
 
void str_utf8_trim_right (char *param)
 
int str_utf8_isstart (char c)
 
int str_utf8_rewind (const char *str, int cursor)
 
int str_utf8_fix_truncation (char *str)
 
int str_utf8_forward (const char *str, int cursor)
 
int str_utf8_encode (char *ptr, int chr)
 
static unsigned char str_byte_next (const char **ptr)
 
static void str_byte_rewind (const char **ptr)
 
int str_utf8_decode (const char **ptr)
 
int str_utf8_check (const char *str)
 
void str_utf8_copy_num (char *dst, const char *src, int dst_size, int num)
 
void str_utf8_stats (const char *str, size_t max_size, size_t max_count, size_t *size, size_t *count)
 
size_t str_utf8_offset_bytes_to_chars (const char *str, size_t byte_offset)
 
size_t str_utf8_offset_chars_to_bytes (const char *str, size_t char_offset)
 
unsigned str_quickhash (const char *str)
 
static const char * str_token_get (const char *str, const char *delim, int *length)
 
int str_in_list (const char *list, const char *delim, const char *needle)
 
const char * str_next_token (const char *str, const char *delim, char *buffer, int buffer_size)
 
unsigned bytes_be_to_uint (const unsigned char *bytes)
 
void uint_to_bytes_be (unsigned char *bytes, unsigned value)
 
int pid ()
 
void cmdline_fix (int *argc, const char ***argv)
 
void cmdline_free (int argc, const char **argv)
 
PROCESS shell_execute (const char *file, EShellExecuteWindowState window_state)
 
int kill_process (PROCESS process)
 
bool is_process_alive (PROCESS process)
 
int open_link (const char *link)
 
int open_file (const char *path)
 
int secure_random_init ()
 
int secure_random_uninit ()
 
void generate_password (char *buffer, unsigned length, const unsigned short *random, unsigned random_length)
 
void secure_random_password (char *buffer, unsigned length, unsigned pw_length)
 
void secure_random_fill (void *bytes, unsigned length)
 
int secure_rand ()
 
static unsigned int find_next_power_of_two_minus_one (unsigned int n)
 
int secure_rand_below (int below)
 
bool os_version_str (char *version, size_t length)
 
void os_locale_str (char *locale, size_t length)
 
std::chrono::nanoseconds time_get_nanoseconds ()
 
int net_socket_read_wait (NETSOCKET sock, std::chrono::nanoseconds nanoseconds)
 

Variables

static NETSTATS network_stats = {0}
 
static NETSOCKET_INTERNAL invalid_socket = {NETTYPE_INVALID, -1, -1, -1}
 
std::atomic_bool dbg_assert_failing = false
 
DBG_ASSERT_HANDLER dbg_assert_handler
 
static int new_tick = -1
 
static const std::chrono::time_point< std::chrono::steady_clock > tw_start_time = std::chrono::steady_clock::now()
 
const NETADDR NETADDR_ZEROED = {NETTYPE_INVALID, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0}
 
static struct SECURE_RANDOM_DATA secure_random_data = {0}
 

Macro Definition Documentation

◆ AF_WEBSOCKET_INET

#define AF_WEBSOCKET_INET   (0xee)

◆ ASYNC_BUFSIZE

#define ASYNC_BUFSIZE   (8 * 1024)

◆ ASYNC_LOCAL_BUFSIZE

#define ASYNC_LOCAL_BUFSIZE   (64 * 1024)

◆ B

#define B (   i,
 
)    buf[((j)&1) * (a_len + 1) + (i)]

◆ MAX_PASSWORD_LENGTH

#define MAX_PASSWORD_LENGTH   128

◆ PACKETSIZE

#define PACKETSIZE   1400

◆ str_format

#define str_format   str_format_opt

◆ VLEN

#define VLEN   128

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
ASYNCIO_RUNNING 
ASYNCIO_CLOSE 
ASYNCIO_EXIT 

Function Documentation

◆ aio_handle_free_and_unlock()

static void aio_handle_free_and_unlock ( ASYNCIO aio)
static

◆ aio_thread()

static void aio_thread ( void *  user)
static

◆ base64_digit_value()

static int base64_digit_value ( char  digit)
static

◆ buffer_len()

static unsigned int buffer_len ( ASYNCIO aio)
static

◆ buffer_ptrs()

static void buffer_ptrs ( ASYNCIO aio,
struct BUFFERS buffers 
)
static

◆ bytes_be_to_uint()

unsigned bytes_be_to_uint ( const unsigned char *  bytes)

Packs 4 big endian bytes into an unsigned.

Parameters
bytesPointer to an array of bytes that will be packed.
Returns
The packed unsigned.
Remarks
Assumes the passed array is least 4 bytes in size.
Assumes unsigned is 4 bytes in size.
See also
uint_to_bytes_be

◆ byteval()

static int byteval ( const char *  hex,
unsigned char *  dst 
)
static

◆ dbg_assert_imp()

void dbg_assert_imp ( const char *  filename,
int  line,
bool  test,
const char *  msg 
)

◆ dbg_assert_set_handler()

void dbg_assert_set_handler ( DBG_ASSERT_HANDLER  handler)

◆ find_next_power_of_two_minus_one()

static unsigned int find_next_power_of_two_minus_one ( unsigned int  n)
static

◆ hexval()

static int hexval ( char  x)
static

◆ min3()

static int min3 ( int  a,
int  b,
int  c 
)
static

◆ net_addr_from_str()

int net_addr_from_str ( NETADDR addr,
const char *  string 
)

Turns string into a network address.

Parameters
addrAddress to fill in.
stringString to parse.
Returns
0 on success

◆ net_addr_from_url()

int net_addr_from_url ( NETADDR addr,
const char *  string,
char *  host_buf,
size_t  host_buf_size 
)

Turns url string into a network address struct. The url format is tw-0.6+udp://{ipaddr}[:{port}] ipaddr: can be ipv4 or ipv6 port: is a optional internet protocol port

This format is used for parsing the master server, be careful before changing it.

Examples: tw-0.6+udp://127.0.0.1 tw-0.6+udp://127.0.0.1:8303

Parameters
addrAddress to fill in.
stringString to parse.
host_bufPointer to a buffer to write the host to It will include the port if one is included in the url It can also be set to NULL then it will be ignored
host_buf_sizeSize of the host buffer or 0 if no host_buf pointer is given
Returns
0 on success, positive if the input wasn't a valid DDNet URL, negative if the input is a valid DDNet URL but the host part was not a valid IPv4/IPv6 address

◆ net_addr_str_v6()

void net_addr_str_v6 ( const unsigned short  ip[8],
int  port,
char *  buffer,
int  buffer_size 
)

◆ net_buffer_init()

void net_buffer_init ( NETSOCKET_BUFFER buffer)

◆ net_buffer_reinit()

void net_buffer_reinit ( NETSOCKET_BUFFER buffer)

◆ net_buffer_simple()

void net_buffer_simple ( NETSOCKET_BUFFER buffer,
char **  buf,
int *  size 
)

◆ net_errno()

int net_errno ( )

◆ net_host_lookup()

int net_host_lookup ( const char *  hostname,
NETADDR addr,
int  types 
)

◆ net_host_lookup_impl()

int net_host_lookup_impl ( const char *  hostname,
NETADDR addr,
int  types 
)

◆ net_set_blocking()

int net_set_blocking ( NETSOCKET  sock)

◆ net_set_blocking_impl()

static int net_set_blocking_impl ( NETSOCKET  sock,
bool  blocking 
)
static

◆ net_set_non_blocking()

int net_set_non_blocking ( NETSOCKET  sock)

◆ net_socket_read_wait() [1/2]

int net_socket_read_wait ( NETSOCKET  sock,
int  time 
)

◆ net_socket_read_wait() [2/2]

int net_socket_read_wait ( NETSOCKET  sock,
std::chrono::nanoseconds  nanoseconds 
)

◆ net_socket_type()

int net_socket_type ( NETSOCKET  sock)

◆ net_stats()

void net_stats ( NETSTATS stats_inout)

◆ net_tcp_connect_non_blocking()

int net_tcp_connect_non_blocking ( NETSOCKET  sock,
NETADDR  bindaddr 
)

◆ net_udp_create()

NETSOCKET net_udp_create ( NETADDR  bindaddr)

◆ net_udp_recv()

int net_udp_recv ( NETSOCKET  sock,
NETADDR addr,
unsigned char **  data 
)

◆ net_would_block()

int net_would_block ( )

◆ netaddr_to_sockaddr_in()

static void netaddr_to_sockaddr_in ( const NETADDR src,
struct sockaddr_in *  dest 
)
static

◆ netaddr_to_sockaddr_in6()

static void netaddr_to_sockaddr_in6 ( const NETADDR src,
struct sockaddr_in6 *  dest 
)
static

◆ next_buffer_size()

static unsigned int next_buffer_size ( unsigned int  cur_size,
unsigned int  need_size 
)
static

◆ parse_char()

static int parse_char ( char  c,
const char **  str 
)
static

◆ parse_int()

static int parse_int ( int *  out,
const char **  str 
)
static

◆ parse_uint16()

static int parse_uint16 ( unsigned short *  out,
const char **  str 
)
static

◆ parse_uint8()

static int parse_uint8 ( unsigned char *  out,
const char **  str 
)
static

◆ priv_net_close_all_sockets()

static int priv_net_close_all_sockets ( NETSOCKET  sock)
static

◆ priv_net_close_socket()

static void priv_net_close_socket ( int  sock)
static

◆ priv_net_create_socket()

static int priv_net_create_socket ( int  domain,
int  type,
struct sockaddr *  addr,
int  sockaddrlen 
)
static

◆ priv_net_extract()

static int priv_net_extract ( const char *  hostname,
char *  host,
int  max_host,
int *  port 
)
static

◆ sockaddr_to_netaddr()

static void sockaddr_to_netaddr ( const struct sockaddr *  src,
NETADDR dst 
)
static

◆ str_base64()

void str_base64 ( char *  dst,
int  dst_size,
const void *  data_raw,
int  data_size 
)

◆ str_base64_decode()

int str_base64_decode ( void *  dst_raw,
int  dst_size,
const char *  data 
)

◆ str_byte_next()

static unsigned char str_byte_next ( const char **  ptr)
static

◆ str_byte_rewind()

static void str_byte_rewind ( const char **  ptr)
static

◆ str_endswith()

const char * str_endswith ( const char *  str,
const char *  suffix 
)

◆ str_endswith_nocase()

const char * str_endswith_nocase ( const char *  str,
const char *  suffix 
)

◆ str_escape()

void str_escape ( char **  dst,
const char *  src,
const char *  end 
)

◆ str_find()

const char * str_find ( const char *  haystack,
const char *  needle 
)

◆ str_find_nocase()

const char * str_find_nocase ( const char *  haystack,
const char *  needle 
)

◆ str_format_int()

int str_format_int ( char *  buffer,
size_t  buffer_size,
int  value 
)

◆ str_hex()

void str_hex ( char *  dst,
int  dst_size,
const void *  data,
int  data_size 
)

Takes a datablock and generates a hex string of it, with spaces between bytes.

Parameters
dstBuffer to fill with hex data.
dst_sizeSize of the buffer (at least 3 * data_size + 1 to contain all data).
dataData to turn into hex.
data_sizeSize of the data.
Remarks
The destination buffer will be zero-terminated.

◆ str_hex_cstyle()

void str_hex_cstyle ( char *  dst,
int  dst_size,
const void *  data,
int  data_size,
int  bytes_per_line = 12 
)

Takes a datablock and generates a hex string of it, in the C style array format, i.e. with bytes formatted in 0x00-0xFF notation and commas with spaces between the bytes. The output can be split over multiple lines by specifying the maximum number of bytes that should be printed per line.

Parameters
dstBuffer to fill with hex data.
dst_sizeSize of the buffer (at least 6 * data_size + 1 to contain all data).
dataData to turn into hex.
data_sizeSize of the data.
bytes_per_lineAfter this many printed bytes a newline will be printed.
Remarks
The destination buffer will be zero-terminated.

◆ str_hex_decode()

int str_hex_decode ( void *  dst,
int  dst_size,
const char *  src 
)

◆ str_in_list()

int str_in_list ( const char *  list,
const char *  delim,
const char *  needle 
)

◆ str_isallnum()

int str_isallnum ( const char *  str)

◆ str_isallnum_hex()

int str_isallnum_hex ( const char *  str)

◆ str_isnum()

bool str_isnum ( char  c)

◆ str_next_token()

const char * str_next_token ( const char *  str,
const char *  delim,
char *  buffer,
int  buffer_size 
)

◆ str_quickhash()

unsigned str_quickhash ( const char *  str)

◆ str_sanitize_filename()

void str_sanitize_filename ( char *  str)

Replaces all invalid filename characters with whitespace.

Parameters
strString to sanitize.
Remarks
The strings are treated as zero-terminated strings.

◆ str_startswith_nocase()

const char * str_startswith_nocase ( const char *  str,
const char *  prefix 
)

◆ str_time()

int str_time ( int64_t  centisecs,
int  format,
char *  buffer,
int  buffer_size 
)

◆ str_time_float()

int str_time_float ( float  secs,
int  format,
char *  buffer,
int  buffer_size 
)

◆ str_timestamp()

void str_timestamp ( char *  buffer,
int  buffer_size 
)

◆ str_timestamp_ex()

void str_timestamp_ex ( time_t  time_data,
char *  buffer,
int  buffer_size,
const char *  format 
)

◆ str_timestamp_format()

void str_timestamp_format ( char *  buffer,
int  buffer_size,
const char *  format 
)

◆ str_to_utf32_unchecked()

static int str_to_utf32_unchecked ( const char *  str,
int **  out 
)
static

◆ str_tofloat() [1/2]

float str_tofloat ( const char *  str)

◆ str_tofloat() [2/2]

bool str_tofloat ( const char *  str,
float *  out 
)

◆ str_toint() [1/2]

int str_toint ( const char *  str)

◆ str_toint() [2/2]

bool str_toint ( const char *  str,
int *  out 
)

◆ str_toint64_base()

int64_t str_toint64_base ( const char *  str,
int  base 
)

◆ str_toint_base()

int str_toint_base ( const char *  str,
int  base 
)

◆ str_token_get()

static const char * str_token_get ( const char *  str,
const char *  delim,
int *  length 
)
static

◆ str_toulong_base()

unsigned long str_toulong_base ( const char *  str,
int  base 
)

◆ str_uppercase()

char str_uppercase ( char  c)

◆ str_utf32_dist_buffer()

int str_utf32_dist_buffer ( const int *  a,
int  a_len,
const int *  b,
int  b_len,
int *  buf,
int  buf_len 
)

◆ str_utf8_check()

int str_utf8_check ( const char *  str)

◆ str_utf8_comp_nocase()

int str_utf8_comp_nocase ( const char *  a,
const char *  b 
)

◆ str_utf8_comp_nocase_num()

int str_utf8_comp_nocase_num ( const char *  a,
const char *  b,
int  num 
)

◆ str_utf8_copy_num()

void str_utf8_copy_num ( char *  dst,
const char *  src,
int  dst_size,
int  num 
)

◆ str_utf8_decode()

int str_utf8_decode ( const char **  ptr)

◆ str_utf8_dist()

int str_utf8_dist ( const char *  a,
const char *  b 
)

Computes the edit distance between two strings.

Parameters
aFirst string for the edit distance.
bSecond string for the edit distance.
Returns
The edit distance between the both strings.
Remarks
The strings are treated as zero-terminated strings.

◆ str_utf8_encode()

int str_utf8_encode ( char *  ptr,
int  chr 
)

◆ str_utf8_find_nocase()

const char * str_utf8_find_nocase ( const char *  haystack,
const char *  needle,
const char **  end 
)

◆ str_utf8_fix_truncation()

int str_utf8_fix_truncation ( char *  str)

◆ str_utf8_forward()

int str_utf8_forward ( const char *  str,
int  cursor 
)

◆ str_utf8_isspace()

int str_utf8_isspace ( int  code)

◆ str_utf8_isstart()

int str_utf8_isstart ( char  c)

◆ str_utf8_offset_bytes_to_chars()

size_t str_utf8_offset_bytes_to_chars ( const char *  str,
size_t  byte_offset 
)

Converts a byte offset of a utf8 string to the utf8 character offset.

Parameters
textPointer to the string.
byte_offsetOffset in bytes.
Returns
Offset in utf8 characters. Clamped to the maximum length of the string in utf8 characters.
Remarks
The string is treated as a zero-terminated utf8 string.
It's the user's responsibility to make sure the bounds are aligned.

◆ str_utf8_offset_chars_to_bytes()

size_t str_utf8_offset_chars_to_bytes ( const char *  str,
size_t  char_offset 
)

Converts a utf8 character offset of a utf8 string to the byte offset.

Parameters
textPointer to the string.
char_offsetOffset in utf8 characters.
Returns
Offset in bytes. Clamped to the maximum length of the string in bytes.
Remarks
The string is treated as a zero-terminated utf8 string.
It's the user's responsibility to make sure the bounds are aligned.

◆ str_utf8_rewind()

int str_utf8_rewind ( const char *  str,
int  cursor 
)

◆ str_utf8_skip_whitespaces()

const char * str_utf8_skip_whitespaces ( const char *  str)

◆ str_utf8_stats()

void str_utf8_stats ( const char *  str,
size_t  max_size,
size_t  max_count,
size_t *  size,
size_t *  count 
)

◆ str_utf8_trim_right()

void str_utf8_trim_right ( char *  param)

◆ swap_endian()

void swap_endian ( void *  data,
unsigned  elem_size,
unsigned  num 
)

Swaps the endianness of data. Each element is swapped individually by reversing its bytes.

Parameters
dataPointer to data to be swapped.
elem_sizeSize in bytes of each element.
numNumber of elements.
Remarks
The caller must ensure that the data is at least elem_size * num bytes in size.

◆ thread_run()

static void * thread_run ( void *  user)
static

◆ time_iseasterday()

static bool time_iseasterday ( time_t  time_data,
struct tm *  time_info 
)
static

◆ time_localtime_threadlocal()

static struct tm * time_localtime_threadlocal ( time_t *  time_data)
static

◆ timestamp_from_str()

bool timestamp_from_str ( const char *  string,
const char *  format,
time_t *  timestamp 
)

Parses a string into a timestamp following a specified format.

Parameters
stringPointer to the string to parse
formatThe time format to use (for example FORMAT_NOSPACE below)
timestampPointer to the timestamp result
Returns
true on success, false if the string could not be parsed with the specified format

◆ uint_to_bytes_be()

void uint_to_bytes_be ( unsigned char *  bytes,
unsigned  value 
)

Packs an unsigned into 4 big endian bytes.

Parameters
bytesPointer to an array where the bytes will be stored.
valueThe values that will be packed into the array.
Remarks
Assumes the passed array is least 4 bytes in size.
Assumes unsigned is 4 bytes in size.
See also
bytes_be_to_uint

Variable Documentation

◆ dbg_assert_failing

std::atomic_bool dbg_assert_failing = false

◆ dbg_assert_handler

DBG_ASSERT_HANDLER dbg_assert_handler

◆ invalid_socket

NETSOCKET_INTERNAL invalid_socket = {NETTYPE_INVALID, -1, -1, -1}
static

◆ NETADDR_ZEROED

const NETADDR NETADDR_ZEROED = {NETTYPE_INVALID, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0}

◆ network_stats

NETSTATS network_stats = {0}
static

◆ new_tick

int new_tick = -1
static

◆ secure_random_data

struct SECURE_RANDOM_DATA secure_random_data = {0}
static

◆ tw_start_time

const std::chrono::time_point<std::chrono::steady_clock> tw_start_time = std::chrono::steady_clock::now()
static