Skip to content

Commit e6adf45

Browse files
committed
Properly format IPv6 addresses and return null for unknown addresses
1 parent c7725b3 commit e6adf45

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/Socket.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ public function __construct(LoopInterface $loop, $socket, Buffer $buffer = null)
3737
public function getLocalAddress()
3838
{
3939
if ($this->socket !== false) {
40-
return stream_socket_get_name($this->socket, false);
40+
return $this->sanitizeAddress(stream_socket_get_name($this->socket, false));
4141
}
4242
}
4343

4444
public function getRemoteAddress()
4545
{
4646
if ($this->socket !== false) {
47-
return stream_socket_get_name($this->socket, true);
47+
return $this->sanitizeAddress(stream_socket_get_name($this->socket, true));
4848
}
4949
}
5050

@@ -102,16 +102,15 @@ public function end()
102102

103103
private function sanitizeAddress($address)
104104
{
105-
// doc comment suggests IPv6 address is not enclosed in square brackets?
105+
if ($address === false) {
106+
return null;
107+
}
106108

107-
$pos = strrpos($address, ':');
108109
// this is an IPv6 address which includes colons but no square brackets
109-
if ($pos !== false && substr($address, 0, 1) !== '[') {
110-
if (strpos($address, ':') < $pos) {
111-
$port = substr($address, $pos + 1);
112-
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
113-
}
114-
110+
$pos = strrpos($address, ':');
111+
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
112+
$port = substr($address, $pos + 1);
113+
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
115114
}
116115
return $address;
117116
}

tests/FactoryTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ public function testCreateClient()
2525
$capturedClient = Block\await($promise, $this->loop);
2626
$this->assertInstanceOf('React\Datagram\Socket', $capturedClient);
2727

28+
$this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress());
29+
30+
$this->assertContains('127.0.0.1:', $capturedClient->getLocalAddress());
31+
$this->assertNotEquals('127.0.0.1:12345', $capturedClient->getLocalAddress());
32+
2833
$capturedClient->close();
34+
35+
$this->assertNull($capturedClient->getRemoteAddress());
2936
}
3037

3138
public function testCreateClientLocalhost()
@@ -35,6 +42,11 @@ public function testCreateClientLocalhost()
3542
$capturedClient = Block\await($promise, $this->loop);
3643
$this->assertInstanceOf('React\Datagram\Socket', $capturedClient);
3744

45+
$this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress());
46+
47+
$this->assertContains('127.0.0.1:', $capturedClient->getLocalAddress());
48+
$this->assertNotEquals('127.0.0.1:12345', $capturedClient->getLocalAddress());
49+
3850
$capturedClient->close();
3951
}
4052

@@ -45,6 +57,11 @@ public function testCreateClientIpv6()
4557
$capturedClient = Block\await($promise, $this->loop);
4658
$this->assertInstanceOf('React\Datagram\Socket', $capturedClient);
4759

60+
$this->assertEquals('[::1]:12345', $capturedClient->getRemoteAddress());
61+
62+
$this->assertContains('[::1]:', $capturedClient->getLocalAddress());
63+
$this->assertNotEquals('[::1]:12345', $capturedClient->getLocalAddress());
64+
4865
$capturedClient->close();
4966
}
5067

@@ -55,7 +72,12 @@ public function testCreateServer()
5572
$capturedServer = Block\await($promise, $this->loop);
5673
$this->assertInstanceOf('React\Datagram\Socket', $capturedServer);
5774

75+
$this->assertEquals('127.0.0.1:12345', $capturedServer->getLocalAddress());
76+
$this->assertNull($capturedServer->getRemoteAddress());
77+
5878
$capturedServer->close();
79+
80+
$this->assertNull($capturedServer->getLocalAddress());
5981
}
6082

6183
public function testCreateServerRandomPort()
@@ -66,6 +88,7 @@ public function testCreateServerRandomPort()
6688
$this->assertInstanceOf('React\Datagram\Socket', $capturedServer);
6789

6890
$this->assertNotEquals('127.0.0.1:0', $capturedServer->getLocalAddress());
91+
$this->assertNull($capturedServer->getRemoteAddress());
6992

7093
$capturedServer->close();
7194
}

0 commit comments

Comments
 (0)