diff options
author | bonmas14 <bonmas14@gmail.com> | 2025-09-28 17:53:03 +0000 |
---|---|---|
committer | bonmas14 <bonmas14@gmail.com> | 2025-09-28 17:53:03 +0000 |
commit | 30751aece04ba8954513bbb7aca996244e6fa684 (patch) | |
tree | c40dfac95dd3c0766656324c45465f0375061b5d /src | |
parent | 3ad95773ae4f963435003c260278177f1e22577e (diff) | |
download | ungrateful-30751aece04ba8954513bbb7aca996244e6fa684.tar.gz ungrateful-30751aece04ba8954513bbb7aca996244e6fa684.zip |
changes
Diffstat (limited to 'src')
-rw-r--r-- | src/cynic.h | 8 | ||||
-rw-r--r-- | src/d_linux.c | 56 | ||||
-rw-r--r-- | src/d_udp_win.c | 9 | ||||
-rw-r--r-- | src/d_win.c (renamed from src/d_tcp_win.c) | 24 | ||||
-rw-r--r-- | src/disgrace.c | 44 | ||||
-rw-r--r-- | src/disgrace.h | 2 | ||||
-rw-r--r-- | src/ungrateful.c | 5 | ||||
-rw-r--r-- | src/ungrateful.h | 4 |
8 files changed, 102 insertions, 50 deletions
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_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_tcp_win.c b/src/d_win.c index e033153..3375ee9 100644 --- a/src/d_tcp_win.c +++ b/src/d_win.c @@ -1,5 +1,21 @@ +typedef struct DHandle { + u64 type; +} DHandle; -extern String d_get_hostname_ip(String host, Allocator alloc) { +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; @@ -12,8 +28,6 @@ extern String d_get_hostname_ip(String host, Allocator alloc) { HOSTENT *host_info = gethostbyname(host_cstr); if (!host_info) { - - err = WSAGetLastError(); switch (err) { @@ -40,7 +54,7 @@ extern String d_get_hostname_ip(String host, Allocator alloc) { break; } - return CLITERAL(String) {}; + return CLITERAL(String) { 0 }; } if (host_info) { @@ -52,7 +66,7 @@ extern String d_get_hostname_ip(String host, Allocator alloc) { return un_string_copy(ip, alloc); } - return CLITERAL(String) {}; + return CLITERAL(String) { 0 }; } 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 <winsock2.h> +#include <ws2tcpip.h> #include <windows.h> #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 <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <netinet/in.h> +#include <arpa/inet.h> -// #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 { |