diff --git a/src/Socket.php b/src/Socket.php index 05400b4..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 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)); } public function send($data, $remoteAddress = null) @@ -102,16 +98,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(); }