From a80a0972b7757b23b950ef032019d2b5007d9b19 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 30 Nov 2016 00:25:03 +0200 Subject: Fixed dynlink with OpenSSL >1.1.0. Added loadLibPattern. --- lib/pure/dynlib.nim | 32 ++++++++++++++++++++++++++++---- lib/pure/net.nim | 33 ++++++++++----------------------- 2 files changed, 38 insertions(+), 27 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/dynlib.nim b/lib/pure/dynlib.nim index 906a9d23e..fda41dadb 100644 --- a/lib/pure/dynlib.nim +++ b/lib/pure/dynlib.nim @@ -11,20 +11,22 @@ ## libraries. On POSIX this uses the ``dlsym`` mechanism, on ## Windows ``LoadLibrary``. +import strutils + type LibHandle* = pointer ## a handle to a dynamically loaded library {.deprecated: [TLibHandle: LibHandle].} -proc loadLib*(path: string, global_symbols=false): LibHandle +proc loadLib*(path: string, global_symbols=false): LibHandle {.gcsafe.} ## loads a library from `path`. Returns nil if the library could not ## be loaded. -proc loadLib*(): LibHandle +proc loadLib*(): LibHandle {.gcsafe.} ## gets the handle from the current executable. Returns nil if the ## library could not be loaded. -proc unloadLib*(lib: LibHandle) +proc unloadLib*(lib: LibHandle) {.gcsafe.} ## unloads the library `lib` proc raiseInvalidLibrary*(name: cstring) {.noinline, noreturn.} = @@ -34,7 +36,7 @@ proc raiseInvalidLibrary*(name: cstring) {.noinline, noreturn.} = e.msg = "could not find symbol: " & $name raise e -proc symAddr*(lib: LibHandle, name: cstring): pointer +proc symAddr*(lib: LibHandle, name: cstring): pointer {.gcsafe.} ## retrieves the address of a procedure/variable from `lib`. Returns nil ## if the symbol could not be found. @@ -44,6 +46,28 @@ proc checkedSymAddr*(lib: LibHandle, name: cstring): pointer = result = symAddr(lib, name) if result == nil: raiseInvalidLibrary(name) +proc libCandidates*(s: string, dest: var seq[string]) = + ## given a library name pattern `s` write possible library names to `dest`. + var le = strutils.find(s, '(') + var ri = strutils.find(s, ')', le+1) + if le >= 0 and ri > le: + var prefix = substr(s, 0, le - 1) + var suffix = substr(s, ri + 1) + for middle in split(substr(s, le + 1, ri - 1), '|'): + libCandidates(prefix & middle & suffix, dest) + else: + add(dest, s) + +proc loadLibPattern*(pattern: string, global_symbols=false): LibHandle = + ## loads a library with name matching `pattern`, similar to what `dlimport` + ## pragma does. Returns nil if the library could not be loaded. + ## Warning: this proc uses the GC and so cannot be used to load the GC. + var candidates = newSeq[string]() + libCandidates(pattern, candidates) + for c in candidates: + result = loadLib(c, global_symbols) + if not result.isNil: break + when defined(posix): # # ========================================================================= diff --git a/lib/pure/net.nim b/lib/pure/net.nim index 863a8a6f4..5e10f2291 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -90,8 +90,8 @@ when defineSsl: SslContext* = ref object context*: SslCtx - extraInternalIndex: int referencedData: HashSet[int] + extraInternal: SslContextExtraInternal SslAcceptResult* = enum AcceptNoClient = 0, AcceptNoHandshake, AcceptSuccess @@ -103,6 +103,10 @@ when defineSsl: SslServerGetPskFunc* = proc(identity: string): string + SslContextExtraInternal = ref object of RootRef + serverGetPskFunc: SslServerGetPskFunc + clientGetPskFunc: SslClientGetPskFunc + {.deprecated: [ESSL: SSLError, TSSLCVerifyMode: SSLCVerifyMode, TSSLProtVersion: SSLProtVersion, PSSLContext: SSLContext, TSSLAcceptResult: SSLAcceptResult].} @@ -240,11 +244,6 @@ when defineSsl: ErrLoadBioStrings() OpenSSL_add_all_algorithms() - type - SslContextExtraInternal = ref object of RootRef - serverGetPskFunc: SslServerGetPskFunc - clientGetPskFunc: SslClientGetPskFunc - proc raiseSSLError*(s = "") = ## Raises a new SSL error. if s != "": @@ -257,12 +256,6 @@ when defineSsl: var errStr = ErrErrorString(err, nil) raise newException(SSLError, $errStr) - proc getExtraDataIndex*(ctx: SSLContext): int = - ## Retrieves unique index for storing extra data in SSLContext. - result = SSL_CTX_get_ex_new_index(0, nil, nil, nil, nil).int - if result < 0: - raiseSSLError() - proc getExtraData*(ctx: SSLContext, index: int): RootRef = ## Retrieves arbitrary data stored inside SSLContext. if index notin ctx.referencedData: @@ -347,15 +340,11 @@ when defineSsl: discard newCTX.SSLCTXSetMode(SSL_MODE_AUTO_RETRY) newCTX.loadCertificates(certFile, keyFile) - result = SSLContext(context: newCTX, extraInternalIndex: 0, - referencedData: initSet[int]()) - result.extraInternalIndex = getExtraDataIndex(result) - - let extraInternal = new(SslContextExtraInternal) - result.setExtraData(result.extraInternalIndex, extraInternal) + result = SSLContext(context: newCTX, referencedData: initSet[int](), + extraInternal: new(SslContextExtraInternal)) proc getExtraInternal(ctx: SSLContext): SslContextExtraInternal = - return SslContextExtraInternal(ctx.getExtraData(ctx.extraInternalIndex)) + return ctx.extraInternal proc destroyContext*(ctx: SSLContext) = ## Free memory referenced by SSLContext. @@ -379,7 +368,7 @@ when defineSsl: proc pskClientCallback(ssl: SslPtr; hint: cstring; identity: cstring; max_identity_len: cuint; psk: ptr cuchar; max_psk_len: cuint): cuint {.cdecl.} = - let ctx = SSLContext(context: ssl.SSL_get_SSL_CTX, extraInternalIndex: 0) + let ctx = SSLContext(context: ssl.SSL_get_SSL_CTX) let hintString = if hint == nil: nil else: $hint let (identityString, pskString) = (ctx.clientGetPskFunc)(hintString) if psk.len.cuint > max_psk_len: @@ -398,8 +387,6 @@ when defineSsl: ## ## Only used in PSK ciphersuites. ctx.getExtraInternal().clientGetPskFunc = fun - assert ctx.extraInternalIndex == 0, - "The pskClientCallback assumes the extraInternalIndex is 0" ctx.context.SSL_CTX_set_psk_client_callback( if fun == nil: nil else: pskClientCallback) @@ -407,7 +394,7 @@ when defineSsl: return ctx.getExtraInternal().serverGetPskFunc proc pskServerCallback(ssl: SslCtx; identity: cstring; psk: ptr cuchar; max_psk_len: cint): cuint {.cdecl.} = - let ctx = SSLContext(context: ssl.SSL_get_SSL_CTX, extraInternalIndex: 0) + let ctx = SSLContext(context: ssl.SSL_get_SSL_CTX) let pskString = (ctx.serverGetPskFunc)($identity) if psk.len.cint > max_psk_len: return 0 -- cgit 1.4.1-2-gfad0