2

I have the following script:

$con = @fsockopen('mx.mydomain.com', 587, $errno, $errstr, 10);
if(empty($con)) echo "Not connected: ".$errno." - ".$errstr;
else echo "Connected!";

It's returned:

Not connected: 110 - Connection timed out

Occurs using any domain, but not if I enter the IP or define in the /etc/hosts. resolv.conf:

nameserver 2620:0:ccc::2
nameserver 2620:0:ccd::2
nameserver 208.67.222.222
nameserver 208.67.220.220

I've tried telling other DNS servers but persists. ping works fine too. I do not know where else to solve it.

PHP 5.4 / NGINX 1.8 / CentOS 6.6

Diogo Braga
  • 183
  • 1
  • 8
  • maybe removing the error suppression operator would reveal more of what is going on. – Orangepill Aug 06 '15 at 03:44
  • Nothing different: `Warning: fsockopen(): unable to connect to mx.mydomain.com:587 (Connection timed out) in /usr/share/nginx/www/test.php on line 6` – Diogo Braga Aug 06 '15 at 03:47
  • Shot in the dark here but what happens if you break ipv6 name resolution? – Orangepill Aug 06 '15 at 03:52
  • Orangepill, I did some tests using IPv6 directly and none passed, despite the function specifications said to be compatible. I will report it to PHP, meanwhile I'll set the IPv4 addresses directly in the /etc/hosts. – Diogo Braga Aug 06 '15 at 04:17
  • From what I have recently learned it appears that php is unable to resolve ipv6 addresses itself but you could do a lookup yourself using [dns_get_record](http://php.net/manual/en/function.dns-get-record.php) and use the returned ip for a DNS_AAAA to create an alternate connection string with the ip in the square bracket notation. – Orangepill Aug 06 '15 at 04:38
  • Even informing the IPv6 manually in the function, the result is the same. – Diogo Braga Aug 06 '15 at 04:49

1 Answers1

1

Based on a conversation with @rdlowrey it appears that:

gethostbyname() is incapable of resolving IPv6 records and this is what PHP uses if you pass an actual domain name in a URI. The user needs to specify the IP directly in this case. This means they should:

  1. Try gethostbyname() first to see if it can resolve an IPv4 address.
  2. If that fails (and it will in this user's case) they should manually use dns_get_record() with the DNS_AAAA constant to indicate IPv6

The relevant code from the source demonstrating that gethostbyname() is used internally by PHP to resolve host names in URIs can be found here.

Community
  • 1
  • 1
Orangepill
  • 24,500
  • 3
  • 42
  • 63