diff options
author | daurnimator <quae@daurnimator.com> | 2018-05-28 19:24:49 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2018-05-29 11:27:55 +1000 |
commit | 7accb21ce7912fdb257b860cde1b1891d48e1296 (patch) | |
tree | 717cb16c8cb946ad382dd4157a2767c4faf3ab89 | |
parent | abafd08caed10294156de7d578435841ef5869b0 (diff) | |
download | luaossl-7accb21ce7912fdb257b860cde1b1891d48e1296.tar.gz luaossl-7accb21ce7912fdb257b860cde1b1891d48e1296.tar.bz2 luaossl-7accb21ce7912fdb257b860cde1b1891d48e1296.zip |
src/openssl.c: Add function to push an SSL object from an ffi pointer
-rw-r--r-- | src/openssl.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/openssl.c b/src/openssl.c index 057d7b7..46a9493 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -8950,6 +8950,24 @@ static SSL *ssl_push(lua_State *L, SSL *ssl) { return *ud; } /* ssl_push() */ + +static int ssl_pushffi(lua_State *L) { + SSL *ptr; + + lua_pushvalue(L, lua_upvalueindex(1)); + lua_pushvalue(L, 1); + lua_call(L, 1, 1); + luaL_argcheck(L, lua_toboolean(L, -1), 1, "SSL* ffi pointer expected"); + lua_pop(L, 1); + ptr = *(SSL**)lua_topointer(L, 1); + luaL_argcheck(L, ptr, 1, "SSL* pointer must be non-null"); + + ssl_push(L, ptr); + + return 1; +} /* ssl_pushffi() */ + + static int ssl_new(lua_State *L) { SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS); SSL **ud = prepsimple(L, SSL_CLASS); @@ -9511,6 +9529,7 @@ static const auxL_Reg ssl_metatable[] = { static const auxL_Reg ssl_globals[] = { { "new", &ssl_new }, + { "pushffi", &ssl_pushffi, 1 }, { "interpose", &ssl_interpose }, { NULL, NULL }, }; @@ -9533,6 +9552,26 @@ EXPORT int luaopen__openssl_ssl(lua_State *L) { initall(L); auxL_newlib(L, ssl_globals, 0); + /* FFI argument checking */ + lua_getfield(L, -1, "pushffi"); + luaL_loadstring(L, + "local ffi = require 'ffi'\n" \ + "if not pcall(ffi.typeof, 'SSL*') then\n" \ + " ffi.cdef 'typedef struct ssl_st SSL;'\n" \ + "end\n" \ + "local ffi_istype = ffi.istype\n" \ + "local SSLp = ffi.typeof('SSL*')\n" \ + "return function(p) return ffi_istype(SSLp, p) end\n" + ); + if (lua_pcall(L, 0, 1, 0)) { + /* failed (probably no ffi library available) */ + lua_pop(L, 1); + /* use dummy function instead */ + luaL_loadstring(L, "return false\n"); + }; + lua_setupvalue(L, -2, 1); + lua_pop(L, 1); + auxL_setintegers(L, ssl_version); auxL_setintegers(L, sx_verify); auxL_setintegers(L, sx_option); |