/********************************************************************************/
/*																				*/
/*	NetSock.c																	*/
/*																				*/
/*	This file defines the interface with the WinSock API						*/
/*																				*/
/********************************************************************************/
 
/*
  USING WINSOCK EITHER IN STATIC DLL OR IN DYNAMIC DLL
*/
#define _NETLIB_WINSOCK_DYNAMIC_
 
#include "warnings.h"
#include "netsock.h"
 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
static HINSTANCE gs_h_WinsockDllHandle = NULL;
#endif
static tdstWinSockFunc stWinSockDesc;
static int gs_iCount = 0;
 
 
NetLib_tdeErrorStatus eNetLoadWinSock(tdstWinSockFunc *p_stWinSockDesc)
{
 WSADATA WsaData;
 int iErrorCode;
 
 if(gs_iCount>0) {
  gs_iCount++;
  return NetLib_E_es_NoError;
 }
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  if((gs_h_WinsockDllHandle = LoadLibrary("Wsock32.dll"))==NULL)
   return NetLib_E_es_InvalidHandle;
#endif
 
 gs_iCount=1;
  
 p_stWinSockDesc->m_pfn_ui_accept = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_ui_accept)GetProcAddress(gs_h_WinsockDllHandle,"accept");
#else
 accept;
#endif
 
 p_stWinSockDesc->m_pfn_i_bind = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_bind )GetProcAddress(gs_h_WinsockDllHandle,"bind");
#else
 bind;
#endif
 
 p_stWinSockDesc->m_pfn_i_closesocket = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_closesocket )GetProcAddress(gs_h_WinsockDllHandle,"closesocket");
#else
 closesocket;
#endif
 
 p_stWinSockDesc->m_pfn_i_connect = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_connect )GetProcAddress(gs_h_WinsockDllHandle,"connect");
#else
 connect;
#endif
 
 p_stWinSockDesc->m_pfn_i_ioctlsocket = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_ioctlsocket )GetProcAddress(gs_h_WinsockDllHandle,"ioctlsocket");
#else
 ioctlsocket;
#endif
 
 p_stWinSockDesc->m_pfn_i_getpeername = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_getpeername )GetProcAddress(gs_h_WinsockDllHandle,"getpeername");
#else
 getpeername;
#endif
 
 p_stWinSockDesc->m_pfn_i_getsockname = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_getsockname )GetProcAddress(gs_h_WinsockDllHandle,"getsockname");
#else
 getsockname;
#endif
 
 p_stWinSockDesc->m_pfn_i_getsockopt = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_getsockopt )GetProcAddress(gs_h_WinsockDllHandle,"getsockopt");
#else
 getsockopt;
#endif
 
 p_stWinSockDesc->m_pfn_ul_htonl = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_ul_htonl )GetProcAddress(gs_h_WinsockDllHandle,"htonl");
#else
 htonl;
#endif
 
 
 p_stWinSockDesc->m_pfn_uw_htons = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_uw_htons )GetProcAddress(gs_h_WinsockDllHandle,"htons");
#else
 htons;
#endif
 
 p_stWinSockDesc->m_pfn_ul_inet_addr = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_ul_inet_addr )GetProcAddress(gs_h_WinsockDllHandle,"inet_addr");
#else
 inet_addr;
#endif
 
 p_stWinSockDesc->m_pfn_pc_inet_ntoa = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_pc_inet_ntoa )GetProcAddress(gs_h_WinsockDllHandle,"inet_ntoa");
#else
 inet_ntoa;
#endif
 
 p_stWinSockDesc->m_pfn_i_listen = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_listen )GetProcAddress(gs_h_WinsockDllHandle,"listen");
#else
 listen;
#endif
 
 p_stWinSockDesc->m_pfn_ul_ntohl = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_ul_ntohl )GetProcAddress(gs_h_WinsockDllHandle,"ntohl");
#else
 ntohl;
#endif
 
 p_stWinSockDesc->m_pfn_uw_ntohs = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_uw_ntohs )GetProcAddress(gs_h_WinsockDllHandle,"ntohs");
#else
 ntohs;
#endif
 
 p_stWinSockDesc->m_pfn_i_recv = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_recv )GetProcAddress(gs_h_WinsockDllHandle,"recv");
