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
commit 9c19ce0698

View file

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