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