Skip to content

Commit c18ad41

Browse files
ncosta-icnilmerg
authored andcommitted
fix(Connection): connection gets properly mapped for both IPv4 and IPv6 socket bindings
This fixes an issue which caused the URI mapping of the connections to fail when not using the IPv6 notation for both IPv4 and IPv6 addresses. It now supports both formats (IPv4 and IPv6 notation) and additionally resolves IPv4 addresses that are passed in the IPv6 notation. Tested with: native IPv4, native IPv4 in IPv6 notation, native IPv6
1 parent 5aa604c commit c18ad41

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

library/Notifications/Daemon/Server.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,12 @@ public function reload(): void
177177
protected function mapRequestToConnection(ServerRequestInterface $request): ?Connection
178178
{
179179
$params = $request->getServerParams();
180+
$scheme = $request->getUri()->getScheme();
180181

181182
if (isset($params['REMOTE_ADDR']) && isset($params['REMOTE_PORT'])) {
182-
$address = Connection::parseHostAndPort($params['REMOTE_ADDR'] . ':' . $params['REMOTE_PORT']);
183+
$address = Connection::parseHostAndPort(
184+
$scheme . '://' . $params['REMOTE_ADDR'] . ':' . $params['REMOTE_PORT']
185+
);
183186
foreach ($this->connections as $connection) {
184187
if ($connection->getAddress() === $address->addr) {
185188
return $connection;

library/Notifications/Model/Daemon/Connection.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,28 @@ public static function parseHostAndPort(?string $address)
125125
];
126126

127127
// host
128-
$host = substr(
129-
$raw,
130-
strpos($raw, '[') + 1,
131-
strpos($raw, ']') - (strpos($raw, '[') + 1)
132-
);
133-
if (! $host) {
128+
$host = parse_url($raw, PHP_URL_HOST);
129+
$port = parse_url($raw, PHP_URL_PORT);
130+
131+
if (! $host || ! $port) {
134132
return false;
135133
}
136134

137-
if (strpos($host, '.')) {
138-
// it's an IPv4, stripping empty IPv6 tags
139-
$parsed->host = substr($host, strrpos($host, ':') + 1);
135+
if (strpos($host, '[') !== false) {
136+
// IPv6 format
137+
if (strpos($host, '.')) {
138+
// IPv4 represented in IPv6
139+
$offset = strrpos($host, ':');
140+
$parsed->host = substr($host, $offset === false ? 0 : $offset + 1, -1);
141+
} else {
142+
// it's a native IPv6
143+
$parsed->host = $host;
144+
}
140145
} else {
146+
// IPv4 format
141147
$parsed->host = $host;
142148
}
143149

144-
// port
145-
$port = substr($raw, strpos($raw, ']') + 2);
146-
if (! $port) {
147-
return false;
148-
}
149-
150150
$parsed->port = $port;
151151
$parsed->addr = $parsed->host . ':' . $parsed->port;
152152

0 commit comments

Comments
 (0)