Skip to content

Commit fa4bf6e

Browse files
authored
Merge pull request #14 from clue-labs/address
Properly format IPv6 addresses and return null for unknown addresses
2 parents 31bcbd9 + a6ac236 commit fa4bf6e

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/Socket.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ public function __construct(LoopInterface $loop, $socket, Buffer $buffer = null)
3636

3737
public function getLocalAddress()
3838
{
39-
if ($this->socket !== false) {
40-
return stream_socket_get_name($this->socket, false);
41-
}
39+
return $this->sanitizeAddress(@stream_socket_get_name($this->socket, false));
4240
}
4341

4442
public function getRemoteAddress()
4543
{
46-
if ($this->socket !== false) {
47-
return stream_socket_get_name($this->socket, true);
48-
}
44+
return $this->sanitizeAddress(@stream_socket_get_name($this->socket, true));
4945
}
5046

5147
public function send($data, $remoteAddress = null)
@@ -102,16 +98,15 @@ public function end()
10298

10399
private function sanitizeAddress($address)
104100
{
105-
// doc comment suggests IPv6 address is not enclosed in square brackets?
101+
if ($address === false) {
102+
return null;
103+
}
106104

107-
$pos = strrpos($address, ':');
108105
// 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-
106+
$pos = strrpos($address, ':');
107+
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
108+
$port = substr($address, $pos + 1);
109+
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
115110
}
116111
return $address;
117112
}

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)