net.nim: support storing arbitrary data inside SSLContext

This commit is contained in:
Michał Zieliński
2015-10-22 23:51:52 +02:00
parent a90e23a4dd
commit 3ebf27ddd2
3 changed files with 32 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
# Stores extra data inside the SSL context.
import net
# Our unique index for storing foos
let fooIndex = getSslContextExtraDataIndex()
# And another unique index for storing foos
let barIndex = getSslContextExtraDataIndex()
echo "got indexes ", fooIndex, " ", barIndex
let ctx = newContext()
assert ctx.getExtraData(fooIndex) == nil
let foo: int = 5
ctx.setExtraData(fooIndex, cast[pointer](foo))
assert cast[int](ctx.getExtraData(fooIndex)) == foo

View File

@@ -243,6 +243,20 @@ when defined(ssl):
newCTX.loadCertificates(certFile, keyFile)
return SSLContext(newCTX)
proc getSslContextExtraDataIndex*(): cint =
## Retrieves unique index for storing extra data in SSLContext.
return SSL_CTX_get_ex_new_index(0, nil, nil, nil, nil)
proc setExtraData*(ctx: SSLContext, index: cint, data: pointer) =
## Stores arbitrary data inside SSLContext. The unique `index`
## should be retrieved using getSslContextExtraDataIndex.
if SslCtx(ctx).SSL_CTX_set_ex_data(index, data) == -1:
raiseSSLError()
proc getExtraData*(ctx: SSLContext, index: cint): pointer =
## Retrieves arbitrary data stored inside SSLContext.
return SslCtx(ctx).SSL_CTX_get_ex_data(index)
proc wrapSocket*(ctx: SSLContext, socket: Socket) =
## Wraps a socket in an SSL context. This function effectively turns
## ``socket`` into an SSL socket.

View File

@@ -216,6 +216,10 @@ proc SSL_CTX_use_PrivateKey_file*(ctx: SslCtx,
proc SSL_CTX_check_private_key*(ctx: SslCtx): cInt{.cdecl, dynlib: DLLSSLName,
importc.}
proc SSL_CTX_get_ex_new_index*(argl: clong, argp: pointer, new_func: pointer, dup_func: pointer, free_func: pointer): cint {.cdecl, dynlib: DLLSSLName, importc.}
proc SSL_CTX_set_ex_data*(ssl: SslCtx, idx: cint, arg: pointer): cint {.cdecl, dynlib: DLLSSLName, importc.}
proc SSL_CTX_get_ex_data*(ssl: SslCtx, idx: cint): pointer {.cdecl, dynlib: DLLSSLName, importc.}
proc SSL_set_fd*(ssl: SslPtr, fd: SocketHandle): cint{.cdecl, dynlib: DLLSSLName, importc.}
proc SSL_shutdown*(ssl: SslPtr): cInt{.cdecl, dynlib: DLLSSLName, importc.}