diff options
Diffstat (limited to 'src/gnutls/init.c')
-rw-r--r-- | src/gnutls/init.c | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/src/gnutls/init.c b/src/gnutls/init.c new file mode 100644 index 00000000..8ca5cd28 --- /dev/null +++ b/src/gnutls/init.c | |||
@@ -0,0 +1,217 @@ | |||
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 lib/init.c | ||
22 | * @brief initialization routines | ||
23 | * @author Christian Grothoff | ||
24 | * | ||
25 | * TODO: most of this is only required for gcrypt/GNUtls, | ||
26 | * should probably be moved to TLS plugin! | ||
27 | */ | ||
28 | #include "internal.h" | ||
29 | #include "init.h" | ||
30 | |||
31 | |||
32 | #ifdef MHD_HTTPS_REQUIRE_GRYPT | ||
33 | #if defined(HTTPS_SUPPORT) && GCRYPT_VERSION_NUMBER < 0x010600 | ||
34 | #if defined(MHD_USE_POSIX_THREADS) | ||
35 | GCRY_THREAD_OPTION_PTHREAD_IMPL; | ||
36 | #elif defined(MHD_W32_MUTEX_) | ||
37 | |||
38 | static int | ||
39 | gcry_w32_mutex_init (void **ppmtx) | ||
40 | { | ||
41 | *ppmtx = malloc (sizeof (MHD_mutex_)); | ||
42 | |||
43 | if (NULL == *ppmtx) | ||
44 | return ENOMEM; | ||
45 | if (!MHD_mutex_init_ ((MHD_mutex_*)*ppmtx)) | ||
46 | { | ||
47 | free (*ppmtx); | ||
48 | *ppmtx = NULL; | ||
49 | return EPERM; | ||
50 | } | ||
51 | |||
52 | return 0; | ||
53 | } | ||
54 | |||
55 | |||
56 | static int | ||
57 | gcry_w32_mutex_destroy (void **ppmtx) | ||
58 | { | ||
59 | int res = (MHD_mutex_destroy_ ((MHD_mutex_*)*ppmtx)) ? 0 : EINVAL; | ||
60 | free (*ppmtx); | ||
61 | return res; | ||
62 | } | ||
63 | |||
64 | |||
65 | static int | ||
66 | gcry_w32_mutex_lock (void **ppmtx) | ||
67 | { | ||
68 | return MHD_mutex_lock_ ((MHD_mutex_*)*ppmtx) ? 0 : EINVAL; | ||
69 | } | ||
70 | |||
71 | |||
72 | static int | ||
73 | gcry_w32_mutex_unlock (void **ppmtx) | ||
74 | { | ||
75 | return MHD_mutex_unlock_ ((MHD_mutex_*)*ppmtx) ? 0 : EINVAL; | ||
76 | } | ||
77 | |||
78 | |||
79 | static struct gcry_thread_cbs gcry_threads_w32 = { | ||
80 | (GCRY_THREAD_OPTION_USER | (GCRY_THREAD_OPTION_VERSION << 8)), | ||
81 | NULL, gcry_w32_mutex_init, gcry_w32_mutex_destroy, | ||
82 | gcry_w32_mutex_lock, gcry_w32_mutex_unlock, | ||
83 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; | ||
84 | |||
85 | #endif /* defined(MHD_W32_MUTEX_) */ | ||
86 | #endif /* HTTPS_SUPPORT && GCRYPT_VERSION_NUMBER < 0x010600 */ | ||
87 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ | ||
88 | |||
89 | |||
90 | #ifndef _AUTOINIT_FUNCS_ARE_SUPPORTED | ||
91 | |||
92 | /** | ||
93 | * Track global initialisation | ||
94 | */ | ||
95 | volatile int global_init_count = 0; | ||
96 | #ifdef MHD_MUTEX_STATIC_DEFN_INIT_ | ||
97 | /** | ||
98 | * Global initialisation mutex | ||
99 | */ | ||
100 | MHD_MUTEX_STATIC_DEFN_INIT_(global_init_mutex_); | ||
101 | #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */ | ||
102 | |||
103 | #endif | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Check whether global initialisation was performed | ||
108 | * and call initialiser if necessary. | ||
109 | */ | ||
110 | void | ||
111 | MHD_check_global_init_ (void) | ||
112 | { | ||
113 | #ifdef MHD_MUTEX_STATIC_DEFN_INIT_ | ||
114 | MHD_mutex_lock_chk_(&global_init_mutex_); | ||
115 | #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */ | ||
116 | if (0 == global_init_count++) | ||
117 | MHD_init (); | ||
118 | #ifdef MHD_MUTEX_STATIC_DEFN_INIT_ | ||
119 | MHD_mutex_unlock_chk_(&global_init_mutex_); | ||
120 | #endif /* MHD_MUTEX_STATIC_DEFN_INIT_ */ | ||
121 | } | ||
122 | |||
123 | |||
124 | /** | ||
125 | * Default implementation of the panic function, | ||
126 | * prints an error message and aborts. | ||
127 | * | ||
128 | * @param cls unused | ||
129 | * @param file name of the file with the problem | ||
130 | * @param line line number with the problem | ||
131 | * @param reason error message with details | ||
132 | */ | ||
133 | static void | ||
134 | mhd_panic_std (void *cls, | ||
135 | const char *file, | ||
136 | unsigned int line, | ||
137 | const char *reason) | ||
138 | { | ||
139 | (void)cls; /* Mute compiler warning. */ | ||
140 | #ifdef HAVE_MESSAGES | ||
141 | fprintf (stderr, | ||
142 | _("Fatal error in GNU libmicrohttpd %s:%u: %s\n"), | ||
143 | file, | ||
144 | line, | ||
145 | reason); | ||
146 | #else /* ! HAVE_MESSAGES */ | ||
147 | (void)file; /* Mute compiler warning. */ | ||
148 | (void)line; /* Mute compiler warning. */ | ||
149 | (void)reason; /* Mute compiler warning. */ | ||
150 | #endif | ||
151 | abort (); | ||
152 | } | ||
153 | |||
154 | |||
155 | /** | ||
156 | * Initialize do setup work. | ||
157 | */ | ||
158 | void | ||
159 | MHD_init(void) | ||
160 | { | ||
161 | #if defined(_WIN32) && ! defined(__CYGWIN__) | ||
162 | WSADATA wsd; | ||
163 | #endif /* _WIN32 && ! __CYGWIN__ */ | ||
164 | |||
165 | if (NULL == mhd_panic) | ||
166 | mhd_panic = &mhd_panic_std; | ||
167 | |||
168 | #if defined(_WIN32) && ! defined(__CYGWIN__) | ||
169 | if (0 != WSAStartup(MAKEWORD(2, 2), &wsd)) | ||
170 | MHD_PANIC (_("Failed to initialize winsock\n")); | ||
171 | mhd_winsock_inited_ = 1; | ||
172 | if (2 != LOBYTE(wsd.wVersion) && 2 != HIBYTE(wsd.wVersion)) | ||
173 | MHD_PANIC (_("Winsock version 2.2 is not available\n")); | ||
174 | #endif | ||
175 | #ifdef HTTPS_SUPPORT | ||
176 | #ifdef MHD_HTTPS_REQUIRE_GRYPT | ||
177 | #if GCRYPT_VERSION_NUMBER < 0x010600 | ||
178 | #if defined(MHD_USE_POSIX_THREADS) | ||
179 | if (0 != gcry_control (GCRYCTL_SET_THREAD_CBS, | ||
180 | &gcry_threads_pthread)) | ||
181 | MHD_PANIC (_("Failed to initialise multithreading in libgcrypt\n")); | ||
182 | #elif defined(MHD_W32_MUTEX_) | ||
183 | if (0 != gcry_control (GCRYCTL_SET_THREAD_CBS, | ||
184 | &gcry_threads_w32)) | ||
185 | MHD_PANIC (_("Failed to initialise multithreading in libgcrypt\n")); | ||
186 | #endif /* defined(MHD_W32_MUTEX_) */ | ||
187 | gcry_check_version (NULL); | ||
188 | #else | ||
189 | if (NULL == gcry_check_version ("1.6.0")) | ||
190 | MHD_PANIC (_("libgcrypt is too old. MHD was compiled for libgcrypt 1.6.0 or newer\n")); | ||
191 | #endif | ||
192 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ | ||
193 | gnutls_global_init (); | ||
194 | #endif /* HTTPS_SUPPORT */ | ||
195 | MHD_monotonic_sec_counter_init(); | ||
196 | #ifdef HAVE_FREEBSD_SENDFILE | ||
197 | MHD_conn_init_static_ (); | ||
198 | #endif /* HAVE_FREEBSD_SENDFILE */ | ||
199 | } | ||
200 | |||
201 | |||
202 | void | ||
203 | MHD_fini(void) | ||
204 | { | ||
205 | #ifdef HTTPS_SUPPORT | ||
206 | gnutls_global_deinit (); | ||
207 | #endif /* HTTPS_SUPPORT */ | ||
208 | #if defined(_WIN32) && ! defined(__CYGWIN__) | ||
209 | if (mhd_winsock_inited_) | ||
210 | WSACleanup(); | ||
211 | #endif | ||
212 | MHD_monotonic_sec_counter_finish(); | ||
213 | } | ||
214 | |||
215 | #ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED | ||
216 | _SET_INIT_AND_DEINIT_FUNCS(MHD_init, MHD_fini); | ||
217 | #endif /* _AUTOINIT_FUNCS_ARE_SUPPORTED */ | ||