X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fnixio%2Fsrc%2Ftls-socket.c;h=0f504ccbbea90a8265661ddf922a21a9f665e217;hp=b0cfb5c3f18b03a2f6b0f52c876be26f14a58387;hb=ee4852a5ad79452f2ea76e32463a0af91b857752;hpb=ff2bc9081bb24e42dec9ccd46cdb4c377333dc59 diff --git a/libs/nixio/src/tls-socket.c b/libs/nixio/src/tls-socket.c index b0cfb5c3f..0f504ccbb 100644 --- a/libs/nixio/src/tls-socket.c +++ b/libs/nixio/src/tls-socket.c @@ -27,7 +27,7 @@ static int nixio__tls_sock_perror(lua_State *L, SSL *sock, int code) { } static int nixio__tls_sock_pstatus(lua_State *L, SSL *sock, int code) { - if (code == 1) { + if (code > 0) { lua_pushboolean(L, 1); return 1; } else { @@ -45,8 +45,26 @@ static SSL* nixio__checktlssock(lua_State *L) { return sock->socket; } +#ifndef WITH_AXTLS +#define nixio_tls__check_connected(L) ; + +#define nixio_tls__set_connected(L, val) ; +#else +#define nixio_tls__check_connected(L) \ + nixio_tls_sock *ctsock = luaL_checkudata(L, 1, NIXIO_TLS_SOCK_META); \ + if (!ctsock->connected) { \ + lua_pushnil(L); \ + lua_pushinteger(L, 1); \ + return 2; \ + } + +#define nixio_tls__set_connected(L, val) \ +((nixio_tls_sock*)luaL_checkudata(L, 1, NIXIO_TLS_SOCK_META))->connected = val; +#endif /* WITH_AXTLS */ + static int nixio_tls_sock_recv(lua_State *L) { SSL *sock = nixio__checktlssock(L); + nixio_tls__check_connected(L); int req = luaL_checkinteger(L, 2); luaL_argcheck(L, req >= 0, 2, "out of range"); @@ -82,11 +100,11 @@ static int nixio_tls_sock_recv(lua_State *L) { t->pbufsiz -= req; return 1; } else { - char *axbuf; + uint8_t *axbuf; int axread; /* while handshake pending */ - while ((axread = ssl_read(sock, (uint8_t**)&axbuf)) == SSL_OK); + while ((axread = ssl_read(sock, &axbuf)) == SSL_OK); if (t->pbufsiz) { lua_pushlstring(L, t->pbufpos, t->pbufsiz); @@ -96,20 +114,22 @@ static int nixio_tls_sock_recv(lua_State *L) { /* There is an error */ free(t->pbuffer); t->pbuffer = t->pbufpos = NULL; - t->pbufsiz = 0; if (axread != SSL_ERROR_CONN_LOST) { + t->pbufsiz = 0; return nixio__tls_sock_perror(L, sock, axread); } else { if (!t->pbufsiz) { lua_pushliteral(L, ""); + } else { + t->pbufsiz = 0; } } } else { int stillwant = req - t->pbufsiz; if (stillwant < axread) { /* we got more data than we need */ - lua_pushlstring(L, axbuf, stillwant); + lua_pushlstring(L, (char *)axbuf, stillwant); if(t->pbufsiz) { lua_concat(L, 2); } @@ -128,7 +148,7 @@ static int nixio_tls_sock_recv(lua_State *L) { t->pbufpos = t->pbuffer; memcpy(t->pbufpos, axbuf + stillwant, t->pbufsiz); } else { - lua_pushlstring(L, axbuf, axread); + lua_pushlstring(L, (char *)axbuf, axread); if(t->pbufsiz) { lua_concat(L, 2); } @@ -148,6 +168,7 @@ static int nixio_tls_sock_recv(lua_State *L) { static int nixio_tls_sock_send(lua_State *L) { SSL *sock = nixio__checktlssock(L); + nixio_tls__check_connected(L); size_t len; ssize_t sent; const char *data = luaL_checklstring(L, 2, &len); @@ -156,22 +177,27 @@ static int nixio_tls_sock_send(lua_State *L) { lua_pushinteger(L, sent); return 1; } else { - return nixio__tls_sock_pstatus(L, sock, len); + return nixio__tls_sock_pstatus(L, sock, sent); } } static int nixio_tls_sock_accept(lua_State *L) { SSL *sock = nixio__checktlssock(L); - return nixio__tls_sock_pstatus(L, sock, SSL_accept(sock)); + const int stat = SSL_accept(sock); + nixio_tls__set_connected(L, stat == 1); + return nixio__tls_sock_pstatus(L, sock, stat); } static int nixio_tls_sock_connect(lua_State *L) { SSL *sock = nixio__checktlssock(L); - return nixio__tls_sock_pstatus(L, sock, SSL_connect(sock)); + const int stat = SSL_connect(sock); + nixio_tls__set_connected(L, stat == 1); + return nixio__tls_sock_pstatus(L, sock, stat); } static int nixio_tls_sock_shutdown(lua_State *L) { SSL *sock = nixio__checktlssock(L); + nixio_tls__set_connected(L, 0); return nixio__tls_sock_pstatus(L, sock, SSL_shutdown(sock)); } @@ -198,6 +224,8 @@ static int nixio_tls_sock__tostring(lua_State *L) { static const luaL_reg M[] = { {"recv", nixio_tls_sock_recv}, {"send", nixio_tls_sock_send}, + {"read", nixio_tls_sock_recv}, + {"write", nixio_tls_sock_send}, {"accept", nixio_tls_sock_accept}, {"connect", nixio_tls_sock_connect}, {"shutdown", nixio_tls_sock_shutdown}, @@ -213,5 +241,5 @@ void nixio_open_tls_socket(lua_State *L) { luaL_register(L, NULL, M); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - lua_setfield(L, -2, "tls_socket_meta"); + lua_setfield(L, -2, "meta_tls_socket"); }