Bug #74764 [Com]: Bindto IPv6 works with file_get_contents but fails with stream_socket_client

From: Date: Sat, 20 Jan 2018 10:17:35 +0000
Subject: Bug #74764 [Com]: Bindto IPv6 works with file_get_contents but fails with stream_socket_client
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213631@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74764&edit=1

 ID:                 74764
 Comment by:         vklimin at 51070 dot com
 Reported by:        info at directwebsolutions dot nl
 Summary:            Bindto IPv6 works with file_get_contents but fails
                     with stream_socket_client
 Status:             Open
 Type:               Bug
 Package:            Streams related
 Operating System:   CentOS
 PHP Version:        5.6.30
 Block user comment: N
 Private report:     N

 New Comment:

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 ..."


Previous Comments:
------------------------------------------------------------------------
[2017-06-15 21:12:54] 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.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=74764&edit=1


Thread (4 messages)

« previous php.bugs (#213631) next »