moonbridge

changeset 240:05fab92f20e9

Work on tlsconf function
author jbe
date Sun Aug 21 01:12:01 2016 +0200 (2016-08-21)
parents 90e6db450677
children 58e442fbc5a3
files moonbridge_io.c
line diff
     1.1 --- a/moonbridge_io.c	Sat Aug 20 21:56:30 2016 +0200
     1.2 +++ b/moonbridge_io.c	Sun Aug 21 01:12:01 2016 +0200
     1.3 @@ -1636,8 +1636,42 @@
     1.4  }
     1.5  
     1.6  #ifdef MOONBR_IO_USE_TLS
     1.7 +
     1.8 +#define moonbr_io_tlsconf_string(name, field, func) \
     1.9 +  /* NOTE: use valuetype = lua_getfield(...) for LUA_VERSION_NUM >= 503 */ \
    1.10 +  lua_getfield(L, 1, (field)); \
    1.11 +  valuetype = lua_type(L, -1); \
    1.12 +  if (valuetype != LUA_TNIL) { \
    1.13 +    luaL_argcheck(L, valuetype == LUA_TSTRING, 1, "field \"" field "\" is not a string"); \
    1.14 +    value = lua_tostring(L, -1); \
    1.15 +    if (func(tlsconf, value)) { \
    1.16 +      lua_pushnil(L); \
    1.17 +      lua_pushfstring(L, "Could not set " name " \"%s\"", value); \
    1.18 +      return 2; \
    1.19 +    } \
    1.20 +  } \
    1.21 +  lua_pop(L, 1);
    1.22 +
    1.23 +#define moonbr_io_tlsconf_binary(name, field, func) \
    1.24 +  /* NOTE: use valuetype = lua_getfield(...) for LUA_VERSION_NUM >= 503 */ \
    1.25 +  lua_getfield(L, 1, (field)); \
    1.26 +  valuetype = lua_type(L, -1); \
    1.27 +  if (valuetype != LUA_TNIL) { \
    1.28 +    luaL_argcheck(L, valuetype == LUA_TSTRING, 1, "field \"" field "\" is not a string"); \
    1.29 +    value = lua_tolstring(L, -1, &valuelen); \
    1.30 +    if (func(tlsconf, (void *)value, valuelen)) { \
    1.31 +      lua_pushnil(L); \
    1.32 +      lua_pushliteral(L, "Could not set " name); \
    1.33 +      return 2; \
    1.34 +    } \
    1.35 +  } \
    1.36 +  lua_pop(L, 1);
    1.37 +
    1.38  static int moonbr_io_tlsconf(lua_State *L) {
    1.39    struct tls_config *tlsconf;
    1.40 +  int valuetype;
    1.41 +  const char *value;
    1.42 +  size_t valuelen;
    1.43    luaL_checktype(L, 1, LUA_TTABLE);
    1.44    tlsconf = tls_config_new();
    1.45    if (!tlsconf) {
    1.46 @@ -1647,6 +1681,13 @@
    1.47    luaL_setmetatable(L, MOONBR_IO_TLSCONF_MT_REGKEY);
    1.48    lua_pushvalue(L, 1);
    1.49    lua_setuservalue(L, -2);
    1.50 +  moonbr_io_tlsconf_string("CA file",          "ca_file",  tls_config_set_ca_file);
    1.51 +  moonbr_io_tlsconf_string("CA path",          "ca_path",  tls_config_set_ca_path);
    1.52 +  moonbr_io_tlsconf_binary("CA",               "ca_mem",   tls_config_set_ca_mem);
    1.53 +  moonbr_io_tlsconf_string("certificate file", "ca_file",  tls_config_set_cert_file);
    1.54 +  moonbr_io_tlsconf_binary("certificate",      "cert_mem", tls_config_set_cert_mem);
    1.55 +  moonbr_io_tlsconf_string("key file",         "key_file", tls_config_set_key_file);
    1.56 +  moonbr_io_tlsconf_binary("key",              "key_mem",  tls_config_set_key_mem);
    1.57    return 1;
    1.58  }
    1.59  
    1.60 @@ -1681,6 +1722,7 @@
    1.61    lua_setuservalue(L, 1);
    1.62    return 0;
    1.63  }
    1.64 +
    1.65  #endif
    1.66  
    1.67  static const struct luaL_Reg moonbr_io_handle_methods[] = {

Impressum / About Us