src/ssl.c

changeset 31
87625285de20
parent 30
36ed99e1ce1e
child 34
510432315106
equal deleted inserted replaced
30:36ed99e1ce1e 31:87625285de20
55 socket_destroy(&ssl->sock); 55 socket_destroy(&ssl->sock);
56 SSL_free(ssl->ssl); 56 SSL_free(ssl->ssl);
57 ssl->ssl = NULL; 57 ssl->ssl = NULL;
58 } 58 }
59 return 0; 59 return 0;
60 }
61
62 /**
63 * Object information -- tostring metamethod
64 */
65 static int meth_tostring(lua_State *L)
66 {
67 p_ssl ssl = (p_ssl)lua_touserdata(L, 1);
68 lua_pushfstring(L, "SSL connection: %p", ssl);
69 return 1;
60 } 70 }
61 71
62 /** 72 /**
63 * Perform the TLS/SSL handshake 73 * Perform the TLS/SSL handshake
64 */ 74 */
453 463
454 464
455 /** 465 /**
456 * SSL metamethods 466 * SSL metamethods
457 */ 467 */
458 static luaL_Reg meta[] = { 468 static luaL_Reg methods[] = {
459 {"close", meth_close}, 469 {"close", meth_close},
460 {"getfd", meth_getfd}, 470 {"getfd", meth_getfd},
461 {"dirty", meth_dirty}, 471 {"dirty", meth_dirty},
462 {"dohandshake", meth_handshake}, 472 {"dohandshake", meth_handshake},
463 {"receive", meth_receive}, 473 {"receive", meth_receive},
481 {"rawconnection", meth_rawconn}, 491 {"rawconnection", meth_rawconn},
482 {NULL, NULL} 492 {NULL, NULL}
483 }; 493 };
484 494
485 /** 495 /**
496 * Context metamethods.
497 */
498 static luaL_Reg meta[] = {
499 {"__gc", meth_destroy},
500 {"__tostring", meth_tostring},
501 {NULL, NULL}
502 };
503
504 /**
486 * Initialize modules 505 * Initialize modules
487 */ 506 */
488 LUASEC_API int luaopen_ssl_core(lua_State *L) 507 LUASEC_API int luaopen_ssl_core(lua_State *L)
489 { 508 {
490 /* Initialize SSL */ 509 /* Initialize SSL */
497 /* Initialize internal library */ 516 /* Initialize internal library */
498 socket_open(); 517 socket_open();
499 518
500 /* Register the functions and tables */ 519 /* Register the functions and tables */
501 luaL_newmetatable(L, "SSL:Connection"); 520 luaL_newmetatable(L, "SSL:Connection");
521 luaL_register(L, NULL, meta);
522
502 lua_newtable(L); 523 lua_newtable(L);
503 luaL_register(L, NULL, meta); 524 luaL_register(L, NULL, methods);
504 lua_setfield(L, -2, "__index"); 525 lua_setfield(L, -2, "__index");
505 lua_pushcfunction(L, meth_destroy);
506 lua_setfield(L, -2, "__gc");
507 526
508 luaL_register(L, "ssl.core", funcs); 527 luaL_register(L, "ssl.core", funcs);
509 lua_pushnumber(L, SOCKET_INVALID); 528 lua_pushnumber(L, SOCKET_INVALID);
510 lua_setfield(L, -2, "invalidfd"); 529 lua_setfield(L, -2, "invalidfd");
511 530

mercurial