Team Fortress 2 Source Code as on 22/4/2020
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.

196 lines
4.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // vmpi_ping.cpp : Defines the entry point for the console application.
  9. //
  10. #include "stdafx.h"
  11. #include "iphelpers.h"
  12. #include "vmpi.h"
  13. #include "tier0/platform.h"
  14. #include "bitbuf.h"
  15. #include <conio.h>
  16. #include <stdlib.h>
  17. const char* FindArg( int argc, char **argv, const char *pName, const char *pDefault = "" )
  18. {
  19. for ( int i=0; i < argc; i++ )
  20. {
  21. if ( stricmp( argv[i], pName ) == 0 )
  22. {
  23. if ( (i+1) < argc )
  24. return argv[i+1];
  25. else
  26. return pDefault;
  27. }
  28. }
  29. return NULL;
  30. }
  31. int main(int argc, char* argv[])
  32. {
  33. CUtlVector<CIPAddr> addrs;
  34. printf( "\n" );
  35. printf( "vmpi_ping <option>\n" );
  36. printf( "option can be:\n" );
  37. printf( " -stop .. stop any VMPI services\n" );
  38. printf( " -kill .. kill any processes being run by VMPI\n" );
  39. printf( " -patch <timeout> .. stops VMPI services for <timeout> seconds\n" );
  40. printf( " -mpi_pw <password> .. only talk to services with the specified password\n" );
  41. printf( " -dns .. enable DNS lookups (slows the listing down)\n" );
  42. printf( " -ShowConsole .. show the console window\n" );
  43. printf( " -HideConsole .. hide the console window\n" );
  44. //Scary to show these to users...
  45. //printf( " -ShowCache .. show the cache directory and its capacity\n" );
  46. //printf( " -FlushCache .. flush the cache of ALL VMPI services\n" );
  47. printf( "\n" );
  48. ISocket *pSocket = CreateIPSocket();
  49. if ( !pSocket->BindToAny( 0 ) )
  50. {
  51. printf( "Error binding to a port!\n" );
  52. return 1;
  53. }
  54. const char *pPassword = FindArg( argc, argv, "-mpi_pw" );
  55. // Figure out which action they want to take.
  56. int timeout = 0;
  57. char cRequest = VMPI_PING_REQUEST;
  58. if ( FindArg( argc, argv, "-Stop" ) )
  59. {
  60. cRequest = VMPI_STOP_SERVICE;
  61. }
  62. else if ( FindArg( argc, argv, "-Kill" ) )
  63. {
  64. cRequest = VMPI_KILL_PROCESS;
  65. }
  66. /*
  67. else if ( FindArg( argc, argv, "-ShowConsole" ) )
  68. {
  69. cRequest = VMPI_SHOW_CONSOLE_WINDOW;
  70. }
  71. else if ( FindArg( argc, argv, "-HideConsole" ) )
  72. {
  73. cRequest = VMPI_HIDE_CONSOLE_WINDOW;
  74. }
  75. */
  76. else if ( FindArg( argc, argv, "-ShowCache" ) )
  77. {
  78. cRequest = VMPI_GET_CACHE_INFO;
  79. }
  80. else if ( FindArg( argc, argv, "-FlushCache" ) )
  81. {
  82. cRequest = VMPI_FLUSH_CACHE;
  83. }
  84. else
  85. {
  86. const char *pTimeout = FindArg( argc, argv, "-patch", "60" );
  87. if ( pTimeout )
  88. {
  89. if ( isdigit( pTimeout[0] ) )
  90. {
  91. cRequest = VMPI_SERVICE_PATCH;
  92. timeout = atoi( pTimeout );
  93. printf( "Patching with timeout of %d seconds.\n", timeout );
  94. }
  95. else
  96. {
  97. printf( "-patch requires a timeout parameter.\n" );
  98. return 1;
  99. }
  100. }
  101. }
  102. int nMachines = 0;
  103. printf( "Pinging VMPI Services... press a key to stop.\n\n" );
  104. while ( !kbhit() )
  105. {
  106. for ( int i=VMPI_SERVICE_PORT; i <= VMPI_LAST_SERVICE_PORT; i++ )
  107. {
  108. unsigned char data[256];
  109. bf_write buf( data, sizeof( data ) );
  110. buf.WriteByte( VMPI_PROTOCOL_VERSION );
  111. buf.WriteString( pPassword );
  112. buf.WriteByte( cRequest );
  113. if ( cRequest == VMPI_SERVICE_PATCH )
  114. buf.WriteLong( timeout );
  115. pSocket->Broadcast( data, buf.GetNumBytesWritten(), i );
  116. }
  117. while ( 1 )
  118. {
  119. CIPAddr ipFrom;
  120. char in[256];
  121. int len = pSocket->RecvFrom( in, sizeof( in ), &ipFrom );
  122. if ( len == -1 )
  123. break;
  124. if ( len >= 2 &&
  125. in[0] == VMPI_PROTOCOL_VERSION &&
  126. in[1] == VMPI_PING_RESPONSE &&
  127. addrs.Find( ipFrom ) == -1 )
  128. {
  129. char *pStateString = "(unknown)";
  130. if ( len >= 3 )
  131. {
  132. if ( in[2] )
  133. pStateString = "(running)";
  134. else
  135. pStateString = "(idle) ";
  136. }
  137. ++nMachines;
  138. char nameStr[256];
  139. if ( FindArg( argc, argv, "-dns" ) && ConvertIPAddrToString( &ipFrom, nameStr, sizeof( nameStr ) ) )
  140. {
  141. printf( "%02d. %s - %s:%d (%d.%d.%d.%d)",
  142. nMachines, pStateString, nameStr, ipFrom.port,
  143. ipFrom.ip[0], ipFrom.ip[1], ipFrom.ip[2], ipFrom.ip[3] );
  144. }
  145. else
  146. {
  147. printf( "%02d. %s - %d.%d.%d.%d:%d",
  148. nMachines, pStateString, ipFrom.ip[0], ipFrom.ip[1], ipFrom.ip[2], ipFrom.ip[3], ipFrom.port );
  149. }
  150. if ( cRequest == VMPI_GET_CACHE_INFO )
  151. {
  152. // Next var is a 64-bit int with the size of the cache.
  153. char *pCur = &in[3];
  154. __int64 cacheSize = *((__int64*)pCur);
  155. pCur += sizeof( __int64 );
  156. char *pCacheDir = pCur;
  157. __int64 nMegs = cacheSize / (1024*1024);
  158. printf( "\n\tCache dir: %s, size: %d megs", pCur, nMegs );
  159. }
  160. printf( "\n" );
  161. addrs.AddToTail( ipFrom );
  162. }
  163. }
  164. Sleep( 1000 );
  165. }
  166. return 0;
  167. }