php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74764 Bindto IPv6 works with file_get_contents but fails with stream_socket_client
Submitted: 2017-06-15 21:12 UTC Modified: -
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: info at directwebsolutions dot nl Assigned:
Status: Closed Package: Streams related
PHP Version: 5.6.30 OS: CentOS
Private report: No CVE-ID: None
 [2017-06-15 21:12 UTC] info at directwebsolutions dot nl
Description:
------------
PHP 5.6.30 (cli) (built: Jun  2 2017 12:27:19). I experienced the same problem with PHP 7.1.

I think it is a bug which is in PHP for quite a long time if this is the same as bug ( https://bugs.php.net/bug.php?id=50415 ) which was not solved in the past.

What I try to do is:
- create streaming socket using SSL
- binding to a local IP which is not the default IP of the ethernet card (NIC) but an additional configured address and is IPv6 (important!).

I runned quite a few tests with different parameters. I copied the code to a single file and executed it as root in SSH to make sure it is not a problem with privileges within the user account.

I can connect with the desired IPv6 binding to any website using stream_context_create() on a file_get_contents() execution. I tried this to several websites which have functionality on their website to show my visitor IP address and then my correct IPv6 address is displayed, and thus used.

But when I try to connect with the same IPv6 binding and exactly the same stream_context_create() but change the function using the $context to be stream_socket_client() then all requests fail and falls back to the servers default IPv4 or IPv6 address.

Test script:
---------------
Script 1 (ipv6 binding with file_get_contents):
-----------------------------------------------
$context = stream_context_create(
    array('socket' => array('bindto' => "[octect1:octect2:octect3:octect4:0000:0000:0000:0020]:0", "ipv6_v6only" => "ipv6_v6only"))
);
print file_get_contents('http://ipadres.bit.nl/', false, $context);


Script 2 (ipv6 binding with stream_socket_client):
-----------------------------------------------
$context = stream_context_create(
    array('socket' => array('bindto' => "[octect1:octect2:octect3:octect4:0000:0000:0000:0020]:0", "ipv6_v6only" => "ipv6_v6only"))
);
$socket = stream_socket_client('ssl://'.$epp_host.':'.$epp_port.'', $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $context);
print $errno."<br>";
print $errstr."<br>";
var_dump($socket);

Expected result:
----------------
Script 1:
---------
A lot of HTML with my outgoing IPv6 address (octect1:octect2:octect3:octect4:0000:0000:0000:0020) inside the HTML page code.


Script 2:
---------
0
*empty $errstr*
resource(29) of type (stream)

Actual result:
--------------
Script 1:
---------
My correct outgoing non default IPv6 address is bound and thus is inside the HTML page code.


Script 2:
---------
111
Warning: stream_socket_client(): Invalid IP Address: octect1:octect2:octect3:octect4:0000:0000:0000:0020
bool(false)

And because the IP address is invalid i also cannot connect to the desired servers because my IP does not pass through their firewall, so i get a connection refused.


At the end of this bug report i found another streaming host which i could connect to which does not have a firewall in place, and then the correct IPv6 address CAN be bound with using the stream_socket_client() function. So that would mean that the error "Invalid IP Address" is just a wrong error for the situation when no connection is possible?

What is happening here? Please look into this.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-20 10:17 UTC] vklimin at 51070 dot com
Hi!

I have this problem on Debian 8, PHP 7.1.

$url = 'https://www.yandex.ru';

$ip = '[2a01:d8:4:3::49c]';

$opts = array (

	'http' => array (
		'method' => "GET",
		'header' => 
			"Referer: https://www.yandex.ru/\r\n" . 
			"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36\r\n"
	),
	
	'socket' => array (
		'bindto' => "{$ip}:0"
	),
	
);

$context = stream_context_create( $opts );

$c = file_get_contents( $url, false, $context );

This script generates "FastCGI sent in stderr: "PHP message: PHP Warning:  file_get_contents(): Invalid IP Address: 2a01:d8:4:3::49c in ..."
 [2018-08-30 03:59 UTC] vhu at iki dot fi
There is the PR to fix this: https://github.com/php/php-src/pull/3474
 [2018-09-04 09:15 UTC] krakjoe@php.net
Automatic comment on behalf of vhu@iki.fi
Revision: http://git.php.net/?p=php-src.git;a=commit;h=0f724453c03b7ff500563c019f5dab736d326ad8
Log: Fix bug #74764 and add a test case
 [2018-09-04 09:15 UTC] krakjoe@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC