CURLOPT_CLOSESOCKETFUNCTION.md (2186B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_CLOSESOCKETFUNCTION 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_CLOSESOCKETDATA (3) 9 - CURLOPT_OPENSOCKETFUNCTION (3) 10 Protocol: 11 - All 12 Added-in: 7.21.7 13 --- 14 15 # NAME 16 17 CURLOPT_CLOSESOCKETFUNCTION - callback to socket close replacement 18 19 # SYNOPSIS 20 21 ~~~c 22 #include <curl/curl.h> 23 24 int closesocket_callback(void *clientp, curl_socket_t item); 25 26 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_CLOSESOCKETFUNCTION, 27 closesocket_callback); 28 ~~~ 29 30 # DESCRIPTION 31 32 Pass a pointer to your callback function, which should match the prototype 33 shown above. 34 35 This callback function gets called by libcurl instead of the *close(3)* or 36 *closesocket(3)* call when sockets are closed (not for any other file 37 descriptors). This is pretty much the reverse to the 38 CURLOPT_OPENSOCKETFUNCTION(3) option. Return 0 to signal success and 1 39 if there was an error. 40 41 The *clientp* pointer is set with 42 CURLOPT_CLOSESOCKETDATA(3). *item* is the socket libcurl wants to be 43 closed. 44 45 Note that when using multi/share handles, your callback may get invoked even 46 after the easy handle has been cleaned up. The callback and data is 47 inherited by a new connection and that connection may live longer 48 than the transfer itself in the multi/share handle's connection cache. 49 50 # DEFAULT 51 52 Use the standard socket close function. 53 54 # %PROTOCOLS% 55 56 # EXAMPLE 57 58 ~~~c 59 struct priv { 60 void *custom; 61 }; 62 63 static int closesocket(void *clientp, curl_socket_t item) 64 { 65 struct priv *my = clientp; 66 printf("our ptr: %p\n", my->custom); 67 68 printf("libcurl wants to close %d now\n", (int)item); 69 return 0; 70 } 71 72 int main(void) 73 { 74 struct priv myown; 75 CURL *curl = curl_easy_init(); 76 77 /* call this function to close sockets */ 78 curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, closesocket); 79 curl_easy_setopt(curl, CURLOPT_CLOSESOCKETDATA, &myown); 80 81 curl_easy_perform(curl); 82 curl_easy_cleanup(curl); 83 } 84 ~~~ 85 86 # %AVAILABILITY% 87 88 # RETURN VALUE 89 90 curl_easy_setopt(3) returns a CURLcode indicating success or error. 91 92 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 93 libcurl-errors(3).