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
12Added-in: 7.21.7
13---
14
15# NAME
16
17CURLOPT_CLOSESOCKETFUNCTION - callback to socket close replacement
18
19# SYNOPSIS
20
21~~~c
22#include <curl/curl.h>
23
24int closesocket_callback(void *clientp, curl_socket_t item);
25
26CURLcode curl_easy_setopt(CURL *handle, CURLOPT_CLOSESOCKETFUNCTION,
27                          closesocket_callback);
28~~~
29
30# DESCRIPTION
31
32Pass a pointer to your callback function, which should match the prototype
33shown above.
34
35This 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
37descriptors). This is pretty much the reverse to the
38CURLOPT_OPENSOCKETFUNCTION(3) option. Return 0 to signal success and 1
39if there was an error.
40
41The *clientp* pointer is set with
42CURLOPT_CLOSESOCKETDATA(3). *item* is the socket libcurl wants to be
43closed.
44
45# DEFAULT
46
47Use the standard socket close function.
48
49# %PROTOCOLS%
50
51# EXAMPLE
52
53~~~c
54struct priv {
55  void *custom;
56};
57
58static int closesocket(void *clientp, curl_socket_t item)
59{
60  struct priv *my = clientp;
61  printf("our ptr: %p\n", my->custom);
62
63  printf("libcurl wants to close %d now\n", (int)item);
64  return 0;
65}
66
67int main(void)
68{
69  struct priv myown;
70  CURL *curl = curl_easy_init();
71
72  /* call this function to close sockets */
73  curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, closesocket);
74  curl_easy_setopt(curl, CURLOPT_CLOSESOCKETDATA, &myown);
75
76  curl_easy_perform(curl);
77  curl_easy_cleanup(curl);
78}
79~~~
80
81# %AVAILABILITY%
82
83# RETURN VALUE
84
85Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
86