Skip to content

Commit

Permalink
Fixed up thread callbacks for OpenSSL
Browse files Browse the repository at this point in the history
  • Loading branch information
kjessup committed Oct 14, 2016
1 parent fae1b0f commit a6a8e29
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ fastlane/screenshots

Packages/
PerfectNet.xcodeproj/
.DS_Store
1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

23 changes: 23 additions & 0 deletions Sources/NetTCPSSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import PerfectThread

private typealias passwordCallbackFunc = @convention(c) (UnsafeMutablePointer<Int8>?, Int32, Int32, UnsafeMutableRawPointer?) -> Int32

private var openSSLLocks: [Threading.Lock] = []

public class NetTCPSSL : NetTCP {

public static var opensslVersionText : String {
Expand Down Expand Up @@ -78,6 +80,27 @@ public class NetTCPSSL : NetTCP {
SSL_library_init()
ERR_load_crypto_strings()
SSL_load_error_strings()

for i in 0..<Int(CRYPTO_num_locks()) {
openSSLLocks.append(Threading.Lock())
}

let lockingCallback: @convention(c) (Int32, Int32, UnsafePointer<Int8>?, Int32) -> () = {
(mode:Int32, n:Int32, file:UnsafePointer<Int8>?, line:Int32) in

if (mode & CRYPTO_LOCK) != 0 {
openSSLLocks[Int(n)].lock()
} else {
openSSLLocks[Int(n)].unlock()
}
}
CRYPTO_set_locking_callback(lockingCallback)

let threadIdCallback: @convention(c) () -> UInt = {
return unsafeBitCast(pthread_self(), to: UInt.self)
}

CRYPTO_set_id_callback(threadIdCallback)
return true
}()

Expand Down

0 comments on commit a6a8e29

Please sign in to comment.