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.

226 lines
6.1 KiB

  1. //=================================================================
  2. //
  3. // Ws2_32Api.cpp
  4. //
  5. // Copyright (c) 1998-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #include "precomp.h"
  9. #include <cominit.h>
  10. #define INCL_WINSOCK_API_TYPEDEFS 1
  11. #include <winsock2.h>
  12. #include "DllWrapperBase.h"
  13. #include "Ws2_32Api.h"
  14. #include "DllWrapperCreatorReg.h"
  15. // {643966A2-D19F-11d2-9120-0060081A46FD}
  16. static const GUID g_guidWs2_32Api =
  17. {0x643966a2, 0xd19f, 0x11d2, { 0x91, 0x20, 0x0, 0x60, 0x8, 0x1a, 0x46, 0xfd}};
  18. static const TCHAR g_tstrWs2_32[] = _T("WS2_32.DLL");
  19. /******************************************************************************
  20. * Register this class with the CResourceManager.
  21. *****************************************************************************/
  22. CDllApiWraprCreatrReg<CWs2_32Api, &g_guidWs2_32Api, g_tstrWs2_32> MyRegisteredWs2_32Wrapper;
  23. /******************************************************************************
  24. * Constructor
  25. ******************************************************************************/
  26. CWs2_32Api::CWs2_32Api(LPCTSTR a_tstrWrappedDllName)
  27. : CDllWrapperBase(a_tstrWrappedDllName),
  28. m_pfnWSAStartUp(NULL),
  29. m_pfnWSAEnumProtocols(NULL),
  30. m_pfnWSAIoctl(NULL),
  31. m_pfnWSASocket(NULL),
  32. m_pfnBind(NULL),
  33. m_pfnCloseSocket(NULL),
  34. m_pfnWSACleanup(NULL),
  35. m_pfnWSAGetLastError(NULL)
  36. {
  37. }
  38. /******************************************************************************
  39. * Destructor
  40. ******************************************************************************/
  41. CWs2_32Api::~CWs2_32Api()
  42. {
  43. }
  44. /******************************************************************************
  45. * Initialization function to check that we obtained function addresses.
  46. * Init should fail only if the minimum set of functions was not available;
  47. * functions added in later versions may or may not be present - it is the
  48. * client's responsibility in such cases to check, in their code, for the
  49. * version of the dll before trying to call such functions. Not doing so
  50. * when the function is not present will result in an AV.
  51. *
  52. * The Init function is called by the WrapperCreatorRegistation class.
  53. ******************************************************************************/
  54. bool CWs2_32Api::Init()
  55. {
  56. bool fRet = LoadLibrary();
  57. if(fRet)
  58. {
  59. m_pfnWSAStartUp = (LPFN_WSASTARTUP) GetProcAddress("WSAStartup");
  60. m_pfnWSAIoctl = (LPFN_WSAIOCTL) GetProcAddress("WSAIoctl");
  61. m_pfnBind = (LPFN_BIND) GetProcAddress("bind");
  62. m_pfnCloseSocket = (LPFN_CLOSESOCKET) GetProcAddress("closesocket");
  63. m_pfnWSACleanup = (LPFN_WSACLEANUP) GetProcAddress("WSACleanup");
  64. m_pfnWSAGetLastError = (LPFN_WSAGETLASTERROR)
  65. GetProcAddress("WSAGetLastError");
  66. #ifdef NTONLY
  67. m_pfnWSAEnumProtocols = (LPFN_WSAENUMPROTOCOLS)
  68. GetProcAddress("WSAEnumProtocolsW");
  69. m_pfnWSASocket = (LPFN_WSASOCKET) GetProcAddress("WSASocketW");
  70. #endif
  71. #ifdef WIN9XONLY
  72. m_pfnWSAEnumProtocols = (LPFN_WSAENUMPROTOCOLS) GetProcAddress(
  73. "WSAEnumProtocolsA");
  74. m_pfnWSASocket = (LPFN_WSASOCKET) GetProcAddress("WSASocketA");
  75. #endif
  76. // Check that we have function pointers to functions that should be
  77. // present in all versions of this dll...
  78. if(m_pfnWSAStartUp == NULL ||
  79. m_pfnWSAIoctl == NULL ||
  80. m_pfnBind == NULL ||
  81. m_pfnCloseSocket == NULL ||
  82. m_pfnWSACleanup == NULL ||
  83. m_pfnWSAGetLastError == NULL ||
  84. m_pfnWSAEnumProtocols == NULL ||
  85. m_pfnWSASocket == NULL)
  86. {
  87. fRet = false;
  88. LogErrorMessage(L"Failed find entrypoint in ws2_32api");
  89. }
  90. else
  91. {
  92. fRet = true;
  93. }
  94. }
  95. return fRet;
  96. }
  97. /******************************************************************************
  98. * Member functions wrapping Ws2_32 api functions. Add new functions here
  99. * as required.
  100. ******************************************************************************/
  101. int CWs2_32Api::WSAStartUp
  102. (
  103. WORD a_wVersionRequested,
  104. LPWSADATA a_lpWSAData
  105. )
  106. {
  107. return m_pfnWSAStartUp(a_wVersionRequested, a_lpWSAData);
  108. }
  109. int CWs2_32Api::WSAEnumProtocols
  110. (
  111. LPINT a_lpiProtocols,
  112. LPWSAPROTOCOL_INFO a_lpProtocolBuffer,
  113. LPDWORD a_lpdwBufferLength
  114. )
  115. {
  116. return m_pfnWSAEnumProtocols(a_lpiProtocols, a_lpProtocolBuffer,
  117. a_lpdwBufferLength);
  118. }
  119. int CWs2_32Api::WSAIoctl
  120. (
  121. SOCKET a_s,
  122. DWORD a_dwIoControlCode,
  123. LPVOID a_lpvInBuffer,
  124. DWORD a_cbInBuffer,
  125. LPVOID a_lpvOutBuffer,
  126. DWORD a_cbOutBuffer,
  127. LPDWORD a_lpcbBytesReturned,
  128. LPWSAOVERLAPPED a_lpOverlapped,
  129. LPWSAOVERLAPPED_COMPLETION_ROUTINE a_lpCompletionRoutine
  130. )
  131. {
  132. return m_pfnWSAIoctl(a_s,
  133. a_dwIoControlCode,
  134. a_lpvInBuffer,
  135. a_cbInBuffer,
  136. a_lpvOutBuffer,
  137. a_cbOutBuffer,
  138. a_lpcbBytesReturned,
  139. a_lpOverlapped,
  140. a_lpCompletionRoutine);
  141. }
  142. SOCKET CWs2_32Api::WSASocket
  143. (
  144. int a_af,
  145. int a_type,
  146. int a_protocol,
  147. LPWSAPROTOCOL_INFO a_lpProtocolInfo,
  148. GROUP a_g,
  149. DWORD a_dwFlags
  150. )
  151. {
  152. return m_pfnWSASocket(a_af,
  153. a_type,
  154. a_protocol,
  155. a_lpProtocolInfo,
  156. a_g,
  157. a_dwFlags);
  158. }
  159. int CWs2_32Api::Bind
  160. (
  161. SOCKET a_s,
  162. const struct sockaddr FAR * a_name,
  163. int a_namelen
  164. )
  165. {
  166. return m_pfnBind(a_s, a_name, a_namelen);
  167. }
  168. int CWs2_32Api::CloseSocket
  169. (
  170. SOCKET a_s
  171. )
  172. {
  173. return m_pfnCloseSocket(a_s);
  174. }
  175. int CWs2_32Api::WSACleanup()
  176. {
  177. return m_pfnWSACleanup();
  178. }
  179. int CWs2_32Api::WSAGetLastError()
  180. {
  181. return m_pfnWSAGetLastError();
  182. }