1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_OPENSOCKETDATA
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_CLOSESOCKETFUNCTION (3)
9  - CURLOPT_OPENSOCKETFUNCTION (3)
10  - CURLOPT_SOCKOPTFUNCTION (3)
11---
12
13# NAME
14
15CURLOPT_OPENSOCKETDATA - pointer passed to open socket callback
16
17# SYNOPSIS
18
19~~~c
20#include <curl/curl.h>
21
22CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETDATA, void *pointer);
23~~~
24
25# DESCRIPTION
26
27Pass a *pointer* that is untouched by libcurl and passed as the first
28argument in the open socket callback set with
29CURLOPT_OPENSOCKETFUNCTION(3).
30
31# DEFAULT
32
33The default value of this parameter is NULL.
34
35# PROTOCOLS
36
37All
38
39# EXAMPLE
40
41~~~c
42/* make libcurl use the already established socket 'sockfd' */
43
44static curl_socket_t opensocket(void *clientp,
45                                curlsocktype purpose,
46                                struct curl_sockaddr *address)
47{
48  curl_socket_t sockfd;
49  sockfd = *(curl_socket_t *)clientp;
50  /* the actual externally set socket is passed in via the OPENSOCKETDATA
51     option */
52  return sockfd;
53}
54
55static int sockopt_callback(void *clientp, curl_socket_t curlfd,
56                            curlsocktype purpose)
57{
58  /* This return code was added in libcurl 7.21.5 */
59  return CURL_SOCKOPT_ALREADY_CONNECTED;
60}
61
62int main(void)
63{
64  CURL *curl = curl_easy_init();
65  if(curl) {
66    CURLcode res;
67    extern int sockfd; /* the already connected one */
68
69    /* libcurl thinks that you connect to the host
70     * and port that you specify in the URL option. */
71    curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
72    /* call this function to get a socket */
73    curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
74    curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
75
76    /* call this function to set options for the socket */
77    curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
78
79    res = curl_easy_perform(curl);
80
81    curl_easy_cleanup(curl);
82  }
83}
84~~~
85
86# AVAILABILITY
87
88Added in 7.17.1
89
90# RETURN VALUE
91
92Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
93