Guest User

Untitled

a guest
Jul 15th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.54 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <locale.h>
  3. #include <stdio.h>
  4. #include <tchar.h>
  5.  
  6. #include <winsock2.h>
  7. #include <Windows.h>
  8. #include <ws2spi.h>
  9. //https://github.com/pauldotknopf/WindowsSDK7-Samples/blob/e8fe83b043727e71f5179da11fc6228475e7973c/netds/winsock/lsp/install/instlsp.cpp
  10.  
  11. #pragma comment(lib,"Ws2_32.lib")
  12.  
  13. typedef enum
  14. {
  15. LspCatalogBoth = 0,
  16. LspCatalog32Only,
  17. LspCatalog64Only
  18. } WINSOCK_CATALOG;
  19.  
  20. //*** private LSP heap ****
  21. HANDLE gLspHeap = NULL;
  22.  
  23. int LspCreateHeap( int *lpErrno )
  24. {
  25. gLspHeap = HeapCreate( 0, 128000, 0 );
  26. if ( NULL == gLspHeap )
  27. {
  28. *lpErrno = WSAEPROVIDERFAILEDINIT;
  29. return SOCKET_ERROR;
  30. }
  31. return NO_ERROR;
  32. }
  33.  
  34. void * LspAlloc( SIZE_T size, int *lpErrno )
  35. {
  36. void *mem = NULL;
  37. mem = HeapAlloc(
  38. gLspHeap,
  39. HEAP_ZERO_MEMORY,
  40. size
  41. );
  42. if ( NULL == mem )
  43. {
  44. *lpErrno = WSAENOBUFS;
  45. }
  46.  
  47. return mem;
  48. }
  49.  
  50. void LspFree( LPVOID buf )
  51. {
  52. HeapFree( gLspHeap, 0, buf );
  53. }
  54.  
  55. //**********************************************
  56.  
  57. LPWSAPROTOCOL_INFOW EnumerateProviders(
  58. WINSOCK_CATALOG Catalog,
  59. LPINT TotalProtocols
  60. )
  61. {
  62. LPWSAPROTOCOL_INFOW ProtocolInfo = NULL;
  63. DWORD ProtocolInfoSize = 0;
  64. INT ErrorCode = NO_ERROR,
  65. rc;
  66.  
  67. if ( NULL == TotalProtocols )
  68. goto cleanup;
  69.  
  70. *TotalProtocols = 0;
  71.  
  72. #ifdef _WIN64
  73. // Find out how many entries we need to enumerate
  74. if ( LspCatalog64Only == Catalog )
  75. {
  76. // Find the size of the buffer
  77. rc = WSCEnumProtocols( NULL, ProtocolInfo, &ProtocolInfoSize, &ErrorCode );
  78. if ( SOCKET_ERROR == rc )
  79. {
  80. if ( WSAENOBUFS != ErrorCode )
  81. goto cleanup;
  82. ErrorCode = NO_ERROR;
  83. }
  84.  
  85. // Allocate the buffer
  86. ProtocolInfo = (LPWSAPROTOCOL_INFOW) LspAlloc(
  87. ProtocolInfoSize,
  88. &ErrorCode
  89. );
  90. if (ProtocolInfo == NULL)
  91. goto cleanup;
  92.  
  93. // Enumerate the catalog for real
  94. rc = WSCEnumProtocols( NULL, ProtocolInfo, &ProtocolInfoSize, &ErrorCode );
  95. if ( SOCKET_ERROR == rc )
  96. goto cleanup;
  97.  
  98. // Update the count
  99. *TotalProtocols = rc;
  100. }
  101. else if ( LspCatalog32Only == Catalog )
  102. {
  103. HMODULE hModule;
  104. LPWSCENUMPROTOCOLS fnWscEnumProtocols32 = NULL;
  105.  
  106. // Load ws2_32.dll
  107. hModule = LoadLibrary( TEXT( "ws2_32.dll" ) );
  108. if ( NULL == hModule )
  109. goto cleanup;
  110.  
  111. // Find the 32-bit catalog enumerator
  112. fnWscEnumProtocols32 = (LPWSCENUMPROTOCOLS) GetProcAddress(
  113. hModule,
  114. "WSCEnumProtocols32"
  115. );
  116. if ( NULL == fnWscEnumProtocols32 )
  117. goto cleanup;
  118.  
  119. // Find the required buffer size
  120. rc = fnWscEnumProtocols32(NULL, ProtocolInfo, &ProtocolInfoSize, &ErrorCode);
  121. if ( SOCKET_ERROR == rc )
  122. {
  123. if ( WSAENOBUFS != ErrorCode )
  124. goto cleanup;
  125. ErrorCode = NO_ERROR;
  126. }
  127.  
  128. // Allocate the buffer
  129. ProtocolInfo = (LPWSAPROTOCOL_INFOW) LspAlloc(
  130. ProtocolInfoSize,
  131. &ErrorCode
  132. );
  133. if ( NULL == ProtocolInfo )
  134. goto cleanup;
  135.  
  136. // Enumrate the catalog for real this time
  137. rc = fnWscEnumProtocols32( NULL, ProtocolInfo, &ProtocolInfoSize, &ErrorCode );
  138. if ( SOCKET_ERROR == rc )
  139. goto cleanup;
  140.  
  141. // Update the count
  142. *TotalProtocols = rc;
  143.  
  144. FreeLibrary( hModule );
  145. }
  146. #else
  147. if ( LspCatalog32Only == Catalog )
  148. {
  149. // Find the size of the buffer
  150. rc = WSCEnumProtocols( NULL, ProtocolInfo, &ProtocolInfoSize, &ErrorCode );
  151. if ( SOCKET_ERROR == rc )
  152. {
  153. if ( WSAENOBUFS != ErrorCode )
  154. goto cleanup;
  155. ErrorCode = NO_ERROR;
  156. }
  157.  
  158. // Allocate the buffer
  159. ProtocolInfo = (LPWSAPROTOCOL_INFOW) LspAlloc(
  160. ProtocolInfoSize,
  161. &ErrorCode
  162. );
  163. if ( NULL == ProtocolInfo )
  164. goto cleanup;
  165.  
  166. // Enumerate the catalog for real
  167. rc = WSCEnumProtocols( NULL, ProtocolInfo, &ProtocolInfoSize, &ErrorCode );
  168. if ( SOCKET_ERROR == rc )
  169. goto cleanup;
  170.  
  171. // Update the count
  172. *TotalProtocols = rc;
  173. }
  174. else if ( LspCatalog64Only == Catalog )
  175. {
  176. puts( "Unable to enumerate 64-bit Winsock catalog from 32-bit process!");
  177. }
  178. #endif
  179. else
  180. {
  181. // Find the size of the buffer
  182. rc = WSCEnumProtocols( NULL, ProtocolInfo, &ProtocolInfoSize, &ErrorCode );
  183. if ( SOCKET_ERROR == rc )
  184. {
  185. if ( WSAENOBUFS != ErrorCode )
  186. goto cleanup;
  187. ErrorCode = NO_ERROR;
  188. }
  189.  
  190. // Allocate the buffer
  191. ProtocolInfo = (LPWSAPROTOCOL_INFOW) LspAlloc(
  192. ProtocolInfoSize,
  193. &ErrorCode
  194. );
  195. if ( NULL == ProtocolInfo )
  196. goto cleanup;
  197.  
  198. // Enumerate the catalog for real
  199. rc = WSCEnumProtocols( NULL, ProtocolInfo, &ProtocolInfoSize, &ErrorCode );
  200. if ( SOCKET_ERROR == rc )
  201. goto cleanup;
  202.  
  203. // Update the count
  204. *TotalProtocols = rc;
  205. }
  206.  
  207. cleanup:
  208.  
  209. if ( ( NO_ERROR != ErrorCode ) && ( NULL != ProtocolInfo ) )
  210. {
  211. LspFree( ProtocolInfo );
  212. ProtocolInfo = NULL;
  213. }
  214.  
  215. return ProtocolInfo;
  216. }
  217.  
  218. void PrintProtocolInfo( WSAPROTOCOL_INFOW *wsapi)
  219. {
  220. WCHAR szGuidString[MAX_PATH],
  221. wszProviderPath[MAX_PATH];
  222. INT dwProviderPathLen=MAX_PATH-1;
  223. int rc, error, i;
  224.  
  225. rc = WSCGetProviderPath(
  226. &wsapi->ProviderId,
  227. wszProviderPath,
  228. &dwProviderPathLen,
  229. &error
  230. );
  231. if ( 0 != rc )
  232. {
  233. fprintf(stderr, "WSCGetProviderPath failed: %dn", error);
  234. lstrcpyW(wszProviderPath, L"(error)");
  235. }
  236.  
  237. //
  238. // Display protocol information
  239. //
  240.  
  241. printf("*** %S ***n", wsapi->szProtocol);
  242. printf("Path: %Sn", wszProviderPath);
  243.  
  244. //
  245. // Display provider ID and catalog ID as well as LSP chain information
  246. //
  247.  
  248. StringFromGUID2( wsapi->ProviderId, szGuidString, MAX_PATH-1 );
  249.  
  250. printf("Provider Id: %Sn", szGuidString);
  251.  
  252. if(wsapi->ProtocolChain.ChainLen == LAYERED_PROTOCOL)printf("LSP: Yesn");
  253. else printf("LSP: Non");
  254.  
  255. printf("Catalog Entry Id: %ldn", wsapi->dwCatalogEntryId);
  256. printf("Number of Chain Entries: %d {",wsapi->ProtocolChain.ChainLen);
  257.  
  258. for(i=0; i < wsapi->ProtocolChain.ChainLen ;i++)
  259. printf("%ld ", wsapi->ProtocolChain.ChainEntries[i]);
  260.  
  261. printf("}n");
  262.  
  263. printf("Version: %dnn", wsapi->iVersion);
  264. }
  265.  
  266.  
  267.  
  268. void FreeProviders( LPWSAPROTOCOL_INFOW ProtocolInfo )
  269. {
  270. LspFree( ProtocolInfo );
  271. }
  272.  
  273.  
  274.  
  275.  
  276. void PrintProviders( WINSOCK_CATALOG Catalog)
  277. {
  278. WSAPROTOCOL_INFOW *pProtocolInfo = NULL;
  279. INT iProtocolCount = 0,
  280. i;
  281.  
  282. // Enumerate catalog and print it
  283. pProtocolInfo = EnumerateProviders( Catalog, &iProtocolCount );
  284. if ( NULL == pProtocolInfo )
  285. {
  286. fprintf( stderr, "PrintProviders: Unable to enumerate catalog!n" );
  287. goto cleanup;
  288. }
  289.  
  290.  
  291. for(i=0; i < iProtocolCount ;i++)
  292. {
  293. PrintProtocolInfo( &pProtocolInfo[ i ] );
  294. }
  295.  
  296. cleanup:
  297.  
  298. if ( NULL != pProtocolInfo )
  299. FreeProviders( pProtocolInfo );
  300.  
  301. return;
  302. }
  303.  
  304. int main(void)
  305. {
  306. #ifdef _WIN64
  307. WINSOCK_CATALOG eCatalog = /*LspCatalog64Only*/LspCatalogBoth;
  308. #else
  309. WINSOCK_CATALOG eCatalog = LspCatalog32Only;
  310. #endif
  311.  
  312. WSADATA wsd;
  313. int rc;
  314.  
  315. setlocale(LC_ALL,"Russian");
  316. // Load Winsock
  317. rc = WSAStartup( MAKEWORD(2,2), &wsd );
  318. if ( 0 != rc )
  319. {
  320. fprintf( stderr, "Unable to load Winsock: %dn", rc );
  321. return -1;
  322. }
  323.  
  324. // Initialize data structures
  325. LspCreateHeap( &rc );
  326.  
  327. // Print the 32-bit catalog
  328. if ( ( LspCatalogBoth == eCatalog ) || ( LspCatalog32Only == eCatalog ) )
  329. {
  330. printf( "n32-bit providers:nn" );
  331. PrintProviders( LspCatalog32Only);
  332. printf( "=======================n" );
  333. }
  334. // Print the 64-bit catalog
  335. if ( ( LspCatalogBoth == eCatalog ) || ( LspCatalog64Only == eCatalog ) )
  336. {
  337. printf( "n64-bit providers:nn" );
  338. PrintProviders( LspCatalog64Only);
  339. printf( "=======================n" );
  340.  
  341. }
  342.  
  343. system("PAUSE");
  344. return 0;
  345. }
Add Comment
Please, Sign In to add comment