Search code examples
libcurlipv6

Using CURLOPT_CONNECT_TO with an IPv6 address


I am attempting to use curl's CURLOPT_CONNECT_TO option to connect to a specific address (rather than the result of the DNS lookup of the host part of the url):

CURL * r_curl = NULL;
struct curl_slist * r_connect = NULL;
char connectStr[128];

if (af == AF_INET) {
    sprintf(connectStr, "::%s:", ipAddrString);
} else if (af == AF_INET6) {
    /* in [] per https://curl.haxx.se/libcurl/c/CURLOPT_CONNECT_TO.html */
    sprintf(connectStr, "::[%s]:", ipAddrString);
}
fprintf(stderr, "DEBUG: connect '%s', url %s\n", connectStr, url);

r_curl = curl_easy_init();
    ...
r_connect = curl_slist_append(r_connect, connectStr);
curl_easy_setopt(r_curl, CURLOPT_CONNECT_TO, r_connect);
curl_easy_setopt(r_curl, CURLOPT_URL, url);
curl_easy_perform(r_curl);

When af is AF_INET and ipAddrSring is an IPv4 address this works perfectly. When af is AF_INET6 and ipAddrSring is an IPv6 address, curl looks like it is trying to do a DNS host lookup on the IPv6 address:

DEBUG: connect '::129.186.23.166:', url http://www.iastate.edu/
* Connecting to hostname: 129.186.23.166
* Trying 129.186.23.166...
* TCP_NODELAY set
* Connected to 129.186.23.166 (129.186.23.166) port 80 (#0)

vs

DEBUG: connect '::[2610:130:101:104::2]:', url http://www.iastate.edu/
* Connecting to hostname: 2610:130:101:104::2
* Could not resolve host: 2610:130:101:104::2

What I am doing wrong here?

(Curl is version 7.56.1)


Solution

  • There was a bug in libcurl (before 7.58.0) which made it take IPv6 addresses and attempt to use them for CURLOPT_CONNECT_TO, even if it was built without support for IPv6!

    This was addressed in curl 7.58.0 and from then on it makes libcurl return an error if this is attempted!