#else
 recv;
#endif
 
 p_stWinSockDesc->m_pfn_i_recvfrom = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_recvfrom )GetProcAddress(gs_h_WinsockDllHandle,"recvfrom");
#else
 recvfrom;
#endif
 
 p_stWinSockDesc->m_pfn_i_select = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_select )GetProcAddress(gs_h_WinsockDllHandle,"select");
#else
 select;
#endif
 
 p_stWinSockDesc->m_pfn_i_send = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_send )GetProcAddress(gs_h_WinsockDllHandle,"send");
#else
 send;
#endif
 
 p_stWinSockDesc->m_pfn_i_sendto = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_sendto )GetProcAddress(gs_h_WinsockDllHandle,"sendto");
#else
 sendto;
#endif
 
 p_stWinSockDesc->m_pfn_i_setsockopt = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_setsockopt )GetProcAddress(gs_h_WinsockDllHandle,"setsockopt");
#else
 setsockopt;
#endif
 
 p_stWinSockDesc->m_pfn_i_shutdown = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_shutdown )GetProcAddress(gs_h_WinsockDllHandle,"shutdown");
#else
 shutdown;
#endif
 
 p_stWinSockDesc->m_pfn_ui_socket = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_ui_socket )GetProcAddress(gs_h_WinsockDllHandle,"socket");
#else
 socket;
#endif
 
 p_stWinSockDesc->m_pfn_pst_gethostbyaddr = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_pst_gethostbyaddr )GetProcAddress(gs_h_WinsockDllHandle,"gethostbyaddr");
#else
 gethostbyaddr;
#endif
 
 p_stWinSockDesc->m_pfn_pst_gethostbyname = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_pst_gethostbyname )GetProcAddress(gs_h_WinsockDllHandle,"gethostbyname");
#else
 gethostbyname;
#endif
 
 p_stWinSockDesc->m_pfn_i_gethostname = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_gethostname )GetProcAddress(gs_h_WinsockDllHandle,"gethostname");
#else
 gethostname;
#endif
 
 p_stWinSockDesc->m_pfn_pst_getservbyport = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_pst_getservbyport )GetProcAddress(gs_h_WinsockDllHandle,"getservbyport");
#else
 getservbyport;
#endif
 
 p_stWinSockDesc->m_pfn_pst_getservbyname = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_pst_getservbyname )GetProcAddress(gs_h_WinsockDllHandle,"getservbyname");
#else
 getservbyname;
#endif
 
 p_stWinSockDesc->m_pfn_pst_getprotobynumber = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_pst_getprotobynumber )GetProcAddress(gs_h_WinsockDllHandle,"getprotobynumber");
#else
 getprotobynumber;
#endif
 
 p_stWinSockDesc->m_pfn_pst_getprotobyname = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_pst_getprotobyname )GetProcAddress(gs_h_WinsockDllHandle,"getprotobyname");
#else
 getprotobyname;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSAStartup = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSAStartup )GetProcAddress(gs_h_WinsockDllHandle,"WSAStartup");
#else
 WSAStartup;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSACleanup = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSACleanup )GetProcAddress(gs_h_WinsockDllHandle,"WSACleanup");
#else
 WSACleanup;
#endif
 
 p_stWinSockDesc->m_pfn_v_WSASetLastError = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_v_WSASetLastError )GetProcAddress(gs_h_WinsockDllHandle,"WSASetLastError");
#else
 WSASetLastError;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSAGetLastError = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSAGetLastError )GetProcAddress(gs_h_WinsockDllHandle,"WSAGetLastError");
#else
 WSAGetLastError;
#endif
 
 p_stWinSockDesc->m_pfn_b_WSAIsBlocking = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_b_WSAIsBlocking )GetProcAddress(gs_h_WinsockDllHandle,"WSAIsBlocking");
#else
 WSAIsBlocking;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSAUnhookBlockingHook = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSAUnhookBlockingHook )GetProcAddress(gs_h_WinsockDllHandle,"WSAUnhookBlockingHook");
#else
 WSAUnhookBlockingHook;
