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