moonbridge

changeset 249:53962483bf1c

More work on libtls integration (incomplete/experimental yet)
author jbe
date Fri Aug 26 22:04:08 2016 +0200 (2016-08-26)
parents 4c9102fb77eb
children 28bffa2af1ec
files moonbridge_io.c
line diff
     1.1 --- a/moonbridge_io.c	Thu Aug 25 00:01:40 2016 +0200
     1.2 +++ b/moonbridge_io.c	Fri Aug 26 22:04:08 2016 +0200
     1.3 @@ -275,9 +275,22 @@
     1.4      );
     1.5      luabufcnt += handle->readbufin - handle->readbufout;
     1.6      handle->readbufout = 0;
     1.7 -    do {
     1.8 -      bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
     1.9 -    } while (bytesread < 0 && (errno == EINTR));
    1.10 +#ifdef MOONBR_IO_USE_TLS
    1.11 +    if (handle->tls) {
    1.12 +      do bytesread = tls_read(handle->tls, handle->readbuf, MOONBR_IO_READBUFLEN);
    1.13 +      while (!nonblocking && (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT));
    1.14 +      if (bytesread == TLS_WANT_POLLIN || bytesread == TLS_WANT_POLLOUT) {
    1.15 +        errno = EAGAIN;
    1.16 +      } else if (bytesread < 0) {
    1.17 +        lua_pushnil(L);
    1.18 +        lua_pushstring(L, tls_error(handle->tls));
    1.19 +        return 2;
    1.20 +      }
    1.21 +    }
    1.22 +    else
    1.23 +#endif
    1.24 +    do bytesread = read(handle->fd, handle->readbuf, MOONBR_IO_READBUFLEN);
    1.25 +    while (bytesread < 0 && (errno == EINTR));
    1.26      if (
    1.27        bytesread == 0 || (
    1.28          nonblocking &&
    1.29 @@ -490,6 +503,20 @@
    1.30          handle->writebufin = MOONBR_IO_WRITEBUFLEN;
    1.31          while (handle->writebufout < MOONBR_IO_WRITEBUFLEN) {
    1.32            moonbr_io_handle_set_nopush(L, handle, 1);
    1.33 +#ifdef MOONBR_IO_USE_TLS
    1.34 +          if (handle->tls) {
    1.35 +            do written = tls_write(handle->tls, handle->writebuf + handle->writebufout, MOONBR_IO_WRITEBUFLEN - handle->writebufout);
    1.36 +            while (!nonblocking && (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT));
    1.37 +            if (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT) {
    1.38 +              errno = EAGAIN;
    1.39 +            } else if (written < 0) {
    1.40 +              lua_pushnil(L);
    1.41 +              lua_pushstring(L, tls_error(handle->tls));
    1.42 +              return 2;
    1.43 +            }
    1.44 +          }
    1.45 +          else
    1.46 +#endif
    1.47            written = write(
    1.48              handle->fd,
    1.49              handle->writebuf + handle->writebufout,
    1.50 @@ -528,6 +555,20 @@
    1.51    }
    1.52    while (handle->flushedleft) {
    1.53      moonbr_io_handle_set_nopush(L, handle, 1);
    1.54 +#ifdef MOONBR_IO_USE_TLS
    1.55 +    if (handle->tls) {
    1.56 +      do written = tls_write(handle->tls, handle->writebuf + handle->writebufout, handle->writebufin - handle->writebufout);
    1.57 +      while (!nonblocking && (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT));
    1.58 +      if (written == TLS_WANT_POLLIN || written == TLS_WANT_POLLOUT) {
    1.59 +        errno = EAGAIN;
    1.60 +      } else if (written < 0) {
    1.61 +        lua_pushnil(L);
    1.62 +        lua_pushstring(L, tls_error(handle->tls));
    1.63 +        return 2;
    1.64 +      }
    1.65 +    }
    1.66 +    else
    1.67 +#endif
    1.68      written = write(
    1.69        handle->fd,
    1.70        handle->writebuf + handle->writebufout,
    1.71 @@ -1543,6 +1584,9 @@
    1.72  moonbr_io_yield_wrapper(moonbr_io_wait_yield, moonbr_io_wait_call);
    1.73  
    1.74  static int moonbr_io_poll(lua_State *L) {
    1.75 +#if MOONBR_IO_USE_TLS
    1.76 +  // TODO: tls_handshake must probably not be called when handshake has been completed
    1.77 +#endif
    1.78    moonbr_io_handle_t *handle;
    1.79    moonbr_io_listener_t *listener;
    1.80    int fd, isnum;
    1.81 @@ -1812,7 +1856,7 @@
    1.82    if (handle->finished) return luaL_error(L, "Attempt to start TLS on a finished I/O handle");
    1.83  #ifdef MOONBR_IO_USE_TLS
    1.84    if (handle->tls) return luaL_error(L, "Attempt to start TLS twice");
    1.85 -#endif MOONBR_IO_USE_TLS
    1.86 +#endif
    1.87    if (handle->readbufin || handle->writebufin) {
    1.88      return luaL_error(L, "Attempt to start TLS on an I/O handle with non-empty buffers");
    1.89    }

Impressum / About Us