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.

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