mirror of
https://github.com/libp2p/go-openssl.git
synced 2025-04-24 17:50:13 +08:00
space monkey internal commit export
[katamari commit: 3fd2f40f50e175aaea2666553ce083378a56ade0]
This commit is contained in:
parent
b9b4dfd97b
commit
1748ac5391
43
init.go
43
init.go
@ -58,12 +58,24 @@ package openssl
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/engine.h>
|
||||
|
||||
extern unsigned long sslThreadId();
|
||||
extern void sslMutexOp(int mode, int n, char *file, int line);
|
||||
extern int Goopenssl_init_locks();
|
||||
extern void Goopenssl_thread_locking_callback(int, int, const char*, int);
|
||||
|
||||
static int Goopenssl_init_threadsafety() {
|
||||
// Set up OPENSSL thread safety callbacks. We only set the locking
|
||||
// callback because the default id callback implementation is good
|
||||
// enough for us.
|
||||
int rc = Goopenssl_init_locks();
|
||||
if (rc == 0) {
|
||||
CRYPTO_set_locking_callback(Goopenssl_thread_locking_callback);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void OpenSSL_add_all_algorithms_not_a_macro() {
|
||||
OpenSSL_add_all_algorithms();
|
||||
OpenSSL_add_all_algorithms();
|
||||
}
|
||||
|
||||
*/
|
||||
import "C"
|
||||
|
||||
@ -72,8 +84,6 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"code.spacemonkey.com/go/openssl/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -86,11 +96,10 @@ func init() {
|
||||
C.SSL_load_error_strings()
|
||||
C.SSL_library_init()
|
||||
C.OpenSSL_add_all_algorithms_not_a_macro()
|
||||
sslMutexes = make([]sync.Mutex, int(C.CRYPTO_num_locks()))
|
||||
C.CRYPTO_set_id_callback((*[0]byte)(C.sslThreadId))
|
||||
C.CRYPTO_set_locking_callback((*[0]byte)(C.sslMutexOp))
|
||||
|
||||
// TODO: support dynlock callbacks
|
||||
rc := C.Goopenssl_init_threadsafety()
|
||||
if rc != 0 {
|
||||
panic(fmt.Errorf("Goopenssl_init_locks failed with %d", rc))
|
||||
}
|
||||
}
|
||||
|
||||
// errorFromErrorQueue needs to run in the same OS thread as the operation
|
||||
@ -109,17 +118,3 @@ func errorFromErrorQueue() error {
|
||||
}
|
||||
return errors.New(fmt.Sprintf("SSL errors: %s", strings.Join(errs, "\n")))
|
||||
}
|
||||
|
||||
//export sslMutexOp
|
||||
func sslMutexOp(mode, n C.int, file *C.char, line C.int) {
|
||||
if mode&C.CRYPTO_LOCK > 0 {
|
||||
sslMutexes[n].Lock()
|
||||
} else {
|
||||
sslMutexes[n].Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
//export sslThreadId
|
||||
func sslThreadId() C.ulong {
|
||||
return C.ulong(uintptr(utils.ThreadId()))
|
||||
}
|
||||
|
50
init_posix.go
Normal file
50
init_posix.go
Normal file
@ -0,0 +1,50 @@
|
||||
// Copyright (C) 2014 Space Monkey, Inc.
|
||||
// +build linux darwin cgo
|
||||
|
||||
package openssl
|
||||
|
||||
/*
|
||||
#include <errno.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <pthread.h>
|
||||
|
||||
pthread_mutex_t* goopenssl_locks;
|
||||
|
||||
int Goopenssl_init_locks() {
|
||||
int rc = 0;
|
||||
int nlock;
|
||||
int i;
|
||||
int locks_needed = CRYPTO_num_locks();
|
||||
|
||||
goopenssl_locks = (pthread_mutex_t*)malloc(
|
||||
sizeof(pthread_mutex_t) * locks_needed);
|
||||
if (!goopenssl_locks) {
|
||||
return ENOMEM;
|
||||
}
|
||||
for (nlock = 0; nlock < locks_needed; ++nlock) {
|
||||
rc = pthread_mutex_init(&goopenssl_locks[nlock], NULL);
|
||||
if (rc != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
for (i = nlock - 1; i >= 0; --i) {
|
||||
pthread_mutex_destroy(&goopenssl_locks[i]);
|
||||
}
|
||||
free(goopenssl_locks);
|
||||
goopenssl_locks = NULL;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
void Goopenssl_thread_locking_callback(int mode, int n, const char *file,
|
||||
int line) {
|
||||
if (mode & CRYPTO_LOCK) {
|
||||
pthread_mutex_lock(&goopenssl_locks[n]);
|
||||
} else {
|
||||
pthread_mutex_unlock(&goopenssl_locks[n]);
|
||||
}
|
||||
}
|
||||
*/
|
||||
import "C"
|
@ -1,8 +0,0 @@
|
||||
// Copyright (C) 2014 Space Monkey, Inc.
|
||||
|
||||
#include "runtime.h"
|
||||
|
||||
void ·ThreadId(void *id) {
|
||||
id = (void *)m;
|
||||
FLUSH(&id);
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
// Copyright (C) 2014 Space Monkey, Inc.
|
||||
|
||||
// Package utils provides some small things that implementation of the OpenSSL
|
||||
// wrapper library needed.
|
||||
package utils
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// ThreadId returns the current runtime's thread id. Thanks to Gustavo Niemeyer
|
||||
// for this. https://github.com/niemeyer/qml/blob/master/tref/tref.go
|
||||
func ThreadId() unsafe.Pointer
|
Loading…
Reference in New Issue
Block a user