/***************************************************************************************************************:')

OSNet.h

Network includes, typedefs, defines and initialization.

Fabrice Le Bars

Created: 2007

Version status: Not finished

***************************************************************************************************************:)*/

// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use 
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
#	undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
#	undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)

#ifndef OSNET_H
#define OSNET_H

#include "OSTime.h"

/*
Debug macros specific to OSNet.
*/
#ifdef _DEBUG_MESSAGES_OSUTILS
#	define _DEBUG_MESSAGES_OSNET
#endif // _DEBUG_MESSAGES_OSUTILS

#ifdef _DEBUG_WARNINGS_OSUTILS
#	define _DEBUG_WARNINGS_OSNET
#endif // _DEBUG_WARNINGS_OSUTILS

#ifdef _DEBUG_ERRORS_OSUTILS
#	define _DEBUG_ERRORS_OSNET
#endif // _DEBUG_ERRORS_OSUTILS

#ifdef _DEBUG_MESSAGES_OSNET
#	define PRINT_DEBUG_MESSAGE_OSNET(params) PRINT_DEBUG_MESSAGE(params)
#else
#	define PRINT_DEBUG_MESSAGE_OSNET(params)
#endif // _DEBUG_MESSAGES_OSNET

#ifdef _DEBUG_WARNINGS_OSNET
#	define PRINT_DEBUG_WARNING_OSNET(params) PRINT_DEBUG_WARNING(params)
#else
#	define PRINT_DEBUG_WARNING_OSNET(params)
#endif // _DEBUG_WARNINGS_OSNET

#ifdef _DEBUG_ERRORS_OSNET
#	define PRINT_DEBUG_ERROR_OSNET(params) PRINT_DEBUG_ERROR(params)
#else
#	define PRINT_DEBUG_ERROR_OSNET(params)
#endif // _DEBUG_ERRORS_OSNET

#ifdef _WIN32
// The maximum number of sockets that an application can actually use is independent of 
// the number of sockets supported by a particular implementation. The maximum number of 
// sockets that a Windows Sockets application can use is determined at compile time by the
// manifest constant FD_SETSIZE. This value is used in constructing the FD_SET structures 
// used in select. The default value in Winsock2.h is 64.
//#define FD_SETSIZE 64
#else
//#define FD_SETSIZE 64
#endif // _WIN32

#ifdef _WIN32
#ifdef __BORLANDC__
// Disable some Borland C++ Builder warnings that happen in ws2tcpip.h.
#pragma warn -8004
#endif // __BORLANDC__
#include <winsock2.h>
#if (_WIN32_WINNT <= 0x0500)
#include <ws2tcpip.h>
#include <Wspiapi.h>
#else
#include <ws2tcpip.h>
#endif // (_WIN32_WINNT <= 0x0500)
//#include <iphlpapi.h>
#ifdef __BORLANDC__
// Restore the Borland C++ Builder warnings previously disabled for ws2tcpip.h.
#pragma warn .8004
#endif // __BORLANDC__
#else 
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif // _WIN32

#ifdef _WIN32
/*
Format a message corresponding to the last error in a system call related 
to network with Winsock (thread-safe).

char* buf : (INOUT) Valid pointer to a buffer that will receive the message.
int buflen : (IN) Size of the buffer in bytes.

Return : buf.
*/
inline char* WSAFormatLastErrorMsg(char* buf, int buflen)
{
	memset(buf, 0, (size_t)buflen);
	if (!FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS | 
		FORMAT_MESSAGE_MAX_WIDTH_MASK, // Remove the line break at the end. 
		// Note that there is still a dot and a space at the end.
		NULL,
		WSAGetLastError(),
		0,
		(LPTSTR)buf,
		(DWORD)buflen,
		NULL
		))
	{
#ifdef _MSC_VER
		sprintf_s(buf, (size_t)buflen, "FormatMessage failed. ");
#else
		snprintf(buf, (size_t)buflen, "FormatMessage failed. ");
		buf[buflen-1] = 0;
#endif // _MSC_VER
		return buf;
	}
	return buf;
}
#else
typedef int SOCKET;

#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)

#define SD_RECEIVE SHUT_RD
#define SD_SEND SHUT_WR
#define SD_BOTH SHUT_RDWR

#define closesocket(sock) close((sock))
#endif // _WIN32

