CURLINFO_PRIMARY_PORT(3)

πŸ’… CURLINFO_PRIMARY_PORT(3) β€” like, the manual page, bestie

okay so real talk, this is one of those libcurl manual pages (that's the Library Functions Manual vibe, iykyk) and honestly? it's a little cutie. Let's get into it ✨


🌸 NAME 🌸

CURLINFO_PRIMARY_PORT β€” last destination port number

✨ SYNOPSIS ✨

#include <curl/curl.h>

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

πŸŽ€ DESCRIPTION πŸŽ€

okay so what you're doing here is passing a pointer to a long and it receives the destination port of the most recent connection done with this curl handle. that's it, that's the whole vibe πŸ’…

and it's specifically the destination port of the actual TCP or UDP connection that libcurl used. so like, important nuance incoming:

  • if a proxy was used for the most recent transfer β†’ this is the port number of the proxy
  • if no proxy was used β†’ this is the port number of the most recently accessed URL

if the connection was done using QUIC, the port number is a UDP port number. (not me double-checking which port family we're in 🫢)

and if no connection was established, or if the protocol doesn't even use ports? then -1 is returned. no cap, that's your "nope" signal 😭

πŸ¦‹ PROTOCOLS πŸ¦‹

This functionality affects all supported protocols β€” every single one, periodt πŸ‘‘

🌈 EXAMPLE 🌈

okay here's the actual code, copy it exactly bestie, don't be creative with it:

int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    CURLcode result;
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
    result = curl_easy_perform(curl);
    if(result == CURLE_OK) {
      long port;
      result = curl_easy_getinfo(curl, CURLINFO_PRIMARY_PORT, &port);
      if(!result)
        printf("Connected to remote port: %ld\n", port);
    }
    curl_easy_cleanup(curl);
  }
}

⭐ AVAILABILITY ⭐

Added in curl 7.21.0 β€” so your fave has been iconic for a minute πŸ•°οΈ

πŸ’– 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 deets

πŸ’œ SEE ALSO πŸ’œ


ngl, this page is short and sweet and I'm obsessed. go forth and check your ports, bestie 🌸

libcurl manual, dated 2026-10-12 β€” that's the source/version and date, mwah, signing off 🫢✨

πŸ“¦ where this page comes from