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: -
From: info at directwebsolutions dot nl Assigned:
Status: Open Package: Streams related
PHP Version: 5.6.30 OS: CentOS
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC