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