From e6adf45e303c7727cf7fed1f43736d823f68789d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 21 Jan 2017 11:03:20 +0100 Subject: [PATCH 1/2] Properly format IPv6 addresses and return null for unknown addresses --- src/Socket.php | 19 +++++++++---------- tests/FactoryTest.php | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/Socket.php b/src/Socket.php index 05400b4..eae1d30 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -37,14 +37,14 @@ public function __construct(LoopInterface $loop, $socket, Buffer $buffer = null) public function getLocalAddress() { if ($this->socket !== false) { - return stream_socket_get_name($this->socket, false); + return $this->sanitizeAddress(stream_socket_get_name($this->socket, false)); } } public function getRemoteAddress() { if ($this->socket !== false) { - return stream_socket_get_name($this->socket, true); + return $this->sanitizeAddress(stream_socket_get_name($this->socket, true)); } } @@ -102,16 +102,15 @@ public function end() private function sanitizeAddress($address) { - // doc comment suggests IPv6 address is not enclosed in square brackets? + if ($address === false) { + return null; + } - $pos = strrpos($address, ':'); // this is an IPv6 address which includes colons but no square brackets - if ($pos !== false && substr($address, 0, 1) !== '[') { - if (strpos($address, ':') < $pos) { - $port = substr($address, $pos + 1); - $address = '[' . substr($address, 0, $pos) . ']:' . $port; - } - + $pos = strrpos($address, ':'); + if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') { + $port = substr($address, $pos + 1); + $address = '[' . substr($address, 0, $pos) . ']:' . $port; } return $address; } diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 49edade..2492722 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -25,7 +25,14 @@ public function testCreateClient() $capturedClient = Block\await($promise, $this->loop); $this->assertInstanceOf('React\Datagram\Socket', $capturedClient); + $this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress()); + + $this->assertContains('127.0.0.1:', $capturedClient->getLocalAddress()); + $this->assertNotEquals('127.0.0.1:12345', $capturedClient->getLocalAddress()); + $capturedClient->close(); + + $this->assertNull($capturedClient->getRemoteAddress()); } public function testCreateClientLocalhost() @@ -35,6 +42,11 @@ public function testCreateClientLocalhost() $capturedClient = Block\await($promise, $this->loop); $this->assertInstanceOf('React\Datagram\Socket', $capturedClient); + $this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress()); + + $this->assertContains('127.0.0.1:', $capturedClient->getLocalAddress()); + $this->assertNotEquals('127.0.0.1:12345', $capturedClient->getLocalAddress()); + $capturedClient->close(); } @@ -45,6 +57,11 @@ public function testCreateClientIpv6() $capturedClient = Block\await($promise, $this->loop); $this->assertInstanceOf('React\Datagram\Socket', $capturedClient); + $this->assertEquals('[::1]:12345', $capturedClient->getRemoteAddress()); + + $this->assertContains('[::1]:', $capturedClient->getLocalAddress()); + $this->assertNotEquals('[::1]:12345', $capturedClient->getLocalAddress()); + $capturedClient->close(); } @@ -55,7 +72,12 @@ public function testCreateServer() $capturedServer = Block\await($promise, $this->loop); $this->assertInstanceOf('React\Datagram\Socket', $capturedServer); + $this->assertEquals('127.0.0.1:12345', $capturedServer->getLocalAddress()); + $this->assertNull($capturedServer->getRemoteAddress()); + $capturedServer->close(); + + $this->assertNull($capturedServer->getLocalAddress()); } public function testCreateServerRandomPort() @@ -66,6 +88,7 @@ public function testCreateServerRandomPort() $this->assertInstanceOf('React\Datagram\Socket', $capturedServer); $this->assertNotEquals('127.0.0.1:0', $capturedServer->getLocalAddress()); + $this->assertNull($capturedServer->getRemoteAddress()); $capturedServer->close(); } From a6ac2362f1c65eb040256097b7a7d115fe2b5295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 22 Jan 2017 20:10:29 +0100 Subject: [PATCH 2/2] Accessing socket address must not report errors accross all PHP versions --- src/Socket.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Socket.php b/src/Socket.php index eae1d30..4039b98 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -36,16 +36,12 @@ public function __construct(LoopInterface $loop, $socket, Buffer $buffer = null) public function getLocalAddress() { - if ($this->socket !== false) { - return $this->sanitizeAddress(stream_socket_get_name($this->socket, false)); - } + return $this->sanitizeAddress(@stream_socket_get_name($this->socket, false)); } public function getRemoteAddress() { - if ($this->socket !== false) { - return $this->sanitizeAddress(stream_socket_get_name($this->socket, true)); - } + return $this->sanitizeAddress(@stream_socket_get_name($this->socket, true)); } public function send($data, $remoteAddress = null)