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