# HG changeset patch # User jbe # Date 1471734721 -7200 # Node ID 05fab92f20e94ca4567b90c12125395c03d289b2 # Parent 90e6db4506778b26def90840256db9b95593193e Work on tlsconf function diff -r 90e6db450677 -r 05fab92f20e9 moonbridge_io.c --- a/moonbridge_io.c Sat Aug 20 21:56:30 2016 +0200 +++ b/moonbridge_io.c Sun Aug 21 01:12:01 2016 +0200 @@ -1636,8 +1636,42 @@ } #ifdef MOONBR_IO_USE_TLS + +#define moonbr_io_tlsconf_string(name, field, func) \ + /* NOTE: use valuetype = lua_getfield(...) for LUA_VERSION_NUM >= 503 */ \ + lua_getfield(L, 1, (field)); \ + valuetype = lua_type(L, -1); \ + if (valuetype != LUA_TNIL) { \ + luaL_argcheck(L, valuetype == LUA_TSTRING, 1, "field \"" field "\" is not a string"); \ + value = lua_tostring(L, -1); \ + if (func(tlsconf, value)) { \ + lua_pushnil(L); \ + lua_pushfstring(L, "Could not set " name " \"%s\"", value); \ + return 2; \ + } \ + } \ + lua_pop(L, 1); + +#define moonbr_io_tlsconf_binary(name, field, func) \ + /* NOTE: use valuetype = lua_getfield(...) for LUA_VERSION_NUM >= 503 */ \ + lua_getfield(L, 1, (field)); \ + valuetype = lua_type(L, -1); \ + if (valuetype != LUA_TNIL) { \ + luaL_argcheck(L, valuetype == LUA_TSTRING, 1, "field \"" field "\" is not a string"); \ + value = lua_tolstring(L, -1, &valuelen); \ + if (func(tlsconf, (void *)value, valuelen)) { \ + lua_pushnil(L); \ + lua_pushliteral(L, "Could not set " name); \ + return 2; \ + } \ + } \ + lua_pop(L, 1); + static int moonbr_io_tlsconf(lua_State *L) { struct tls_config *tlsconf; + int valuetype; + const char *value; + size_t valuelen; luaL_checktype(L, 1, LUA_TTABLE); tlsconf = tls_config_new(); if (!tlsconf) { @@ -1647,6 +1681,13 @@ luaL_setmetatable(L, MOONBR_IO_TLSCONF_MT_REGKEY); lua_pushvalue(L, 1); lua_setuservalue(L, -2); + moonbr_io_tlsconf_string("CA file", "ca_file", tls_config_set_ca_file); + moonbr_io_tlsconf_string("CA path", "ca_path", tls_config_set_ca_path); + moonbr_io_tlsconf_binary("CA", "ca_mem", tls_config_set_ca_mem); + moonbr_io_tlsconf_string("certificate file", "ca_file", tls_config_set_cert_file); + moonbr_io_tlsconf_binary("certificate", "cert_mem", tls_config_set_cert_mem); + moonbr_io_tlsconf_string("key file", "key_file", tls_config_set_key_file); + moonbr_io_tlsconf_binary("key", "key_mem", tls_config_set_key_mem); return 1; } @@ -1681,6 +1722,7 @@ lua_setuservalue(L, 1); return 0; } + #endif static const struct luaL_Reg moonbr_io_handle_methods[] = {