CURLINFO_LOCAL_PORT(3)

πŸ’… CURLINFO_LOCAL_PORT(3) πŸ’…

okay so bestie, we're in the libcurl section of the manual β€” Library Functions Manual, if you wanna be formal about it β€” and today's main character is CURLINFO_LOCAL_PORT: the latest local port number. no cap, that's the whole vibe. πŸŽ€


✨ NAME ✨

CURLINFO_LOCAL_PORT β€” latest local port number πŸ¦‹


✨ SYNOPSIS ✨

#include <curl/curl.h>

CURLcode curl_easy_getinfo(CURL *handle, CURLINFO_LOCAL_PORT, long *portp);

🌸 DESCRIPTION 🌸

so what you do is, you pass a pointer to a long, and it receives the local port number of the most recent connection done with this curl handle. literally that's it. 🫢

if the connection was done using QUIC, the port number is a UDP port number, otherwise it is a TCP port number. iykyk. πŸ”₯

and if no connection was established, or if the protocol doesn't use ports β€” no shade to it β€” then -1 is returned. periodt. πŸ’€


🧚 PROTOCOLS 🧚

this functionality affects quic and tcp. that's the vibe check, and they both passed. βœ…


🌈 EXAMPLE 🌈

here's the whole thing, copy it exactly, don't @ me:

int main(void)
{
  CURL *curl;
  CURLcode result;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    result = curl_easy_perform(curl);

    if(CURLE_OK == result) {
      long port;
      result = curl_easy_getinfo(curl, CURLINFO_LOCAL_PORT, &port);

      if(CURLE_OK == result) {
        printf("We used local port: %ld\n", port);
      }
    }
    curl_easy_cleanup(curl);
  }
  return 0;
}

like, iconic. it just prints We used local port: %ld and honestly? ate. πŸ‘‘


⭐ AVAILABILITY ⭐

added in curl 7.21.0 β€” so she's been serving since way back. vintage queen. πŸŽ€


πŸ’– RETURN VALUE πŸ’–

curl_easy_getinfo(3) returns a CURLcode indicating success or error.

CURLE_OK (0) means everything was OK πŸ₯³ β€” non-zero means an error occurred 😭, see libcurl-errors(3) for the full rundown.


🌟 SEE ALSO 🌟

go read them, bestie, they're all linked in the group chat. πŸ’œ


okay that's the whole page β€” this one's from libcurl, dated 2026-10-12, and it was CURLINFO_LOCAL_PORT(3) the whole time. mwah. πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from