Implemented IPv6 support (#4554)

This commit is contained in:
Dylan T
2021-11-08 20:03:28 +00:00
committed by GitHub
parent eb9012401b
commit cc4bb91fcb
7 changed files with 58 additions and 23 deletions

View File

@@ -88,7 +88,7 @@ class RakLibInterface implements ServerEventListener, AdvancedNetworkInterface{
/** @var PacketBroadcaster */
private $broadcaster;
public function __construct(Server $server){
public function __construct(Server $server, string $ip, int $port, bool $ipV6){
$this->server = $server;
$this->rakServerId = mt_rand(0, PHP_INT_MAX);
@@ -101,7 +101,7 @@ class RakLibInterface implements ServerEventListener, AdvancedNetworkInterface{
$this->server->getLogger(),
$mainToThreadBuffer,
$threadToMainBuffer,
new InternetAddress($this->server->getIp(), $this->server->getPort(), 4),
new InternetAddress($ip, $port, $ipV6 ? 6 : 4),
$this->rakServerId,
$this->server->getConfigGroup()->getPropertyInt("network.max-mtu-size", 1492),
self::MCPE_RAKNET_PROTOCOL_VERSION,

View File

@@ -34,11 +34,14 @@ use function socket_recvfrom;
use function socket_select;
use function socket_sendto;
use function socket_set_nonblock;
use function socket_set_option;
use function socket_strerror;
use function strlen;
use function time;
use function trim;
use const AF_INET;
use const IPPROTO_IPV6;
use const IPV6_V6ONLY;
use const PHP_INT_MAX;
use const SOCK_DGRAM;
use const SOCKET_EADDRINUSE;
@@ -74,15 +77,18 @@ final class DedicatedQueryNetworkInterface implements AdvancedNetworkInterface{
/** @var string[] */
private $rawPacketPatterns = [];
public function __construct(string $ip, int $port, \Logger $logger){
public function __construct(string $ip, int $port, bool $ipV6, \Logger $logger){
$this->ip = $ip;
$this->port = $port;
$this->logger = $logger;
$socket = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$socket = @socket_create($ipV6 ? AF_INET6 : AF_INET, SOCK_DGRAM, SOL_UDP);
if($socket === false){
throw new \RuntimeException("Failed to create socket");
}
if($ipV6){
socket_set_option($socket, IPPROTO_IPV6, IPV6_V6ONLY, 1); //disable linux's cool but annoying ipv4-over-ipv6 network stack
}
$this->socket = $socket;
}

View File

@@ -27,7 +27,6 @@ declare(strict_types=1);
*/
namespace pocketmine\network\query;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\network\AdvancedNetworkInterface;
use pocketmine\network\RawPacketHandler;
use pocketmine\Server;
@@ -57,8 +56,6 @@ class QueryHandler implements RawPacketHandler{
public function __construct(Server $server){
$this->server = $server;
$this->logger = new \PrefixedLogger($this->server->getLogger(), "Query Handler");
$addr = $this->server->getIp();
$port = $this->server->getPort();
/*
The Query protocol is built on top of the existing Minecraft PE UDP network stack.
@@ -71,7 +68,6 @@ class QueryHandler implements RawPacketHandler{
$this->regenerateToken();
$this->lastToken = $this->token;
$this->logger->info($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_server_query_running($addr, (string) $port)));
}
public function getPattern() : string{