1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
#include "pycurl.h"
#ifdef WITH_THREAD
PYCURL_INTERNAL PyThreadState *
pycurl_get_thread_state(const CurlObject *self)
{
/* Get the thread state for callbacks to run in.
* This is either `self->state' when running inside perform() or
* `self->multi_stack->state' when running inside multi_perform().
* When the result is != NULL we also implicitly assert
* a valid `self->handle'.
*/
if (self == NULL)
return NULL;
assert(PyObject_IsInstance((PyObject *) self, (PyObject *) p_Curl_Type) == 1);
if (self->state != NULL)
{
/* inside perform() */
assert(self->handle != NULL);
if (self->multi_stack != NULL) {
assert(self->multi_stack->state == NULL);
}
return self->state;
}
if (self->multi_stack != NULL && self->multi_stack->state != NULL)
{
/* inside multi_perform() */
assert(self->handle != NULL);
assert(self->multi_stack->multi_handle != NULL);
assert(self->state == NULL);
return self->multi_stack->state;
}
return NULL;
}
PYCURL_INTERNAL PyThreadState *
pycurl_get_thread_state_multi(const CurlMultiObject *self)
{
/* Get the thread state for callbacks to run in when given
* multi handles instead of regular handles
*/
if (self == NULL)
return NULL;
assert(PyObject_IsInstance((PyObject *) self, (PyObject *) p_CurlMulti_Type) == 1);
if (self->state != NULL)
{
/* inside multi_perform() */
assert(self->multi_handle != NULL);
return self->state;
}
return NULL;
}
PYCURL_INTERNAL int
pycurl_acquire_thread(const CurlObject *self, PyThreadState **state)
{
*state = pycurl_get_thread_state(self);
if (*state == NULL)
return 0;
PyEval_AcquireThread(*state);
return 1;
}
PYCURL_INTERNAL int
pycurl_acquire_thread_multi(const CurlMultiObject *self, PyThreadState **state)
{
*state = pycurl_get_thread_state_multi(self);
if (*state == NULL)
return 0;
PyEval_AcquireThread(*state);
return 1;
}
PYCURL_INTERNAL void
pycurl_release_thread(PyThreadState *state)
{
PyEval_ReleaseThread(state);
}
/*************************************************************************
// SSL TSL
**************************************************************************/
#ifdef PYCURL_NEED_OPENSSL_TSL
#if OPENSSL_VERSION_NUMBER < 0x10100000
static PyThread_type_lock *pycurl_openssl_tsl = NULL;
static void
pycurl_ssl_lock(int mode, int n, const char * file, int line)
{
if (mode & CRYPTO_LOCK) {
PyThread_acquire_lock(pycurl_openssl_tsl[n], 1);
} else {
PyThread_release_lock(pycurl_openssl_tsl[n]);
}
}
#if OPENSSL_VERSION_NUMBER >= 0x10000000
/* use new CRYPTO_THREADID API. */
static void
pycurl_ssl_threadid_callback(CRYPTO_THREADID *id)
{
CRYPTO_THREADID_set_numeric(id, (unsigned long)PyThread_get_thread_ident());
}
#else
/* deprecated CRYPTO_set_id_callback() API. */
static unsigned long
pycurl_ssl_id(void)
{
return (unsigned long) PyThread_get_thread_ident();
}
#endif
#endif
PYCURL_INTERNAL int
pycurl_ssl_init(void)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000
int i, c = CRYPTO_num_locks();
pycurl_openssl_tsl = PyMem_New(PyThread_type_lock, c);
if (pycurl_openssl_tsl == NULL) {
PyErr_NoMemory();
return -1;
}
memset(pycurl_openssl_tsl, 0, sizeof(PyThread_type_lock) * c);
for (i = 0; i < c; ++i) {
pycurl_openssl_tsl[i] = PyThread_allocate_lock();
if (pycurl_openssl_tsl[i] == NULL) {
for (--i; i >= 0; --i) {
PyThread_free_lock(pycurl_openssl_tsl[i]);
}
PyMem_Free(pycurl_openssl_tsl);
PyErr_NoMemory();
return -1;
}
}
#if OPENSSL_VERSION_NUMBER >= 0x10000000
CRYPTO_THREADID_set_callback(pycurl_ssl_threadid_callback);
#else
CRYPTO_set_id_callback(pycurl_ssl_id);
#endif
CRYPTO_set_locking_callback(pycurl_ssl_lock);
#endif
return 0;
}
PYCURL_INTERNAL void
pycurl_ssl_cleanup(void)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000
if (pycurl_openssl_tsl) {
int i, c = CRYPTO_num_locks();
#if OPENSSL_VERSION_NUMBER >= 0x10000000
CRYPTO_THREADID_set_callback(NULL);
#else
CRYPTO_set_id_callback(NULL);
#endif
CRYPTO_set_locking_callback(NULL);
for (i = 0; i < c; ++i) {
PyThread_free_lock(pycurl_openssl_tsl[i]);
}
PyMem_Free(pycurl_openssl_tsl);
pycurl_openssl_tsl = NULL;
}
#endif
}
#endif
#ifdef PYCURL_NEED_GNUTLS_TSL
static int
pycurl_ssl_mutex_create(void **m)
{
if ((*((PyThread_type_lock *) m) = PyThread_allocate_lock()) == NULL) {
return -1;
} else {
return 0;
}
}
static int
pycurl_ssl_mutex_destroy(void **m)
{
PyThread_free_lock(*((PyThread_type_lock *) m));
return 0;
}
static int
pycurl_ssl_mutex_lock(void **m)
{
return !PyThread_acquire_lock(*((PyThread_type_lock *) m), 1);
}
static int
pycurl_ssl_mutex_unlock(void **m)
{
PyThread_release_lock(*((PyThread_type_lock *) m));
return 0;
}
static struct gcry_thread_cbs pycurl_gnutls_tsl = {
GCRY_THREAD_OPTION_USER,
NULL,
pycurl_ssl_mutex_create,
pycurl_ssl_mutex_destroy,
pycurl_ssl_mutex_lock,
pycurl_ssl_mutex_unlock
};
PYCURL_INTERNAL int
pycurl_ssl_init(void)
{
gcry_control(GCRYCTL_SET_THREAD_CBS, &pycurl_gnutls_tsl);
return 0;
}
PYCURL_INTERNAL void
pycurl_ssl_cleanup(void)
{
return;
}
#endif
/* mbedTLS */
#ifdef PYCURL_NEED_MBEDTLS_TSL
static int
pycurl_ssl_mutex_create(void **m)
{
if ((*((PyThread_type_lock *) m) = PyThread_allocate_lock()) == NULL) {
return -1;
} else {
return 0;
}
}
static int
pycurl_ssl_mutex_destroy(void **m)
{
PyThread_free_lock(*((PyThread_type_lock *) m));
return 0;
}
static int
pycurl_ssl_mutex_lock(void **m)
{
return !PyThread_acquire_lock(*((PyThread_type_lock *) m), 1);
}
PYCURL_INTERNAL int
pycurl_ssl_init(void)
{
return 0;
}
PYCURL_INTERNAL void
pycurl_ssl_cleanup(void)
{
return;
}
#endif
/*************************************************************************
// CurlShareObject
**************************************************************************/
PYCURL_INTERNAL void
share_lock_lock(ShareLock *lock, curl_lock_data data)
{
PyThread_acquire_lock(lock->locks[data], 1);
}
PYCURL_INTERNAL void
share_lock_unlock(ShareLock *lock, curl_lock_data data)
{
PyThread_release_lock(lock->locks[data]);
}
PYCURL_INTERNAL ShareLock *
share_lock_new(void)
{
int i;
ShareLock *lock = PyMem_New(ShareLock, 1);
if (lock == NULL) {
PyErr_NoMemory();
return NULL;
}
for (i = 0; i < CURL_LOCK_DATA_LAST; ++i) {
lock->locks[i] = PyThread_allocate_lock();
if (lock->locks[i] == NULL) {
PyErr_NoMemory();
goto error;
}
}
return lock;
error:
for (--i; i >= 0; --i) {
PyThread_free_lock(lock->locks[i]);
lock->locks[i] = NULL;
}
PyMem_Free(lock);
return NULL;
}
PYCURL_INTERNAL void
share_lock_destroy(ShareLock *lock)
{
int i;
assert(lock);
for (i = 0; i < CURL_LOCK_DATA_LAST; ++i){
assert(lock->locks[i] != NULL);
PyThread_free_lock(lock->locks[i]);
}
PyMem_Free(lock);
lock = NULL;
}
PYCURL_INTERNAL void
share_lock_callback(CURL *handle, curl_lock_data data, curl_lock_access locktype, void *userptr)
{
CurlShareObject *share = (CurlShareObject*)userptr;
share_lock_lock(share->lock, data);
}
PYCURL_INTERNAL void
share_unlock_callback(CURL *handle, curl_lock_data data, void *userptr)
{
CurlShareObject *share = (CurlShareObject*)userptr;
share_lock_unlock(share->lock, data);
}
#else /* WITH_THREAD */
#if defined(PYCURL_NEED_SSL_TSL)
PYCURL_INTERNAL void
pycurl_ssl_init(void)
{
return 0;
}
PYCURL_INTERNAL void
pycurl_ssl_cleanup(void)
{
return;
}
#endif
#endif /* WITH_THREAD */
/* vi:ts=4:et:nowrap
*/
|