diff options
author | daurnimator <quae@daurnimator.com> | 2018-05-29 11:20:33 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2018-05-29 11:27:55 +1000 |
commit | bbe0e3538c498c19949991c4ccdc4aa124d5f43e (patch) | |
tree | 3eb60e119d39d434a634a941cf7a8dddf68174d2 /src | |
parent | bac568e59ef630d3236f5cd69c5b7ba8fc8f4906 (diff) | |
download | luaossl-bbe0e3538c498c19949991c4ccdc4aa124d5f43e.tar.gz luaossl-bbe0e3538c498c19949991c4ccdc4aa124d5f43e.tar.bz2 luaossl-bbe0e3538c498c19949991c4ccdc4aa124d5f43e.zip |
src/openssl.c: Cache SSL* pointers so that re-pushing same pointer is same object
Diffstat (limited to 'src')
-rw-r--r-- | src/openssl.c | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/src/openssl.c b/src/openssl.c index d0011ca..feff022 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -8942,10 +8942,22 @@ EXPORT int luaopen__openssl_ssl_context(lua_State *L) { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ static void ssl_push(lua_State *L, SSL *ssl) { - SSL **ud = prepsimple(L, SSL_CLASS); + lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)&ssl_push); + if (LUA_TNIL == lua_rawgetp(L, -1, ssl)) { + SSL **ud; + + lua_pop(L, 1); /* pop nil */ + + ud = prepsimple(L, SSL_CLASS); - SSL_up_ref(ssl); - *ud = ssl; + SSL_up_ref(ssl); + *ud = ssl; + + /* Add to SSL* cache */ + lua_pushvalue(L, -1); + lua_rawsetp(L, -3, ssl); + } + lua_remove(L, -2); } /* ssl_push() */ @@ -8975,6 +8987,12 @@ static int ssl_new(lua_State *L) { if (!*ud) return auxL_error(L, auxL_EOPENSSL, "ssl.new"); + /* Add to SSL* cache */ + lua_rawgetp(L, LUA_REGISTRYINDEX, (void *)&ssl_push); + lua_pushvalue(L, -2); + lua_rawsetp(L, -2, *ud); + lua_pop(L, 1); + return 1; } /* ssl_new() */ @@ -11088,5 +11106,13 @@ static void initall(lua_State *L) { auxL_addclass(L, CIPHER_CLASS, cipher_methods, cipher_metatable, 0); auxL_addclass(L, OCSP_RESPONSE_CLASS, or_methods, or_metatable, 0); auxL_addclass(L, OCSP_BASICRESP_CLASS, ob_methods, ob_metatable, 0); + + /* Create cache for SSL* pointers */ + lua_newtable(L); + lua_createtable(L, 0, 1); + lua_pushliteral(L, "kv"); + lua_setfield(L, -2, "__mode"); + lua_setmetatable(L, -2); + lua_rawsetp(L, LUA_REGISTRYINDEX, (void *)&ssl_push); } /* initall() */ |