1CURLOPT_OPENSOCKETFUNCTION(3c)url_easy_setopt optionCsURLOPT_OPENSOCKETFUNCTION(3)
2
3
4

NAME

6       CURLOPT_OPENSOCKETFUNCTION - callback for opening socket
7

SYNOPSIS

9       #include <curl/curl.h>
10
11       typedef enum  {
12         CURLSOCKTYPE_IPCXN,  /* socket created for a specific IP connection */
13       } curlsocktype;
14
15       struct curl_sockaddr {
16         int family;
17         int socktype;
18         int protocol;
19         unsigned int addrlen;
20         struct sockaddr addr;
21       };
22
23       curl_socket_t opensocket_callback(void *clientp,
24                                         curlsocktype purpose,
25                                         struct curl_sockaddr *address);
26
27       CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETFUNCTION, opensocket_callback);
28

DESCRIPTION

30       Pass a pointer to your callback function, which should match the proto‐
31       type shown above.
32
33       This callback function gets called by libcurl instead of the  socket(2)
34       call.  The callback's purpose argument identifies the exact purpose for
35       this particular socket. CURLSOCKTYPE_IPCXN is for IP based  connections
36       and  is  the only purpose currently used in libcurl. Future versions of
37       libcurl may support more purposes.
38
39       The clientp pointer contains whatever user-defined value set using  the
40       CURLOPT_OPENSOCKETDATA(3) function.
41
42       The callback gets the resolved peer address as the address argument and
43       is allowed to modify the address or refuse to connect  completely.  The
44       callback   function   should   return   the  newly  created  socket  or
45       CURL_SOCKET_BAD in case no connection could be established  or  another
46       error was detected. Any additional setsockopt(2) calls can of course be
47       done on the socket at the user's discretion.  A CURL_SOCKET_BAD  return
48       value  from the callback function will signal an unrecoverable error to
49       libcurl and it will return CURLE_COULDNT_CONNECT from the function that
50       triggered  this  callback.  This return code can be used for IP address
51       block listing.
52
53       If you want to pass in a socket with an already established connection,
54       pass  the  socket back with this callback and then use CURLOPT_SOCKOPT‐
55       FUNCTION(3) to signal that it already is connected.
56

DEFAULT

58       The default behavior is the equivalent of this:
59          return socket(addr->family, addr->socktype, addr->protocol);
60

PROTOCOLS

62       All
63

EXAMPLE

65       /* make libcurl use the already established socket 'sockfd' */
66
67       static curl_socket_t opensocket(void *clientp,
68                                       curlsocktype purpose,
69                                       struct curl_sockaddr *address)
70       {
71         curl_socket_t sockfd;
72         sockfd = *(curl_socket_t *)clientp;
73         /* the actual externally set socket is passed in via the OPENSOCKETDATA
74            option */
75         return sockfd;
76       }
77
78       static int sockopt_callback(void *clientp, curl_socket_t curlfd,
79                                   curlsocktype purpose)
80       {
81         /* This return code was added in libcurl 7.21.5 */
82         return CURL_SOCKOPT_ALREADY_CONNECTED;
83       }
84
85       curl = curl_easy_init();
86       if(curl) {
87         /* libcurl will internally think that you connect to the host
88          * and port that you specify in the URL option. */
89         curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
90         /* call this function to get a socket */
91         curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
92         curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
93
94         /* call this function to set options for the socket */
95         curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
96
97         res = curl_easy_perform(curl);
98
99         curl_easy_cleanup(curl);
100       }
101

AVAILABILITY

103       Added in 7.17.1.
104

RETURN VALUE

106       Returns CURLE_OK if the option is supported,  and  CURLE_UNKNOWN_OPTION
107       if not.
108

SEE ALSO

110       CURLOPT_OPENSOCKETDATA(3),   CURLOPT_SOCKOPTFUNCTION(3),  CURLOPT_CLOS‐
111       ESOCKETFUNCTION(3),
112
113
114
115libcurl 7.82.0                September 08, 2021 CURLOPT_OPENSOCKETFUNCTION(3)
Impressum