CURLOPT_SOCKOPTDATA(3)

πŸ’… CURLOPT_SOCKOPTDATA(3) πŸ’…

okay so this is a page from the Library Functions Manual (the libcurl crew πŸ’–) and it's honestly such a cute lil concept: it's the "give my callback a snack" option. bestie, it's literally one pointer. that's the whole vibe. ✨


✨ NAME ✨

CURLOPT_SOCKOPTDATA β€” a pointer to pass to your sockopt callback 🫢

🌸 SYNOPSIS 🌸

#include <curl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SOCKOPTDATA, void *pointer);

πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea πŸ”₯: you hand libcurl a pointer, and libcurl is soooo respectful about it β€” it does not touch it, periodt. it just holds onto it and passes it straight through as the first argument of your sockopt callback, the one you set with CURLOPT_SOCKOPTFUNCTION(3).

it's giving "here's my emotional support pointer, pls deliver it to my callback untouched" πŸ˜­πŸ’œ

πŸŽ€ DEFAULT πŸŽ€

NULL

(iconic empty-hands behaviour, no notes)

🌈 PROTOCOLS 🌈

this functionality affects all supported protocols β€” every single one, no cap πŸ‘‘

⭐ EXAMPLE ⭐

ok so this is the whole thing, copy it exactly, it's gorgeous:

static int sockopt_callback(void *clientp, curl_socket_t curlfd,
                            curlsocktype purpose)
{
  int val = *(int *)clientp;
  setsockopt((int)curlfd, SOL_SOCKET, SO_RCVBUF,
             (const char *)&val, sizeof(val));
  return CURL_SOCKOPT_OK;
}

int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    CURLcode result;
    int recvbuffersize = 256 * 1024;

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

    /* call this function to set options for the socket */
    curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
    curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, &recvbuffersize);

    result = curl_easy_perform(curl);

    curl_easy_cleanup(curl);
  }
}

see how &recvbuffersize (that's 256 * 1024, a whole 256KB of buffer, she's thriving πŸ’…) gets handed over as clientp? the callback then dereferences it into val and calls setsockopt((int)curlfd, SOL_SOCKET, SO_RCVBUF, (const char *)&val, sizeof(val)) and returns CURL_SOCKOPT_OK. main character energy, honestly. 🌟

🧚 AVAILABILITY 🧚

Added in curl 7.16.0 β€” vintage but still slaying πŸ’–

πŸ’€ RETURN VALUE πŸ’€

  • CURLE_OK β€” if the option is supported πŸ₯³
  • CURLE_UNKNOWN_OPTION β€” if it's not πŸ™ˆ

🌸 SEE ALSO 🌸


⚠️ friendly reminder bestie: the pointer is passed through untouched, so keep whatever it points at alive for as long as the callback might need it. no dangling vibes, iykyk 😭

and that's the whole page, mwah 🫢 this was libcurl version 2026-10-12, page CURLOPT_SOCKOPTDATA(3) β€” go set some socket options, love u, bye πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from