moonbridge
diff moonbridge_io.c @ 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 | 6bab0621806a |
children | 90e6db450677 |
line diff
1.1 --- a/moonbridge_io.c Tue Jul 26 16:22:21 2016 +0200 1.2 +++ b/moonbridge_io.c Sat Aug 20 21:54:15 2016 +0200 1.3 @@ -25,6 +25,10 @@ 1.4 #include <bsd/unistd.h> 1.5 #endif 1.6 1.7 +#ifdef MOONBR_IO_USE_TLS 1.8 +#include <tls.h> 1.9 +#endif 1.10 + 1.11 #include <lua.h> 1.12 #include <lauxlib.h> 1.13 #include <lualib.h> 1.14 @@ -48,6 +52,10 @@ 1.15 #define MOONBR_IO_CHILD_MT_REGKEY "moonbridge_io_child" 1.16 #define MOONBR_IO_CHILD_PT_REGKEY "moonbridge_io_child_pt" 1.17 1.18 +#ifdef MOONBR_IO_USE_TLS 1.19 +#define MOONBR_IO_TLSCONF_MT_REGKEY "moonbridge_io_tlsconf" 1.20 +#endif 1.21 + 1.22 typedef struct { 1.23 int fd; 1.24 int issock; 1.25 @@ -1630,6 +1638,54 @@ 1.26 return 1; 1.27 } 1.28 1.29 +#ifdef MOONBR_IO_USE_TLS 1.30 +static int moonbr_io_tlsconf(lua_State *L) { 1.31 + struct tls_config *tlsconf; 1.32 + luaL_checktype(L, 1, LUA_TTABLE); 1.33 + tlsconf = tls_config_new(); 1.34 + if (!tlsconf) { 1.35 + return luaL_error(L, "Could not allocate memory for TLS configuration"); 1.36 + } 1.37 + lua_pushlightuserdata(L, tlsconf); 1.38 + luaL_setmetatable(L, MOONBR_IO_TLSCONF_MT_REGKEY); 1.39 + lua_pushvalue(L, 1); 1.40 + lua_setuservalue(L, -2); 1.41 + return 1; 1.42 +} 1.43 + 1.44 +static int moonbr_io_tlsconfindex(lua_State *L) { 1.45 + struct tls_config *tlsconf; 1.46 + tlsconf = luaL_checkudata(L, 1, MOONBR_IO_TLSCONF_MT_REGKEY); 1.47 + luaL_checkany(L, 2); 1.48 +#if LUA_VERSION_NUM >= 503 1.49 + if (lua_getuservalue(L, 1) == LUA_TNIL) { 1.50 +#else 1.51 + lua_getuservalue(L, 1); 1.52 + if (lua_isnil(L, -1)) { 1.53 +#endif 1.54 + return luaL_error(L, "Attempt to use a destroyed TLS configuration"); 1.55 + } 1.56 + lua_pushvalue(L, 2); 1.57 + lua_gettable(L, -2); 1.58 + return 1; 1.59 +} 1.60 + 1.61 +static int moonbr_io_tlsconfgc(lua_State *L) { 1.62 + struct tls_config *tlsconf; 1.63 + tlsconf = luaL_checkudata(L, 1, MOONBR_IO_TLSCONF_MT_REGKEY); 1.64 +#if LUA_VERSION_NUM >= 503 1.65 + if (lua_getuservalue(L, 1) == LUA_TNIL) return 0; 1.66 +#else 1.67 + lua_getuservalue(L, 1); 1.68 + if (lua_isnil(L, -1)) return 0; 1.69 +#endif 1.70 + tls_config_free(tlsconf); 1.71 + lua_pushnil(L); 1.72 + lua_setuservalue(L, 1); 1.73 + return 0; 1.74 +} 1.75 +#endif 1.76 + 1.77 static const struct luaL_Reg moonbr_io_handle_methods[] = { 1.78 {"read", moonbr_io_read}, 1.79 {"read_nb", moonbr_io_read_nb}, 1.80 @@ -1698,9 +1754,20 @@ 1.81 {"exec", moonbr_io_exec}, 1.82 {"poll", moonbr_io_poll}, 1.83 {"timeref", moonbr_io_timeref}, 1.84 +#ifdef MOONBR_IO_USE_TLS 1.85 + {"tlsconf", moonbr_io_tlsconf}, 1.86 +#endif 1.87 {NULL, NULL} 1.88 }; 1.89 1.90 +#ifdef MOONBR_IO_USE_TLS 1.91 +static const struct luaL_Reg moonbr_io_tlsconf_metamethods[] = { 1.92 + {"__index", moonbr_io_tlsconfindex}, 1.93 + {"__gc", moonbr_io_tlsconfgc}, 1.94 + {NULL, NULL} 1.95 +}; 1.96 +#endif 1.97 + 1.98 int luaopen_moonbridge_io(lua_State *L) { 1.99 1.100 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */ 1.101 @@ -1743,6 +1810,17 @@ 1.102 lua_setfield(L, -3, "child_mt"); 1.103 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_CHILD_MT_REGKEY); 1.104 1.105 +#ifdef MOONBR_IO_USE_TLS 1.106 + if(tls_init()) { 1.107 + return luaL_error(L, "Could not initialize TLS library"); 1.108 + } 1.109 + lua_newtable(L); // tlsconf metatable 1.110 + luaL_setfuncs(L, moonbr_io_tlsconf_metamethods, 0); 1.111 + lua_pushvalue(L, -1); 1.112 + lua_setfield(L, -3, "tlsconf_mt"); 1.113 + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_TLSCONF_MT_REGKEY); 1.114 +#endif 1.115 + 1.116 moonbr_io_pushhandle(L, 0); 1.117 lua_setfield(L, -2, "stdin"); 1.118 moonbr_io_pushhandle(L, 1);