DDraceNetwork Documentation
Loading...
Searching...
No Matches
system.cpp File Reference
#include "system.h"
#include "lock.h"
#include "logger.h"
#include "windows.h"
#include <sys/types.h>
#include <atomic>
#include <cctype>
#include <charconv>
#include <chrono>
#include <cinttypes>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <mutex>
#include <string_view>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <unistd.h>
#include <csignal>
#include <locale>
#include <arpa/inet.h>
#include <dirent.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <cerrno>
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 ASYNC_BUFSIZE   (8 * 1024)
#define ASYNC_LOCAL_BUFSIZE   (64 * 1024)
#define str_format   str_format_opt
#define B(i, j)
#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)
IOHANDLE io_open (const char *filename, int flags)
unsigned io_read (IOHANDLE io, void *buffer, unsigned size)
bool io_read_all (IOHANDLE io, void **result, unsigned *result_len)
char * io_read_all_str (IOHANDLE io)
int io_skip (IOHANDLE io, int64_t size)
int io_seek (IOHANDLE io, int64_t offset, ESeekOrigin origin)
int64_t io_tell (IOHANDLE io)
int64_t 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)
static void netaddr_to_sockaddr_in (const NETADDR *src, sockaddr_in *dest)
static void netaddr_to_sockaddr_in6 (const NETADDR *src, sockaddr_in6 *dest)
static void sockaddr_to_netaddr (const sockaddr *src, socklen_t src_len, 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)
void net_addr_str (const NETADDR *addr, char *string, int max_length, bool add_port)
static int priv_net_extract (const char *hostname, char *host, int max_host, int *port)
static int net_host_lookup_fallback (const char *hostname, NETADDR *addr, int types, int port)
static 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)
bool net_addr_is_local (const NETADDR *addr)
int net_addr_from_str (NETADDR *addr, const char *string)
static void priv_net_close_socket (int sock)
static void priv_net_close_all_sockets (NETSOCKET sock)
static int priv_net_create_socket (int domain, int type, const NETADDR *bindaddr)
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)
void 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)
void net_tcp_close (NETSOCKET sock)
int net_errno ()
std::string net_error_message ()
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_executable_path (char *buffer, int buffer_size)
int fs_makedir_rec_for (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)
void fs_normalize_path (char *path)
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, std::chrono::nanoseconds nanoseconds)
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,...)
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)
void net_stats (NETSTATS *stats_inout)
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, const char **arguments, const size_t num_arguments)
int kill_process (PROCESS process)
bool is_process_alive (PROCESS process)
int open_link (const char *link)
int open_file (const char *path)
static void ensure_secure_random_init ()
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)
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)

Variables

static NETSTATS network_stats = {0}
static NETSOCKET_INTERNAL invalid_socket = {NETTYPE_INVALID, -1, -1, -1, -1}
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 = {}

Macro Definition Documentation

◆ ASYNC_BUFSIZE

#define ASYNC_BUFSIZE   (8 * 1024)

◆ ASYNC_LOCAL_BUFSIZE

#define ASYNC_LOCAL_BUFSIZE   (64 * 1024)

◆ B

#define B ( i,
j )
Value:
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()

void aio_handle_free_and_unlock ( ASYNCIO * aio)
static

◆ aio_thread()

void aio_thread ( void * user)
static

◆ buffer_len()

unsigned int buffer_len ( ASYNCIO * aio)
static

◆ buffer_ptrs()

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

◆ ensure_secure_random_init()

void ensure_secure_random_init ( )
static

◆ find_next_power_of_two_minus_one()

unsigned int find_next_power_of_two_minus_one ( unsigned int n)
static

◆ min3()

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

◆ 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_host_lookup_fallback()

int net_host_lookup_fallback ( const char * hostname,
NETADDR * addr,
int types,
int port )
static

◆ net_host_lookup_impl()

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

◆ net_set_blocking()

int net_set_blocking ( NETSOCKET sock)

Make a socket block on operations.

Parameters
sockThe socket to set the mode on.
Returns
0 on success.

◆ net_set_blocking_impl()

int net_set_blocking_impl ( NETSOCKET sock,
bool blocking )
static

◆ net_stats()

void net_stats ( NETSTATS * stats_inout)

◆ netaddr_to_sockaddr_in()

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

◆ netaddr_to_sockaddr_in6()

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

◆ next_buffer_size()

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

◆ parse_char()

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

◆ parse_int()

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

◆ parse_uint16()

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

◆ parse_uint8()

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

◆ priv_net_close_all_sockets()

void priv_net_close_all_sockets ( NETSOCKET sock)
static

◆ priv_net_close_socket()

void priv_net_close_socket ( int sock)
static

◆ priv_net_create_socket()

int priv_net_create_socket ( int domain,
int type,
const NETADDR * bindaddr )
static

◆ priv_net_extract()

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

◆ sockaddr_to_netaddr()

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

◆ str_format_int()

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

◆ str_to_utf32_unchecked()

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

◆ 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 null-terminated strings.

◆ 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()

void * thread_run ( void * user)
static

◆ 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

◆ invalid_socket

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

◆ network_stats

NETSTATS network_stats = {0}
static

◆ secure_random_data

struct SECURE_RANDOM_DATA secure_random_data = {}
static