GNURLOPT_OPENSOCKETFUNCTION(3)
okay bestie, you're on the GNURLOPT_OPENSOCKETFUNCTION(3) page β it's one of the curl_easy_setopt options, and it's literally about handing libcurl your own callback for opening sockets. slay. π
β¨ NAME β¨
CURLOPT_OPENSOCKETFUNCTION β set callback for opening sockets π
πΈ SYNOPSIS πΈ
here's the whole signature situation, bestie β keep it exact:
#include <gnurl/curl.h>
typedef enum {
CURLSOCKTYPE_IPCXN, /* socket created for a specific IP connection */
} curlsocktype;
struct curl_sockaddr {
int family;
int socktype;
int protocol;
unsigned int addrlen;
struct sockaddr addr;
};
curl_socket_t opensocket_callback(void *clientp,
curlsocktype purpose,
struct curl_sockaddr *address);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETFUNCTION, opensocket_callback);
it's giving #include <gnurl/curl.h> main character energy, fr fr. π¦
π DESCRIPTION π
okay so you pass a pointer to your callback function, and it should match the prototype shown above. literally that's the assignment. π
libcurl calls this callback instead of calling socket(2) itself. the callback's purpose argument tells you the exact purpose for this particular socket. CURLSOCKTYPE_IPCXN is for IP based connections, and it's the only purpose currently used in libcurl. future versions of libcurl may support more purposes. noted. β¨
the clientp pointer holds whatever user-defined value you set with CURLOPT_OPENSOCKETDATA(3). yep, that's the one. π«Ά
the callback gets the resolved peer address as the address argument, and it's allowed to modify the address or refuse to connect completely. the callback should return the newly created socket, or CURL_SOCKET_BAD if no connection could be established or another error was detected. any additional setsockopt(2) calls can of course be done on the socket at the user's discretion. a CURL_SOCKET_BAD return value from the callback signals an unrecoverable error to libcurl, and libcurl will return CURLE_COULDNT_CONNECT from the function that triggered this callback. that return code can be used for IP address block listing. iconic for blocklists, honestly. π
if you wanna pass in a socket with an already established connection, pass the socket back with this callback, then use CURLOPT_SOCKOPTFUNCTION(3) to signal that it already is connected. easy. π¦
π DEFAULT π
the default behavior is the equivalent of this:
return socket(addr->family, addr->socktype, addr->protocol);
so if you don't set the callback, libcurl is just doing that exact socket() call. periodt. π
π PROTOCOLS π
All β every protocol, bestie. π
π EXAMPLE π
here's the example where libcurl uses the already established socket 'sockfd'. copy it exact, okay?
/* make libcurl use the already established socket 'sockfd' */
static curl_socket_t opensocket(void *clientp,
curlsocktype purpose,
struct curl_sockaddr *address)
{
curl_socket_t sockfd;
sockfd = *(curl_socket_t *)clientp;
/* the actual externally set socket is passed in via the OPENSOCKETDATA
option */
return sockfd;
}
static int sockopt_callback(void *clientp, curl_socket_t curlfd,
curlsocktype purpose)
{
/* This return code was added in libcurl 7.21.5 */
return CURL_SOCKOPT_ALREADY_CONNECTED;
}
curl = curl_easy_init();
if(curl) {
/* libcurl will internally think that you connect to the host
* and port that you specify in the URL option. */
curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
/* call this function to get a socket */
curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
/* call this function to set options for the socket */
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
that's the whole example. it's giving external socket realness. π
β AVAILABILITY β
added in 7.17.1. noted and timestamped, bestie. β¨
π RETURN VALUE π
returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not. that's it. that's the vibe check. π«Ά
π¦ SEE ALSO π¦
go read those too, iykyk. π
okay that's the whole page! πΈ source/version: libcurl 7.37.0; date: 16 Jun 2014; page: GNURLOPT_OPENSOCKETFUNCTION(3) β mwah, bestie! π
π¦ where this page comes from
- nixos 26.05: libgnurl 7.72.0
- nixpkgs unstable: libgnurl 7.72.0
- Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms