Skip to content

Commit

Permalink
Added option to force IPv4 addresses when resolving hostnames (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveTherrien authored Aug 7, 2020
1 parent cc444c3 commit ef62362
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ $client->setCAPath("/ca/path"); // if you need.

*Note:* Certificate must match with host name in `Client` address (`localhost` in example above).

### DNS Resolution

This error may indicate your system is having trouble resolving IPv6 addresses:

```
cURL error: Resolving timed out after [value] milliseconds
```

By default, both IPv4 and IPv6 addresses will attempt to be resolved. You can force it to only resolve IPv4 addresses with:

```php
$client->forceIpResolveV4();
```

### Testing

Requirements:
Expand Down
15 changes: 15 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Client

private $connectTimeoutOption;
private $timeoutOption;
private $forceIpResolveV4;

private $safety = true;
private $useAssoc = false;
Expand Down Expand Up @@ -117,6 +118,17 @@ public function setTimeoutOption($timeoutOption)
return $this;
}

/**
* Forces DNS to only resolve IPv4 addresses.
*
* @return Client
*/
public function forceIpResolveV4()
{
$this->forceIpResolveV4 = true;
return $this;
}

/**
* Publish data into channel.
*
Expand Down Expand Up @@ -381,6 +393,9 @@ private function request($method, $params)
if ($this->timeoutOption) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeoutOption);
}
if ($this->forceIpResolveV4) {
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
}
if (!$this->safety) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Expand Down

0 comments on commit ef62362

Please sign in to comment.