CURLOPT_OPENSOCKETDATA.md (2219B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_OPENSOCKETDATA 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_CLOSESOCKETFUNCTION (3) 9 - CURLOPT_OPENSOCKETFUNCTION (3) 10 - CURLOPT_SOCKOPTFUNCTION (3) 11 Protocol: 12 - All 13 Added-in: 7.17.1 14 --- 15 16 # NAME 17 18 CURLOPT_OPENSOCKETDATA - pointer passed to open socket callback 19 20 # SYNOPSIS 21 22 ~~~c 23 #include <curl/curl.h> 24 25 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_OPENSOCKETDATA, void *pointer); 26 ~~~ 27 28 # DESCRIPTION 29 30 Pass a *pointer* that is untouched by libcurl and passed as the first 31 argument in the open socket callback set with 32 CURLOPT_OPENSOCKETFUNCTION(3). 33 34 # DEFAULT 35 36 NULL 37 38 # %PROTOCOLS% 39 40 # EXAMPLE 41 42 ~~~c 43 /* make libcurl use the already established socket 'sockfd' */ 44 45 static 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 56 static 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 63 int 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 91 curl_easy_setopt(3) returns a CURLcode indicating success or error. 92 93 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 94 libcurl-errors(3).