Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
6.9 KiB

  1. //=================================================================
  2. //
  3. // Wsock32API.cpp
  4. //
  5. // Copyright (c) 1999-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #include "precomp.h"
  9. #include <cominit.h>
  10. #include "Wsock32Api.h"
  11. #include "DllWrapperCreatorReg.h"
  12. // {D60E9C22-D127-11d2-911F-0060081A46FD}
  13. static const GUID g_guidWsock32Api =
  14. {0xd60e9c22, 0xd127, 0x11d2, {0x91, 0x1f, 0x0, 0x60, 0x8, 0x1a, 0x46, 0xfd}};
  15. static const TCHAR g_tstrWsock32[] = _T("WSOCK32.DLL");
  16. /******************************************************************************
  17. * Register this class with the CResourceManager.
  18. *****************************************************************************/
  19. CDllApiWraprCreatrReg<CWsock32Api, &g_guidWsock32Api, g_tstrWsock32> MyRegisteredWsock32Wrapper;
  20. /******************************************************************************
  21. * Constructor
  22. ******************************************************************************/
  23. CWsock32Api::CWsock32Api(LPCTSTR a_tstrWrappedDllName)
  24. : CDllWrapperBase(a_tstrWrappedDllName),
  25. m_pfnWsControl(NULL),
  26. m_pfnWsEnumProtocols(NULL),
  27. m_pfnWsCloseSocket(NULL),
  28. m_pfnWsStartup(NULL),
  29. m_pfnWsCleanup(NULL) ,
  30. m_pfnWsGetSockopt(NULL) ,
  31. m_pfnWsBind(NULL),
  32. m_pfnWsSocket(NULL),
  33. m_pfnWsWSAGetLastError(NULL),
  34. m_pfnWsInet_NtoA(NULL)
  35. {
  36. }
  37. /******************************************************************************
  38. * Destructor
  39. ******************************************************************************/
  40. CWsock32Api::~CWsock32Api()
  41. {
  42. }
  43. /******************************************************************************
  44. * Initialization function to check that we obtained function addresses.
  45. * Init should fail only if the minimum set of functions was not available;
  46. * functions added in later versions may or may not be present - it is the
  47. * client's responsibility in such cases to check, in their code, for the
  48. * version of the dll before trying to call such functions. Not doing so
  49. * when the function is not present will result in an AV.
  50. *
  51. * The Init function is called by the WrapperCreatorRegistation class.
  52. ******************************************************************************/
  53. bool CWsock32Api::Init()
  54. {
  55. bool fRet = LoadLibrary();
  56. if(fRet)
  57. {
  58. #ifdef WIN9XONLY
  59. m_pfnWsControl = (PFN_WSOCK32_WSCONTROL) GetProcAddress("WsControl");
  60. m_pfnWsEnumProtocols = (PFN_WSOCK32_ENUMPROTOCOLS) GetProcAddress("EnumProtocolsA");
  61. m_pfnWsStartup = (PFN_WSOCK32_STARTUP) GetProcAddress("WSAStartup");
  62. m_pfnWsCleanup = (PFN_WSOCK32_CLEANUP) GetProcAddress("WSACleanup");
  63. m_pfnWsCloseSocket = (PFN_WSOCK32_CLOSESOCKET) GetProcAddress("closesocket");
  64. m_pfnWsGetSockopt = ( PFN_WSOCK32_GETSOCKOPT ) GetProcAddress("getsockopt");
  65. m_pfnWsBind = ( PFN_WSOCK32_BIND ) GetProcAddress("bind");
  66. m_pfnWsSocket = ( PFN_WSOCK32_SOCKET )GetProcAddress("socket");;
  67. m_pfnWsWSAGetLastError = ( PFN_WSOCK32_WSAGETLASTERROR )GetProcAddress("WSAGetLastError"); ;
  68. m_pfnWsInet_NtoA = ( PFN_WSOCK32_INET_NTOA )GetProcAddress("inet_ntoa"); ;
  69. // Check that we have function pointers to functions that should be
  70. // present...
  71. if ( m_pfnWsControl == NULL ||
  72. m_pfnWsEnumProtocols == NULL ||
  73. m_pfnWsStartup == NULL ||
  74. m_pfnWsCleanup == NULL ||
  75. m_pfnWsCloseSocket == NULL ||
  76. m_pfnWsGetSockopt == NULL ||
  77. m_pfnWsBind == NULL ||
  78. m_pfnWsSocket == NULL ||
  79. m_pfnWsWSAGetLastError == NULL ||
  80. m_pfnWsInet_NtoA == NULL )
  81. {
  82. fRet = false;
  83. LogErrorMessage(L"Failed find entrypoint in wsock32api");
  84. }
  85. #else
  86. m_pfnWsEnumProtocols = (PFN_WSOCK32_ENUMPROTOCOLS) GetProcAddress("EnumProtocolsW");
  87. m_pfnWsStartup = (PFN_WSOCK32_STARTUP) GetProcAddress("WSAStartup");
  88. m_pfnWsCleanup = (PFN_WSOCK32_CLEANUP) GetProcAddress("WSACleanup");
  89. m_pfnWsCloseSocket = (PFN_WSOCK32_CLOSESOCKET) GetProcAddress("closesocket");
  90. m_pfnWsGetSockopt = ( PFN_WSOCK32_GETSOCKOPT ) GetProcAddress("getsockopt");
  91. m_pfnWsBind = ( PFN_WSOCK32_BIND ) GetProcAddress("bind");
  92. m_pfnWsSocket = ( PFN_WSOCK32_SOCKET )GetProcAddress("socket");;
  93. m_pfnWsWSAGetLastError = ( PFN_WSOCK32_WSAGETLASTERROR )GetProcAddress("WSAGetLastError"); ;
  94. m_pfnWsInet_NtoA = ( PFN_WSOCK32_INET_NTOA )GetProcAddress("inet_ntoa"); ;
  95. if ( m_pfnWsEnumProtocols == NULL ||
  96. m_pfnWsStartup == NULL ||
  97. m_pfnWsCleanup == NULL ||
  98. m_pfnWsCloseSocket == NULL ||
  99. m_pfnWsGetSockopt == NULL ||
  100. m_pfnWsBind == NULL ||
  101. m_pfnWsSocket == NULL ||
  102. m_pfnWsWSAGetLastError == NULL ||
  103. m_pfnWsInet_NtoA == NULL )
  104. {
  105. fRet = false;
  106. LogErrorMessage(L"Failed find entrypoint in wsock32api");
  107. }
  108. #endif
  109. }
  110. return fRet;
  111. }
  112. /******************************************************************************
  113. * Member functions wrapping Wsock32 api functions. Add new functions here
  114. * as required.
  115. ******************************************************************************/
  116. bool CWsock32Api::WsControl
  117. (
  118. DWORD a_dw1,
  119. DWORD a_dw2,
  120. LPVOID a_lpv1,
  121. LPDWORD a_lpdw1,
  122. LPVOID a_lpv2,
  123. LPDWORD a_lpdw2,
  124. DWORD *a_pdwRetval
  125. )
  126. {
  127. bool t_fExists = false;
  128. if(m_pfnWsControl != NULL)
  129. {
  130. DWORD t_dwTemp = m_pfnWsControl(a_dw1, a_dw2, a_lpv1,
  131. a_lpdw1, a_lpv2, a_lpdw2);
  132. t_fExists = true;
  133. if(a_pdwRetval != NULL)
  134. {
  135. *a_pdwRetval = t_dwTemp;
  136. }
  137. }
  138. return t_fExists;
  139. }
  140. INT CWsock32Api::WsEnumProtocols (
  141. LPINT lpiProtocols,
  142. LPVOID lpProtocolBuffer,
  143. LPDWORD lpdwBufferLength
  144. )
  145. {
  146. return m_pfnWsEnumProtocols (
  147. lpiProtocols,
  148. lpProtocolBuffer,
  149. lpdwBufferLength
  150. ) ;
  151. }
  152. INT CWsock32Api :: WsWSAStartup (
  153. IN WORD wVersionRequired,
  154. OUT LPWSADATA lpWSAData
  155. )
  156. {
  157. return m_pfnWsStartup (
  158. wVersionRequired ,
  159. lpWSAData
  160. ) ;
  161. }
  162. INT CWsock32Api :: WsWSACleanup ()
  163. {
  164. return m_pfnWsCleanup () ;
  165. }
  166. INT CWsock32Api :: Wsclosesocket ( SOCKET s )
  167. {
  168. return m_pfnWsCloseSocket ( s ) ;
  169. }
  170. int CWsock32Api :: Wsbind (
  171. SOCKET s,
  172. const struct sockaddr FAR *addr,
  173. int namelen
  174. )
  175. {
  176. return m_pfnWsBind (
  177. s,
  178. addr,
  179. namelen
  180. ) ;
  181. }
  182. int CWsock32Api :: Wsgetsockopt (
  183. SOCKET s,
  184. int level,
  185. int optname,
  186. char FAR * optval,
  187. int FAR *optlen
  188. )
  189. {
  190. return m_pfnWsGetSockopt (
  191. s,
  192. level,
  193. optname,
  194. optval,
  195. optlen
  196. ) ;
  197. }
  198. SOCKET CWsock32Api :: Wssocket (
  199. int af,
  200. int type,
  201. int protocol
  202. )
  203. {
  204. return m_pfnWsSocket (
  205. af,
  206. type,
  207. protocol
  208. ) ;
  209. }
  210. int CWsock32Api :: WsWSAGetLastError(void)
  211. {
  212. return m_pfnWsWSAGetLastError () ;
  213. }
  214. char *CWsock32Api :: Wsinet_ntoa (
  215. struct in_addr in
  216. )
  217. {
  218. return m_pfnWsInet_NtoA (
  219. in
  220. ) ;
  221. }