// Specific macro used with PRINT_DEBUG_MESSAGE, PRINT_DEBUG_WARNING 
// or PRINT_DEBUG_ERROR to return the message corresponding to the last error 
// in a system call related to network (with Winsock for Windows) in the 
// current thread.
#ifdef _WIN32
#define WSAGetLastErrorMsg() WSAFormatLastErrorMsg(szLastErrMsg, LAST_ERROR_MSG_SIZE)
#else 
#define WSAGetLastErrorMsg GetLastErrorMsg
#endif // _WIN32

/*
Initialize the network. Must be called before the use of any function from OSTCPSock.c/.h 
or OSUDPSock.c/.h. Use ReleaseNet() to release the network at the end.

Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int InitNet(void)
{
#ifdef _WIN32
	WSADATA wsaData;

	// Initiate use of the Winsock DLL by a process.
	int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
	switch (iResult)
	{
	case EXIT_SUCCESS: 
		break;
#ifdef _DEBUG_ERRORS_OSNET
	case WSASYSNOTREADY: 
		PRINT_DEBUG_ERROR_OSNET(("InitNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"The underlying network subsystem is not ready for network communication. "));
		return EXIT_FAILURE;
	case WSAVERNOTSUPPORTED: 
		PRINT_DEBUG_ERROR_OSNET(("InitNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"The version of Windows Sockets support requested is not provided by this particular Windows Sockets implementation. "));
		return EXIT_FAILURE;
	case WSAEINPROGRESS: 
		PRINT_DEBUG_ERROR_OSNET(("InitNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"A blocking Windows Sockets 1.1 operation is in progress. "));
		return EXIT_FAILURE;
	case WSAEPROCLIM: 
		PRINT_DEBUG_ERROR_OSNET(("InitNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"A limit on the number of tasks supported by the Windows Sockets implementation has been reached. "));
		return EXIT_FAILURE;
	case WSAEFAULT: 
		PRINT_DEBUG_ERROR_OSNET(("InitNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"The lpWSAData parameter is not a valid pointer. "));
		return EXIT_FAILURE;
#endif // _DEBUG_ERRORS_OSNET
	default:
		PRINT_DEBUG_ERROR_OSNET(("InitNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"WSAStartup failed. "));
		return EXIT_FAILURE;
	}
#endif

	return EXIT_SUCCESS;
}

/*
Release the network (previously initialized by InitNet()).

Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int ReleaseNet(void)
{
#ifdef _WIN32
	// Terminate use of the Winsock 2 DLL (Ws2_32.dll).
	int iResult = WSACleanup();
	switch (iResult)
	{
	case EXIT_SUCCESS: 
		break;
#ifdef _DEBUG_ERRORS_OSNET
	case WSANOTINITIALISED: 
		PRINT_DEBUG_ERROR_OSNET(("ReleaseNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"A successful WSAStartup call must occur before using this function. "));
		return EXIT_FAILURE;
	case WSAENETDOWN: 
		PRINT_DEBUG_ERROR_OSNET(("ReleaseNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"The network subsystem has failed. "));
		return EXIT_FAILURE;
	case WSAEINPROGRESS: 
		PRINT_DEBUG_ERROR_OSNET(("ReleaseNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. "));
		return EXIT_FAILURE;
#endif // _DEBUG_ERRORS_OSNET
	default:
		PRINT_DEBUG_ERROR_OSNET(("ReleaseNet error (%s) : %s"
			"\n", 
			strtime_m(), 
			"WSACleanup failed. "));
		return EXIT_FAILURE;
	}
#endif

	return EXIT_SUCCESS;
}

//////////////////////////////////////////////////////////////////////////////////////////
// Portable functions that can be directly used.
//////////////////////////////////////////////////////////////////////////////////////////

// Note that struct sockaddr variables should be defined as struct sockaddr_storage (to be protocol independant) and then 
// cast to struct sockaddr when called in functions that need this type (especially when it is a pointer).

// If a send or receive operation times out on a socket, the socket state is indeterminate, and should not be used.

// If the pNodeName parameter points to a computer name, all permanent addresses for the computer that can be used as a source address are returned. 
// If the pNodeName parameter points to a string equal to "localhost", all loopback addresses on the local computer are returned. 
// If the pNodeName parameter contains an empty string, all registered addresses on the local computer are returned. 
// ppResult is a linked list.
// Do not set errno, use gai_strerror() for Linux (thread-safe?) or WSAGetLastErrorMsg() for Windows.
//int getaddrinfo(char* pNodeName, char* pServiceName, struct addrinfo* pHints, struct addrinfo** ppResult);

// Free the linked-list returned by getaddrinfo()
//void freeaddrinfo(struct addrinfo* pAi);

// Convert a socket address to a corresponding host/address and service/port.
// Use NI_MAXHOST and NI_MAXSERV for the size of host and serv.
// flags can be a combination of NI_NAMEREQD (a host name that cannot be resolved by DNS results in an error),
// NI_NOFQDN (return only the hostname part of the fully qualified domain name for local hosts)
// NI_NUMERICHOST (returns the numeric form of the host name instead of its name) 
// NI_NUMERICSERV (returns the port number of the service instead of its name)
// NI_DGRAM (indicates that the service is an UDP service rather than TCP)
// Do not set errno, use gai_strerror() for Linux (thread-safe?) or WSAGetLastErrorMsg() for Windows.
//int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags);

// Translate the error codes of getaddrinfo() and getnameinfo() to a string for Linux.
//const char* gai_strerror(int errcode);

/*
Create a socket for a specific protocol (TCP/IPv4, UDP/IPv4, TCP/IPv6 or UDP/IPv6 are the 
most common).
Used at the beginning to create a server or client application.
Often use the addrinfo results of a call to getaddrinfo().
InitNet() must be called before using any network function.

int af : (IN) Address family : AF_INET for IPv4, AF_INET6 for IPv6. Often set to the ai_family 
field of an addrinfo element of the linked list returned by getaddrinfo().
int type : (IN) Socket type : SOCK_STREAM for TCP, SOCK_DGRAM for UDP. Often set to the ai_socktype 
field of an addrinfo element of the linked list returned by getaddrinfo().
int protocol : (IN) Protocol to be used : IPPROTO_TCP for TCP, IPPROTO_UDP for UDP. Often set to the ai_protocol 
field of an addrinfo element of the linked list returned by getaddrinfo().

Return : The socket created or INVALID_SOCKET if there is an error.
*/
//SOCKET socket(int af, int type, int protocol);

