xref: /curl/lib/asyn.h (revision c074ba64)
1 #ifndef HEADER_CURL_ASYN_H
2 #define HEADER_CURL_ASYN_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26 
27 #include "curl_setup.h"
28 #include "curl_addrinfo.h"
29 
30 struct addrinfo;
31 struct hostent;
32 struct Curl_easy;
33 struct connectdata;
34 struct Curl_dns_entry;
35 
36 /*
37  * This header defines all functions in the internal asynch resolver interface.
38  * All asynch resolvers need to provide these functions.
39  * asyn-ares.c and asyn-thread.c are the current implementations of asynch
40  * resolver backends.
41  */
42 
43 /*
44  * Curl_resolver_global_init()
45  *
46  * Called from curl_global_init() to initialize global resolver environment.
47  * Returning anything else than CURLE_OK fails curl_global_init().
48  */
49 int Curl_resolver_global_init(void);
50 
51 /*
52  * Curl_resolver_global_cleanup()
53  * Called from curl_global_cleanup() to destroy global resolver environment.
54  */
55 void Curl_resolver_global_cleanup(void);
56 
57 /*
58  * Curl_resolver_init()
59  * Called from curl_easy_init() -> Curl_open() to initialize resolver
60  * URL-state specific environment ('resolver' member of the UrlState
61  * structure). Should fill the passed pointer by the initialized handler.
62  * Returning anything else than CURLE_OK fails curl_easy_init() with the
63  * correspondent code.
64  */
65 CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver);
66 
67 /*
68  * Curl_resolver_cleanup()
69  * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
70  * URL-state specific environment ('resolver' member of the UrlState
71  * structure). Should destroy the handler and free all resources connected to
72  * it.
73  */
74 void Curl_resolver_cleanup(void *resolver);
75 
76 /*
77  * Curl_resolver_duphandle()
78  * Called from curl_easy_duphandle() to duplicate resolver URL-state specific
79  * environment ('resolver' member of the UrlState structure). Should
80  * duplicate the 'from' handle and pass the resulting handle to the 'to'
81  * pointer. Returning anything else than CURLE_OK causes failed
82  * curl_easy_duphandle() call.
83  */
84 CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to,
85                                  void *from);
86 
87 /*
88  * Curl_resolver_cancel().
89  *
90  * It is called from inside other functions to cancel currently performing
91  * resolver request. Should also free any temporary resources allocated to
92  * perform a request. This never waits for resolver threads to complete.
93  *
94  * It is safe to call this when conn is in any state.
95  */
96 void Curl_resolver_cancel(struct Curl_easy *data);
97 
98 /*
99  * Curl_resolver_kill().
100  *
101  * This acts like Curl_resolver_cancel() except it will block until any threads
102  * associated with the resolver are complete. This never blocks for resolvers
103  * that do not use threads. This is intended to be the "last chance" function
104  * that cleans up an in-progress resolver completely (before its owner is about
105  * to die).
106  *
107  * It is safe to call this when conn is in any state.
108  */
109 void Curl_resolver_kill(struct Curl_easy *data);
110 
111 /* Curl_resolver_getsock()
112  *
113  * This function is called from the multi_getsock() function.  'sock' is a
114  * pointer to an array to hold the file descriptors, with 'numsock' being the
115  * size of that array (in number of entries). This function is supposed to
116  * return bitmask indicating what file descriptors (referring to array indexes
117  * in the 'sock' array) to wait for, read/write.
118  */
119 int Curl_resolver_getsock(struct Curl_easy *data, curl_socket_t *sock);
120 
121 /*
122  * Curl_resolver_is_resolved()
123  *
124  * Called repeatedly to check if a previous name resolve request has
125  * completed. It should also make sure to time-out if the operation seems to
126  * take too long.
127  *
128  * Returns normal CURLcode errors.
129  */
130 CURLcode Curl_resolver_is_resolved(struct Curl_easy *data,
131                                    struct Curl_dns_entry **dns);
132 
133 /*
134  * Curl_resolver_wait_resolv()
135  *
136  * Waits for a resolve to finish. This function should be avoided since using
137  * this risk getting the multi interface to "hang".
138  *
139  * If 'entry' is non-NULL, make it point to the resolved dns entry
140  *
141  * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
142  * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
143  */
144 CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
145                                    struct Curl_dns_entry **dnsentry);
146 
147 /*
148  * Curl_resolver_getaddrinfo() - when using this resolver
149  *
150  * Returns name information about the given hostname and port number. If
151  * successful, the 'hostent' is returned and the fourth argument will point to
152  * memory we need to free after use. That memory *MUST* be freed with
153  * Curl_freeaddrinfo(), nothing else.
154  *
155  * Each resolver backend must of course make sure to return data in the
156  * correct format to comply with this.
157  */
158 struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
159                                                 const char *hostname,
160                                                 int port,
161                                                 int *waitp);
162 
163 #ifndef CURLRES_ASYNCH
164 /* convert these functions if an asynch resolver is not used */
165 #define Curl_resolver_cancel(x) Curl_nop_stmt
166 #define Curl_resolver_kill(x) Curl_nop_stmt
167 #define Curl_resolver_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST
168 #define Curl_resolver_wait_resolv(x,y) CURLE_COULDNT_RESOLVE_HOST
169 #define Curl_resolver_duphandle(x,y,z) CURLE_OK
170 #define Curl_resolver_init(x,y) CURLE_OK
171 #define Curl_resolver_global_init() CURLE_OK
172 #define Curl_resolver_global_cleanup() Curl_nop_stmt
173 #define Curl_resolver_cleanup(x) Curl_nop_stmt
174 #endif
175 
176 #ifdef CURLRES_ASYNCH
177 #define Curl_resolver_asynch() 1
178 #else
179 #define Curl_resolver_asynch() 0
180 #endif
181 
182 
183 /********** end of generic resolver interface functions *****************/
184 #endif /* HEADER_CURL_ASYN_H */
185