Add a handshake parameter to wrapSocket() to allow it to work on an already-connected socket.

This commit is contained in:
Wim Lewis
2015-04-10 15:51:39 -07:00
parent 0c947f31ba
commit 9c19ce0698

View File

@@ -87,6 +87,12 @@ type
of false: nil
AsyncSocket* = ref AsyncSocketDesc
when defined(ssl):
type HandshakeType* = enum
handshakeNone,
handshakeAsClient,
handshakeAsServer
{.deprecated: [PAsyncSocket: AsyncSocket].}
# TODO: Save AF, domain etc info and reuse it in procs which need it like connect.
@@ -418,7 +424,7 @@ proc close*(socket: AsyncSocket) =
socket.closed = true # TODO: Add extra debugging checks for this.
when defined(ssl):
proc wrapSocket*(ctx: SslContext, socket: AsyncSocket) =
proc wrapSocket*(ctx: SslContext, socket: AsyncSocket, handshake: HandshakeType = handshakeNone) =
## Wraps a socket in an SSL context. This function effectively turns
## ``socket`` into an SSL socket.
##
@@ -434,6 +440,14 @@ when defined(ssl):
socket.bioOut = bioNew(bio_s_mem())
sslSetBio(socket.sslHandle, socket.bioIn, socket.bioOut)
case handshake
of handshakeNone:
discard
of handshakeAsClient:
sslSetConnectState(socket.sslHandle)
of handshakeAsServer:
sslSetAcceptState(socket.sslHandle)
proc getSockOpt*(socket: AsyncSocket, opt: SOBool, level = SOL_SOCKET): bool {.
tags: [ReadIOEffect].} =
## Retrieves option ``opt`` as a boolean value.