• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.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)
11Protocol:
12  - All
13Added-in: 7.17.1
14---
15
16# NAME
17
18CURLOPT_OPENSOCKETFUNCTION - callback for opening socket
19
20# SYNOPSIS
21
22~~~c
23#include <curl/curl.h>
24
25typedef enum  {
26  CURLSOCKTYPE_IPCXN,  /* socket created for a specific IP connection */
27} curlsocktype;
28
29struct curl_sockaddr {
30  int family;
31  int socktype;
32  int protocol;
33  unsigned int addrlen;
34  struct sockaddr addr;
35};
36
37curl_socket_t opensocket_callback(void *clientp,
38                                  curlsocktype purpose,
39                                  struct curl_sockaddr *address);
40
41CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETFUNCTION, opensocket_callback);
42~~~
43
44# DESCRIPTION
45
46Pass a pointer to your callback function, which should match the prototype
47shown above.
48
49This callback function gets called by libcurl instead of the *socket(2)*
50call. The callback's *purpose* argument identifies the exact purpose for
51this particular socket. *CURLSOCKTYPE_IPCXN* is for IP based connections
52and is the only purpose currently used in libcurl. Future versions of libcurl
53may support more purposes.
54
55The *clientp* pointer contains whatever user-defined value set using the
56CURLOPT_OPENSOCKETDATA(3) function.
57
58The callback gets the resolved peer address as the *address* argument and
59is allowed to modify the address or refuse to connect completely. The callback
60function should return the newly created socket or *CURL_SOCKET_BAD* in
61case no connection could be established or another error was detected. Any
62additional *setsockopt(2)* calls can of course be done on the socket at
63the user's discretion.
64
65If *CURL_SOCKET_BAD* is returned by the callback then libcurl treats it as a
66failed connection and tries to open a socket to connect to a different IP
67address associated with the transfer. If there are no more addresses to try
68then libcurl fails the transfer with error code *CURLE_COULDNT_CONNECT*.
69
70You can get the IP address that curl is opening the socket for by casting
71*address-\>addr* to `sockaddr_in` if *address-\>family* is `AF_INET`, or to
72`sockaddr_in6` if *address-\>family* is `AF_INET6`. For an example of how that
73data can be compared against refer to *docs/examples/block_ip.c*.
74
75If you want to pass in a socket with an already established connection, pass
76the socket back with this callback and then use CURLOPT_SOCKOPTFUNCTION(3) to
77signal that it already is connected.
78
79# DEFAULT
80
81The equivalent of this:
82~~~c
83   return socket(addr->family, addr->socktype, addr->protocol);
84~~~
85
86# %PROTOCOLS%
87
88# EXAMPLE
89
90~~~c
91/* make libcurl use the already established socket 'sockfd' */
92
93static curl_socket_t opensocket(void *clientp,
94                                curlsocktype purpose,
95                                struct curl_sockaddr *address)
96{
97  curl_socket_t sockfd;
98  sockfd = *(curl_socket_t *)clientp;
99  /* the actual externally set socket is passed in via the OPENSOCKETDATA
100     option */
101  return sockfd;
102}
103
104static int sockopt_callback(void *clientp, curl_socket_t curlfd,
105                            curlsocktype purpose)
106{
107  /* This return code was added in libcurl 7.21.5 */
108  return CURL_SOCKOPT_ALREADY_CONNECTED;
109}
110
111int main(void)
112{
113  CURL *curl = curl_easy_init();
114  if(curl) {
115    CURLcode res;
116    extern int sockfd; /* the already connected one */
117    /* libcurl thinks that you connect to the host
118     * and port that you specify in the URL option. */
119    curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
120    /* call this function to get a socket */
121    curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
122    curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
123
124    /* call this function to set options for the socket */
125    curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
126
127    res = curl_easy_perform(curl);
128
129    curl_easy_cleanup(curl);
130  }
131}
132~~~
133
134# %AVAILABILITY%
135
136# RETURN VALUE
137
138curl_easy_setopt(3) returns a CURLcode indicating success or error.
139
140CURLE_OK (0) means everything was OK, non-zero means an error occurred, see
141libcurl-errors(3).
142