Added additional functions for windows-specific actions
This commit is contained in:
37
easysock.cpp
37
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) {
|
||||
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 */
|
||||
|
@@ -1,15 +1,24 @@
|
||||
#ifndef EASYSOCK_HPP_
|
||||
#define EASYSOCK_HPP_
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#ifdef _WIN_32
|
||||
#include <winsock2.h>
|
||||
#include <Ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cerrno>
|
||||
|
||||
#ifndef _WIN_32
|
||||
typedef int SOCKET;
|
||||
#endif
|
||||
|
||||
/* This function takes:
|
||||
a layer 3 - network layer - integer, which must be '4' for IPv4
|
||||
@@ -19,7 +28,7 @@ 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
|
||||
|
Reference in New Issue
Block a user