diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gnutls/init.c | 217 | ||||
-rw-r--r-- | src/gnutls/init.h | 45 | ||||
-rw-r--r-- | src/lib/init.c | 107 | ||||
-rw-r--r-- | src/lib/init.h | 11 |
4 files changed, 277 insertions, 103 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 */ | ||
diff --git a/src/gnutls/init.h b/src/gnutls/init.h new file mode 100644 index 00000000..fae96159 --- /dev/null +++ b/src/gnutls/init.h | |||
@@ -0,0 +1,45 @@ | |||
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 | * @file lib/init.h | ||
21 | * @brief functions to initialize library | ||
22 | * @author Christian Grothoff | ||
23 | */ | ||
24 | #ifndef INIT_H | ||
25 | #define INIT_H | ||
26 | |||
27 | #include "internal.h" | ||
28 | |||
29 | #ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED | ||
30 | /** | ||
31 | * Do nothing - global initialisation is | ||
32 | * performed by library constructor. | ||
33 | */ | ||
34 | #define MHD_check_global_init_() (void)0 | ||
35 | #else /* ! _AUTOINIT_FUNCS_ARE_SUPPORTED */ | ||
36 | /** | ||
37 | * Check whether global initialisation was performed | ||
38 | * and call initialiser if necessary. | ||
39 | */ | ||
40 | void | ||
41 | MHD_check_global_init_ (void); | ||
42 | #endif /* ! _AUTOINIT_FUNCS_ARE_SUPPORTED */ | ||
43 | |||
44 | |||
45 | #endif /* INIT_H */ | ||
diff --git a/src/lib/init.c b/src/lib/init.c index 8ca5cd28..d3965763 100644 --- a/src/lib/init.c +++ b/src/lib/init.c | |||
@@ -21,78 +21,17 @@ | |||
21 | * @file lib/init.c | 21 | * @file lib/init.c |
22 | * @brief initialization routines | 22 | * @brief initialization routines |
23 | * @author Christian Grothoff | 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 | */ | 24 | */ |
28 | #include "internal.h" | 25 | #include "internal.h" |
29 | #include "init.h" | 26 | #include "init.h" |
30 | 27 | ||
31 | 28 | ||
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 | 29 | #ifndef _AUTOINIT_FUNCS_ARE_SUPPORTED |
91 | |||
92 | /** | 30 | /** |
93 | * Track global initialisation | 31 | * Track global initialisation |
94 | */ | 32 | */ |
95 | volatile int global_init_count = 0; | 33 | volatile unsigned int global_init_count = 0; |
34 | |||
96 | #ifdef MHD_MUTEX_STATIC_DEFN_INIT_ | 35 | #ifdef MHD_MUTEX_STATIC_DEFN_INIT_ |
97 | /** | 36 | /** |
98 | * Global initialisation mutex | 37 | * Global initialisation mutex |
@@ -153,10 +92,10 @@ mhd_panic_std (void *cls, | |||
153 | 92 | ||
154 | 93 | ||
155 | /** | 94 | /** |
156 | * Initialize do setup work. | 95 | * Globally initialize library. |
157 | */ | 96 | */ |
158 | void | 97 | void |
159 | MHD_init(void) | 98 | MHD_init (void) |
160 | { | 99 | { |
161 | #if defined(_WIN32) && ! defined(__CYGWIN__) | 100 | #if defined(_WIN32) && ! defined(__CYGWIN__) |
162 | WSADATA wsd; | 101 | WSADATA wsd; |
@@ -166,50 +105,32 @@ MHD_init(void) | |||
166 | mhd_panic = &mhd_panic_std; | 105 | mhd_panic = &mhd_panic_std; |
167 | 106 | ||
168 | #if defined(_WIN32) && ! defined(__CYGWIN__) | 107 | #if defined(_WIN32) && ! defined(__CYGWIN__) |
169 | if (0 != WSAStartup(MAKEWORD(2, 2), &wsd)) | 108 | if (0 != WSAStartup (MAKEWORD(2, 2), |
109 | &wsd)) | ||
170 | MHD_PANIC (_("Failed to initialize winsock\n")); | 110 | MHD_PANIC (_("Failed to initialize winsock\n")); |
171 | mhd_winsock_inited_ = 1; | 111 | mhd_winsock_inited_ = 1; |
172 | if (2 != LOBYTE(wsd.wVersion) && 2 != HIBYTE(wsd.wVersion)) | 112 | if ( (2 != LOBYTE(wsd.wVersion)) && |
113 | (2 != HIBYTE(wsd.wVersion)) ) | ||
173 | MHD_PANIC (_("Winsock version 2.2 is not available\n")); | 114 | MHD_PANIC (_("Winsock version 2.2 is not available\n")); |
174 | #endif | 115 | #endif |
175 | #ifdef HTTPS_SUPPORT | 116 | MHD_monotonic_sec_counter_init (); |
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 | 117 | #ifdef HAVE_FREEBSD_SENDFILE |
197 | MHD_conn_init_static_ (); | 118 | MHD_conn_init_static_ (); |
198 | #endif /* HAVE_FREEBSD_SENDFILE */ | 119 | #endif /* HAVE_FREEBSD_SENDFILE */ |
199 | } | 120 | } |
200 | 121 | ||
201 | 122 | ||
123 | /** | ||
124 | * Global teardown work. | ||
125 | */ | ||
202 | void | 126 | void |
203 | MHD_fini(void) | 127 | MHD_fini (void) |
204 | { | 128 | { |
205 | #ifdef HTTPS_SUPPORT | ||
206 | gnutls_global_deinit (); | ||
207 | #endif /* HTTPS_SUPPORT */ | ||
208 | #if defined(_WIN32) && ! defined(__CYGWIN__) | 129 | #if defined(_WIN32) && ! defined(__CYGWIN__) |
209 | if (mhd_winsock_inited_) | 130 | if (mhd_winsock_inited_) |
210 | WSACleanup(); | 131 | WSACleanup(); |
211 | #endif | 132 | #endif |
212 | MHD_monotonic_sec_counter_finish(); | 133 | MHD_monotonic_sec_counter_finish (); |
213 | } | 134 | } |
214 | 135 | ||
215 | #ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED | 136 | #ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED |
diff --git a/src/lib/init.h b/src/lib/init.h index f64f9b51..fae96159 100644 --- a/src/lib/init.h +++ b/src/lib/init.h | |||
@@ -16,24 +16,15 @@ | |||
16 | License along with this library; if not, write to the Free Software | 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 | 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | */ | 18 | */ |
19 | |||
20 | /** | 19 | /** |
21 | * @file lib/init.h | 20 | * @file lib/init.h |
22 | * @brief functions to initialize library | 21 | * @brief functions to initialize library |
23 | * @author Christian Grothoff | 22 | * @author Christian Grothoff |
24 | */ | 23 | */ |
25 | #include "internal.h" | ||
26 | |||
27 | |||
28 | #ifndef INIT_H | 24 | #ifndef INIT_H |
29 | #define INIT_H | 25 | #define INIT_H |
30 | 26 | ||
31 | /** | 27 | #include "internal.h" |
32 | * Globally initialise library. | ||
33 | */ | ||
34 | void | ||
35 | MHD_init(void); | ||
36 | |||
37 | 28 | ||
38 | #ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED | 29 | #ifdef _AUTOINIT_FUNCS_ARE_SUPPORTED |
39 | /** | 30 | /** |