Added additional functions for windows-specific actions

master
Aadhavan Srinivasan 11 months ago
parent 4c256d8800
commit c8bc17fbce

@ -1,7 +1,29 @@
#include "easysock.hpp" #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 domain;
int type; int type;
@ -127,6 +149,21 @@ int create_remote (int network,char transport,char* address,int port,struct sock
return socket; 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) { int check_ip_ver(const char* address) {
char buffer[16]; /* 16 chars - 128 bits - is enough to hold an ipv6 address */ char buffer[16]; /* 16 chars - 128 bits - is enough to hold an ipv6 address */

@ -1,25 +1,34 @@
#ifndef EASYSOCK_HPP_ #ifndef EASYSOCK_HPP_
#define EASYSOCK_HPP_ #define EASYSOCK_HPP_
#include <string.h> #ifdef _WIN_32
#include <sys/types.h> #include <winsock2.h>
#include <sys/socket.h> #include <Ws2tcpip.h>
#include <netdb.h> #else
#include <arpa/inet.h> #include <sys/types.h>
#include <stdlib.h> #include <sys/socket.h>
#include <stdio.h> #include <netdb.h>
#include <errno.h> #include <arpa/inet.h>
#endif
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#ifndef _WIN_32
typedef int SOCKET;
#endif
/* This function takes: /* This function takes:
a layer 3 - network layer - integer, which must be '4' for IPv4 a layer 3 - network layer - integer, which must be '4' for IPv4
and 6 for IPv6; and and 6 for IPv6; and
a layer 4 - transport layer - character, which must be 'T' for 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.*/ 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. /* 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' The function returns with -202 if the network parameter contained neither '4'
nor '6'. */ 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 ).*/ 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 /* 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 ). */ 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), /* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4),
IPv6 address (returns 6) or neither (returns -1). */ IPv6 address (returns 6) or neither (returns -1). */
@ -72,5 +81,10 @@ int char_to_socktype(char transport);
and returns the appropriate int value. */ and returns the appropriate int value. */
int inet_to_int(int af_type); 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 #endif

Loading…
Cancel
Save