📄 mttest.c
字号:
fprintf(stderr,"SSL SERVER STARTUP FAILED\n"); return(1); } else { s_write=1; s_w=1;#ifdef undef fprintf(stdout,"SERVER:from client:"); fwrite(sbuf,1,i,stdout); fflush(stdout);#endif } } else { i=BIO_write(s_bio,"hello from server\n",18); if (i < 0) { s_r=0; s_w=0; if (BIO_should_retry(s_bio)) { if (BIO_should_read(s_bio)) s_r=1; if (BIO_should_write(s_bio)) s_w=1; } else { fprintf(stderr,"ERROR in SERVER\n"); ERR_print_errors_fp(stderr); return(1); } } else if (i == 0) { fprintf(stderr,"SSL SERVER STARTUP FAILED\n"); return(1); } else { s_write=0; s_r=1; done|=S_DONE; } } } if ((done & S_DONE) && (done & C_DONE)) break;# if defined(OPENSSL_SYS_NETWARE) ThreadSwitchWithDelay();# endif } SSL_set_shutdown(c_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); SSL_set_shutdown(s_ssl,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);#ifdef undef fprintf(stdout,"DONE\n");#endiferr: /* We have to set the BIO's to NULL otherwise they will be * free()ed twice. Once when th s_ssl is SSL_free()ed and * again when c_ssl is SSL_free()ed. * This is a hack required because s_ssl and c_ssl are sharing the same * BIO structure and SSL_set_bio() and SSL_free() automatically * BIO_free non NULL entries. * You should not normally do this or be required to do this */ if (s_ssl != NULL) { s_ssl->rbio=NULL; s_ssl->wbio=NULL; } if (c_ssl != NULL) { c_ssl->rbio=NULL; c_ssl->wbio=NULL; } /* The SSL's are optionally freed in the following calls */ if (c_to_s != NULL) BIO_free(c_to_s); if (s_to_c != NULL) BIO_free(s_to_c); if (c_bio != NULL) BIO_free(c_bio); if (s_bio != NULL) BIO_free(s_bio); return(0); }int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx) { char *s, buf[256]; if (verbose) { s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), buf,256); if (s != NULL) { if (ok) fprintf(stderr,"depth=%d %s\n", ctx->error_depth,buf); else fprintf(stderr,"depth=%d error=%d %s\n", ctx->error_depth,ctx->error,buf); } } return(ok); }#define THREAD_STACK_SIZE (16*1024)#ifdef OPENSSL_SYS_WIN32static HANDLE *lock_cs;void thread_setup(void) { int i; lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE)); for (i=0; i<CRYPTO_num_locks(); i++) { lock_cs[i]=CreateMutex(NULL,FALSE,NULL); } CRYPTO_set_locking_callback((void (*)(int,int,char *,int))win32_locking_callback); /* id callback defined */ }void thread_cleanup(void) { int i; CRYPTO_set_locking_callback(NULL); for (i=0; i<CRYPTO_num_locks(); i++) CloseHandle(lock_cs[i]); OPENSSL_free(lock_cs); }void win32_locking_callback(int mode, int type, char *file, int line) { if (mode & CRYPTO_LOCK) { WaitForSingleObject(lock_cs[type],INFINITE); } else { ReleaseMutex(lock_cs[type]); } }void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) { double ret; SSL_CTX *ssl_ctx[2]; DWORD thread_id[MAX_THREAD_NUMBER]; HANDLE thread_handle[MAX_THREAD_NUMBER]; int i; SYSTEMTIME start,end; ssl_ctx[0]=s_ctx; ssl_ctx[1]=c_ctx; GetSystemTime(&start); for (i=0; i<thread_number; i++) { thread_handle[i]=CreateThread(NULL, THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)ndoit, (void *)ssl_ctx, 0L, &(thread_id[i])); } printf("reaping\n"); for (i=0; i<thread_number; i+=50) { int j; j=(thread_number < (i+50))?(thread_number-i):50; if (WaitForMultipleObjects(j, (CONST HANDLE *)&(thread_handle[i]),TRUE,INFINITE) == WAIT_FAILED) { fprintf(stderr,"WaitForMultipleObjects failed:%d\n",GetLastError()); exit(1); } } GetSystemTime(&end); if (start.wDayOfWeek > end.wDayOfWeek) end.wDayOfWeek+=7; ret=(end.wDayOfWeek-start.wDayOfWeek)*24; ret=(ret+end.wHour-start.wHour)*60; ret=(ret+end.wMinute-start.wMinute)*60; ret=(ret+end.wSecond-start.wSecond); ret+=(end.wMilliseconds-start.wMilliseconds)/1000.0; printf("win32 threads done - %.3f seconds\n",ret); }#endif /* OPENSSL_SYS_WIN32 */#ifdef SOLARISstatic mutex_t *lock_cs;/*static rwlock_t *lock_cs; */static long *lock_count;void thread_setup(void) { int i; lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t)); lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); for (i=0; i<CRYPTO_num_locks(); i++) { lock_count[i]=0; /* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */ mutex_init(&(lock_cs[i]),USYNC_THREAD,NULL); } CRYPTO_set_id_callback((unsigned long (*)())solaris_thread_id); CRYPTO_set_locking_callback((void (*)())solaris_locking_callback); }void thread_cleanup(void) { int i; CRYPTO_set_locking_callback(NULL); fprintf(stderr,"cleanup\n"); for (i=0; i<CRYPTO_num_locks(); i++) { /* rwlock_destroy(&(lock_cs[i])); */ mutex_destroy(&(lock_cs[i])); fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i)); } OPENSSL_free(lock_cs); OPENSSL_free(lock_count); fprintf(stderr,"done cleanup\n"); }void solaris_locking_callback(int mode, int type, char *file, int line) {#ifdef undef fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n", CRYPTO_thread_id(), (mode&CRYPTO_LOCK)?"l":"u", (type&CRYPTO_READ)?"r":"w",file,line);#endif /* if (CRYPTO_LOCK_SSL_CERT == type) fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n", CRYPTO_thread_id(), mode,file,line); */ if (mode & CRYPTO_LOCK) { /* if (mode & CRYPTO_READ) rw_rdlock(&(lock_cs[type])); else rw_wrlock(&(lock_cs[type])); */ mutex_lock(&(lock_cs[type])); lock_count[type]++; } else {/* rw_unlock(&(lock_cs[type])); */ mutex_unlock(&(lock_cs[type])); } }void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) { SSL_CTX *ssl_ctx[2]; thread_t thread_ctx[MAX_THREAD_NUMBER]; int i; ssl_ctx[0]=s_ctx; ssl_ctx[1]=c_ctx; thr_setconcurrency(thread_number); for (i=0; i<thread_number; i++) { thr_create(NULL, THREAD_STACK_SIZE, (void *(*)())ndoit, (void *)ssl_ctx, 0L, &(thread_ctx[i])); } printf("reaping\n"); for (i=0; i<thread_number; i++) { thr_join(thread_ctx[i],NULL,NULL); } printf("solaris threads done (%d,%d)\n", s_ctx->references,c_ctx->references); }unsigned long solaris_thread_id(void) { unsigned long ret; ret=(unsigned long)thr_self(); return(ret); }#endif /* SOLARIS */#ifdef IRIXstatic usptr_t *arena;static usema_t **lock_cs;void thread_setup(void) { int i; char filename[20]; strcpy(filename,"/tmp/mttest.XXXXXX"); mktemp(filename); usconfig(CONF_STHREADIOOFF); usconfig(CONF_STHREADMALLOCOFF); usconfig(CONF_INITUSERS,100); usconfig(CONF_LOCKTYPE,US_DEBUGPLUS); arena=usinit(filename); unlink(filename); lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(usema_t *)); for (i=0; i<CRYPTO_num_locks(); i++) { lock_cs[i]=usnewsema(arena,1); } CRYPTO_set_id_callback((unsigned long (*)())irix_thread_id); CRYPTO_set_locking_callback((void (*)())irix_locking_callback); }void thread_cleanup(void) { int i; CRYPTO_set_locking_callback(NULL); for (i=0; i<CRYPTO_num_locks(); i++) { char buf[10]; sprintf(buf,"%2d:",i); usdumpsema(lock_cs[i],stdout,buf); usfreesema(lock_cs[i],arena); } OPENSSL_free(lock_cs); }void irix_locking_callback(int mode, int type, char *file, int line) { if (mode & CRYPTO_LOCK) { printf("lock %d\n",type); uspsema(lock_cs[type]); } else { printf("unlock %d\n",type); usvsema(lock_cs[type]); } }void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) { SSL_CTX *ssl_ctx[2]; int thread_ctx[MAX_THREAD_NUMBER]; int i; ssl_ctx[0]=s_ctx; ssl_ctx[1]=c_ctx; for (i=0; i<thread_number; i++) { thread_ctx[i]=sproc((void (*)())ndoit, PR_SADDR|PR_SFDS,(void *)ssl_ctx); } printf("reaping\n"); for (i=0; i<thread_number; i++) { wait(NULL); } printf("irix threads done (%d,%d)\n", s_ctx->references,c_ctx->references); }unsigned long irix_thread_id(void) { unsigned long ret; ret=(unsigned long)getpid(); return(ret); }#endif /* IRIX */#ifdef PTHREADSstatic pthread_mutex_t *lock_cs;static long *lock_count;void thread_setup(void) { int i; lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); for (i=0; i<CRYPTO_num_locks(); i++) { lock_count[i]=0; pthread_mutex_init(&(lock_cs[i]),NULL); } CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id); CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback); }void thread_cleanup(void) { int i; CRYPTO_set_locking_callback(NULL); fprintf(stderr,"cleanup\n"); for (i=0; i<CRYPTO_num_locks(); i++) { pthread_mutex_destroy(&(lock_cs[i])); fprintf(stderr,"%8ld:%s\n",lock_count[i], CRYPTO_get_lock_name(i)); } OPENSSL_free(lock_cs); OPENSSL_free(lock_count); fprintf(stderr,"done cleanup\n"); }void pthreads_locking_callback(int mode, int type, char *file, int line) {#ifdef undef fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n", CRYPTO_thread_id(), (mode&CRYPTO_LOCK)?"l":"u", (type&CRYPTO_READ)?"r":"w",file,line);#endif/* if (CRYPTO_LOCK_SSL_CERT == type) fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n", CRYPTO_thread_id(), mode,file,line);*/ if (mode & CRYPTO_LOCK) { pthread_mutex_lock(&(lock_cs[type])); lock_count[type]++; } else { pthread_mutex_unlock(&(lock_cs[type])); } }void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx) { SSL_CTX *ssl_ctx[2]; pthread_t thread_ctx[MAX_THREAD_NUMBER]; int i; ssl_ctx[0]=s_ctx; ssl_ctx[1]=c_ctx; /* thr_setconcurrency(thread_number); */ for (i=0; i<thread_number; i++) { pthread_create(&(thread_ctx[i]), NULL, (void *(*)())ndoit, (void *)ssl_ctx); } printf("reaping\n"); for (i=0; i<thread_number; i++) { pthread_join(thread_ctx[i],NULL); } printf("pthreads threads done (%d,%d)\n", s_ctx->references,c_ctx->references); }unsigned long pthreads_thread_id(void) { unsigned long ret; ret=(unsigned long)pthread_self(); return(ret); }#endif /* PTHREADS */#ifdef OPENSSL_SYS_NETWAREvoid thread_setup(void){ int i; lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(MPKMutex)); lock_count=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long)); for (i=0; i<CRYPTO_num_locks(); i++) { lock_count[i]=0; lock_cs[i]=MPKMutexAlloc("OpenSSL mutex"); } ThreadSem = MPKSemaphoreAlloc("OpenSSL mttest semaphore", 0 ); CRYPTO_set_id_callback((unsigned long (*)())netware_thread_id); CRYPTO_set_locking_callback((void (*)())netware_locking_callback);}void thread_cleanup(void){ int i; CRYPTO_set_locking_callback(NULL); fprintf(stdout,"thread_cleanup\n"); for (i=0; i<CRYPTO_num_locks(); i++) { MPKMutexFree(lock_cs[i]); fprintf(stdout,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i)); } OPENSSL_free(lock_cs); OPENSSL_free(lock_count); MPKSemaphoreFree(ThreadSem); fprintf(stdout,"done cleanup\n");}void netware_locking_callback(int mode, int type, char *file, int line){ if (mode & CRYPTO_LOCK) { MPKMutexLock(lock_cs[type]); lock_count[type]++; } else MPKMutexUnlock(lock_cs[type]);}void do_threads(SSL_CTX *s_ctx, SSL_CTX *c_ctx){ SSL_CTX *ssl_ctx[2]; int i; ssl_ctx[0]=s_ctx; ssl_ctx[1]=c_ctx; for (i=0; i<thread_number; i++) { BeginThread( (void(*)(void*))ndoit, NULL, THREAD_STACK_SIZE, (void*)ssl_ctx); ThreadSwitchWithDelay(); } printf("reaping\n"); /* loop until all threads have signaled the semaphore */ for (i=0; i<thread_number; i++) { MPKSemaphoreWait(ThreadSem); } printf("netware threads done (%d,%d)\n", s_ctx->references,c_ctx->references);}unsigned long netware_thread_id(void){ unsigned long ret; ret=(unsigned long)GetThreadID(); return(ret);}#endif /* NETWARE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -