About using IPv6 with file_get_contents in PHP (switching IP when going outside)

Asked 1 years ago, Updated 1 years ago, 69 views

I rent Conoha's VPS (CentOS 6.5).
PHP is 5.6.

Conoha has 17 IPv6s if you select standard os (CentOS 6.5).
Adding IPv4 is a fee, so I would like to make the most of IPv6 as much as possible.

I tried the following code in the PHP documentation because file_get_contents can use context.

<?php
$opts=array(
    'socket' = > array(
        'bindto' = > '[IPv6 in my environment]: Any port',
    ),
);

$context=stream_context_create($opts);
echo file_get_contents('http://www.example.com', false,$context);

?>

Even if you specify a context as described above,

  • file_get_contents():Invalid IP Address: IPv6 in your environment

The error appears.

The following is the situation.

  • All 17 of the IPv6s in question can be reversed, and I think the configuration is correct.
  • I tried all 17 IPv6s, but I got an error (both abbreviated and full).
  • Context specification (IP switching) is successful for IPv4 added separately.
  • The site you are connecting to is IPv6-capable.

How can I get out of PHP via IPv6?
If you can switch between IP (IPv6) and retrieve data, I don't care about file_get_contents.

I look forward to your kind cooperation.

Additional information about the matter you confirmed in the comment

  • 1.ping6 is successful.(Successful on sites such as ipv6.google.com)
  • 2.ip6 tables has the following settings:

    —INPUT ACCEPT [0:0]
    —FORWARD ACCEPT [0:0]
    —OUTPUT ACCEPT [0:0]
    -AINPUT-m state --state ESTABLISHED, RELATED-j ACCEPT
    -AINPUT-pipv6-icmp-j ACCEPT-AINPUT-ilo-j ACCEPT
    -AINPUT-m state --state NEW-mudp-pudp --dport546-d [band of IPv6 in your environment] -j ACCEPT
    -AINPUT-m state --state NEW-m tcp-p tcp --dport22-j ACCEPT
    -AINPUT-j REJECT -- reject-with icmp6-adm-prohibited
    -A FORWARD-j REJECT -- reject-with icmp6-adm-prohibited
    COMMIT

  • Disabling 3.ip6 tables did not change the error that appeared.
     

  • 4.php-i | grep-ipv6 has the following results:

    $php-i | grep-ipv6
    IPv6 Support=>enabled
    IPv6 = > Yes

  • 5. Try 'bindto'=>'[0]:0' (leave it to os).

2. The ip6 tables settings are as follows:

—INPUT ACCEPT [0:0]
—FORWARD ACCEPT [0:0]
—OUTPUT ACCEPT [0:0]
-AINPUT-m state --state ESTABLISHED, RELATED-j ACCEPT
-AINPUT-pipv6-icmp-j ACCEPT-AINPUT-ilo-j ACCEPT
-AINPUT-m state --state NEW-mudp-pudp --dport546-d [band of IPv6 in your environment] -j ACCEPT
-AINPUT-m state --state NEW-m tcp-p tcp --dport22-j ACCEPT
-AINPUT-j REJECT -- reject-with icmp6-adm-prohibited
-A FORWARD-j REJECT -- reject-with icmp6-adm-prohibited
COMMIT

3. Disabling ip6 tables did not change the error that appeared.
 

4. The results of the php-i | grep-ipv6 attempt are as follows:

$php-i | grep-ipv6
IPv6 Support=>enabled
IPv6 = > Yes

5. Try 'bindto'=>'[0]:0' for the bindto part of the PHP program.

No errors occur, but when you try to connect to a server that you own separately, you can get the default IPv4 awarded when you rent a VPS tied to interface eth0:

<?php
    echo$_SERVER ["REMOTE_ADDR" ];
  • 6. 'bindto'=>'[::1]:0' has the following results:

The following error occurred:

file_get_contents():Invalid IP Address:::1

The default IPv4 IP was obtained in the script for another server used in 5. above.

  • 7. Make sure the IP address is properly formatted (with or without full-width characters, abbreviated errors)

I checked again, but there were no inappropriate characters such as full-width characters.
There is no description of whether :: is : because there is no contiguous area in your IPv6 at 0000.
"Also, there are several parts of the IPv6 that can omit ""0"" such as ""0090"", so I checked again to see if there are any mistakes in the omission."I also tried again with a full notation that does not omit zero, but the error still appears.

  • A temporary solution?

In conclusion, I found that IPv6 context switching is possible in the file_get_contents directly specified by IPv6 that you suggested in the comment.

If IPv6 is directly specified, no errors are received and the source IP is IPv6.I have a question about why I get an error when I specify a domain, but I would like to solve it once because the method you suggested will satisfy my purpose.

Thank you for all your advice.

php linux centos ipv6

2022-09-30 19:40

1 Answers

If you look at the source code, you can see that "Invalid IP Address" is when the string is not properly formatted as an IP address.

·Are full-width characters mixed in?
·The : that omits 0 is not :

and so on

Another thing is that if the destination is IPv4, even if you write an IPv6 address, you still get "Invalid IP Address" when you try to interpret it as IPv4.

$host www.google.com
www.google.com has address 173.194.126.209
www.google.com has address 173.194.126.212
www.google.com has address 173.194.126.208
www.google.com has address 173.194.126.210
www.google.com has address 173.194.126.211
www.google.com has IPv6 address 2404:6800:4004:808::1010

file_get_contents('http://www.google.com', false, $context);
// =>no warning

$ host www.yahoo.co.jp
www.yahoo.co.jp is an alias for www.g.yahoo.co.jp.
www.g.yahoo.co.jp has address 182.22.72.251
www.g.yahoo.co.jp has address 183.79.197.250
www.g.yahoo.co.jp has address 183.79.198.79
www.g.yahoo.co.jp has address 183.79.227.90

file_get_contents('http://www.yahoo.co.jp', false, $context);
// = > PHP Warning: file_get_contents(): Invalid IP Address: (snip)


2022-09-30 19:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.