Add lsec_testcontext().

This commit is contained in:
Bruno Silvestre 2015-10-28 00:05:30 -02:00
parent 9340ce0916
commit 96401bdf67
2 changed files with 35 additions and 0 deletions

View File

@ -35,6 +35,29 @@ typedef const SSL_METHOD LSEC_SSL_METHOD;
typedef SSL_METHOD LSEC_SSL_METHOD; typedef SSL_METHOD LSEC_SSL_METHOD;
#endif #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 ----------------------------*/ /*--------------------------- Auxiliary Functions ----------------------------*/
/** /**
@ -45,6 +68,11 @@ static p_context checkctx(lua_State *L, int idx)
return (p_context)luaL_checkudata(L, idx, "SSL:Context"); 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. * Prepare the SSL options flag.
*/ */
@ -709,6 +737,12 @@ SSL_CTX* lsec_checkcontext(lua_State *L, int idx)
return ctx->context; 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. * Retrieve the mode from the context in the Lua stack.
*/ */

View File

@ -29,6 +29,7 @@ typedef t_context* p_context;
/* Retrieve the SSL context from the Lua stack */ /* Retrieve the SSL context from the Lua stack */
SSL_CTX *lsec_checkcontext(lua_State *L, int idx); 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 */ /* Retrieve the mode from the context in the Lua stack */
int lsec_getmode(lua_State *L, int idx); int lsec_getmode(lua_State *L, int idx);