1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_OPENSOCKETFUNCTION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_CLOSESOCKETFUNCTION (3)
9  - CURLOPT_OPENSOCKETFUNCTION (3)
10  - CURLOPT_SOCKOPTFUNCTION (3)
11---
12
13# NAME
14
15CURLOPT_OPENSOCKETFUNCTION - callback for opening socket
16
17# SYNOPSIS
18
19~~~c
20#include <curl/curl.h>
21
22typedef enum  {
23  CURLSOCKTYPE_IPCXN,  /* socket created for a specific IP connection */
24} curlsocktype;
25
26struct curl_sockaddr {
27  int family;
28  int socktype;
29  int protocol;
30  unsigned int addrlen;
31  struct sockaddr addr;
32};
33
34curl_socket_t opensocket_callback(void *clientp,
35                                  curlsocktype purpose,
36                                  struct curl_sockaddr *address);
37
38CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETFUNCTION, opensocket_callback);
39~~~
40
41# DESCRIPTION
42
43Pass a pointer to your callback function, which should match the prototype
44shown above.
45
46This callback function gets called by libcurl instead of the *socket(2)*
47call. The callback's *purpose* argument identifies the exact purpose for
48this particular socket. *CURLSOCKTYPE_IPCXN* is for IP based connections
49and is the only purpose currently used in libcurl. Future versions of libcurl
50may support more purposes.
51
52The *clientp* pointer contains whatever user-defined value set using the
53CURLOPT_OPENSOCKETDATA(3) function.
54
55The callback gets the resolved peer address as the *address* argument and
56is allowed to modify the address or refuse to connect completely. The callback
57function should return the newly created socket or *CURL_SOCKET_BAD* in
58case no connection could be established or another error was detected. Any
59additional *setsockopt(2)* calls can of course be done on the socket at
60the user's discretion. A *CURL_SOCKET_BAD* return value from the callback
61function signals an unrecoverable error to libcurl and it returns
62*CURLE_COULDNT_CONNECT* from the function that triggered this callback.
63This return code can be used for IP address block listing.
64
65If you want to pass in a socket with an already established connection, pass
66the socket back with this callback and then use
67CURLOPT_SOCKOPTFUNCTION(3) to signal that it already is connected.
68
69# DEFAULT
70
71The default behavior is the equivalent of this:
72~~~c
73   return socket(addr->family, addr->socktype, addr->protocol);
74~~~
75
76# PROTOCOLS
77
78All
79
80# EXAMPLE
81
82~~~c
83/* make libcurl use the already established socket 'sockfd' */
84
85static curl_socket_t opensocket(void *clientp,
86                                curlsocktype purpose,
87                                struct curl_sockaddr *address)
88{
89  curl_socket_t sockfd;
90  sockfd = *(curl_socket_t *)clientp;
91  /* the actual externally set socket is passed in via the OPENSOCKETDATA
92     option */
93  return sockfd;
94}
95
96static int sockopt_callback(void *clientp, curl_socket_t curlfd,
97                            curlsocktype purpose)
98{
99  /* This return code was added in libcurl 7.21.5 */
100  return CURL_SOCKOPT_ALREADY_CONNECTED;
101}
102
103int main(void)
104{
105  CURL *curl = curl_easy_init();
106  if(curl) {
107    CURLcode res;
108    extern int sockfd; /* the already connected one */
109    /* libcurl thinks that you connect to the host
110     * and port that you specify in the URL option. */
111    curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
112    /* call this function to get a socket */
113    curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
114    curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
115
116    /* call this function to set options for the socket */
117    curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
118
119    res = curl_easy_perform(curl);
120
121    curl_easy_cleanup(curl);
122  }
123}
124~~~
125
126# AVAILABILITY
127
128Added in 7.17.1.
129
130# RETURN VALUE
131
132Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
133