GNU libmicrohttpd  0.9.72
daemon_options.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2007-2018 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 
25 #include "internal.h"
26 #if HAVE_DLFCN_H
27 #include <dlfcn.h>
28 #endif
29 
39 void
41  MHD_LoggingCallback logger,
42  void *logger_cls)
43 {
44  daemon->logger = logger;
45  daemon->logger_cls = logger_cls;
46 }
47 
48 
54 void
56 {
57  daemon->suppress_date = true;
58 }
59 
60 
78 void
80 {
81  daemon->disable_itc = true;
82 }
83 
84 
93 void
95 {
96  daemon->enable_turbo = true;
97 }
98 
99 
109 void
111 {
112  daemon->disallow_suspend_resume = true;
113 }
114 
115 
127 void
129 {
130  daemon->disallow_upgrade = true;
131 }
132 
133 
149 enum MHD_Bool
150 MHD_daemon_tcp_fastopen (struct MHD_Daemon *daemon,
151  enum MHD_FastOpenMethod fom,
152  unsigned int queue_length)
153 {
154  daemon->fast_open_method = fom;
155  daemon->fo_queue_length = queue_length;
156  switch (fom)
157  {
158  case MHD_FOM_DISABLE:
159  return MHD_YES;
160  case MHD_FOM_AUTO:
161  return MHD_YES;
162  case MHD_FOM_REQUIRE:
163 #ifdef TCP_FASTOPEN
164  return MHD_YES;
165 #else
166  return MHD_NO;
167 #endif
168  }
169  return MHD_NO;
170 }
171 
172 
186 void
188  enum MHD_AddressFamily af,
189  uint16_t port)
190 {
191  daemon->listen_af = af;
192  daemon->listen_port = port;
193 }
194 
195 
205 void
207  const struct sockaddr *sa,
208  size_t sa_len)
209 {
210  memcpy (&daemon->listen_sa,
211  sa,
212  sa_len);
213  daemon->listen_sa_len = sa_len;
214 }
215 
216 
224 void
226  int listen_backlog)
227 {
228  daemon->listen_backlog = listen_backlog;
229 }
230 
231 
242 void
244 {
245  daemon->allow_address_reuse = true;
246 }
247 
248 
255 _MHD_EXTERN void
257 {
258  daemon->enable_shoutcast = true;
259 }
260 
261 
276 void
278  MHD_socket listen_socket)
279 {
280  daemon->listen_socket = listen_socket;
281 }
282 
283 
291 enum MHD_Bool
292 MHD_daemon_event_loop (struct MHD_Daemon *daemon,
293  enum MHD_EventLoopSyscall els)
294 {
295  switch (els)
296  {
297  case MHD_ELS_AUTO:
298  break; /* should always be OK */
299  case MHD_ELS_SELECT:
300  break; /* should always be OK */
301  case MHD_ELS_POLL:
302 #ifdef HAVE_POLL
303  break;
304 #else
305  return MHD_NO; /* not supported */
306 #endif
307  case MHD_ELS_EPOLL:
308 #ifdef EPOLL_SUPPORT
309  break;
310 #else
311  return MHD_NO; /* not supported */
312 #endif
313  default:
314  return MHD_NO; /* not supported (presumably future ABI extension) */
315  }
316  daemon->event_loop_syscall = els;
317  return MHD_YES;
318 }
319 
320 
327 void
329  enum MHD_ProtocolStrictLevel sl)
330 {
331  daemon->protocol_strict_level = sl;
332 }
333 
334 
350 enum MHD_StatusCode
351 MHD_daemon_set_tls_backend (struct MHD_Daemon *daemon,
352  const char *tls_backend,
353  const char *ciphers)
354 {
355 #if ! (defined(HTTPS_SUPPORT) && defined (HAVE_DLFCN_H))
356  return MHD_SC_TLS_DISABLED;
357 #else
358  char filename[1024];
359  int res;
360  MHD_TLS_PluginInit init;
361 
362  /* todo: .dll on W32? */
363  res = MHD_snprintf_ (filename,
364  sizeof (filename),
365  "%s/libmicrohttpd_tls_%s.so",
366  MHD_PLUGIN_INSTALL_PREFIX,
367  tls_backend);
368  if (0 >= res)
369  return MHD_SC_TLS_BACKEND_UNSUPPORTED; /* string too long? */
370  if (NULL ==
371  (daemon->tls_backend_lib = dlopen (filename,
372  RTLD_NOW | RTLD_LOCAL)))
373  return MHD_SC_TLS_BACKEND_UNSUPPORTED; /* plugin not found */
374  if (NULL == (init = dlsym (daemon->tls_backend_lib,
375  "MHD_TLS_init_" MHD_TLS_ABI_VERSION_STR)))
376 
377  {
378  dlclose (daemon->tls_backend_lib);
379  daemon->tls_backend_lib = NULL;
380  return MHD_SC_TLS_BACKEND_UNSUPPORTED; /* possibly wrong version installed */
381  }
382  if (NULL == (daemon->tls_api = init (ciphers)))
383  {
384  dlclose (daemon->tls_backend_lib);
385  daemon->tls_backend_lib = NULL;
386  return MHD_SC_TLS_CIPHERS_INVALID; /* possibly wrong version installed */
387  }
388  return MHD_SC_OK;
389 #endif
390 }
391 
392 
406 enum MHD_StatusCode
408  const char *mem_key,
409  const char *mem_cert,
410  const char *pass)
411 {
412 #ifndef HTTPS_SUPPORT
413  return MHD_SC_TLS_DISABLED;
414 #else
415  struct MHD_TLS_Plugin *plugin;
416 
417  if (NULL == (plugin = daemon->tls_api))
418  return MHD_SC_TLS_BACKEND_UNINITIALIZED;
419  return plugin->init_kcp (plugin->cls,
420  mem_key,
421  mem_cert,
422  pass);
423 #endif
424 }
425 
426 
436 enum MHD_StatusCode
438  const char *dh)
439 {
440 #ifndef HTTPS_SUPPORT
441  return MHD_SC_TLS_DISABLED;
442 #else
443  struct MHD_TLS_Plugin *plugin;
444 
445  if (NULL == (plugin = daemon->tls_api))
446  return MHD_SC_TLS_BACKEND_UNINITIALIZED;
447  return plugin->init_dhparams (plugin->cls,
448  dh);
449 #endif
450 }
451 
452 
462 enum MHD_StatusCode
463 MHD_daemon_tls_mem_trust (struct MHD_Daemon *daemon,
464  const char *mem_trust)
465 {
466 #ifndef HTTPS_SUPPORT
467  return MHD_SC_TLS_DISABLED;
468 #else
469  struct MHD_TLS_Plugin *plugin;
470 
471  if (NULL == (plugin = daemon->tls_api))
472  return MHD_SC_TLS_BACKEND_UNINITIALIZED;
473  return plugin->init_mem_trust (plugin->cls,
474  mem_trust);
475 #endif
476 }
477 
478 
486 enum MHD_StatusCode
488  int gnutls_credentials)
489 {
490 #ifndef HTTPS_SUPPORT
491  return MHD_SC_TLS_DISABLED;
492 #else
493  struct MHD_TLS_Plugin *plugin;
494 
495  if (NULL == (plugin = daemon->tls_api))
496  return MHD_SC_TLS_BACKEND_UNINITIALIZED;
497  return MHD_SC_TLS_BACKEND_OPERATION_UNSUPPORTED;
498 #endif
499 }
500 
501 
519 enum MHD_StatusCode
521  void *cb)
522 {
523 #ifndef HTTPS_SUPPORT
524  return MHD_SC_TLS_DISABLED;
525 #else
526  struct MHD_TLS_Plugin *plugin;
527 
528  if (NULL == (plugin = daemon->tls_api))
529  return MHD_SC_TLS_BACKEND_UNINITIALIZED;
530  return MHD_SC_TLS_BACKEND_OPERATION_UNSUPPORTED;
531 #endif
532 }
533 
534 
542 void
544  enum MHD_ThreadingMode tm)
545 {
546  daemon->threading_mode = tm;
547 }
548 
549 
559 void
562  void *apc_cls)
563 {
564  daemon->accept_policy_cb = apc;
565  daemon->accept_policy_cb_cls = apc_cls;
566 }
567 
568 
578 void
580  MHD_EarlyUriLogCallback cb,
581  void *cb_cls)
582 {
583  daemon->early_uri_logger_cb = cb;
584  daemon->early_uri_logger_cb_cls = cb_cls;
585 }
586 
587 
596 void
599  void *ncc_cls)
600 {
601  daemon->notify_connection_cb = ncc;
602  daemon->notify_connection_cb_cls = ncc_cls;
603 }
604 
605 
617 void
619  size_t memory_limit_b,
620  size_t memory_increment_b)
621 {
622  if (memory_increment_b >= memory_limit_b)
623  MHD_PANIC ("Sane memory increment must be below memory limit.\n");
624  daemon->connection_memory_limit_b = memory_limit_b;
625  daemon->connection_memory_increment_b = memory_increment_b;
626 }
627 
628 
637 void
639  size_t stack_limit_b)
640 {
641  daemon->thread_stack_limit_b = stack_limit_b;
642 }
643 
644 
660 void
662  unsigned int global_connection_limit,
663  unsigned int ip_connection_limit)
664 {
665  daemon->global_connection_limit = global_connection_limit;
666  daemon->ip_connection_limit = ip_connection_limit;
667 }
668 
669 
678 void
680  unsigned int timeout_s)
681 {
682  daemon->connection_default_timeout = (time_t) timeout_s;
683 }
684 
685 
697 void
699  MHD_UnescapeCallback unescape_cb,
700  void *unescape_cb_cls)
701 {
702  daemon->unescape_cb = unescape_cb;
703  daemon->unescape_cb_cls = unescape_cb_cls;
704 }
705 
706 
716 void
718  size_t buf_size,
719  const void *buf)
720 {
721 #if ENABLE_DAUTH
722  daemon->digest_auth_random_buf = buf;
723  daemon->digest_auth_random_buf_size = buf_size;
724 #else
725  (void) daemon;
726  (void) buf_size;
727  (void) buf;
728  MHD_PANIC ("Digest authentication not supported by this build.\n");
729 #endif
730 }
731 
732 
740 enum MHD_StatusCode
742  size_t nc_length)
743 {
744 #if ENABLE_DAUTH
745  if ( ( (size_t) (nc_length * sizeof (struct MHD_NonceNc)))
746  / sizeof (struct MHD_NonceNc) != nc_length)
747  {
748 #ifdef HAVE_MESSAGES
749  MHD_DLOG (daemon,
750  _ ("Specified value for NC_SIZE too large.\n"));
751 #endif
752  return MHD_SC_DIGEST_AUTH_NC_LENGTH_TOO_BIG;
753  }
754  if (0 < nc_length)
755  {
756  if (NULL != daemon->nnc)
757  free (daemon->nnc);
758  daemon->nnc = malloc (daemon->nonce_nc_size
759  * sizeof (struct MHD_NonceNc));
760  if (NULL == daemon->nnc)
761  {
762 #ifdef HAVE_MESSAGES
763  MHD_DLOG (daemon,
764  _ ("Failed to allocate memory for nonce-nc map: %s\n"),
765  MHD_strerror_ (errno));
766 #endif
767  return MHD_SC_DIGEST_AUTH_NC_ALLOCATION_FAILURE;
768  }
769  }
770  daemon->digest_nc_length = nc_length;
771  return MHD_SC_OK;
772 #else
773  (void) daemon;
774  (void) nc_length;
775  return MHD_SC_DIGEST_AUTH_NOT_SUPPORTED_BY_BUILD;
776 #endif
777 }
778 
779 
780 /* end of daemon_options.c */
void MHD_daemon_set_early_uri_logger(struct MHD_Daemon *daemon, MHD_EarlyUriLogCallback cb, void *cb_cls)
void MHD_daemon_bind_socket_address(struct MHD_Daemon *daemon, const struct sockaddr *sa, size_t sa_len)
_MHD_EXTERN void MHD_daemon_enable_shoutcast(struct MHD_Daemon *daemon)
void MHD_daemon_digest_auth_random(struct MHD_Daemon *daemon, size_t buf_size, const void *buf)
enum MHD_StatusCode MHD_daemon_tls_mem_trust(struct MHD_Daemon *daemon, const char *mem_trust)
enum MHD_StatusCode MHD_daemon_tls_mem_dhparams(struct MHD_Daemon *daemon, const char *dh)
void MHD_daemon_disable_itc(struct MHD_Daemon *daemon)
void MHD_daemon_suppress_date_no_clock(struct MHD_Daemon *daemon)
void MHD_daemon_listen_socket(struct MHD_Daemon *daemon, MHD_socket listen_socket)
void MHD_daemon_disallow_upgrade(struct MHD_Daemon *daemon)
void MHD_daemon_thread_stack_size(struct MHD_Daemon *daemon, size_t stack_limit_b)
void MHD_daemon_bind_port(struct MHD_Daemon *daemon, enum MHD_AddressFamily af, uint16_t port)
void MHD_daemon_listen_backlog(struct MHD_Daemon *daemon, int listen_backlog)
void MHD_daemon_connection_limits(struct MHD_Daemon *daemon, unsigned int global_connection_limit, unsigned int ip_connection_limit)
enum MHD_StatusCode MHD_daemon_set_tls_backend(struct MHD_Daemon *daemon, const char *tls_backend, const char *ciphers)
void MHD_daemon_disallow_suspend_resume(struct MHD_Daemon *daemon)
enum MHD_StatusCode MHD_daemon_gnutls_key_and_cert_from_callback(struct MHD_Daemon *daemon, void *cb)
void MHD_daemon_enable_turbo(struct MHD_Daemon *daemon)
enum MHD_StatusCode MHD_daemon_tls_key_and_cert_from_memory(struct MHD_Daemon *daemon, const char *mem_key, const char *mem_cert, const char *pass)
enum MHD_StatusCode MHD_daemon_digest_auth_nc_length(struct MHD_Daemon *daemon, size_t nc_length)
void MHD_daemon_threading_mode(struct MHD_Daemon *daemon, enum MHD_ThreadingMode tm)
enum MHD_Bool MHD_daemon_event_loop(struct MHD_Daemon *daemon, enum MHD_EventLoopSyscall els)
void MHD_daemon_connection_memory_limit(struct MHD_Daemon *daemon, size_t memory_limit_b, size_t memory_increment_b)
enum MHD_Bool MHD_daemon_tcp_fastopen(struct MHD_Daemon *daemon, enum MHD_FastOpenMethod fom, unsigned int queue_length)
void MHD_daemon_set_notify_connection(struct MHD_Daemon *daemon, MHD_NotifyConnectionCallback ncc, void *ncc_cls)
void MHD_daemon_listen_allow_address_reuse(struct MHD_Daemon *daemon)
void MHD_daemon_unescape_cb(struct MHD_Daemon *daemon, MHD_UnescapeCallback unescape_cb, void *unescape_cb_cls)
void MHD_daemon_accept_policy(struct MHD_Daemon *daemon, MHD_AcceptPolicyCallback apc, void *apc_cls)
enum MHD_StatusCode MHD_daemon_gnutls_credentials(struct MHD_Daemon *daemon, int gnutls_credentials)
void MHD_daemon_connection_default_timeout(struct MHD_Daemon *daemon, unsigned int timeout_s)
void MHD_daemon_protocol_strict_level(struct MHD_Daemon *daemon, enum MHD_ProtocolStrictLevel sl)
void MHD_daemon_set_logger(struct MHD_Daemon *daemon, MHD_LoggingCallback logger, void *logger_cls)
void(* MHD_NotifyConnectionCallback)(void *cls, struct MHD_Connection *connection, void **socket_context, enum MHD_ConnectionNotificationCode toe)
Definition: microhttpd.h:2239
#define MHD_PANIC(msg)
Definition: internal.h:69
#define MHD_strerror_(errnum)
Definition: mhd_compat.h:44
#define NULL
Definition: reason_phrase.c:30
#define _(String)
Definition: mhd_options.h:42
#define _MHD_EXTERN
Definition: mhd_options.h:50
internal shared structures
int MHD_socket
Definition: microhttpd.h:196
@ MHD_YES
Definition: microhttpd.h:151
@ MHD_NO
Definition: microhttpd.h:146
enum MHD_Result(* MHD_AcceptPolicyCallback)(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Definition: microhttpd.h:2145
#define MHD_TLS_ABI_VERSION_STR
struct MHD_TLS_Plugin *(* MHD_TLS_PluginInit)(const char *ciphers)
MHD_EarlyUriLogCallback early_uri_logger_cb
Definition: internal.h:1036
unsigned int ip_connection_limit
Definition: internal.h:1356
enum MHD_AddressFamily listen_af
Definition: internal.h:1429
int listen_backlog
Definition: internal.h:1336
MHD_AcceptPolicyCallback accept_policy_cb
Definition: internal.h:1026
uint16_t listen_port
Definition: internal.h:1449
MHD_NotifyConnectionCallback notify_connection_cb
Definition: internal.h:1047
void * notify_connection_cb_cls
Definition: internal.h:1052
size_t listen_sa_len
Definition: internal.h:1271
void * early_uri_logger_cb_cls
Definition: internal.h:1041
bool disable_itc
Definition: internal.h:1462
void * unescape_cb_cls
Definition: internal.h:1063
enum MHD_FastOpenMethod fast_open_method
Definition: internal.h:1423
bool suppress_date
Definition: internal.h:1456
enum MHD_EventLoopSyscall event_loop_syscall
Definition: internal.h:1436
bool enable_shoutcast
Definition: internal.h:1521
bool disallow_upgrade
Definition: internal.h:1474
size_t connection_memory_increment_b
Definition: internal.h:1296
MHD_LoggingCallback logger
Definition: internal.h:1015
bool disallow_suspend_resume
Definition: internal.h:1468
bool enable_turbo
Definition: internal.h:1490
struct sockaddr_storage listen_sa
Definition: internal.h:1254
bool allow_address_reuse
Definition: internal.h:1516
enum MHD_ProtocolStrictLevel protocol_strict_level
Definition: internal.h:1442
time_t connection_default_timeout
Definition: internal.h:1371
MHD_socket listen_socket
Definition: internal.h:1377
size_t connection_memory_limit_b
Definition: internal.h:1281
enum MHD_ThreadingMode threading_mode
Definition: internal.h:1417
void * logger_cls
Definition: internal.h:1020
unsigned int global_connection_limit
Definition: internal.h:1351
MHD_UnescapeCallback unescape_cb
Definition: internal.h:1058
void * accept_policy_cb_cls
Definition: internal.h:1031
size_t thread_stack_limit_b
Definition: internal.h:1302
unsigned int fo_queue_length
Definition: internal.h:1346
enum MHD_StatusCode(* init_mem_trust)(void *cls, const char *mem_trust)
enum MHD_StatusCode(* init_kcp)(void *cls, const char *mem_key, const char *mem_cert, const char *pass)
enum MHD_StatusCode(* init_dhparams)(void *cls, const char *dh)