Assign specific IPv6 address of subnet to cURL

curl, ip-address, ipv6, php, subnet

Solution

cURL cannot bind to 2a01:4f8:XXXX:YYYY::3 because 2a01:4f8:XXXX:YYYY::3 is not an IP local address assigned to any interface. Hosts can only receive packets on addresses that are assigned to them, and, accordingly, you can only bind to those addresses.

As you described, if you do add 2a01:4f8:XXXX:YYYY::3 as an address on the interface using `ip addr add`, it works. That is to be expected.

I don't know of a way to assign a large group of addresses to the local system without having to add them one by one. There is a trick but it only works for some operating systems (perhaps only Linux) and only for IPv4 and only for the loopback interface.

Problem

I've assigned a /64 IPv6 subnet to my dedicated host via `/etc/network/interfaces` (distribution is Debian Squeeze) ``` iface eth0 inet6 static address 2a01:4f8:XXXX:YYYY::2 netmask 64 gateway fe80::1 ``` Now, I want to get cURL (PHP 5.3.22-1~dotdeb.0) to use one specific IP address of that subnet, p.e. `2a01:4f8:XXXX:YYYY::3` so my code looks like: ``` curl_setopt($this->_curl, CURLOPT_INTERFACE, '2a01:4f8:XXXX:YYYY::3'); curl_setopt($this->_curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); ``` When running `curl_exec` I get `bind failed with errno 99: Cannot assign requested address`. Why does this happen? When defining each IPv6 address as /128 in `/etc/network/interfaces`, like: ``` iface eth0 inet6 static address 2a01:4f8:XXXX:YYYY::2 netmask 128 up ip -6 addr add 2a01:4f8:XXXX:YYYY::3/128 dev eth0 preferred_lft 0 gateway fe80::1 ``` then all is fine and cURL uses the `::3` IP. But my intention is to use a lot of IPs from my subnet (really a lot of IPs), so that this is not practicable. Does anyone know why cURL cannot assign a sub IP of the /64 subnet?

Original source