diff --git a/src/context.c b/src/context.c index 821b675..5bd757d 100644 --- a/src/context.c +++ b/src/context.c @@ -35,6 +35,29 @@ typedef const SSL_METHOD LSEC_SSL_METHOD; typedef SSL_METHOD LSEC_SSL_METHOD; #endif + +/*-- Compat - Lua 5.1 --------------------------------------------------------*/ + +#if (LUA_VERSION_NUM == 501) + +#define luaL_testudata(L, ud, tname) testudata(L, ud, tname) + +static void *testudata (lua_State *L, int ud, const char *tname) { + void *p = lua_touserdata(L, ud); + if (p != NULL) { /* value is a userdata? */ + if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ + luaL_getmetatable(L, tname); /* get correct metatable */ + if (!lua_rawequal(L, -1, -2)) /* not the same? */ + p = NULL; /* value is a userdata with wrong metatable */ + lua_pop(L, 2); /* remove both metatables */ + return p; + } + } + return NULL; /* value is not a userdata with a metatable */ +} + +#endif + /*--------------------------- Auxiliary Functions ----------------------------*/ /** @@ -45,6 +68,11 @@ static p_context checkctx(lua_State *L, int idx) return (p_context)luaL_checkudata(L, idx, "SSL:Context"); } +static p_context testctx(lua_State *L, int idx) +{ + return (p_context)luaL_testudata(L, idx, "SSL:Context"); +} + /** * Prepare the SSL options flag. */ @@ -709,6 +737,12 @@ SSL_CTX* lsec_checkcontext(lua_State *L, int idx) return ctx->context; } +SSL_CTX* lsec_testcontext(lua_State *L, int idx) +{ + p_context ctx = testctx(L, idx); + return (ctx) ? ctx->context : NULL; +} + /** * Retrieve the mode from the context in the Lua stack. */ diff --git a/src/context.h b/src/context.h index 13fc664..07cc575 100644 --- a/src/context.h +++ b/src/context.h @@ -29,6 +29,7 @@ typedef t_context* p_context; /* Retrieve the SSL context from the Lua stack */ SSL_CTX *lsec_checkcontext(lua_State *L, int idx); +SSL_CTX *lsec_testcontext(lua_State *L, int idx); /* Retrieve the mode from the context in the Lua stack */ int lsec_getmode(lua_State *L, int idx);