aboutsummaryrefslogtreecommitdiff
path: root/src/d_tcp_win.c
diff options
context:
space:
mode:
authorbonmas14 <bonmas14@gmail.com>2025-09-25 14:17:07 +0300
committerbonmas14 <bonmas14@gmail.com>2025-09-25 14:17:07 +0300
commit166877cf15f6afa89c9f8a61e267d485868d0ee1 (patch)
tree9f080314edc4fb293f91227cdc1c01cfec8d7bae /src/d_tcp_win.c
parent8ebdc95621bc61fdf3c98cd7ae4ddca67398df23 (diff)
downloadungrateful-166877cf15f6afa89c9f8a61e267d485868d0ee1.tar.gz
ungrateful-166877cf15f6afa89c9f8a61e267d485868d0ee1.zip
+disgrace and rework of ungrateful.h
Diffstat (limited to 'src/d_tcp_win.c')
-rw-r--r--src/d_tcp_win.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/d_tcp_win.c b/src/d_tcp_win.c
new file mode 100644
index 0000000..e033153
--- /dev/null
+++ b/src/d_tcp_win.c
@@ -0,0 +1,78 @@
+
+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()
+
+}