init.c (4102B)
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 20 /** 21 * @file gnutls/init.c 22 * @brief gnutls-specific initialization routines 23 * @author Christian Grothoff 24 */ 25 #include "internal.h" 26 #include "init.h" 27 28 29 #ifdef MHD_HTTPS_REQUIRE_GCRYPT 30 #if defined(HTTPS_SUPPORT) && GCRYPT_VERSION_NUMBER < 0x010600 31 #if defined(MHD_USE_POSIX_THREADS) 32 GCRY_THREAD_OPTION_PTHREAD_IMPL; 33 #elif defined(MHD_W32_MUTEX_) 34 35 36 static int 37 gcry_w32_mutex_init (void **ppmtx) 38 { 39 *ppmtx = malloc (sizeof (MHD_mutex_)); 40 41 if (NULL == *ppmtx) 42 return ENOMEM; 43 if (! MHD_mutex_init_ ((MHD_mutex_ *) *ppmtx)) 44 { 45 free (*ppmtx); 46 *ppmtx = NULL; 47 return EPERM; 48 } 49 return 0; 50 } 51 52 53 static int 54 gcry_w32_mutex_destroy (void **ppmtx) 55 { 56 int res = (MHD_mutex_destroy_ ((MHD_mutex_ *) *ppmtx)) ? 0 : EINVAL; 57 free (*ppmtx); 58 return res; 59 } 60 61 62 static int 63 gcry_w32_mutex_lock (void **ppmtx) 64 { 65 return MHD_mutex_lock_ ((MHD_mutex_ *) *ppmtx) ? 0 : EINVAL; 66 } 67 68 69 static int 70 gcry_w32_mutex_unlock (void **ppmtx) 71 { 72 return MHD_mutex_unlock_ ((MHD_mutex_ *) *ppmtx) ? 0 : EINVAL; 73 } 74 75 76 static struct gcry_thread_cbs gcry_threads_w32 = { 77 (GCRY_THREAD_OPTION_USER | (GCRY_THREAD_OPTION_VERSION << 8)), 78 NULL, gcry_w32_mutex_init, gcry_w32_mutex_destroy, 79 gcry_w32_mutex_lock, gcry_w32_mutex_unlock, 80 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 81 }; 82 83 #endif /* defined(MHD_W32_MUTEX_) */ 84 #endif /* HTTPS_SUPPORT && GCRYPT_VERSION_NUMBER < 0x010600 */ 85 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */ 86 87 88 #ifndef _AUTOINIT_FUNCS_ARE_SUPPORTED 89 90 /** 91 * Track global initialisation 92 */ 93 volatile int global_init_count = 0; 94 #ifdef MHD_MUTEX_STATIC_DEFN_INIT_ 95 /** 96 * Global initialisation mutex 97 */ 98 MHD_MUTEX_STATIC_DEFN_INIT_ (global_init_mutex_); 99 #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */ 100 101 #endif 102 103 104 /** 105 * Check whether global initialisation was performed 106 * and call initialiser if necessary. 107 */ 108 void 109 MHD_TLS_check_global_init_ (void) 110 { 111 #ifdef MHD_MUTEX_STATIC_DEFN_INIT_ 112 MHD_mutex_lock_chk_ (&global_init_mutex_); 113 #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */ 114 if (0 == global_init_count++) 115 MHD_init (); 116 #ifdef MHD_MUTEX_STATIC_DEFN_INIT_ 117 MHD_mutex_unlock_chk_ (&global_init_mutex_); 118 #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */ 119 } 120 121 122 /** 123 * Initialize do setup work. 124 */ 125 void 126 MHD_TLS_init (void) 127 { 128 #if defined(_WIN32) && ! defined(__CYGWIN__) 129 WSADATA wsd; 130 #endif /* _WIN32 && ! __CYGWIN__ */ 131 132 #ifdef MHD_HTTPS_REQUIRE_GCRYPT 133 #if GCRYPT_VERSION_NUMBER < 0x010600 134 #if defined(MHD_USE_POSIX_THREADS) 135 if (0 != gcry_control (GCRYCTL_SET_THREAD_CBS, 136 &gcry_threads_pthread)) 137 MHD_PANIC (_ ("Failed to initialise multithreading in libgcrypt.\n")); 138 #elif defined(MHD_W32_MUTEX_) 139 if (0 != gcry_control (GCRYCTL_SET_THREAD_CBS, 140 &gcry_threads_w32)) 141 MHD_PANIC (_ ("Failed to initialise multithreading in libgcrypt.\n")); 142 #endif /* defined(MHD_W32_MUTEX_) */ 143 gcry_check_version (NULL); 144 #else 145 if (NULL == gcry_check_version ("1.6.0")) 146 MHD_PANIC (_ ( 147 "libgcrypt is too old. MHD was compiled for libgcrypt 1.6.0 or newer.\n")); 148 #endif 149 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */ 150 gnutls_global_init (); 151 } 152 153 154 void 155 MHD_TLS_fini (void) 156 { 157 gnutls_global_deinit (); 158 } 159 160 161 #ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED 162 _SET_INIT_AND_DEINIT_FUNCS (MHD_TLS_init, MHD_TLS_fini); 163 #endif /* _AUTOINIT_FUNCS_ARE_SUPPORTED */