# HG changeset patch # User jbe # Date 1472241848 -7200 # Node ID 53962483bf1c0a94b51df069950da14eb680d99a # Parent 4c9102fb77ebb10e18d60af42ccb6bb115ee9e95 More work on libtls integration (incomplete/experimental yet) diff -r 4c9102fb77eb -r 53962483bf1c moonbridge_io.c --- a/moonbridge_io.c Thu Aug 25 00:01:40 2016 +0200 +++ b/moonbridge_io.c Fri Aug 26 22:04:08 2016 +0200 @@ -275,9 +275,22 @@ ); luabufcnt += handle->readbufin - handle->readbufout; handle->readbufout = 0; - do { - bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN); - } while (bytesread < 0 && (errno == EINTR)); +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) { + do bytesread = tls_read(handle->tls, handle->readbuf, MOONBR_IO_READBUFLEN); + while (!nonblocking && (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT)); + if (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT) { + errno = EAGAIN; + } else if (bytesread < 0) { + lua_pushnil(L); + lua_pushstring(L, tls_error(handle->tls)); + return 2; + } + } + else +#endif + do bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN); + while (bytesread < 0 && (errno == EINTR)); if ( bytesread == 0 || ( nonblocking && @@ -490,6 +503,20 @@ handle->writebufin = MOONBR_IO_WRITEBUFLEN; while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) { moonbr_io_handle_set_nopush(L, handle, 1); +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) { + do written = tls_write(handle->tls, handle->writebuf + handle->writebufout, MOONBR_IO_WRITEBUFLEN - handle->writebufout); + while (!nonblocking && (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT)); + if (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT) { + errno = EAGAIN; + } else if (written < 0) { + lua_pushnil(L); + lua_pushstring(L, tls_error(handle->tls)); + return 2; + } + } + else +#endif written = write( handle->fd, handle->writebuf + handle->writebufout, @@ -528,6 +555,20 @@ } while (handle->flushedleft) { moonbr_io_handle_set_nopush(L, handle, 1); +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) { + do written = tls_write(handle->tls, handle->writebuf + handle->writebufout, handle->writebufin - handle->writebufout); + while (!nonblocking && (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT)); + if (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT) { + errno = EAGAIN; + } else if (written < 0) { + lua_pushnil(L); + lua_pushstring(L, tls_error(handle->tls)); + return 2; + } + } + else +#endif written = write( handle->fd, handle->writebuf + handle->writebufout, @@ -1543,6 +1584,9 @@ moonbr_io_yield_wrapper(moonbr_io_wait_yield, moonbr_io_wait_call); static int moonbr_io_poll(lua_State *L) { +#if MOONBR_IO_USE_TLS + // TODO: tls_handshake must probably not be called when handshake has been completed +#endif moonbr_io_handle_t *handle; moonbr_io_listener_t *listener; int fd, isnum; @@ -1812,7 +1856,7 @@ if (handle->finished) return luaL_error(L, "Attempt to start TLS on a finished I/O handle"); #ifdef MOONBR_IO_USE_TLS if (handle->tls) return luaL_error(L, "Attempt to start TLS twice"); -#endif MOONBR_IO_USE_TLS +#endif if (handle->readbufin || handle->writebufin) { return luaL_error(L, "Attempt to start TLS on an I/O handle with non-empty buffers"); }