/*
Close an existing socket. 
InitNet() must be called before using any network function.

SOCKET s : (IN) Socket to close.

Return : EXIT_SUCCESS or SOCKET_ERROR if there is an error.
*/
//int closesocket(SOCKET s);

/*
Associate a local address and port with a server socket.
InitNet() must be called before using any network function.

SOCKET s : (IN) Unbound server socket.
struct sockaddr* name : (IN) Address family, host address and port to assign to the socket as a sockaddr structure. 
Can be obtained from getaddrinfo() (ai_addr field of the resulting addrinfo structures) or built using its 
fields sin_family, sin_addr.s_addr (using the conversion function inet_addr()) and sin_port (using the 
conversion function htons()). There are several special addresses : INADDR_LOOPBACK ("127.0.0.1") always 
refers to the local host via the loopback device, INADDR_ANY ("0.0.0.0") means any address for binding. 
When INADDR_ANY is specified, the socket will be bound to all local interfaces.
int namelen : (IN) Length of the value in the name parameter in bytes.

Return : EXIT_SUCCESS or SOCKET_ERROR if there is an error.
*/
//int bind(SOCKET s, struct sockaddr* name, int namelen);

/* 
Place a socket in a state in which it is listening for an incoming connection. The socket is put into 
passive mode where incoming connection requests are acknowledged and queued pending acceptance by the 
process. If a connection request arrives and the queue is full, the client will receive an error.
Should be used only with TCP sockets.
InitNet() must be called before using any network function.

SOCKET s : (IN) Bound and unconnected server socket.
int backlog : (IN) Maximum length of the queue of pending connections. Set to 1 if there 
should be only 1 simultaneous client or to SOMAXCONN for a default maximum reasonable value.

Return : EXIT_SUCCESS or SOCKET_ERROR if there is an error.
*/
//int listen(SOCKET s, int backlog);