#endif
 
 p_stWinSockDesc->m_pfn_pfn_WSASetBlockingHook = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_pfn_WSASetBlockingHook )GetProcAddress(gs_h_WinsockDllHandle,"WSASetBlockingHook");
#else
 WSASetBlockingHook;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSACancelBlockingCall = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSACancelBlockingCall )GetProcAddress(gs_h_WinsockDllHandle,"WSACancelBlockingCall");
#else
 WSACancelBlockingCall;
#endif
 
 p_stWinSockDesc->m_pfn_h_WSAAsyncGetServByName = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_h_WSAAsyncGetServByName )GetProcAddress(gs_h_WinsockDllHandle,"WSAAsyncGetServByName");
#else
 WSAAsyncGetServByName;
#endif
 
 p_stWinSockDesc->m_pfn_h_WSAAsyncGetServByPort = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_h_WSAAsyncGetServByPort )GetProcAddress(gs_h_WinsockDllHandle,"WSAAsyncGetServByPort");
#else
 WSAAsyncGetServByPort;
#endif
 
 p_stWinSockDesc->m_pfn_h_WSAAsyncGetProtoByName = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_h_WSAAsyncGetProtoByName )GetProcAddress(gs_h_WinsockDllHandle,"WSAAsyncGetProtoByName");
#else
 WSAAsyncGetProtoByName;
#endif
 
 p_stWinSockDesc->m_pfn_h_WSAAsyncGetProtoByNumber = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_h_WSAAsyncGetProtoByNumber )GetProcAddress(gs_h_WinsockDllHandle,"WSAAsyncGetProtoByNumber");
#else
 WSAAsyncGetProtoByNumber;
#endif
 
 p_stWinSockDesc->m_pfn_h_WSAAsyncGetHostByName = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_h_WSAAsyncGetHostByName )GetProcAddress(gs_h_WinsockDllHandle,"WSAAsyncGetHostByName");
#else
 WSAAsyncGetHostByName;
#endif
 
 p_stWinSockDesc->m_pfn_h_WSAAsyncGetHostByAddr = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_h_WSAAsyncGetHostByAddr )GetProcAddress(gs_h_WinsockDllHandle,"WSAAsyncGetHostByAddr");
#else
 WSAAsyncGetHostByAddr;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSACancelAsyncRequest = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSACancelAsyncRequest )GetProcAddress(gs_h_WinsockDllHandle,"WSACancelAsyncRequest");
#else
 WSACancelAsyncRequest;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSAAsyncSelect = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSAAsyncSelect )GetProcAddress(gs_h_WinsockDllHandle,"WSAAsyncSelect");
#else
 WSAAsyncSelect;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSARecvEx = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSARecvEx )GetProcAddress(gs_h_WinsockDllHandle,"WSARecvEx");
#else
 WSARecvEx;
#endif
 
 p_stWinSockDesc->m_pfn_i_WSAFDIsSet = 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
  (tdp_fn_i_WSAFDIsSet)GetProcAddress(gs_h_WinsockDllHandle,"__WSAFDIsSet");
#else
  __WSAFDIsSet;
#endif
 
 iErrorCode=p_stWinSockDesc->m_pfn_i_WSAStartup(0x0101, &WsaData);
 
 if(iErrorCode) 
	 return NetLib_E_es_InitialisationSocketError;
 else 
	 return NetLib_E_es_NoError;
}
 
NetLib_tdeErrorStatus eNetGetWinSockDesc(tdstWinSockFunc **p_pstWinSocDesc)
{
 NetLib_tdeErrorStatus eErrorCode;
 
 if((eErrorCode=eNetLoadWinSock(&stWinSockDesc))!=NetLib_E_es_NoError)
    *p_pstWinSocDesc = NULL;
 else
    *p_pstWinSocDesc = &stWinSockDesc;
 
 return eErrorCode;
}
 
NetLib_tdeErrorStatus eNetRestoreWinSock(void)
{
	int iErrorCode;
 
	gs_iCount--;
 
#ifdef _NETLIB_WINSOCK_DYNAMIC_
	if(!gs_iCount) {
		if(iErrorCode=WSACleanup())
			return NetLib_E_es_UnknownError;
		FreeLibrary(gs_h_WinsockDllHandle);
	}
#endif
 
	return NetLib_E_es_NoError;
}
