From b1fce6a668f58c884a357f706d3e0b88c447d2ef Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 4 Mar 2026 20:47:48 +0200 Subject: [PATCH 01/10] fix ipv6/xash3d-master compatibility; update method name --- src/Xash3D/Master.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index d710254..531396e 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -35,10 +35,10 @@ class Master } } - public function getServersIPv6( + public function getServers( int $limit = 100, string $region = "\xFF", - string $host = "0.0.0.0:0", + string $host = "0.0.0.0", int $port = 0, string $gamedir = "valve" ): ?array @@ -76,7 +76,7 @@ class Master } // Filter query - if (false === fwrite($socket, "1{$region}{$host}:{$port}\0\gamedir\t{$gamedir}\0")) + if (false === fwrite($socket, fwrite($socket, "1{$region}{$host}:{$port}\0\\gamedir\\{$gamedir}\0"))) { $this->_errors[] = _('Could not send socket query'); From 82c8564645da6193710d1c1fd26e8af5f9971396 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 4 Mar 2026 20:50:03 +0200 Subject: [PATCH 02/10] fix syntax error --- src/Xash3D/Master.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index 531396e..08cbc2a 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -76,7 +76,7 @@ class Master } // Filter query - if (false === fwrite($socket, fwrite($socket, "1{$region}{$host}:{$port}\0\\gamedir\\{$gamedir}\0"))) + if (false === fwrite($socket, "1{$region}{$host}:{$port}\0\\gamedir\\{$gamedir}\0")) { $this->_errors[] = _('Could not send socket query'); From bacfce5f1bb057180f9350968b9b2e20066822ee Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 4 Mar 2026 21:24:12 +0200 Subject: [PATCH 03/10] reorder arguments --- src/Xash3D/Master.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index 08cbc2a..42b14cc 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -37,10 +37,10 @@ class Master public function getServers( int $limit = 100, - string $region = "\xFF", string $host = "0.0.0.0", int $port = 0, - string $gamedir = "valve" + string $gamedir = "valve", + string $region = "\xFF" ): ?array { // Init connection From 167f092a28ba7523dee0f8a8f16ab76685dd78bb Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 4 Mar 2026 22:32:21 +0200 Subject: [PATCH 04/10] continue, not break --- src/Xash3D/Master.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index 42b14cc..2714627 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -107,13 +107,13 @@ class Master // Get host if (false === $host = fread($socket, 16)) { - break; + continue; } // Is end of packet if (true === str_ends_with(bin2hex($host), bin2hex("\0\0\0\0\0\0"))) { - break; + continue; } // Skip invalid host value From 34390a0713fe00214df17d07e3c3f7691a006225 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 4 Mar 2026 22:56:37 +0200 Subject: [PATCH 05/10] simplify port decoding --- src/Xash3D/Master.php | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index 2714627..5ef7d01 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -125,29 +125,14 @@ class Master continue; } - // Decode first byte of port - if (false === $byte1 = fread($socket, 1)) - { - // Shift port byte - fread($socket, 1); - - continue; - } - - // Decode second byte of port - if (false === $byte2 = fread($socket, 1)) - { - continue; - } - // Calculate port value - $port = ord($byte1) * 256 + ord($byte2); + $port = unpack('nport', fread($socket, 2)); // Validate IPv6 result if ( false !== strpos($host, '.') || // filter_var not always works with mixed IPv6 false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || - false === filter_var($port, FILTER_VALIDATE_INT) + false === filter_var($port, FILTER_VALIDATE_INT) // @TODO ) { continue; From 13a7c607e11dd7e1c11be59af05738fd72b58ae3 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 4 Mar 2026 23:19:46 +0200 Subject: [PATCH 06/10] update parsing conditions, remove hardcoded ipv6 impl --- src/Xash3D/Master.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index 5ef7d01..fcf951c 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -107,13 +107,13 @@ class Master // Get host if (false === $host = fread($socket, 16)) { - continue; + break; } // Is end of packet if (true === str_ends_with(bin2hex($host), bin2hex("\0\0\0\0\0\0"))) { - continue; + break; } // Skip invalid host value @@ -126,22 +126,27 @@ class Master } // Calculate port value - $port = unpack('nport', fread($socket, 2)); - - // Validate IPv6 result - if ( - false !== strpos($host, '.') || // filter_var not always works with mixed IPv6 - false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || - false === filter_var($port, FILTER_VALIDATE_INT) // @TODO - ) + if (false === $port = fread($socket, 2)) { continue; } - $servers["[{$host}]:{$port}"] = // keep unique + if (false === $port = unpack('nport', $port)) + { + continue; + } + + // Validate IPv6 result + if ((false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && + false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) || empty($port['port'])) + { + continue; + } + + $servers["[{$host}]:{$port['port']}"] = // keep unique [ 'host' => $host, - 'port' => $port + 'port' => $port['port'] ]; } From a101a55715c5bdee7c865dd779994b805f91ded4 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 5 Mar 2026 00:42:21 +0200 Subject: [PATCH 07/10] revert getServersIPv6 method as mixed address families are not supported by legacy protocol implementation --- src/Xash3D/Master.php | 66 +++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index fcf951c..238ff4e 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -35,9 +35,11 @@ class Master } } - public function getServers( + // Legacy protocol implementation does not support mixed address families + // in the binary master socket response, use separated method for IPv4 servers. + public function getServersIPv6( int $limit = 100, - string $host = "0.0.0.0", + string $host = "[::]", int $port = 0, string $gamedir = "valve", string $region = "\xFF" @@ -52,6 +54,8 @@ class Master $this->_timeout ); + $master = "{$this->_host}:{$this->_port}"; + // Is connected if (true === is_resource($socket)) { @@ -64,7 +68,7 @@ class Master else { $this->_errors[] = sprintf( - _('Connection error: %s'), + _("Connection error for $master: %s"), $message ); @@ -78,7 +82,7 @@ class Master // Filter query if (false === fwrite($socket, "1{$region}{$host}:{$port}\0\\gamedir\\{$gamedir}\0")) { - $this->_errors[] = _('Could not send socket query'); + $this->_errors[] = _("Could not send socket query for $master"); $this->_fclose( $socket @@ -90,7 +94,7 @@ class Master // Skip header if (false === fread($socket, 6)) { - $this->_errors[] = _('Could not init packet header'); + $this->_errors[] = _("Could not init packet header for $master"); $this->_fclose( $socket @@ -104,49 +108,51 @@ class Master for ($i = 0; $i < $limit; $i++) { - // Get host - if (false === $host = fread($socket, 16)) + // Get host bytes + if (false === $h = fread($socket, 16)) + { + $this->_errors[] = _("Invalid `host` fragment in packet at $i for $master"); + break; + } + + // End of packet + if (true === str_ends_with(bin2hex($h), bin2hex("\0\0\0\0\0\0"))) { break; } - // Is end of packet - if (true === str_ends_with(bin2hex($host), bin2hex("\0\0\0\0\0\0"))) + // Get host string + if (false === $h = inet_ntop($h)) { + $this->_errors[] = _("Invalid `host` value in packet at $i for $master"); break; } - // Skip invalid host value - if (false === $host = inet_ntop($host)) + // Get port bytes + if (false === $p = fread($socket, 2)) { - // Shift port bytes - fread($socket, 2); + $this->_errors[] = _("Invalid `port` fragment in packet at $i for $master"); + break; + } + // Get port value + if (false === $p = unpack('nport', $p)) + { + $this->_errors[] = _("Invalid `port` value in packet at $i for $master"); continue; } - // Calculate port value - if (false === $port = fread($socket, 2)) + // Validate result + if (false === filter_var($h, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || empty($p['port'])) { + $this->_errors[] = _("Invalid socket address in packet at $i for $master"); continue; } - if (false === $port = unpack('nport', $port)) - { - continue; - } - - // Validate IPv6 result - if ((false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && - false === filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) || empty($port['port'])) - { - continue; - } - - $servers["[{$host}]:{$port['port']}"] = // keep unique + $servers["{$h}{$p['port']}"] = // keep unique [ - 'host' => $host, - 'port' => $port['port'] + 'host' => $h, + 'port' => $p['port'] ]; } From 795f3047895a875c35f426c5f9d8649967f7b3db Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 5 Mar 2026 01:08:13 +0200 Subject: [PATCH 08/10] use enumeration types --- src/Xash3D/Master.php | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index 238ff4e..b05e201 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -4,6 +4,21 @@ declare(strict_types=1); namespace Yggverse\Hl\Xash3D; +enum Family: int { + case IPv4 = 4; + case IPv6 = 16; +} + +enum Region: string { + case Europe = "\x03"; + case US_East = "\x00"; + case World = "\xFF"; +} + +enum Game: string { + case Valve = "valve"; +} + class Master { private string $_host; @@ -14,7 +29,7 @@ class Master public function __construct( string $host, - int $port, + int $port = 27010, int $timeout = 5 ) { @@ -37,14 +52,16 @@ class Master // Legacy protocol implementation does not support mixed address families // in the binary master socket response, use separated method for IPv4 servers. - public function getServersIPv6( + public function getServers( int $limit = 100, - string $host = "[::]", + string $host = "0.0.0.0", int $port = 0, - string $gamedir = "valve", - string $region = "\xFF" + Game $game = Game::Valve, + Region $region = Region::World ): ?array { + $family = filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? Family::IPv4 : Family::IPv6; + // Init connection $socket = fsockopen( "udp://{$this->_host}", @@ -80,7 +97,7 @@ class Master } // Filter query - if (false === fwrite($socket, "1{$region}{$host}:{$port}\0\\gamedir\\{$gamedir}\0")) + if (false === fwrite($socket, "1{$region->value}{$host}:{$port}\0\\gamedir\\{$game->value}\0")) { $this->_errors[] = _("Could not send socket query for $master"); @@ -109,20 +126,20 @@ class Master for ($i = 0; $i < $limit; $i++) { // Get host bytes - if (false === $h = fread($socket, 16)) + if (false === $host = fread($socket, $family->value)) { $this->_errors[] = _("Invalid `host` fragment in packet at $i for $master"); break; } // End of packet - if (true === str_ends_with(bin2hex($h), bin2hex("\0\0\0\0\0\0"))) + if (true === str_ends_with(bin2hex($host), bin2hex("\0\0\0\0\0\0"))) { break; } // Get host string - if (false === $h = inet_ntop($h)) + if (false === $host = inet_ntop($host)) { $this->_errors[] = _("Invalid `host` value in packet at $i for $master"); break; @@ -143,15 +160,16 @@ class Master } // Validate result - if (false === filter_var($h, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || empty($p['port'])) + if (false === filter_var($host, FILTER_VALIDATE_IP, $family == Family::IPv6 ? FILTER_FLAG_IPV6 + : FILTER_FLAG_IPV4) || empty($p['port'])) { $this->_errors[] = _("Invalid socket address in packet at $i for $master"); continue; } - $servers["{$h}{$p['port']}"] = // keep unique + $servers["{$host}{$p['port']}"] = // keep unique [ - 'host' => $h, + 'host' => $host, 'port' => $p['port'] ]; } From 9f4e92459283f6775127f0bba8fad0ed1ce88dbf Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 5 Mar 2026 01:11:24 +0200 Subject: [PATCH 09/10] use break as nothing to continue on any piece fail --- src/Xash3D/Master.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index b05e201..f0aafe4 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -156,7 +156,7 @@ class Master if (false === $p = unpack('nport', $p)) { $this->_errors[] = _("Invalid `port` value in packet at $i for $master"); - continue; + break; } // Validate result @@ -164,7 +164,7 @@ class Master : FILTER_FLAG_IPV4) || empty($p['port'])) { $this->_errors[] = _("Invalid socket address in packet at $i for $master"); - continue; + break; } $servers["{$host}{$p['port']}"] = // keep unique From 42b1195b83f10f8e6054a2c45e9181c3a8910292 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 5 Mar 2026 03:26:58 +0200 Subject: [PATCH 10/10] add all supported regions --- src/Xash3D/Master.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Xash3D/Master.php b/src/Xash3D/Master.php index f0aafe4..5ea743e 100644 --- a/src/Xash3D/Master.php +++ b/src/Xash3D/Master.php @@ -10,9 +10,15 @@ enum Family: int { } enum Region: string { - case Europe = "\x03"; - case US_East = "\x00"; - case World = "\xFF"; + case USEastCoast = "0x00"; + case USWestCoast = "0x01"; + case SouthAmerica = "0x02"; + case Europe = "0x03"; + case Asia = "0x04"; + case Australia = "0x05"; + case MiddleEast = "0x06"; + case Africa = "0x07"; + case World = "0xff"; } enum Game: string {