Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly format IPv6 addresses and return null for unknown addresses #14

Merged
merged 2 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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()
Expand All @@ -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();
}
Expand Down