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