Source code of Windows XP (NT5)
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.

353 lines
7.2 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. discover.c
  5. Abstract:
  6. Contains code that implements the service location apis.
  7. Author:
  8. Madan Appiah (madana) 15-May-1995
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. MuraliK 11-July-1995 Extended to discover any service besides Gateway
  13. --*/
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <svcloc.h>
  18. // consists of information map from service name to other details
  19. typedef struct _SERVICE_INFO {
  20. LPCSTR pszName;
  21. ULONGLONG ulMask;
  22. } SERVICE_INFO;
  23. SERVICE_INFO g_Services[] = {
  24. { "gopher", INET_GOPHER_SERVICE},
  25. { "http", INET_W3_SERVICE},
  26. { "w3", INET_W3_SERVICE},
  27. { "ftp", INET_FTP_SERVICE},
  28. { "gate", INET_GATEWAY_SERVICE},
  29. { "msn", INET_MSN_SERVICE}
  30. };
  31. # define NUM_SERVICES ( sizeof(g_Services)/ sizeof( SERVICE_INFO))
  32. ULONGLONG GetUlMaskForService( IN LPSTR pszServiceName)
  33. {
  34. DWORD i;
  35. for ( i = 0; i < NUM_SERVICES; i++) {
  36. if ( !stricmp( pszServiceName, g_Services[i].pszName)) {
  37. return ( g_Services[i].ulMask);
  38. }
  39. }
  40. return ( 0);
  41. } // GetUlMaskForService()
  42. VOID
  43. PrintServerInfo(
  44. DWORD Index,
  45. LPINET_SERVER_INFO ServerInfo
  46. )
  47. {
  48. DWORD j, k;
  49. printf("%ld. ServerName = %s \n ", Index+1, ServerInfo->ServerName );
  50. printf(" ServicesMask = %lx \n", (DWORD)ServerInfo->ServicesMask );
  51. printf(" NumServices = %ld \n", ServerInfo->Services.NumServices );
  52. for( j = 0; j < ServerInfo->Services.NumServices; j++ ) {
  53. LPINET_SERVICE_INFO ServiceInfo;
  54. ServiceInfo = ServerInfo->Services.Services[j];
  55. printf("\n");
  56. printf(" %ld. ServiceMask = %ld \n",
  57. j+1, (DWORD)ServiceInfo->ServiceMask );
  58. printf(" ServiceState = %ld \n",
  59. (DWORD)ServiceInfo->ServiceState );
  60. printf(" ServiceComment = %s \n",
  61. (DWORD)ServiceInfo->ServiceComment );
  62. printf(" NumBindings = %ld \n",
  63. ServiceInfo->Bindings.NumBindings );
  64. for( k = 0; k < ServiceInfo->Bindings.NumBindings; k++) {
  65. LPINET_BIND_INFO BindInfo;
  66. BindInfo = &ServiceInfo->Bindings.BindingsInfo[k];
  67. printf(" %ld. Bind (%ld) = %s\n",
  68. k+1,
  69. BindInfo->Length,
  70. (LPWSTR)BindInfo->BindData );
  71. }
  72. }
  73. printf("\n");
  74. return;
  75. }
  76. VOID
  77. PrintServersInfoList( IN LPINET_SERVERS_LIST pServersList)
  78. {
  79. DWORD i;
  80. for( i = 0; i < pServersList->NumServers; i++ ) {
  81. PrintServerInfo( i, pServersList->Servers[i] );
  82. }
  83. return;
  84. } // PrintServersInfoList()
  85. VOID
  86. PrintUsageMessage(IN LPCSTR pszProgName)
  87. {
  88. int i;
  89. printf( "Usage: %s [ -tTimeToWait] [ -sServerName] { service-names }\n",
  90. pszProgName);
  91. printf("\t Services supported are:\n");
  92. for (i = 0; i < NUM_SERVICES; i++) {
  93. printf( "\t\t %s\n", g_Services[i].pszName);
  94. }
  95. } // PrintUsageMessage()
  96. DWORD
  97. DiscoverServerInfo(
  98. LPSTR ServerName,
  99. ULONGLONG ulMask,
  100. DWORD dwWaitTime
  101. )
  102. {
  103. DWORD Error;
  104. LPINET_SERVER_INFO ServerInfo = NULL;
  105. if( dwWaitTime == 0 ) {
  106. Error = INetGetServerInfo(
  107. ServerName,
  108. ulMask,
  109. SVC_DEFAULT_WAIT_TIME,
  110. &ServerInfo );
  111. } else {
  112. Error = INetGetServerInfo(
  113. ServerName,
  114. ulMask,
  115. 0,
  116. &ServerInfo );
  117. if( (Error != ERROR_BAD_NETPATH ) && (Error != ERROR_SUCCESS) ) {
  118. return( Error );
  119. }
  120. //
  121. // display server info if it is found.
  122. //
  123. if( ServerInfo != NULL ) {
  124. //
  125. // display server info.
  126. //
  127. PrintServerInfo( 0, ServerInfo );
  128. INetFreeServerInfo( &ServerInfo );
  129. return( ERROR_SUCCESS );
  130. }
  131. //
  132. // wait for server response.
  133. //
  134. Sleep( dwWaitTime * 1000 );
  135. Error = INetGetServerInfo(
  136. ServerName,
  137. ulMask,
  138. 0,
  139. &ServerInfo );
  140. }
  141. if( Error != ERROR_SUCCESS ) {
  142. return( Error );
  143. }
  144. if( ServerInfo != NULL ) {
  145. PrintServerInfo( 0, ServerInfo );
  146. }
  147. else {
  148. printf( "INetGetServerInfo found no relevant servers\n");
  149. }
  150. //
  151. // free server info structure.
  152. //
  153. INetFreeServerInfo( &ServerInfo );
  154. return( ERROR_SUCCESS );
  155. }
  156. DWORD
  157. DiscoverInetServers(
  158. ULONGLONG ulMask,
  159. DWORD dwWaitTime
  160. )
  161. {
  162. DWORD Error;
  163. LPINET_SERVERS_LIST ServersList = NULL;
  164. if( dwWaitTime == 0 ) {
  165. Error = INetDiscoverServers(
  166. ulMask,
  167. SVC_DEFAULT_WAIT_TIME,
  168. &ServersList );
  169. } else {
  170. Error = INetDiscoverServers(
  171. ulMask,
  172. 0,
  173. &ServersList );
  174. if( Error != ERROR_SUCCESS ) {
  175. return( Error );
  176. }
  177. //
  178. // ignore first enum, must have zero entry.
  179. //
  180. INetFreeDiscoverServersList( &ServersList );
  181. Sleep( dwWaitTime * 1000 );
  182. Error = INetDiscoverServers(
  183. ulMask,
  184. 0,
  185. &ServersList );
  186. }
  187. if( Error != ERROR_SUCCESS ) {
  188. return( Error );
  189. }
  190. //
  191. // list server info.
  192. //
  193. if ( ServersList->NumServers != 0) {
  194. PrintServersInfoList( ServersList );
  195. } else {
  196. printf( "INetDiscoverServers() found no relevant servers\n");
  197. }
  198. //
  199. // free server info structure.
  200. //
  201. INetFreeDiscoverServersList( &ServersList );
  202. return( ERROR_SUCCESS );
  203. }
  204. VOID __cdecl
  205. main(
  206. int argc,
  207. char *argv[]
  208. )
  209. {
  210. DWORD Error;
  211. ULONGLONG ulMask = 0;
  212. int iArgs = 1;
  213. DWORD dwWaitTime = 0;
  214. LPSTR ServerName = NULL;
  215. while ( argv[iArgs] != NULL ){
  216. if( argv[iArgs][0] == '-' ) {
  217. switch ( argv[iArgs][1] ) {
  218. case 't':
  219. // get the wait time
  220. dwWaitTime = strtoul( argv[iArgs] + 2, NULL, 0);
  221. break;
  222. case 's':
  223. // get the server name.
  224. ServerName = argv[iArgs] + 2;
  225. break;
  226. default:
  227. PrintUsageMessage(argv[0]);
  228. exit(1);
  229. }
  230. }
  231. iArgs++; // skip one more argument
  232. }
  233. //
  234. // form the mask for all services
  235. //
  236. for ( iArgs = 1; iArgs < argc; iArgs++) {
  237. ulMask = ulMask | GetUlMaskForService( argv[iArgs]);
  238. } // for
  239. if ( ulMask == 0) {
  240. PrintUsageMessage(argv[0]);
  241. exit(1);
  242. }
  243. if( ServerName != NULL ) {
  244. Error = DiscoverServerInfo( ServerName, ulMask, dwWaitTime );
  245. }
  246. else {
  247. Error = DiscoverInetServers( ulMask, dwWaitTime );
  248. }
  249. if( Error != ERROR_SUCCESS ) {
  250. printf("%s failed with error, %ld.\n", argv[0], Error );
  251. return;
  252. }
  253. printf( "Command completed successfully.\n" );
  254. return;
  255. } // main()
  256. /*************************** End Of File **************************/