diff --git a/easysock.cpp b/easysock.cpp index ace9f79..450fd64 100644 --- a/easysock.cpp +++ b/easysock.cpp @@ -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 */ diff --git a/includes/easysock.hpp b/includes/easysock.hpp index fed9e0e..16a6264 100644 --- a/includes/easysock.hpp +++ b/includes/easysock.hpp @@ -1,25 +1,34 @@ #ifndef EASYSOCK_HPP_ #define EASYSOCK_HPP_ -#include -#include -#include -#include -#include -#include -#include -#include +#ifdef _WIN_32 + #include + #include +#else + #include + #include + #include + #include +#endif + +#include +#include +#include +#include +#ifndef _WIN_32 + typedef int SOCKET; +#endif /* This function takes: a layer 3 - network layer - integer, which must be '4' for IPv4 and 6 for IPv6; and a layer 4 - transport layer - character, which must be 'T' for -TCP or 'U' for UDP. +TCP or 'U' for UDP. It returns the created socket, or -1 if the socket creation failed.*/ -int create_socket(int network, char transport); +SOCKET create_socket(int network, char transport); /* This function fills in the sockaddr struct 'dest' based on the given information. @@ -31,7 +40,7 @@ and dest is a pointer to the sockaddr struct that will be filled in. The function returns with -202 if the network parameter contained neither '4' nor '6'. */ -int create_addr(int network, char* address, int port,struct sockaddr* dest); +SOCKET create_addr(int network, char* address, int port,struct sockaddr* dest); @@ -41,7 +50,7 @@ same as above. It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/ -int create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct); +SOCKET create_local (int network, char transport, char* address, int port,struct sockaddr* addr_struct); /* This function utilizes the same functions as 'create_local' but _connects_ to the @@ -50,7 +59,7 @@ as above. This function needs an empty 'sockaddr *' structure passed to it, whic If something goes wrong, this function returns with ( -1 * errno ). */ -int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct); +SOCKET create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct); /* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4), IPv6 address (returns 6) or neither (returns -1). */ @@ -72,5 +81,10 @@ int char_to_socktype(char transport); and returns the appropriate int value. */ int inet_to_int(int af_type); +/* sockQuit - Only for windows - Cleans up the socket */ +int sock_quit(void); + +/* sock_close - Closes the given socket */ +int sock_close(SOCKET); #endif