diff options
Diffstat (limited to 'src/lib/internal.h')
-rw-r--r-- | src/lib/internal.h | 412 |
1 files changed, 412 insertions, 0 deletions
diff --git a/src/lib/internal.h b/src/lib/internal.h new file mode 100644 index 00000000..8de88598 --- /dev/null +++ b/src/lib/internal.h | |||
@@ -0,0 +1,412 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 2007-2017 Daniel Pittman and Christian Grothoff | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file microhttpd/internal.h | ||
22 | * @brief internal shared structures | ||
23 | * @author Daniel Pittman | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #ifndef INTERNAL_H | ||
28 | #define INTERNAL_H | ||
29 | |||
30 | #include "mhd_options.h" | ||
31 | #include "platform.h" | ||
32 | #include "microhttpd2.h" | ||
33 | #include "microhttpd_tls.h" | ||
34 | #include "mhd_assert.h" | ||
35 | |||
36 | #ifdef HTTPS_SUPPORT | ||
37 | #include <gnutls/gnutls.h> | ||
38 | #if GNUTLS_VERSION_MAJOR >= 3 | ||
39 | #include <gnutls/abstract.h> | ||
40 | #endif | ||
41 | #endif /* HTTPS_SUPPORT */ | ||
42 | |||
43 | #ifdef HAVE_STDBOOL_H | ||
44 | #include <stdbool.h> | ||
45 | #endif | ||
46 | #ifdef MHD_PANIC | ||
47 | /* Override any defined MHD_PANIC macro with proper one */ | ||
48 | #undef MHD_PANIC | ||
49 | #endif /* MHD_PANIC */ | ||
50 | |||
51 | #ifdef HAVE_MESSAGES | ||
52 | /** | ||
53 | * Trigger 'panic' action based on fatal errors. | ||
54 | * | ||
55 | * @param msg error message (const char *) | ||
56 | */ | ||
57 | #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, msg); BUILTIN_NOT_REACHED; } while (0) | ||
58 | #else | ||
59 | /** | ||
60 | * Trigger 'panic' action based on fatal errors. | ||
61 | * | ||
62 | * @param msg error message (const char *) | ||
63 | */ | ||
64 | #define MHD_PANIC(msg) do { mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); BUILTIN_NOT_REACHED; } while (0) | ||
65 | #endif | ||
66 | |||
67 | #include "mhd_threads.h" | ||
68 | #include "mhd_locks.h" | ||
69 | #include "mhd_sockets.h" | ||
70 | #include "mhd_itc_types.h" | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Close FD and abort execution if error is detected. | ||
75 | * @param fd the FD to close | ||
76 | */ | ||
77 | #define MHD_fd_close_chk_(fd) do { \ | ||
78 | if (0 == close ((fd)) && (EBADF == errno)) \ | ||
79 | MHD_PANIC(_("Failed to close FD.\n")); \ | ||
80 | } while(0) | ||
81 | |||
82 | /** | ||
83 | * Should we perform additional sanity checks at runtime (on our internal | ||
84 | * invariants)? This may lead to aborts, but can be useful for debugging. | ||
85 | */ | ||
86 | #define EXTRA_CHECKS MHD_NO | ||
87 | |||
88 | #define MHD_MAX(a,b) (((a)<(b)) ? (b) : (a)) | ||
89 | #define MHD_MIN(a,b) (((a)<(b)) ? (a) : (b)) | ||
90 | |||
91 | |||
92 | /** | ||
93 | * Minimum size by which MHD tries to increment read/write buffers. | ||
94 | * We usually begin with half the available pool space for the | ||
95 | * IO-buffer, but if absolutely needed we additively grow by the | ||
96 | * number of bytes given here (up to -- theoretically -- the full pool | ||
97 | * space). | ||
98 | */ | ||
99 | #define MHD_BUF_INC_SIZE 1024 | ||
100 | |||
101 | |||
102 | /** | ||
103 | * Handler for fatal errors. | ||
104 | */ | ||
105 | extern MHD_PanicCallback mhd_panic; | ||
106 | |||
107 | /** | ||
108 | * Closure argument for "mhd_panic". | ||
109 | */ | ||
110 | extern void *mhd_panic_cls; | ||
111 | |||
112 | /* If we have Clang or gcc >= 4.5, use __buildin_unreachable() */ | ||
113 | #if defined(__clang__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) | ||
114 | #define BUILTIN_NOT_REACHED __builtin_unreachable() | ||
115 | #elif defined(_MSC_FULL_VER) | ||
116 | #define BUILTIN_NOT_REACHED __assume(0) | ||
117 | #else | ||
118 | #define BUILTIN_NOT_REACHED | ||
119 | #endif | ||
120 | |||
121 | #ifndef MHD_STATICSTR_LEN_ | ||
122 | /** | ||
123 | * Determine length of static string / macro strings at compile time. | ||
124 | */ | ||
125 | #define MHD_STATICSTR_LEN_(macro) (sizeof(macro)/sizeof(char) - 1) | ||
126 | #endif /* ! MHD_STATICSTR_LEN_ */ | ||
127 | |||
128 | |||
129 | /** | ||
130 | * State kept for each MHD daemon. All connections are kept in two | ||
131 | * doubly-linked lists. The first one reflects the state of the | ||
132 | * connection in terms of what operations we are waiting for (read, | ||
133 | * write, locally blocked, cleanup) whereas the second is about its | ||
134 | * timeout state (default or custom). | ||
135 | */ | ||
136 | struct MHD_Daemon | ||
137 | { | ||
138 | /** | ||
139 | * Function to call to handle incoming requests. | ||
140 | */ | ||
141 | MHD_RequestCallback rc; | ||
142 | |||
143 | /** | ||
144 | * Closure for @e rc. | ||
145 | */ | ||
146 | void *rc_cls; | ||
147 | |||
148 | /** | ||
149 | * Function to call for logging. | ||
150 | */ | ||
151 | MHD_LoggingCallback logger; | ||
152 | |||
153 | /** | ||
154 | * Closure for @e logger. | ||
155 | */ | ||
156 | void *logger_cls; | ||
157 | |||
158 | /** | ||
159 | * Function to call to accept/reject connections based on | ||
160 | * the client's IP address. | ||
161 | */ | ||
162 | MHD_AcceptPolicyCallback accept_policy_cb; | ||
163 | |||
164 | /** | ||
165 | * Closure for @e accept_policy_cb. | ||
166 | */ | ||
167 | void *accept_policy_cb_cls; | ||
168 | |||
169 | /** | ||
170 | * Function to call on the full URL early for logging. | ||
171 | */ | ||
172 | MHD_EarlyUriLogCallback early_uri_logger_cb; | ||
173 | |||
174 | /** | ||
175 | * Closure for @e early_uri_logger_cb. | ||
176 | */ | ||
177 | void *early_uri_logger_cls; | ||
178 | |||
179 | /** | ||
180 | * Function to call whenever a connection is started or | ||
181 | * closed. | ||
182 | */ | ||
183 | MHD_NotifyConnectionCallback notify_connection_cb; | ||
184 | |||
185 | /** | ||
186 | * Closure for @e notify_connection_cb. | ||
187 | */ | ||
188 | void *notify_connection_cb_cls; | ||
189 | |||
190 | /** | ||
191 | * Function to call to unescape sequences in URIs and URI arguments. | ||
192 | * See #MHD_daemon_unescape_cb(). | ||
193 | */ | ||
194 | MHD_UnescapeCallback unescape_cb; | ||
195 | |||
196 | /** | ||
197 | * Closure for @e unescape_cb. | ||
198 | */ | ||
199 | void *unescape_cb_cls; | ||
200 | |||
201 | #if HTTPS_SUPPORT | ||
202 | /** | ||
203 | * Which TLS backend should be used. NULL for no TLS. | ||
204 | * This is merely the handle to the dlsym() object, not | ||
205 | * the API. | ||
206 | */ | ||
207 | void *tls_backend_lib; | ||
208 | |||
209 | /** | ||
210 | * Callback functions to use for TLS operations. | ||
211 | */ | ||
212 | struct MHD_TLS_Plugin *tls_api; | ||
213 | #endif | ||
214 | #if ENABLE_DAUTH | ||
215 | |||
216 | /** | ||
217 | * Random values to be used by digest authentication module. | ||
218 | * Size given in @e digest_auth_random_buf_size. | ||
219 | */ | ||
220 | const void *digest_auth_random_buf; | ||
221 | #endif | ||
222 | |||
223 | /** | ||
224 | * Socket address to bind to for the listen socket. | ||
225 | */ | ||
226 | struct sockaddr_storage listen_sa; | ||
227 | |||
228 | /** | ||
229 | * Number of (valid) bytes in @e listen_sa. Zero | ||
230 | * if @e listen_sa is not initialized. | ||
231 | */ | ||
232 | size_t listen_sa_len; | ||
233 | |||
234 | /** | ||
235 | * Buffer size to use for each connection. Default | ||
236 | * is #MHD_POOL_SIZE_DEFAULT. | ||
237 | */ | ||
238 | size_t connection_memory_limit_b; | ||
239 | |||
240 | /** | ||
241 | * Default minimum size by which MHD tries to increment read/write | ||
242 | * buffers. We usually begin with half the available pool space for | ||
243 | * the IO-buffer, but if absolutely needed we additively grow by the | ||
244 | * number of bytes given here (up to -- theoretically -- the full pool | ||
245 | * space). | ||
246 | */ | ||
247 | #define BUF_INC_SIZE_DEFAULT 1024 | ||
248 | |||
249 | /** | ||
250 | * Increment to use when growing the read buffer. Smaller | ||
251 | * than @e connection_memory_limit_b. | ||
252 | */ | ||
253 | size_t connection_memory_increment_b; | ||
254 | |||
255 | /** | ||
256 | * Desired size of the stack for threads created by MHD, | ||
257 | * 0 for system default. | ||
258 | */ | ||
259 | size_t thread_stack_limit_b; | ||
260 | |||
261 | #if ENABLE_DAUTH | ||
262 | |||
263 | /** | ||
264 | * Size of @e digest_auth_random_buf. | ||
265 | */ | ||
266 | size_t digest_auth_random_buf_size; | ||
267 | |||
268 | /** | ||
269 | * Default value for @e digest_nc_length. | ||
270 | */ | ||
271 | #define DIGEST_NC_LENGTH_DEFAULT 4 | ||
272 | |||
273 | /** | ||
274 | * Desired length of the internal array with the nonce and | ||
275 | * nonce counters for digest authentication. | ||
276 | */ | ||
277 | size_t digest_nc_length; | ||
278 | #endif | ||
279 | |||
280 | /** | ||
281 | * Default value we use for the listen backlog. | ||
282 | */ | ||
283 | #ifdef SOMAXCONN | ||
284 | #define LISTEN_BACKLOG_DEFAULT SOMAXCONN | ||
285 | #else /* !SOMAXCONN */ | ||
286 | #define LISTEN_BACKLOG_DEFAULT 511 | ||
287 | #endif | ||
288 | |||
289 | /** | ||
290 | * Backlog argument to use for listen. See | ||
291 | * #MHD_daemon_listen_backlog(). | ||
292 | */ | ||
293 | int listen_backlog; | ||
294 | |||
295 | /** | ||
296 | * Default queue length to use with fast open. | ||
297 | */ | ||
298 | #define FO_QUEUE_LENGTH_DEFAULT 50 | ||
299 | |||
300 | /** | ||
301 | * Queue length to use with fast open. | ||
302 | */ | ||
303 | unsigned int fo_queue_length; | ||
304 | |||
305 | /** | ||
306 | * Maximum number of connections MHD accepts. 0 for unlimited. | ||
307 | */ | ||
308 | unsigned int global_connection_limit; | ||
309 | |||
310 | /** | ||
311 | * Maximum number of connections we accept per IP, 0 for unlimited. | ||
312 | */ | ||
313 | unsigned int ip_connection_limit; | ||
314 | |||
315 | /** | ||
316 | * Default timeout in seconds for idle connections. | ||
317 | */ | ||
318 | unsigned int connection_default_timeout_s; | ||
319 | |||
320 | /** | ||
321 | * Listen socket we should use, MHD_INVALID_SOCKET means | ||
322 | * we are to initialize the socket from the other options given. | ||
323 | */ | ||
324 | MHD_socket listen_socket; | ||
325 | |||
326 | /** | ||
327 | * Which threading model do we use? Postive | ||
328 | * numbers indicate the number of worker threads to be used. | ||
329 | */ | ||
330 | enum MHD_ThreadingModel threading_model; | ||
331 | |||
332 | /** | ||
333 | * When should we use TCP_FASTOPEN? | ||
334 | * See #MHD_daemon_tcp_fastopen(). | ||
335 | */ | ||
336 | enum MHD_FastOpenMethod fast_open_method; | ||
337 | |||
338 | /** | ||
339 | * Address family to use when listening. | ||
340 | * Default is #MHD_AF_AUTO. | ||
341 | */ | ||
342 | enum MHD_AddressFamily listen_af; | ||
343 | |||
344 | /** | ||
345 | * Sets active/desired style of the event loop. | ||
346 | * (Auto only possible during initialization, later set to | ||
347 | * the actual style we use.) | ||
348 | */ | ||
349 | enum MHD_EventLoopSyscall event_loop_syscall; | ||
350 | |||
351 | /** | ||
352 | * How strictly do we enforce the HTTP protocol? | ||
353 | * See #MHD_daemon_protocol_strict_level(). | ||
354 | */ | ||
355 | enum MHD_ProtocolStrictLevel protocol_strict_level; | ||
356 | |||
357 | /** | ||
358 | * On which port should we listen on? Only effective if we were not | ||
359 | * given a listen socket or a full address via | ||
360 | * #MHD_daemon_bind_sa(). 0 means not set, which means to default | ||
361 | * to 80 (http) or 443 (https) respectively. | ||
362 | */ | ||
363 | uint16_t listen_port; | ||
364 | |||
365 | /** | ||
366 | * Suppress generating the "Date:" header, this system | ||
367 | * lacks an RTC (or developer is hyper-optimizing). See | ||
368 | * #MHD_daemon_suppress_date_no_clock(). | ||
369 | */ | ||
370 | bool suppress_date; | ||
371 | |||
372 | /** | ||
373 | * The use of the inter-thread communication channel is disabled. | ||
374 | * See #MHD_daemon_disable_itc(). | ||
375 | */ | ||
376 | bool disable_itc; | ||
377 | |||
378 | /** | ||
379 | * Disable #MHD_action_suspend() functionality. See | ||
380 | * #MHD_daemon_disallow_suspend_resume(). | ||
381 | */ | ||
382 | bool disallow_suspend_resume; | ||
383 | |||
384 | /** | ||
385 | * Disable #MHD_action_upgrade() functionality. See | ||
386 | * #MHD_daemon_disallow_upgrade(). | ||
387 | */ | ||
388 | bool disallow_upgrade; | ||
389 | |||
390 | /** | ||
391 | * Disables optional calls to `shutdown()` and enables aggressive | ||
392 | * non-blocking optimistic reads and other potentially unsafe | ||
393 | * optimizations. See #MHD_daemon_enable_turbo(). | ||
394 | */ | ||
395 | bool enable_turbo; | ||
396 | |||
397 | /** | ||
398 | * Allow reusing the address:port combination when binding. | ||
399 | * See #MHD_daemon_listen_allow_address_reuse(). | ||
400 | */ | ||
401 | bool allow_address_reuse; | ||
402 | |||
403 | |||
404 | |||
405 | }; | ||
406 | |||
407 | |||
408 | |||
409 | |||
410 | |||
411 | |||
412 | #endif | ||