Skip to content

Commit 3ea2375

Browse files
committed
Hostnames in socket address will now be resolved via DNS again
1 parent 1c3e2b2 commit 3ea2375

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

Datagram/Factory.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function createClient($address)
2424
$factory = $this;
2525
$loop = $this->loop;
2626

27-
return $this->resolve($address)->then(function ($address) use ($loop) {
27+
return $this->resolveAddress($address)->then(function ($address) use ($loop) {
2828
$socket = stream_socket_client($address, $errno, $errstr);
2929
if (!$socket) {
3030
throw new Exception('Unable to create client socket: ' . $errstr, $errno);
@@ -39,7 +39,7 @@ public function createServer($address)
3939
$factory = $this;
4040
$loop = $this->loop;
4141

42-
return $this->resolve($address)->then(function ($address) use ($loop) {
42+
return $this->resolveAddress($address)->then(function ($address) use ($loop) {
4343
$socket = stream_socket_server($address, $errno, $errstr, STREAM_SERVER_BIND);
4444
if (!$socket) {
4545
throw new Exception('Unable to create server socket: ' . $errstr, $errno);
@@ -49,12 +49,40 @@ public function createServer($address)
4949
});
5050
}
5151

52-
protected function resolve($address)
52+
protected function resolveAddress($address)
5353
{
5454
if (strpos($address, '://') === false) {
5555
$address = 'udp://' . $address;
5656
}
5757
$parts = parse_url($address);
58+
59+
if (!$parts || !isset($parts['host'])) {
60+
return When::resolve($address);
61+
}
62+
63+
// remove square brackets for IPv6 addresses
64+
$host = trim($parts['host'], '[]');
65+
66+
return $this->resolveHost($host)->then(function ($host) use ($parts) {
67+
$address = $parts['scheme'] . '://';
68+
69+
if (isset($parts['port']) && strpos($host, ':') !== false) {
70+
// enclose IPv6 address in square brackets if a port will be appended
71+
$host = '[' . $host . ']';
72+
}
73+
74+
$address .= $host;
75+
76+
if (isset($parts['port'])) {
77+
$address .= ':' . $parts['port'];
78+
}
79+
80+
return $address;
81+
});
82+
}
83+
84+
protected function resolveHost($host)
85+
{
5886
// there's no need to resolve if the host is already given as an IP address
5987
if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
6088
return When::resolve($host);
@@ -65,17 +93,8 @@ protected function resolve($address)
6593
}
6694

6795
if ($this->resolver === null) {
68-
return When::reject(\Exception('No resolver given in order to get IP address for given hostname'));
96+
return When::reject(new Exception('No resolver given in order to get IP address for given hostname'));
6997
}
7098
return $this->resolver->resolve($host);
7199
}
72-
73-
public function createAddress($host, $port)
74-
{
75-
if (strpos($host, ':') !== false) {
76-
// enclose IPv6 address in square brackets
77-
$host = '[' . $host . ']';
78-
}
79-
return $host . ':' . $port;
80-
}
81100
}

0 commit comments

Comments
 (0)