| 1 |
/* |
| 2 |
* stunnel TLS offloading and load-balancing proxy |
| 3 |
* Copyright (C) 1998-2026 Michal Trojnara <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the |
| 7 |
* Free Software Foundation; either version 2 of the License, or (at your |
| 8 |
* option) any later version. |
| 9 |
* |
| 10 |
* This program 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. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, see <http://www.gnu.org/licenses>. |
| 17 |
* |
| 18 |
* Linking stunnel statically or dynamically with other modules is making |
| 19 |
* a combined work based on stunnel. Thus, the terms and conditions of |
| 20 |
* the GNU General Public License cover the whole combination. |
| 21 |
* |
| 22 |
* In addition, as a special exception, the copyright holder of stunnel |
| 23 |
* gives you permission to combine stunnel with free software programs or |
| 24 |
* libraries that are released under the GNU LGPL and with code included |
| 25 |
* in the standard release of OpenSSL under the OpenSSL License (or |
| 26 |
* modified versions of such code, with unchanged license). You may copy |
| 27 |
* and distribute such a system following the terms of the GNU GPL for |
| 28 |
* stunnel and the licenses of the other code concerned. |
| 29 |
* |
| 30 |
* Note that people who make modified versions of stunnel are not obligated |
| 31 |
* to grant this special exception for their modified versions; it is their |
| 32 |
* choice whether to do so. The GNU General Public License gives permission |
| 33 |
* to release a modified version without this exception; this exception |
| 34 |
* also makes it possible to release a modified version which carries |
| 35 |
* forward this exception. |
| 36 |
*/ |
| 37 |
|
| 38 |
#ifdef USE_OS2 |
| 39 |
#define INCL_DOSPROCESS |
| 40 |
#include <os2.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#include "prototypes.h" |
| 44 |
|
| 45 |
/**************************************** prototypes */ |
| 46 |
|
| 47 |
NOEXPORT void thread_id_init(void); |
| 48 |
NOEXPORT void locking_init(void); |
| 49 |
#ifndef USE_FORK |
| 50 |
CLI *thread_head=NULL; |
| 51 |
NOEXPORT void thread_list_add(CLI *); |
| 52 |
#endif |
| 53 |
|
| 54 |
/**************************************** thread ID callbacks */ |
| 55 |
|
| 56 |
#ifdef USE_UCONTEXT |
| 57 |
|
| 58 |
unsigned long stunnel_process_id(void) { |
| 59 |
return (unsigned long)getpid(); |
| 60 |
} |
| 61 |
|
| 62 |
unsigned long stunnel_thread_id(void) { |
| 63 |
return ready_head ? ready_head->id : 0; |
| 64 |
} |
| 65 |
|
| 66 |
#endif /* USE_UCONTEXT */ |
| 67 |
|
| 68 |
#ifdef USE_FORK |
| 69 |
|
| 70 |
unsigned long stunnel_process_id(void) { |
| 71 |
return (unsigned long)getpid(); |
| 72 |
} |
| 73 |
|
| 74 |
unsigned long stunnel_thread_id(void) { |
| 75 |
return 0L; |
| 76 |
} |
| 77 |
|
| 78 |
#endif /* USE_FORK */ |
| 79 |
|
| 80 |
#ifdef USE_PTHREAD |
| 81 |
|
| 82 |
unsigned long stunnel_process_id(void) { |
| 83 |
return (unsigned long)getpid(); |
| 84 |
} |
| 85 |
|
| 86 |
unsigned long stunnel_thread_id(void) { |
| 87 |
#if defined(SYS_gettid) && defined(__linux__) |
| 88 |
return (unsigned long)syscall(SYS_gettid); |
| 89 |
#else |
| 90 |
return (unsigned long)pthread_self(); |
| 91 |
#endif |
| 92 |
} |
| 93 |
|
| 94 |
#endif /* USE_PTHREAD */ |
| 95 |
|
| 96 |
#ifdef USE_WIN32 |
| 97 |
|
| 98 |
unsigned long stunnel_process_id(void) { |
| 99 |
return GetCurrentProcessId() & 0x00ffffff; |
| 100 |
} |
| 101 |
|
| 102 |
unsigned long stunnel_thread_id(void) { |
| 103 |
return GetCurrentThreadId() & 0x00ffffff; |
| 104 |
} |
| 105 |
|
| 106 |
#endif /* USE_WIN32 */ |
| 107 |
|
| 108 |
#if OPENSSL_VERSION_NUMBER>=0x10000000L && OPENSSL_VERSION_NUMBER<0x10100004L |
| 109 |
NOEXPORT void threadid_func(CRYPTO_THREADID *tid) { |
| 110 |
CRYPTO_THREADID_set_numeric(tid, stunnel_thread_id()); |
| 111 |
} |
| 112 |
#endif |
| 113 |
|
| 114 |
NOEXPORT void thread_id_init(void) { |
| 115 |
#if OPENSSL_VERSION_NUMBER>=0x10000000L && OPENSSL_VERSION_NUMBER<0x10100000L |
| 116 |
CRYPTO_THREADID_set_callback(threadid_func); |
| 117 |
#endif |
| 118 |
#if OPENSSL_VERSION_NUMBER<0x10000000L |
| 119 |
CRYPTO_set_id_callback(stunnel_thread_id); |
| 120 |
#endif |
| 121 |
} |
| 122 |
|
| 123 |
/**************************************** locking */ |
| 124 |
|
| 125 |
/* we only need to initialize locking with OpenSSL older than 1.1.0 */ |
| 126 |
#if OPENSSL_VERSION_NUMBER<0x10100004L |
| 127 |
|
| 128 |
#ifdef USE_PTHREAD |
| 129 |
|
| 130 |
NOEXPORT void s_lock_init_debug(struct CRYPTO_dynlock_value *lock, |
| 131 |
const char *file, int line) { |
| 132 |
pthread_rwlock_init(&lock->rwlock, NULL); |
| 133 |
#ifdef DEBUG_LOCKS |
| 134 |
lock->init_file=file; |
| 135 |
lock->init_line=line; |
| 136 |
#else |
| 137 |
(void)file; /* squash the unused parameter warning */ |
| 138 |
(void)line; /* squash the unused parameter warning */ |
| 139 |
#endif |
| 140 |
} |
| 141 |
|
| 142 |
NOEXPORT void s_read_lock_debug(struct CRYPTO_dynlock_value *lock, |
| 143 |
const char *file, int line) { |
| 144 |
pthread_rwlock_rdlock(&lock->rwlock); |
| 145 |
#ifdef DEBUG_LOCKS |
| 146 |
lock->read_lock_file=file; |
| 147 |
lock->read_lock_line=line; |
| 148 |
#else |
| 149 |
(void)file; /* squash the unused parameter warning */ |
| 150 |
(void)line; /* squash the unused parameter warning */ |
| 151 |
#endif |
| 152 |
} |
| 153 |
|
| 154 |
NOEXPORT void s_write_lock_debug(struct CRYPTO_dynlock_value *lock, |
| 155 |
const char *file, int line) { |
| 156 |
pthread_rwlock_wrlock(&lock->rwlock); |
| 157 |
#ifdef DEBUG_LOCKS |
| 158 |
lock->write_lock_file=file; |
| 159 |
lock->write_lock_line=line; |
| 160 |
#else |
| 161 |
(void)file; /* squash the unused parameter warning */ |
| 162 |
(void)line; /* squash the unused parameter warning */ |
| 163 |
#endif |
| 164 |
} |
| 165 |
|
| 166 |
NOEXPORT void s_unlock_debug(struct CRYPTO_dynlock_value *lock, |
| 167 |
const char *file, int line) { |
| 168 |
pthread_rwlock_unlock(&lock->rwlock); |
| 169 |
#ifdef DEBUG_LOCKS |
| 170 |
lock->unlock_file=file; |
| 171 |
lock->unlock_line=line; |
| 172 |
#else |
| 173 |
(void)file; /* squash the unused parameter warning */ |
| 174 |
(void)line; /* squash the unused parameter warning */ |
| 175 |
#endif |
| 176 |
} |
| 177 |
|
| 178 |
NOEXPORT void s_lock_destroy_debug(struct CRYPTO_dynlock_value *lock, |
| 179 |
const char *file, int line) { |
| 180 |
pthread_rwlock_destroy(&lock->rwlock); |
| 181 |
#ifdef DEBUG_LOCKS |
| 182 |
lock->destroy_file=file; |
| 183 |
lock->destroy_line=line; |
| 184 |
#else |
| 185 |
(void)file; /* squash the unused parameter warning */ |
| 186 |
(void)line; /* squash the unused parameter warning */ |
| 187 |
#endif |
| 188 |
str_free(lock); |
| 189 |
} |
| 190 |
|
| 191 |
#endif /* USE_PTHREAD */ |
| 192 |
|
| 193 |
#ifdef USE_WIN32 |
| 194 |
|
| 195 |
/* Slim Reader/Writer (SRW) Lock would be better than CRITICAL_SECTION, |
| 196 |
* but it is unsupported on Windows XP (and earlier versions of Windows): |
| 197 |
* https://msdn.microsoft.com/en-us/library/windows/desktop/aa904937%28v=vs.85%29.aspx */ |
| 198 |
|
| 199 |
NOEXPORT void s_lock_init_debug(struct CRYPTO_dynlock_value *lock, |
| 200 |
const char *file, int line) { |
| 201 |
InitializeCriticalSection(&lock->critical_section); |
| 202 |
#ifdef DEBUG_LOCKS |
| 203 |
lock->init_file=file; |
| 204 |
lock->init_line=line; |
| 205 |
#else |
| 206 |
(void)file; /* squash the unused parameter warning */ |
| 207 |
(void)line; /* squash the unused parameter warning */ |
| 208 |
#endif |
| 209 |
} |
| 210 |
|
| 211 |
NOEXPORT void s_read_lock_debug(struct CRYPTO_dynlock_value *lock, |
| 212 |
const char *file, int line) { |
| 213 |
EnterCriticalSection(&lock->critical_section); |
| 214 |
#ifdef DEBUG_LOCKS |
| 215 |
lock->read_lock_file=file; |
| 216 |
lock->read_lock_line=line; |
| 217 |
#else |
| 218 |
(void)file; /* squash the unused parameter warning */ |
| 219 |
(void)line; /* squash the unused parameter warning */ |
| 220 |
#endif |
| 221 |
} |
| 222 |
|
| 223 |
NOEXPORT void s_write_lock_debug(struct CRYPTO_dynlock_value *lock, |
| 224 |
const char *file, int line) { |
| 225 |
EnterCriticalSection(&lock->critical_section); |
| 226 |
#ifdef DEBUG_LOCKS |
| 227 |
lock->write_lock_file=file; |
| 228 |
lock->write_lock_line=line; |
| 229 |
#else |
| 230 |
(void)file; /* squash the unused parameter warning */ |
| 231 |
(void)line; /* squash the unused parameter warning */ |
| 232 |
#endif |
| 233 |
} |
| 234 |
|
| 235 |
NOEXPORT void s_unlock_debug(struct CRYPTO_dynlock_value *lock, |
| 236 |
const char *file, int line) { |
| 237 |
LeaveCriticalSection(&lock->critical_section); |
| 238 |
#ifdef DEBUG_LOCKS |
| 239 |
lock->unlock_file=file; |
| 240 |
lock->unlock_line=line; |
| 241 |
#else |
| 242 |
(void)file; /* squash the unused parameter warning */ |
| 243 |
(void)line; /* squash the unused parameter warning */ |
| 244 |
#endif |
| 245 |
} |
| 246 |
|
| 247 |
NOEXPORT void s_lock_destroy_debug(struct CRYPTO_dynlock_value *lock, |
| 248 |
const char *file, int line) { |
| 249 |
DeleteCriticalSection(&lock->critical_section); |
| 250 |
#ifdef DEBUG_LOCKS |
| 251 |
lock->destroy_file=file; |
| 252 |
lock->destroy_line=line; |
| 253 |
#else |
| 254 |
(void)file; /* squash the unused parameter warning */ |
| 255 |
(void)line; /* squash the unused parameter warning */ |
| 256 |
#endif |
| 257 |
str_free(lock); |
| 258 |
} |
| 259 |
|
| 260 |
#endif /* USE_WIN32 */ |
| 261 |
|
| 262 |
NOEXPORT int s_atomic_add(int *val, int amount, CRYPTO_RWLOCK *lock) { |
| 263 |
int ret; |
| 264 |
|
| 265 |
(void)lock; /* squash the unused parameter warning */ |
| 266 |
#if !defined(USE_OS_THREADS) |
| 267 |
/* no synchronization is needed */ |
| 268 |
return *val+=amount; |
| 269 |
#elif defined(__ATOMIC_ACQ_REL) |
| 270 |
if(__atomic_is_lock_free(sizeof *val, val)) |
| 271 |
return __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL); |
| 272 |
#elif defined(_MSC_VER) |
| 273 |
/* casting is safe, because sizeof(long)==sizeof(int) on Windows */ |
| 274 |
return InterlockedExchangeAdd((long *)val, amount)+amount; |
| 275 |
#endif |
| 276 |
CRYPTO_THREAD_write_lock(lock); |
| 277 |
ret=(*val+=amount); |
| 278 |
CRYPTO_THREAD_unlock(lock); |
| 279 |
return ret; |
| 280 |
} |
| 281 |
|
| 282 |
#endif /* OPENSSL_VERSION_NUMBER<0x10100004L */ |
| 283 |
|
| 284 |
CRYPTO_RWLOCK *stunnel_locks[STUNNEL_LOCKS]; |
| 285 |
|
| 286 |
#if OPENSSL_VERSION_NUMBER<0x10100004L |
| 287 |
|
| 288 |
#ifdef USE_OS_THREADS |
| 289 |
|
| 290 |
static struct CRYPTO_dynlock_value *lock_cs; |
| 291 |
|
| 292 |
NOEXPORT struct CRYPTO_dynlock_value *s_dynlock_create_cb(const char *file, |
| 293 |
int line) { |
| 294 |
struct CRYPTO_dynlock_value *lock; |
| 295 |
|
| 296 |
lock=str_alloc_detached(sizeof(struct CRYPTO_dynlock_value)); |
| 297 |
s_lock_init_debug(lock, file, line); |
| 298 |
return lock; |
| 299 |
} |
| 300 |
|
| 301 |
NOEXPORT void s_dynlock_lock_cb(int mode, struct CRYPTO_dynlock_value *lock, |
| 302 |
const char *file, int line) { |
| 303 |
if(mode&CRYPTO_LOCK) { |
| 304 |
/* either CRYPTO_READ or CRYPTO_WRITE (but not both) are needed */ |
| 305 |
if(!(mode&CRYPTO_READ)==!(mode&CRYPTO_WRITE)) |
| 306 |
fatal("Invalid locking mode"); |
| 307 |
if(mode&CRYPTO_WRITE) |
| 308 |
s_write_lock_debug(lock, file, line); |
| 309 |
else |
| 310 |
s_read_lock_debug(lock, file, line); |
| 311 |
} else |
| 312 |
s_unlock_debug(lock, file, line); |
| 313 |
} |
| 314 |
|
| 315 |
NOEXPORT void s_dynlock_destroy_cb(struct CRYPTO_dynlock_value *lock, |
| 316 |
const char *file, int line) { |
| 317 |
s_lock_destroy_debug(lock, file, line); |
| 318 |
} |
| 319 |
|
| 320 |
NOEXPORT void s_locking_cb(int mode, int type, const char *file, int line) { |
| 321 |
s_dynlock_lock_cb(mode, lock_cs+type, file, line); |
| 322 |
} |
| 323 |
|
| 324 |
NOEXPORT int s_add_lock_cb(int *num, int amount, int type, |
| 325 |
const char *file, int line) { |
| 326 |
(void)file; /* squash the unused parameter warning */ |
| 327 |
(void)line; /* squash the unused parameter warning */ |
| 328 |
return s_atomic_add(num, amount, lock_cs+type); |
| 329 |
} |
| 330 |
|
| 331 |
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) { |
| 332 |
struct CRYPTO_dynlock_value *lock; |
| 333 |
|
| 334 |
lock=str_alloc_detached(sizeof(CRYPTO_RWLOCK)); |
| 335 |
s_lock_init_debug(lock, __FILE__, __LINE__); |
| 336 |
return lock; |
| 337 |
} |
| 338 |
|
| 339 |
int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) { |
| 340 |
s_read_lock_debug(lock, __FILE__, __LINE__); |
| 341 |
return 1; |
| 342 |
} |
| 343 |
|
| 344 |
int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) { |
| 345 |
s_write_lock_debug(lock, __FILE__, __LINE__); |
| 346 |
return 1; |
| 347 |
} |
| 348 |
|
| 349 |
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) { |
| 350 |
s_unlock_debug(lock, __FILE__, __LINE__); |
| 351 |
return 1; |
| 352 |
} |
| 353 |
|
| 354 |
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) { |
| 355 |
s_lock_destroy_debug(lock, __FILE__, __LINE__); |
| 356 |
} |
| 357 |
|
| 358 |
#else /* USE_OS_THREADS */ |
| 359 |
|
| 360 |
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) { |
| 361 |
return NULL; |
| 362 |
} |
| 363 |
|
| 364 |
int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) { |
| 365 |
(void)lock; /* squash the unused parameter warning */ |
| 366 |
return 1; |
| 367 |
} |
| 368 |
|
| 369 |
int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) { |
| 370 |
(void)lock; /* squash the unused parameter warning */ |
| 371 |
return 1; |
| 372 |
} |
| 373 |
|
| 374 |
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) { |
| 375 |
(void)lock; /* squash the unused parameter warning */ |
| 376 |
return 1; |
| 377 |
} |
| 378 |
|
| 379 |
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) { |
| 380 |
(void)lock; /* squash the unused parameter warning */ |
| 381 |
} |
| 382 |
|
| 383 |
#endif /* USE_OS_THREADS */ |
| 384 |
|
| 385 |
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) { |
| 386 |
*ret=s_atomic_add(val, amount, lock); |
| 387 |
return 1; |
| 388 |
} |
| 389 |
|
| 390 |
#endif /* OPENSSL_VERSION_NUMBER<0x10100004L */ |
| 391 |
|
| 392 |
NOEXPORT void locking_init(void) { |
| 393 |
size_t i; |
| 394 |
#if defined(USE_OS_THREADS) && OPENSSL_VERSION_NUMBER<0x10100004L |
| 395 |
size_t num; |
| 396 |
|
| 397 |
/* initialize the OpenSSL static locking */ |
| 398 |
num=(size_t)CRYPTO_num_locks(); |
| 399 |
lock_cs=str_alloc_detached(num*sizeof(struct CRYPTO_dynlock_value)); |
| 400 |
for(i=0; i<num; i++) |
| 401 |
s_lock_init_debug(lock_cs+i, __FILE__, __LINE__); |
| 402 |
|
| 403 |
/* initialize the OpenSSL static locking callbacks */ |
| 404 |
CRYPTO_set_locking_callback(s_locking_cb); |
| 405 |
CRYPTO_set_add_lock_callback(s_add_lock_cb); |
| 406 |
|
| 407 |
/* initialize the OpenSSL dynamic locking callbacks */ |
| 408 |
CRYPTO_set_dynlock_create_callback(s_dynlock_create_cb); |
| 409 |
CRYPTO_set_dynlock_lock_callback(s_dynlock_lock_cb); |
| 410 |
CRYPTO_set_dynlock_destroy_callback(s_dynlock_destroy_cb); |
| 411 |
#endif /* defined(USE_OS_THREADS) && OPENSSL_VERSION_NUMBER<0x10100004L */ |
| 412 |
|
| 413 |
/* initialize stunnel critical sections */ |
| 414 |
for(i=0; i<STUNNEL_LOCKS; i++) /* all the mutexes */ |
| 415 |
stunnel_locks[i]=CRYPTO_THREAD_lock_new(); |
| 416 |
} |
| 417 |
|
| 418 |
/**************************************** creating a client */ |
| 419 |
|
| 420 |
#if defined(USE_UCONTEXT) || defined(USE_FORK) |
| 421 |
/* no need for critical sections */ |
| 422 |
|
| 423 |
#endif /* USE_UCONTEXT || USE_FORK */ |
| 424 |
|
| 425 |
#ifdef USE_UCONTEXT |
| 426 |
|
| 427 |
#if defined(CPU_SPARC) && ( \ |
| 428 |
defined(OS_SOLARIS2_0) || \ |
| 429 |
defined(OS_SOLARIS2_1) || \ |
| 430 |
defined(OS_SOLARIS2_2) || \ |
| 431 |
defined(OS_SOLARIS2_3) || \ |
| 432 |
defined(OS_SOLARIS2_4) || \ |
| 433 |
defined(OS_SOLARIS2_5) || \ |
| 434 |
defined(OS_SOLARIS2_6) || \ |
| 435 |
defined(OS_SOLARIS2_7) || \ |
| 436 |
defined(OS_SOLARIS2_8)) |
| 437 |
#define ARGC 2 |
| 438 |
#else |
| 439 |
#define ARGC 1 |
| 440 |
#endif |
| 441 |
|
| 442 |
/* first context on the ready list is the active context */ |
| 443 |
CONTEXT *ready_head=NULL, *ready_tail=NULL; /* ready to execute */ |
| 444 |
CONTEXT *waiting_head=NULL, *waiting_tail=NULL; /* waiting on poll() */ |
| 445 |
|
| 446 |
NOEXPORT CONTEXT *new_context(void) { |
| 447 |
static unsigned long next_id=1; |
| 448 |
CONTEXT *context; |
| 449 |
|
| 450 |
/* allocate and fill the CONTEXT structure */ |
| 451 |
context=str_alloc_detached(sizeof(CONTEXT)); |
| 452 |
context->id=next_id++; |
| 453 |
context->fds=NULL; |
| 454 |
context->ready=0; |
| 455 |
|
| 456 |
/* append to the tail of the ready queue */ |
| 457 |
context->next=NULL; |
| 458 |
if(ready_tail) |
| 459 |
ready_tail->next=context; |
| 460 |
ready_tail=context; |
| 461 |
if(!ready_head) |
| 462 |
ready_head=context; |
| 463 |
|
| 464 |
return context; |
| 465 |
} |
| 466 |
|
| 467 |
int sthreads_init(void) { |
| 468 |
thread_id_init(); |
| 469 |
locking_init(); |
| 470 |
/* create the first (listening) context and put it in the running queue */ |
| 471 |
if(!new_context()) { |
| 472 |
s_log(LOG_ERR, "Cannot create the listening context"); |
| 473 |
return 1; |
| 474 |
} |
| 475 |
/* update tls for newly allocated ready_head */ |
| 476 |
ui_tls=tls_alloc(NULL, ui_tls, "ui"); |
| 477 |
/* no need to initialize ucontext_t structure here |
| 478 |
it will be initialized with swapcontext() call */ |
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
int create_client(SOCKET ls, SOCKET s, CLI *arg) { |
| 483 |
CONTEXT *context; |
| 484 |
|
| 485 |
(void)ls; /* this parameter is only used with USE_FORK */ |
| 486 |
|
| 487 |
s_log(LOG_DEBUG, "Creating a new context"); |
| 488 |
context=new_context(); |
| 489 |
if(!context) { |
| 490 |
str_free(arg); |
| 491 |
if(s>=0) |
| 492 |
closesocket(s); |
| 493 |
return -1; |
| 494 |
} |
| 495 |
|
| 496 |
/* initialize context_t structure */ |
| 497 |
if(getcontext(&context->context)<0) { |
| 498 |
str_free(context); |
| 499 |
str_free(arg); |
| 500 |
if(s>=0) |
| 501 |
closesocket(s); |
| 502 |
ioerror("getcontext"); |
| 503 |
return -1; |
| 504 |
} |
| 505 |
context->context.uc_link=NULL; /* stunnel does not use uc_link */ |
| 506 |
|
| 507 |
/* create stack */ |
| 508 |
context->stack=str_alloc_detached(arg->opt->stack_size); |
| 509 |
#if defined(__sgi) || ARGC==2 /* obsolete ss_sp semantics */ |
| 510 |
context->context.uc_stack.ss_sp=context->stack+arg->opt->stack_size-8; |
| 511 |
#else |
| 512 |
context->context.uc_stack.ss_sp=context->stack; |
| 513 |
#endif |
| 514 |
context->context.uc_stack.ss_size=arg->opt->stack_size; |
| 515 |
context->context.uc_stack.ss_flags=0; |
| 516 |
|
| 517 |
makecontext(&context->context, (void(*)(void))client_thread, ARGC, arg); |
| 518 |
thread_list_add(arg); |
| 519 |
s_log(LOG_DEBUG, "New context created"); |
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
#endif /* USE_UCONTEXT */ |
| 524 |
|
| 525 |
#ifdef USE_FORK |
| 526 |
|
| 527 |
int sthreads_init(void) { |
| 528 |
thread_id_init(); |
| 529 |
locking_init(); |
| 530 |
return 0; |
| 531 |
} |
| 532 |
|
| 533 |
NOEXPORT void null_handler(int sig) { |
| 534 |
(void)sig; /* squash the unused parameter warning */ |
| 535 |
signal(SIGCHLD, null_handler); |
| 536 |
} |
| 537 |
|
| 538 |
int create_client(SOCKET ls, SOCKET s, CLI *arg) { |
| 539 |
switch(fork()) { |
| 540 |
case -1: /* error */ |
| 541 |
str_free(arg); |
| 542 |
if(s>=0) |
| 543 |
closesocket(s); |
| 544 |
return -1; |
| 545 |
case 0: /* child */ |
| 546 |
if(ls>=0) |
| 547 |
closesocket(ls); |
| 548 |
signal(SIGCHLD, null_handler); |
| 549 |
client_thread(arg); |
| 550 |
_exit(0); |
| 551 |
default: /* parent */ |
| 552 |
str_free(arg); |
| 553 |
if(s>=0) |
| 554 |
closesocket(s); |
| 555 |
} |
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
#endif /* USE_FORK */ |
| 560 |
|
| 561 |
#ifdef USE_PTHREAD |
| 562 |
|
| 563 |
NOEXPORT void *dummy_thread(void *arg) { |
| 564 |
pthread_exit(arg); |
| 565 |
return arg; |
| 566 |
} |
| 567 |
|
| 568 |
int sthreads_init(void) { |
| 569 |
pthread_t thread_id; |
| 570 |
|
| 571 |
/* this is a workaround for NPTL threads failing to invoke |
| 572 |
* pthread_exit() or pthread_cancel() from a chroot jail */ |
| 573 |
if(!pthread_create(&thread_id, NULL, dummy_thread, NULL)) |
| 574 |
pthread_join(thread_id, NULL); |
| 575 |
|
| 576 |
thread_id_init(); |
| 577 |
locking_init(); |
| 578 |
return 0; |
| 579 |
} |
| 580 |
|
| 581 |
int create_client(SOCKET ls, SOCKET s, CLI *arg) { |
| 582 |
pthread_attr_t pth_attr; |
| 583 |
int error; |
| 584 |
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(__APPLE__) |
| 585 |
/* disabled on OS X due to strange problems on Mac OS X 10.5 |
| 586 |
it seems to restore signal mask somewhere (I couldn't find where) |
| 587 |
effectively blocking signals after first accepted connection */ |
| 588 |
sigset_t new_set, old_set; |
| 589 |
#endif /* HAVE_PTHREAD_SIGMASK && !__APPLE__*/ |
| 590 |
|
| 591 |
(void)ls; /* this parameter is only used with USE_FORK */ |
| 592 |
|
| 593 |
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(__APPLE__) |
| 594 |
/* the idea is that only the main thread handles all the signals with |
| 595 |
* posix threads; signals are blocked for any other thread */ |
| 596 |
sigfillset(&new_set); |
| 597 |
pthread_sigmask(SIG_SETMASK, &new_set, &old_set); /* block signals */ |
| 598 |
#endif /* HAVE_PTHREAD_SIGMASK && !__APPLE__*/ |
| 599 |
pthread_attr_init(&pth_attr); |
| 600 |
pthread_attr_setstacksize(&pth_attr, arg->opt->stack_size); |
| 601 |
|
| 602 |
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_THREAD_LIST]); |
| 603 |
error=pthread_create(&arg->thread_id, &pth_attr, client_thread, arg); |
| 604 |
pthread_attr_destroy(&pth_attr); |
| 605 |
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(__APPLE__) |
| 606 |
pthread_sigmask(SIG_SETMASK, &old_set, NULL); /* unblock signals */ |
| 607 |
#endif /* HAVE_PTHREAD_SIGMASK && !__APPLE__*/ |
| 608 |
if(error) { |
| 609 |
errno=error; |
| 610 |
ioerror("pthread_create"); |
| 611 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_THREAD_LIST]); |
| 612 |
str_free(arg); |
| 613 |
if(s>=0) |
| 614 |
closesocket(s); |
| 615 |
return -1; |
| 616 |
} |
| 617 |
thread_list_add(arg); |
| 618 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_THREAD_LIST]); |
| 619 |
return 0; |
| 620 |
} |
| 621 |
|
| 622 |
#endif /* USE_PTHREAD */ |
| 623 |
|
| 624 |
#ifdef USE_WIN32 |
| 625 |
|
| 626 |
#if !defined(_MT) |
| 627 |
#error _beginthreadex requires a multithreaded C run-time library |
| 628 |
#endif |
| 629 |
|
| 630 |
int sthreads_init(void) { |
| 631 |
thread_id_init(); |
| 632 |
locking_init(); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
int create_client(SOCKET ls, SOCKET s, CLI *arg) { |
| 637 |
(void)ls; /* this parameter is only used with USE_FORK */ |
| 638 |
s_log(LOG_DEBUG, "Creating a new thread"); |
| 639 |
CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_THREAD_LIST]); |
| 640 |
arg->thread_id=(HANDLE)_beginthreadex(NULL, |
| 641 |
(unsigned)arg->opt->stack_size, client_thread, arg, |
| 642 |
STACK_SIZE_PARAM_IS_A_RESERVATION, NULL); |
| 643 |
if(!arg->thread_id) { |
| 644 |
ioerror("_beginthreadex"); |
| 645 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_THREAD_LIST]); |
| 646 |
str_free(arg); |
| 647 |
if(s!=INVALID_SOCKET) |
| 648 |
closesocket(s); |
| 649 |
return -1; |
| 650 |
} |
| 651 |
thread_list_add(arg); |
| 652 |
CRYPTO_THREAD_unlock(stunnel_locks[LOCK_THREAD_LIST]); |
| 653 |
s_log(LOG_DEBUG, "New thread created"); |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
#endif /* USE_WIN32 */ |
| 658 |
|
| 659 |
#ifdef USE_OS2 |
| 660 |
|
| 661 |
int sthreads_init(void) { |
| 662 |
return 0; |
| 663 |
} |
| 664 |
|
| 665 |
unsigned long stunnel_process_id(void) { |
| 666 |
PTIB ptib=NULL; |
| 667 |
DosGetInfoBlocks(&ptib, NULL); |
| 668 |
return (unsigned long)ptib->tib_ordinal; |
| 669 |
} |
| 670 |
|
| 671 |
unsigned long stunnel_thread_id(void) { |
| 672 |
PPIB ppib=NULL; |
| 673 |
DosGetInfoBlocks(NULL, &ppib); |
| 674 |
return (unsigned long)ppib->pib_ulpid; |
| 675 |
} |
| 676 |
|
| 677 |
int create_client(SOCKET ls, SOCKET s, CLI *arg) { |
| 678 |
(void)ls; /* this parameter is only used with USE_FORK */ |
| 679 |
s_log(LOG_DEBUG, "Creating a new thread"); |
| 680 |
if((long)_beginthread(client_thread, NULL, arg->opt->stack_size, arg)==-1L) { |
| 681 |
ioerror("_beginthread"); |
| 682 |
str_free(arg); |
| 683 |
if(s>=0) |
| 684 |
closesocket(s); |
| 685 |
return -1; |
| 686 |
} |
| 687 |
s_log(LOG_DEBUG, "New thread created"); |
| 688 |
return 0; |
| 689 |
} |
| 690 |
|
| 691 |
#endif /* USE_OS2 */ |
| 692 |
|
| 693 |
#ifdef _WIN32_WCE |
| 694 |
|
| 695 |
uintptr_t _beginthreadex(void *security, unsigned stack_size, |
| 696 |
unsigned ( __stdcall *start_address)(void *), |
| 697 |
void *arglist, unsigned initflag, unsigned *thrdaddr) { |
| 698 |
return CreateThread(NULL, stack_size, |
| 699 |
(LPTHREAD_START_ROUTINE)start_address, arglist, |
| 700 |
(DWORD)initflag, (LPDWORD)thrdaddr); |
| 701 |
} |
| 702 |
|
| 703 |
void _endthreadex(unsigned retval) { |
| 704 |
ExitThread(retval); |
| 705 |
} |
| 706 |
|
| 707 |
#endif /* _WIN32_WCE */ |
| 708 |
|
| 709 |
#ifdef DEBUG_STACK_SIZE |
| 710 |
|
| 711 |
#define STACK_RESERVE 16384 |
| 712 |
|
| 713 |
/* some heuristic to determine the usage of client stack size */ |
| 714 |
NOEXPORT size_t stack_num(size_t stack_size, int init) { |
| 715 |
#ifdef _WIN64 |
| 716 |
typedef unsigned long long TL; |
| 717 |
#else |
| 718 |
typedef unsigned long TL; |
| 719 |
#endif |
| 720 |
size_t verify_area, verify_num, i; |
| 721 |
TL test_value, *table; |
| 722 |
|
| 723 |
if(stack_size<STACK_RESERVE) |
| 724 |
return 0; |
| 725 |
verify_area=(stack_size-STACK_RESERVE)&~(sizeof(TL)-1); |
| 726 |
verify_num=verify_area/sizeof(TL); |
| 727 |
test_value=(TL)0x1337deadbeef1337; |
| 728 |
table=alloca(verify_area); |
| 729 |
|
| 730 |
if(init) { |
| 731 |
for(i=0; i<verify_num; i++) |
| 732 |
table[i]=test_value; |
| 733 |
ignore_value(table); /* prevent code optimization */ |
| 734 |
return 0; |
| 735 |
} else { |
| 736 |
/* the stack grows down */ |
| 737 |
for(i=0; i<verify_num; i++) |
| 738 |
if(table[i]!=test_value) |
| 739 |
break; |
| 740 |
if(i>=16) |
| 741 |
return stack_size-i*sizeof(TL); |
| 742 |
/* the stack grows up */ |
| 743 |
for(i=0; i<verify_num; i++) |
| 744 |
if(table[verify_num-i-1]!=test_value) |
| 745 |
break; |
| 746 |
if(i>=16) |
| 747 |
return stack_size-(i*sizeof(TL)+STACK_RESERVE); |
| 748 |
return 0; /* not enough samples for meaningful results */ |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
#ifdef __GNUC__ |
| 753 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 754 |
#pragma GCC diagnostic push |
| 755 |
#endif /* __GNUC__>=4.6 */ |
| 756 |
#pragma GCC diagnostic ignored "-Wformat" |
| 757 |
#endif /* __GNUC__ */ |
| 758 |
void stack_info(size_t stack_size, int init) { /* 1-initialize, 0-display */ |
| 759 |
static size_t max_num=0; |
| 760 |
size_t num; |
| 761 |
|
| 762 |
#ifdef USE_WIN32 |
| 763 |
SYSTEM_INFO si; |
| 764 |
GetSystemInfo(&si); |
| 765 |
stack_size&=~((size_t)si.dwPageSize-1); |
| 766 |
#elif defined(_SC_PAGESIZE) |
| 767 |
stack_size&=~((size_t)sysconf(_SC_PAGESIZE)-1); |
| 768 |
#elif defined(_SC_PAGE_SIZE) |
| 769 |
stack_size&=~((size_t)sysconf(_SC_PAGE_SIZE)-1); |
| 770 |
#else |
| 771 |
stack_size&=~(4096-1); /* just a guess */ |
| 772 |
#endif |
| 773 |
num=stack_num(stack_size, init); |
| 774 |
if(init) |
| 775 |
return; |
| 776 |
if(!num) { |
| 777 |
s_log(LOG_NOTICE, "STACK_RESERVE is too high"); |
| 778 |
return; |
| 779 |
} |
| 780 |
if(num>max_num) |
| 781 |
max_num=num; |
| 782 |
s_log(LOG_NOTICE, |
| 783 |
#ifdef USE_WIN32 |
| 784 |
"stack_info: size=%Iu, current=%Iu (%Iu%%), maximum=%Iu (%Iu%%)", |
| 785 |
#else |
| 786 |
"stack_info: size=%zu, current=%zu (%zu%%), maximum=%zu (%zu%%)", |
| 787 |
#endif |
| 788 |
stack_size, |
| 789 |
num, num*100/stack_size, |
| 790 |
max_num, max_num*100/stack_size); |
| 791 |
} |
| 792 |
#ifdef __GNUC__ |
| 793 |
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 794 |
#pragma GCC diagnostic pop |
| 795 |
#endif /* __GNUC__>=4.6 */ |
| 796 |
#endif /* __GNUC__ */ |
| 797 |
|
| 798 |
#endif /* DEBUG_STACK_SIZE */ |
| 799 |
|
| 800 |
#ifndef USE_FORK |
| 801 |
NOEXPORT void thread_list_add(CLI *c) { |
| 802 |
c->thread_next=thread_head; |
| 803 |
c->thread_prev=NULL; |
| 804 |
if(thread_head) |
| 805 |
thread_head->thread_prev=c; |
| 806 |
thread_head=c; |
| 807 |
} |
| 808 |
#endif /* !USE_FORK */ |
| 809 |
|
| 810 |
/* end of sthreads.c */ |