Skip to content

Commit

Permalink
InternetAddress is no longer mutable
Browse files Browse the repository at this point in the history
closes #45
  • Loading branch information
dktapps committed May 8, 2021
1 parent 9d6f564 commit 6c83e0f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/generic/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ class Socket{
*/
public function __construct(InternetAddress $bindAddress){
$this->bindAddress = $bindAddress;
$socket = @socket_create($bindAddress->version === 4 ? AF_INET : AF_INET6, SOCK_DGRAM, SOL_UDP);
$socket = @socket_create($bindAddress->getVersion() === 4 ? AF_INET : AF_INET6, SOCK_DGRAM, SOL_UDP);
if($socket === false){
throw new \RuntimeException("Failed to create socket: " . trim(socket_strerror(socket_last_error())));
}
$this->socket = $socket;

if($bindAddress->version === 6){
if($bindAddress->getVersion() === 6){
socket_set_option($this->socket, IPPROTO_IPV6, IPV6_V6ONLY, 1); //Don't map IPv4 to IPv6, the implementation can create another RakLib instance to handle IPv4
}

if(@socket_bind($this->socket, $bindAddress->ip, $bindAddress->port) === true){
if(@socket_bind($this->socket, $bindAddress->getIp(), $bindAddress->getPort()) === true){
$this->setSendBuffer(1024 * 1024 * 8)->setRecvBuffer(1024 * 1024 * 8);
}else{
$error = socket_last_error($this->socket);
Expand Down
17 changes: 9 additions & 8 deletions src/protocol/PacketSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,27 @@ public function putString(string $v) : void{
}

public function putAddress(InternetAddress $address) : void{
$this->putByte($address->version);
if($address->version === 4){
$parts = explode(".", $address->ip);
$version = $address->getVersion();
$this->putByte($version);
if($version === 4){
$parts = explode(".", $address->getIp());
assert(count($parts) === 4, "Wrong number of parts in IPv4 IP, expected 4, got " . count($parts));
foreach($parts as $b){
$this->putByte((~((int) $b)) & 0xff);
}
$this->putShort($address->port);
}elseif($address->version === 6){
$this->putShort($address->getPort());
}elseif($version === 6){
$this->putLShort(AF_INET6);
$this->putShort($address->port);
$this->putShort($address->getPort());
$this->putInt(0);
$rawIp = inet_pton($address->ip);
$rawIp = inet_pton($address->getIp());
if($rawIp === false){
throw new \InvalidArgumentException("Invalid IPv6 address could not be encoded");
}
$this->put($rawIp);
$this->putInt(0);
}else{
throw new \InvalidArgumentException("IP version $address->version is not supported");
throw new \InvalidArgumentException("IP version $version is not supported");
}
}
}
30 changes: 12 additions & 18 deletions src/server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ class Server implements ServerInterface{
/** @var int */
protected $maxMtuSize;

/** @var InternetAddress */
protected $reusableAddress;

/** @var int */
protected $nextSessionId = 0;

Expand All @@ -122,8 +119,6 @@ public function __construct(int $serverId, \Logger $logger, Socket $socket, int
$this->startTimeMS = (int) (microtime(true) * 1000);

$this->unconnectedMessageHandler = new UnconnectedMessageHandler($this, $protocolAcceptor);

$this->reusableAddress = clone $this->socket->getBindAddress();
}

/**
Expand All @@ -134,7 +129,7 @@ public function getRakNetTimeMS() : int{
}

public function getPort() : int{
return $this->socket->getBindAddress()->port;
return $this->socket->getBindAddress()->getPort();
}

public function getMaxMtuSize() : int{
Expand Down Expand Up @@ -225,10 +220,8 @@ private function tick() : void{

/** @phpstan-impure */
private function receivePacket() : bool{
$address = $this->reusableAddress;

try{
$buffer = $this->socket->readPacket($address->ip, $address->port);
$buffer = $this->socket->readPacket($addressIp, $addressPort);
}catch(SocketException $e){
$error = $e->getCode();
if($error === SOCKET_ECONNRESET){ //client disconnected improperly, maybe crash or lost connection
Expand All @@ -244,23 +237,24 @@ private function receivePacket() : bool{
$len = strlen($buffer);

$this->receiveBytes += $len;
if(isset($this->block[$address->ip])){
if(isset($this->block[$addressIp])){
return true;
}

if(isset($this->ipSec[$address->ip])){
if(++$this->ipSec[$address->ip] >= $this->packetLimit){
$this->blockAddress($address->ip);
if(isset($this->ipSec[$addressIp])){
if(++$this->ipSec[$addressIp] >= $this->packetLimit){
$this->blockAddress($addressIp);
return true;
}
}else{
$this->ipSec[$address->ip] = 1;
$this->ipSec[$addressIp] = 1;
}

if($len < 1){
return true;
}

$address = new InternetAddress($addressIp, $addressPort, $this->socket->getBindAddress()->getVersion());
try{
$session = $this->getSessionByAddress($address);
if($session !== null){
Expand All @@ -283,7 +277,7 @@ private function receivePacket() : bool{
foreach($this->rawPacketFilters as $pattern){
if(preg_match($pattern, $buffer) > 0){
$handled = true;
$this->eventListener->onRawPacketReceive($address->ip, $address->port, $buffer);
$this->eventListener->onRawPacketReceive($address->getIp(), $address->getPort(), $buffer);
break;
}
}
Expand All @@ -307,7 +301,7 @@ private function receivePacket() : bool{
}else{
$logFn();
}
$this->blockAddress($address->ip, 5);
$this->blockAddress($address->getIp(), 5);
}

return true;
Expand All @@ -317,7 +311,7 @@ public function sendPacket(Packet $packet, InternetAddress $address) : void{
$out = new PacketSerializer(); //TODO: reusable streams to reduce allocations
$packet->encode($out);
try{
$this->sendBytes += $this->socket->writePacket($out->getBuffer(), $address->ip, $address->port);
$this->sendBytes += $this->socket->writePacket($out->getBuffer(), $address->getIp(), $address->getPort());
}catch(SocketException $e){
$this->logger->debug($e->getMessage());
}
Expand Down Expand Up @@ -413,7 +407,7 @@ private function removeSessionInternal(Session $session) : void{

public function openSession(Session $session) : void{
$address = $session->getAddress();
$this->eventListener->onClientConnect($session->getInternalId(), $address->ip, $address->port, $session->getID());
$this->eventListener->onClientConnect($session->getInternalId(), $address->getIp(), $address->getPort(), $session->getID());
}

private function checkSessions() : void{
Expand Down
2 changes: 1 addition & 1 deletion src/server/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private function handleEncapsulatedPacketRoute(EncapsulatedPacket $packet) : voi
$dataPacket = new NewIncomingConnection();
$dataPacket->decode(new PacketSerializer($packet->buffer));

if($dataPacket->address->port === $this->server->getPort() or !$this->server->portChecking){
if($dataPacket->address->getPort() === $this->server->getPort() or !$this->server->portChecking){
$this->state = self::STATE_CONNECTED; //FINALLY!
$this->isTemporal = false;
$this->server->openSession($this);
Expand Down
4 changes: 2 additions & 2 deletions src/server/UnconnectedMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool
$this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), false, $packet->mtuSize + 28), $address);
}
}elseif($packet instanceof OpenConnectionRequest2){
if($packet->serverAddress->port === $this->server->getPort() or !$this->server->portChecking){
if($packet->serverAddress->getPort() === $this->server->getPort() or !$this->server->portChecking){
if($packet->mtuSize < Session::MIN_MTU_SIZE){
$this->server->getLogger()->debug("Not creating session for $address due to bad MTU size $packet->mtuSize");
return true;
Expand All @@ -93,7 +93,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool
$this->server->sendPacket(OpenConnectionReply2::create($this->server->getID(), $address, $mtuSize, false), $address);
$this->server->createSession($address, $packet->clientID, $mtuSize);
}else{
$this->server->getLogger()->debug("Not creating session for $address due to mismatched port, expected " . $this->server->getPort() . ", got " . $packet->serverAddress->port);
$this->server->getLogger()->debug("Not creating session for $address due to mismatched port, expected " . $this->server->getPort() . ", got " . $packet->serverAddress->getPort());
}
}else{
return false;
Expand Down
8 changes: 4 additions & 4 deletions src/utils/InternetAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

namespace raklib\utils;

class InternetAddress{
final class InternetAddress{

/**
* @var string
*/
public $ip;
private $ip;
/**
* @var int
*/
public $port;
private $port;
/**
* @var int
*/
public $version;
private $version;

public function __construct(string $address, int $port, int $version){
$this->ip = $address;
Expand Down

0 comments on commit 6c83e0f

Please sign in to comment.