From 96f8fe1b9e65cb549edebb9bf7e4de33e4c89815 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Thu, 29 Sep 2016 16:00:18 -0400 Subject: [PATCH] ssl fix --- Sources/TLS/Context.swift | 2 +- Sources/TLS/Socket.swift | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Sources/TLS/Context.swift b/Sources/TLS/Context.swift index 8fccf1d..e6a5e40 100644 --- a/Sources/TLS/Context.swift +++ b/Sources/TLS/Context.swift @@ -13,7 +13,7 @@ import Foundation public final class Context { public typealias CContext = OpaquePointer public let mode: Mode - public let cContext: CContext + public var cContext: CContext /** Creates an SSL Context. diff --git a/Sources/TLS/Socket.swift b/Sources/TLS/Socket.swift index 5cbd5b9..714bac7 100644 --- a/Sources/TLS/Socket.swift +++ b/Sources/TLS/Socket.swift @@ -20,6 +20,9 @@ public final class Socket { self.socket = socket } + public var currSocket: TCPInternetSocket? + public var currContext: OpaquePointer? + public convenience init( mode: Mode, hostname: String, @@ -57,6 +60,8 @@ public final class Socket { socket.descriptor, servername ) + currSocket = socket + currContext = config.context.cContext guard result == Result.OK else { throw TLSError.connect(config.context.error) @@ -69,11 +74,13 @@ public final class Socket { This should only be called if the Context's mode is `.server` */ public func accept() throws { + let new = try socket.accept() let result = tls_accept_socket( config.context.cContext, - nil, - socket.descriptor + &currContext, + new.descriptor ) + currSocket = new guard result == Result.OK else { throw TLSError.accept(config.context.error) @@ -91,7 +98,7 @@ public final class Socket { pointer.deallocate(capacity: max) } - let result = tls_read(config.context.cContext, pointer, max) + let result = tls_read(currContext, pointer, max) let bytesRead = Int(result) guard bytesRead >= 0 else { @@ -111,7 +118,7 @@ public final class Socket { public func send(_ bytes: [UInt8]) throws { let buffer = UnsafeBufferPointer(start: bytes, count: bytes.count) - let bytesSent = tls_write(config.context.cContext, buffer.baseAddress, bytes.count) + let bytesSent = tls_write(currContext, buffer.baseAddress, bytes.count) guard bytesSent >= 0 else { throw TLSError.send(config.context.error) @@ -122,8 +129,8 @@ public final class Socket { Sends a shutdown to secure socket */ public func close() throws { - let result = tls_close(config.context.cContext) - try socket.close() + let result = tls_close(currContext) + try currSocket?.close() guard result == Result.OK else { throw TLSError.close(config.context.error) }