You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.4 KiB
C
107 lines
3.4 KiB
C
10 months ago
|
#ifdef __cplusplus
|
||
|
extern "C" {
|
||
|
#endif
|
||
|
#ifndef EASYSOCK_H_
|
||
|
#define EASYSOCK_H_
|
||
11 months ago
|
|
||
10 months ago
|
#ifdef _WIN32
|
||
11 months ago
|
#include <winsock2.h>
|
||
10 months ago
|
#include <winsock.h>
|
||
|
#include <ws2tcpip.h>
|
||
|
#endif
|
||
10 months ago
|
#ifdef __unix__
|
||
11 months ago
|
#include <sys/types.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <netdb.h>
|
||
10 months ago
|
#include <unistd.h>
|
||
11 months ago
|
#include <arpa/inet.h>
|
||
|
#endif
|
||
|
|
||
10 months ago
|
#ifndef _WIN32
|
||
11 months ago
|
typedef int SOCKET;
|
||
|
#endif
|
||
11 months ago
|
|
||
10 months ago
|
/* Constants that can be used in place of characters, when specifying
|
||
|
the transport layer protocol */
|
||
10 months ago
|
const char ES_TCP = 'T';
|
||
|
const char ES_UDP = 'U';
|
||
10 months ago
|
|
||
11 months ago
|
/* This function takes:
|
||
|
a layer 3 - network layer - integer, which must be '4' for IPv4
|
||
10 months ago
|
and 6 for IPv6;
|
||
11 months ago
|
a layer 4 - transport layer - character, which must be 'T' for
|
||
10 months ago
|
TCP or 'U' for UDP.
|
||
11 months ago
|
|
||
10 months ago
|
It creates a _blocking_ socket, and returns the created socket, or -1
|
||
|
if the socket creation failed.*/
|
||
11 months ago
|
|
||
10 months ago
|
SOCKET create_socket(int network, char transport);
|
||
11 months ago
|
|
||
|
|
||
|
/* This function fills in the sockaddr struct 'dest' based on the given information.
|
||
|
'network' is an integer that contains '4' for IPv4 or '6' for IPv6;
|
||
|
'address' is the address that is filled into the struct;
|
||
|
port is self-explanatory;
|
||
|
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'. */
|
||
|
|
||
10 months ago
|
int create_addr(int network, const char* address, int port,struct sockaddr* dest);
|
||
11 months ago
|
|
||
|
|
||
|
|
||
|
/* This function utilizes the above two functions; it creates the socket and
|
||
|
_binds_ the addresses. It is used for local sockets (server sockets). Parameters are
|
||
|
same as above.
|
||
|
|
||
|
It prints the error returned by 'bind' if something went wrong, and returns ( -1 * errno ).*/
|
||
|
|
||
10 months ago
|
SOCKET create_local (int network, char transport, const char* address, int port,struct sockaddr* addr_struct);
|
||
11 months ago
|
|
||
|
|
||
|
/* This function utilizes the same functions as 'create_local' but _connects_ to the
|
||
|
requested address. It is used for remote sockets (client sockets). The paramters are same
|
||
|
as above. This function needs an empty 'sockaddr *' structure passed to it, which it will fill.
|
||
|
|
||
|
If something goes wrong, this function returns with ( -1 * errno ). */
|
||
|
|
||
10 months ago
|
SOCKET create_remote (int network,char transport, const char* address,int port,struct sockaddr* remote_addr_struct);
|
||
11 months ago
|
|
||
|
/* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4),
|
||
|
IPv6 address (returns 6) or neither (returns -1). */
|
||
|
|
||
11 months ago
|
int check_ip_ver(const char* address);
|
||
11 months ago
|
|
||
10 months ago
|
/* port_to_num - Converts a string representing a port, into a numeric value.
|
||
|
Returns -1 if the string is not numeric, or exceeds the maximum port length.
|
||
|
Returns -2 if the string is lower than 1024, This serves as a warning, as ports less
|
||
|
than 1023 are reserved. */
|
||
|
int port_to_num(const char* port_str);
|
||
|
|
||
11 months ago
|
/* int_to_inet - Takes an int value (4 for IPv4, 6 for IPv6) and returns AF_INET or
|
||
|
AF_INET6 respectively. */
|
||
|
|
||
|
int int_to_inet(int network);
|
||
|
|
||
|
/* char_to_socktype - Takes a character that represents a transport-layer protocol
|
||
|
(currently only supports 'T' for TCP or 'U' for UDP - it returns -250 if
|
||
|
the given characters is neither of these) and return the appropriate SOCKTYPE value. */
|
||
|
|
||
|
int char_to_socktype(char transport);
|
||
|
|
||
|
/* inet_to_int - Takes an int value that corresponds to AF_INET or AF_INET6,
|
||
|
and returns the appropriate int value. */
|
||
|
int inet_to_int(int af_type);
|
||
|
|
||
11 months ago
|
/* sockQuit - Only for windows - Cleans up the socket */
|
||
|
int sock_quit(void);
|
||
|
|
||
|
/* sock_close - Closes the given socket */
|
||
|
int sock_close(SOCKET);
|
||
11 months ago
|
|
||
|
#endif
|
||
10 months ago
|
#ifdef __cplusplus
|
||
|
}
|
||
|
#endif
|