Guest User

Untitled

a guest
Feb 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. ## the C code for running lua as the callback language for Utu
  2.  
  3. #include "hub.h"
  4. #include <lua.h>
  5. #include <lualib.h>
  6. #include <lauxlib.h>
  7. lua_State *L = NULL;
  8.  
  9. const char *callback_names[] = {
  10. "start", "client_connect", "confirm_key", "peer_established",
  11. "member_registered", "member_failed", "peer_failed",
  12. "msg_recv", "msg_sent", "member_close", "error", "destroyed",
  13. "gen_key_start", "gen_key_done", "listening", "done"
  14. };
  15.  
  16.  
  17. int event_cb(UtuHub *hub, UtuHubEvent ev, void *data)
  18. {
  19. int lerr = 0;
  20. int on_stack = 0, top = 0;
  21. bstring key;
  22. CryptState *state = NULL;
  23. MyriadClient *client = NULL;
  24.  
  25. assert(ev >= UEv_START && ev <= UEv_DONE && "event not within possible value range");
  26.  
  27. top = lua_gettop(L);
  28. lua_getfield(L, LUA_GLOBALSINDEX, "Hub");
  29. lua_getfield(L, -1, callback_names[ev]);
  30. lua_replace(L, -3); // takes the callback and puts it before the hub so hub is self
  31. on_stack++;
  32.  
  33. // some of the callbacks we just want to simplify rather than making
  34. // lua handle tons of data it doesn't need
  35. switch(ev) {
  36. case UEv_START:
  37. // register the hub for later
  38. lua_pushstring(L, "utu_hub");
  39. lua_pushlightuserdata(L, (void *)hub);
  40. /* registry["utu_hub"] = hub */
  41. lua_settable(L, LUA_REGISTRYINDEX);
  42. break;
  43. case UEv_CONFIRM_KEY:
  44. // push the identity and pubkey onto the stack as parameters
  45. state = (CryptState *)data;
  46. key = CryptState_export_key(state, CRYPT_THEIR_KEY, PK_PUBLIC);
  47. lua_pushlstring(L, (const char *)bdata(key), blength(key));
  48. on_stack++;
  49.  
  50. bdestroy(key);
  51. break;
  52. case UEv_CLIENT_CONNECT: // fallthrough
  53. case UEv_PEER_FAILED:
  54. client = (MyriadClient *)data;
  55. struct sockaddr_in addr;
  56. unsigned int ip = 0;
  57. size_t addrlen = sizeof(addr);
  58.  
  59. if (getpeername(client->fd, (struct sockaddr *)&addr, &addrlen) < 0){
  60. perror("getpeername");
  61. } else {
  62. ip = ntohl(addr.sin_addr.s_addr);
  63. }
  64.  
  65. lua_pushinteger(L, ip);
  66. on_stack++;
  67. break;
  68. case UEv_GEN_KEY_DONE:
  69. // push the key bstring onto the stack
  70. key = (bstring)data;
  71. lua_pushlstring(L, (const char *)bdata(key), blength(key));
  72. on_stack++;
  73. break;
  74. default:
  75. if(data) {
  76. lua_pushlightuserdata(L, data);
  77. on_stack++;
  78. }
  79. }
  80.  
  81. // do the function call and abort this client if there's an error
  82. if((lerr = lua_pcall(L, on_stack, 1, 0)) != 0) {
  83. log(ERROR, "calling %s lua function: %s", callback_names[ev], luaL_optstring (L, -1, "unknown error"));
  84. return 0;
  85. }
  86.  
  87. // get the boolean off and return it
  88. lerr = lua_toboolean(L, -1);
  89. lua_pop(L, top);
  90. return lerr;
  91. }
  92.  
  93.  
  94. void setup_lua(const char *server) {
  95. int rc = 0;
  96. L = luaL_newstate();
  97. assert_mem(L);
  98.  
  99. // open all the standard libraries
  100. luaL_openlibs(L);
  101.  
  102. // load our little server script
  103. rc = luaL_loadfile(L, server);
  104. if(rc == 0) {
  105. // loading the file worked
  106.  
  107. rc = lua_pcall(L, 0, LUA_MULTRET, 0);
  108. if(rc == 0) return; // good
  109. }
  110.  
  111. // oops, an error fell through
  112. switch(rc) {
  113. case LUA_ERRRUN:
  114. log(ERROR, "lua error: %s", luaL_optstring (L, -1, "unknown error")); break;
  115. case LUA_ERRMEM: log(ERROR, "lua memory error"); break;
  116. case LUA_ERRERR: log(ERROR, "lua error while running error handler"); break;
  117. case LUA_ERRSYNTAX:
  118. log(ERROR, "syntax error: %s", luaL_optstring (L, -1, "unknown error"));
  119. break;
  120. case LUA_ERRFILE: log(ERROR, "couldn't load the file: %s", server); break;
  121. default: log(ERROR, "unknown lua error: %d", rc);
  122. }
  123.  
  124. exit(1);
  125. }
  126.  
  127. int main ( int argc, char *argv[] )
  128. {
  129. bstring host, port, name;
  130.  
  131. if(argc != 4) {
  132. log(FAIL, "usage: server host port name");
  133. return 1;
  134. }
  135.  
  136. host = bfromcstr(argv[1]);
  137. port = bfromcstr(argv[2]);
  138. name = bfromcstr(argv[3]);
  139.  
  140.  
  141. UtuHub_init(argv[0]);
  142.  
  143. setup_lua("server.lua");
  144.  
  145. UtuHub *hub = UtuHub_create_receiver(host, port, name, NULL, event_cb);
  146.  
  147. UtuHub_start(hub);
  148.  
  149. gc_shutdown();
  150.  
  151. lua_close(L);
  152. return 0;
  153. }
  154.  
  155.  
  156.  
  157.  
  158. ## the simple Lua script that does the work
  159.  
  160. -- functions needed:
  161. -- UtuHub
  162. -- UtuMsg
  163. -- UtuPeer
  164. -- Member
  165.  
  166.  
  167. function hex(digest)
  168. local dgst = {digest:byte(1,#digest)}
  169. local val = ""
  170.  
  171. for i = 1, #dgst do
  172. val = val .. string.format("%02x:", dgst[i])
  173. end
  174.  
  175. return val
  176. end
  177.  
  178. Hub = { }
  179.  
  180. function Hub:confirm_key(key)
  181. print("confim_key", hex(key))
  182. return true
  183. end
  184.  
  185. function Hub:msg_sent(msg)
  186. print("msg_sent")
  187. return true
  188. end
  189.  
  190. function Hub:client_connect(addr)
  191. print("client_connect", addr)
  192. return true
  193. end
  194.  
  195. function Hub:peer_failed(addr)
  196. print("peer_failed", addr)
  197. return true
  198. end
  199.  
  200. function Hub:peer_established(peer)
  201. print("peer_established")
  202. return true
  203. end
  204.  
  205. function Hub:member_failed(peer)
  206. print("member_failed")
  207. return true
  208. end
  209.  
  210. function Hub:member_registered(member)
  211. print("member_registered")
  212. return true
  213. end
  214.  
  215. function Hub:msg_recv(msg)
  216. print("msg_recv")
  217. return true
  218. end
  219.  
  220. function Hub:member_close(member)
  221. print("member_close")
  222. return true
  223. end
  224.  
  225. function Hub:gen_key_start()
  226. print("gen_key_start")
  227. return true
  228. end
  229.  
  230. function Hub:gen_key_done(key)
  231. print("gen_key_done", hex(key))
  232. return true
  233. end
  234.  
  235. function Hub:start()
  236. print("start")
  237. return true
  238. end
  239.  
  240. function Hub:listening()
  241. print("listening")
  242. return true
  243. end
  244.  
  245. function Hub:done()
  246. print("done")
  247. return true
  248. end
  249.  
  250. function Hub:destroyed()
  251. print("destroyed")
  252. return true
  253. end
Add Comment
Please, Sign In to add comment