1CURLOPT_SOCKOPTFUNCTION(3) curl_easy_setopt options CURLOPT_SOCKOPTFUNCTION(3)
2
3
4

NAME

6       CURLOPT_SOCKOPTFUNCTION - set callback for setting socket options
7

SYNOPSIS

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       #define CURL_SOCKOPT_OK 0
18       #define CURL_SOCKOPT_ERROR 1 /* causes libcurl to abort and return
19                                       CURLE_ABORTED_BY_CALLBACK */
20       #define CURL_SOCKOPT_ALREADY_CONNECTED 2
21
22       int sockopt_callback(void *clientp,
23                            curl_socket_t curlfd,
24                            curlsocktype purpose);
25
26       CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
27

DESCRIPTION

29       Pass a pointer to your callback function, which should match the proto‐
30       type shown above.
31
32       When set, this callback function gets called by libcurl when the socket
33       has  been created, but before the connect call to allow applications to
34       change specific socket options. The callback's purpose argument identi‐
35       fies the exact purpose for this particular socket:
36
37       CURLSOCKTYPE_IPCXN  for  actively  created  connections or since 7.28.0
38       CURLSOCKTYPE_ACCEPT  for  FTP  when  the  connection  was  setup   with
39       PORT/EPSV  (in  earlier  versions  these sockets weren't passed to this
40       callback).
41
42       Future versions of libcurl may support more  purposes.  libcurl  passes
43       the  newly  created  socket  descriptor  to  the callback in the curlfd
44       parameter so additional setsockopt() calls can be done  at  the  user's
45       discretion.
46
47       The  clientp pointer contains whatever user-defined value set using the
48       CURLOPT_SOCKOPTDATA(3) function.
49
50       Return CURL_SOCKOPT_OK from the callback on success. Return  CURL_SOCK‐
51       OPT_ERROR  from  the callback function to signal an unrecoverable error
52       to  the  library  and   it   will   close   the   socket   and   return
53       CURLE_COULDNT_CONNECT.  Alternatively, the callback function can return
54       CURL_SOCKOPT_ALREADY_CONNECTED, to tell  libcurl  that  the  socket  is
55       already connected and then libcurl will not attempt to connect it. This
56       allows an application to pass in an already connected socket with  CUR‐
57       LOPT_OPENSOCKETFUNCTION(3) and then have this function make libcurl not
58       attempt to connect (again).
59

DEFAULT

61       By default, this callback is NULL and unused.
62

PROTOCOLS

64       All
65

EXAMPLE

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

AVAILABILITY

104       Added in 7.16.0. The  CURL_SOCKOPT_ALREADY_CONNECTED  return  code  was
105       added in 7.21.5.
106

RETURN VALUE

108       Returns  CURLE_OK  if the option is supported, and CURLE_UNKNOWN_OPTION
109       if not.
110

SEE ALSO

112       CURLOPT_SOCKOPTDATA(3), CURLOPT_OPENSOCKETFUNCTION(3),
113
114
115
116libcurl 7.71.1                   May 15, 2017       CURLOPT_SOCKOPTFUNCTION(3)
Impressum