<commit_before>/* gnutls.cpp
 * Copyright (C) 2006 <NAME>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */


#include "tnt/gnutls.h"
#include "tnt/tntnet.h"
#include <cxxtools/thread.h>
#include <cxxtools/log.h>
#include <sstream>
#include "tnt/gcryptinit.h"
#include <sys/poll.h>
#include <errno.h>

log_define("tntnet.ssl")

namespace tnt
{
  namespace
  {
    cxxtools::Mutex mutex;

    // read with timeout; assume non-blocking of underlying fd
    ssize_t pull_func(gnutls_transport_ptr_t ptr, void* buffer, size_t bufsize)
    {
      const GnuTlsStream::FdInfo& fdInfo = *static_cast<const GnuTlsStream::FdInfo*>(ptr);

      while (true)
      {
        log_debug("read");
        ssize_t n = ::read(fdInfo.fd, buffer, bufsize);
        log_debug("read returns " << n << " errno=" << errno);

        if (n > 0)
          return n;
        else if (n == 0 || errno == ECONNRESET)
          return 0;
        else if (errno == EAGAIN)
        {
          // poll
          struct pollfd fds;
          fds.fd = fdInfo.fd;
          fds.events = POLLIN;

          log_debug("poll; timeout " << fdInfo.timeout);
          int p = ::poll(&fds, 1, fdInfo.timeout);
          if (p == 0)
          {
            errno = EAGAIN;
            return -1;
          }
          else if (p < 0)
            return p;
        }
        else if (errno != EINTR)
          return -1;
      }

      return -1;
    }

    // write with timeout; assume non-blocking of underlying fd
    ssize_t push_func(gnutls_transport_ptr_t ptr, const void* buffer, size_t bufsize)
    {
      const GnuTlsStream::FdInfo& fdInfo = *static_cast<const GnuTlsStream::FdInfo*>(ptr);

      while (true)
      {
        log_debug("write");
        ssize_t n = ::write(fdInfo.fd, buffer, bufsize);
        log_debug("write returns " << n);

        if (n > 0)
          return n;
        else if (n == 0 || errno == ECONNRESET)
          return 0;
        else if (errno == EAGAIN)
        {
          // poll
          struct pollfd fds;
          fds.fd = fdInfo.fd;
          fds.events = POLLOUT;

          int p = ::poll(&fds, 1, fdInfo.timeout);
          if (p == 0)
          {
            errno = EAGAIN;
            return -1;
          }
          else if (p < 0)
            return p;
        }
        else if (errno != EINTR)
          return -1;
      }

      return -1;
    }
<commit_msg>more detailed debug message when reading or writing in gnutls<commit_after>/* gnutls.cpp
 * Copyright (C) 2006 <NAME>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */


#include "tnt/gnutls.h"
#include "tnt/tntnet.h"
#include <cxxtools/thread.h>
#include <cxxtools/log.h>
#include <sstream>
#include "tnt/gcryptinit.h"
#include <sys/poll.h>
#include <errno.h>

log_define("tntnet.ssl")

namespace tnt
{
  namespace
  {
    cxxtools::Mutex mutex;

    // read with timeout; assume non-blocking of underlying fd
    ssize_t pull_func(gnutls_transport_ptr_t ptr, void* buffer, size_t bufsize)
    {
      const GnuTlsStream::FdInfo& fdInfo = *static_cast<const GnuTlsStream::FdInfo*>(ptr);

      while (true)
      {
        log_debug("read fd=" << fdInfo.fd << "; size=" << bufsize);
        ssize_t n = ::read(fdInfo.fd, buffer, bufsize);
        log_debug("read returns " << n << " errno=" << errno);

        if (n > 0)
          return n;
        else if (n == 0 || errno == ECONNRESET)
          return 0;
        else if (errno == EAGAIN)
        {
          // poll
          struct pollfd fds;
          fds.fd = fdInfo.fd;
          fds.events = POLLIN;

          log_debug("poll; timeout " << fdInfo.timeout);
          int p = ::poll(&fds, 1, fdInfo.timeout);
          if (p == 0)
          {
            errno = EAGAIN;
            return -1;
          }
          else if (p < 0)
            return p;
        }
        else if (errno != EINTR)
          return -1;
      }

      return -1;
    }

    // write with timeout; assume non-blocking of underlying fd
    ssize_t push_func(gnutls_transport_ptr_t ptr, const void* buffer, size_t bufsize)
    {
      const GnuTlsStream::FdInfo& fdInfo = *static_cast<const GnuTlsStream::FdInfo*>(ptr);

      while (true)
      {
        log_debug("write fd=" << fdInfo.fd << "; size=" << bufsize);
        ssize_t n = ::write(fdInfo.fd, buffer, bufsize);
        log_debug("write returns " << n);

        if (n > 0)
          return n;
        else if (n == 0 || errno == ECONNRESET)
          return 0;
        else if (errno == EAGAIN)
        {
          // poll
          struct pollfd fds;
          fds.fd = fdInfo.fd;
          fds.events = POLLOUT;

          int p = ::poll(&fds, 1, fdInfo.timeout);
          if (p == 0)
          {
            errno = EAGAIN;
            return -1;
          }
          else if (p < 0)
            return p;
        }
        else if (errno != EINTR)
          return -1;
      }

      return -1;
    }

{'fraction_non_alphanumeric': 0.10931045440101685, 'fraction_numerical': 0.011121703209405783, 'mean_word_length': 1.9834123222748816, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2958064', 'n_tokens_mistral': 2052, 'n_tokens_neox': 1963, 'n_words': 645}