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