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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* XEvil(TM) Copyright (C) 1994,2000 Steve Hardt and Michael Judge
* http://www.xevil.com
* satan@xevil.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program, the file "gpl.txt"; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA, or visit http://www.gnu.org.
*/
// "serverping.cpp"
// Although this is currently in the X11 directory, it is all cross-platform
// code and should be reasonably easy to compile on Windows.
#include <iostream>
#include <sstream>
#include "utils.h"
#include "streams.h"
#include "xetp_basic.h"
#include "neth.h"
#include "id.h"
using namespace std;
class ServerPing {
public:
ServerPing(int argc,char** argv);
~ServerPing();
void go();
private:
enum {
COUNT_DEFAULT = 3,
TIMEOUT_DEFAULT = 1000,
};
void message(const char* msg);
void error(const char* msg);
void error(const char* msg1,const char* msg2);
void print_usage_and_exit();
void print_results_and_exit();
/* REQUIRES: a XETPBasic::SERVER_PONG header was just received on
udpIn. */
/* EFFECTS: Print the results of a SERVER_PONG, reading data from
udpIn. */
int count; // Number of pings to send.
int timeout; // ms
char* serverName;
CMN_SOCKET udpSock;
CMN_PORT clientPort;
CMN_PORT serverPort;
CMN_SOCKADDR_IN serverAddr;
UDPInStreamP udpIn;
UDPOutStreamP udpOut;
};
ServerPing::ServerPing(int argc,char** argv) {
XETPBasic::check_sizes();
count = COUNT_DEFAULT;
timeout = TIMEOUT_DEFAULT;
// Means not set.
clientPort = 0;
// Parse arguments.
int n;
const char* value;
for (n = 0; n < argc; n++) {
if (value = Utils::arg_value_check(n,argc,argv,"-count")) {
count = Utils::atoi(value);
}
if (value = Utils::arg_value_check(n,argc,argv,"-timeout")) {
timeout = Utils::atoi(value);
}
if (Utils::arg_name_check(n,argc,argv,"-h") ||
Utils::arg_name_check(n,argc,argv,"-help")) {
print_usage_and_exit();
}
if (value = Utils::arg_value_check(n,argc,argv,"-client_port")) {
clientPort = Utils::atoi(value);
}
}
if (argc < 2) {
print_usage_and_exit();
}
char* sString = argv[argc - 1];
char* port = Utils::strchr(sString,':');
// server:port
if (port) {
serverName = new char[port - sString + 1];
assert(serverName);
Utils::strncpy(serverName,sString,port - sString);
serverName[port - sString] = '\0';
serverPort = (CMN_PORT)Utils::atoi(port + 1);
}
// No port specified, use default.
else {
serverName = Utils::strdup(sString);
serverPort = XETPBasic::DEFAULT_PORT;
}
// If client port wasn't set on the command line.
if (clientPort == 0) {
// Don't use +1, may conflict with XEvil client on this machine.
clientPort = XETPBasic::DEFAULT_PORT + 7;
}
// Set up sockets and streams.
udpSock = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSock < 0) {
error("Error opening client UDP socket.");
}
stringstream str;
str << "Looking up IP address for server " << serverName;
message(str.str().c_str());
// Create server address.
memset((void *)&serverAddr,'\0',sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
struct hostent *hp = gethostbyname(serverName);
if (hp == 0) {
error("Invalid server name ",serverName);
return;
}
memcpy((void *)&serverAddr.sin_addr,(void *)hp->h_addr,hp->h_length);
serverAddr.sin_port = htons((u_short)serverPort);
#if 0
ostrstream str2;
str2 << serverName << " has address "
<< hex << serverAddr.sin_addr.s_addr << ends;
message(str2.str());
delete str2.str();
#endif
// Bind local address for UDP
CMN_SOCKADDR_IN client;
memset((void *)&client,'\0',sizeof(client));
client.sin_family = AF_INET;
client.sin_addr.s_addr = htonl(INADDR_ANY);
client.sin_port = htons((u_short)clientPort);
if (bind(udpSock,(CMN_SOCKADDR *)&client,sizeof(client)) < 0) {
stringstream str;
str << "Could not bind local UDP port " << clientPort;
error(str.str().c_str());
return;
}
// Create streams, only one will own the socket.
udpIn = new UDPInStream(udpSock,True);
udpOut = new UDPOutStream(udpSock,&serverAddr,False);
}
ServerPing::~ServerPing() {
delete udpOut;
delete udpIn;
delete serverName;
}
void ServerPing::go() {
// Send "count" number of SERVER_PINGs.
for (int tries = 0; tries < count; tries++) {
// Send the ping.
stringstream msg;
msg << "Sending XETP::SERVER_PING to "
<< serverName << ':' << serverPort;
message(msg.str().c_str());
XETPBasic::send_server_ping(udpOut);
// Don't buffer the ping.
udpOut->flush();
// Wait for return from server, for specified timeout.
fd_set fdvar;
FD_ZERO(&fdvar);
FD_SET(udpSock,&fdvar);
// Factor into seconds and microseconds.
int timeoutUS = 1000 * timeout;
struct timeval waitTime;
waitTime.tv_sec = timeoutUS / 1000000;
waitTime.tv_usec = timeoutUS % 1000000;
int cond = CMN_SELECT(udpSock + 1,&fdvar,NULL,NULL,&waitTime);
if (cond > 0 && FD_ISSET(udpSock,&fdvar)) {
// Prepare to read from the incoming packet.
CMN_SOCKADDR_IN addr;
if (udpIn->prepare_packet(&addr) < 0) {
error("Failed to read UDP packet.");
return;
}
// Should check that addr == serverAddr.
u_short method = 0;
u_int length = 0;
if (!XETPBasic::receive_header(udpIn,method,length)) {
error("Received invalid UDP header.");
return;
}
if (method == XETPBasic::SERVER_PONG) {
print_results_and_exit();
}
else {
message("Received packet other than SERVER_PONG.");
}
// Done with the packet.
// There might be more than one XETP packet in the UDP packet,
// who cares.
udpIn->done_packet();
}
}
// If we get here, we failed to reach the server.
stringstream msg;
msg << "No return from " << serverName << ':' << serverPort << " after "
<< count << " tries.";
error(msg.str().c_str());
}
void ServerPing::message(const char* msg) {
cout << msg << endl;
}
void ServerPing::error(const char* msg) {
cerr << "ERROR: " << msg << endl;
exit(1);
}
void ServerPing::error(const char* msg1,const char* msg2) {
stringstream str;
str << msg1 << msg2;
error(str.str().c_str());
}
void ServerPing::print_usage_and_exit() {
cout
<< "Serverping allows you to remotely check the status of an XEvil server."
<< endl
<< endl;
cout
<< "usage: serverping {-count num_tries} {-timeout timeout_ms} "
<< "server_name{:port}" << endl;
cout
<< " num_tries is the number of pings to send before giving up,"
<< " default=" << COUNT_DEFAULT << "." << endl;
cout
<< " timeout_ms is the time in milliseconds to wait for a response before"
<< endl
<< " giving up on each ping, default=" << TIMEOUT_DEFAULT << "."
<< endl;
cout
<< endl
<< "The output has the following form:" << endl
<< "SUCCESS: XEvil Server <server_name>:<server_port>" << endl
<< "GameStyle <game_style>" << endl
<< "MachinesPlaying <machines_num>" << endl
<< "HumansPlaying <humans_num>" << endl
<< "<server_OS_info>" << endl
<< "Name@Hostname HumanKills MachineKills" << endl
<< "<name>@<hostname> <human_kills> <machine_kills>" << endl
<< "<name>@<hostname> <human_kills> <machine_kills>" << endl
<< "... {line repeats for each human player}" << endl;
exit(1);
}
void ServerPing::print_results_and_exit() {
cout
<< endl
<< "SUCCESS: XEvil Server " << serverName << ":"
<< serverPort << endl;
GameStyleType gsType = (GameStyleType)udpIn->read_char();
cout << "GameStyle " << Utils::game_style_to_string(gsType) << endl;
int enemiesNum = udpIn->read_int();
cout << "MachinesPlaying " << enemiesNum << endl;
int humansNum = udpIn->read_short();
cout << "HumansPlaying " << humansNum << endl;
char* version = Utils::string_read(udpIn);
cout << version << endl;
delete version;
cout << "Name@Hostname HumanKills MachineKills" << endl;
// Read in data specific to each Human.
const int NAME_LENGTH = 40;
char name[NAME_LENGTH];
char clientName[NAME_LENGTH];
for (int n = 0; n < humansNum; n++) {
Utils::string_read(udpIn,name,NAME_LENGTH);
Utils::string_read(udpIn,clientName,NAME_LENGTH);
int humanKills = udpIn->read_int();
int enemyKills = udpIn->read_int();
Id id;
id.read(udpIn);
cout << '\"' << name << "\"@" << clientName << ' ' << humanKills
<< ' ' << enemyKills << endl;
}
exit(0);
}
main(int argc,char** argv) {
ServerPing ping(argc,argv);
ping.go();
}
|