Question

I m using libcurl as a http client in my application.

I communicate to the HTTP serve with its url with the following libcurl function:

curl_easy_setopt(curl, CURLOPT_URL, url);

I want to know the IP address of the server url after sending the first http message. How I can do that with libcurl.

Note: the HTTP server could redirect the first HTTP message to another server. How to know the IP address of the second server?

Was it helpful?

Solution

From curl_easy_getinfo

CURLINFO_PRIMARY_IP
Pass a pointer to a char pointer to receive the pointer to a zero-terminated string holding the IP address of the most recent connection done with this curl handle. This string may be IPv6 if that's enabled. Note that you get a pointer to a memory area that will be re-used at next request so you need to copy the string if you want to keep the information. (Added in 7.19.0)

This should be the final connection after any redirection.

Another way might be to use CURLINFO_LASTSOCKET and extract the peer from that, but I don't know how long the used socket will be valid after the connection is complete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top