moonbridge

changeset 237:1fd00eed96ee

Work on optional libtls integration for moonbridge_io (tls_config stub)
author jbe
date Sat Aug 20 21:54:15 2016 +0200 (2016-08-20)
parents 5f2a2f73e9d3
children 90e6db450677
files Makefile moonbridge_io.c
line diff
     1.1 --- a/Makefile	Tue Jul 26 16:22:21 2016 +0200
     1.2 +++ b/Makefile	Sat Aug 20 21:54:15 2016 +0200
     1.3 @@ -61,6 +61,10 @@
     1.4  moonbridge_io.so: moonbridge_io.o
     1.5  	ld -shared -o moonbridge_io.so moonbridge_io.o $(UTIL_FLAGS)
     1.6  
     1.7 +freebsd_with_tls::
     1.8 +	#TODO
     1.9 +	cc -D MOONBR_IO_USE_TLS -c -Wall -O2 -fPIC -I /usr/local/include/lua52 -I /usr/local/include -o moonbridge_io.o moonbridge_io.c && ld -L/usr/local/lib -shared -o moonbridge_io.so moonbridge_io.o -lutil -ltls
    1.10 +
    1.11  clean::
    1.12  	rm -f moonbridge moonbridge_io.o moonbridge_io.so
    1.13  
     2.1 --- a/moonbridge_io.c	Tue Jul 26 16:22:21 2016 +0200
     2.2 +++ b/moonbridge_io.c	Sat Aug 20 21:54:15 2016 +0200
     2.3 @@ -25,6 +25,10 @@
     2.4  #include <bsd/unistd.h>
     2.5  #endif
     2.6  
     2.7 +#ifdef MOONBR_IO_USE_TLS
     2.8 +#include <tls.h>
     2.9 +#endif
    2.10 +
    2.11  #include <lua.h>
    2.12  #include <lauxlib.h>
    2.13  #include <lualib.h>
    2.14 @@ -48,6 +52,10 @@
    2.15  #define MOONBR_IO_CHILD_MT_REGKEY "moonbridge_io_child"
    2.16  #define MOONBR_IO_CHILD_PT_REGKEY "moonbridge_io_child_pt"
    2.17  
    2.18 +#ifdef MOONBR_IO_USE_TLS
    2.19 +#define MOONBR_IO_TLSCONF_MT_REGKEY "moonbridge_io_tlsconf"
    2.20 +#endif
    2.21 +
    2.22  typedef struct {
    2.23    int fd;
    2.24    int issock;
    2.25 @@ -1630,6 +1638,54 @@
    2.26    return 1;
    2.27  }
    2.28  
    2.29 +#ifdef MOONBR_IO_USE_TLS
    2.30 +static int moonbr_io_tlsconf(lua_State *L) {
    2.31 +  struct tls_config *tlsconf;
    2.32 +  luaL_checktype(L, 1, LUA_TTABLE);
    2.33 +  tlsconf = tls_config_new();
    2.34 +  if (!tlsconf) {
    2.35 +    return luaL_error(L, "Could not allocate memory for TLS configuration");
    2.36 +  }
    2.37 +  lua_pushlightuserdata(L, tlsconf);
    2.38 +  luaL_setmetatable(L, MOONBR_IO_TLSCONF_MT_REGKEY);
    2.39 +  lua_pushvalue(L, 1);
    2.40 +  lua_setuservalue(L, -2);
    2.41 +  return 1;
    2.42 +}
    2.43 +
    2.44 +static int moonbr_io_tlsconfindex(lua_State *L) {
    2.45 +  struct tls_config *tlsconf;
    2.46 +  tlsconf = luaL_checkudata(L, 1, MOONBR_IO_TLSCONF_MT_REGKEY);
    2.47 +  luaL_checkany(L, 2);
    2.48 +#if LUA_VERSION_NUM >= 503
    2.49 +  if (lua_getuservalue(L, 1) == LUA_TNIL) {
    2.50 +#else
    2.51 +  lua_getuservalue(L, 1);
    2.52 +  if (lua_isnil(L, -1)) {
    2.53 +#endif
    2.54 +    return luaL_error(L, "Attempt to use a destroyed TLS configuration");
    2.55 +  }
    2.56 +  lua_pushvalue(L, 2);
    2.57 +  lua_gettable(L, -2);
    2.58 +  return 1;
    2.59 +}
    2.60 +
    2.61 +static int moonbr_io_tlsconfgc(lua_State *L) {
    2.62 +  struct tls_config *tlsconf;
    2.63 +  tlsconf = luaL_checkudata(L, 1, MOONBR_IO_TLSCONF_MT_REGKEY);
    2.64 +#if LUA_VERSION_NUM >= 503
    2.65 +  if (lua_getuservalue(L, 1) == LUA_TNIL) return 0;
    2.66 +#else
    2.67 +  lua_getuservalue(L, 1);
    2.68 +  if (lua_isnil(L, -1)) return 0;
    2.69 +#endif
    2.70 +  tls_config_free(tlsconf);
    2.71 +  lua_pushnil(L);
    2.72 +  lua_setuservalue(L, 1);
    2.73 +  return 0;
    2.74 +}
    2.75 +#endif
    2.76 +
    2.77  static const struct luaL_Reg moonbr_io_handle_methods[] = {
    2.78    {"read", moonbr_io_read},
    2.79    {"read_nb", moonbr_io_read_nb},
    2.80 @@ -1698,9 +1754,20 @@
    2.81    {"exec", moonbr_io_exec},
    2.82    {"poll", moonbr_io_poll},
    2.83    {"timeref", moonbr_io_timeref},
    2.84 +#ifdef MOONBR_IO_USE_TLS
    2.85 +  {"tlsconf", moonbr_io_tlsconf},
    2.86 +#endif
    2.87    {NULL, NULL}
    2.88  };
    2.89  
    2.90 +#ifdef MOONBR_IO_USE_TLS
    2.91 +static const struct luaL_Reg moonbr_io_tlsconf_metamethods[] = {
    2.92 +  {"__index", moonbr_io_tlsconfindex},
    2.93 +  {"__gc", moonbr_io_tlsconfgc},
    2.94 +  {NULL, NULL}
    2.95 +};
    2.96 +#endif
    2.97 +
    2.98  int luaopen_moonbridge_io(lua_State *L) {
    2.99  
   2.100    signal(SIGPIPE, SIG_IGN);  /* generate I/O errors instead of signal 13 */
   2.101 @@ -1743,6 +1810,17 @@
   2.102    lua_setfield(L, -3, "child_mt");
   2.103    lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_MT_REGKEY);
   2.104  
   2.105 +#ifdef MOONBR_IO_USE_TLS
   2.106 +  if(tls_init()) {
   2.107 +    return luaL_error(L, "Could not initialize TLS library");
   2.108 +  }
   2.109 +  lua_newtable(L);  // tlsconf metatable
   2.110 +  luaL_setfuncs(L, moonbr_io_tlsconf_metamethods, 0);
   2.111 +  lua_pushvalue(L, -1);
   2.112 +  lua_setfield(L, -3, "tlsconf_mt");
   2.113 +  lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_TLSCONF_MT_REGKEY);
   2.114 +#endif
   2.115 +
   2.116    moonbr_io_pushhandle(L, 0);
   2.117    lua_setfield(L, -2, "stdin");
   2.118    moonbr_io_pushhandle(L, 1);

Impressum / About Us