1CURLOPT_OPENSOCKETFUNCTION(3c)url_easy_setopt optionCsURLOPT_OPENSOCKETFUNCTION(3)
2
3
4
6 CURLOPT_OPENSOCKETFUNCTION - set callback for opening sockets
7
9 #include <curl/curl.h>
10
11 typedef enum {
12 CURLSOCKTYPE_IPCXN, /* socket created for a specific IP connection */
13 CURLSOCKTYPE_ACCEPT, /* socket created by accept() call */
14 CURLSOCKTYPE_LAST /* never use */
15 } curlsocktype;
16
17 struct curl_sockaddr {
18 int family;
19 int socktype;
20 int protocol;
21 unsigned int addrlen;
22 struct sockaddr addr;
23 };
24
25 curl_socket_t opensocket_callback(void *clientp,
26 curlsocktype purpose,
27 struct curl_sockaddr *address);
28
29 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETFUNCTION, opensocket_callback);
30
32 Pass a pointer to your callback function, which should match the proto‐
33 type shown above.
34
35 This callback function gets called by libcurl instead of the socket(2)
36 call. The callback's purpose argument identifies the exact purpose for
37 this particular socket: CURLSOCKTYPE_IPCXN is for IP based connections
38 and CURLSOCKTYPE_ACCEPT is for sockets created after accept() - such as
39 when doing active FTP. Future versions of libcurl may support more pur‐
40 poses.
41
42 The clientp pointer contains whatever user-defined value set using the
43 CURLOPT_OPENSOCKETDATA(3) function.
44
45 The callback gets the resolved peer address as the address argument and
46 is allowed to modify the address or refuse to connect completely. The
47 callback function should return the newly created socket or
48 CURL_SOCKET_BAD in case no connection could be established or another
49 error was detected. Any additional setsockopt(2) calls can of course be
50 done on the socket at the user's discretion. A CURL_SOCKET_BAD return
51 value from the callback function will signal an unrecoverable error to
52 libcurl and it will return CURLE_COULDNT_CONNECT from the function that
53 triggered this callback. This return code can be used for IP address
54 blacklisting.
55
56 If you want to pass in a socket with an already established connection,
57 pass the socket back with this callback and then use CURLOPT_SOCKOPT‐
58 FUNCTION(3) to signal that it already is connected.
59
61 The default behavior is the equivalent of this:
62 return socket(addr->family, addr->socktype, addr->protocol);
63
65 All
66
68 /* make libcurl use the already established socket 'sockfd' */
69
70 static curl_socket_t opensocket(void *clientp,
71 curlsocktype purpose,
72 struct curl_sockaddr *address)
73 {
74 curl_socket_t sockfd;
75 sockfd = *(curl_socket_t *)clientp;
76 /* the actual externally set socket is passed in via the OPENSOCKETDATA
77 option */
78 return sockfd;
79 }
80
81 static int sockopt_callback(void *clientp, curl_socket_t curlfd,
82 curlsocktype purpose)
83 {
84 /* This return code was added in libcurl 7.21.5 */
85 return CURL_SOCKOPT_ALREADY_CONNECTED;
86 }
87
88 curl = curl_easy_init();
89 if(curl) {
90 /* libcurl will internally think that you connect to the host
91 * and port that you specify in the URL option. */
92 curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
93 /* call this function to get a socket */
94 curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
95 curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
96
97 /* call this function to set options for the socket */
98 curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
99
100 res = curl_easy_perform(curl);
101
102 curl_easy_cleanup(curl);
103 }
104
106 Added in 7.17.1.
107
109 Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION
110 if not.
111
113 CURLOPT_OPENSOCKETDATA(3), CURLOPT_SOCKOPTFUNCTION(3), CURLOPT_CLOS‐
114 ESOCKETFUNCTION(3),
115
116
117
118libcurl 7.64.0 May 15, 2017 CURLOPT_OPENSOCKETFUNCTION(3)