Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket options: bindto => 0:0 throwing Invalid IP address 0 #12190

Closed
remizyaka opened this issue Sep 12, 2023 · 1 comment
Closed

Socket options: bindto => 0:0 throwing Invalid IP address 0 #12190

remizyaka opened this issue Sep 12, 2023 · 1 comment

Comments

@remizyaka
Copy link

Description

It is not possible to bint to ip4 address using context options any more. In documentation:

bindto
Used to specify the IP address (either IPv4 or IPv6) and/or the port number that PHP will use to access the network. The syntax is ip:port for IPv4 addresses, and [ip]:port for IPv6 addresses. Setting the IP or the port to 0 will let the system choose the IP and/or port.

but in fact it is not allow us to put 0 for ip address.

The following code:

<?php
$url = 'https://google.com';
$ctx  = stream_context_create([
                    'socket' => [
                        'bindto'=>'0:0'
                    ],
                    'http' =>
                        [
                            'timeout' => 10,
                        ],
                ]);
                $feed = file_get_contents($url, false, $ctx);

Resulted in this output:

file_get_contents(): Invalid IP Address: 0

But I expected this output instead:

actual content of google page

PHP Version

PHP 8.2.5

Operating System

No response

@devnexen
Copy link
Member

devnexen commented Sep 12, 2023

The script works with PHP 7.4 for example indeed. One of the key differences is the priorisation of the inet_pton's usage above inet_aton in >= PHP 8.1

@devnexen devnexen self-assigned this Sep 12, 2023
devnexen added a commit to devnexen/php-src that referenced this issue Sep 12, 2023
Prior to the 8.1 rewrite, inet_aton was used for ipv4 addresses
therefore addresses like `0` passed.
For the bindto's case where both ip and port are set as such, we discard
the address binding.
devnexen added a commit to devnexen/php-src that referenced this issue Sep 12, 2023
Prior to the 8.1 rewrite, inet_aton was used for ipv4 addresses
therefore addresses like `0` passed.
For the bindto's case where both ip and port are set as such, we discard
the address binding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants