Skip to content

Commit

Permalink
Properly handle bzflag server query errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kongr45gpen committed Dec 11, 2015
1 parent 32ec36b commit 4a8d0f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
13 changes: 6 additions & 7 deletions includes/bzfquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function bzfquery ($hostport) {
$get_prot = getprotobyname($protocol);
if ($get_prot == -1) {
// if nothing found, returns -1
echo 'Invalid Protocol';
$server['error'] = 'Invalid Protocol';
return $server;
}
if (!$server['port']) {
Expand All @@ -79,9 +79,9 @@ function bzfquery ($hostport) {
$server['port'] = getservbyname($server['port'], $protocol);
}
$server['ip'] = gethostbyname($server['host']);
$fp = fsockopen($server['host'], $server['port'], $errno, $errstr, 5);
$fp = @fsockopen($server['host'], $server['port'], $errno, $errstr, 5);
if (!$fp) {
echo "$errstr ($errno)\n";
$server['error'] = $errstr;
return $server;
}

Expand All @@ -90,20 +90,19 @@ function bzfquery ($hostport) {
$buffer=fread($fp, 9);
//var_dump($buffer);
if (strlen($buffer) != 9) {
echo "not a bzflag server";
return $server;
$server['error'] = "not a bzflag server";
}
# parse reply
$server += unpack("a4magic/a4protocol/Cid", $buffer);
//var_dump($server);
if ($server['magic'] != "BZFS") {
echo "not a bzflag server\n";
$server['error'] = "not a bzflag server";
fclose($fp);
return $server;
}

if ($server['protocol'] != '0221') {
echo "incompatible version\n";
$server['error'] = "incompatible version";
fclose($fp);
return $server;
}
Expand Down
44 changes: 19 additions & 25 deletions models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,34 +136,28 @@ public static function addServer($name, $domain, $port, $country, $owner)
*/
public function forceUpdate()
{
$this->info = @bzfquery($this->address);
$this->info = bzfquery($this->getAddress());
$this->updated = TimeDate::now();
$this->db->query("UPDATE servers SET info = ?, updated = UTC_TIMESTAMP() WHERE id = ?", "si", array(serialize($this->info), $this->id));

$this->updateOnline();

return $this;
}

/**
* Checks if the server is online (listed on the public list server)
* @todo Fix performance issues (many calls to the list server)
* @return self
*/
private function updateOnline()
{
$online = false;
$listServer = Service::getParameter('bzion.miscellaneous.list_server');
$servers = file($listServer);

foreach ($servers as $server) {
list($host, $protocol, $hex, $ip, $title) = explode(' ', $server, 5);
if ($this->getAddress() == $host) {
$online = true;
}
$this->online = !isset($this->info['error']);

$this->db->query(
"UPDATE servers SET info = ?, online = ?, updated = UTC_TIMESTAMP() WHERE id = ?",
"sii",
array(serialize($this->info), $this->online, $this->id)
);

// If a server is offline, log it
if (!$this->online) {
if ($logger = \Service::getContainer()->get('logger')) {
$id = $this->getId();
$address = $this->getAddress();
$reason = $this->info['error'];

$logger->notice("Connection to server #$id ($address) failed: $reason");
}
}

return $this->updateProperty($this->online, 'online', $online, 'i');
return $this;
}

/**
Expand Down

0 comments on commit 4a8d0f6

Please sign in to comment.