/*
Determine the status of the sockets in the different fd_set structures, waiting if necessary until 
at least one socket meets the specified criteria depending on timeout. Upon return, the fd_set structures 
are updated to reflect the subset of the sockets that meet the specified condition. 
Macros to manipulate and check fd_set contents :

FD_ZERO(*set) 
Initialize the set to the null set.

FD_SET(s, *set) 
Add socket s to set.

FD_ISSET(s, *set) 
Nonzero if s is a member of the set. Otherwise, 0.

FD_CLR(s, *set) 
Remove the socket s from set.

Structure to define the timeout :

struct timeval {
long tv_sec; // Seconds.
long tv_usec; // Microseconds.
};

InitNet() must be called before using any network function.

int nfds : (IN) Highest-numbered file descriptor in any of the three sets, plus 1.
fd_set* pReadfds : (INOUT) Valid pointer to a set of sockets to be checked for readability 
(data is available i.e. a recv(), recvfrom() is guaranteed not to block, a connect request has been received 
by a listening socket such that an accept() is guaranteed to complete without blocking, or a request to 
close the socket has been received). NULL to ignore.
fd_set* pWritefds : (INOUT) Valid pointer to a set of sockets to be checked for writability (data can be 
sent i.e. a send(), sendto() is guaranteed to succeed). NULL to ignore.
fd_set* pExceptfds : (INOUT) Valid pointer to a set of sockets to be checked for errors/exceptions (do not use). 
NULL to ignore.
struct timeval* pTimeout : (INOUT) Valid pointer to a timeval structure specifying the maximum time to wait, 
NULL for infinite. Return immediately if the structure is initialized to {0,0}. The structure may be modified 
after the call.

Return : The total number of sockets contained in the updated fd_set structures, 0 if the time limit expired 
or SOCKET_ERROR if there is an error.
*/
//int select(int nfds, fd_set* pReadfds, fd_set* pWritefds, fd_set* pExceptfds, struct timeval* pTimeout);

/*
Extract the first connection request on the queue of pending connections for a listening socket.
Can block the caller until a connection is present.
Should be used only with TCP sockets.
InitNet() must be called before using any network function.

SOCKET s : (IN) Bound and listening server socket.
struct sockaddr* addr : (INOUT) Address family, host address and port of the accepted client socket as a 
sockaddr structure (fields sin_family, sin_addr.s_addr (using the conversion function inet_addr()) and 
sin_port (using the conversion function htons()). NULL to ignore.
int* addrlen : (INOUT) Valid pointer to the length of the addr parameter in bytes. 
On return it will contain the actual length in bytes of the address returned. NULL to ignore.

Return : The client socket or INVALID_SOCKET if there is an error.
*/
//SOCKET accept(SOCKET s, struct sockaddr* addr, int* addrlen);

/*
Establish a connection to a server.
Usually used with TCP sockets.
InitNet() must be called before using any network function.

SOCKET s : (IN) Unconnected client socket.
struct sockaddr* name : (IN) Address family, host address and port of the server socket as a sockaddr 
structure (fields sin_family, sin_addr.s_addr (using the conversion function inet_addr()) and sin_port 
(using the conversion function htons()).
int namelen : (IN) Length of the value in the name parameter in bytes.

Return : EXIT_SUCCESS or SOCKET_ERROR if there is an error.
*/
//int connect(SOCKET s, struct sockaddr* name, int namelen);

//int send(SOCKET s);

//int recv(SOCKET s);

/*
Usually used with UDP sockets.
InitNet() must be called before using any network function.

struct sockaddr* to : (IN) Address family, host address and port of the target socket as a sockaddr 
structure (fields sin_family, sin_addr.s_addr (using the conversion function inet_addr()) and sin_port 
(using the conversion function htons()).
int tolen : (IN) Length of the value in the to parameter in bytes.

*/
//int sendto(SOCKET s, char* buf, int len, int flags, struct sockaddr* to, int tolen);

/*
Usually used with UDP sockets.
InitNet() must be called before using any network function.

struct sockaddr* from : (INOUT) Address family, host address and port of the source socket as a 
sockaddr structure (fields sin_family, sin_addr.s_addr (using the conversion function inet_addr()) and 
sin_port (using the conversion function htons()). NULL to ignore.
int fromlen : (IN) INOUT) Valid pointer to the length of the from parameter in bytes. 
On return it will contain the actual length in bytes of the address returned. NULL to ignore.

*/
//int recvfrom(SOCKET s, char* buf, int len, int flags, struct sockaddr* from, int fromlen);

//int shutdown(SOCKET s);

#endif // OSNET_H
