Skip to content

Commit 4d12be1

Browse files
committed
Merge pull request #12 from clue/ex
Fix processing ICMP rejection datagrams
2 parents 726b8ea + b030392 commit 4d12be1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Datagram/Socket.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use React\EventLoop\LoopInterface;
66
use Evenement\EventEmitter;
7+
use Exception;
78

89
class Socket extends EventEmitter implements SocketInterface
910
{
@@ -124,7 +125,7 @@ protected function handleReceive(&$peerAddress)
124125
// due to the nature of UDP, there's no way to tell which one exactly
125126
// $peer is not filled either
126127

127-
throw new \Exception('Invalid message');
128+
throw new Exception('Invalid message');
128129
}
129130

130131
$peerAddress = $this->sanitizeAddress($peerAddress);

tests/SocketTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@ public function testClientSendHugeWillFail()
8787
$this->loop->run();
8888
}
8989

90+
public function testClientSendNoServerWillFail()
91+
{
92+
$promise = $this->factory->createClient('127.0.0.1:1234');
93+
$client = $this->getValueFromResolvedPromise($promise);
94+
95+
// send a message to a socket that is not actually listening
96+
// expect the remote end to reject this by sending an ICMP message
97+
// which we will receive as an error message. This depends on the
98+
// host to actually reject UDP datagrams, which not all systems do.
99+
$client->send('hello');
100+
$client->on('error', $this->expectCallableOnce());
101+
102+
$loop = $this->loop;
103+
$client->on('error', function () use ($loop) {
104+
$loop->stop();
105+
});
106+
107+
$that = $this;
108+
$this->loop->addTimer(1.0, function () use ($that, $loop) {
109+
$loop->stop();
110+
$that->markTestSkipped('UDP packet was not rejected after 0.5s, ignoring test');
111+
});
112+
113+
$this->loop->run();
114+
}
115+
90116
public function testCreatePair()
91117
{
92118
$promise = $this->factory->createServer('127.0.0.1:0');

0 commit comments

Comments
 (0)