From 30751aece04ba8954513bbb7aca996244e6fa684 Mon Sep 17 00:00:00 2001 From: bonmas14 Date: Sun, 28 Sep 2025 17:53:03 +0000 Subject: changes --- src/cynic.h | 8 ++--- src/d_linux.c | 56 ++++++++++++++++++++++++++++++++++ src/d_tcp_win.c | 78 ----------------------------------------------- src/d_udp_win.c | 9 ------ src/d_win.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/disgrace.c | 44 ++++++++------------------- src/disgrace.h | 2 +- src/ungrateful.c | 5 +++ src/ungrateful.h | 4 +++ 9 files changed, 175 insertions(+), 123 deletions(-) create mode 100644 src/d_linux.c delete mode 100644 src/d_tcp_win.c delete mode 100644 src/d_udp_win.c create mode 100644 src/d_win.c (limited to 'src') diff --git a/src/cynic.h b/src/cynic.h index 5380b84..ef56555 100644 --- a/src/cynic.h +++ b/src/cynic.h @@ -34,6 +34,8 @@ extern "C" { #endif +extern void cyn_init(void); + /* ---- Library loading API ---- */ /* ------- Threading API ------- */ /* -------- Logging API -------- */ @@ -111,15 +113,13 @@ typedef enum { CYN_FILE_OPEN_EXISTING = 1 << 9, } File_Open_Flags; -typedef struct File_Handle File_Handle; - // extern File_Handle *cyn_file_open(String path, File_Open_Flags flags, File_Status *status); -extern File_Handle *cyn_file_open(String path, File_Status *status); +extern struct File_Handle *cyn_file_open(String path, File_Status *status); /* path - could be partial or full, should be always with '/' * status - is optional, * returns NULL on error and writes error into a `status`. * */ -extern void cyn_file_close(File_Handle *handle); +extern void cyn_file_close(struct File_Handle *handle); /* --------- Config API -------- */ diff --git a/src/d_linux.c b/src/d_linux.c new file mode 100644 index 0000000..5ff6abd --- /dev/null +++ b/src/d_linux.c @@ -0,0 +1,56 @@ +typedef struct DHandle { + u64 type; +} DHandle; + +b32 d_init(void) { + return true; +} + +String d_hostname_ip_get(String host, Allocator alloc) { + char *host_cstr; + Allocator talloc; + int err; + String ip; + + talloc = un_alloc_temp_get(); + host_cstr = (char *) un_string_to_cstring(host, talloc); + + // @note, host_info can return multiple ip addresses? + struct hostent *host_info = gethostbyname(host_cstr); + + if (!host_info) { + err = h_errno; + + switch (err) { + case TRY_AGAIN: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (TRY_AGAIN)."); + break; + case HOST_NOT_FOUND: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (HOST_NOT_FOUND)."); + break; + case NO_DATA: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: no host data (NO_DATA)."); + break; + default: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: unspecified error."); + break; + } + + return CLITERAL(String) { 0 }; + } + + if (host_info) { + assert(host_info->h_addrtype == AF_INET); + + assert(host_info->h_length >= (s32)sizeof(struct in_addr)); + ip = un_string_from_cstring(inet_ntoa(*(struct in_addr *)*host_info->h_addr_list)); + + return un_string_copy(ip, alloc); + } + + return CLITERAL(String) { 0 }; +} + +DHandle* d_tcp_connect(String ip, u16 port) { + +} diff --git a/src/d_tcp_win.c b/src/d_tcp_win.c deleted file mode 100644 index e033153..0000000 --- a/src/d_tcp_win.c +++ /dev/null @@ -1,78 +0,0 @@ - -extern String d_get_hostname_ip(String host, Allocator alloc) { - char *host_cstr; - Allocator talloc; - int err; - String ip; - - talloc = un_alloc_temp_get(); - host_cstr = (char *) un_string_to_cstring(host, talloc); - - // @note, host_info can return multiple ip addresses? - HOSTENT *host_info = gethostbyname(host_cstr); - - if (!host_info) { - - - err = WSAGetLastError(); - - switch (err) { - case WSANOTINITIALISED: - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: disgrace wasn't initialized, run d_init() before this call."); - break; - case WSAENETDOWN: - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: network error."); - break; - case WSATRY_AGAIN: - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (WSATRY_AGAIN)."); - break; - case WSAHOST_NOT_FOUND: - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (WSAHOST_NOT_FOUND)."); - break; - case WSANO_DATA: - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: no host data (WSA_NO_DATA)."); - break; - case WSAEFAULT: - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host name is not valid."); - break; - default: - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: unspecified error."); - break; - } - - return CLITERAL(String) {}; - } - - if (host_info) { - assert(host_info->h_addrtype == AF_INET); - - assert(host_info->h_length >= sizeof(struct in_addr)); - ip = un_string_from_cstring(inet_ntoa(*(struct in_addr *)*host_info->h_addr_list)); - - return un_string_copy(ip, alloc); - } - - return CLITERAL(String) {}; -} - - -DHandle* d_tcp_connect(String ip, u16 port) { - SOCKET sock; - struct sockaddr_in addr_info; - char *ip_addr_cstr; - - ip_addr_cstr = (char *) un_string_to_cstring(ip, un_alloc_temp_get()); - - sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - - if (sock == INVALID_SOCKET) { - return NULL; - } - - addr_info.sin_family = AF_INET; - addr_info.sin_addr.s_addr = inet_addr(ip_addr_cstr); - addr_info.sin_port = htons(port); - - //connect() - -} diff --git a/src/d_udp_win.c b/src/d_udp_win.c deleted file mode 100644 index e3c0074..0000000 --- a/src/d_udp_win.c +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/src/d_win.c b/src/d_win.c new file mode 100644 index 0000000..3375ee9 --- /dev/null +++ b/src/d_win.c @@ -0,0 +1,92 @@ +typedef struct DHandle { + u64 type; +} DHandle; + +b32 d_init(void) { + WSADATA data; + int error; + + if (WSAStartup(MAKEWORD(2, 2), &data)) { + error = WSAGetLastError(); + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "WsaStartup failed, code: %d.", (s64)error); + return false; + } + + return true; +} + +extern String d_hostname_ip_get(String host, Allocator alloc) { + char *host_cstr; + Allocator talloc; + int err; + String ip; + + talloc = un_alloc_temp_get(); + host_cstr = (char *) un_string_to_cstring(host, talloc); + + // @note, host_info can return multiple ip addresses? + HOSTENT *host_info = gethostbyname(host_cstr); + + if (!host_info) { + err = WSAGetLastError(); + + switch (err) { + case WSANOTINITIALISED: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: disgrace wasn't initialized, run d_init() before this call."); + break; + case WSAENETDOWN: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: network error."); + break; + case WSATRY_AGAIN: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (WSATRY_AGAIN)."); + break; + case WSAHOST_NOT_FOUND: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host not found (WSAHOST_NOT_FOUND)."); + break; + case WSANO_DATA: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: no host data (WSA_NO_DATA)."); + break; + case WSAEFAULT: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: host name is not valid."); + break; + default: + cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "d_get_hostname_ip: unspecified error."); + break; + } + + return CLITERAL(String) { 0 }; + } + + if (host_info) { + assert(host_info->h_addrtype == AF_INET); + + assert(host_info->h_length >= sizeof(struct in_addr)); + ip = un_string_from_cstring(inet_ntoa(*(struct in_addr *)*host_info->h_addr_list)); + + return un_string_copy(ip, alloc); + } + + return CLITERAL(String) { 0 }; +} + + +DHandle* d_tcp_connect(String ip, u16 port) { + SOCKET sock; + struct sockaddr_in addr_info; + char *ip_addr_cstr; + + ip_addr_cstr = (char *) un_string_to_cstring(ip, un_alloc_temp_get()); + + sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + + if (sock == INVALID_SOCKET) { + return NULL; + } + + addr_info.sin_family = AF_INET; + addr_info.sin_addr.s_addr = inet_addr(ip_addr_cstr); + addr_info.sin_port = htons(port); + + //connect() + +} diff --git a/src/disgrace.c b/src/disgrace.c index b5e5666..424ca5b 100644 --- a/src/disgrace.c +++ b/src/disgrace.c @@ -1,53 +1,35 @@ #include "disgrace.h" +enum { + D_TYPE_TCP, + D_TYPE_UDP +}; + #if defined(OS_WINDOWS) #include +#include #include #if defined(MSVC_COMPILER) #pragma comment(lib, "Ws2_32.lib") #endif -enum { - D_TYPE_TCP, - D_TYPE_UDP -}; - -typedef struct DHandle { - u64 type; -} DHandle; - -b32 d_init(void) { - WSADATA data; - int error; - - if (WSAStartup(MAKEWORD(2, 2), &data)) { - error = WSAGetLastError(); - cyn_log_write_cstring(CYN_LOG_ERROR, (u8*) "WsaStartup failed, code: %d.", (s64)error); - return false; - } - - return true; -} - - -#include "d_tcp_win.c" -#include "d_udp_win.c" +#include "d_win.c" #elif defined(OS_LINUX) -b32 d_init(void) { - return true; -} +#include +#include +#include +#include +#include -// #error "Linux todo" -// #include "d_udp_linux.c" +#include "d_linux.c" #else #error "unknown platform" - #endif diff --git a/src/disgrace.h b/src/disgrace.h index 43f78fb..d129938 100644 --- a/src/disgrace.h +++ b/src/disgrace.h @@ -38,7 +38,7 @@ struct DHandle; b32 d_init(void); -extern String d_get_hostname_ip(String host, Allocator alloc); +extern String d_hostname_ip_get(String host, Allocator alloc); extern struct DHandle* d_tcp_connect(String ip, u16 port); extern struct DHandle* d_tcp_listen(); diff --git a/src/ungrateful.c b/src/ungrateful.c index 5031a45..96c9648 100644 --- a/src/ungrateful.c +++ b/src/ungrateful.c @@ -14,3 +14,8 @@ #include "un_vec.c" #include "un_vecd.c" + + +void un_init(u64 size_of_temp_mem) { + un_alloc_temp_init(size_of_temp_mem); +} diff --git a/src/ungrateful.h b/src/ungrateful.h index f9b2234..a3918aa 100644 --- a/src/ungrateful.h +++ b/src/ungrateful.h @@ -171,6 +171,10 @@ typedef struct double4 { double x, y, z, w; } double4; #define un_vec_unwrap3(vec) (vec).x, (vec).y, (vec).z #define un_vec_unwrap4(vec) (vec).x, (vec).y, (vec).z, (vec).w +/* ---- Default ---- */ + +extern void un_init(u64 size_of_temp_mem); + /* ---- Memory Allocators API ---- */ typedef enum { -- cgit v1.2.3-70-g09d2