GNURLINFO_PRIMARY_IP(3)

πŸ“¦ version 7.72.0, in nixos 26.05, nixpkgs unstable

πŸ’… GNURLINFO_PRIMARY_IP(3) πŸ’…

okay so welcome to the curl_easy_getinfo options corner of the manual, bestie β€” this page is all about GNURLINFO_PRIMARY_IP(3) and it's lowkey iconic. vibe check: passed.


🌸 NAME 🌸

CURLINFO_PRIMARY_IP β€” get IP address of last connection. that's the name section, short and slay.

✨ SYNOPSIS ✨

include the header, call the function, and the vibes are literally:

#include <gnurl/curl.h>

CURLcode curl_easy_getinfo(CURL *handle, CURLINFO_PRIMARY_IP, char **ip);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay bestie, here's the tea: you pass a pointer to a char pointer to receive the pointer to a null-terminated string holding the IP address of the most recent connection done with this curl handle. this string may be IPv6 when that is 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. highkey important, fr fr.

⚠️ important, bestie: the ip pointer will be NULL or pointing to private memory you MUST NOT free β€” it gets freed when you call curl_easy_cleanup(3) on the corresponding CURL handle. do not do it.

🌈 PROTOCOLS 🌈

All network based ones β€” every network-based protocol, periodt.

πŸŽ€ EXAMPLE πŸŽ€

here's the example, and yes, copy it exactly β€” no creative rewrites, bestie:

{
  char *ip;

  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");

  /* Perform the request, res will get the return code */
  res = curl_easy_perform(curl);
  /* Check for errors */
  if((res == CURLE_OK) &&
     !curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip) && ip) {
    printf("IP: %s\n", ip);
  }

  /* always cleanup */
  curl_easy_cleanup(curl);
}

⭐ AVAILABILITY ⭐

Added in 7.19.0 β€” vintage but still iconic.

πŸ’œ RETURN VALUE πŸ’œ

Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not. that's it, no cap.

πŸ”₯ SEE ALSO πŸ”₯

go check out these besties: curl_easy_getinfo(3), curl_easy_setopt(3), CURLINFO_PRIMARY_PORT(3), CURLINFO_LOCAL_IP(3), <-- yes, the original list had a lil trailing comma, so iconic.


xoxo, your Unix bestie πŸ’– this is GNURLINFO_PRIMARY_IP(3) from libcurl 7.44.0, dated 12 Sep 2015 β€” mwah πŸ’‹

πŸ“¦ where this page comes from