Added additional functions for windows-specific actions

This commit is contained in:
2024-02-11 23:22:39 -05:00
parent 4c256d8800
commit c8bc17fbce
2 changed files with 65 additions and 14 deletions

View File

@@ -1,7 +1,29 @@
#include "easysock.hpp"
#ifndef _WIN_32
const int INVALID_SOCKET = -1;
#endif
/* Internal functions, to be used if platform is windows */
int sock_init(void) {
#ifdef _WIN32
WSADATA wsa_data;
return WSAStartup(MAKEWORD(1,1), &wsa_data);
#else
return 0;
#endif
}
int sock_quit(void) {
#ifdef _WIN32
return WSACleanup();
#else
return 0;
#endif
}
int create_socket(int network, char transport) {
int create_socket(int network, char transport) {
sock_init();
int domain;
int type;
@@ -127,6 +149,21 @@ int create_remote (int network,char transport,char* address,int port,struct sock
return socket;
}
/* sock_close - Closes the given socket */
int sock_close(SOCKET sock) {
int status = 0;
#ifdef _WIN32
status = shutdown(sock, SD_BOTH);
if (status == 0) { status = closesocket(sock); }
#else
status = shutdown(sock, SHUT_RDWR);
if (status == 0) { status = close(sock); }
#endif
return status;
}
int check_ip_ver(const char* address) {
char buffer[16]; /* 16 chars - 128 bits - is enough to hold an ipv6 address */