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.

192 lines
5.5 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <winsock2.h>
  6. #include <wsipx.h>
  7. #include <svcguid.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <rpc.h>
  11. #include <rpcdce.h>
  12. GUID ServiceClassId = { /* 5b50962a-e5a5-11cf-a555-00c04fd8d4ac */
  13. 0x5b50962a,
  14. 0xe5a5,
  15. 0x11cf,
  16. {0xa5, 0x55, 0x00, 0xc0, 0x4f, 0xd8, 0xd4, 0xac}
  17. };
  18. #define BUFFSIZE 3000
  19. _cdecl
  20. main(int argc, char **argv)
  21. {
  22. HANDLE hLib;
  23. WCHAR Buffer[BUFFSIZE];
  24. WCHAR Buffer1[BUFFSIZE];
  25. PWSAQUERYSETW Query = (PWSAQUERYSETW)Buffer;
  26. PWSAQUERYSETW Query1 = (PWSAQUERYSETW)Buffer1;
  27. HANDLE hRnr;
  28. DWORD dwNameSpace = NS_NTDS;
  29. DWORD dwIp;
  30. DWORD dwQuerySize = BUFFSIZE;
  31. WSADATA wsaData;
  32. WSAStartup(MAKEWORD(1, 1), &wsaData);
  33. memset(Query, 0, sizeof(*Query));
  34. memset(Query1, 0, sizeof(*Query1));
  35. Query->lpszServiceInstanceName = 0;
  36. Query->dwNumberOfCsAddrs = 0;
  37. Query->lpcsaBuffer = 0;
  38. Query->dwNameSpace = dwNameSpace;
  39. Query->dwSize = sizeof(*Query);
  40. Query->lpServiceClassId = &ServiceClassId;
  41. //
  42. // Find containers. This should fail.
  43. //
  44. if ( WSALookupServiceBegin( Query,
  45. LUP_CONTAINERS,
  46. &hRnr ) == SOCKET_ERROR )
  47. {
  48. printf( "LookupBegin containers failed %d\n", GetLastError() );
  49. }
  50. else
  51. {
  52. //
  53. // Ready to actually look for one of these ...
  54. //
  55. Query->dwSize = BUFFSIZE;
  56. while ( WSALookupServiceNext( hRnr,
  57. 0,
  58. &dwQuerySize,
  59. Query ) == NO_ERROR )
  60. {
  61. printf( "Next containers got %ws\n",
  62. Query->lpszServiceInstanceName );
  63. }
  64. printf( "Next finished with %d\n", GetLastError() );
  65. WSALookupServiceEnd( hRnr );
  66. }
  67. Query1->dwSize = sizeof(*Query1); // TEST LATER
  68. Query1->lpszServiceInstanceName = 0; // TEST NAME AND WILD CARDS
  69. Query1->lpServiceClassId = &ServiceClassId; // REQUIRED - TEST
  70. Query1->lpVersion = 0; // TEST LATER
  71. Query1->dwNameSpace = dwNameSpace; // TEST
  72. Query1->lpNSProviderId = &ServiceClassId; //BOGUS
  73. Query1->lpafpProtocols = 0; // TEST LATER
  74. if( WSALookupServiceBegin( Query1,
  75. LUP_RETURN_NAME | // TEST ALL COMBINATIONS
  76. LUP_RETURN_ADDR | // OF LUP FLAGS
  77. LUP_RETURN_TYPE,
  78. &hRnr) == SOCKET_ERROR)
  79. {
  80. printf( "LookupBegin for local name failed %d\n", GetLastError() );
  81. goto more;
  82. }
  83. while ( WSALookupServiceNext( hRnr,
  84. 0,
  85. &dwQuerySize,
  86. Query1) == NO_ERROR )
  87. {
  88. printf( "Next succeeded with %d addresses for Service %ws:",
  89. Query1->dwNumberOfCsAddrs,
  90. Query1->lpszServiceInstanceName );
  91. //
  92. // get the address out. This is the local machine address that
  93. // can be used in a revere lookup
  94. //
  95. if( Query1->dwNumberOfCsAddrs )
  96. {
  97. struct sockaddr_in * psock;
  98. PBYTE p;
  99. DWORD dwX;
  100. for( dwX = 0; dwX < Query1->dwNumberOfCsAddrs; dwX++ )
  101. {
  102. psock = (struct sockaddr_in *)
  103. Query1->lpcsaBuffer[dwX].RemoteAddr.lpSockaddr;
  104. dwIp = psock->sin_addr.S_un.S_addr;
  105. p = (PBYTE)&dwIp;
  106. printf( "\n socket type %d, protocol %d, length %d addr: %d.%d.%d.%d, port %d\n",
  107. Query1->lpcsaBuffer[dwX].iSocketType,
  108. Query1->lpcsaBuffer[dwX].iProtocol,
  109. Query1->lpcsaBuffer[dwX].RemoteAddr.iSockaddrLength,
  110. (DWORD)p[0], (DWORD)p[1], (DWORD)p[2], (DWORD)p[3],
  111. (DWORD)ntohs(psock->sin_port));
  112. }
  113. break;
  114. }
  115. }
  116. if( WSALookupServiceEnd( hRnr ) )
  117. {
  118. printf("ServiceEnd failed %d\n", GetLastError());
  119. }
  120. else
  121. {
  122. printf("ServiceEnd succeeded\n");
  123. }
  124. //
  125. // Let's try a reverse lookup on this address!
  126. //
  127. more:
  128. Query->lpServiceClassId = &ServiceClassId;
  129. Query->dwNameSpace = dwNameSpace;
  130. Query->lpafpProtocols = 0;
  131. //
  132. // Ready to actually look for one of these ...
  133. Query->lpszServiceInstanceName = 0;
  134. Query->dwNumberOfCsAddrs = 1;
  135. Query->lpcsaBuffer = Query1->lpcsaBuffer;
  136. if( WSALookupServiceBegin( Query,
  137. LUP_RETURN_NAME |
  138. LUP_RETURN_ADDR,
  139. &hRnr ) == SOCKET_ERROR )
  140. {
  141. printf( "LookupBegin for reverse failed %d\n", GetLastError() );
  142. }
  143. //
  144. // Ready to actually look for one of these ...
  145. //
  146. while ( WSALookupServiceNext( hRnr,
  147. 0,
  148. &dwQuerySize,
  149. Query ) == NO_ERROR )
  150. {
  151. printf( "Next got %ws\n", Query->lpszServiceInstanceName );
  152. }
  153. printf( "Next finished with %d\n", GetLastError() );
  154. //
  155. // done.
  156. //
  157. if( WSALookupServiceEnd( hRnr ) )
  158. {
  159. printf( "ServiceEnd failed %d\n", GetLastError() );
  160. }
  161. WSACleanup();
  162. return(0);
  163. }