|  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: -
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: info at directwebsolutions dot nl
New email:
PHP Version: OS:


 [2017-06-15 21:12 UTC] info at directwebsolutions dot nl
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 ( ) 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('', 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>";

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:
*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:
Warning: stream_socket_client(): Invalid IP Address: octect1:octect2:octect3:octect4:0000:0000:0000:0020

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.


Add a Patch

Pull Requests

Add a Pull Request


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

I have this problem on Debian 8, PHP 7.1.

$url = '';

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

$opts = array (

	'http' => array (
		'method' => "GET",
		'header' => 
			"Referer:\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:
 [2018-09-04 09:15 UTC]
Automatic comment on behalf of
Log: Fix bug #74764 and add a test case
 [2018-09-04 09:15 UTC]
-Status: Open +Status: Closed
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 25 00:01:30 2024 UTC